-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEffectZones.cs
More file actions
236 lines (208 loc) · 9.08 KB
/
EffectZones.cs
File metadata and controls
236 lines (208 loc) · 9.08 KB
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using ExileCore2;
using ExileCore2.PoEMemory.Components;
using ExileCore2.PoEMemory.MemoryObjects;
using ExileCore2.Shared.Enums;
using ExileCore2.Shared.Nodes;
namespace EffectZones;
public class EffectZones : BaseSettingsPlugin<EffectZonesSettings>
{
private readonly ConditionalWeakTable<string, Func<string, bool>> _pathMatchers = [];
public const int TileToGridConversion = 23;
public const int TileToWorldConversion = 250;
public const float WorldToGridConversion = TileToGridConversion / (float)TileToWorldConversion;
private IngameUIElements _ingameUi;
private readonly ConditionalWeakTable<Entity, Stopwatch> _rejectedCache = [];
private readonly TimeBasedEntityCache entityCache = new(TimeSpan.FromSeconds(2));
private readonly ConcurrentBag<uint> soundAlertCache = new();
private readonly Stopwatch soundAlertTimer = Stopwatch.StartNew();
private readonly TimeSpan soundAlertCooldown = TimeSpan.FromSeconds(5);
public override bool Initialise()
{
Settings.RemoveMatchedUnknownEffects.OnPressed += () =>
{
var contentToRemove = new HashSet<string>();
foreach (var node in Settings.UnknownEffects.Content)
{
if (Settings.BlacklistTemplates.Content.Any(x => IsMatch(x.Value, node.Value)) ||
Settings.EntityGroups.Content.Any(g => g.PathTemplates.Content.Any(p => IsMatch(p.Value, node.Value))))
{
contentToRemove.Add(node.Value);
}
}
if (contentToRemove.Any())
{
Settings.UnknownEffects.Content.RemoveAll(x => contentToRemove.Contains(x.Value));
}
};
Settings.RemoveAllUnknownEffects.OnPressed += () =>
{
Settings.UnknownEffects.Content.Clear();
Settings.LethalUnknownEffects.Content.Clear();
};
return true;
}
public override void Tick()
{
_ingameUi = GameController.Game.IngameState.IngameUi;
}
private bool IsMatch(string template, string path)
{
if (string.IsNullOrWhiteSpace(template) ||
string.IsNullOrWhiteSpace(path))
{
return false;
}
return _pathMatchers.GetValue(template, t =>
{
var regexes = t.Split('&').Select(x => x.StartsWith('!')
? (false, x[1..])
: (true, x))
.Select(p => (p.Item1, new Regex(p.Item2, RegexOptions.IgnoreCase)))
.ToList();
return s => regexes.All(x => x.Item2.IsMatch(s) == x.Item1);
})!(path);
}
public override void Render()
{
if (!Settings.IgnoreFullscreenPanels &&
_ingameUi.FullscreenPanels.Any(x => x.IsVisible) ||
!Settings.IgnoreLargePanels &&
_ingameUi.LargePanels.Any(x => x.IsVisible))
return;
var entityLists = new List<IEnumerable<Entity>>
{
GameController?.EntityListWrapper?.ValidEntitiesByType[EntityType.Effect] ?? [],
GameController?.EntityListWrapper?.ValidEntitiesByType[EntityType.MonsterMods] ?? [],
GameController?.EntityListWrapper?.ValidEntitiesByType[EntityType.Terrain] ?? [],
GameController?.EntityListWrapper?.ValidEntitiesByType[EntityType.None] ?? [],
GameController?.EntityListWrapper?.ValidEntitiesByType[EntityType.Monster] ?? [],
};
var entityList = entityLists.SelectMany(list => list).ToList();
foreach (var entity in entityList)
{
if (entity == null) continue;
if (_rejectedCache.TryGetValue(entity, out var stopwatch))
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(1))
{
_rejectedCache.Remove(entity);
}
else
{
continue;
}
}
if (!entity.TryGetComponent<Animated>(out var animated) || animated.BaseAnimatedObjectEntity is not { } baseEntity || string.IsNullOrEmpty(baseEntity.Path))
{
_rejectedCache.AddOrUpdate(entity, Stopwatch.StartNew());
continue;
}
if (entity.DistancePlayer >= Settings.EntityLookupRange)
{
continue;
}
if (Settings.BlacklistTemplates.Content.Any(x => IsMatch(x.Value, baseEntity.Path)))
{
_rejectedCache.AddOrUpdate(entity, Stopwatch.StartNew());
continue;
}
var matchingGroup = Settings.EntityGroups.Content.FirstOrDefault(g => g.PathTemplates.Content.Any(p => IsMatch(p.Value, baseEntity.Path)));
if (matchingGroup != null)
{
float? baseRadius = matchingGroup.BaseSizeOverride.Value is > 0 and { } baseOverride
? baseOverride
: entity.TryGetComponent<GroundEffect>(out var effect) && effect.EffectDescription?.BaseSize is { } groundEffectSize
? groundEffectSize
: animated.MiscAnimated?.BaseSize;
if (baseRadius == null && !matchingGroup.IgnoreBaseSize)
{
_rejectedCache.AddOrUpdate(entity, Stopwatch.StartNew());
continue;
}
var scale = matchingGroup.IgnoreScale ? 1 : entity.GetComponent<Positioned>()?.Scale;
if (scale == null && !matchingGroup.IgnoreBaseSize)
{
DebugWindow.LogError($"Unable to grab scale for entity {entity} {baseEntity}");
_rejectedCache.AddOrUpdate(entity, Stopwatch.StartNew());
continue;
}
if (matchingGroup.PlayAlert && soundAlertTimer.Elapsed > soundAlertCooldown && !soundAlertCache.Contains(entity.Id))
{
soundAlertTimer.Restart();
soundAlertCache.Add(entity.Id);
GameController.SoundController.PlaySound("alert.wav");
}
var finalRadius = 0f;
if (matchingGroup.IgnoreBaseSize)
{
finalRadius = matchingGroup.CustomSize;
}
else
{
finalRadius = baseRadius.Value * scale.Value * matchingGroup.CustomScale;
}
if (matchingGroup.CircleColor.Value.A > 0)
{
if (Settings.EnableDebugging)
Graphics.DrawText(baseEntity.Path.Split('/').Last(), GameController.IngameState.Camera.WorldToScreen(entity.Pos));
Graphics.DrawFilledCircleInWorld(entity.Pos, finalRadius, matchingGroup.CircleColor);
}
if (matchingGroup.BorderColor.Value.A > 0 && matchingGroup.BorderThickness.Value > 0)
{
Graphics.DrawCircleInWorld(entity.Pos, finalRadius, matchingGroup.BorderColor, matchingGroup.BorderThickness);
}
}
else
{
if (Settings.EnableDebugging)
DebugWindow.LogMsg($"EffectZone for Entity Path: {baseEntity.Path}");
if (Settings.CollectUnknownEffects)
{
if (!Settings.UnknownEffects.Content.Any(x => x.Value == baseEntity.Path))
{
Settings.UnknownEffects.Content.Add(new TextNode(baseEntity.Path));
}
}
_rejectedCache.AddOrUpdate(entity, Stopwatch.StartNew());
}
HandleLethalEntities(entity);
}
}
private bool PlayerInEffect(Entity entity)
{
if (!entity.TryGetComponent<Animated>(out var animated)) return false;
float? baseRadius = entity.TryGetComponent<GroundEffect>(out var effect) && effect.EffectDescription?.BaseSize is { } groundEffectSize
? groundEffectSize
: animated.MiscAnimated?.BaseSize;
if (baseRadius == null) return false;
return entity.DistancePlayer <= (baseRadius * WorldToGridConversion);
}
private void HandleLethalEntities(Entity entity)
{
if (GameController.Player.IsAlive)
{
entityCache.Add(entity);
}
else
{
var recentEntities = entityCache.GetAll();
foreach (var recentEntity in recentEntities.Where(PlayerInEffect))
{
var animated = entity.GetComponent<Animated>();
var baseEntity = animated.BaseAnimatedObjectEntity;
var path = baseEntity.Path;
if (!Settings.LethalUnknownEffects.Content.Any(x => x.Value == path))
{
Settings.LethalUnknownEffects.Content.Add(new TextNode(path));
}
}
}
}
}