1
0
mirror of https://github.com/Wyrrrd/Gun_Turret_Alerts.git synced 2026-07-07 07:26:55 +02:00

Compare commits

..

3 Commits

Author SHA1 Message Date
Wyrrrd ad2e1d597b Fix typo 2024-11-06 21:50:31 +01:00
Wyrrrd 0eabcfc23e Add surface cleanup 2024-11-06 20:30:08 +01:00
Wyrrrd 4bf3081c31 Update README.md 2024-11-06 13:13:10 +01:00
2 changed files with 69 additions and 43 deletions
+1 -1
View File
@@ -12,4 +12,4 @@ Thanks to [unhott](https://mods.factorio.com/user/unhott) for the [original mod]
Thanks to [snouz](https://mods.factorio.com/user/snouz) for the new icons and thumbnail.
Thanks to [Friendch](https://mods.factorio.com/user/Friendch) for the french locale.
Thanks to [eugenikus8](https://github.com/eugenikus8) for the russian locale.
Thanks to [Met_en_Bouldry](https://crowdin.com/profile/Met_en_Bouldry) for the ukranian locale.
Thanks to [Met_en_Bouldry](https://crowdin.com/profile/Met_en_Bouldry) for the ukrainian locale.
+33 -7
View File
@@ -64,7 +64,7 @@ local get_ammo_flag = {
}
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 index = entity.surface.name.."_"..entity.force.name
if storage.ammo_entities[index] then
@@ -73,7 +73,7 @@ local function add_entity_to_list(event)
end
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
if storage.ammo_entities[index] then
for i,entity in pairs(storage.ammo_entities[index]) do
@@ -86,7 +86,7 @@ local function remove_entity_from_list(event)
end
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
if event.player_index then
player = game.get_player(event.player_index)
@@ -130,8 +130,25 @@ local function remove_force_from_list(event)
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()
-- index init
--Create global table for existing forces
storage.ammo_entities = {}
local param = {}
for _,force in pairs(game.forces) do
@@ -141,7 +158,7 @@ local function init_list()
end
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
local turret_enabled = player.mod_settings["gun-turret-alerts-enabled"].value
@@ -153,9 +170,10 @@ local function generate_alerts()
if ammo_entities then
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
if turret_enabled and entity.type == "ammo-turret" then
inventory = entity.get_inventory(defines.inventory.turret_ammo)
@@ -193,6 +211,11 @@ local function generate_alerts()
player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", entity.localised_name}, true)
end
end
else
local param = {}
param.entity = entity
remove_entity_from_list(param)
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_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_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"}})