fix tracks to all be EW. no more NS tracks

This commit is contained in:
Jesse
2026-08-05 01:00:11 -04:00
parent 821d3cdffa
commit 2d348e1ecf
16 changed files with 1157 additions and 355 deletions
+184 -51
View File
@@ -11,6 +11,7 @@ import type {
GridCoord,
OfficeArea,
RollingStock,
TrackArc,
TrackCard,
TurnoutOrientation,
} from '../src/engine/state.ts';
@@ -28,8 +29,12 @@ import {
opposite,
reachableDestinations,
connectionsFor,
joins,
slopeAt,
slopeOfPair,
variantsFor,
} from '../src/engine/track.ts';
import { TRACK_SUPPLY } from '../src/engine/content.ts';
// ---------------------------------------------------------------------------
// Fixture helpers
@@ -44,9 +49,13 @@ const straight = (standing: RollingStock[] = []): TrackCard => ({
enhancements: [],
});
/** A straight rotated to run north-south. Needed to meet the Office's junction stubs (Gap 11). */
const nsStraight = (standing: RollingStock[] = []): TrackCard => ({
geometry: { kind: 'track', geometry: 'straight', axis: 'ns' },
/**
* A curve: along the centre line from the east or west edge to a frog, then out at 45° through the
* middle of the north or south edge. `ne`/`sw` lie on one diagonal, `nw`/`se` on the other, and only
* cards on the SAME diagonal meet across a horizontal edge.
*/
const curve = (arc: TrackArc, standing: RollingStock[] = []): TrackCard => ({
geometry: { kind: 'track', geometry: 'curved', arc },
baseOperationalRail: true,
standing,
facility: null,
@@ -100,7 +109,7 @@ const car = (): RollingStock => ({ type: 'boxcar', loaded: false });
function straightCard(): TrackCard {
return {
geometry: { kind: 'track', geometry: 'straight', axis: 'ew' },
geometry: { kind: 'track', geometry: 'straight' },
baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [],
};
}
@@ -174,26 +183,33 @@ describe('ports and geometry', () => {
assert.ok(exitsFrom(t, 's').includes('e'), 'diverging leg back to stem');
});
it('gives the Office card a junction stub BELOW only', () => {
// Q7 — the card art draws the through-track along the top edge with everything diverging
// downward, so a district grows beneath the Running Track. Supersedes Gap 8.
it('gives the Office card a plain east-west track and no stub at all', () => {
// Every office and industry card on the printed sheet — Depot, Station, Terminal, Refinery,
// Freighthouse, Coal Tipple, Manufacturing, Town — is a straight east-west rail with no
// diverging leg. A district hangs off a TURNOUT laid on the Running Track instead.
const o = officeCard();
for (const p of ['s', 'e', 'w'] as Port[]) assert.ok(hasPort(o, p), `office port ${p}`);
assert.ok(!hasPort(o, 'n'), 'nothing may attach above the Running Track');
assert.ok(exitsFrom(o, 's').includes('e'));
assert.ok(exitsFrom(o, 's').includes('w'));
for (const p of ['e', 'w'] as Port[]) assert.ok(hasPort(o, p), `office port ${p}`);
for (const p of ['n', 's'] as Port[]) assert.ok(!hasPort(o, p), `office must not have a ${p} stub`);
assert.deepEqual(exitsFrom(o, 'e'), ['w']);
});
it('joins a curve from one end of the through track downward', () => {
// Curves come in left and right hands as distinct cards; the leg always goes DOWN (Q7), and
// handedness decides which end of the through track it leaves from.
it('runs every straight east-west, with no north-south rotation to choose', () => {
// There is no north-south track anywhere on the printed sheet, and there cannot be: a vertical
// rail meets a card edge at 90°, and everything crossing a horizontal edge does so at 45°.
assert.deepEqual(connectionsFor(straight()), [['e', 'w']]);
assert.equal(variantsFor('straight', 'none').length, 1, 'a straight has ONE orientation');
});
it('joins a curve from one end of the through track to the middle of an edge', () => {
// A curve runs along the centre line to a frog and leaves at 45°. Handedness decides which
// diagonal; turning the card 180° decides whether the leg goes north or south.
const r: TrackCard = {
geometry: { kind: 'track', geometry: 'curved', hand: 'left' },
baseOperationalRail: true,
standing: [],
facility: null,
modifiers: [],
enhancements: [],
enhancements: [],
};
assert.deepEqual(exitsFrom(r, 's'), ['w']);
});
@@ -201,16 +217,107 @@ describe('ports and geometry', () => {
// ---------------------------------------------------------------------------
describe('the 45° matching rule', () => {
const arcCard = (arc: TrackArc): TrackCard => curve(arc);
const turnoutOf = (o: TurnoutOrientation): TrackCard => turnout(o);
it('puts a pair of arcs on each diagonal, named after the pair', () => {
// The name IS the rule: `ne` and `sw` lie on one line, `nw` and `se` on the other.
assert.equal(slopeOfPair('n', 'e'), 'ne_sw');
assert.equal(slopeOfPair('s', 'w'), 'ne_sw');
assert.equal(slopeOfPair('n', 'w'), 'nw_se');
assert.equal(slopeOfPair('s', 'e'), 'nw_se');
assert.equal(slopeOfPair('e', 'w'), null, 'the through track has no slope');
});
it('joins two cards stacked vertically only when their legs share a diagonal', () => {
// `sw` above `ne` is one unbroken 45° rail across the card edge. `sw` above `nw` is a V: both
// cards have the port, the rails meet at the same point, and they still do not connect.
assert.ok(joins(arcCard('sw'), 's', arcCard('ne')), 'sw over ne is continuous rail');
assert.ok(joins(arcCard('se'), 's', arcCard('nw')), 'se over nw is continuous rail');
assert.ok(!joins(arcCard('sw'), 's', arcCard('nw')), 'sw over nw is a V');
assert.ok(!joins(arcCard('se'), 's', arcCard('ne')), 'se over ne is a V');
});
it('matches a turnout to the curve of the same hand, and to no other', () => {
// A siding needs one card of each hand: a left turnout to drop off the main, a right curve to
// climb back. This is the pairing that makes that true.
const leftTurnout = turnoutOf({ stem: 'w', through: 'e', diverge: 's' });
const rightTurnout = turnoutOf({ stem: 'e', through: 'w', diverge: 's' });
assert.ok(joins(leftTurnout, 's', arcCard('ne')));
assert.ok(!joins(leftTurnout, 's', arcCard('nw')));
assert.ok(joins(rightTurnout, 's', arcCard('nw')));
assert.ok(!joins(rightTurnout, 's', arcCard('ne')));
});
it('ignores slope on an east-west edge, where every card meets at the same height', () => {
assert.ok(joins(straight(), 'e', straight()));
assert.ok(joins(arcCard('ne'), 'e', straight()), 'a curve reaching east meets the through line');
});
it('never connects north to south on any card the supply can produce', () => {
// Descending a row therefore costs at least two cards — leg down, then a curve turning the run
// back east-west. A district can never be a vertical column, which is what the sheet shows.
for (const { geometry, hand } of TRACK_SUPPLY) {
for (const v of variantsFor(geometry, hand)) {
const card = { ...straight(), geometry: { kind: 'track' as const, geometry, ...v, ...(hand !== 'none' ? { hand } : {}) } };
for (const [a, b] of connectionsFor(card)) {
const pair = new Set([a, b]);
assert.ok(!(pair.has('n') && pair.has('s')), `${geometry}/${hand} joins north to south`);
}
}
}
});
it('gives every north or south port exactly one slope', () => {
// `slopeAt` returns the first matching pair, so a card carrying two differently-sloped legs at
// one edge would silently answer with whichever came first. Nothing may.
for (const { geometry, hand } of TRACK_SUPPLY) {
for (const v of variantsFor(geometry, hand)) {
const card = { ...straight(), geometry: { kind: 'track' as const, geometry, ...v, ...(hand !== 'none' ? { hand } : {}) } };
for (const p of ['n', 's'] as Port[]) {
const slopes = new Set(
connectionsFor(card).filter(([a, b]) => a === p || b === p).map(([a, b]) => slopeOfPair(a, b)),
);
assert.ok(slopes.size <= 1, `${geometry}/${hand} has two slopes at its ${p} edge`);
}
}
}
});
it('ties the slope to the printed hand, so turning a card never changes diagonal', () => {
// A printed card has a back: it turns 180° but never flips. That is the whole reason handedness
// is not decoration.
for (const geometry of ['curved', 'sharpCurved', 'turnout'] as const) {
for (const [hand, expected] of [['left', 'ne_sw'], ['right', 'nw_se']] as const) {
const variants = variantsFor(geometry, hand);
assert.equal(variants.length, 2, `${geometry}/${hand} should offer 0° and 180°, and nothing else`);
const legs = variants.map((v) => {
const card = { ...straight(), geometry: { kind: 'track' as const, geometry, ...v, hand } };
return (slopeAt(card, 'n') ?? slopeAt(card, 's'));
});
assert.deepEqual(legs, [expected, expected], `${geometry}/${hand} changes diagonal when turned`);
}
}
});
});
// ---------------------------------------------------------------------------
describe('Move legality', () => {
// row 0: [D] [E office ] [F] <- Running Track
// row -1: [B n-s straight] <- Secondary Track hangs BELOW (Q7)
// row 0: [straight] [office] [turnout, leg south] [straight] <- Running Track
// row -1: [curve ne ] <- Secondary Track
//
// The turnout's leg is on the `ne_sw` diagonal ({w,s}), so the card beneath it must be the arc on
// the SAME diagonal that reaches north — `ne`. `nw` would meet the same point and still not join.
const basicArea = (): OfficeArea =>
areaFrom(
{
[coordKey(at(0, -1))]: straight(),
[coordKey(at(0, 0))]: officeCard(),
[coordKey(at(0, 1))]: straight(),
[coordKey(at(-1, 0))]: nsStraight(),
[coordKey(at(0, 1))]: turnout({ stem: 'w', through: 'e', diverge: 's' }),
[coordKey(at(0, 2))]: straight(),
[coordKey(at(-1, 1))]: curve('ne'),
},
at(0, 0),
);
@@ -218,15 +325,24 @@ describe('Move legality', () => {
it('travels any distance in a straight line', () => {
// §2.4 — "regardless of distance".
const area = basicArea();
const dests = reachableDestinations(ctxFor(area), at(0, 1), 'w');
const dests = reachableDestinations(ctxFor(area), at(0, 2), 'w');
assert.ok(has(dests, 0, 0), 'reaches the office');
assert.ok(has(dests, 0, -1), 'reaches past it');
});
it('branches onto Secondary Track through the Office stub', () => {
it('branches onto Secondary Track through a turnout on the Running Track', () => {
// The crew must enter the turnout through its STEM (west) to take the diverging leg (§A.1).
const area = basicArea();
const dests = reachableDestinations(ctxFor(area), at(0, 1), 'w');
assert.ok(has(dests, -1, 0), 'reaches the card below the office');
const dests = reachableDestinations(ctxFor(area), at(0, 0), 'e');
assert.ok(has(dests, -1, 1), 'reaches the curve below the turnout');
});
it('will not take a diverging leg entered from the through end', () => {
// §A.1 — through and diverge are not joined to each other. Approaching from the east reaches
// the stem and nothing else, so the leg below is unreachable from that side.
const area = basicArea();
const dests = reachableDestinations(ctxFor(area), at(0, 2), 'w');
assert.ok(!has(dests, -1, 1), 'no route from the through end onto the diverging leg');
});
it('never reverses within a single Move', () => {
@@ -239,19 +355,20 @@ describe('Move legality', () => {
it('separates forward and reverse into different Moves', () => {
const area = basicArea();
const both = allReachable(ctxFor(area), at(0, 0), 'e');
assert.ok(has(both.forward, 0, 1));
// Col 1 is the turnout, which carries no wheel icon, so east means running through it to col 2.
assert.ok(has(both.forward, 0, 2));
assert.ok(has(both.reverse, 0, -1));
assert.ok(!has(both.forward, 0, -1), 'reverse destinations must cost their own Move');
});
it('will not stop on a turnout', () => {
// row 0: [start] [turnout ]
// row -1: [C n-s straight]
// row 0: [start] [turnout, leg south]
// row -1: [curve ne ]
const area = areaFrom(
{
[coordKey(at(0, 0))]: straight(),
[coordKey(at(0, 1))]: turnout({ stem: 'w', through: 'e', diverge: 's' }),
[coordKey(at(-1, 1))]: nsStraight(),
[coordKey(at(-1, 1))]: curve('ne'),
},
at(0, 0),
);
@@ -372,32 +489,46 @@ describe('occupancy', () => {
// ---------------------------------------------------------------------------
describe('placement and drop-off', () => {
it('lets the first Facility attach to the Office stub', () => {
// Gap 8 — the case that was impossible before Office cards carried junction stubs.
it('grows a district on BOTH sides of the Running Track', () => {
// A turnout turned 180° sends its 45° leg north instead of south, so there is nothing special
// about the rows below. Q7 used to reject everything above the Running Track outright.
const area = areaFrom(
{
[coordKey(at(0, -1))]: straight(),
[coordKey(at(0, 0))]: officeCard(),
[coordKey(at(0, 1))]: straight(),
[coordKey(at(0, 1))]: turnout({ stem: 'w', through: 'e', diverge: 's' }),
[coordKey(at(0, 2))]: turnout({ stem: 'e', through: 'w', diverge: 'n' }),
},
at(0, 0),
);
assert.ok(canPlaceAt(area, at(-1, 0), nsStraight()), 'below the Office');
assert.ok(!canPlaceAt(area, at(1, 0), nsStraight()), 'nothing attaches above the Running Track');
assert.ok(canPlaceAt(area, at(-1, 1), curve('ne')), 'below a south-diverging turnout');
assert.ok(canPlaceAt(area, at(1, 2), curve('sw')), 'above a north-diverging turnout');
});
it('refuses a card whose 45° leg lies on the wrong diagonal', () => {
// Both cards have the port and both legs meet the same point on the shared edge. They still do
// not join: one descends to the right and the other to the left, so the rails form a V.
const area = areaFrom(
{
[coordKey(at(0, 0))]: officeCard(),
[coordKey(at(0, 1))]: turnout({ stem: 'w', through: 'e', diverge: 's' }),
},
at(0, 0),
);
assert.ok(canPlaceAt(area, at(-1, 1), curve('ne')), 'the matching diagonal joins');
assert.ok(!canPlaceAt(area, at(-1, 1), curve('nw')), 'the opposite diagonal must not');
});
it('refuses a card whose ports do not meet the neighbour it touches', () => {
// Gap 11 in miniature: an east-west straight placed north of the Office has no south port,
// so it cannot attach to the stub. Orientation is not cosmetic.
// An east-west straight below a turnout has no north port at all, so nothing can reach it.
const area = areaFrom(
{
[coordKey(at(0, -1))]: straight(),
[coordKey(at(0, 0))]: officeCard(),
[coordKey(at(0, 1))]: straight(),
[coordKey(at(0, 1))]: turnout({ stem: 'w', through: 'e', diverge: 's' }),
},
at(0, 0),
);
assert.ok(!canPlaceAt(area, at(-1, 0), straight()), 'e-w straight cannot meet a n-s stub');
assert.ok(!canPlaceAt(area, at(-1, 1), straight()), 'an east-west straight cannot meet a 45° leg');
});
it('refuses a card that connects to nothing', () => {
@@ -464,21 +595,20 @@ describe('curves are arcs, and arcs make sidings possible', () => {
}
});
it('can reach NORTH, which nothing but an n-s straight could before', () => {
// This is the whole point. With every leg diverging south, a district could only ever be a
// vertical column — no siding, no parallel track, no way back up to the Running Track.
it('can reach NORTH, which is the only way back up to the Running Track', () => {
// Without it a district could only ever hang below the main — no siding, no run-around.
const reachesNorth = (['ne', 'nw'] as const).every((arc) =>
connectionsFor(arcCard(arc)).some((p) => p.includes('n')),
);
assert.ok(reachesNorth, 'a curve still cannot reach north');
});
it('offers all four rotations when placed', () => {
// A two-port arc has no handedness that survives turning — its mirror IS one of its rotations —
// so the printed hand governs supply, not what can be built.
it('offers two rotations, and which two depends on the printed hand', () => {
// A card turns 180° but never flips, so a left-hand curve reaches `sw` and `ne` and can never
// become `se` or `nw` — those are the right-hand card.
for (const g of ['curved', 'sharpCurved'] as const) {
const arcs = variantsFor(g).map((v) => v.arc).sort();
assert.deepEqual(arcs, ['ne', 'nw', 'se', 'sw'], `${g} does not offer all four rotations`);
assert.deepEqual(variantsFor(g, 'left').map((v) => v.arc).sort(), ['ne', 'sw'], `${g} left`);
assert.deepEqual(variantsFor(g, 'right').map((v) => v.arc).sort(), ['nw', 'se'], `${g} right`);
}
});
@@ -493,16 +623,19 @@ describe('curves are arcs, and arcs make sidings possible', () => {
});
it('lets a crew run a siding and rejoin the main', () => {
// The shape the whole change exists for: leave the Running Track, run parallel, come back up.
// THE CANONICAL SIDING, and the reason handedness matters: leave the Running Track on a LEFT
// turnout, drop onto its matching left curve (`ne`), run parallel, and climb back on a RIGHT
// curve (`nw`) into a RIGHT turnout. One card of each hand, and the slopes agree at both ends.
// A crew must ENTER a turnout through its stem to take the diverging leg (§A.1).
const ew = (): TrackCard => straightCard();
const grid: Record<string, TrackCard> = {
'0,-2': { geometry: { kind: 'track', geometry: 'straight', axis: 'ew' }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'0,-1': { geometry: { kind: 'track', geometry: 'turnout', turnout: { stem: 'w', through: 'e', diverge: 's' } }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'0,-2': ew(),
'0,-1': { geometry: { kind: 'track', geometry: 'turnout', turnout: { stem: 'w', through: 'e', diverge: 's' }, hand: 'left' }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'-1,-1': arcCard('ne'),
'-1,0': { geometry: { kind: 'track', geometry: 'straight', axis: 'ew' }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'-1,0': ew(),
'-1,1': arcCard('nw'),
'0,1': { geometry: { kind: 'track', geometry: 'turnout', turnout: { stem: 'e', through: 'w', diverge: 's' } }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'0,0': { geometry: { kind: 'track', geometry: 'straight', axis: 'ew' }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'0,1': { geometry: { kind: 'track', geometry: 'turnout', turnout: { stem: 'e', through: 'w', diverge: 's' }, hand: 'right' }, baseOperationalRail: true, standing: [], facility: null, modifiers: [], enhancements: [] },
'0,0': ew(),
};
const area = areaFrom(grid, { row: 0, col: 0 });
const ctx = { area, occupancy: { trayAt: () => null, freeAdTracks: () => 2 }, consistSize: 0, self: 'crew' as const };