From 757915e4237e8a1c4b4be40303166b7bda3befbd Mon Sep 17 00:00:00 2001 From: robot256 Date: Thu, 17 Mar 2022 02:21:51 -0400 Subject: [PATCH] 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. --- autodeconstruct.lua | 84 ++++++++++++++++++++++++++-------------- changelog.txt | 7 ++++ control.lua | 15 ++++++- info.json | 2 +- locale/en/base.cfg | 5 ++- settings-final-fixes.lua | 4 ++ settings.lua | 15 +++++++ 7 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 settings-final-fixes.lua diff --git a/autodeconstruct.lua b/autodeconstruct.lua index 79ca98f..8ba70d5 100644 --- a/autodeconstruct.lua +++ b/autodeconstruct.lua @@ -55,6 +55,7 @@ end local function find_target(entity) if entity.drop_target then + if global.debug then msg_all({"autodeconstruct-debug", "found " .. entity.drop_target.name .. " at " .. util.positiontostr(entity.drop_target.position)}) end return entity.drop_target else local entities = entity.surface.find_entities_filtered{position=entity.drop_position} @@ -133,6 +134,10 @@ local function debug_message_with_position(entity, msg) msg_all({"autodeconstruct-debug", util.positiontostr(entity.position) .. " " .. entity.name .. " " .. msg}) end +function autodeconstruct.is_valid_pipe(name) + return game.entity_prototypes[name] and game.entity_prototypes[name].type == "pipe" +end + function autodeconstruct.init_globals() -- Find largest-range miner in the game global.max_radius = 0.99 @@ -208,11 +213,15 @@ function autodeconstruct.deconstruct_target(drill) if target.to_be_deconstructed(target.force) then target.cancel_deconstruction(target.force) end - + local ent_dat = {name=target.name, position=target.position} if target.order_deconstruction(target.force, target.last_user) then - debug_message_with_position(target, "marked for deconstruction") + if target and target.valid then + debug_message_with_position(target, "marked for deconstruction") + else + debug_message_with_position(ent_dat, "instantly deconstructed") + end else - msg_all({"autodeconstruct-err-specific", "target.order_deconstruction", util.positiontostr(target.position) .. " failed to order deconstruction on " .. target.name}) + msg_all({"autodeconstruct-err-specific", "target.order_deconstruction", util.positiontostr(ent_dat.position) .. " failed to order deconstruction on " .. ent_dat.name}) end end end @@ -270,7 +279,7 @@ local function snap_box_to_grid(box) return box end -function autodeconstruct.build_pipes(drill) +function autodeconstruct.build_pipes(drill, pipeType) -- future improvement: a mod setting for the pipeType to allow modded pipes local drillData = { position = { @@ -283,16 +292,6 @@ function autodeconstruct.build_pipes(drill) surface = drill.surface } - --Space Exploration Compatibility check for space-surfaces - local pipeType = "pipe" - if game.active_mods["space-exploration"] then - local se_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = drillData.surface.index}) - local is_space = remote.call("space-exploration", "get_zone_is_space", {zone_index = se_zone.index}) - if is_space then - pipeType = "se-space-pipe" - end - end - -- Drills only have one fluidbox, get the first local connected_fluidboxes = drill.fluidbox.get_connections(1) @@ -355,11 +354,33 @@ function autodeconstruct.order_deconstruction(drill) return end local has_fluid = false + local pipeType = nil if drill.fluidbox and #drill.fluidbox > 0 then has_fluid = true if not settings.global['autodeconstruct-remove-fluid-drills'].value then debug_message_with_position(drill, "has a non-empty fluidbox and fluid deconstruction is not enabled, skipping") + return + end + --Space Exploration Compatibility check for space-surfaces + -- Select the pipe to use for replacements + pipeType = settings.global['autodeconstruct-pipe-name'].value + local is_space = false + if game.active_mods["space-exploration"] then + local se_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = drill.surface.index}) + is_space = remote.call("space-exploration", "get_zone_is_space", {zone_index = se_zone.index}) + if is_space then + pipeType = settings.global['autodeconstruct-space-pipe-name'].value + end + end + + if not autodeconstruct.is_valid_pipe(pipeType) then + if is_space then + debug_message_with_position(drill, "can't find space pipe named '"..pipeType.."' to infill depleted fluid miner in space.") + else + debug_message_with_position(drill, "can't find pipe named '"..pipeType.."' to infill depleted fluid miner.") + end + return end end @@ -399,24 +420,29 @@ function autodeconstruct.order_deconstruction(drill) if settings.global['autodeconstruct-remove-target'].value then autodeconstruct.deconstruct_target(drill) end - + + local ent_dat = {name=drill.name, position=drill.position} if drill.order_deconstruction(drill.force, drill.last_user) then - debug_message_with_position(drill, "marked for deconstruction") - -- Handle pipes - if has_fluid and settings.global['autodeconstruct-build-pipes'].value then - if #drill.fluidbox.get_connections(1) > 1 then - debug_message_with_position(drill, "adding pipe blueprints") - autodeconstruct.build_pipes(drill) - else - debug_message_with_position(drill, "skipping pipe blueprints, only one connection") + if drill and drill.valid then + debug_message_with_position(drill, "marked for deconstruction") + -- Handle pipes + if has_fluid and settings.global['autodeconstruct-build-pipes'].value then + if #drill.fluidbox.get_connections(1) > 1 then + debug_message_with_position(drill, "adding pipe blueprints") + autodeconstruct.build_pipes(drill, pipeType) + else + debug_message_with_position(drill, "skipping pipe blueprints, only one connection") + end end - end - -- Check for inserters providing fuel to this miner - if drill.burner then - local targeting = find_targeting(drill, {'inserter'}) - for _,e in pairs(targeting) do - e.order_deconstruction(e.force, e.last_user) + -- Check for inserters providing fuel to this miner + if drill.valid and drill.burner then + local targeting = find_targeting(drill, {'inserter'}) + for _,e in pairs(targeting) do + e.order_deconstruction(e.force, e.last_user) + end end + else + msg_all({"autodeconstruct-err-specific", "drill.order_deconstruction", util.positiontostr(ent_dat.position) .. " " .. ent_dat.name .. " instantly deconstructed, nothing else done" }) end else msg_all({"autodeconstruct-err-specific", "drill.order_deconstruction", util.positiontostr(drill.position) .. " " .. drill.name .. " failed to order deconstruction" }) diff --git a/changelog.txt b/changelog.txt index 195f3d5..587e5f4 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,11 @@ --------------------------------------------------------------------------------------------------- +Version: 0.3.1 +Date: 2022-03-16 + 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. +--------------------------------------------------------------------------------------------------- Version: 0.3.0 Date: 2021-11-21 New: diff --git a/control.lua b/control.lua index bc77cdf..ff818f9 100644 --- a/control.lua +++ b/control.lua @@ -25,19 +25,30 @@ script.on_init(function() end) script.on_configuration_changed(function() + if not autodeconstruct.is_valid_pipe(settings.global["autodeconstruct-pipe-name"].value) then + msg_all({"autodeconstruct-err-pipe-name", settings.global["autodeconstruct-pipe-name"].value}) + end + if game.active_mods["space-exploration"] and + not autodeconstruct.is_valid_pipe(settings.global["autodeconstruct-space-pipe-name"].value) then + msg_all({"autodeconstruct-err-pipe-name", settings.global["autodeconstruct-space-pipe-name"].value}) + end local _, err = pcall(autodeconstruct.init_globals) if err then msg_all({"autodeconstruct-err-generic", err}) end end) script.on_event(defines.events.on_runtime_mod_setting_changed, function(event) - if (event.setting == "autodeconstruct-remove-fluid-drills" and + if (event.setting == "autodeconstruct-pipe-name" or event.setting == "autodeconstruct-space-pipe-name") then + if not autodeconstruct.is_valid_pipe(settings.global[event.setting].value) then + msg_all({"autodeconstruct-err-pipe-name", settings.global[event.setting].value}) + end + elseif (event.setting == "autodeconstruct-remove-fluid-drills" and settings.global['autodeconstruct-remove-fluid-drills'].value == true) then local _, err = pcall(autodeconstruct.init_globals) if err then msg_all({"autodeconstruct-err-generic", err}) end end end) -script.on_event(defines.events.on_cancelled_deconstruction, +script.on_event(defines.events.on_cancelled_deconstruction, function(event) local _, err = pcall(autodeconstruct.on_cancelled_deconstruction, event) if err then msg_all({"autodeconstruct-err-specific", "on_cancelled_deconstruction", err}) end diff --git a/info.json b/info.json index aaf8d58..f957a2a 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "AutoDeconstruct", -"version": "0.3.0", +"version": "0.3.1", "factorio_version":"1.1", "title": "Auto Deconstruct", "author": "mindmix", diff --git a/locale/en/base.cfg b/locale/en/base.cfg index a7b8727..123a359 100644 --- a/locale/en/base.cfg +++ b/locale/en/base.cfg @@ -1,5 +1,6 @@ autodeconstruct-err-generic=[autodeconstruct] Error: __1__ autodeconstruct-err-specific=[autodeconstruct|__1__] Error: __2__ +autodeconstruct-err-pipe-name=[autodeconstruct] Error: Invalid setting. Cannot find pipe with name "__1__". autodeconstruct-notify=[autodeconstruct] Notify: __1__ autodeconstruct-debug=[autodeconstruct.__1__] Debug: __2__ @@ -7,4 +8,6 @@ autodeconstruct-debug=[autodeconstruct.__1__] Debug: __2__ [mod-setting-name] autodeconstruct-remove-target=Also mark output chests for deconstruction autodeconstruct-remove-fluid-drills=Also remove drills that are using fluids -autodeconstruct-build-pipes=Create pipes when removing drills that are using fluids \ No newline at end of file +autodeconstruct-build-pipes=Create pipes when removing drills that are using fluids +autodeconstruct-pipe-name=Type of pipe to build when removing drills +autodeconstruct-space-pipe-name=Type of pipe to build when removing drills in space diff --git a/settings-final-fixes.lua b/settings-final-fixes.lua new file mode 100644 index 0000000..e314172 --- /dev/null +++ b/settings-final-fixes.lua @@ -0,0 +1,4 @@ +-- Unhide space pipe setting if space-exploration is loaded +if mods["space-exploration"] then + data.raw["string-setting"]["autodeconstruct-space-pipe-name"].hidden = false +end diff --git a/settings.lua b/settings.lua index 65622f4..ebd3f4d 100644 --- a/settings.lua +++ b/settings.lua @@ -20,4 +20,19 @@ data:extend({ default_value = true, order = "ad-c", }, + { + type = "string-setting", + name = "autodeconstruct-pipe-name", + setting_type = "runtime-global", + default_value = "pipe", + order = "ad-d", + }, + { + type = "string-setting", + name = "autodeconstruct-space-pipe-name", + setting_type = "runtime-global", + default_value = "se-space-pipe", + hidden = true, + order = "ad-e", + } })