diff --git a/FishKey/FishKey.lua b/FishKey/FishKey.lua index 77b2128..21539af 100644 --- a/FishKey/FishKey.lua +++ b/FishKey/FishKey.lua @@ -1,25 +1,46 @@ -- Fish Key: while Fishing channels, every key bound to Fishing is overridden to --- "Interact with Target", so the same key clicks the bobber, and the Interact --- Key setting is switched on for the cast. The override is --- dropped the moment the channel ends (catch, miss, timeout, move) or combat --- starts. Saved keybinds are never touched. +-- "Interact with Target", so the same key clicks the bobber. A few settings are +-- changed for the cast (Interact Key on, longer interact range, press and hold +-- casting off). Saved keybinds and settings are never touched. +-- +-- Everything is put back only once the catch is looted (or the cast ended +-- 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. local FISHING_SPELL = 7620 --- Settings raised while fishing, as temp CVars that are never saved, and --- dropped again when the cast ends. Each is only raised if it is below the value. +-- How long to wait for the loot window after the cast ends before treating it +-- as a miss. +local LOOT_WAIT = 1.5 +local POLL = 0.05 + +-- Settings changed while fishing, as temp CVars that are never saved, and +-- removed again afterwards. "min" raises to at least the value, "max" lowers +-- to at most it; a setting already there is left alone. local FISHING_CVARS = { -- Interact Key: 0 off, 1 gamepad, 2 mouse and keyboard, 3 always. - { name = "SoftTargetInteract", value = 3 }, + { name = "SoftTargetInteract", value = 3, mode = "min" }, -- The default (about 10 yards) is shorter than a cast, so the bobber -- would never become the interact target. - { name = "SoftTargetInteractRange", value = 30 }, + { name = "SoftTargetInteractRange", value = 30, mode = "min" }, + -- Press and hold casting repeats the spell while the key is held and + -- casts again on release. + { name = "ActionButtonUseKeyHeldSpell", value = 0, mode = "max" }, } local owner = CreateFrame("Frame") local overridden = false local clearAfterCombat = false -local raisedCVars = {} +local changedCVars = {} local warned = false +-- Keys currently overridden to interact. +local activeKeys = {} +-- "fishing" while the channel runs, "ending" while waiting for loot or key +-- release, nil otherwise. +local state +local looting = false +-- Bumped on every new cast and restore, so stale timers do nothing. +local generation = 0 local function Print(msg) DEFAULT_CHAT_FRAME:AddMessage("|cff4fc3f7Fish Key|r: " .. msg) @@ -104,43 +125,53 @@ local function FishingKeys() return keys end -local function RaiseCVars() +-- "SHIFT-F" -> "F", "CTRL--" -> "-". +local function BaseKey(key) + local base = key + repeat + local stripped = base:gsub("^ALT%-(.)", "%1"):gsub("^CTRL%-(.)", "%1") + :gsub("^SHIFT%-(.)", "%1"):gsub("^META%-(.)", "%1") + local changed = stripped ~= base + base = stripped + until not changed + return base +end + +local function AnyKeyHeld() + for _, key in ipairs(activeKeys) do + if IsKeyDown(BaseKey(key)) then + return true + end + end + return false +end + +local function ChangeCVars() for _, cvar in ipairs(FISHING_CVARS) do local current = tonumber(C_CVar.GetCVar(cvar.name)) - if current and current < cvar.value and not raisedCVars[cvar.name] then + local wrong = current and (cvar.mode == "min" and current < cvar.value or cvar.mode == "max" and current > cvar.value) + if wrong and not changedCVars[cvar.name] then if pcall(C_CVar.SetTempCVar, cvar.name, tostring(cvar.value)) then - raisedCVars[cvar.name] = true + changedCVars[cvar.name] = true elseif not warned then warned = true - Print("couldn't change " .. cvar.name .. ", turn Interact Key on in Settings") + Print("couldn't change " .. cvar.name .. ", set it in Settings") end end end end local function RestoreCVars() - for name in pairs(raisedCVars) do + for name in pairs(changedCVars) do pcall(C_CVar.RemoveTempCVar, name) - raisedCVars[name] = nil + changedCVars[name] = nil end end -local function Apply() - if not FishKeyDB.enabled or InCombatLockdown() then - return - end - local keys = FishingKeys() - if #keys == 0 then - return - end - for _, key in ipairs(keys) do - SetOverrideBinding(owner, true, key, "INTERACTTARGET") - end - overridden = true - RaiseCVars() -end - local function Restore() + generation = generation + 1 + state = nil + looting = false if overridden then if InCombatLockdown() then clearAfterCombat = true @@ -149,12 +180,56 @@ local function Restore() overridden = false end end + activeKeys = {} RestoreCVars() 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 + return + end + if AnyKeyHeld() then + C_Timer.After(POLL, function() TryFinish(gen) end) + return + end + Restore() +end + +local function Apply() + if not FishKeyDB.enabled or InCombatLockdown() then + return + end + generation = generation + 1 + looting = false + local keys = FishingKeys() + if #keys == 0 then + return + end + for _, key in ipairs(keys) do + SetOverrideBinding(owner, true, key, "INTERACTTARGET") + end + activeKeys = keys + overridden = true + state = "fishing" + ChangeCVars() +end + +local function ChannelEnded() + if state ~= "fishing" then + return + 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) +end + owner:RegisterEvent("ADDON_LOADED") owner:RegisterEvent("PLAYER_REGEN_DISABLED") owner:RegisterEvent("PLAYER_REGEN_ENABLED") +owner:RegisterEvent("LOOT_OPENED") +owner:RegisterEvent("LOOT_CLOSED") owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_START", "player") owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_STOP", "player") owner:SetScript("OnEvent", function(_, event, arg1, _, spellID) @@ -169,7 +244,19 @@ owner:SetScript("OnEvent", function(_, event, arg1, _, spellID) if IsFishing(spellID) then Apply() end - elseif event == "UNIT_SPELLCAST_CHANNEL_STOP" or event == "PLAYER_REGEN_DISABLED" then + elseif event == "UNIT_SPELLCAST_CHANNEL_STOP" then + ChannelEnded() + elseif event == "LOOT_OPENED" then + -- The loot can arrive just before or just after the channel stops. + if state then + looting = true + end + elseif event == "LOOT_CLOSED" then + if looting then + looting = false + TryFinish(generation) + end + elseif event == "PLAYER_REGEN_DISABLED" then -- Bindings still change inside PLAYER_REGEN_DISABLED; lockdown starts after it. Restore() elseif event == "PLAYER_REGEN_ENABLED" and clearAfterCombat then diff --git a/README.md b/README.md index 3af70ed..357570f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Seven small quality-of-life addons for WoW Forever (client 1.60.1, Interface 160 | Quest Shopping List | A panel beside the auction house listing items your quests still need, with cost against the quest's reward. Click to search. Requires [Auctionator](https://www.curseforge.com/wow/addons/auctionator). | `/qsl` | | Dungeon Pre-Quest | Marks dungeon quests and quests whose chain leads to one, in the quest log, NPC quest lists and the quest offer window, with a purple "!" on the minimap for ones you can pick up now. | `/dpq ` | | Ship Routes | Draws every boat, zeppelin and airship route on the world map, coloured by faction. Hover a dock marker to see where it sails and light up its route, click it to open the destination's map. On a continent or zone map, a route that sails off it ends in a marker at the edge naming where it goes. | `/routes`, `/routes mine`, `/routes all` | -| Fish Key | While you fish, the key you cast Fishing with clicks the bobber instead. It casts Fishing again once the cast ends. It also turns the Interact Key setting on for the cast, then off again. | `/fish`, `/fish on`, `/fish off` | +| Fish Key | While you fish, the key you cast Fishing with clicks the bobber instead. It casts Fishing again once the fish is looted and the key is released. For the cast it turns the Interact Key setting on and press and hold casting off, then puts both back. | `/fish`, `/fish on`, `/fish off` | ## Install diff --git a/tests/test_fk.lua b/tests/test_fk.lua index 3f82eac..29e723f 100644 --- a/tests/test_fk.lua +++ b/tests/test_fk.lua @@ -38,7 +38,30 @@ function ClearOverrideBindings() overrides = {} end local inCombat = false function InCombatLockdown() return inCombat end -local cvars = { SoftTargetInteract = "0", SoftTargetInteractRange = "10" } +local held = {} +function IsKeyDown(key) + assert(not key:find("^SHIFT%-") and not key:find("^CTRL%-") and not key:find("^ALT%-"), "modifier passed to IsKeyDown: " .. key) + return held[key] or false +end + +local clock, timers = 0, {} +C_Timer = { After = function(sec, fn) timers[#timers + 1] = { at = clock + sec, fn = fn } end } +local function advance(sec) + local target = clock + sec + while true do + local nextIndex + for i, t in ipairs(timers) do + if t.at <= target and (not nextIndex or t.at < timers[nextIndex].at) then nextIndex = i end + end + if not nextIndex then break end + local t = table.remove(timers, nextIndex) + clock = t.at + t.fn() + end + clock = target +end + +local cvars = { SoftTargetInteract = "0", SoftTargetInteractRange = "10", ActionButtonUseKeyHeldSpell = "1" } local temp = {} C_CVar = { GetCVar = function(n) return temp[n] or cvars[n] end, @@ -64,6 +87,9 @@ local function check(cond, what) print((cond and "ok " or "FAIL ") .. what) end local function count(t) local n = 0 for _ in pairs(t) do n = n + 1 end return n end +local function stop() handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7620) end +-- Cast ends with no loot and every key released: everything back to normal. +local function finish() stop(); advance(2) end assert(loadfile(arg[1]))("FishKey", {}) handler(nil, "ADDON_LOADED", "FishKey") @@ -80,10 +106,55 @@ check(count(overrides) == 5, "only Fishing keys overridden") check(C_CVar.GetCVar("SoftTargetInteractRange") == "30", "interact range raised while fishing") check(C_CVar.GetCVar("SoftTargetInteract") == "3", "Interact Key turned on while fishing") +local function restored() + return count(overrides) == 0 and C_CVar.GetCVar("SoftTargetInteract") == "0" + and C_CVar.GetCVar("SoftTargetInteractRange") == "10" and C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "1" +end +check(C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "press and hold casting off while fishing") + +-- Catch with the key held through the loot (press and hold, slow release). +held["F"] = true handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7731) -check(count(overrides) == 0, "keys back to Fishing when the channel ends") -check(C_CVar.GetCVar("SoftTargetInteractRange") == "10", "range restored") -check(C_CVar.GetCVar("SoftTargetInteract") == "0", "Interact Key off again") +check(overrides["F"] == "INTERACTTARGET", "still interact right after the channel ends") +handler(nil, "LOOT_OPENED") +advance(2) +check(overrides["F"] == "INTERACTTARGET", "still interact while the loot window is open") +handler(nil, "LOOT_CLOSED") +advance(0.3) +check(overrides["F"] == "INTERACTTARGET" and C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "still interact while the key is held") +held["F"] = nil +advance(0.1) +check(restored(), "key and settings restored once looted and released") + +-- Miss: no loot window. +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") + +-- Loot arrives before the channel stop, modifier key held. +handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) +handler(nil, "LOOT_OPENED") +stop() +held["1"] = true +handler(nil, "LOOT_CLOSED") +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") + +-- A stale miss timer must not cut short the next cast. +handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) +handler(nil, "LOOT_OPENED") +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") +finish() handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 133) check(count(overrides) == 0, "other channels ignored") @@ -99,18 +170,18 @@ inCombat = false cvars.SoftTargetInteractRange = "40" handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(temp.SoftTargetInteractRange == nil, "longer range left alone") -handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7620) +finish() cvars.SoftTargetInteract = "3" handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(temp.SoftTargetInteract == nil, "Interact Key already on is left alone") -handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7620) +finish() cvars.SoftTargetInteract = "0" C_CVar.SetTempCVar = function() error("secure cvar") end handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620) check(overrides["F"] == "INTERACTTARGET" and printed[#printed]:find("Settings", 1, true), "blocked setting warns, key still overridden") -handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7620) +finish() C_CVar.SetTempCVar = function(n, v) temp[n] = v end SlashCmdList.FISHKEY("off")