From fcc0a127d95429ba726f9c7be3faa4a292498420 Mon Sep 17 00:00:00 2001 From: MikeIsom <103458985+MikeIsom@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:50:04 -0700 Subject: [PATCH] Deal a standard-sized deck for "all three decks shuffled" (#166) Shuffled games put every plant of all three decks in play, leaving about 114 cards in the draw pile, so games rarely reached Step 3. New shuffled games instead draw 42 plants at random from all three decks, keeping the standard deck's 13 plug and 29 socket cards plus the Step 3 card; the usual player-count removals still apply. Duplicates and missing numbers are possible, unlike the one-per-number deck. New games store options.shuffledSample. Older shuffled games have none (reconstructionOptions forces it off), so they keep the full pile and replay exactly as before. --- engine/src/decks.spec.ts | 47 ++++++++++++++++++++++++++++++++++-- engine/src/engine.ts | 16 +++++++++--- engine/src/gamestate.ts | 4 +++ engine/src/maps.ts | 6 +++-- engine/src/maps/europe.ts | 11 +++++++-- engine/src/maps/ukireland.ts | 11 +++++++-- engine/src/powerPlants.ts | 29 +++++++++++++++++++--- 7 files changed, 109 insertions(+), 15 deletions(-) diff --git a/engine/src/decks.spec.ts b/engine/src/decks.spec.ts index 8b62481..7bb572d 100644 --- a/engine/src/decks.spec.ts +++ b/engine/src/decks.spec.ts @@ -1,14 +1,16 @@ import { expect } from 'chai'; import { range } from 'lodash'; import 'mocha'; +import seedrandom from 'seedrandom'; import { availableMoves } from './available-moves'; import { QUEBEC_RECHARGED_TOP_PLANTS, removedPlants, SPAIN_STEP_2_PLANTS } from './deckChanges'; -import { ended, move, moveAI, setup, updatePlayerCapacity } from './engine'; +import { ended, move, moveAI, reconstructionOptions, setup, updatePlayerCapacity } from './engine'; import { GameState, MapName, Phase, PowerPlantDeck, PowerPlantType, ResourceType, Variant } from './gamestate'; import { maps, mapsRecharged } from './maps'; import { Move, MoveName } from './move'; import { alternatePowerPlants, + dealtCatalogue, deck1PowerPlants, isPlugPlant, labelledPlant, @@ -413,11 +415,52 @@ describe('shuffled power plant decks', () => { }); it('opens the Original market with the eight smallest plants and 13 on top', () => { - const G = setup(4, { map: 'USA', variant: 'original', powerPlantDeck: 'shuffled' }, 'o'); + const G = setup(4, { map: 'USA', variant: 'original', powerPlantDeck: 'shuffled', shuffledSample: false }, 'o'); expect(numbers([...G.actualMarket, ...G.futureMarket])).to.deep.equal([1.1, 2.1, 3, 3.1, 3.2, 4, 4.1, 4.2]); expect(G.powerPlantsDeck[0].number).to.equal(13); }); + it('deals a standard-sized deck from all three decks, with the standard plug/socket split', () => { + const standardPlug = powerPlants.filter((pp) => pp.type != PowerPlantType.Step3 && isPlugPlant(pp, 'standard')); + const standardSize = powerPlants.length; + const dealtNumbers = new Set(); + for (const seed of range(20).map((i) => `sample-${i}`)) { + const dealt = dealtCatalogue('USA', 'shuffled', seedrandom(seed), true); + expect(dealt.length, seed).to.equal(standardSize); + expect( + dealt.filter((pp) => pp.type != PowerPlantType.Step3 && isPlugPlant(pp, 'shuffled')).length + ).to.equal(standardPlug.length); + expect(dealt[dealt.length - 1].number).to.equal(99); + expect(numbers(dealt)).to.deep.equal([...numbers(dealt)].sort((a, b) => a - b)); + dealtNumbers.add(numbers(dealt).join()); + } + expect(dealtNumbers.size, 'varies with the seed').to.equal(20); + }); + + it('leaves as many cards in play as the standard deck on every map, edition and player count', () => { + for (const map of ['USA', 'Germany', 'Italy', 'Benelux', 'Europe', 'UK & Ireland'] as MapName[]) { + for (const variant of ['original', 'recharged'] as Variant[]) { + for (const numPlayers of [2, 3, 4, 5, 6]) { + const label = `${map} ${variant} ${numPlayers}P`; + const standard = setup(numPlayers, { map, variant }, label); + const G = setup(numPlayers, { map, variant, powerPlantDeck: 'shuffled' }, label); + expect(G.options.shuffledSample, label).to.be.true; + expect(visible(G).length, label).to.equal(visible(standard).length); + } + } + } + }); + + it('keeps the full three-deck pile for shuffled games from before the sampled deck', () => { + const legacy = reconstructionOptions({ map: 'USA', variant: 'recharged', powerPlantDeck: 'shuffled' }); + expect(legacy.shuffledSample).to.be.false; + const G = setup(4, legacy, 'legacy'); + expect(G.options).to.not.have.property('shuffledSample'); + expect(visible(G).length).to.be.greaterThan(100); + const unique = setup(4, { map: 'USA', variant: 'recharged', powerPlantDeck: 'shuffledUnique' }, 'unique'); + expect(unique.options).to.not.have.property('shuffledSample'); + }); + it('keeps one random copy of each number in the one-per-number deck', () => { const dealt = (seed: string) => visible(setup(5, { map: 'USA', variant: 'recharged', powerPlantDeck: 'shuffledUnique' }, seed)); diff --git a/engine/src/engine.ts b/engine/src/engine.ts index 9524cf5..ad7a804 100644 --- a/engine/src/engine.ts +++ b/engine/src/engine.ts @@ -82,12 +82,13 @@ export function defaultSetupDeck( useNewRechargedSetup: boolean, deck: PowerPlantDeck = 'standard', mapName = '', - step3FromBottom = 0 + step3FromBottom = 0, + sampleShuffled = false ) { let actualMarket: PowerPlant[]; let futureMarket: PowerPlant[]; const removed = removedPlants(mapName, deck, numPlayers, variant); - const catalogue = dealtCatalogue(mapName, deck, rng).filter((p) => !removed.includes(p.number)); + const catalogue = dealtCatalogue(mapName, deck, rng, sampleShuffled).filter((p) => !removed.includes(p.number)); let powerPlantsDeck: PowerPlant[] = cloneDeep(catalogue); if (variant == 'original') { @@ -170,6 +171,7 @@ export function setup( powerPlantDeck, rechargedStartingResources = true, stallEnds = true, + shuffledSample = true, fusionEcological = FUSION_IS_ECOLOGICAL, legacyEcological = false, wienFreeGarbage = false, @@ -220,6 +222,7 @@ export function setup( : requestedDeck && decks.includes(requestedDeck) ? requestedDeck : 'standard'; + const sampleShuffled = chosenDeck == 'shuffled' && shuffledSample; const ecologicalRules = ECOLOGICAL_RULE_MAPS.includes(chosenMap.name) && !legacyEcological; let actualMarket: PowerPlant[]; @@ -237,7 +240,8 @@ export function setup( variant, rng, chosenDeck, - legacyEcological ? true : fusionEcological + legacyEcological ? true : fusionEcological, + sampleShuffled )); } else { ({ actualMarket, futureMarket, powerPlantsDeck } = defaultSetupDeck( @@ -246,7 +250,9 @@ export function setup( rng, useNewRechargedSetup, chosenDeck, - chosenMap.name + chosenMap.name, + 0, + sampleShuffled )); } } @@ -528,6 +534,7 @@ export function setup( : {}), ...(variantStartingResources && rechargedStartingResources ? { rechargedStartingResources: true } : {}), ...(stallEnds ? { stallEnds: true } : {}), + ...(sampleShuffled ? { shuffledSample: true } : {}), ...(ecologicalRules ? { fusionEcological } : {}), ...(wienFreeGarbage ? { wienFreeGarbage: true } : {}), }, @@ -2941,6 +2948,7 @@ export function reconstructionOptions(options: GameOptions, log: LogItem[] = []) ...options, rechargedStartingResources: options.rechargedStartingResources ?? false, stallEnds: options.stallEnds ?? false, + shuffledSample: options.shuffledSample ?? false, legacyEcological: options.fusionEcological === undefined, ...(wienFreeGarbage ? { wienFreeGarbage: true } : {}), }; diff --git a/engine/src/gamestate.ts b/engine/src/gamestate.ts index 46b6b88..67a10e9 100644 --- a/engine/src/gamestate.ts +++ b/engine/src/gamestate.ts @@ -74,6 +74,10 @@ export interface GameOptions { // finish ends (Step 3, no player could ever build again), and the 9999 "impossible" price is // never affordable. Older games have none, so they keep playing and replaying as they did. stallEnds?: boolean; + // Stored by setup on 'shuffled' games created since this existed (2026-11-07): the + // game deals a standard-sized deck at random from all three decks. Older shuffled + // games have none and play with every plant of all three decks. + shuffledSample?: boolean; // Whether fusion plants count as ecological (see FUSION_IS_ECOLOGICAL). Stored by // setup on the maps in ECOLOGICAL_RULE_MAPS; older games have none and play as // wind-only, except that their Québec deck setup protected fusion too. diff --git a/engine/src/maps.ts b/engine/src/maps.ts index 9aafd4d..e4a0931 100644 --- a/engine/src/maps.ts +++ b/engine/src/maps.ts @@ -120,13 +120,15 @@ export interface GameMap { uraniumPrices?: number[]; // `deck` is the power plant deck in play (see deckChanges.ts for the plant numbers // map rules name per deck). `fusionEcological` is the game's ruling, for maps - // that protect ecological plants. + // that protect ecological plants. `sampleShuffled` deals a 'shuffled' game a + // standard-sized deck (see dealtCatalogue). setupDeck?: ( numPlayers: number, variant: string, rng: seedrandom.prng, deck?: PowerPlantDeck, - fusionEcological?: boolean + fusionEcological?: boolean, + sampleShuffled?: boolean ) => { actualMarket: PowerPlant[]; futureMarket: PowerPlant[]; diff --git a/engine/src/maps/europe.ts b/engine/src/maps/europe.ts index 78a5404..e84d330 100644 --- a/engine/src/maps/europe.ts +++ b/engine/src/maps/europe.ts @@ -375,8 +375,15 @@ export const map: GameMap = { // The Step 2 trigger for Europe is handled in engine.ts (special branch): it // removes the lowest plant from the current market once, then re-sorts the // remaining 8 plants into 4 actual + 4 future without drawing from the deck. - setupDeck(numPlayers: number, variant: string, rng: seedrandom.prng, powerPlantDeck: PowerPlantDeck = 'standard') { - const powerPlantsDeck = cloneDeep(dealtCatalogue('Europe', powerPlantDeck, rng)); + setupDeck( + numPlayers: number, + variant: string, + rng: seedrandom.prng, + powerPlantDeck: PowerPlantDeck = 'standard', + _fusionEcological?: boolean, + sampleShuffled = false + ) { + const powerPlantsDeck = cloneDeep(dealtCatalogue('Europe', powerPlantDeck, rng, sampleShuffled)); const isPlug = (p: PowerPlant) => isPlugPlant(p, powerPlantDeck); const step3Index = powerPlantsDeck.findIndex((p) => p.type === PowerPlantType.Step3); diff --git a/engine/src/maps/ukireland.ts b/engine/src/maps/ukireland.ts index 4c39c8c..70d0f89 100644 --- a/engine/src/maps/ukireland.ts +++ b/engine/src/maps/ukireland.ts @@ -237,10 +237,17 @@ export const map: GameMap = { // Step 3 is placed 3rd from last in the deck (two plants below it) so Step // 3 starts earlier than on most maps. Otherwise standard recharged/original // deck prep. - setupDeck(numPlayers: number, variant: string, rng: seedrandom.prng, deck: PowerPlantDeck = 'standard') { + setupDeck( + numPlayers: number, + variant: string, + rng: seedrandom.prng, + deck: PowerPlantDeck = 'standard', + _fusionEcological?: boolean, + sampleShuffled = false + ) { // UK&I: Step 3 sits 3rd from last (two plants below it). The engine's standard // Step 3 trigger reshuffles plants below + discard into the new face-down deck. - return defaultSetupDeck(numPlayers, variant, rng, true, deck, 'UK & Ireland', 2); + return defaultSetupDeck(numPlayers, variant, rng, true, deck, 'UK & Ireland', 2, sampleShuffled); }, mapSpecificRules: 'Two networks: Ireland (Green and Orange regions) and Great Britain ' + diff --git a/engine/src/powerPlants.ts b/engine/src/powerPlants.ts index 58e463c..ecbf753 100644 --- a/engine/src/powerPlants.ts +++ b/engine/src/powerPlants.ts @@ -1,5 +1,6 @@ import { cloneDeep } from 'lodash'; import { GameOptions, PowerPlant, PowerPlantDeck, PowerPlantType, SingleDeck } from './gamestate'; +import { shuffle } from './utils'; const powerPlants: PowerPlant[] = [ { number: 3, type: PowerPlantType.Oil, cost: 2, citiesPowered: 1 }, @@ -253,10 +254,32 @@ function plantCatalogue(mapName: string, deck: PowerPlantDeck = 'standard'): Pow return deck == 'deck1' ? deck1PowerPlants : deck == 'deck2' ? alternatePowerPlants : powerPlants; } -// The plants a game is dealt from: the catalogue, or for 'shuffledUnique' one random -// copy of each number (the Step 3 card stays last). -function dealtCatalogue(mapName: string, deck: PowerPlantDeck, rng: () => number): PowerPlant[] { +// A sampled 'shuffled' game deals as many plug and socket cards as the standard deck has. +const STANDARD_PLUG_PLANTS = powerPlants.filter( + (pp) => pp.type != PowerPlantType.Step3 && isPlugPlant(pp, 'standard') +).length; +const STANDARD_SOCKET_PLANTS = + powerPlants.filter((pp) => pp.type != PowerPlantType.Step3).length - STANDARD_PLUG_PLANTS; + +// The plants a game is dealt from: the catalogue; for 'shuffledUnique' one random +// copy of each number; for a sampled 'shuffled' game a standard-sized random draw +// from all three decks. The Step 3 card stays last. +function dealtCatalogue(mapName: string, deck: PowerPlantDeck, rng: () => number, sample = false): PowerPlant[] { const catalogue = plantCatalogue(mapName, deck); + if (deck == 'shuffled' && sample) { + const plants = catalogue.filter((pp) => pp.type != PowerPlantType.Step3); + const plug = shuffle( + plants.filter((pp) => isPlugPlant(pp, deck)), + rng() + '' + ).slice(0, STANDARD_PLUG_PLANTS); + const socket = shuffle( + plants.filter((pp) => !isPlugPlant(pp, deck)), + rng() + '' + ).slice(0, STANDARD_SOCKET_PLANTS); + return [...plug, ...socket] + .sort((a, b) => a.number - b.number) + .concat(catalogue.filter((pp) => pp.type == PowerPlantType.Step3)); + } if (deck != 'shuffledUnique') { return catalogue; }