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

Update control.lua

- commentary
- fix for multislot turrets
- small fixes
This commit is contained in:
Wyrrrd
2021-04-20 10:07:26 +02:00
parent 72e8b5509d
commit fa2781aee2
+15 -11
View File
@@ -1,8 +1,9 @@
--control.lua --control.lua
--This mod scans the map for cars and gun-turrets and places alerts when they are low. --This mod scans the map for cars and gun-turrets and places alerts when they are low on ammo.
local get_ammo_flags = { local get_ammo_flags = {
["added"] = function (inventory) --Applies mode to inventory, calculates and returns respective inventory flags
["added"] = function (inventory,player_threshold)
local no,low = false local no,low = false
if inventory.is_empty() then if inventory.is_empty() then
no = true no = true
@@ -20,7 +21,7 @@ local get_ammo_flags = {
return no, low return no, low
end, end,
["individually"] = function (inventory) ["individually"] = function (inventory,player_threshold)
local no,low = false local no,low = false
if inventory.is_empty() then if inventory.is_empty() then
no = true no = true
@@ -36,15 +37,15 @@ local get_ammo_flags = {
return no, low return no, low
end, end,
["selected"] = function (inventory,gun_index) ["selected"] = function (inventory,player_threshold,gun_index)
local no,low = false local no,low = false
if inventory.is_empty() then if inventory.is_empty() then
no = true no = true
else else
if not gun_index then if not gun_index then
gun_index = 1 -- default to added mode if no slot selected (probably multislot turret)
end no, low = get_ammo_flags["added"](inventory, player_threshold)
if inventory[gun_index].count == 0 then elseif inventory[gun_index].count == 0 then
no = true no = true
elseif inventory[gun_index].count < player_threshold then elseif inventory[gun_index].count < player_threshold then
low = true low = true
@@ -66,7 +67,7 @@ script.on_configuration_changed(function (event)
end) end)
script.on_nth_tick(3600, function (event) script.on_nth_tick(3600, function (event)
--Every minute the surface is rescanned for car and ammo-turret type entities. This is stored in two global tables. --Every minute the surface is rescanned for car and ammo-turret type entities. This is stored in a global table.
for index,surface in pairs(game.surfaces) do for index,surface in pairs(game.surfaces) do
global.ammo_entities[index] = surface.find_entities_filtered{type = {"ammo-turret","car"}} global.ammo_entities[index] = surface.find_entities_filtered{type = {"ammo-turret","car"}}
end end
@@ -86,22 +87,25 @@ script.on_nth_tick(600, function (event)
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 and entity.force == player.force then
--Get ammo inventory based on entity type, skip cars 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)
elseif car_enabled and entity.type == "car" and entity.prototype.guns then elseif car_enabled and entity.type == "car" --[[and entity.prototype.guns]] then
inventory = entity.get_inventory(defines.inventory.car_ammo) inventory = entity.get_inventory(defines.inventory.car_ammo)
end end
--Check for states of no or low ammo based on mode
local no, low = false local no, low = false
if inventory and get_ammo_flags[mode] then if inventory and get_ammo_flags[mode] then
if entity.type == "ammo-turret" then if entity.type == "ammo-turret" then
no, low = get_ammo_flags[mode](inventory) no, low = get_ammo_flags[mode](inventory, player_threshold)
elseif entity.type == "car" then elseif entity.type == "car" then
no, low = get_ammo_flags[mode](inventory,entity.selected_gun_index) no, low = get_ammo_flags[mode](inventory, player_threshold, entity.selected_gun_index)
end end
end end
--Create alert for present state
if no then if no then
-- no ammo alert -- no ammo alert
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)