-
Notifications
You must be signed in to change notification settings - Fork 16
/
Program.cs
414 lines (360 loc) · 18.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using AutomaticTypeMapper;
using EOBot.Interpreter;
using EOLib;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Domain.NPC;
using EOLib.Domain.Spells;
using EOLib.IO.Repositories;
using Optional;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EOBot
{
static class Program
{
private static BotFramework f;
[AutoMappedType]
class NpcWalkNotifier : INPCActionNotifier
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly ICharacterProvider _characterProvider;
private readonly IENFFileProvider _enfFileProvider;
public NpcWalkNotifier(ICurrentMapStateRepository currentMapStateRepository,
ICharacterProvider characterProvider,
IENFFileProvider enfFileProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_characterProvider = characterProvider;
_enfFileProvider = enfFileProvider;
}
public void NPCTakeDamage(short npcIndex, int fromPlayerId, int damageToNpc, short npcPctHealth, Option<int> spellId)
{
if (fromPlayerId != _characterProvider.MainCharacter.ID)
return;
var npc = _currentMapStateRepository.NPCs.SingleOrDefault(x => x.Index == npcIndex);
var npcName = _enfFileProvider.ENFFile.SingleOrDefault(x => npc != null && npc.ID == x.ID)?.Name;
var color = npcPctHealth < 25
? ConsoleColor.Red
: npcPctHealth < 50
? ConsoleColor.Yellow
: ConsoleColor.Green;
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Hit, $"{damageToNpc,7} - {npcPctHealth,3}% HP - {npcIndex,2} - {npcName ?? string.Empty}", color);
}
public void RemoveNPCFromView(int npcIndex, int playerId, Option<short> spellId, Option<int> damage, bool showDeathAnimation)
{
}
public void ShowNPCSpeechBubble(int npcIndex, string message)
{
}
public void StartNPCAttackAnimation(int npcIndex)
{
}
public void StartNPCWalkAnimation(int npcIndex)
{
// immediately walk the NPC to the destination index
var npc = _currentMapStateRepository.NPCs.SingleOrDefault(x => x.Index == npcIndex);
if (npc == null) return;
var newNpc = npc.WithX((byte)npc.GetDestinationX()).WithY((byte)npc.GetDestinationY()).WithFrame(NPCFrame.Standing);
_currentMapStateRepository.NPCs.Remove(npc);
_currentMapStateRepository.NPCs.Add(newNpc);
}
public void NPCDropItem(MapItem item)
{
}
}
[AutoMappedType]
class CharacterTakeDamageNotifier : IMainCharacterEventNotifier
{
private readonly ICharacterProvider _characterProvider;
private readonly IExperienceTableProvider _experienceTableProvider;
private readonly ICharacterInventoryProvider _characterInventoryProvider;
public CharacterTakeDamageNotifier(ICharacterProvider characterProvider,
IExperienceTableProvider experienceTableProvider,
ICharacterInventoryProvider characterInventoryProvider)
{
_characterProvider = characterProvider;
_experienceTableProvider = experienceTableProvider;
_characterInventoryProvider = characterInventoryProvider;
}
public void NotifyGainedExp(int expDifference)
{
var nextLevelExp = _experienceTableProvider.ExperienceByLevel[_characterProvider.MainCharacter.Stats[CharacterStat.Level] + 1];
var tnl = nextLevelExp - _characterProvider.MainCharacter.Stats[CharacterStat.Experience] - expDifference;
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Experience, $"{expDifference,7} - {tnl} TNL", ConsoleColor.DarkCyan);
}
public void NotifyTakeDamage(int damageTaken, int playerPercentHealth, bool isHeal)
{
var type = isHeal ? ConsoleHelper.Type.Heal : ConsoleHelper.Type.Damage;
var color = isHeal ? ConsoleColor.DarkGreen
: playerPercentHealth < 25
? ConsoleColor.Red
: playerPercentHealth < 50
? ConsoleColor.Yellow
: ConsoleColor.Green;
ConsoleHelper.WriteMessage(type, $"{damageTaken,7} - {playerPercentHealth,3}% HP", color);
var hp = _characterProvider.MainCharacter.Stats[CharacterStat.HP];
if (!isHeal && hp - damageTaken <= 0)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Dead, "**** YOU DIED ****", ConsoleColor.DarkRed);
}
}
public void TakeItemFromMap(short id, int amountTaken)
{
var inventoryCount = _characterInventoryProvider.ItemInventory.SingleOrDefault(x => x.ItemID == id);
var weight = _characterProvider.MainCharacter.Stats[CharacterStat.Weight];
var maxWeight = _characterProvider.MainCharacter.Stats[CharacterStat.MaxWeight];
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Item, $"{weight,3}/{maxWeight,3} - weight - {inventoryCount.Amount} in inventory");
}
public void DropItem(short id, int amountDropped)
{
}
public void JunkItem(short id, int amountRemoved)
{
var inventoryCount = _characterInventoryProvider.ItemInventory.SingleOrDefault(x => x.ItemID == id);
var weight = _characterProvider.MainCharacter.Stats[CharacterStat.Weight];
var maxWeight = _characterProvider.MainCharacter.Stats[CharacterStat.MaxWeight];
ConsoleHelper.WriteMessage(ConsoleHelper.Type.JunkItem, $"{weight,3}/{maxWeight,3} - weight - {inventoryCount?.Amount ?? 0} in inventory");
}
public void MakeDrunk() { }
}
[AutoMappedType]
class CharacterAnimationNotifier : IOtherCharacterAnimationNotifier
{
private readonly ICharacterProvider _characterProvider;
public CharacterAnimationNotifier(ICharacterProvider characterProvider)
{
_characterProvider = characterProvider;
}
public void NotifySelfSpellCast(short playerId, short spellId, int spellHp, byte percentHealth)
{
if (playerId == _characterProvider.MainCharacter.ID && spellHp > 0)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Heal, $"{spellHp,7} - {percentHealth}% HP", ConsoleColor.DarkGreen);
}
public void NotifyStartSpellCast(short playerId, short spellId) { }
public void NotifyTargetNpcSpellCast(short playerId) { }
public void NotifyTargetOtherSpellCast(short sourcePlayerID, short targetPlayerID, short spellId, int recoveredHP, byte targetPercentHealth) { }
public void StartOtherCharacterAttackAnimation(int characterID, int noteIndex) { }
public void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction) { }
public void NotifyGroupSpellCast(short playerId, short spellId, short spellHp, List<GroupSpellTarget> spellTargets) { }
}
static async Task<int> Main(string[] args)
{
var assemblyNames = new[]
{
"EOBot",
"EOLib",
"EOLib.Config",
"EOLib.IO",
"EOLib.Localization",
"EOLib.Logger"
};
Console.CancelKeyPress += HandleCtrlC;
ArgumentsParser parsedArgs = new ArgumentsParser(args);
if (parsedArgs.Error != ArgsError.NoError || parsedArgs.ExtendedHelp)
{
ShowError(parsedArgs);
return 1;
}
DependencyMaster.TypeRegistry = new ITypeRegistry[parsedArgs.NumBots];
for (int i = 0; i < parsedArgs.NumBots; ++i)
{
DependencyMaster.TypeRegistry[i] = new UnityRegistry(assemblyNames);
DependencyMaster.TypeRegistry[i].RegisterDiscoveredTypes();
}
IBotFactory botFactory;
if (parsedArgs.ScriptFile != null)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, $"Executing script {parsedArgs.ScriptFile}...");
botFactory = new ScriptedBotFactory(parsedArgs);
}
else
{
botFactory = new TrainerBotFactory(parsedArgs);
}
ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, "Starting bots...");
try
{
using (f = new BotFramework(parsedArgs))
{
await f.InitializeAsync(botFactory, parsedArgs.InitDelay);
await f.RunAsync();
}
Console.WriteLine();
ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, "All bots completed.");
}
catch (BotException bex)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, bex.Message, ConsoleColor.DarkRed);
return 1;
}
catch (BotScriptErrorException bse)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, bse.Message, ConsoleColor.DarkRed);
return 1;
}
catch (AggregateException ae)
{
var botExceptions = ae.InnerExceptions.OfType<BotScriptErrorException>().ToList();
foreach (var ie in botExceptions)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, ie.Message, ConsoleColor.DarkRed);
var otherExceptions = ae.InnerExceptions.Except(botExceptions);
foreach (var ie in otherExceptions)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, $"Unhandled error: {ie.Message}\nStack Trace:\n{ie.StackTrace}", ConsoleColor.DarkRed);
return 1;
}
catch (Exception ex)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, $"Unhandled error: {ex.Message}\nStack Trace:\n{ex.StackTrace}", ConsoleColor.DarkRed);
return 1;
}
return 0;
}
static void HandleCtrlC(object sender, ConsoleCancelEventArgs e)
{
var name = Enum.GetName(e.SpecialKey.GetType(), e.SpecialKey);
ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, $"Exiting due to {name} event from system");
f?.TerminateBots();
}
static void ShowError(ArgumentsParser args)
{
switch (args.Error)
{
case ArgsError.WrongNumberOfArgs:
Console.WriteLine("Invalid: specify host, port, and the number of bots to run");
break;
case ArgsError.InvalidPort:
Console.WriteLine("Invalid: port number could not be parsed!");
break;
case ArgsError.InvalidNumberOfBots:
Console.WriteLine("Invalid: specify an integer argument for number of bots.");
break;
case ArgsError.TooManyBots:
Console.WriteLine("Invalid: unable to launch > 25 threads of execution. Please use 25 or less.");
break;
case ArgsError.NotEnoughBots:
Console.WriteLine("Invalid: unable to launch < 1 thread of execution. Please use 1 or more.");
break;
case ArgsError.BadFormat:
Console.WriteLine("Badly formatted argument. Enter args like \"argument=value\".");
break;
case ArgsError.InvalidSimultaneousNumberOfBots:
Console.WriteLine("Invalid: Simultaneous number of bots must not be more than total number of bots.");
break;
case ArgsError.InvalidWaitFlag:
Console.WriteLine("Invalid: Wait flag could not be parsed. Use 0, 1, false, true, no, or yes.");
break;
case ArgsError.InvalidInitDelay:
Console.WriteLine("Invalid: specify an integer argument for delay between inits (> 1100ms).");
break;
case ArgsError.InvalidPath:
Console.WriteLine("Invalid: Script file does not exist or is not a valid path.");
break;
case ArgsError.InvalidScriptArgs:
Console.WriteLine("Invalid: User-defined arguments and disabling autoconnect require a script.");
break;
case ArgsError.AutoConnectRequired:
Console.WriteLine("Invalid: AutoConnect is required when using a script with more than 1 bot due to eoserv connection throttling.");
break;
}
Console.WriteLine("\n\nUsage: (enter arguments in any order) (angle brackets is entry) (square brackets is optional)");
Console.WriteLine("EOBot.exe host=<host>\n" +
" port=<port>\n" +
" bots=<numBots>[,<simultaneousBots>]\n" +
" initDelay=<timeInMS>\n" +
" account=<account>\n" +
" password=<password>\n" +
" character=<character>\n" +
" script=<file>\n" +
" autoconnect=<true|false>" +
" [-- arg1, [arg2..argn]]");
Console.WriteLine("\t host: hostname or IP address");
Console.WriteLine("\t port: port to connect on (probably 8078)");
Console.WriteLine("\t bots: number of bots to execute. \n\t numBots is the total number, simultaneousBots is how many will run at once");
Console.WriteLine("\t initDelay: time in milliseconds to delay between doing the INIT handshake with the server");
Console.WriteLine("\t account: account to connect with (created if it does not exist)");
Console.WriteLine("\t password: password for account");
Console.WriteLine("\t character: character to use (created if it does not exist)");
Console.WriteLine("\t script: script file to execute\n\t if script is not specified, default trainer bot will be used");
Console.WriteLine("\t autoconnect: (default true) true to automatically connect/disconnect to server with initDelay timeout between connection attempts for bots, false otherwise");
Console.WriteLine("\t --: Any arguments passed after '--' will be available in a script under the '$args' array");
if (!args.ExtendedHelp)
return;
Console.WriteLine(@"
===============================================================
Bot Script Info
===============================================================
GENERAL
---------------------------------------------------------------
Semicolons are not part of the script language grammar
Do not end statements with semicolons
---------------------------------------------------------------
VARIBLES
---------------------------------------------------------------
variables are prefixed with a $
ex: $var_1 = 1
datatypes supported: bool, int, string, array
variables are dynamically typed
---------------------------------------------------------------
KEYWORDS
---------------------------------------------------------------
if ($expression) $statement
if ($expression) { $statement_list }
while ($expression) $statement
while ($expression) { $statement_list }
goto LABEL
LABEL:
Labels are a separate set of identifiers from variables
Labels do not use a $ prefix and do not need to be all caps
---------------------------------------------------------------
FUNCTIONS
---------------------------------------------------------------
Functions are called like normal programming languages
ex: print(""my message"")
Functions can be used in any expression, e.g. an array access
operation or a keyword evaluation (if/while)
Parameters to functions can be expressions, variables,
or literals
---------------------------------------------------------------
BUILT-IN IDENTIFIERS AND FUNCTIONS
---------------------------------------------------------------
print($message) - returns nothing; writes $message to console
len($array) - returns int; gets length of $array
array($size) - returns array; creates a fixed-size array
of $size with all contents set to 'Undefined'
sleep($time_ms) - returns nothing; sleeps for $time_ms
milliseconds
time() - returns string; gets the current system time in
HH:MM:SS format
Connect($host, $port) - connect to a server
Disconnect() - disconnect from a server
CreateAccount($user, $pass) - returns AccountReply
Login($user, $pass) - returns LoginReply
CreateAndLogin($user, $pass) - returns LoginReply
ChangePassword($user, $oldpass, $newpass) - returns AccountReply
CreateCharacter($name) - returns CharacterReply
DeleteCharacter($name, $force) - returns CharacterReply
LoginToCharacter($name)
NOTE: IT IS HIGHLY RECOMMENDED TO SLEEP IF USING MULTIPLE BOTS
AND CALLING ANY OF THE EO FUNCTIONS ABOVE, SINCE THEY
RUN ASYNCHRONOUSLY THEY ARE NOT GUARANTEED TO FINISH BY THE
TIME THE SCRIPT FINISHES EXECUTING
$host - string : host passed via command-line
$port - int : port passed via command-line
$user - string : account passed via command-line
$pass - string : account password passed via command-line
$botindex - int : index of this bot (for when numBots > 1)
$args - array : user-defined arguments passed after '--'
$version - int : allows setting client version
$result - dynamic: get/set result of last function call
$account - object : reserved
$character - object: reserved
$mapstate - object : reserved
");
}
}
}