forked from Da-Teach/Questor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombat.cs
571 lines (476 loc) · 23.7 KB
/
Combat.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
// ------------------------------------------------------------------------------
// <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;
/// <summary>
/// The combat class will target and kill any NPC that is targeting the questor.
/// It will also kill any NPC that is targeted but not agressing the questor.
/// </summary>
public class Combat
{
private readonly Dictionary<long, DateTime> _lastModuleActivation = new Dictionary<long, DateTime>();
private readonly Dictionary<long, DateTime> _lastWeaponReload = new Dictionary<long, DateTime>();
private bool _isJammed;
public CombatState State { get; set; }
private int MaxCharges { get; set; }
/// <summary>
/// Reload correct (tm) ammo for the NPC
/// </summary>
/// <param name = "weapon"></param>
/// <param name = "entity"></param>
/// <returns>True if the (enough/correct) ammo is loaded, false if wrong/not enough ammo is loaded</returns>
public bool ReloadNormalAmmo(ModuleCache weapon, EntityCache entity)
{
var cargo = Cache.Instance.DirectEve.GetShipsCargo();
// Get ammo based on damage type
var correctAmmo = Settings.Instance.Ammo.Where(a => a.DamageType == Cache.Instance.DamageType);
// Check if we still have that ammo in our cargo
correctAmmo = correctAmmo.Where(a => cargo.Items.Any(i => i.TypeId == a.TypeId && i.Quantity >= Settings.Instance.MinimumAmmoCharges));
// We are out of ammo! :(
if (correctAmmo.Count() == 0)
{
State = CombatState.OutOfAmmo;
return false;
}
// Get the best possible ammo
var ammo = correctAmmo.Where(a => a.Range > entity.Distance).OrderBy(a => a.Range).FirstOrDefault();
// We do not have any ammo left that can hit targets at that range!
if (ammo == null)
return false;
// We have enough ammo loaded
if (weapon.Charge != null && weapon.Charge.TypeId == ammo.TypeId && weapon.CurrentCharges >= Settings.Instance.MinimumAmmoCharges)
return true;
// Retry later, assume its ok now
if (weapon.MatchingAmmo.Count() == 0)
return true;
var charge = cargo.Items.FirstOrDefault(i => i.TypeId == ammo.TypeId && i.Quantity >= Settings.Instance.MinimumAmmoCharges);
// This should have shown up as "out of ammo"
if (charge == null)
return false;
// We are reloading, wait at least 11 seconds
if (_lastWeaponReload.ContainsKey(weapon.ItemId) && DateTime.Now < _lastWeaponReload[weapon.ItemId].AddSeconds(22))
return false;
_lastWeaponReload[weapon.ItemId] = DateTime.Now;
// Reload or change ammo
if (weapon.Charge != null && weapon.Charge.TypeId == charge.TypeId)
{
Logging.Log("Combat: Reloading [" + weapon.ItemId + "] with [" + charge.TypeName + "][" + charge.TypeId + "]");
weapon.ReloadAmmo(charge);
}
else
{
Logging.Log("Combat: Changing [" + weapon.ItemId + "] with [" + charge.TypeName + "][" + charge.TypeId + "]");
weapon.ChangeAmmo(charge);
}
// Return false as we are reloading ammo
return false;
}
public bool ReloadEnergyWeaponAmmo(ModuleCache weapon, EntityCache entity)
{
var cargo = Cache.Instance.DirectEve.GetShipsCargo();
// Get ammo based on damage type
var correctAmmo = Settings.Instance.Ammo.Where(a => a.DamageType == Cache.Instance.DamageType);
// Check if we still have that ammo in our cargo
correctAmmo = correctAmmo.Where(a => cargo.Items.Any(i => i.TypeId == a.TypeId));
// We are out of ammo! :(
if (correctAmmo.Count() == 0)
{
State = CombatState.OutOfAmmo;
return false;
}
// Get the best possible ammo
var ammo = correctAmmo.Where(a => a.Range > entity.Distance).OrderBy(a => a.Range).FirstOrDefault();
// We do not have any ammo left that can hit targets at that range!
if (ammo == null)
return false;
var charge = cargo.Items.OrderBy(i => i.Quantity).FirstOrDefault(i => i.TypeId == ammo.TypeId);
// We do not have any ammo left that can hit targets at that range!
if (charge == null)
return false;
// We have enough ammo loaded
if (weapon.Charge != null && weapon.Charge.TypeId == ammo.TypeId)
return true;
// We are reloading, wait at least 5 seconds
if (_lastWeaponReload.ContainsKey(weapon.ItemId) && DateTime.Now < _lastWeaponReload[weapon.ItemId].AddSeconds(5))
return false;
_lastWeaponReload[weapon.ItemId] = DateTime.Now;
// Reload or change ammo
if (weapon.Charge != null && weapon.Charge.TypeId == charge.TypeId)
{
Logging.Log("Combat: Reloading [" + weapon.ItemId + "] with [" + charge.TypeName + "][" + charge.TypeId + "]");
weapon.ReloadAmmo(charge);
}
else
{
Logging.Log("Combat: Changing [" + weapon.ItemId + "] with [" + charge.TypeName + "][" + charge.TypeId + "]");
weapon.ChangeAmmo(charge);
}
// Return false as we are reloading ammo
return false;
}
/// <summary>
/// Reload correct (tm) ammo for the NPC
/// </summary>
/// <param name = "weapon"></param>
/// <param name = "entity"></param>
/// <returns>True if the (enough/correct) ammo is loaded, false if wrong/not enough ammo is loaded</returns>
public bool ReloadAmmo(ModuleCache weapon, EntityCache entity)
{
// We need the cargo bay open for both reload actions
var cargo = Cache.Instance.DirectEve.GetShipsCargo();
if (cargo.Window == null)
{
Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.OpenCargoHoldOfActiveShip);
return false;
}
if (!cargo.IsReady)
return false;
return weapon.IsEnergyWeapon ? ReloadEnergyWeaponAmmo(weapon, entity) : ReloadNormalAmmo(weapon, entity);
}
/// <summary>
/// Returns true if it can activate the weapon on the target
/// </summary>
/// <remarks>
/// The idea behind this function is that a target that explodes isnt being fired on within 5 seconds
/// </remarks>
/// <param name = "module"></param>
/// <param name = "entity"></param>
/// <param name = "isWeapon"></param>
/// <returns></returns>
public bool CanActivate(ModuleCache module, EntityCache entity, bool isWeapon)
{
// We have changed target, allow activation
if (entity.Id != module.LastTargetId)
return true;
// We have reloaded, allow activation
if (isWeapon && module.CurrentCharges == MaxCharges)
return true;
// We havent reloaded, insert a wait-time
if (_lastModuleActivation.ContainsKey(module.ItemId))
{
if (DateTime.Now.Subtract(_lastModuleActivation[module.ItemId]).TotalSeconds < 3)
return false;
_lastModuleActivation.Remove(module.ItemId);
return true;
}
_lastModuleActivation.Add(module.ItemId, DateTime.Now);
return false;
}
/// <summary>
/// Returns the target we need to activate everything on
/// </summary>
/// <returns></returns>
private EntityCache GetTarget()
{
// Find the first active weapon's target
EntityCache weaponTarget = null;
foreach (var weapon in Cache.Instance.Weapons.Where(m => m.IsActive))
{
// Find the target associated with the weapon
weaponTarget = Cache.Instance.EntityById(weapon.TargetId);
if (weaponTarget != null)
break;
}
// Is our current target a warp scrambling priority target?
if (weaponTarget != null && Cache.Instance.PriorityTargets.Any(pt => pt.Id == weaponTarget.Id && pt.IsWarpScramblingMe))
return weaponTarget;
// Get the closest warp scrambling priority target
var target = Cache.Instance.PriorityTargets.FirstOrDefault(pt => pt.Distance < Cache.Instance.WeaponRange && pt.IsWarpScramblingMe && Cache.Instance.Targets.Any(t => t.Id == pt.Id));
if (target != null)
return target;
// Is our current target any other priority target?
if (weaponTarget != null && Cache.Instance.PriorityTargets.Any(pt => pt.Id == weaponTarget.Id))
return weaponTarget;
// Get the closest priority target
target = Cache.Instance.PriorityTargets.FirstOrDefault(pt => pt.Distance < Cache.Instance.WeaponRange && Cache.Instance.Targets.Any(t => t.Id == pt.Id));
if (target != null)
return target;
// Do we have a target?
if (weaponTarget != null)
return weaponTarget;
// Get all entity targets
var targets = Cache.Instance.Targets.Where(e => e.CategoryId == (int)CategoryID.Entity && e.IsNpc && !e.IsContainer && e.GroupId != (int)Group.LargeCollidableStructure);
// Get the closest high value target
target = targets.Where(t => t.TargetValue.HasValue && t.Distance < Cache.Instance.WeaponRange).OrderByDescending(t => t.TargetValue.Value).ThenBy(t => t.ShieldPct + t.ArmorPct + t.StructurePct).ThenBy(t => t.Distance).FirstOrDefault();
if (target != null)
return target;
// Get the closest low value target instead
target = targets.Where(t => !t.TargetValue.HasValue && t.Distance < Cache.Instance.WeaponRange).OrderBy(t => t.ShieldPct + t.ArmorPct + t.StructurePct).ThenBy(t => t.Distance).FirstOrDefault();
return target;
}
/// <summary>
/// Activate weapons
/// </summary>
private void ActivateWeapons(EntityCache target)
{
// Enable speed tank
if (Cache.Instance.Approaching == null && Settings.Instance.SpeedTank)
target.Orbit(Settings.Instance.OrbitDistance);
// Get the weapons
var weapons = Cache.Instance.Weapons;
// TODO: Add check to see if there is better ammo to use! :)
// Get distance of the target and compare that with the ammo currently loaded
foreach (var weapon in weapons)
{
if (!weapon.IsActive)
continue;
// No ammo loaded
if (weapon.Charge == null)
continue;
var ammo = Settings.Instance.Ammo.FirstOrDefault(a => a.TypeId == weapon.Charge.TypeId);
// How can this happen? Someone manually loaded ammo
if (ammo == null)
continue;
// Target is in range
if (target.Distance <= ammo.Range)
continue;
// Target is out of range, stop firing
weapon.Deactivate();
}
// Hax for max charges returning incorrect value
if (!weapons.Any(w => w.IsEnergyWeapon))
{
MaxCharges = Math.Max(MaxCharges, weapons.Max(l => l.MaxCharges));
MaxCharges = Math.Max(MaxCharges, weapons.Max(l => l.CurrentCharges));
}
// Activate the weapons (it not yet activated)))
foreach (var weapon in weapons)
{
// Are we on the right target?
if (weapon.IsActive)
{
if (weapon.TargetId != target.Id)
weapon.Deactivate();
continue;
}
// Are we reloading, deactivating or changing ammo?
if (weapon.IsReloadingAmmo || weapon.IsDeactivating || weapon.IsChangingAmmo)
continue;
// No, check ammo type and if thats correct, activate weapon
if (ReloadAmmo(weapon, target) && CanActivate(weapon, target, true))
{
Logging.Log("Combat: Activating weapon [" + weapon.ItemId + "] on [" + target.Name + "][" + target.Id + "]");
weapon.Activate(target.Id);
}
}
}
/// <summary>
/// Activate target painters
/// </summary>
public void ActivateTargetPainters(EntityCache target)
{
var targetPainters = Cache.Instance.Modules.Where(m => m.GroupId == (int) Group.TargetPainter).ToList();
// Find the first active weapon
// Assist this weapon
foreach (var painter in targetPainters)
{
// Are we on the right target?
if (painter.IsActive)
{
if (painter.TargetId != target.Id)
painter.Deactivate();
continue;
}
// Are we deactivating?
if (painter.IsDeactivating)
continue;
if (CanActivate(painter, target, false))
{
Logging.Log("Combat: Activating painter [" + painter.ItemId + "] on [" + target.Name + "][" + target.Id + "]");
painter.Activate(target.Id);
}
}
}
/// <summary>
/// Activate target painters
/// </summary>
public void ActivateStasisWeb(EntityCache target)
{
var webs = Cache.Instance.Modules.Where(m => m.GroupId == (int) Group.StasisWeb).ToList();
// Find the first active weapon
// Assist this weapon
foreach (var web in webs)
{
// Are we on the right target?
if (web.IsActive)
{
if (web.TargetId != target.Id)
web.Deactivate();
continue;
}
// Are we deactivating?
if (web.IsDeactivating)
continue;
// Target is out of web range
if (target.Distance >= web.OptimalRange)
continue;
if (CanActivate(web, target, false))
{
Logging.Log("Combat: Activating stasis web [" + web.ItemId + "] on [" + target.Name + "][" + target.Id + "]");
web.Activate(target.Id);
}
}
}
/// <summary>
/// Target combatants
/// </summary>
/// <remarks>
/// This only targets ships that are targeting you
/// </remarks>
private void TargetCombatants()
{
// We are jammed, forget targeting anything...
if (Cache.Instance.DirectEve.ActiveShip.MaxLockedTargets == 0)
{
if (!_isJammed)
Logging.Log("Combat: We are jammed and can't target anything");
_isJammed = true;
return;
}
if (_isJammed)
{
// Clear targeting list as it doesnt apply
Cache.Instance.TargetingIDs.Clear();
Logging.Log("Combat: We are no longer jammed, retargeting");
}
_isJammed = false;
// Whats the range that we can target at
var maxRange = Math.Min(Cache.Instance.DirectEve.ActiveShip.MaxTargetRange, Cache.Instance.WeaponRange);
// Get a list of combat targets (combine targets + targeting)
var targets = new List<EntityCache>();
targets.AddRange(Cache.Instance.Targets);
targets.AddRange(Cache.Instance.Targeting);
var combatTargets = targets.Where(e => e.CategoryId == (int)CategoryID.Entity && e.IsNpc && !e.IsContainer && e.GroupId != (int)Group.LargeCollidableStructure).ToList();
// Remove any target that is too far out of range (Weapon Range * 1.5)
for (var i = combatTargets.Count - 1; i >= 0; i --)
{
var target = combatTargets[i];
if (target.Distance > maxRange*1.5d)
{
Logging.Log("Combat: Target [" + target.Name + "][" + target.Id + "] out of range");
}
else if (Cache.Instance.IgnoreTargets.Contains(target.Name.Trim()))
{
Logging.Log("Combat: Target [" + target.Name + "][" + target.Id + "] on ignore list");
}
else continue;
target.UnlockTarget();
combatTargets.RemoveAt(i);
}
// Get a list of current high and low value targets
var highValueTargets = combatTargets.Where(t => t.TargetValue.HasValue || Cache.Instance.PriorityTargets.Any(pt => pt.Id == t.Id)).ToList();
var lowValueTargets = combatTargets.Where(t => !t.TargetValue.HasValue && !Cache.Instance.PriorityTargets.Any(pt => pt.Id == t.Id)).ToList();
// Build a list of things targeting me
var targetingMe = Cache.Instance.TargetedBy.Where(t => t.IsNpc && t.CategoryId == (int) CategoryID.Entity && !t.IsContainer && t.Distance < maxRange && !targets.Any(c => c.Id == t.Id) && !Cache.Instance.IgnoreTargets.Contains(t.Name.Trim())).ToList();
var highValueTargetingMe = targetingMe.Where(t => t.TargetValue.HasValue).OrderByDescending(t => t.TargetValue.Value).ThenBy(t => t.Distance).ToList();
var lowValueTargetingMe = targetingMe.Where(t => !t.TargetValue.HasValue).OrderBy(t => t.Distance).ToList();
// Get the number of maximum targets, if there are no low or high value targets left, use the combined total of targets
var maxHighValueTarget = (lowValueTargetingMe.Count + lowValueTargets.Count) == 0 ? Settings.Instance.MaximumLowValueTargets + Settings.Instance.MaximumHighValueTargets : Settings.Instance.MaximumHighValueTargets;
var maxLowValueTarget = (highValueTargetingMe.Count + highValueTargets.Count) == 0 ? Settings.Instance.MaximumLowValueTargets + Settings.Instance.MaximumHighValueTargets : Settings.Instance.MaximumLowValueTargets;
// Do we have too many high (none-priority) value targets targeted?
while (highValueTargets.Where(t => !Cache.Instance.PriorityTargets.Any(pt => pt.Id == t.Id)).Count() > Math.Max(maxHighValueTarget - Cache.Instance.PriorityTargets.Count(), 0))
{
// Unlock any target
var target = highValueTargets.OrderByDescending(t => t.Distance).Where(t => !Cache.Instance.PriorityTargets.Any(pt => pt.Id == t.Id)).FirstOrDefault();
if (target == null)
break;
target.UnlockTarget();
highValueTargets.Remove(target);
}
// Do we have too many low value targets targeted?
while (lowValueTargets.Count > maxLowValueTarget)
{
// Unlock any target
var target = lowValueTargets.OrderByDescending(t => t.Distance).First();
target.UnlockTarget();
lowValueTargets.Remove(target);
}
// Do we have enough targeted?
if ((highValueTargets.Count >= maxHighValueTarget && lowValueTargets.Count >= maxLowValueTarget) ||
((highValueTargets.Count + lowValueTargets.Count) >= (maxHighValueTarget + maxLowValueTarget)))
return;
// Do we have any priority targets?
var priority = Cache.Instance.PriorityTargets.Where(t => t.Distance < maxRange && !targets.Any(c => c.Id == t.Id) && !Cache.Instance.IgnoreTargets.Contains(t.Name.Trim()));
foreach (var entity in priority)
{
// Have we reached the limit of high value targets?
if (highValueTargets.Count >= maxHighValueTarget)
break;
Logging.Log("Combat: Targeting priority target [" + entity.Name + "][" + entity.Id + "]{" + highValueTargets.Count + "}");
entity.LockTarget();
highValueTargets.Add(entity);
}
foreach (var entity in highValueTargetingMe)
{
// Have we reached the limit of high value targets?
if (highValueTargets.Count >= maxHighValueTarget)
break;
Logging.Log("Combat: Targeting high value target [" + entity.Name + "][" + entity.Id + "]{" + highValueTargets.Count + "}");
entity.LockTarget();
highValueTargets.Add(entity);
}
foreach (var entity in lowValueTargetingMe)
{
// Have we reached the limit of low value targets?
if (lowValueTargets.Count >= maxLowValueTarget)
break;
Logging.Log("Combat: Targeting low value target [" + entity.Name + "][" + entity.Id + "]{" + lowValueTargets.Count + "}");
entity.LockTarget();
lowValueTargets.Add(entity);
}
}
public void ProcessState()
{
// There is really no combat in stations (yet)
if (Cache.Instance.InStation)
return;
// What? No ship entity?
if (Cache.Instance.DirectEve.ActiveShip.Entity == null)
return;
// There is no combat when cloaked
if (Cache.Instance.DirectEve.ActiveShip.Entity.IsCloaked)
return;
if (!Cache.Instance.Weapons.Any())
{
Logging.Log("Combat: No weapons with GroupId [" + Settings.Instance.WeaponGroupId + "] found!");
State = CombatState.OutOfAmmo;
}
switch (State)
{
case CombatState.CheckTargets:
// Next state
State = CombatState.KillTargets;
TargetCombatants();
break;
case CombatState.KillTargets:
// Next state
State = CombatState.CheckTargets;
var target = GetTarget();
if (target != null)
{
ActivateWeapons(target);
ActivateTargetPainters(target);
ActivateStasisWeb(target);
}
break;
case CombatState.OutOfAmmo:
break;
default:
// Next state
State = CombatState.CheckTargets;
break;
}
}
}
}