diff --git a/server/src/content/civilizations/huns.rs b/server/src/content/civilizations/huns.rs index 56c23f88..1e87ea08 100644 --- a/server/src/content/civilizations/huns.rs +++ b/server/src/content/civilizations/huns.rs @@ -176,7 +176,7 @@ pub(crate) fn finish_city_move(game: &mut Game, player: usize, from: Position, t game.player_mut(player).event_info.remove("Nomads moving"); let origin = EventOrigin::SpecialAdvance(SpecialAdvance::Nomads); activate_city(from, game, &origin); - let mountain = game.map.get(to) == Some(&Terrain::Mountain); + let mountain = game.map.get(to).map(Terrain::movement_terrain) == Some(&Terrain::Mountain); let city = game.player_mut(player).get_city_mut(from); city.position = to; city.nomad_mountain = mountain; diff --git a/server/src/map.rs b/server/src/map.rs index 9e9c2af6..8a43da0c 100644 --- a/server/src/map.rs +++ b/server/src/map.rs @@ -184,6 +184,16 @@ pub enum Terrain { } impl Terrain { + /// Terrain identity for movement rules only. Exhaustion still prevents resource + /// collection and founding; callers of those rules must keep the wrapper. + #[must_use] + pub fn movement_terrain(&self) -> &Self { + match self { + Self::Exhausted(terrain) => terrain.movement_terrain(), + terrain => terrain, + } + } + #[must_use] pub fn is_water(&self) -> bool { matches!(self, Terrain::Water) diff --git a/server/src/movement.rs b/server/src/movement.rs index 0bd7ef79..fd72b817 100644 --- a/server/src/movement.rs +++ b/server/src/movement.rs @@ -831,7 +831,7 @@ fn terrain_movement_restriction( let terrain = map .get(destination) .expect("the destination position should exist on the map"); - match terrain { + match terrain.movement_terrain() { Fertile => Some(MovementRestriction::Fertile), Mountain => Some(MovementRestriction::Mountain), Forest if unit.is_army_unit() => Some(MovementRestriction::Forest), diff --git a/server/src/web_view/actions.rs b/server/src/web_view/actions.rs index 56c54fcf..98e85e91 100644 --- a/server/src/web_view/actions.rs +++ b/server/src/web_view/actions.rs @@ -199,7 +199,7 @@ fn terrain_notes( destination: Position, ) -> Vec<&'static str> { let mut notes = vec![]; - match game.map.get(destination) { + match game.map.get(destination).map(Terrain::movement_terrain) { Some(Terrain::Mountain) => { if crate::content::civilizations::carthage::ignores_mountains(player, units) { notes.push("Hannibal with elephants: ignores the mountain movement stop."); @@ -268,7 +268,9 @@ pub fn movement(game: &Game, seat: usize, units: Vec) -> Result + JSON.parse(engine.webQuery(JSON.stringify(state), 0, JSON.stringify({ kind: 'movement', units }))) + .destinations; +const move = (state, action, seat = 0) => + JSON.parse(engine.move(JSON.stringify(state), JSON.stringify(action), seat)); +const action = (destination, units = [0], payment = {}) => ({ + Movement: { Move: { units, destination, payment } }, +}); +function fixture(terrain, exhausted, unitType = 'Infantry') { + const s = structuredClone(template); + s.state = 'Playing'; + s.actions_left = 3; + s.map.tiles = [ + ['A1', 'Barren'], + ['B1', exhausted ? { Exhausted: terrain } : terrain], + ['C2', 'Barren'], + ['B2', 'Barren'], + ]; + for (const p of s.players) { + p.cities = []; + p.units = []; + } + s.players[0].advances.push('Tactics'); + s.players[0].units = [ + { id: 0, position: 'A1', unit_type: unitType }, + { id: 1, position: 'B1', unit_type: 'Infantry' }, + ]; + s.players[1].units = [{ id: 0, position: 'B2', unit_type: 'Infantry' }]; + return s; +} +for (const exhausted of [false, true]) { + for (const terrain of ['Forest', 'Mountain', 'Fertile']) { + for (const type of ['Infantry', 'Settler', { Leader: 'Caesar' }]) { + let s = fixture(terrain, exhausted, type); + assert(query(s, [1]).some((d) => d.position === 'C2')); + const entry = query(s).find((d) => d.position === 'B1'); + assert(entry); + s = move(s, entry.action); + assert(!query(s).some((d) => d.position === 'C2'), 'same-action unit identity'); + s = move(s, { Movement: 'Stop' }); + if (terrain === 'Mountain') + assert.throws(() => engine.tryMove(JSON.stringify(s), JSON.stringify(action('C2')), 0)); + else assert.doesNotThrow(() => engine.tryMove(JSON.stringify(s), JSON.stringify(action('C2')), 0)); + if (type !== 'Settler' && terrain !== 'Fertile') + assert.throws(() => engine.tryMove(JSON.stringify(s), JSON.stringify(action('B2')), 0)); + assert.equal( + query(s).some((d) => d.position === 'C2'), + terrain !== 'Mountain', + ); + if (type !== 'Settler') + assert.equal( + query(s).some((d) => d.position === 'B2'), + terrain === 'Fertile', + ); + if (terrain === 'Mountain') assert(entry.terrainNotes.some((n) => n.includes('cannot move again'))); + if (terrain === 'Forest') assert(entry.terrainNotes.length); + assert( + query(s, [1]).some((d) => d.position === 'C2'), + 'different units unaffected', + ); + assert.equal( + query(s, [0, 1]).some((d) => d.position === 'C2'), + terrain !== 'Mountain', + ); + if (terrain === 'Fertile') { + s.players[1].cities = [ + { position: 'B2', mood_state: 'Neutral', city_pieces: { wonders: ['GreatGardens'] } }, + ]; + assert(!query(s).some((d) => d.position === 'B2'), 'exhausted Plains still protects Great Gardens'); + } + s = move(s, { Playing: 'EndTurn' }); + assert.deepEqual(s.players[0].units[0].movement_restrictions ?? [], []); + } + if (terrain === 'Fertile') continue; + let s = fixture(terrain, exhausted); + s.players[0].cities = [{ position: 'A1', mood_state: 'Happy' }]; + s.players[0].advances.push('Roads'); + const offers = query(s).filter((d) => d.position === 'B1'); + assert(offers.some((d) => !Object.keys(d.payment).length)); + const paid = offers.find((d) => d.payment.food === 1 && d.payment.ore === 1); + assert(paid, 'paid adjacent Roads terrain bypass remains available'); + s = move(s, paid.action); + s = move(s, { Movement: 'Stop' }); + assert(query(s).some((d) => d.position === 'C2')); + assert(query(s).some((d) => d.position === 'B2')); + for (const restriction of ['Battle', 'Navigator']) { + const stopped = structuredClone(s); + stopped.players[0].units[0].movement_restrictions = [restriction]; + assert.equal(query(stopped).length, 0, 'Roads cannot bypass battle/navigation'); + } + } + for (const type of ['Settler', { Leader: 'Pakal' }, 'Infantry']) { + let s = fixture('Mountain', exhausted, type); + s.players[0].civilization = 'Maya'; + s.players[0].advances.push('Irrigation'); + s.players[0].special_advances = ['Terracing']; + s = move(s, action('B1')); + s = move(s, { Movement: 'Stop' }); + assert.equal( + query(s).some((d) => d.position === 'C2'), + type !== 'Infantry', + ); + } + let nomad = fixture('Mountain', exhausted); + nomad.players[0].civilization = 'Huns'; + nomad.players[0].advances.push('Storage'); + nomad.players[0].cities = [{ position: 'A1', mood_state: 'Happy' }]; + nomad.players[0].resources.ore = 0; + nomad = move(nomad, { Movement: { Move: { city: 'A1', units: [], destination: 'B1', payment: {} } } }); + assert.equal(nomad.players[0].cities[0].nomad_mountain, true); + assert.equal(nomad.players[0].resources.ore ?? 0, exhausted ? 0 : 1); + nomad = move(nomad, { Movement: 'Stop' }); + const cityRoutes = JSON.parse( + engine.webQuery(JSON.stringify(nomad), 0, JSON.stringify({ kind: 'movement', city: 'B1', units: [] })), + ); + assert.equal(cityRoutes.destinations.length, 0); + let s = fixture('Mountain', exhausted, { Leader: 'Hannibal' }); + s.players[0].civilization = 'Carthage'; + s.players[0].units.push({ id: 2, position: 'A1', unit_type: 'Elephant' }); + s = move(s, action('B1', [0, 2])); + s = move(s, { Movement: 'Stop' }); + assert(query(s, [0, 2]).some((d) => d.position === 'C2')); +} +console.log( + 'Exhausted/ordinary terrain: actual WASM moves, turn stops, identity, leaders/settlers, Great Gardens, Roads and exceptions passed', +); diff --git a/web-client/src/terrain-rules.test.ts b/web-client/src/terrain-rules.test.ts index e4a02c37..b7eda73c 100644 --- a/web-client/src/terrain-rules.test.ts +++ b/web-client/src/terrain-rules.test.ts @@ -33,11 +33,11 @@ for (const terrain of ['Forest', 'Mountain']) g.players[0].advances.push('Roads'); assert.match(quote(g).terrainNotes.join(' '), /Roads route/); }); -test('forest settlers and exhausted mountains do not promise restrictions that will not apply', () => { +test('forest settlers remain unrestricted; exhausted mountains retain their warning', () => { const g = fixture('Forest'); assert.match(quote(g, [5]).terrainNotes.join(' '), /No forest movement restriction/); const exhausted = fixture({ Exhausted: 'Mountain' }); - assert.deepEqual(quote(exhausted).terrainNotes, []); + assert.deepEqual(quote(exhausted).terrainNotes, quote(fixture('Mountain')).terrainNotes); }); test('Hannibal and Terracing show their mountain exceptions before moving', () => { const g = fixture('Mountain');