1
0
Fork 0
polaris-task-force/src/lib/interior/gates.ts
Z8MB1E 68cd3cce7c feat(interior): fixture repairs, condition decay, and station blocking
completeInteriorJob restores broken fixtures on repair jobs. New decay.ts applies per-room condition loss (elapsed days x conditionPerDay x Game Rules multiplier) on the interior-tick bin, gated by interior.decayEnabled with a baseline first run that only stamps the structure clock; fixtures at or below the authored effectFloor break with new interior:fixture-broken events, floors condition at 0, and station starts fail room_damaged below the floor.

Declares blocksStation on structure-rooms fixture state: provisioning wrote it but the missing column silently stripped it, so broken doorway fixtures could never block stations. Regression test covers persistence through provisioning plus the end-to-end gate.
2026-10-20 16:17:01 -04:00

134 lines
5 KiB
TypeScript

import type { InteriorBlueprintRoom, JobType, RoomEffectType, RoomFixtureState, RuntimeRoom } from "@/lib/interior/types";
/**
* Pure job-start gates for the interior.
*
* These functions take a room's runtime state + its blueprint definition and
* decide whether a job of a given type may start. They contain no Payload I/O
* so they are fully unit-testable; the Payload-aware orchestration in jobs.ts
* calls them after loading docs.
*/
export type JobStartError =
| "no_access"
| "room_busy"
| "already_open"
| "nothing_to_cleanup"
| "max_level_reached"
| "fixture_blocks_station"
| "recipe_locked"
| "station_disabled"
| "station_locked"
| "room_level_insufficient"
| "recipe_disabled"
| "tech_locked"
| "labor_insufficient"
| "room_damaged";
export type JobStartResult = { ok: true } | { ok: false; reason: JobStartError };
/** A fixture that blocks the room's station when broken. */
export function isBlockingFixtureBroken(fixtures: RoomFixtureState[] | undefined): boolean {
if (!fixtures) return false;
return fixtures.some((f) => f.broken && f.blocksStation);
}
/**
* Whether a job of `jobType` may start on this room right now.
*
* `opts` carries the recipe/station context needed by produce jobs, plus the
* level requirements (researched tech, staffed labor) that jobs.ts resolves for
* open/upgrade jobs. `undefined` means "met" so callers that do not evaluate a
* requirement never trip the gate.
*/
export function canStartJob(
room: RuntimeRoom,
roomType: "open" | "upgrade" | "cleanup" | "repair" | "produce",
opts?: {
maxLevel?: number;
stationEnabled?: boolean;
stationUnlocked?: boolean;
recipeUnlocked?: boolean;
recipeEnabled?: boolean;
recipeRequiredRoomLevel?: number;
recipeBlockingFixture?: boolean;
/**
* Produce only: the room's condition is at or below its effect floor. An
* authored positive floor blocks at `condition <= floor`; a room with no
* authored floor blocks only at `condition <= 0` (fully degraded).
*/
conditionBlocked?: boolean;
techRequirementMet?: boolean;
laborRequirementMet?: boolean;
},
): JobStartResult {
// A room can only run one job at a time (the output slot).
if (room.pendingJob) return { ok: false, reason: "room_busy" };
// Level requirements gate open/upgrade; check them before the type switch so
// the reason is deterministic when several gates would fail.
if (opts?.techRequirementMet === false) return { ok: false, reason: "tech_locked" };
if (opts?.laborRequirementMet === false) return { ok: false, reason: "labor_insufficient" };
switch (roomType) {
case "open":
return (room.level ?? 0) > 0 ? { ok: false, reason: "already_open" } : { ok: true };
case "cleanup":
return (room.junkLevel ?? 0) > 0 ? { ok: true } : { ok: false, reason: "nothing_to_cleanup" };
case "upgrade": {
const max = opts?.maxLevel ?? (room.level ?? 0);
if ((room.level ?? 0) >= max) return { ok: false, reason: "max_level_reached" };
if (opts?.recipeBlockingFixture && isBlockingFixtureBroken(room.fixtures)) {
return { ok: false, reason: "fixture_blocks_station" };
}
return { ok: true };
}
case "repair":
return { ok: true };
case "produce": {
if (!opts?.stationEnabled) return { ok: false, reason: "station_disabled" };
if (opts?.stationUnlocked === false) return { ok: false, reason: "station_locked" };
if (opts?.conditionBlocked === true) return { ok: false, reason: "room_damaged" };
if (opts?.recipeBlockingFixture && isBlockingFixtureBroken(room.fixtures)) {
return { ok: false, reason: "fixture_blocks_station" };
}
if (!opts?.recipeUnlocked) return { ok: false, reason: "recipe_locked" };
if (!opts?.recipeEnabled) return { ok: false, reason: "recipe_disabled" };
if (
opts?.recipeRequiredRoomLevel != null &&
(room.level ?? 0) < opts.recipeRequiredRoomLevel
) {
return { ok: false, reason: "room_level_insufficient" };
}
return { ok: true };
}
}
}
/**
* Convenience: does the room have a fixture that blocks the station and is
* currently broken? Used by produce/upgrade gating.
*/
export function stationBlockedByFixture(
room: { fixtures?: RoomFixtureState[] },
): boolean {
return isBlockingFixtureBroken(room.fixtures);
}
/**
* Whether the room currently has any decay-driven issues that block work
* (condition below the effect floor, or junk at junkMax).
*/
export function roomBlockedByDecay(room: { condition: number; junkLevel: number }): boolean {
return room.condition <= 0 || room.junkLevel >= 0;
}
/** Type guard so callers can narrow effect types without a switch. */
export function isRoomEffectType(value: unknown): value is RoomEffectType {
return (
typeof value === "string" &&
(["production_speed", "output_bonus", "storage_capacity", "labor_capacity", "unlocks_recipe", "unlocks_station"] as string[]).includes(value)
);
}
export type { RoomEffectType, InteriorBlueprintRoom, JobType };