-
Notifications
You must be signed in to change notification settings - Fork 4
/
pvp.lua
4273 lines (4062 loc) · 150 KB
/
pvp.lua
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("config")
require("balance")
local mod_gui = require("mod-gui")
require("production-score")
local util = require("util")
local statistics_period = 150 -- Seconds
local game_message_color = {r = 1, g = 0.2, b = 0.8, a = 1} --hot pink
local events =
{
on_round_end = script.generate_event_name(),
on_round_start = script.generate_event_name(),
on_team_lost = script.generate_event_name(),
on_team_won = script.generate_event_name()
}
remote.add_interface("pvp",
{
get_event_name = function(name)
return events[name]
end,
get_teams = function()
return global.teams
end
})
function create_spawn_positions()
local config = global.map_config
local width = config.map_width
local height = config.map_height
local displacement = math.max(config.average_team_displacement, 64)
local horizontal_offset = (width/displacement) * 10
local vertical_offset = (height/displacement) * 10
global.spawn_offset = {x = math.floor(0.5 + math.random(-horizontal_offset, horizontal_offset) / 32) * 32, y = math.floor(0.5 + math.random(-vertical_offset, vertical_offset) / 32) * 32}
local height_scale = height/width
local radius = get_starting_area_radius()
local count = #global.teams
local max_distance = get_starting_area_radius(true) * 2 + displacement
local min_distance = get_starting_area_radius(true) + (32 * (count - 1))
local edge_addition = (radius + 2) * 32
local elevator_set = false
if height_scale == 1 then
if max_distance > width then
displacement = width - edge_addition
end
end
if height_scale < 1 then
if #global.teams == 2 then
if max_distance > width then
displacement = width - edge_addition
end
max_distance = 0
end
if max_distance > height then
displacement = height - edge_addition
end
end
if height_scale > 1 then
if #global.teams == 2 then
if max_distance > height then
displacement = height - edge_addition
end
elevator_set = true
max_distance = 0
end
if max_distance > width then
displacement = width - edge_addition
end
end
local distance = 0.5*displacement
if distance < min_distance then
game.print({"map-size-below-minimum"})
end
local positions = {}
if count == 1 then
positions[1] = {x = 0, y = 0}
else
for k = 1, count do
local rotation = (k*2*math.pi)/count
local X = 32*(math.floor((math.cos(rotation)*distance+0.5)/32))
local Y = 32*(math.floor((math.sin(rotation)*distance+0.5)/32))
if elevator_set then
--[[Swap X and Y for elevators]]
Y = 32*(math.floor((math.cos(rotation)*distance+0.5)/32))
X = 32*(math.floor((math.sin(rotation)*distance+0.5)/32))
end
positions[k] = {x = X, y = Y}
end
end
if #positions == 2 and height_scale == 1 then
--If there are 2 teams in a square map, we adjust positions so they are in the corners of the map
for k, position in pairs (positions) do
if position.x == 0 then position.x = position.y end
if position.y == 0 then position.y = -position.x end
end
end
if #positions == 4 then
--If there are 4 teams we adjust positions so they are in the corners of the map
height_scale = math.min(height_scale, 2)
height_scale = math.max(height_scale, 0.5)
for k, position in pairs (positions) do
if position.x == 0 then position.x = position.y end
if position.y == 0 then position.y = -position.x end
if height_scale > 1 then
position.y = position.y * height_scale
else
position.x = position.x * (1/height_scale)
end
end
if height_scale < 1 then
--If the map is wider than tall, swap 1 and 3 so two allied teams will be together
positions[1], positions[3] = positions[3], positions[1]
end
end
for k, position in pairs (positions) do
position.x = position.x + global.spawn_offset.x
position.y = position.y + global.spawn_offset.y
end
global.spawn_positions = positions
--error(serpent.block(positions))
return positions
end
function create_next_surface()
local name = "battle_surface_1"
if game.surfaces[name] ~= nil then
name = "battle_surface_2"
end
global.round_number = global.round_number + 1
local settings = game.surfaces[1].map_gen_settings
settings.starting_area = global.map_config.starting_area_size.selected
if global.map_config.biters_disabled then
settings.autoplace_controls["enemy-base"].size = "none"
end
if global.map_config.map_seed == 0 then
settings.seed = math.random(4000000000)
else
settings.seed = global.map_config.map_seed
end
if global.map_config.map_height < 1 then
global.map_config.map_height = 2000000
end
if global.map_config.map_width < 1 then
global.map_config.map_width = 2000000
end
settings.height = global.map_config.map_height
settings.width = global.map_config.map_width
settings.starting_points = create_spawn_positions()
global.surface = game.create_surface(name, settings)
global.surface.daytime = 0
global.surface.always_day = global.map_config.always_day
end
function destroy_player_gui(player)
local button_flow = mod_gui.get_button_flow(player)
for k, name in pairs (
{
"objective_button", "diplomacy_button", "admin_button",
"silo_gui_sprite_button", "production_score_button", "oil_harvest_button",
"space_race_button", "spectator_join_team_button", "list_teams_button"
}) do
if button_flow[name] then
button_flow[name].destroy()
end
end
local frame_flow = mod_gui.get_frame_flow(player)
for k, name in pairs (
{
"objective_frame", "admin_button", "admin_frame",
"silo_gui_frame", "production_score_frame", "oil_harvest_frame",
"space_race_frame", "team_list"
}) do
if frame_flow[name] then
frame_flow[name].destroy()
end
end
local center_gui = player.gui.center
for k, name in pairs ({"diplomacy_frame", "progress_bar", "start_match_frame"}) do
if center_gui[name] then
center_gui[name].destroy()
end
end
end
function destroy_joining_guis(gui)
if gui.random_join_frame then
gui.random_join_frame.destroy()
end
if gui.pick_join_frame then
gui.pick_join_frame.destroy()
end
if gui.auto_assign_frame then
gui.auto_assign_frame.destroy()
end
end
function make_color_dropdown(k, gui)
local team = global.teams[k]
local menu = gui.add{type = "drop-down", name = k.."_color"}
local count = 1
for k, color in pairs (global.colors) do
menu.add_item({"color."..color.name})
if color.name == team.color then
menu.selected_index = count
end
count = count + 1
end
end
function add_team_to_team_table(gui, k)
local team = global.teams[k]
local textfield = gui.add{type = "textfield", name = k, text = team.name}
textfield.style.minimal_width = 0
textfield.style.horizontally_stretchable = true
make_color_dropdown(k, gui)
local caption
if tonumber(team.team) then
caption = team.team
elseif team.team:find("?") then
caption = "?"
else
caption = team.team
end
set_button_style(gui.add{type = "button", name = k.."_next_team_button", caption = caption, tooltip = {"team-button-tooltip"}})
local bin = gui.add{name = k.."_trash_button", type = "sprite-button", sprite = "utility/trash_bin", tooltip = {"remove-team-tooltip"}}
bin.style.top_padding = 0
bin.style.bottom_padding = 0
bin.style.right_padding = 0
bin.style.left_padding = 0
bin.style.minimal_height = 26
bin.style.minimal_width = 26
end
function create_game_config_gui(gui)
local name = "game_config_gui"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"game-config-gui"}, direction = "vertical", style = "inner_frame"}
frame.clear()
make_config_table(frame, global.game_config)
create_disable_frame(frame)
end
function create_team_config_gui(gui)
local name = "team_config_gui"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"team-config-gui"}, direction = "vertical", style = "inner_frame"}
frame.clear()
local inner_frame = frame.add{type = "frame", style = "image_frame", name = "team_config_gui_inner_frame", direction = "vertical"}
inner_frame.style.left_padding = 8
inner_frame.style.right_padding = 8
inner_frame.style.top_padding = 8
inner_frame.style.bottom_padding = 8
local scroll = inner_frame.add{type = "scroll-pane", name = "team_config_gui_scroll"}
scroll.style.maximal_height = 200
local team_table = scroll.add{type = "table", column_count = 4, name = "team_table"}
for k, name in pairs ({"team-name", "color", "team", "remove"}) do
team_table.add{type = "label", caption = {name}}
end
for k, team in pairs (global.teams) do
add_team_to_team_table(team_table, k)
end
set_button_style(inner_frame.add{name = "add_team_button", type = "button", caption = {"add-team"}, tooltip = {"add-team-tooltip"}})
make_config_table(frame, global.team_config)
end
function get_config_holder(player)
local gui = player.gui.center
local frame = gui.config_holding_frame
if frame then return frame.scrollpane.horizontal_flow end
frame = gui.add{name = "config_holding_frame", type = "frame", direction = "vertical"}
frame.style.scaleable = false
frame.style.maximal_height = player.display_resolution.height * 0.95
frame.style.maximal_width = player.display_resolution.width * 0.95
local scroll = frame.add{name = "scrollpane", type = "scroll-pane"}
local flow = scroll.add{name = "horizontal_flow", type = "table", column_count = 4}
flow.draw_vertical_lines = true
flow.style.horizontal_spacing = 32
flow.style.horizontally_stretchable = true
flow.style.horizontally_squashable = true
return flow
end
function get_config_frame(player)
local gui = player.gui.center
local frame = gui.config_holding_frame
if frame then return frame end
get_config_holder(player)
return gui.config_holding_frame
end
function check_config_frame_size(event)
local player = game.players[event.player_index]
if not player then return end
local frame = player.gui.center.config_holding_frame
if not frame then return end
local visiblity = frame.style.visible
frame.destroy()
--In this case, it is better to destroy and re-create, instead of handling the sizing and scaling of all the elements in the gui
create_config_gui(player)
get_config_frame(player).style.visible = visiblity
end
function check_balance_frame_size(event)
local player = game.players[event.player_index]
if not player then return end
local frame = player.gui.center.balance_options_frame
if not frame then return end
toggle_balance_options_gui(player)
toggle_balance_options_gui(player)
end
function create_config_gui(player)
local gui = get_config_holder(player)
create_map_config_gui(gui)
create_game_config_gui(gui)
create_team_config_gui(gui)
local frame = get_config_frame(player)
if not frame.config_holder_button_flow then
local button_flow = frame.add{type = "flow", direction = "horizontal", name = "config_holder_button_flow"}
button_flow.style.horizontally_stretchable = true
button_flow.add{type = "button", name = "balance_options", caption = {"balance-options"}}
local spacer = button_flow.add{type = "flow"}
spacer.style.horizontally_stretchable = true
button_flow.add{type = "sprite-button", name = "pvp_export_button", sprite = "utility/export_slot", tooltip = {"gui.export-to-string"}, style = "slot_button"}
button_flow.add{type = "sprite-button", name = "pvp_import_button", sprite = "utility/import_slot", tooltip = {"gui-blueprint-library.import-string"}, style = "slot_button"}
button_flow.add{type = "button", name = "config_confirm", caption = {"config-confirm"}}
end
set_mode_input(player)
end
function create_map_config_gui(gui)
local name = "map_config_gui"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"map-config-gui"}, direction = "vertical", style = "inner_frame"}
frame.clear()
make_config_table(frame, global.map_config)
end
function create_waiting_gui(player)
local gui = player.gui.center
local frame = gui.waiting_frame or gui.add{type = "frame", name = "waiting_frame"}
frame.clear()
local label = frame.add{type = "label", caption = {"setup-in-progress"}}
end
function player_join_lobby(player)
local character = player.character
player.character = nil
if character then character.destroy() end
player.set_controller{type = defines.controllers.ghost}
player.teleport({0, 1000}, game.surfaces.Lobby)
player.color = global.colors[global.color_map["black"]].color
player.chat_color = global.colors[global.color_map["yellow"]].color
end
function end_round(admin)
for k, player in pairs (game.players) do
player.force = game.forces.player
player.tag = ""
destroy_player_gui(player)
destroy_joining_guis(player.gui.center)
if player.connected then
if player.ticks_to_respawn then
player.ticks_to_respawn = nil
end
player_join_lobby(player)
if player.admin then
create_config_gui(player)
else
create_waiting_gui(player)
end
end
end
if global.surface ~= nil then
game.delete_surface(global.surface)
end
if admin then
game.print({"admin-ended-round", admin.name})
end
global.setup_finished = false
global.check_starting_area_generation = false
global.average_score = nil
global.scores = nil
global.exclusion_map = nil
global.protected_teams = nil
global.check_base_exclusion = nil
global.oil_harvest_scores = nil
global.production_scores = nil
global.space_race_scores = nil
global.last_defcon_tick = nil
global.next_defcon_tech = nil
global.research_time_wasted = nil
global.previous_tech = nil
global.silos = nil
global.wrecks = nil
script.raise_event(events.on_round_end, {})
end
function prepare_next_round()
global.setup_finished = false
global.team_won = false
check_game_speed()
game.speed = 3
create_next_surface()
setup_teams()
chart_starting_area_for_force_spawns()
set_evolution_factor()
set_difficulty()
end
function set_mode_input(player)
if not (player and player.valid and player.gui.center.config_holding_frame) then return end
local visibility_map = {
peaceful_mode = function(gui)
local option = gui.biters_disabled_boolean
if not option then return end
return not option.state
end,
evolution_factor = function(gui)
local option = gui.biters_disabled_boolean
if not option then return end
return not option.state
end,
chunks_to_extend_duplication = function(gui)
local option = gui.duplicate_starting_area_entities_boolean
if not option then return end
return option.state
end,
time_limit = function(gui)
local score_option = gui.production_score_boolean
local oil_option = gui.oil_harvest_boolean
if not (score_option and oil_option) then return end
return score_option.state or oil_option.state
end,
disband_on_loss = function(gui)
local option = gui.last_silo_standing_boolean
if not option then return end
return option.state
end,
required_production_score = function(gui)
local option = gui.production_score_boolean
if not option then return end
return option.state
end,
required_satellites_sent = function(gui)
local option = gui.space_race_boolean
if not option then return end
return option.state
end,
required_oil_barrels = function(gui)
local option = gui.oil_harvest_boolean
if not option then return end
return option.state
end,
oil_only_in_center = function(gui)
local option = gui.oil_harvest_boolean
if not option then return end
return option.state
end,
turret_ammunition = function(gui)
local option = gui.team_turrets_boolean
if not option then return end
return option.state
end,
give_artillery_remote = function(gui)
local option = gui.team_artillery_boolean
if not option then return end
return option.state
end,
who_decides_diplomacy = function(gui)
local option = gui.diplomacy_enabled_boolean
if not option then return end
return option.state
end,
defcon_random = function(gui)
local option = gui.defcon_mode_boolean
if not option then return end
return option.state
end,
defcon_timer = function(gui)
local defcon_option = gui.defcon_mode_boolean
local defcon_random = gui.defcon_random_boolean
if not (defcon_option and defcon_random) then return end
return defcon_option.state and defcon_random.state
end,
defcon_random_multiplier = function(gui)
local defcon_option = gui.defcon_mode_boolean
local defcon_random = gui.defcon_random_boolean
if not (defcon_option and defcon_random) then return end
return defcon_option.state and not defcon_random.state
end,
starting_chest_multiplier = function(gui)
local dropdown = gui.starting_chest_dropdown
local name = global.team_config.starting_chest.options[dropdown.selected_index]
return name ~= "none"
end,
}
local gui = get_config_holder(player)
for k, frame in pairs ({gui.map_config_gui, gui.game_config_gui, gui.team_config_gui}) do
if frame and frame.valid then
local config = frame.config_table
if (config and config.valid) then
local children = config.children
if frame == gui.game_config_gui then
local silo_option = config.last_silo_standing_boolean
local score_option = config.production_score_boolean
local space_option = config.space_race_boolean
local oil_option = config.oil_harvest_boolean
if silo_option and score_option and space_option and oil_option then
local is_freeplay = not (silo_option.state or score_option.state or space_option.state or oil_option.state)
if is_freeplay then
config.game_mode_dummy.caption = {"", "(", {"freeplay"}, ")"}
config.game_mode_dummy.tooltip = {"freeplay_tooltip"}
else
config.game_mode_dummy.caption = ""
config.game_mode_dummy.tooltip = ""
end
end
end
for k, child in pairs (children) do
local name = child.name or ""
local mapped = visibility_map[name]
local localized_caption = children[k].caption
local is_victory_option = {
["last_silo_standing"] = true,
["production_score"] = true,
["space_race"] = true,
["oil_harvest"] = true,
["last_silo_standing"] = true,
["production_score"] = true,
["required_production_score"] = true,
["space_race"] = true,
["required_satellites_sent"] = true,
["oil_harvest"] = true,
["required_oil_barrels"] = true,
["oil_only_in_center"] = true,
["time_limit"] = true,
["disband_on_loss"] = true
}
local indent = ""
if is_victory_option[name] then
indent = " "
end
if mapped then
local bool = mapped(config)
children[k].style.visible = bool
children[k+1].style.visible = bool
indent = indent.." "
end
if localized_caption[3] ~= indent and (mapped or is_victory_option[name]) then
children[k].caption = {"", indent, localized_caption}
end
end
end
end
end
end
function init_player_gui(player)
destroy_player_gui(player)
if not global.setup_finished then return end
local button_flow = mod_gui.get_button_flow(player)
button_flow.add{type = "button", caption = {"objective"}, name = "objective_button", style = mod_gui.button_style}
button_flow.add{type = "button", caption = {"teams"}, name = "list_teams_button", style = mod_gui.button_style}
if global.team_config.diplomacy_enabled then
local button = button_flow.add{type = "button", caption = {"diplomacy"}, name = "diplomacy_button", style = mod_gui.button_style}
button.style.visible = #global.teams > 1 and player.force.name ~= "spectator"
end
if global.game_config.production_score then
button_flow.add{type = "button", caption = {"production_score"}, name = "production_score_button", style = mod_gui.button_style}
end
if global.game_config.space_race then
button_flow.add{type = "button", caption = {"space_race"}, name = "space_race_button", style = mod_gui.button_style}
end
if global.game_config.oil_harvest then
button_flow.add{type = "button", caption = {"oil_harvest"}, name = "oil_harvest_button", style = mod_gui.button_style}
end
if player.admin then
button_flow.add{type = "button", caption = {"admin"}, name = "admin_button", style = mod_gui.button_style}
end
if player.force.name == "spectator" and not global.team_won then
button_flow.add{type = "button", caption = {"join-team"}, name = "spectator_join_team_button", style = mod_gui.button_style}
end
if not global.match_started then
create_start_match_gui(player)
end
end
function get_color(team, lighten)
local c = global.colors[global.color_map[team.color]].color
if lighten then
return {r = 1 - (1 - c.r) * 0.5, g = 1 - (1 - c.g) * 0.5, b = 1 - (1 - c.b) * 0.5, a = 1}
end
return c
end
function add_player_list_gui(force, gui)
if not (force and force.valid) then return end
if #force.players == 0 then
gui.add{type = "label", caption = {"none"}}
return
end
local scroll = gui.add{type = "scroll-pane"}
scroll.style.maximal_height = 120
local name_table = scroll.add{type = "table", column_count = 1}
name_table.style.vertical_spacing = 0
local added = {}
local first = true
if #force.connected_players > 0 then
local online_names = ""
for k, player in pairs (force.connected_players) do
if not first then
online_names = online_names..", "
end
first = false
online_names = online_names..player.name
added[player.name] = true
end
local online_label = name_table.add{type = "label", caption = {"online", online_names}}
online_label.style.single_line = false
online_label.style.maximal_width = 180
end
first = true
if #force.players > #force.connected_players then
local offline_names = ""
for k, player in pairs (force.players) do
if not added[player.name] then
if not first then
offline_names = offline_names..", "
end
first = false
offline_names = offline_names..player.name
added[player.name] = true
end
end
local offline_label = name_table.add{type = "label", caption = {"offline", offline_names}}
offline_label.style.single_line = false
offline_label.style.font_color = {r = 0.7, g = 0.7, b = 0.7}
offline_label.style.maximal_width = 180
end
end
function update_diplomacy_frame(player)
local flow = player.gui.center.diplomacy_frame
if not flow then return end
gui = flow.diplomacy_inner_frame
if not gui then return end
local diplomacy_table = gui.diplomacy_table
if not diplomacy_table then
diplomacy_table = gui.add{type = "table", name = "diplomacy_table", column_count = 5}
diplomacy_table.style.horizontal_spacing = 16
diplomacy_table.style.vertical_spacing = 8
diplomacy_table.draw_horizontal_lines = true
diplomacy_table.draw_vertical_lines = true
else
diplomacy_table.clear()
end
for k, name in pairs ({"team-name", "stance", "enemy", "neutral", "ally"}) do
local label = diplomacy_table.add{type = "label", name = name, caption = {name}}
label.style.font = "default-bold"
end
for k, team in pairs (global.teams) do
local force = game.forces[team.name]
if force and force ~= player.force then
local label = diplomacy_table.add{type = "label", name = team.name.."_name", caption = team.name}
label.style.single_line = false
label.style.maximal_width = 150
label.style.font = "default-semibold"
label.style.font_color = get_color(team, true)
local stance = get_stance(player.force, force)
local their_stance = get_stance(force, player.force)
local stance_label = diplomacy_table.add{type = "label", name = team.name.."_stance", caption = {their_stance}}
if their_stance == "ally" then
stance_label.style.font_color = {r = 0.5, g = 1, b = 0.5}
elseif their_stance == "enemy" then
stance_label.style.font_color = {r = 1, g = 0.5, b = 0.5}
end
diplomacy_table.add{type = "checkbox", name = team.name.."_enemy", state = (stance == "enemy")}
diplomacy_table.add{type = "checkbox", name = team.name.."_neutral", state = (stance == "neutral")}
diplomacy_table.add{type = "checkbox", name = team.name.."_ally", state = (stance == "ally")}
end
end
if not flow.diplomacy_confirm then
flow.add{type = "button", name = "diplomacy_confirm", caption = {"confirm"}}
end
end
function place_player_on_battle_surface(player)
local force = player.force
local surface = global.surface
if not surface.valid then return end
local force_spawn = force.get_spawn_position(surface)
local offset_spawn = {force_spawn.x, force_spawn.y + 15}
local position = surface.find_non_colliding_position("player", offset_spawn, 320, 1)
if position then
player.teleport(position, surface)
else
player.print({"cant-find-position"}, player.name)
player.force = "player"
player.tag = ""
choose_joining_gui(player)
return false
end
if player.character then
player.character.destroy()
end
player.set_controller
{
type = defines.controllers.character,
character = surface.create_entity{name = "player", position = position, force = force}
}
player.spectator = false
if global.map_config.team_artillery and global.map_config.give_artillery_remote and game.item_prototypes["artillery-targeting-remote"] then
player.insert("artillery-targeting-remote")
end
give_equipment(player)
apply_character_modifiers(player)
check_force_protection(force)
init_player_gui(player)
return true
end
function set_player(player, team)
local force = game.forces[team.name]
player.force = force
player.color = get_color(team)
player.chat_color = get_color(team, true)
player.tag = "["..force.name.."]"
if global.match_started then
if not place_player_on_battle_surface(player) then return end
end
for k, other_player in pairs (game.connected_players) do
update_team_list_frame(player)
end
game.print({"joined", player.name, player.force.name})
end
function choose_joining_gui(player)
if #global.teams == 1 then
local team = global.teams[1]
local force = game.forces[team.name]
set_player(player, team)
return
end
local setting = global.team_config.team_joining.selected
if setting == "random" then
create_random_join_gui(player.gui.center)
return
end
if setting == "player_pick" then
create_pick_join_gui(player.gui.center)
return
end
if setting == "auto_assign" then
create_auto_assign_gui(player.gui.center)
return
end
end
function add_join_spectator_button(gui)
local player = game.players[gui.player_index]
if (not global.map_config.allow_spectators) and (not player.admin) and (not global.team_won) then return end
set_button_style(gui.add{type = "button", name = "join_spectator", caption = {"join-spectator"}})
end
function create_random_join_gui(gui)
local name = "random_join_frame"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"random-join"}}
frame.clear()
if not global.team_won then
set_button_style(frame.add{type = "button", name = "random_join_button", caption = {"random-join-button"}})
end
add_join_spectator_button(frame)
end
function create_auto_assign_gui(gui)
local name = "auto_assign_frame"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"auto-assign"}}
frame.clear()
if not global.team_won then
set_button_style(frame.add{type = "button", name = "auto_assign_button", caption = {"auto-assign-button"}})
end
add_join_spectator_button(frame)
end
function create_pick_join_gui(gui)
local name = "pick_join_frame"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"pick-join"}, direction = "vertical"}
frame.clear()
if not global.team_won then
local inner_frame = frame.add{type = "frame", style = "image_frame", name = "pick_join_inner_frame", direction = "vertical"}
inner_frame.style.left_padding = 8
inner_frame.style.top_padding = 8
inner_frame.style.right_padding = 8
inner_frame.style.bottom_padding = 8
local pick_join_table = inner_frame.add{type = "table", name = "pick_join_table", column_count = 4}
pick_join_table.style.horizontal_spacing = 16
pick_join_table.style.vertical_spacing = 8
pick_join_table.draw_horizontal_lines = true
pick_join_table.draw_vertical_lines = true
pick_join_table.style.column_alignments[3] = "right"
pick_join_table.add{type = "label", name = "pick_join_table_force_name", caption = {"team-name"}}.style.font = "default-semibold"
pick_join_table.add{type = "label", name = "pick_join_table_player_count", caption = {"players"}}.style.font = "default-semibold"
pick_join_table.add{type = "label", name = "pick_join_table_team", caption = {"team-number"}}.style.font = "default-semibold"
pick_join_table.add{type = "label", name = "pick_join_table_pad"}.style.font = "default-semibold"
local limit = global.team_config.max_players
local teams = get_eligible_teams(game.players[gui.player_index])
if not teams then return end
for k, team in pairs (teams) do
local force = game.forces[team.name]
if force then
local name = pick_join_table.add{type = "label", name = force.name.."_label", caption = force.name}
name.style.font = "default-semibold"
name.style.font_color = get_color(team, true)
add_player_list_gui(force, pick_join_table)
local caption
if tonumber(team.team) then
caption = team.team
elseif team.team:find("?") then
caption = team.team:gsub("?", "")
else
caption = team.team
end
pick_join_table.add{type = "label", name = force.name.."_team", caption = caption}
set_button_style(pick_join_table.add{type = "button", name = force.name.."_pick_join", caption = {"join"}})
end
end
end
add_join_spectator_button(frame)
end
function on_pick_join_button_press(event)
local gui = event.element
local player = game.players[event.player_index]
if not (gui and gui.valid and player and player.valid) then return end
local name = gui.name
if not name then return end
local suffix = "_pick_join"
if not name:find(suffix) then return end
team_name = name:gsub(suffix, "")
local joined_team
for k, team in pairs (global.teams) do
if team_name == team.name then
joined_team = team
break
end
end
if not joined_team then return end
local force = game.forces[joined_team.name]
if not force then return end
set_player(player, joined_team)
player.gui.center.pick_join_frame.destroy()
for k, player in pairs (game.forces.player.players) do
create_pick_join_gui(player.gui.center)
end
for k, player in pairs (game.connected_players) do
update_team_list_frame(player)
end
end
function create_start_match_gui(player)
if not player.admin then return end
local gui = player.gui.center
local name = "start_match_frame"
local frame = gui[name] or gui.add{type = "frame", name = name, caption = {"admin"}}
frame.clear()
set_button_style(frame.add{type = "button", name = "start_match_button", caption = {"start-match-button"}})
end
function add_team_button_press(event)
local gui = event.element
local index = #global.teams + 1
for k = 1, index do
if not global.teams[k] then
index = k
break
end
end
if index > 24 then
local player = game.players[event.player_index]
if player then
player.print({"too-many-teams", 24})
end
return
end
local color = global.colors[(1+index%(#global.colors))]
local name = game.backer_names[math.random(#game.backer_names)]
local team = {name = name, color = color.name, team = "-"}
global.teams[index] = team
for k, player in pairs (game.players) do
local gui = get_config_holder(player).team_config_gui
if gui then
add_team_to_team_table(gui.team_config_gui_inner_frame.team_config_gui_scroll.team_table, index)
end
end
end
function trash_team_button_press(event)
local gui = event.element
if not gui.valid then return end
if not gui.name:find("_trash_button") then
return
end
local team_index = gui.name:gsub("_trash_button", "")
team_index = tonumber(team_index)
local count = 0
for k, team in pairs (global.teams) do
count = count + 1
end
if count > 1 then
global.teams[team_index] = nil
remove_team_from_team_table(gui)
else
game.players[event.player_index].print({"cant-remove-only-team"})
end
end
function remove_team_from_team_table(gui)
local index = nil
for k, child in pairs (gui.parent.children) do
if child == gui then
index = k
break
end
end
local delete_list = {}
for k, player in pairs (game.players) do
local gui = get_config_holder(player).team_config_gui
if gui then
local children = gui.team_config_gui_inner_frame.team_config_gui_scroll.team_table.children
for k = -3, 0 do
children[index+k].destroy()
end
end
end
end
function set_teams_from_gui(player)
local gui = get_config_holder(player).team_config_gui
if not gui then return end
local teams = {}
local team = {}
local duplicates = {}
local team_table = gui.team_config_gui_inner_frame.team_config_gui_scroll.team_table
local children = team_table.children
for index = 1, 25 do
local element = team_table[index]
if element and element.valid then
local text = element.text
if is_ignored_force(text) then
player.print({"disallowed-team-name", text})
return
end
if text == "" then
player.print({"empty-team-name"})
return
end
if duplicates[text] then
player.print({"duplicate-team-name", text})
return
end
duplicates[text] = true
local team = {}
team.name = text
team.color = global.colors[team_table[index.."_color"].selected_index].name
local caption = team_table[index.."_next_team_button"].caption
team.team = tonumber(caption) or caption
table.insert(teams, team)
end
end
if #teams > 24 then
player.print({"too-many-teams", 24})
return
end
global.teams = teams
return true
end
function on_team_button_press(event)
local gui = event.element
if not gui.valid then return end
if not gui.name:find("_next_team_button") then return end
local left_click = (event.button == defines.mouse_button_type.left)
local index = gui.caption
if index == "-" then
if left_click then
index = 1
else
index = "?"
end
elseif index == "?" then
if left_click then
index = "-"
else
index = #global.teams
end
elseif index == tostring(#global.teams) then