1
0
mirror of https://github.com/Wyrrrd/Gun_Turret_Alerts.git synced 2026-06-09 18:40:20 +02:00

Add surface cleanup

This commit is contained in:
Wyrrrd
2024-11-06 20:30:08 +01:00
parent 4bf3081c31
commit 0eabcfc23e
+34 -8
View File
@@ -64,7 +64,7 @@ local get_ammo_flag = {
} }
local function add_entity_to_list(event) local function add_entity_to_list(event)
--Whenever an ammo-turret, car or artillery type entity is built, add it to the global table. --Whenever an ammo-turret, vehicle or artillery type entity is built, add it to the global table.
local entity = event.created_entity or event.entity local entity = event.created_entity or event.entity
local index = entity.surface.name.."_"..entity.force.name local index = entity.surface.name.."_"..entity.force.name
if storage.ammo_entities[index] then if storage.ammo_entities[index] then
@@ -73,7 +73,7 @@ local function add_entity_to_list(event)
end end
local function remove_entity_from_list(event) local function remove_entity_from_list(event)
--Whenever an ammo-turret, car or artillery type entity dies / is mined, remove it from the global table. --Whenever an ammo-turret, vehicle or artillery type entity dies / is mined, remove it from the global table.
local index = event.entity.surface.name.."_"..event.entity.force.name local index = event.entity.surface.name.."_"..event.entity.force.name
if storage.ammo_entities[index] then if storage.ammo_entities[index] then
for i,entity in pairs(storage.ammo_entities[index]) do for i,entity in pairs(storage.ammo_entities[index]) do
@@ -86,7 +86,7 @@ local function remove_entity_from_list(event)
end end
local function add_force_to_list(event) local function add_force_to_list(event)
--Whenever a player of an unscanned force joins the game or a force is created, add all ammo-turret, car or artillery type entities of that force to the global table. --Whenever a player of an unscanned force joins the game or a force is created, add all ammo-turret, vehicle or artillery type entities of that force to the global table.
local player, force local player, force
if event.player_index then if event.player_index then
player = game.get_player(event.player_index) player = game.get_player(event.player_index)
@@ -130,8 +130,25 @@ local function remove_force_from_list(event)
end end
end end
local function remove_surface_from_list(event)
--Whenever a surface is renamed or deleted, move/remove all entities in/from the global table.
for force,_ in pairs(game.forces) do
local index
if event.new_name then
storage.ammo_entities[new_name.."_"..force.name] = table.deepcopy(storage.ammo_entities[old_name.."_"..force.name])
index = old_name.."_"..force.name
elseif event.surface_index then
index = game.surfaces[surface_index].name.."_"..force.name
end
if index then
storage.ammo_entities[index] = nil
end
end
end
local function init_list() local function init_list()
-- index init --Create global table for existing forces
storage.ammo_entities = {} storage.ammo_entities = {}
local param = {} local param = {}
for _,force in pairs(game.forces) do for _,force in pairs(game.forces) do
@@ -141,7 +158,7 @@ local function init_list()
end end
local function generate_alerts() local function generate_alerts()
--Every 10 seconds recheck and give alerts to players for ammo-turret, car, spidertron or artillery type entities on the same force as them. --Every 10 seconds recheck and give alerts to players for ammo-turret, vehicle, spidertron or artillery type entities on the same force as them.
for _,player in pairs(game.connected_players) do for _,player in pairs(game.connected_players) do
local turret_enabled = player.mod_settings["gun-turret-alerts-enabled"].value local turret_enabled = player.mod_settings["gun-turret-alerts-enabled"].value
@@ -153,9 +170,10 @@ local function generate_alerts()
if ammo_entities then if ammo_entities then
for _,entity in pairs(ammo_entities) do for _,entity in pairs(ammo_entities) do
if entity.valid and entity.force == player.force then if entity.valid then
if entity.force == player.force then
--Get ammo inventory based on entity type, skip cars without guns --Get ammo inventory based on entity type, skip vehicles without guns
local inventory local inventory
if turret_enabled and entity.type == "ammo-turret" then if turret_enabled and entity.type == "ammo-turret" then
inventory = entity.get_inventory(defines.inventory.turret_ammo) inventory = entity.get_inventory(defines.inventory.turret_ammo)
@@ -190,9 +208,14 @@ local function generate_alerts()
player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-red"}, {"gun-turret-alerts.message-empty", entity.localised_name}, true) player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-red"}, {"gun-turret-alerts.message-empty", entity.localised_name}, true)
elseif ammo_flag == false then elseif ammo_flag == false then
-- low ammo alert -- low ammo alert
player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", entity.localised_name}, true) playr.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", entity.localised_name}, true)
end end
end end
else
local param = {}
param.entity = entity
remove_entity_from_list(param)
end
end end
end end
end end
@@ -211,6 +234,9 @@ script.on_event(defines.events.on_player_changed_force, remove_force_from_list)
script.on_event(defines.events.on_force_created, add_force_to_list) script.on_event(defines.events.on_force_created, add_force_to_list)
script.on_event(defines.events.on_forces_merged, remove_force_from_list) script.on_event(defines.events.on_forces_merged, remove_force_from_list)
script.on_event(defines.events.on_surface_renamed, remove_surface_from_list)
script.on_event(defines.events.on_surface_deleted, remove_surface_from_list)
script.on_event(defines.events.on_built_entity, add_entity_to_list, {{filter="type", type = "ammo-turret"},{filter="type", type = "car"},{filter="type", type = "spider-vehicle"},{filter="type", type = "artillery-turret"},{filter="type", type = "artillery-wagon"}}) script.on_event(defines.events.on_built_entity, add_entity_to_list, {{filter="type", type = "ammo-turret"},{filter="type", type = "car"},{filter="type", type = "spider-vehicle"},{filter="type", type = "artillery-turret"},{filter="type", type = "artillery-wagon"}})
script.on_event(defines.events.on_robot_built_entity, add_entity_to_list, {{filter="type", type = "ammo-turret"},{filter="type", type = "car"},{filter="type", type = "spider-vehicle"},{filter="type", type = "artillery-turret"},{filter="type", type = "artillery-wagon"}}) script.on_event(defines.events.on_robot_built_entity, add_entity_to_list, {{filter="type", type = "ammo-turret"},{filter="type", type = "car"},{filter="type", type = "spider-vehicle"},{filter="type", type = "artillery-turret"},{filter="type", type = "artillery-wagon"}})
script.on_event(defines.events.script_raised_built, add_entity_to_list, {{filter="type", type = "ammo-turret"},{filter="type", type = "car"},{filter="type", type = "spider-vehicle"},{filter="type", type = "artillery-turret"},{filter="type", type = "artillery-wagon"}}) script.on_event(defines.events.script_raised_built, add_entity_to_list, {{filter="type", type = "ammo-turret"},{filter="type", type = "car"},{filter="type", type = "spider-vehicle"},{filter="type", type = "artillery-turret"},{filter="type", type = "artillery-wagon"}})