-
Notifications
You must be signed in to change notification settings - Fork 0
/
competitive_production.lua
1138 lines (1016 loc) · 34.8 KB
/
competitive_production.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("map_sets")
require("map_scripts")
require("points")
require("config")
local mod_gui = require("mod-gui")
local util = require("util")
function select_from_probability_table(probability_table)
local roll_max = 0
for _, item in pairs(probability_table) do
roll_max = roll_max + item.probability
end
local roll_value = math.random(0, roll_max - 1)
for _, item in pairs(probability_table) do
roll_value = roll_value - item.probability
if (roll_value < 0) then
return item.value
end
end
end
function select_inventory() return select_from_probability_table(global.inventory_probabilities) end
function select_equipment() return select_from_probability_table(global.equipment_probabilities) end
function select_challange_type() return select_from_probability_table(global.challange_type_probabilities) end
function save_round_statistics()
local round_data = "\nRound "..global.round_number.."\n Time: "..format_time(time_left()).."\n Type: "..global.challenge_type.."\n Inventory: "..global.round_inventory.."\n Equipment: "..global.round_equipment.."\n Players per team: "..global.players_per_team.."\n Number of players: "..#global.online_players.."\n Tasks:"
for k, item in pairs (global.task_items) do
round_data = round_data.."\n "..item.name..": "..item.count
end
if global.winners then
round_data = round_data.."\n Number of winning teams: "..#global.winners
for k, force in pairs (global.winners) do
round_data = round_data.."\n "..force.name..": "
for j, player in pairs (force.players) do
if j == 1 then
round_data = round_data..player.name
else
round_data = round_data..", "..player.name
end
end
end
end
round_data = round_data.."\n"
game.write_file("round_statistics.txt", round_data, true, 0)
end
function start_challenge()
global.winners = {}
global.round_number = global.round_number + 1
if global.recent_round_number == global.recent_round_count then
global.recent_round_number = 0
global.recent_points = {}
end
global.recent_round_number = global.recent_round_number + 1
global.round_timer_value = game.tick
global.winners = {}
global.force_points = {}
global.round_inventory = select_inventory()
global.round_equipment = select_equipment()
global.challenge_type = select_challange_type()
set_teams()
if global.challenge_type == "production" then
generate_production_task()
fill_input_chests()
return
end
if global.challenge_type == "shopping_list" then
generate_shopping_list_task()
return
end
end
function create_teams()
if not game.forces["spectators"] then
game.create_force("spectators")
end
for k, force in pairs(global.force_list) do
if not game.forces[force.name] then
local force = game.create_force(force.name)
setup_unlocks(force)
force.disable_research()
force.set_ammo_damage_modifier("bullet", -1)
force.set_ammo_damage_modifier("flamethrower", -1)
force.set_ammo_damage_modifier("capsule", -1)
force.set_ammo_damage_modifier("cannon-shell", -1)
force.set_ammo_damage_modifier("grenade", -1)
force.set_ammo_damage_modifier("electric", -1)
force.worker_robots_speed_modifier = 3
end
end
for k, force in pairs (game.forces) do
for j, friend in pairs (game.forces) do
if force.name ~= friend.name then
force.set_cease_fire(friend, true)
force.set_friend(friend, true)
end
end
end
end
function set_areas(i)
if not global.previous_map_size then
global.previous_map_size = 5
else
global.previous_map_size = map_sets[global.current_map_index].map_set_size
end
global.previous_map_index = global.current_map_index
global.current_map_index = i
if not map_sets[i] then return end
for k, player in pairs (game.players) do
set_spectator(player)
end
global.clear_areas_tick = game.tick + global.number_of_teams + 1
end
function decide_player_team(player)
if not global.online_players then return end
if not player.connected then
player.force = game.forces.spectators
return
end
if player.afk_time >= global.afk_time then player.force = game.forces.spectators end
if player.force.name == "spectators" then return end
table.insert(global.online_players, player)
end
function set_teams()
global.online_players = {}
for k, player in pairs(game.players) do
decide_player_team(player)
end
global.number_of_teams = 2
for k, player in pairs (global.online_players) do
for j, check_player in pairs(global.team1_players) do
if player == check_player then
set_player(player, 1)
break
end
end
if not team then
for j, check_player in pairs(global.team2_players) do
if player == check_player then
set_player(player, 2)
break
end
end
end
end
for k, player in pairs (global.online_players) do
give_starting_inventory(player)
end
end
function toggle_leaderboard(player)
local gui = player.gui.center
local frame = gui.leaderboard
if frame then
frame.destroy()
return
end
frame = gui.add{type = "table", name = "leaderboard", column_count = 2}
frame.style.horizontal_spacing = 0
update_leaderboard(player)
player.opened = frame
end
function update_leaderboard(player)
local flow = player.gui.center.leaderboard
if not flow then return end
flow.clear()
local frame = flow.add{type = "frame", caption = {"recent"}}
add_leaderboard_table(frame, global.recent_points)
local frame = flow.add{type = "frame", caption = {"all-time"}}
add_leaderboard_table(frame, global.points)
end
function add_leaderboard_table(gui, points)
local any = false
for k, v in pairs (points) do
any = true
break
end
if any then
local check_name = game.players[gui.player_index].name
local scroll = gui.add{type = "scroll-pane"}
scroll.style.maximal_height = 560
local inner_flow = scroll.add{type = "frame", style = "image_frame"}
inner_flow.style.left_padding = 4
inner_flow.style.right_padding = 12
inner_flow.style.bottom_padding = 4
inner_flow.style.top_padding = 4
leaderboard_table = inner_flow.add{type = "table", column_count = 3}
leaderboard_table.style.column_alignments[1] = "right"
leaderboard_table.style.column_alignments[3] = "right"
leaderboard_table.style.horizontal_spacing = 16
leaderboard_table.style.vertical_spacing = 8
leaderboard_table.draw_horizontal_line_after_headers = true
leaderboard_table.draw_vertical_lines = true
count = 1
for k, caption in pairs ({"", "name", "points"}) do
local label = leaderboard_table.add{type = "label", caption = {caption}}
label.style.font = "default-bold"
end
for name, points in spairs(points, function(t, a, b) return t[b] < t[a] end) do
local this = leaderboard_table.add{type = "label", caption = "#"..count}
this.style.font_color = {r = 1, g = 1, b = 0.2}
this.style.font = "default-semibold"
local that = leaderboard_table.add{type = "label", caption = name}
if name == check_name then
that.style.font_color = {r = 1, g = 0.6, b = 0.1}
that.style.font = "default-semibold"
end
leaderboard_table.add{type = "label", caption = util.format_number(points)}
count = count + 1
end
else
gui.add{type = "label", caption = {"none-in-leaderboard"}}
end
end
function set_player(player, k)
set_spectator(player)
local index = k
local team = global.force_list[index]
local force = game.forces[team.name]
set_character(player, force)
local c = team.color
player.color = {r = c[1], g = c[2], b = c[3], a = c[4]}
end
function setup_unlocks(force)
if not force.valid then return end
force.research_all_technologies()
local disallowed_map = {}
for k, name in pairs (global.disabled_items) do
disallowed_map[name] = true
end
for recipe_name, recipe in pairs (force.recipes) do
if disallowed_map[recipe_name] then
recipe.enabled = false
end
end
end
function generate_production_task()
local items_to_choose
local number_of_items
local type = global.challenge_type
number_of_items = math.random(1, global.max_count_of_production_tasks)
items_to_choose = global.item_list
shuffle_table(items_to_choose)
local task_items = {}
local max_count = calculate_task_item_multiplayer(number_of_items)
global.round_input = nil
for k = 1, number_of_items do
local item = items_to_choose[k]
if item.input then
if not global.round_input then
global.round_input = item.input
else
break
end
end
task_items[k] = {}
task_items[k].name = item.name
task_items[k].count = math.random(2, max_count)*item.count
end
global.task_items = task_items
global.progress = {}
for j, force in pairs (game.forces) do
global.progress[force.name] = {}
for k, item in pairs (global.task_items) do
global.progress[force.name][item.name] = 0
end
end
end
function generate_shopping_list_task()
shuffle_table(global.item_list)
local multiplier = math.ceil((#global.online_players)/global.players_per_team)
local number_of_items = math.random(1, global.max_count_of_production_tasks)
local max_count = calculate_task_item_multiplayer(number_of_items)
task_items = {}
global.round_input = nil
for k = 1, number_of_items do
local item = global.item_list[k]
if item.input then
if not global.round_input then
global.round_input = item.input
else
break
end
end
task_items[k] = {}
task_items[k].name = global.item_list[k].name
task_items[k].count = math.random(2, max_count)*global.item_list[k].count*multiplier
task_items[k].remaining = task_items[k].count
end
global.task_items = task_items
global.progress = {}
for j, force in pairs (game.forces) do
global.progress[force.name] = {}
for k, item in pairs (global.task_items) do
global.progress[force.name][item.name] = 0
end
end
end
function create_visibility_buttons(player)
local gui = mod_gui.get_button_flow(player)
for k, button in pairs ({{name = "toggle_leaderboard_button", type = "button", caption = {"leaderboard"}}}) do
if not gui[button.name] then
local button = gui.add(button)
button.style = mod_gui.button_style
end
end
end
function update_team_buttons(player)
local gui = mod_gui.get_button_flow(player)
local button1 = gui.team1_button
local button2 = gui.team2_button
local button3 = gui.leaveteam_button
local visibility = global.start_round_tick ~= nil
if not button1 then
button1 = gui.add{type = "button", name = "team1_button", style = mod_gui.button_style, caption={"join-team", "Blue"}}
end
if not button2 then
button2 = gui.add{type = "button", name = "team2_button", style = mod_gui.button_style, caption={"join-team", "Red"}}
end
if not button3 then
button3 = gui.add{type = "button", name = "leaveteam_button", style = mod_gui.button_style, caption={"leave-team"}}
end
if not visibility then
button1.style.visible = false
button2.style.visible = false
button3.style.visible = false
else
if player.force.name == "spectators" then
button1.style.visible = true
button2.style.visible = true
button3.style.visible = false
else
button1.style.visible = false
button2.style.visible = false
button3.style.visible = true
end
end
end
function check_end_of_round()
if game.tick ~= global.end_round_tick then return end
save_round_statistics()
global.end_round_tick = nil
global.start_round_tick = game.tick + global.time_between_rounds
global.chests = nil
global.input_chests = nil
global.task_items = nil
global.progress = nil
global.challenge_type = nil
for k, player in pairs(game.players) do
end_round_gui_update(player)
end
game.play_sound{path = "utility/research_completed"}
end
function chart_all()
if game.tick % 120 ~= 0 then return end
for k, force in pairs (game.forces) do
force.chart_all()
end
end
function end_round_gui_update(player)
local gui = mod_gui.get_frame_flow(player)
update_end_timer(player)
update_task_table(player)
player.print({"next-round-soon", (global.time_between_rounds/60)})
set_spectator(player)
update_team_buttons(player)
if not gui.winners_frame then return end
gui.winners_frame.caption = {"round-winners"}
end
function update_gui()
local tick = game.tick
for k, player in pairs(game.players) do
if k % 60 == tick % 60 then
update_end_timer(player)
update_task_table(player)
end
end
end
function check_start_round()
if game.tick ~= global.start_round_tick then return end
global.start_round_tick = nil
game.surfaces[1].regenerate_decorative()
start_challenge()
for k, player in pairs(game.players) do
update_task_table(player)
update_winners_list(player)
update_team_buttons(player)
end
end
function check_start_set_areas()
if not global.start_round_tick then return end
--Calculates when to start settings the areas
if global.start_round_tick - ((2 * global.number_of_teams) + 1 + ((global.number_of_teams) * global.ticks_to_generate_entities)) == game.tick then
set_areas(math.random(#map_sets))
end
end
function check_start_setting_entities()
--Start setting the entities
if not global.set_entities_tick then return end
local entities = map_sets[global.current_map_index].map_set_entities
local distance = map_sets[global.current_map_index].map_set_size
local index = math.ceil((global.set_entities_tick - game.tick)/global.ticks_to_generate_entities)
if index == 0 then
global.set_entities_tick = nil
return
end
local listed = global.force_list[index]
if not listed then return end
local grid_position = get_spawn_coordinate(index)
local force = game.forces[listed.name]
local offset_x = grid_position[1] * (distance*2 + global.distance_between_areas)
local offset_y = grid_position[2] * (distance*2 + global.distance_between_areas)
recreate_entities(entities, offset_x, offset_y, force, global.ticks_to_generate_entities)
end
function check_set_areas()
if not global.set_areas_tick then return end
local set = map_sets[global.current_map_index]
local distance = set.map_set_size
local index = global.set_areas_tick - game.tick
if index == 0 then
global.set_areas_tick = nil
global.set_entities_tick = game.tick + (global.number_of_teams * global.ticks_to_generate_entities)
return
end
local listed = global.force_list[index]
if not listed then return end
local grid_position = get_spawn_coordinate(index)
local force = game.forces[listed.name]
if not force then
game.print(listed.name.." is not a valid force")
return
end
if not force.valid then return end
local offset_x = grid_position[1] * (distance*2 + global.distance_between_areas)
local offset_y = grid_position[2] * (distance*2 + global.distance_between_areas)
create_tiles(set, offset_x, offset_y, true)
force.set_spawn_position({offset_x, offset_y}, game.surfaces[1])
force.rechart()
end
function check_clear_areas()
if not global.clear_areas_tick then return end
if not global.previous_map_index then
global.previous_map_index = 1
end
local set = map_sets[global.previous_map_index]
local distance = set.map_set_size
local index = global.clear_areas_tick - game.tick
if index == 0 then
global.clear_areas_tick = nil
global.set_areas_tick = game.tick + global.number_of_teams
return
end
local grid_position = get_spawn_coordinate(index)
local offset_x = grid_position[1] * (distance*2 + global.distance_between_areas)
local offset_y = grid_position[2] * (distance*2 + global.distance_between_areas)
create_tiles(set, offset_x, offset_y, true, true)
end
function check_chests()
if not global.chests then return end
if game.tick % 30 ~= 0 then return end
local task = global.challenge_type
if not task then return end
local update_chest
if task == "production" then
update_chest = check_chests_production
elseif task == "shopping_list" then
update_chest = check_chests_shopping_list
else
error("Unknown challenge type: "..task)
end
for k, chest in pairs (global.chests) do
if not chest.valid then
table.remove(global.chests, k)
else
update_chest(chest)
end
end
for k, force in pairs (game.forces) do
check_victory(force)
end
end
function check_chests_shopping_list(chest)
if not global.task_items then return end
for k, item in pairs (global.task_items) do
local count = chest.get_item_count(item.name)
if count > item.remaining then
count = item.remaining
end
if count > 0 then
chest.remove_item({name = item.name, count = count})
global.progress[chest.force.name][item.name] = global.progress[chest.force.name][item.name] + count
item.remaining = item.remaining - count
end
end
end
function check_chests_production(chest)
if not global.task_items then return end
for k, item in pairs (global.task_items) do
local count = chest.get_item_count(item.name)
if count + global.progress[chest.force.name][item.name] > item.count then
count = item.count - global.progress[chest.force.name][item.name]
end
if count > 0 then
chest.remove_item({name = item.name, count = count})
global.progress[chest.force.name][item.name] = global.progress[chest.force.name][item.name] + count
end
end
end
function check_input_chests()
if game.tick % 1024 ~= 0 then return end
fill_input_chests()
end
function fill_input_chests()
if not global.input_chests then return end
if not global.round_input then return end
if not game.item_prototypes[global.round_input] then game.print("BAD INPUT ITEM") return end
for k, chest in pairs (global.input_chests) do
if chest.valid then
chest.clear_items_inside()
chest.insert{name = global.round_input, count = 10000}
else
table.remove(global.input_chests, k)
end
end
end
function check_victory(force)
if not global.challenge_type then return end
if not force.valid then return end
if not global.winners then return end
for k, winners in pairs (global.winners) do
if force == winners then
return
end
end
local challenge_type = global.challenge_type
if challenge_type == "production" then
local finished_tasks = 0
for k, item in pairs (global.task_items) do
if global.progress[force.name][item.name] >= item.count then
finished_tasks = finished_tasks +1
end
end
if finished_tasks >= #global.task_items then
team_finished(force)
end
return
end
if challenge_type == "shopping_list" then
if global.winners[1] then return end
local finished_tasks = 0
for k, item in pairs (global.task_items) do
if item.remaining == 0 then
finished_tasks = finished_tasks +1
end
end
if finished_tasks >= #global.task_items then
shopping_task_finished()
end
return
end
end
function shopping_task_finished()
local total_points = global.points_per_win * global.number_of_teams
local points_per_task = total_points/(#global.task_items)
for k, item in pairs (global.task_items) do
for j, force in pairs (game.forces) do
calculate_force_points(force, item, points_per_task)
end
end
for name, points in spairs(global.force_points, function(t, a, b) return t[b] < t[a] end) do
if points > 0 then
table.insert(global.winners, game.forces[name])
end
end
global.end_round_tick = game.tick + 1
for k, player in pairs (game.players) do
update_winners_list(player)
end
end
function calculate_force_points(force,item, points)
if points <= 0 then return end
if not global.progress then return end
if not global.progress[force.name] then return end
if not global.progress[force.name][item.name] then return end
if not item.count then return end
if global.progress[force.name][item.name] <= 0 then return end
local count = global.progress[force.name][item.name]
local total = item.count
local awarded_points = math.floor((count/total)*points)
give_force_players_points(force, awarded_points)
end
function create_task_frame(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.task_frame
if frame then
frame.destroy()
end
frame = gui.add{name = "task_frame", type = "frame", direction = "vertical", caption = {"round", global.recent_round_number, global.recent_round_count}}
update_task_table(player)
end
function update_task_table(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.task_frame
if not frame then return end
frame.clear()
frame.caption = {"round", global.recent_round_number, global.recent_round_count}
local task = global.challenge_type
if global.start_round_tick ~= nil then
frame.add{type = "label", caption = {"round-starting-soon", format_time(global.start_round_tick - game.tick)}}
return
end
local task_label = frame.add{type = "label", caption = {task}, style = "caption_label"}
frame.add{type = "label", name = "round_timer", caption = {"elapsed-time", format_time(time_left())}}
local task_table = frame.add{type = "table", column_count = 3}
task_table.style.horizontal_spacing = 8
task_table.style.vertical_spacing = 8
task_table.style.column_alignments[2] = "right"
task_table.style.column_alignments[3] = "right"
local spectating = player.force.name == "spectators"
local headers
local table_string
if task == "production" then
headers = {"item-name", "current", "goal"}
table_string = "count"
elseif task == "shopping_list" then
headers = {"item-name", "current", "remaining"}
table_string = "remaining"
else
error("Unknown task type: "..task)
end
if spectating then
headers[2] = ""
end
for k, caption in pairs (headers) do
local label = task_table.add{type = "label", caption = {caption}}
label.style.font = "default-bold"
end
local progress = global.progress[player.force.name]
if not progress then error("force progress is nil: "..player.force.name) end
local items = game.item_prototypes
for k, item in pairs (global.task_items) do
local label = task_table.add{type = "label", caption = items[item.name].localised_name}
label.style.font = "default-semibold"
local progress = task_table.add{type = "label", caption = util.format_number(progress[item.name])}
if spectating then
progress.caption = ""
end
task_table.add{type = "label", caption = util.format_number(item[table_string])}
end
end
function time_left()
return game.tick - global.round_timer_value
end
function update_end_timer(player)
if not player.connected then return end
if not global.end_round_tick then return end
local gui = mod_gui.get_frame_flow(player)
if not gui.winners_frame then return end
gui.winners_frame.caption = {"winner-end-round", format_time(global.end_round_tick - game.tick)}
end
function team_finished(force)
if not force.valid then return end
if not global.progress then return end
if not global.progress[force.name] then return end
table.insert(global.winners, force)
local points = global.points_per_win
for j, winning_force in pairs (global.winners) do
if winning_force == force then
points = math.floor(points/j)
break
end
end
if #global.winners == 1 then
global.end_round_tick = game.tick + global.time_before_round_end
end
give_force_players_points(force, points)
for k, player in pairs(game.players) do
if player.force ~= force then
player.print({"finished-task", {"color."..force.name}})
player.play_sound({path = "utility/game_lost"})
else
player.print({"your-team-win", global.force_points[force.name]})
player.play_sound({path = "utility/game_won"})
set_spectator(player)
end
update_winners_list(player)
update_leaderboard(player)
end
save_points_list()
end
function save_points_list()
local points_lua = "function give_points()\n return\n {\n"
for name, points in pairs (global.points) do
points_lua = points_lua .. " [\""..name.."\"] = "..points..", \n";
end
points_lua = points_lua .. " }\nend"
game.write_file("points.lua", points_lua, false, 0)
end
function give_force_players_points(force, points)
if not force.valid then return end
if points <= 0 then return end
if not global.force_points then global.force_points = {} end
if not global.force_points[force.name] then
global.force_points[force.name] = points
else
global.force_points[force.name] = global.force_points[force.name] + points
end
for k, player in pairs (force.players) do
if not global.points[player.name] then
global.points[player.name] = points
else
global.points[player.name] = global.points[player.name] + points
end
if not global.recent_points[player.name] then
global.recent_points[player.name] = points
else
global.recent_points[player.name] = global.recent_points[player.name] + points
end
end
update_player_tags()
end
function update_player_tags()
local count = 1
local players = game.players
for name, points in spairs(global.points, function(t, a, b) return t[b] < t[a] end) do
local player = players[name]
if player then
player.tag = "[#"..count.."]"
end
count = count + 1
end
end
function update_winners_list(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.winners_frame
if not global.winners then return end
if #global.winners == 0 then
if frame then frame.destroy() end return
end
if not global.force_points then return end
if not frame then
frame = gui.add{type = "frame", name = "winners_frame", caption = {"winner-end-round", format_time(global.end_round_tick - game.tick)}, direction = "vertical"}
local winners_table = frame.add{type = "table", name = "winners_table", column_count = 5}
winners_table.style.column_alignments[4] = "right"
winners_table.style.column_alignments[5] = "right"
winners_table.style.horizontal_spacing = 8
for k, caption in pairs ({"", "name", "members", "time", "points"}) do
local label = winners_table.add{type = "label", caption = {caption}}
label.style.font = "default-bold"
end
end
for k, force in pairs(global.winners) do
if k > 5 then break end
if not global.force_points[force.name] then break end
if not gui.winners_frame.winners_table[force.name] then
local winners_table = gui.winners_frame.winners_table
local place = winners_table.add{type = "label", caption = "#"..k}
place.style.font = "default-semibold"
place.style.font_color = {r = 1, g = 1, b = 0.2}
local this = winners_table.add{type = "label", name = force.name, caption = {"", {"color."..force.name}, " ", {"team"}}}
local color = {r = 0.8, g = 0.8, b = 0.8, a = 0.8}
for i, check_force in pairs (global.force_list) do
if force.name == check_force.name then
local c = check_force.color
color = {r = 1 - (1 - c[1]) * 0.5, g = 1 - (1 - c[2]) * 0.5, b = 1 - (1 - c[3]) * 0.5, a = 1}
break
end
end
this.style.font_color = color
local caption = ""
local count = 0
for j, player in pairs(force.connected_players) do
count = count + 1
if count == 1 then
caption = caption..player.name
else
caption = caption..", "..player.name
end
end
local players_label = winners_table.add{type = "label", caption = caption}
players_label.style.single_line = false
players_label.style.maximal_width = 300
winners_table.add{type = "label", caption = format_time(time_left())}
winners_table.add{type = "label", caption = global.force_points[force.name]}
end
end
end
function set_spectator(player)
if not player.connected then return end
if not player.character then return end
local character = player.character
player.character = nil
if character then
character.destroy()
end
player.set_controller{type = defines.controllers.ghost}
end
function set_character(player, force)
if not player.connected then return end
if not force.valid then return end
player.force = force
local character = player.surface.create_entity{name = "player", position = player.surface.find_non_colliding_position("player", player.force.get_spawn_position(player.surface), 10, 2), force = force}
player.set_controller{type = defines.controllers.character, character = character}
give_equipment(player)
end
function give_starting_inventory(player)
if not player.connected then return end
if not player.character then return end
if not global.starting_inventories[global.round_inventory] then return end
for k, item in pairs (global.starting_inventories[global.round_inventory]) do
local count = math.ceil(item.count*((#global.online_players/global.number_of_teams)/#player.force.players))
if count > 0 then
player.insert{name = item.name, count = count}
end
end
end
function give_equipment(player)
if not player.connected then return end
if not player.character then return end
if not global.round_equipment then return end
if global.round_equipment == "small" then
player.insert{name = "power-armor", count = 1}
local p_armor = player.get_inventory(5)[1].grid
p_armor.put({name = "fusion-reactor-equipment"})
p_armor.put({name = "exoskeleton-equipment"})
p_armor.put({name = "personal-roboport-equipment"})
p_armor.put({name = "personal-roboport-equipment"})
p_armor.put({name = "personal-roboport-equipment"})
player.insert{name="construction-robot", count = 30}
player.insert{name="blueprint", count = 3}
player.insert{name="deconstruction-planner", count = 1}
return
end
end
function shuffle_table(t)
local iterations = #t
for i = iterations, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
function format_time(ticks)
local seconds = ticks / 60
local minutes = math.floor((seconds)/60)
local seconds = math.floor(seconds - 60*minutes)
return string.format("%d:%02d", minutes, seconds)
end
function spairs(t, order)
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
if order then
table.sort(keys, function(a, b) return order(t, a, b) end)
else
table.sort(keys)
end
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
function fill_leaderboard()
global.points = give_points()
global.recent_points = {}
for k, player in pairs (game.players) do
update_leaderboard(player)
end
end
function is_in_area(entity, force)
local origin = force.get_spawn_position(entity.surface)
local position = entity.position
local max_distance = map_sets[global.current_map_index].map_set_size
if origin.x + max_distance < position.x or
origin.x - max_distance > position.x or
origin.y + max_distance < position.y or
origin.y - max_distance > position.y then
return false
end
return true
end
competitive_production = {}
competitive_production.on_init = function ()
setup_config()
global.online_players = {}
global.winners = {}
global.points = {}
global.recent_points = {}
global.chests = {}
global.input_chests = {}
global.round_number = 0
global.recent_round_number = 0
global.number_of_teams = 2
global.team1_players = {}
global.team2_players = {}
create_teams()
fill_leaderboard()