diff --git a/FishKey/FishKey.lua b/FishKey/FishKey.lua index 21539af..594e57c 100644 --- a/FishKey/FishKey.lua +++ b/FishKey/FishKey.lua @@ -7,11 +7,17 @@ -- without loot) AND every Fishing key is released. Restoring earlier lets the -- still-held key, a double tap, or the key release cast Fishing again, which -- replaces the bobber and loses the fish. Combat start restores at once. +-- +-- Loot can only be coming if the interact key was pressed just before the cast +-- ended. Otherwise (timeout, moving, another spell) there is nothing to wait for. local FISHING_SPELL = 7620 --- How long to wait for the loot window after the cast ends before treating it --- as a miss. -local LOOT_WAIT = 1.5 +-- An interact this recent counts as the one that ended the cast. +local PRESS_WINDOW = 1 +-- After a press with neither a loot window nor an error, wait this long plus +-- two round trips before treating it as a miss. +local LOOT_WAIT_BASE = 0.25 +local LOOT_WAIT_MAX = 1.5 local POLL = 0.05 -- Settings changed while fishing, as temp CVars that are never saved, and @@ -39,6 +45,10 @@ local activeKeys = {} -- release, nil otherwise. local state local looting = false +-- Interact was pressed as the cast ended; a loot window may be on its way. +local awaitingLoot = false +-- GetTime() of the last interact during the cast. +local lastInteract -- Bumped on every new cast and restore, so stale timers do nothing. local generation = 0 @@ -172,6 +182,8 @@ local function Restore() generation = generation + 1 state = nil looting = false + awaitingLoot = false + lastInteract = nil if overridden then if InCombatLockdown() then clearAfterCombat = true @@ -186,7 +198,7 @@ end -- Restores once nothing can still turn a press into a new cast. local function TryFinish(gen) - if gen ~= generation or state ~= "ending" or looting then + if gen ~= generation or state ~= "ending" or looting or awaitingLoot then return end if AnyKeyHeld() then @@ -202,6 +214,8 @@ local function Apply() end generation = generation + 1 looting = false + awaitingLoot = false + lastInteract = nil local keys = FishingKeys() if #keys == 0 then return @@ -221,15 +235,34 @@ local function ChannelEnded() end state = "ending" local gen = generation - -- No loot window by then: the cast missed, timed out or was interrupted. - C_Timer.After(LOOT_WAIT, function() TryFinish(gen) end) + if not looting and lastInteract and GetTime() - lastInteract <= PRESS_WINDOW then + awaitingLoot = true + local _, _, _, latency = GetNetStats() + local wait = math.min(LOOT_WAIT_MAX, LOOT_WAIT_BASE + 2 * (latency or 0) / 1000) + C_Timer.After(wait, function() + if gen == generation and awaitingLoot then + awaitingLoot = false + TryFinish(gen) + end + end) + return + end + TryFinish(gen) end +-- Runs after the binding (or anything else) interacts; only notes the time. +hooksecurefunc(C_PlayerInteractionManager, "InteractUnit", function() + if state == "fishing" then + lastInteract = GetTime() + end +end) + owner:RegisterEvent("ADDON_LOADED") owner:RegisterEvent("PLAYER_REGEN_DISABLED") owner:RegisterEvent("PLAYER_REGEN_ENABLED") owner:RegisterEvent("LOOT_OPENED") owner:RegisterEvent("LOOT_CLOSED") +owner:RegisterEvent("UI_ERROR_MESSAGE") owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_START", "player") owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_STOP", "player") owner:SetScript("OnEvent", function(_, event, arg1, _, spellID) @@ -250,10 +283,21 @@ owner:SetScript("OnEvent", function(_, event, arg1, _, spellID) -- The loot can arrive just before or just after the channel stops. if state then looting = true + awaitingLoot = false + end + elseif event == "UI_ERROR_MESSAGE" then + -- "No fish are hooked", "Your fish got away" and the like: no loot coming. + if awaitingLoot then + awaitingLoot = false + TryFinish(generation) end elseif event == "LOOT_CLOSED" then if looting then looting = false + if state == "fishing" then + -- Looted before the channel-stop event arrived: nothing left to wait for. + lastInteract = nil + end TryFinish(generation) end elseif event == "PLAYER_REGEN_DISABLED" then diff --git a/tests/test_fk.lua b/tests/test_fk.lua index 29e723f..8b14217 100644 --- a/tests/test_fk.lua +++ b/tests/test_fk.lua @@ -46,6 +46,13 @@ end local clock, timers = 0, {} C_Timer = { After = function(sec, fn) timers[#timers + 1] = { at = clock + sec, fn = fn } end } +function GetTime() return clock end +function GetNetStats() return 0, 0, 20, 50 end +function hooksecurefunc(t, name, fn) + local orig = t[name] + t[name] = function(...) local r = orig(...); fn(...); return r end +end +C_PlayerInteractionManager = { InteractUnit = function() return true end } local function advance(sec) local target = clock + sec while true do @@ -112,8 +119,11 @@ local function restored() end check(C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "press and hold casting off while fishing") +local function press() C_PlayerInteractionManager.InteractUnit("anyinteract") end + -- Catch with the key held through the loot (press and hold, slow release). held["F"] = true +press() handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7731) check(overrides["F"] == "INTERACTTARGET", "still interact right after the channel ends") handler(nil, "LOOT_OPENED") @@ -126,31 +136,52 @@ held["F"] = nil advance(0.1) check(restored(), "key and settings restored once looted and released") --- Miss: no loot window. +-- Cast ends with no press (timeout, moved): back at once. handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) stop() -advance(1) -check(overrides["F"] == "INTERACTTARGET", "waits a moment for loot after a miss") -advance(1) -check(restored(), "restored after a miss") +check(restored(), "no press: restored immediately") --- Loot arrives before the channel stop, modifier key held. +-- An early press long before the timeout doesn't count. handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) -handler(nil, "LOOT_OPENED") +press() +advance(10) stop() +check(restored(), "old press: restored immediately") + +-- Pressed with nothing hooked: the error message ends the wait. +handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) +press() +stop() +check(overrides["F"] == "INTERACTTARGET", "press: waits for loot") +handler(nil, "UI_ERROR_MESSAGE", 1, "No fish are hooked.") +check(restored(), "error message: restored immediately") + +-- Pressed, no loot and no error: short latency-based wait (0.25 + 2 x 50ms). +handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) +press() +stop() +advance(0.3) +check(overrides["F"] == "INTERACTTARGET", "still waiting at 0.3s") +advance(0.1) +check(restored(), "restored after 0.35s") + +-- Loot arrives and closes before the channel stop, modifier key held. +handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) +press() +handler(nil, "LOOT_OPENED") held["1"] = true handler(nil, "LOOT_CLOSED") +stop() advance(0.2) check(overrides["SHIFT-1"] == "INTERACTTARGET", "held SHIFT-1 counts as held") held["1"] = nil -advance(0.1) -check(restored(), "restored after loot that came first") +advance(0.06) +check(restored(), "restored after loot that came first, no extra wait") --- A stale miss timer must not cut short the next cast. +-- A stale loot timer must not cut short the next cast. handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) -handler(nil, "LOOT_OPENED") +press() stop() -handler(nil, "LOOT_CLOSED") handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) advance(2) check(overrides["F"] == "INTERACTTARGET", "old timer leaves the new cast alone")