-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrol.lua
More file actions
828 lines (750 loc) · 35.6 KB
/
Copy pathcontrol.lua
File metadata and controls
828 lines (750 loc) · 35.6 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
-- ------------------------------
-- External Dependencies
-- ------------------------------
local util = require("__core__/lualib/util")
local math2d = require("__yafla__/scripts/extended_math2d")
-- ------------------------------
-- Internal Dependencies
-- ------------------------------
local technology_functions = require("scripts.technology_functions")
local selector_gui = require("scripts.selector_gui")
local inserter_functions = require("scripts.inserter_functions")
local player_functions = require("scripts.player_functions")
local world_editor = require("scripts.world_selector")
local storage_functions = require("scripts.storage_functions")
local si_util = require("scripts.si_util")
local events = require("scripts.events")
-- ------------------------------
-- Settings
-- ------------------------------
local offset_selector = settings.startup["si-offset-selector"].value
local directional_inserters = settings.startup["si-directional-inserters"].value
local directional_slim_inserters = settings.startup["si-directional-slim-inserters"].value
-- ------------------------------
-- Storage
-- ------------------------------
storage.welcomed = storage.welcomed or false
-- ------------------------------
-- Event Handlers
-- ------------------------------
local function on_init()
storage.SI_Storage = storage.SI_Storage or {}
storage_functions.populate_storage()
technology_functions.migrate_all()
end
local function welcome()
if storage.welcomed then return end
game.print({ "smart-inserters.welcome" })
storage.welcomed = true
--game.print({ "smart-inserters.experimental" })
end
local function on_configuration_changed(event)
storage.SI_Storage = storage.SI_Storage or {}
storage.welcomed = false
storage_functions.populate_storage()
technology_functions.migrate_all()
--game.print({ "smart-inserters.experimental" })
end
local function on_player_created(event)
storage_functions.add_player(game.get_player(event.player_index))
end
local function on_research_changes(event)
-- technology_functions.generate_Tech_lookup_table(event.research.force)
end
local function on_gui_opened(event)
local player = game.get_player(event.player_index)
if player and event.entity and inserter_functions.is_inserter(event.entity) then
if player.gui.relative.smart_inserters then
selector_gui.delete(player)
end
selector_gui.create(player, event.entity)
end
end
local function on_gui_closed(event)
local player = game.get_player(event.player_index)
if player and event.entity and inserter_functions.is_inserter(event.entity) then
selector_gui.delete(player)
end
end
---@param event InserterArmChanged
local function on_inserter_arm_changed(event)
selector_gui.update_all(event.entity, event)
world_editor.update_all(event.entity, event)
end
local function on_player_rotated_entity(event)
local player = game.get_player(event.player_index)
if player and event.entity and inserter_functions.is_inserter(event.entity) then
--Note: it's easier to make factorio calculate the old positions, storing them and setting back the correct direction
local dir = event.entity.direction
event.entity.direction = event.previous_direction
local old_res = inserter_functions.get_arm_positions(event.entity)
event.entity.direction = dir
--local res = inserter_functions.get_arm_positions(event.entity)
--inserter_functions.set_arm_positions(res, event.entity)
selector_gui.update_all(event.entity)
world_editor.update_all_positions(event.entity)
script.raise_event(events.on_inserter_arm_changed, {
entity = event.entity,
old_drop = old_res.drop,
old_pickup = old_res.pickup,
old_drop_offset = old_res.drop_offset,
old_pickup_offset = old_res.pickup_offset,
})
end
end
local function on_entity_settings_pasted(event)
local player = game.get_player(event.player_index)
if player and inserter_functions.is_inserter(event.source) and inserter_functions.is_inserter(event.destination) then
-- local dir = event.entity.direction
-- event.entity.direction = event.previous_direction
-- local old_res = inserter_functions.get_arm_positions(event.entity)
-- event.entity.direction = dir
--local res = inserter_functions.get_arm_positions(event.destination)
--inserter_functions.set_arm_positions(res, event.destination)
--Verificare che gli inserter hanno la stessa base, se no adattare x e y
---- x/y se in base ignora, se fuori base [[ avvicina alla base/riporta allo 0 ]]
--Verificare se le posizioni sono valide per entrambi gli inserter
---- Se non sono valide: [[ resettare/ lasciare le vecchie/ adattarle ]] (una di queste)
script.raise_event(events.on_inserter_arm_changed, {
entity = event.entity,
})
end
end
local function on_pre_entity_settings_pasted(event)
local player = game.get_player(event.player_index)
if player and inserter_functions.is_inserter(event.source) and inserter_functions.is_inserter(event.destination) then
local old_res = inserter_functions.get_arm_positions(event.destination)
--Verificare che gli inserter hanno la stessa base, se no adattare x e y
---- x/y se in base ignora, se fuori base [[ avvicina alla base/riporta allo 0 ]]
--Verificare se le posizioni sono valide per entrambi gli inserter
---- Se non sono valide: [[ resettare/ lasciare le vecchie/ adattarle ]] (una di queste)
script.raise_event(events.on_inserter_arm_changed, {
entity = event.destination,
old_drop = old_res.drop,
old_pickup = old_res.pickup,
old_drop_offset = old_res.drop_offset,
old_pickup_offset = old_res.pickup_offset,
do_not_popolate = true
})
end
end
local function on_rotation_adjust(event)
local player = game.get_player(event.player_index)
assert(player, "[control.lua:on_rotation_adjust] Player is nil")
local inserter = player.selected
if inserter.valid == false then
return
end
if inserter_functions.is_inserter(inserter) then
assert(inserter, "[control.lua:on_rotation_adjust] Inserter is nil")
if technology_functions.get_diagonal_increment(inserter.force) < 1 then
---@diagnostic disable-next-line: missing-fields
player.create_local_flying_text({
text = { "flying-text-smart-inserters.required-technology-missing", "technology-name.si-unlock-diagonal" },
position = inserter.position,
color = { 0.8, 0.8, 0.8 }
})
return
end
local width, height = inserter.tile_width, inserter.tile_height
local lower_width = width % 2 == 0 and width / 2 or width / 2 - 0.5
local higher_width = width % 2 == 0 and width / 2 or width / 2 + 0.5
local lower_height = height % 2 == 0 and height / 2 or height / 2 - 0.5
local higher_height = height % 2 == 0 and height / 2 or height / 2 + 0.5
---@type Position
local move = { x = 0, y = 0 }
local arms = inserter_functions.get_arm_positions(inserter)
local update = string.find(event.input_name, "d", 17, true) == 17 and "drop" or "pickup"
local direction = string.find(event.input_name, "s", #event.input_name - 1, true) == #event.input_name - 1 and -1 or
1
local position = util.copy(arms[update])
local radius
---@type Position
local absolute_position
if arms[update .. "_direction"] == defines.direction.northeast then
position.x = position.x - higher_width + 1
position.y = position.y + lower_height
absolute_position = math2d.position.abs(position)
radius = math.max(absolute_position.x, absolute_position.y)
if absolute_position.x == absolute_position.y then
if direction == 1 then
move.y = 1
else
move.x = -1
end
else
if math.abs(position.x) < radius then
if direction == 1 then
move.x = 1
else
move.x = -1
end
else
if direction == 1 then
move.y = 1
else
move.y = -1
end
end
end
elseif arms[update .. "_direction"] == defines.direction.east then
position.y = 0
position.x = position.x - higher_width + 1
if direction == 1 then
move.y = 1
else
move.y = -1
end
elseif arms[update .. "_direction"] == defines.direction.southeast then
position.x = position.x - higher_width + 1
position.y = position.y - higher_height + 1
absolute_position = math2d.position.abs(position)
radius = math.max(absolute_position.x, absolute_position.y)
if absolute_position.x == absolute_position.y then
if direction == 1 then
move.x = -1
else
move.y = -1
end
else
if math.abs(position.x) < radius then
if direction == 1 then
move.x = -1
else
move.x = 1
end
else
if direction == 1 then
move.y = 1
else
move.y = -1
end
end
end
elseif arms[update .. "_direction"] == defines.direction.south then
position.x = 0
position.y = position.y - higher_height + 1
if direction == 1 then
move.x = -1
else
move.x = 1
end
elseif arms[update .. "_direction"] == defines.direction.southwest then
position.x = position.x + lower_width
position.y = position.y - higher_height + 1
absolute_position = math2d.position.abs(position)
radius = math.max(absolute_position.x, absolute_position.y)
if absolute_position.x == absolute_position.y then
if direction == 1 then
move.y = -1
else
move.x = 1
end
else
if math.abs(position.x) < radius then
if direction == 1 then
move.x = -1
else
move.x = 1
end
else
if direction == 1 then
move.y = -1
else
move.y = 1
end
end
end
elseif arms[update .. "_direction"] == defines.direction.west then
position.y = 0
position.x = position.x + lower_width
if direction == 1 then
move.y = -1
else
move.y = 1
end
elseif arms[update .. "_direction"] == defines.direction.northwest then
position.x = position.x + lower_width
position.y = position.y + lower_height
absolute_position = math2d.position.abs(position)
radius = math.max(absolute_position.x, absolute_position.y)
if absolute_position.x == absolute_position.y then
if direction == 1 then
move.x = 1
else
move.y = 1
end
else
if math.abs(position.x) < radius then
if direction == 1 then
move.x = 1
else
move.x = -1
end
else
if direction == 1 then
move.y = -1
else
move.y = 1
end
end
end
elseif arms[update .. "_direction"] == defines.direction.north then
position.x = 0
position.y = position.y + lower_height
if direction == 1 then
move.x = 1
else
move.x = -1
end
end
absolute_position = absolute_position or math2d.position.abs(position)
radius = radius or math.max(absolute_position.x, absolute_position.y)
local count = 0
local shift = { x = move.x, y = move.y }
while not inserter_functions.should_cell_be_enabled(inserter, { x = arms[update].x + shift.x, y = arms[update].y + shift.y }) do
shift.x = shift.x + move.x
shift.y = shift.y + move.y
count = count + 1
if radius < math.max(math.abs(position.x + shift.x), math.abs(position.y + shift.y)) then
shift.x = shift.x - move.x
shift.y = shift.y - move.y
move = { x = 0, y = 0 }
if position.x + shift.x > 0 and position.y + shift.y < 0 then --northeast
if math.abs(position.x + shift.x) < radius then
if direction == 1 then
move.x = 1
else
move.x = -1
end
elseif math.abs(position.y + shift.y) < radius then
if direction == 1 then
move.y = 1
else
move.y = -1
end
else
if direction == 1 then
move.y = 1
else
move.x = -1
end
end
elseif position.x + shift.x > 0 and position.y + shift.y > 0 then --southeast
if math.abs(position.x + shift.x) < radius then
if direction == 1 then
move.x = -1
else
move.x = 1
end
elseif math.abs(position.y + shift.y) < radius then
if direction == 1 then
move.y = 1
else
move.y = -1
end
else
if direction == 1 then
move.x = -1
else
move.y = -1
end
end
elseif position.x + shift.x < 0 and position.y + shift.y > 0 then --southwest
if math.abs(position.x + shift.x) < radius then
if direction == 1 then
move.x = -1
else
move.x = 1
end
elseif math.abs(position.y + shift.y) < radius then
if direction == 1 then
move.y = -1
else
move.y = 1
end
else
if direction == 1 then
move.y = -1
else
move.x = 1
end
end
elseif position.x + shift.x < 0 and position.y + shift.y < 0 then --northwest
if math.abs(position.x + shift.x) < radius then
if direction == 1 then
move.x = -1
else
move.x = 1
end
elseif math.abs(position.y + shift.y) < radius then
if direction == 1 then
move.y = 1
else
move.y = -1
end
else
if direction == 1 then
move.x = 1
else
move.y = 1
end
end
end
end
if inserter.tile_height == 0 and position.y + shift.y == 0 then
if position.y > 0 and shift.y < 0 then
position.y = position.y - 1
elseif position.y < 0 and shift.y > 0 then
position.y = position.y + 1
end
elseif inserter.tile_width == 0 and position.x + shift.x == 0 then
if position.x > 0 and shift.x < 0 then
position.x = position.x - 1
elseif position.x < 0 and shift.x > 0 then
position.x = position.x + 1
end
end
if count > 100 then
game.print(
"Something went wrong and the rotation function exceeded 100 iterations, please report this to the mod author")
break
end
end
arms[update].x = arms[update].x + shift.x
arms[update].y = arms[update].y + shift.y
inserter_functions.set_arm_positions(arms, inserter)
end
end
local function on_distance_adjust(event)
local player = game.get_player(event.player_index)
assert(player, "[control.lua:on_distance_adjust] Player is nil")
local inserter = player.selected
if inserter.valid == false then
return
end
if inserter_functions.is_inserter(inserter) then
assert(inserter, "[control.lua:on_offset_adjust] Inserter is nil")
---@diagnostic disable-next-line: param-type-mismatch
if (technology_functions.get_actual_increment(inserter.force) < 1) or (technology_functions.get_diagonal_increment(inserter.force) < 1) then
player.create_local_flying_text({
position = inserter.position,
text = { "flying-text-smart-inserters.required-technology-missing", "technology-name.si-unlock-range-1 / technology-name.si-unlock-cross" },
color = { 0.8, 0.8, 0.8 }
})
return
end
local width, height = inserter.tile_width, inserter.tile_height
local lower_width = width % 2 == 0 and width / 2 or width / 2 - 0.5
local higher_width = width % 2 == 0 and width / 2 or width / 2 + 0.5
local lower_height = height % 2 == 0 and height / 2 or height / 2 - 0.5
local higher_height = height % 2 == 0 and height / 2 or height / 2 + 0.5
local arms = inserter_functions.get_arm_positions(inserter)
local update = string.find(event.input_name, "d", 17, true) == 17 and "drop" or "pickup"
local direction = string.find(event.input_name, "e", #event.input_name, true) == #event.input_name and -1 or 1
local position = util.copy(arms[update])
-- I need max range to avoid the position to be outside the range
local x_changes, y_changes = false, false
local max_range, min_range = inserter_functions.get_max_and_min_inserter_range(inserter)
max_range, min_range = math.max(1, max_range), math.max(1, min_range)
local extreme_positions = {
min = { x = 0, y = 0 },
max = { x = 0, y = 0 }
}
-- I could add the min here and remove the double check max after this code, but for now it works
-- -1 is because the higher height is outside the base
if arms[update .. "_direction"] == defines.direction.north then
position.y = position.y - direction
extreme_positions.max.y = -max_range - lower_height
extreme_positions.min.y = -min_range - lower_height
y_changes = true
elseif arms[update .. "_direction"] == defines.direction.northeast then
position.x = position.x + direction
position.y = position.y - direction
extreme_positions.max.x = max_range + higher_width - 1
extreme_positions.min.x = min_range + higher_width - 1
extreme_positions.max.y = -max_range - lower_height
extreme_positions.min.y = -min_range - lower_height
x_changes = true
y_changes = true
elseif arms[update .. "_direction"] == defines.direction.east then
position.x = position.x + direction
extreme_positions.max.x = max_range + higher_width - 1
extreme_positions.min.x = min_range + higher_width - 1
x_changes = true
elseif arms[update .. "_direction"] == defines.direction.southeast then
position.x = position.x + direction
position.y = position.y + direction
extreme_positions.max.x = max_range + higher_width - 1
extreme_positions.min.x = min_range + higher_width - 1
extreme_positions.max.y = max_range + higher_height - 1
extreme_positions.min.y = min_range + higher_height - 1
x_changes = true
y_changes = true
elseif arms[update .. "_direction"] == defines.direction.south then
position.y = position.y + direction
extreme_positions.max.y = max_range + higher_height - 1
extreme_positions.min.y = min_range + higher_height - 1
y_changes = true
elseif arms[update .. "_direction"] == defines.direction.southwest then
position.x = position.x - direction
position.y = position.y + direction
extreme_positions.min.x = -min_range - lower_width
extreme_positions.max.x = -max_range - lower_width
extreme_positions.max.y = max_range + higher_height - 1
extreme_positions.min.y = min_range + higher_height - 1
x_changes = true
y_changes = true
elseif arms[update .. "_direction"] == defines.direction.west then
position.x = position.x - direction
extreme_positions.max.x = -max_range - lower_width
extreme_positions.min.x = -min_range - lower_width
x_changes = true
elseif arms[update .. "_direction"] == defines.direction.northwest then
position.x = position.x - direction
position.y = position.y - direction
extreme_positions.max.x = -max_range - lower_width
extreme_positions.min.x = -min_range - lower_width
extreme_positions.max.y = -max_range - lower_height
extreme_positions.min.y = -min_range - lower_height
x_changes = true
y_changes = true
end
if x_changes then
if position.x < -max_range - lower_width or position.x > max_range + higher_width - 1 then
position.x = extreme_positions.min.x
elseif position.x > -min_range - lower_width and position.x < min_range + higher_width - 1 then
position.x = extreme_positions.max.x
elseif arms[update .. "_direction"] ~= inserter_functions.calculate_arm_direction(inserter, position) then
position.x = extreme_positions.max.x
end
end
if y_changes then
if position.y < -max_range - lower_height or position.y > max_range + higher_height - 1 then
position.y = extreme_positions.min.y
elseif position.y > -min_range - lower_height and position.y < min_range + higher_height - 1 then
position.y = extreme_positions.max.y
elseif arms[update .. "_direction"] ~= inserter_functions.calculate_arm_direction(inserter, position) then
position.y = extreme_positions.max.y
end
end
arms[update] = position
if inserter_functions.should_cell_be_enabled(inserter, arms[update]) then
inserter_functions.set_arm_positions(arms, inserter)
end
end
end
local function on_offset_adjust(event)
local player = game.get_player(event.player_index)
assert(player, "[control.lua:on_offset_adjust] Player is nil")
local inserter = player.selected
if inserter.valid == false then
return
end
if inserter_functions.is_inserter(inserter) then
assert(inserter, "[control.lua:on_offset_adjust] Inserter is nil")
---@diagnostic disable-next-line: param-type-mismatch
if not offset_selector then
player.create_local_flying_text({
position = inserter.position,
text = { "flying-text-smart-inserters.offset-selector-is-disabled" },
color = { 0.8, 0.8, 0.8 }
})
return
elseif not technology_functions.check_offset_tech(inserter.force) then
player.create_local_flying_text({
position = inserter.position,
text = { "flying-text-smart-inserters.required-technology-missing", "technology-name.si-unlock-offsets" },
color = { 0.8, 0.8, 0.8 }
})
return
end
assert(inserter, "[control.lua:on_offset_adjust] Inserter is nil")
local arms = inserter_functions.get_arm_positions(inserter)
local update = string.find(event.input_name, "d", 17, true) == 17 and "drop" or "pickup"
local position = util.copy(arms[update .. "_offset"])
if arms[update .. "_direction"] == defines.direction.north then
position.y = position.y > 0.5 and 0.25 or 0.75
position.x = 0.5
elseif arms[update .. "_direction"] == defines.direction.northeast then
local x = position.x > 0.5 and 0.25 or 0.75
local y = x == 0.75 and 0.25 or 0.75
position.x = x
position.y = y
elseif arms[update .. "_direction"] == defines.direction.east then
position.y = 0.5
position.x = position.x > 0.5 and 0.25 or 0.75
elseif arms[update .. "_direction"] == defines.direction.southeast then
local new = position.x > 0.5 and 0.25 or 0.75
position.x = new
position.y = new
elseif arms[update .. "_direction"] == defines.direction.south then
position.y = position.y > 0.5 and 0.25 or 0.75
position.x = 0.5
elseif arms[update .. "_direction"] == defines.direction.southwest then
local x = position.x > 0.5 and 0.25 or 0.75
local y = x == 0.75 and 0.25 or 0.75
position.x = x
position.y = y
elseif arms[update .. "_direction"] == defines.direction.west then
position.y = 0.5
position.x = position.x > 0.5 and 0.25 or 0.75
elseif arms[update .. "_direction"] == defines.direction.northwest then
local new = position.x > 0.5 and 0.25 or 0.75
position.x = new
position.y = new
end
old = arms[update .. "_offset"]
arms[update .. "_offset"] = position
inserter_functions.set_arm_positions(arms, inserter)
end
end
-- To check if the player placed an in world selector blueprint
local function on_built_entity(event)
local entity = event.created_entity and event.created_entity or event.entity
if entity.name ~= "entity-ghost" or (entity.ghost_name ~= "si-in-world-drop-entity" and entity.ghost_name ~= "si-in-world-pickup-entity") then
return
end
storage_functions.ensure_data(event.player_index)
local player = game.get_player(event.player_index)
if player == nil then return end
local inserter = storage.SI_Storage[event.player_index].selected_inserter.inserter
if inserter.valid == false then
return
end
local slim = inserter_functions.is_slim(inserter)
local update = string.find(entity.ghost_name, "d", 13) == 13 and "drop" or "pickup"
local arm_positions = inserter_functions.get_arm_positions(inserter)
arm_positions[update] = math2d.position.subtract(entity.position, inserter.position)
arm_positions[update].x = arm_positions[update].x - (inserter.tile_width ~= 1 and 0.5 or 0)
arm_positions[update].y = arm_positions[update].y - (inserter.tile_height ~= 1 and 0.5 or 0)
local check_directional_slim = true
if (directional_inserters and not slim) or (directional_slim_inserters and slim) then
if inserter.direction == defines.direction.north then
if arm_positions[update].y < 0 then
check_directional_slim = ("pickup" == update)
else
check_directional_slim = ("drop" == update)
end
elseif inserter.direction == defines.direction.south then
if arm_positions[update].y < 0 then
check_directional_slim = ("drop" == update)
else
check_directional_slim = ("pickup" == update)
end
elseif inserter.direction == defines.direction.east then
if arm_positions[update].x < 0 then
check_directional_slim = ("drop" == update)
else
check_directional_slim = ("pickup" == update)
end
elseif inserter.direction == defines.direction.west then
if arm_positions[update].x < 0 then
check_directional_slim = ("pickup" == update)
else
check_directional_slim = ("drop" == update)
end
end
end
if inserter_functions.should_cell_be_enabled(inserter, arm_positions[update]) and check_directional_slim then
inserter_functions.set_arm_positions(arm_positions, inserter)
else
player.create_local_flying_text({
position = inserter.position,
text = { "flying-text-smart-inserters.invalid-position" },
color = { 0.8, 0.8, 0.8 }
})
end
entity.destroy()
end
-- To check if the player destroyed an inserter to clear the in world selector
local function on_entity_destroyed(event)
if not inserter_functions.is_inserter(event.entity) then
return
end
for player_index, player in pairs(game.players) do
storage_functions.ensure_data(player_index)
if storage.SI_Storage[player_index].is_selected == true and math2d.position.equal(storage.SI_Storage[player_index].selected_inserter.position, event.entity.position) then
--world_editor.clear_positions(player_index)
player_functions.safely_change_cursor(player)
end
end
end
local function on_in_world_editor(event)
local player = game.get_player(event.player_index)
local update = string.find(event.input_name, "d", 48) == 48 and "drop" or "pickup"
storage_functions.ensure_data(event.player_index)
if player == nil then return end
if player.cursor_stack.is_blueprint then
local changing = string.find(player.cursor_stack.name, "d", 13) == 13 and "drop" or "pickup"
if changing == update then
player_functions.safely_change_cursor(player)
world_editor.clear_positions(event.player_index)
return
else
player_functions.safely_change_cursor(player, "si-in-world-" .. update .. "-changer")
player_functions.configure_pickup_drop_changer(player, update)
return
end
end
if player.selected and inserter_functions.is_inserter(player.selected) and player.selected.position and si_util.check_blacklist(player.selected) then
world_editor.draw_positions(event.player_index, player.selected)
player_functions.safely_change_cursor(player, "si-in-world-" .. update .. "-changer")
player_functions.configure_pickup_drop_changer(player, update)
end
end
-- To check if the player destroyes out blueprint
local function on_player_cursor_stack_changed(event)
local player = game.get_player(event.player_index)
if player ~= nil and player.cursor_stack.is_blueprint then
if player.cursor_stack.name == "si-in-world-drop-changer" or player.cursor_stack.name == "si-in-world-pickup-changer" then
return
end
else
world_editor.clear_positions(event.player_index)
end
end
-- ------------------------------
-- Event Handlers registration
-- ------------------------------
-- Player and Init events
script.on_init(on_init) --[DONE]
script.on_configuration_changed(on_configuration_changed) --[DONE]
script.on_event(defines.events.on_player_created, on_player_created) --[DONE]
script.on_event(defines.events.on_cutscene_cancelled, welcome) --[DONE]
script.on_event(defines.events.on_cutscene_finished, welcome) --[DONE]
-- Gui events
script.on_event(defines.events.on_gui_opened, on_gui_opened) --[DONE]
script.on_event(defines.events.on_gui_closed, on_gui_closed) --[DONE]
script.on_event(events.on_inserter_arm_changed, on_inserter_arm_changed) --[DONE]
script.on_event(defines.events.on_player_rotated_entity, on_player_rotated_entity) --[PROBABLY INCOMPLETE]
script.on_event(defines.events.on_entity_settings_pasted, on_entity_settings_pasted) --[PROBABLY INCOMPLETE]
script.on_event(defines.events.on_pre_entity_settings_pasted, on_pre_entity_settings_pasted) --[PROBABLY INCOMPLETE]
-- Shortcut events
script.on_event("smart-inserters-drop-rotate", on_rotation_adjust) --[DONE] [UNTESTED: SLIM DIRECTIONAL/SINGLE LINE INSERTER]
script.on_event("smart-inserters-drop-rotate-reverse", on_rotation_adjust) --[DONE] [UNTESTED: SLIM DIRECTIONAL/SINGLE LINE INSERTER]
script.on_event("smart-inserters-drop-distance-adjust", on_distance_adjust) --[DONE]
script.on_event("smart-inserters-drop-distance-adjust-reverse", on_distance_adjust) --[DONE]
script.on_event("smart-inserters-drop-offset-adjust", on_offset_adjust) --[DONE]
script.on_event("smart-inserters-pickup-rotate", on_rotation_adjust) --[UNTESTED] [UNTESTED: SLIM DIRECTIONAL/SINGLE LINE INSERTER]
script.on_event("smart-inserters-pickup-rotate-reverse", on_rotation_adjust) --[UNTESTED] [UNTESTED: SLIM DIRECTIONAL/SINGLE LINE INSERTER]
script.on_event("smart-inserters-pickup-distance-adjust", on_distance_adjust) --[UNTESTED]
script.on_event("smart-inserters-pickup-distance-adjust-reverse", on_distance_adjust) --[UNTESTED]
script.on_event("smart-inserters-pickup-offset-adjust", on_offset_adjust) --[UNTESTED]
-- World editor events
script.on_event(defines.events.on_built_entity, on_built_entity) --[DONE]
script.on_event(defines.events.script_raised_built, on_built_entity) --[DONE]
script.on_event(defines.events.script_raised_destroy, on_entity_destroyed) --[DONE]
script.on_event(defines.events.on_player_mined_entity, on_entity_destroyed) --[DONE]
script.on_event(defines.events.on_robot_mined_entity, on_entity_destroyed) --[DONE]
--script.on_event(defines.events.on_entity_died, on_entity_destroyed)
--script.on_event(defines.events.on_entity_destroyed, on_entity_destroyed)
script.on_event(defines.events.on_player_cursor_stack_changed, on_player_cursor_stack_changed) --[DONE]
script.on_event("smart-inserters-in-world-inserter-configurator-pickup", on_in_world_editor) --[DONE]
script.on_event("smart-inserters-in-world-inserter-configurator-drop", on_in_world_editor) --[DONE]
-- Optimization
script.on_event(defines.events.on_research_finished, on_research_changes)
script.on_event(defines.events.on_research_reversed, on_research_changes)
-- TODO: optimize should cell be enabled
-- compatibility with renai trasportation
-- compatibility with ghost entity