-
Notifications
You must be signed in to change notification settings - Fork 2
/
EssenceDrainContagion.cs
241 lines (220 loc) · 9.6 KB
/
EssenceDrainContagion.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using ExileCore;
using ExileCore.PoEMemory.Components;
using ExileCore.PoEMemory.MemoryObjects;
using ExileCore.Shared;
using ExileCore.Shared.Enums;
using ExileCore.Shared.Helpers;
using SharpDX;
namespace EssenceDrainContagion
{
public class EssenceDrainContagion : BaseSettingsPlugin<EssenceDrainContagionSettings>
{
private bool _aiming;
private Vector2 _oldMousePos;
private HashSet<string> _ignoredMonsters;
private Coroutine _mainCoroutine;
private Tuple<float, Entity> _currentTarget;
private Stopwatch _lastTargetSwap = new Stopwatch();
private readonly string[] _ignoredBuffs =
{
"capture_monster_captured",
"capture_monster_disappearing"
};
private readonly string[] _lightLessGrub =
{
"Metadata/Monsters/HuhuGrub/AbyssGrubMobile",
"Metadata/Monsters/HuhuGrub/AbyssGrubMobileMinion"
};
private readonly string[] _raisedZombie =
{
"Metadata/Monsters/RaisedZombies/RaisedZombieStandard",
"Metadata/Monsters/RaisedZombies/RaisedZombieMummy",
"Metadata/Monsters/RaisedZombies/NecromancerRaisedZombieStandard"
};
private readonly string[] _summonedSkeleton =
{
"Metadata/Monsters/RaisedSkeletons/RaisedSkeletonStandard",
"Metadata/Monsters/RaisedSkeletons/RaisedSkeletonStatue",
"Metadata/Monsters/RaisedSkeletons/RaisedSkeletonMannequin",
"Metadata/Monsters/RaisedSkeletons/RaisedSkeletonStatueMale",
"Metadata/Monsters/RaisedSkeletons/RaisedSkeletonStatueGold",
"Metadata/Monsters/RaisedSkeletons/RaisedSkeletonStatueGoldMale",
"Metadata/Monsters/RaisedSkeletons/NecromancerRaisedSkeletonStandard",
"Metadata/Monsters/RaisedSkeletons/TalismanRaisedSkeletonStandard"
};
public override bool Initialise()
{
LoadIgnoredMonsters($@"{DirectoryFullName}\Ignored Monsters.txt");
Input.RegisterKey(Settings.AimKey);
_mainCoroutine = new Coroutine(
MainCoroutine(),
this,
"EDC");
Core.ParallelRunner.Run(_mainCoroutine);
return true;
}
private IEnumerator MainCoroutine()
{
while (true)
{
try
{
if (_currentTarget == null ||
!ValidTarget(_currentTarget?.Item2))
{
_currentTarget = ScanValidMonsters()?.FirstOrDefault();
_lastTargetSwap.Restart();
}
else if (_lastTargetSwap.ElapsedMilliseconds > 100)
{
var best = ScanValidMonsters()?.FirstOrDefault();
if (best?.Item1 > 1.2f * _currentTarget?.Item1) _currentTarget = best;
_lastTargetSwap.Restart();
}
}
catch
{
// ignored
}
if (!Input.IsKeyDown(Settings.AimKey))
_oldMousePos = Input.MousePosition;
if (Input.IsKeyDown(Settings.AimKey)
&& !GameController.Game.IngameState.IngameUi.InventoryPanel.IsVisible
&& !GameController.Game.IngameState.IngameUi.OpenLeftPanel.IsVisible)
{
_aiming = true;
yield return Attack();
}
if (!Input.IsKeyDown(Settings.AimKey) && _aiming)
{
Input.SetCursorPos(_oldMousePos);
_aiming = false;
}
yield return new WaitTime(10);
}
// ReSharper disable once IteratorNeverReturns
}
private bool ValidTarget(Entity entity)
{
try
{
return entity != null &&
entity.IsValid &&
entity.IsAlive &&
entity.HasComponent<Monster>() &&
entity.IsHostile &&
entity.HasComponent<Targetable>() &&
entity.GetComponent<Targetable>().isTargetable &&
entity.HasComponent<Life>() &&
entity.GetComponent<Life>().CurHP > 0 &&
entity.DistancePlayer < Settings.AimRangeGrid &&
GameController.Window.GetWindowRectangleTimeCache.Contains(
GameController.Game.IngameState.Camera.WorldToScreen(entity.Pos));
}
catch
{
return false;
}
}
public override void Render()
{
if (_currentTarget != null)
{
var position = GameController.Game.IngameState.Camera.WorldToScreen(_currentTarget.Item2.Pos);
Graphics.DrawFrame(position, position.Translate(20, 20), Color.Chocolate, 3);
}
base.Render();
}
private void LoadIgnoredMonsters(string fileName)
{
_ignoredMonsters = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (!File.Exists(fileName))
{
LogError($@"Failed to find {fileName}", 10);
return;
}
foreach (var line in File.ReadAllLines(fileName))
if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))
_ignoredMonsters.Add(line.Trim().ToLower());
}
private IEnumerator Attack()
{
if (_currentTarget == null) yield break;
var position = GameController.Game.IngameState.Camera.WorldToScreen(_currentTarget.Item2.Pos);
Input.SetCursorPos(position);
yield return Input.KeyPress(_currentTarget.Item2.HasBuff("contagion", true)
? Settings.EssenceDrainKey.Value
: Settings.ContagionKey.Value);
}
private IEnumerable<Tuple<float, Entity>> ScanValidMonsters()
{
var queue =
from entity in GameController?.EntityListWrapper?.ValidEntitiesByType?[EntityType.Monster]
where ValidTarget(entity)
where !Extensions.HaveStat(entity, GameStat.CannotDie) &&
!Extensions.HaveStat(entity, GameStat.CannotBeDamaged) &&
!Extensions.HaveStat(entity, GameStat.IgnoredByEnemyTargetSelection) &&
!_ignoredBuffs.Any(b => entity.HasBuff(b)) &&
!_ignoredMonsters.Any(im => entity.Path.ToLower().Contains(im))
let weight = ComputeWeight(entity)
orderby weight descending
select new Tuple<float, Entity>(weight, entity);
return queue;
}
private float ComputeWeight(Entity entity)
{
var weight = 0;
if (Settings.ClosestToMouse)
{
var p1 = Input.MousePosition;
var p2 = GameController.Game.IngameState.Camera.WorldToScreen(entity.Pos);
weight -= (int) (p1.Distance(p2) / 10f);
}
else
{
var p1 = GameController.Game.IngameState.Camera.WorldToScreen(GameController.Player.Pos);
var p2 = GameController.Game.IngameState.Camera.WorldToScreen(entity.Pos);
weight -= (int) (p1.Distance(p2) / 10f);
}
if (entity.GetComponent<Buffs>().HasBuff("contagion")) weight += Settings.HasContagionWeight;
if (entity.GetComponent<Buffs>().HasBuff("capture_monster_trapped"))
weight += Settings.capture_monster_trapped;
if (entity.GetComponent<Buffs>().HasBuff("harbinger_minion_new")) weight += Settings.HarbingerMinionWeight;
if (entity.GetComponent<Buffs>().HasBuff("capture_monster_enraged"))
weight += Settings.capture_monster_enraged;
if (entity.Path.Contains("/BeastHeart")) weight += Settings.BeastHearts;
if (entity.Path == "Metadata/Monsters/Tukohama/TukohamaShieldTotem") weight += Settings.TukohamaShieldTotem;
switch (entity.GetComponent<ObjectMagicProperties>().Rarity)
{
case MonsterRarity.Unique:
weight += Settings.UniqueRarityWeight;
break;
case MonsterRarity.Rare:
weight += Settings.RareRarityWeight;
break;
case MonsterRarity.Magic:
weight += Settings.MagicRarityWeight;
break;
case MonsterRarity.White:
weight += Settings.NormalRarityWeight;
break;
case MonsterRarity.Error:
break;
default:
throw new ArgumentOutOfRangeException();
}
if (entity.HasComponent<DiesAfterTime>()) weight += Settings.DiesAfterTime;
if (_summonedSkeleton.Any(path => entity.Path == path)) weight += Settings.SummonedSkeoton;
if (_raisedZombie.Any(path => entity.Path == path)) weight += Settings.RaisedZombie;
if (_lightLessGrub.Any(path => entity.Path == path)) weight += Settings.LightlessGrub;
if (entity.Path.Contains("TaniwhaTail")) weight += Settings.TaniwhaTail;
return weight;
}
}
}