forked from Da-Teach/Questor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissionController.cs
638 lines (529 loc) · 26 KB
/
MissionController.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// ------------------------------------------------------------------------------
// <copyright from='2010' to='2015' company='THEHACKERWITHIN.COM'>
// Copyright (c) TheHackerWithin.COM. All Rights Reserved.
//
// Please look in the accompanying license.htm file for the license that
// applies to this source code. (a copy can also be found at:
// http://www.thehackerwithin.com/license.htm)
// </copyright>
// -------------------------------------------------------------------------------
namespace Questor.Modules
{
using System;
using System.Collections.Generic;
using System.Linq;
using DirectEve;
public class MissionController
{
private DateTime? _clearPocketTimeout;
private int _currentAction;
private DateTime _lastActivateAction;
private double _lastX;
private double _lastY;
private double _lastZ;
private int _pocket;
private List<Action> _pocketActions;
private bool _waiting;
private DateTime _waitingSince;
public long AgentId { get; set; }
public MissionController()
{
_pocketActions = new List<Action>();
}
public MissionControllerState State { get; set; }
private void BookmarkPocketForSalvaging()
{
// We arent suppose to create bookmarks
if (!Settings.Instance.CreateSalvageBookmarks)
return;
// Nothing to loot
if (Cache.Instance.UnlootedContainers.Count() < Settings.Instance.MinimumWreckCount)
{
Logging.Log("MissionController: Not created bookmarked because the pocket has [" + Cache.Instance.UnlootedContainers.Count() + "] wrecks and the minimum is [" + Settings.Instance.MinimumWreckCount + "]");
return;
}
// Do we already have a bookmark?
var bookmarks = Cache.Instance.BookmarksByLabel(Settings.Instance.BookmarkPrefix + " ");
var bookmark = bookmarks.FirstOrDefault(b => Cache.Instance.DistanceFromMe(b.X ?? 0, b.Y ?? 0, b.Z ?? 0) < 250000);
if (bookmark != null)
{
Logging.Log("MissionController: Pocket already bookmarked for salvaging [" + bookmark.Title + "]");
return;
}
// No, create a bookmark
var label = string.Format("{0} {1:HHmm}", Settings.Instance.BookmarkPrefix, DateTime.UtcNow);
Logging.Log("MissionController: Bookmarking pocket for salvaging [" + label + "]");
Cache.Instance.CreateBookmark(label);
}
private void ActivateAction(Action action)
{
var target = action.GetParameterValue("target");
// No parameter? Although we shouldnt really allow it, assume its the acceleration gate :)
if (string.IsNullOrEmpty(target))
target = "Acceleration Gate";
var targets = Cache.Instance.EntitiesByName(target);
if (targets == null || targets.Count() == 0)
{
Logging.Log("MissionController.Activate: Can't find [" + target + "] to activate! Stopping Questor!");
State = MissionControllerState.Error;
return;
}
var closest = targets.OrderBy(t => t.Distance).First();
if (closest.Distance < 2500)
{
// Tell the drones module to retract drones
Cache.Instance.IsMissionPocketDone = true;
// We cant activate if we have drones out
if (Cache.Instance.ActiveDrones.Count() > 0)
return;
// Add bookmark (before we activate)
BookmarkPocketForSalvaging();
// Activate it and move to the next Pocket
closest.Activate();
// Do not change actions, if NextPocket gets a timeout (>2 mins) then it reverts to the last action
Logging.Log("MissionController.Activate: Activate [" + closest.Name + "] and change state to 'NextPocket'");
State = MissionControllerState.NextPocket;
_lastActivateAction = DateTime.Now;
}
else if (closest.Distance < 150000)
{
// Move to the target
if (Cache.Instance.Approaching == null || Cache.Instance.Approaching.Id != closest.Id)
{
Logging.Log("MissionController.Activate: Approaching target [" + closest.Name + "][" + closest.Id + "]");
closest.Approach();
}
}
else
{
// We cant warp if we have drones out
if (Cache.Instance.ActiveDrones.Count() > 0)
return;
// Probably never happens
closest.WarpTo();
}
}
private void ClearPocketAction(Action action)
{
var activeTargets = new List<EntityCache>();
activeTargets.AddRange(Cache.Instance.Targets);
activeTargets.AddRange(Cache.Instance.Targeting);
// Get lowest range
var range = Math.Min(Cache.Instance.WeaponRange, Cache.Instance.DirectEve.ActiveShip.MaxTargetRange);
// We are obviously still killing stuff that's in range
if (activeTargets.Count(t => t.Distance < range && t.IsNpc && t.CategoryId == (int) CategoryID.Entity) > 0)
{
// Reset timeout
_clearPocketTimeout = null;
// If we are still moving, stop (we do not want to 'over-agro', if possible) (unless we are speed tanking)
if (Cache.Instance.Approaching != null && !Settings.Instance.SpeedTank)
{
Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.CmdStopShip);
Cache.Instance.Approaching = null;
}
return;
}
// Is there a priority target out of range?
var target = Cache.Instance.PriorityTargets.OrderBy(t => t.Distance).Where(t => !Cache.Instance.IgnoreTargets.Contains(t.Name.Trim())).FirstOrDefault();
// Or is there a target out of range that is targeting us?
target = target ?? Cache.Instance.TargetedBy.Where(t => !t.IsSentry && !t.IsContainer && t.IsNpc && t.CategoryId == (int)CategoryID.Entity && t.GroupId != (int)Group.LargeCollidableStructure && !Cache.Instance.IgnoreTargets.Contains(t.Name.Trim())).OrderBy(t => t.Distance).FirstOrDefault();
// Or is there any target out of range?
target = target ?? Cache.Instance.Entities.Where(t => !t.IsSentry && !t.IsContainer && t.IsNpc && t.CategoryId == (int) CategoryID.Entity && t.GroupId != (int) Group.LargeCollidableStructure && !Cache.Instance.IgnoreTargets.Contains(t.Name.Trim())).OrderBy(t => t.Distance).FirstOrDefault();
if (target != null)
{
// Reset timeout
_clearPocketTimeout = null;
// Lock priority target if within weapons range
if (target.Distance < range)
{
if (Cache.Instance.DirectEve.ActiveShip.MaxLockedTargets > 0)
{
Logging.Log("MissionController.ClearPocket: Targeting [" + target.Name + "][" + target.Id + "]");
target.LockTarget();
}
return;
}
// Are we approaching the active (out of range) target?
// Wait for it (or others) to get into range
if (Cache.Instance.Approaching == null || Cache.Instance.Approaching.Id != target.Id)
{
Logging.Log("MissionController.ClearPocket: Approaching target [" + target.Name + "][" + target.Id + "]");
if (Settings.Instance.SpeedTank)
target.Orbit(Settings.Instance.OrbitDistance);
else
target.Approach((int) (Cache.Instance.WeaponRange*0.8d));
}
return;
}
// Do we have a timeout? No, set it to now + 5 seconds
if (!_clearPocketTimeout.HasValue)
_clearPocketTimeout = DateTime.Now.AddSeconds(5);
// Are we in timeout?
if (DateTime.Now < _clearPocketTimeout.Value)
return;
// We have cleared the Pocket, perform the next action \o/
_currentAction++;
// Reset timeout
_clearPocketTimeout = null;
}
private void MoveToAction(Action action)
{
var target = action.GetParameterValue("target");
// No parameter? Although we shouldnt really allow it, assume its the acceleration gate :)
if (string.IsNullOrEmpty(target))
target = "Acceleration Gate";
var targets = Cache.Instance.EntitiesByName(target);
if (targets == null || targets.Count() == 0)
{
// Unlike activate, no target just means next action
_currentAction++;
return;
}
var closest = targets.OrderBy(t => t.Distance).First();
if (closest.Distance < 2500)
{
// We are close enough to whatever we needed to move to
_currentAction++;
if (Cache.Instance.Approaching != null)
{
Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.CmdStopShip);
Cache.Instance.Approaching = null;
}
}
else
{
// Move to the target
if (Cache.Instance.Approaching == null || Cache.Instance.Approaching.Id != closest.Id)
{
Logging.Log("MissionController.MoveTo: Approaching target [" + closest.Name + "][" + closest.Id + "]");
closest.Approach();
}
}
}
private void WaitUntilTargeted(Action action)
{
var targetedBy = Cache.Instance.TargetedBy;
if (targetedBy != null && targetedBy.Count() > 0)
{
Logging.Log("MissionController.WaitUntilTargeted: We have been targeted!");
// We have been locked, go go go ;)
_waiting = false;
_currentAction++;
return;
}
// Default timeout is 30 seconds
int timeout;
if (!int.TryParse(action.GetParameterValue("timeout"), out timeout))
timeout = 30; // Probably don't need to do this
if (_waiting)
{
if (DateTime.Now.Subtract(_waitingSince).TotalSeconds < timeout)
return;
Logging.Log("MissionController.WaitUntilTargeted: Nothing targeted us within the timeout!");
// Nothing has targeted us in the specified timeout
_waiting = false;
_currentAction++;
return;
}
// Start waiting
_waiting = true;
_waitingSince = DateTime.Now;
}
private void KillAction(Action action)
{
bool ignoreAttackers;
if (!bool.TryParse(action.GetParameterValue("ignoreattackers"), out ignoreAttackers))
ignoreAttackers = false;
bool breakOnAttackers;
if (!bool.TryParse(action.GetParameterValue("breakonattackers"), out breakOnAttackers))
breakOnAttackers = false;
var targetNames = action.GetParameterValues("target");
// No parameter? Ignore kill action
if (targetNames.Count == 0)
{
Logging.Log("MissionController.Kill: No targets defined!");
_currentAction++;
return;
}
var targets = Cache.Instance.Entities.Where(e => targetNames.Contains(e.Name));
if (targets.Count() == 0)
{
Logging.Log("MissionController.Kill: All targets killed " + targetNames.Aggregate((current, next) => current + "[" + next + "]"));
// We killed it/them !?!?!? :)
_currentAction++;
return;
}
if (breakOnAttackers && Cache.Instance.TargetedBy.Any(t => !t.IsSentry && t.Distance < Cache.Instance.WeaponRange))
{
// We are being attacked, break the kill order
if (Cache.Instance.RemovePriorityTargets(targets))
Logging.Log("MissionController.Kill: Breaking off kill order, new spawn has arived!");
foreach (var target in Cache.Instance.Targets.Where(e => targets.Any(t => t.Id == e.Id)))
{
Logging.Log("MissionController.Kill: Unlocking [" + target.Name + "][" + target.Id + "] due to kill order being put on hold");
target.UnlockTarget();
}
return;
}
if (!ignoreAttackers || breakOnAttackers)
{
// Apparently we are busy, wait for combat to clear attackers first
var targetedBy = Cache.Instance.TargetedBy;
if (targetedBy != null && targetedBy.Count(t => !t.IsSentry && t.Distance < Cache.Instance.WeaponRange) > 0)
return;
}
var closest = targets.OrderBy(t => t.Distance).First();
if (closest.Distance < Cache.Instance.WeaponRange)
{
if (!Cache.Instance.PriorityTargets.Any(pt => pt.Id == closest.Id))
{
Logging.Log("MissionController.Kill: Adding [" + closest.Name + "][" + closest.Id + "] as a priority target");
Cache.Instance.AddPriorityTargets(new[] {closest}, Priority.PriorityKillTarget);
}
if (Cache.Instance.Approaching != null && !Settings.Instance.SpeedTank)
{
Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.CmdStopShip);
Cache.Instance.Approaching = null;
}
}
else
{
// Move within 80% max distance
if (Cache.Instance.Approaching == null || Cache.Instance.Approaching.Id != closest.Id)
{
Logging.Log("MissionController.Kill: Approaching target [" + closest.Name + "][" + closest.Id + "]");
if (Settings.Instance.SpeedTank)
closest.Orbit(Settings.Instance.OrbitDistance);
else
closest.Approach((int) (Cache.Instance.WeaponRange*0.8d));
}
}
}
private void LootItemAction(Action action)
{
var items = action.GetParameterValues("item");
var targetNames = action.GetParameterValues("target");
var done = items.Count == 0;
if (!done)
{
var cargo = Cache.Instance.DirectEve.GetShipsCargo();
// We assume that the ship's cargo will be opened somewhere else
if (cargo.IsReady)
done |= cargo.Items.Any(i => items.Contains(i.TypeName));
}
if (done)
{
Logging.Log("MissionController.LootItem: We are done looting");
_currentAction++;
return;
}
var containers = Cache.Instance.Containers.Where(e => !Cache.Instance.LootedContainers.Contains(e.Id)).OrderBy(e => e.Distance);
if (containers.Count() == 0)
{
Logging.Log("MissionController.LootItem: We are done looting");
_currentAction++;
return;
}
var closest = containers.FirstOrDefault(c => targetNames.Contains(c.Name)) ?? containers.First();
if (closest.Distance > 2500 && (Cache.Instance.Approaching == null || Cache.Instance.Approaching.Id != closest.Id))
{
Logging.Log("MissionController.LootItem: Approaching target [" + closest.Name + "][" + closest.Id + "]");
closest.Approach();
}
}
private void LootAction(Action action)
{
var items = action.GetParameterValues("item");
var targetNames = action.GetParameterValues("target");
if (!Settings.Instance.LootEverything)
{
var done = items.Count == 0;
if (!done)
{
var cargo = Cache.Instance.DirectEve.GetShipsCargo();
// We assume that the ship's cargo will be opened somewhere else
if (cargo.IsReady)
done |= cargo.Items.Any(i => items.Contains(i.TypeName));
}
if (done)
{
Logging.Log("MissionController.Loot: We are done looting");
_currentAction++;
return;
}
}
var containers = Cache.Instance.Containers.Where(e => !Cache.Instance.LootedContainers.Contains(e.Id)).OrderBy(e => e.Distance);
if (containers.Count() == 0)
{
Logging.Log("MissionController.Loot: We are done looting");
_currentAction++;
return;
}
var closest = containers.FirstOrDefault(c => targetNames.Contains(c.Name)) ?? containers.First();
if (closest.Distance > 2500 && (Cache.Instance.Approaching == null || Cache.Instance.Approaching.Id != closest.Id))
{
Logging.Log("MissionController.Loot: Approaching target [" + closest.Name + "][" + closest.Id + "]");
closest.Approach();
}
}
private void IgnoreAction(Action action)
{
var add = action.GetParameterValues("add");
var remove = action.GetParameterValues("remove");
add.ForEach(a => Cache.Instance.IgnoreTargets.Add(a.Trim()));
remove.ForEach(a => Cache.Instance.IgnoreTargets.Remove(a.Trim()));
Logging.Log("MissionController.Ignore: Updated ignore list");
if (Cache.Instance.IgnoreTargets.Any())
Logging.Log("MissionController.Ignore: Currently ignoring: " + Cache.Instance.IgnoreTargets.Aggregate((current, next) => current + "[" + next + "]"));
else
Logging.Log("MissionController.Ignore: Your ignore list is empty");
_currentAction++;
}
private void PerformAction(Action action)
{
switch (action.State)
{
case ActionState.Activate:
ActivateAction(action);
break;
case ActionState.ClearPocket:
ClearPocketAction(action);
break;
case ActionState.Done:
// Tell the drones module to retract drones
Cache.Instance.IsMissionPocketDone = true;
// We do not switch to "done" status if we still have drones out
if (Cache.Instance.ActiveDrones.Count() > 0)
return;
BookmarkPocketForSalvaging();
State = MissionControllerState.Done;
break;
case ActionState.Kill:
KillAction(action);
break;
case ActionState.MoveTo:
MoveToAction(action);
break;
case ActionState.Loot:
LootAction(action);
break;
case ActionState.LootItem:
LootItemAction(action);
break;
case ActionState.Ignore:
IgnoreAction(action);
break;
case ActionState.WaitUntilTargeted:
WaitUntilTargeted(action);
break;
}
}
public void ProcessState()
{
// What? No ship entity?
if (Cache.Instance.DirectEve.ActiveShip.Entity == null)
return;
switch (State)
{
case MissionControllerState.Idle:
case MissionControllerState.Done:
case MissionControllerState.Error:
break;
case MissionControllerState.Start:
_pocket = 0;
// Reload the items needed for this mission from the XML file
Cache.Instance.RefreshMissionItems(AgentId);
// Update x/y/z so that NextPocket wont think we are there yet because its checking (very) old x/y/z cords
_lastX = Cache.Instance.DirectEve.ActiveShip.Entity.X;
_lastY = Cache.Instance.DirectEve.ActiveShip.Entity.Y;
_lastZ = Cache.Instance.DirectEve.ActiveShip.Entity.Z;
State = MissionControllerState.LoadPocket;
break;
case MissionControllerState.LoadPocket:
_pocketActions.Clear();
_pocketActions.AddRange(Cache.Instance.LoadMissionActions(AgentId, _pocket));
if (_pocketActions.Count == 0)
{
// No Pocket action, load default actions
Logging.Log("MissionController: No mission actions specified, loading default actions");
// Wait for 30 seconds to be targeted
_pocketActions.Add(new Action {State = ActionState.WaitUntilTargeted});
_pocketActions[0].AddParameter("timeout", "15");
// Clear the Pocket
_pocketActions.Add(new Action {State = ActionState.ClearPocket});
// Is there a gate?
var gates = Cache.Instance.EntitiesByName("Acceleration Gate");
if (gates != null && gates.Count() > 0)
{
// Activate it (Activate action also moves to the gate)
_pocketActions.Add(new Action {State = ActionState.Activate});
_pocketActions[_pocketActions.Count - 1].AddParameter("target", "Acceleration Gate");
}
else // No, were done
_pocketActions.Add(new Action {State = ActionState.Done});
// TODO: Check mission HTML to see if we need to pickup any items
// Not a priority, apparently retrieving HTML causes a lot of crashes
}
Logging.Log("MissionController: Pocket loaded, executing the following actions");
foreach (var a in _pocketActions)
Logging.Log("MissionController: Action." + a);
// Reset pocket information
_currentAction = 0;
Cache.Instance.IsMissionPocketDone = false;
Cache.Instance.IgnoreTargets.Clear();
State = MissionControllerState.ExecutePocketActions;
break;
case MissionControllerState.ExecutePocketActions:
if (_currentAction >= _pocketActions.Count)
{
// No more actions, but we're not done?!?!?!
Logging.Log("MissionController: We're out of actions but did not process a 'Done' or 'Activate' action");
State = MissionControllerState.Error;
break;
}
var action = _pocketActions[_currentAction];
var currentAction = _currentAction;
PerformAction(action);
if (currentAction != _currentAction)
{
Logging.Log("MissionController: Finished Action." + action);
if (_currentAction < _pocketActions.Count)
{
action = _pocketActions[_currentAction];
Logging.Log("MissionController: Starting Action." + action);
}
}
if (Settings.Instance.DebugStates)
Logging.Log("Action.State = " + action);
break;
case MissionControllerState.NextPocket:
var distance = Cache.Instance.DistanceFromMe(_lastX, _lastY, _lastZ);
if (distance > 100000)
{
Logging.Log("MissionController: We've moved to the next Pocket [" + distance + "]");
// If we moved more then 100km, assume next Pocket
_pocket++;
State = MissionControllerState.LoadPocket;
}
else if (DateTime.Now.Subtract(_lastActivateAction).TotalMinutes > 2)
{
Logging.Log("MissionController: We've timed out, retry last action");
// We have reached a timeout, revert to ExecutePocketActions (e.g. most likely Activate)
State = MissionControllerState.ExecutePocketActions;
}
break;
}
var newX = Cache.Instance.DirectEve.ActiveShip.Entity.X;
var newY = Cache.Instance.DirectEve.ActiveShip.Entity.Y;
var newZ = Cache.Instance.DirectEve.ActiveShip.Entity.Z;
// For some reason x/y/z returned 0 sometimes
if (newX != 0 && newY != 0 && newZ != 0)
{
// Save X/Y/Z so that NextPocket can check if we actually went to the next Pocket :)
_lastX = newX;
_lastY = newY;
_lastZ = newZ;
}
}
}
}