Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions addon/Polestar/Core/Core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<Script file="StepOrder.lua"/>
<Script file="Engine.lua"/>
<Script file="Reroute.lua"/>
<Script file="Course/Course.lua"/>
<Script file="Course/Index.lua"/>
<Script file="Course/Snapshot.lua"/>
<Script file="Course/Sources.lua"/>
<Script file="ChainValue.lua"/>
<Script file="OnTheWay.lua"/>
<Script file="Advice.lua"/>
Expand Down
276 changes: 276 additions & 0 deletions addon/Polestar/Core/Course/Course.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
-- The Course: the one door for the fork's planned moves of the arrow and of the step on screen (design:
-- docs/COURSE.md). A module that plans (the step reorder, the arrow that takes nearby objectives, a Next stop
-- trip) does not write `pin`, `aim` or the arrow's override itself. It calls Course.Move, which runs the write,
-- records who made it and why, and, with the switch fork.courseClaims on, lets one planner at a time hold the
-- arrow.
--
-- Course.Move(who, what, arg, why, apply, alive, detail) -> ok, result
-- who "reorder" | "aim" | "trip" | "player": the module that moves
-- what "view" (the step on screen) | "aim" (the goal the arrow leads to) | "trip" (a place the arrow is led to)
-- arg what it is set to (a step, a goal, a place); nil lets go
-- why one short line for the routing report
-- apply the write itself: a function; it returns false when it could not write
-- alive optional: returns whether the move still holds; a claim whose move is gone is dropped
-- detail optional: what the planner weighed, see "A decision" below
-- With the claims on, a planner that moves while another holds the arrow is refused (apply does not run). The
-- player is never refused, and a release always runs.
-- Course.Note(who, why, detail, result) a decision that moved nothing: the planner kept things as they are, and why
-- Course.Rest(id, scope, why) the player walked off an offer, or said "not now": it rests. scope "step" rests it
-- until the live step changes, "area" until the zone changes. A second rest of the
-- same offer in the same zone makes it "area". Every planner reads it, so one
-- planner's rest holds for all (only with the claims switch on)
-- Course.Rested(id) -> rested, scope whether offer `id` rests now ("q:<quest>:<role>" or a line key)
-- Course.Last(who) -> entry | nil the latest decision of one planner
-- Course.Log() -> { entry, ... } oldest first, the last Course.MAX decisions
-- entry = { at, clock, who, what, arg, why, result, heldBy, n, offers, score, wrote }
-- result: "done" | "failed" | "refused" | "released" | "kept" | "offered" | "rested"
-- n how many polls in a row gave this same answer ("kept" entries fold into one)
--
-- A decision (the `detail` of Move and Note, copied into the entry; numbers and ids only, never a name):
-- offers { { source, id, xp, work, walk, chain, score, rank, verdict }, ... } at most Course.OFFERS
-- source who listed it ("aim", "reorder", a source id)
-- id "q:<quest>" or "s:<step>"
-- xp what the work pays (the planner's own unit: a line's value, a step's XP)
-- work seconds of work at the place
-- walk seconds of walking it adds
-- chain the score after one of the guide's next stops, when the planner measured it
-- score the planner's score for it
-- rank its place in the order the planner chose (1 is first)
-- verdict "won" | "lost" | "held" | "first" | "later"
-- score the numbers of the choice as a whole (best, saved, base, cost, later, limit ...)
-- wrote what was written, filled by the Course from `arg` and `detail.wrote`: { step, quest, kind, released }
-- Course.Holder() -> { who, what, since } | nil the claim now
-- Course.ClearLog() forget the decisions (Clear routing report); the claim stays
-- Course.Clear() forget the decisions and the claim (the tests)
-- Course.On() / Course.ClaimsOn() the switches fork.routingReport (the log) and fork.courseClaims
--
-- Plain tables only; the log never holds a name.

local ADDON, ns = ...
local type, tostring, ipairs, pairs = type, tostring, ipairs, pairs

local Course = {}
ns.Course = Course

Course.MAX = 32 -- decisions kept
Course.OFFERS = 8 -- offers kept in one decision
Course.CLAIM_SECS = 60 -- a claim older than this is dropped, whatever it says
Course.PLAYER = "player"

local log, count = {}, 0
local claim -- { who, what, since, alive }

local function now() return GetTime and GetTime() or 0 end

-- The decision log is part of the routing report: its switch (fork.routingReport) turns the log on and off.
function Course.On() return ns.Fork == nil or ns.Fork.On("routingReport") end
function Course.ClaimsOn() return ns.Fork ~= nil and ns.Fork.On("courseClaims") end

-- A short, name-free word for what a move was set to.
local function describe(arg)
local t = type(arg)
if t == "number" or t == "boolean" or t == "string" then return tostring(arg) end
if t == "table" then
if arg.quest then return "quest " .. tostring(arg.quest) end
if arg.name then return "place" end
return "goal"
end
return nil
end

-- A plain copy of what a planner weighed: only the known number and word fields, so no table of the game stays here.
local NUMS = { "xp", "work", "walk", "chain", "score", "rank" }
local WORDS = { "source", "id", "verdict" }
local function copyOffers(offers)
if type(offers) ~= "table" then return nil end
local out = {}
for i = 1, #offers do
if i > Course.OFFERS then break end
local o, c = offers[i], {}
for _, k in ipairs(NUMS) do
if type(o[k]) == "number" then c[k] = o[k] end
end
for _, k in ipairs(WORDS) do
if type(o[k]) == "string" then c[k] = o[k] end
end
out[i] = c
end
return out
end

local function copyScore(score)
if type(score) ~= "table" then return nil end
local out = {}
for k, v in pairs(score) do
if type(k) == "string" and (type(v) == "number" or type(v) == "string" or type(v) == "boolean") then out[k] = v end
end
return out
end

-- What a move wrote, from what it was set to: a step, a goal (its quest) or nothing (the arrow went back to the guide).
local function wroteOf(arg, detail)
local w = {}
if arg == nil then
w.released = true
elseif type(arg) == "number" then
w.step = arg
elseif type(arg) == "table" then
w.quest = arg.quest
w.kind = arg.t
end
local d = detail and detail.wrote
if type(d) == "table" then
for k, v in pairs(d) do
if type(v) == "number" or type(v) == "string" or type(v) == "boolean" then w[k] = v end
end
end
return w
end

local function push(e)
if count >= Course.MAX then
table.remove(log, 1)
count = count - 1
end
count = count + 1
log[count] = e
end

local function record(who, what, arg, why, result, heldBy, detail)
if not Course.On() then return end
push({
at = now(), clock = date and date("%H:%M:%S") or nil, who = who, what = what, arg = describe(arg),
why = why, result = result, heldBy = heldBy, n = 1,
offers = detail and copyOffers(detail.offers) or nil, score = detail and copyScore(detail.score) or nil,
wrote = (result == "done" or result == "released") and wroteOf(arg, detail) or nil,
})
end

-- A decision that moved nothing. The same answer from the same planner as its last one folds into that entry
-- (n counts the polls), so a planner asked every second does not push the moves out of the ring.
function Course.Note(who, why, detail, result)
result = result or "kept"
if not Course.On() then return end
for i = count, 1, -1 do
local e = log[i]
if e.who == who then
if e.result == result and e.why == why then
e.n, e.at, e.clock = e.n + 1, now(), date and date("%H:%M:%S") or nil
e.offers = detail and copyOffers(detail.offers) or e.offers
e.score = detail and copyScore(detail.score) or e.score
return
end
break
end
end
record(who, nil, nil, why, result, nil, detail)
end

-- The claim now, dropped when it is old or its move is gone.
function Course.Holder()
if claim and (now() - claim.since > Course.CLAIM_SECS or (claim.alive and not claim.alive())) then claim = nil end
return claim
end

function Course.Move(who, what, arg, why, apply, alive, detail)
local release = arg == nil
local held = Course.Holder()
if not release and who ~= Course.PLAYER and held and held.who ~= who and Course.ClaimsOn() then
record(who, what, arg, why, "refused", held.who, detail)
return false
end
local res = apply and apply()
if res == false then
record(who, what, arg, why, "failed", held and held.who or nil, detail)
return false, res
end
if release then
if claim and claim.who == who then claim = nil end
record(who, what, arg, why, "released", nil, detail)
else
claim = { who = who, what = what, since = now(), alive = alive }
record(who, what, arg, why, "done", held and held.who ~= who and held.who or nil, detail)
end
return true, res
end

-- Rests: offer id -> { scope, step, zone }; strikes: offer id -> rests in this zone. One list for every planner.
local rests, strikes = {}, {}
local zoneOf

local function zone()
return GetRealZoneText and GetRealZoneText() or ""
end

local function liveStep()
local E = ns.Engine
return E and E.GetStepIndex and E.GetStepIndex() or nil
end

function Course.Rest(id, scope, why)
if type(id) ~= "string" or not Course.ClaimsOn() then return false end
local z = zone()
-- A new zone starts the counts again.
if zoneOf ~= z then
zoneOf = z
for k in pairs(strikes) do strikes[k] = nil end
for k, r in pairs(rests) do
if r.scope == "area" then rests[k] = nil end
end
end
local n = (strikes[id] or 0) + 1
strikes[id] = n
scope = (n >= 2 or scope == "area") and "area" or "step"
rests[id] = { scope = scope, step = liveStep(), zone = z }
Course.Note("course", ("%s rests until the %s changes%s"):format(id, scope == "area" and "zone" or "step", why and (": " .. why) or ""),
{ offers = { { id = id, verdict = "rested" } }, score = { strikes = n } }, "rested")
return true
end

function Course.Rested(id)
local r = id and rests[id]
if not r then return false end
if r.scope == "area" then
if zone() ~= r.zone then
rests[id] = nil
return false
end
elseif liveStep() ~= r.step then
rests[id] = nil
return false
end
return true, r.scope
end

-- The latest entry of one planner, or nil.
function Course.Last(who)
for i = count, 1, -1 do
if log[i].who == who then return log[i] end
end
return nil
end

function Course.Log()
local out = {}
for i = 1, count do out[i] = log[i] end
return out
end

-- Forget the decisions only: what the report shows of the past, never who holds the arrow (Clear routing report).
function Course.ClearLog()
for i = 1, count do log[i] = nil end
count = 0
end

function Course.Clear()
Course.ClearLog()
claim = nil
for k in pairs(rests) do rests[k] = nil end
for k in pairs(strikes) do strikes[k] = nil end
end

-- A Recalibrate chooses again from scratch: nobody holds the arrow and nothing rests.
ns.On("RECALIBRATED", function()
claim = nil
for k in pairs(rests) do rests[k] = nil end
end)
101 changes: 101 additions & 0 deletions addon/Polestar/Core/Course/Index.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
-- The Course's index of a guide: what the runtime needs to know about the guide's steps and quests, read once from
-- the compiled data and kept (a pure function of the steps; no game calls, no writes). Six places of the Engine
-- and the fork's planners each rebuilt parts of this; they can ask here.
--
-- Index.Of(guide) -> idx built on the first call, kept until Index.Forget(guide)
-- idx.keys[s] the stable key of step s: "<kind>:<what>" of its first goal, "#2", "#3" for a repeat
-- idx.byKey[key] the step with that key
-- idx.quests[q] { accept = { s, g }, turnin = { s, g }, work = { { s, g }, ... }, pre = { q, ... } }
-- the first accept and hand-in of quest q in the guide, every goal of its work, its prerequisites
-- idx.walls[s] why the order cannot move step s: "when", "skipto", "kind <t>", "escort", "timed", "group", "either"
-- (what the step reorder reads as a step that never moves; nil: the step may move)
-- Index.Key(step, seen) -> key the key of one step (seen: the keys given so far, to number repeats)
-- Index.Wall(step) -> reason | nil
--
-- The key does not change when the steps around it move, so a saved choice, a baked walk or a Course move can name a
-- step in a way that outlives a reorder of the guide.

local ADDON, ns = ...
local type, tostring, ipairs, pairs = type, tostring, ipairs, pairs

local Index = {}
ns.Course = ns.Course or {}
ns.Course.Index = Index

-- The goals that are work on a quest's objective.
Index.WORK = { kill = true, collect = true, interact = true, use = true, complete = true, cast = true, talk = true, explore = true }
-- The goals a step may hold and still be moved (the step reorder's list, Engine Own.ORDER_KINDS).
Index.MOVES = { accept = true, turnin = true, kill = true, collect = true, interact = true, talk = true, goto = true, note = true, complete = true, use = true }

local cache = setmetatable({}, { __mode = "k" })

function Index.Wall(step)
if step.when ~= nil then return "when" end
if step.skipto ~= nil then return "skipto" end
for _, goal in ipairs(step.goals or {}) do
if not Index.MOVES[goal.t] then return "kind " .. tostring(goal.t) end
if goal.escort then return "escort" end
if goal.limit then return "timed" end
if goal.group then return "group" end
if goal.either then return "either" end
end
return nil
end

function Index.Key(step, seen)
local goal = step.goals and step.goals[1]
local what = goal and (goal.quest or goal.npc or goal.area or goal.node or goal.text or goal.label) or step.title or ""
local key = (goal and goal.t or "step") .. ":" .. tostring(what)
if seen then
local n = (seen[key] or 0) + 1
seen[key] = n
if n > 1 then key = key .. "#" .. n end
end
return key
end

function Index.Build(guide)
local idx = { keys = {}, byKey = {}, quests = {}, walls = {} }
local seen = {}
for s, step in ipairs(guide.steps or {}) do
local key = Index.Key(step, seen)
idx.keys[s], idx.byKey[key] = key, s
idx.walls[s] = Index.Wall(step) or nil
for g, goal in ipairs(step.goals or {}) do
local q = goal.quest
if q then
local e = idx.quests[q]
if not e then
e = { work = {} }
idx.quests[q] = e
end
if goal.t == "accept" then
e.accept = e.accept or { s, g }
if goal.pre and not e.pre then
e.pre = {}
for i, p in ipairs(goal.pre) do e.pre[i] = p end
end
elseif goal.t == "turnin" then
e.turnin = e.turnin or { s, g }
elseif Index.WORK[goal.t] then
e.work[#e.work + 1] = { s, g }
end
end
end
end
return idx
end

function Index.Of(guide)
if type(guide) ~= "table" then return nil end
local idx = cache[guide]
if not idx then
idx = Index.Build(guide)
cache[guide] = idx
end
return idx
end

function Index.Forget(guide)
cache[guide] = nil
end
Loading
Loading