v0.4.4 — New Game dialog for the opening hand and the three revenue rates, Yard renamed Interchange, and two movement bugs behind a mirrored consist

This commit is contained in:
Jesse
2026-08-14 22:39:50 -04:00
parent d1314066bd
commit 6655e20ea8
29 changed files with 5450 additions and 3902 deletions
+44 -6
View File
@@ -42,8 +42,15 @@ import {
trainName,
variantLabel,
} from '../sim/view.ts';
import { HAND_LIMIT, mainlineProfile, trainProfile } from '../engine/content.ts';
import type { Hand, TrackGeometry } from '../engine/content.ts';
import {
DEFAULT_HOUSE_RULES,
HAND_LIMIT,
LEGACY_HOUSE_RULES,
houseRules,
mainlineProfile,
trainProfile,
} from '../engine/content.ts';
import type { Hand, HouseRuleOverrides, TrackGeometry } from '../engine/content.ts';
import type { Port } from '../engine/track.ts';
import { connectionsFor, joins, neighbour, variantsFor } from '../engine/track.ts';
import { areaOf, trainNeedingCars } from '../engine/apply.ts';
@@ -59,8 +66,16 @@ export const SOLO_CONFIG: GameConfig = {
employeeRotation: false,
emergencyToolbox: false,
},
// Spelt out rather than left to fall through, so a save written by the page always names the rules
// it was played under — see `Save.rules`.
houseRules: DEFAULT_HOUSE_RULES,
};
/** The same config with the New Game dialog's answers in it. */
export function configWith(rules: HouseRuleOverrides): GameConfig {
return { ...SOLO_CONFIG, houseRules: houseRules({ houseRules: rules }) };
}
/** A group of legal actions of one kind, ready to put on screen. */
export type ActionGroup = {
kind: string;
@@ -708,10 +723,31 @@ function record(game: Game, events: GameEvent[], actor: PlayerIndex | null = nul
// Saving — seed plus intents, replayed
// ---------------------------------------------------------------------------
export type Save = { seed: number; history: Intent[] };
/**
* A save is a seed, the house rules it was dealt under, and the intents submitted.
*
* `rules` is optional because saves written before the New Game dialog existed do not have it, and
* those replay under `LEGACY_HOUSE_RULES` — see `configFor`. Everything written from now on carries
* its rules, so a save can never again be silently re-dealt by a change of default.
*/
export type Save = { seed: number; history: Intent[]; rules?: HouseRuleOverrides };
export function toSave(game: Game): Save {
return { seed: game.seed, history: game.history };
const rules = game.state.config.houseRules;
return rules ? { seed: game.seed, history: game.history, rules } : { seed: game.seed, history: game.history };
}
/**
* THE CONFIG A SAVE MUST BE REPLAYED UNDER, which is not necessarily today's default.
*
* A save is a seed and a list of intents: replay it under different rules and it is a different
* game, and the symptom is not an error but a replay that quietly stops early. `TODO.md` records
* that happening twice unnoticed, once 42 intents into 360. So a save that names its rules gets
* exactly those, and a save that names none is from before the dialog and gets the rules that were
* in force then — never the current defaults.
*/
function configFor(save: Save, config: GameConfig): GameConfig {
return { ...config, houseRules: save.rules ?? LEGACY_HOUSE_RULES };
}
/**
@@ -736,7 +772,9 @@ export function toSave(game: Game): Save {
*/
export function undo(game: Game, config: GameConfig = SOLO_CONFIG): Game | null {
if (game.history.length === 0) return null;
return fromSave({ seed: game.seed, history: game.history.slice(0, -1) }, config);
// `toSave` first, so the rules this game was dealt under come with it. Rebuilding the save by hand
// here dropped them, and undo re-dealt the game under the defaults instead of its own settings.
return fromSave({ ...toSave(game), history: game.history.slice(0, -1) }, config);
}
/**
@@ -747,7 +785,7 @@ export function undo(game: Game, config: GameConfig = SOLO_CONFIG): Game | null
* stops the replay rather than being forced — better a short game than a corrupt one.
*/
export function fromSave(save: Save, config: GameConfig = SOLO_CONFIG): Game {
const game = newGame(save.seed, config);
const game = newGame(save.seed, configFor(save, config));
for (const intent of save.history) {
const actor = currentActor(game);
if (actor === null) break;