generated from fgardt/factorio-mod-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrol.lua
289 lines (248 loc) · 9.19 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
require "gui"
local shared = require "shared"
local update_rate = shared.update_rate
local update_slots = shared.update_slots
local compactify = shared.compactify
local validity_check = shared.validity_check
local combine_tempatures = shared.combine_tempatures
local function setup()
storage.units = storage.units or {}
if remote.interfaces["PickerDollies"] then
remote.call("PickerDollies", "add_blacklist_name", "fluid-memory-unit", true)
remote.call("PickerDollies", "add_blacklist_name", "fluid-memory-unit-combinator", true)
end
end
script.on_init(setup)
script.on_configuration_changed(function()
setup()
for unit_number, unit_data in pairs(storage.units) do
if unit_data.item and not validity_check(unit_number, unit_data) then
if not prototypes.fluid[unit_data.item] then shared.memory_unit_corruption(unit_number, unit_data) end
end
end
end)
local min = math.min
local function render_fluid_animation(item, entity)
local color = prototypes.fluid[item].base_color
rendering.draw_animation {
animation = "fluid-memory-unit-animation",
tint = {
min(0.9, color.r + 0.2),
min(0.9, color.g + 0.2),
min(0.9, color.b + 0.2)
},
render_layer = "higher-object-above",
target = entity,
surface = entity.surface_index
}
end
local function update_unit_exterior(unit_data, inventory_count)
local entity = unit_data.entity
unit_data.previous_inventory_count = inventory_count
local total_count = unit_data.count + inventory_count
if inventory_count > 0 then
local temperature = combine_tempatures(unit_data.count, unit_data.temperature, inventory_count, entity.fluidbox[1].temperature)
entity.fluidbox[1].temperature = temperature
unit_data.temperature = temperature
end
shared.update_combinator(unit_data.combinator, {type = "fluid", name = unit_data.item, quality = "normal"}, total_count)
shared.update_display_text(unit_data, entity, compactify(total_count))
shared.update_power_usage(unit_data, total_count)
end
local function detect_item(unit_data)
local fluidbox = unit_data.entity.fluidbox
local fluid = fluidbox[1]
if fluid then
fluidbox.set_filter(1, {name = fluid.name, force = true})
render_fluid_animation(fluid.name, unit_data.entity)
unit_data.item = fluid.name
unit_data.temperature = fluid.temperature
return true
end
return false
end
local function update_unit(unit_data, unit_number, force)
local entity = unit_data.entity
if validity_check(unit_number, unit_data, force) then return end
local changed = false
if unit_data.item == nil then changed = detect_item(unit_data) end
local item = unit_data.item
if item == nil then return end
local comfortable = unit_data.comfortable
local inventory_count = entity.get_fluid_count(item)
if inventory_count > comfortable then
local amount_removed = entity.remove_fluid {name = item, amount = inventory_count - comfortable}
unit_data.temperature = combine_tempatures(unit_data.count, unit_data.temperature, amount_removed, entity.fluidbox[1].temperature)
unit_data.count = unit_data.count + amount_removed
inventory_count = inventory_count - amount_removed
changed = true
elseif inventory_count < comfortable then
if unit_data.previous_inventory_count ~= inventory_count then
changed = true
end
local to_add = comfortable - inventory_count
if unit_data.count < to_add then
to_add = unit_data.count
end
if to_add > 0.001 then
local amount_added = entity.insert_fluid {name = item, amount = to_add, temperature = unit_data.temperature}
unit_data.count = unit_data.count - amount_added
inventory_count = inventory_count + amount_added
end
end
if force or changed then update_unit_exterior(unit_data, inventory_count) end
end
script.on_nth_tick(update_rate, function(event)
local smooth_ups = event.tick % update_slots
for unit_number, unit_data in pairs(storage.units) do
if unit_data.lag_id == smooth_ups then
update_unit(unit_data, unit_number)
end
end
end)
local function on_created(event)
local entity = event.entity
if entity.name ~= "fluid-memory-unit" then return end
local position = entity.position
local surface = entity.surface
local force = entity.force
local combinator = surface.create_entity {
name = "fluid-memory-unit-combinator",
position = {position.x, position.y - 1.25},
force = force,
quality = entity.quality
}
combinator.operable = false
combinator.destructible = false
local powersource = surface.create_entity {
name = "fluid-memory-unit-powersource",
position = position,
force = force,
quality = entity.quality
}
powersource.destructible = false
local unit_data = {
entity = entity,
comfortable = 0.5 * entity.fluidbox.get_capacity(1),
powersource = powersource,
combinator = combinator,
count = 0,
lag_id = math.random(0, update_slots - 1)
}
storage.units[entity.unit_number] = unit_data
local inventory = event.consumed_items
local tags = event.tags or (inventory and not inventory.is_empty() and inventory[1].valid_for_read and inventory[1].is_item_with_tags and inventory[1].tags) or nil
if tags and tags.name then
unit_data.count = tags.count
unit_data.temperature = tags.temperature
unit_data.item = tags.name
entity.fluidbox.set_filter(1, {name = tags.name, force = true})
render_fluid_animation(tags.name, entity)
update_unit(unit_data, entity.unit_number, true)
else
shared.update_power_usage(unit_data, 0)
end
end
script.on_event(defines.events.on_built_entity, on_created)
script.on_event(defines.events.on_robot_built_entity, on_created)
script.on_event(defines.events.script_raised_built, on_created)
script.on_event(defines.events.script_raised_revive, on_created)
script.on_event(defines.events.on_space_platform_built_entity, on_created)
script.on_event(defines.events.on_entity_cloned, function(event)
local entity = event.source
if entity.name ~= "fluid-memory-unit" then return end
local destination = event.destination
local unit_data = storage.units[entity.unit_number]
local position = destination.position
local surface = destination.surface
local powersource, combinator = unit_data.powersource, unit_data.combinator
if powersource.valid then
powersource = powersource.clone {position = position, surface = surface}
else
powersource = surface.create_entity {
name = "memory-unit-powersource",
position = position,
force = entity.force_index
}
powersource.destructible = false
end
if combinator.valid then
combinator = combinator.clone {position = {position.x, position.y - 1.25}, surface = surface}
else
combinator = surface.create_entity {
name = "fluid-memory-unit-combinator",
position = {position.x, position.y - 1.25},
force = entity.force_index
}
combinator.destructible = false
combinator.operable = false
end
local item = unit_data.item
storage.units[destination.unit_number] = {
powersource = powersource,
combinator = combinator,
item = item,
count = unit_data.count,
entity = destination,
temperature = unit_data.temperature,
comfortable = unit_data.comfortable,
lag_id = math.random(0, update_slots - 1)
}
if item then
render_fluid_animation(item, destination)
destination.fluidbox.set_filter(1, {name = item, force = true})
update_unit(storage.units[destination.unit_number], destination.unit_number, true)
end
end)
local function on_destroyed(event)
local entity = event.entity
if entity.name ~= "fluid-memory-unit" then return end
local unit_data = storage.units[entity.unit_number]
storage.units[entity.unit_number] = nil
unit_data.powersource.destroy()
unit_data.combinator.destroy()
local item = unit_data.item
local count = unit_data.count
local buffer = event.buffer
if buffer and item and count ~= 0 then
buffer.clear()
local temperature = unit_data.temperature
buffer.insert {
name = "fluid-memory-unit-with-tags",
count = 1,
health = entity.health / entity.max_health,
custom_description = {
"item-description.fluid-memory-unit-with-tags",
compactify(count),
item,
string.format("%.2f", temperature)
},
tags = {name = item, count = count, temperature = temperature},
quality = entity.quality
}
end
end
script.on_event(defines.events.on_player_mined_entity, on_destroyed)
script.on_event(defines.events.on_robot_mined_entity, on_destroyed)
script.on_event(defines.events.on_entity_died, on_destroyed)
script.on_event(defines.events.script_raised_destroy, on_destroyed)
script.on_event(defines.events.on_space_platform_mined_entity, on_destroyed)
local function pre_mined(event)
local entity = event.entity
if entity.name ~= "fluid-memory-unit" then return end
local unit_data = storage.units[entity.unit_number]
local item = unit_data.item
if item then
local in_inventory = entity.get_fluid_count(item)
if in_inventory > 0 then
local temperature = entity.fluidbox[1].temperature
local new_count = unit_data.count + entity.remove_fluid {name = item, amount = in_inventory}
unit_data.temperature = combine_tempatures(unit_data.count, unit_data.temperature, in_inventory, temperature)
unit_data.count = new_count
end
end
end
script.on_event(defines.events.on_pre_player_mined_item, pre_mined)
script.on_event(defines.events.on_robot_pre_mined, pre_mined)
script.on_event(defines.events.on_marked_for_deconstruction, pre_mined)
script.on_event(defines.events.on_space_platform_pre_mined, pre_mined)