-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
579 lines (556 loc) · 25.2 KB
/
control.lua
File metadata and controls
579 lines (556 loc) · 25.2 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
require "__perel__.util.scripts.general"
require "__perel__.util.scripts.fluids"
local mod_data = assert(prototypes.mod_data["parallel-piping"], "ERROR: mod-data for parallel-piping not found!")
local base_pipe = assert(mod_data.data.base_pipe, "ERROR: data.base_pipe for parallel-piping not found!")
local variations = assert(mod_data.data.variations, "ERROR: data.variations for parallel-piping not found!")
local bitmasks = assert(mod_data.data.bitmasks, "ERROR: data.bitmasks for parallel-piping not found!")
local event_filter = {{filter = "type", type = "pipe"}, {filter = "ghost_type", type = "pipe"}, {filter = "type", type = "storage-tank"}, {filter = "ghost_type", type = "storage-tank"}}
local tank_to_pipe = {
nothingburger = {
[0] = 0,
[4] = 0,
[8] = 0,
[12] = 0
},
ending = {
[0] = 4,
[4] = 8,
[8] = 1,
[12] = 2
},
straight = {
[0] = 5,
[4] = 10,
[8] = 5,
[12] = 10
},
junction = {
[0] = 14,
[4] = 13,
[8] = 11,
[12] = 7
},
corner = {
[0] = 6,
[4] = 12,
[8] = 9,
[12] = 3
},
cross = {
[0] = 15,
[4] = 15,
[8] = 15,
[12] = 15
}
}
local pipe_to_tank = {
[0] = {mask = "nothingburger"},
{mask = "ending", direction = defines.direction.south},
{mask = "ending", direction = defines.direction.west},
{mask = "corner", direction = defines.direction.west},
{mask = "ending", direction = defines.direction.north},
{mask = "straight", direction = defines.direction.north},
{mask = "corner", direction = defines.direction.north},
{mask = "junction", direction = defines.direction.west},
{mask = "ending", direction = defines.direction.east},
{mask = "corner", direction = defines.direction.south},
{mask = "straight", direction = defines.direction.east},
{mask = "junction", direction = defines.direction.south},
{mask = "corner", direction = defines.direction.east},
{mask = "junction", direction = defines.direction.east},
{mask = "junction", direction = defines.direction.north},
{mask = "cross"},
}
-- transform "0" to 0 etc
for index, set in pairs(variations) do
local new_set = {}
for mask, entity in pairs(set) do
if tonumber(mask) then
new_set[tonumber(mask)] = entity
else
new_set[mask] = entity
end
end
variations[index] = new_set
end
-- update PEREL conneciton categories for 00 entities
for _, set in pairs(variations) do
perel.set_entity_connection_categories(set[0], perel.get_entity_connection_categories(prototypes.entity[set[1]]))
end
script.on_init(function()
---@type table<uint, uint> player index -> tick
storage.build_ticks = {}
---@type table<uint, LuaEntity> player index -> entity
storage.previous = {}
---@type table<uint, uint> player index -> bitmask
storage.existing_connections = {}
---@type table<uint, uint> player index -> health
storage.old_health = {}
---@type table<uint, uint> player index -> health
storage.old_fluid = {}
end)
script.on_configuration_changed(function()
storage.build_ticks = storage.build_ticks or {}
storage.previous = storage.previous or {}
storage.existing_connections = storage.existing_connections or {}
storage.old_health = storage.old_health or {}
storage.old_fluid = storage.old_fluid or {}
end)
--- @param event EventData.on_built_entity|EventData.on_robot_built_entity|EventData.on_space_platform_built_entity|EventData.script_raised_built|EventData.script_raised_revive|EventData.on_cancelled_deconstruction
local function on_built(event)
local player = event.player_index and game.get_player(event.player_index)
local entity = event.entity
local previous = player and storage.previous[player.index]
local prev_name = previous and previous.valid and (previous.name == "entity-ghost" and previous.ghost_name or previous.name)
if player then
storage.previous[player.index] = entity
end
local prototype = entity.name == "entity-ghost" and entity.ghost_prototype or entity.prototype
local name = prototype.name
local base = base_pipe[name]
local surface = entity.surface
local stack = player and player.undo_redo_stack
local blueprint = stack and stack.get_undo_item_count() > 0 and #stack.get_undo_item(1) ~= 1
if base then
if blueprint then -- multiple items (blueprint or otherwise) do complicated checks
local i = perel.find_build_action(stack.get_undo_item(1), entity)
if i then stack.remove_undo_action(1, i) end
end
-- just placed a blueprint, convert to normal
if entity.type == "entity-ghost" and entity.ghost_type == "storage-tank" or entity.type == "storage-tank" then
local mask = tank_to_pipe[bitmasks[name]][entity.direction]
local new_name = variations[base_pipe[name]][mask]
local new_entity = surface.create_entity{
name = entity.name == "entity-ghost" and "entity-ghost" or new_name,
ghost_name = entity.name == "entity-ghost" and new_name or nil,
position = entity.position,
quality = entity.quality,
force = entity.force,
player = event.player_index,
undo_index = player and 1 or nil,
create_build_effect_smoke = false,
raise_built = true
}
entity.destroy()
if player then
storage.previous[player.index] = new_entity
end
if stack and not blueprint then
stack.remove_undo_action(1, 1)
end
return
end
end
if not player then return end
local existing = player and storage.existing_connections[player.index]
local variation = existing and bitmasks[existing] or 0
if player then
storage.existing_connections[player.index] = nil
end
local can_place = base and surface.can_place_entity{name = variations[base][0], position = entity.position, force = entity.force}
local ignore = not not bitmasks[name] -- cancel if this is already a variation
local other_fluid
if prev_name and not ignore then
if base_pipe[prev_name] then
local prev_variation = 2 ^ (perel.get_direction(previous.position, entity.position) / 4)
local new_mask = bit32.bor(bitmasks[prev_name], prev_variation)
local previous_prototype = prototypes.entity[prev_name]
local connect = base_pipe[existing or can_place and name or ""] == base_pipe[prev_name]
local dx, dy = math.abs(entity.position.x - previous.position.x), math.abs(entity.position.y - previous.position.y)
local dist = (math.ceil(perel.get_side_length(prototype)) + math.ceil(perel.get_side_length(previous_prototype))) / 2
if (not can_place or dx ~= dy and math.max(dx, dy) == dist) then -- do distance based check first, then more expensive checks
local fluid_amount
if not connect then
if base_pipe[existing or can_place and name or ""] then
-- both are pipes, check if they can connect via connection categories
local categories = perel.get_entity_connection_categories(prototypes.entity[variations[base_pipe[existing or can_place and name]][1]])
for category in pairs(perel.get_entity_connection_categories(prototypes.entity[variations[base_pipe[prev_name]][1]])) do
if categories[category] then
connect = true
break
end
end
-- check fluid compatibility
local existing_fluid = player and storage.old_fluid[player.index]
local previous_fluid = previous.fluidbox[1]
connect = connect and (not existing_fluid or not previous_fluid or existing_fluid.name == previous_fluid.name)
else
-- only the previous entity is a valid pipe, check if it can connect to this entity
for _, existing_entity in pairs(surface.find_entities_filtered{position = entity.position, force = entity.force}) do
if existing_entity ~= entity then
for i, fluidbox in pairs(existing_entity and perel.get_possible_fluidbox_neighbours_by_fluidbox_and_connection(existing_entity) or {}) do
for _, neighbour in pairs(fluidbox) do
if neighbour == previous then
connect = true
-- check fluid compatibility
local existing_fluid = existing_entity.fluidbox[i]
if existing_fluid then
local amount = existing_entity.fluidbox.get_fluid_segment_contents(i)
existing_fluid.amount = amount and amount[existing_fluid.name] or existing_fluid.amount
end
local previous_fluid = previous.fluidbox[1]
if previous_fluid then
local amount = previous.fluidbox.get_fluid_segment_contents(1)
previous_fluid.amount = amount and amount[previous_fluid.name] or other_fluid.amount
end
connect = connect and (not existing_fluid or not previous_fluid or existing_fluid.name == previous_fluid.name)
fluid_amount = connect and (existing_fluid and existing_fluid.amount or 0) + (previous_fluid and previous_fluid.amount or 0)
break
end
end
end
if connect ~= nil then break end
end
end
end
end
if connect then
-- update variation, regardless of if the previous entity is updated
variation = bit32.bor(variation, 2 ^ (perel.get_direction(entity.position, previous.position) / 4))
if new_mask ~= bitmasks[prev_name] then -- actually update the previously selected pipe
local build_index, build_action = perel.find_build_item(stack, previous)
local health = previous.health
other_fluid = previous.fluidbox[1]
if other_fluid then
local amount = previous.fluidbox.get_fluid_segment_contents(1)
other_fluid.amount = fluid_amount or amount and amount[other_fluid.name] or other_fluid.amount
end
local new_prev = surface.create_entity{
name = previous.name == "entity-ghost" and "entity-ghost" or variations[base_pipe[prev_name]][new_mask],
ghost_name = previous.name == "entity-ghost" and variations[base_pipe[prev_name]][new_mask] or nil,
position = previous.position,
quality = previous.quality,
force = previous.force,
player = build_index and player.index or nil,
undo_index = build_index,
create_build_effect_smoke = false,
raise_built = true
}
previous.destroy()
if build_index then stack.remove_undo_action(build_index, build_action) end
if health then new_prev.health = health end
if other_fluid then new_prev.fluidbox[1] = other_fluid end
end
end
end
else -- not a pipe, connect generically if allowed
local found = false
for i = 1, #previous.fluidbox do
for _, connection in pairs(previous.fluidbox.get_pipe_connections(i)) do
if surface.find_entity(name, connection.target_position) then
variation = bit32.bor(variation, 2 ^ (perel.get_direction(entity.position, previous.position) / 4))
found = true
break
end
end
if found then break end
end
end
end
if can_place and not ignore then
local health = player and storage.old_health[player.index] or entity.health
local fluid = player and storage.old_fluid[player.index]
if fluid then
fluid.amount = fluid.amount + (other_fluid and other_fluid.amount or 0)
end
local new_name = variations[base][variation]
if player then
storage.old_health[player.index] = nil
end
local new_entity = surface.create_entity{
name = entity.name == "entity-ghost" and "entity-ghost" or new_name,
ghost_name = entity.name == "entity-ghost" and new_name or nil,
position = entity.position,
quality = entity.quality,
force = entity.force,
player = event.player_index,
undo_index = player and 1 or nil,
create_build_effect_smoke = false,
raise_built = true
}
entity.destroy()
if health then new_entity.health = health end
if fluid then new_entity.fluidbox[1] = fluid end
if player then
storage.previous[player.index] = new_entity
end
elseif base and not ignore then
if entity.name ~= "entity-ghost" then
if player and player.cursor_stack and player.cursor_stack.valid_for_read then
player.cursor_stack.count = player.cursor_stack.count + 1
elseif player and event.consumed_items and player.cursor_stack and player.is_cursor_empty() then
-- just placed last item, put it back
player.cursor_stack.transfer_stack(event.consumed_items[1])
end
end
local params = {
position = entity.position,
force = entity.force,
collision_mask = prototypes.entity[variations[base][variation]].collision_mask.layers
}
entity.destroy()
if player then
local found
for _, e in pairs(surface.find_entities_filtered(params)) do
local p = e.type == "entity-ghost" and e.ghost_prototype or e.prototype
if #p.fluidbox_prototypes ~= 0 then
storage.previous[player.index] = e
found = true
break
end
end
if not found then
-- even more basic check, ignore collision mask
params.collision_mask = nil
for _, e in pairs(surface.find_entities_filtered(params)) do
local p = e.type == "entity-ghost" and e.ghost_prototype or e.prototype
if #p.fluidbox_prototypes ~= 0 then
storage.previous[player.index] = e
break
end
end
end
end
end
-- simple remove only item in list (this thing that was just built)
if stack and not blueprint then
stack.remove_undo_action(1, 1)
end
end
script.on_event(defines.events.on_built_entity, on_built)
script.on_event(defines.events.on_robot_built_entity, on_built, event_filter)
script.on_event(defines.events.on_space_platform_built_entity, on_built, event_filter)
script.on_event(defines.events.script_raised_built, on_built, event_filter)
script.on_event(defines.events.script_raised_revive, on_built, event_filter)
---@param event EventData.on_pre_build
script.on_event(defines.events.on_pre_build, function(event)
storage.build_ticks[event.player_index] = event.tick
storage.old_health[event.player_index] = nil
storage.old_fluid[event.player_index] = nil
local player = game.get_player(event.player_index)
local place_result = player.cursor_ghost and player.cursor_ghost.name.place_result or
player.cursor_stack and player.cursor_stack.valid_for_read and player.cursor_stack.prototype.place_result or nil
if not place_result or place_result.type ~= "pipe" then return end
local position = event.position
local mask = prototypes.entity[variations[place_result.name][0]].collision_mask.layers.object and "object" or "tomwub-underground"
local entity = player.surface.find_entities_filtered{
type = "pipe",
position = position,
force = player.force,
collision_mask = mask
}[1]
local ghost
for _, e in pairs(player.surface.find_entities_filtered{
type = "entity-ghost",
ghost_type = "pipe",
position = position,
force = player.force
}) do
if e.ghost_prototype.collision_mask.layers[mask] then
ghost = e
break
end
end
if entity or ghost then
storage.existing_connections[event.player_index] = entity and entity.name or ghost.ghost_name
local fluid = entity and entity.fluidbox[1]
local previous = storage.previous[event.player_index]
if fluid and previous.valid and previous.name ~= "entity-ghost" then
local old_fluid = previous and previous.valid and previous.fluidbox[1]
-- only check validity if we're attempting to mix fluids
if fluid and old_fluid and fluid.name ~= old_fluid.name then
local dx, dy = math.abs(entity.position.x - previous.position.x), math.abs(entity.position.y - previous.position.y)
local dist = (
math.ceil(perel.get_side_length(entity.name == "entity-ghost" and entity.ghost_prototype or entity.prototype)) +
math.ceil(perel.get_side_length(previous.name == "entity-ghost" and previous.ghost_prototype or previous.prototype))
) / 2
if dx ~= dy and math.max(dx, dy) == dist then
-- entities will be connected, so prevent this
player.create_local_flying_text{
text = {"action-leads-to-fluid-mixing"},
create_at_cursor = true
} -- notify
entity.surface.create_entity{
name = "parallel-piping-blockage",
position = entity.position
} -- block placement
storage.previous[event.player_index] = entity -- update last entity
return
end
end
end
if entity and event.build_mode == defines.build_mode.normal then
-- no mixing happening, update fluid count
if fluid then
local amount = entity.fluidbox.get_fluid_segment_contents(1)
fluid.amount = amount and amount[fluid.name] or fluid.amount
end
storage.old_fluid[event.player_index] = fluid
storage.old_health[event.player_index] = entity.health
entity.health = entity.max_health
end
end
if entity and (event.build_mode ~= defines.build_mode.normal or player.controller_type == defines.controllers.remote) then
-- mimic normal build event
storage.old_health[event.player_index] = entity and entity.health or nil
local fluid = entity.fluidbox[1]
if fluid then
local amount = entity.fluidbox.get_fluid_segment_contents(1)
fluid.amount = amount and amount[fluid.name] or fluid.amount
storage.old_fluid[event.player_index] = fluid
end
entity.health = entity.max_health
local event_data = event
event.entity = entity.surface.create_entity{
name = base_pipe[entity.name],
position = entity.position,
quality = entity.quality,
force = entity.force,
create_build_effect_smoke = false,
raise_built = true
}
entity.destroy();
on_built(event_data)
end
end)
--- @param event EventData.on_player_mined_entity|EventData.on_robot_mined_entity|EventData.on_space_platform_mined_entity|EventData.script_raised_destroy|EventData.on_entity_died
local function on_destroyed(event)
if storage.build_ticks[event.player_index] == event.tick then
storage.build_ticks[event.player_index] = nil
return -- early return for fast-replace events
end
-- something got removed, disconnect neighbours
local entity = event.entity
local player = event.player_index and game.get_player(event.player_index)
local surface = entity.surface
local stack = player and player.undo_redo_stack
local blueprint = stack and stack.get_undo_item_count() > 0 and #stack.get_undo_item(1) ~= 1
if blueprint then -- multiple items (blueprint or otherwise) do complicated checks
local i = perel.find_build_action(stack.get_undo_item(1), entity)
if i then stack.remove_undo_action(1, i) end
end
local fluid_target_thresholds = {}
---@cast fluid_target_thresholds Fluid[]
for i = 1, #entity.fluidbox do
local fluid = entity.fluidbox[i]
if fluid then
local amount = entity.fluidbox.get_fluid_segment_contents(i)
amount = amount and amount[fluid.name] or fluid.amount
local total_capacity = entity.fluidbox.get_capacity(i)
local this_capacity = entity.prototype.fluidbox_prototypes[i].volume
if total_capacity ~= this_capacity then
fluid.amount = amount / (total_capacity - this_capacity)
fluid_target_thresholds[i] = fluid
end
end
end
local fluid_targets = {}
---@cast fluid_targets {target: LuaFluidBox, target_fluidbox_index: int, target_pipe_connection_index: int}[][]
for i, fluidbox in pairs(perel.get_fluidbox_targets_by_fluidbox_and_connection(entity, true, true)) do
fluid_targets[i] = {}
for _, tuple in pairs(fluidbox) do
local neighbour = tuple.target.owner --[[@as LuaEntity]]
local mask = bitmasks[neighbour.name == "entity-ghost" and neighbour.ghost_name or neighbour.name]
local b2 = base_pipe[neighbour.name == "entity-ghost" and neighbour.ghost_name or neighbour.name]
local bit = 2 ^ (perel.get_direction(neighbour.position, entity.position) / 4)
fluid_targets[i][#fluid_targets[i]+1] = tuple
if mask and b2 and bit32.btest(mask, bit) and not neighbour.to_be_deconstructed() then
mask = mask - bit
local build_index, build_action = perel.find_build_item(stack, neighbour)
local health = neighbour.health
local marked = neighbour.to_be_deconstructed()
local new_neighbour = surface.create_entity{
name = neighbour.name == "entity-ghost" and "entity-ghost" or variations[b2][mask],
ghost_name = neighbour.name == "entity-ghost" and variations[b2][mask] or nil,
position = neighbour.position,
quality = neighbour.quality,
force = neighbour.force,
player = build_index and player.index or nil,
undo_index = build_index,
create_build_effect_smoke = false,
raise_built = true
}
neighbour.destroy()
if build_index then stack.remove_undo_action(build_index, build_action) end
if health then new_neighbour.health = health end
if marked then new_neighbour.order_deconstruction(new_neighbour.force) end
fluid_targets[i][#fluid_targets[i]] = {target = new_neighbour.fluidbox, target_fluidbox_index = 1} -- update fluid distribution target
end
end
end
-- evenly distribute fluid
for i, fluid in pairs(fluid_target_thresholds) do
local fill_percent = fluid.amount
for _, tuple in pairs(fluid_targets[i]) do
fluid.amount = fill_percent * tuple.target.get_capacity(tuple.target_fluidbox_index)
if fluid.amount > 0 then
tuple.target[tuple.target_fluidbox_index] = fluid
end
end
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_space_platform_mined_entity, on_destroyed)
script.on_event(defines.events.script_raised_destroy, on_destroyed)
script.on_event(defines.events.on_entity_died, on_destroyed)
script.on_event(defines.events.on_cancelled_deconstruction, function (event)
local entity = event.entity
local prototype = entity.name == "entity-ghost" and entity.ghost_prototype or entity.prototype
local base = base_pipe[prototype.name]
if not base then return end
local mask = bitmasks[prototype.name]
local new_mask = 0
local player = game.get_player(event.player_index)
local stack = player.undo_redo_stack
local surface = entity.surface
for _, neighbour in pairs(perel.get_fluidbox_neighoburs(entity)) do
new_mask = new_mask + 2 ^ (perel.get_direction(entity.position, neighbour.position) / 4)
end
if mask == new_mask then return end
-- something was removed, replace this entity
local build_index, build_action = perel.find_build_item(stack, entity)
local health = entity.health
local fluid = entity.fluidbox[1]
if fluid then
local amount = entity.fluidbox.get_fluid_segment_contents(1)
fluid.amount = amount and amount[fluid.name] or fluid.amount
end
local params = {
name = entity.name == "entity-ghost" and "entity-ghost" or variations[base][mask],
ghost_name = entity.name == "entity-ghost" and variations[base][mask] or nil,
position = entity.position,
quality = entity.quality,
force = entity.force,
player = build_index and player.index or nil,
undo_index = build_index,
create_build_effect_smoke = false,
raise_built = true
}
entity.destroy()
local new_entity = surface.create_entity(params)
if build_index then stack.remove_undo_action(build_index, build_action) end
if health then new_entity.health = health end
if fluid then new_entity.fluidbox[1] = fluid end
end)
script.on_event(defines.events.on_player_setup_blueprint, function (event)
local player = game.get_player(event.player_index)
local blueprint = player.blueprint_to_setup
-- if normally invalid
if not blueprint or not blueprint.valid_for_read then blueprint = player.cursor_stack end
-- if non existant, cancel
local entities = blueprint and blueprint.get_blueprint_entities()
if not entities then return end
local changed = false
-- update entities
for _, entity in pairs(entities) do
if base_pipe[entity.name] then
changed = true
local variation = pipe_to_tank[bitmasks[entity.name]]
entity.name = variations[base_pipe[entity.name]][variation.mask]
entity.direction = variation.direction
end
end
if not changed then return end -- make no changes unless required
blueprint.set_blueprint_entities(entities)
end)