-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
880 lines (784 loc) · 25.3 KB
/
control.lua
File metadata and controls
880 lines (784 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
-------------------------------------------------------------------------------
-- TIDY PLS
--
-- Most of the code here was initially RIPPED LIKE A SAVAGE from concreep-redux
-- https://github.com/utoxin/concreep-redux
--
-- Which means that this code is also licensed under the MIT license.
-- There's an argument that the license needs to be included in the actual source
-- files, however I've worked on relicensing exceptionally large codebases from
-- multiple licenses TO multiple licenses - and I disagree.
--
-- COME AT ME lulz
local DEBUG_LOG = false
local INTERVAL = 30 * 60
local LOG_INIT = "Tidying pls."
local LOG_NETWORK = "Processing network: %d"
local LOG_NO_PORTS = "Network %d has no valid roboports or is otherwise invalid."
local LOG_NUMPORTS = " - Available roboports: %d"
local LOG_SURFACE = " - Surface: %s#%d"
local LOG_NUMBOTS = " - Available bots: %d, minimum %d"
local LOG_ITEMCOUNT = " - Item: %s: %d"
local LOG_RADIUS_INCREASED = " - Roboport radius increased: %s"
local LOG_DONE_EXPANDING = " - Roboport done expanding: %s"
local LOG_EXPANDED = " - Expanded port %s: %d bots remaining"
local LOG_UPGRADED = " - Upgraded port %s: %d bots remaining"
local LOG_DONE = " - Roboport is done: %s"
local LOG_DISABLED = " - Tidying has been disabled for this network."
---@param var string
---@param ... string|number
local function log(var, ...)
if not DEBUG_LOG then return end
print("TIDY: ", var:format(...))
end
local MASK_GROUND_TILE = "ground_tile"
local TYPE_TILE_GHOST = "tile-ghost"
local TYPE_CLIFF = "cliff"
local TYPE_TREE = "tree"
local TYPE_SIMPLE = "simple-entity"
local TYPE_LANDFILL = "landfill"
local TYPE_ICE_PLATFORM = "ice-platform"
local TYPE_FOUNDATION = "foundation"
local TYPE_ROBOPORT = "roboport"
local FIND_FILTER = { type = TYPE_ROBOPORT, }
local TYPE_CONCRETE = "concrete"
local TYPE_BRICK = "stone-brick"
local TYPE_REFINED = "refined-concrete"
local TYPE_PATH = "stone-path"
local TYPE_EXPLOSIVES = "cliff-explosives"
local ITEM_EXPLOSIVES = { name = TYPE_EXPLOSIVES, quality = "normal", }
local ITEM_CONCRETE = { name = TYPE_CONCRETE, quality = "normal", }
local ITEM_BRICK = { name = TYPE_BRICK, quality = "normal", }
local ITEM_REFINED = { name = TYPE_REFINED, quality = "normal", }
local ITEM_TO_TILE = {
[TYPE_BRICK] = TYPE_PATH,
[TYPE_CONCRETE] = TYPE_CONCRETE,
[TYPE_REFINED] = TYPE_REFINED,
}
local UPGRADEABLE_PAVEMENTS = { TYPE_PATH, TYPE_CONCRETE, }
local C_LUA_EVENT = "folk-tidypls-toggle"
local C_TECH_ENABLE = "folk-tidypls"
---@class TidyPort
---@field roboport LuaEntity
---@field doneExpanding boolean
---@field doneUpgrading boolean
---@field upgradeArea BoundingBox @The full construction area of the roboport in which it can upgrade its tiles.
---@field radius number
---@field maxRadius number
---@field buildArea BoundingBox @The current build area, as determined by the .radius.
---@field maxEnergy number
---@class TidyNetwork
---@field id number
---@field capacity number
---@field force LuaForce|ForceID
---@field surface LuaSurface
---@field ports TidyPort[]
---@field items { [string]: number }
---@field totalPavementItems number
---@field bots number
---@class storage
---@field networks TidyNetwork[]
local getClearableEntities, countClearableEntities
do
---@type EntitySearchFilters
local filter = {
type = { TYPE_TREE, TYPE_SIMPLE, TYPE_CLIFF, },
to_be_deconstructed = false,
}
---@param surface LuaSurface
---@param area BoundingBox
---@return LuaEntity[]
getClearableEntities = function(surface, area)
filter.area = area
return surface.find_entities_filtered(filter)
end
---@param surface LuaSurface
---@param area BoundingBox
---@return number
countClearableEntities = function(surface, area)
filter.area = area
return surface.count_entities_filtered(filter)
end
end
-- Virgin/natural_width tiles are tiles that are placed at map generation
local getTilesNatural, countTilesNatural
do
-- XXX We should generate this from the prototype data
-- tileproto.is_foundation, .allows_being_covered
-- itemproto.place_as_tile_result.result = tileproto
local IGNORE_TILES = {
-- XXX when these tiles are present we need to pause and reevaluate after landfilling is done
["natural-yumako-soil"] = true,
["artificial-yumako-soil"] = true,
["overgrowth-yumako-soil"] = true,
["natural-jellynut-soil"] = true,
["artificial-jellynut-soil"] = true,
["overgrowth-jellynut-soil"] = true,
}
---@type TileSearchFilters
local filter = {
--has_hidden_tile = false,
--has_double_hidden_tile = false,
--has_tile_ghost = false,
to_be_deconstructed = false,
collision_mask = MASK_GROUND_TILE,
name = {},
-- ZZZ Unfortunately, invert also inverts collision_mask and potentially some of the other properties
}
local function generateTileFilter()
local protos = prototypes.get_tile_filtered({
{ filter = "item-to-place", invert = true, mode = "and", },
{ filter = "collision-mask", mask_mode = "layers-equals", mask = "ground_tile", mode = "and", },
})
for name in pairs(protos) do
if not IGNORE_TILES[name] then
table.insert(filter.name, name)
end
end
end
---@param surface LuaSurface
---@param limit number|nil
---@param area BoundingBox
---@return LuaTile[]
getTilesNatural = function(surface, limit, area)
if #filter.name == 0 then generateTileFilter() end
filter.limit = limit
filter.area = area
return surface.find_tiles_filtered(filter)
end
---@param surface LuaSurface
---@param limit number|nil
---@param area BoundingBox
---@return number
countTilesNatural = function(surface, limit, area)
if #filter.name == 0 then generateTileFilter() end
filter.limit = limit
filter.area = area
return surface.count_tiles_filtered(filter)
end
end
local getTilesManMade, countTilesManMade
do
---@type TileSearchFilters
local filter
local function generateTileFilter()
filter = {
--has_hidden_tile = true,
has_tile_ghost = false,
to_be_deconstructed = false,
collision_mask = MASK_GROUND_TILE,
}
-- XXX We should generate this from the prototype data
if script.active_mods["space-age"] then
filter.name = { TYPE_LANDFILL, TYPE_ICE_PLATFORM, TYPE_FOUNDATION, }
else
filter.name = TYPE_LANDFILL
end
end
---@param surface LuaSurface
---@param limit number|nil
---@param area BoundingBox
---@return LuaTile[]
getTilesManMade = function(surface, limit, area)
if not filter then generateTileFilter() end
filter.limit = limit
filter.area = area
return surface.find_tiles_filtered(filter)
end
---@param surface LuaSurface
---@param limit number|nil
---@param area BoundingBox
---@return number
countTilesManMade = function(surface, limit, area)
if not filter then generateTileFilter() end
filter.limit = limit
filter.area = area
return surface.count_tiles_filtered(filter)
end
end
local getTilesUpgradeable, countTilesUpgradeable
do
---@type TileSearchFilters
local filter = {
has_tile_ghost = false,
to_be_deconstructed = false,
collision_mask = MASK_GROUND_TILE,
}
---@param surface LuaSurface
---@param limit number|nil
---@param area BoundingBox
---@param upgrade TileID[]
---@return LuaTile[]
getTilesUpgradeable = function(surface, limit, area, upgrade)
filter.area = area
filter.name = upgrade
filter.limit = limit
return surface.find_tiles_filtered(filter)
end
---@param surface LuaSurface
---@param limit number|nil
---@param area BoundingBox
---@param upgrade TileID[]
---@return number
countTilesUpgradeable = function(surface, limit, area, upgrade)
filter.area = area
filter.name = upgrade
filter.limit = limit
return surface.count_tiles_filtered(filter)
end
end
--Is this a valid roboport?
---@param ent LuaEntity
---@return boolean
local function validPort(ent)
return ent and
ent.valid and
ent.type == TYPE_ROBOPORT and
ent.prototype.electric_energy_source_prototype and
ent.logistic_cell and
ent.logistic_cell.valid and
ent.logistic_cell.construction_radius > 0 and
ent.logistic_cell.logistic_network and
ent.logistic_cell.logistic_network.valid and
ent.logistic_cell.logistic_network.network_id and
ent.logistic_cell.logistic_network.network_id > 0 and
not storage.forget[ent] and
not ent.logistic_cell.mobile
end
---@param nid number
---@param surface LuaSurface
---@param force LuaForce|ForceID
---@return TidyNetwork
local function getTidyNetworkByNID(nid, surface, force)
---@type TidyNetwork[]
local nets = storage.networks
local index = -1
for j, net in next, nets do
if net.id == nid then
index = j
break
end
end
if index == -1 then
---@type TidyNetwork
local skynet = {
id = nid,
surface = surface,
force = force,
ports = {},
items = {},
bots = 0,
totalPavementItems = 0,
capacity = force.worker_robots_storage_bonus + 1,
}
table.insert(nets, skynet)
index = #nets
end
return nets[index]
end
---@param roboport LuaEntity
---@param area BoundingBox
---@return number, number, number
local function getPotentialJobs(roboport, area)
local possibleExpansions = countTilesNatural(roboport.surface, nil, area)
if possibleExpansions == 0 then
possibleExpansions = countTilesManMade(roboport.surface, nil, area)
end
return possibleExpansions,
countTilesUpgradeable(roboport.surface, nil, area, UPGRADEABLE_PAVEMENTS),
countClearableEntities(roboport.surface, area)
end
-- So when you're creating a new network;
-- 1. The first roboport you plop down creates a new network.
-- 2. The second roboport you plop down in range creates a new network
-- 3. The roboport from #1 is adopted into the network from #2
-- ... ffs
---@param ... LuaEntity
local function addPorts(...)
for i = 1, select("#", ...) do
---@type LuaEntity
local roboport = (select(i, ...))
if validPort(roboport) then
local already = false
-- Check to see that this roboport is not part of any existing network
for _, net in next, storage.networks do
for _, p in next, net.ports do
if p.roboport and p.roboport.valid and p.roboport.unit_number == roboport.unit_number then
already = true
break
end
end
if already then break end
end
if not already then
local max = roboport.logistic_cell.construction_radius
local maxArea = {
{ roboport.position.x - max, roboport.position.y - max, },
{ roboport.position.x + max, roboport.position.y + max, },
}
local possibleExpansions, possibleUpgrades, possibleTidying = getPotentialJobs(roboport, maxArea)
if possibleExpansions > 0 or possibleUpgrades > 0 or possibleTidying > 0 then
local nid = roboport.logistic_cell.logistic_network.network_id
local net = getTidyNetworkByNID(nid, roboport.surface, roboport.force)
---@type TidyPort
local port = {
roboport = roboport,
radius = 3,
maxRadius = max,
doneUpgrading = (possibleUpgrades == 0),
doneExpanding = (possibleExpansions == 0),
maxEnergy = roboport.prototype.electric_energy_source_prototype.buffer_capacity,
upgradeArea = maxArea,
buildArea = {
{ roboport.position.x - 3, roboport.position.y - 3, },
{ roboport.position.x + 3, roboport.position.y + 3, },
},
}
table.insert(net.ports, port)
end
end
end
end
end
-- This gets called when the mod inits, or when we tick and there's zero ports in storage
local function findPorts()
for _, surface in pairs(game.surfaces) do
addPorts(table.unpack(surface.find_entities_filtered(FIND_FILTER)))
end
end
local function initTidyPls()
---@type TidyNetwork[]
storage.networks = {}
---@type { [LuaEntity]: boolean }
storage.forget = {}
findPorts()
end
local attemptBuild
do
---@param net TidyNetwork
---@param type string
---@param position MapPosition
---@return number
local function build(net, type, position)
local count = 0
local ent = {
name = TYPE_TILE_GHOST,
position = position,
inner_name = ITEM_TO_TILE[type],
force = net.force,
}
if net.surface.can_place_entity(ent) then
-- ZZZ why was this here?
--ent.expires = false
if net.surface.create_entity(ent) then
-- Account for worker robot capacity a bit. We reduce researched capacity by -1.
--count = (1 * (1 / math.max(1, net.capacity - 1)))
count = 1
end
end
return count
end
---@param net TidyNetwork
---@param position MapPosition
---@param ... string
attemptBuild = function(net, position, ...)
for i = 1, select("#", ...) do
local item = (select(i, ...))
if net.items[item] > 0 then
local used = math.ceil(build(net, item, position))
net.bots = net.bots - used
net.items[item] = net.items[item] - used
net.totalPavementItems = net.totalPavementItems - used
return used
end
end
return 0
end
end
---@param net TidyNetwork
---@param area BoundingBox
---@param tidy boolean
---@return boolean
local function tidyExpand(net, area, tidy)
local tiles = getTilesNatural(net.surface, net.bots, area)
if #tiles == 0 then
tiles = getTilesManMade(net.surface, net.bots, area)
end
local used = 0
for _, tile in next, tiles do
used = used + attemptBuild(net, tile.position, TYPE_REFINED, TYPE_CONCRETE, TYPE_BRICK)
if net.bots < 1 then return used > 0 end
end
if tidy then
for _, clear in next, getClearableEntities(net.surface, area) do
if not clear.to_be_deconstructed() and (clear.type ~= TYPE_CLIFF or net.items[TYPE_EXPLOSIVES] > 0) then
clear.order_deconstruction(net.force)
used = used + 1
net.bots = net.bots - 1
if net.bots < 1 then return used > 0 end
end
end
end
return used > 0
end
local countItems = {
ITEM_REFINED,
ITEM_CONCRETE,
ITEM_BRICK,
ITEM_EXPLOSIVES,
}
---@param net TidyNetwork
local function recalcUpgradeTargets(net)
local ret = {}
if net.items[TYPE_REFINED] > 0 or net.items[TYPE_CONCRETE] > 0 then
table.insert(ret, TYPE_PATH)
end
if net.items[TYPE_REFINED] > 0 then
table.insert(ret, TYPE_CONCRETE)
end
return ret
end
---@param net TidyNetwork
local function validatePortsAndNetworks(net)
for i = #net.ports, 1, -1 do
if not validPort(net.ports[i].roboport) or net.ports[i].roboport.logistic_network.network_id ~= net.id then
local rp = net.ports[i].roboport
table.remove(net.ports, i)
if rp and rp.valid then
if rp.logistic_cell and rp.logistic_cell.valid and #rp.logistic_cell.neighbours > 0 then
-- Adopt into neighbouring network
addPorts(rp)
else
-- Recheck this port later
storage.forget[rp] = true
end
end
end
end
end
---@param net TidyNetwork
local function recalculateAvailableBots(net)
local first = net.ports[1].roboport
log(LOG_SURFACE, first.surface.name, first.surface.index)
local available = first.logistic_network.available_construction_robots
local minimum = math.floor(first.logistic_network.all_construction_robots * 0.1)
log(LOG_NUMBOTS, available, minimum)
if available > 0 and available >= minimum then
net.bots = math.min(available, minimum)
else
net.bots = 0
end
end
---@param net TidyNetwork
local function recalculateAvailableItems(net)
local any = false
net.totalPavementItems = 0
for _, item in next, countItems do
local c = net.ports[1].roboport.logistic_network.get_item_count(item) - 100
net.items[item.name] = c
if ITEM_TO_TILE[item.name] then
net.totalPavementItems = net.totalPavementItems + c
end
log(LOG_ITEMCOUNT, item.name, c)
if c > 0 then any = true end
end
return any
end
---@param net TidyNetwork
local function isResearched(net)
return net.force.technologies[C_TECH_ENABLE] and
net.force.technologies[C_TECH_ENABLE].researched
end
---@param net TidyNetwork
local function isEnabled(net)
local enabled = false
for _, player in pairs(game.players) do
if player.force == net.force then
enabled = player.is_shortcut_toggled(C_LUA_EVENT)
break
end
end
if not enabled then
log(LOG_DISABLED)
end
return enabled
end
---@param net TidyNetwork
---@param ghosts { [LuaEntity]: number }
---@param expanded { [LuaEntity]: boolean }
local function performExpansion(net, ghosts, expanded)
for _, port in next, net.ports do
if not port.doneExpanding and port.maxEnergy == port.roboport.energy then
local roboport = port.roboport
-- Dont do anything if there's any ghosts in the build area
if not ghosts[roboport] then
ghosts[roboport] = net.surface.count_entities_filtered({
area = port.buildArea,
name = TYPE_TILE_GHOST,
force = roboport.force,
limit = 1,
})
end
if ghosts[roboport] == 0 then
expanded[roboport] = tidyExpand(net, port.buildArea, port.radius < port.maxRadius)
if not expanded[roboport] then
if port.radius < port.maxRadius then
log(LOG_RADIUS_INCREASED, roboport.backer_name)
port.radius = port.radius + 1
port.buildArea = {
{ roboport.position.x - port.radius, roboport.position.y - port.radius, },
{ roboport.position.x + port.radius, roboport.position.y + port.radius, },
}
-- Dont upgrade around this roboport.
-- The mod has probably been added to the game mid-run
expanded[roboport] = true
else
-- Roboport is done expanding.
port.doneExpanding = true
log(LOG_DONE_EXPANDING, roboport.backer_name)
end
else
log(LOG_EXPANDED, roboport.backer_name, net.bots)
end
end
end
if net.bots < 1 or net.totalPavementItems < 1 then break end
end
end
---@param net TidyNetwork
---@param ghosts { [LuaEntity]: number }
---@param expanded { [LuaEntity]: boolean }
local function performUpgrades(net, ghosts, expanded)
-- We've expanded, now see if we can upgrade
local upgradeTargets = recalcUpgradeTargets(net)
if #upgradeTargets > 0 then
for _, port in next, net.ports do
if not port.doneUpgrading and not expanded[port.roboport] and port.maxEnergy == port.roboport.energy then
-- We dont need to recheck this because expanded[] will fail
-- for those ports that built anything
if not ghosts[port.roboport] then
ghosts[port.roboport] = net.surface.count_entities_filtered({
area = port.upgradeArea,
name = TYPE_TILE_GHOST,
force = port.roboport.force,
limit = 1,
})
end
if ghosts[port.roboport] == 0 then
local upgrades = getTilesUpgradeable(
net.surface,
math.min(
math.max(net.items[TYPE_CONCRETE], net.items[TYPE_REFINED], 0),
net.bots
),
port.upgradeArea,
upgradeTargets
)
if #upgrades == 0 then
port.doneUpgrading = true
else
local used = 0
for _, tile in next, upgrades do
used = used +
attemptBuild(
net,
tile.position,
TYPE_REFINED,
TYPE_CONCRETE
)
if net.bots < 1 then break end
end
if used > 0 then
log(LOG_UPGRADED, port.roboport.backer_name, net.bots)
upgradeTargets = recalcUpgradeTargets(net)
end
end
end
end
if port.doneExpanding and port.doneUpgrading then
log(LOG_DONE, port.roboport.backer_name)
storage.forget[port.roboport] = true
end
if net.bots < 1 or #upgradeTargets == 0 or (net.items[TYPE_REFINED] < 1 and net.items[TYPE_CONCRETE] < 1) then
break
end
end
end
end
---@param net TidyNetwork
local function isInvalidNetwork(net)
return #net.ports == 0 or not net.force or not net.force.valid or not net.surface or not net.surface.valid
end
local function tidypls()
if not storage.networks then initTidyPls() end
if #storage.networks == 0 then findPorts() end
---@type { [LuaEntity]: boolean }
local expanded = {}
---@type { [LuaEntity]: number }
local ghosts = {}
log(LOG_INIT)
for j = #storage.networks, 1, -1 do
local net = storage.networks[j]
log(LOG_NETWORK, net.id)
validatePortsAndNetworks(net)
if isInvalidNetwork(net) then
log(LOG_NO_PORTS, net.id)
table.remove(storage.networks, j)
else
log(LOG_NUMPORTS, #net.ports)
recalculateAvailableBots(net)
if net.bots > 0 and isResearched(net) and isEnabled(net) then
local any = recalculateAvailableItems(net)
if any then
-- utoxin pls what is this
if net.force.max_successful_attempts_per_tick_per_construction_queue * 60 < net.bots then
net.force.max_successful_attempts_per_tick_per_construction_queue = math.floor(net.bots / 60)
end
-- First check if we can expand at all
performExpansion(net, ghosts, expanded)
if net.bots > 0 and net.totalPavementItems > 0 then
performUpgrades(net, ghosts, expanded)
end
-- Shufflefeet penguins
for i = #net.ports, 2, -1 do
local x = math.random(i)
net.ports[i], net.ports[x] = net.ports[x], net.ports[i]
end
end
end
end
end
end
---@param event OnBuiltEntity|OnRobotBuiltEntity|OnEntityCloned|ScriptRaisedRevive
local function built(event)
local ent = event.destination or event.entity
if ent and ent.valid and ent.type == TYPE_ROBOPORT then
if not storage.networks or not storage.forget then
initTidyPls()
end
if validPort(ent) then
addPorts(ent)
end
end
end
script.on_event(defines.events.on_built_entity, built)
script.on_event(defines.events.on_robot_built_entity, built)
script.on_event(defines.events.on_entity_cloned, built)
script.on_event(defines.events.script_raised_revive, built)
script.on_nth_tick(INTERVAL, tidypls)
script.on_init(initTidyPls)
script.on_configuration_changed(initTidyPls)
local INFO_TOGGLED_FORCE = { "folk-tidypls.enabled-force", }
local INFO_TOGGLED_BY = "folk-tidypls.enabled-by"
local function keyCombo(event)
local clicker = game.players[event.player_index]
if not clicker or not clicker.valid then return end
-- You can toggle it with the keyboard shortcut even if it's not researched so we check
local researched = clicker.force.technologies[C_TECH_ENABLE] and clicker.force.technologies[C_TECH_ENABLE]
.researched
if not researched then return end
local state = clicker.is_shortcut_toggled(C_LUA_EVENT)
for _, pl in pairs(game.players) do
if pl.force == clicker.force then
pl.set_shortcut_toggled(C_LUA_EVENT, not state)
pl.print({ INFO_TOGGLED_BY, clicker.name, })
end
end
end
script.on_event(C_LUA_EVENT, keyCombo)
script.on_event(defines.events.on_lua_shortcut, function(event)
---@cast event OnLuaShortcut
if not event or event.prototype_name ~= C_LUA_EVENT then return end
keyCombo(event)
end)
script.on_event(defines.events.on_research_finished, function(event)
---@cast event OnResearchFinished
if event.research.name == C_TECH_ENABLE then
for _, pl in pairs(game.players) do
if pl.force == event.research.force then
pl.set_shortcut_toggled(C_LUA_EVENT, true)
pl.print(INFO_TOGGLED_FORCE)
end
end
end
if not storage.networks or not storage.forget then return end
-- Make sure we reevaluate all nets and ports
-- First, remove all ports from all nets that are no longer valid, and put them
-- into storage.forget if they still exist.
for _, net in next, storage.networks do
for j = #net.ports, 1, -1 do
local rob = net.ports[j].roboport
if not validPort(rob) or rob.logistic_network.network_id ~= net.id then
if rob and rob.valid then
-- Recheck this port later
storage.forget[rob] = true
end
table.remove(net.ports, j)
end
end
end
-- Second, nuke all empty or invalid nets
for i = #storage.networks, 1, -1 do
local net = storage.networks[i]
if #net.ports == 0 or not net.force or not net.force.valid or not net.surface or not net.surface.valid then
-- But preserve all valid roboports. Not sure if there can ever be any though
for _, port in next, net.ports do
if port.roboport and port.roboport.valid then
storage.forget[port.roboport] = true
end
end
table.remove(storage.networks, i)
end
end
-- Third, revive any forgotten roboports that are still valid and that still have work to do
for port in pairs(storage.forget) do
if port and port.valid then
local max = port.logistic_cell.construction_radius
local area = {
{ port.position.x - max, port.position.y - max, },
{ port.position.x + max, port.position.y + max, },
}
local exp, ups, tidyings = getPotentialJobs(port, area)
if exp ~= 0 or ups ~= 0 or tidyings ~= 0 then
-- Clear first because addPorts checks validPort, which checks .forget
storage.forget[port] = nil
addPorts(port)
end
else
storage.forget[port] = nil
end
end
-- Fourth, upgrade the radius of all registered ports if necessary
for _, net in next, storage.networks do
net.capacity = net.force.worker_robots_storage_bonus + 1
for _, port in next, net.ports do
if port.roboport.logistic_cell.construction_radius ~= port.maxRadius then
port.maxRadius = port.roboport.logistic_cell.construction_radius
port.upgradeArea = {
{ port.roboport.position.x - port.maxRadius, port.roboport.position.y - port.maxRadius, },
{ port.roboport.position.x + port.maxRadius, port.roboport.position.y + port.maxRadius, },
}
local exp, ups, tidyings = getPotentialJobs(port.roboport, port.upgradeArea)
port.doneExpanding = (exp == 0)
port.doneUpgrading = (ups == 0) and (tidyings == 0) -- XXX double check
end
end
end
-- Fifth, find any roboports in the networks that we've missed for some reason
-- XXX This takes way too much processing, and I'm not even sure if it's necessary.
-- for _, net in next, storage.networks do
-- local port = net.ports[1]
-- if port then
-- local rp = port.roboport
-- if rp and rp.valid and rp.logistic_network then
-- local ln = rp.logistic_network
-- local ports = {}
-- for _, cell in next, ln.cells do
-- table.insert(ports, cell.owner)
-- end
-- -- addPorts checks storage.forget and valid and so forth, so just add indiscriminately
-- addPorts(table.unpack(ports))
-- end
-- end
-- end
end)