forked from sharpobject/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainloop.lua
More file actions
2813 lines (2693 loc) · 108 KB
/
mainloop.lua
File metadata and controls
2813 lines (2693 loc) · 108 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
local utf8 = require("utf8")
local wait, resume = coroutine.yield, coroutine.resume
local main_select_mode, main_endless, make_main_puzzle, main_net_vs_setup,
main_replay_endless, main_replay_puzzle, main_net_vs,
main_config_input, main_dumb_transition, main_select_puzz,
menu_up, menu_down, menu_left, menu_right, menu_enter, menu_escape, menu_backspace,
main_replay_vs, main_local_vs_setup, main_local_vs, menu_key_func,
multi_func, normal_key, main_set_name, main_character_select, main_net_vs_lobby,
main_local_vs_yourself_setup, main_local_vs_yourself,
main_options, exit_options_menu, main_music_test, exit_game
local PLAYING = "playing" -- room states
local CHARACTERSELECT = "character select" --room states
local currently_spectating = false
connection_up_time = 0
logged_in = 0
connected_server_ip = nil
my_user_id = nil
leaderboard_report = nil
replay_of_match_so_far = nil
spectator_list = nil
spectators_string = ""
leftover_time = 0
local main_menu_screen_pos = { 300 + (canvas_width-legacy_canvas_width)/2, 280 + (canvas_height-legacy_canvas_height)/2 }
function fmainloop()
local func, arg = main_select_mode, nil
replay = {}
-- Default configuration values
config = {
-- The lastly used version
version = VERSION,
-- Player character
character = "lip",
-- Vsync
vsync = true,
-- Level (2P modes / 1P vs yourself mode)
level = 5,
endless_speed = 1,
endless_difficulty = 1,
-- Player name
name = "defaultname",
-- Volume settings
master_volume = 100,
SFX_volume = 100,
music_volume = 100,
-- Debug mode flag
debug_mode = false,
-- Show FPS in the top-left corner of the screen
show_fps = false,
-- Enable ready countdown flag
ready_countdown_1P = true,
-- Change danger music back later flag
danger_music_changeback_delay = false,
-- analytics
enable_analytics = false,
-- Save replays setting
save_replays_publicly = "with my name",
-- Default directories for graphics/panels/sounds
assets_dir = default_assets_dir,
sounds_dir = default_sounds_dir,
panels_dir = default_assets_dir,
-- Retrocompatibility, please remove whenever possible, it's so ugly!
panels_dir_when_not_using_set_from_assets_folder = default_panels_dir,
use_panels_from_assets_folder = true,
-- Retrocompatibility
use_default_characters = false,
}
gprint("Reading config file", unpack(main_menu_screen_pos))
wait()
read_conf_file() -- TODO: stop making new config files
local x,y, display = love.window.getPosition()
love.window.setPosition(
config.window_x or x,
config.window_y or y,
config.display or display)
gprint("Copying Puzzles Readme")
wait()
copy_file("Custom Puzzles Readme.txt", "puzzles/README.txt")
gprint("Reading replay file", unpack(main_menu_screen_pos))
wait()
read_replay_file()
gprint("Preloading characters...", unpack(main_menu_screen_pos))
wait()
characters_init() -- load images and set up stuff
gprint("Loading graphics...", unpack(main_menu_screen_pos))
wait()
graphics_init() -- load images and set up stuff
gprint("Loading panels...", unpack(main_menu_screen_pos))
wait()
panels_init() -- load panels
gprint("Loading sounds...", unpack(main_menu_screen_pos))
wait()
sound_init()
gprint("Loading analytics...", unpack(main_menu_screen_pos))
wait()
analytics_init()
while true do
leftover_time = 1/120
consuming_timesteps = false
func,arg = func(unpack(arg or {}))
collectgarbage("collect")
end
end
-- Wrapper for doing something at 60hz
-- The rest of the stuff happens at whatever rate is convenient
function variable_step(f)
for i=1,4 do
if leftover_time >= 1/60 then
joystick_ax()
f()
key_counts()
this_frame_keys = {}
this_frame_unicodes = {}
leftover_time = leftover_time - 1/60
end
end
end
-- Changes the behavior of menu_foo functions.
-- In a menu that doesn't specifically pertain to multiple players,
-- up, down, left, right should always work. But in a multiplayer
-- menu, those keys should definitely not move many cursors each.
local multi = false
function multi_func(func)
return function(...)
multi = true
local res = {func(...)}
multi = false
return unpack(res)
end
end
-- Keys that have a fixed function in menus can be bound to other
-- meanings, but should continue working the same way in menus.
local menu_reserved_keys = {}
function repeating_key(key)
local key_time = keys[key]
return this_frame_keys[key] or
(key_time and key_time > 25 and key_time % 3 ~= 0)
end
function normal_key(key) return this_frame_keys[key] end
function menu_key_func(fixed, configurable, rept, sound)
sound = sound or nil
local query = normal_key
if rept then
query = repeating_key
end
for i=1,#fixed do
menu_reserved_keys[#menu_reserved_keys+1] = fixed[i]
end
return function(k)
local res = false
if multi then
for i=1,#configurable do
res = res or query(k[configurable[i]])
end
else
for i=1,#fixed do
res = res or query(fixed[i])
end
for i=1,#configurable do
local keyname = k[configurable[i]]
res = res or query(keyname) and
not menu_reserved_keys[keyname]
end
end
if res and sound ~= nil then
play_optional_sfx(sound())
end
return res
end
end
menu_up = menu_key_func({"up"}, {"up"}, true, function() return sounds.SFX.menu_move end )
menu_down = menu_key_func({"down"}, {"down"}, true, function() return sounds.SFX.menu_move end)
menu_left = menu_key_func({"left"}, {"left"}, true, function() return sounds.SFX.menu_move end)
menu_right = menu_key_func({"right"}, {"right"}, true, function() return sounds.SFX.menu_move end)
menu_enter = menu_key_func({"return","kenter","z"}, {"swap1"}, false, function() return sounds.SFX.menu_validate end)
menu_escape = menu_key_func({"escape","x"}, {"swap2"}, false, function() return sounds.SFX.menu_cancel end)
menu_prev_page = menu_key_func({"pageup"}, {"raise1"}, true, function() return sounds.SFX.menu_move end)
menu_next_page = menu_key_func({"pagedown"}, {"raise2"}, true, function() return sounds.SFX.menu_move end)
menu_backspace = menu_key_func({"backspace"}, {"backspace"}, true)
do
local active_idx = 1
function main_select_mode()
love.audio.stop()
currently_spectating = false
stop_the_music()
character_loader_clear()
close_socket()
bg = title
logged_in = 0
connection_up_time = 0
connected_server_ip = ""
current_server_supports_ranking = false
match_type = ""
match_type_message = ""
local items = {
{"1P endless", main_select_speed_99, {main_endless}},
{"1P puzzle", main_select_puzz},
{"1P time attack", main_select_speed_99, {main_time_attack}},
{"1P vs yourself", main_local_vs_yourself_setup},
--{"2P vs online at burke.ro", main_net_vs_setup, {"burke.ro"}},
{"2P vs online at Jon's server", main_net_vs_setup, {"18.188.43.50"}},
--{"2P vs online at betaserver.panelattack.com", main_net_vs_setup, {"betaserver.panelattack.com"}},
--{"2P vs online (USE ONLY WITH OTHER CLIENTS ON THIS TEST BUILD 025beta)", main_net_vs_setup, {"18.188.43.50"}},
--{"This test build is for offline-use only"--[["2P vs online at Jon's server"]], main_select_mode},
--{"2P vs online at domi1819.xyz (Europe, beta for spectating and ranking)", main_net_vs_setup, {"domi1819.xyz"}},
--{"2P vs online at localhost (development-use only)", main_net_vs_setup, {"localhost"}},
--{"2P vs online at LittleEndu's server", main_net_vs_setup, {"51.15.207.223"}},
{"2P vs local game", main_local_vs_setup},
{"Replay of 1P endless", main_replay_endless},
{"Replay of 1P puzzle", main_replay_puzzle},
{"Replay of 2P vs", main_replay_vs},
{"Configure input", main_config_input},
{"Set name", main_set_name},
{"Options", main_options},
{"Music test", main_music_test}
}
if love.graphics.getSupported("canvas") then
items[#items+1] = {"Fullscreen (LAlt+Enter)", fullscreen}
else
items[#items+1] = {"Your graphics card doesn't support canvases for fullscreen", main_select_mode}
end
items[#items+1] = {"Quit", exit_game }
local k = K[1]
while true do
local to_print = ""
local arrow = ""
for i=1,#items do
if active_idx == i then
arrow = arrow .. ">"
else
arrow = arrow .. "\n"
end
to_print = to_print .. " " .. items[i][1] .. "\n"
end
gprint(arrow, unpack(main_menu_screen_pos))
gprint(to_print, unpack(main_menu_screen_pos))
wait()
local ret = nil
variable_step(function()
if menu_up(k) then
active_idx = wrap(1, active_idx-1, #items)
elseif menu_down(k) then
active_idx = wrap(1, active_idx+1, #items)
elseif menu_enter(k) then
ret = {items[active_idx][2], items[active_idx][3]}
elseif menu_escape(k) then
if active_idx == #items then
ret = {items[active_idx][2], items[active_idx][3]}
else
active_idx = #items
end
end
end)
if ret then
return unpack(ret)
end
end
end
end
function main_select_speed_99(next_func, ...)
local difficulties = {"Easy", "Normal", "Hard", "EX Mode"}
local items = {{"Speed"},
{"Difficulty"},
{"Go!", next_func},
{"Back", main_select_mode}}
local speed = config.endless_speed or 1
local difficulty = config.endless_difficulty or 1
local active_idx = 1
local k = K[1]
local ret = nil
while true do
local to_print, to_print2, arrow = "", "", ""
for i=1,#items do
if active_idx == i then
arrow = arrow .. ">"
else
arrow = arrow .. "\n"
end
to_print = to_print .. " " .. items[i][1] .. "\n"
end
to_print2 = " " .. speed .. "\n "
.. difficulties[difficulty]
gprint(arrow, unpack(main_menu_screen_pos))
gprint(to_print, unpack(main_menu_screen_pos))
gprint(to_print2, unpack(main_menu_screen_pos))
wait()
variable_step(function()
if menu_up(k) then
active_idx = wrap(1, active_idx-1, #items)
elseif menu_down(k) then
active_idx = wrap(1, active_idx+1, #items)
elseif menu_right(k) then
if active_idx==1 then speed = bound(1,speed+1,99)
elseif active_idx==2 then difficulty = bound(1,difficulty+1,4) end
elseif menu_left(k) then
if active_idx==1 then speed = bound(1,speed-1,99)
elseif active_idx==2 then difficulty = bound(1,difficulty-1,4) end
elseif menu_enter(k) then
if active_idx == 3 then
if config.endless_speed ~= speed or config.endless_difficulty ~= difficulty then
config.endless_speed = speed
config.endless_difficulty = difficulty
gprint("saving settings...", unpack(main_menu_screen_pos))
wait()
write_conf_file()
end
ret = {items[active_idx][2], {speed, difficulty}}
elseif active_idx == 4 then
ret = {items[active_idx][2], items[active_idx][3]}
else
active_idx = wrap(1, active_idx + 1, #items)
end
elseif menu_escape(k) then
if active_idx == #items then
ret = {items[active_idx][2], items[active_idx][3]}
else
active_idx = #items
end
end
end)
if ret then
return unpack(ret)
end
end
end
function main_endless(...)
bg = IMG_stages[math.random(#IMG_stages)]
consuming_timesteps = true
replay.endless = {}
local replay=replay.endless
replay.pan_buf = ""
replay.in_buf = ""
replay.gpan_buf = ""
replay.mode = "endless"
P1 = Stack(1, "endless", config.panels_dir, ...)
P1.do_countdown = config.ready_countdown_1P or false
P1.enable_analytics = true
replay.do_countdown = P1.do_countdown or false
replay.speed = P1.speed
replay.difficulty = P1.difficulty
make_local_panels(P1, "000000")
make_local_gpanels(P1, "000000")
P1:starting_state()
while true do
P1:render()
wait()
if P1.game_over then
-- TODO: proper game over.
write_replay_file()
local end_text = "You scored "..P1.score.."\nin "..frames_to_time_string(P1.game_stopwatch, true)
analytics_game_ends()
return main_dumb_transition, {main_select_mode, end_text, 60}
end
variable_step(function() P1:local_run() end)
--groundhogday mode
--[[if P1.CLOCK == 1001 then
local prev_states = P1.prev_states
P1 = prev_states[600]
P1.prev_states = prev_states
end--]]
end
end
function main_time_attack(...)
bg = IMG_stages[math.random(#IMG_stages)]
consuming_timesteps = true
P1 = Stack(1, "time", config.panels_dir, ...)
P1.enable_analytics = true
make_local_panels(P1, "000000")
P1:starting_state()
while true do
P1:render()
wait()
if P1.game_over or (P1.game_stopwatch and P1.game_stopwatch == 120*60) then
-- TODO: proper game over.
local end_text = "You scored "..P1.score.."\nin "..frames_to_time_string(P1.game_stopwatch)
analytics_game_ends()
return main_dumb_transition, {main_select_mode, end_text, 30}
end
variable_step(function()
if (not P1.game_over) and P1.game_stopwatch and P1.game_stopwatch < 120 * 60 then
P1:local_run() end end)
end
end
function main_net_vs_room()
character_select_mode = "2p_net_vs"
return main_character_select()
end
-- fills the provided map based on the provided template and return the amount of pages. __Empty values will be replaced by character_ids
local function fill_map(template_map,map)
local X,Y = 5,7
local pages_amount = 0
local character_id_index = 1
while true do
-- new page handling
pages_amount = pages_amount+1
map[pages_amount] = deepcpy(template_map)
-- go through the page and replace __Empty with characters_ids_for_current_theme
for i=1,X do
for j=1,Y do
if map[pages_amount][i][j] == "__Empty" then
map[pages_amount][i][j] = characters_ids_for_current_theme[character_id_index]
character_id_index = character_id_index+1
-- end case: no more characters_ids_for_current_theme to add
if character_id_index == #characters_ids_for_current_theme+1 then
print("filled "..#characters_ids_for_current_theme.." characters across "..pages_amount.." page(s)")
return pages_amount
end
end
end
end
end
end
local fallback_when_missing = nil
local function refresh_based_on_own_mods(refreshed,ask_change_fallback)
ask_change_fallback = ask_change_fallback or false
if refreshed ~= nil then
if refreshed.panels_dir == nil or IMG_panels[refreshed.panels_dir] == nil then
refreshed.panels_dir = config.panels_dir
end
if characters[refreshed.character] == nil then
if refreshed.character_display_name and characters_ids_by_display_names[refreshed.character_display_name] then
refreshed.character = characters_ids_by_display_names[refreshed.character_display_name][1]
else
if not fallback_when_missing or ask_change_fallback then
fallback_when_missing = uniformly(characters_ids_for_current_theme)
end
refreshed.character = fallback_when_missing
end
end
end
end
local current_page = 1
function main_character_select()
love.audio.stop()
stop_the_music()
bg = charselect
fallback_when_missing = nil
local function add_client_data(state)
state.loaded = characters[state.character] and characters[state.character].fully_loaded
state.wants_ready = state.ready
end
local function refresh_loaded_and_ready(state_1,state_2)
state_1.loaded = characters[state_1.character] and characters[state_1.character].fully_loaded
state_2.loaded = characters[state_2.character] and characters[state_2.character].fully_loaded
if character_select_mode == "2p_net_vs" then
state_1.ready = state_1.wants_ready and state_1.loaded and state_2.loaded
else
state_1.ready = state_1.wants_ready and state_1.loaded
state_2.ready = state_2.wants_ready and state_2.loaded
end
end
print("character_select_mode = "..(character_select_mode or "nil"))
-- map is composed of special values prefixed by __ and character ids
local template_map = {}
local map = {}
if character_select_mode == "2p_net_vs" then
local opponent_connected = false
local retries, retry_limit = 0, 250
while not global_initialize_room_msg and retries < retry_limit do
for _,msg in ipairs(this_frame_messages) do
if msg.create_room or msg.character_select or msg.spectate_request_granted then
global_initialize_room_msg = msg
end
end
gprint("Waiting for room initialization...", unpack(main_menu_screen_pos))
wait()
if not do_messages() then
return main_dumb_transition, {main_select_mode, "Disconnected from server.\n\nReturning to main menu...", 60, 300}
end
retries = retries + 1
end
-- if room_number_last_spectated and retries >= retry_limit and currently_spectating then
-- request_spectate(room_number_last_spectated)
-- retries = 0
-- while not global_initialize_room_msg and retries < retry_limit do
-- for _,msg in ipairs(this_frame_messages) do
-- if msg.create_room or msg.character_select or msg.spectate_request_granted then
-- global_initialize_room_msg = msg
-- end
-- end
-- gprint("Lost connection. Trying to rejoin...", unpack(main_menu_screen_pos))
-- wait()
-- if not do_messages() then
-- return main_dumb_transition, {main_select_mode, "Disconnected from server.\n\nReturning to main menu...", 60, 300}
-- end
-- retries = retries + 1
-- end
-- end
if not global_initialize_room_msg then
return main_dumb_transition, {main_select_mode, "Room initialization failed.\n\nReturning to main menu...", 60, 300}
end
msg = global_initialize_room_msg
global_initialize_room_msg = nil
if msg.ratings then
global_current_room_ratings = msg.ratings
end
if msg.your_player_number then
my_player_number = msg.your_player_number
elseif currently_spectating then
my_player_number = 1
elseif my_player_number and my_player_number ~= 0 then
print("We assumed our player number is still "..my_player_number)
else
error("We never heard from the server as to what player number we are")
print("Error: The server never told us our player number. Assuming it is 1")
my_player_number = 1
end
if msg.op_player_number then
op_player_number = msg.op_player_number or op_player_number
elseif currently_spectating then
op_player_number = 2
elseif op_player_number and op_player_number ~= 0 then
print("We assumed op player number is still "..op_player_number)
else
error("We never heard from the server as to what player number we are")
print("Error: The server never told us our player number. Assuming it is 2")
op_player_number = 2
end
if my_player_number == 2 and msg.a_menu_state ~= nil and msg.b_menu_state ~= nil then
print("inverting the states to match player number!")
msg.a_menu_state, msg.b_menu_state = msg.b_menu_state, msg.a_menu_state
end
global_my_state = msg.a_menu_state
refresh_based_on_own_mods(global_my_state)
global_op_state = msg.b_menu_state
refresh_based_on_own_mods(global_op_state)
if msg.win_counts then
update_win_counts(msg.win_counts)
end
if msg.replay_of_match_so_far then
replay_of_match_so_far = msg.replay_of_match_so_far
end
if msg.ranked then
match_type = "Ranked"
match_type_message = ""
else
match_type = "Casual"
end
if currently_spectating then
P1 = {panel_buffer="", gpanel_buffer=""}
print("we reset P1 buffers at start of main_character_select()")
end
P2 = {panel_buffer="", gpanel_buffer=""}
print("we reset P2 buffers at start of main_character_select()")
print("current_server_supports_ranking: "..tostring(current_server_supports_ranking))
if current_server_supports_ranking then
template_map = {{"__Mode", "__Mode", "__Level", "__Level", "__Panels", "__Panels", "__Ready"},
{"__Random", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Leave"}}
else
template_map = {{"__Level", "__Level", "__Level", "__Panels", "__Panels", "__Panels", "__Ready"},
{"__Random", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Leave"}}
end
end
if character_select_mode == "2p_local_vs" or character_select_mode == "1p_vs_yourself" then
template_map = {{"__Level", "__Level", "__Level", "__Panels", "__Panels", "__Panels", "__Ready"},
{"__Random", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty"},
{"__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Empty", "__Leave"}}
end
local pages_amount = fill_map(template_map, map)
if current_page > pages_amount then
current_page = 1
end
op_win_count = op_win_count or 0
if character_select_mode == "2p_net_vs" then
global_current_room_ratings = global_current_room_ratings or {{new=0,old=0,difference=0},{new=0,old=0,difference=0}}
my_expected_win_ratio = nil
op_expected_win_ratio = nil
print("my_player_number = "..my_player_number)
print("op_player_number = "..op_player_number)
if global_current_room_ratings[my_player_number].new
and global_current_room_ratings[my_player_number].new ~= 0
and global_current_room_ratings[op_player_number]
and global_current_room_ratings[op_player_number].new ~= 0 then
my_expected_win_ratio = (100*round(1/(1+10^
((global_current_room_ratings[op_player_number].new
-global_current_room_ratings[my_player_number].new)
/RATING_SPREAD_MODIFIER))
,2))
op_expected_win_ratio = (100*round(1/(1+10^
((global_current_room_ratings[my_player_number].new
-global_current_room_ratings[op_player_number].new)
/RATING_SPREAD_MODIFIER))
,2))
end
match_type = match_type or "Casual"
if match_type == "" then match_type = "Casual" end
end
match_type_message = match_type_message or ""
local function do_leave()
my_win_count = 0
op_win_count = 0
return json_send({leave_room=true})
end
-- be wary: name_to_xy_per_page is kinda buggy for larger blocks as they span multiple positions (we retain the last one), and is completely broken with __Empty
local name_to_xy_per_page = {}
local X,Y = 5,7
for p=1,pages_amount do
name_to_xy_per_page[p] = {}
for i=1,X do
for j=1,Y do
if map[p][i][j] then
name_to_xy_per_page[p][map[p][i][j]] = {i,j}
end
end
end
end
my_win_count = my_win_count or 0
local cursor_data = {{position=shallowcpy(name_to_xy_per_page[current_page]["__Ready"]),selected=false},{position=shallowcpy(name_to_xy_per_page[current_page]["__Ready"]),selected=false}}
if global_my_state ~= nil then
cursor_data[1].state = shallowcpy(global_my_state)
global_my_state = nil
else
cursor_data[1].state = {character=config.character, character_display_name=characters[config.character].display_name, level=config.level, panels_dir=config.panels_dir, cursor="__Ready", ready=false, ranked=config.ranked}
end
if global_op_state ~= nil then
cursor_data[2].state = shallowcpy(global_op_state)
if character_select_mode ~= "2p_local_vs" then
global_op_state = nil -- retains state of the second player, also: don't unload its character when going back and forth
end
else
cursor_data[2].state = {character=config.character, character_display_name=characters[config.character].display_name, level=config.level, panels_dir=config.panels_dir, cursor="__Ready", ready=false, ranked=false}
end
add_client_data(cursor_data[1].state)
add_client_data(cursor_data[2].state)
refresh_loaded_and_ready(cursor_data[1].state, cursor_data[2].state)
local prev_state = shallowcpy(cursor_data[1].state)
local function draw_button(x,y,w,h,str,halign,valign,no_rect)
no_rect = no_rect or str == "__Empty"
halign = halign or "center"
valign = valign or "top"
local menu_width = Y*100
local menu_height = X*80
local spacing = 8
local text_height = 13
local x_padding = math.floor((canvas_width-menu_width)/2)
local y_padding = math.floor((canvas_height-menu_height)/2)
set_color(unpack(colors.white))
render_x = x_padding+(y-1)*100+spacing
render_y = y_padding+(x-1)*100+spacing
button_width = w*100-2*spacing
button_height = h*100-2*spacing
if no_rect == false then
grectangle("line", render_x, render_y, button_width, button_height)
end
local character = characters[str]
if str == "P1" then
character = characters[cursor_data[1].state.character]
elseif str == "P2" then
character = characters[cursor_data[2].state.character]
end
local width_for_alignment = button_width
local x_add,y_add = 0,0
if valign == "center" then
y_add = math.floor(0.5*button_height-0.5*text_height)-3
elseif valign == "bottom" then
y_add = math.floor(button_height-text_height)
end
if character and character.images["icon"] then
x_add = 0.025*button_width
width_for_alignment = 0.95*button_width
local orig_w, orig_h = character.images["icon"]:getDimensions()
local scale = button_width/math.max(orig_w,orig_h) -- keep image ratio
menu_drawf(character.images["icon"], render_x+0.5*button_width, render_y+0.5*button_height,"center","center", 0, scale, scale )
end
local function draw_cursor(button_height, spacing, player_num,ready)
local cur_blink_frequency = 4
local cur_pos_change_frequency = 8
local draw_cur_this_frame = false
local cursor_frame = 1
if ready then
if (math.floor(menu_clock/cur_blink_frequency)+player_num)%2+1 == player_num then
draw_cur_this_frame = true
end
else
draw_cur_this_frame = true
cursor_frame = (math.floor(menu_clock/cur_pos_change_frequency)+player_num)%2+1
end
if draw_cur_this_frame then
local cur_img = IMG_char_sel_cursors[player_num][cursor_frame]
local cur_img_left = IMG_char_sel_cursor_halves.left[player_num][cursor_frame]
local cur_img_right = IMG_char_sel_cursor_halves.right[player_num][cursor_frame]
local cur_img_w, cur_img_h = cur_img:getDimensions()
local cursor_scale = (button_height+(spacing*2))/cur_img_h
menu_drawq(cur_img, cur_img_left, render_x-spacing, render_y-spacing, 0, cursor_scale , cursor_scale)
menu_drawq(cur_img, cur_img_right, render_x+button_width+spacing-cur_img_w*cursor_scale/2, render_y-spacing, 0, cursor_scale, cursor_scale)
end
end
local function draw_player_state(cursor_data,player_number)
if characters[cursor_data.state.character] and not characters[cursor_data.state.character].fully_loaded then
menu_drawf(IMG_loading, render_x+button_width*0.5, render_y+button_height*0.5, "center", "center" )
elseif cursor_data.state.wants_ready then
menu_drawf(IMG_ready, render_x+button_width*0.5, render_y+button_height*0.5, "center", "center" )
end
local scale = 0.25*button_width/math.max(IMG_players[player_number]:getWidth(),IMG_players[player_number]:getHeight()) -- keep image ratio
menu_drawf(IMG_players[player_number], render_x+1, render_y+button_height-1, "left", "bottom", 0, scale, scale )
scale = 0.25*button_width/math.max(IMG_levels[cursor_data.state.level]:getWidth(),IMG_levels[cursor_data.state.level]:getHeight()) -- keep image ratio
menu_drawf(IMG_levels[cursor_data.state.level], render_x+button_width-1, render_y+button_height-1, "right", "bottom", 0, scale, scale )
end
local function draw_panels(cursor_data,player_number,y_padding)
local panels_max_width = 0.25*button_height
local panels_width = math.min(panels_max_width,IMG_panels[cursor_data.state.panels_dir][1][1]:getWidth())
local padding_x = 0.5*button_width-3*panels_width -- center them, not 3.5 mysteriously?
if cursor_data.state.level >= 9 then
padding_x = padding_x-0.5*panels_width
end
local is_selected = cursor_data.selected and cursor_data.state.cursor == "__Panels"
if is_selected then
padding_x = padding_x-panels_width
end
local panels_scale = panels_width/IMG_panels[cursor_data.state.panels_dir][1][1]:getWidth()
menu_drawf(IMG_players[player_number], render_x+padding_x, render_y+y_padding, "center", "center" )
padding_x = padding_x + panels_width
if is_selected then
gprintf("<", render_x+padding_x-0.5*panels_width, render_y+y_padding-0.5*text_height,panels_width,"center")
padding_x = padding_x + panels_width
end
for i=1,8 do
if i ~= 7 and (i ~= 6 or cursor_data.state.level >= 9) then
menu_drawf(IMG_panels[cursor_data.state.panels_dir][i][1], render_x+padding_x, render_y+y_padding, "center", "center", 0, panels_scale, panels_scale )
padding_x = padding_x + panels_width
end
end
if is_selected then
gprintf(">", render_x+padding_x-0.5*panels_width, render_y+y_padding-0.5*text_height,panels_width,"center")
end
end
local function draw_levels(cursor_data,player_number,y_padding)
local level_max_width = 0.2*button_height
local level_width = math.min(level_max_width,IMG_levels[1]:getWidth())
local padding_x = 0.5*button_width-6*level_width
local is_selected = cursor_data.selected and cursor_data.state.cursor == "__Level"
if is_selected then
padding_x = padding_x-level_width
end
local level_scale = level_width/IMG_levels[1]:getWidth()
menu_drawf(IMG_players[player_number], render_x+padding_x, render_y+y_padding, "center", "center" )
local ex_scaling = level_width/IMG_levels[11]:getWidth()
menu_drawf(IMG_players[player_number], render_x+padding_x, render_y+y_padding, "center", "center")
padding_x = padding_x + level_width + 1 -- [[Thank you Eole!]]
if is_selected then
gprintf("<", render_x+padding_x-0.5*level_width, render_y+y_padding-0.5*text_height,level_width,"center")
padding_x = padding_x + level_width
end
for i=1,11 do
local use_unfocus = cursor_data.state.level < i
if use_unfocus then
menu_drawf(IMG_levels_unfocus[i], render_x+padding_x, render_y+y_padding, "center", "center", 0, (i == 11 and ex_scaling or level_scale), (i == 11 and ex_scaling or level_scale))
--[[if i >= 11 then
menu_drawf(IMG_levels_unfocus[i], render_x+padding_x, render_y+y_padding, "center", "center", 0, ex_scaling, ex_scaling)
end]]
else
menu_drawf(IMG_levels[i], render_x+padding_x, render_y+y_padding, "center", "center", 0, (i == 11 and ex_scaling or level_scale), (i == 11 and ex_scaling or level_scale))
--[[if i >= 11 then
menu_drawf(IMG_levels[i], render_x+padding_x, render_y+y_padding, "center", "center", 0, ex_scaling, ex_scaling)
end]]
end
padding_x = padding_x + level_width + 1
--[[if i == 11 then
padding_x = padding_x + 16
end]]
end
if is_selected then
gprintf(">", render_x+padding_x-0.5*level_width, render_y+y_padding-0.5*text_height,level_width,"center")
end
end
local function draw_match_type(cursor_data,player_number,y_padding)
local padding_x = math.floor(0.5*button_width - IMG_players[player_number]:getWidth()*0.5 - 46) -- ty GIMP; no way to know the size of the text?
menu_drawf(IMG_players[player_number], render_x+padding_x, render_y+y_padding, "center", "center" )
padding_x = padding_x+IMG_players[player_number]:getWidth()
local to_print
if cursor_data.state.ranked then
to_print = "casual [ranked]"
else
to_print = "[casual] ranked"
end
if cursor_data.state.level >= 11 then
to_print = "[EX Mode]"
end
gprint(to_print, render_x+padding_x, render_y+y_padding-0.5*text_height-1)
end
local pstr
if string.sub(str, 1, 2) == "__" then
pstr = string.sub(str, 3)
end
if str == "__Mode" then
if (character_select_mode == "2p_net_vs" or character_select_mode == "2p_local_vs") then
draw_match_type(cursor_data[1],1,0.4*button_height)
draw_match_type(cursor_data[2],2,0.7*button_height)
else
draw_match_type(cursor_data[1],1,0.5*button_height)
end
elseif str == "__Panels" then
if (character_select_mode == "2p_net_vs" or character_select_mode == "2p_local_vs") then
draw_panels(cursor_data[1],1,0.4*button_height)
draw_panels(cursor_data[2],2,0.7*button_height)
else
draw_panels(cursor_data[1],1,0.5*button_height)
end
elseif str == "__Level" then
if (character_select_mode == "2p_net_vs" or character_select_mode == "2p_local_vs") then
draw_levels(cursor_data[1],1,0.4*button_height)
draw_levels(cursor_data[2],2,0.7*button_height)
else
draw_levels(cursor_data[1],1,0.5*button_height)
end
elseif str == "P1" then
draw_player_state(cursor_data[1],1)
pstr = my_name
elseif str == "P2" then
draw_player_state(cursor_data[2],2)
pstr = op_name
elseif character then
pstr = character.display_name
elseif string.sub(str, 1, 2) ~= "__" then
pstr = str:gsub("^%l", string.upper)
end
if x ~= 0 then
if cursor_data[1].state and cursor_data[1].state.cursor == str
and ( str ~= "__Empty" or ( cursor_data[1].position[1] == x and cursor_data[1].position[2] == y ) ) then
draw_cursor(button_height, spacing, 1, cursor_data[1].state.ready)
end
if (character_select_mode == "2p_net_vs" or character_select_mode == "2p_local_vs")
and cursor_data[2].state and cursor_data[2].state.cursor == str
and ( str ~= "__Empty" or ( cursor_data[2].position[1] == x and cursor_data[2].position[2] == y ) ) then
draw_cursor(button_height, spacing, 2, cursor_data[2].state.ready)
end
end
if str ~= "__Empty" then
gprintf(pstr, render_x+x_add, render_y+y_add,width_for_alignment,halign)
end
end
print("got to LOC before net_vs_room character select loop")
menu_clock = 0
while true do
if character_select_mode == "2p_net_vs" then
for _,msg in ipairs(this_frame_messages) do
if msg.win_counts then
update_win_counts(msg.win_counts)
end
if msg.menu_state then
if currently_spectating then
if msg.player_number == 1 or msg.player_number == 2 then
cursor_data[msg.player_number].state = msg.menu_state
refresh_based_on_own_mods(cursor_data[msg.player_number].state)
character_loader_load(cursor_data[msg.player_number].state.character)
end
else
cursor_data[2].state = msg.menu_state
refresh_based_on_own_mods(cursor_data[2].state)
character_loader_load(cursor_data[2].state.character)
end
refresh_loaded_and_ready(cursor_data[1],cursor_data[2])
end
if msg.ranked_match_approved then
match_type = "Ranked"
match_type_message = ""
if msg.caveats then
match_type_message = match_type_message..(msg.caveats[1] or "")
end
elseif msg.ranked_match_denied then
match_type = "Casual"
match_type_message = "Not ranked. "
if msg.reasons then
match_type_message = match_type_message..(msg.reasons[1] or "Reason unknown")
end
--[[elseif msg.ranked_match_denied and cursor_data.state.level >= 11 then
match_type = "Casual"
match_type_message = "EX Mode Activated "
if msg.reasons then
match_type_message = match_type_message..(msg.reasons[1] or "Reason unknown")
end]]
end
if msg.leave_room then
my_win_count = 0
op_win_count = 0
return main_net_vs_lobby
end
if msg.match_start or replay_of_match_so_far then
print("currently_spectating: "..tostring(currently_spectating))
local fake_P1 = P1
local fake_P2 = P2
refresh_based_on_own_mods(msg.opponent_settings)
refresh_based_on_own_mods(msg.player_settings, true)
-- mainly for spectator mode, those characters have already been loaded otherwise
character_loader_load(msg.player_settings.character)
character_loader_load(msg.opponent_settings.character)
character_loader_wait()
P1 = Stack(1, "vs", msg.player_settings.panels_dir, msg.player_settings.level, msg.player_settings.character, msg.player_settings.player_number)
P1.enable_analytics = not currently_spectating and not replay_of_match_so_far
P2 = Stack(2, "vs", msg.opponent_settings.panels_dir, msg.opponent_settings.level, msg.opponent_settings.character, msg.opponent_settings.player_number)
if currently_spectating then
P1.panel_buffer = fake_P1.panel_buffer
P1.gpanel_buffer = fake_P1.gpanel_buffer
end
P2.panel_buffer = fake_P2.panel_buffer
P2.gpanel_buffer = fake_P2.gpanel_buffer
P1.garbage_target = P2
P2.garbage_target = P1
move_stack(P2,2)
replay.vs = {P="",O="",I="",Q="",R="",in_buf="",
P1_level=P1.level,P2_level=P2.level,
P1_name=my_name, P2_name=op_name,
P1_char=P1.character,P2_char=P2.character,
ranked=msg.ranked, do_countdown=true}
if currently_spectating and replay_of_match_so_far then --we joined a match in progress
replay.vs = replay_of_match_so_far.vs
P1.input_buffer = replay_of_match_so_far.vs.in_buf
P1.panel_buffer = replay_of_match_so_far.vs.P
P1.gpanel_buffer = replay_of_match_so_far.vs.Q
P2.input_buffer = replay_of_match_so_far.vs.I
P2.panel_buffer = replay_of_match_so_far.vs.O
P2.gpanel_buffer = replay_of_match_so_far.vs.R
if replay.vs.ranked then
match_type = "Ranked"
match_type_message = ""
else
match_type = "Casual"
end
replay_of_match_so_far = nil
P1.play_to_end = true --this makes foreign_run run until caught up
P2.play_to_end = true
end
if not currently_spectating then
ask_for_gpanels("000000")
ask_for_panels("000000")
end
to_print = "Game is starting!\n".."Level: "..P1.level.."\nOpponent's level: "..P2.level
if P1.play_to_end or P2.play_to_end then
to_print = "Joined a match in progress.\nCatching up..."
end
for i=1,30 do
gprint(to_print,unpack(main_menu_screen_pos))
if not do_messages() then
return main_dumb_transition, {main_select_mode, "Disconnected from server.\n\nReturning to main menu...", 60, 300}
end
wait()
end
local game_start_timeout = 0