forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainloop.lua
More file actions
1700 lines (1629 loc) · 59.2 KB
/
mainloop.lua
File metadata and controls
1700 lines (1629 loc) · 59.2 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
require("panels")
require("theme")
require("click_menu")
local select_screen = require("select_screen")
local replay_browser = require("replay_browser")
local options = require("options")
local utf8 = require("utf8")
local analytics = require("analytics")
local wait, resume = coroutine.yield, coroutine.resume
local playground, main_endless, make_main_puzzle, main_net_vs_setup,
main_config_input, main_select_puzz,
main_local_vs_setup, main_set_name, main_local_vs_yourself_setup,
main_options, main_music_test,
main_replay_browser, exit_game
-- main_select_mode, main_dumb_transition, main_net_vs, main_net_vs_lobby, main_local_vs_yourself, main_local_vs, main_replay_endless, main_replay_puzzle, main_replay_vs are not local since they are also used elsewhere
local PLAYING = "playing" -- room states
local CHARACTERSELECT = "character select" --room states
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
main_menu_screen_pos = { 300 + (canvas_width-legacy_canvas_width)/2, 220 + (canvas_height-legacy_canvas_height)/2 }
wait_game_update = nil
has_game_update = false
local arrow_padding = 12
P1_win_quads = {}
P1_rating_quads = {}
P1_health_quad = {}
P2_rating_quads = {}
P2_win_quads = {}
P2_health_quad = {}
function fmainloop()
local func, arg = main_select_mode, nil
replay = {}
gprint("Reading config file", unpack(main_menu_screen_pos))
wait()
read_conf_file()
local x, y, display = love.window.getPosition()
love.window.setPosition( config.window_x or x, config.window_y or y, config.display or display )
love.window.setFullscreen(config.fullscreen or false)
love.window.setVSync( config.vsync and 1 or 0 )
gprint("Loading localization...", unpack(main_menu_screen_pos))
wait()
Localization.init(localization)
gprint(loc("ld_puzzles"), unpack(main_menu_screen_pos))
wait()
copy_file("readme_puzzles.txt", "puzzles/README.txt")
gprint(loc("ld_replay"), unpack(main_menu_screen_pos))
wait()
read_replay_file()
gprint(loc("ld_theme"), unpack(main_menu_screen_pos))
wait()
theme_init()
-- stages and panels before characters since they are part of their loading!
gprint(loc("ld_stages"), unpack(main_menu_screen_pos))
wait()
stages_init()
gprint(loc("ld_panels"), unpack(main_menu_screen_pos))
wait()
panels_init()
gprint(loc("ld_characters"), unpack(main_menu_screen_pos))
wait()
characters_init()
gprint(loc("ld_analytics"), unpack(main_menu_screen_pos))
wait()
analytics.init()
apply_config_volume()
-- create folders in appdata for those who don't have them already
love.filesystem.createDirectory("characters")
love.filesystem.createDirectory("panels")
love.filesystem.createDirectory("themes")
love.filesystem.createDirectory("stages")
if GAME_UPDATER_CHECK_UPDATE_INGAME then
wait_game_update = GAME_UPDATER:async_download_latest_version()
end
while true do
leftover_time = 1/120
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_released_keys = {}
this_frame_unicodes = {}
leftover_time = leftover_time - 1/60
end
end
end
do
function main_select_mode()
click_menus = {}
currently_spectating = false
if themes[config.theme].musics["main"] then
find_and_add_music(themes[config.theme].musics, "main")
end
character_loader_clear()
stage_loader_clear()
close_socket()
background = themes[config.theme].images.bg_main
reset_filters()
logged_in = 0
connection_up_time = 0
connected_server_ip = ""
current_server_supports_ranking = false
match_type = ""
match_type_message = ""
local items = {
--{"playground", main_select_speed_99, {playground}},
{loc("mm_1_endless"), main_select_speed_99, {main_endless}},
{loc("mm_1_puzzle"), main_select_puzz},
{loc("mm_1_time"), main_select_speed_99, {main_time_attack}},
{loc("mm_1_vs"), main_local_vs_yourself_setup},
--{loc("mm_2_vs_online", "burke.ro"), main_net_vs_setup, {"burke.ro"}},
{loc("mm_2_vs_online", "Jon's server"), main_net_vs_setup, {"18.188.43.50"}},
--{loc("mm_2_vs_online", "betaserver.panelattack.com"), main_net_vs_setup, {"betaserver.panelattack.com"}},
--{loc("mm_2_vs_online", "(USE ONLY WITH OTHER CLIENTS ON THIS TEST BUILD 025beta)"), main_net_vs_setup, {"18.188.43.50"}},
--{loc("mm_2_vs_online", "This test build is for offline-use only"), main_select_mode},
--{loc("mm_2_vs_online", "domi1819.xyz"), main_net_vs_setup, {"domi1819.xyz"}},
--{loc("mm_2_vs_online", "(development-use only)"), main_net_vs_setup, {"localhost"}},
--{loc("mm_2_vs_online", "LittleEndu's server"), main_net_vs_setup, {"51.15.207.223"}},
{loc("mm_2_vs_online", "server for ranked Ex Mode"), main_net_vs_setup, {"exserver.panelattack.com",49568}},
{loc("mm_2_vs_local"), main_local_vs_setup},
--{loc("mm_replay_of", loc("mm_1_endless")), main_replay_endless},
--{loc("mm_replay_of", loc("mm_1_puzzle")), main_replay_puzzle},
--{loc("mm_replay_of", loc("mm_2_vs")), main_replay_vs},
{loc("mm_replay_browser"), replay_browser.main},
{loc("mm_configure"), main_config_input},
{loc("mm_set_name"), main_set_name},
{loc("mm_options"), options.main},
{loc("mm_music_test"), main_music_test}
}
if love.graphics.getSupported("canvas") then
items[#items+1] = {loc("mm_fullscreen", "(LAlt+Enter)"), fullscreen}
else
items[#items+1] = {loc("mm_no_support_fullscreen"), main_select_mode}
end
items[#items+1] = {loc("mm_quit"), exit_game }
local k = K[1]
local menu_x, menu_y = unpack(main_menu_screen_pos)
local main_menu = Click_menu(nil, menu_x, menu_y, nil, love.graphics.getHeight()-menu_y-80, 8, 1, true, 2)
for i=1,#items do
main_menu:add_button(items[i][1])
end
while true do
main_menu:draw()
if wait_game_update ~= nil then
has_game_update = wait_game_update:pop()
if has_game_update ~= nil and has_game_update then
wait_game_update = nil
GAME_UPDATER_GAME_VERSION = "NEW VERSION FOUND! RESTART THE GAME!"
end
end
if GAME_UPDATER_GAME_VERSION then
gprintf("version: "..GAME_UPDATER_GAME_VERSION, -2, 705, canvas_width, "right")
if has_game_update then
menu_draw(panels[config.panels].images.classic[1][1], 1262, 685)
end
end
wait()
local ret = nil
variable_step(function()
if menu_up(k) then
main_menu:set_active_idx(wrap(1, main_menu.active_idx-1, #items))
elseif menu_down(k) then
main_menu:set_active_idx(wrap(1, main_menu.active_idx+1, #items))
elseif menu_enter(k) then
ret = {items[main_menu.active_idx][2], items[main_menu.active_idx][3]}
elseif menu_escape(k) then
if main_menu.active_idx == #items then
ret = {items[main_menu.active_idx][2], items[main_menu.active_idx][3]}
else
main_menu:set_active_idx(#items)
end
end
end)
if ret then
return unpack(ret)
end
if main_menu.idx_selected then
local active_idx = main_menu.idx_selected
main_menu.idx_selected = nil
main_menu:remove_self()
return items[active_idx][2], items[active_idx][3]
end
end
end
end
function main_select_speed_99(next_func, ...)
local difficulties = {"Easy", "Normal", "Hard", "EX Mode"}
local loc_difficulties = { loc("easy"), loc("normal"), loc("hard"), "EX Mode" } -- TODO: localize "EX Mode"
local items = {{"Speed"},
{"Difficulty"},
{"Go!", next_func},
{"Back", main_select_mode}}
local loc_items = {loc("speed"), loc("difficulty"), loc("go_"), loc("back")}
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 .. " " .. loc_items[i] .. "\n"
end
to_print2 = " " .. speed .. "\n "
.. loc_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
stop_the_music()
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
local function use_current_stage()
stage_loader_load(current_stage)
stage_loader_wait()
background = stages[current_stage].images.background
background_overlay = themes[config.theme].images.bg_overlay
foreground_overlay = themes[config.theme].images.fg_overlay
end
local function pick_random_stage()
current_stage = uniformly(stages_ids_for_current_theme)
if stages[current_stage]:is_bundle() then -- may pick a bundle!
current_stage = uniformly(stages[current_stage].sub_stages)
end
use_current_stage()
end
local function pick_use_music_from()
if config.use_music_from == "stage" or config.use_music_from == "characters" then
current_use_music_from = config.use_music_from
return
end
local percent = math.random(1,4)
if config.use_music_from == "either" then
current_use_music_from = percent <= 2 and "stage" or "characters"
elseif config.use_music_from == "often_stage" then
current_use_music_from = percent == 1 and "characters" or "stage"
else
current_use_music_from = percent == 1 and "stage" or "characters"
end
end
function Stack.wait_for_random_character(self)
if self.character == random_character_special_value then
self.character = uniformly(characters_ids_for_current_theme)
elseif characters[self.character]:is_bundle() then -- may have picked a bundle
self.character = uniformly(characters[self.character].sub_characters)
end
character_loader_load(self.character)
character_loader_wait()
end
function Stack.handle_pause(self)
local k = K[self.which]
if self.wait_for_not_pausing then
if not keys[k.pause] and not this_frame_keys[k.pause] then
self.wait_for_not_pausing = false
else
return
end
end
if keys[k.pause] or this_frame_keys[k.pause] then
game_is_paused = not game_is_paused
self.wait_for_not_pausing = true
if game_is_paused then
stop_the_music()
end
end
end
function main_endless(...)
pick_random_stage()
pick_use_music_from()
replay.endless = {}
local replay=replay.endless
replay.pan_buf = ""
replay.in_buf = ""
replay.gpan_buf = ""
replay.mode = "endless"
P1 = Stack(1, "endless", config.panels, ...)
P1:wait_for_random_character()
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
replay.cur_wait_time = P1.cur_wait_time or default_input_repeat_delay
make_local_panels(P1, "000000")
make_local_gpanels(P1, "000000")
P1:starting_state()
while true do
if game_is_paused then
draw_pause()
else
P1:render()
end
wait()
if P1.game_over then
-- TODO: proper game over.
write_replay_file()
local end_text = loc("rp_score", P1.score, frames_to_time_string(P1.game_stopwatch, true))
analytics.game_ends()
return main_dumb_transition, {main_select_mode, end_text, 0, -1, P1:pick_win_sfx()}
end
variable_step(function()
P1:local_run()
P1:handle_pause()
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(...)
pick_random_stage()
pick_use_music_from()
P1 = Stack(1, "time", config.panels, ...)
P1:wait_for_random_character()
P1.enable_analytics = true
make_local_panels(P1, "000000")
P1:starting_state()
while true do
if game_is_paused then
draw_pause()
else
P1:render()
end
wait()
if P1.game_over or (P1.game_stopwatch and P1.game_stopwatch == 120*60) then
-- TODO: proper game over.
local end_text = loc("rp_score", P1.score, frames_to_time_string(P1.game_stopwatch))
analytics.game_ends()
return main_dumb_transition, {main_select_mode, end_text, 30, -1, P1:pick_win_sfx()}
end
variable_step(function()
if not P1.game_over and P1.game_stopwatch and P1.game_stopwatch < 120 * 60 then
P1:local_run()
P1:handle_pause()
end
end)
end
end
function main_net_vs_room()
select_screen.character_select_mode = "2p_net_vs"
return select_screen.main()
end
function main_net_vs_lobby()
if themes[config.theme].musics.main then
find_and_add_music(themes[config.theme].musics, "main")
end
background = themes[config.theme].images.bg_main
reset_filters()
character_loader_clear()
stage_loader_clear()
local active_name, active_idx, active_back = "", 1
local items
local unpaired_players = {} -- list
local willing_players = {} -- set
local spectatable_rooms = {}
local k = K[1]
my_player_number = nil
op_player_number = nil
local notice = {[true]=loc("lb_select_player"), [false]=loc("lb_alone")}
local leaderboard_string = ""
local my_rank
match_type = ""
match_type_message = ""
--attempt login
read_user_id_file()
if not my_user_id then
my_user_id = "need a new user id"
end
local login_status_message = " "..loc("lb_login")
local login_status_message_duration = 2
local login_denied = false
local prev_act_idx = active_idx
local showing_leaderboard = false
local lobby_menu_x = {[true]=main_menu_screen_pos[1]-200, [false]=main_menu_screen_pos[1]} --will be used to make room in case the leaderboard should be shown.
local lobby_menu_y = main_menu_screen_pos[2] + 50
local sent_requests = {}
if connection_up_time <= login_status_message_duration then
json_send({login_request=true, user_id=my_user_id})
end
local lobby_menu = Click_menu()
local items = {}
local lastPlayerIndex = 0
local updated = false
while true do
if connection_up_time <= login_status_message_duration then
gprint(login_status_message, lobby_menu_x[showing_leaderboard], lobby_menu_y-120)
local messages = server_queue:pop_all_with("login_successful", "login_denied")
for _,msg in ipairs(messages) do
if msg.login_successful then
current_server_supports_ranking = true
logged_in = true
if msg.new_user_id then
my_user_id = msg.new_user_id
print("about to write user id file")
write_user_id_file()
login_status_message = loc("lb_user_new", my_name)
elseif msg.name_changed then
login_status_message = loc("lb_user_update", msg.old_name, msg.new_name)
login_status_message_duration = 5
else
login_status_message = loc("lb_welcome_back", my_name)
end
elseif msg.login_denied then
current_server_supports_ranking = true
login_denied = true
--TODO: create a menu here to let the user choose "continue unranked" or "get a new user_id"
--login_status_message = "Login for ranked matches failed.\n"..msg.reason.."\n\nYou may continue unranked,\nor delete your invalid user_id file to have a new one assigned."
login_status_message_duration = 10
return main_dumb_transition, {main_select_mode, loc("lb_error_msg").."\n\n"..json.encode(msg),60,600}
end
end
if connection_up_time == 2 and not current_server_supports_ranking then
login_status_message = loc("lb_login_timeout")
login_status_message_duration = 7
end
end
local messages = server_queue:pop_all_with("choose_another_name", "create_room", "unpaired", "game_request", "leaderboard_report", "spectate_request_granted")
for _,msg in ipairs(messages) do
updated = true
items = {}
if msg.choose_another_name and msg.choose_another_name.used_names then
return main_dumb_transition, {main_select_mode, loc("lb_used_name"), 60, 600}
elseif msg.choose_another_name and msg.choose_another_name.reason then
return main_dumb_transition, {main_select_mode, "Error: ".. msg.choose_another_name.reason, 60, 300}
end
if msg.create_room or msg.spectate_request_granted then
global_initialize_room_msg = msg
select_screen.character_select_mode = "2p_net_vs"
love.window.requestAttention()
play_optional_sfx(themes[config.theme].sounds.notification)
return select_screen.main
end
if msg.unpaired then
unpaired_players = msg.unpaired
-- players who leave the unpaired list no longer have standing invitations to us.\
-- we also no longer have a standing invitation to them, so we'll remove them from sent_requests
local new_willing = {}
local new_sent_requests = {}
for _,player in ipairs(unpaired_players) do
new_willing[player] = willing_players[player]
new_sent_requests[player] = sent_requests[player]
end
willing_players = new_willing
sent_requests = new_sent_requests
if msg.spectatable then
spectatable_rooms = msg.spectatable
end
end
if msg.game_request then
willing_players[msg.game_request.sender] = true
love.window.requestAttention()
play_optional_sfx(themes[config.theme].sounds.notification)
end
if msg.leaderboard_report then
showing_leaderboard = true
lobby_menu:show_controls(true)
leaderboard_report = msg.leaderboard_report
for k,v in ipairs(leaderboard_report) do
if v.is_you then
my_rank = k
end
end
leaderboard_first_idx_to_show = math.max((my_rank or 1)-8,1)
leaderboard_last_idx_to_show = math.min(leaderboard_first_idx_to_show + 20,#leaderboard_report)
leaderboard_string = build_viewable_leaderboard_string(leaderboard_report, leaderboard_first_idx_to_show, leaderboard_last_idx_to_show)
end
end
local print_x, print_y = unpack(main_menu_screen_pos)
local to_print = ""
local arrow = ""
if updated then
local last_lobby_menu_active_idx = lobby_menu.active_idx
lobby_menu:remove_self()
items = {}
for _,v in ipairs(unpaired_players) do
if v ~= config.name then
items[#items+1] = v
end
end
lastPlayerIndex = #items --the rest of the items will be spectatable rooms, except the last two items (leaderboard and back to main menu)
for _,v in ipairs(spectatable_rooms) do
items[#items+1] = v
end
if showing_leaderboard then
items[#items+1] = loc("lb_hide_board")
else
items[#items+1] = loc("lb_show_board") -- the second to last item is "Leaderboard"
end
items[#items+1] = loc("lb_back") -- the last item is "Back to the main menu"
local items_to_print = {}
for i=1,#items do
if i <= lastPlayerIndex then
items_to_print[i] = items[i] ..(sent_requests[items[i]] and " "..loc("lb_request") or "").. (willing_players[items[i]] and " "..loc("lb_received") or "")
elseif i < #items - 1 and items[i].name then
items_to_print[i] = loc("lb_spectate").." " .. items[i].name .. " (".. items[i].state .. ")" --printing room names
elseif i < #items then
items_to_print[i] = items[i]
else
items_to_print[i] = items[i]
end
end
lobby_menu = Click_menu(items_to_print, lobby_menu_x[showing_leaderboard], lobby_menu_y, nil, love.graphics.getHeight() - lobby_menu_y - 90, 8, last_lobby_menu_active_idx)
lobby_menu:set_active_idx(last_active_idx)
if active_back then
lobby_menu:set_active_idx(#items)
elseif showing_leaderboard then
lobby_menu:set_active_idx(#items - 1) --the position of the "hide leaderboard" menu item
else
while lobby_menu.active_idx > #items do
lobby_menu:set_active_idx(lobby_menu.active_idx - 1)
end
active_name = items[lobby_menu.active_idx]
end
end
gprint(notice[#items > 2], lobby_menu_x[showing_leaderboard], lobby_menu_y-30)
gprint(arrow, lobby_menu_x[showing_leaderboard], lobby_menu_y)
gprint(to_print, lobby_menu_x[showing_leaderboard], lobby_menu_y)
if showing_leaderboard then
gprint(leaderboard_string, lobby_menu_x[showing_leaderboard]+400, lobby_menu_y - 120)
end
gprint(join_community_msg, main_menu_screen_pos[1]+30, love.graphics.getHeight() - 50)
lobby_menu:draw()
updated = false
wait()
local ret = nil
variable_step(function()
if menu_up(k) then
if showing_leaderboard then
if leaderboard_first_idx_to_show>1 then
leaderboard_first_idx_to_show = leaderboard_first_idx_to_show - 1
leaderboard_last_idx_to_show = leaderboard_last_idx_to_show - 1
leaderboard_string = build_viewable_leaderboard_string(leaderboard_report, leaderboard_first_idx_to_show, leaderboard_last_idx_to_show)
end
else
lobby_menu:set_active_idx(wrap(1, lobby_menu.active_idx-1, #items))
end
elseif menu_down(k) then
if showing_leaderboard then
if leaderboard_last_idx_to_show < #leaderboard_report then
leaderboard_first_idx_to_show = leaderboard_first_idx_to_show + 1
leaderboard_last_idx_to_show = leaderboard_last_idx_to_show + 1
leaderboard_string = build_viewable_leaderboard_string(leaderboard_report, leaderboard_first_idx_to_show, leaderboard_last_idx_to_show)
end
else
lobby_menu:set_active_idx(wrap(1, lobby_menu.active_idx+1, #items))
end
elseif menu_enter(k) or lobby_menu.idx_selected then
updated = true
lobby_menu:set_active_idx(lobby_menu.idx_selected or lobby_menu.active_idx)
lobby_menu.idx_selected = nil
spectator_list = {}
spectators_string = ""
if lobby_menu.active_idx == #items then
ret = {main_select_mode}
end
if lobby_menu.active_idx == #items - 1 then
if not showing_leaderboard then
json_send({leaderboard_request=true})
else
showing_leaderboard = false --toggle it off
lobby_menu:show_controls(false)
lobby_menu:move(lobby_menu_x[showing_leaderboard], lobby_menu_y)
end
elseif lobby_menu.active_idx <= lastPlayerIndex then
my_name = config.name
op_name = items[lobby_menu.active_idx]
currently_spectating = false
sent_requests[op_name] = true
request_game(items[lobby_menu.active_idx])
else
my_name = items[lobby_menu.active_idx].a
op_name = items[lobby_menu.active_idx].b
currently_spectating = true
room_number_last_spectated = items[lobby_menu.active_idx].roomNumber
request_spectate(items[lobby_menu.active_idx].roomNumber)
end
elseif menu_escape(k) then
if lobby_menu.active_idx == #items then
ret = {main_select_mode}
elseif showing_leaderboard then
showing_leaderboard = false
lobby_menu:show_controls(#lobby_menu.buttons > lobby_menu.button_limit)
lobby_menu:move(lobby_menu_x[showing_leaderboard], lobby_menu_y)
else
lobby_menu:set_active_idx(#items)
end
end
end)
if ret then
json_send({logout=true})
return unpack(ret)
end
active_back = lobby_menu.active_idx == #items
if lobby_menu.active_idx ~= prev_act_idx then
prev_act_idx = lobby_menu.active_idx
end
if not do_messages() then
return main_dumb_transition, {main_select_mode, loc("ss_disconnect").."\n\n"..loc("ss_return"), 60, 300}
end
end
end
function update_win_counts(win_counts)
if (P1 and P1.player_number == 1) or currently_spectating then
my_win_count = win_counts[1] or 0
op_win_count = win_counts[2] or 0
elseif P1 and P1.player_number == 2 then
my_win_count = win_counts[2] or 0
op_win_count = win_counts[1] or 0
end
end
function spectator_list_string(list)
local str = ""
for k,v in ipairs(list) do
str = str..v
if k<#list then
str = str.."\n"
end
end
if str ~= "" then
str = loc("pl_spectators").."\n"..str
end
return str
end
function build_viewable_leaderboard_string(report, first_viewable_idx, last_viewable_idx)
str = loc("lb_header_board").."\n"
first_viewable_idx = math.max(first_viewable_idx,1)
last_viewable_idx = math.min(last_viewable_idx, #report)
for i=first_viewable_idx,last_viewable_idx do
if report[i].is_you then
str = str..loc("lb_you").."-> "
else
str = str.." "
end
str = str..i.." "..report[i].rating.." "..report[i].user_name
if i < #report then
str = str.."\n"
end
end
return str
end
function main_net_vs_setup(ip, network_port)
if not config.name then
return main_set_name
else my_name = config.name
end
while config.name == "defaultname" do
if main_set_name() == {main_select_mode} and config.name ~= "defaultname" then
return main_net_vs_setup
end
end
P1, P1_level, P2_level, got_opponent = nil
P2 = {panel_buffer="", gpanel_buffer=""}
gprint(loc("lb_set_connect"), unpack(main_menu_screen_pos))
wait()
network_init(ip, network_port)
local timeout_counter = 0
while not connection_is_ready() do
gprint(loc("lb_connecting"), unpack(main_menu_screen_pos))
wait()
if not do_messages() then
return main_dumb_transition, {main_select_mode, loc("ss_disconnect").."\n\n"..loc("ss_return"), 60, 300}
end
end
connected_server_ip = ip
logged_in = false
return main_net_vs_lobby
end
function main_net_vs()
--STONER_MODE = true
if current_stage then
use_current_stage()
else
pick_random_stage()
end
pick_use_music_from()
local k = K[1] --may help with spectators leaving games in progress
local end_text = nil
local op_name_y = 40
if string.len(my_name) > 12 then
op_name_y = 55
end
while true do
-- Uncomment this to cripple your game :D
-- love.timer.sleep(0.030)
local messages = server_queue:pop_all_with("taunt", "leave_room")
for _,msg in ipairs(messages) do
if msg.taunt then
local taunts = nil
-- P1.character and P2.character are supposed to be already filtered with current mods, taunts may differ though!
if msg.player_number == my_player_number then
taunts = characters[P1.character].sounds[msg.type]
elseif msg.player_number == op_player_number then
taunts = characters[P2.character].sounds[msg.type]
end
if taunts then
for _,t in ipairs(taunts) do
t:stop()
end
if msg.index <= #taunts then
taunts[msg.index]:play()
elseif #taunts ~= 0 then
taunts[math.random(#taunts)]:play()
end
end
elseif msg.leave_room then
my_win_count = 0
op_win_count = 0
return main_dumb_transition, {main_net_vs_lobby, "", 0, 0}
end
end
local name_and_score = { (my_name or "").."\n"..loc("ss_wins").." "..my_win_count, (op_name or "").."\n"..loc("ss_wins").." "..op_win_count}
gprint((my_name or ""), P1.score_x+themes[config.theme].name_Pos[1], P1.score_y+themes[config.theme].name_Pos[2])
gprint((op_name or ""), P2.score_x+themes[config.theme].name_Pos[1], P2.score_y+themes[config.theme].name_Pos[2])
draw_label(themes[config.theme].images.IMG_wins, (P1.score_x+themes[config.theme].winLabel_Pos[1])/GFX_SCALE, (P1.score_y+themes[config.theme].winLabel_Pos[2])/GFX_SCALE, 0, themes[config.theme].winLabel_Scale)
draw_number(my_win_count, themes[config.theme].images.IMG_timeNumber_atlas, 12, P1_win_quads, P1.score_x+themes[config.theme].win_Pos[1], P1.score_y+themes[config.theme].win_Pos[2], themes[config.theme].win_Scale,
20/themes[config.theme].images.timeNumberWidth*themes[config.theme].time_Scale, 26/themes[config.theme].images.timeNumberHeight*themes[config.theme].time_Scale, "center")
draw_label(themes[config.theme].images.IMG_wins, (P2.score_x+themes[config.theme].winLabel_Pos[1])/GFX_SCALE, (P2.score_y+themes[config.theme].winLabel_Pos[2])/GFX_SCALE, 0, themes[config.theme].winLabel_Scale)
draw_number(op_win_count, themes[config.theme].images.IMG_timeNumber_atlas, 12, P2_win_quads, P2.score_x+themes[config.theme].win_Pos[1], P2.score_y+themes[config.theme].win_Pos[2], themes[config.theme].win_Scale,
20/themes[config.theme].images.timeNumberWidth*themes[config.theme].time_Scale, 26/themes[config.theme].images.timeNumberHeight*themes[config.theme].time_Scale, "center")
if not config.debug_mode then --this is printed in the same space as the debug details
gprint(spectators_string, themes[config.theme].spectators_Pos[1], themes[config.theme].spectators_Pos[2])
end
if match_type == "Ranked" then
if global_current_room_ratings[my_player_number]
and global_current_room_ratings[my_player_number].new then
local rating_to_print = loc("ss_rating").."\n"
if global_current_room_ratings[my_player_number].new > 0 then
rating_to_print = global_current_room_ratings[my_player_number].new
end
--gprint(rating_to_print, P1.score_x, P1.score_y-30)
draw_label(themes[config.theme].images.IMG_rating_1P, (P1.score_x+themes[config.theme].ratingLabel_Pos[1])/GFX_SCALE, (P1.score_y+themes[config.theme].ratingLabel_Pos[2])/GFX_SCALE, 0, themes[config.theme].ratingLabel_Scale)
if type(rating_to_print) == "number" then
draw_number(rating_to_print, themes[config.theme].images.IMG_number_atlas_1P, 10, P1_rating_quads, P1.score_x+themes[config.theme].rating_Pos[1], P1.score_y+themes[config.theme].rating_Pos[2],
themes[config.theme].rating_Scale, (15/themes[config.theme].images.numberWidth_1P*themes[config.theme].rating_Scale), (19/themes[config.theme].images.numberHeight_1P*themes[config.theme].rating_Scale), "center")
end
end
if global_current_room_ratings[op_player_number]
and global_current_room_ratings[op_player_number].new then
local op_rating_to_print = loc("ss_rating").."\n"
if global_current_room_ratings[op_player_number].new > 0 then
op_rating_to_print = global_current_room_ratings[op_player_number].new
end
--gprint(op_rating_to_print, P2.score_x, P2.score_y-30)
draw_label(themes[config.theme].images.IMG_rating_2P, (P2.score_x+themes[config.theme].ratingLabel_Pos[1])/GFX_SCALE, (P2.score_y+themes[config.theme].ratingLabel_Pos[2])/GFX_SCALE, 0, themes[config.theme].ratingLabel_Scale)
if type(op_rating_to_print) == "number" then
draw_number(op_rating_to_print, themes[config.theme].images.IMG_number_atlas_2P, 10, P2_rating_quads, P2.score_x+themes[config.theme].rating_Pos[1], P2.score_y+themes[config.theme].rating_Pos[2],
themes[config.theme].rating_Scale, (15/themes[config.theme].images.numberWidth_2P*themes[config.theme].rating_Scale), (19/themes[config.theme].images.numberHeight_2P*themes[config.theme].rating_Scale), "center")
end
end
end
if not (P1 and P1.play_to_end) and not (P2 and P2.play_to_end) then
P1:render()
P2:render()
wait()
if currently_spectating and menu_escape(K[1]) then
print("spectator pressed escape during a game")
my_win_count = 0
op_win_count = 0
json_send({leave_room=true})
return main_dumb_transition, {main_net_vs_lobby, "", 0, 0}
end
if not do_messages() then
return main_dumb_transition, {main_select_mode, loc("ss_disconnect").."\n\n"..loc("ss_return"), 60, 300}
end
end
--print(P1.CLOCK, P2.CLOCK)
if (P1 and P1.play_to_end) or (P2 and P2.play_to_end) then
if not P1.game_over then
if currently_spectating then
P1:foreign_run()
else
P1:local_run()
end
end
if not P2.game_over then
P2:foreign_run()
end
else
variable_step(function()
if not P1.game_over then
if currently_spectating then
P1:foreign_run()
else
P1:local_run()
end
end
if not P2.game_over then
P2:foreign_run()
end
end)
end
local outcome_claim = nil
local winSFX = nil
if P1.game_over and P2.game_over and P1.CLOCK == P2.CLOCK then
end_text = loc("ss_draw")
outcome_claim = 0
elseif P1.game_over and P1.CLOCK <= P2.CLOCK then
winSFX = P2:pick_win_sfx()
end_text = loc("ss_p_wins", op_name)
op_win_count = op_win_count + 1 -- leaving these in just in case used with an old server that doesn't keep score. win_counts will get overwritten after this by the server anyway.
outcome_claim = P2.player_number
elseif P2.game_over and P2.CLOCK <= P1.CLOCK then
winSFX = P1:pick_win_sfx()
end_text = loc("ss_p_wins", my_name)
my_win_count = my_win_count + 1 -- leave this in
outcome_claim = P1.player_number
end
if end_text then
analytics.game_ends()
undo_stonermode()
json_send({game_over=true, outcome=outcome_claim})
local now = os.date("*t",to_UTC(os.time()))
local sep = "/"
local path = "replays"..sep.."v"..VERSION..sep..string.format("%04d"..sep.."%02d"..sep.."%02d", now.year, now.month, now.day)
local rep_a_name, rep_b_name = my_name, op_name
--sort player names alphabetically for folder name so we don't have a folder "a-vs-b" and also "b-vs-a"
if rep_b_name < rep_a_name then
path = path..sep..rep_b_name.."-vs-"..rep_a_name
else
path = path..sep..rep_a_name.."-vs-"..rep_b_name
end
local filename = "v"..VERSION.."-"..string.format("%04d-%02d-%02d-%02d-%02d-%02d", now.year, now.month, now.day, now.hour, now.min, now.sec).."-"..rep_a_name.."-L"..P1.level.."-vs-"..rep_b_name.."-L"..P2.level
if match_type and match_type ~= "" then
filename = filename.."-"..match_type
end
if outcome_claim == 1 or outcome_claim == 2 then
filename = filename.."-P"..outcome_claim.."wins"
elseif outcome_claim == 0 then
filename = filename.."-draw"
end
filename = filename..".txt"
print("saving replay as "..path..sep..filename)
write_replay_file(path, filename)
print("also saving replay as replay.txt")
write_replay_file()
select_screen.character_select_mode = "2p_net_vs"
if currently_spectating then
return main_dumb_transition, {select_screen.main, end_text, 30, 30, winSFX}
else
return main_dumb_transition, {select_screen.main, end_text, 30, 180, winSFX}
end
end
end
end
function main_local_vs_setup()
currently_spectating = false
my_name = config.name or "Player 1"
op_name = "Player 2"
op_state = nil
select_screen.character_select_mode = "2p_local_vs"
return select_screen.main
end
function main_local_vs()
-- TODO: replay!
use_current_stage()
pick_use_music_from()
local end_text = nil
while true do
if game_is_paused then
draw_pause()
else
P1:render()
P2:render()
end
wait()
variable_step(function()
if not P1.game_over and not P2.game_over then
P1:local_run()
P2:local_run()
P1:handle_pause()
P2:handle_pause()
end
end)
local winSFX = nil
if P1.game_over and P2.game_over and P1.CLOCK == P2.CLOCK then
end_text = loc("ss_draw")
elseif P1.game_over and P1.CLOCK <= P2.CLOCK then
winSFX = P2:pick_win_sfx()
op_win_count = op_win_count + 1
end_text = loc("pl_2_win")
elseif P2.game_over and P2.CLOCK <= P1.CLOCK then
winSFX = P1:pick_win_sfx()
my_win_count = my_win_count + 1
end_text = loc("pl_1_win")
end
if end_text then
analytics.game_ends()
return main_dumb_transition, {select_screen.main, end_text, 45, -1, winSFX}
end
end
end
function main_local_vs_yourself_setup()