-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
175 lines (133 loc) · 4.72 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
local util = require("__core__/lualib/util")
local SelectorAppearance = require("scripts.selector_appearance")
local SelectorGui = require("scripts.selector_gui")
local SelectorRuntime = require("scripts.selector_runtime")
script.on_init(function()
SelectorRuntime.init()
end)
local selector_filter = {
filter = "name",
name = "selector-combinator",
}
local function on_added(event)
SelectorRuntime.add_combinator(event)
end
local function on_entity_settings_pasted(event)
local source = event.source
local destination = event.destination
if not source or not destination or
source.name ~= "selector-combinator" or
destination.name ~= "selector-combinator" then
return
end
source = global.selector_combinators[source.unit_number]
destination = global.selector_combinators[destination.unit_number]
if not source or not destination then return end
destination.settings = util.table.deepcopy(source.settings)
SelectorAppearance.update_combinator_appearance(destination)
-- Get this selector into its running state
SelectorRuntime.clear_caches_and_force_update(destination)
end
local function get_blueprint(event)
local player = game.get_player(event.player_index)
if not player then return end
local bp = player.blueprint_to_setup
if bp and bp.valid_for_read then
return bp
end
bp = player.cursor_stack
if not bp or not bp.valid_for_read then return end
if bp.type == "blueprint-book" then
local item_inventory = bp.get_inventory(defines.inventory.item_main)
if item_inventory then
bp = item_inventory[bp.active_index]
else
return
end
end
return bp
end
local function on_player_setup_blueprint(event)
local blueprint = get_blueprint(event)
if not blueprint then return end
local entities = blueprint.get_blueprint_entities()
if not entities then return end
for i, entity in pairs(entities) do
if entity.name == "selector-combinator" then
local selector = event.surface.find_entity(entity.name, entity.position)
if selector then
selector = global.selector_combinators[selector.unit_number]
if selector then
blueprint.set_blueprint_entity_tag(i, "selector-combinator", util.table.deepcopy(selector.settings))
end
end
end
end
end
local function on_entity_destroyed(event)
SelectorRuntime.remove_combinator(event.entity.unit_number)
end
local function on_destroyed(event)
SelectorRuntime.remove_combinator(event.unit_number)
end
local function on_gui_opened(event)
local entity = event.entity
if not entity then
return
end
if not entity.valid then
return
end
if entity.name ~= "selector-combinator" then
return
end
local player = game.get_player(event.player_index)
if not player then
return
end
SelectorGui.on_gui_added(player, entity)
end
local function on_gui_closed(event)
local element = event.element
if not element or element.name ~= "selector_gui" then
return
end
local player = game.get_player(event.player_index)
if player then
SelectorGui.on_gui_removed(player)
end
end
SelectorGui.bind_all_events()
-- Added Events
script.on_event(defines.events.on_built_entity, on_added, {selector_filter})
script.on_event(defines.events.on_robot_built_entity, on_added, {selector_filter})
-- Paste events
script.on_event(defines.events.on_entity_settings_pasted, on_entity_settings_pasted)
-- Blueprint events
script.on_event(defines.events.on_player_setup_blueprint, on_player_setup_blueprint)
-- Removed Events
script.on_event(defines.events.on_player_mined_entity, on_entity_destroyed, {selector_filter})
script.on_event(defines.events.on_robot_mined_entity, on_entity_destroyed, {selector_filter})
script.on_event(defines.events.script_raised_destroy, on_entity_destroyed, {selector_filter})
-- *Special* Removed Events
script.on_event(defines.events.on_entity_destroyed, on_destroyed)
-- GUI Events
script.on_event(defines.events.on_gui_opened, on_gui_opened)
script.on_event(defines.events.on_gui_closed, on_gui_closed)
-- Update Event
script.on_event(defines.events.on_tick, function()
for _, selector in pairs(global.selector_combinators) do
selector:on_tick()
end
end)
-- Put every player that joins into editor mode
script.on_event(defines.events.on_player_created, function(event)
local player = game.get_player(event.player_index)
player.cheat_mode = true
if not player then
return
end
for _, tech in pairs(player.force.technologies) do
tech.researched = true
end
end)