/** * Authored wooden-door passage gates (ConditionSet + toast). * Empty requirements = unrestricted. Players who fail may not pass even if the door is held open. */ import { normalizeConditionSet, emptyConditionSet, evaluateConditionSet } from "../quests/conditions.js"; export const DEFAULT_DOOR_BLOCKED_MESSAGE = "You cannot go through here."; /** * Preserve runtime restingState while normalizing authored gate fields. * @param {object | null | undefined} raw */ export function normalizeDoorConfig (raw) { if (!raw || typeof raw !== "object") { return null; } const when = normalizeConditionSet(raw.when ?? raw.requirements); const blockedMessage = String(raw.blockedMessage ?? raw.denyMessage ?? "") .trim() .slice(0, 200); const restingState = typeof raw.restingState === "string" && raw.restingState ? raw.restingState : undefined; const hasWhen = (when.all?.length ?? 0) > 0; const hasMessage = blockedMessage.length > 0; if (!hasWhen && !hasMessage && restingState == null) { return null; } const out = {}; if (restingState != null) { out.restingState = restingState; } if (hasWhen) { out.when = when; } if (hasMessage) { out.blockedMessage = blockedMessage; } return Object.keys(out).length ? out : null; } export function doorRequirementSet (block) { const when = block?.config?.when ?? block?.config?.requirements; return normalizeConditionSet(when ?? emptyConditionSet()); } export function doorHasRequirements (block) { return (doorRequirementSet(block).all?.length ?? 0) > 0; } export function doorBlockedMessage (block) { const text = String(block?.config?.blockedMessage ?? block?.config?.denyMessage ?? "").trim(); return text || DEFAULT_DOOR_BLOCKED_MESSAGE; } /** * @param {object} block * @param {object | null} ctx — evaluateConditionSet snapshot; null/undefined fails closed when gated */ export function doorRequirementsMet (block, ctx) { if (!doorHasRequirements(block)) { return true; } if (!ctx) { return false; } return evaluateConditionSet(doorRequirementSet(block), ctx); }