forked from Oarcinae/FactorioScenarioMultiplayerSpawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseparate_spawns_guis.lua
More file actions
executable file
·1176 lines (963 loc) · 48.8 KB
/
separate_spawns_guis.lua
File metadata and controls
executable file
·1176 lines (963 loc) · 48.8 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
-- separate_spawns_guis.lua
-- Nov 2016
-- I made a separate file for all the GUI related functions
require("separate_spawns")
local SPAWN_GUI_MAX_WIDTH = 500
local SPAWN_GUI_MAX_HEIGHT = 1000
-- Use this for testing shared spawns...
-- local sharedSpawnExample1 = {openAccess=true,
-- position={x=50,y=50},
-- players={"ABC", "DEF"}}
-- local sharedSpawnExample2 = {openAccess=false,
-- position={x=200,y=200},
-- players={"ABC", "DEF"}}
-- local sharedSpawnExample3 = {openAccess=true,
-- position={x=400,y=400},
-- players={"A", "B", "C", "D"}}
-- global.sharedSpawns = {testName1=sharedSpawnExample1,
-- testName2=sharedSpawnExample2,
-- Oarc=sharedSpawnExample3}
-- A display gui message
-- Meant to be display the first time a player joins.
function DisplayWelcomeTextGui(player)
player.gui.center.add{name = "welcome_msg",
type = "frame",
direction = "vertical",
caption=global.welcome_msg_title}
local wGui = player.gui.center.welcome_msg
wGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
wGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
-- Start with server message.
AddLabel(wGui, "server_msg_lbl1", SERVER_MSG, my_label_style)
AddLabel(wGui, "contact_info_msg_lbl1", CONTACT_MSG, my_label_style)
AddSpacer(wGui, "server_msg_spacer1")
-- Informational message about the scenario
AddLabel(wGui, "scenario_info_msg_lbl1", SCENARIO_INFO_MSG, my_label_style)
AddSpacer(wGui, "scenario_info_msg_spacer1")
-- Warning about spawn creation time
AddLabel(wGui, "spawn_time_msg_lbl1", SPAWN_WARN_MSG, my_warning_style)
wGui.add{name = "welcome_okay_btn",
type = "button",
caption="I Understand"}
end
-- Handle the gui click of the welcome msg
function WelcomeTextGuiClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.player_index]
local buttonClicked = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (buttonClicked == "welcome_okay_btn") then
if (player.gui.center.welcome_msg ~= nil) then
player.gui.center.welcome_msg.destroy()
end
DisplaySpawnOptions(player)
end
end
-- Display the spawn options and explanation
function DisplaySpawnOptions(player)
if (player == nil) then
DebugPrint("DisplaySpawnOptions with no valid player...")
return
end
if (player.gui.center.spawn_opts ~= nil) then
DebugPrint("Tried to display spawn options when it was already displayed!")
return
end
player.gui.center.add{name = "spawn_opts",
type = "frame",
direction = "vertical",
caption="Spawn Options"}
local sGui = player.gui.center.spawn_opts
sGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
sGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
-- Warnings and explanations...
local warn_msg = "Click the INFO button in the top left to learn more about this scenario! This is your ONLY chance to choose a spawn option. Choose carefully..."
AddLabel(sGui, "warning_lbl1", warn_msg, my_warning_style)
AddLabel(sGui, "spawn_msg_lbl1", SPAWN_MSG1, my_label_style)
-- Button and message about the regular vanilla spawn
if ENABLE_DEFAULT_SPAWN then
sGui.add{name = "default_spawn_btn",
type = "button",
caption="Vanilla Spawn"}
local normal_spawn_text = "This is the default spawn behavior of a vanilla game. You join the default team in the center of the map."
AddLabel(sGui, "normal_spawn_lbl1", normal_spawn_text, my_label_style)
AddSpacerLine(sGui, "normal_spawn_spacer")
end
-- The main spawning options. Solo near and solo far.
-- If enable, you can also choose to be on your own team.
local soloSpawnFlow = sGui.add{name = "spawn_solo_flow",
type = "flow",
direction="vertical"}
-- Radio buttons to pick your team.
if (ENABLE_SEPARATE_TEAMS) then
soloSpawnFlow.add{name = "isolated_spawn_main_team_radio",
type = "radiobutton",
caption="Join Main Team (shared research)",
state=false}
soloSpawnFlow.add{name = "isolated_spawn_new_team_radio",
type = "radiobutton",
caption="Create Your Own Team (own research tree)",
state=false}
end
-- Allow players to spawn with a moat around their area.
if (SPAWN_MOAT_CHOICE_ENABLED) then
soloSpawnFlow.add{name = "isolated_spawn_moat_option_checkbox",
type = "checkbox",
caption="Surround your spawn with a moat",
state=false}
end
-- Isolated spawn options. The core gameplay of this scenario.
local soloSpawnbuttons = soloSpawnFlow.add{name = "spawn_solo_flow",
type = "flow",
direction="horizontal"}
soloSpawnbuttons.add{name = "isolated_spawn_near",
type = "button",
caption="Solo Spawn (Near)"}
soloSpawnbuttons.add{name = "isolated_spawn_far",
type = "button",
caption="Solo Spawn (Far)"}
AddLabel(soloSpawnFlow, "isolated_spawn_lbl1",
"You are spawned in a new area, with some starting resources.", my_label_style)
AddSpacerLine(soloSpawnFlow, "isolated_spawn_spacer")
-- Spawn options to join another player's base.
if ENABLE_SHARED_SPAWNS then
local numAvailSpawns = GetNumberOfAvailableSharedSpawns()
if (numAvailSpawns > 0) then
sGui.add{name = "join_other_spawn",
type = "button",
caption="Join Someone (" .. numAvailSpawns .. " available)"}
local join_spawn_text = "You are spawned in someone else's base. This requires at least 1 person to have allowed access to their base. This choice is final and you will not be able to create your own spawn later."
AddLabel(sGui, "join_other_spawn_lbl1", join_spawn_text, my_label_style)
else
AddLabel(sGui, "join_other_spawn_lbl1", "There are currently no shared bases availble to spawn at.", my_label_style)
sGui.add{name = "join_other_spawn_check",
type = "button",
caption="Check Again"}
end
else
AddLabel(soloSpawnFlow, "join_other_spawn_lbl1",
"Shared spawns are disabled in this mode.", my_warning_style)
end
-- New awesome buddy spawning system
if ENABLE_SHARED_SPAWNS and ENABLE_BUDDY_SPAWN then
AddSpacerLine(sGui, "buddy_spawn_msg_spacer")
sGui.add{name = "buddy_spawn",
type = "button",
caption="Buddy Spawn"}
AddLabel(sGui, "buddy_spawn_lbl1",
"The buddy system requires 2 players in this menu at the same time, you spawn beside each other, each with your own resources.", my_label_style)
end
-- Some final notes
AddSpacerLine(sGui, "note_spacer1")
if (MAX_ONLINE_PLAYERS_AT_SHARED_SPAWN > 0) then
AddLabel(sGui, "buddy_spawn_lbl1",
"If you create your own spawn point you can allow up to " .. MAX_ONLINE_PLAYERS_AT_SHARED_SPAWN-1 .. " other online players to join.",
my_note_style)
end
spawn_distance_notes="Near spawn is between " .. NEAR_MIN_DIST .. "-" .. NEAR_MAX_DIST .. " chunks away from the center of the map.\n"..
"Far spawn is between " .. FAR_MIN_DIST .. "-" .. FAR_MAX_DIST .. " chunks away from the center of the map.\n"..
"Solo spawns are dangerous! Expect a fight to reach other players."
AddLabel(sGui, "note_lbl1", spawn_distance_notes, my_note_style)
end
-- This just updates the radio buttons when players click them.
function SpawnOptsRadioSelect(event)
if not (event and event.element and event.element.valid) then return end
local elemName = event.element.name
if (elemName == "isolated_spawn_main_team_radio") then
event.element.parent.isolated_spawn_new_team_radio.state=false
elseif (elemName == "isolated_spawn_new_team_radio") then
event.element.parent.isolated_spawn_main_team_radio.state=false
end
if (elemName == "buddy_spawn_main_team_radio") then
event.element.parent.buddy_spawn_new_team_radio.state=false
event.element.parent.buddy_spawn_buddy_team_radio.state=false
elseif (elemName == "buddy_spawn_new_team_radio") then
event.element.parent.buddy_spawn_main_team_radio.state=false
event.element.parent.buddy_spawn_buddy_team_radio.state=false
elseif (elemName == "buddy_spawn_buddy_team_radio") then
event.element.parent.buddy_spawn_main_team_radio.state=false
event.element.parent.buddy_spawn_new_team_radio.state=false
end
end
-- Handle the gui click of the spawn options
function SpawnOptsGuiClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.player_index]
local elemName = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (player.gui.center.spawn_opts == nil) then
return -- Gui event unrelated to this gui.
end
local joinMainTeamRadio, joinOwnTeamRadio, moatChoice = false
-- Check if a valid button on the gui was pressed
-- and delete the GUI
if ((elemName == "default_spawn_btn") or
(elemName == "isolated_spawn_near") or
(elemName == "isolated_spawn_far") or
(elemName == "join_other_spawn") or
(elemName == "buddy_spawn") or
(elemName == "join_other_spawn_check")) then
if (ENABLE_SEPARATE_TEAMS) then
joinMainTeamRadio =
player.gui.center.spawn_opts.spawn_solo_flow.isolated_spawn_main_team_radio.state
joinOwnTeamRadio =
player.gui.center.spawn_opts.spawn_solo_flow.isolated_spawn_new_team_radio.state
else
joinMainTeamRadio = true
joinOwnTeamRadio = false
end
if (SPAWN_MOAT_CHOICE_ENABLED) then
moatChoice =
player.gui.center.spawn_opts.spawn_solo_flow.isolated_spawn_moat_option_checkbox.state
end
player.gui.center.spawn_opts.destroy()
else
return -- Do nothing, no valid element item was clicked.
end
if (elemName == "default_spawn_btn") then
GivePlayerStarterItems(player)
ChangePlayerSpawn(player, player.force.get_spawn_position(GAME_SURFACE_NAME))
SendBroadcastMsg(player.name .. " is joining the main force!")
ChartArea(player.force, player.position, math.ceil(ENFORCE_LAND_AREA_TILE_DIST/CHUNK_SIZE), player.surface)
-- Create the button at the top left for setting respawn point and sharing base.
CreateSpawnCtrlGui(player)
elseif ((elemName == "isolated_spawn_near") or (elemName == "isolated_spawn_far")) then
-- Create a new spawn point
local newSpawn = {x=0,y=0}
-- Create a new force for player if they choose that radio button
if ENABLE_SEPARATE_TEAMS and joinOwnTeamRadio then
local newForce = CreatePlayerCustomForce(player)
end
-- Find coordinates of a good place to spawn
if (elemName == "isolated_spawn_far") then
newSpawn = FindUngeneratedCoordinates(FAR_MIN_DIST,FAR_MAX_DIST, player.surface)
elseif (elemName == "isolated_spawn_near") then
newSpawn = FindUngeneratedCoordinates(NEAR_MIN_DIST,NEAR_MAX_DIST, player.surface)
end
-- If that fails, find a random map edge in a rand direction.
if ((newSpawn.x == 0) and (newSpawn.x == 0)) then
newSpawn = FindMapEdge(GetRandomVector(), player.surface)
DebugPrint("Resorting to find map edge! x=" .. newSpawn.x .. ",y=" .. newSpawn.y)
end
-- Create that spawn in the global vars
ChangePlayerSpawn(player, newSpawn)
-- Send the player there
QueuePlayerForDelayedSpawn(player.name, newSpawn, moatChoice)
if (elemName == "isolated_spawn_near") then
SendBroadcastMsg(player.name .. " is joining the game from a distance!")
elseif (elemName == "isolated_spawn_far") then
SendBroadcastMsg(player.name .. " is joining the game from a great distance!")
end
-- Create the button at the top left for setting respawn point and sharing base.
CreateSpawnCtrlGui(player)
player.print("PLEASE WAIT WHILE YOUR SPAWN POINT IS GENERATED!")
player.print("PLEASE WAIT WHILE YOUR SPAWN POINT IS GENERATED!!")
player.print("PLEASE WAIT WHILE YOUR SPAWN POINT IS GENERATED!!!")
elseif (elemName == "join_other_spawn") then
DisplaySharedSpawnOptions(player)
-- Provide a way to refresh the gui to check if people have shared their
-- bases.
elseif (elemName == "join_other_spawn_check") then
DisplaySpawnOptions(player)
-- Hacky buddy spawn system
elseif (elemName == "buddy_spawn") then
table.insert(global.waitingBuddies, player.name)
SendBroadcastMsg(player.name .. " is looking for a buddy.")
DisplayBuddySpawnOptions(player)
end
end
-- Display the spawn options and explanation
function DisplaySharedSpawnOptions(player)
player.gui.center.add{name = "shared_spawn_opts",
type = "frame",
direction = "vertical",
caption="Available Bases to Join:"}
local shGuiFrame = player.gui.center.shared_spawn_opts
local shGui = shGuiFrame.add{type="scroll-pane", name="spawns_scroll_pane", caption=""}
ApplyStyle(shGui, my_fixed_width_style)
shGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
shGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
shGui.horizontal_scroll_policy = "never"
for spawnName,sharedSpawn in pairs(global.sharedSpawns) do
if (sharedSpawn.openAccess and
(game.players[spawnName] ~= nil) and
game.players[spawnName].connected) then
local spotsRemaining = MAX_ONLINE_PLAYERS_AT_SHARED_SPAWN - GetOnlinePlayersAtSharedSpawn(spawnName)
if (MAX_ONLINE_PLAYERS_AT_SHARED_SPAWN == 0) then
shGui.add{type="button", caption=spawnName, name=spawnName}
elseif (spotsRemaining > 0) then
shGui.add{type="button", caption=spawnName .. " (" .. spotsRemaining .. " spots remaining)", name=spawnName}
end
if (shGui.spawnName ~= nil) then
-- AddSpacer(buddyGui, spawnName .. "spacer_lbl")
ApplyStyle(shGui[spawnName], my_small_button_style)
end
end
end
shGui.add{name = "shared_spawn_cancel",
type = "button",
caption="Cancel (Return to Previous Options)"}
end
-- Handle the gui click of the shared spawn options
function SharedSpwnOptsGuiClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.player_index]
local buttonClicked = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (event.element.parent) then
if (event.element.parent.name ~= "spawns_scroll_pane") then
return
end
end
-- Check for cancel button, return to spawn options
if (buttonClicked == "shared_spawn_cancel") then
DisplaySpawnOptions(player)
if (player.gui.center.shared_spawn_opts ~= nil) then
player.gui.center.shared_spawn_opts.destroy()
end
-- Else check for which spawn was selected
-- If a spawn is removed during this time, the button will not do anything
else
for spawnName,sharedSpawn in pairs(global.sharedSpawns) do
if ((buttonClicked == spawnName) and
(game.players[spawnName] ~= nil) and
(game.players[spawnName].connected)) then
-- Add the player to that shared spawns join queue.
if (global.sharedSpawns[spawnName].joinQueue == nil) then
global.sharedSpawns[spawnName].joinQueue = {}
end
table.insert(global.sharedSpawns[spawnName].joinQueue, player.name)
-- Clear the shared spawn options gui.
if (player.gui.center.shared_spawn_opts ~= nil) then
player.gui.center.shared_spawn_opts.destroy()
end
-- Display wait menu with cancel button.
DisplaySharedSpawnJoinWaitMenu(player)
-- Tell other player they are requesting a response.
game.players[spawnName].print(player.name .. " is requesting to join your base!")
break
end
end
end
end
function DisplaySharedSpawnJoinWaitMenu(player)
player.gui.center.add{name = "join_shared_spawn_wait_menu",
type = "frame",
direction = "vertical",
caption="Waiting for spawn owner to respond..."}
local sGui = player.gui.center.join_shared_spawn_wait_menu
sGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
sGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
-- Warnings and explanations...
AddLabel(sGui, "warning_lbl1", "You will spawn once the host selects yes...", my_warning_style)
sGui.add{name = "cancel_shared_spawn_wait_menu",
type = "button",
caption="Cancel (Return to starting spawn options)"}
end
-- Handle the gui click of the buddy wait menu
function SharedSpawnJoinWaitMenuClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.player_index]
local elemName = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (player.gui.center.join_shared_spawn_wait_menu == nil) then
return -- Gui event unrelated to this gui.
end
-- Check if player is cancelling the request.
if (elemName == "cancel_shared_spawn_wait_menu") then
player.gui.center.join_shared_spawn_wait_menu.destroy()
DisplaySpawnOptions(player)
-- Find and remove the player from the joinQueue they were in.
for spawnName,sharedSpawn in pairs(global.sharedSpawns) do
if (sharedSpawn.joinQueue ~= nil) then
for index,requestingPlayer in pairs(sharedSpawn.joinQueue) do
if (requestingPlayer == player.name) then
table.remove(global.sharedSpawns[spawnName].joinQueue, index)
game.players[spawnName].print(player.name .. " cancelled their request to join your spawn.")
return
end
end
end
end
DebugPrint("ERROR! Failed to remove player from joinQueue!")
player.print("ERROR! Failed to remove player from joinQueue!")
end
end
function CreateSpawnCtrlGui(player)
if player and (player.gui.top.spwn_ctrls == nil) then
player.gui.top.add{name="spwn_ctrls", type="button", caption="Spawn Ctrl"}
end
end
local function IsSharedSpawnActive(player)
if ((global.sharedSpawns[player.name] == nil) or
(global.sharedSpawns[player.name].openAccess == false)) then
return false
else
return true
end
end
-- Get a random warp point to go to
function GetRandomSpawnPoint()
local numSpawnPoints = TableLength(global.sharedSpawns)
if (numSpawnPoints > 0) then
local randSpawnNum = math.random(1,numSpawnPoints)
local counter = 1
for _,sharedSpawn in pairs(global.sharedSpawns) do
if (randSpawnNum == counter) then
return sharedSpawn.position
end
counter = counter + 1
end
end
return {x=0,y=0}
end
-- This is a toggle function, it either shows or hides the spawn controls
function ExpandSpawnCtrlGui(player, tick)
local spwnCtrlPanel = player.gui.left["spwn_ctrl_panel"]
if (spwnCtrlPanel) then
spwnCtrlPanel.destroy()
else
local spwnCtrlPanel = player.gui.left.add{type="frame",
name="spwn_ctrl_panel", caption="Spawn Controls:"}
local spwnCtrls = spwnCtrlPanel.add{type="scroll-pane",
name="spwn_ctrl_panel", caption=""}
ApplyStyle(spwnCtrls, my_fixed_width_style)
spwnCtrls.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
spwnCtrls.horizontal_scroll_policy = "never"
if ENABLE_SHARED_SPAWNS then
if (global.uniqueSpawns[player.name] ~= nil) then
-- This checkbox allows people to join your base when they first
-- start the game.
spwnCtrls.add{type="checkbox", name="accessToggle",
caption="Allow others to join your base.",
state=IsSharedSpawnActive(player)}
spwnCtrls["accessToggle"].style.top_padding = 10
spwnCtrls["accessToggle"].style.bottom_padding = 10
ApplyStyle(spwnCtrls["accessToggle"], my_fixed_width_style)
end
end
-- Sets the player's custom spawn point to their current location
if ((tick - global.playerCooldowns[player.name].setRespawn) > RESPAWN_COOLDOWN_TICKS) then
spwnCtrls.add{type="button", name="setRespawnLocation", caption="Set New Respawn Location (1 hour cooldown)"}
spwnCtrls["setRespawnLocation"].style.font = "default-small-semibold"
else
AddLabel(spwnCtrls, "respawn_cooldown_note1", "Set Respawn Cooldown Remaining: " .. formattime(RESPAWN_COOLDOWN_TICKS-(tick - global.playerCooldowns[player.name].setRespawn)), my_note_style)
end
AddLabel(spwnCtrls, "respawn_cooldown_note2", "This will set your respawn point to your current location.", my_note_style)
-- Display a list of people in the join queue for your base.
if (ENABLE_SHARED_SPAWNS and IsSharedSpawnActive(player)) then
if ((global.sharedSpawns[player.name].joinQueue ~= nil) and
(#global.sharedSpawns[player.name].joinQueue > 0)) then
AddLabel(spwnCtrls, "drop_down_msg_lbl1", "Select a player from the join queue:", my_label_style)
spwnCtrls.add{name = "join_queue_dropdown",
type = "drop-down",
items = global.sharedSpawns[player.name].joinQueue}
spwnCtrls.add{name = "accept_player_request",
type = "button",
caption="Accept"}
spwnCtrls.add{name = "reject_player_request",
type = "button",
caption="Reject"}
else
AddLabel(spwnCtrls, "empty_join_queue_note1", "You have no players requesting to join you at this time.", my_note_style)
end
spwnCtrls.add{name = "join_queue_spacer", type = "label",
caption=" "}
end
end
end
function SpawnCtrlGuiOptionsSelect(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.element.player_index]
local name = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
-- Handle changes to spawn sharing.
if (name == "accessToggle") then
if event.element.state then
if DoesPlayerHaveCustomSpawn(player) then
if (global.sharedSpawns[player.name] == nil) then
CreateNewSharedSpawn(player)
else
global.sharedSpawns[player.name].openAccess = true
end
SendBroadcastMsg("New players can now join " .. player.name .. "'s base!")
end
else
if (global.sharedSpawns[player.name] ~= nil) then
global.sharedSpawns[player.name].openAccess = false
SendBroadcastMsg("New players can no longer join " .. player.name .. "'s base!")
end
end
end
end
function SpawnCtrlGuiClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.element.player_index]
local elemName = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (elemName == "spwn_ctrls") then
ExpandSpawnCtrlGui(player, event.tick)
end
if (event.element.parent) then
if (event.element.parent.name ~= "spwn_ctrl_panel") then
return
end
end
-- Sets a new respawn point and resets the cooldown.
if (elemName == "setRespawnLocation") then
if DoesPlayerHaveCustomSpawn(player) then
ChangePlayerSpawn(player, player.position)
ExpandSpawnCtrlGui(player, event.tick)
player.print("Re-spawn point updated!")
end
end
-- Accept or reject pending player join requests to a shared base
if ((elemName == "accept_player_request") or (elemName == "reject_player_request")) then
if ((event.element.parent.join_queue_dropdown == nil) or
(event.element.parent.join_queue_dropdown.selected_index == 0)) then
player.print("Selected player is no longer waiting to join!")
ExpandSpawnCtrlGui(player, event.tick)
return
end
joinQueueIndex = event.element.parent.join_queue_dropdown.selected_index
joinQueuePlayerChoice = event.element.parent.join_queue_dropdown.get_item(joinQueueIndex)
if ((game.players[joinQueuePlayerChoice] == nil) or
(not game.players[joinQueuePlayerChoice].connected)) then
player.print("Selected player is no longer waiting to join!")
ExpandSpawnCtrlGui(player, event.tick)
return
end
if (elemName == "reject_player_request") then
player.print("You rejected " .. joinQueuePlayerChoice .. "'s request to join your base.")
SendMsg(joinQueuePlayerChoice, "Your request to join was rejected.")
ExpandSpawnCtrlGui(player, event.tick)
-- Close the waiting players menu
if (game.players[joinQueuePlayerChoice].gui.center.join_shared_spawn_wait_menu) then
game.players[joinQueuePlayerChoice].gui.center.join_shared_spawn_wait_menu.destroy()
DisplaySpawnOptions(game.players[joinQueuePlayerChoice])
end
-- Find and remove the player from the joinQueue they were in.
for index,requestingPlayer in pairs(global.sharedSpawns[player.name].joinQueue) do
if (requestingPlayer == joinQueuePlayerChoice) then
table.remove(global.sharedSpawns[player.name].joinQueue, index)
return
end
end
elseif (elemName == "accept_player_request") then
-- Find and remove the player from the joinQueue they were in.
for index,requestingPlayer in pairs(global.sharedSpawns[player.name].joinQueue) do
if (requestingPlayer == joinQueuePlayerChoice) then
table.remove(global.sharedSpawns[player.name].joinQueue, index)
end
end
-- If player exists, then do stuff.
if (game.players[joinQueuePlayerChoice]) then
-- Send an announcement
SendBroadcastMsg(joinQueuePlayerChoice .. " is joining " .. player.name .. "'s base!")
-- Close the waiting players menu
if (game.players[joinQueuePlayerChoice].gui.center.join_shared_spawn_wait_menu) then
game.players[joinQueuePlayerChoice].gui.center.join_shared_spawn_wait_menu.destroy()
end
-- Spawn the player
joiningPlayer = game.players[joinQueuePlayerChoice]
ChangePlayerSpawn(joiningPlayer, global.sharedSpawns[player.name].position)
SendPlayerToSpawn(joiningPlayer)
GivePlayerStarterItems(joiningPlayer)
table.insert(global.sharedSpawns[player.name].players, joiningPlayer.name)
joiningPlayer.force = game.players[player.name].force
-- Create the button at the top left for setting respawn point and sharing base.
CreateSpawnCtrlGui(joiningPlayer)
ExpandSpawnCtrlGui(player, event.tick)
else
SendBroadcastMsg(joinQueuePlayerChoice .. " left the game. What an ass.")
end
end
end
end
-- Display the buddy spawn menu
function DisplayBuddySpawnOptions(player)
player.gui.center.add{name = "buddy_spawn_opts",
type = "frame",
direction = "vertical",
caption="Buddy Spawn Options"}
local buddyGui = player.gui.center.buddy_spawn_opts
buddyGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
buddyGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
-- Warnings and explanations...
buddy_info_msg="To use this, make sure you and your buddy are in this menu at the same time. Only one of you must send the request. Select your buddy from the list (refresh if your buddy's name is not visible) and select your spawn options. Click one of the request buttons to send the request. The other buddy can then accept (or deny) the request. This will allow you both to spawn next to each other, each with your own spawn area. Once a buddy accepts a spawn request, it is final!"
AddLabel(buddyGui, "buddy_info_msg", buddy_info_msg, my_label_style)
AddSpacerLine(buddyGui, "buddy_info_spacer")
buddyList = {}
for _,buddyName in pairs(global.waitingBuddies) do
if (buddyName ~= player.name) then
table.insert(buddyList, buddyName)
end
end
AddLabel(buddyGui, "drop_down_msg_lbl1", "First, select a buddy from the waiting list. Then choose the spawn options and send your request:", my_label_style)
buddyGui.add{name = "waiting_buddies_dropdown",
type = "drop-down",
items = buddyList}
buddyGui.add{name = "refresh_buddy_list",
type = "button",
caption="Refresh Buddy List"}
-- AddSpacerLine(buddyGui, "waiting_buddies_spacer")
-- The buddy spawning options.
local buddySpawnFlow = buddyGui.add{name = "spawn_buddy_flow",
type = "flow",
direction="vertical"}
-- Allow picking of teams
if (ENABLE_SEPARATE_TEAMS) then
buddySpawnFlow.add{name = "buddy_spawn_main_team_radio",
type = "radiobutton",
caption="Join Main Team (shared research)",
state=true}
buddySpawnFlow.add{name = "buddy_spawn_new_team_radio",
type = "radiobutton",
caption="Create Your Own Team (own research tree)",
state=false}
buddySpawnFlow.add{name = "buddy_spawn_buddy_team_radio",
type = "radiobutton",
caption="Create Your Own Buddy Team (buddy and you share research)",
state=false}
end
if (SPAWN_MOAT_CHOICE_ENABLED) then
buddySpawnFlow.add{name = "buddy_spawn_moat_option_checkbox",
type = "checkbox",
caption="Surround your spawn with a moat",
state=false}
end
AddSpacer(buddyGui, "buddy_options_spacer")
buddySpawnFlow.add{name = "buddy_spawn_request_near",
type = "button",
caption="Request Buddy Spawn (Near)"}
buddySpawnFlow.add{name = "buddy_spawn_request_far",
type = "button",
caption="Request Buddy Spawn (Far)"}
AddSpacerLine(buddyGui, "buddy_spawn_spacer")
buddyGui.add{name = "buddy_spawn_cancel",
type = "button",
caption="Cancel (Return to Previous Options)"}
-- Some final notes
AddSpacerLine(buddyGui, "note_spacer1")
if (MAX_ONLINE_PLAYERS_AT_SHARED_SPAWN > 0) then
AddLabel(buddyGui, "buddy_spawn_lbl1",
"You can allow up to " .. MAX_ONLINE_PLAYERS_AT_SHARED_SPAWN-1 .. " other online players to join.",
my_note_style)
end
spawn_distance_notes="Near spawn is between " .. NEAR_MIN_DIST .. "-" .. NEAR_MAX_DIST .. " chunks away from the center of the map.\n"..
"Far spawn is between " .. FAR_MIN_DIST .. "-" .. FAR_MAX_DIST .. " chunks away from the center of the map.\n"..
"Solo spawns are dangerous! Expect a fight to reach other players."
AddLabel(buddyGui, "note_lbl1", spawn_distance_notes, my_note_style)
end
-- Handle the gui click of the spawn options
function BuddySpawnOptsGuiClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.player_index]
local elemName = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (player.gui.center.buddy_spawn_opts == nil) then
return -- Gui event unrelated to this gui.
end
-- Just refresh the buddy list dropdown values only.
if (elemName == "refresh_buddy_list") then
player.gui.center.buddy_spawn_opts.waiting_buddies_dropdown.clear_items()
for _,buddyName in pairs(global.waitingBuddies) do
if (player.name ~= buddyName) then
player.gui.center.buddy_spawn_opts.waiting_buddies_dropdown.add_item(buddyName)
end
end
return
end
-- Handle the cancel button to exit this menu
if (elemName == "buddy_spawn_cancel") then
player.gui.center.buddy_spawn_opts.destroy()
DisplaySpawnOptions(player)
-- Remove them from the buddy list when they cancel
for i=#global.waitingBuddies,1,-1 do
name = global.waitingBuddies[i]
if (name == player.name) then
table.remove(global.waitingBuddies, i)
end
end
end
local joinMainTeamRadio, joinOwnTeamRadio, joinBuddyTeamRadio, moatChoice = false
local buddyChoice = nil
-- Handle the spawn request button clicks
if ((elemName == "buddy_spawn_request_near") or
(elemName == "buddy_spawn_request_far")) then
buddySpawnGui = player.gui.center.buddy_spawn_opts
dropDownIndex = buddySpawnGui.waiting_buddies_dropdown.selected_index
if (dropDownIndex > 0) then
buddyChoice = buddySpawnGui.waiting_buddies_dropdown.get_item(dropDownIndex)
else
player.print("You have not selected a valid buddy! Please try again.")
return
end
buddyIsStillWaiting = false
for _,buddyName in pairs(global.waitingBuddies) do
if (buddyChoice == buddyName) then
if (game.players[buddyChoice]) then
buddyIsStillWaiting = true
end
break
end
end
if (not buddyIsStillWaiting) then
player.print("Selected buddy is no longer available! Please try again.")
player.gui.center.buddy_spawn_opts.destroy()
DisplayBuddySpawnOptions(player)
return
end
if (ENABLE_SEPARATE_TEAMS) then
joinMainTeamRadio = buddySpawnGui.spawn_buddy_flow.buddy_spawn_main_team_radio.state
joinOwnTeamRadio = buddySpawnGui.spawn_buddy_flow.buddy_spawn_new_team_radio.state
joinBuddyTeamRadio = buddySpawnGui.spawn_buddy_flow.buddy_spawn_buddy_team_radio.state
else
joinMainTeamRadio = true
joinOwnTeamRadio = false
joinBuddyTeamRadio = false
end
if (SPAWN_MOAT_CHOICE_ENABLED) then
moatChoice = buddySpawnGui.spawn_buddy_flow.buddy_spawn_moat_option_checkbox.state
end
-- Save the chosen spawn options somewhere for later use.
global.buddySpawnOptions[player.name] = {joinMainTeamRadio=joinMainTeamRadio,
joinOwnTeamRadio=joinOwnTeamRadio,
joinBuddyTeamRadio=joinBuddyTeamRadio,
moatChoice=moatChoice,
buddyChoice=buddyChoice,
distChoice=elemName}
player.gui.center.buddy_spawn_opts.destroy()
-- Display prompts to the players
DisplayBuddySpawnWaitMenu(player)
DisplayBuddySpawnRequestMenu(game.players[buddyChoice], player.name)
if (game.players[buddyChoice].gui.center.buddy_spawn_opts ~= nil) then
game.players[buddyChoice].gui.center.buddy_spawn_opts.destroy()
end
-- Remove them from the buddy list while they make up their minds.
for i=#global.waitingBuddies,1,-1 do
name = global.waitingBuddies[i]
if ((name == player.name) or (name == buddyChoice)) then
table.remove(global.waitingBuddies, i)
end
end
else
return -- Do nothing, no valid element item was clicked.
end
end
function DisplayBuddySpawnWaitMenu(player)
player.gui.center.add{name = "buddy_wait_menu",
type = "frame",
direction = "vertical",
caption="Waiting for buddy to respond..."}
local sGui = player.gui.center.buddy_wait_menu
sGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
sGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
-- Warnings and explanations...
AddLabel(sGui, "warning_lbl1", "You will spawn once your buddy selects yes...", my_warning_style)
AddSpacer(sGui, "warning_spacer")
sGui.add{name = "cancel_buddy_wait_menu",
type = "button",
caption="Cancel (Return to starting spawn options)"}
end
-- Handle the gui click of the buddy wait menu
function BuddySpawnWaitMenuClick(event)
if not (event and event.element and event.element.valid) then return end
local player = game.players[event.player_index]
local elemName = event.element.name
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
if (player.gui.center.buddy_wait_menu == nil) then
return -- Gui event unrelated to this gui.
end
-- Check if player is cancelling the request.
if (elemName == "cancel_buddy_wait_menu") then
player.gui.center.buddy_wait_menu.destroy()
DisplaySpawnOptions(player)
buddy = game.players[global.buddySpawnOptions[player.name].buddyChoice]
if (buddy.gui.center.buddy_request_menu ~= nil) then
buddy.gui.center.buddy_request_menu.destroy()
end
if (buddy.gui.center.buddy_spawn ~= nil) then
buddy.gui.center.buddy_spawn_opts.destroy()
end
DisplaySpawnOptions(buddy)
buddy.print(player.name .. " cancelled their buddy request!")
end
end
function DisplayBuddySpawnRequestMenu(player, requestingBuddyName)
if not player then
DebugPrint("Another gui click happened with no valid player...")
return
end
player.gui.center.add{name = "buddy_request_menu",
type = "frame",
direction = "vertical",
caption="Buddy Request!"}
local sGui = player.gui.center.buddy_request_menu
sGui.style.maximal_width = SPAWN_GUI_MAX_WIDTH
sGui.style.maximal_height = SPAWN_GUI_MAX_HEIGHT
-- Warnings and explanations...
AddLabel(sGui, "warning_lbl1", requestingBuddyName .. " is requesting a buddy spawn from you!", my_warning_style)
teamText = "error!"