forked from aldelaro5/LiveSplit.BugFables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiveSplitLogic.cs
242 lines (208 loc) · 6.25 KB
/
LiveSplitLogic.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
using LiveSplit.BugFables.UI;
using System;
namespace LiveSplit.BugFables
{
public class LiveSplitLogic
{
private enum EndTimeState
{
NotArrivedYet,
ArrivedInRoom,
SongLevelUpStarting,
SongLevelIsPlaying,
SongIsFading
}
private EndTimeState currentEndTimeState = EndTimeState.NotArrivedYet;
private GameMemory gameMemory = new GameMemory();
private bool oldListeningToTitleSong = false;
private byte[] oldEnemyEncounter = null;
private Split[] splits;
private SettingsUserControl settings;
public LiveSplitLogic(GameMemory gameMemory, SettingsUserControl settings)
{
this.gameMemory = gameMemory;
this.settings = settings;
InitSplits();
}
private void InitSplits()
{
splits = settings.GetSplitsGlitchless();
}
public bool ShouldStart()
{
int currentSong;
byte[] flags;
try
{
if (!gameMemory.ReadFirstMusicId(out currentSong))
return false;
if (!gameMemory.ReadFlags(out flags))
return false;
}
catch (Exception)
{
return false;
}
bool shouldStart = false;
bool newListeningToTitleSong = (currentSong == (int)GameEnums.Song.Title);
if (oldListeningToTitleSong && !newListeningToTitleSong)
{
shouldStart = BitConverter.ToBoolean(flags, (int)GameEnums.Flag.NewGameStarted);
}
oldListeningToTitleSong = newListeningToTitleSong;
return shouldStart;
}
public bool ShouldSplit(int currentSplitIndex, int currentRunSplitsCount)
{
if (settings.Mode != SettingsUserControl.AutoSplitterMode.StartEndOnly &&
currentSplitIndex != currentRunSplitsCount - 1)
{
return ShouldMidSplit(currentSplitIndex);
}
else
{
return ShouldEnd();
}
}
private bool ShouldMidSplit(int currentSplitIndex)
{
if (splits.Length - 1 < currentSplitIndex)
return false;
int currentRoomId;
byte[] flags;
byte[] enemyEncounter;
long battlePtr;
try
{
if (!gameMemory.ReadCurrentRoomId(out currentRoomId))
return false;
if (!gameMemory.ReadFlags(out flags))
return false;
if (!gameMemory.ReadEnemyEncounter(out enemyEncounter))
return false;
if (!gameMemory.ReadBattlePtr(out battlePtr))
return false;
}
catch (Exception)
{
return false;
}
if (oldEnemyEncounter == null)
{
oldEnemyEncounter = new byte[GameMemory.nbrBytesEnemyEncounter];
enemyEncounter.CopyTo(oldEnemyEncounter, 0);
}
Split split = splits[currentSplitIndex];
if (!MidSplitRoomCheck(split, currentRoomId))
return false;
if (!MidSplitFlagsCheck(split, flags))
return false;
if (!MidSplitEnemyDefeatedCheck(split, enemyEncounter, battlePtr))
return false;
enemyEncounter.CopyTo(oldEnemyEncounter, 0);
return true;
}
private bool MidSplitRoomCheck(Split split, int currentRoomId)
{
return (split.requiredRoom == GameEnums.Room.UNASSIGNED ||
currentRoomId == (int)split.requiredRoom);
}
private bool MidSplitFlagsCheck(Split split, byte[] flags)
{
if (split.requiredFlags != null)
{
if (split.requiredFlags.Length != 0)
{
bool allFlagsTrue = true;
foreach (var requiredFlag in split.requiredFlags)
{
if (!BitConverter.ToBoolean(flags, (int)requiredFlag))
{
allFlagsTrue = false;
break;
}
}
return allFlagsTrue;
}
}
return true;
}
private bool MidSplitEnemyDefeatedCheck(Split split, byte[] enemyEncounter, long battlePtr)
{
if (split.requiredEnemiesDefeated != null)
{
if (split.requiredEnemiesDefeated.Length != 0)
{
if (battlePtr != 0)
return false;
bool allEnemiesDefeated = true;
foreach (var requiredEnemy in split.requiredEnemiesDefeated)
{
int newNbrDefeated = gameMemory.GetNbrDefeatedForEnemyId(enemyEncounter, requiredEnemy);
int oldNbrDefeated = gameMemory.GetNbrDefeatedForEnemyId(oldEnemyEncounter, requiredEnemy);
if (newNbrDefeated <= oldNbrDefeated)
{
allEnemiesDefeated = false;
break;
}
}
return allEnemiesDefeated;
}
}
return true;
}
private bool ShouldEnd()
{
int currentSong;
long musicCoroutine;
int currentRoomId;
try
{
if (!gameMemory.ReadFirstMusicId(out currentSong))
return false;
if (!gameMemory.ReadMusicCoroutineInProgress(out musicCoroutine))
return false;
if (!gameMemory.ReadCurrentRoomId(out currentRoomId))
return false;
}
catch (Exception)
{
return false;
}
if (currentEndTimeState == EndTimeState.NotArrivedYet)
{
if (currentRoomId == (int)GameEnums.Room.BugariaEndThrone)
currentEndTimeState = EndTimeState.ArrivedInRoom;
}
else
{
bool newMusicCouroutineInProgress = (musicCoroutine != 0);
if (currentEndTimeState == EndTimeState.ArrivedInRoom && currentSong == (int)GameEnums.Song.LevelUp)
{
currentEndTimeState = EndTimeState.SongLevelUpStarting;
}
else if (currentEndTimeState == EndTimeState.SongLevelUpStarting && !newMusicCouroutineInProgress)
{
currentEndTimeState = EndTimeState.SongLevelIsPlaying;
}
else if (currentEndTimeState == EndTimeState.SongLevelIsPlaying && newMusicCouroutineInProgress)
{
currentEndTimeState = EndTimeState.SongIsFading;
}
else if (currentEndTimeState == EndTimeState.SongIsFading && !newMusicCouroutineInProgress)
{
currentEndTimeState = EndTimeState.NotArrivedYet;
return true;
}
}
return false;
}
public void ResetLogic()
{
oldListeningToTitleSong = false;
oldEnemyEncounter = null;
currentEndTimeState = EndTimeState.NotArrivedYet;
InitSplits();
}
}
}