-
Notifications
You must be signed in to change notification settings - Fork 4
/
Helper.cs
355 lines (306 loc) · 14.2 KB
/
Helper.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
using Blish_HUD;
using Blish_HUD.Controls.Extern;
using Blish_HUD.Gw2Mumble;
using Blish_HUD.Input;
using Blish_HUD.Modules.Managers;
using Blish_HUD.Settings;
using Gw2Sharp.Models;
using Gw2Sharp.WebApi.V2.Models;
using Manlaan.Mounts.Things;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Mounts.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Taimi.UndaDaSea_BlishHUD;
namespace Manlaan.Mounts
{
public class RangedThingUpdatedEvent : EventArgs
{
public Thing NewThing { get; set; }
public RangedThingUpdatedEvent(Thing newThing)
{
NewThing = newThing;
}
}
public class Helper
{
private static readonly Logger Logger = Logger.GetLogger<Helper>();
private Dictionary<string, Thing> StoredThingForLater = new Dictionary<string, Thing>();
private Thing _storedRangedThing;
public Thing StoredRangedThing {
get { return _storedRangedThing; }
set{
if (RangedThingUpdated != null)
{
RangedThingUpdated(this, new ValueChangedEventArgs<Thing>(_storedRangedThing, value));
}
Logger.Debug($"Setting {nameof(StoredRangedThing)} to: {value?.Name}");
_storedRangedThing = value;
}
}
public event EventHandler<ValueChangedEventArgs<Thing>> RangedThingUpdated;
public event EventHandler<ValueChangedEventArgs<Dictionary<string, Thing>>> StoredThingForLaterUpdated;
private readonly Dictionary<(Keys, ModifierKeys), SemaphoreSlim> _semaphores;
private float _lastZPosition = 0.0f;
private double _lastUpdateSeconds = 0.0f;
private bool _isPlayerGlidingOrFalling = false;
private Gw2ApiManager Gw2ApiManager;
private bool _isCombatLaunchUnlocked;
private DateTime lastTimeJumped = DateTime.MinValue;
public SkyLake _lake = null;
public Helper(Gw2ApiManager gw2ApiManager)
{
_semaphores = new Dictionary<(Keys, ModifierKeys), SemaphoreSlim>();
Gw2ApiManager = gw2ApiManager;
Module._debug.Add("StoreThingForLaterActivation", () => $"{string.Join(", ", StoredThingForLater.Select(x => x.Key + "=" + x.Value.Name).ToArray())}");
Module._debug.Add("Lake", () => $"name: {_lake?.Name} surface {_lake?.WaterSurface} Z {GameService.Gw2Mumble.PlayerCharacter.Position.Z} distance {_lake?.Distance}");
}
public bool IsCombatLaunchUnlocked()
{
return _isCombatLaunchUnlocked && Module._settingCombatLaunchMasteryUnlocked.Value;
}
public async Task IsCombatLaunchUnlockedAsync()
{
if (!Gw2ApiManager.HasPermissions(new List<TokenPermission> { TokenPermission.Progression }))
{
_isCombatLaunchUnlocked = false;
}
var masteries = await Gw2ApiManager.Gw2ApiClient.V2.Masteries.AllAsync();
_isCombatLaunchUnlocked = masteries.Any(m => m.Levels.Any(ml => ml.Name == "Combat Launch"));
}
public bool IsPlayerInWvwMap()
{
MapType[] warclawOnlyMaps = {
MapType.RedBorderlands,
MapType.BlueBorderlands,
MapType.GreenBorderlands,
MapType.EternalBattlegrounds,
MapType.Center,
MapType.WvwLounge
};
return Array.Exists(warclawOnlyMaps, mapType => mapType == GameService.Gw2Mumble.CurrentMap.Type);
}
public bool IsPlayerUnderWater()
{
var playerPosition = GameService.Gw2Mumble.PlayerCharacter.Position;
var waterSurface = GetRelevantWaterSurface(playerPosition);
return playerPosition.Z <= waterSurface-1.2;
}
private float GetRelevantWaterSurface(Vector3 playerPosition)
{
var currentMap = GameService.Gw2Mumble.CurrentMap.Id;
var lake = Module._skyLakes.Where(lake => lake.Map == currentMap).Where(lake => lake.IsNearby(playerPosition)).OrderBy(lake => lake.Distance).FirstOrDefault();
_lake = lake;
var waterSurface = 0f;
if (lake != null)
{
if (lake.IsInWater(playerPosition))
{
waterSurface = lake.WaterSurface;
}
}
return waterSurface;
}
public bool IsPlayerOnWaterSurface()
{
var playerPosition = GameService.Gw2Mumble.PlayerCharacter.Position;
var zpos = playerPosition.Z;
var waterSurface = GetRelevantWaterSurface(playerPosition);
return zpos > waterSurface - 1.2 && zpos < waterSurface;
}
public bool IsPlayerInCombat()
{
return GameService.Gw2Mumble.PlayerCharacter.IsInCombat;
}
public bool IsPlayerMounted()
{
return GameService.Gw2Mumble.PlayerCharacter.CurrentMount != Gw2Sharp.Models.MountType.None;
}
public bool IsPlayerGlidingOrFalling()
{
return _isPlayerGlidingOrFalling;
}
public void UpdateLastJumped()
{
if (!IsPlayerMounted())
{
lastTimeJumped = DateTime.UtcNow;
}
}
private bool DidPlayerJumpRecently()
{
return DateTime.UtcNow.Subtract(lastTimeJumped).TotalMilliseconds < 5000;
}
public void UpdatePlayerGlidingOrFalling(GameTime gameTime)
{
var currentZPosition = GameService.Gw2Mumble.PlayerCharacter.Position.Z;
var currentUpdateSeconds = gameTime.TotalGameTime.TotalSeconds;
var secondsDiff = currentUpdateSeconds - _lastUpdateSeconds;
var zPositionDiff = currentZPosition - _lastZPosition;
if (NewStuff(zPositionDiff, secondsDiff))
{
_lastZPosition = currentZPosition;
_lastUpdateSeconds = currentUpdateSeconds;
}
}
private bool NewStuff(float zPositionDiff, double secondsDiff)
{
var velocity = zPositionDiff / secondsDiff;
if (secondsDiff < Module._settingFallingOrGlidingUpdateFrequency.Value)
{
return false;
}
//Module._debug.Add("velocity", () => $"{velocity.ToString("N2")}");
Module._debug.Add("DidPlayerJumpRecently", () => $"{DidPlayerJumpRecently()}.");
if (velocity > 10 || velocity < -10)
_isPlayerGlidingOrFalling = true;
else if (DidPlayerJumpRecently() && velocity < -2)
_isPlayerGlidingOrFalling = true;
else
_isPlayerGlidingOrFalling = false;
return true;
}
private SemaphoreSlim GetOrCreateSemaphore(KeyBinding keyBinding)
{
lock (_semaphores)
{
if (!_semaphores.TryGetValue((keyBinding.PrimaryKey, keyBinding.ModifierKeys), out var semaphore))
{
semaphore = new SemaphoreSlim(1, 1);
_semaphores[(keyBinding.PrimaryKey, keyBinding.ModifierKeys)] = semaphore;
}
return semaphore;
}
}
public async Task TriggerKeybind(SettingEntry<KeyBinding> keybindingSetting, WhichKeybindToRun whichKeyBindToRun)
{
var semaphore = GetOrCreateSemaphore(keybindingSetting.Value);
await semaphore.WaitAsync();
try
{
Logger.Debug("TriggerKeybind entered");
if (whichKeyBindToRun == WhichKeybindToRun.Both || whichKeyBindToRun == WhichKeybindToRun.Press)
{
if (keybindingSetting.Value.ModifierKeys != ModifierKeys.None)
{
Logger.Debug($"TriggerKeybind press modifiers {keybindingSetting.Value.ModifierKeys}");
if (keybindingSetting.Value.ModifierKeys.HasFlag(ModifierKeys.Alt))
Blish_HUD.Controls.Intern.Keyboard.Press(VirtualKeyShort.MENU, false);
if (keybindingSetting.Value.ModifierKeys.HasFlag(ModifierKeys.Ctrl))
Blish_HUD.Controls.Intern.Keyboard.Press(VirtualKeyShort.CONTROL, false);
if (keybindingSetting.Value.ModifierKeys.HasFlag(ModifierKeys.Shift))
Blish_HUD.Controls.Intern.Keyboard.Press(VirtualKeyShort.SHIFT, false);
}
Logger.Debug($"TriggerKeybind press PrimaryKey {keybindingSetting.Value.PrimaryKey}");
Blish_HUD.Controls.Intern.Keyboard.Press(ToVirtualKey(keybindingSetting.Value.PrimaryKey), false);
await Task.Delay(50);
}
if (whichKeyBindToRun == WhichKeybindToRun.Both || whichKeyBindToRun == WhichKeybindToRun.Release)
{
Logger.Debug($"TriggerKeybind release PrimaryKey {keybindingSetting.Value.PrimaryKey}");
Blish_HUD.Controls.Intern.Keyboard.Release(ToVirtualKey(keybindingSetting.Value.PrimaryKey), false);
if (keybindingSetting.Value.ModifierKeys != ModifierKeys.None)
{
Logger.Debug($"TriggerKeybind release modifiers {keybindingSetting.Value.ModifierKeys}");
if (keybindingSetting.Value.ModifierKeys.HasFlag(ModifierKeys.Shift))
Blish_HUD.Controls.Intern.Keyboard.Release(VirtualKeyShort.SHIFT, false);
if (keybindingSetting.Value.ModifierKeys.HasFlag(ModifierKeys.Ctrl))
Blish_HUD.Controls.Intern.Keyboard.Release(VirtualKeyShort.CONTROL, false);
if (keybindingSetting.Value.ModifierKeys.HasFlag(ModifierKeys.Alt))
Blish_HUD.Controls.Intern.Keyboard.Release(VirtualKeyShort.MENU, false);
}
}
}
finally
{
semaphore.Release();
}
}
private static VirtualKeyShort ToVirtualKey(Keys key)
{
try
{
return (VirtualKeyShort)key;
}
catch
{
return new VirtualKeyShort();
}
}
internal Thing GetQueuedThing()
{
return Module._things.Where(m => m.QueuedTimestamp != null).OrderByDescending(m => m.QueuedTimestamp).FirstOrDefault();
}
internal async Task DoRangedThing()
{
if(StoredRangedThing != null)
{
var thing = StoredRangedThing;
Logger.Debug($"{nameof(DoRangedThing)} {thing?.Name}");
await thing?.DoAction(false, false);
StoredRangedThing = null;
}
}
internal void StoreThingForLaterActivation(Thing thing, string reason)
{
var characterName = GameService.Gw2Mumble.PlayerCharacter.Name;
Logger.Debug($"{nameof(StoreThingForLaterActivation)}: {thing.Name} for character: {characterName} with reason: {reason}");
StoredThingForLater[characterName] = thing;
if (StoredThingForLaterUpdated != null)
{
StoredThingForLaterUpdated(this, new ValueChangedEventArgs<Dictionary<string, Thing>>(null, StoredThingForLater));
}
}
internal Thing IsSomethingStoredForLaterActivation()
{
var characterName = GameService.Gw2Mumble.PlayerCharacter.Name;
StoredThingForLater.TryGetValue(characterName, out Thing result);
Logger.Debug($"{nameof(IsSomethingStoredForLaterActivation)} for character {characterName} : {result?.Name}");
return result;
}
internal void ClearSomethingStoredForLaterActivation()
{
var characterName = GameService.Gw2Mumble.PlayerCharacter.Name;
Logger.Debug($"{nameof(ClearSomethingStoredForLaterActivation)} for character: {characterName}");
StoredThingForLater.Remove(characterName);
if (StoredThingForLaterUpdated != null)
{
StoredThingForLaterUpdated(this, new ValueChangedEventArgs<Dictionary<string, Thing>>(null, StoredThingForLater));
}
}
internal async Task DoThingActionForLaterActivation()
{
var characterName = GameService.Gw2Mumble.PlayerCharacter.Name;
var thing = StoredThingForLater[characterName];
Logger.Debug($"{nameof(DoThingActionForLaterActivation)} {thing?.Name} for character: {characterName}");
await thing?.DoAction(false, false);
ClearSomethingStoredForLaterActivation();
}
internal ContextualRadialThingSettings GetApplicableContextualRadialThingSettings() => Module.ContextualRadialSettings.OrderBy(c => c.Order).FirstOrDefault(c => c.IsEnabled.Value && c.IsApplicable());
internal ContextualRadialThingSettings GetApplicableTriggeringContextualRadialThingSettings() => Module.ContextualRadialSettings.OrderBy(c => c.Order).FirstOrDefault(c => c.IsEnabled.Value && c.IsApplicable() && c.GetKeybind().Value.IsTriggering);
internal IEnumerable<RadialThingSettings> GetAllGenericRadialThingSettings()
{
var contextualRadialSettingsCasted = Module.ContextualRadialSettings.ConvertAll(x => (RadialThingSettings)x);
var userDefinedRadialSettingsCasted = Module.UserDefinedRadialSettings.ConvertAll(x => (RadialThingSettings)x);
return contextualRadialSettingsCasted.Concat(userDefinedRadialSettingsCasted);
}
internal RadialThingSettings GetTriggeredRadialSettings()
{
var contextual = GetApplicableTriggeringContextualRadialThingSettings();
if(contextual != null)
{
return contextual;
}
var userdefinedList = Module.UserDefinedRadialSettings.Where(s => s.GetKeybind().Value.IsTriggering);
if (userdefinedList.Count() == 1)
{
return userdefinedList.Single();
}
return null;
}
}
}