Commit 1d856572 authored by Rob Nelson's avatar Rob Nelson
Browse files

Updates

parent 3ced4f2f
require "util"
require "config"
local function find_resources(surface, position, range, resource_category)
local resource_category = resource_category or 'basic-solid'
local top_left = {x = position.x - range, y = position.y - range}
local bottom_right = {x = position.x + range, y = position.y + range}
local resources = surface.find_entities_filtered{area={top_left, bottom_right}, type='resource'}
categorized = {}
for _, resource in pairs(resources) do
if resource.prototype.resource_category == resource_category then
table.insert(categorized, resource)
end
end
return categorized
end
local function find_all_entities(entity_type)
local surface = game.surfaces['nauvis']
local entities = {}
for chunk in surface.get_chunks() do
local chunk_area = {lefttop = {x = chunk.x*32, y = chunk.y*32}, rightbottom = {x = chunk.x*32+32, y = chunk.y*32+32}}
local chunk_entities = surface.find_entities_filtered({area = chunk_area, type = entity_type})
for i = 1, #chunk_entities do
entities[#entities + 1] = chunk_entities[i]
end
end
return entities
end
local function find_target(entity)
if entity.drop_target then
return entity.drop_target
else
local entities = entity.surface.find_entities_filtered{position=entity.drop_position}
if global.debug then msg_all({"autodeconstruct-debug", "found " .. entities[1].name .. " at " .. util.positiontostr(entities[1].position)}) end
return entities[1]
end
end
local function find_targeting(entity)
local range = global.max_radius
local position = entity.position
local top_left = {x = position.x - range, y = position.y - range}
local bottom_right = {x = position.x + range, y = position.y + range}
local surface = entity.surface
local entities = {}
local targeting = {}
local entities = surface.find_entities_filtered{area={top_left, bottom_right}, type='mining-drill'}
for i = 1, #entities do
if find_target(entities[i]) == entity then
targeting[#targeting + 1] = entities[i]
end
end
entities = surface.find_entities_filtered{area={top_left, bottom_right}, type='inserter'}
for i = 1, #entities do
if find_target(entities[i]) == entity then
targeting[#targeting + 1] = entities[i]
end
end
if global.debug then msg_all({"autodeconstruct-debug", "found " .. #targeting .. " targeting"}) end
return targeting
end
local function find_drills(entity)
local position = entity.position
local surface = entity.surface
local top_left = {x = position.x - global.max_radius, y = position.y - global.max_radius}
local bottom_right = {x = position.x + global.max_radius, y = position.y + global.max_radius}
local entities = {}
local targeting = {}
local entities = surface.find_entities_filtered{area={top_left, bottom_right}, type='mining-drill'}
if global.debug then msg_all({"autodeconstruct-debug", "found " .. #entities .. " drills"}) end
for i = 1, #entities do
if math.abs(entities[i].position.x - position.x) < entities[i].prototype.mining_drill_radius and math.abs(entities[i].position.y - position.y) < entities[i].prototype.mining_drill_radius then
autodeconstruct.check_drill(entities[i])
end
end
end
function autodeconstruct.init_globals()
global.max_radius = 0.99
drill_entities = find_all_entities('mining-drill')
for _, drill_entity in pairs(drill_entities) do
autodeconstruct.check_drill(drill_entity)
end
end
function autodeconstruct.on_resource_depleted(event)
if event.entity.prototype.resource_category ~= 'basic-solid' or event.entity.prototype.infinite_resource ~= false then
if global.debug then msg_all({"autodeconstruct-debug", "on_resource_depleted", game.tick .. " amount " .. event.entity.amount .. " resource_category " .. event.entity.prototype.resource_category .. " infinite_resource " .. (event.entity.prototype.infinite_resource == true and "true" or "false" )}) end
return
end
drill = find_drills(event.entity)
end
function autodeconstruct.check_drill(drill)
if drill.mining_target ~= nil and drill.mining_target.valid then
if drill.mining_target.amount > 0 then return end -- this should also filter out pumpjacks and infinite resources
end
local mining_drill_radius = drill.prototype.mining_drill_radius
if mining_drill_radius > global.max_radius then
global.max_radius = mining_drill_radius
end
if mining_drill_radius == nil then return end
resources = find_resources(drill.surface, drill.position, mining_drill_radius)
for i = 1, #resources do
if resources[i].amount > 0 then return end
end
if global.debug then msg_all({"autodeconstruct-debug", util.positiontostr(drill.position) .. " found no resources, deconstructing"}) end
autodeconstruct.order_deconstruction(drill)
end
function autodeconstruct.on_canceled_deconstruction(event)
if event.player_index ~= nil or event.entity.type ~= 'mining-drill' then return end
if global.debug then msg_all({"autodeconstruct-debug", "on_canceled_deconstruction", util.positiontostr(event.entity.position) .. " deconstruction timed out, checking again"}) end
autodeconstruct.check_drill(event.entity)
end
function autodeconstruct.on_built_entity(event)
if event.created_entity.type ~= 'mining-drill' then return end
if event.created_entity.prototype.mining_drill_radius > global.max_radius then
global.max_radius = event.created_entity.prototype.mining_drill_radius
if global.debug then msg_all({"autodeconstruct-debug", "on_built_entity", "global.max_radius updated to " .. global.max_radius}) end
end
end
function autodeconstruct.order_deconstruction(drill)
if drill.to_be_deconstructed(drill.force) then
if global.debug then msg_all({"autodeconstruct-debug", util.positiontostr(drill.position) .. " already marked"}) end
return
end
local deconstruct = false
--[[ #TODO
config.lua: autodeconstruct.wait_for_robots = false
if autodeconstruct.wait_for_robots then
logistic_network = drill.surface.find_logistic_network_by_position(drill.position, drill.force.name)
if logistic_network ~= nil then
if logistic_network.available_construction_robots > 0 then
deconstruct = true
end
end
else
deconstruct = true
end
--]]
deconstruct = true
--[[ END TODO
--]]
if drill.fluidbox and #drill.fluidbox > 0 then
deconstruct = false
end
if deconstruct == true and drill.minable and drill.has_flag("not-deconstructable") == false then
if drill.order_deconstruction(drill.force) then
if global.debug then msg_all({"autodeconstruct-debug", util.positiontostr(drill.position) .. " " .. drill.name .. " success"}) end
else
msg_all({"autodeconstruct-err-specific", "drill.order_deconstruction", util.positiontostr(drill.position) .. " failed to order deconstruction on " .. drill.name })
end
if autodeconstruct.remove_target then
target = find_target(drill)
if target ~= nil and target.minable then
if target.type == "logistic-container" or target.type == "container" then
targeting = find_targeting(target)
if targeting ~= nil then
for i = 1, #targeting do
if not targeting[i].to_be_deconstructed(targeting[i].force) then return end
end
-- we are the only one targeting
if target.to_be_deconstructed(target.force) then
target.cancel_deconstruction(target.force)
end
if target.order_deconstruction(target.force) then
if global.debug then msg_all({"autodeconstruct-debug", util.positiontostr(target.position) .. " " .. target.name .. " success"}) end
else
msg_all({"autodeconstruct-err-specific", "target.order_deconstruction", util.positiontostr(target.position) .. " failed to order deconstruction on " .. target.name})
end
end
end
--[[ #TODO
if target.type == "transport-belt" then
-- find entities with this belt as target
end
--]]
end
end
end
end
if not autodeconstruct then autodeconstruct = {} end
autodeconstruct.remove_target = true
--[[
To toggle debug messages
/c remote.call("ad", "debug")
To call init
/c remote.call("ad", "init")
--]]
require "autodeconstruct"
function msg_all(message)
if message[1] == "autodeconstruct-debug" then
table.insert(message, 2, debug.getinfo(2).name)
end
for _,p in pairs(game.players) do
p.print(message)
end
end
global.debug = false
remote.add_interface("ad", {
debug = function()
global.debug = not global.debug
end,
init = function()
autodeconstruct.init_globals()
end
})
script.on_init(function()
local _, err = pcall(autodeconstruct.init_globals)
if err then msg_all({"autodeconstruct-err-generic", err}) end
end)
script.on_configuration_changed(function()
local _, err = pcall(autodeconstruct.init_globals)
if err then msg_all({"autodeconstruct-err-generic", err}) end
end)
script.on_event(defines.events.on_canceled_deconstruction, function(event)
local _, err = pcall(autodeconstruct.on_canceled_deconstruction, event)
if err then msg_all({"autodeconstruct-err-specific", "on_canceled_deconstruction", err}) end
end)
script.on_event(defines.events.on_resource_depleted, function(event)
local _, err = pcall(autodeconstruct.on_resource_depleted, event)
if err then msg_all({"autodeconstruct-err-specific", "on_resource_depleted", err}) end
end)
script.on_event(defines.events.on_robot_built_entity, function(event)
local _, err = pcall(autodeconstruct.on_built_entity, event)
if err then msg_all({"autodeconstruct-err-specific", "on_robot_built_entity", err}) end
end)
script.on_event(defines.events.on_built_entity, function(event)
local _, err = pcall(autodeconstruct.on_built_entity, event)
if err then msg_all({"autodeconstruct-err-specific", "on_built_entity", err}) end
end)
\ No newline at end of file
{
"name": "AutoDeconstruct",
"version": "0.1.8",
"factorio_version":"0.15",
"title": "Auto Deconstruct",
"author": "mindmix",
"homepage": "",
"description": "This mod marks drills that have no more resources to mine for deconstruction."
}
\ No newline at end of file
autodeconstruct-err-generic=[autodeconstruct] Error: __1__
autodeconstruct-err-specific=[autodeconstruct|__1__] Error: __2__
autodeconstruct-notify=[autodeconstruct] Notify: __1__
autodeconstruct-debug=[autodeconstruct.__1__] Debug: __2__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment