-- OctoAI: writes a JSON snapshot of the current character for AI assistants. -- Target: WoW 1.12 engine (Lua 5.0), OctoWoW / Turtle WoW 1.18.1 client. -- Writing uses VanillaHelpers.dll WriteFile() -> \VanillaHelpersData\octo_.json -- Quest IDs use ClassicAPI.dll GetQuestIDForLogIndex() when available. -- Without WriteFile, the last snapshot is kept in SavedVariables (saved on logout or /reload). local VERSION = "1.3.0" local SCHEMA = 3 local PREFIX = "|cff66ccffOctoAI|r: " local db -- OctoAIDB, set on ADDON_LOADED local function msg(s) DEFAULT_CHAT_FRAME:AddMessage(PREFIX .. s) end local function nowMs() if type(debugprofilestop) == "function" then return debugprofilestop() end return GetTime() * 1000 end local function round(x, mult) if not x then return nil end return math.floor(x * mult + 0.5) / mult end --------------------------------------------------------------------------- -- Text helpers --------------------------------------------------------------------------- local function strip(s) if not s then return nil end s = string.gsub(s, "|c%x%x%x%x%x%x%x%x", "") s = string.gsub(s, "|r", "") s = string.gsub(s, "|H.-|h(.-)|h", "%1") return s end -- Cut a UTF-8 string to at most n bytes without splitting a character. local function utf8cut(s, n) if string.len(s) <= n then return s end local i = n while i > 0 do local b = string.byte(s, i + 1) if not b or b < 128 or b > 191 then break end i = i - 1 end return string.sub(s, 1, i) .. "..." end local function parseLink(link) if not link then return nil end local _, _, id = string.find(link, "item:(%d+)") local _, _, istr = string.find(link, "(item:[%-%d:]+)") local _, _, name = string.find(link, "%[(.-)%]") return tonumber(id), istr, name end local function isOn(v) return v and v ~= 0 end --------------------------------------------------------------------------- -- Hidden tooltip scanner --------------------------------------------------------------------------- local tip = CreateFrame("GameTooltip", "OctoAITip", nil, "GameTooltipTemplate") local function tipLines(method, a, b, c) local out = {} if not method then return out end tip:SetOwner(WorldFrame, "ANCHOR_NONE") tip:ClearLines() method(tip, a, b, c) local n = tip:NumLines() or 0 for i = 1, n do local l = getglobal("OctoAITipTextLeft" .. i) local r = getglobal("OctoAITipTextRight" .. i) local lt = l and l:GetText() local rt = r and r:IsShown() and r:GetText() if lt and lt ~= "" then if rt and rt ~= "" then lt = lt .. " | " .. rt end table.insert(out, strip(lt)) end end tip:Hide() return out end --------------------------------------------------------------------------- -- Minimal JSON encoder (Lua 5.0 safe) --------------------------------------------------------------------------- local function esc(s) return (string.gsub(s, '[%c"\\]', function(ch) if ch == '"' then return '\\"' elseif ch == "\\" then return "\\\\" elseif ch == "\n" then return "\\n" elseif ch == "\t" then return "\\t" elseif ch == "\r" then return "\\r" end return string.format("\\u%04x", string.byte(ch)) end)) end local function isArray(t) local count = 0 for k in pairs(t) do if type(k) ~= "number" then return false end count = count + 1 end return count == table.getn(t) end local INF = 1 / 0 local encode encode = function(v, buf) local tv = type(v) if tv == "nil" then table.insert(buf, "null") elseif tv == "boolean" then table.insert(buf, v and "true" or "false") elseif tv == "number" then if v ~= v or v == INF or v == -INF then table.insert(buf, "null") elseif math.floor(v) == v then table.insert(buf, string.format("%.0f", v)) else table.insert(buf, string.format("%.4f", v)) end elseif tv == "string" then table.insert(buf, '"' .. esc(v) .. '"') elseif tv == "table" then if isArray(v) then table.insert(buf, "[") for i = 1, table.getn(v) do if i > 1 then table.insert(buf, ",") end encode(v[i], buf) end table.insert(buf, "]") else local keys = {} for k in pairs(v) do table.insert(keys, tostring(k)) end table.sort(keys) table.insert(buf, "{") for i = 1, table.getn(keys) do local k = keys[i] local val = v[k] if val == nil then val = v[tonumber(k)] end if i > 1 then table.insert(buf, ",") end table.insert(buf, '"' .. esc(k) .. '":') encode(val, buf) end table.insert(buf, "}") end else table.insert(buf, "null") end end local function toJSON(v) local buf = {} encode(v, buf) return table.concat(buf) end --------------------------------------------------------------------------- -- Event suppression: our own expand/collapse calls fire update events. --------------------------------------------------------------------------- local suppressUntil = {} local function suppress(ev) suppressUntil[ev] = GetTime() + 1.5 end --------------------------------------------------------------------------- -- Section collectors (each runs inside pcall) --------------------------------------------------------------------------- local lastCoords local function getCharacter() local c = {} c.name = UnitName("player") c.realm = GetRealmName() local cls, clsToken = UnitClass("player") c.class = cls c.classToken = clsToken local race, raceToken = UnitRace("player") c.race = race c.raceToken = raceToken c.faction = UnitFactionGroup("player") c.level = UnitLevel("player") c.xp = UnitXP("player") c.xpMax = UnitXPMax("player") c.restedXP = GetXPExhaustion() local money = GetMoney() or 0 c.moneyCopper = money c.money = string.format("%dg %ds %dc", math.floor(money / 10000), math.floor(money / 100) - math.floor(money / 10000) * 100, money - math.floor(money / 100) * 100) c.guild = GetGuildInfo("player") c.healthMax = UnitHealthMax("player") c.powerMax = UnitManaMax("player") c.resting = IsResting() and true or false c.zone = GetRealZoneText() c.subzone = GetSubZoneText() if type(GetBindLocation) == "function" then c.hearth = GetBindLocation() end -- Never touch the map the player is looking at; reuse the last position instead. if not (WorldMapFrame and WorldMapFrame:IsVisible()) then SetMapToCurrentZone() local x, y = GetPlayerMapPosition("player") if x and (x > 0 or y > 0) then lastCoords = { x = round(x * 100, 10), y = round(y * 100, 10) } else lastCoords = nil end end c.coords = lastCoords if UnitExists("pet") then c.pet = { name = UnitName("pet"), level = UnitLevel("pet"), family = UnitCreatureFamily("pet") } pcall(function() if type(GetPetTrainingPoints) == "function" then local total, spent = GetPetTrainingPoints() c.pet.trainingPoints = { total = total, spent = spent } end if type(GetPetLoyalty) == "function" then c.pet.loyalty = GetPetLoyalty() end if type(GetPetHappiness) == "function" then c.pet.happiness = GetPetHappiness() end local spells, seen = {}, {} local i = 1 while true do local sname, srank = GetSpellName(i, "pet") if not sname then break end local key = sname .. (srank or "") if not seen[key] then seen[key] = true if srank and srank ~= "" then table.insert(spells, sname .. " (" .. srank .. ")") else table.insert(spells, sname) end end i = i + 1 if i > 60 then break end end c.pet.spells = spells end) end return c end local STAT_NAMES = { "Strength", "Agility", "Stamina", "Intellect", "Spirit" } local RES_NAMES = { [1] = "Holy", [2] = "Fire", [3] = "Nature", [4] = "Frost", [5] = "Shadow", [6] = "Arcane" } local function optional(fn, a) if type(fn) ~= "function" then return nil end local ok, v = pcall(fn, a) if ok then return v end return nil end local function getStats() local s = { primary = {}, resist = {} } for i = 1, 5 do local _, stat = UnitStat("player", i) s.primary[STAT_NAMES[i]] = stat end local _, armor = UnitArmor("player") s.armor = armor local apBase, apPos, apNeg = UnitAttackPower("player") if apBase then s.attackPower = apBase + (apPos or 0) + (apNeg or 0) end local rBase, rPos, rNeg = UnitRangedAttackPower("player") if rBase then s.rangedAttackPower = rBase + (rPos or 0) + (rNeg or 0) end for i = 1, 6 do local _, total = UnitResistance("player", i) s.resist[RES_NAMES[i]] = total end s.critChance = optional(GetCritChance) s.rangedCritChance = optional(GetRangedCritChance) s.spellCritChance = optional(GetSpellCritChance, 2) s.dodgeChance = optional(GetDodgeChance) s.parryChance = optional(GetParryChance) s.blockChance = optional(GetBlockChance) return s end local function getTalents() local T = { unspent = UnitCharacterPoints("player"), trees = {} } local spent = {} for tab = 1, GetNumTalentTabs() do local tname, _, tspent = GetTalentTabInfo(tab) local tree = { name = tname, spent = tspent, talents = {} } table.insert(spent, tspent or 0) for i = 1, GetNumTalents(tab) do local name, _, tier, col, rank, maxRank = GetTalentInfo(tab, i) local t = { name = name, tier = tier, col = col, rank = rank, max = maxRank } local pt, pc = GetTalentPrereqs(tab, i) if pt then t.requires = { tier = pt, col = pc } end t.tooltip = tipLines(tip.SetTalent, tab, i) table.insert(tree.talents, t) end table.insert(T.trees, tree) end T.build = table.concat(spent, "/") return T end local SLOT_NAMES = { [0] = "Ammo", [1] = "Head", [2] = "Neck", [3] = "Shoulder", [4] = "Shirt", [5] = "Chest", [6] = "Waist", [7] = "Legs", [8] = "Feet", [9] = "Wrist", [10] = "Hands", [11] = "Finger1", [12] = "Finger2", [13] = "Trinket1", [14] = "Trinket2", [15] = "Back", [16] = "MainHand", [17] = "OffHand", [18] = "Ranged", [19] = "Tabard", } local QUALITY = { [0] = "Poor", [1] = "Common", [2] = "Uncommon", [3] = "Rare", [4] = "Epic", [5] = "Legendary", [6] = "Artifact" } local function itemBasics(link) local id, istr, linkName = parseLink(link) local it = { id = id, name = linkName, itemString = istr } if id then local iname, _, quality, minLevel, itype, isub, _, equipLoc = GetItemInfo(id) if iname then -- the link name keeps random suffixes ("of the Monkey"); GetItemInfo returns the base name it.name = linkName or iname it.quality = QUALITY[quality] or quality if minLevel and minLevel > 0 then it.reqLevel = minLevel end it.type = itype it.subtype = isub if equipLoc and equipLoc ~= "" then it.equipLoc = equipLoc end end end return it end local function getEquipment() local E = {} for slot = 0, 19 do local link = GetInventoryItemLink("player", slot) if link then local it = itemBasics(link) it.slot = SLOT_NAMES[slot] it.tooltip = tipLines(tip.SetInventoryItem, "player", slot) if slot == 0 then it.count = GetInventoryItemCount("player", 0) end table.insert(E, it) end end return E end -- Scan containers. Every item gets its tooltip (gear per slot, other items once, -- merged by item string). Bank slots are read through the item link. local function scanContainers(bagList, isBank) local equippable, other, order = {}, {}, {} local bags = {} local free, total = 0, 0 for _, bag in ipairs(bagList) do local n = GetContainerNumSlots(bag) or 0 if n > 0 then local bname = (bag == 0 and "Backpack") or (bag == -1 and "Bank") or GetBagName(bag) table.insert(bags, { id = bag, name = bname, slots = n }) total = total + n for slot = 1, n do local link = GetContainerItemLink(bag, slot) if not link then free = free + 1 else local _, count = GetContainerItemInfo(bag, slot) local it = itemBasics(link) if not count or count < 1 then count = 1 end if it.equipLoc and it.equipLoc ~= "INVTYPE_AMMO" then it.count = count if isBank then it.tooltip = tipLines(tip.SetHyperlink, it.itemString or link) else it.tooltip = tipLines(tip.SetBagItem, bag, slot) end table.insert(equippable, it) else local key = it.itemString or it.name or link if other[key] then other[key].count = other[key].count + count else it.count = count if it.itemString then it.tooltip = tipLines(tip.SetHyperlink, it.itemString) end other[key] = it table.insert(order, key) end end end end end end local items = {} for _, k in ipairs(order) do table.insert(items, other[k]) end return { bags = bags, freeSlots = free, totalSlots = total, equippable = equippable, items = items } end local function getBags() return scanContainers({ 0, 1, 2, 3, 4 }, false) end local function snapshotBank() local list = { -1 } for b = 5, 10 do table.insert(list, b) end local ok, res = pcall(scanContainers, list, true) if ok and db then res.scannedAt = date("%Y-%m-%d %H:%M:%S") db.bank = res end end local function questIDForIndex(i) if type(GetQuestIDForLogIndex) == "function" then local ok, id = pcall(GetQuestIDForLogIndex, i) if ok and type(id) == "number" and id > 0 then return id end end if C_QuestLog and type(C_QuestLog.GetQuestIDForLogIndex) == "function" then local ok, id = pcall(C_QuestLog.GetQuestIDForLogIndex, i) if ok and type(id) == "number" and id > 0 then return id end end return nil end local function hasQuestIDs() return (type(GetQuestIDForLogIndex) == "function") or (C_QuestLog and type(C_QuestLog.GetQuestIDForLogIndex) == "function") and true or false end local function getQuests() local Q = {} local selected = GetQuestLogSelection() local collapsed, hadCollapsed = {}, false local n = GetNumQuestLogEntries() for i = 1, n do local title, _, _, isHeader, isCollapsed = GetQuestLogTitle(i) if isOn(isHeader) and isOn(isCollapsed) then collapsed[title] = true; hadCollapsed = true end end suppress("QUEST_LOG_UPDATE") if hadCollapsed then ExpandQuestHeader(0) end local header n = GetNumQuestLogEntries() for i = 1, n do local title, level, tag, isHeader, _, isComplete = GetQuestLogTitle(i) if isOn(isHeader) then header = title elseif title then local q = { title = title, level = level, header = header } if tag and tag ~= "" then q.tag = tag end if isComplete == 1 then q.status = "complete" elseif isComplete == -1 then q.status = "failed" else q.status = "in progress" end q.id = questIDForIndex(i) SelectQuestLogEntry(i) local desc, obj = GetQuestLogQuestText() q.objective = obj if desc then q.description = utf8cut(desc, 700) end local nb = GetNumQuestLeaderBoards(i) or 0 if nb > 0 then q.progress = {} for j = 1, nb do local text, otype, finished = GetQuestLogLeaderBoard(j, i) table.insert(q.progress, { text = text, type = otype, done = isOn(finished) and true or false }) end end table.insert(Q, q) end end if hadCollapsed then for i = GetNumQuestLogEntries(), 1, -1 do local title, _, _, isHeader = GetQuestLogTitle(i) if isOn(isHeader) and collapsed[title] then CollapseQuestHeader(i) end end end SelectQuestLogEntry(selected or 0) if QuestLogFrame and QuestLogFrame:IsVisible() and type(QuestLog_Update) == "function" then QuestLog_Update() end return Q end -- Expand collapsed headers bottom-up (indices above stay valid), read, then re-collapse by name. local function withExpanded(count, info, expand, collapse, event, reader) local collapsed = {} suppress(event) for i = count(), 1, -1 do local name, isHeader, isCollapsed = info(i) if isOn(isHeader) and isOn(isCollapsed) then collapsed[name] = true expand(i) end end local ok, res = pcall(reader) for i = count(), 1, -1 do local name, isHeader = info(i) if isOn(isHeader) and collapsed[name] then collapse(i) end end if not ok then error(res) end return res end --------------------------------------------------------------------------- -- Professions: recipes are only readable while the trade skill / craft -- window is open, so they are scanned then and kept in SavedVariables. --------------------------------------------------------------------------- local function linkId(link) if not link then return nil end local _, _, id = string.find(link, "item:(%d+)") if id then return tonumber(id) end local _, _, eid = string.find(link, "enchant:(%d+)") if eid then return "enchant:" .. eid end return nil end local function readyAt(cd) if not cd or cd <= 0 then return nil end local ok, v = pcall(function() return date("%Y-%m-%d %H:%M", time() + cd) end) if ok then return v end return nil end local function readTradeSkill() local pname, rank, maxRank = GetTradeSkillLine() if not pname or pname == "UNKNOWN" then return nil end local P = { name = pname, rank = rank, max = maxRank, kind = "tradeskill", recipes = {} } local header for i = 1, GetNumTradeSkills() do local sname, stype, numAvail = GetTradeSkillInfo(i) if stype == "header" then header = sname elseif sname then local r = { name = sname, difficulty = stype, header = header } if numAvail and numAvail > 0 then r.canMake = numAvail end r.itemId = linkId(GetTradeSkillItemLink(i)) local mn, mx = GetTradeSkillNumMade(i) if mn and mx and (mn ~= 1 or mx ~= 1) then if mn == mx then r.made = mn else r.made = mn .. "-" .. mx end end local cd = GetTradeSkillCooldown(i) if cd and cd > 0 then r.cooldownSec = math.floor(cd); r.readyAt = readyAt(cd) end r.reagents = {} for j = 1, (GetTradeSkillNumReagents(i) or 0) do local rn, _, rc, have = GetTradeSkillReagentInfo(i, j) local rg = { name = rn, count = rc, have = have } if type(GetTradeSkillReagentItemLink) == "function" then rg.id = linkId(GetTradeSkillReagentItemLink(i, j)) end table.insert(r.reagents, rg) end table.insert(P.recipes, r) end end return P end local function readCraft() local pname, rank, maxRank if type(GetCraftDisplaySkillLine) == "function" then pname, rank, maxRank = GetCraftDisplaySkillLine() end if not pname and type(GetCraftName) == "function" then pname = GetCraftName() end if not pname or pname == "UNKNOWN" then return nil end local P = { name = pname, rank = rank, max = maxRank, kind = "craft", recipes = {} } local header for i = 1, GetNumCrafts() do local cname, csub, ctype, numAvail, _, tpCost, reqLevel = GetCraftInfo(i) if ctype == "header" then header = cname elseif cname then local r = { name = cname, difficulty = ctype, header = header } if csub and csub ~= "" then r.rank = csub end if numAvail and numAvail > 0 then r.canMake = numAvail end if tpCost and tpCost > 0 then r.trainingPoints = tpCost end if reqLevel and reqLevel > 0 then r.reqLevel = reqLevel end if type(GetCraftItemLink) == "function" then r.itemId = linkId(GetCraftItemLink(i)) end r.reagents = {} for j = 1, (GetCraftNumReagents(i) or 0) do local rn, _, rc, have = GetCraftReagentInfo(i, j) local rg = { name = rn, count = rc, have = have } if type(GetCraftReagentItemLink) == "function" then rg.id = linkId(GetCraftReagentItemLink(i, j)) end table.insert(r.reagents, rg) end table.insert(P.recipes, r) end end return P end local function scanTradeSkill() return withExpanded(GetNumTradeSkills, function(i) local n, t, _, e = GetTradeSkillInfo(i); return n, t == "header", not isOn(e) end, ExpandTradeSkillSubClass, CollapseTradeSkillSubClass, "TRADE_SKILL_UPDATE", readTradeSkill) end local function scanCraft() return withExpanded(GetNumCrafts, function(i) local n, _, t, _, e = GetCraftInfo(i); return n, t == "header", not isOn(e) end, ExpandCraftSkillLine, CollapseCraftSkillLine, "CRAFT_UPDATE", readCraft) end --------------------------------------------------------------------------- -- Trainer window: what the open trainer teaches (all filters), with cost --------------------------------------------------------------------------- local function scanTrainer() if type(GetNumTrainerServices) ~= "function" then return nil end local saved = {} local types = { "available", "unavailable", "used" } if type(GetTrainerServiceTypeFilter) == "function" and type(SetTrainerServiceTypeFilter) == "function" then suppress("TRAINER_UPDATE") for _, t in ipairs(types) do if not isOn(GetTrainerServiceTypeFilter(t)) then saved[t] = true; SetTrainerServiceTypeFilter(t, 1) end end end if type(ExpandTrainerSkillLine) == "function" then ExpandTrainerSkillLine(0) end local T = { trainer = UnitName("npc"), services = {} } local header local ok, err = pcall(function() for i = 1, GetNumTrainerServices() do local name, sub, stype = GetTrainerServiceInfo(i) if stype == "header" then header = name elseif name then local sv = { name = name, status = stype, header = header } if sub and sub ~= "" then sv.rank = sub end local money = GetTrainerServiceCost(i) if money and money > 0 then sv.costCopper = money end if type(GetTrainerServiceSkillReq) == "function" then local skill, rank, has = GetTrainerServiceSkillReq(i) if skill then sv.requires = skill .. " " .. tostring(rank); sv.meetsSkill = isOn(has) and true or false end end if type(GetTrainerServiceLevelReq) == "function" then local lvl = GetTrainerServiceLevelReq(i) if lvl and lvl > 0 then sv.reqLevel = lvl end end table.insert(T.services, sv) end end end) for _, t in ipairs(types) do if saved[t] then SetTrainerServiceTypeFilter(t, 0) end end if not ok then error(err) end return T end local function getSkills() return withExpanded(GetNumSkillLines, function(i) local name, isHeader, isExpanded = GetSkillLineInfo(i); return name, isHeader, not isOn(isExpanded) end, ExpandSkillHeader, CollapseSkillHeader, "SKILL_LINES_CHANGED", function() local S, header = {}, nil for i = 1, GetNumSkillLines() do local name, isHeader, _, rank, _, modifier, maxRank = GetSkillLineInfo(i) if isOn(isHeader) then header = name elseif name then local s = { name = name, category = header, rank = rank, max = maxRank } if modifier and modifier ~= 0 then s.bonus = modifier end table.insert(S, s) end end return S end) end local function getReputation() return withExpanded(GetNumFactions, function(i) local name, _, _, _, _, _, _, _, isHeader, isCollapsed = GetFactionInfo(i); return name, isHeader, isCollapsed end, ExpandFactionHeader, CollapseFactionHeader, "UPDATE_FACTION", function() local R, header = {}, nil for i = 1, GetNumFactions() do local name, _, standingID, barMin, barMax, barValue, atWar, _, isHeader = GetFactionInfo(i) if isOn(isHeader) then header = name elseif name then local r = { name = name, group = header, standing = getglobal("FACTION_STANDING_LABEL" .. (standingID or 0)) } if barMax and barMin and barValue then r.progress = (barValue - barMin) .. "/" .. (barMax - barMin) end if isOn(atWar) then r.atWar = true end table.insert(R, r) end end return R end) end local function getSpells() local tabs = {} for t = 1, GetNumSpellTabs() do local tname, _, offset, num = GetSpellTabInfo(t) local ranks, order = {}, {} for i = offset + 1, offset + num do local sname, srank = GetSpellName(i, BOOKTYPE_SPELL) if sname then if ranks[sname] == nil then table.insert(order, sname) end ranks[sname] = srank or "" end end local list = {} for _, sname in ipairs(order) do local r = ranks[sname] if r and r ~= "" then table.insert(list, sname .. " (" .. r .. ")") else table.insert(list, sname) end end local entry = { tab = tname, spells = list } -- full tooltip of the highest rank of every spell (ranks are listed low to high) local lastIdx = {} for i = offset + 1, offset + num do local sname = GetSpellName(i, BOOKTYPE_SPELL) if sname then lastIdx[sname] = i end end entry.details = {} for sname, i in pairs(lastIdx) do entry.details[sname] = tipLines(tip.SetSpell, i, BOOKTYPE_SPELL) end table.insert(tabs, entry) end return tabs end local function getBuffs() local B = {} for i = 0, 31 do local idx = GetPlayerBuff(i, "HELPFUL") if not idx or idx < 0 then break end local lines = tipLines(tip.SetPlayerBuff, idx) if lines[1] then table.insert(B, lines[1]) end end return B end --------------------------------------------------------------------------- -- Snapshot with per-section caching --------------------------------------------------------------------------- local SECTIONS = { { "character", getCharacter, true }, -- third field: refresh on every export (cheap) { "stats", getStats, true }, { "buffs", getBuffs, true }, { "talents", getTalents }, { "equipment", getEquipment }, { "bags", getBags }, { "quests", getQuests }, { "skills", getSkills }, { "reputation", getReputation }, { "spells", getSpells }, } local cache, stale = {}, {} local function buildSnapshot(reason, full) local snap = { schema = SCHEMA, addonVersion = VERSION, server = "OctoWoW", client = "Turtle WoW 1.18.1 based client (WoW 1.12 engine)", exportedAt = date("%Y-%m-%d %H:%M:%S"), trigger = reason, } local errs = {} for _, sec in ipairs(SECTIONS) do local key, fn, always = sec[1], sec[2], sec[3] if full or always or stale[key] or cache[key] == nil then local ok, res = pcall(fn) if ok then cache[key] = res stale[key] = nil else table.insert(errs, key .. ": " .. tostring(res)) end end snap[key] = cache[key] end if db and db.bank then snap.bank = db.bank end if db and db.professions then snap.professions = db.professions end if db and db.trainers then snap.trainers = db.trainers end if db and db.flights then local fl = {} for name in pairs(db.flights) do table.insert(fl, name) end table.sort(fl) snap.flights = fl end snap.questIdSource = hasQuestIDs() and "ClassicAPI" or "none" snap.errors = errs return snap, errs end local function safeName(name) -- WriteFile rejects < > : " / \ | ? * ; keep the file name plain ASCII return (string.gsub(name or "unknown", "[^%w%-_]", "_")) end local function writeFile(name, content) if type(WriteFile) ~= "function" then return false, "WriteFile() not available (VanillaHelpers.dll not loaded)" end local ok, r1, r2 = pcall(WriteFile, name, "w", content) if not ok then return false, tostring(r1) end if r1 == false or (r1 == nil and r2) then return false, tostring(r2) end return true end local busy = false local warnedWrite, warnedErr = false, false local function doExport(reason, quiet, full) if busy then return end busy = true local t0 = nowMs() local ok, snap, errs = pcall(buildSnapshot, reason, full) local json if ok then ok, json = pcall(toJSON, snap) else json = snap end if not ok then busy = false msg("|cffff5555export failed:|r " .. tostring(json)) return end local fname = "octo_" .. safeName(UnitName("player")) .. ".json" local written, werr = writeFile(fname, json) local dt = nowMs() - t0 if db then db.last = json db.lastExport = snap.exportedAt db.lastMs = dt end busy = false local kb = string.format("%.1f KB", string.len(json) / 1024) if written then if not quiet or (db and db.verbose) then msg(string.format("exported %s in %d ms -> VanillaHelpersData\\%s", kb, dt, fname)) end elseif not quiet or not warnedWrite then warnedWrite = true msg("|cffffaa00file write unavailable|r (" .. tostring(werr) .. "). Snapshot kept in SavedVariables; it is saved on logout or /reload.") end if errs and table.getn(errs) > 0 and (not quiet or not warnedErr) then warnedErr = true msg("|cffffaa00" .. table.getn(errs) .. " section(s) failed:|r " .. errs[1]) end end --------------------------------------------------------------------------- -- Auto export: event driven, debounced, never in combat --------------------------------------------------------------------------- local FAST_DEBOUNCE, FAST_MAXWAIT, SLOW_DELAY = 3, 15, 30 local fastDue, fastFirst, slowDue, lastReason = nil, nil, nil, nil local function markDirty(reason, sections, slow, delay) if sections then for _, s in ipairs(sections) do stale[s] = true end end if not db or not db.auto then return end local now = GetTime() lastReason = reason if slow then if not slowDue then slowDue = now + SLOW_DELAY end else if not fastDue then fastFirst = now end local due = now + (delay or FAST_DEBOUNCE) if fastDue and fastDue > due then due = fastDue end if due > fastFirst + FAST_MAXWAIT and not delay then due = fastFirst + FAST_MAXWAIT end fastDue = due end end local profDue, profKind = nil, nil local bankDue = nil local loggedIn, bankOpen = false, false local warnedProf = false local function scanProfession(kind) local ok, P if kind == "craft" then ok, P = pcall(scanCraft) else ok, P = pcall(scanTradeSkill) end if not ok then if not warnedProf then warnedProf = true; msg("|cffffaa00profession scan failed:|r " .. tostring(P)) end return end if not P or not db then return end P.scannedAt = date("%Y-%m-%d %H:%M:%S") db.professions = db.professions or {} local old = db.professions[P.name] db.professions[P.name] = P if not old and (not db.verbose) then msg("saved " .. table.getn(P.recipes) .. " " .. P.name .. " recipes") end markDirty("profession:" .. P.name, nil) end local function saveTrainer() local ok, T = pcall(scanTrainer) if not ok then msg("|cffffaa00trainer scan failed:|r " .. tostring(T)); return end if not T or not T.trainer or not db then return end T.scannedAt = date("%Y-%m-%d %H:%M:%S") db.trainers = db.trainers or {} db.trainers[T.trainer] = T markDirty("trainer", nil) end -- Flight master map: remember the flight paths this character knows local function saveFlights() if type(NumTaxiNodes) ~= "function" or not db then return end db.flights = db.flights or {} for i = 1, (NumTaxiNodes() or 0) do local name = TaxiNodeName(i) local t = (type(TaxiNodeGetType) == "function") and TaxiNodeGetType(i) or "REACHABLE" if name and name ~= "" then if t == "NONE" then db.flights[name] = nil else db.flights[name] = true end end end markDirty("flights", nil) end local function scheduleProfession(kind) profKind = kind profDue = GetTime() + 0.7 end local frame = CreateFrame("Frame", "OctoAIEventFrame") local elapsedAcc = 0 frame:SetScript("OnUpdate", function() elapsedAcc = elapsedAcc + (arg1 or 0) if elapsedAcc < 0.5 then return end elapsedAcc = 0 local now = GetTime() if bankDue and now >= bankDue then bankDue = nil if bankOpen then snapshotBank(); markDirty("bank", { "bags" }, true) end end if profDue and now >= profDue and not UnitAffectingCombat("player") then local k = profKind profDue, profKind = nil, nil scanProfession(k) end if ((fastDue and now >= fastDue) or (slowDue and now >= slowDue)) and not UnitAffectingCombat("player") then fastDue, slowDue = nil, nil doExport("auto:" .. (lastReason or "?"), true) end end) local ALL_SECTIONS = { "talents", "equipment", "bags", "quests", "skills", "reputation", "spells" } local EVENTS = { CHARACTER_POINTS_CHANGED = { "talents", { "talents" } }, PLAYER_LEVEL_UP = { "level", ALL_SECTIONS }, ZONE_CHANGED_NEW_AREA = { "zone", nil }, SKILL_LINES_CHANGED = { "skills", { "skills" } }, SPELLS_CHANGED = { "spells", { "spells", "talents" } }, UPDATE_FACTION = { "reputation", { "reputation" } }, QUEST_LOG_UPDATE = { "quests", { "quests" } }, BAG_UPDATE = { "bags", { "bags" }, true }, PLAYER_MONEY = { "money", nil, true }, } frame:RegisterEvent("ADDON_LOADED") frame:RegisterEvent("PLAYER_ENTERING_WORLD") frame:RegisterEvent("UNIT_INVENTORY_CHANGED") frame:RegisterEvent("BANKFRAME_OPENED") frame:RegisterEvent("BANKFRAME_CLOSED") frame:RegisterEvent("PLAYERBANKSLOTS_CHANGED") frame:RegisterEvent("TRADE_SKILL_SHOW") frame:RegisterEvent("TRADE_SKILL_UPDATE") frame:RegisterEvent("CRAFT_SHOW") frame:RegisterEvent("CRAFT_UPDATE") frame:RegisterEvent("TRADE_SKILL_CLOSE") frame:RegisterEvent("CRAFT_CLOSE") frame:RegisterEvent("TRAINER_SHOW") frame:RegisterEvent("TAXIMAP_OPENED") for ev in pairs(EVENTS) do frame:RegisterEvent(ev) end frame:SetScript("OnEvent", function() if event == "ADDON_LOADED" then if arg1 == "OctoAI" then OctoAIDB = OctoAIDB or {} db = OctoAIDB if db.auto == nil then db.auto = true end end return end if suppressUntil[event] and GetTime() < suppressUntil[event] then return end if event == "PLAYER_ENTERING_WORLD" then if not loggedIn then loggedIn = true markDirty("login", ALL_SECTIONS, false, 8) end elseif not loggedIn then return elseif event == "UNIT_INVENTORY_CHANGED" then if arg1 == "player" then markDirty("equipment", { "equipment", "bags" }) end elseif event == "BANKFRAME_OPENED" then bankOpen = true snapshotBank() markDirty("bank", { "bags" }) elseif event == "BANKFRAME_CLOSED" then bankOpen = false bankDue = nil elseif event == "TRADE_SKILL_SHOW" then -- read right away: the window may be closed again before a delayed scan runs scanProfession("tradeskill") elseif event == "CRAFT_SHOW" then scanProfession("craft") elseif event == "TRADE_SKILL_UPDATE" then scheduleProfession("tradeskill") elseif event == "CRAFT_UPDATE" then scheduleProfession("craft") elseif event == "TRAINER_SHOW" then saveTrainer() elseif event == "TAXIMAP_OPENED" then pcall(saveFlights) elseif event == "TRADE_SKILL_CLOSE" or event == "CRAFT_CLOSE" then profDue, profKind = nil, nil elseif event == "PLAYERBANKSLOTS_CHANGED" then if bankOpen then bankDue = GetTime() + 0.6 end else local e = EVENTS[event] if e then if event == "BAG_UPDATE" and bankOpen then bankDue = GetTime() + 0.6 end markDirty(e[1], e[2], e[3]) end end end) --------------------------------------------------------------------------- -- Item tooltip cache: every item the player looks at is remembered (account wide, -- SavedVariables OctoAIItems). Lets an AI read exact stats of items you hovered -- in the auction house, at vendors, in chat links, loot rolls and so on. -- Entry format (one string, easy to parse): "