forked from LeagueSharp/LeagueSharp.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthPrediction.cs
183 lines (155 loc) · 6.64 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
#region LICENSE
/*
Copyright 2014 - 2014 LeagueSharp
HealthPrediction.cs is part of LeagueSharp.Common.
LeagueSharp.Common is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LeagueSharp.Common is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LeagueSharp.Common. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
#region
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace LeagueSharp.Common
{
/// <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
{
private static readonly Dictionary<int, PredictedDamage> ActiveAttacks = new Dictionary<int, PredictedDamage>();
static HealthPrediction()
{
Obj_AI_Base.OnProcessSpellCast += ObjAiBaseOnOnProcessSpellCast;
Game.OnUpdate += Game_OnGameUpdate;
Spellbook.OnStopCast += SpellbookOnStopCast;
}
private static void Game_OnGameUpdate(EventArgs args)
{
ActiveAttacks.ToList()
.Where(pair => pair.Value.StartTick < Utils.GameTimeTickCount - 3000)
.ToList()
.ForEach(pair => ActiveAttacks.Remove(pair.Key));
}
private static void SpellbookOnStopCast(Spellbook spellbook, SpellbookStopCastEventArgs args)
{
if (spellbook.Owner.IsValid && args.DestroyMissile && args.StopAnimation)
{
if (ActiveAttacks.ContainsKey(spellbook.Owner.NetworkId))
{
ActiveAttacks.Remove(spellbook.Owner.NetworkId);
}
}
}
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>
/// Returns the unit health after a set time milliseconds.
/// </summary>
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.Source.IsValidTarget(float.MaxValue, false) &&
attack.Target.IsValidTarget(float.MaxValue, false) && attack.Target.NetworkId == unit.NetworkId)
{
var landTime = attack.StartTick + attack.Delay +
1000 * unit.Distance(attack.Source) / attack.ProjectileSpeed + delay;
if (Utils.GameTimeTickCount < landTime - delay && landTime < Utils.GameTimeTickCount + time)
{
attackDamage = attack.Damage;
}
}
predictedDamage += attackDamage;
}
return unit.Health - predictedDamage;
}
/// <summary>
/// Returns the unit health after time milliseconds assuming that the past auto-attacks are periodic.
/// </summary>
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 + unit.Distance(attack.Source) / attack.ProjectileSpeed < toT))
{
n++;
}
fromT += (int) attack.AnimationTime;
}
}
predictedDamage += n * attack.Damage;
}
return unit.Health - predictedDamage;
}
public static bool HasMinionAggro(Obj_AI_Minion minion)
{
return ActiveAttacks.Values.Any(m => (m.Source is Obj_AI_Minion) && m.Target == minion);
}
private class PredictedDamage
{
public readonly float AnimationTime;
public readonly float Damage;
public readonly float Delay;
public readonly int ProjectileSpeed;
public readonly Obj_AI_Base Source;
public readonly int StartTick;
public readonly Obj_AI_Base Target;
public PredictedDamage(Obj_AI_Base source,
Obj_AI_Base target,
int startTick,
float delay,
float animationTime,
int projectileSpeed,
float damage)
{
Source = source;
Target = target;
StartTick = startTick;
Delay = delay;
ProjectileSpeed = projectileSpeed;
Damage = damage;
AnimationTime = animationTime;
}
}
}
}