mirror of
https://github.com/Wyrrrd/Gun_Turret_Alerts.git
synced 2026-06-04 00:06:38 +02:00
Fix crash on load
"selected" mode defaulting to first slot now
This commit is contained in:
@@ -1,4 +1,11 @@
|
|||||||
---------------------------------------------------------------------------------------------------
|
---------------------------------------------------------------------------------------------------
|
||||||
|
Version: 1.1.6
|
||||||
|
Date: 2021-04-22
|
||||||
|
Bugfixes:
|
||||||
|
- Defaulting multislot mode "selected" to first slot, if no slot selected. Defaulting to "added" mode caused a crash.
|
||||||
|
Scripting:
|
||||||
|
- Simplified some boolean logic misusing it as ternary
|
||||||
|
---------------------------------------------------------------------------------------------------
|
||||||
Version: 1.1.5
|
Version: 1.1.5
|
||||||
Date: 2021-04-21
|
Date: 2021-04-21
|
||||||
Features:
|
Features:
|
||||||
|
|||||||
+21
-25
@@ -13,57 +13,53 @@ end
|
|||||||
|
|
||||||
--local functions
|
--local functions
|
||||||
|
|
||||||
local get_ammo_flags = {
|
local get_ammo_flag = {
|
||||||
--Applies mode to inventory, calculates and returns respective inventory flags
|
--Applies mode to inventory, calculates and returns respective inventory flags
|
||||||
|
--Uses boolean return value as ternary, with true = no ammo, false = low ammo and nil = enough ammo
|
||||||
["added"] = function (inventory,player_threshold)
|
["added"] = function (inventory,player_threshold)
|
||||||
local no,low = false
|
|
||||||
if inventory.is_empty() then
|
if inventory.is_empty() then
|
||||||
no = true
|
return true
|
||||||
else
|
else
|
||||||
local ammo_count = 0
|
local ammo_count = 0
|
||||||
for i=1, #inventory do
|
for i=1, #inventory do
|
||||||
ammo_count = ammo_count + inventory[i].count
|
ammo_count = ammo_count + inventory[i].count
|
||||||
end
|
end
|
||||||
if ammo_count == 0 then
|
if ammo_count == 0 then
|
||||||
no = true
|
return true
|
||||||
elseif ammo_count < player_threshold then
|
elseif ammo_count < player_threshold then
|
||||||
low = true
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return no, low
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
["individually"] = function (inventory,player_threshold)
|
["individually"] = function (inventory,player_threshold)
|
||||||
local no,low = false
|
|
||||||
if inventory.is_empty() then
|
if inventory.is_empty() then
|
||||||
no = true
|
return true
|
||||||
else
|
else
|
||||||
for i=1, #inventory do
|
for i=1, #inventory do
|
||||||
if inventory[i].count == 0 then
|
if inventory[i].count == 0 then
|
||||||
no = true
|
return true
|
||||||
elseif inventory[i].count < player_threshold then
|
elseif inventory[i].count < player_threshold then
|
||||||
low = true
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return no, low
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
["selected"] = function (inventory,player_threshold,gun_index)
|
["selected"] = function (inventory,player_threshold,gun_index)
|
||||||
local no,low = false
|
|
||||||
if inventory.is_empty() then
|
if inventory.is_empty() then
|
||||||
no = true
|
return true
|
||||||
else
|
else
|
||||||
if not gun_index then
|
if not gun_index then
|
||||||
-- default to added mode if no slot selected (probably multislot turret)
|
-- default to first slot if no slot selected (probably multislot turret)
|
||||||
no, low = get_ammo_flags["added"](inventory, player_threshold)
|
gun_index = 1
|
||||||
elseif inventory[gun_index].count == 0 then
|
end
|
||||||
no = true
|
if inventory[gun_index].count == 0 then
|
||||||
|
return true
|
||||||
elseif inventory[gun_index].count < player_threshold then
|
elseif inventory[gun_index].count < player_threshold then
|
||||||
low = true
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return no, low
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,20 +162,20 @@ local function generate_alerts()
|
|||||||
end
|
end
|
||||||
|
|
||||||
--Check for states of no or low ammo based on mode
|
--Check for states of no or low ammo based on mode
|
||||||
local no, low = false
|
local ammo_flag
|
||||||
if inventory and get_ammo_flags[mode] then
|
if inventory and get_ammo_flag[mode] then
|
||||||
if entity.type == "ammo-turret" then
|
if entity.type == "ammo-turret" then
|
||||||
no, low = get_ammo_flags[mode](inventory, player_threshold)
|
ammo_flag = get_ammo_flag[mode](inventory, player_threshold)
|
||||||
elseif entity.type == "car" then
|
elseif entity.type == "car" then
|
||||||
no, low = get_ammo_flags[mode](inventory, player_threshold, entity.selected_gun_index)
|
ammo_flag = get_ammo_flag[mode](inventory, player_threshold, entity.selected_gun_index)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--Create alert for present state
|
--Create alert for present state
|
||||||
if no then
|
if ammo_flag 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)
|
||||||
elseif low 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)
|
player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", entity.localised_name}, true)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user