-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvp.lua
3830 lines (3584 loc) · 136 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
local mod_gui = require("mod-gui")
local util = require("util")
local balance = require("balance")
local config = require("config")
local production_score = require("production-score")
local kill_score = require("kill-score")
local research_sync = require("research-sync")
local insert = table.insert
local script_data =
{
gui_actions = {},
team_players = {},
elements =
{
config = {},
balance = {},
import = {},
admin = {},
admin_button = {},
spectate_button = {},
join = {},
progress_bar = {},
team_frame = {},
team_list_button = {},
production_score_frame = {},
production_score_inner_frame = {},
recipe_frame = {},
recipe_button = {},
inventory = {},
space_race_frame = {},
kill_score_frame = {},
oil_harvest_frame = {}
},
timers = {},
setup_finished = false,
ready_players = {},
config = {},
round_number = 0,
selected_recipe = {}
}
local statistics_period = 150 -- Seconds
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(),
on_player_joined_team = script.generate_event_name()
}
get_starting_area_radius = function(as_tiles)
local surface = game.surfaces[1]
local radius = math.ceil(surface.get_starting_area_radius() / 32)
if as_tiles then
return radius * 32
end
return radius
end
function set_config_visibility(player)
local index = player.index
local elements = script_data.elements
local config = elements.config[index]
if not (config and config.valid) then return end
local visible = true
for k, name in pairs({"balance", "import", "inventory"}) do
local guis = elements[name]
local blocking_gui = guis[index]
if blocking_gui and blocking_gui.valid and blocking_gui.visible then
visible = false
break
end
end
config.visible = visible
end
local is_in_map = function(width, height, position)
return position.x >= -width
and position.x < width
and position.y >= -height
and position.y < height
end
function create_spawn_positions()
local settings = game.surfaces[1].map_gen_settings
local width = settings.width
local height = settings.height
local height_scale = height / width
local radius = get_starting_area_radius()
local diameter = radius * 32 * 2 * math.sqrt(2)
local count = #script_data.config.teams
local angle = math.pi / count
local hypotenuse = math.abs(math.sin(angle) * diameter)
if hypotenuse < 0.01 then hypotenuse = diameter end
--local min_distance = math.ceil((radius - 1) * 32) * (#script_data.config.teams)
local min_distance = math.ceil(diameter * (diameter/hypotenuse))
local displacement = script_data.config.team_config.average_team_displacement
if displacement < min_distance then
script_data.config.team_config.average_team_displacement = min_distance
displacement = min_distance
end
local mag = displacement + (radius * 32)
local y_scale = 1
if mag > (height / 2) then
--circle too tall
y_scale = height_scale
end
local x_scale = 1
if mag > (width / 2) then
--circle too wide
x_scale = 1 / height_scale
end
local horizontal_offset = (width > 10000 and (width / displacement) * 10) or 0
local vertical_offset = (height > 10000 and (height / displacement) * 10) or 0
script_data.spawn_offset = {x = math.floor(0.5 + math.random(math.floor(-horizontal_offset), math.floor(horizontal_offset)) / 32) * 32, y = math.floor(0.5 + math.random(math.floor(-vertical_offset), math.floor(vertical_offset)) / 32) * 32}
local distance = 0.5 * displacement
local positions = {}
local rotation_offset = ((count - 1) * math.pi / 4)
for k = 1, count do
local rotation = rotation_offset + ((k * 2 * math.pi) / count)
local X = (math.cos(rotation) * distance * x_scale) / 32
if X > 0 then
X = 32 * (math.floor(X))
else
X = 32 * (math.ceil(X))
end
local Y = (math.sin(rotation) * distance * y_scale) / 32
if Y > 0 then
Y = 32 * (math.floor(Y))
else
Y = 32 * (math.ceil(Y))
end
positions[k] = {x = X + script_data.spawn_offset.x, y = Y + script_data.spawn_offset.y}
end
script_data.spawn_positions = positions
return positions
end
function create_next_surface()
local name = "battle_surface_1"
if game.surfaces[name] ~= nil then
name = "battle_surface_2"
end
script_data.round_number = script_data.round_number + 1
local settings = game.surfaces[1].map_gen_settings
settings.starting_points = create_spawn_positions()
script_data.surface = game.create_surface(name, settings)
script_data.surface.always_day = script_data.config.team_config.always_day
end
function destroy_player_gui(player)
local elements = script_data.elements
local index = player.index
for name, guis in pairs (elements) do
local frame = guis[index]
if frame and frame.valid then
deregister_gui(frame)
frame.destroy()
end
guis[index] = nil
end
for k, timer in pairs (script_data.timers) do
if timer.valid and (timer.player_index == index) then
timer.destroy()
end
end
end
function deregister_gui(gui)
local player_gui_actions = script_data.gui_actions[gui.player_index]
if not player_gui_actions then return end
player_gui_actions[gui.index] = nil
for k, child in pairs (gui.children) do
deregister_gui(child)
end
end
function register_gui_action(gui, param)
local gui_actions = script_data.gui_actions
local player_gui_actions = gui_actions[gui.player_index]
if not player_gui_actions then
gui_actions[gui.player_index] = {}
player_gui_actions = gui_actions[gui.player_index]
end
player_gui_actions[gui.index] = param
end
local red = function(str)
return "[color=1,0.2,0.2]"..str.."[/color]"
end
local green = function(str)
return "[color=0.2,1,0.2]"..str.."[/color]"
end
function add_team_to_new_flow(team, flow, current_team, admin)
local frame = flow.add{type = "frame", direction = "vertical", style = "bordered_frame"}
frame.style.horizontally_stretchable = true
local title_flow = frame.add{type = "flow", direction = "horizontal"}
local show_flow = title_flow.add{type = "flow", style = "slot_table_spacing_horizontal_flow"}
local label = show_flow.add{type = "label", caption = team.name, style = "caption_label"}
local pusher = show_flow.add{type = "flow"}
pusher.style.horizontally_stretchable = true
label.style.font_color = get_color(team, true)
if admin then
local edit_button = show_flow.add{type = "sprite-button", style = "slot_button", sprite = "utility/rename_icon_small"}
edit_button.style.height = 20
edit_button.style.width = 20
local edit_flow = title_flow.add{type = "flow", style = "slot_table_spacing_horizontal_flow", visible = false}
edit_flow.style.horizontally_squashable = true
local textfield = edit_flow.add{type = "textfield", text = team.name}
textfield.style.width = 100
local color_drop = edit_flow.add{type = "drop-down"}
local index = 1
for k, color in pairs (script_data.config.colors) do
color_drop.add_item({"color."..color.name})
if color.name == team.color then
index = k
end
end
color_drop.selected_index = index
local pusher = edit_flow.add{type = "flow"}
pusher.style.horizontally_stretchable = true
local textfield_confirm = edit_flow.add{type = "sprite-button", style = "green_slot_button", sprite = "utility/confirm_slot"}
local textfield_cancel = edit_flow.add{type = "sprite-button", style = "not_available_slot_button", sprite = "utility/reset"}
register_gui_action(textfield_cancel, {type = "cancel_rename", edit_flow = edit_flow, show_flow = show_flow})
register_gui_action(edit_button, {type = "rename_team", edit_flow = edit_flow, show_flow = show_flow})
local delete_button = show_flow.add{type = "sprite-button", style = "not_available_slot_button", sprite = "utility/trash"}
delete_button.style.height = 20
delete_button.style.width = 20
register_gui_action(delete_button, {type = "remove_team", team = team, frame = frame})
register_gui_action(textfield_confirm, {type = "confirm_rename", textfield = textfield, team = team, dropdown = color_drop})
end
local team_number = frame.add{type = "flow", direction = "horizontal"}
team_number.add{type = "label", caption = {"", {"team"}, {"colon"}}, style = "description_label"}
if admin then
local button = team_number.add{type = "button", caption = team.team, style = "small_slot_button"}
register_gui_action(button, {type = "team_button_press", team = team})
else
team_number.add{type = "label", caption = team.team}
end
local ready = ""
local first_ready = true
local ready_data = script_data.ready_players
local limit = script_data.config.team_config.max_players
limit = (limit > 0 and limit) or math.huge
local player_count = 0
for k, member in pairs (team.members or {}) do
player_count = player_count + 1
if player_count > limit then
team.members[k] = nil
ready_data[k] = nil
script_data.team_players[k] = nil
else
if first_ready then
first_ready = false
else
ready = ready..", "
end
if ready_data[k] then
ready = ready .. green(member.name)
else
ready = ready .. red(member.name)
end
end
end
if first_ready then
ready = {"none"}
end
local label = frame.add{type = "label", caption = {"members", ready}, style = "description_label"}
label.style.single_line = false
label.style.maximal_width = 400
local within_limit = player_count < limit
if within_limit and (not current_team or current_team ~= team) then
local join_team = frame.add{type = "button", caption = {"join-team"}}
join_team.style.font = "default"
join_team.style.height = 24
join_team.style.top_padding = 0
join_team.style.bottom_padding = 0
register_gui_action(join_team, {type = "join_team", team = team})
end
if current_team == team then
local leave_team = frame.add{type = "button", caption = {"leave-team"}}
leave_team.style.font = "default"
leave_team.style.height = 24
leave_team.style.top_padding = 0
leave_team.style.bottom_padding = 0
register_gui_action(leave_team, {type = "leave_team", team = team})
end
end
refresh_config = function(excluded_player_index)
for k, player in pairs (game.connected_players) do
if player.index ~= excluded_player_index then
create_config_gui(player)
end
end
end
refresh_balance = function(excluded_player_index)
for k, player in pairs (game.connected_players) do
if player.index ~= excluded_player_index then
toggle_balance_options_gui(player)
toggle_balance_options_gui(player)
end
end
end
refresh_inventory = function(excluded_player_index)
for k, player in pairs (game.connected_players) do
if player.index ~= excluded_player_index then
toggle_starting_chest_gui(player)
toggle_starting_chest_gui(player)
end
end
end
local name_allowed = function(name, team)
if name == "" then return false end
for k, other_team in pairs (script_data.config.teams) do
if other_team.name == name then
if other_team ~= team then
return false
end
end
end
return true
end
local is_text_valid = function(text, no_below_100)
if text == "" then return false end
local number = tonumber(text)
if not number then return false end
if number < 0 then return false end
if number > 4294967295 then return false end
if no_below_100 then return number >= 100 end
return true
end
local check_all_ready = function()
local all_ready = true
for k, player in pairs (game.connected_players) do
if not script_data.ready_players[player.index] then
all_ready = false
break
end
end
if all_ready then
start_all_ready_preparations()
elseif script_data.ready_start_tick then
script_data.ready_start_tick = nil
game.print({"ready-cancelled"})
end
end
toggle_starting_chest_gui = function(player)
if not (player and player.valid and player.connected) then return end
local gui = player.gui.center
local frame = script_data.elements.inventory[player.index]
if frame then
deregister_gui(frame)
frame.destroy()
script_data.elements.inventory[player.index] = nil
set_config_visibility(player)
return
end
local frame = gui.add{type = "frame", caption = {"change-starting-items"}, direction = "vertical"}
local group = frame.add{type = "frame", style = "window_content_frame"}
script_data.elements.inventory[player.index] = frame
local admin = player.admin
local config = script_data.config
local types =
{
{name = {"equipment"}, list = config.equipment_list, option = config.starting_equipment},
{name = {"chest"}, list = config.inventory_list, option = config.starting_chest}
}
for k, param in pairs (types) do
local data = param.list
local options = param.option
if data and options then
local inner = group.add{type = "frame", caption = name, style = "bordered_frame", direction = "vertical"}
inner.add{type = "label", caption = param.name, style = "caption_label"}
local selected = options.selected
if admin then
local dropdown = inner.add{type = "drop-down"}
dropdown.style.horizontally_stretchable = true
register_gui_action(dropdown, {type = "starting_item_dropdown_changed", options = options})
local index = 1
for k, option in pairs (options.options) do
if option == selected then index = k end
dropdown.add_item({option})
end
dropdown.selected_index = index
else
inner.add{type = "label", caption = {selected}}
end
local scroll = inner.add{type = "scroll-pane"}
local items = data[selected]
if not items then return end
local prototypes = game.item_prototypes
local table = scroll.add{type = "flow", direction = "horizontal"}
local table1 = table.add{type = "table", column_count = 3}
table1.style.vertically_stretchable = true
local spacer = table.add{type = "flow"}
spacer.style.width = 16
local table2 = table.add{type = "table", column_count = 3}
table2.style.vertically_stretchable = true
local which
for name, count in pairs (items) do
local prototype = prototypes[name]
if prototype then
if which == table1 then
which = table2
else
which = table1
end
if admin then
local elem = which.add{type = "choose-elem-button", elem_type = "item"}
elem.elem_value = name
register_gui_action(elem, {type = "starting_item_elem_changed", items = items, previous = name})
else
local sprite = which.add{type = "sprite", sprite = "item/"..name}
sprite.style.width = 32
sprite.style.height = 32
end
which.add{type = "label", caption = prototype.localised_name}
if admin then
local text = which.add{type = "textfield", text = count}
text.style.maximal_width = 60
register_gui_action(text, {type = "starting_item_textfield_changed", items = items, key = name})
else
which.add{type = "label", caption = count}
end
end
end
if admin then
local elem = table1.add{type = "choose-elem-button", elem_type = "item"}
local pusher = table.add{type = "flow"}
pusher.style.vertically_stretchable = true
local pusher = table.add{type = "flow"}
pusher.style.vertically_stretchable = true
register_gui_action(elem, {type = "starting_item_elem_changed", items = items})
end
end
end
register_gui_action(frame.add{type = "button", caption = {"gui.close"}, style = "dialog_button"}, {type = "starting_chest"})
set_config_visibility(player)
end
local gui_functions =
{
new_team = function(event, param)
local name
repeat name = game.backer_names[math.random(#game.backer_names)]
until name_allowed(name)
local team =
{
name = name,
color = script_data.config.colors[math.random(#script_data.config.colors)].name,
members = {},
team = "-"
}
insert(script_data.config.teams, team)
refresh_config()
end,
remove_team = function(event, param)
if #script_data.config.teams == 1 then
game.players[event.player_index].print("Can't remove last team")
return
end
for k, team in pairs (script_data.config.teams) do
if team == param.team then
table.remove(script_data.config.teams, k)
for k, members in pairs (team.members) do
script_data.ready_players[k] = nil
script_data.team_players[k] = nil
end
break
end
end
refresh_config()
end,
rename_team = function(event, param)
param.edit_flow.visible = true
param.show_flow.visible = false
end,
cancel_rename = function(event, param)
param.edit_flow.visible = false
param.show_flow.visible = true
end,
confirm_rename = function(event, param)
local name = param.textfield.text
if not name_allowed(name, param.team) then
game.players[event.player_index].print("Name not allowed") --[[TODO locale]]
return
end
param.team.name = name
param.team.color = script_data.config.colors[param.dropdown.selected_index].name
refresh_config()
end,
join_team = function(event, param)
local player = game.players[event.player_index]
local current_team = script_data.team_players[player.index]
if current_team then
current_team.members[player.index] = nil
end
param.team.members[player.index] = player
script_data.team_players[player.index] = param.team
script_data.ready_players[player.index] = nil
refresh_config()
check_all_ready()
end,
leave_team = function(event, param)
local player = game.players[event.player_index]
local current_team = param.team
if current_team then
current_team.members[player.index] = nil
end
script_data.team_players[player.index] = nil
script_data.ready_players[player.index] = nil
refresh_config()
check_all_ready()
end,
team_button_press = function(event, param)
local gui = event.element
if not (gui and gui.valid) 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 = #script_data.config.teams
end
elseif index == tostring(#script_data.config.teams) then
if left_click then
index = "?"
else
index = index -1
end
else
if left_click then
index = tonumber(index) + 1
elseif index == tostring(1) then
index = "-"
else
index = index -1
end
end
gui.caption = index
param.team.team = index
refresh_config(event.player_index)
end,
config_text_value_changed = function(event, param)
if event.name ~= defines.events.on_gui_text_changed then return end
local textfield = event.element
if not (textfield and textfield.valid) then return end
local text = textfield.text
local valid = is_text_valid(text)
if not valid then
textfield.style = "invalid_value_textfield"
return
end
textfield.style = "textbox"
param.config[param.key] = tonumber(text)
refresh_config(event.player_index)
end,
config_dropdown_value_changed = function(event, param)
if event.name ~= defines.events.on_gui_selection_state_changed then return end
local dropdown = event.element
if not (dropdown and dropdown.valid) then return end
param.value.selected = param.value.options[dropdown.selected_index]
refresh_config(event.player_index)
end,
config_boolean_changed = function(event, param)
if event.name ~= defines.events.on_gui_checked_state_changed then return end
local check = event.element
if not (check and check.valid) then return end
param.config[param.key] = check.state
refresh_config(event.player_index)
end,
start_round = function(event, param)
start_round()
end,
ready_up = function(event, param)
if event.name ~= defines.events.on_gui_checked_state_changed then return end
local checkbox = event.element
if not (checkbox and checkbox.valid) then return end
local player = game.players[event.player_index]
if checkbox.state then
script_data.ready_players[event.player_index] = true
game.print({"player-is-ready", player.name})
else
script_data.ready_players[event.player_index] = nil
game.print({"player-is-not-ready", player.name})
end
refresh_config()
check_all_ready()
end,
toggle_balance_options = function(event, param)
toggle_balance_options_gui(game.players[event.player_index])
end,
reset_balance_options = function(event, param)
for name, modifiers in pairs (script_data.config.modifier_list) do
for key, value in pairs (modifiers) do
modifiers[key] = 0
end
end
refresh_balance()
end,
balance_textfield_changed = function(event, param)
if event.name ~= defines.events.on_gui_text_changed then return end
local textfield = event.element
if not (textfield and textfield.valid) then return end
local text = textfield.text
local valid = is_text_valid(text, param.no_below_100)
if not valid then
textfield.style = "invalid_value_textfield"
return
end
textfield.style = "textbox"
local value = (text - 100) / 100
script_data.config.modifier_list[param.modifier][param.key] = value
refresh_balance(event.player_index)
end,
balance_options_confirm = function(event, param)
local player = game.players[event.player_index]
end,
pvp_import = function(event, param)
local player = game.players[event.player_index]
if not (player and player.valid) then return end
local gui = player.gui.center
local frame = gui.add{type = "frame", caption = {"gui-blueprint-library.import-string"}, direction = "vertical"}
script_data.elements.import[player.index] = frame
local textfield = frame.add{type = "text-box"}
textfield.word_wrap = true
textfield.style.height = player.display_resolution.height * 0.6 / player.display_scale
textfield.style.width = player.display_resolution.width * 0.6 / player.display_scale
local flow = frame.add{type = "flow", direction = "horizontal"}
register_gui_action
(
flow.add{type = "button", caption = {"gui.close"}, style = "dialog_button"},
{type = "import_export_close", frame = frame}
)
local pusher = flow.add{type = "flow"}
pusher.style.horizontally_stretchable = true
register_gui_action
(
flow.add{type = "button", caption = {"gui-blueprint-library.import"}, style = "confirm_button"},
{type = "import_confirm", frame = frame, textfield = textfield}
)
set_config_visibility(player)
end,
import_confirm = function(event, param)
local player = game.players[event.player_index]
if not (player and player.valid) then return end
local gui = player.gui.center
local frame = param.frame
if not (frame and frame.valid) then return end
local textfield = param.textfield
if not (textfield and textfield.valid) then return end
local text = textfield.text
if text == "" then player.print({"import-failed"}) return end
local new_config = game.json_to_table(util.decode(text))
if not new_config then
player.print({"import-failed"})
return
end
for k, v in pairs (new_config) do
script_data.config[k] = v
end
refresh_config()
deregister_gui(frame)
frame.destroy()
script_data.elements.import[player.index] = nil
player.print({"import-success"})
log("Pvp config import success")
set_config_visibility(player)
end,
pvp_export = function(event, param)
local player = game.players[event.player_index]
if not (player and player.valid) then return end
local gui = player.gui.center
local frame = gui.add{type = "frame", caption = {"gui.export-to-string"}, direction = "vertical"}
script_data.elements.import[player.index] = frame
local textfield = frame.add{type = "text-box"}
textfield.word_wrap = true
textfield.read_only = true
textfield.style.height = player.display_resolution.height * 0.6 / player.display_scale
textfield.style.width = player.display_resolution.width * 0.6 / player.display_scale
local config = script_data.config
local data =
{
game_config = config.game_config,
team_config = config.team_config,
modifier_list = config.modifier_list,
teams = config.teams,
disabled_items = config.disabled_items,
inventory_list = config.inventory_list,
equipment_list = config.equipment_list,
victory = config.victory,
starting_equipment = config.starting_equipment,
starting_chest = config.starting_chest
}
textfield.text = util.encode(game.table_to_json(data))
register_gui_action
(
frame.add{type = "button", caption = {"gui.close"}, style = "dialog_button"},
{type = "import_export_close", frame = frame}
)
set_config_visibility(player)
end,
import_export_close = function(event, param)
local frame = param.frame
if not (frame and frame.valid) then return end
deregister_gui(frame)
frame.destroy()
script_data.elements.import[event.player_index] = nil
set_config_visibility(game.players[event.player_index])
end,
starting_chest = function(event, param)
toggle_starting_chest_gui(game.players[event.player_index])
end,
starting_item_textfield_changed = function(event, param)
if event.name ~= defines.events.on_gui_text_changed then return end
local textfield = event.element
if not (textfield and textfield.valid) then return end
local text = textfield.text
local valid = is_text_valid(text)
if not valid then
textfield.style = "invalid_value_textfield"
return
end
textfield.style = "textbox"
param.items[param.key] = tonumber(text)
refresh_config(event.player_index)
end,
starting_item_elem_changed = function(event, param)
if event.name ~= defines.events.on_gui_elem_changed then return end
local element = event.element
if not (element and element.valid) then return end
local items = param.items
local previous = param.previous
if previous then
items[param.previous] = nil
end
local name = element.elem_value
if name then
if items[name] then
game.players[event.player_index].print("No doofus, its already there")
else
items[name] = game.item_prototypes[name].stack_size
end
end
refresh_inventory()
end,
starting_item_dropdown_changed = function(event, param)
if event.name ~= defines.events.on_gui_selection_state_changed then return end
local dropdown = event.element
if not (dropdown and dropdown.valid) then return end
local data = param.options
data.selected = data.options[dropdown.selected_index]
refresh_inventory()
end,
disable_elem_changed = function(event, param)
if event.name ~= defines.events.on_gui_elem_changed then return end
local gui = event.element
local player = game.players[event.player_index]
if not (player and player.valid and gui and gui.valid) then return end
local parent = gui.parent
if not script_data.config.disabled_items then
script_data.config.disabled_items = {}
end
local items = script_data.config.disabled_items
local value = gui.elem_value
if not value then
local map = {}
for k, child in pairs (parent.children) do
if child.elem_value then
map[child.elem_value] = true
end
end
for item, bool in pairs (items) do
if not map[item] then
items[item] = nil
end
end
deregister_gui(gui)
gui.destroy()
return
end
if items[value] then
if items[value] ~= gui.index then
gui.elem_value = nil
player.print({"duplicate-disable"})
end
else
items[value] = gui.index
register_gui_action(parent.add{type = "choose-elem-button", elem_type = "item"}, {type = "disable_elem_changed"})
end
script_data.config.disabled_items = items
refresh_config(event.player_index)
end,
join_spectator = function(event, param)
local frame = param.frame
if (frame and frame.valid) then
deregister_gui(frame)
frame.destroy()
end
spectator_join(game.players[event.player_index])
end,
admin_button = function(event, param)
local gui = event.element
local player = game.players[event.player_index]
local frame = script_data.elements.admin[event.player_index]
if (frame and frame.valid) then
frame.visible = not frame.visible
return
end
local flow = mod_gui.get_frame_flow(player)
local frame = flow.add{type = "frame", caption = {"admin"}, direction = "vertical"}
script_data.elements.admin[player.index] = frame
frame.visible = true
register_gui_action(frame.add{type = "button", caption = {"end-round"}, tooltip = {"end-round-tooltip"}, style = "dialog_button"}, {type = "admin_end_round"})
register_gui_action(frame.add{type = "button", caption = {"reroll-round"}, tooltip = {"reroll-round-tooltip"}, style = "dialog_button"}, {type = "admin_reroll_round"})
register_gui_action(frame.add{type = "button", caption = {"admin-change-team"}, tooltip = {"admin-change-team-tooltip"}, style = "dialog_button"}, {type = "spectator_join_team_button"})
end,
admin_end_round = function(event, param)
end_round(game.players[event.player_index])
end,
admin_reroll_round = function(event, param)
end_round()
start_round()
return
end,
spectator_join_team_button = function(event, param)
choose_joining_gui(game.players[event.player_index])
end,
pick_team = function(event, param)
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 team = param.team
if not team then return end
set_player(player, team)
for k, other_player in pairs (game.connected_players) do
choose_joining_gui(other_player)
choose_joining_gui(other_player)
update_team_list_frame(other_player)
end
end,
list_teams_button = function(event, param)
local player = game.players[event.player_index]
if not (player and player.valid) then return end
local frame = script_data.elements.team_frame[player.index]
if frame and frame.valid then
frame.destroy()
script_data.elements.team_frame[player.index] = nil
return
end
local flow = mod_gui.get_frame_flow(player)
frame = flow.add{type = "frame", caption = {"teams"}, direction = "vertical"}
frame.style.vertically_stretchable = false
script_data.elements.team_frame[player.index] = frame
update_team_list_frame(player)
end,
production_score_button = function(event, param)
local gui = event.element
local player = game.players[event.player_index]
local frame = script_data.elements.production_score_frame[player.index]
if frame and frame.valid then
deregister_gui(frame)
script_data.elements.production_score_frame[player.index] = nil
frame.destroy()
return
end
local flow = mod_gui.get_frame_flow(player)
frame = flow.add{type = "frame", caption = {"production_score"}, direction = "vertical"}
script_data.elements.production_score_frame[player.index] = frame
frame.style.vertically_stretchable = false
if script_data.config.victory.required_production_score > 0 then
frame.add{type = "label", caption = {"", {"required_production_score"}, {"colon"}, " ", util.format_number(script_data.config.victory.required_production_score)}}
end
if script_data.config.game_config.time_limit > 0 then
insert(script_data.timers, frame.add{type = "label", caption = {"time_left", get_time_left()}})
end
local inner_frame = frame.add{type = "frame", style = "image_frame", direction = "vertical"}
script_data.elements.production_score_inner_frame[player.index] = inner_frame
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 flow = frame.add{type = "flow", direction = "horizontal"}
flow.add{type = "label", caption = {"", {"recipe-calculator"}, {"colon"}}}
local recipe_button = flow.add{type = "choose-elem-button", elem_type = "recipe"}
register_gui_action(recipe_button, {type = "recipe_picker_elem_changed", frame = frame})
script_data.elements.recipe_button[player.index] = recipe_button
flow.style.vertical_align = "center"
update_production_score_frame(player)
recipe_picker_elem_update(player)
end,
recipe_picker_elem_changed = function(event, param)
if event.name ~= defines.events.on_gui_elem_changed then return end
local elem_button = event.element
if not (elem_button and elem_button.valid) then return end
local player = game.players[event.player_index]
if not (player and player.valid) then return end
script_data.selected_recipe[player.index] = elem_button.elem_value
recipe_picker_elem_update(player)
end,
calculator_button_press = function(event, param)
on_calculator_button_press(event, param)
end,
space_race_button = function(event, param)
local player = game.players[event.player_index]
local frame = script_data.elements.space_race_frame[player.index]
if frame and frame.valid then
frame.destroy()
script_data.elements.space_race_frame[player.index] = nil
return
end
local flow = mod_gui.get_frame_flow(player)
frame = flow.add{type = "frame", caption = {"space_race"}, direction = "vertical"}
frame.style.vertically_stretchable = false
script_data.elements.space_race_frame[player.index] = frame
update_space_race_frame(player)
end,
kill_score_button = function(event, param)
local player = game.players[event.player_index]
local frame = script_data.elements.kill_score_frame[player.index]
if frame and frame.valid then
frame.destroy()
script_data.elements.kill_score_frame[player.index] = nil
return
end
local flow = mod_gui.get_frame_flow(player)
frame = flow.add{type = "frame", caption = {"kill_score"}, direction = "vertical"}
frame.style.vertically_stretchable = false
script_data.elements.kill_score_frame[player.index] = frame
update_kill_score_frame(player)
end,
oil_harvest_button = function(event, param)
local player = game.players[event.player_index]
local frame = script_data.elements.oil_harvest_frame[player.index]
if (frame and frame.valid) then
frame.destroy()
script_data.elements.oil_harvest_frame[player.index] = nil
return
end
local flow = mod_gui.get_frame_flow(player)
frame = flow.add{type = "frame", caption = {"oil_harvest"}, direction = "vertical"}
frame.style.vertically_stretchable = false
script_data.elements.oil_harvest_frame[player.index] = frame
update_oil_harvest_frame(player)
end
}
function start_all_ready_preparations()
local seconds = 10
game.print({"everybody-ready", seconds})
script_data.ready_start_tick = game.tick + (seconds * 60)
end
function add_new_config_gui(config_data, flow, admin)
local bool_flow = flow.add{type = "flow", direction = "vertical"}
--local bottom_frame = flow.add{type = "frame", style = "bordered_frame_bottom"}
local other_flow = flow.add{type = "table", column_count = 2}
other_flow.style.column_alignments[2] = "right"
--other_flow.style.column_alignments[1] = "right"
local items = game.item_prototypes
for name, value in pairs (config_data) do
if type(value) == "boolean" then
local check = bool_flow.add{type = "checkbox", state = value, caption = config.localised_names[name] or {name}, ignored_by_interaction = not admin, tooltip = config.localised_tooltips[name] or {name.."_tooltip"}}
register_gui_action(check, {type = "config_boolean_changed", config = config_data, key = name})