-
Notifications
You must be signed in to change notification settings - Fork 244
/
MinionManager.cs
418 lines (367 loc) · 14.8 KB
/
MinionManager.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
namespace LeagueSharp.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
using SharpDX;
/// <summary>
/// An enum representing the order the minions should be listed.
/// </summary>
public enum MinionOrderTypes
{
/// <summary>
/// No order.
/// </summary>
None,
/// <summary>
/// Ordered by the current health of the minion. (Least to greatest)
/// </summary>
Health,
/// <summary>
/// Ordered by the maximum health of the minions. (Greatest to least)
/// </summary>
MaxHealth
}
/// <summary>
/// The team of the minion.
/// </summary>
public enum MinionTeam
{
/// <summary>
/// The minion is not on either team.
/// </summary>
Neutral,
/// <summary>
/// The minions is an ally
/// </summary>
Ally,
/// <summary>
/// The minions is an enemy
/// </summary>
Enemy,
/// <summary>
/// The minion is not an ally
/// </summary>
NotAlly,
/// <summary>
/// The minions is not an ally for the enemy
/// </summary>
NotAllyForEnemy,
/// <summary>
/// Any minion.
/// </summary>
All
}
/// <summary>
/// The type of minion.
/// </summary>
public enum MinionTypes
{
/// <summary>
/// Ranged minions.
/// </summary>
Ranged,
/// <summary>
/// Melee minions.
/// </summary>
Melee,
/// <summary>
/// Any minion
/// </summary>
All,
/// <summary>
/// Any wards. (TODO)
/// </summary>
[Obsolete("Wards have not been implemented yet in the minion manager.")]
Wards
}
/// <summary>
/// Manages minions.
/// </summary>
public static class MinionManager
{
#region Public Methods and Operators
/// <summary>
/// Returns the point where, when casted, the circular spell with hit the maximum amount of minions.
/// </summary>
/// <param name="minionPositions">The minion positions.</param>
/// <param name="width">The width.</param>
/// <param name="range">The range.</param>
/// <param name="useMECMax">The use mec maximum.</param>
/// <returns>FarmLocation.</returns>
public static FarmLocation GetBestCircularFarmLocation(
List<Vector2> minionPositions,
float width,
float range,
int useMECMax = 9)
{
var result = new Vector2();
var minionCount = 0;
var startPos = ObjectManager.Player.ServerPosition.To2D();
range = range * range;
if (minionPositions.Count == 0)
{
return new FarmLocation(result, minionCount);
}
/* Use MEC to get the best positions only when there are less than 9 positions because it causes lag with more. */
if (minionPositions.Count <= useMECMax)
{
var subGroups = GetCombinations(minionPositions);
foreach (var subGroup in subGroups)
{
if (subGroup.Count > 0)
{
var circle = MEC.GetMec(subGroup);
if (circle.Radius <= width && circle.Center.Distance(startPos, true) <= range)
{
minionCount = subGroup.Count;
return new FarmLocation(circle.Center, minionCount);
}
}
}
}
else
{
foreach (var pos in minionPositions)
{
if (pos.Distance(startPos, true) <= range)
{
var count = minionPositions.Count(pos2 => pos.Distance(pos2, true) <= width * width);
if (count >= minionCount)
{
result = pos;
minionCount = count;
}
}
}
}
return new FarmLocation(result, minionCount);
}
/// <summary>
/// Returns the point where, when casted, the linear spell with hit the maximum amount of minions.
/// </summary>
/// <param name="minionPositions">The minion positions.</param>
/// <param name="width">The width.</param>
/// <param name="range">The range.</param>
/// <returns>FarmLocation.</returns>
public static FarmLocation GetBestLineFarmLocation(List<Vector2> minionPositions, float width, float range)
{
var result = new Vector2();
var minionCount = 0;
var startPos = ObjectManager.Player.ServerPosition.To2D();
var posiblePositions = new List<Vector2>();
posiblePositions.AddRange(minionPositions);
var max = minionPositions.Count;
for (var i = 0; i < max; i++)
{
for (var j = 0; j < max; j++)
{
if (minionPositions[j] != minionPositions[i])
{
posiblePositions.Add((minionPositions[j] + minionPositions[i]) / 2);
}
}
}
foreach (var pos in posiblePositions)
{
if (pos.Distance(startPos, true) <= range * range)
{
var endPos = startPos + range * (pos - startPos).Normalized();
var count =
minionPositions.Count(pos2 => pos2.Distance(startPos, endPos, true, true) <= width * width);
if (count >= minionCount)
{
result = endPos;
minionCount = count;
}
}
}
return new FarmLocation(result, minionCount);
}
/// <summary>
/// Gets minions based on range, type, team and then orders them.
/// </summary>
/// <param name="from">The point to get the minions from.</param>
/// <param name="range">The range.</param>
/// <param name="type">The type.</param>
/// <param name="team">The team.</param>
/// <param name="order">The order.</param>
/// <returns>List<Obj_AI_Base>.</returns>
public static List<Obj_AI_Base> GetMinions(
Vector3 from,
float range,
MinionTypes type = MinionTypes.All,
MinionTeam team = MinionTeam.Enemy,
MinionOrderTypes order = MinionOrderTypes.Health)
{
var result = (from minion in ObjectManager.Get<Obj_AI_Minion>()
where minion.IsValidTarget(range, false, @from)
let minionTeam = minion.Team
where
team == MinionTeam.Neutral && minionTeam == GameObjectTeam.Neutral
|| team == MinionTeam.Ally
&& minionTeam
== (ObjectManager.Player.Team == GameObjectTeam.Chaos
? GameObjectTeam.Chaos
: GameObjectTeam.Order)
|| team == MinionTeam.Enemy
&& minionTeam
== (ObjectManager.Player.Team == GameObjectTeam.Chaos
? GameObjectTeam.Order
: GameObjectTeam.Chaos)
|| team == MinionTeam.NotAlly && minionTeam != ObjectManager.Player.Team
|| team == MinionTeam.NotAllyForEnemy
&& (minionTeam == ObjectManager.Player.Team || minionTeam == GameObjectTeam.Neutral)
|| team == MinionTeam.All
where
minion.IsMelee() && type == MinionTypes.Melee
|| !minion.IsMelee() && type == MinionTypes.Ranged || type == MinionTypes.All
where
IsMinion(minion)
|| minionTeam == GameObjectTeam.Neutral && minion.MaxHealth > 5 && minion.IsHPBarRendered
select minion).Cast<Obj_AI_Base>().ToList();
switch (order)
{
case MinionOrderTypes.Health:
result = result.OrderBy(o => o.Health).ToList();
break;
case MinionOrderTypes.MaxHealth:
result = result.OrderByDescending(o => o.MaxHealth).ToList();
break;
}
return result;
}
/// <summary>
/// Gets the minions.
/// </summary>
/// <param name="range">The range.</param>
/// <param name="type">The type.</param>
/// <param name="team">The team.</param>
/// <param name="order">The order.</param>
/// <returns>List<Obj_AI_Base>.</returns>
public static List<Obj_AI_Base> GetMinions(
float range,
MinionTypes type = MinionTypes.All,
MinionTeam team = MinionTeam.Enemy,
MinionOrderTypes order = MinionOrderTypes.Health)
{
return GetMinions(ObjectManager.Player.ServerPosition, range, type, team, order);
}
/// <summary>
/// Gets the minions predicted positions.
/// </summary>
/// <param name="minions">The minions.</param>
/// <param name="delay">The delay.</param>
/// <param name="width">The width.</param>
/// <param name="speed">The speed.</param>
/// <param name="from">From.</param>
/// <param name="range">The range.</param>
/// <param name="collision">if set to <c>true</c>, checks for collision.</param>
/// <param name="stype">The skillshot type.</param>
/// <param name="rangeCheckFrom">The position to check the range from.</param>
/// <returns>List<Vector2>.</returns>
public static List<Vector2> GetMinionsPredictedPositions(
List<Obj_AI_Base> minions,
float delay,
float width,
float speed,
Vector3 from,
float range,
bool collision,
SkillshotType stype,
Vector3 rangeCheckFrom = new Vector3())
{
from = from.To2D().IsValid() ? from : ObjectManager.Player.ServerPosition;
return (from minion in minions
select
Prediction.GetPrediction(
new PredictionInput
{
Unit = minion, Delay = delay, Radius = width, Speed = speed, From = @from,
Range = range, Collision = collision, Type = stype, RangeCheckFrom = rangeCheckFrom
})
into pos
where pos.Hitchance >= HitChance.High
select pos.UnitPosition.To2D()).ToList();
}
/// <summary>
/// Determines whether the specified object is a minion.
/// </summary>
/// <param name="minion">The minion.</param>
/// <param name="includeWards">if set to <c>true</c> [include wards].</param>
/// <returns><c>true</c> if the specified minion is minion; otherwise, <c>false</c>.</returns>
public static bool IsMinion(Obj_AI_Minion minion, bool includeWards = false)
{
return minion.Name.Contains("Minion") || includeWards && IsWard(minion);
}
/// <summary>
/// Determines whether the specified base skin name is ward.
/// </summary>
/// <param name="baseSkinName">Name of the base skin. Should be lowercase.</param>
/// <returns><c>true</c> if the specified base skin name is ward; otherwise, <c>false</c>.</returns>
[Obsolete("Use IsWard(Obj_AI_Minion)")]
public static bool IsWard(string baseSkinName)
{
return baseSkinName.Contains("ward");
}
/// <summary>
/// Determines whether the specified minion is a valid attackable ward.
/// </summary>
/// <param name="minion">The minion you want to check for</param>
/// <returns><c>true</c> if the given minion is a valid attackable ward, otherwise returns <c>false</c>.</returns>
public static bool IsWard(Obj_AI_Minion minion)
{
return minion.Name.Contains("Ward") && minion.IsHPBarRendered;
}
#endregion
#region Methods
/*
from: https://stackoverflow.com/questions/10515449/generate-all-combinations-for-a-list-of-strings :^)
*/
/// <summary>
/// Returns all the subgroup combinations that can be made from a group
/// </summary>
/// <param name="allValues">All values.</param>
/// <returns>List<List<Vector2>>.</returns>
private static List<List<Vector2>> GetCombinations(List<Vector2> allValues)
{
var collection = new List<List<Vector2>>();
for (var counter = 0; counter < (1 << allValues.Count); ++counter)
{
var combination = allValues.Where((t, i) => (counter & (1 << i)) == 0).ToList();
collection.Add(combination);
}
return collection;
}
#endregion
/// <summary>
/// A struct that represents the best position to cast a skillshot to hit the best number of minions, as well as the
/// number of minions hit.
/// </summary>
public struct FarmLocation
{
#region Fields
/// <summary>
/// The minions hit
/// </summary>
public int MinionsHit;
/// <summary>
/// The position
/// </summary>
public Vector2 Position;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="FarmLocation" /> struct.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="minionsHit">The minions hit.</param>
public FarmLocation(Vector2 position, int minionsHit)
{
this.Position = position;
this.MinionsHit = minionsHit;
}
#endregion
}
}
}