-- The gate's driver (docs/lua-modding.md): from the title's menus into the world with no person, then still or -- walking a looped course. It only uses what it is given (`api`), so a test can run it with a stub. -- -- States: boot (frames must be coming) -> menus (Enter now and then, only once frames have moved on since the last -- press, until the player's position is known) -> probe (W held: the player must move) -> idle or roam. -- It writes, through api.log, "GATE PRESS", "GATE PROBE", "GATE CONTROL", "GATE MARK" every mark_every seconds, -- "GATE SHOT" and "GATE FAIL ": distances only, never a position. local M = {} M.defaults = { mode = "idle", -- "idle": stand still once in control (for measuring); "roam": walk the loop below boot_frames = 600, -- frames presented before the first press boot_seconds = 20, press_every = 25, -- seconds between presses of Enter in the menus press_seconds = 0.2, give_up = 900, -- seconds with no control: GATE FAIL probe_seconds = 3, -- W held to show control probe_metres = 1.0, -- the least it must move -- The most: more is not a walk but the title placing the player (a load, a cutscene), which shows no control. probe_max_metres = 30, mark_every = 10, shots = { 10, 120 }, -- seconds after control (screenshots exist in diagnostics builds) -- The roam loop: seconds, keys held. J and L turn the camera (the stand-in pad's right stick), W walks. roam = { { 4, { "W" } }, { 0.4, { "W", "L" } }, { 4, { "W" } }, { 0.4, { "W", "J" } } }, } function M.new(api, config) local cfg = {} for k, v in pairs(M.defaults) do cfg[k] = v end for k, v in pairs(config or {}) do cfg[k] = v end return setmetatable({ api = api, cfg = cfg, state = "boot", presses = 0, roam_step = 1, shots_done = 0 }, { __index = M }) end local function distance(a, b) return math.sqrt((a[1] - b[1]) ^ 2 + (a[2] - b[2]) ^ 2 + (a[3] - b[3]) ^ 2) end function M:position() local x, y, z = self.api.position() if type(x) == "number" then return { x, y, z } end return nil end function M:say(...) self.api.log("GATE", ...) end function M:fail(why, now) self.api.release() self:say("FAIL", why, string.format("t=%.1f", now)) self.state = "done" end function M:press(now, frame) self.api.hold({ "Enter" }) self.release_at = now + self.cfg.press_seconds self.last_press, self.press_frame = now, frame self.presses = self.presses + 1 self:say("PRESS", self.presses, string.format("t=%.1f", now), "frame=" .. frame) end function M:step() local now, frame = self.api.time(), self.api.frame() if self.state == "done" then return end if self.release_at and now >= self.release_at then self.api.release() self.release_at = nil end local in_control = self.state == "idle" or self.state == "roam" if not in_control and now >= self.cfg.give_up then return self:fail(self:position() and "no control" or "no position", now) end if self.state == "boot" then if frame >= self.cfg.boot_frames and now >= self.cfg.boot_seconds then self.state = "menus" end elseif self.state == "menus" then local may_press = not self.release_at and (self.press_frame == nil or frame >= self.press_frame + 3) and (self.last_press == nil or now - self.last_press >= self.cfg.press_every) if self:position() and not self.want_press then -- Once Enter is up: a probe holding W would cut its press short. if not self.release_at then self.state = "probe" end elseif may_press then self:press(now, frame) self.want_press = false end elseif self.state == "probe" then if not self.probe then local at = self:position() if not at then self.state = "menus" return end self.release_at = nil self.api.hold({ "W" }) self.probe = { from = at, ends = now + self.cfg.probe_seconds } return end if now < self.probe.ends then return end self.api.release() local at = self:position() local moved = at and distance(at, self.probe.from) or 0 self.probe = nil if moved >= self.cfg.probe_metres and moved <= self.cfg.probe_max_metres then self.control_at, self.last_mark, self.home = now, now, at self.state = self.cfg.mode == "roam" and "roam" or "idle" self:say("CONTROL", string.format("t=%.1f", now), "frame=" .. frame, string.format("moved=%.2f", moved), "mode=" .. self.state) elseif moved > self.cfg.probe_max_metres then -- The title placed the player itself (a load, a cutscene): no screen to press through, show control again. self:say("PROBE", string.format("t=%.1f", now), string.format("moved=%.1f", moved), "placed") else -- A screen may still want a press: one, at the menus' pace (press_every, frames moving on), then show -- control again. self:say("PROBE", string.format("t=%.1f", now), string.format("moved=%.1f", moved), "still") self.state, self.want_press = "menus", true end else if self.state == "roam" and (self.roam_until == nil or now >= self.roam_until) then local leg = self.cfg.roam[self.roam_step] self.api.hold(leg[2]) self.roam_until = now + leg[1] self.roam_step = self.roam_step % #self.cfg.roam + 1 end local since = now - self.control_at if self.shots_done < #self.cfg.shots and since >= self.cfg.shots[self.shots_done + 1] then self.shots_done = self.shots_done + 1 local taken, why = self.api.shot() self:say("SHOT", self.shots_done, taken and "taken" or ("not taken: " .. tostring(why))) end if now - self.last_mark >= self.cfg.mark_every then self.last_mark = now local at = self:position() self:say("MARK", string.format("t=%.1f", now), "frame=" .. frame, self.state, string.format("from_control_m=%.1f", at and distance(at, self.home) or -1)) end end end return M