Files
ClassToolkit/Hunter.lua
T
notforever-ship-it 81f6c5abc8 Class Toolkit 1.0.0
Swing timer for every class (melee, Auto Shot, wand), buff reminder, spell ready icons, reagent and ammo warnings, and the hunter range icon and feed reminder moved over from PokeHuntLog.
2026-09-19 15:44:42 +01:00

252 lines
9.6 KiB
Lua

-- Class Toolkit: hunter helpers, moved over from PokeHuntLog.
--
-- Range icon: Auto Shot's range check says whether you can shoot. When you can't and the target is within
-- ~10 yards (CheckInteractDistance 3), Wing Clip's range check tells melee range from the dead zone. Both
-- spells have to be on an action bar (any slot, even a hidden page) for their range to be readable.
--
-- Feed reminder: GetPetHappiness() drops from Happy (3) to Content (2) to Unhappy (1). The reminder shows
-- while the pet is below the chosen level and isn't already eating. Clicking it casts Feed Pet.
local CTK = ClassToolkit
local AUTO_SHOT, WING_CLIP, FEED_PET = "Auto Shot", "Wing Clip", "Feed Pet"
local FEED_EFFECT_ICON = "Interface\\Icons\\Ability_Hunter_BeastTraining"
local AUTO_SHOT_ICON = "Interface\\Icons\\Ability_Whirlwind"
local HAPPINESS_TEXTURE = "Interface\\PetPaperDollFrame\\UI-PetHappiness"
local RANGE_STATES = {
range = { text = "In range", r = 0.25, g = 1, b = 0.25 },
melee = { text = "Melee", r = 1, g = 0.6, b = 0.1 },
deadzone = { text = "Dead zone", r = 1, g = 0.15, b = 0.15 },
close = { text = "Too close", r = 1, g = 0.15, b = 0.15 },
far = { text = "Out of range", r = 0.6, g = 0.6, b = 0.6 },
}
local HAPPINESS = {
[1] = { text = "Unhappy", r = 1, g = 0.15, b = 0.15, coords = { 0.375, 0.5625, 0, 0.359375 } },
[2] = { text = "Content", r = 1, g = 0.82, b = 0, coords = { 0.1875, 0.375, 0, 0.359375 } },
[3] = { text = "Happy", r = 0.25, g = 1, b = 0.25, coords = { 0, 0.1875, 0, 0.359375 } },
}
local rangeFrame, feedFrame
local warnedAutoShot, warnedWingClip = false, false
local lastHappiness, happinessSince, lastFed = nil, nil, nil
------------------------------------------------------------------------------------------------
-- Range icon
------------------------------------------------------------------------------------------------
local function RangeState()
local shot = CTK.SpellInRange(AUTO_SHOT)
if shot then return "range" end
if CheckInteractDistance("target", 3) then
if CTK.ActionSlotFor(WING_CLIP) then
if CTK.SpellInRange(WING_CLIP) then return "melee" end
return "deadzone"
end
return "close"
end
if CTK.ActionSlotFor(AUTO_SHOT) then return "far" end
-- Auto Shot isn't on the action bars: follow distance (28 yards) is the closest guess.
if CheckInteractDistance("target", 4) then return "range" end
return "far"
end
local rangeElapsed = 0
local function RangeOnUpdate()
rangeElapsed = rangeElapsed + arg1
if rangeElapsed < 0.1 then return end
rangeElapsed = 0
if CTK.movingIcons then return end
if not CTK.char.range or not CTK.IsClass("HUNTER") or not CTK.HasAttackableTarget() then
rangeFrame:Hide()
return
end
local hasShot = CTK.ActionSlotFor(AUTO_SHOT)
if not hasShot and not warnedAutoShot then
warnedAutoShot = true
CTK.Print("the range icon needs |cffffffffAuto Shot|r on one of your action bars (any slot). " ..
"Drag it from your spellbook.")
elseif hasShot and not CTK.ActionSlotFor(WING_CLIP) and not warnedWingClip and
CTK.KnowsSpell(WING_CLIP) then
warnedWingClip = true
CTK.Print("put |cffffffffWing Clip|r on an action bar too, so the range icon can tell melee range " ..
"from the dead zone.")
end
local state = RANGE_STATES[RangeState()]
rangeFrame.label:SetText(state.text)
CTK.SetIconState(rangeFrame, state.r, state.g, state.b, state.text)
rangeFrame.icon:SetTexture(CTK.SpellTexture(AUTO_SHOT) or AUTO_SHOT_ICON)
if state == RANGE_STATES.range then
rangeFrame.icon:SetVertexColor(1, 1, 1)
else
rangeFrame.icon:SetVertexColor(0.5, 0.5, 0.5)
end
rangeFrame:Show()
end
------------------------------------------------------------------------------------------------
-- Feed reminder
------------------------------------------------------------------------------------------------
local function PetIsEating()
for i = 1, 16 do
local texture = UnitBuff("pet", i)
if not texture then break end
if texture == FEED_EFFECT_ICON then return true end
end
return false
end
-- The game only tells addons Happy, Content or Unhappy, never the hidden 0-1050 number. It does say when
-- that level changes, so the advice is based on how long the pet has been at this level.
local function FeedAdvice()
if not UnitExists("pet") then return nil end
local happiness = GetPetHappiness()
if not happiness then return nil end
local minutes = happinessSince and math.floor((time() - happinessSince) / 60) or nil
local since = minutes and (" for " .. minutes .. " min") or ""
local fed = lastFed and (" Last fed " .. math.floor((time() - lastFed) / 60) .. " min ago.") or ""
if happiness == 3 then
return "Happy" .. since .. ". Feeding now would waste most of the food." .. fed
elseif happiness == 2 then
return "Content" .. since .. ". A full meal fits with nothing wasted." .. fed
end
return "Unhappy" .. since .. ". Feed it now: it is doing less damage." .. fed
end
local function UpdateFeed(fromEvent)
if not feedFrame then return end
local c = CTK.char
if CTK.movingIcons then return end
local _, isHunterPet = HasPetUI()
if not c.feed or not CTK.IsClass("HUNTER") or not isHunterPet or not UnitExists("pet") or UnitIsDead("pet") then
feedFrame:Hide()
lastHappiness = nil
return
end
local happiness = GetPetHappiness()
local look = happiness and HAPPINESS[happiness]
if not look then
feedFrame:Hide()
return
end
if happiness ~= lastHappiness then happinessSince = time() end
if PetIsEating() then lastFed = time() end
local remindAt = (c.feedWhen == "unhappy") and 1 or 2
if happiness <= remindAt and not PetIsEating() then
feedFrame.icon:SetTexCoord(look.coords[1], look.coords[2], look.coords[3], look.coords[4])
CTK.SetIconState(feedFrame, look.r, look.g, look.b, "Feed " .. (UnitName("pet") or "your pet") ..
" (" .. look.text .. ")")
feedFrame:Show()
if fromEvent and lastHappiness and happiness < lastHappiness then
local who = UnitName("pet") or "Your pet"
CTK.Print("|cffffd100" .. who .. " is " .. string.lower(look.text) .. ". Time to feed it!|r")
UIErrorsFrame:AddMessage(who .. " is " .. string.lower(look.text) .. " - feed your pet", look.r, look.g, look.b, 1.0, 3)
if c.feedSound then pcall(PlaySound, "TellMessage") end
end
else
feedFrame:Hide()
end
lastHappiness = happiness
end
------------------------------------------------------------------------------------------------
-- Setup
------------------------------------------------------------------------------------------------
local function Update()
if not rangeFrame then return end
local c = CTK.char
local hunter = CTK.IsClass("HUNTER")
rangeFrame:EnableMouse(CTK.movingIcons and c.range and true or false)
if CTK.movingIcons then
if c.range and hunter then
rangeFrame.icon:SetTexture(AUTO_SHOT_ICON)
rangeFrame.icon:SetVertexColor(1, 1, 1)
CTK.SetIconState(rangeFrame, 0.25, 1, 0.25, "Range icon (drag)")
rangeFrame:Show()
else
rangeFrame:Hide()
end
if c.feed and hunter then
local look = HAPPINESS[2]
feedFrame.icon:SetTexCoord(look.coords[1], look.coords[2], look.coords[3], look.coords[4])
CTK.SetIconState(feedFrame, look.r, look.g, look.b, "Feed reminder (drag)")
feedFrame:Show()
else
feedFrame:Hide()
end
return
end
UpdateFeed(false)
end
function CTK.InitHunter()
rangeFrame = CTK.CreateIcon("ClassToolkitRangeIcon", UIParent, 36)
CTK.MakeDraggable(rangeFrame, "range", 0, -140)
rangeFrame:EnableMouse(false) -- don't block clicks in the middle of the screen
rangeFrame:SetScript("OnUpdate", RangeOnUpdate)
-- A hidden frame's OnUpdate never runs, so a small always-shown driver brings the icon back.
local rangeDriver = CreateFrame("Frame")
rangeDriver:SetScript("OnUpdate", function()
if not rangeFrame:IsShown() and not CTK.movingIcons and CTK.char and CTK.char.range and
CTK.HasAttackableTarget() and CTK.IsClass("HUNTER") then
rangeFrame:Show()
end
end)
feedFrame = CTK.CreateIcon("ClassToolkitFeedReminder", UIParent, 40)
CTK.MakeDraggable(feedFrame, "feed", 0, 180)
feedFrame.icon:SetTexture(HAPPINESS_TEXTURE)
feedFrame:EnableMouse(true)
feedFrame:RegisterForClicks("LeftButtonUp")
feedFrame:SetScript("OnClick", function()
if CTK.movingIcons or IsShiftKeyDown() then return end
if UnitAffectingCombat("player") then
CTK.Print("you can't feed your pet in combat.")
else
CastSpellByName(FEED_PET)
end
end)
feedFrame:SetScript("OnEnter", function()
GameTooltip:SetOwner(this, "ANCHOR_BOTTOM")
GameTooltip:SetText("Feed your pet")
local advice = FeedAdvice()
if advice then GameTooltip:AddLine(advice, 1, 0.82, 0, 1) end
GameTooltip:AddLine("Click to cast Feed Pet, then click a food in your bags.", 1, 1, 1, 1)
GameTooltip:AddLine("Shift-drag to move.", 0.7, 0.7, 0.7)
GameTooltip:Show()
end)
feedFrame:SetScript("OnLeave", function() GameTooltip:Hide() end)
local f = CreateFrame("Frame")
local events = { "UNIT_HAPPINESS", "UNIT_PET", "UNIT_AURA", "PLAYER_ENTERING_WORLD" }
for i = 1, table.getn(events) do
pcall(f.RegisterEvent, f, events[i])
end
f:SetScript("OnEvent", function()
if event == "UNIT_HAPPINESS" then
UpdateFeed(true)
elseif event == "UNIT_AURA" then
if arg1 == "pet" then UpdateFeed(false) end
elseif event == "UNIT_PET" then
if arg1 == "player" then
lastHappiness = nil
CTK.After(1, function() UpdateFeed(false) end)
end
elseif event == "PLAYER_ENTERING_WORLD" then
CTK.After(2, function() UpdateFeed(false) end)
end
end)
CTK.RegisterModule({ update = Update })
end