-
Notifications
You must be signed in to change notification settings - Fork 32
/
Prediction.cs
641 lines (571 loc) · 21 KB
/
Prediction.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
639
640
641
// <copyright file="Prediction.cs" company="EnsageSharp">
// Copyright (c) 2017 EnsageSharp.
// This program 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.
// This program 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 this program. If not, see http://www.gnu.org/licenses/
// </copyright>
namespace Ensage.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
using Ensage.Common.Extensions;
using Ensage.Common.Objects;
using SharpDX;
/// <summary>
/// The prediction.
/// </summary>
public class Prediction
{
#region Static Fields
/// <summary>
/// The rot speed dictionary.
/// </summary>
public static Dictionary<float, double> RotSpeedDictionary = new Dictionary<float, double>();
/// <summary>
/// The rot time dictionary.
/// </summary>
public static Dictionary<float, float> RotTimeDictionary = new Dictionary<float, float>();
/// <summary>
/// The speed dictionary.
/// </summary>
public static Dictionary<float, Vector3> SpeedDictionary = new Dictionary<float, Vector3>();
/// <summary>
/// The track table.
/// </summary>
public static List<Prediction> TrackTable = new List<Prediction>();
/// <summary>
/// The last rot r dictionary.
/// </summary>
private static Dictionary<float, float> lastRotRDictionary = new Dictionary<float, float>();
/// <summary>
/// The loaded.
/// </summary>
private static bool loaded;
/// <summary>
/// The player list.
/// </summary>
private static List<Hero> playerList = new List<Hero>();
/// <summary>
/// The prediction drawings.
/// </summary>
private static Dictionary<float, ParticleEffect> predictionDrawings = new Dictionary<float, ParticleEffect>();
#endregion
#region Fields
/// <summary>
/// The last position.
/// </summary>
public Vector3 LastPosition;
/// <summary>
/// The last rot r.
/// </summary>
public float LastRotR;
/// <summary>
/// The lasttick.
/// </summary>
public float Lasttick;
/// <summary>
/// The rot speed.
/// </summary>
public float RotSpeed;
/// <summary>
/// The speed.
/// </summary>
public Vector3 Speed;
/// <summary>
/// The unit class id.
/// </summary>
public string UnitNetworkName;
/// <summary>
/// The unit name.
/// </summary>
public string UnitName;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes static members of the <see cref="Prediction" /> class.
/// </summary>
static Prediction()
{
Events.OnLoad += Events_OnLoad;
Events.OnClose += Events_OnClose;
if (Game.IsInGame)
{
Events_OnLoad(null, null);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Prediction" /> class.
/// </summary>
public Prediction()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Prediction" /> class.
/// </summary>
/// <param name="unitName">
/// The unit name.
/// </param>
/// <param name="unitNetworkName">
/// The unit network name.
/// </param>
/// <param name="speed">
/// The speed.
/// </param>
/// <param name="rotSpeed">
/// The rot speed.
/// </param>
/// <param name="lastPosition">
/// The last position.
/// </param>
/// <param name="lastRotR">
/// The last rot r.
/// </param>
/// <param name="lasttick">
/// The lasttick.
/// </param>
public Prediction(
string unitName,
string unitNetworkName,
Vector3 speed,
float rotSpeed,
Vector3 lastPosition,
float lastRotR,
float lasttick)
{
this.UnitName = unitName;
this.UnitNetworkName = unitNetworkName;
this.Speed = speed;
this.RotSpeed = rotSpeed;
this.LastPosition = lastPosition;
this.LastRotR = lastRotR;
this.Lasttick = lasttick;
}
#endregion
#region Public Methods and Operators
/// <summary>
/// The ability move.
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="bool" />.
/// </returns>
public static bool AbilityMove(Unit unit)
{
return
unit.HasModifiers(
new[]
{
"modifier_spirit_breaker_charge_of_darkness", "modifier_earth_spirit_boulder_smash",
"modifier_earth_spirit_rolling_boulder_caster", "modifier_earth_spirit_geomagnetic_grip",
"modifier_spirit_breaker_charge_of_darkness", "modifier_huskar_life_break_charge",
"modifier_magnataur_skewer_movement", "modifier_storm_spirit_ball_lightning",
"modifier_faceless_void_time_walk", "modifier_mirana_leap", "modifier_slark_pounce"
},
false);
}
/// <summary>
/// The calculate reach time.
/// </summary>
/// <param name="target">
/// The target.
/// </param>
/// <param name="speed">
/// The speed.
/// </param>
/// <param name="dePos">
/// The de pos.
/// </param>
/// <returns>
/// The <see cref="float" />.
/// </returns>
public static float CalculateReachTime(Unit target, float speed, Vector3 dePos)
{
Vector3 targetSpeed;
if ((!SpeedDictionary.TryGetValue(target.Handle, out targetSpeed) || targetSpeed == new Vector3(0, 0, 0))
&& target.NetworkActivity == NetworkActivity.Move)
{
var rotSpeed = 0d;
if (RotSpeedDictionary.ContainsKey(target.Handle))
{
rotSpeed = RotSpeedDictionary[target.Handle];
}
targetSpeed = target.Vector3FromPolarAngle((float)rotSpeed) * target.MovementSpeed / 1000;
}
var a = Math.Pow(targetSpeed.X, 2) + Math.Pow(targetSpeed.Y, 2) - Math.Pow(speed / 1000, 2);
var b = 2 * (dePos.X * targetSpeed.X + dePos.Y * targetSpeed.Y);
var c = Math.Pow(dePos.X, 2) + Math.Pow(dePos.Y, 2);
return (float)((-b - Math.Sqrt(Math.Pow(b, 2) - 4 * a * c)) / (2 * a));
}
/// <summary>
/// The draw predictions.
/// </summary>
/// <param name="delay">
/// The delay.
/// </param>
public static void DrawPredictions(float delay = 1000)
{
var heroes = Heroes.All.Where(x => !x.IsIllusion);
foreach (var unit in heroes)
{
ParticleEffect effect;
if (!predictionDrawings.TryGetValue(unit.Handle, out effect))
{
effect = new ParticleEffect(
@"particles\ui_mouseactions\range_display.vpcf",
PredictedXYZ(unit, delay));
effect.SetControlPoint(1, new Vector3(unit.HullRadius + 20, 0, 0));
predictionDrawings.Add(unit.Handle, effect);
}
effect.SetControlPoint(0, PredictedXYZ(unit, delay));
}
}
/// <summary>
/// Returns vector in facing direction of given unit with given distance
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <param name="distance">
/// The distance.
/// </param>
/// <returns>
/// The <see cref="Vector3" />.
/// </returns>
public static Vector3 InFront(Unit unit, float distance)
{
var v = unit.Position + unit.Vector3FromPolarAngle() * distance;
return new Vector3(v.X, v.Y, 0);
}
/// <summary>
/// Checks if enemy is not moving
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="bool" />.
/// </returns>
public static bool IsIdle(Unit unit)
{
return unit.NetworkActivity != NetworkActivity.Move;
// && ((unit.NetworkActivity == NetworkActivity.Idle
// && (!SpeedDictionary.ContainsKey(unit.Handle) || SpeedDictionary[unit.Handle] == Vector3.Zero))
// || unit.IsAttacking() || unit.IsInvul() || unit.IsStunned());
}
/// <summary>
/// Checks if a unit is currently changing their direction
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <param name="tolerancy">
/// tolerancy of rotation speed
/// </param>
/// <returns>
/// The <see cref="bool" />.
/// </returns>
public static bool IsTurning(Unit unit, double tolerancy = 0)
{
double rotSpeed;
if (!RotSpeedDictionary.TryGetValue(unit.Handle, out rotSpeed))
{
return false;
}
return Math.Abs(rotSpeed) > tolerancy;
}
/// <summary>
/// Returns predicted location of given unit after given delay in ms
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <param name="delay">
/// The delay.
/// </param>
/// <returns>
/// The <see cref="Vector3" />.
/// </returns>
public static Vector3 PredictedXYZ(Unit unit, float delay)
{
if (IsIdle(unit))
{
return unit.Position;
}
var targetSpeed = new Vector3();
if (!lastRotRDictionary.ContainsKey(unit.Handle))
{
lastRotRDictionary.Add(unit.Handle, unit.RotationRad);
}
var straightTime = StraightTime(unit);
if (straightTime > 180)
{
lastRotRDictionary[unit.Handle] = unit.RotationRad;
}
lastRotRDictionary[unit.Handle] = unit.RotationRad;
if ((unit.NetworkName == "CDOTA_Unit_Hero_StormSpirit" || unit.NetworkName == "CDOTA_Unit_Hero_Rubick")
&& unit.HasModifier("modifier_storm_spirit_ball_lightning"))
{
var ballLightning = unit.FindSpell("storm_spirit_ball_lightning", true);
var firstOrDefault =
ballLightning.AbilitySpecialData.FirstOrDefault(x => x.Name == "ball_lightning_move_speed");
if (firstOrDefault != null)
{
var ballSpeed = firstOrDefault.GetValue(ballLightning.Level - 1);
var newpredict = unit.Vector3FromPolarAngle() * (ballSpeed / 1000f);
targetSpeed = newpredict;
}
}
else
{
targetSpeed = unit.Vector3FromPolarAngle() * (unit.MovementSpeed / 1000f);
}
var v = unit.Position + targetSpeed * delay;
return new Vector3(v.X, v.Y, 0);
}
/// <summary>
/// The skill shot xyz.
/// </summary>
/// <param name="source">
/// The source.
/// </param>
/// <param name="target">
/// The target.
/// </param>
/// <param name="delay">
/// The delay.
/// </param>
/// <param name="speed">
/// The speed.
/// </param>
/// <param name="radius">
/// The radius.
/// </param>
/// <returns>
/// The <see cref="Vector3" />.
/// </returns>
public static Vector3 SkillShotXYZ(Unit source, Unit target, float delay, float speed, float radius)
{
if (IsIdle(target))
{
return target.Position;
}
if (!(speed < 6000) || speed <= 0)
{
return PredictedXYZ(target, delay);
}
var predict = PredictedXYZ(target, delay);
var sourcePos = source.Position;
var reachTime = CalculateReachTime(target, speed, predict - sourcePos);
predict = PredictedXYZ(target, delay + reachTime);
if (!(source.Distance2D(predict) > radius))
{
return PredictedXYZ(target, delay + reachTime);
}
if (target.MovementSpeed * ((predict.Distance2D(sourcePos) - radius) / speed) < radius)
{
sourcePos = (sourcePos - predict) * (sourcePos.Distance2D(predict) - radius)
/ sourcePos.Distance2D(predict) + predict;
reachTime = CalculateReachTime(target, speed, predict - sourcePos);
}
else
{
sourcePos = (sourcePos - predict)
* (sourcePos.Distance2D(predict)
+ target.MovementSpeed * ((predict.Distance2D(sourcePos) - radius) / speed) - radius)
/ sourcePos.Distance2D(predict) + predict;
reachTime = CalculateReachTime(target, speed, predict - sourcePos);
}
return PredictedXYZ(target, delay + reachTime);
}
/// <summary>
/// Tracks heroes movements
/// </summary>
/// <param name="args">
/// The args.
/// </param>
public static void SpeedTrack(EventArgs args)
{
if (!Game.IsInGame || Game.IsPaused)
{
return;
}
var me = ObjectManager.LocalHero;
if (me == null)
{
return;
}
if (!Utils.SleepCheck("Prediction.SpeedTrack.Sleep"))
{
return;
}
if (playerList == null || playerList.Count < 10 && Utils.SleepCheck("Prediction.SpeedTrack"))
{
playerList = Heroes.All;
Utils.Sleep(1000, "Prediction.SpeedTrack");
}
if (!playerList.Any())
{
return;
}
Utils.Sleep(70, "Prediction.SpeedTrack.Sleep");
var tick = Utils.TickCount;
var tempTable = new List<Prediction>(TrackTable);
foreach (var unit in playerList)
{
if (!unit.IsValid)
{
continue;
}
var data =
tempTable.FirstOrDefault(
unitData => unitData.UnitName == unit.StoredName() || unitData.UnitNetworkName == unit.NetworkName);
if (data == null && unit.IsAlive && unit.IsVisible)
{
data = new Prediction(
unit.StoredName(),
unit.NetworkName,
new Vector3(0, 0, 0),
0,
new Vector3(0, 0, 0),
0,
0);
TrackTable.Add(data);
}
if (data != null && (!unit.IsAlive || !unit.IsVisible))
{
data.LastPosition = new Vector3(0, 0, 0);
data.LastRotR = 0;
data.Lasttick = 0;
continue;
}
if (data == null || data.LastPosition != new Vector3(0, 0, 0) && !(tick - data.Lasttick > 0))
{
continue;
}
if (data.LastPosition == new Vector3(0, 0, 0))
{
data.LastPosition = unit.Position;
data.LastRotR = unit.RotationRad;
data.Lasttick = tick;
}
else
{
data.RotSpeed = data.LastRotR - unit.RotationRad;
if (!RotTimeDictionary.ContainsKey(unit.Handle))
{
RotTimeDictionary.Add(unit.Handle, tick);
}
if (!lastRotRDictionary.ContainsKey(unit.Handle))
{
lastRotRDictionary.Add(unit.Handle, unit.RotationRad);
}
var speed = (unit.Position - data.LastPosition) / (tick - data.Lasttick);
if (Math.Abs(data.RotSpeed) > 0.18 && data.Speed != new Vector3(0, 0, 0))
{
data.Speed = speed;
RotTimeDictionary[unit.Handle] = tick;
}
else
{
lastRotRDictionary[unit.Handle] = unit.RotationRad;
data.Speed = speed;
}
data.LastPosition = unit.Position;
data.LastRotR = unit.RotationRad;
data.Lasttick = tick;
if (!SpeedDictionary.ContainsKey(unit.Handle))
{
SpeedDictionary.Add(unit.Handle, data.Speed);
}
else
{
SpeedDictionary[unit.Handle] = data.Speed;
}
if (!RotSpeedDictionary.ContainsKey(unit.Handle))
{
RotSpeedDictionary.Add(unit.Handle, data.RotSpeed);
}
else
{
RotSpeedDictionary[unit.Handle] = data.RotSpeed;
}
}
}
}
/// <summary>
/// Returns in miliseconds how long is unit walking straight
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="float" />.
/// </returns>
public static float StraightTime(Unit unit)
{
if (!RotTimeDictionary.ContainsKey(unit.Handle))
{
return 0;
}
return Utils.TickCount - RotTimeDictionary[unit.Handle] + Game.Ping;
}
#endregion
#region Methods
/// <summary>
/// The events_ on close.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private static void Events_OnClose(object sender, EventArgs e)
{
loaded = false;
Events.OnUpdate -= SpeedTrack;
playerList = new List<Hero>();
RotSpeedDictionary = new Dictionary<float, double>();
RotTimeDictionary = new Dictionary<float, float>();
SpeedDictionary = new Dictionary<float, Vector3>();
TrackTable = new List<Prediction>();
predictionDrawings = new Dictionary<float, ParticleEffect>();
}
/// <summary>
/// The events_ on load.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private static void Events_OnLoad(object sender, EventArgs e)
{
if (loaded)
{
return;
}
loaded = true;
playerList = new List<Hero>();
RotSpeedDictionary = new Dictionary<float, double>();
RotTimeDictionary = new Dictionary<float, float>();
SpeedDictionary = new Dictionary<float, Vector3>();
TrackTable = new List<Prediction>();
predictionDrawings = new Dictionary<float, ParticleEffect>();
lastRotRDictionary = new Dictionary<float, float>();
Events.OnUpdate += SpeedTrack;
}
#endregion
}
}