forked from drekdrek/grinding-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grinding Stats.as
341 lines (302 loc) · 13.9 KB
/
Grinding Stats.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
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
// thanks to Loupphok for allowing me to use their account for testing MP4 stuff.
// thanks to dequubi for doing most of the annoying work of porting to TURBO
enum display_setting {
Only_when_Openplanet_menu_is_open,
Always_except_when_interface_is_hidden,
Always
}
[Setting name="Enabled" category="UI"]
bool setting_enabled = true;
[Setting name="Lock window location" category="UI"]
bool setting_lock_window_location = false;
[Setting name="Display setting" category="UI"]
display_setting setting_display = display_setting::Always_except_when_interface_is_hidden;
[Setting name="Show only one number" category="UI" description="Only one number is shown, instead of showing the same number twice."]
bool setting_show_only_one_number = false;
[Setting name="Show only one time" category="UI" description="Only one time is shown, instead of showing the same time twice."]
bool setting_show_only_one_time = false;
[Setting name="Show map name/author" category="UI"]
bool setting_show_map_name = true;
[Setting name="Show thousands" category="UI"]
bool setting_show_thousands = true;
[Setting name="Show hour if 0" category="UI"]
bool setting_show_hour_if_0 = true;
[Setting name="Show Total time" category="Stats"]
bool setting_show_total_time = true;
[Setting name="Show Session time" category="Stats"]
bool setting_show_session_time = true;
[Setting name="Show Session finishes" category="Stats"]
bool setting_show_finishes_session = true;
[Setting name="Show Total finishes" category="Stats"]
bool setting_show_finishes_total = true;
[Setting name="Show Session resets" category="Stats"]
bool setting_show_resets_session = true;
[Setting name="Show Total resets" category="Stats"]
bool setting_show_resets_total = true;
#if TMNEXT
[Setting name="Show Session respawns" category="Stats"]
bool setting_show_respawns_session = false;
[Setting name="Show Total respawns" category="Stats"]
bool setting_show_respawns_total = false;
#endif
uint finishes = 0;
uint resets = 0;
uint respawns = 0;
uint64 start_time = 0;
uint64 time = 0;
uint64 disabled_time = 0;
uint64 total_disabled_time = 0;
uint64 disabled_start_time = 0;
string map_id = "";
vec2 anchor = vec2(0,500);
Files file;
bool handled_timer = false;
bool handled_reset = false ;
bool handled_finish = false;
bool handled_respawn = false;
bool handled_pb = false;
bool handled_disabled_time = false;
bool handled_file = false;
bool loaded = false;
bool handled_save = true;
void file_handler() {
bool startup = true;
while (true) {
if (setting_enabled) {
#if TMNEXT
CGameCtnApp@ app = GetApp();
auto loadProgress = GetApp().LoadProgress;
auto playground = cast<CSmArenaClient>(app.CurrentPlayground);
map_id = (playground is null || playground.Map is null) ? "" : playground.Map.IdName;
auto rootmap = app.RootMap;
if (rootmap !is null && playground !is null && playground.GameTerminals.Length > 0) {
auto terminal = playground.GameTerminals[0];
auto gui_player = cast<CSmPlayer>(terminal.GUIPlayer);
if (gui_player !is null) {
auto script = gui_player.ScriptAPI;
auto post = script.Post;
if (startup || handled_save && post == CSmScriptPlayer::EPost::Char) {
startup = false;
handled_save = false;
file = Files(map_id);
loaded = false;
}
}
}
#elif MP4
auto app = GetApp();
auto playground = app.CurrentPlayground;
auto rootmap = app.RootMap;
map_id = (rootmap is null ) ? "" : rootmap.IdName;
if (rootmap !is null && playground !is null && playground.GameTerminals.Length > 0){
auto terminal = playground.GameTerminals[0];
auto gui_player = cast<CTrackManiaPlayer>(terminal.GUIPlayer);
if (gui_player !is null) {
auto script = gui_player.ScriptAPI;
auto race_state = script.RaceState;
if (startup || handled_save && race_state == CTrackManiaPlayer::ERaceState::BeforeStart) {
startup = false;
handled_save = false;
file = Files(map_id);
loaded = false;
}
}
}
#elif TURBO
// basically copy & paste of the MP4 code, the only change is
// RootMap becomes Challenge, and RaceState can be accessed
// directly through gui_player (which is also called ControlledPlayer)
auto app = GetApp();
auto playground = app.CurrentPlayground;
auto rootmap = app.Challenge;
map_id = (rootmap is null ) ? "" : rootmap.IdName;
if (rootmap !is null && playground !is null && playground.GameTerminals.Length > 0){
auto terminal = playground.GameTerminals[0];
auto gui_player = cast<CTrackManiaPlayer>(terminal.ControlledPlayer);
if (gui_player !is null) {
auto race_state = gui_player.RaceState;
if (startup || handled_save && race_state == CTrackManiaPlayer::ERaceState::BeforeStart) {
startup = false;
handled_save = false;
file = Files(map_id);
loaded = false;
}
}
}
#endif
#if TMNEXT||MP4||TURBO
// this peace of code was the same between all versions
// so i thought there was no reason to duplicate it
if (!handled_save && playground is null && !loaded){
handled_save = true;
loaded = true;
if (file !is null) {
save_time();
}
}
#endif
}
yield();
}
}
void Main() {
bool startup = true;
uint temp_respawns = 0;
startnew(file_handler);
while(true) {
auto app = GetApp();
auto playground = app.CurrentPlayground;
auto network = cast<CTrackManiaNetwork>(app.Network);
#if TMNEXT||MP4
auto map = app.RootMap;
#elif TURBO
auto map = app.Challenge;
#endif
if (startup) {
start_time = network.PlaygroundClientScriptAPI is null ? 0 : network.PlaygroundClientScriptAPI.GameTime;
startup = false;
}
if (!setting_enabled && !handled_disabled_time) {
handled_disabled_time = true;
disabled_start_time = network.PlaygroundClientScriptAPI.GameTime;
disabled_time = 0;
}
if (setting_enabled) {
if (handled_disabled_time) {
handled_disabled_time = false;
disabled_time = network.PlaygroundClientScriptAPI.GameTime;
total_disabled_time = total_disabled_time + (disabled_time - disabled_start_time);
}
if (map is null) {
handled_timer = false;
handled_reset = false;
handled_finish = false;
handled_respawn = false;
resets = 0;
finishes = 0;
respawns = 0;
total_disabled_time = 0;
}
if (map !is null) {
if (playground !is null && playground.GameTerminals.Length > 0) {
#if TMNEXT
auto terminal = playground.GameTerminals[0];
auto gui_player = cast<CSmPlayer>(terminal.GUIPlayer);
auto ui_sequence = terminal.UISequence_Current;
if (gui_player !is null) {
auto script = gui_player.ScriptAPI;
auto post = script.Post;
if (!handled_timer && post == CSmScriptPlayer::EPost::Char) {
start_time = network.PlaygroundClientScriptAPI.GameTime;
handled_timer = true;
resets--;
}
if (!handled_reset && post == CSmScriptPlayer::EPost::Char) {
handled_reset = true;
resets++;
file.set_resets(file.get_resets() + 1);
}
if (!handled_finish && ui_sequence == CGamePlaygroundUIConfig::EUISequence::Finish) {
handled_finish = true;
finishes++;
file.set_finishes(file.get_finishes() + 1);
}
if (script.Score.NbRespawnsRequested != temp_respawns && post != CSmScriptPlayer::EPost::Char) {
temp_respawns = script.Score.NbRespawnsRequested;
respawns++;
file.set_respawns(file.get_respawns() + 1);
}
if (handled_reset && post != CSmScriptPlayer::EPost::Char) {
handled_reset = false;
}
if (handled_finish && ui_sequence != CGamePlaygroundUIConfig::EUISequence::Finish) {
handled_finish = false;
}
}
#elif MP4
auto terminal = playground.GameTerminals[0];
auto gui_player = cast<CTrackManiaPlayer>(terminal.GUIPlayer);
if (gui_player !is null) {
auto script = gui_player.ScriptAPI;
auto race_state = script.RaceState;
if (!handled_timer && race_state == CTrackManiaPlayer::ERaceState::BeforeStart) {
start_time = network.PlaygroundClientScriptAPI.GameTime;
handled_timer = true;
if (finishes == 1) {
finishes--;
}
}
if (!handled_reset && race_state == CTrackManiaPlayer::ERaceState::BeforeStart) {
handled_reset = true;
resets++;
file.set_resets(file.get_resets() + 1);
}
if (!handled_finish && race_state == CTrackManiaPlayer::ERaceState::Finished) {
handled_finish = true;
finishes++;
file.set_finishes(file.get_finishes() + 1);
}
if (handled_reset && race_state != CTrackManiaPlayer::ERaceState::BeforeStart) {
handled_reset = false;
}
if (handled_finish && race_state != CTrackManiaPlayer::ERaceState::Finished) {
handled_finish = false;
}
}
#elif TURBO
auto terminal = playground.GameTerminals[0];
auto gui_player = cast<CTrackManiaPlayer>(terminal.ControlledPlayer);
auto ui_sequence = cast<CGamePlaygroundUIConfig>(network.PlaygroundClientScriptAPI.UI);
if (gui_player !is null) {
auto race_state = gui_player.RaceState;
if (!handled_timer && race_state == CTrackManiaPlayer::ERaceState::BeforeStart) {
start_time = network.PlaygroundClientScriptAPI.GameTime;
handled_timer = true;
if (finishes == 1) {
finishes--;
}
if (resets == 1) {
resets--;
}
}
if (!handled_reset && race_state == CTrackManiaPlayer::ERaceState::BeforeStart && ui_sequence.UISequence == CGamePlaygroundUIConfig::EUISequence::Playing) {
handled_reset = true;
resets++;
file.set_resets(file.get_resets() + 1);
}
if (!handled_finish && race_state == CTrackManiaPlayer::ERaceState::Finished && !network.PlaygroundClientScriptAPI.IsSpectator) {
handled_finish = true;
finishes++;
file.set_finishes(file.get_finishes() + 1);
}
if (handled_reset && race_state != CTrackManiaPlayer::ERaceState::BeforeStart && (race_state != CTrackManiaPlayer::ERaceState::Finished && !network.PlaygroundClientScriptAPI.IsSpectator)) {
handled_reset = false;
}
if (handled_finish && race_state != CTrackManiaPlayer::ERaceState::Finished) {
handled_finish = false;
}
}
#endif
}
}
}
yield();
}
}
void save_time() {
file.set_time(file.get_time() + time - start_time);
file.write_file();
}
void OnDestroyed() {
save_time();
}
void RenderSettings() {
if (UI::Button("Reset current map's data")) {
file.reset_file();
UI::ShowNotification("Reset current map's data",4000);
}
if (UI::Button("Reset all map data")) {
file.reset_all();
UI::ShowNotification("Reset all map data",4000);
}
}