Fix SmartAmmo low-ammo warnings

- Remove infinite tail-recursion threshold getter that hard-froze the client on /rl
- Fix double-count (equipped ammo was added to the bag total)
- Make it true crossing-only: warn only when the total drops across the threshold, never on cold baseline or restock
- Use the shaking warning banner instead of a celebration, with sarcastic chat/banner copy
- Options: fix legacy EditBox HasFocus error on the threshold input; move Pet Auto-Teach to the right column
This commit is contained in:
DuvelCorp
2026-09-12 12:08:54 +02:00
parent 22232364b1
commit f3af63926e
4 changed files with 69 additions and 25 deletions
+19 -7
View File
@@ -48,7 +48,7 @@ local function MTHSmartAmmo_IsLowAmmoWarningEnabled()
return MTHSmartAmmo_ReadFlag("lowAmmoWarningEnabled")
end
local function MTHSmartAmmo_GetLowAmmoThreshold()
local function MTHSmartAmmo_ReadLowAmmoThreshold()
local settings = MTHSmartAmmo_GetSettings()
local threshold = tonumber(settings and settings.lowAmmoThreshold) or 200
if threshold < 1 then threshold = 1 end
@@ -145,7 +145,7 @@ function MTHSmartAmmo_SetLowAmmoThreshold(value)
end
function MTHSmartAmmo_GetLowAmmoThreshold()
return MTHSmartAmmo_GetLowAmmoThreshold()
return MTHSmartAmmo_ReadLowAmmoThreshold()
end
local MTHSmartAmmo_CastSpell_Hook
@@ -275,9 +275,9 @@ local function MTHSmartAmmo_ShowAmmoStockWarning(ammoType, total, threshold)
local isZero = total <= 0
local warning
if isZero then
warning = "YOU HAVE NO " .. string.upper(ammoType) .. " LEFT!"
warning = "Out of " .. ammoType .. "! Your ranged weapon is now a very expensive club."
else
warning = "LOW " .. string.upper(ammoType) .. ": " .. tostring(total) .. " LEFT (WARNING AT " .. tostring(threshold) .. ")."
warning = "Running low on " .. ammoType .. " — only " .. tostring(total) .. " left. Restock before you're just angrily pointing at things."
end
if MTH_SA_Print then
MTH_SA_Print(warning)
@@ -298,6 +298,8 @@ end
local function MTHSmartAmmo_CountAmmoStock(ammoType)
local rankMap = (ammoType == "Bullets" and MTH_AMMO_BULLET_RANK) or MTH_AMMO_ARROW_RANK or {}
-- Bag scan already covers the quiver/ammo pouch; GetInventoryItemCount("player", 0)
-- returns the bag copies of the loaded ammo, so adding it would double-count.
local total = 0
for bag = 4, 0, -1 do
for slot = GetContainerNumSlots(bag), 1, -1 do
@@ -308,7 +310,15 @@ local function MTHSmartAmmo_CountAmmoStock(ammoType)
end
end
end
return total + (tonumber(GetInventoryItemCount("player", 0)) or 0)
return total
end
-- ready < low < zero; a warning fires only when severity INCREASES (a real
-- downward crossing during play), never on the cold baseline or on restock.
local function MTHSmartAmmo_StockSeverity(state)
if state == "zero" then return 2 end
if state == "low" then return 1 end
return 0
end
local function MTHSmartAmmo_CheckAmmoStock()
@@ -319,9 +329,11 @@ local function MTHSmartAmmo_CheckAmmoStock()
return
end
local total = MTHSmartAmmo_CountAmmoStock(ammoType)
local threshold = MTHSmartAmmo_GetLowAmmoThreshold()
local threshold = MTHSmartAmmo_ReadLowAmmoThreshold()
local state = total <= 0 and "zero" or (total <= threshold and "low" or "ready")
if state ~= MTH_SA_AmmoStockState and state ~= "ready" then
local prev = MTH_SA_AmmoStockState
if prev ~= nil and MTHSmartAmmo_StockSeverity(state) > MTHSmartAmmo_StockSeverity(prev) then
MTHSmartAmmo_ShowAmmoStockWarning(ammoType, total, threshold)
end
MTH_SA_AmmoStockState = state