-
Notifications
You must be signed in to change notification settings - Fork 0
/
control-items.lua
135 lines (106 loc) · 4.09 KB
/
control-items.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
local d = require "sl-defines"
local u = require "sl-util"
local export = {}
local function SwapToBaseEntityType(itemStack)
-- Step 1: Swap the items
local old = itemStack.get_blueprint_entities()
if not old then
return
end
local new = {}
for index, e in pairs(old) do
-- Build the base searchlight instead of the alarm/safe mode versions
if e.name == d.searchlightAlarmName then
e.name = d.searchlightBaseName
elseif e.name == d.searchlightSafeName then
e.name = d.searchlightBaseName
elseif u.EndsWith(e.name, d.boostSuffix) then
e.name = e.name:gsub(d.boostSuffix, "")
end
-- Resort the items so the signal interface ghost stops appearing on top
if e.name == d.searchlightSignalInterfaceName then
table.insert(new, 1, e)
else
table.insert(new, e)
end
end
itemStack.set_blueprint_entities(new)
end
-- Check that any deleted searchlights have their
-- corresponding interface also deleted
-- (If someone somehow deletes just an interface from a blueprint,
-- I guess it's okay if we still keep the light?)
local function CheckForSignalSearchlightParity(itemStack)
local old = itemStack.get_blueprint_entities()
if not old then
return
end
local new = {}
local sigPositions = {}
local lightPositions = {}
for index, e in pairs(old) do
if e.name == d.searchlightSignalInterfaceName then
table.insert(sigPositions, index, e.position)
elseif e.name == d.searchlightBaseName then
table.insert(lightPositions, index, e.position)
table.insert(new, e)
else
table.insert(new, e)
end
end
for sigIndex, sigPos in pairs(sigPositions) do
for lightIndex, lightPos in pairs(lightPositions) do
if sigPos.x == lightPos.x and sigPos.y == lightPos.y then
table.insert(new, 1, old[sigIndex])
end
end
end
itemStack.set_blueprint_entities(new)
end
-- Since the on_player_setup_blueprint event doesn't point you to the actual blueprint
-- which has been setup, we have to trawl all of the player's blueprints recursively.
local function SeekBlueprints(inventory)
if not inventory then
return
end
for index = 1, #inventory - inventory.count_empty_stacks() do
local item = inventory[index]
if item.valid_for_read and item.name == "blueprint" and item.is_blueprint_setup() then
SwapToBaseEntityType(item)
CheckForSignalSearchlightParity(item)
elseif item.valid_for_read and item.name == "blueprint-book" then
-- Currently, the game prevents you from making a blueprint book that contains itself somewhere.
SeekBlueprints(item.get_inventory(defines.inventory.item_main))
end
end
end
export.ScanBP_StacksAndSwapToBaseType = function(event)
local player = game.players[event.player_index]
local cstack = player.cursor_stack
local pstack = player.blueprint_to_setup
if cstack and cstack.valid_for_read and cstack.is_blueprint and cstack.is_blueprint_setup() then
SwapToBaseEntityType(cstack)
CheckForSignalSearchlightParity(cstack)
elseif pstack and pstack.valid_for_read and pstack.is_blueprint and pstack.is_blueprint_setup() then
SwapToBaseEntityType(pstack)
CheckForSignalSearchlightParity(pstack)
else
SeekBlueprints(player.get_main_inventory())
end
end
-- It's possible for the player to right click to destroy a searchlight ghost,
-- but leave behind the signal interface ghost from a blueprint.
-- The best we can do is detect when such ghosts are built and destroy them
-- after the fact, since there doesn't seem to be an event to detect when
-- the player manually clears a ghost via right click.
export.CheckSignalInterfaceHasSearchlight = function(i)
local sLight = i.surface.find_entities_filtered{name={d.searchlightBaseName,d.searchlightAlarmName},
position = i.position}
local slGhost = i.surface.find_entities_filtered{ghost_name={d.searchlightBaseName},
position = i.position}
if not ((sLight and sLight[1])
or (slGhost and slGhost[1])) then
i.destroy()
end
end
return export