Skip to content

Commit 757915e

Browse files
authored
Krastorio compatibility and bugfix (#20)
* Nico's code improvements Make fluid direction finding code more compact and readable. * Further improve pipe building logic * Fixed pipes not building correctly to rectangular neighbors by detecting adjacent edges. * Fixed pipes being build in a grid if connector is offset. Will now build an L-shape if the miner's connector is offset in both X and Y. * Added more debug messages about pipes. * Whitespace * Add settings to specify what pipes to use - Add setting for what pipe to replace fluid-using miners with. - Add second setting for what pipe to use in space zones. - Validate that settings specify an existing pipe entity names after setting is changed, mods are changed, and when deconstructions are ordered. * Version 0.3.1 + Compatibility: - Added mod settings to customize what pipe is used when replacing miners (i.e. kr-steel-pipe). Bugfixes: - Fixed crashes when Editor Extensions "instant deconstruction" is enabled.
1 parent ff1c475 commit 757915e

File tree

7 files changed

+99
-33
lines changed

7 files changed

+99
-33
lines changed

autodeconstruct.lua

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ end
5555

5656
local function find_target(entity)
5757
if entity.drop_target then
58+
if global.debug then msg_all({"autodeconstruct-debug", "found " .. entity.drop_target.name .. " at " .. util.positiontostr(entity.drop_target.position)}) end
5859
return entity.drop_target
5960
else
6061
local entities = entity.surface.find_entities_filtered{position=entity.drop_position}
@@ -133,6 +134,10 @@ local function debug_message_with_position(entity, msg)
133134
msg_all({"autodeconstruct-debug", util.positiontostr(entity.position) .. " " .. entity.name .. " " .. msg})
134135
end
135136

137+
function autodeconstruct.is_valid_pipe(name)
138+
return game.entity_prototypes[name] and game.entity_prototypes[name].type == "pipe"
139+
end
140+
136141
function autodeconstruct.init_globals()
137142
-- Find largest-range miner in the game
138143
global.max_radius = 0.99
@@ -208,11 +213,15 @@ function autodeconstruct.deconstruct_target(drill)
208213
if target.to_be_deconstructed(target.force) then
209214
target.cancel_deconstruction(target.force)
210215
end
211-
216+
local ent_dat = {name=target.name, position=target.position}
212217
if target.order_deconstruction(target.force, target.last_user) then
213-
debug_message_with_position(target, "marked for deconstruction")
218+
if target and target.valid then
219+
debug_message_with_position(target, "marked for deconstruction")
220+
else
221+
debug_message_with_position(ent_dat, "instantly deconstructed")
222+
end
214223
else
215-
msg_all({"autodeconstruct-err-specific", "target.order_deconstruction", util.positiontostr(target.position) .. " failed to order deconstruction on " .. target.name})
224+
msg_all({"autodeconstruct-err-specific", "target.order_deconstruction", util.positiontostr(ent_dat.position) .. " failed to order deconstruction on " .. ent_dat.name})
216225
end
217226
end
218227
end
@@ -270,7 +279,7 @@ local function snap_box_to_grid(box)
270279
return box
271280
end
272281

273-
function autodeconstruct.build_pipes(drill)
282+
function autodeconstruct.build_pipes(drill, pipeType)
274283
-- future improvement: a mod setting for the pipeType to allow modded pipes
275284
local drillData = {
276285
position = {
@@ -283,16 +292,6 @@ function autodeconstruct.build_pipes(drill)
283292
surface = drill.surface
284293
}
285294

286-
--Space Exploration Compatibility check for space-surfaces
287-
local pipeType = "pipe"
288-
if game.active_mods["space-exploration"] then
289-
local se_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = drillData.surface.index})
290-
local is_space = remote.call("space-exploration", "get_zone_is_space", {zone_index = se_zone.index})
291-
if is_space then
292-
pipeType = "se-space-pipe"
293-
end
294-
end
295-
296295
-- Drills only have one fluidbox, get the first
297296
local connected_fluidboxes = drill.fluidbox.get_connections(1)
298297

@@ -355,11 +354,33 @@ function autodeconstruct.order_deconstruction(drill)
355354
return
356355
end
357356
local has_fluid = false
357+
local pipeType = nil
358358
if drill.fluidbox and #drill.fluidbox > 0 then
359359
has_fluid = true
360360
if not settings.global['autodeconstruct-remove-fluid-drills'].value then
361361
debug_message_with_position(drill, "has a non-empty fluidbox and fluid deconstruction is not enabled, skipping")
362362

363+
return
364+
end
365+
--Space Exploration Compatibility check for space-surfaces
366+
-- Select the pipe to use for replacements
367+
pipeType = settings.global['autodeconstruct-pipe-name'].value
368+
local is_space = false
369+
if game.active_mods["space-exploration"] then
370+
local se_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = drill.surface.index})
371+
is_space = remote.call("space-exploration", "get_zone_is_space", {zone_index = se_zone.index})
372+
if is_space then
373+
pipeType = settings.global['autodeconstruct-space-pipe-name'].value
374+
end
375+
end
376+
377+
if not autodeconstruct.is_valid_pipe(pipeType) then
378+
if is_space then
379+
debug_message_with_position(drill, "can't find space pipe named '"..pipeType.."' to infill depleted fluid miner in space.")
380+
else
381+
debug_message_with_position(drill, "can't find pipe named '"..pipeType.."' to infill depleted fluid miner.")
382+
end
383+
363384
return
364385
end
365386
end
@@ -399,24 +420,29 @@ function autodeconstruct.order_deconstruction(drill)
399420
if settings.global['autodeconstruct-remove-target'].value then
400421
autodeconstruct.deconstruct_target(drill)
401422
end
402-
423+
424+
local ent_dat = {name=drill.name, position=drill.position}
403425
if drill.order_deconstruction(drill.force, drill.last_user) then
404-
debug_message_with_position(drill, "marked for deconstruction")
405-
-- Handle pipes
406-
if has_fluid and settings.global['autodeconstruct-build-pipes'].value then
407-
if #drill.fluidbox.get_connections(1) > 1 then
408-
debug_message_with_position(drill, "adding pipe blueprints")
409-
autodeconstruct.build_pipes(drill)
410-
else
411-
debug_message_with_position(drill, "skipping pipe blueprints, only one connection")
426+
if drill and drill.valid then
427+
debug_message_with_position(drill, "marked for deconstruction")
428+
-- Handle pipes
429+
if has_fluid and settings.global['autodeconstruct-build-pipes'].value then
430+
if #drill.fluidbox.get_connections(1) > 1 then
431+
debug_message_with_position(drill, "adding pipe blueprints")
432+
autodeconstruct.build_pipes(drill, pipeType)
433+
else
434+
debug_message_with_position(drill, "skipping pipe blueprints, only one connection")
435+
end
412436
end
413-
end
414-
-- Check for inserters providing fuel to this miner
415-
if drill.burner then
416-
local targeting = find_targeting(drill, {'inserter'})
417-
for _,e in pairs(targeting) do
418-
e.order_deconstruction(e.force, e.last_user)
437+
-- Check for inserters providing fuel to this miner
438+
if drill.valid and drill.burner then
439+
local targeting = find_targeting(drill, {'inserter'})
440+
for _,e in pairs(targeting) do
441+
e.order_deconstruction(e.force, e.last_user)
442+
end
419443
end
444+
else
445+
msg_all({"autodeconstruct-err-specific", "drill.order_deconstruction", util.positiontostr(ent_dat.position) .. " " .. ent_dat.name .. " instantly deconstructed, nothing else done" })
420446
end
421447
else
422448
msg_all({"autodeconstruct-err-specific", "drill.order_deconstruction", util.positiontostr(drill.position) .. " " .. drill.name .. " failed to order deconstruction" })

changelog.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
---------------------------------------------------------------------------------------------------
2+
Version: 0.3.1
3+
Date: 2022-03-16
4+
Compatibility:
5+
- Added mod settings to customize what pipe is used when replacing miners (i.e. kr-steel-pipe).
6+
Bugfixes:
7+
- Fixed crashes when Editor Extensions "instant deconstruction" is enabled.
8+
---------------------------------------------------------------------------------------------------
29
Version: 0.3.0
310
Date: 2021-11-21
411
New:

control.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,30 @@ script.on_init(function()
2525
end)
2626

2727
script.on_configuration_changed(function()
28+
if not autodeconstruct.is_valid_pipe(settings.global["autodeconstruct-pipe-name"].value) then
29+
msg_all({"autodeconstruct-err-pipe-name", settings.global["autodeconstruct-pipe-name"].value})
30+
end
31+
if game.active_mods["space-exploration"] and
32+
not autodeconstruct.is_valid_pipe(settings.global["autodeconstruct-space-pipe-name"].value) then
33+
msg_all({"autodeconstruct-err-pipe-name", settings.global["autodeconstruct-space-pipe-name"].value})
34+
end
2835
local _, err = pcall(autodeconstruct.init_globals)
2936
if err then msg_all({"autodeconstruct-err-generic", err}) end
3037
end)
3138

3239
script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
33-
if (event.setting == "autodeconstruct-remove-fluid-drills" and
40+
if (event.setting == "autodeconstruct-pipe-name" or event.setting == "autodeconstruct-space-pipe-name") then
41+
if not autodeconstruct.is_valid_pipe(settings.global[event.setting].value) then
42+
msg_all({"autodeconstruct-err-pipe-name", settings.global[event.setting].value})
43+
end
44+
elseif (event.setting == "autodeconstruct-remove-fluid-drills" and
3445
settings.global['autodeconstruct-remove-fluid-drills'].value == true) then
3546
local _, err = pcall(autodeconstruct.init_globals)
3647
if err then msg_all({"autodeconstruct-err-generic", err}) end
3748
end
3849
end)
3950

40-
script.on_event(defines.events.on_cancelled_deconstruction,
51+
script.on_event(defines.events.on_cancelled_deconstruction,
4152
function(event)
4253
local _, err = pcall(autodeconstruct.on_cancelled_deconstruction, event)
4354
if err then msg_all({"autodeconstruct-err-specific", "on_cancelled_deconstruction", err}) end

info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "AutoDeconstruct",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"factorio_version":"1.1",
55
"title": "Auto Deconstruct",
66
"author": "mindmix",

locale/en/base.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
autodeconstruct-err-generic=[autodeconstruct] Error: __1__
22
autodeconstruct-err-specific=[autodeconstruct|__1__] Error: __2__
3+
autodeconstruct-err-pipe-name=[autodeconstruct] Error: Invalid setting. Cannot find pipe with name "__1__".
34

45
autodeconstruct-notify=[autodeconstruct] Notify: __1__
56
autodeconstruct-debug=[autodeconstruct.__1__] Debug: __2__
67

78
[mod-setting-name]
89
autodeconstruct-remove-target=Also mark output chests for deconstruction
910
autodeconstruct-remove-fluid-drills=Also remove drills that are using fluids
10-
autodeconstruct-build-pipes=Create pipes when removing drills that are using fluids
11+
autodeconstruct-build-pipes=Create pipes when removing drills that are using fluids
12+
autodeconstruct-pipe-name=Type of pipe to build when removing drills
13+
autodeconstruct-space-pipe-name=Type of pipe to build when removing drills in space

settings-final-fixes.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Unhide space pipe setting if space-exploration is loaded
2+
if mods["space-exploration"] then
3+
data.raw["string-setting"]["autodeconstruct-space-pipe-name"].hidden = false
4+
end

settings.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ data:extend({
2020
default_value = true,
2121
order = "ad-c",
2222
},
23+
{
24+
type = "string-setting",
25+
name = "autodeconstruct-pipe-name",
26+
setting_type = "runtime-global",
27+
default_value = "pipe",
28+
order = "ad-d",
29+
},
30+
{
31+
type = "string-setting",
32+
name = "autodeconstruct-space-pipe-name",
33+
setting_type = "runtime-global",
34+
default_value = "se-space-pipe",
35+
hidden = true,
36+
order = "ad-e",
37+
}
2338
})

0 commit comments

Comments
 (0)