forked from Fatmice/Uranium-Power-Cont
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlaaaaa.lua
2087 lines (1946 loc) · 87.7 KB
/
controlaaaaa.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 "util"
require "library.constants"
require "library.mathlibs"
--/c game.local_player.surface.create_entity({name="medium-explosion", position=game.local_player.position, force=game.local_player.force})
--local newExplosion = util.table.deepcopy(data.raw.explosion["medium-explosion"])
--newExplosion.name = "new-medium-explosion"
--newExplosion.created_effect = nil
--data:extend({newExplosion})
--pressure_to_speed_ratio flow_to_energy_ratio, max_push_amount and ratio_to_push
--speed = columnDiff * fluidPrototype->pressureToSpeedRatio + connection.flowEnergy - oppositeConnection.flowEnergy;
--max_push_amount = 1
--ratio_to_push= 2.1
--[03:02:23] <Rseding91> Are they named?
--[03:02:32] <Rseding91> It's just table indexing.
--[03:02:37] <Rseding91> If they're named, add in the new names.
--[03:02:42] <Rseding91> If they're numbered, use insert.
--[03:36:41] <Rseding91> un-named: data.raw.player.something = {{height=2}, {height=3}, {height=4}}
--[03:36:46] <Rseding91> each table is un-named
--[03:37:03] <Rseding91> named: data.raw.player.something = {entry1={height=2}, entry2={height=3}, entry3={height=4}}
--[03:37:08] <Rseding91> each one is named
local task_names = {}
local task_to_index = {}
local task_default_states = {}
local logging = false
script.on_init(function()
global.TickerA = 59
global.ROSTER = global.ROSTER or {}
global.ACTIVE = global.ACTIVE or {}
global.dirty= {}
global.dirty[game.tick+54000] = true
global.fluidProperties = {}
--Instantiate fluidProperties
--Fluid physical properties {type = {Default Temperature, Max Temperature, Heat Capacity}}
--Default Temperature in C as defined in prototype.fluid
--Max Temperature in C as defined in prototype.fluid
--Heat Capacity in KJ/C as defined in prototype.fluid
--Pressurised Water at 16.6 MPa, 350C has specific isobar heat capacity of 10.0349 kJ/(kg K)
--Water at 101325 Pa, 15C has specific isobar heat capacity of 4.1891 kJ / kg K
--Superheated steam at 6.5 MPa, 350C has specific isobar heat capacity of 2.9561 kJ/(kg K)
--Saturated steam at 0.1 MPa, 100C has specific isobar heat capacity steam of 2.0759 kJ/(kg K) , specific isobar heat capacity water of 4.2161 kJ/(kg K)
for fluid_name,fluid_properties in pairs(game.fluid_prototypes) do
--game.print(fluid..","..game.fluid_prototypes[fluid].default_temperature..","..game.fluid_prototypes[fluid].max_temperature..","..game.fluid_prototypes[fluid].heat_capacity/1000)
global.fluidProperties[fluid_name] = {
[1] = fluid_properties.default_temperature,
[2] = fluid_properties.max_temperature,
[3] = (fluid_properties.heat_capacity / 1000)
}
end
-- game.write_file("/UraniumPower/fluid.txt", serpent.block(global.fluidProperties))
initialize_tasks_table()
end)
function version_is_older_than(v1,v2)
if v1 and v2 then
local n1 = string.gsub(v1,"%.","")
local n2 = string.gsub(v2,"%.","")
n1 = tonumber(n1)
n2 = tonumber(n2)
if (n1 < n2) then return true else return false end
end
return false
end
script.on_configuration_changed(function(event)
global.TickerA = global.TickerA or 59
global.ROSTER = global.ROSTER or {}
global.ACTIVE = global.ACTIVE or {}
global.fluidProperties = {}
for fluid_name,fluid_properties in pairs(game.fluid_prototypes) do
--game.print(fluid..","..game.fluid_prototypes[fluid].default_temperature..","..game.fluid_prototypes[fluid].max_temperature..","..game.fluid_prototypes[fluid].heat_capacity/1000)
global.fluidProperties[fluid_name] = {
[1] = fluid_properties.default_temperature,
[2] = fluid_properties.max_temperature,
[3] = (fluid_properties.heat_capacity / 1000)
}
end
-- game.write_file("/UraniumPower/fluid.txt", serpent.block(global.fluidProperties))
if event.mod_changes["UraniumPower"] ~= nil then
local v1 = event.mod_changes["UraniumPower"].old_version
if version_is_older_than(v1,"0.5.1") then
-- Initialize a new row in old reactors
if global.LReactorAndChest ~= nil then
for k,LReactorandChest in pairs(global.LReactorAndChest) do
LReactorandChest[5] = LReactorandChest[5] or 0
end
end
game.print("Applied UraniumPower 0.5.1 changes")
end
if version_is_older_than(v1,"0.5.2") then
-- Initialize a new row in old reactors
if global.LReactorAndChest ~= nil then
for k,LReactorandChest in pairs(global.LReactorAndChest) do
LReactorandChest[5] = LReactorandChest[5] or 0
end
end
-- Heat Exchanger global table migration
global.oldheatExchanger = global.oldheatExchanger or {}
if global.LHeatExchanger ~= nil then
for k,LHeatExchanger in ipairs(global.LHeatExchanger) do
table.insert(global.oldheatExchanger, LHeatExchanger)
table.remove(global.LHeatExchanger, k)
end
end
game.print("Applied UraniumPower 0.5.2 changes")
end
if version_is_older_than(v1,"0.6.0") then
if global.LReactorAndChest ~= nil then
for k,LReactorandChest in pairs(global.LReactorAndChest) do
LReactorandChest[6] = LReactorandChest[6] or false
end
end
game.print("Applied UraniumPower 0.6.0 changes")
end
if version_is_older_than(v1,"0.6.6") then
if global.LReactorAndChest ~= nil then
for _,entityrecord in pairs(global.LReactorAndChest) do
local reactor = entityrecord[1]
entityrecord["record"] = {
[1] = {
["id"] = reactor.unit_number,
["position"] = {
["x"] = reactor.position.x,
["y"] = reactor.position.y
},
["name"] = reactor.name,
["direction"] = reactor.direction,
["type"] = game.entity_prototypes[reactor.name].type
},
[2] = {
["id"] = entityrecord[2].unit_number,
["position"] = {
["x"] = entityrecord[2].position.x,
["y"] = entityrecord[2].position.y
},
["name"] = entityrecord[2].name,
["direction"] = entityrecord[2].direction,
["type"] = game.entity_prototypes[entityrecord[2].name].type
},
["surfacename"] = reactor.surface.name,
["force"] = reactor.force
}
entityrecord[1] = {
[1] = reactor,
[2] = reactor.fluidbox
}
entityrecord["update"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["return_state"] = nil, ["frequency"] = 60, ["task"] = "inspect_reactor"},
[2] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["return_state"] = nil, ["frequency"] = 1, ["task"] = "add_reactor_energy"}
}
table.insert(global.ROSTER, entityrecord)
end
global.LReactorAndChest = nil
end
if global.steamGenerators ~= nil then
for _,entityrecord in pairs(global.steamGenerators) do
local steamGenerator = entityrecord[1]
local hotLegBox = entityrecord[3]
local coldLegBox = entityrecord[4]
entityrecord["record"] = {
[1] = {
["id"] = steamGenerator.unit_number,
["position"] = {
["x"] = steamGenerator.position.x,
["y"] = steamGenerator.position.y
},
["name"] = steamGenerator.name,
["direction"] = steamGenerator.direction,
["type"] = game.entity_prototypes[steamGenerator.name].type
},
[2] = {
["id"] = entityrecord[2].unit_number,
["position"] = {
["x"] = entityrecord[2].position.x,
["y"] = entityrecord[2].position.y
},
["name"] = entityrecord[2].name,
["direction"] = entityrecord[2].direction,
["type"] = game.entity_prototypes[entityrecord[2].name].type
},
[3] = {
["id"] = hotLegBox.unit_number,
["position"] = {
["x"] = hotLegBox.position.x,
["y"] = hotLegBox.position.y
},
["name"] = hotLegBox.name,
["direction"] = hotLegBox.direction,
["type"] = game.entity_prototypes[hotLegBox.name].type
},
[4] = {
["id"] = coldLegBox.unit_number,
["position"] = {
["x"] = coldLegBox.position.x,
["y"] = coldLegBox.position.y
},
["name"] = coldLegBox.name,
["direction"] = coldLegBox.direction,
["type"] = game.entity_prototypes[coldLegBox.name].type
},
["surfacename"] = steamGenerator.surface.name,
["force"] = steamGenerator.force
}
entityrecord[1] = {
[1] = steamGenerator,
[2] = steamGenerator.fluidbox
}
entityrecord[3] = {
[1] = hotLegBox,
[2] = hotLegBox.fluidbox
}
entityrecord[4] = {
[1] = coldLegBox,
[2] = coldLegBox.fluidbox
}
entityrecord["update"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["frequency"] = 60, ["return_state"] = nil, ["task"] = "high_pressure_steam_generation"}
}
table.insert(global.ROSTER, entityrecord)
end
global.steamGenerators = nil
end
if global.turbineGenerators ~= nil then
for _,entityrecord in pairs(global.turbineGenerators) do
local turbineGenerator = entityrecord[1]
local lowPressureSteamBox = entityrecord[2]
local coldLegBox = entityrecord[3]
local coolingWaterBox = entityrecord[4]
entityrecord["record"] = {
[1] = {
["id"] = turbineGenerator.unit_number,
["position"] = {
["x"] = turbineGenerator.position.x,
["y"] = turbineGenerator.position.y
},
["name"] = turbineGenerator.name,
["direction"] = turbineGenerator.direction,
["type"] = game.entity_prototypes[turbineGenerator.name].type
},
[2] = {
["id"] = lowPressureSteamBox[1].unit_number,
["position"] = {
["x"] = lowPressureSteamBox[1].position.x,
["y"] = lowPressureSteamBox[1].position.y
},
["name"] = lowPressureSteamBox[1].name,
["direction"] = lowPressureSteamBox[1].direction,
["type"] = game.entity_prototypes[lowPressureSteamBox[1].name].type
},
[3] = {
["id"] = coldLegBox.unit_number,
["position"] = {
["x"] = coldLegBox.position.x,
["y"] = coldLegBox.position.y
},
["name"] = coldLegBox.name,
["direction"] = coldLegBox.direction
},
[4] = {
["id"] = coolingWaterBox.unit_number,
["position"] = {
["x"] = coolingWaterBox.position.x,
["y"] = coolingWaterBox.position.y
},
["name"] = coolingWaterBox.name,
["direction"] = coolingWaterBox.direction,
["type"] = game.entity_prototypes[coolingWaterBox.name].type
},
["surfacename"] = turbineGenerator.surface.name,
["force"] = turbineGenerator.force
}
entityrecord[1] = {
[1] = turbineGenerator,
[2] = turbineGenerator.fluidbox
}
entityrecord[2] = {
[1] = {
[1] = lowPressureSteamBox[1],
[2] = lowPressureSteamBox[1].fluidbox
},
[2] = lowPressureSteamBox[2],
[3] = lowPressureSteamBox[3]
}
entityrecord[3] = {
[1] = coldLegBox,
[2] = coldLegBox.fluidbox
}
entityrecord[4] = {
[1] = coolingWaterBox,
[2] = coolingWaterBox.fluidbox
}
entityrecord["update"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["frequency"] = 60, ["return_state"] = nil, ["task"] = "low_pressure_steam_condensation"},
[2] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["frequency"] = 1, ["return_state"] = nil, ["task"] = "calculate_generator_power_output"}
}
table.insert(global.ROSTER, entityrecord)
end
global.turbineGenerators = nil
end
if global.oldheatExchanger ~= nil then
for _,entityrecord in pairs(global.oldheatExchanger) do
local entity = entityrecord[1]
local box1 = entityrecord[2]
local box2 = entityrecord[3]
entityrecord["record"] = {
[1] = {
["id"] = entity.unit_number,
["position"] = {
["x"] = entity.position.x,
["y"] = entity.position.y
},
["type"] = game.entity_prototypes[entity.name].type
},
["surfacename"] = entity.surface.name,
["force"] = entity.force
}
entityrecord[1] = {
[1] = entity
}
entityrecord[2] = {
[1] = box1,
[2] = box1.fluidbox
}
entityrecord[3] = {
[1] = box2,
[2] = box2.fluidbox
}
entityrecord["update"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["frequency"] = 5, ["return_state"] = nil, ["task"] = "wall_heat_exchange"}
}
table.insert(global.ROSTER, entityrecord)
end
global.oldheatExchanger = nil
end
if global.NHeatExchanger ~= nil then
for _,entityrecord in pairs(global.NHeatExchanger) do
local recipeHeatExchanger = {
["record"] = {
[1] = {
["id"] = entityrecord[1].unit_number,
["position"] = {
["x"] = entityrecord[1].position.x,
["y"] = entityrecord[1].position.y
},
["type"] = game.entity_prototypes[entityrecord[1].name].type
},
["surfacename"] = entityrecord[1].surface.name,
["force"] = entityrecord[1].force
},
[1] = {
[1] = entityrecord[1],
[2] = entityrecord[1].fluidbox
},
["update"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["frequency"] = 5, ["return_state"] = nil, ["task"] = "recipe_heat_exchange_crafting_progress"},
[2] = {["use_roster"] = false, ["active"] = false, ["ticked"] = false, ["ticked_on"] = 0, ["frequency"] = 1, ["return_state"] = nil, ["task"] = "recipe_heat_exchange"}
}
}
if entityrecord[1].get_inventory(defines.inventory.fuel).is_empty() then
entityrecord[1].get_fuel_inventory().insert({name="solid-fuel",count=1})
end
table.insert(global.ROSTER, recipeHeatExchanger)
end
global.NHeatExchanger = nil
end
global.TickerB = nil
global.dirty = {}
global.dirty[game.tick+54000] = true
--game.write_file("/test/ROSTER.txt", serpent.block(global.ROSTER))
game.print("Applied UraniumPower 0.6.6 changes")
end
end
end)
function initialize_tasks_table()
-----------------------------------------------------------------------------
-- Defining table of update functions for each entity names
task_names = {
["inspect_reactor"] = inspect_reactor,
["add_reactor_energy"] = add_reactor_energy,
["high_pressure_steam_generation"] = high_pressure_steam_generation,
["low_pressure_steam_condensation"] = low_pressure_steam_condensation,
["calculate_generator_power_output"] = calculate_generator_power_output,
["recipe_heat_exchange_crafting_progress"] = recipe_heat_exchange_crafting_progress,
["recipe_heat_exchange"] = recipe_heat_exchange,
["wall_heat_exchange"] = wall_heat_exchange
}
task_to_index = {
["nuclear-fission-reactor-3-by-3"] = {
["inspect_reactor"] = 1,
["add_reactor_energy"] = 2
},
["nuclear-fission-reactor-5-by-5"] = {
["inspect_reactor"] = 1,
["add_reactor_energy"] = 2
},
["reactor-steam-generator-01"] = {
["high_pressure_steam_generation"] = 1
},
["reactor-turbine-generator-01a"] = {
["low_pressure_steam_condensation"] = 1,
["calculate_generator_power_output"] = 2
},
["reactor-turbine-generator-01b"] = {
["low_pressure_steam_condensation"] = 1,
["calculate_generator_power_output"] = 2
},
["heat-exchanger"] = {
["wall_heat_exchange"] = 1,
},
["S-new-heat-exchanger-01"] = {
["recipe_heat_exchange_crafting_progress"] = 1,
["recipe_heat_exchange"] = 2
},
["R-new-heat-exchanger-01"] = {
["recipe_heat_exchange_crafting_progress"] = 1,
["recipe_heat_exchange"] = 2
},
["S-new-heat-exchanger-02"] = {
["recipe_heat_exchange_crafting_progress"] = 1,
["recipe_heat_exchange"] = 2
},
["R-new-heat-exchanger-02"] = {
["recipe_heat_exchange_crafting_progress"] = 1,
["recipe_heat_exchange"] = 2
}
}
task_default_states = {
["nuclear-fission-reactor-3-by-3"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["nuclear-fission-reactor-5-by-5"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["reactor-steam-generator-01"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["reactor-turbine-generator-01a"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["reactor-turbine-generator-01b"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["heat-exchanger"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["S-new-heat-exchanger-01"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = false, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["R-new-heat-exchanger-01"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = false, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["S-new-heat-exchanger-02"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = false, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
},
["R-new-heat-exchanger-02"] = {
[1] = {["use_roster"] = true, ["active"] = false, ["ticked"] = false, ["return_state"] = nil},
[2] = {["use_roster"] = false, ["active"] = false, ["ticked"] = false, ["return_state"] = nil}
}
}
end
script.on_load(function()
initialize_tasks_table()
end)
function clean_global()
-- game.print("Table cleaning due on "..game.tick)
-----------------------------------------------------------------------------
-- Cleaning ROSTER
-- Disables for now as this messes up ROSTER indexing and I am currently using for ticking logic
if global.ROSTER then
-- game.print("Cleaning ROSTER")
local new_ROSTER = {}
for index,value in pairs(global.ROSTER) do
if value ~=nil then
new_ROSTER[index] = value
end
end
global.ROSTER = new_ROSTER
end
-----------------------------------------------------------------------------
-- Cleaning ACTIVE
if global.ACTIVE then
-- game.print("Cleaning ACTIVE")
local new_ACTIVE = {}
for tick,tasks_table in pairs(global.ACTIVE) do
if tasks_table ~= nil then
new_ACTIVE[tick] = new_ACTIVE[tick] or {}
local new_tasks_table = {}
for _, task_entry in pairs(tasks_table) do
if task_entry ~= nil then
table.insert(new_tasks_table,task_entry)
end
end
new_ACTIVE[tick] = new_tasks_table
end
end
global.ACTIVE = new_ACTIVE
end
-- game.print("Next table cleaning due on "..game.tick+54000)
global.dirty[game.tick] = nil
global.dirty[game.tick+54000] = true
end
function setActiveTick(entity_index, entity_name, task, tick)
local do_task_on = {entity_index, entity_name, task}
local task_index = task_to_index[entity_name][task]
global.ACTIVE[tick] = global.ACTIVE[tick] or {}
table.insert(global.ACTIVE[tick],do_task_on)
logger("afterticking", game.tick, tick, entity_index, entity_name, task, global.ROSTER[entity_index]["update"][task_index]["task"])
end
function inspect_table(entity_index)
if entity_index == nil then
return
elseif global.ROSTER[entity_index] ~= nil then
local entity_table = global.ROSTER[entity_index]
if entity_table[1][1].valid then
local entity_name = entity_table[1][1].name
if entity_name == "nuclear-fission-reactor-3-by-3" or entity_name == "nuclear-fission-reactor-5-by-5" then
if not entity_table[2].valid then
-- Reactor chest reference
local x = entity_table["record"][2]["position"]["x"]
local y = entity_table["record"][2]["position"]["y"]
local subentity_name = entity_table["record"][2]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][2]["direction"]
local subentity_force = entity_table["record"]["force"]
local ghost = game.surfaces[surface_name].find_entity("entity-ghost",{x,y})
local entity
if ghost ~= nil then
entity = select(2,ghost.revive())
end
if entity ~= nil then
entity_table[2] = entity
entity_table["record"][2]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
else
local entity = game.surfaces[surface_name].create_entity{name = subentity_name, position = {x,y}, direction = direction, force = subentity_force}
entity_table[2] = entity
entity_table["record"][2]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
end
end
elseif entity_name == "reactor-steam-generator-01" then
if not entity_table[2].valid then
-- Reactor reference
-- Sorry no reviving reactor but can keep checking for a new one in the same place
local x = entity_table["record"][2]["position"]["x"]
local y = entity_table["record"][2]["position"]["y"]
local subentity_name = entity_table["record"][2]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][2]["direction"]
local subentity_force = entity_table["record"]["force"]
local reactor = game.surfaces[surface_name].find_entity("nuclear-fission-reactor-3-by-3",{x,y})
if reactor ~= nil then
entity_table[2] = reactor
entity_table["record"][2]["id"] = reactor.unit_number
global.ROSTER[entity_index] = entity_table
end
end
if not entity_table[3][1].valid then
-- Hot Leg reference
local x = entity_table["record"][3]["position"]["x"]
local y = entity_table["record"][3]["position"]["y"]
local subentity_name = entity_table["record"][3]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][3]["direction"]
local subentity_force = entity_table["record"]["force"]
local ghost = game.surfaces[surface_name].find_entity("entity-ghost",{x,y})
local entity
if ghost ~= nil then
entity = select(2,ghost.revive())
end
if entity ~= nil then
entity_table[3][1] = entity
entity_table[3][2] = entity.fluidbox
entity_table["record"][3]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
else
local entity = game.surfaces[surface_name].create_entity{name = subentity_name, position = {x,y}, direction = direction, force = subentity_force}
entity_table[3][1] = entity
entity_table[3][2] = entity.fluidbox
entity_table["record"][3]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
end
end
if not entity_table[4][1].valid then
-- Cold Leg reference
local x = entity_table["record"][4]["position"]["x"]
local y = entity_table["record"][4]["position"]["y"]
local subentity_name = entity_table["record"][4]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][4]["direction"]
local subentity_force = entity_table["record"]["force"]
local ghost = game.surfaces[surface_name].find_entity("entity-ghost",{x,y})
local entity
if ghost ~= nil then
entity = select(2,ghost.revive())
end
if entity ~= nil then
entity_table[4][1] = entity
entity_table[4][2] = entity.fluidbox
entity_table["record"][4]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
else
local entity = game.surfaces[surface_name].create_entity{name = subentity_name, position = {x,y}, direction = direction, force = subentity_force}
entity_table[4][1] = entity
entity_table[4][2] = entity.fluidbox
entity_table["record"][4]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
end
end
elseif entity_name == "reactor-turbine-generator-01a" or entity_name == "reactor-turbine-generator-01b" then
if not entity_table[2][1][1].valid then
-- Low pressure steam box reference
local x = entity_table["record"][2]["position"]["x"]
local y = entity_table["record"][2]["position"]["y"]
local subentity_name = entity_table["record"][2]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][2]["direction"]
local subentity_force = entity_table["record"]["force"]
local ghost = game.surfaces[surface_name].find_entity("entity-ghost",{x,y})
local entity
if ghost ~= nil then
entity = select(2,ghost.revive())
end
if entity ~= nil then
entity_table[2][1][1] = entity
entity_table[2][1][2] = entity.fluidbox
entity_table["record"][2]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
else
local entity = game.surfaces[surface_name].create_entity{name = subentity_name, position = {x,y}, direction = direction, force = subentity_force}
entity_table[2][1][1] = entity
entity_table[2][1][2] = entity.fluidbox
entity_table["record"][2]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
end
end
if not entity_table[3][1].valid then
-- Cold Leg reference
local x = entity_table["record"][3]["position"]["x"]
local y = entity_table["record"][3]["position"]["y"]
local subentity_name = entity_table["record"][3]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][3]["direction"]
local subentity_force = entity_table["record"]["force"]
local ghost = game.surfaces[surface_name].find_entity("entity-ghost",{x,y})
local entity
if ghost ~= nil then
entity = select(2,ghost.revive())
end
if entity ~= nil then
entity_table[3][1] = entity
entity_table[3][2] = entity.fluidbox
entity_table["record"][3]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
else
local entity = game.surfaces[surface_name].create_entity{name = subentity_name, position = {x,y}, direction = direction, force = subentity_force}
entity_table[3][1] = entity
entity_table[3][2] = entity.fluidbox
entity_table["record"][3]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
end
end
if not entity_table[4][1].valid then
-- Cooling Water reference
local x = entity_table["record"][4]["position"]["x"]
local y = entity_table["record"][4]["position"]["y"]
local subentity_name = entity_table["record"][4]["name"]
local surface_name = entity_table["record"]["surfacename"]
local direction = entity_table["record"][4]["direction"]
local subentity_force = entity_table["record"]["force"]
local ghost = game.surfaces[surface_name].find_entity("entity-ghost",{x,y})
local entity
if ghost ~= nil then
entity = select(2,ghost.revive())
end
if entity ~= nil then
entity_table[4][1] = entity
entity_table[4][2] = entity.fluidbox
entity_table["record"][4]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
else
local entity = game.surfaces[surface_name].create_entity{name = subentity_name, position = {x,y}, direction = direction, force = subentity_force}
entity_table[4][1] = entity
entity_table[4][2] = entity.fluidbox
entity_table["record"][4]["id"] = entity.unit_number
global.ROSTER[entity_index] = entity_table
end
end
end
end
end
end
function logger(logtype, ...)
if logging then
local data = {...}
if logtype == "beginticking" then
local tick, roster_index, entity_name, assigned_task = data[1], data[2], data[3], data[4]
local str = string.format("%-10s%3s%33s%40s\n", tick, roster_index, entity_name, assigned_task)
game.write_file("/UraniumPower/BEGINTICKING.txt", str, true)
elseif logtype == "afterticking" then
local tick, next_tick, roster_index, entity_name, assigned_task = data[1], data[2], data[3], data[4], data[5]
local str = string.format("%-10s %-10s%3s%33s%40s\n", tick, next_tick, roster_index, entity_name, assigned_task)
game.write_file("/UraniumPower/AFTERTICKING.txt", str, true)
elseif logtype == "stopticking" then
local tick, roster_index, entity_name, assigned_task = data[1], data[2], data[3], data[4]
local str = string.format("%-10s%3s%33s%40s\n", tick, roster_index, entity_name, assigned_task)
game.write_file("/UraniumPower/STOPTICKING.txt", str, true)
end
end
end
script.on_event(defines.events.on_tick, function(event)
if global.dirty[game.tick] then
clean_global()
end
if global.TickerA == 0 then
global.TickerA = 59
-- Once per second check ROSTER for things that could be set to ACTIVE
for entity_index,entity_table in pairs(global.ROSTER) do
if entity_table ~= nil then
if entity_table[1][1].valid then
local entity_tasks = entity_table["update"]
if entity_tasks ~= nil then
for task_index = 1,#entity_tasks do
if entity_tasks[task_index]["use_roster"] then
if not entity_tasks[task_index]["active"] then
if not entity_tasks[task_index]["ticked"] then
local next_tick = game.tick
local task = entity_tasks[task_index]["task"]
setActiveTick(entity_index, entity_table[1][1].name, task, next_tick)
-- game.print("On game tick"..game.tick.." activated task:"..task.." for "..entity_table[1][1].name.." at "..entity_table[1][1].position.x..", "..entity_table[1][1].position.y.." to tick on "..next_tick)
end
entity_table["update"][task_index]["active"] = true
end
end
end
end
else
-- game.print("Something died while in ROSTER.")
inspect_table(entity_index, entity_table)
global.ROSTER[entity_index] = nil
end
end
end
else
global.TickerA = global.TickerA - 1
end
-- ACTIVE entities. Tick according to rules. Subscription based ticking.
if global.ACTIVE[game.tick] then
-- game.print("There are tasks to do on game tick: "..game.tick)
for k,task_entry in pairs(global.ACTIVE[game.tick]) do
local entity_index, task , entity_table = nil, nil, nil
if task_entry ~= nil then
entity_index = task_entry[1]
task = task_entry[3]
entity_table = global.ROSTER[entity_index]
if entity_table ~= nil then
if entity_table[1][1].valid then
logger("beginticking", game.tick, entity_index, entity_table[1][1].name, task)
local task_completed = false
local return_code = nil
local extra_return_parameter = nil
local task_index = nil
-- game.print("Performing task on "..entity_table[1][1].name)
-- Check task compatibility on entity..For some reason a task can be wrongly assigned to an entity..WTF!?
if task_to_index[entity_table[1][1].name][task] ~= nil then
-- Task compatible. Run task!
task_index = task_to_index[entity_table[1][1].name][task]
task_completed, return_code, extra_return_parameter = task_names[task](entity_index)
else
-- Task incompatible. WTF!? How did this happen?!
local roster = {}
local active = {}
roster = table.deepcopy(global.ROSTER)
active = table.deepcopy(global.ACTIVE)
game.print("Task: "..task.." was wrongly assigned for "..entity_table[1][1].name.." on tick: "..game.tick.."! It can be found at(x,y): "..entity_table[1][1].position.x..","..entity_table[1][1].position.y)
game.print("Entity might not recovered from this error and might need to be rebuilt!")
game.print("global.ACTIVE and global.ROSTER have been dumped to script-output/UraniumPower. Please include them and a screen-shot of this message when reporting this error!")
game.write_file("/UraniumPower/global.ROSTER_tick_"..game.tick.."_entity-index_"..entity_index..".txt", serpent.block(roster))
game.write_file("/UraniumPower/global.ACTIVE_tick_"..game.tick..".txt", serpent.block(active))
-- Try to salvage global.ACTIVE by striking out any entries containing the same entity_index
for tick,tasks_table in pairs(global.ACTIVE) do
if tasks_table ~= nil then
for _, task_entry in pairs(tasks_table) do
if task_entry ~= nil then
if task_entry[1] == entity_index then
task_entry = nil
end
end
end
end
end
-- Reset all tasks to default states for entity
for taskIndex,_ in pairs(entity_table["update"]) do
entity_table["update"][taskIndex]["use_roster"] = task_default_states[entity_table[1][1].name][taskIndex]["use_roster"]
entity_table["update"][taskIndex]["active"] = task_default_states[entity_table[1][1].name][taskIndex]["active"]
entity_table["update"][taskIndex]["ticked"] = task_default_states[entity_table[1][1].name][taskIndex]["ticked"]
entity_table["update"][taskIndex]["return_state"] = task_default_states[entity_table[1][1].name][taskIndex]["return_state"]
end
global.ACTIVE[game.tick][k] = nil
end
if task_completed then
-- game.print("Task: "..task.." completed on "..entity_table[1][1].name.." at(x,y) "..entity_table[1][1].position.x..","..entity_table[1][1].position.y.." on tick: "..game.tick)
local next_tick = nil
entity_table["update"][task_index]["ticked"] = true
entity_table["update"][task_index]["ticked_on"] = game.tick
entity_table["update"][task_index]["return_state"] = return_code
if extra_return_parameter == nil then
if entity_table["update"][task_index]["active"] then
-- game.print("Retick: "..entity_table[1][1].name.." at "..entity_table[1][1].position.x..", "..entity_table[1][1].position.y.." Index: "..entity_index.." Task index: "..task_index.." on tick: "..next_tick)
local entity_name = entity_table[1][1].name
if task_to_index[entity_name][task] ~= nil then
task_index = task_to_index[entity_name][task]
next_tick = game.tick + entity_table["update"][task_index]["frequency"]
setActiveTick(entity_index, entity_name, task, next_tick)
else
entity_table["update"][task_index]["active"] = false
end
end
elseif type(extra_return_parameter) == "number" then
-- task next_tick changed to another number, but continues on the same task
local entity_name = entity_table[1][1].name
next_tick = extra_return_parameter
if task_to_index[entity_name][task] ~= nil then
setActiveTick(entity_index, entity_name, task, next_tick)
else
entity_table["update"][task_index]["active"] = false
end
elseif extra_return_parameter == "recipe_heat_exchange" then
-- task recipe_heat_exchange_crafting_progress caught crafting progress at 100%, disabling task recipe_heat_exchange_crafting_progress
entity_table["update"][task_index]["use_roster"] = false
entity_table["update"][task_index]["active"] = false
-- activating task recipe_heat_exchange, which checks on each tick until it can be performed
local entity_name = entity_table[1][1].name
task = extra_return_parameter
if task_to_index[entity_name][task] ~= nil then
task_index = task_to_index[entity_name][task]
next_tick = game.tick + entity_table["update"][task_index]["frequency"]
entity_table["update"][task_index]["active"] = true
setActiveTick(entity_index, entity_name, task, next_tick)
else
entity_table["update"][task_index]["active"] = false
end
elseif extra_return_parameter == "recipe_heat_exchange_crafting_progress" then
-- task recipe_heat_exchange succeeded, disabling until called again by recipe_heat_exchange_crafting_progress
entity_table["update"][task_index]["active"] = false
-- activating recipe_heat_exchange_crafting_progress
local entity_name = entity_table[1][1].name
task = extra_return_parameter
if task_to_index[entity_name][task] ~= nil then
task_index = task_to_index[entity_name][task]
next_tick = game.tick + entity_table["update"][task_index]["frequency"]
entity_table["update"][task_index]["use_roster"] = true
entity_table["update"][task_index]["active"] = true
setActiveTick(entity_index, entity_name, task, next_tick)
else
entity_table["update"][task_index]["active"] = false
end
end
global.ACTIVE[game.tick][k] = nil
else
if return_code == 1 then
-- game.print("Failed to tick "..entity_table[1][1].name.." at "..entity_table[1][1].position.x..", "..entity_table[1][1].position.y.." due to insufficient condition on task: "..task.." on game tick: "..game.tick)
entity_table["update"][task_index]["active"] = false
entity_table["update"][task_index]["ticked"] = false
entity_table["update"][task_index]["return_state"] = return_code
end
if return_code == 2 then
-- game.print("Failed to tick "..entity_table[1][1].name.." at "..entity_table[1][1].position.x..", "..entity_table[1][1].position.y.." due to invalid references")
entity_table["update"][task_index]["ticked"] = false
entity_table["update"][task_index]["active"] = false
entity_table["update"][task_index]["return_state"] = return_code
inspect_table(entity_index)
end
global.ACTIVE[game.tick][k] = nil
end
else
-- game.print("Something died while in ACTIVE.")
global.ACTIVE[game.tick][k] = nil
end
end
end
end
global.ACTIVE[game.tick] = nil
end
end)
script.on_event(defines.events.on_built_entity, function(event)
local x1 = event.created_entity.position.x
local y1 = event.created_entity.position.y
local x2 = x1
local y2 = y1
-- Fission reactor stuff
if event.created_entity.name == "nuclear-fission-reactor-3-by-3" then
event.created_entity.operable = false
if (game.players[event.player_index].get_inventory(defines.inventory.player_quickbar).get_item_count("nuclear-fission-reactor-chest-15") + game.players[event.player_index].get_inventory(defines.inventory.player_main).get_item_count("nuclear-fission-reactor-chest-15")) < 1 then
game.players[event.player_index].insert({name = "nuclear-fission-reactor-chest-15", count = 1})
end
game.players[event.player_index].print("Place the reactor access port next to the fission reactor.")
elseif event.created_entity.name == "nuclear-fission-reactor-5-by-5" then
event.created_entity.operable = false
if (game.players[event.player_index].get_inventory(defines.inventory.player_quickbar).get_item_count("nuclear-fission-reactor-chest-25") + game.players[event.player_index].get_inventory(defines.inventory.player_main).get_item_count("nuclear-fission-reactor-chest-25")) < 1 then
game.players[event.player_index].insert({name = "nuclear-fission-reactor-chest-25", count = 1})
end
game.players[event.player_index].print("Place the reactor access port next to the fission reactor.")
elseif event.created_entity.name == "nuclear-fission-reactor-chest-15" then
x1 = x1 - 1
y1 = y1 - 1
x2 = x2 + 1
y2 = y2 + 1
results = event.created_entity.surface.find_entities_filtered{area = {{x1, y1}, {x2, y2}}, name = "nuclear-fission-reactor-3-by-3"}
if #results == 1 then
local reactorAndChest = {}
-- Reference to reactor building
reactorAndChest["record"] = {
[1] = {
["id"] = results[1].unit_number,
["position"] = {
["x"] = results[1].position.x,
["y"] = results[1].position.y
},
["name"] = results[1].name,
["direction"] = results[1].direction,
["type"] = game.entity_prototypes[results[1].name].type
},
[2] = {
["id"] = event.created_entity.unit_number,
["position"] = {
["x"] = event.created_entity.position.x,
["y"] = event.created_entity.position.y
},
["name"] = event.created_entity.name,
["direction"] = event.created_entity.direction,
["type"] = game.entity_prototypes[event.created_entity.name].type
},
["surfacename"] = results[1].surface.name,
["force"] = game.players[event.player_index].force
}
reactorAndChest[1] = {
[1] = results[1],