v0.6.1 — five of six playtest bugs: one button per train, and a load that has to go somewhere

Gameplay testing on 0.4.9d returned six reports. Five are fixed; the sixth could not be
reproduced and is written up in TODO.md with the two questions that would pin it down.

TWO TRAINS AT ONE PLATFORM ANSWERED TO ONE BUTTON. `porter.board` and `porter.detrain`
carried no tray, so there was one button per platform however many trains stood at it and
the reducer filled the first empty coach on the A/D tracks. `check` and the reducer were not
even asking the same question: `check` skipped a train whose card refuses passenger work and
the reducer did not. Both intents now carry an optional `trayId`, one function resolves the
train and the coach for check/execute/reduce alike, `legal.ts` offers one candidate per train,
and the label names it.

A LOAD COULD BE MADE AND BROKEN WITHOUT GOING ANYWHERE. A Freight House could unload the
boxcar it had just loaded; a platform could detrain the passengers it had just boarded. Full
Revenue at both ends for a movement that never happened. Jesse's rule: a load made anywhere in
an Office Area may not be broken anywhere in that Office Area, ever — it has to be carried to
another district. The load carries the seat that made it (`RollingStock.origin`), stripped by
`pooled` at every yard push. Measured at -0.60 +/- 0.10 Revenue a game (t = -6.1) over 400
paired deals: 78 worse, 3 better, 319 unchanged — free Revenue coming off the board, not a nerf.

THE GROCER'S WAREHOUSE SHIPPED AND THE REFINERY RECEIVED. Both were `flow: 'both'` on the
reading that "Freight House" was a collective term for exactly those two, and therefore what
§9.3 described. The engine has dealt a Freight House CARD since before v0.4.9, so §9.3 names
it and the argument goes. The card set agrees: all three Refinery modifiers grant +1 outbound.
Refinery outbound-only, Grocer's inbound-only, Freight House the one two-way industry — which
leaves exactly the one same-district pairing the rule above refuses.

NOT REPRODUCED: cars left behind when backing up over them. Five layouts tried, including cars
spotted at an industry; every one couples the lot. Three are pinned in `apply.test.ts`. One way
to create such cars was closed anyway — `flyingSwitch` wrote its cut past `carsOn`.

Both published replays that had gone dead were re-recorded; a rules change retires a save, and
`harness.test.ts` is what catches it.

The same change ships as v0.4.9e on the 0.4.9 line, branched from the v0.4.9d commit — the engine
files these fixes touch are identical across the two lines, so the patch applied cleanly both ways.

Also carries the two "Queued 2026-08-22, from playing on StartOS" TODO items that were staged
before this work started (Games in Progress readability, and getting back into a game after
losing a browser). They are notes, and items 9-12 below them are numbered against them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011nbvwWMef8CuEP6t5cgkTv
This commit is contained in:
Jesse.Markowitz
2026-08-21 23:54:24 -04:00
co-authored by Claude Opus 5
parent 40f07b0710
commit 83a5450866
25 changed files with 2853 additions and 1811 deletions
+137 -60
View File
@@ -59,6 +59,7 @@ import {
cutTowards,
isOperationalRail,
playerAtSeat,
pooled,
railFacingOf,
seatOf,
spaceOn,
@@ -388,17 +389,11 @@ export function canStartLoad(f: Facility): boolean {
}
/** §9.2 — boarding needs a loaded coach in a green slot and a train with an empty coach. */
export function canBoard(s: GameState, player: PlayerIndex, at: GridCoord): boolean {
export function canBoard(s: GameState, player: PlayerIndex, at: GridCoord, trayId?: TrayId): boolean {
const f = facilityAt(s, player, at);
if (!f || f.kind !== 'passenger' || portersLeft(f) < 1) return false;
if (!f.outboundBox.some((c) => c.type === 'coach' && c.loaded)) return false;
// §7 — a train whose card refuses passenger work, or which is not booked to stop here, is not a
// train these passengers can board however many empty coaches it is carrying.
return trainAtOfficeWith(
s, player,
(c) => c.type === 'coach' && !c.loaded,
(t) => !refusesPassengers(t) && !refusesThisOffice(s, player, t),
);
return passengerWork(s, player, 'board', trayId) !== null;
}
/**
@@ -413,29 +408,54 @@ export function canBoard(s: GameState, player: PlayerIndex, at: GridCoord): bool
* de-training MINTED a coach: the loaded one went to the red box and a new empty one appeared in the
* train. Measured at 1.29 cars a game created out of nothing across the two inbound paths.
*/
export function canDetrain(s: GameState, player: PlayerIndex, at: GridCoord): boolean {
export function canDetrain(s: GameState, player: PlayerIndex, at: GridCoord, trayId?: TrayId): boolean {
const f = facilityAt(s, player, at);
if (!f || f.kind !== 'passenger' || portersLeft(f) < 1) return false;
if (f.inboundBox.length >= f.capacity.inbound) return false;
if (!s.yards.divisionYard.some((c) => c.type === 'coach' && !c.loaded)) return false;
return trainAtOfficeWith(
s, player,
(c) => c.type === 'coach' && c.loaded,
(t) => !refusesPassengers(t) && !refusesThisOffice(s, player, t),
);
return passengerWork(s, player, 'detrain', trayId) !== null;
}
function trainAtOfficeWith(
/**
* WHICH TRAIN, AND WHICH COACH ON IT — the one answer `check`, `execute` and the reducer all use.
*
* TWO PLAYTEST BUGS SHARED ONE CAUSE HERE. Reported against v0.4.9d: "operating two trains in a
* station, the select button does not work — regardless of which you pick, it is always one train,
* not the other". `porter.board` carried no tray at all, so `check` asked whether SOME train at the
* Office had an empty coach and the reducer then walked `adOccupancy` and filled the first one it
* found. The two were not even asking the same question: `check` skipped a train whose card refuses
* passenger work and the reducer did not, so a Military train could be boarded as long as some other
* train at the platform was eligible. The intent now names its tray (`intents.ts`) and this is the
* one place that resolves it.
*
* And "passengers just boarded cannot be immediately unloaded": a coach carries the district that
* filled it (`RollingStock.origin`), and a homegrown coach is not a coach these passengers may
* alight from — they have to be carried to a different Office Area first.
*
* `trayId` absent means "any eligible train", which is what every intent recorded before this
* existed meant, so an old save replays unchanged.
*/
function passengerWork(
s: GameState,
player: PlayerIndex,
pred: (c: RollingStock) => boolean,
trayOk: (t: CrewTray) => boolean = () => true,
): boolean {
dir: 'board' | 'detrain',
trayId?: TrayId,
): { trayId: TrayId; coachIndex: number } | null {
const area = areaOf(s, player);
return area.adOccupancy.some((id) => {
const t = s.trays.get(id);
return !!t && trayOk(t) && t.consist.some(pred);
});
const seat = seatOf(s, player);
const wanted = (c: RollingStock): boolean =>
c.type === 'coach' && (dir === 'board' ? !c.loaded : c.loaded && c.origin !== seat);
for (const id of area.adOccupancy) {
if (trayId !== undefined && id !== trayId) continue;
const tray = s.trays.get(id);
if (!tray) continue;
// §7 — a train whose card refuses passenger work, or which is not booked to stop here, is not a
// train these passengers can board however many empty coaches it is carrying.
if (refusesPassengers(tray) || refusesThisOffice(s, player, tray)) continue;
const coachIndex = tray.consist.findIndex(wanted);
if (coachIndex >= 0) return { trayId: id, coachIndex };
}
return null;
}
// ---------------------------------------------------------------------------
@@ -570,12 +590,30 @@ function passengerRefusal(
player: PlayerIndex,
at: GridCoord,
dir: 'board' | 'detrain',
trayId?: TrayId,
): RejectionCode {
const area = areaOf(s, player);
const trains = area.adOccupancy.map((id) => s.trays.get(id)).filter((t): t is CrewTray => !!t);
const trains = area.adOccupancy
.filter((id) => trayId === undefined || id === trayId)
.map((id) => s.trays.get(id))
.filter((t): t is CrewTray => !!t);
if (trains.length > 0 && trains.every((t) => refusesThisOffice(s, player, t))) return 'NOT_A_TERMINAL';
if (trains.length > 0 && trains.every(refusesPassengers)) return 'NO_PASSENGER_WORK';
if (trains.length === 0) return 'NO_TRAIN_AT_OFFICE';
/**
* EVERY LOADED COACH ABOARD BOARDED HERE — so the refusal is the district rule, not "no loaded
* coach". Told apart because the two read as opposite situations to a player: one is an empty
* train, the other is a train full of passengers who have not been anywhere yet.
*/
if (
dir === 'detrain' &&
trains.some((t) => t.consist.some((c) => c.type === 'coach' && c.loaded)) &&
trains.every((t) =>
t.consist.every((c) => !(c.type === 'coach' && c.loaded) || c.origin === seatOf(s, player)),
)
) {
return 'LOADED_IN_THIS_DISTRICT';
}
/**
* A TRAIN IS STANDING THERE, so say what is actually missing.
@@ -981,7 +1019,8 @@ export function check(s: GameState, player: PlayerIndex, i: Intent): RejectionCo
if (!f) return 'NO_SUCH_FACILITY';
if (f.porters < 1) return 'NO_PORTERS_HERE';
if (portersLeft(f) < 1) return 'RESOURCE_SPENT';
return canBoard(s, player, i.at) ? null : passengerRefusal(s, player, i.at, 'board');
if (i.trayId !== undefined && !s.trays.has(i.trayId)) return 'NO_SUCH_TRAY';
return canBoard(s, player, i.at, i.trayId) ? null : passengerRefusal(s, player, i.at, 'board', i.trayId);
}
case 'porter.detrain': {
@@ -990,7 +1029,8 @@ export function check(s: GameState, player: PlayerIndex, i: Intent): RejectionCo
if (!f) return 'NO_SUCH_FACILITY';
if (f.porters < 1) return 'NO_PORTERS_HERE';
if (portersLeft(f) < 1) return 'RESOURCE_SPENT';
return canDetrain(s, player, i.at) ? null : passengerRefusal(s, player, i.at, 'detrain');
if (i.trayId !== undefined && !s.trays.has(i.trayId)) return 'NO_SUCH_TRAY';
return canDetrain(s, player, i.at, i.trayId) ? null : passengerRefusal(s, player, i.at, 'detrain', i.trayId);
}
case 'laborer.startLoad': {
@@ -1026,6 +1066,19 @@ export function check(s: GameState, player: PlayerIndex, i: Intent): RejectionCo
if (laborersLeft(f) < 1) return 'RESOURCE_SPENT';
const car = f.industryTrack.cars[i.carIndex];
if (!car || !car.loaded) return 'WRONG_CAR_TYPE';
/**
* A LOAD MAY NOT BE BROKEN IN THE DISTRICT THAT MADE IT (Jesse's ruling, v0.4.9e).
*
* Reported from playtesting v0.4.9d: "Freight House: boxcars loaded cannot be immediately
* unloaded." They could — a Freight House permits both directions, so the car its own Laborers
* had just loaded was standing on its own track, loaded, with an empty of that type in the
* yard, and every gate below said yes. Full Revenue at both ends for a load that never moved.
*
* The rule is district-wide and permanent, not "not at this facility" and not "not this
* Stage": the stamp says which Office Area made the load, and it never expires. Traffic runs
* BETWEEN districts, which is what the lockout pairs in `content.ts` exist to force.
*/
if (car.origin === seatOf(s, player)) return 'LOADED_IN_THIS_DISTRICT';
/**
* §9.3 — "*Requirements: a load on the industry's track AND AN EMPTY CAR OF THAT TYPE IN THE
* DIVISION YARD. The first Laborer replaces the load with an empty car of that type.*"
@@ -1617,17 +1670,23 @@ function execute(s: GameState, player: PlayerIndex, i: Intent): GameEvent[] {
* `passengerPerCoach` (`content.ts`). Half a passenger movement is half the work, and the rate
* is named per COACH because a Porter handles exactly one coach per action.
*/
case 'porter.board':
case 'porter.board': {
// `check` has already established there is one; resolving it HERE, once, is what stops the
// reducer from finding a different train than the one the rules were tested against.
const work = passengerWork(s, player, 'board', i.trayId)!;
return [
{ type: 'passengersBoarded', player, at: i.at },
{ type: 'passengersBoarded', player, at: i.at, ...work },
...earns(s, player, houseRules(s.config).revenue.passengerPerCoach, 'boarding'),
];
}
case 'porter.detrain':
case 'porter.detrain': {
const work = passengerWork(s, player, 'detrain', i.trayId)!;
return [
{ type: 'passengersDetrained', player, at: i.at },
{ type: 'passengersDetrained', player, at: i.at, ...work },
...earns(s, player, houseRules(s.config).revenue.passengerPerCoach, 'detraining'),
];
}
case 'laborer.startLoad': {
const f = facilityAt(s, player, i.at)!;
@@ -1956,9 +2015,14 @@ export function reduce(s: GameState, e: GameEvent): void {
const card = area.grid.get(coordKey(e.to));
if (tray && card) {
tray.consist = tray.consist.slice(0, tray.consist.length - e.stock.length);
const track = card.facility?.industryTrack;
if (track) track.cars.push(...e.stock);
else card.standing.push(...e.stock);
// `carsOn` is the one function that knows WHERE cars stand on a given card — an industry
// track for a freight facility, the card itself for everything else. Written out longhand
// here it was a second copy of that rule, and the copy was wrong for a Passenger Facility:
// it has an `industryTrack` too (an empty one, `setup.ts`), so a cut pushed into an Office
// would have landed somewhere `carsOn` cannot see — cars on the board that no train can
// couple and no walk is blocked by. `check` refuses a non-freight target, so this never
// fired; a trap that needs another rule to stay unsprung is still a trap.
carsOn(card).push(...e.stock);
}
turnOf(s, e.player).movesRemaining -= 1;
spendCard(s, e.player, e.cardId);
@@ -2049,7 +2113,8 @@ export function reduce(s: GameState, e: GameEvent): void {
const f = facilityAt(s, e.player, e.at)!;
const idx = f.inboundBox.findIndex((c) => c.type === e.stock.type && c.loaded === e.stock.loaded);
if (idx >= 0) f.inboundBox.splice(idx, 1);
s.yards.classificationYard.push(e.stock);
// `pooled` — a car back in a yard is back in the common supply, carrying nothing (state.ts).
s.yards.classificationYard.push(pooled(e.stock));
turnOf(s, e.player).freightAgentUsed = true;
break;
}
@@ -2064,7 +2129,7 @@ export function reduce(s: GameState, e: GameEvent): void {
const idx = box.findIndex((c) => c.type === e.stock.type);
if (idx >= 0) box.splice(idx, 1);
}
s.yards.classificationYard.push(e.stock);
s.yards.classificationYard.push(pooled(e.stock));
turnOf(s, e.player).freightAgentUsed = true;
break;
}
@@ -2132,19 +2197,28 @@ export function reduce(s: GameState, e: GameEvent): void {
break;
}
/**
* THE TRAIN AND THE COACH THE PLAYER PICKED, not "the first one on the A/D tracks".
*
* This used to walk `adOccupancy` and fill the first empty coach it met, which is why two trains
* standing at one station both answered to whichever chip was clicked (v0.4.9d playtest), and
* why it could fill a coach on a train whose card refuses passenger work — `check` skipped such
* a train and the reducer did not. `e.trayId`/`e.coachIndex` are exactly what `passengerWork`
* resolved for `check`, carried on the event rather than looked up again here.
*/
case 'passengersBoarded': {
const f = facilityAt(s, e.player, e.at)!;
const area = areaOf(s, e.player);
const idx = f.outboundBox.findIndex((c) => c.type === 'coach' && c.loaded);
const loaded = f.outboundBox.splice(idx, 1)[0]!;
for (const id of area.adOccupancy) {
const tray = s.trays.get(id);
const ci = tray?.consist.findIndex((c) => c.type === 'coach' && !c.loaded) ?? -1;
if (tray && ci >= 0) {
s.yards.classificationYard.push(tray.consist[ci]!);
tray.consist[ci] = loaded;
break;
}
const tray = s.trays.get(e.trayId);
if (tray && tray.consist[e.coachIndex]) {
s.yards.classificationYard.push(pooled(tray.consist[e.coachIndex]!));
/**
* Stamped with the district that filled it — the chip turned upside down in the tray. These
* passengers may not alight anywhere in this Office Area; the train has to carry them to a
* different one. See `RollingStock.origin` in state.ts.
*/
tray.consist[e.coachIndex] = { ...loaded, origin: seatOf(s, e.player) };
}
f.usedThisStage.porters += 1;
break;
@@ -2152,21 +2226,18 @@ export function reduce(s: GameState, e: GameEvent): void {
case 'passengersDetrained': {
const f = facilityAt(s, e.player, e.at)!;
const area = areaOf(s, e.player);
for (const id of area.adOccupancy) {
const tray = s.trays.get(id);
const ci = tray?.consist.findIndex((c) => c.type === 'coach' && c.loaded) ?? -1;
if (tray && ci >= 0) {
// The empty coach comes OUT OF THE DIVISION YARD, as §9.2 says. It used to be conjured,
// which minted a coach on every de-training. Throws now, for the reason in `unloadBegan`.
const yi = s.yards.divisionYard.findIndex((c) => c.type === 'coach' && !c.loaded);
if (yi < 0) throw new Error('passengersDetrained: no empty coach in the Division Yard');
const empty = s.yards.divisionYard.splice(yi, 1)[0]!;
refillDivisionYardIfEmpty(s);
f.inboundBox.push(tray.consist[ci]!);
tray.consist[ci] = empty;
break;
}
const tray = s.trays.get(e.trayId);
if (tray && tray.consist[e.coachIndex]) {
// The empty coach comes OUT OF THE DIVISION YARD, as §9.2 says. It used to be conjured,
// which minted a coach on every de-training. Throws now, for the reason in `unloadBegan`.
const yi = s.yards.divisionYard.findIndex((c) => c.type === 'coach' && !c.loaded);
if (yi < 0) throw new Error('passengersDetrained: no empty coach in the Division Yard');
const empty = s.yards.divisionYard.splice(yi, 1)[0]!;
refillDivisionYardIfEmpty(s);
// The arriving coach goes into the red box carrying nothing: the journey it was stamped for
// is over, and the box feeds straight back to a yard through the Freight Agent.
f.inboundBox.push(pooled(tray.consist[e.coachIndex]!));
tray.consist[e.coachIndex] = empty;
}
f.usedThisStage.porters += 1;
break;
@@ -2202,8 +2273,14 @@ export function reduce(s: GameState, e: GameEvent): void {
workTrack(f)[workTrack(f).length - 1] = null;
const ci = f.industryTrack.cars.findIndex((c) => !c.loaded && c.type === e.carType);
if (ci >= 0) {
s.yards.classificationYard.push(f.industryTrack.cars[ci]!);
f.industryTrack.cars[ci] = { type: e.carType, loaded: true };
s.yards.classificationYard.push(pooled(f.industryTrack.cars[ci]!));
/**
* THE LOAD IS STAMPED WITH THE DISTRICT THAT MADE IT — the chip turned upside down in the
* tray. `laborer.beginUnload` refuses a car stamped with the district it is standing in, so
* this load now has to leave the Office Area on a train before anyone can break it. See
* `RollingStock.origin` in state.ts for the rule and why it is a seat.
*/
f.industryTrack.cars[ci] = { type: e.carType, loaded: true, origin: seatOf(s, e.player) };
}
f.usedThisStage.laborers += 1;
break;