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 };