-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStorageLink.lua
More file actions
2270 lines (1877 loc) · 88.5 KB
/
Copy pathStorageLink.lua
File metadata and controls
2270 lines (1877 loc) · 88.5 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
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
-- Static
FRAMES_BETWEEN_CHECK = 10
LAST_TIME_DELTA = 0
WORLD_LIMITS = {1,1}
VERSION_WITH_CLASSMETHODCHECK_FUNCTION = "137.15" -- dev version. Update before uploading to steam!!
-- Internal Databases
OBJECTS_IN_FLIGHT = {}
SWITCHES_TURNED_OFF = {} -- key = '>some name'. use pairs().
TIMEOUT_DB = {}
-- Internal Caching
LINK_UIDS = {}
STORAGE_UIDS = {}
-- Multiple Types
BALANCER_TYPES = {'Balancer (SL)', 'Balancer Long (SL)'}
PUMP_TYPES = {'Pump (SL)', 'Pump Long (SL)'}
OVERFLOW_TYPES = {'Overflow Pump (SL)', 'Overflow Pump Long (SL)'}
RECEIVER_TYPES = {'Receiver (SL)'}
TRANSMITTER_TYPES = {'Transmitter (SL)'}
-- Unlock a level when this is built
GOOD_UNLOCK_BUILDINGS = {'MortarMixerCrude', 'MortarMixerGood'}
SUPER_UNLOCK_BUILDINGS = {'MetalWorkbench'}
SECONDS_BETWEEN_UNLOCK_CHECKS = 5
UNLOCK_TIMER_SECOND = 0
-- Levels of speed
CRUDE_CHECKS_PER_SECOND = 0.25
GOOD_CHECKS_PER_SECOND = 1
SUPER_CHECKS_PER_SECOND = 1 -- but we transfer way more per check!
-- Tracking timers (these change with each OnUpdate() call)
CRUDE_TIMER_SECOND = 0
GOOD_TIMER_SECOND = 0
SUPER_TIMER_SECOND = 0
FIVE_SECOND_TIMER = 0
-- Variables that can change
FRAME_COUNTER = 0
DEBUG_ENABLED = false
USE_EVENT_STYLE = false
-- Wishlist
-- OVERFLOW PIPE (/pressure relief) -> when source side is full, move one if possible.
-- Transmitter Priority by name "Priority 1", "Priority 2"... 1 being most important.
-- Receiver Priority "Priority 1", "Priority 2"...etc
-- Priority: - Unprioritized? use level 50.
function SteamDetails()
-- Setting of Steam details
ModBase.SetSteamWorkshopDetails("Storage Links", [[
A set of links that can hook storages together. This is a great minimal mod.
=== Crude, Good, Super ===
- Crude _____ are available at all times, can move up to 1 item every 5 seconds.
- Good _____ are available once a Mortar Mixer is built and can move up to 5 items every second.
- Super _____ are available once a Steam Hammer is built and can move unlimited items per second.
- All levels have the capability to be SWITCHED on or off using the 'Super Switch (SL)'
=== Balancer (SL) ===
- Keeps two storages of similiar type balanced.
- Long version functions exactly the same.
=== Pump (SL) ===
- Pumps product into the storage indicated by the arrow.
- Long version functions exactly the same.
=== Overflow Pump (SL) ===
- Only operates if the source side is at max capacity.
- Removes the qty as defined below:
- Crude: 1% (rounded up).
- Good: 5% (rounded up).
- Super: 10% (rounded up).
=== Receiver (SL) ===
- Requests and will receive whatever it can of the type that fits into this storage.
- If there are multiple receivers per type of item stored, the emptiest storage is always dealt with first.
=== Transmitter (SL) ===
- Transmits any requested types, if it can from the attached storage.
=== Magnet (SL) ===
- Attach to a storage. Collects items that fit in storage, within 10x10 around magnet.
- If you can name the magnet, setting name to '80x80' will collect items 80 tiles wide by 80 tiles tall.
=== Switch (SL) ===
- Name the link you wish to control with a name like "sw[GROUP NAME]" (Use "L" key.).
- Build the switch anywhere.
- Name the SWITCH like ">GROUP NAME". (always start with ">")
- You can only have one switch per group name. (use anything you want for group name)
~~ Future ~~
- Be able to transfer to/from train carriages. As of 136.24, the Modding API does not support interacting with train carriages.
~= Enjoy =~
]], {"transport", "storage"}, "logo.jpg")
end
function Expose()
if ModBase.GetGameVersion() == 'Version 136.17.2' or ModBase.GetGameVersion() == 'Version 136.17.2' or ModBase.GetGameVersion() == '136.17.2' then
return
end
ModBase.ExposeVariable("Enable Debug Mode", false, ExposedVariableCallback)
ModBase.ExposeKeybinding("Debug: Move", 8, ExposedKeyCallback)
end
function ExposedKeyCallback(name)
if ModBase.GetGameState() ~= 'Normal' then return end
if (name == 'Debug: Move') then
locateLinks('Crude')
locateLinks('Good')
locateLinks('Super')
end
end
function ExposedVariableCallback(value, name)
if (name == 'Enable Debug Mode') then DEBUG_ENABLED = value end
end
function Creation()
-- Pump
ModBuilding.CreateBuilding("Crude Pump (SL)" , {"Log","Pole"} , {1, 2} , "PumpCrude" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Good Pump (SL)" , {"Mortar","Pole"} , {4, 8} , "PumpGood" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Pump (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "PumpSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Pump Long (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "PumpSuperLong" , {0,0} , {0,0}, null, true )
-- Overflow Pump
ModBuilding.CreateBuilding("Crude Overflow Pump (SL)" , {"Log","Pole"} , {1, 2} , "OverflowCrude" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Good Overflow Pump (SL)" , {"Mortar","Pole"} , {4, 8} , "OverflowGood" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Overflow Pump (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "OverflowSuper" , {0,0} , {0,0}, null, true )
-- Balancer
ModBuilding.CreateBuilding("Crude Balancer (SL)" , {"Log","Pole"} , {1, 2} , "BalCrude" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Good Balancer (SL)" , {"Mortar","Pole"} , {4, 8} , "BalGood" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Balancer (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "BalSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Balancer Long (SL)",{"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "BalSuperLong" , {0,0} , {0,0}, null, true )
-- Transmitter
ModBuilding.CreateBuilding("Crude Transmitter (SL)" , {"Log","Pole","TreeSeed"} , {2, 3, 1} , "TransmitterCrude", {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Good Transmitter (SL)" , {"Mortar","Pole","TreeSeed"} , {4, 10, 1}, "TransmitterGood" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Transmitter (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets", "UpgradeWorkerCarrySuper"}, {4, 6, 6, 1}, "TransmitterSuper" , {0,0} , {0,0}, null, true )
-- Receiver
ModBuilding.CreateBuilding("Crude Receiver (SL)" , {"Log","Pole","TreeSeed"} , {2, 3, 1} , "ReceiverCrude" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Good Receiver (SL)" , {"Mortar","Pole","TreeSeed"} , {4, 10, 1}, "ReceiverGood" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Receiver (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets", "UpgradeWorkerCarrySuper"}, {4, 6, 6, 1}, "ReceiverSuper" , {0,0} , {0,0}, null, true )
-- Magnet
ModBuilding.CreateBuilding("Crude Magnet (SL)" , {"Rock","TreeSeed"} , {2, 1} , "MagnetCrude" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Good Magnet (SL)" , {"Rock","StringBall"} , {4, 3} , "MagnetGood" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Super Magnet (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets", "UpgradeWorkerCarrySuper"}, {2, 2, 4, 1}, "MagnetSuper" , {0,0} , {0,0}, null, true )
-- Switch
ModBuilding.CreateBuilding("Super Switch (SL)" , {"MetalPlateCrude","Plank"} , {1, 3}, "Switch" , {0,0} , {0,0}, null, true )
ModDecorative.CreateDecorative("Switch On Symbol (SL)", {"TreeSeed",} , {1}, "SwitchOn" , true )
-- Misc Symbols
ModDecorative.CreateDecorative("Broken Symbol (SL)" , {"TreeSeed"} , {1}, "BrokenSymbol", true )
-- Buildings that can be walked through
ModBuilding.SetBuildingWalkable("Super Switch (SL)", true)
-- Discontinuing these names -- here so they show up in existing games
ModBuilding.CreateBuilding("Storage Pump (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "PumpSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Storage Pump XL (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "PumpSuperLong" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Storage Transmitter (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets", "UpgradeWorkerCarrySuper"}, {4, 6, 6, 1}, "TransmitterSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Storage Receiver (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets", "UpgradeWorkerCarrySuper"}, {4, 6, 6, 1}, "ReceiverSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Storage Magnet (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets", "UpgradeWorkerCarrySuper"}, {2, 2, 4, 1}, "MagnetSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Storage Balancer (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "BalSuper" , {0,0} , {0,0}, null, true )
ModBuilding.CreateBuilding("Storage Balancer XL (SL)" , {"MetalPlateCrude","MetalPoleCrude","Rivets"} , {4, 8, 8} , "BalSuperLong" , {0,0} , {0,0}, null, true )
-- Set some overall globals that determine if we want to use a TIMER, or callbacks.
if ModBase.IsGameVersionGreaterThanEqualTo(VERSION_WITH_CLASSMETHODCHECK_FUNCTION) then
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingRenamedCallback') then
USE_EVENT_STYLE = true
end
end
end
function OnUpdate(timeDelta)
-- Called on every cycle!
updateFlightPositions()
everyFrame(timeDelta)
-- Every Five SECONDS_BETWEEN_UNLOCK_CHECKS
FIVE_SECOND_TIMER = FIVE_SECOND_TIMER + timeDelta
if FIVE_SECOND_TIMER >= 5 then
-- discoverUnknownMagnets()
FIVE_SECOND_TIMER = 0
end
if DEBUG_ENABLED == false then
--local secondsDiff = timeDelta + LAST_TIME_DELTA
--LAST_TIME_DELTA = timeDelta -- time is in decimal seconds
-- Update timing trackers
CRUDE_TIMER_SECOND = CRUDE_TIMER_SECOND + timeDelta
GOOD_TIMER_SECOND = GOOD_TIMER_SECOND + timeDelta
SUPER_TIMER_SECOND = SUPER_TIMER_SECOND + timeDelta
UNLOCK_TIMER_SECOND = UNLOCK_TIMER_SECOND + timeDelta
-- Crude Level
if CRUDE_TIMER_SECOND >= (1 / CRUDE_CHECKS_PER_SECOND) then
locateLinks('Crude')
CRUDE_TIMER_SECOND = 0
end
-- Good Level
if GOOD_TIMER_SECOND >= (1 / GOOD_CHECKS_PER_SECOND) then
locateLinks('Good')
GOOD_TIMER_SECOND = 0
end
-- Super Level
if SUPER_TIMER_SECOND >= (1 / SUPER_CHECKS_PER_SECOND) then
locateLinks('Super')
SUPER_TIMER_SECOND = 0
end
-- UnlockCheck
-- if UNLOCK_TIMER_SECOND >= SECONDS_BETWEEN_UNLOCK_CHECKS then
-- checkUnlockLevels()
-- UNLOCK_TIMER_SECOND = 0
-- end
end
end
function BeforeLoad()
-- -- Pump
-- ModVariable.SetVariableForBuildingUpgrade("Crude Pump (SL)", "Good Pump (SL)" )
-- ModVariable.SetVariableForBuildingUpgrade("Good Pump (SL)" , "Super Pump (SL)")
-- -- Overflow Pump
-- ModVariable.SetVariableForBuildingUpgrade("Crude Overflow Pump (SL)", "Good Overflow Pump (SL)" )
-- ModVariable.SetVariableForBuildingUpgrade("Good Overflow Pump (SL)" , "Super Overflow Pump (SL)")
-- -- Balancer
-- ModVariable.SetVariableForBuildingUpgrade("Crude Balancer (SL)", "Good Balancer (SL)" )
-- ModVariable.SetVariableForBuildingUpgrade("Good Balancer (SL)" , "Super Balancer (SL)")
-- -- Magnet
-- ModVariable.SetVariableForBuildingUpgrade("Crude Magnet (SL)", "Good Magnet (SL)" )
-- ModVariable.SetVariableForBuildingUpgrade("Good Magnet (SL)" , "Super Magnet (SL)")
-- -- Transmitter
-- ModVariable.SetVariableForBuildingUpgrade("Crude Transmitter (SL)", "Good Transmitter (SL)" )
-- ModVariable.SetVariableForBuildingUpgrade("Good Transmitter (SL)" , "Super Transmitter (SL)")
-- -- Receiver
-- ModVariable.SetVariableForBuildingUpgrade("Crude Receiver (SL)", "Good Receiver (SL)" )
-- ModVariable.SetVariableForBuildingUpgrade("Good Receiver (SL)" , "Super Receiver (SL)")
-- Access Points
-- ModBuilding.ShowBuildingAccessPoint("Crude Pump (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Good Pump (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Crude Overflow Pump (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Good Overflow Pump (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Crude Balancer (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Good Balancer (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Crude Magnet (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Good Magnet (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Crude Transmitter (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Good Transmitter (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Crude Receiver (SL)" , true)
-- ModBuilding.ShowBuildingAccessPoint("Good Receiver (SL)" , true)
-- Hide old names
ModVariable.SetVariableForObjectAsInt("Storage Pump (SL)","Unlocked",0 )
ModVariable.SetVariableForObjectAsInt("Storage Pump XL (SL)","Unlocked",0 )
ModVariable.SetVariableForObjectAsInt("Storage Transmitter (SL)","Unlocked",0 )
ModVariable.SetVariableForObjectAsInt("Storage Receiver (SL)","Unlocked",0 )
ModVariable.SetVariableForObjectAsInt("Storage Magnet (SL)","Unlocked",0 )
ModVariable.SetVariableForObjectAsInt("Storage Balancer (SL)","Unlocked",0 )
ModVariable.SetVariableForObjectAsInt("Storage Balancer XL (SL)","Unlocked",0 )
-- Hide symbols
ModVariable.SetVariableForObjectAsInt("Switch On Symbol (SL)","Unlocked", 0)
ModVariable.SetVariableForObjectAsInt("Broken Symbol (SL)" ,"Unlocked", 0)
--lockLevels()
checkUnlockLevels()
end
function AfterLoad() -- Once a game has loaded key functionality, this is called.
swapOldNamesToNew()
end
function AfterLoad_CreatedWorld() -- Only called when creating a game.
end
function AfterLoad_LoadedWorld() -- Only called on loading a game.
lockLevels()
checkUnlockLevels()
WORLD_LIMITS = ModTiles.GetMapLimits()
-- Reset caches
LINK_UIDS = {}
STORAGE_UIDS = {}
-- When world is loaded, find Magnets!
discoverUnknownMagnets()
end
function lockLevels()
ModVariable.SetVariableForObjectAsInt("Good Pump (SL)","Unlocked", 0)
ModVariable.SetVariableForObjectAsInt("Good Pump Long (SL)","Unlocked", 0)
ModVariable.SetVariableForObjectAsInt("Good Balancer (SL)","Unlocked", 0)
ModVariable.SetVariableForObjectAsInt("Good Transmitter (SL)","Unlocked", 0)
ModVariable.SetVariableForObjectAsInt("Good Receiver (SL)","Unlocked", 0)
ModVariable.SetVariableForObjectAsInt("Good Magnet (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Pump (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Pump Long (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Overflow Pump (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Balancer (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Balancer Long (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Transmitter (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Receiver (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Magnet (SL)","Unlocked", 0)
-- ModVariable.SetVariableForObjectAsInt("Super Switch (SL)","Unlocked", 0)
end
function checkUnlockLevels()
-- Is one of the GOOD_UNLOCK_BUILDINGS built?
if ModVariable.GetVariableForObjectAsInt('Good Pump (SL)', 'Unlocked') == 0 then
if isABuildingInTableOnMap(GOOD_UNLOCK_BUILDINGS) then
ModVariable.SetVariableForObjectAsInt("Good Pump (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Good Overflow Pump (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Good Balancer (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Good Transmitter (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Good Receiver (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Good Magnet (SL)","Unlocked", 1)
end
end
-- Is one of the SUPER_UNLOCK_BUILDINGS built?
if ModVariable.GetVariableForObjectAsInt('Super Pump (SL)', 'Unlocked') == 0 then
if isABuildingInTableOnMap(SUPER_UNLOCK_BUILDINGS) then
ModVariable.SetVariableForObjectAsInt("Super Pump (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Pump Long (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Overflow Pump (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Balancer (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Balancer Long (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Transmitter (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Receiver (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Magnet (SL)","Unlocked", 1)
ModVariable.SetVariableForObjectAsInt("Super Switch (SL)","Unlocked", 1)
end
end
end
function swapOldNamesToNew()
-- swapOldNameToNew('Storage Pump (SL)','Super Pump (SL)')
-- swapOldNameToNew('Storage Pump XL (SL)','Super Pump Long (SL)')
-- swapOldNameToNew('Storage Transmitter (SL)','Super Transmitter (SL)')
-- swapOldNameToNew('Storage Receiver (SL)','Super Receiver (SL)')
-- swapOldNameToNew('Storage Balancer (SL)','Super Balancer (SL)')
-- swapOldNameToNew('Storage Balancer XL (SL)','Super Balancer Long (SL)')
end
function swapOldNameToNew(oldName,newName)
local oldB = ModTiles.GetObjectsOfTypeInAreaUIDs(oldName, 0, 0, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
if oldB == nil or oldB == -1 or oldB[1] == nil or oldB[1] == -1 then return false end
local props, newUID, rot
for _, uid in ipairs(oldB) do
props = ModObject.GetObjectProperties(uid) -- Properties [1]=Type, [2]=TileX, [3]=TileY, [4]=Rotation, [5]=Name,
rot = ModBulding.GetRotation(uid)
if ModObject.DestroyObject(uid) then
newUID = ModBase.SpawnItem(newName, props[2], props[3], false, true, false)
if newUID == -1 or newUID == nil then
ModDebug.Log('Could not re-create the ', props[1], ' @ ', props[2], ':', props[3])
else
ModBuilding.SetRotation(newUID, rot)
ModBuilding.SetBuildingNam(newUID, props[5])
end
end -- of if object destroyed
end -- of oldB loop
end
function locateLinks(levelPrefix)
if ModBase.GetGameState() ~= 'Normal' then return end
setTimeout(locateSwitches, {levelPrefix}, 0 )
setTimeout(locatePumps, {levelPrefix}, 150 )
setTimeout(locateOverflowPumps, {levelPrefix}, 300 )
setTimeout(locateBalancers, {levelPrefix}, 450 )
setTimeout(locateReceiversAndTransmitters, {levelPrefix}, 600 )
-- setTimeout(locateMagnets, {levelPrefix}, 750 )
-- new style - this should only look for types that fit in attached storage, in area
setTimeout(fireAllMagnets, {levelPrefix}, 750 )
end
function locateBalancers(levelPrefix)
-- Find all Balancers
local tmpUIDs = {}
local balancerUIDs = {}
for _, bType in ipairs(BALANCER_TYPES) do
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType(levelPrefix .. ' ' .. bType, 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
balancerUIDs[#balancerUIDs+1] = tUID
end
end
--Legacy
if levelPrefix == 'Super' then
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType('Storage Balancer (SL)', 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
balancerUIDs[#balancerUIDs+1] = tUID
end
end
-- END Legacy
-- quit if none
if balancerUIDs == nil or balancerUIDs[1] == nil or balancerUIDs[1] == -1 then return end
-- List the balancer's found
for _, uid in ipairs(balancerUIDs) do
if DEBUG_ENABLED then ModDebug.Log(' locateBalancers: ', uid ) end
end
-- Handle Each Balancer
for _, uid in ipairs(balancerUIDs)
do
locateStoragesForLink(uid,'both', levelPrefix)
end
end
function locatePumps(levelPrefix)
-- Find all Pumps
local tmpUIDs = {}
local pumpUIDs = {}
for _, pType in ipairs(PUMP_TYPES) do
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType(levelPrefix .. ' ' .. pType, 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
pumpUIDs[#pumpUIDs+1] = tUID
end
end
-- LEGACY
if levelPrefix == 'Super' then
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType('Storage Pump (SL)', 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
pumpUIDs[#pumpUIDs+1] = tUID
end
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType('Storage Pump XL (SL)', 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
pumpUIDs[#pumpUIDs+1] = tUID
end
end
-- END LEGACY
-- quit if none
if pumpUIDs == nil or pumpUIDs[1] == nil or pumpUIDs[1] == -1 then return end
-- List the pumps's found
for _, uid in ipairs(pumpUIDs) do
if DEBUG_ENABLED then ModDebug.Log(' locatePumps: ', uid ) end
end
-- handle each pump
for _, uid in ipairs(pumpUIDs)
do
if uid ~= -1 then
locateStoragesForLink(uid,'one', levelPrefix)
end
end
end
function locateOverflowPumps(levelPrefix)
-- Find all Pumps
local tmpUIDs = {}
local pumpUIDs = {}
for _, pType in ipairs(OVERFLOW_TYPES) do
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType(levelPrefix .. ' ' .. pType, 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
pumpUIDs[#pumpUIDs+1] = tUID
end
end
-- quit if none
if pumpUIDs == nil or pumpUIDs[1] == nil or pumpUIDs[1] == -1 then return end
-- List the pumps's found
for _, uid in ipairs(pumpUIDs) do
if DEBUG_ENABLED then ModDebug.Log(' locatePumps: ', uid ) end
end
-- handle each pump
for _, uid in ipairs(pumpUIDs)
do
if uid ~= -1 then
locateStoragesForLink(uid, 'one', levelPrefix, true)
end
end
end
-- Switches
function locateSwitches(levelPrefix)
-- Find all Pumps
local tmpUIDs = {}
local switchUIDs = {}
tmpUIDs = ModBuilding.GetAllBuildingsUIDsOfType(levelPrefix .. ' Switch (SL)', 1, 1, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
for _2, tUID in ipairs(tmpUIDs) do
switchUIDs[#switchUIDs+1] = tUID
end
-- quit if none
if switchUIDs == nil or switchUIDs[1] == nil or switchUIDs[1] == -1 then return end
-- List the switches's found
if DEBUG_ENABLED then
for _, uid in ipairs(switchUIDs) do ModDebug.Log(' localSwitches: ', uid ) end
end
-- handle each of switchUIDs
for _, switchUID in ipairs(switchUIDs)
do
if switchUID ~= -1 then
determineSwitchTargetState(switchUID, ModPlayer.GetLocation())
end
end
end
function determineSwitchTargetState(switchUID, playerXY)
-- If "farmerPlayer" or "Worker" is on tile, state should be OnUpdate
-- otherwise OFF.
local switchProps = ModObject.GetObjectProperties(switchUID) -- [1]=Type, [2]=TileX, [3]=TileY, [4]=Rotation, [5]=Name,
-- Ignore this switch if it's name does not start with ">"
local ms, me = string.find(switchProps[5],'>.+')
if ms == nil then return end
-- Do we have multiple with the same name?
local switchXY = {switchProps[2], switchProps[3]}
local numBrokenSymbols = ModTiles.GetAmountObjectsOfTypeInArea('Broken Symbol (SL)', switchXY[1], switchXY[2], switchXY[1], switchXY[2])
local switchesByName = ModBuilding.GetAllBuildingsUIDsFromName(switchProps[5])
if switchesByName ~= nil and switchesByName[1] ~= nil and switchesByName[1] ~= -1 and #switchesByName > 1 then
if numBrokenSymbols == 0 then
ModBase.SpawnItem('Broken Symbol (SL)', switchXY[1], switchXY[2], false, true, false)
ModUI.ShowPopup('Oops','Each switch must have a unique name! This switch will be disabled until you rename it.')
end
return false
else
if numBrokenSymbols > 0 then
clearTypesInArea('Broken Symbol (SL)', switchXY, switchXY)
end
end
-- Are bots or player on tile?
local numBotsOnTile = ModTiles.GetAmountObjectsOfTypeInArea('Worker', switchXY[1], switchXY[2], switchXY[1], switchXY[2])
if numBotsOnTile > 0 or (switchXY[1] == playerXY[1] and switchXY[2] == playerXY[2]) then
setSwitchState(switchUID, switchProps, true)
else
setSwitchState(switchUID, switchProps, false)
end
end
function setSwitchState(switchUID, switchProps, turnOn)
local xy = {switchProps[2], switchProps[3]}
local onSymbols = ModTiles.GetAmountObjectsOfTypeInArea('Switch On Symbol (SL)', xy[1], xy[2], xy[1], xy[2])
if turnOn then
if DEBUG_ENABLED then ModDebug.Log(' switch @ ' .. xy[1] .. ':' .. xy[2] .. ' is ON.') end
if onSymbols == 0 then ModBase.SpawnItem('Switch On Symbol (SL)', xy[1], xy[2], false, true, false) end
if SWITCHES_TURNED_OFF[switchProps[5]] then SWITCHES_TURNED_OFF[switchProps[5]] = nil end
else
if DEBUG_ENABLED then ModDebug.Log(' switch @ ' .. xy[1] .. ':' .. xy[2] .. ' is OFF.') end
if onSymbols > 0 then clearTypesInArea('Switch On Symbol (SL)', xy, xy) end
if SWITCHES_TURNED_OFF[switchProps[5]] == nil then SWITCHES_TURNED_OFF[switchProps[5]] = true end
end
end
function linkIsSwitchedOff(linkName)
-- Find switches
if linkName == nil then return false end
-- extract switchname
local ms, me = string.find(linkName,'.*sw%[.+%]') -- string.find('sw[48847]','.*sw%[.+%]') = 1, 9
if ms == nil or me == nil then return false end
-- assemble switchname
local switchName = '>' .. string.sub(linkName, ms + 3, me - 1)
if DEBUG_ENABLED then ModDebug.Log(' linkIsSwitchedOff? ', switchName, ':', SWITCHES_TURNED_OFF[switchName] ) end
-- If it does not exist, then we are golden
if SWITCHES_TURNED_OFF[switchName] == nil then return false end
-- It must exist
if DEBUG_ENABLED then ModDebug.Log(' linkIsSwitchedOff: true ' ) end
return true
end
-- Magnets
function discoverUnknownMagnets()
locateMagnets('Crude')
locateMagnets('Good')
locateMagnets('Super')
end
function locateMagnets(levelPrefix)
-- Create EVENT to catch when new ones are added
if ModBase.IsGameVersionGreaterThanEqualTo(VERSION_WITH_CLASSMETHODCHECK_FUNCTION) then
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingTypeSpawnedCallback') then
ModBuilding.RegisterForBuildingTypeSpawnedCallback(levelPrefix .. ' Magnet (SL)', onMagnetSpawn)
end
end
-- Find all Magnets
local magnetUIDs = ModBuilding.GetAllBuildingsUIDsOfType(levelPrefix .. ' Magnet (SL)', 0, 0, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
-- Legacy
if levelPrefix == 'Super' then
local oldMagnetUIDs = ModBuilding.GetAllBuildingsUIDsOfType('Storage Magnet (SL)', 0, 0, WORLD_LIMITS[1]-1, WORLD_LIMITS[2]-1)
if oldMagnetUIDs ~= nil and oldMagnetUIDs[1] ~= -1 and oldMagnetUIDs[1] ~= nil then
for _, uid in ipairs(oldMagnetUIDs) do
magnetUIDs[#magnetUIDs + 1] = uid
end
end
end
-- END Legacy
-- quit if none
if magnetUIDs == nil or magnetUIDs[1] == nil then return end
-- List the magnets if in debug mode
if DEBUG_ENABLED then
for _, uid in ipairs(magnetUIDs) do ModDebug.Log(' locateMagnets: ', uid ) end
end
if reset == nil then reset = false end
-- handle each magnet
for _, uid in ipairs(magnetUIDs)
do
if LINK_UIDS[uid] == nil then
locateStorageForMagnet(uid, levelPrefix)
end
end
end
function onMagnetSpawn(BuildingUID, BuildingType, IsBlueprint, IsDragging)
if DEBUG_ENABLED then ModDebug.Log(' ** onMagnetSpawn: ', BuildingUID, ' ', BuildingType) end
if IsDragging then return end
if IsBlueprint then return end
local levelPrefix = BuildingType:match("(%w+)(.+)")
locateStorageForMagnet(magUID, levelPrefix)
end
function locateStorageForMagnet(magUID, levelPrefix)
local storageUID, dir
local magStorage = {}
-- Cache the Link
local bType, tileX, tileY, rotation, name = unpack(ModObject.GetObjectProperties(magUID)) -- [1]=Type, [2]=TileX, [3]=TileY, [4]=Rotation, [5]=Name
rotation = math.floor(rotation + 0.5) -- round the rotation to a whole number
local dir
if rotation == 270 then dir = 'n' end -- the only place for detecting rotation of magnet.
if rotation == 0 then dir = 'e' end
if rotation == 90 then dir = 's' end
if rotation == 180 then dir = 'w' end
local x, y = tileXYFromDir({tileX, tileY}, dir)
LINK_UIDS[magUID] = {bType=bType, tileX=tileX, tileY=tileY, rotation=rotation, name=name, levelPrefix=levelPrefix, connectToXY={x,y}}
if ModBase.IsGameVersionGreaterThanEqualTo(VERSION_WITH_CLASSMETHODCHECK_FUNCTION) then
-- Make sure we have a callback for when magnet is renamed
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingRenamedCallback') then
ModBuilding.RegisterForBuildingRenamedCallback(magUID, magnetNameUpdated)
end
-- Make sure we have a callback for when a magnet is moved / rotated
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingRepositionedCallback') then
ModBuilding.RegisterForBuildingRepositionedCallback(magUID, magnetRepositioned)
end
-- And if the magnet is destroyed?
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingDestroyedCallback') then
ModBuilding.RegisterForBuildingDestroyedCallback(magUID, linkDestroyed)
end
end
addStorageForMagnet(magUID)
end
function addStorageForMagnet(magUID)
if DEBUG_ENABLED then ModDebug.Log(' locateStorageForMagnet: magnetRotation: ', rotation ) end
local storageUID = storageUidOnTileWithCallbacks(LINK_UIDS[magUID].connectToXY[1], LINK_UIDS[magUID].connectToXY[2])
if storageUID == nil then return false end -- no storage there.
addStorageToMagnet(magUID, storageUID)
end
function addStorageToMagnet(magUID, storageUID)
if DEBUG_ENABLED then ModDebug.Log(' locateStorageForMagnet: magnetRotation: ', rotation ) end
local sProps = ModStorage.GetStorageProperties(storageUID)
-- if sProps[2] == nil then return false end -- Was not actually a storage.
-- Cache the storageUID
local bType, tileX, tileY, rotation, name = unpack(ModObject.GetObjectProperties(storageUID))
rotation = math.floor(rotation + 0.5) -- round the rotation to a whole number
-- Create if needed
if STORAGE_UIDS[storageUID] == nil then STORAGE_UIDS[storageUID] = { linkUIDs = {} } end
-- Set properties
STORAGE_UIDS[storageUID].bType = bType
STORAGE_UIDS[storageUID].tileX = tileX
STORAGE_UIDS[storageUID].tileY = tileY
STORAGE_UIDS[storageUID].rotation = rotation
STORAGE_UIDS[storageUID].name = name
STORAGE_UIDS[storageUID].sType = sProps[1] -- type stored or NIL if no type yet...
STORAGE_UIDS[storageUID].linkUIDs = addToTableIfDoesNotExist(STORAGE_UIDS[storageUID].linkUIDs, magUID) -- each storage might have multiple links of any kind
-- Put storage UID in magnets cache.
LINK_UIDS[magUID].storageUID = storageUID
magnetNameUpdated(magUID, LINK_UIDS[magUID].name) -- this sets up the area
-- Make sure we have a callback for when the storage is moved/rotated/destroyed
if ModBase.IsGameVersionGreaterThanEqualTo(VERSION_WITH_CLASSMETHODCHECK_FUNCTION) then
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingRepositionedCallback') then
ModBuilding.RegisterForBuildingRepositionedCallback(storageUID, storageRepositioned)
end
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForBuildingDestroyedCallback') then
ModBuilding.RegisterForBuildingDestroyedCallback(storageUID, storageDestroyed)
end
if ModBase.ClassAndMethodExist('ModBuilding','RegisterForStorageItemChangedCallback') then
ModBuilding.RegisterForStorageItemChangedCallback(storageUID, storageItemChanged)
end
end
if DEBUG_ENABLED then ModDebug.Log(' locateStorageForMagnet: sProps[1] ', sProps[1], ' of ', storageUID) end
end
function magnetNameUpdated(magUID, objName)
if DEBUG_ENABLED then ModDebug.Log(' magnetNameUpdated: magUID: ', magUID, ' : ', objName ) end
-- Does the magnet exist in the tracker?
if LINK_UIDS[magUID] == nil then return false end
-- Update in tracker
LINK_UIDS[magUID].name = objName -- '40x40'
-- Does storageUID exist for magnet?
if LINK_UIDS[magUID].storageUID == nil then return false end
-- Update area for storage UID!
-- {left = left, top = top, right = right, bottom = bottom}
LINK_UIDS[magUID].area = getAreaForMagnetStorage(LINK_UIDS[magUID], STORAGE_UIDS[LINK_UIDS[magUID].storageUID])
end
function magnetRepositioned(MagnetUID, BuildingType, Rotation, TileX, TileY, IsBlueprint, IsDragging)
if IsDragging then return false end
if IsBlueprint then return false end
if DEBUG_ENABLED then ModDebug.Log(' magnetRepositioned: MagnetUID ', MagnetUID) end
resetCachedLink(MagnetUID)
end
function fireAllMagnets(levelPrefix)
if DEBUG_ENABLED then ModDebug.Log(' fireAllMagnets: levelPrefix: ', levelPrefix) end
-- loop over cached magnets
for uid, props in pairs(LINK_UIDS) do
if props.bType == (levelPrefix .. ' Magnet (SL)') then
if props.storageUID ~= nil then
-- Is there now a storage there?
end
fireMagnetByUID(uid, levelPrefix)
end
end
end
function fireMagnetByUID(magnetUID, levelPrefix)
if DEBUG_ENABLED then ModDebug.Log(' fireMagnetByUID: (a) ', magnetUID) end
-- looking in area for items that can be picked up.
if USE_EVENT_STYLE == false then
updateLinkPropsAsNeeded(magnetUID)
-- Does it still exist?
if LINK_UIDS[magnetUID] == nil then return end
if DEBUG_ENABLED then ModDebug.Log(' fireMagnetByUID: (b) ', magnetUID) end
-- no more storage attached?
if LINK_UIDS[magnetUID].storageUID == nil then addStorageForMagnet(magnetUID) end -- Heavy calcs here...
-- Still no storage? then we quit.
if LINK_UIDS[magnetUID].storageUID == nil then return end
if DEBUG_ENABLED then ModDebug.Log(' fireMagnetByUID: (c) ', magnetUID) end
-- What if the storage itself moved?
updateStoragePropsAsNeeded(LINK_UIDS[magnetUID].storageUID)
if DEBUG_ENABLED then ModDebug.Log(' fireMagnetByUID: (d) ', magnetUID) end
end
-- If there are no storages here, exit!
if LINK_UIDS[magnetUID].storageUID == nil then return end
-- How many can we put in crate?
local maxQty = getQtyToGrabForMagnet(magnetUID)
-- Get them
findAndCollectHoldablesIntoMagneticStorage(magnetUID, maxQty)
if DEBUG_ENABLED then ModDebug.Log(' fireMagnetByUID: (g) ', magnetUID) end
end
function getQtyToGrabForMagnet(magnetUID)
-- LINK_UIDS[magnetUID] = {bType=bType, tileX=tileX, tileY=tileY, rotation=rotation, name=name, levelPrefix=levelPrefix, area={top,left,bottom,right}, storageUID}
-- Figure out how many are already moving through the air, and if that is greater than allowed by the level, then return 0;
local alreadyFlyingForStorage = listInFlightWithProp('storageUID', LINK_UIDS[magnetUID].storageUID, true)
local alreadyFlyingQty
if alreadyFlyingForStorage == nil or alreadyFlyingForStorage[1] == nil then
alreadyFlyingQty = 0
else
alreadyFlyingQty = #alreadyFlyingForStorage
end
-- query storage for min/max
local sProps = ModStorage.GetStorageProperties(LINK_UIDS[magnetUID].storageUID) -- [1]=type-stored, [2] = on-hand, [3] = max-qty, [4] = storage container type
if sProps == nil or sProps == -1 then return 0 end
if sProps[2] == nil then return 0 end
if sProps[3] == nil then return 0 end
-- if crate is full, return 0
if sProps[2] >= sProps[3] then return 0 end
-- Adjust max to be "how many could actually fit into crate"
local maxQtyToCollect = sProps[3] - sProps[2] - alreadyFlyingQty
-- if qty flying will fill up crate, return 0
if maxQtyToCollect <= 0 then return 0 end
-- Adjust based on level prefix
if LINK_UIDS[magnetUID].levelPrefix == 'Crude' then
maxQtyToCollect = 1
elseif LINK_UIDS[magnetUID].levelPrefix == 'Good' then
maxQtyToCollect = 5
end
return maxQtyToCollect
end
function getAreaForMagnetStorage(magProps, storProps)
if DEBUG_ENABLED then ModDebug.Log(' getAreaForMagnetStorage: magnet Name: "', magProps.name, '"' ) end
-- Calculate AREA or SIZE from the name. default to 10x10
local w1, w2 = string.find(magProps.name,'%d+x')
local h1, h2 = string.find(magProps.name,'x%d+')
local width = 10 -- default height
local height = 10 -- default width
if DEBUG_ENABLED then ModDebug.Log(' getAreaForMagnetStorage: w1 w2: ', w1, ' ', w2 ) end
if DEBUG_ENABLED then ModDebug.Log(' getAreaForMagnetStorage: h1 h2: ', h1, ' ', h2 ) end
if w1 ~= nil then width = tonumber(string.sub(magProps.name, w1 , w2 - 1)) end
if h1 ~= nil then height = tonumber(string.sub(magProps.name, h1 + 1, h2 )) end
local left = storProps.tileX - math.floor(width/2)
local top = storProps.tileY - math.floor(height/2)
local right = left + width
local bottom = top + height
-- Limit to map limits!!
if top < 0 then top = 0 end
if left < 0 then left = 0 end
if bottom > WORLD_LIMITS[2] - 1 then bottom = WORLD_LIMITS[2] - 1 end
if right > WORLD_LIMITS[1] - 1 then right = WORLD_LIMITS[1] - 1 end
if DEBUG_ENABLED then ModDebug.Log(' getAreaForMagnetStorage: area: ', left, ':', top, ', to ', right, ':', bottom ) end
return {left = left, top = top, right = right, bottom = bottom}
end
function findAndCollectHoldablesIntoMagneticStorage(magnetUID, maxQty)
if DEBUG_ENABLED then ModDebug.Log(' findAndCollectHoldablesIntoMagneticStorage: (a)', table.show(LINK_UIDS[magnetUID].area) ) end
if LINK_UIDS[magnetUID] == nil then return end
if STORAGE_UIDS[LINK_UIDS[magnetUID].storageUID] == nil then return end
-- Get all items of sType in area
local holdables = ModTiles.GetObjectsOfTypeInAreaUIDs(
STORAGE_UIDS[LINK_UIDS[magnetUID].storageUID].sType,
LINK_UIDS[magnetUID].area.left,
LINK_UIDS[magnetUID].area.top,
LINK_UIDS[magnetUID].area.right,
LINK_UIDS[magnetUID].area.bottom
)
if DEBUG_ENABLED then ModDebug.Log(' findAndCollectHoldablesIntoMagneticStorage: holdables ', holdables ) end
if DEBUG_ENABLED then ModDebug.Log(' findAndCollectHoldablesIntoMagneticStorage: holdables ', table.show(holdables) ) end
if holdables == nil or holdables[1] == -1 or #holdables == 0 then return false end
if DEBUG_ENABLED then ModDebug.Log(' findAndCollectHoldablesIntoMagneticStorage: #holdables ', #holdables ) end
if maxQty == 0 then
if DEBUG_ENABLED then ModDebug.Log(' findAndCollectHoldablesIntoMagneticStorage: 0 max qty??? ') end
return false
end
local s = STORAGE_UIDS[LINK_UIDS[magnetUID].storageUID]
for _, uid in ipairs(holdables)
do
if _ > maxQty then return false end -- already requested max Qty
if OBJECTS_IN_FLIGHT[uid] ~= nil then return false end -- already flying
if uid ~= -1 then -- Is a valid UID
ModObject.StartMoveTo(uid, s.tileX, s.tileY, 15, 10)
OBJECTS_IN_FLIGHT[uid] = { arch=true, wobble=false, storageUID = LINK_UIDS[magnetUID].storageUID, onFlightComplete = onFlightCompleteForMagnets }
--ModObject.SetObjectActive(uid, false)
if DEBUG_ENABLED then ModDebug.Log(' findAndCollectHoldablesIntoMagneticStorage: moving! uid:', uid ) end
end
end
end
function calcQtyToGrabForMagneticStorage(magStorage, levelPrefix)
-- {storageUID = storageUID, magUID = magUID, linkProps = linkProps, storageProps = storageProps}
-- storageProps [1]=type-stored, [2] = on-hand, [3] = max-qty, [4] = storage container type
local alreadyFlyingForStorage = listInFlightWithProp('storageUID', magStorage.storageUID, true)
local alreadyFlyingQty
if alreadyFlyingForStorage == nil or alreadyFlyingForStorage[1] == nil then
alreadyFlyingQty = 0
else
alreadyFlyingQty = #alreadyFlyingForStorage
end
local maxQtyToCollect = magStorage.storageProps[3] - magStorage.storageProps[2] - alreadyFlyingQty
-- If this is "Crude" or "Good" level
if levelPrefix == 'Crude' then
maxQtyToCollect = 1
elseif levelPrefix == 'Good' then
maxQtyToCollect = 5
end
if maxQtyToCollect == 0 then return false end
calcAreaForMagneticStorage(magStorage, maxQtyToCollect, alreadyFlyingForStorage)
end
function calcAreaForMagneticStorage(magStorage, maxQtyToCollect, alreadyFlyingForStorage)
local stXY = ModObject.GetObjectTileCoord(magStorage.storageUID)
if DEBUG_ENABLED then ModDebug.Log(' calcQtyToGrabForMagneticStorage: magnet Name: "', magStorage.magProps[5], '"' ) end
-- Calculate AREA or SIZE from the name. default to 10x10
local w1, w2 = string.find(magStorage.magProps[5],'%d+x')
local h1, h2 = string.find(magStorage.magProps[5],'x%d+')
local width = 10 -- default height
local height = 10 -- default width
if DEBUG_ENABLED then ModDebug.Log(' collectGoodsIntoMagneticStorage: w1 w2: ', w1, ' ', w2 ) end
if DEBUG_ENABLED then ModDebug.Log(' collectGoodsIntoMagneticStorage: h1 h2: ', h1, ' ', h2 ) end
if w1 ~= nil then width = tonumber(string.sub(magStorage.magProps[5], w1 , w2 - 1)) end
if h1 ~= nil then height = tonumber(string.sub(magStorage.magProps[5], h1 + 1, h2 )) end
local left = stXY[1] - math.floor(width/2)
local top = stXY[2] - math.floor(height/2)
local right = left + width
local bottom = top + height
-- Limit to map limits!!
if top < 0 then top = 0 end
if left < 0 then left = 0 end
if bottom > WORLD_LIMITS[2] - 1 then bottom = WORLD_LIMITS[2] - 1 end
if right > WORLD_LIMITS[1] - 1 then right = WORLD_LIMITS[1] - 1 end
if DEBUG_ENABLED then ModDebug.Log(' calcAreaForMagneticStorage: area: ', left, ':', top, ', to ', right, ':', bottom ) end
collectGoodsIntoMagneticStorage(magStorage, maxQtyToCollect, stXY, {left=left, top=top, right=right, bottom=bottom}, alreadyFlyingForStorage)
end
function collectGoodsIntoMagneticStorage(magStorage, maxQtyToCollect, stXY, area, alreadyFlyingForStorage)
if DEBUG_ENABLED then ModDebug.Log(' collectGoodsIntoMagneticStorage: (a)' ) end
-- Clip to max area on map
local pickables = ModTiles.GetObjectsOfTypeInAreaUIDs(magStorage.storageProps[1], area.left, area.top, area.right, area.bottom)
if pickables == nil or pickables[1] == -1 or #pickables == 0 then return false end
if DEBUG_ENABLED then ModDebug.Log(' collectGoodsIntoMagneticStorage: #pickables ', #pickables ) end
for _, uid in ipairs(pickables)
do
if _ > maxQtyToCollect then return false end -- done requestiong.
if hasValue(alreadyFlyingForStorage, uid) == false and uid ~= -1 then -- Not already in flight for area
ModObject.StartMoveTo(uid, stXY[1], stXY[2], 15, 10)
OBJECTS_IN_FLIGHT[uid] = { arch=true, wobble=false, storageUID = magStorage.storageUID, onFlightComplete = onFlightCompleteForMagnets }
if DEBUG_ENABLED then ModDebug.Log(' collectGoodsIntoMagneticStorage: moving! uid:', uid ) end
end
end
end
function onFlightCompleteForMagnets(flyingUID, ob)
-- ob has arrived!
if ModObject.IsValidObjectUID(flyingUID) and ModObject.IsValidObjectUID(ob.storageUID) then -- both UID and storageUID are valid
-- Use 'AddToStorage' only if it has durability.
local maxUsage = ModVariable.GetVariableForObjectAsInt(ModObject.GetObjectType(flyingUID),'MaxUsage')
if maxUsage == nil or maxUsage == 0 then -- No durability, just up storage qty
local sProps = ModStorage.GetStorageProperties(ob.storageUID) -- [2] = current amount, [3] = max
if sProps ~= nil and sProps[1] ~= -1 and sProps[2] ~= nil then
if sProps[2] < 0 then
sProps[2] = 0
ModStorage.SetStorageQuantityStored(ob.storageUID, 0)
end
ModStorage.SetStorageQuantityStored(ob.storageUID, sProps[2] + 1)