Files
no-stone-wall-alarm/control.lua
2025-12-30 11:00:06 +01:00

74 lines
2.4 KiB
Lua

local function split_string_to_map(str, separator)
local parts = {}
for part in string.gmatch(str, "([^" .. separator .. "]+)") do
-- Trim leading/trailing whitespace:
part = part:gsub("^%s*(.-)%s*$", "%1")
parts[part] = true
end
return parts
end
local no_alarm_entities = {}
local no_ghost_possible_entities = {
["car"] = true,
["tank"] = true,
["construction-robot"] = true,
["logistic-robot"] = true,
["player"] = true
}
local function build_no_alarm_table(setting_value)
no_alarm_entities = split_string_to_map(setting_value, ",")
end
script.on_init(function()
build_no_alarm_table(settings.global["no-alarm-entities"].value)
end)
script.on_load(function()
build_no_alarm_table(settings.global["no-alarm-entities"].value)
end)
script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
if event.setting == "no-alarm-entities" then
build_no_alarm_table(settings.global["no-alarm-entities"].value)
end
end)
script.on_event(defines.events.on_entity_damaged, function(event)
local entity = event.entity
-- Check if the damaged entity is in our list of no-alarm entities
if entity and entity.valid and no_alarm_entities[entity.name] then
-- Check if the damage would destroy the entity
if event.final_health <= 0 then
local surface = entity.surface
local position = entity.position
local force = entity.force
local direction = entity.direction
local name = entity.name
-- Remove the original entity
entity.destroy()
-- Check if the force has researched ghost placement
if force.technologies["construction-robotics"] and force.technologies["construction-robotics"].researched then
-- Don't create a ghost for entities that cannot have a ghost
if no_ghost_possible_entities[name] then
return
end
-- Create a ghost of the entity
surface.create_entity {
name = "entity-ghost",
inner_name = name,
position = position,
force = force,
direction = direction
}
end
end
end
end)