Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 55 additions & 29 deletions autodeconstruct.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" })
Expand Down
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
15 changes: 13 additions & 2 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AutoDeconstruct",
"version": "0.3.0",
"version": "0.3.1",
"factorio_version":"1.1",
"title": "Auto Deconstruct",
"author": "mindmix",
Expand Down
5 changes: 4 additions & 1 deletion locale/en/base.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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__

[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
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
4 changes: 4 additions & 0 deletions settings-final-fixes.lua
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
})