This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
forked from Klonan/upgrade-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
1310 lines (1229 loc) · 42.3 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require("mod-gui")
MAX_CONFIG_SIZE = 16
MAX_STORAGE_SIZE = 12
in_range_check_is_annoying = true
function global_init()
global.config = {}
global["config-tmp"] = {}
global.storage = {}
global.storage_index = {}
end
function get_type(entity)
if game.item_prototypes[entity] then
return game.item_prototypes[entity].type
end
return ""
end
function count_keys(hashmap)
local result = 0
for _, __ in pairs(hashmap) do
result = result + 1
end
return result
end
function get_config_item(player, index, type)
if not global["config-tmp"][player.name]
or index > #global["config-tmp"][player.name]
or global["config-tmp"][player.name][index][type] == "" then
return nil
end
if not game.item_prototypes[global["config-tmp"][player.name][index][type]] then
return nil
end
if not game.item_prototypes[global["config-tmp"][player.name][index][type]].valid then
return nil
end
return game.item_prototypes[global["config-tmp"][player.name][index][type]].name
end
function gui_init(player)
local flow = mod_gui.get_button_flow(player)
if not flow.upgrade_planner_config_button then
local button = flow.add
{
type = "sprite-button",
name = "upgrade_planner_config_button",
style = mod_gui.button_style,
sprite = "item/upgrade-builder",
tooltip = {"upgrade-planner.button-tooltip"}
}
button.style.visible = true
end
end
function gui_open_frame(player)
local flow = player.gui.center
local frame = flow.upgrade_planner_config_frame
if frame then
frame.destroy()
global["config-tmp"][player.name] = nil
return
end
global.config[player.name] = global.config[player.name] or {}
global["config-tmp"][player.name] = {}
local i = 0
for i = 1, MAX_CONFIG_SIZE do
if i > #global.config[player.name] then
global["config-tmp"][player.name][i] = { from = "", to = "" }
else
global["config-tmp"][player.name][i] = {
from = global.config[player.name][i].from,
to = global.config[player.name][i].to
}
end
end
-- Now we can build the GUI.
frame = flow.add{
type = "frame",
caption = {"upgrade-planner.config-frame-title"},
name = "upgrade_planner_config_frame",
direction = "vertical"
}
if not global.storage_index then global.storage_index = {} end
if not global.storage_index[player.name] then
global.storage_index[player.name] = 1
end
local storage_flow = frame.add{type = "table", name = "upgrade_planner_storage_flow", column_count = 3}
--storage_flow.style.horizontal_spacing = 2
local drop_down = storage_flow.add{type = "drop-down", name = "upgrade_planner_drop_down"}
--drop_down.style.minimal_height = 50
drop_down.style.minimal_width = 164
drop_down.style.maximal_width = 0
if not global.storage then
global.storage = {}
end
if not global.storage[player.name] then
global.storage[player.name] = {}
end
for key, _ in pairs(global.storage[player.name]) do
drop_down.add_item(key)
end
if not global.storage[player.name]["New storage"] then
drop_down.add_item("New storage")
end
local items = drop_down.items
local index = math.min(global.storage_index[player.name], #items)
index = math.max(index, 1)
drop_down.selected_index = index
global.storage_index[player.name] = index
local storage_to_restore = drop_down.get_item(drop_down.selected_index)
local rename_button = storage_flow.add{type="sprite-button", name = "upgrade_planner_storage_rename", sprite = "utility/rename_icon_normal", tooltip = {"upgrade-planner.rename-button-tooltip"}}
rename_button.style = "slot_button"
rename_button.style.maximal_width = 24
rename_button.style.minimal_width = 24
rename_button.style.maximal_height = 24
rename_button.style.minimal_height = 24
local remove_button = storage_flow.add{type="sprite-button", name = "upgrade_planner_storage_delete", sprite = "utility/remove", tooltip = {"upgrade-planner.delete-storage-button-tooltip"} }
remove_button.style = "red_slot_button"
remove_button.style.maximal_width = 24
remove_button.style.minimal_width = 24
remove_button.style.maximal_height = 24
remove_button.style.minimal_height = 24
local rename_field = storage_flow.add{type = "textfield", name = "upgrade_planner_storage_rename_textfield", text = drop_down.get_item(drop_down.selected_index)}
rename_field.style.visible = false
local confirm_button = storage_flow.add{type="sprite-button", name = "upgrade_planner_storage_confirm", sprite = "utility/confirm_slot", tooltip = {"upgrade-planner.confirm-storage-name"} }
confirm_button.style = "green_slot_button"
confirm_button.style.maximal_width = 24
confirm_button.style.minimal_width = 24
confirm_button.style.maximal_height = 24
confirm_button.style.minimal_height = 24
confirm_button.style.visible = false
local cancel_button = storage_flow.add{type="sprite-button", name = "upgrade_planner_storage_cancel", sprite = "utility/set_bar_slot", tooltip = {"upgrade-planner.cancel-storage-name"} }
cancel_button.style = "red_slot_button"
cancel_button.style.maximal_width = 24
cancel_button.style.minimal_width = 24
cancel_button.style.maximal_height = 24
cancel_button.style.minimal_height = 24
cancel_button.style.visible = false
local ruleset_grid = frame.add{
type = "table",
column_count = 6,
name = "upgrade_planner_ruleset_grid",
style = "slot_table"
}
ruleset_grid.add{
type = "label",
caption = {"upgrade-planner.config-header-1"}
}
ruleset_grid.add{
type = "label",
caption = {"upgrade-planner.config-header-2"}
}
ruleset_grid.add{
type = "label",
caption = {"upgrade-planner.config-clear", " "}
}
ruleset_grid.add{
type = "label",
caption = {"upgrade-planner.config-header-1"}
}
ruleset_grid.add{
type = "label",
caption = {"upgrade-planner.config-header-2"}
}
ruleset_grid.add{
type = "label",
caption = {"upgrade-planner.config-clear", ""}
}
local items = game.item_prototypes
for i = 1, MAX_CONFIG_SIZE do
local sprite = nil
local tooltip = nil
local from = get_config_item(player, i, "from")
if from then
--sprite = "item/"..get_config_item(player, i, "from")
tooltip = items[from].localised_name
end
local elem = ruleset_grid.add{
type = "choose-elem-button",
name = "upgrade_planner_from_" .. i,
style = "slot_button",
--sprite = sprite,
elem_type = "item",
tooltip = tooltip
}
elem.elem_value = from
local sprite = nil
local tooltip = nil
local to = get_config_item(player, i, "to")
if to then
--sprite = "item/"..get_config_item(player, i, "to")
tooltip = items[to].localised_name
end
local elem = ruleset_grid.add{
type = "choose-elem-button",
name = "upgrade_planner_to_" .. i,
--style = "slot_button",
--sprite = sprite,
elem_type = "item",
tooltip = tooltip
}
elem.elem_value = to
ruleset_grid.add{
type = "sprite-button",
name = "upgrade_planner_clear_" .. i,
style = "red_slot_button",
sprite = "utility/remove",
tooltip = {"upgrade-planner.config-clear", ""}
}
end
local button_grid = frame.add{
type = "table",
column_count = 5
}
button_grid.add{
type = "sprite-button",
name = "upgrade_blueprint",
sprite = "item/blueprint",
tooltip = {"upgrade-planner.config-button-upgrade-blueprint"},
style = mod_gui.button_style
}
button_grid.add{
type = "sprite-button",
name = "give_upgrade_tool",
sprite = "item/upgrade-builder",
tooltip = {"upgrade-planner.config-button-give-upgrade-tool"},
style = mod_gui.button_style
}
button_grid.add{
type = "sprite-button",
name = "remove_upgrade_tool",
sprite = "utility/remove",
tooltip = {"upgrade-planner.config-button-remove-upgrade-tool"},
style = mod_gui.button_style
}
button_grid.add{
type = "sprite-button",
name = "upgrade_planner_import_config",
sprite = "utility/import_slot",
tooltip = {"upgrade-planner.config-button-import-config"},
style = mod_gui.button_style
}
button_grid.add{
type = "sprite-button",
name = "upgrade_planner_export_config",
sprite = "utility/export_slot",
tooltip = {"upgrade-planner.config-button-export-config"},
style = mod_gui.button_style
}
gui_restore(player, storage_to_restore)
player.opened = frame
end
function gui_save_changes(player)
-- Saving changes consists in:
-- 1. copying config-tmp to config
-- 2. removing config-tmp
if global["config-tmp"][player.name] then
local i = 0
global.config[player.name] = {}
for i = 1, #global["config-tmp"][player.name] do
global.config[player.name][i] = {
from = global["config-tmp"][player.name][i].from,
to = global["config-tmp"][player.name][i].to
}
end
end
if not global.storage then
global.storage = {}
end
if not global.storage[player.name] then
global.storage[player.name] = {}
end
local gui = player.gui.center.upgrade_planner_config_frame
if not gui then return end
local drop_down = gui.upgrade_planner_storage_flow.children[1]
local name = drop_down.get_item(global.storage_index[player.name])
global.storage[player.name][name] = global.config[player.name]
end
function gui_set_rule(player, type, index, element)
local items = game.item_prototypes
local name = element.elem_value
local frame = player.gui.center.upgrade_planner_config_frame
local ruleset_grid = frame["upgrade_planner_ruleset_grid"]
local storage_name = element.parent.parent.upgrade_planner_storage_flow.children[1].get_item(global.storage_index[player.name])
local storage = global["config-tmp"][player.name]
if not frame or not storage then return end
if not name then
ruleset_grid["upgrade_planner_" .. type .. "_" .. index].tooltip = ""
storage[index][type] = ""
gui_save_changes(player)
return
end
local opposite = "from"
local i = 0
if type == "from" then
opposite = "to"
for i = 1, #storage do
if index ~= i and storage[i].from == name then
player.print({"upgrade-planner.item-already-set"})
gui_restore(player, storage_name)
return
end
end
end
local related = storage[index][opposite]
if related ~= "" then
if related == name then
player.print({"upgrade-planner.item-is-same"})
gui_restore(player, storage_name)
return
end
if get_type(name) ~= get_type(related) and (not is_exception(get_type(name), get_type(related))) then
player.print({"upgrade-planner.item-not-same-type"})
if storage[index][type] == "" then
element.elem_value = nil
elseif items[storage[index][type]] then
element.elem_value = storage[index][type]
else
element.elem_value = nil
end
return
end
end
storage[index][type] = name
ruleset_grid["upgrade_planner_" .. type .. "_" .. index].tooltip = items[name].localised_name
gui_save_changes(player)
end
function gui_clear_rule(player, index)
local frame = player.gui.center.upgrade_planner_config_frame
if not frame or not global["config-tmp"][player.name] then return end
local ruleset_grid = frame["upgrade_planner_ruleset_grid"]
global["config-tmp"][player.name][index] = { from = "", to = "" }
ruleset_grid["upgrade_planner_from_" .. index].elem_value = nil
ruleset_grid["upgrade_planner_from_" .. index].tooltip = ""
ruleset_grid["upgrade_planner_to_" .. index].elem_value = nil
ruleset_grid["upgrade_planner_to_" .. index].tooltip = ""
gui_save_changes(player)
end
function gui_restore(player, name)
local frame = player.gui.center.upgrade_planner_config_frame
if not frame then return end
if not global.storage[player.name] then return end
local storage = global.storage[player.name][name]
if not storage and name == "New storage" then
storage = {}
end
if not storage then return end
global["config-tmp"][player.name] = {}
local items = game.item_prototypes
local i = 0
local ruleset_grid = frame["upgrade_planner_ruleset_grid"]
local items = game.item_prototypes
for i = 1, MAX_CONFIG_SIZE do
if i > #storage then
global["config-tmp"][player.name][i] = { from = "", to = "" }
else
global["config-tmp"][player.name][i] = {
from = storage[i].from,
to = storage[i].to
}
end
local name = get_config_item(player, i, "from")
ruleset_grid["upgrade_planner_from_" .. i].elem_value = name
if name and name ~= "" then
ruleset_grid["upgrade_planner_from_" .. i].tooltip = items[name].localised_name
else
ruleset_grid["upgrade_planner_from_" .. i].tooltip = ""
end
local name = get_config_item(player, i, "to")
ruleset_grid["upgrade_planner_to_" .. i].elem_value = name
if name and name ~= "" then
ruleset_grid["upgrade_planner_to_" .. i].tooltip = items[name].localised_name
else
ruleset_grid["upgrade_planner_to_" .. i].tooltip = ""
end
end
global.config[player.name] = global["config-tmp"][player.name]
end
script.on_event(defines.events.on_gui_click, function(event)
local element = event.element
--print_full_gui_name(element)
local name = element.name
local player = game.players[event.player_index]
--game.print(element.type)
--game.print(element.name)
if name == "upgrade_blueprint" then
upgrade_blueprint(player)
return
end
if name == "give_upgrade_tool" then
player.clean_cursor()
player.cursor_stack.set_stack({name = "upgrade-builder"})
return
end
if name == "remove_upgrade_tool" then
player.clean_cursor()
-- Remove stacks of item until there is none left
while player.get_item_count("upgrade-builder") > 0 do
player.remove_item("upgrade-builder")
end
return
end
if name == "upgrade_planner_storage_rename" then
local children = element.parent.children
for k, child in pairs (children) do
child.style.visible = true
end
children[4].text = children[1].get_item(children[1].selected_index)
if children[4].text == "New storage" then
children[4].text = ""
end
return
end
if name == "upgrade_planner_storage_cancel" then
local children = element.parent.children
for k = 4, 6 do
children[k].style.visible = false
end
children[4].text = children[1].get_item(children[1].selected_index)
return
end
if name == "upgrade_planner_storage_confirm" then
local index = global.storage_index[player.name]
local children = element.parent.children
local new_name = children[4].text
local length = string.len(new_name)
if length < 1 then
player.print({"upgrade-planner.storage-name-too-short"})
return
end
for k = 4, 6 do
children[k].style.visible = false
end
local items = children[1].items
if index > #items then
index = #items
end
local old_name = items[index]
if old_name == "New storage" then
children[1].add_item("New storage")
end
if not global.storage then
global.storage = {}
end
if not global.storage[player.name] then
global.storage[player.name] = {}
end
if global.storage[player.name][old_name] then
global.storage[player.name][new_name] = global.storage[player.name][old_name]
else
global.storage[player.name][new_name] = {}
end
global.storage[player.name][old_name] = nil
--game.print(serpent.block(global.storage[player.name][new_name]))
children[1].set_item(index, new_name)
children[1].selected_index = 0
children[1].selected_index = index
global.storage_index[player.name] = index
return
end
if name == "upgrade_planner_storage_delete" then
local children = element.parent.children
local dropdown = children[1]
local index = dropdown.selected_index
local name = dropdown.get_item(index)
global.storage[player.name][name] = nil
if name ~= "New storage" then
dropdown.remove_item(index)
end
if index > 1 then
index = index - 1
end
dropdown.selected_index = 0
dropdown.selected_index = index
gui_restore(player, dropdown.get_item(index))
global.storage_index[player.name] = index
return
end
if name == "upgrade_planner_config_button" then
gui_open_frame(player)
return
end
if name == "upgrade_planner_export_config" then
export_config(player)
return
end
if name == "upgrade_planner_import_config" then
import_config(player)
return
end
if name == "upgrade_planner_frame_close" then
player.opened.destroy()
gui_open_frame(player)
return
end
if name == "upgrade_planner_import_config_button" then
import_config_action(player)
return
end
local type, index = string.match(name, "upgrade_planner_(%a+)_(%d+)")
if type and index then
if type == "clear" then
gui_clear_rule(player, tonumber(index))
return
end
end
end)
script.on_event(defines.events.on_gui_selection_state_changed, function(event)
local element = event.element
local player = game.players[event.player_index]
if not string.find(element.name, "upgrade_planner_") then return end
if element.selected_index > 0 then
global.storage_index[player.name] = element.selected_index
local name = element.get_item(element.selected_index)
gui_restore(player, name)
global.config[player.name] = global.storage[player.name][name]
end
end)
script.on_event(defines.events.on_gui_elem_changed, function(event)
local element = event.element
local player = game.players[event.player_index]
if not string.find(element.name, "upgrade_planner_") then return end
local type, index = string.match(element.name, "upgrade_planner_(%a+)_(%d+)")
if type and index then
if type == "from" or type == "to" then
gui_set_rule(player, type, tonumber(index), element)
end
end
end)
script.on_init(function()
global_init()
for k, player in pairs (game.players) do
gui_init(player)
end
end)
script.on_event(defines.events.on_player_selected_area, function(event)
on_selected_area(event)
end)
script.on_event(defines.events.on_player_alt_selected_area, function(event)
on_alt_selected_area(event)
end)
function on_selected_area(event)
if event.item ~= "upgrade-builder" then return end--If its a upgrade builder
local player = game.players[event.player_index]
local config = global.config[player.name]
if config == nil then return end
local hashmap = get_hashmap(config)
local surface = player.surface
global.temporary_ignore = {}
for k, belt in pairs (event.entities) do --Get the items that are set to be upgraded
if belt.valid then
local upgrade = hashmap[belt.name]
if belt.get_module_inventory() then
player_upgrade_modules(player, belt.get_module_inventory(), hashmap, belt)
end
if upgrade ~= nil and upgrade ~= "" then
player_upgrade(player,belt,upgrade,true)
end
end
end
global.temporary_ignore = nil
end
function get_hashmap(config)
local items = game.item_prototypes
local hashmap = {}
for k, entry in pairs (config) do
local item_from = items[entry.from]
local item_to = items[entry.to]
if item_to and item_from then
hashmap[entry.from] = {item_to = entry.to}
local entity_from = item_from.place_result
local entity_to = item_to.place_result
if entity_from and entity_to then
hashmap[entity_from.name] = {entity_to = entity_to.name, item_to = entry.to, item_from = entry.from}
end
if item_from.type == "rail-planner" and item_to.type == "rail-planner" then
hashmap[item_from.straight_rail.name] = {entity_to = item_to.straight_rail.name, item_to = entry.to, item_from = entry.from}
hashmap[item_from.curved_rail.name] = {entity_to = item_to.curved_rail.name, item_to = entry.to, item_from = entry.from, item_amount = 4}
end
end
end
return hashmap
end
function player_upgrade_modules(player, inventory, map, owner)
for k = 1, #inventory do
local slot = inventory[k]
if slot.valid and slot.valid_for_read then
if not global.temporary_ignore[slot.name] then
local upgrade = map[slot.name]
local recipe = get_recipe(owner)
if upgrade and upgrade.item_to and recipe and check_module_eligibility(upgrade.item_to, recipe) then
if player.get_item_count(upgrade.item_to) >= slot.count or player.cheat_mode then
player.remove_item{name = upgrade.item_to, count = slot.count}
player.insert{name = slot.name, count = slot.count}
slot.set_stack{name = upgrade.item_to, count = slot.count}
else
global.temporary_ignore[slot.name] = true
owner.surface.create_entity{name = "flying-text", position = {owner.position.x-1.3,owner.position.y-0.5}, text = "Insufficient items", color = {r=1,g=0.6,b=0.6}}
end
end
end
end
end
end
function check_module_eligibility(name, recipe)
if not recipe then return true end
local item = game.item_prototypes[name]
if not item then return false end
local effects = item.module_effects
if not effects then return true end
if not effects.productivity then return true end
if not item.limitations then return true end
if item.limitations[recipe.name] then return true end
return false
end
function player_upgrade(player,belt,upgrade, bool)
if not belt then return end
if not upgrade.entity_to then
log("Tried to upgrade when entry had no entity: "..serpent.line(upgrade))
return
end
if global.temporary_ignore[belt.name] then return end
local surface = player.surface
local amount = upgrade.item_amount or 1
if player.get_item_count(upgrade.item_to) >= amount or player.cheat_mode then
local d = belt.direction
local f = belt.force
local p = belt.position
local n = belt.name
local new_item
script.raise_event(defines.events.on_pre_player_mined_item,{player_index = player.index, entity = belt})
if belt.type == "underground-belt" then
if belt.neighbours and bool then
player_upgrade(player,belt.neighbours,upgrade,false)
end
new_item = surface.create_entity
{
name = upgrade.entity_to,
position = belt.position,
force = belt.force,
fast_replace = true,
direction = belt.direction,
type = belt.belt_to_ground_type,
player = player,
spill=false
}
elseif belt.type == "loader" then
new_item = surface.create_entity
{
name = upgrade.entity_to,
position = belt.position,
force = belt.force,
fast_replace = true,
direction = belt.direction,
type = belt.loader_type,
player = player,
spill=false
}
elseif belt.type == "inserter" then
local drop = {x = belt.drop_position.x, y = belt.drop_position.y}
local pickup = {x = belt.pickup_position.x, y = belt.pickup_position.y}
new_item = surface.create_entity
{
name = upgrade.entity_to,
position = belt.position,
force = belt.force,
fast_replace = true,
direction = belt.direction,
player = player,
spill=false
}
if new_item.valid then
new_item.pickup_position = pickup
new_item.drop_position = drop
end
elseif belt.type == "straight-rail" or belt.type == "curved-rail" then
belt.destroy()
new_item = surface.create_entity
{
name = upgrade.entity_to,
position = p,
force = f,
direction = d,
}
else
new_item = surface.create_entity
{
name = upgrade.entity_to,
position = belt.position,
force = belt.force,
fast_replace = true,
direction = belt.direction,
player = player,
spill=false
}
end
if belt.valid then
--If the create entity fast replace didn't work, we use this blueprint technique
if new_item and new_item.valid then new_item.destroy() end
local a = belt.bounding_box
player.cursor_stack.set_stack{name = "blueprint", count = 1}
player.cursor_stack.create_blueprint{surface = surface, force = belt.force, area = a}
local old_blueprint = player.cursor_stack.get_blueprint_entities()
local record_index = nil
for index, entity in pairs (old_blueprint) do
if (entity.name == belt.name) then
record_index = index
else
old_blueprint[index] = nil
end
end
if record_index == nil then player.print("Blueprint index error line "..debug.getinfo(1).currentline) return end
old_blueprint[record_index].name = upgrade.entity_to
old_blueprint[record_index].position = p
player.cursor_stack.set_stack{name = "blueprint", count = 1}
player.cursor_stack.set_blueprint_entities({old_blueprint[record_index]})
if not player.cheat_mode then
player.insert{name = upgrade.item_from, count = amount}
end
script.raise_event(defines.events.on_player_mined_item,
{
player_index = player.index,
item_stack =
{
name = upgrade.item_from,
count = amount
}
})
--And then copy the inventory to some table
local inventories = {}
for index = 1, 10 do
if belt.get_inventory(index) ~= nil then
inventories[index] = {}
inventories[index].name = index
inventories[index].contents = belt.get_inventory(index).get_contents()
end
end
belt.destroy()
player.cursor_stack.build_blueprint{surface = surface, force = f, position = {0,0}}
local ghost = surface.find_entities_filtered{area = a, name = "entity-ghost"}
player.remove_item{name = upgrade.item_from, count = count}
local p_x = player.position.x
local p_y = player.position.y
while ghost[1] ~= nil do
ghost[1].revive()
player.teleport({math.random(p_x -5, p_x +5), math.random(p_y -5, p_y +5)})
ghost = surface.find_entities_filtered{area = a, name = "entity-ghost"}
end
player.teleport({p_x,p_y})
local assembling = surface.find_entities_filtered{area = a, name = upgrade.entity_to}[1]
if not assembling then
player.print("Upgrade planner error - Entity to raise was not found")
player.cursor_stack.set_stack{name = "upgrade-builder", count = 1}
player.insert{name = upgrade.item_from, count = amount}
return
end
script.raise_event(defines.events.on_built_entity,{player_index = player.index, created_entity = assembling})
--Give back the inventory to the new entity
for j, items in pairs (inventories) do
for l, contents in pairs (items.contents) do
if assembling ~= nil then
local inv = assembling.get_inventory(items.name)
if inv then inv.insert{name = l, count = contents} end
end
end
end
inventories = nil
local proxy = surface.find_entities_filtered{area = a, name = "item-request-proxy"}
if proxy[1] ~= nil then
proxy[1].destroy()
end
player.cursor_stack.set_stack{name = "upgrade-builder", count = 1}
else
player.remove_item{name = upgrade.item_to, count = amount}
--player.insert{name = upgrade.item_from, count = amount}
script.raise_event(defines.events.on_player_mined_item,{player_index = player.index, item_stack = {name = upgrade.item_from, count = 1}})
script.raise_event(defines.events.on_built_entity,{player_index = player.index, created_entity = new_item, stack = player.cursor_stack})
end
else
global.temporary_ignore[belt.name] = true
surface.create_entity{name = "flying-text", position = {belt.position.x-1.3,belt.position.y-0.5}, text = "Insufficient items", color = {r=1,g=0.6,b=0.6}}
end
end
function on_alt_selected_area(event)
--this is a lot simpler... but less cool
if event.item ~= "upgrade-builder" then return end
local player = game.players[event.player_index]
local config = global.config[player.name]
if not config then return end
local hashmap = get_hashmap(config)
local surface = player.surface
for k, belt in pairs (event.entities) do
if belt.valid then
local upgrade = hashmap[belt.name]
if upgrade and upgrade ~= "" then
bot_upgrade(player,belt,upgrade,true, hashmap)
end
if belt.valid and belt.get_module_inventory() then
robot_upgrade_modules(belt.get_module_inventory(), hashmap, belt)
end
end
end
end
function robot_upgrade_modules(inventory, map, owner)
if not owner then return end
if not owner.valid then return end
local surface = owner.surface
local modules = {}
local proxy = false
for k = 1, #inventory do
local slot = inventory[k]
if slot.valid and slot.valid_for_read then
local upgrade = map[slot.name]
local recipe = get_recipe(owner)
if upgrade and upgrade.item_to and recipe and check_module_eligibility(upgrade.item_to, recipe) then
local entity = surface.create_entity{name = "item-on-ground", stack = {name = slot.name, count = slot.count}, position = owner.position, force = owner.force}
entity.order_deconstruction(owner.force)
if modules[upgrade.item_to] then
modules[upgrade.item_to] = modules[upgrade.item_to] + slot.count
else
modules[upgrade.item_to] = slot.count
end
proxy = true
slot.clear()
end
end
end
if proxy then
surface.create_entity{name = "item-request-proxy", force = owner.force, position = owner.position, modules = modules, target = owner}
end
end
function get_recipe(owner)
local recipe
if not owner.valid then return end
if owner.type == "beacon" then
recipe = game.recipe_prototypes["stone-furnace"] --Some dummy recipe to get correct limitation
elseif owner.type == "assembling-machine" or owner.type == "furnace" then
recipe = owner.get_recipe() or "iron-gear-wheel"
end
return recipe
end
function bot_upgrade(player, belt, upgrade, bool, hashmap)
if not (belt and belt.valid) then return end
if not upgrade.entity_to then
log("Tried to upgrade when entry had no entity: "..serpent.line(upgrade))
return
end
local surface = player.surface
local p = belt.position
local d = belt.direction
local f = belt.force
local p = belt.position
local a = {{p.x-0.5,p.y-0.5},{p.x+0.5,p.y+0.5}}
if belt.to_be_deconstructed(f) then
return
end
if belt.type == "underground-belt" then
if belt.neighbours and bool then
bot_upgrade(player,belt.neighbours, upgrade, false)
end
end
if belt.type == "straight-rail" or belt.type == "curved-rail" then
belt.order_deconstruction(f)
local new_ghost = surface.create_entity{
name = "entity-ghost",
inner_name = upgrade.entity_to,
position = p,
force = f,
direction = d,
expires = false
}
return
end
player.cursor_stack.set_stack{name = "blueprint", count = 1}
player.cursor_stack.create_blueprint{surface = surface, force = belt.force,area = a}
local old_blueprint = player.cursor_stack.get_blueprint_entities()
local blueprint_index = nil
for k, entity in pairs (old_blueprint) do
if entity.name == belt.name then
blueprint_index = k
break
end
end
if not blueprint_index then
player.print("Upgrade planner bot upgrade blueprint index error - Upgrade unsuccessful: "..belt.name.." -> "..serpent.block(upgrade))
player.cursor_stack.set_stack{name = "upgrade-builder", count = 1}
return
end
--game.print(serpent.block(old_blueprint))
local blueprint_entity = old_blueprint[blueprint_index]
blueprint_entity.name = upgrade.entity_to
local new_items = {}
if blueprint_entity.items then
for item, count in pairs (blueprint_entity.items) do
local new = hashmap[item]
if new then
if new_items[new] then
new_items[new] = new_items[new] + count
else
new_items[new] = count
end
end
end
end
blueprint_entity.items = new_items
player.cursor_stack.set_stack{name = "blueprint", count = 1}
player.cursor_stack.set_blueprint_entities({blueprint_entity})
belt.order_deconstruction(f)
player.cursor_stack.build_blueprint{surface = surface, force = f, position = p}
player.cursor_stack.set_stack{name = "upgrade-builder", count = 1}
end
script.on_configuration_changed(function(data)
if not data or not data.mod_changes then
return
end
verify_all_configs()
nuke_all_guis()
if data.mod_changes["upgrade-planner"] then
if not global.storage_index then
global.storage_index = {}
end
if not global.storage then
global.storage = {}
end
for k, player in pairs (game.players) do
if not global.storage_index[player.name] then
global.storage[player.name] = global.storage[player.name] or {}
global.storage[player.name]["New storage"] = global.config[player.name]
gui_open_frame(player)
gui_open_frame(player)
end
end
end
end)
function verify_all_configs()
local items = game.item_prototypes
local verify_config = function (config)
for k, entry in pairs (config) do
local to = items[entry.to]
local from = items[entry.from]
if not (to and from) then
log("Deleted invalid config: "..k..serpent.line(entry))
entry[k] = nil