-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDishonoredComponent.cs
468 lines (392 loc) · 17.3 KB
/
DishonoredComponent.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using LiveSplit.Model;
using LiveSplit.UI.Components;
using LiveSplit.UI;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Xml;
using System.Windows.Forms;
using System.Collections.Generic;
using static LiveSplit.Dishonored.GameMemory;
using System.Numerics;
namespace LiveSplit.Dishonored
{
class Speedup
{
public int Duration { get; set; }
public int Delay { get; set; } = 0;
public float X { get; set; } = float.NaN;
public float Y { get; set; } = float.NaN;
public float Z { get; set; } = float.NaN;
public float Tolerance { get; set; } = 1.0f;
public Speedup Followup { get; set; }
public int PlayerSpeedDelay { get; set; } = 0;
protected bool ApproxEqual(float value, float expected)
{
return float.IsNaN(expected) || (Math.Abs(value - expected) <= Tolerance);
}
public bool Matches(Vector3 pos)
{
return ApproxEqual(pos.X, X) && ApproxEqual(pos.Y, Y) && ApproxEqual(pos.Z, Z);
}
}
class MovieSpeedup : Speedup
{
public Movie Movie { get; set; }
public bool Matches(Movie movie, Vector3 pos)
{
return movie == Movie && Matches(pos);
}
}
class CutsceneSpeedup : Speedup
{
public Level Level { get; set; }
public int Count { get; set; } = 0;
private int _count = 0;
private bool _triggered = false;
public bool Matches(Level level, Vector3 pos)
{
if (!_triggered && level == Level && Matches(pos))
{
_triggered = true;
if (Count == 0)
{
return true;
}
else if (_count >= 0)
{
if (++_count == Count)
{
_count = -1;
return true;
}
else
{
Debug.WriteLine($"Incrementing cutscene position count level={level} pos={pos} counts=({_count} < {Count})");
}
}
}
return false;
}
public void OnReset()
{
_count = 0;
}
public void OnCutsceneEnd()
{
_triggered = false;
}
public void OnLoad()
{
// If a level is loaded and the `currentCount` has been incremented,
// we could be loading a save before or after the it was increment.
// There is no way to tell so we just disable the speedup.
if (_count > 0) {
_count = -1;
}
}
}
class LoadSpeedup : Speedup
{
public Level Level { get; set; }
public Level PreviousLevel { get; set; }
public bool Matches(Level level, Level previousLevel, Vector3 pos)
{
return level == Level && previousLevel == PreviousLevel && Matches(pos);
}
}
class DishonoredComponent : LogicComponent
{
public override string ComponentName => "Dishonored";
public DishonoredSettings Settings { get; }
private readonly TimerModel _timer;
private readonly GameMemory _gameMemory;
private readonly Timer _updateTimer;
private readonly Timer _speedupTimer;
private Speedup _pendingSpeedup;
private Speedup _currentSpeedup;
private long _elapsedTime = 0;
private int _timeMultiplier = 1;
private TimeSpan _lastTime;
private readonly List<MovieSpeedup> _movieSpeedups;
private readonly List<CutsceneSpeedup> _cutsceneSpeedups;
private readonly List<LoadSpeedup> _loadPositionSpeedups;
private readonly List<LoadSpeedup> _loadDelaySpeedups;
public DishonoredComponent(LiveSplitState state)
{
#if DEBUG
Debug.Listeners.Clear();
Debug.Listeners.Add(TimedTraceListener.Instance);
#endif
Settings = new DishonoredSettings();
_timer = new TimerModel { CurrentState = state };
_timer.CurrentState.OnStart += timer_OnStart;
_updateTimer = new Timer() { Interval = 15, Enabled = true };
_updateTimer.Tick += updateTimer_Tick;
// these are triggered after a (non-loading) movie finishes playing
_movieSpeedups = new List<MovieSpeedup>
{
new MovieSpeedup { Movie = Movie.Intro, Duration = 9200, X = -3908f, Z = -231f },
};
// these are triggered after an in-game cutscene starts
_cutsceneSpeedups = new List<CutsceneSpeedup>
{
new CutsceneSpeedup { Level = Level.Intro, Duration = 1600, X = 13276.1f, Y = 23073.2f, Z = 2636.3f, Tolerance = 0.1f },
new CutsceneSpeedup { Level = Level.Intro, Duration = 3700, X = 16242.7f, Y = 21620.0f, Z = 3354.9f, Tolerance = 0.1f },
new CutsceneSpeedup { Level = Level.Intro, Count = 2, Duration = 6350, X = 15591.5f, Y = 21025.2f, Z = 3425.3f, Tolerance = 0.1f },
};
// these are triggered after a delay after a load finishes
_loadDelaySpeedups = new List<LoadSpeedup>
{
new LoadSpeedup { Level = Level.Prison, PreviousLevel = Level.Intro, Duration = 720, Delay = 500 },
new LoadSpeedup { Level = Level.FloodedStreets, PreviousLevel = Level.FloodedIntro, Duration = 1600, Delay = 1500 },
new LoadSpeedup { Level = Level.FloodedStreets, PreviousLevel = Level.FloodedRefinery, Duration = 1600, Delay = 1500, X = -5397.104f },
};
// these are triggered after hitting coordinates after a load finishes
_loadPositionSpeedups = new List<LoadSpeedup>
{
new LoadSpeedup { Level = Level.PubDay, PreviousLevel = Level.Sewers, Duration = 5640, Y = -7993.3f, Z = -598.1f, Tolerance = 3.0f },
new LoadSpeedup { Level = Level.CampbellStreets, PreviousLevel = Level.PubDusk, Duration = 4250, X = 12809f },
new LoadSpeedup { Level = Level.PubMorning, PreviousLevel = Level.CampbellBack, Duration = 2050, X = -2369.2f, Z = -600.6f },
new LoadSpeedup { Level = Level.CatStreets, PreviousLevel = Level.PubDay, Duration = 4550, X = 4460.5f, Z = 1849.5f },
new LoadSpeedup { Level = Level.PubDusk, PreviousLevel = Level.CatStreets, Duration = 5050, X = -11185f, Tolerance = 0.5f,
Followup = new Speedup { Duration = 1400, Delay = 8000,
Followup = new Speedup { Duration = 900, Delay = 12100 } } },
new LoadSpeedup { Level = Level.Bridge1, PreviousLevel = Level.PubDusk, Duration = 4040, X = -12312f, Z = -583f },
new LoadSpeedup { Level = Level.PubNight, PreviousLevel = Level.Bridge4, Duration = 4000, X = -11180.5f, Z = -583.3f },
new LoadSpeedup { Level = Level.BoyleExterior, PreviousLevel = Level.PubDay, Duration = 2500, X = -9340f, Z = -1951.4f },
new LoadSpeedup { Level = Level.PubMorning, PreviousLevel = Level.BoyleExterior, Duration = 3130, Y = -9403f, Tolerance = 4f },
new LoadSpeedup { Level = Level.TowerReturnYard, PreviousLevel = Level.PubMorning, Duration = 5250, X = -8714f, Y = 33057f },
new LoadSpeedup { Level = Level.PubDusk, PreviousLevel = Level.TowerReturnYard, Duration = 3340, Y = -10615f, Z = -583.1f, Tolerance = 2f },
new LoadSpeedup { Level = Level.FloodedIntro, PreviousLevel = Level.PubDusk, Duration = 5000, X = -23249f, Tolerance = 0.5f, PlayerSpeedDelay = 2000,
Followup = new Speedup { Duration = 6850, Delay = 7850 } },
new LoadSpeedup { Level = Level.KingsparrowIsland, PreviousLevel = Level.Loyalists, Duration = 4550, Y = 18200f, Z = 1040.8f, Tolerance = 3.0f },
new LoadSpeedup { Level = Level.KingsparrowLighthouse, PreviousLevel = Level.KingsparrowIsland, Duration = 900, Z = 1060f, Tolerance = 10f },
};
_speedupTimer = new Timer() { Enabled = false };
_speedupTimer.Tick += speedupTimer_Tick;
_gameMemory = new GameMemory();
_gameMemory.OnFirstLevelLoading += gameMemory_OnFirstLevelLoading;
_gameMemory.OnPlayerGainedControl += gameMemory_OnPlayerGainedControl;
_gameMemory.OnCutsceneEnd += gameMemory_OnCutsceneEnd;
_gameMemory.OnLoadStarted += gameMemory_OnLoadStarted;
_gameMemory.OnLoadFinished += gameMemory_OnLoadFinished;
_gameMemory.OnPlayerLostControl += gameMemory_OnPlayerLostControl;
_gameMemory.OnAreaCompleted += gameMemory_OnAreaCompleted;
_gameMemory.OnPostMoviePlayerPositionChanged += gameMemory_OnPostMoviePlayerPositionChanged;
_gameMemory.OnPostCutscenePlayerPositionChanged += gameMemory_OnPostCutscenePlayerPositionChanged;
_gameMemory.OnPostLoadPlayerPositionChanged += gameMemory_OnPostLoadPlayerPositionChanged;
}
public override void Dispose()
{
_timer.CurrentState.OnStart -= timer_OnStart;
_updateTimer?.Dispose();
_speedupTimer?.Dispose();
_gameMemory?.Dispose();
}
void updateTimer_Tick(object sender, EventArgs eventArgs)
{
try
{
_gameMemory.Update();
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
var now = _timer.CurrentState.CurrentTime.RealTime;
if (now != null)
{
if (!_timer.CurrentState.IsGameTimePaused && Settings.EnableSpeedups)
{
// when speeding up the game, also speed up IGT in the timer
_elapsedTime += ((TimeSpan)now - _lastTime).Ticks * _timeMultiplier;
_timer.CurrentState.SetGameTime(new TimeSpan(_elapsedTime));
}
_lastTime = (TimeSpan)now;
}
}
void speedupTimer_Tick(object sender, EventArgs eventArgs)
{
_speedupTimer.Stop();
if (_pendingSpeedup != null)
{
TriggerSpeedup(_pendingSpeedup);
}
else
{
EndSpeedup();
}
}
void timer_OnStart(object sender, EventArgs e)
{
_timer.InitializeGameTime();
_elapsedTime = 0;
foreach (var speedup in _cutsceneSpeedups)
{
speedup.OnReset();
}
}
public override void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
}
void gameMemory_OnFirstLevelLoading(object sender, EventArgs e)
{
if (Settings.AutoReset && (!Settings.ResetProtection || _timer.CurrentState.CurrentSplitIndex == 0))
_timer.Reset();
}
void gameMemory_OnPlayerGainedControl(object sender, EventArgs e)
{
if (Settings.AutoStart)
_timer.Start();
}
void gameMemory_OnCutsceneEnd(object sender, EventArgs e)
{
foreach (var speedup in _cutsceneSpeedups)
{
speedup.OnCutsceneEnd();
}
}
void gameMemory_OnLoadStarted(object sender, EventArgs e)
{
_timer.CurrentState.IsGameTimePaused = true;
foreach (var speedup in _cutsceneSpeedups)
{
speedup.OnLoad();
}
// must be a manual load, just let the player deal with it
if (_speedupTimer.Enabled)
{
EndSpeedup(true);
_speedupTimer.Stop();
_pendingSpeedup = null;
}
}
void gameMemory_OnLoadFinished(object sender, Level level, Level previousLevel, Movie movie, Vector3 pos)
{
_timer.CurrentState.IsGameTimePaused = false;
if (!Settings.EnableSpeedups || !Settings.SpeedupLoadDelays || _speedupTimer.Enabled)
return;
var speedup = _loadDelaySpeedups.Find(cs => cs.Matches(level, previousLevel, pos));
if (speedup != null)
{
Debug.WriteLine($"Found load delay speedup level={previousLevel}->{level} pos={pos}");
DelaySpeedup(speedup);
}
}
void gameMemory_OnPlayerLostControl(object sender, EventArgs e)
{
if (Settings.AutoSplitEmily)
_timer.Split();
}
void gameMemory_OnAreaCompleted(object sender, AreaCompletionType type)
{
if ((type == AreaCompletionType.IntroEnd && Settings.AutoSplitIntroEnd)
|| (type == AreaCompletionType.MissionEnd && Settings.AutoSplitMissionEnd)
|| (type == AreaCompletionType.PrisonEscape && Settings.AutoSplitPrisonEscape)
|| (type == AreaCompletionType.OutsidersDream && Settings.AutoSplitOutsidersDream)
|| (type == AreaCompletionType.Weepers && Settings.AutoSplitWeepers)
|| (type == AreaCompletionType.DLC06IntroEnd && Settings.AutoSplitDLC06IntroEnd))
{
_timer.Split();
}
}
void gameMemory_OnPostMoviePlayerPositionChanged(object sender, Movie movie, Vector3 pos)
{
if (!Settings.EnableSpeedups || !Settings.SpeedupMovies || _speedupTimer.Enabled)
return;
var speedup = _movieSpeedups.Find(ps => ps.Matches(movie, pos));
if (speedup != null)
{
Debug.WriteLine($"Found movie position speedup movie={movie} pos={pos}");
DelaySpeedup(speedup);
}
}
void gameMemory_OnPostCutscenePlayerPositionChanged(object sender, Level level, Vector3 pos)
{
if (!Settings.EnableSpeedups || !Settings.SpeedupInGameCutscenes || _speedupTimer.Enabled)
return;
var speedup = _cutsceneSpeedups.Find(ps => ps.Matches(level, pos));
if (speedup != null)
{
Debug.WriteLine($"Found cutscene position speedup level={level} pos={pos}");
DelaySpeedup(speedup);
}
}
void gameMemory_OnPostLoadPlayerPositionChanged(object sender, Level level, Level previousLevel, Vector3 pos)
{
if (!Settings.EnableSpeedups || !Settings.SpeedupLoadPositions || _speedupTimer.Enabled)
return;
var speedup = _loadPositionSpeedups.Find(ps => ps.Matches(level, previousLevel, pos));
if (speedup != null)
{
Debug.WriteLine($"Found load position speedup level={previousLevel}->{level} pos={pos}");
DelaySpeedup(speedup);
}
}
void DelaySpeedup(Speedup speedup)
{
if (!Settings.EnableSpeedups || _speedupTimer.Enabled)
return;
if (speedup.Delay <= 0)
{
TriggerSpeedup(speedup);
return;
}
Debug.WriteLine($"Delaying speedup for {speedup.Delay}ms");
_pendingSpeedup = speedup;
_speedupTimer.Interval = speedup.Delay;
_speedupTimer.Start();
}
void TriggerSpeedup(Speedup speedup)
{
_pendingSpeedup = null;
if (!Settings.EnableSpeedups || _speedupTimer.Enabled || speedup.Duration <= 0)
return;
Debug.WriteLine($"Triggering speedup for {speedup.Duration}ms");
_timeMultiplier = 10;
_gameMemory.SetSpeed(_timeMultiplier, speedup.PlayerSpeedDelay);
_speedupTimer.Interval = speedup.Duration;
_speedupTimer.Start();
_currentSpeedup = speedup;
}
void EndSpeedup(bool stopAll = false)
{
Debug.WriteLine("Ending active speedup, if any");
_timeMultiplier = 1;
_gameMemory.SetSpeed(_timeMultiplier);
if (!stopAll && _currentSpeedup != null && _currentSpeedup.Followup != null)
DelaySpeedup(_currentSpeedup.Followup);
_currentSpeedup = null;
}
public override XmlNode GetSettings(XmlDocument document)
{
return Settings.GetSettings(document);
}
public override Control GetSettingsControl(LayoutMode mode)
{
return Settings;
}
public override void SetSettings(XmlNode settings)
{
Settings.SetSettings(settings);
}
}
public class TimedTraceListener : DefaultTraceListener
{
private static TimedTraceListener _instance;
public static TimedTraceListener Instance => _instance ?? (_instance = new TimedTraceListener());
private TimedTraceListener() { }
public int UpdateCount
{
[MethodImpl(MethodImplOptions.Synchronized)]
get;
[MethodImpl(MethodImplOptions.Synchronized)]
set;
}
public override void WriteLine(string message)
{
base.WriteLine("Dishonored: " + UpdateCount + " " + message);
}
}
}