Add SmartAmmo low-ammo alerts

This commit is contained in:
DuvelCorp
2026-09-12 10:57:27 +02:00
parent af31acdb3e
commit 22232364b1
4 changed files with 189 additions and 17 deletions
+101 -1
View File
@@ -44,6 +44,17 @@ local function MTHSmartAmmo_IsWeaponSwapEnabled()
return MTHSmartAmmo_ReadFlag("weaponSwapEnabled")
end
local function MTHSmartAmmo_IsLowAmmoWarningEnabled()
return MTHSmartAmmo_ReadFlag("lowAmmoWarningEnabled")
end
local function MTHSmartAmmo_GetLowAmmoThreshold()
local settings = MTHSmartAmmo_GetSettings()
local threshold = tonumber(settings and settings.lowAmmoThreshold) or 200
if threshold < 1 then threshold = 1 end
return math.floor(threshold)
end
local MTHSmartAmmo_InitializeHooks
MTHSmartAmmo_EnsureHooks = nil
@@ -114,6 +125,29 @@ function MTHSmartAmmo_GetWeaponSwapEnabled()
return MTHSmartAmmo_IsWeaponSwapEnabled() and true or false
end
function MTHSmartAmmo_SetLowAmmoWarningEnabled(enabled, silent)
MTHSmartAmmo_WriteFlag("lowAmmoWarningEnabled", enabled and true or false)
if not silent and DEFAULT_CHAT_FRAME then
MTH_SA_Print("Smart Ammo low-ammo warnings " .. (enabled and "Enabled." or "Disabled."))
end
end
function MTHSmartAmmo_GetLowAmmoWarningEnabled()
return MTHSmartAmmo_IsLowAmmoWarningEnabled() and true or false
end
function MTHSmartAmmo_SetLowAmmoThreshold(value)
local settings = MTHSmartAmmo_GetSettings()
if not settings then return end
local threshold = tonumber(value) or 200
if threshold < 1 then threshold = 1 end
settings.lowAmmoThreshold = math.floor(threshold)
end
function MTHSmartAmmo_GetLowAmmoThreshold()
return MTHSmartAmmo_GetLowAmmoThreshold()
end
local MTHSmartAmmo_CastSpell_Hook
local MTHSmartAmmo_UseAction_Hook
local MTHSmartAmmo_CastSpellByName_Hook
@@ -178,6 +212,8 @@ MTH_SA_LastKnownEquippedName = nil
MTH_SA_LastFallbackNotice = { key = nil, time = 0 }
MTH_SA_LastKnownWeaponAmmoType = nil
MTH_SA_LastWeaponCheck = 0
MTH_SA_LastAmmoStockCheck = 0
MTH_SA_AmmoStockState = nil
MTH_SA_StartupProtectUntil = (GetTime() or 0) + 2.0
MTH_SA_StartupPrimed = nil
@@ -216,7 +252,11 @@ local function MTHSmartAmmo_ShowFallbackWarning(previousAmmo, newAmmo)
DEFAULT_CHAT_FRAME:AddMessage(warning)
end
if RaidWarningFrame and RaidNotice_AddMessage and ChatTypeInfo and ChatTypeInfo["RAID_WARNING"] then
local hasWarningAnimation = type(MTH_CelebrateAmmoFallback) == "function"
and type(MTH_FX_IsEnabled) == "function" and MTH_FX_IsEnabled("combat.ammoout")
if hasWarningAnimation then
MTH_CelebrateAmmoFallback(previousAmmo, newAmmo)
elseif RaidWarningFrame and RaidNotice_AddMessage and ChatTypeInfo and ChatTypeInfo["RAID_WARNING"] then
RaidNotice_AddMessage(RaidWarningFrame, warning, ChatTypeInfo["RAID_WARNING"])
elseif UIErrorsFrame and UIErrorsFrame.AddMessage then
UIErrorsFrame:AddMessage(warning, 1.0, 0.15, 0.15, 1.0)
@@ -231,6 +271,62 @@ local function MTHSmartAmmo_ShowFallbackWarning(previousAmmo, newAmmo)
end
end
local function MTHSmartAmmo_ShowAmmoStockWarning(ammoType, total, threshold)
local isZero = total <= 0
local warning
if isZero then
warning = "YOU HAVE NO " .. string.upper(ammoType) .. " LEFT!"
else
warning = "LOW " .. string.upper(ammoType) .. ": " .. tostring(total) .. " LEFT (WARNING AT " .. tostring(threshold) .. ")."
end
if MTH_SA_Print then
MTH_SA_Print(warning)
elseif DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.AddMessage then
DEFAULT_CHAT_FRAME:AddMessage(warning)
end
local flag = isZero and "combat.ammozero" or "combat.lowammo"
local celebrate = isZero and MTH_CelebrateAmmoZero or MTH_CelebrateLowAmmo
if type(celebrate) == "function" and type(MTH_FX_IsEnabled) == "function" and MTH_FX_IsEnabled(flag) then
celebrate(ammoType, total, threshold)
elseif RaidWarningFrame and RaidNotice_AddMessage and ChatTypeInfo and ChatTypeInfo["RAID_WARNING"] then
RaidNotice_AddMessage(RaidWarningFrame, warning, ChatTypeInfo["RAID_WARNING"])
elseif UIErrorsFrame and UIErrorsFrame.AddMessage then
UIErrorsFrame:AddMessage(warning, 1.0, 0.15, 0.15, 1.0)
end
end
local function MTHSmartAmmo_CountAmmoStock(ammoType)
local rankMap = (ammoType == "Bullets" and MTH_AMMO_BULLET_RANK) or MTH_AMMO_ARROW_RANK or {}
local total = 0
for bag = 4, 0, -1 do
for slot = GetContainerNumSlots(bag), 1, -1 do
local itemName = MTH_SA_GetContainerItemName(bag, slot)
if itemName and rankMap[itemName] then
local _, count = GetContainerItemInfo(bag, slot)
total = total + (tonumber(count) or 1)
end
end
end
return total + (tonumber(GetInventoryItemCount("player", 0)) or 0)
end
local function MTHSmartAmmo_CheckAmmoStock()
if not MTH_SA_IsModuleEnabled() or not MTHSmartAmmo_IsLowAmmoWarningEnabled() then return end
local ammoType = MTH_SA_GetEquippedWeaponAmmoType()
if not ammoType then
MTH_SA_AmmoStockState = nil
return
end
local total = MTHSmartAmmo_CountAmmoStock(ammoType)
local threshold = MTHSmartAmmo_GetLowAmmoThreshold()
local state = total <= 0 and "zero" or (total <= threshold and "low" or "ready")
if state ~= MTH_SA_AmmoStockState and state ~= "ready" then
MTHSmartAmmo_ShowAmmoStockWarning(ammoType, total, threshold)
end
MTH_SA_AmmoStockState = state
end
local MTHSmartAmmo_FallbackCheckFrame = nil
local function MTHSmartAmmo_HandleFallbackResult(previousAmmo)
@@ -467,6 +563,10 @@ frame:SetScript("OnUpdate", function()
MTH_SA_LastWeaponCheck = now
MTHSmartAmmo_HandleWeaponSwapAutoEquip("OnUpdate-weapon-check")
end
if (now - (MTH_SA_LastAmmoStockCheck or 0)) >= 1.0 then
MTH_SA_LastAmmoStockCheck = now
MTHSmartAmmo_CheckAmmoStock()
end
end)
function MTH_SA_CountEquippedAmmo()