-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlivesplit64.lua
More file actions
1439 lines (1280 loc) · 51.7 KB
/
livesplit64.lua
File metadata and controls
1439 lines (1280 loc) · 51.7 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- name: ! LiveSplit 64
-- description: LiveSplit + an auto splitter for coop
-- pausable: false
-- Edit `livesplit64.sav` in your appdata folder to configure splits.
-- You can only fit as much into a configuration as mod storage supports
local math_min, math_max, math_floor, math_ceil, math_abs, math_sqrt, coss, sins, atan2s, vec3f_copy, vec3f_normalize, vec3f_dot, vec3f_mul, vec3f_dif, vec3f_length, vec3f_rotate_zxy, clamp =
math.min, math.max, math.floor, math.ceil, math.abs, math.sqrt, coss, sins, atan2s, vec3f_copy, vec3f_normalize, vec3f_dot, vec3f_mul, vec3f_dif, vec3f_length, vec3f_rotate_zxy, clamp
local string_format = string.format
local pairs, ipairs, djui_hud_print_text, djui_hud_render_rect, djui_hud_set_color, network_local_index_from_global, network_global_index_from_local, network_is_server, djui_hud_measure_text =
pairs, ipairs, djui_hud_print_text, djui_hud_render_rect, djui_hud_set_color, network_local_index_from_global, network_global_index_from_local, network_is_server, djui_hud_measure_text
local djui_hud_set_resolution = djui_hud_set_resolution
local djui_hud_get_screen_width = djui_hud_get_screen_width
local djui_hud_get_screen_height = djui_hud_get_screen_height
local djui_hud_set_font = djui_hud_set_font
local sLiveSplitSettings = {}
local function storage_save(key, value)
if network_is_server() then
mod_storage_save(key, value)
end
end
local function storage_load(key)
if network_is_server() then
return mod_storage_load(key)
end
end
local function debug_log(...)
print(...)
log_to_console(...)
end
local sMenuX = tonumber(mod_storage_load("menuX")) or 0
local sMenuY = tonumber(mod_storage_load("menuY")) or 0
local function throw_error(str)
local errStr = "LiveSplit64: " .. str
log_to_console(errStr)
print(errStr)
hook_event(HOOK_ON_HUD_RENDER, function ()
djui_hud_set_font(FONT_NORMAL)
djui_hud_set_resolution(RESOLUTION_N64)
djui_hud_set_rotation(0, 0, 0)
local x = sMenuX
local w = 50
local h = 95
djui_hud_set_color(0x00, 0x00, 0x00, 0x9F)
djui_hud_render_rect(x, sMenuY, w + 1, h + 10)
local text = "Error, see console"
local len = djui_hud_measure_text(text) * 0.2
djui_hud_print_text(text, x + ((w - len) / 2), sMenuY + (h / 2), 0.2)
end)
end
local sCountdownLength = 0
local error = false
if network_is_server() then
sCountdownLength = tonumber(mod_storage_load("countdown")) or 0
if sCountdownLength == 0 then
mod_storage_save("countdown", "0")
end
sCountdownLength = sCountdownLength * 30
local function load_variable(name, isNumber, table, saveStr)
if error then return end
local var = storage_load(name)
if not var then return end
if not table then table = sLiveSplitSettings end
if not saveStr then saveStr = name end
table[saveStr] = isNumber and tonumber(var) or var
end
load_variable("numberOfPlayers", true)
if sLiveSplitSettings.numberOfPlayers then
for num = 1, sLiveSplitSettings.numberOfPlayers do
sLiveSplitSettings[num] = {}
load_variable("numberOfSplits" .. tostring(num), true, sLiveSplitSettings[num], "numberOfSplits")
if sLiveSplitSettings[num].numberOfSplits then
if sLiveSplitSettings[num].numberOfSplits < 0 then
throw_error("Invalid number of splits " .. sLiveSplitSettings[num].numberOfSplits)
return
end
else
sLiveSplitSettings[num].numberOfSplits = 0
end
end
else
sLiveSplitSettings.numberOfPlayers = 0
end
if error then return end
if sLiveSplitSettings.numberOfPlayers < 0 or sLiveSplitSettings.numberOfPlayers >= MAX_PLAYERS then
throw_error("Invalid number of players " .. sLiveSplitSettings.numberOfPlayers)
return
end
end
local courseToLevel = {
[COURSE_NONE] = LEVEL_NONE,
[COURSE_BOB] = LEVEL_BOB,
[COURSE_WF] = LEVEL_WF,
[COURSE_JRB] = LEVEL_JRB,
[COURSE_CCM] = LEVEL_CCM,
[COURSE_BBH] = LEVEL_BBH,
[COURSE_HMC] = LEVEL_HMC,
[COURSE_LLL] = LEVEL_LLL,
[COURSE_SSL] = LEVEL_SSL,
[COURSE_DDD] = LEVEL_DDD,
[COURSE_SL] = LEVEL_SL,
[COURSE_WDW] = LEVEL_WDW,
[COURSE_TTM] = LEVEL_TTM,
[COURSE_THI] = LEVEL_THI,
[COURSE_TTC] = LEVEL_TTC,
[COURSE_RR] = LEVEL_RR,
[COURSE_BITDW] = LEVEL_BITDW,
[COURSE_BITFS] = LEVEL_BITFS,
[COURSE_BITS] = LEVEL_BITS,
[COURSE_PSS] = LEVEL_PSS,
[COURSE_COTMC] = LEVEL_COTMC,
[COURSE_TOTWC] = LEVEL_TOTWC,
[COURSE_VCUTM] = LEVEL_VCUTM,
[COURSE_WMOTR] = LEVEL_WMOTR,
[COURSE_SA] = LEVEL_SA,
[COURSE_CAKE_END] = LEVEL_ENDING,
}
local function course_to_level(course)
return courseToLevel[course] or LEVEL_NONE
end
local levelToCourse = {}
for k, v in pairs(courseToLevel) do
levelToCourse[v] = k
end
local function level_to_course(level)
return levelToCourse[level] or COURSE_NONE
end
local sSplits = {}
local sSplitsMap = {} -- For translating in memory splits to saved splits
local SPLIT_PAUSE_EXIT = 0
local SPLIT_BOWSER = 1
local SPLIT_STAR = 2
local SPLIT_STAR_EXIT = 3
local SPLIT_COUNT = SPLIT_STAR_EXIT
local SPLIT_MENU_RUN = -1
local SPLIT_MENU_MAIN = 0
local SPLIT_MENU_SETTINGS = 1
local SPLIT_MENU_ADD_SPLIT = 2
local SPLIT_MENU_EDIT = 3
local sSplitMenu = SPLIT_MENU_RUN
local sSplitEdit = nil
local sSplitEditSegmentNum = 0
local function get_abbreviated_level_name(levelNum)
local levelName = get_level_name(level_to_course(levelNum), levelNum, 0)
if not levelName then return "" end
-- Level abbreviation
local levelStr = ""
for word in levelName:upper():gmatch("[^ %-%s]+") do
local firstLetter = word:match("^(%a)")
if firstLetter then levelStr = levelStr .. firstLetter end
end
if levelNum == LEVEL_PSS and levelStr == "TPSS" then levelStr = "PSS" end
return levelStr
end
local sIsWaitingLevelNameRefresh = true
local old_smlua_text_utils_course_acts_replace = smlua_text_utils_course_acts_replace
_G.smlua_text_utils_course_acts_replace = function (courseNum, courseName, act1, act2, act3, act4, act5, act6)
sIsWaitingLevelNameRefresh = true
old_smlua_text_utils_course_acts_replace(courseNum, courseName, act1, act2, act3, act4, act5, act6)
end
local old_smlua_text_utils_course_name_replace = smlua_text_utils_course_name_replace
_G.smlua_text_utils_course_name_replace = function (courseNum, name)
sIsWaitingLevelNameRefresh = true
old_smlua_text_utils_course_name_replace(courseNum, name)
end
local function parse_split(key, orig, flags)
if #flags < 2 then return end
local levelNum = tonumber(flags[2])
local pbSegment = tonumber(flags[3])
local split = {
key = key,
orig = orig,
type = tonumber(flags[1]),
levelID = levelNum,
levelName = get_abbreviated_level_name(levelNum),
pbSegment = pbSegment,
prevPbSegment = pbSegment,
thisSegment = -1,
}
return split
end
---@diagnostic disable-next-line: duplicate-set-field
function string:split(delimiter) -- from stack overflow
local result = {}
local from = 1
local delim_from, delim_to = string.find(self, delimiter, from )
while delim_from do
table.insert(result, string.sub(self, from, delim_from - 1))
from = delim_to + 1
delim_from, delim_to = string.find(self, delimiter, from)
end
table.insert(result, string.sub(self, from))
return result
end
for i = 0, MAX_PLAYERS - 1 do
local syncTable = gPlayerSyncTable[i]
syncTable.speedrunSplit = 1
end
-- Translates a split index as it appears on the screen,
-- to the order that splits have in the save file
local function translate_split(player, split)
for k, v in pairs(sSplitsMap[player]) do
if v == split then
return k
end
end
return nil -- error!
end
local function split_map_get_top(player)
local highest = -1
for _, value in pairs(sSplitsMap[player]) do
if value > highest then highest = value end
end
if highest ~= -1 then return highest end
return 0
end
local function get_split_key(player, split)
return tostring(player) .. "P_split_" .. tostring(split)
end
-- Load splits
if network_is_server() then
for player = 1, sLiveSplitSettings.numberOfPlayers do
sSplits[player] = {}
sSplitsMap[player] = {}
local split, i = 1, 1
local numSplits = sLiveSplitSettings[player].numberOfSplits
while i <= numSplits do
local splitStr = get_split_key(player, i)
local str = storage_load(splitStr)
if not str then throw_error("Expected to find key '" .. splitStr .. "', but it was undefined.") return end
sSplitsMap[player][i] = str ~= "cleared" and split or -1
if str ~= "cleared" then
sSplits[player][split] = parse_split(splitStr, str, str:split("_"))
split = split + 1
end
i = i + 1
end
end
if error then return end
end
local m0 = gMarioStates[0]
local np0 = gNetworkPlayers[0]
local sSpeedrunTimer = 0
local sSpeedrunTimerPlaying = false
local sSpeedrunTimerFinished = false
local sSpeedrunSplitFrames = 0
local sCountdown = sCountdownLength
-- Warp Node constants
local WARP_NODE_F0 = 0xF0
local sLocalPlayer = network_global_index_from_local(0)
local sNetworkLoaded = network_is_server()
local PACKET_TYPE_SPLIT = 0
local PACKET_TYPE_ADD_SPLIT = 1
local PACKET_TYPE_RESET_TIMER = 2
local PACKET_TYPE_REQUEST_JOIN_INFO = 3
local PACKET_TYPE_TIMER_SET_STATE = 4
local PACKET_TYPE_EDIT_SPLIT = 5
local function get_uncolored_string(str)
local s = ""
local ignore = false
for c in str:gmatch(".") do
if c == "\\" then ignore = not ignore
elseif not ignore then s = s .. c end
end
return s
end
local function on_network_load(m)
if m and m.playerIndex ~= 0 then return end
if not network_is_server() then
-- Request from the server join info
network_send_to(network_local_index_from_global(0), true, {
globalIndex = sLocalPlayer,
packetType = PACKET_TYPE_REQUEST_JOIN_INFO,
})
end
end
if network_is_server() then
hook_event(HOOK_ON_PLAYER_CONNECTED, on_network_load)
else
hook_event(HOOK_JOINED_GAME, on_network_load)
end
local function speedrun_get_split_frames(type)
if type == SPLIT_STAR then return 1 end
if type == SPLIT_PAUSE_EXIT then return 4 end
if type == SPLIT_STAR_EXIT then return 4 end
if type == SPLIT_BOWSER then return 1 end
end
local sBowserLevels = { [LEVEL_BITDW] = 1, [LEVEL_BITFS] = 1, [LEVEL_BITS] = 1 }
local sBowserArenas = { [LEVEL_BOWSER_1] = 1, [LEVEL_BOWSER_2] = 1, [LEVEL_BOWSER_3] = 1 }
local function split_same_level(split)
local localLevel = np0.currLevelNum
if (sBowserLevels[split.levelID] or split.type == SPLIT_BOWSER) and sBowserArenas[localLevel] then
return true
end
if split.type == SPLIT_PAUSE_EXIT and split.levelID == LEVEL_HMC and localLevel == LEVEL_COTMC then
return true
end
return split.levelID == np0.currLevelNum
end
local function speedrun_split(type)
if sSpeedrunTimerPlaying then
local splits = sSplits[sLocalPlayer + 1]
if splits then
local split = splits[gPlayerSyncTable[0].speedrunSplit]
if split and split_same_level(split) and split.type == type and sSpeedrunSplitFrames <= 0 then
sSpeedrunSplitFrames = speedrun_get_split_frames(type)
end
end
end
end
-- Only to be executed by server
local function speedrun_add_split(levelNum, type, playerNum)
if not sSplits[playerNum] then sSplits[playerNum] = {} end
if not sSplitsMap[playerNum] then sSplitsMap[playerNum] = {} end
local newMapCount = #sSplitsMap[playerNum] + 1
local newCount = #sSplits[playerNum] + 1
sSplitsMap[playerNum][newMapCount] = split_map_get_top(playerNum) + 1
local key = get_split_key(playerNum, newMapCount)
local value = tostring(type) .. "_" .. tostring(levelNum) .. "_" .. tostring(-1)
sSplits[playerNum][newCount] = {
key = key,
orig = value,
type = type,
levelID = levelNum,
levelName = get_abbreviated_level_name(levelNum),
pbSegment = -1,
prevPbSegment = -1,
thisSegment = -1,
}
if not sLiveSplitSettings[playerNum] then
sLiveSplitSettings[playerNum] = { numberOfSplits = 0 }
storage_save("numberOfPlayers", tostring(#sSplits))
end
sLiveSplitSettings[playerNum].numberOfSplits = newMapCount
storage_save("numberOfSplits" .. tostring(playerNum), tostring(sLiveSplitSettings[playerNum].numberOfSplits))
storage_save(key, value)
-- Tell the other players about the new split
network_send(true, {
packetType = PACKET_TYPE_ADD_SPLIT,
playerNum = playerNum,
splitNum = newCount,
type = type,
levelNum = levelNum,
pbSegment = -1,
prevPbSegment = -1,
})
end
local function speedrun_reset_timer()
if not network_is_server() then return end
sCountdown = sCountdownLength
sSpeedrunTimer = sCountdownLength
-- Tell the network to reset
network_send(true, {
packetType = PACKET_TYPE_RESET_TIMER,
timer = sSpeedrunTimer,
})
sSplitMenu = SPLIT_MENU_RUN
sSpeedrunTimerFinished = false
sSpeedrunTimerPlaying = false
gPlayerSyncTable[0].speedrunSplit = 1
sSpeedrunSplitFrames = 0
for _, player in ipairs(sSplits) do
for _, s in ipairs(player) do
s.thisSegment = -1
s.prevPbSegment = s.pbSegment
end
end
end
local function mh_is_runner_timer()
if not mhApi then return false end
if mhApi.getState() ~= 1 then return false end
local timer = mhApi.getGlobalField("mhTimer")
local countdown = mhApi.getGlobalField("countdown")
return timer > countdown
end
local function is_countdown()
return mh_is_runner_timer() or (sCountdown > 0)
end
hook_event(HOOK_UPDATE, function ()
if mhApi then
local state = mhApi.getState()
if state ~= nil then
sSpeedrunTimer = mhApi.getGlobalField("speedrunTimer")
if state == 1 then
if mh_is_runner_timer() then
sSpeedrunTimer = mhApi.getGlobalField("mhTimer") - mhApi.getGlobalField("countdown") + 30
end
end
sSpeedrunTimerPlaying = state == 1 or state == 2
if state ~= 0 then
if network_is_server() then
if sSpeedrunTimerFinished and state <= 2 then
speedrun_reset_timer()
end
if state == 1 and sSpeedrunTimer == 180 and mhApi.getGlobalField("speedrunTimer") == 0 then
speedrun_reset_timer()
sSpeedrunTimerPlaying = true
end
end
sSpeedrunTimerFinished = state > 2
end
end
end
-- Countdown
if sCountdown > 0 then
if sSpeedrunTimerPlaying then sCountdown = sCountdown - 1 end
sSpeedrunTimer = sCountdown
end
if sSpeedrunTimerPlaying then
if not mhApi or sCountdown > 0 then sSpeedrunTimer = sSpeedrunTimer + 1 end -- Timer is manually updated by mh
if sSpeedrunSplitFrames == 1 then
local curSplit = gPlayerSyncTable[0].speedrunSplit
local splits = sSplits[sLocalPlayer + 1]
local split = splits[curSplit]
split.thisSegment = sSpeedrunTimer
network_send(true, {
globalIndex = sLocalPlayer,
packetType = PACKET_TYPE_SPLIT,
curSplit = curSplit,
segmentTime = sSpeedrunTimer,
})
if curSplit >= #splits then -- Run finished
sSpeedrunTimerPlaying = false
sSpeedrunTimerFinished = true
for i, player in ipairs(sSplits) do
gPlayerSyncTable[i].speedrunSplit = #player
player[#player].thisSegment = sSpeedrunTimer
end
-- Got a personal best?
if splits[#splits].pbSegment == -1 or splits[#splits].pbSegment > sSpeedrunTimer then
for _, player in ipairs(sSplits) do
for _, s in ipairs(player) do
s.prevPbSegment = s.pbSegment
s.pbSegment = s.thisSegment
if network_is_server() then storage_save(s.key, s.orig:match("^(.*)_") .. "_" .. tostring(s.thisSegment)) end
end
end
end
else
gPlayerSyncTable[0].speedrunSplit = curSplit + 1
end
end
if sSpeedrunSplitFrames > 0 then sSpeedrunSplitFrames = sSpeedrunSplitFrames - 1 end
end
if sIsWaitingLevelNameRefresh then
sIsWaitingLevelNameRefresh = false
for _, player in ipairs(sSplits) do
for _, segment in ipairs(player) do
segment.levelName = get_abbreviated_level_name(segment.levelID)
end
end
end
end)
local sPacketTable = {
[PACKET_TYPE_REQUEST_JOIN_INFO] = function (data)
if not network_is_server() then return end
local localIndex = network_local_index_from_global(data.globalIndex)
local sendData = {
packetType = PACKET_TYPE_ADD_SPLIT,
playerNum = 0,
splitNum = 0,
type = 0,
levelNum = 0,
pbSegment = 0,
prevPbSegment = 0,
complete = false,
compTime = 0,
noSplits = false,
finished = false,
playing = false,
countdown = 0,
}
for playerNum, player in ipairs(sSplits) do
sendData.playerNum = playerNum
for splitNum, split in ipairs(player) do
sendData.splitNum = splitNum
sendData.type = split.type
sendData.levelNum = split.levelID
sendData.pbSegment = split.pbSegment
sendData.prevPbSegment = split.prevPbSegment
if #sSplits == playerNum and #player == splitNum then
sendData.complete = true
sendData.countdown = sCountdown
sendData.compTime = sSpeedrunTimer
sendData.finished = sSpeedrunTimerFinished
sendData.playing = sSpeedrunTimerPlaying
end
network_send_to(localIndex, true, sendData)
end
end
if #sSplits == 0 then
sendData.noSplits = true
sendData.complete = true
sendData.compTime = sSpeedrunTimer
sendData.finished = sSpeedrunTimerFinished
sendData.playing = sSpeedrunTimerPlaying
network_send_to(localIndex, true, sendData)
end
end,
[PACKET_TYPE_SPLIT] = function (data)
local localIndex = network_local_index_from_global(data.globalIndex)
local curSplit = gPlayerSyncTable[localIndex].speedrunSplit
local splits = sSplits[data.globalIndex + 1]
local split = splits[curSplit]
split.thisSegment = data.segmentTime
if curSplit >= #splits then -- Run finished
sSpeedrunTimerPlaying = false
sSpeedrunTimerFinished = true
local clientSplits = sSplits[sLocalPlayer + 1]
if clientSplits then
gPlayerSyncTable[0].speedrunSplit = #clientSplits
clientSplits[#clientSplits].thisSegment = data.segmentTime
end
-- Got a personal best?
if splits[#splits].pbSegment == -1 or splits[#splits].pbSegment > sSpeedrunTimer then
for _, player in ipairs(sSplits) do
for _, s in ipairs(player) do
s.prevPbSegment = s.pbSegment
s.pbSegment = s.thisSegment
if network_is_server() then storage_save(s.key, s.orig:match("^(.*)_") .. "_" .. tostring(s.thisSegment)) end
end
end
end
else
gPlayerSyncTable[localIndex].speedrunSplit = curSplit + 1
end
end,
[PACKET_TYPE_ADD_SPLIT] = function (data)
if not data.noSplits then
if not sSplits[data.playerNum] then sSplits[data.playerNum] = {} end
sSplits[data.playerNum][data.splitNum] = {
type = data.type,
levelID = data.levelNum,
levelName = get_abbreviated_level_name(data.levelNum),
pbSegment = data.pbSegment,
prevPbSegment = data.prevPbSegment,
thisSegment = -1,
}
end
if data.complete then
sCountdown = data.countdown
sSpeedrunTimer = data.compTime
sSpeedrunTimerFinished = data.finished
sSpeedrunTimerPlaying = data.playing
sNetworkLoaded = true
end
end,
[PACKET_TYPE_RESET_TIMER] = function (data)
sSplitMenu = SPLIT_MENU_RUN
sSpeedrunTimerFinished = false
sSpeedrunTimerPlaying = false
sSpeedrunTimer = data.timer
sCountdown = data.timer
gPlayerSyncTable[0].speedrunSplit = 1
sSpeedrunSplitFrames = 0
for _, player in ipairs(sSplits) do
for _, s in ipairs(player) do
s.thisSegment = -1
s.prevPbSegment = s.pbSegment
end
end
end,
[PACKET_TYPE_TIMER_SET_STATE] = function (data)
sSpeedrunTimerPlaying = data.playing
sSpeedrunTimerFinished = data.finished
end,
[PACKET_TYPE_EDIT_SPLIT] = function (data)
if not sSplits[data.playerNum] then return end -- If this triggers it means we're desynced
if data.delete then
table.remove(sSplits[data.playerNum], data.splitNum)
if #sSplits[data.playerNum] == 0 then table.remove(sSplits, data.playerNum) end
return
end
local split = sSplits[data.playerNum][data.splitNum]
split.type = data.type
split.levelID = data.levelNum
split.levelName = get_abbreviated_level_name(data.levelNum)
split.pbSegment = data.pbSegment
split.prevPbSegment = data.prevPbSegment
split.thisSegment = data.thisSegment
end,
}
hook_event(HOOK_ON_PACKET_RECEIVE, function (data) if sPacketTable[data.packetType] then sPacketTable[data.packetType](data) end end)
local function get_last_warp_node_id() return m0.area.warpNodes.node.id end
------------
-- Splits --
------------
local sWarpEntryArea = 0
local sWarpEntryLevel = 0
local sHookOnPauseExit = {}
local old_hook_event = hook_event
local function custom_hook_event(hook, func)
if hook == HOOK_ON_PAUSE_EXIT then
sHookOnPauseExit[#sHookOnPauseExit+1] = func
return
end
old_hook_event(hook, func)
end
hook_event(HOOK_ON_PAUSE_EXIT, function (usedExitToCastle)
-- Used to check if a mod has refused pause exiting
for _, func in ipairs(sHookOnPauseExit) do
local ret = func(usedExitToCastle)
if ret ~= nil and not ret then return false end
end
-- Split
speedrun_split(SPLIT_PAUSE_EXIT)
end)
local function on_warp()
local warpNodeID = get_last_warp_node_id()
if warpNodeID == WARP_NODE_F0 then speedrun_split(SPLIT_STAR_EXIT) end -- Star dance exit (does not work)
end
hook_event(HOOK_MARIO_UPDATE, function (m)
if m.playerIndex ~= 0 then return end
if sWarpEntryLevel ~= np0.currLevelNum or sWarpEntryArea ~= np0.currAreaIndex then
sWarpEntryLevel = np0.currLevelNum
sWarpEntryArea = np0.currAreaIndex
on_warp()
end
end)
hook_event(HOOK_ON_WARP, function ()
if m0.action ~= ACT_TELEPORT_FADE_IN and m0.action ~= ACT_TELEPORT_FADE_OUT then
sWarpEntryLevel = np0.currLevelNum
sWarpEntryArea = np0.currAreaIndex
on_warp()
end
end)
-- Stars and keys
bhvGrandStar = get_behavior_from_id(id_bhvGrandStar)
hook_event(HOOK_ON_INTERACT, function (m, o, intType, intVal)
if m.playerIndex ~= 0 then return end
if intVal then
if intType == INTERACT_STAR_OR_KEY then
if (sBowserArenas[np0.currLevelNum]) then
speedrun_split(SPLIT_BOWSER)
else
speedrun_split(SPLIT_STAR)
end
end
if intType == INTERACT_WARP then
if o.behavior == bhvGrandStar then
speedrun_split(SPLIT_BOWSER)
end
end
end
end)
------------
-- Inputs --
------------
local sMouse = {
click = false,
hoverElement = nil,
clickedElement = nil,
prevClickedElement = nil,
x = 0, -- DJUI Resolution
y = 0, -- DJUI Resolution
cx = 0, -- Converted to N64 resolution
cy = 0, -- Converted to N64 resolution
prevX = 0, -- Previous X value
prevY = 0, -- Previous Y value
}
local HAND_OPEN_TEX = get_texture_info("gd_texture_hand_open")
local HAND_CLOSED_TEX = get_texture_info("gd_texture_hand_closed")
local isUpdate = false
local wasHolding = false
-- This is to make sure we grab inputs before they're needed
local function update_inputs()
local cntr = m0.controller
if not isUpdate then
isUpdate = true
sMouse.click = cntr.buttonDown & A_BUTTON ~= 0 and not (is_game_paused() or djui_hud_is_pause_menu_created())
if cntr.buttonDown & U_JPAD ~= 0 and cntr.buttonDown & U_CBUTTONS ~= 0 then
wasHolding = true
else
if wasHolding then
if sSplitMenu == SPLIT_MENU_MAIN then
sSplitMenu = SPLIT_MENU_RUN
else
sSplitMenu = SPLIT_MENU_MAIN
end
end
wasHolding = false
end
end
if sMouse.hoverElement ~= nil and sMouse.click then
cntr.buttonDown = cntr.buttonDown & ~A_BUTTON
cntr.buttonPressed = cntr.buttonPressed & ~A_BUTTON
end
end
hook_event(HOOK_UPDATE, function () isUpdate = false end)
local function mouse_set_element(set, tag)
if sMouse.clickedElement and sMouse.clickedElement ~= tag and set then
sMouse.clickedElement = tag
end
if not sMouse.clickedElement and set then
sMouse.clickedElement = tag
end
if not sMouse.click then
sMouse.clickedElement = nil
end
end
hook_event(HOOK_BEFORE_MARIO_UPDATE, function (m)
if m.playerIndex == 0 then
update_inputs()
end
end)
------------
-- Render --
------------
local sLiveSplit64Chars = {
["."] = 10,
[":"] = 10,
["0"] = 14,
["1"] = 14,
["2"] = 14,
["3"] = 14,
["4"] = 14,
["5"] = 14,
["6"] = 14,
["7"] = 14,
["8"] = 14,
["9"] = 14,
}
-- String rendering function that uses a custom width for characters
-- intended for rendering the timer
local function livesplit64_render(str, x, y, s)
for i = 1, #str do
local c = str:sub(i, i)
djui_hud_print_text(c, x, y, s)
local charSize = sLiveSplit64Chars[c] or 16
x = x + (charSize * s)
end
end
local function livesplit64_measure(str, size)
local len = 0
for i = 1, #str do
local c = str:sub(i, i)
local charSize = sLiveSplit64Chars[c] or 16
len = len + (charSize * size)
end
return len
end
local function livesplit64_print_text(message, x, y, scale, shadow, r, g, b, a)
if shadow and shadow > 0 then
djui_hud_set_color(0, 0, 0, a or 0xFF)
livesplit64_render(message, x + shadow, y + shadow, scale)
end
djui_hud_set_color(r or 0xFF, g or 0xFF, b or 0xFF, a or 0xFF)
livesplit64_render(message, x, y, scale)
end
local old_djui_hud_print_text = djui_hud_print_text
local function djui_hud_print_text(message, x, y, scale, shadow, r, g, b, a)
if shadow and shadow > 0 then
djui_hud_set_color(0, 0, 0, a or 0xFF)
old_djui_hud_print_text(message, x + shadow, y + shadow, scale)
end
djui_hud_set_color(r or 0xFF, g or 0xFF, b or 0xFF, a or 0xFF)
old_djui_hud_print_text(message, x, y, scale)
end
local function draw_rectangle(x, y, h, w, lineW)
djui_hud_render_rect(x, y, lineW + w, lineW)
djui_hud_render_rect(x, y + h, lineW + w, lineW)
djui_hud_render_rect(x, y, lineW, lineW + h)
djui_hud_render_rect(x + w, y, lineW, lineW + h)
end
local function is_point_in_box(px, py, bx, by, bw, bh)
return (px > bx and px < bx + bw) and (py > by and py < by + bh)
end
local sButtons = {}
local function render_button(t, ts, x, y, w, h, callback, id, fadeOut)
local mouseInBox = is_point_in_box(sMouse.cx, sMouse.cy, x, y, w, h) and not (is_game_paused() or djui_hud_is_pause_menu_created())
local r, g, b = 0x30, 0x30, 0x30
djui_hud_set_color(r, g, b, (fadeOut and 0x50) or 0xFF)
local outlineS = 1
local hos = outlineS / 2
draw_rectangle(x, y, h - hos, w - hos, outlineS)
local isEnabled = callback and callback(true)
if isEnabled then
r, g, b = 0x00, 0xFF, 0x00
if fadeOut then
r, g, b = 0x10, 0x10, 0x10
end
end
djui_hud_print_text(t, x + ((w - (djui_hud_measure_text(t) * ts))/2), y + (h / 2) - ((32 * ts) / 2), ts, 1, 0xFF, 0xFF, 0xFF, (fadeOut and 0x50) or 0xFF)
djui_hud_set_color(r, g, b, 0x50)
if not id then id = t end
if not (isEnabled and fadeOut) and mouseInBox and (not sMouse.clickedElement or sMouse.clickedElement == t) then
sMouse.hoverElement = t
djui_hud_set_color(r, g, b, 0x96)
if sMouse.click then
mouse_set_element(true, t)
djui_hud_set_color(r, g, b, 0xFF)
elseif sButtons[id] and sMouse.prevClickedElement == t then
if callback then callback() end
end
end
sButtons[id] = sMouse.click and mouseInBox
djui_hud_render_rect(x + hos, y + hos, w - hos, h - hos)
end
local function frames_to_time_str(frames, forceMinutes)
local s = frames // 30
local seconds = s % 60
local minutes = s // 60 % 60
local hours = s // 60 // 60
if minutes == 0 and not forceMinutes then
return string_format("%d", seconds)
elseif hours == 0 then
return string_format("%d:%02d", minutes, seconds)
else
return string_format("%d:%02d:%02d", hours, minutes, seconds)
end
end
local function frames_to_time_str_decimal(frames, forceMinutes)
local milliseconds = math_floor(frames / 30 % 1 * 100)
local s = frames // 30
local seconds = s % 60
local minutes = s // 60 % 60
local hours = s // 60 // 60
if minutes == 0 and not forceMinutes then
return string_format("%d.%02d", seconds, milliseconds)
elseif hours == 0 then
return string_format("%d:%02d.%02d", minutes, seconds, milliseconds)
else
return string_format("%d:%02d:%02d.%02d", hours, minutes, seconds, milliseconds)
end
end
local sCourseID = COURSE_BOB
local sSplitAddType = SPLIT_PAUSE_EXIT
local sSplitAddPlayer = 1
local function split_menu_run_render(x, y, w, h, segmentW)
djui_hud_set_color(0x00, 0x00, 0x00, 0xCF)
djui_hud_render_rect(x, sMenuY, w, 10)
for playerIndex, player in ipairs(sSplits) do
local connected = gNetworkPlayers[playerIndex - 1].connected
for segmentNum, segment in ipairs(player) do
local speedRunSplit = gPlayerSyncTable[network_local_index_from_global(playerIndex - 1)].speedrunSplit
if sSpeedrunTimerPlaying and connected and segmentNum == speedRunSplit then
djui_hud_set_color(0x00, 0x00, 0xFF, 0xEF)
else
if segmentNum % 2 == 1 then
djui_hud_set_color(0x00, 0x00, 0x00, 0xBF)
else
djui_hud_set_color(0x30, 0x30, 0x30, 0xBF)
end
end
local drawS = 7
local x = x + (segmentW * (playerIndex - 1))
local y = y + (drawS * (segmentNum - 1))
djui_hud_render_rect(x, y, segmentW, drawS)
local time = segment.pbSegment == -1 and "-" or frames_to_time_str(segment.pbSegment, true)
local m = djui_hud_measure_text(time) * 0.2
local sideX = x + segmentW - m - 2
djui_hud_print_text(time, sideX, y, 0.2, 1)
if connected then
if segment.prevPbSegment ~= -1 and segmentNum <= speedRunSplit then
local tracker = segment.thisSegment == -1 and sSpeedrunTimer or segment.thisSegment
if segment.prevPbSegment <= tracker then
local time = "+" .. frames_to_time_str_decimal(tracker - segment.prevPbSegment)
local m2 = livesplit64_measure(time, 0.2)
sideX = sideX - m2 - 3
livesplit64_print_text(time, sideX, y, 0.2, 0.5, 0xFF, 0x00, 0x00)
end
if segment.thisSegment ~= -1 and segment.thisSegment < segment.prevPbSegment then
local time = "-" .. frames_to_time_str_decimal(segment.prevPbSegment - segment.thisSegment)
local m2 = livesplit64_measure(time, 0.2)
sideX = sideX - m2 - 3
livesplit64_print_text(time, sideX, y, 0.2, 0.5, 0x00, 0xFF, 0x00)
end
end
end
local levelStr = segment.levelName
local levelLen = djui_hud_measure_text(levelStr) * 0.2
if x + 1 + levelLen > sideX then -- Shorten the level name if there's overlap
local i = #levelStr
while i > 0 do
levelLen = djui_hud_measure_text(levelStr) * 0.2
if x + 1 + levelLen < sideX then break end
i = i - 1
levelStr = levelStr:sub(0, i)
end
end
djui_hud_print_text(levelStr, x + 1, y, 0.2, 1)
end
end
-- Timer
local r, g, b = 0xFF, 0xFF, 0xFF
if not is_countdown() then
if #sSplits > 0 then
local splits = sSplits[sLocalPlayer + 1] or sSplits[1]
local split = splits[gPlayerSyncTable[0].speedrunSplit] or splits[gPlayerSyncTable[network_local_index_from_global(0)].speedrunSplit]