-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCheckpointLib.as
269 lines (239 loc) · 9.75 KB
/
CheckpointLib.as
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
/*
* author: Phlarx
*/
namespace CP {
bool _inGame = false;
bool _strictMode = true;
string _curMapId = "";
uint _curCP = 0;
int _curCPLapTime = 0;
int _curCPRaceTime = 0;
uint _curLap = 0;
uint _maxCP = 0;
uint _maxLap = 0;
bool get_inGame() property { return _inGame; }
bool get_strictMode() property { return _strictMode; }
string get_curMapId() property { return _curMapId; }
uint get_curCP() property { return _curCP; }
int get_curCPLapTime() property { return _curCPLapTime; }
int get_curCPRaceTime() property { return _curCPRaceTime; }
uint get_curLap() property { return _curLap; }
uint get_maxCP() property { return _maxCP; }
uint get_maxLap() property { return _maxLap; }
#if TMNEXT
uint _preCPIdx = 0;
uint _preLapStartTime = 0;
#endif
void Main() {
#if TMNEXT && DEPENDENCY_PLAYERSTATE
print("CheckpointCounter lib is using PlayerState for checkpoint data");
#elif TMNEXT
print("CheckpointCounter lib is not using PlayerState for checkpoint data");
#endif
}
/*
* Update detects map changes, and re-counts CPs when it occurs.
* Additionally, it updates the CP/Lap info while a race is underway.
*/
void Update() {
#if TMNEXT
auto playground = cast<CSmArenaClient>(GetApp().CurrentPlayground);
if(playground is null
|| playground.Arena is null
|| playground.Map is null
|| playground.GameTerminals.Length <= 0
|| playground.GameTerminals[0].UISequence_Current != CGamePlaygroundUIConfig::EUISequence::Playing
|| cast<CSmPlayer>(playground.GameTerminals[0].GUIPlayer) is null) {
_inGame = false;
return;
}
auto player = cast<CSmPlayer>(playground.GameTerminals[0].GUIPlayer);
auto scriptPlayer = cast<CSmPlayer>(playground.GameTerminals[0].GUIPlayer).ScriptAPI;
if(scriptPlayer is null) {
_inGame = false;
return;
}
if(player.CurrentLaunchedRespawnLandmarkIndex == uint(-1)) {
// sadly, can't see CPs of spectated players any more
_inGame = false;
return;
}
MwFastBuffer<CGameScriptMapLandmark@> landmarks = playground.Arena.MapLandmarks;
if(!_inGame && (_curMapId != playground.Map.IdName || GetApp().Editor !is null)) {
// keep the previously-determined CP data, unless in the map editor
_curMapId = playground.Map.IdName;
_curCP = 0;
_maxCP = 0;
_curLap = 0;
_maxLap = playground.Map.TMObjective_IsLapRace ? playground.Map.TMObjective_NbLaps : 1;
_strictMode = true;
array<int> links = {};
for(uint i = 0; i < landmarks.Length; i++) {
if(landmarks[i].Waypoint !is null && !landmarks[i].Waypoint.IsFinish && !landmarks[i].Waypoint.IsMultiLap) {
// we have a CP, but we don't know if it is Linked or not
if(landmarks[i].Tag == "Checkpoint") {
_maxCP += 1;
} else if(landmarks[i].Tag == "LinkedCheckpoint") {
if(links.Find(landmarks[i].Order) < 0) {
_maxCP += 1;
links.InsertLast(landmarks[i].Order);
}
} else {
// this waypoint looks like a CP, acts like a CP, but is not called a CP.
if(_strictMode) {
warn("The current map, " + string(playground.Map.MapName) + " (" + playground.Map.IdName + "), is not compliant with checkpoint naming rules."
+ " If the CP count for this map is inaccurate, please report this map on the GitHub issues page:"
+ " https://github.com/Phlarx/tm-checkpoint-counter/issues");
}
_maxCP += 1;
_strictMode = false;
}
}
}
}
_inGame = true;
#if DEPENDENCY_PLAYERSTATE
// PlayerState is heavier, but allows detecting multiple CPs in one frame, as well as getting lap times
if(PlayerState::GetRaceData().PlayerState == PlayerState::EPlayerState::EPlayerState_Driving) {
auto info = PlayerState::GetRaceData().dPlayerInfo;
_curCP = info.NumberOfCheckpointsPassed;
if(info.LatestCPTime > 0) {
// LatestCPTime currently only exists for 1 frame
_curCPLapTime = info.LatestCPTime - info.LapStartTime;
_curCPRaceTime = info.LatestCPTime;
}
_curLap = info.CurrentLapNumber;
} else {
_curCP = 0;
_curCPLapTime = 0;
_curCPRaceTime = 0;
_curLap = 0;
}
#else
// The original method
if(_preCPIdx != player.CurrentLaunchedRespawnLandmarkIndex && landmarks.Length > player.CurrentLaunchedRespawnLandmarkIndex) {
_preCPIdx = player.CurrentLaunchedRespawnLandmarkIndex;
if(landmarks[_preCPIdx].Waypoint is null || landmarks[_preCPIdx].Waypoint.IsFinish || landmarks[_preCPIdx].Waypoint.IsMultiLap) {
// if null, it's a start block. if the other flags, it's either a multilap or a finish.
// in all such cases, we reset the completed cp count to zero.
_curCP = 0;
} else {
_curCP++;
}
}
#endif
#elif TURBO
auto playground = cast<CTrackManiaRaceNew>(GetApp().CurrentPlayground);
auto playgroundScript = cast<CTrackManiaRaceRules>(GetApp().PlaygroundScript);
if(playground is null
|| playgroundScript is null
|| playground.GameTerminals.Length <= 0
|| cast<CTrackManiaPlayer>(playground.GameTerminals[0].ControlledPlayer) is null) {
_inGame = false;
return;
}
auto player = cast<CTrackManiaPlayer>(playground.GameTerminals[0].ControlledPlayer);
if(player is null
|| player.CurLap is null
|| player.RaceState != CTrackManiaPlayer::ERaceState::Running) {
_inGame = false;
return;
}
/* Turbo doesn't support linked checkpoints, so this is sufficient */
_maxCP = playgroundScript.MapCheckpointPos.Length;
_maxLap = playgroundScript.MapNbLaps;
_inGame = true;
/* Checkpoints gains the time value of each CP as it is passed */
_curCP = player.CurLap.Checkpoints.Length;
_curCPLapTime = player.CurLap.Checkpoints.Length > 0 ? player.CurLap.Checkpoints[player.CurLap.Checkpoints.Length - 1] : 0;
_curCPRaceTime = player.CurRace.Checkpoints.Length > 0 ? player.CurRace.Checkpoints[player.CurRace.Checkpoints.Length - 1] : 0;
_curLap = player.CurrentNbLaps;
#elif MP4
auto playground = cast<CTrackManiaRaceNew>(GetApp().CurrentPlayground);
auto rootMap = GetApp().RootMap;
if(playground is null
|| rootMap is null
|| playground.GameTerminals.Length <= 0
|| cast<CTrackManiaPlayer>(playground.GameTerminals[0].GUIPlayer) is null) {
_inGame = false;
return;
}
auto scriptPlayer = cast<CTrackManiaPlayer>(playground.GameTerminals[0].GUIPlayer).ScriptAPI;
if(scriptPlayer is null
|| scriptPlayer.CurLap is null
|| scriptPlayer.RaceState != CTrackManiaPlayer::ERaceState::Running) {
_inGame = false;
return;
}
/* GetApp().PlaygroundScript.MapCheckpointPos.Length would be easier, but incorrect for linked CPs */
if(!_inGame && (_curMapId != rootMap.IdName || GetApp().Editor !is null)) {
// keep the previously-determined CP data, unless in the map editor
_curMapId = rootMap.IdName;
_curCP = 0;
_maxCP = 0;
_curLap = 0;
_maxLap = rootMap.TMObjective_NbLaps;
_strictMode = true;
array<int> links = {};
for(uint i = 0; i < rootMap.Blocks.Length; i++) {
if(rootMap.Blocks[i].WaypointSpecialProperty !is null && rootMap.Blocks[i].BlockInfo !is null) {
auto tag = rootMap.Blocks[i].WaypointSpecialProperty.Tag;
auto type = rootMap.Blocks[i].BlockInfo.WaypointType;
if(type == CGameCtnBlockInfo::EWayPointType::Checkpoint) {
// we have a CP, but we don't know if it is Linked or not
if(tag == "Checkpoint" || tag == "Goal") {
_maxCP++;
} else if(tag == "LinkedCheckpoint") {
if(links.Find(rootMap.Blocks[i].WaypointSpecialProperty.Order) < 0) {
_maxCP++;
links.InsertLast(rootMap.Blocks[i].WaypointSpecialProperty.Order);
}
} else {
// this waypoint looks like a CP, acts like a CP, but is not called a CP.
if(_strictMode) {
warn("The current map, " + string(rootMap.MapName) + " (" + rootMap.IdName + "), is not compliant with checkpoint naming rules."
+ " If the CP count for this map is inaccurate, please report this map on the GitHub issues page:"
+ " https://github.com/Phlarx/tm-checkpoint-counter/issues");
}
_maxCP++;
_strictMode = false;
}
}
}
}
for(uint i = 0; i < rootMap.AnchoredObjects.Length; i++) {
if(rootMap.AnchoredObjects[i].WaypointSpecialProperty !is null) {
auto tag = rootMap.AnchoredObjects[i].WaypointSpecialProperty.Tag;
auto type = rootMap.AnchoredObjects[i].ItemModel.WaypointType;
if(type == CGameItemModel::EnumWaypointType::Checkpoint) {
// we have a CP, but we don't know if it is Linked or not
if(tag == "Checkpoint" || tag == "Goal") {
_maxCP++;
} else if(tag == "LinkedCheckpoint") {
if(links.Find(rootMap.AnchoredObjects[i].WaypointSpecialProperty.Order) < 0) {
_maxCP++;
links.InsertLast(rootMap.AnchoredObjects[i].WaypointSpecialProperty.Order);
}
} else {
// this waypoint looks like a CP, acts like a CP, but is not called a CP.
if(_strictMode) {
warn("The current map, " + string(rootMap.MapName) + " (" + rootMap.IdName + "), is not compliant with checkpoint naming rules."
+ " If the CP count for this map is inaccurate, please report this map on the GitHub issues page:"
+ " https://github.com/Phlarx/tm-checkpoint-counter/issues");
}
_maxCP++;
_strictMode = false;
}
}
}
}
}
_inGame = true;
/* Checkpoints gains the time value of each CP as it is passed */
_curCP = scriptPlayer.CurLap.Checkpoints.Length;
_curCPLapTime = Math::Max(0, scriptPlayer.CurCheckpointLapTime);
_curCPRaceTime = Math::Max(0, scriptPlayer.CurCheckpointRaceTime);
_curLap = scriptPlayer.CurrentNbLaps;
#endif
}
}