forked from LeagueSharp/LeagueSharp.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthPrediction.cs
360 lines (315 loc) · 13.2 KB
/
HealthPrediction.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
namespace LeagueSharp.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// This class allows you to calculate the health of units after a set time. Only works on minions and only taking into
/// account the auto-attack damage.
/// </summary>
public class HealthPrediction
{
#region Static Fields
/// <summary>
/// The active attacks
/// </summary>
private static readonly Dictionary<int, PredictedDamage> ActiveAttacks = new Dictionary<int, PredictedDamage>();
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes static members of the <see cref="HealthPrediction" /> class.
/// </summary>
static HealthPrediction()
{
Obj_AI_Base.OnProcessSpellCast += ObjAiBaseOnOnProcessSpellCast;
Game.OnUpdate += Game_OnGameUpdate;
Spellbook.OnStopCast += SpellbookOnStopCast;
GameObject.OnDelete += MissileClient_OnDelete;
Obj_AI_Base.OnDoCast += Obj_AI_Base_OnDoCast;
}
#endregion
#region Public Methods and Operators
/// <summary>
/// Return the Attacking turret.
/// </summary>
/// <param name="minion"></param>
/// <returns></returns>
public static Obj_AI_Base GetAggroTurret(Obj_AI_Minion minion)
{
var ActiveTurret =
ActiveAttacks.Values.FirstOrDefault(
m => (m.Source is Obj_AI_Turret) && m.Target.NetworkId == minion.NetworkId);
return ActiveTurret != null ? ActiveTurret.Source : null;
}
/// <summary>
/// Returns the unit health after a set time milliseconds.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="time">The time.</param>
/// <param name="delay">The delay.</param>
/// <returns></returns>
public static float GetHealthPrediction(Obj_AI_Base unit, int time, int delay = 70)
{
var predictedDamage = 0f;
foreach (var attack in ActiveAttacks.Values)
{
var attackDamage = 0f;
if (!attack.Processed && attack.Source.IsValidTarget(float.MaxValue, false)
&& attack.Target.IsValidTarget(float.MaxValue, false) && attack.Target.NetworkId == unit.NetworkId)
{
var landTime = attack.StartTick + attack.Delay
+ 1000 * Math.Max(0, unit.Distance(attack.Source) - attack.Source.BoundingRadius)
/ attack.ProjectileSpeed + delay;
if ( /*Utils.GameTimeTickCount < landTime - delay &&*/ landTime < Utils.GameTimeTickCount + time)
{
attackDamage = attack.Damage;
}
}
predictedDamage += attackDamage;
}
return unit.Health - predictedDamage;
}
/// <summary>
/// Determines whether the specified minion has minion aggro.
/// </summary>
/// <param name="minion">The minion.</param>
/// <returns></returns>
public static bool HasMinionAggro(Obj_AI_Minion minion)
{
return ActiveAttacks.Values.Any(m => (m.Source is Obj_AI_Minion) && m.Target.NetworkId == minion.NetworkId);
}
/// <summary>
/// Determines whether the specified minion has turret aggro.
/// </summary>
/// <param name="minion">The minion</param>
/// <returns></returns>
public static bool HasTurretAggro(Obj_AI_Minion minion)
{
return ActiveAttacks.Values.Any(m => (m.Source is Obj_AI_Turret) && m.Target.NetworkId == minion.NetworkId);
}
/// <summary>
/// Returns the unit health after time milliseconds assuming that the past auto-attacks are periodic.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="time">The time.</param>
/// <param name="delay">The delay.</param>
/// <returns></returns>
public static float LaneClearHealthPrediction(Obj_AI_Base unit, int time, int delay = 70)
{
var predictedDamage = 0f;
foreach (var attack in ActiveAttacks.Values)
{
var n = 0;
if (Utils.GameTimeTickCount - 100 <= attack.StartTick + attack.AnimationTime
&& attack.Target.IsValidTarget(float.MaxValue, false)
&& attack.Source.IsValidTarget(float.MaxValue, false) && attack.Target.NetworkId == unit.NetworkId)
{
var fromT = attack.StartTick;
var toT = Utils.GameTimeTickCount + time;
while (fromT < toT)
{
if (fromT >= Utils.GameTimeTickCount
&& (fromT + attack.Delay
+ Math.Max(0, unit.Distance(attack.Source) - attack.Source.BoundingRadius)
/ attack.ProjectileSpeed < toT))
{
n++;
}
fromT += (int)attack.AnimationTime;
}
}
predictedDamage += n * attack.Damage;
}
return unit.Health - predictedDamage;
}
/// <summary>
/// Return the starttick of the attacking turret.
/// </summary>
/// <param name="minion"></param>
/// <returns></returns>
public static int TurretAggroStartTick(Obj_AI_Minion minion)
{
var ActiveTurret =
ActiveAttacks.Values.FirstOrDefault(
m => (m.Source is Obj_AI_Turret) && m.Target.NetworkId == minion.NetworkId);
return ActiveTurret != null ? ActiveTurret.StartTick : 0;
}
#endregion
#region Methods
/// <summary>
/// Fired when the game is updated.
/// </summary>
/// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
private static void Game_OnGameUpdate(EventArgs args)
{
ActiveAttacks.ToList()
.Where(pair => pair.Value.StartTick < Utils.GameTimeTickCount - 3000)
.ToList()
.ForEach(pair => ActiveAttacks.Remove(pair.Key));
}
/// <summary>
/// Fired when a <see cref="MissileClient" /> is deleted from the game.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
static void MissileClient_OnDelete(GameObject sender, EventArgs args)
{
var missile = sender as MissileClient;
if (missile != null && missile.SpellCaster != null)
{
var casterNetworkId = missile.SpellCaster.NetworkId;
foreach (var activeAttack in ActiveAttacks)
{
if (activeAttack.Key == casterNetworkId)
{
ActiveAttacks[casterNetworkId].Processed = true;
}
}
}
}
/// <summary>
/// Fired when a unit does an auto attack.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="GameObjectProcessSpellCastEventArgs" /> instance containing the event data.</param>
private static void Obj_AI_Base_OnDoCast(Obj_AI_Base sender, GameObjectProcessSpellCastEventArgs args)
{
if (ActiveAttacks.ContainsKey(sender.NetworkId) && sender.IsMelee)
{
ActiveAttacks[sender.NetworkId].Processed = true;
}
}
/// <summary>
/// Fired when the game processes a spell cast.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="GameObjectProcessSpellCastEventArgs" /> instance containing the event data.</param>
private static void ObjAiBaseOnOnProcessSpellCast(Obj_AI_Base sender, GameObjectProcessSpellCastEventArgs args)
{
if (!sender.IsValidTarget(3000, false) || sender.Team != ObjectManager.Player.Team || sender is Obj_AI_Hero
|| !Orbwalking.IsAutoAttack(args.SData.Name) || !(args.Target is Obj_AI_Base))
{
return;
}
var target = (Obj_AI_Base)args.Target;
ActiveAttacks.Remove(sender.NetworkId);
var attackData = new PredictedDamage(
sender,
target,
Utils.GameTimeTickCount - Game.Ping / 2,
sender.AttackCastDelay * 1000,
sender.AttackDelay * 1000 - (sender is Obj_AI_Turret ? 70 : 0),
sender.IsMelee() ? int.MaxValue : (int)args.SData.MissileSpeed,
(float)sender.GetAutoAttackDamage(target, true));
ActiveAttacks.Add(sender.NetworkId, attackData);
}
/// <summary>
/// Fired when the spellbooks stops a cast.
/// </summary>
/// <param name="spellbook">The spellbook.</param>
/// <param name="args">The <see cref="SpellbookStopCastEventArgs" /> instance containing the event data.</param>
private static void SpellbookOnStopCast(Spellbook spellbook, SpellbookStopCastEventArgs args)
{
if (spellbook.Owner.IsValid && args.StopAnimation)
{
if (ActiveAttacks.ContainsKey(spellbook.Owner.NetworkId))
{
ActiveAttacks.Remove(spellbook.Owner.NetworkId);
}
}
}
#endregion
/// <summary>
/// Represetns predicted damage.
/// </summary>
private class PredictedDamage
{
#region Fields
/// <summary>
/// The animation time
/// </summary>
public readonly float AnimationTime;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="PredictedDamage" /> class.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="startTick">The start tick.</param>
/// <param name="delay">The delay.</param>
/// <param name="animationTime">The animation time.</param>
/// <param name="projectileSpeed">The projectile speed.</param>
/// <param name="damage">The damage.</param>
public PredictedDamage(
Obj_AI_Base source,
Obj_AI_Base target,
int startTick,
float delay,
float animationTime,
int projectileSpeed,
float damage)
{
this.Source = source;
this.Target = target;
this.StartTick = startTick;
this.Delay = delay;
this.ProjectileSpeed = projectileSpeed;
this.Damage = damage;
this.AnimationTime = animationTime;
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the damage.
/// </summary>
/// <value>
/// The damage.
/// </value>
public float Damage { get; private set; }
/// <summary>
/// Gets or sets the delay.
/// </summary>
/// <value>
/// The delay.
/// </value>
public float Delay { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="PredictedDamage" /> is processed.
/// </summary>
/// <value>
/// <c>true</c> if processed; otherwise, <c>false</c>.
/// </value>
public bool Processed { get; internal set; }
/// <summary>
/// Gets or sets the projectile speed.
/// </summary>
/// <value>
/// The projectile speed.
/// </value>
public int ProjectileSpeed { get; private set; }
/// <summary>
/// Gets or sets the source.
/// </summary>
/// <value>
/// The source.
/// </value>
public Obj_AI_Base Source { get; private set; }
/// <summary>
/// Gets or sets the start tick.
/// </summary>
/// <value>
/// The start tick.
/// </value>
public int StartTick { get; internal set; }
/// <summary>
/// Gets or sets the target.
/// </summary>
/// <value>
/// The target.
/// </value>
public Obj_AI_Base Target { get; private set; }
#endregion
}
}
}