-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDangerMods.cs
275 lines (236 loc) · 10.2 KB
/
DangerMods.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using ExileCore2;
using ExileCore2.PoEMemory.Components;
using ExileCore2.PoEMemory.MemoryObjects;
using ExileCore2.Shared.Cache;
using ExileCore2.Shared.Enums;
using ExileCore2.Shared.Helpers;
using System.Numerics;
using System.IO;
namespace DangerMods {
public class DangerMods : BaseSettingsPlugin<DangerModsSettings> {
private HashSet<long> alertedEntities = new HashSet<long>();
private const string ALERT_SOUND_FILE = "danger.wav";
private List<string> cachedAlertModifiers;
private string lastModifierSettings = string.Empty;
private Dictionary<Entity, List<string>> currentAlerts = new Dictionary<Entity, List<string>>();
private readonly HashSet<string> observedModifiers = new HashSet<string>();
private string ObservedModifiersPath => Path.Combine(DirectoryFullName, Settings.ObservedModifiersFile.Value);
private DateTime lastSoundPlayed = DateTime.MinValue;
private const int SOUND_DELAY_MS = 1000; // One second delay between sounds
private List<string> GetAlertModifiers() {
var currentSettings = Settings.ModifiersToAlert.Value;
if (cachedAlertModifiers == null || currentSettings != lastModifierSettings) {
if (Settings.DebugMessages) {
LogMessage($"Parsing new modifiers: {currentSettings}");
}
cachedAlertModifiers = ParseAlertModifiers();
lastModifierSettings = currentSettings;
}
return cachedAlertModifiers;
}
private List<string> ParseAlertModifiers() {
return Settings.ModifiersToAlert.Value
.Split(',')
.Select(x => x.Trim().ToLower())
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToList();
}
public override void Render() {
if (!Settings.Enable || !Settings.ShowAlertText)
return;
// Always draw the fixed position alerts
DrawFixedPositionAlerts();
// Additionally draw healthbar-anchored alerts if enabled
if (Settings.AnchorToHealthbar) {
DrawHealthbarAnchoredAlerts();
}
}
private void DrawFixedPositionAlerts() {
var lineHeight = 25;
var position = new Vector2(Settings.AlertPositionX, Settings.AlertPositionY);
foreach (var alert in currentAlerts) {
// Format text with entity name and indented modifiers
var text = $"{alert.Key.RenderName}\n {string.Join("\n ", alert.Value)}";
var textSize = Graphics.MeasureText(text);
Graphics.DrawBox(
position,
position + textSize,
Color.FromArgb(180, 0, 0, 0)
);
Graphics.DrawText(
text,
position,
Color.Red,
Settings.Font.Value,
FontAlign.Left
);
position.Y += textSize.Y + lineHeight;
}
}
private void DrawHealthbarAnchoredAlerts() {
foreach (var alert in currentAlerts.ToList()) { // ToList to avoid modification during enumeration
var entity = alert.Key;
// Skip if entity is dead
if (!entity.IsAlive) {
currentAlerts.Remove(entity);
alertedEntities.Remove(entity.Id);
continue;
}
// Get world coordinates
var worldCoords = entity.Pos;
// Match HealthBars.cs Z-axis logic
if (entity.GetComponent<Render>()?.Bounds is { } boundsNum)
{
worldCoords.Z -= 2 * boundsNum.Z;
}
worldCoords.Z += Settings.HealthBarYOffset;
// Convert to screen coordinates
var screenCoords = GameController.IngameState.Camera.WorldToScreen(worldCoords);
if (screenCoords == Vector2.Zero) continue;
// Format text with entity name and indented modifiers
var text = $"{entity.RenderName}\n {string.Join("\n ", alert.Value)}";
var textSize = Graphics.MeasureText(text);
var position = new Vector2(
screenCoords.X - (textSize.X / 2),
screenCoords.Y
);
// Draw background
Graphics.DrawBox(
position,
position + textSize,
Color.FromArgb(180, 0, 0, 0)
);
// Draw text
Graphics.DrawText(
text,
position,
Color.Red,
Settings.Font.Value,
FontAlign.Left
);
}
}
public override void EntityAdded(Entity entity) {
if (!Settings.Enable)
return;
try {
if (!entity.IsAlive)
return;
var rarity = entity.GetComponent<ObjectMagicProperties>();
if (rarity == null || !rarity.Rarity.Equals(MonsterRarity.Rare))
return;
var mods = rarity.Mods;
if (mods == null)
return;
// Track new modifiers if enabled
if (Settings.TrackModifiers) {
bool newModifiersFound = false;
foreach (var mod in mods) {
var normalizedMod = mod.ToLower().Trim();
if (observedModifiers.Add(normalizedMod)) {
newModifiersFound = true;
if (Settings.DebugMessages) {
LogMessage($"New modifier found: {normalizedMod}");
}
}
}
if (newModifiersFound) {
SaveObservedModifiers();
}
}
// Existing alert logic
var alertModifiers = GetAlertModifiers();
var matchingMods = mods
.Where(mod => alertModifiers
.Any(alertMod => mod.Contains(alertMod, StringComparison.OrdinalIgnoreCase)))
.ToList();
if (matchingMods.Any()) {
currentAlerts[entity] = matchingMods;
if (Settings.EnableSoundAlerts && !alertedEntities.Contains(entity.Id)) {
PlayAlertSound(ALERT_SOUND_FILE);
alertedEntities.Add(entity.Id);
}
}
}
catch (Exception e) {
if (Settings.DebugMessages) {
DebugWindow.LogError(e.Message);
}
}
}
private void PlayAlertSound(string name) {
try {
// Check if enough time has passed since last sound
var now = DateTime.Now;
if ((now - lastSoundPlayed).TotalMilliseconds < SOUND_DELAY_MS) {
return;
}
lastSoundPlayed = now;
GameController.SoundController.PreloadSound(name);
GameController.SoundController.SetVolume(Settings.Volume.Value / 100f);
GameController.SoundController.PlaySound(name);
}
catch (Exception ex) {
DebugWindow.LogError($"Failed to play sound: {ex.Message}");
}
}
public override bool Initialise() {
LoadObservedModifiers();
Settings.PlayAlert.OnPressed += () => {
PlayAlertSound(ALERT_SOUND_FILE);
};
// Add handler for modifier settings changes
Settings.ModifiersToAlert.OnValueChanged += () => {
if (Settings.DebugMessages) {
LogMessage("ModifiersToAlert changed, refreshing caches...");
}
cachedAlertModifiers = null; // Force refresh of cached modifiers
currentAlerts.Clear(); // Clear current alerts to force re-check
alertedEntities.Clear(); // Clear alerted entities to allow new alerts
// Re-check all existing entities
foreach (var entity in GameController.EntityListWrapper.ValidEntitiesByType[EntityType.Monster]) {
EntityAdded(entity);
}
};
GameController.Area.OnAreaChange += OnAreaChange;
return true;
}
private void OnAreaChange(AreaInstance area) {
alertedEntities.Clear();
currentAlerts.Clear();
}
private void LoadObservedModifiers() {
try {
if (File.Exists(ObservedModifiersPath)) {
var lines = File.ReadAllLines(ObservedModifiersPath);
foreach (var line in lines) {
if (!string.IsNullOrWhiteSpace(line)) {
observedModifiers.Add(line.Trim().ToLower());
}
}
}
}
catch (Exception e) {
LogError($"Failed to load observed modifiers: {e.Message}");
}
}
private void SaveObservedModifiers() {
try {
File.WriteAllLines(ObservedModifiersPath,
observedModifiers.OrderBy(x => x).ToList());
}
catch (Exception e) {
LogError($"Failed to save observed modifiers: {e.Message}");
}
}
public override void EntityRemoved(Entity entity)
{
currentAlerts.Remove(entity);
alertedEntities.Remove(entity.Id);
}
}
}