Repository navigation
Expand file tree
/
Copy pathcontrol.lua
More file actions
234 lines (213 loc) · 7.9 KB
/
Copy pathcontrol.lua
File metadata and controls
234 lines (213 loc) · 7.9 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
local progression = require("scripts.progression")
script.on_event(defines.events.on_entity_spawned, require("scripts.stomp-a-trons"))
-- Off-world Yuoki crushers are recorded so a save without this list can be rebuilt.
-- Native selection rejects quinityn-hand-sort when quinityn-industry is not 1.
-- A save that already has that recipe selected is cleared once, not on every tick.
local crusher_names = { "y-crusher", "y_crusher2" }
local crusher_event_filters = {
{ filter = "name", name = "y-crusher" },
{ filter = "name", name = "y_crusher2" },
}
local function is_crusher_name(name)
return name == "y-crusher" or name == "y_crusher2"
end
local function surface_is_offworld(surface)
return surface.valid and surface.get_property("quinityn-industry") ~= 1
end
local function rebuild_offworld_crushers()
storage.offworld_crushers = {}
for _, surface in pairs(game.surfaces) do
if surface_is_offworld(surface) then
for _, machine in pairs(surface.find_entities_filtered({ name = crusher_names })) do
if machine.valid then
storage.offworld_crushers[machine.unit_number] = machine
end
end
end
end
end
local function remember_crusher(entity)
if not entity or not entity.valid or not is_crusher_name(entity.name) then
return
end
if storage.offworld_crushers == nil then
rebuild_offworld_crushers()
end
if surface_is_offworld(entity.surface) then
storage.offworld_crushers[entity.unit_number] = entity
else
storage.offworld_crushers[entity.unit_number] = nil
end
end
local function forget_crusher(entity)
if storage.offworld_crushers == nil or not entity then
return
end
if entity.valid then
storage.offworld_crushers[entity.unit_number] = nil
end
end
local function prune_invalid_offworld_crushers()
if storage.offworld_crushers == nil then
return
end
for unit_number, entity in pairs(storage.offworld_crushers) do
if not entity.valid then
storage.offworld_crushers[unit_number] = nil
end
end
end
local function salvage_recipe_is_surface_locked()
local recipe = prototypes.recipe["quinityn-hand-sort"]
local conditions = recipe and recipe.surface_conditions
if not conditions then
return false
end
for _, condition in pairs(conditions) do
if condition.property == "quinityn-industry" and condition.min == 1 and condition.max == 1 then
return true
end
end
return false
end
local function spill_cleared_items(machine, returned)
if type(returned) ~= "table" then
return
end
for _, stack in pairs(returned) do
if type(stack) == "table" and stack.name and stack.count and stack.count > 0 then
machine.surface.spill_item_stack({
position = machine.position,
stack = { name = stack.name, count = stack.count, quality = stack.quality },
force = machine.force,
})
end
end
end
local function clear_saved_offworld_salvage_sort()
if not salvage_recipe_is_surface_locked() or storage.offworld_crushers == nil then
return
end
for _, machine in pairs(storage.offworld_crushers) do
if machine.valid then
local selected = machine.get_recipe()
if selected and selected.name == "quinityn-hand-sort" then
spill_cleared_items(machine, machine.set_recipe(nil))
end
end
end
end
local function on_startup()
progression.reconcile()
rebuild_offworld_crushers()
clear_saved_offworld_salvage_sort()
storage.salvage_sort_migration_done = true
end
script.on_init(on_startup)
script.on_configuration_changed(on_startup)
script.on_event(defines.events.on_player_changed_surface, function(e)
progression.arrive(game.get_player(e.player_index))
end)
script.on_event(defines.events.on_player_created, function(e)
progression.arrive(game.get_player(e.player_index))
end)
script.on_event(defines.events.on_player_respawned, function(e)
progression.arrive(game.get_player(e.player_index))
end)
script.on_event(defines.events.on_player_changed_force, function(e)
progression.arrive(game.get_player(e.player_index))
end)
script.on_event(defines.events.on_research_finished, progression.research)
script.on_event(defines.events.on_forces_merged, progression.reconcile)
script.on_event(defines.events.on_chunk_generated, progression.chunk)
script.on_event(defines.events.on_player_joined_game, function(e)
progression.arrive(game.get_player(e.player_index))
end)
script.on_event(defines.events.on_player_controller_changed, function(e)
progression.arrive(game.get_player(e.player_index))
end)
local function on_crusher_built(event)
remember_crusher(event.entity)
end
local function on_crusher_removed(event)
forget_crusher(event.entity)
end
local build_events = {
defines.events.on_built_entity,
defines.events.on_robot_built_entity,
defines.events.script_raised_built,
defines.events.script_raised_revive,
defines.events.on_space_platform_built_entity,
}
for _, build_event in ipairs(build_events) do
script.on_event(build_event, on_crusher_built, crusher_event_filters)
end
script.on_event(defines.events.on_entity_cloned, function(event)
remember_crusher(event.destination)
end)
local removal_events = {
defines.events.on_player_mined_entity,
defines.events.on_robot_mined_entity,
defines.events.on_entity_died,
defines.events.script_raised_destroy,
}
for _, removal_event in ipairs(removal_events) do
script.on_event(removal_event, on_crusher_removed, crusher_event_filters)
end
script.on_event({ defines.events.on_surface_deleted, defines.events.on_surface_cleared }, function()
prune_invalid_offworld_crushers()
end)
script.on_event(defines.events.on_tick, function()
if storage.offworld_crushers == nil then
rebuild_offworld_crushers()
end
-- Old saves have no flag. One pass clears a recipe selected before this migration.
if storage.salvage_sort_migration_done then
return
end
clear_saved_offworld_salvage_sort()
storage.salvage_sort_migration_done = true
end)
-- The optional Milestones mod appends this addon to the active preset.
-- It must be registered in normal play, not only beside the test mod.
local remote_functions = {
milestones_preset_addons = function()
return {
["Yuoki Industries: Quinityn"] = {
required_mods = { "yuoki-quinityn" },
milestones = {
{ type = "group", name = "Science" },
{ type = "item", name = "quinityn-research-data", quantity = 1 },
{ type = "group", name = "Science at scale" },
{ type = "item", name = "quinityn-research-data", quantity = 1000, next = "x10" },
{ type = "group", name = "Progress" },
{ type = "item", name = "yi_construction-robot", quantity = 1 },
{ type = "item", name = "yi_logistic-robot", quantity = 1 },
{ type = "item", name = "j_construction2-robot", quantity = 1 },
{ type = "item", name = "j_logistic2-robot", quantity = 1 },
{ type = "group", name = "Kills" },
{ type = "kill", name = "quinityn-small-biter", quantity = 1 },
{ type = "kill", name = "quinityn-medium-biter", quantity = 1 },
{ type = "kill", name = "quinityn-big-biter", quantity = 1 },
{ type = "kill", name = "quinityn-behemoth-biter", quantity = 1 },
{ type = "kill", name = "quinityn-small-stomp-a-tron", quantity = 1 },
{ type = "kill", name = "quinityn-medium-stomp-a-tron", quantity = 1 },
{ type = "kill", name = "quinityn-big-stomp-a-tron", quantity = 1 },
},
},
}
end,
}
if script.active_mods["quinityn-tests"] then
remote_functions.offworld_crusher_tracked = function(unit_number)
local entity = storage.offworld_crushers and storage.offworld_crushers[unit_number]
return entity ~= nil and entity.valid
end
remote_functions.clear_offworld_crusher_list = function()
storage.offworld_crushers = nil
end
remote_functions.clear_salvage_sort_migration_flag = function()
storage.salvage_sort_migration_done = nil
end
end
remote.add_interface("yuoki-quinityn", remote_functions)