Phase 4: menu descriptors for 16 states
Game-file footprint unchanged: still one line in main.lua. Adds descriptors for actions_menu, worldmap, alchemy_table, journal, character, inventory, and a shared one for the full-screen prose states (menu_text, ingame_message, menu_message). The prose states also push their text into the console history, so the existing review keys walk the manual paragraph by paragraph rather than delivering it as one unusable utterance. Two driver improvements, both prompted by real failures the self-test caught: - menus.getAnyUpvalue finds a file-local across all of a state's functions and one level into inner closures. Safe because a Lua file-local is a single shared upvalue. Needed because actions_menu and worldmap build their lists in open() and never reference them in keypressed, so the named probe function found nothing. The generic probe path now falls back to this. - menus.hasUpvalue checks that a local exists rather than that it holds a value. getUpvalue cannot distinguish 'no such local' from 'declared but currently nil', which made the self-test report PROBE BROKEN for worldmap, whose 'local options' is nil until the screen is first opened. patches/popups.lua now stands down for any state that has a descriptor. inventoryCursorPopup.set only fires when an item is under the cursor, so empty slots and empty tabs were silent and you could not tell the cursor had moved. The inventory descriptor reports item-or-empty plus slot and tab instead. 21/21 testbed checks, history unit tests passing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
bd338087ce
commit
0477097e29
+125
-9
@@ -47,6 +47,52 @@ end
|
||||
|
||||
menus.getUpvalue = getUpvalue
|
||||
|
||||
-- Find a file-local by name across ALL of a state's exported functions, and one
|
||||
-- level into any inner functions they close over.
|
||||
--
|
||||
-- Safe because a Lua file-local is a single shared upvalue: every closure in that
|
||||
-- file that references it sees the same variable, so whichever function we reach
|
||||
-- it through yields the same value. This saves hand-picking a probe function per
|
||||
-- descriptor, and survives the common case where the local we want is touched by
|
||||
-- draw() or an inner helper rather than by keypressed().
|
||||
function menus.getAnyUpvalue(stateTable, name)
|
||||
if type(stateTable) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
|
||||
local nested = {}
|
||||
|
||||
for _, fn in pairs(stateTable) do
|
||||
if type(fn) == "function" then
|
||||
local value = getUpvalue(fn, name)
|
||||
if value ~= nil then
|
||||
return value
|
||||
end
|
||||
|
||||
local i = 1
|
||||
while true do
|
||||
local upName, upValue = debug.getupvalue(fn, i)
|
||||
if not upName then
|
||||
break
|
||||
end
|
||||
if type(upValue) == "function" then
|
||||
nested[#nested + 1] = upValue
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, fn in ipairs(nested) do
|
||||
local value = getUpvalue(fn, name)
|
||||
if value ~= nil then
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
-- IMPORTANT: states.lua loads states with require("states/"..name) -- a SLASH.
|
||||
-- require() caches on the exact string, so require("states.main_menu") would load
|
||||
-- a SECOND, independent copy of the file with its own upvalues, and we would be
|
||||
@@ -62,6 +108,63 @@ end
|
||||
|
||||
menus.getStateTable = getStateTable
|
||||
|
||||
-- Does a file-local of this name EXIST, regardless of its current value?
|
||||
--
|
||||
-- getUpvalue returns the value, so it cannot tell "no such local" from "local
|
||||
-- exists but is currently nil". That distinction matters for the self-test:
|
||||
-- several states declare `local options` at the top and only assign it in
|
||||
-- open(), so before the screen is first visited the value is legitimately nil.
|
||||
-- Treating that as a broken probe produced a false alarm on worldmap.
|
||||
function menus.hasUpvalue(stateTable, name)
|
||||
if type(stateTable) ~= "table" then
|
||||
return false
|
||||
end
|
||||
|
||||
local function scan(fn)
|
||||
local i = 1
|
||||
while true do
|
||||
local upName = debug.getupvalue(fn, i)
|
||||
if not upName then
|
||||
return false
|
||||
end
|
||||
if upName == name then
|
||||
return true
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
local nested = {}
|
||||
|
||||
for _, fn in pairs(stateTable) do
|
||||
if type(fn) == "function" then
|
||||
if scan(fn) then
|
||||
return true
|
||||
end
|
||||
|
||||
local i = 1
|
||||
while true do
|
||||
local upName, upValue = debug.getupvalue(fn, i)
|
||||
if not upName then
|
||||
break
|
||||
end
|
||||
if type(upValue) == "function" then
|
||||
nested[#nested + 1] = upValue
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, fn in ipairs(nested) do
|
||||
if scan(fn) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Descriptors
|
||||
--------------------------------------------------------------------------------
|
||||
@@ -111,9 +214,21 @@ local function readFocus(stateName)
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
-- Try the named probe function first, then fall back to searching every
|
||||
-- function in the state. Several states (actions_menu, worldmap) build their
|
||||
-- list in open() and never mention it in keypressed, so the named function
|
||||
-- alone finds nothing.
|
||||
local probeFn = stateTable[probe.fn or "keypressed"]
|
||||
|
||||
local index = getUpvalue(probeFn, probe.focus)
|
||||
if index == nil then
|
||||
index = menus.getAnyUpvalue(stateTable, probe.focus)
|
||||
end
|
||||
|
||||
local list = getUpvalue(probeFn, probe.list)
|
||||
if list == nil then
|
||||
list = menus.getAnyUpvalue(stateTable, probe.list)
|
||||
end
|
||||
|
||||
if type(index) ~= "number" or type(list) ~= "table" then
|
||||
return nil, nil
|
||||
@@ -216,18 +331,19 @@ function menus.selfTest(stateList)
|
||||
if not stateTable then
|
||||
results[stateName] = "STATE NOT LOADABLE"
|
||||
else
|
||||
local probeFn = stateTable[descriptor.probe.fn or "keypressed"]
|
||||
local index = getUpvalue(probeFn, descriptor.probe.focus)
|
||||
local list = getUpvalue(probeFn, descriptor.probe.list)
|
||||
-- Check that the NAMES exist, not that they currently hold values.
|
||||
-- Most of these are assigned in open(), so before the screen has
|
||||
-- been visited they are legitimately nil.
|
||||
local hasFocus = menus.hasUpvalue(stateTable, descriptor.probe.focus)
|
||||
local hasList = menus.hasUpvalue(stateTable, descriptor.probe.list)
|
||||
|
||||
-- index is often nil until the screen is opened; the list existing
|
||||
-- is the real signal that the probe still points at something.
|
||||
if list == nil then
|
||||
if not hasList then
|
||||
results[stateName] = "PROBE BROKEN: no upvalue '" .. tostring(descriptor.probe.list) .. "'"
|
||||
elseif index == nil and not descriptor.probe.focusMayBeNil then
|
||||
results[stateName] = "probe partial: '" .. tostring(descriptor.probe.focus) .. "' nil (may be normal before open)"
|
||||
elseif not hasFocus then
|
||||
results[stateName] = "PROBE BROKEN: no upvalue '" .. tostring(descriptor.probe.focus) .. "'"
|
||||
else
|
||||
results[stateName] = "ok"
|
||||
local list = menus.getAnyUpvalue(stateTable, descriptor.probe.list)
|
||||
results[stateName] = list == nil and "ok (unset until opened)" or "ok"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user