2017-10-01 15:58:10 +02:00
|
|
|
--Author: Valansch
|
2018-04-06 21:58:50 +02:00
|
|
|
|
2018-07-19 14:28:00 +02:00
|
|
|
local Event = require 'utils.event'
|
2018-11-26 03:07:03 +02:00
|
|
|
local Token = require 'utils.token'
|
2019-01-04 22:02:55 +02:00
|
|
|
local Task = require 'utils.task'
|
2019-05-13 12:08:57 +02:00
|
|
|
local Global = require 'utils.global'
|
|
|
|
local table = require 'utils.table'
|
2018-04-06 21:58:50 +02:00
|
|
|
|
2019-05-13 12:08:57 +02:00
|
|
|
local drill_radius_map = {}
|
|
|
|
local max_radius = 0
|
|
|
|
local require_fluid_ores = {}
|
|
|
|
local pumpjack_resources_map = {}
|
|
|
|
local drill_names = {}
|
|
|
|
|
|
|
|
Global.register_init(
|
|
|
|
{
|
|
|
|
drill_radius_map = drill_radius_map,
|
|
|
|
require_fluid_ores = require_fluid_ores,
|
|
|
|
pumpjack_resources_map = pumpjack_resources_map
|
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
local map = tbl.drill_radius_map
|
|
|
|
local max = 0
|
|
|
|
local fluid_ores = tbl.require_fluid_ores
|
|
|
|
local pumpjack_map = tbl.pumpjack_resources_map
|
|
|
|
|
2024-10-22 21:22:35 +02:00
|
|
|
for name, entity in pairs(prototypes.entity) do
|
2019-05-13 12:08:57 +02:00
|
|
|
if entity.type == 'mining-drill' and entity.resource_categories['basic-solid'] then
|
|
|
|
local radius = entity.mining_drill_radius
|
|
|
|
|
|
|
|
map[name] = radius
|
|
|
|
|
|
|
|
if radius > max then
|
|
|
|
max = radius
|
|
|
|
end
|
|
|
|
elseif entity.type == 'resource' then
|
|
|
|
local props = entity.mineable_properties
|
|
|
|
|
|
|
|
if props.required_fluid then
|
|
|
|
fluid_ores[name] = true
|
|
|
|
end
|
|
|
|
|
2023-01-15 14:34:00 +02:00
|
|
|
local products = props.products or {}
|
2019-05-13 12:08:57 +02:00
|
|
|
for i = 1, #products do
|
|
|
|
local product = products[i]
|
|
|
|
if product.type == 'fluid' then
|
|
|
|
pumpjack_map[name] = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
tbl.max_radius = max
|
|
|
|
end,
|
|
|
|
function(tbl)
|
|
|
|
drill_radius_map = tbl.drill_radius_map
|
|
|
|
max_radius = tbl.max_radius
|
|
|
|
require_fluid_ores = tbl.require_fluid_ores
|
|
|
|
pumpjack_resources_map = tbl.pumpjack_resources_map
|
|
|
|
|
|
|
|
drill_names = table.keys(drill_radius_map)
|
|
|
|
end
|
|
|
|
)
|
2019-05-13 01:30:51 +02:00
|
|
|
|
2018-07-19 14:28:00 +02:00
|
|
|
local function is_depleted(drill, entity)
|
2019-05-13 12:08:57 +02:00
|
|
|
local radius = drill_radius_map[drill.name]
|
2019-05-13 01:30:51 +02:00
|
|
|
|
2019-05-13 12:08:57 +02:00
|
|
|
if radius == nil then
|
|
|
|
return false
|
2017-10-04 01:51:42 +02:00
|
|
|
end
|
2017-10-01 15:58:10 +02:00
|
|
|
|
2019-05-13 12:08:57 +02:00
|
|
|
local pos = drill.position
|
|
|
|
local x, y = pos.x, pos.y
|
|
|
|
|
|
|
|
local area = {
|
|
|
|
{x - radius, y - radius},
|
|
|
|
{x + radius, y + radius}
|
|
|
|
}
|
2019-05-13 01:30:51 +02:00
|
|
|
|
2019-05-13 12:08:57 +02:00
|
|
|
local resources = drill.surface.find_entities_filtered {type = 'resource', area = area}
|
|
|
|
for i = 1, #resources do
|
|
|
|
local resource = resources[i]
|
|
|
|
if resource ~= entity and not pumpjack_resources_map[resource.name] then
|
2018-07-19 14:28:00 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
2017-10-01 15:58:10 +02:00
|
|
|
end
|
|
|
|
|
2018-07-19 14:28:00 +02:00
|
|
|
local callback =
|
|
|
|
Token.register(
|
|
|
|
function(drill)
|
|
|
|
if drill.valid then
|
|
|
|
drill.order_deconstruction(drill.force)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2017-10-01 15:58:10 +02:00
|
|
|
local function on_resource_depleted(event)
|
2018-07-19 14:28:00 +02:00
|
|
|
local entity = event.entity
|
2019-05-13 12:08:57 +02:00
|
|
|
|
|
|
|
if not entity or not entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if require_fluid_ores[entity.name] then
|
|
|
|
return
|
2018-07-19 14:28:00 +02:00
|
|
|
end
|
|
|
|
|
2019-05-13 12:08:57 +02:00
|
|
|
local pos = entity.position
|
|
|
|
local x, y = pos.x, pos.y
|
|
|
|
local radius = max_radius
|
|
|
|
|
|
|
|
local area = {
|
|
|
|
{x - radius, y - radius},
|
|
|
|
{x + radius, y + radius}
|
|
|
|
}
|
|
|
|
|
|
|
|
local drills = event.entity.surface.find_entities_filtered {area = area, name = drill_names}
|
|
|
|
for i = 1, #drills do
|
|
|
|
local drill = drills[i]
|
|
|
|
if is_depleted(drill, entity) then
|
2018-07-19 14:28:00 +02:00
|
|
|
Task.set_timeout_in_ticks(5, callback, drill)
|
|
|
|
end
|
2017-10-04 01:51:42 +02:00
|
|
|
end
|
2017-10-01 15:58:10 +02:00
|
|
|
end
|
|
|
|
|
2018-04-06 21:58:50 +02:00
|
|
|
Event.add(defines.events.on_resource_depleted, on_resource_depleted)
|