The Yard Office is offered, reachable, and can be run into (Gitea#5)

"Trains that are only freight (cabooses ok, no coaches allowed) that arrive in a
player's area who has the yard office card get an extra ability… the game will
offer that player the option… They can of course still choose to have the train go
to the standard office."

It was implemented, in a stripped form missing all three conditions: a qualifying
train was TELEPORTED onto the Yard Office card. Nobody was asked, no route was
computed — so the card's own printed "that can reach the yard office in one move"
was unenforced — and because nothing was walked, nothing was ever met on the way.

All three now hold:

  - OFFERED to whoever sits in the district, interrupting the Mainline Phase on the
    turn the train arrives. Declining is an ordinary arrival onto an A/D track.
  - REACHABILITY is the engine's own move walk. `exploreMoves` already means what
    the card means — any distance without changing direction, finishing on
    Operational Rail (§2.4, §A.1) — so using it is what makes code and card agree.
    Reversing is a separate Move, so a yard that can only be reached by backing up
    is correctly out of reach.
  - CARS ON THE LEAD COLLIDE. The walk does not treat standing cars as obstacles;
    it COUPLES them, because that is what a switching move does. An arriving train
    is not switching, so what it would have coupled is what it is about to hit —
    the same reading §8.3 already applies to the Running Track. `destination.couples`
    is therefore the fouling signal, and it needed no new machinery.

Per Jesse's ruling (2026-08-29) the two failures his issue names are kept apart: no
route means no offer, with the history saying why ("make sure this is logged in
history — why can't move so user knows why they can't get to yard"); a route that
exists but is fouled IS offered, and taking it crashes. A silent absence is
indistinguishable from a broken feature, which is how the missing check survived.

THE SHARED REFACTOR THIS NEEDED. `pendingDecision` was one question asked of one
player — §8.1's clearance, always the Superintendent — and `currentActor` hardcoded
that. It is a discriminated union now, with `decisionActor` as the single place that
maps a question to whoever must answer it, and `clearanceRuling` generalised to
`decisionAnswer`. Six copies of `pendingDecision !== null ? superintendent :
currentActor` across the engine, the sim, the web client and the tests collapse into
`actingPlayer`; they had already stopped being right the moment a second kind of
question existed. Gitea#19 needs the same machinery and now only has to add a case.

A BUG THE FIRST CUT WALKED INTO, worth recording because it is a trap the next
interruption will meet too: the offer must be put BEFORE the train is taken off its
Mainline card. `needsClearance` unwinds the whole phase and the driver re-enters
from the top, so asking after the `transits` filter cost the train its place on the
card and the answer had nowhere to land. §8.1 gets this right by asking before it
commits, and the Yard Office now does the same.

The developer bot declines: the Yard Office frees an A/D track, but the lead may be
fouled and the bot cannot read its own yard well enough to tell (`TODO.md`, Bot
Performance). Declining is always safe and keeps the harness comparable with every
measurement taken before this rule existed.

851 tests pass.

Closes #5

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EAgJSmeV8zrMh55Mj85ESb
This commit is contained in:
Jesse.Markowitz
2026-08-29 06:57:31 -04:00
co-authored by Claude Opus 5
parent 5e34c73b16
commit 228027637b
17 changed files with 461 additions and 58 deletions
+112 -5
View File
@@ -14,7 +14,7 @@ import { applyIntent, areaOf, check, hasDistrictEnhancement, isProtectedFromDera
import { ENHANCEMENT_RULES, enhancementRule, trainProfile } from '../src/engine/content.ts';
import { createGame } from '../src/engine/setup.ts';
import type { GameConfig, GameState, GridCoord, TrackCard } from '../src/engine/state.ts';
import { coordKey, subdivisions, turnOf } from '../src/engine/state.ts';
import { coordKey, decisionActor, subdivisions, turnOf } from '../src/engine/state.ts';
const config: GameConfig = {
mode: 'solitaire',
@@ -287,17 +287,124 @@ describe('Interlocking and Yard Office relieve the Office', () => {
assert.equal(s.players[0]!.revenue, -5);
});
it('diverts a coachless train to the Yard Office', () => {
/**
* §11, THE YARD OFFICE (Gitea#5) — offered, not imposed, and only down a route that exists.
*
* Jesse: "you have to ask if non-coach trains wish to go in there, rather than to the office",
* "if the Yard Office is not accessible in one move, you should not get the option", and cars on
* the way in "result in a crash". All three were missing: the train was teleported onto the card.
*/
const answer = (s: GameState, take: boolean) => {
const who = decisionActor(s);
assert.notEqual(who, null, 'nothing was pending, so there was nothing to answer');
const r = applyIntent(s, who!, { type: 'mainline.yardOffice', take });
assert.ok(r.ok, 'the district owner could not answer the Yard Office offer');
advance(s);
};
it('OFFERS the Yard Office to the district owner rather than diverting automatically', () => {
const s = game();
const card = straight();
card.enhancements.push('yardOffice');
addCard(s, at(-1, 0), card);
addCard(s, at(0, 2), card);
const id = inbound(s, [{ type: 'hopper', loaded: true }]);
advance(s);
assert.equal(s.clock.pendingDecision?.kind, 'yardOffice', 'the phase did not stop to ask');
assert.equal(decisionActor(s), 0, 'the question went to the wrong player');
const pos = s.trays.get(id)!.position;
assert.ok(pos.at === 'grid' && pos.coord.row === -1, 'arrived at the Yard Office');
assert.ok(!areaOf(s, 0).adOccupancy.includes(id), 'did not take an A/D track');
assert.ok(pos.at !== 'grid' || pos.coord.col !== 2, 'the train moved before anyone answered');
});
it('takes the Yard Office when the owner says yes', () => {
const s = game();
const card = straight();
card.enhancements.push('yardOffice');
addCard(s, at(0, 2), card);
const id = inbound(s, [{ type: 'hopper', loaded: true }]);
advance(s);
answer(s, true);
const pos = s.trays.get(id)!.position;
assert.ok(pos.at === 'grid' && pos.coord.col === 2, 'did not arrive at the Yard Office');
assert.ok(!areaOf(s, 0).adOccupancy.includes(id), 'took an A/D track anyway');
assert.equal(s.players[0]!.revenue, 0, 'a clear lead should not have collided');
});
it('goes to the Train Order Office when the owner says no', () => {
// "They can of course still choose to have the train go to the standard office."
const s = game();
const card = straight();
card.enhancements.push('yardOffice');
addCard(s, at(0, 2), card);
const id = inbound(s, [{ type: 'hopper', loaded: true }]);
advance(s);
answer(s, false);
assert.ok(areaOf(s, 0).adOccupancy.includes(id), 'declining did not put it on an A/D track');
});
it('does not offer what cannot be reached, and says why in the history', () => {
/**
* Jesse, 2026-08-29: "make sure this is logged in history — why can't move so user knows why
* they can't get to yard." A silent absence is indistinguishable from a broken feature, which
* is how the missing reachability check survived this long.
*/
const s = game();
const card = straight();
card.enhancements.push('yardOffice');
// Far off the Running Track, with nothing laid between: no route in one move.
addCard(s, at(3, 4), card);
inbound(s, [{ type: 'hopper', loaded: true }]);
const events = advance(s).events;
assert.equal(s.clock.pendingDecision, null, 'offered a Yard Office it cannot reach');
const said = events.find(
(e) => e.type === 'trainDiverted' && e.reason.includes('could not be offered'),
);
assert.ok(said, `nothing in the history explains why:\n${JSON.stringify(events, null, 1)}`);
assert.match(
(said as { reason: string }).reason,
/one move/,
'the reason does not say it is out of reach in one move',
);
});
it('offers a fouled lead, and taking it collides', () => {
/**
* The third missing condition. "Just like other trains finding cars on the tracks you use to
* get into either result in a crash" — and Jesse's ruling keeps the OFFER: a route that exists
* is offered, and the consequence of taking it is the player's. §8.3 already reads cars in the
* path of an arriving train as a collision rather than a coupling.
*/
const s = game();
const card = straight();
card.enhancements.push('yardOffice');
addCard(s, at(0, 2), card);
// A car standing on the lead between the Office and the yard.
areaOf(s, 0).grid.get(coordKey(at(0, 1)))!.standing = [{ type: 'boxcar', loaded: false }];
const id = inbound(s, [{ type: 'hopper', loaded: true }]);
advance(s);
assert.equal(s.clock.pendingDecision?.kind, 'yardOffice', 'a fouled lead was not offered at all');
answer(s, true);
assert.equal(s.players[0]!.revenue, -5, 'running through standing cars did not collide');
assert.ok(!s.trays.has(id), 'the train survived the collision');
});
it('declining a fouled lead is safe — the standard Office is unaffected', () => {
const s = game();
const card = straight();
card.enhancements.push('yardOffice');
addCard(s, at(0, 2), card);
areaOf(s, 0).grid.get(coordKey(at(0, 1)))!.standing = [{ type: 'boxcar', loaded: false }];
const id = inbound(s, [{ type: 'hopper', loaded: true }]);
advance(s);
answer(s, false);
assert.equal(s.players[0]!.revenue, 0, 'declining the Yard Office still cost a collision');
assert.ok(areaOf(s, 0).adOccupancy.includes(id), 'the train did not reach the Office');
});
it('does not divert a train carrying coaches', () => {