Files
alchemist-accessibility/accessibility/init.lua
T
Accessibility ModandClaude Opus 5 fe872be683 Phase 1: console history, review keys, and wall tones
Game-file footprint unchanged: still one line in main.lua.

Console (Q4: speak everything queued, cancelled by explicit action):
- history.lua keeps our own unbounded history, since the game's console buffer
  expires entries after 3s. Mirrors the game's (xN) coalescing so what is heard
  matches what is on screen. The review cursor holds position while new lines
  arrive rather than being yanked to the tail mid-read.
- patches/console.lua wraps console.print, calling the real one first so a bug
  on our side cannot swallow a message the player would otherwise see.
- Any keypress reaching the game silences the pending speech backlog.

Hotkeys: [ and ] review, Shift+[/] jump to ends, \ repeat, Shift+\ copy.
Chosen from scancodes the game does not bind. Note keybindings.getCommand()
ignores modifiers, so Ctrl+Up would read to the game as a movement -- our keys
are consumed and never overlay a game-claimed scancode.

Wall tones (Q5: on step, 3D-positioned, radius 5):
- Direction and distance are deliberately decoupled. The listener sits at the
  camera 4 units above the plane under an inverse distance model, so placing a
  tone at the wall's real position would make distant walls quieter (the
  opposite of the request) and collapse the stereo pan. Instead the source sits
  at a fixed radius in the wall's direction at listener height, and volume is
  computed explicitly from distance.
- Step detection polls player coordinates rather than hooking movement, so it
  covers walking, pathing, portals and pushes for free.
- Respects FoV/rememberedFoV; never leaks unseen geometry.

tests/test_history.lua: 16 assertions, all passing, pure Lua.

Not yet verified in actual play -- needs a human at the keyboard.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 20:38:28 +02:00

143 lines
4.3 KiB
Lua

-- accessibility/init.lua — mod entry point.
--
-- This is the ONLY thing the game calls into. main.lua gains exactly one line:
--
-- require("accessibility").initialize()
--
-- placed after console.prepare() and before the first states.switch(). By that
-- point templates are preprocessed, keybindings are loaded, states.setup() has
-- run, and the console canvas exists — everything we patch is ready.
--
-- Everything here is wrapped so that a bug in the mod degrades to "no speech"
-- rather than taking the game down with it.
local accessibility = {}
local speech = require("accessibility.speech")
local stateNames = require("accessibility.stateNames")
local hotkeys = require("accessibility.hotkeys")
local walltones = require("accessibility.walltones")
local initialized = false
--------------------------------------------------------------------------------
-- Patch registry
--
-- Later phases register their patches here. Each entry is {name, fn}; fn is called
-- inside a pcall during initialize(), and a failure disables only that patch.
--------------------------------------------------------------------------------
local patches = {}
function accessibility.registerPatch(name, fn)
patches[#patches + 1] = {name = name, fn = fn}
end
local patchResults = {}
local function applyPatches()
for _, patch in ipairs(patches) do
local ok, err = pcall(patch.fn)
patchResults[patch.name] = ok and "ok" or tostring(err)
if not ok then
print("[accessibility] patch '" .. patch.name .. "' FAILED: " .. tostring(err))
end
end
end
-- Startup self-test output. This is the version-bump canary: when the game
-- updates and something we hook has moved, it shows up here immediately rather
-- than as silence three screens into a playthrough.
function accessibility.selfTest()
print("[accessibility] --- self test ---")
print("[accessibility] TTS: " .. (speech.isAvailable() and "available"
or ("UNAVAILABLE (" .. tostring(speech.getFailureReason()) .. ")")))
if speech.isAvailable() then
-- Distinguishes "DLL loaded" from "actually reached a screen reader or SAPI".
print("[accessibility] speech channel: " .. (speech.hasSpeech() and "yes" or "NO — nothing will be audible"))
end
for name, result in pairs(patchResults) do
print("[accessibility] patch " .. name .. ": " .. result)
end
print("[accessibility] --- end self test ---")
end
--------------------------------------------------------------------------------
-- Core hooks into states.lua
--------------------------------------------------------------------------------
local function hookStates()
local states = require("states")
-- Screen changes: announce the new screen.
local realSwitch = states.switch
states.switch = function(newState, params)
local result = realSwitch(newState, params)
-- After the switch, so a state's open() has already set up its focus.
pcall(function()
local name = stateNames[newState] or newState
speech.say(name, true)
end)
return result
end
-- Per-frame speech flush. main.lua calls states.update(dt) via a table lookup,
-- so wrapping the field here avoids editing love.update.
local realUpdate = states.update
states.update = function(dt)
local result = realUpdate(dt)
pcall(walltones.update)
pcall(speech.flush)
return result
end
-- Global hotkey layer. Ours are consumed; everything else falls through to the
-- game untouched.
local realKeypressed = states.keypressed
states.keypressed = function(key, scancode)
local consumed = false
local ok, result = pcall(hotkeys.keypressed, key, scancode)
if ok then
consumed = result
end
if consumed then
return
end
pcall(hotkeys.onGameKey)
return realKeypressed(key, scancode)
end
end
--------------------------------------------------------------------------------
-- Entry point
--------------------------------------------------------------------------------
function accessibility.initialize()
if initialized then
return
end
initialized = true
accessibility.registerPatch("console", require("accessibility.patches.console"))
local ok, err = pcall(function()
speech.initialize()
walltones.load()
hookStates()
applyPatches()
end)
if not ok then
print("[accessibility] initialisation FAILED, game continues unmodified: " .. tostring(err))
return
end
accessibility.selfTest()
end
return accessibility