Files
Dune/game/movement-promises.ts
T
Quintine 49586efd2f Support waiting-turn Basic ground-movement promises
Allow unused respondents in the current Movement queue to answer against their current owned board and resources. Isolate foreign declarations, prices, flights and searches in detached forecasting without future income/discard assumptions. Preserve native per-player Hajr history. Other players cannot fulfill or close the promise with their own moves/end. Ordinary executable guidance waits until the real owned turn.

Keep an actual already-paid Box decision actionable while waiting. Enumerate its full entitled choice, prioritizing movement cards without excluding ordinary cards that preserve an existing route; pre-payment known-discard rules remain unchanged. Preserve the original event, fee, private inspection and source continuation. Check deferred Emperor gift and Spiritual Advisor costs on their allowed outcome before another player responds. Localize native pending-movement deferral to the actual mover so another waiting owner cannot hide voluntary Box overspending. Minimal profiles may decline optional accompaniment under a positive promise.

Verify 18 files125/125, types/lint and four original-action guidance/paid-choice/table-rendering programmes. Original source19 reverses the unopened classic deal; native44 earns Box1/Hajr1 and genuinely spends Richese Truth, with Guild's actual later question supplying a second ordinary paid candidate. Eight existing-profile runs make32 accepted proposals plus12 separate neutral acknowledgements. Controlled pact/gift, BG losses, original Ornithopter acquisition and native Fremen/Box cases disclose their staging. Paired affordable/unaffordable interruptions preserve exact frames and prevent voluntary route loss.

The production build passes at the initial freeze. Only the regression gains its affordable counterpart afterward; source identity proves every application/build/dependency and other file unchanged, while final tests/types/runtime run again. Final checkpoint/reference prose overlays are separately reviewed. Earlier phases/later rounds, remaining preparations, No-Field/modules, full modes, comprehensive assurance and deployed acceptance remain open. No strategic tuning or saved-room/reset/restart/deployment operation.
2026-10-24 15:31:17 +11:00

107 lines
5.2 KiB
TypeScript

import { TERRITORIES, territory } from './board';
import { FACTIONS } from './catalog';
import type { Game } from './engine';
/** One qualifying ground movement, never the sum of several smaller groups. */
export type MovementClaim = { territory: string; minimum: number };
export type MovementEvent = { territory: string; amount: number };
export type MovementPromise = MovementClaim & {
turn: number;
player: string;
asker: string;
answer: boolean;
fulfilled?: boolean;
released?: boolean;
};
export class MovementClaimError extends Error {}
function check(value: unknown, message: string): asserts value {
if (!value) throw new MovementClaimError(message);
}
/** Extract claim fields; the caller validates surrounding question/ledger keys. */
export function parseMovementClaim(value: unknown): MovementClaim {
check(value && typeof value === 'object' && !Array.isArray(value),
'Choose a valid ground-movement statement.');
const claim = value as Record<string, unknown>;
check(typeof claim.territory === 'string' &&
TERRITORIES.some(destination => destination.id === claim.territory),
'Choose a printed ground-movement destination.');
check(typeof claim.minimum === 'number' && Number.isSafeInteger(claim.minimum) &&
claim.minimum >= 1 && claim.minimum <= 20,
'Choose a minimum of one to twenty physical forces.');
return { territory: claim.territory, minimum: claim.minimum };
}
export function validMovementClaim(value: unknown): value is MovementClaim {
try { parseMovementClaim(value); return true; }
catch (error) {
if (error instanceof MovementClaimError) return false;
throw error;
}
}
export function movementClaimText(claim: MovementClaim): string {
return `make a ground movement of at least ${claim.minimum} physical forces to ${territory(claim.territory).name}`;
}
export function movementMatches(claim: MovementClaim, event: MovementEvent | null): boolean {
return event !== null && event.territory === claim.territory &&
Number.isSafeInteger(event.amount) && event.amount >= claim.minimum && event.amount <= 20;
}
export function liveMovementPromises<T extends MovementPromise>(
promises: readonly T[], player: string, turn: number,
): T[] {
return promises.filter(promise => promise.player === player && promise.turn === turn &&
!promise.fulfilled && !promise.released);
}
/** The caller supplies the relevant current promises. Yes is existential: a
* nonmatching event may be followed by another ordinary movement opportunity. */
export function movementEventAllowed(
promises: readonly MovementPromise[], event: MovementEvent | null,
): boolean {
return promises.every(promise => promise.fulfilled || promise.released ||
promise.answer || !movementMatches(promise, event));
}
export function settleMovementEvent<T extends MovementPromise>(
promises: readonly T[], player: string, turn: number, event: MovementEvent | null,
): T[] {
const live = liveMovementPromises(promises, player, turn);
check(movementEventAllowed(live, event),
'This ground movement would break an accepted No answer.');
return promises.map(promise => live.includes(promise) && promise.answer && movementMatches(promise, event)
? { ...promise, fulfilled: true } : promise);
}
/** Close only the actual player's movement turn. No stays binding until here;
* ending early cannot evade an unfulfilled Yes, even if this event was skipped. */
export function finishMovementPromises<T extends MovementPromise>(
promises: readonly T[], player: string, turn: number,
): T[] {
const live = liveMovementPromises(promises, player, turn);
check(live.every(promise => !promise.answer),
'Complete your accepted ground-movement promise before ending movement.');
return promises.map(promise => live.includes(promise) ? { ...promise, fulfilled: true } : promise);
}
/** Prototype profile/timing only. Actual route feasibility stays authoritative
* in the engine. Truthtrance and an ordinary active Ornithopter may remain. */
export function movementPromiseTargetAvailable(g: Game, player: string): boolean {
const target = g.players.find(candidate => candidate.id === player);
return g.status === 'playing' && g.advanced === false && g.phase === 5 &&
!!target && (g.active === player || (!target.shipped && target.moved === 0)) &&
!!g.movementRemaining?.includes(player) &&
g.players.length >= 2 && g.players.length <= 6 &&
g.expansions.every(id => ['ix', 'choam', 'ecaz'].includes(id)) &&
new Set(g.expansions).size === g.expansions.length &&
g.players.every(candidate => {
const family = FACTIONS.find(entry => entry.id === candidate.faction)?.expansion;
return family === 'base' || (!!family && g.expansions.includes(family));
}) &&
!g.homeworlds && !g.nexusCards && !g.leaderSkills && !g.techTokens &&
!g.strongholdCards && !g.discoveryEnabled && !g.discoveries &&
!g.discoveryStash && !g.greatMaker && !g.ecazTreachery &&
!g.response && !g.decision && !g.phaseOpening &&
!g.pendingShipment && !g.pendingHomeworldShipment && !g.pendingGuildTransport &&
!g.pendingAmbassador && !g.pendingTerrorEntry && !g.pendingArrivalOverlap &&
!g.pendingArrivalReaction?.length && !g.pendingFremenMove && !g.pendingIxMove &&
!g.pendingChoamMove && !g.pendingTreacheryDiscard;
}