-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
330 lines (278 loc) · 11.5 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
require "defines"
--[[
--debugging code
script.on_event(defines.events.on_player_created, function(event)
local player = game.get_player(event.player_index)
player.insert{name="overflow-valve", count=10}
player.insert{name="pipe", count=50}
player.insert{name="offshore-pump", count=3}
player.insert{name="storage-tank", count = 10}
player.insert{name="wrench", count = 1}
end)
--]]--
global.locations = {}
global.player_selected = {}
local gui_frame_caption = "Overflow Valve Settings"
---- Event functions ----
function entity_built (event)
if event.created_entity ~= nil and event.created_entity.name == "overflow-valve" then
local entity = event.created_entity
create_proxies(entity)
local location_info = {entity.surface.name, entity.position}
global.locations[location_info] = {overflow = 9, underflow = 10}
end
end
function on_entity_click (event)
local player = event.player
local entity = event.entity
if entity.name == "overflow-valve" then
local location_info, flow_data = search_locations(entity)
if location_info then
show_gui(entity, player, location_info, flow_data)
else
player.print("Error in overflow-valve (on_entity_click), no location info")
end
end
end
function entity_rotated (event)
if event.entity ~= nil and event.entity.name == "overflow-valve" then
local entity = event.entity
local left, right = get_proxies_from_entity(entity)
left.direction = entity.direction
right.direction = (entity.direction + 4) % 8
end
end
function entity_removed (event)
if event.entity ~= nil and event.entity.name == "overflow-valve" then
local entity = event.entity
local left, right = get_proxies_from_entity(entity)
left.destroy()
right.destroy()
local location_info = search_locations(entity)
global.locations[location_info] = nil
for player_index, player_location_info in pairs(global.player_selected) do
if location_info == player_location_info then
destroy_gui(player_index)
end
end
end
end
function on_tick (event)
if(need_init) then
init()
end
for location_info, flow_info in pairs(global.locations) do
local surface_name, position = table.unpack(location_info)
local entity = game.surfaces[surface_name].find_entity("overflow-valve", position)
if entity == nil then
global.locations[location_info] = nil
else
local left, right = get_proxies_from_entity(entity)
if left.fluidbox[1] and (right.fluidbox[1] == nil or left.fluidbox[1].amount > right.fluidbox[1].amount) then
flow_in(entity, left, flow_info.overflow)
flow_out(entity, right, flow_info.underflow)
elseif right.fluidbox[1] and (left.fluidbox[1] == nil or right.fluidbox[1].amount > left.fluidbox[1].amount) then
flow_in(entity, right, flow_info.overflow)
flow_out(entity, left, flow_info.underflow)
end
end
end
for player_index, target_location_info in pairs(global.player_selected) do
local player_position = game.players[player_index].position
local target_position = target_location_info[2]
local reach_distance = 6 --data.raw.player.player.reach_distance
if math.abs((player_position.x - target_position.x) * (player_position.y - target_position.y)) >
reach_distance * reach_distance
then
destroy_gui(player_index)
end
end
end
function on_gui_click (event)
local element = event.element
local player_index = event.player_index
local valve_gui = game.players[player_index].gui.center.wrench
if valve_gui and valve_gui.caption == gui_frame_caption then
if valve_gui.buttons.cancel == element then
destroy_gui(player_index)
elseif valve_gui.buttons.accept == element then
try_gui_accept(valve_gui, player_index)
elseif valve_gui.defaults.prod == element then
valve_gui.over_flow.input.text = "0"
valve_gui.under_flow.input.text = "90"
elseif valve_gui.defaults.cons == element then
valve_gui.over_flow.input.text = "90"
valve_gui.under_flow.input.text = "100"
end
end
end
function on_configuration_changed(...)
if(global.valve_locations) then
need_init = true
end
if global.valve then
global.locations = global.valve.locations
global.player_selected = global.valve.player_selected
global.valve = nil
end
end
function on_load()
if next(global.player_selected) then
script.on_event(defines.events.on_gui_click, on_gui_click)
end
end
---- Helper functions ----
function create_proxies (entity)
entity.surface.create_entity{
name = "valve-proxy",
position = entity.position,
direction = entity.direction,
force = entity.force
}
entity.surface.create_entity{
name = "valve-proxy",
position = entity.position,
direction = (entity.direction + 4) % 8,
force = entity.force
}
end
function destroy_gui(player_index)
local valve_gui = game.players[player_index].gui.center.wrench
if valve_gui then
valve_gui.destroy()
global.player_selected[player_index] = nil
if next(global.player_selected) == nil then
script.on_event(defines.events.on_gui_click, nil)
end
end
end
function try_gui_accept (valve_gui, player_index)
local errors = {}
local overflow = tonumber(valve_gui.over_flow.input.text)
if overflow == nil then
table.insert(errors, "Minimum input must be a number!")
elseif overflow < 0 then
table.insert(errors, "Minimum input was too small. It must be between 0 and 100 but was " .. overflow .. ".")
elseif overflow > 100 then
table.insert(errors, "Minimum input was too large. It must be between 0 and 100 but was " .. overflow .. ".")
end
local underflow = tonumber(valve_gui.under_flow.input.text)
if underflow == nil then
table.insert(errors, "Maximum output must be a number!")
elseif underflow < 0 then
table.insert(errors, "Maximum output was too small. It must be between 0 and 100 but was " .. underflow .. ".")
elseif underflow > 100 then
table.insert(errors, "Maximum output was too large. It must be between 0 and 100 but was " .. underflow .. ".")
end
if next(errors) == nil then
global.locations[global.player_selected[player_index]] = {overflow = overflow/10, underflow = underflow/10}
destroy_gui(player_index)
else
if valve_gui.errors then
valve_gui.errors.destroy()
end
valve_gui.add{type = "flow", name = "errors", direction = "vertical"}
for _, error_text in ipairs(errors) do
local error_label = valve_gui.errors.add{type = "label", caption = error_text}
error_label.style.font_color = {r = 1}
end
end
end
function show_gui (entity, player, location_info, flow_data)
local center_gui = player.gui.center
if center_gui.wrench then
destroy_gui(player.index)
end
center_gui.add{type = "frame", caption = gui_frame_caption, name = "wrench", direction = "vertical"}
local valve_gui = center_gui.wrench
valve_gui.add{type = "flow", name = "defaults", direction = "horizontal"}
valve_gui.defaults.add{type = "button", name = "prod", caption = "Restrict Production"}
valve_gui.defaults.add{type = "button", name = "cons", caption = "Restrict Consumption"}
valve_gui.add{type = "flow", name = "over_flow", direction = "horizontal"}
valve_gui.over_flow.add{type = "label", caption = "Minimum input % full for flow:"}
local over_field = valve_gui.over_flow.add{type = "textfield", name = "input"}
over_field.text = tostring(flow_data.overflow * 10)
over_field.style.maximal_width = 50
valve_gui.over_flow.add{type = "label", caption = "%"}
valve_gui.add{type = "flow", name = "under_flow", direction = "horizontal"}
valve_gui.under_flow.add{type = "label", caption = "Maximum output % full for flow:"}
local under_field = valve_gui.under_flow.add{type = "textfield", name = "input"}
under_field.text = tostring(flow_data.underflow * 10)
under_field.style.maximal_width = 50
valve_gui.under_flow.add{type = "label", caption = "%"}
valve_gui.add{type = "flow", name = "buttons", direction = "horizontal"}
valve_gui.buttons.add{type = "button", name = "accept", caption = "Accept"}
valve_gui.buttons.add{type = "button", name = "cancel", caption = "Cancel"}
global.player_selected[player.index] = location_info
script.on_event(defines.events.on_gui_click, on_gui_click)
end
function search_locations (entity)
for location_info, flow_info in pairs(global.locations) do
if entity.surface.name == location_info[1] then
local position = location_info[2]
if entity.position.x == position.x and entity.position.y == position.y then
return location_info, flow_info
end
end
end
return nil, nil
end
function get_proxies_from_entity(entity)
local search_area = {entity.position, entity.position}
local proxies = entity.surface.find_entities_filtered{area = search_area, name = "valve-proxy"}
return table.unpack(proxies)
end
function flow (in_box, out_box, flow_amount)
out_box.amount = out_box.amount - flow_amount
in_box.temperature =
(in_box.temperature * in_box.amount + out_box.temperature * flow_amount)
/ (in_box.amount + flow_amount)
in_box.amount = in_box.amount + flow_amount
in_box.type = out_box.type
return in_box, out_box
end
function flow_in (entity, proxy, overflow)
local proxy_box = proxy.fluidbox[1]
if (proxy_box ~= nil) then
local valve_box = entity.fluidbox[1] or {amount = 0, type = "water", temperature = 0}
if proxy_box.amount > overflow and (proxy_box.type == valve_box.type or valve_box.amount == 0) then
local flow_amount = math.min(proxy_box.amount - overflow, 10 - valve_box.amount)
entity.fluidbox[1], proxy.fluidbox[1] = flow(valve_box, proxy_box, flow_amount)
end
end
end
function flow_out (entity, proxy, underflow)
local valve_box = entity.fluidbox[1]
if valve_box ~= nil then
local proxy_box = proxy.fluidbox[1] or {amount = 0, type = "water", temperature = 0}
if proxy_box.amount < underflow and (proxy_box.type == valve_box.type or proxy_box.amount == 0) then
local flow_amount = math.min((underflow - proxy_box.amount)/2, valve_box.amount/2)
proxy.fluidbox[1], entity.fluidbox[1] = flow(proxy_box, valve_box, flow_amount)
end
end
end
function init()
global.locations = global.locations or global.valve.locations or {}
global.player_selected = global.player_selected or (global.valve and global.valve.player_selected) or {}
for location_info, _ in pairs(global.valve_locations) do
local position, surface = table.unpack(location_info)
for k, v in pairs(surface) do
game.player.print("k " .. tostring(k) .. " v " .. tostring(v))
end
surface = game.get_surface(surface.surfaceindex + 1)
new_location_info = {surface.name, position}
global.locations[new_location_info] = {overflow = 9, underflow = 10}
end
global.valve_locations = nil
need_init = false
end
script.on_event(defines.events.on_built_entity, entity_built)
script.on_event(defines.events.on_robot_built_entity, entity_built)
script.on_event(defines.events.on_player_rotated_entity, entity_rotated)
script.on_event(defines.events.on_preplayer_mined_item, entity_removed)
script.on_event(defines.events.on_robot_pre_mined, entity_removed)
script.on_event(defines.events.on_entity_died, entity_removed)
script.on_event(defines.events.on_tick, on_tick)
script.on_event(remote.call("wrench.events", "entity_click"), on_entity_click)
script.on_load(on_load)
script.on_configuration_changed(on_configuration_changed)