import assert from 'assert'; import { cloneDeep, isEqual, range } from 'lodash'; import seedrandom from 'seedrandom'; import { applyAnalysisMoney, isAnalysisMoneyCommand } from './analysis-money'; import { availableMoves, buildOptions, coalOilOverCapacity, computeRegionGraph, fuelOverCapacity, garbagePrice, hasGarbageOilHybrid, maxStorable, regionPickable, } from './available-moves'; import { canReviseChoice, finishPoweringLog, rememberPowering, replacePoweringLog, restorePowering, } from './choice-revisions'; import { originalTopPlants, removedPlants, spainStep2Plants, SPAIN_STEP_2_PLANTS } from './deckChanges'; import { countHeldPowerPlants, GameOptions, GameState, isUraniumMine, MapName, Phase, Player, playerColors, PowerPlant, PowerPlantDeck, PowerPlantType, ResourceType, resupplyUraniumMine, sellUraniumMine, SingleDeck, } from './gamestate'; import { LogItem } from './log'; import { City, GameMap, maps, mapsRecharged } from './maps'; import { Move, MoveName, Moves } from './move'; import { availableDecks, dealtCatalogue, ECOLOGICAL_RULE_MAPS, FUSION_IS_ECOLOGICAL, gameDeck, isEcological, isPlugPlant, phaseOutPlants, plantCatalogue, plantLabel, usesAlternateDeck, } from './powerPlants'; import prices from './prices'; import { createRandomizedMap } from './randomizeMap'; import { asserts, shuffle } from './utils'; // Re-exported from gamestate (single source of truth) so existing importers that // reference engine.playerColors (e.g. the wrapper's factions()) keep working. export { playerColors }; const citiesToStep2 = [10, 7, 7, 7, 6]; const citiesToStep2BadenWurttemberg = [9, 6, 6, 6, 5]; const citiesToStep2UKIreland = [10, 7, 7, 7, 6]; const citiesToEndGame = [21, 17, 17, 15, 14]; const citiesToEndGameSouthAfrica = [18, 17, 17, 15, 14]; const citiesToStep2Bremen = [5, 5, 5, 5, 4]; const citiesToEndGameBremen = [13, 13, 13, 12, 11]; const citiesToEndGameManhattan = [18, 17, 17, 15, 14]; const cityIncome = [10, 22, 33, 44, 54, 64, 73, 82, 90, 98, 105, 112, 118, 124, 129, 134, 138, 142, 145, 148, 150, 150]; const regionsInPlay = [3, 3, 4, 5, 5]; // The base game's deck setup, with each map's removed and opening plants for the // deck in play (see deckChanges.ts). Several maps call it with their own name. // `step3FromBottom` puts the Step 3 card that many plants above the bottom. export function defaultSetupDeck( numPlayers: number, variant: string, rng: seedrandom.prng, useNewRechargedSetup: boolean, deck: PowerPlantDeck = 'standard', mapName = '', step3FromBottom = 0, sampleShuffled = false ) { let actualMarket: PowerPlant[]; let futureMarket: PowerPlant[]; const removed = removedPlants(mapName, deck, numPlayers, variant); const dealt = dealtCatalogue(mapName, deck, rng, sampleShuffled); const catalogue = dealt.filter((p) => !removed.includes(p.number)); let powerPlantsDeck: PowerPlant[] = cloneDeep(catalogue); // Spain & Portugal with the shuffled decks: which held-back copies were dealt. const held = spainStep2Plants(deck); const heldPowerPlants = mapName == 'Spain & Portugal' && (deck == 'shuffled' || deck == 'shuffledUnique') ? cloneDeep(dealt.filter((p) => held.includes(p.number))) : undefined; if (variant == 'original') { // The eight smallest plants open the market; the map's opening plants (13 in // the standard deck, 11 in Deck 1) then go on top of the deck. powerPlantsDeck = powerPlantsDeck.slice(8); const topPlants: PowerPlant[] = []; for (const number of originalTopPlants(mapName, deck)) { const index = powerPlantsDeck.findIndex((pp) => pp.number == number); if (index >= 0) topPlants.push(powerPlantsDeck.splice(index, 1)[0]); } const step3 = powerPlantsDeck.pop()!; powerPlantsDeck = shuffle(powerPlantsDeck, rng() + ''); if (numPlayers == 2 || numPlayers == 3) { powerPlantsDeck = powerPlantsDeck.slice(8); } else if (numPlayers == 4) { powerPlantsDeck = powerPlantsDeck.slice(4); } powerPlantsDeck.unshift(...topPlants); powerPlantsDeck.splice(powerPlantsDeck.length - step3FromBottom, 0, step3); actualMarket = catalogue.slice(0, 4); futureMarket = catalogue.slice(4, 8); } else { // Plug cards come first in the single decks; the shuffled decks interleave them. let initialPowerPlants = shuffle( powerPlantsDeck.filter((p) => isPlugPlant(p, deck)), rng() + '' ); powerPlantsDeck = powerPlantsDeck.filter((p) => !isPlugPlant(p, deck)); let initialPlantMarket = initialPowerPlants.splice(0, 8); initialPlantMarket = initialPlantMarket.sort((a, b) => a.number - b.number); actualMarket = initialPlantMarket.slice(0, 4); futureMarket = initialPlantMarket.slice(4); const first = initialPowerPlants.shift()!; const step3 = powerPlantsDeck.pop()!; powerPlantsDeck = shuffle(powerPlantsDeck, rng() + ''); if (numPlayers == 2) { // Recharged 2P removes 1 plug + 5 socket (6 total). The original edition // removes 8 — see the variant == 'original' branch above. Confirmed with Mike. initialPowerPlants = initialPowerPlants.slice(1); powerPlantsDeck = shuffle(powerPlantsDeck.slice(5).concat(initialPowerPlants), rng() + ''); } else if (numPlayers == 3) { initialPowerPlants = initialPowerPlants.slice(2); powerPlantsDeck = shuffle(powerPlantsDeck.slice(6).concat(initialPowerPlants), rng() + ''); } else if (numPlayers == 4) { initialPowerPlants = initialPowerPlants.slice(1); powerPlantsDeck = shuffle(powerPlantsDeck.slice(3).concat(initialPowerPlants), rng() + ''); } else if (useNewRechargedSetup) { // TODO: This flag exists solely to make old tests pass. We should eventually // fix the test and remove the flag. powerPlantsDeck = shuffle(powerPlantsDeck.concat(initialPowerPlants), rng() + ''); } powerPlantsDeck.unshift(first); powerPlantsDeck.splice(powerPlantsDeck.length - step3FromBottom, 0, step3); } return { actualMarket, futureMarket, powerPlantsDeck, ...(heldPowerPlants ? { heldPowerPlants } : {}) }; } export function setup( numPlayers: number, { fastBid = false, map = 'USA', variant = 'original', showMoney = false, useNewRechargedSetup = true, endPlantDiscountAtStep3 = false, trackTotalSpent = true, randomizeMap = false, chooseRegions = false, chooseColors = false, alternatePowerPlants = false, powerPlantDeck, rechargedStartingResources = true, stallEnds = true, step2BeforeStep3 = true, shuffledSample = true, fusionEcological = FUSION_IS_ECOLOGICAL, legacyEcological = false, wienFreeGarbage = false, }: GameOptions, seed?: string, forceDeck?: PowerPlant[], forceMap?: GameMap, preferredPlayerColors?: (string | null)[] ): GameState { seed = seed ?? Math.random().toString(); const rng = seedrandom(seed); const variantMaps = variant == 'original' ? maps : mapsRecharged; if (map == 'Random') { // Dedicated rng so the pick doesn't shift the rest of the setup's draws. const mapRng = seedrandom(seed + '-map'); // Chile is in testing: left out of the random pick, which also keeps every other seed's pick unchanged. const pool = variantMaps.filter((m) => m.name != 'Chile'); map = pool[Math.floor(mapRng() * pool.length)].name as MapName; } const chosenMapRaw = variantMaps.find((m) => m.name == map); if (!chosenMapRaw) { throw new Error(`Map "${map}" not found for variant "${variant}"`); } const chosenMap = cloneDeep(chosenMapRaw); // Resolve the variant's starting market here, so the map stored in the game // state keeps a plain array, as it always has. const variantStartingResources = variant === 'recharged' && !!chosenMap.startingResources && !Array.isArray(chosenMap.startingResources); const startingResources = !chosenMap.startingResources ? undefined : Array.isArray(chosenMap.startingResources) ? chosenMap.startingResources : variantStartingResources && rechargedStartingResources ? chosenMap.startingResources.recharged : chosenMap.startingResources.original; if (startingResources) chosenMap.startingResources = startingResources; const decks = availableDecks(); // A random deck is one of the three single decks. Dedicated rng so the pick // doesn't shift the rest of the setup's draws. const singleDecks: PowerPlantDeck[] = ['standard', 'deck1', 'deck2']; const requestedDeck = powerPlantDeck == 'random' ? singleDecks[Math.floor(seedrandom(seed + '-deck')() * singleDecks.length)] : powerPlantDeck; // The legacy option only applies when no deck is asked for, and only on its own maps. const legacyAlternateDeck = !requestedDeck && usesAlternateDeck(chosenMap.name, alternatePowerPlants); const chosenDeck: PowerPlantDeck = legacyAlternateDeck ? 'deck2' : requestedDeck && decks.includes(requestedDeck) ? requestedDeck : 'standard'; const sampleShuffled = chosenDeck == 'shuffled' && shuffledSample; const ecologicalRules = ECOLOGICAL_RULE_MAPS.includes(chosenMap.name) && !legacyEcological; if (chosenMap.name == 'Chile' && randomizeMap) { throw new Error('Chile cannot be played on a randomized map: its gates and grids belong to its own board'); } let actualMarket: PowerPlant[]; let futureMarket: PowerPlant[]; let powerPlantsDeck: PowerPlant[]; let heldPowerPlants: PowerPlant[] | undefined; let setAsidePlants: PowerPlant[] | undefined; if (forceDeck) { powerPlantsDeck = forceDeck; actualMarket = powerPlantsDeck.splice(0, 4); futureMarket = powerPlantsDeck.splice(0, 4); } else { if (chosenMap.setupDeck) { ({ actualMarket, futureMarket, powerPlantsDeck, heldPowerPlants, setAsidePlants } = chosenMap.setupDeck( numPlayers, variant, rng, chosenDeck, legacyEcological ? true : fusionEcological, sampleShuffled )); } else { ({ actualMarket, futureMarket, powerPlantsDeck } = defaultSetupDeck( numPlayers, variant, rng, useNewRechargedSetup, chosenDeck, chosenMap.name, 0, sampleShuffled )); } } const players: Player[] = range(numPlayers).map((id) => ({ id, powerPlants: [], coalCapacity: 0, coalLeft: 0, oilCapacity: 0, oilLeft: 0, garbageCapacity: 0, garbageLeft: 0, uraniumCapacity: 0, uraniumLeft: 0, hybridCapacity: 0, hybridLeft: 0, ...(chosenDeck == 'deck2' ? { garbageOilHybridCapacity: 0 } : {}), money: 50, housesLeft: 22, cities: [], powerPlantsNotUsed: [], availableMoves: null, lastMove: null, isDropped: false, isAI: false, bid: 0, passed: false, skipAuction: false, citiesPowered: 0, resourcesUsed: [], totalIncome: 0, totalSpentCities: 0, totalSpentConnections: 0, totalSpentPlants: 0, totalSpentResources: 0, usedFreeJump: false, totalTimeUsed: 0, })); const p = players.length - 2; const playerOrder = range(numPlayers); const startingPlayer = playerOrder[0]; let coalResupply: number[][]; let oilResupply: number[][]; let garbageResupply: number[][]; let uraniumResupply: number[][]; // Korea: parallel North-side resupply tables (no uranium row). let coalResupplyNorth: number[][] | undefined; let oilResupplyNorth: number[][] | undefined; let garbageResupplyNorth: number[][] | undefined; if (chosenMap.resupplyNorth) { coalResupplyNorth = chosenMap.resupplyNorth[0]; oilResupplyNorth = chosenMap.resupplyNorth[1]; garbageResupplyNorth = chosenMap.resupplyNorth[2]; } if (chosenMap.resupply) { coalResupply = chosenMap.resupply[0]; oilResupply = chosenMap.resupply[1]; garbageResupply = chosenMap.resupply[2]; uraniumResupply = chosenMap.resupply[3]; } else { coalResupply = [ [3, 4, 3], [4, 5, 3], [5, 6, 4], [5, 7, 5], [7, 9, 6], ]; oilResupply = [ [2, 2, 4], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], ]; garbageResupply = [ [1, 2, 3], [1, 2, 3], [2, 3, 4], [3, 3, 5], [3, 5, 6], ]; uraniumResupply = [ [1, 1, 1], [1, 1, 1], [1, 2, 2], [2, 3, 2], [2, 3, 3], ]; } if (chosenMap.layout == 'Portrait' || randomizeMap) { const isUsaRecharged = chosenMap.name === 'USA' && variant === 'recharged'; chosenMap.viewBox = chosenMap.viewBox || [1480, 1060]; chosenMap.adjustRatio = chosenMap.adjustRatio || [1, 1]; chosenMap.playerOrderPosition = chosenMap.playerOrderPosition || [1160, 160]; chosenMap.cityCountPosition = chosenMap.cityCountPosition || [0, 0]; chosenMap.powerPlantMarketPosition = chosenMap.powerPlantMarketPosition || [745, 0]; chosenMap.actualMarketWidth = chosenMap.actualMarketWidth || 430; chosenMap.mapPosition = chosenMap.mapPosition || [0, 0]; chosenMap.buttonsPosition = chosenMap.buttonsPosition || [1305, 0]; chosenMap.playerBoardsPosition = chosenMap.playerBoardsPosition || [1105, 240]; chosenMap.roundInfoPosition = chosenMap.roundInfoPosition || [20, 920]; chosenMap.supplyPosition = chosenMap.supplyPosition || [isUsaRecharged ? 480 : 675, 920]; } else { chosenMap.viewBox = chosenMap.viewBox || [1465, 860]; chosenMap.adjustRatio = chosenMap.adjustRatio || [1, 1]; chosenMap.playerOrderPosition = chosenMap.playerOrderPosition || [1160, 140]; chosenMap.cityCountPosition = chosenMap.cityCountPosition || [0, 0]; chosenMap.powerPlantMarketPosition = chosenMap.powerPlantMarketPosition || [745, 0]; chosenMap.actualMarketWidth = chosenMap.actualMarketWidth || 430; chosenMap.mapPosition = chosenMap.mapPosition || [-10, 0]; chosenMap.buttonsPosition = chosenMap.buttonsPosition || [1305, 0]; chosenMap.playerBoardsPosition = chosenMap.playerBoardsPosition || [1105, 200]; chosenMap.roundInfoPosition = chosenMap.roundInfoPosition || [20, 590]; chosenMap.supplyPosition = chosenMap.supplyPosition || [0, 720]; } let finalMap: GameMap; // Set when the chooseRegions option defers region selection to a player draft; // consumed after the GameState is built to enter the RegionSelection phase. let pendingRegionDraft: { regionsNeeded: number; picked: string[] } | undefined; if (randomizeMap) { finalMap = createRandomizedMap(chosenMap, regionsInPlay[p], rng); } else { chosenMap.cities = chosenMap.cities.map((city) => ({ ...city, x: city.x * chosenMap.adjustRatio![0], y: city.y * chosenMap.adjustRatio![1], })); // Region adjacency graph (shared with the chooseRegions draft via // available-moves) — single source of truth for what "connected" means. const graph = computeRegionGraph(chosenMap); const regions = graph.regions; const regionsNeeded = Math.min(regionsInPlay[p], regions.length); // Regions always in play (Chile's two northern regions); the rest are picked or drafted as usual. const fixedRegions = chosenMap.fixedRegions ?? []; if (chooseRegions && regions.length > regionsNeeded) { // Defer region selection to a player draft (the RegionSelection phase). // Keep the full map in play for now; finalizeRegions() filters it to the // chosen regions once the draft completes, and applies the // regionalPowerPlants swap at that point. finalMap = chosenMap; pendingRegionDraft = { regionsNeeded, picked: [...fixedRegions] }; } else { const playRegions = new Set(fixedRegions); while (playRegions.size != regionsNeeded) { const region = regions[Math.floor(rng() * regions.length)]; // regionPickable enforces connectivity with the UK & Ireland // (fresh-landmass) and Australia (any-region) exceptions. A region // that is already picked or that would be stranded is rejected, and // the loop draws again — see regionPickable for the soft-lock // reasoning behind the connectivity requirement. if (regionPickable(chosenMap.name, graph, [...playRegions], region)) { playRegions.add(region); // Avoid italy Red Green Blue if (chosenMap.name === 'Italy') { if (playRegions.has('red') && playRegions.has('green') && playRegions.has('blue')) { playRegions.clear(); } } } } finalMap = filterMapToRegions(chosenMap, playRegions, numPlayers); applyRegionalPowerPlants( chosenMap, playRegions, actualMarket, futureMarket, powerPlantsDeck, chosenDeck, seed ); } } const coalMarket = startingResources ? startingResources[0] : 24; const oilMarket = startingResources ? startingResources[1] : 18; const garbageMarket = startingResources ? startingResources[2] : variant == 'original' ? 6 : 9; const uraniumMarket = startingResources ? startingResources[3] : 2; const totalCoal = chosenMap.startingSupply ? chosenMap.startingSupply[0] : 24; const totalOil = chosenMap.startingSupply ? chosenMap.startingSupply[1] : 24; const totalGarbage = chosenMap.startingSupply ? chosenMap.startingSupply[2] : 24; const totalUranium = chosenMap.startingSupply ? chosenMap.startingSupply[3] : 12; // Korea: parallel North-side market and prices. Supply is shared — see below. const coalMarketNorth = chosenMap.startingResourcesNorth?.[0]; const oilMarketNorth = chosenMap.startingResourcesNorth?.[1]; const garbageMarketNorth = chosenMap.startingResourcesNorth?.[2]; // Supply pools are shared between both sides for Korea. `startingSupply` // represents the TOTAL cubes in the game; the supply pool is whatever is // left after both markets are filled. const coalSupply = totalCoal - coalMarket - (coalMarketNorth ?? 0); const oilSupply = totalOil - oilMarket - (oilMarketNorth ?? 0); const garbageSupply = totalGarbage - garbageMarket - (garbageMarketNorth ?? 0); const uraniumSupply = totalUranium - uraniumMarket; const coalPrices = cloneDeep(chosenMap.coalPrices ?? prices.coal); const oilPrices = cloneDeep(chosenMap.oilPrices ?? prices.oil); const garbagePrices = cloneDeep(chosenMap.garbagePrices ?? prices.garbage); const uraniumPrices = cloneDeep(chosenMap.uraniumPrices ?? prices.uranium); const coalPricesNorth = chosenMap.coalPricesNorth ? cloneDeep(chosenMap.coalPricesNorth) : undefined; const oilPricesNorth = chosenMap.oilPricesNorth ? cloneDeep(chosenMap.oilPricesNorth) : undefined; const garbagePricesNorth = chosenMap.garbagePricesNorth ? cloneDeep(chosenMap.garbagePricesNorth) : undefined; const isSouthAfrica = (forceMap || finalMap).name == 'South Africa'; const G: GameState = { map: forceMap || finalMap, players, playerOrder, currentPlayers: [startingPlayer], powerPlantsDeck, coalSupply, oilSupply, garbageSupply, uraniumSupply, // South Africa: separate coal pool below the market. Starts empty; // used coal returns here; refill draws from here first. coalStorage: isSouthAfrica ? 0 : undefined, coalResupply, oilResupply, garbageResupply, uraniumResupply, coalMarket, oilMarket, garbageMarket, uraniumMarket, // Australia: the separate uranium-mine market starts full (6 prices $2–$7, // two tokens each). Undefined on every other map. uraniumMineMarket: (forceMap || finalMap).name == 'Australia' ? [2, 2, 2, 2, 2, 2] : undefined, ...(heldPowerPlants ? { heldPowerPlants } : {}), coalPrices, oilPrices, garbagePrices, uraniumPrices, // Korea: parallel North-side fields. Undefined for non-Korea maps. // Supply is shared with the primary `*Supply` fields above. coalMarketNorth, oilMarketNorth, garbageMarketNorth, coalResupplyNorth, oilResupplyNorth, garbageResupplyNorth, coalPricesNorth, oilPricesNorth, garbagePricesNorth, actualMarket, futureMarket, // Chile: the plants its setupDeck set aside (copies, never the catalogue's shared objects). ...(setAsidePlants ? { setAsidePlants } : {}), chosenPowerPlant: undefined, currentBid: undefined, highestBidders: [], auctioningPlayer: undefined, step: 1, phase: Phase.Auction, options: { fastBid, map, variant, showMoney, useNewRechargedSetup, ...(variant === 'recharged' && endPlantDiscountAtStep3 ? { endPlantDiscountAtStep3: true } : {}), trackTotalSpent, chooseRegions, chooseColors, // Only stored when in effect, so states of every other game are unchanged. ...(legacyAlternateDeck ? { alternatePowerPlants: true } : chosenDeck != 'standard' ? { powerPlantDeck: chosenDeck } : {}), ...(variantStartingResources && rechargedStartingResources ? { rechargedStartingResources: true } : {}), ...(stallEnds ? { stallEnds: true } : {}), ...(step2BeforeStep3 ? { step2BeforeStep3: true } : {}), ...(sampleShuffled ? { shuffledSample: true } : {}), ...(ecologicalRules ? { fusionEcological } : {}), ...(wienFreeGarbage ? { wienFreeGarbage: true } : {}), }, log: [], hiddenLog: [], seed, round: 1, newTurn: true, auctionSkips: 0, citiesToStep2: (forceMap || finalMap).name == 'Baden-Württemberg' ? citiesToStep2BadenWurttemberg[numPlayers - 2] : (forceMap || finalMap).name == 'UK & Ireland' ? citiesToStep2UKIreland[numPlayers - 2] : (forceMap || finalMap).name == 'Bremen' ? citiesToStep2Bremen[numPlayers - 2] : citiesToStep2[numPlayers - 2], citiesToEndGame: (forceMap || finalMap).name == 'South Africa' ? citiesToEndGameSouthAfrica[numPlayers - 2] : (forceMap || finalMap).name == 'UK & Ireland' && numPlayers == 2 ? Math.min(citiesToEndGame[numPlayers - 2], (forceMap || finalMap).cities.length) : (forceMap || finalMap).name == 'Bremen' ? citiesToEndGameBremen[numPlayers - 2] : (forceMap || finalMap).name == 'Manhattan' ? citiesToEndGameManhattan[numPlayers - 2] : citiesToEndGame[numPlayers - 2], resourceResupply: [ `[${coalResupply[p][0]}, ${oilResupply[p][0]}, ${garbageResupply[p][0]}, ${uraniumResupply[p][0]}]`, `[${coalResupply[p][1]}, ${oilResupply[p][1]}, ${garbageResupply[p][1]}, ${uraniumResupply[p][1]}]`, `[${coalResupply[p][2]}, ${oilResupply[p][2]}, ${garbageResupply[p][2]}, ${uraniumResupply[p][2]}]`, ], resourceResupplyNorth: coalResupplyNorth && oilResupplyNorth && garbageResupplyNorth ? [ `[${coalResupplyNorth[p][0]}, ${oilResupplyNorth[p][0]}, ${garbageResupplyNorth[p][0]}]`, `[${coalResupplyNorth[p][1]}, ${oilResupplyNorth[p][1]}, ${garbageResupplyNorth[p][1]}]`, `[${coalResupplyNorth[p][2]}, ${oilResupplyNorth[p][2]}, ${garbageResupplyNorth[p][2]}]`, ] : undefined, paymentTable: cityIncome, variant, minimunBid: 0, plantDiscountActive: variant == 'recharged' && (forceMap || finalMap).name != 'China' && (forceMap || finalMap).name != 'Russia' && (forceMap || finalMap).name != 'Baden-Württemberg', discardSmallestPlant: false, cardsLeft: powerPlantsDeck.length, nextCardWeak: variant == 'recharged', card39Bought: false, knownPowerPlantDeck: actualMarket.concat(futureMarket), knownPowerPlantDeckStep3: [], powerPlantDeckAfterStep3: undefined, } as GameState; G.log.push({ type: 'event', event: 'Game Start!' }); if (G.map.name == 'Middle East') { removePlantsForMiddleEastStep1(G); G.cardsLeft = G.powerPlantsDeck.length; G.nextCardWeak = nextCardIsPlug(G); } // chooseRegions: start in the region draft instead of the auction. The map // currently holds every region; finalizeRegions() filters it once the players // finish picking. if (pendingRegionDraft) { G.phase = Phase.RegionSelection; G.regionDraft = pendingRegionDraft; } // chooseColors: players draft their colors first of all. The region draft (if // any) stays pending in G.regionDraft and only activates once colors are done // (see advanceColorDraft). The phase set here wins over RegionSelection above. if (chooseColors) { if (preferredPlayerColors?.some((color) => typeof color === 'string' && /^#[a-f0-9]{6}$/i.test(color))) { G.setupPlayerColors = G.players.map((_, i) => { const color = preferredPlayerColors[i]; return typeof color === 'string' && /^#[a-f0-9]{6}$/i.test(color) ? color.toLowerCase() : null; }); } G.phase = Phase.ColorSelection; G.colorDraft = { picked: [] }; advanceColorDraft(G); } if (G.map.blockSpaces) { G.blockedCities = G.map.blockSpaces(numPlayers, G.map.cities, rng); if (G.blockedCities.length > 0) { G.log.push({ type: 'event', event: `${G.blockedCities.length} building spaces are blocked for ${numPlayers} players.`, }); } } for (const index of G.currentPlayers) G.players[index].availableMoves = availableMoves(G, G.players[index]); return G; } // Filters a (full) map down to just the chosen regions: keeps only cities in // those regions and connections whose endpoints are both kept. Returns a new map; // the input is not mutated. A city with minPlayers is left out below that player count. function filterMapToRegions(map: GameMap, playRegions: Set, numPlayers: number): GameMap { const filteredMap = cloneDeep(map); const counts = (city: City) => !city.minPlayers || numPlayers >= city.minPlayers; const kept = new Set( map.cities.filter((city) => playRegions.has(city.region) && counts(city)).map((city) => city.name) ); // A city in no region (Chile's Salta) is in play when a connection joins it to a city that is. for (const city of map.cities.filter((c) => !c.region && counts(c))) { if (map.connections.some((con) => con.nodes.includes(city.name) && con.nodes.some((n) => kept.has(n)))) { kept.add(city.name); } } filteredMap.cities = filteredMap.cities.filter((city) => kept.has(city.name)); filteredMap.connections = filteredMap.connections.filter((con) => con.nodes.every((n) => kept.has(n))); if (filteredMap.step3Connections) { filteredMap.step3Connections = filteredMap.step3Connections.filter((con) => con.nodes.every((n) => kept.has(n)) ); } return filteredMap; } // Applies the map's regionalPowerPlants overrides for the chosen regions, swapping // the replacement plants into whichever of the three decks currently hold the // matching plant numbers. Mutates the passed arrays in place. // The map's numbers win over the deck's: a regional card the deck has no plant for // (Deck 1 has no 18) is shuffled into the draw pile, below its top card. // The shuffled decks: the regional card takes the place of one dealt copy of its // number and the other copies leave the game; a regional card whose number wasn't // dealt is shuffled in the same way. function applyRegionalPowerPlants( map: GameMap, playRegions: Set, actualMarket: PowerPlant[], futureMarket: PowerPlant[], powerPlantsDeck: PowerPlant[], deck: PowerPlantDeck, seed: string ) { if (!map.regionalPowerPlants) { return; } const catalogue = plantCatalogue(map.name, deck); const rng = seedrandom(seed + '-regional'); const shuffledDeck = deck == 'shuffled' || deck == 'shuffledUnique'; const actualSize = actualMarket.length; const marketSize = actualSize + futureMarket.length; for (const region of playRegions) { const replacements = map.regionalPowerPlants[region]; if (replacements) { for (const newPlant of replacements) { if (shuffledDeck) { const sameNumber = (p: PowerPlant) => Math.floor(p.number) === newPlant.number; const holders = [actualMarket, futureMarket, powerPlantsDeck]; const keeper = holders.find((arr) => arr.some(sameNumber)); if (keeper) { const regional = { ...newPlant }; keeper[keeper.findIndex(sameNumber)] = regional; for (const arr of holders) { for (let i = arr.length - 1; i >= 0; i--) { if (sameNumber(arr[i]) && arr[i] !== regional) arr.splice(i, 1); } } } else { const index = 1 + Math.floor(rng() * (powerPlantsDeck.length - 1)); powerPlantsDeck.splice(index, 0, { ...newPlant }); } continue; } const swapIn = (arr: PowerPlant[]) => { const idx = arr.findIndex((p) => p.number === newPlant.number); if (idx !== -1) arr[idx] = { ...newPlant }; }; swapIn(actualMarket); swapIn(futureMarket); swapIn(powerPlantsDeck); if (!catalogue.some((p) => p.number === newPlant.number)) { // Between the top card and the Step 3 card at the bottom. const index = 1 + Math.floor(rng() * (powerPlantsDeck.length - 1)); powerPlantsDeck.splice(index, 0, { ...newPlant }); } } } } if (shuffledDeck) { // A removed copy may have left the market: refill it from the top of the deck. const market = [...actualMarket, ...futureMarket]; while (market.length < marketSize && powerPlantsDeck.length > 1) market.push(powerPlantsDeck.shift()!); market.sort((a, b) => a.number - b.number); actualMarket.splice(0, actualMarket.length, ...market.slice(0, actualSize)); futureMarket.splice(0, futureMarket.length, ...market.slice(actualSize)); } } // Completes the chooseRegions draft: filters the map to the picked regions, applies // the deferred regionalPowerPlants swap, fixes region-dependent end-game targets, // and hands off to the auction with the starting player to move. function finalizeRegions(G: GameState) { const playRegions = new Set(G.regionDraft!.picked); const fullMap = G.map; G.map = filterMapToRegions(fullMap, playRegions, G.players.length); applyRegionalPowerPlants( fullMap, playRegions, G.actualMarket, G.futureMarket, G.powerPlantsDeck, gameDeck(G.options), G.seed ); // knownPowerPlantDeck mirrors actualMarket+futureMarket and must reflect any swap. G.knownPowerPlantDeck = G.actualMarket.concat(G.futureMarket); // Regional cards can add plants to the deck, or (shuffled decks) take copies out. G.cardsLeft = G.powerPlantsDeck.length; G.nextCardWeak = G.options.variant === 'recharged' && G.cardsLeft > 0 && G.powerPlantsDeck[0].number <= 15; // UK & Ireland 2p caps the end-game target at the number of cities actually in // play, which is only known once the regions are chosen. if (G.map.name == 'UK & Ireland' && G.players.length == 2) { G.citiesToEndGame = Math.min(citiesToEndGame[G.players.length - 2], G.map.cities.length); } G.regionDraft = undefined; G.phase = Phase.Auction; G.currentPlayers = [G.playerOrder[0]]; } // Manual picks precede fallback assignments for players using a custom BGS colour. function advanceColorDraft(G: GameState) { const next = G.playerOrder.find((index) => !G.players[index].color && !G.setupPlayerColors?.[index]); if (next !== undefined) { G.currentPlayers = [next]; return; } // Custom-colour players take only leftovers, keeping every colour available to manual pickers. const unused = playerColors.filter((color) => !G.colorDraft!.picked.includes(color)); for (const index of G.playerOrder) { if (G.players[index].color) continue; const color = unused.includes(playerColors[index]) ? playerColors[index] : unused[0]; G.players[index].color = color; unused.splice(unused.indexOf(color), 1); } G.colorDraft = undefined; G.currentPlayers = [G.playerOrder[0]]; if (G.regionDraft) { G.phase = Phase.RegionSelection; } else { G.phase = Phase.Auction; } } // The draw pile shows whether its top card has a plug on the back (Recharged only). // Which plants are plug cards depends on the deck: Deck 1 has plugs up to 16, and the // shuffled decks mix copies from every deck (16.1, 15.2, ...), so ask the deck rather // than comparing the number with 15. function nextCardIsPlug(G: GameState): boolean { return ( G.options.variant == 'recharged' && G.powerPlantsDeck.length > 0 && isPlugPlant(G.powerPlantsDeck[0], gameDeck(G.options)) ); } export function stripSecret(G: GameState, player?: number): GameState { return { ...G, seed: 'secret', poweringChoices: player !== undefined && G.poweringChoices?.[player] ? { [player]: G.poweringChoices[player] } : {}, pendingMessages: undefined, automation: { version: 1, plans: player != undefined && G.automation?.plans[player] ? { [player]: G.automation.plans[player] } : {}, increments: G.automation?.increments || G.players.map(() => 0), liveUpdate: G.automation?.liveUpdate || false, }, hiddenLog: [], powerPlantsDeck: [], players: G.players.map((pl, i) => { if (player === i) { return pl; } else { return { ...pl, settings: undefined, availableMoves: pl.availableMoves ? {} : null, money: ended(G) || G.options.showMoney || G.analysisMoney === 'hypothetical' ? pl.money : -1, bid: G.options.fastBid ? 0 : pl.bid, // A sealed response must not leak via its duplicate in lastMove. // Hide every opponent's last move throughout the pending auction, // including passes and revisions, rather than revealing its kind. lastMove: G.options.fastBid && G.phase === Phase.Auction && G.chosenPowerPlant ? null : pl.lastMove, }; } }), log: G.log, }; } export function currentPlayers(G: GameState): number[] { return G.currentPlayers; } export function move( G: GameState, move: Move, playerNumber: number, { updateBuildingMarket = true }: { updateBuildingMarket?: boolean } = {} ): GameState { if (isAnalysisMoneyCommand(move)) throw new Error('SetAnalysisMoney is only available in an analysis'); const player = G.players[playerNumber]; const initialPhase = G.phase; const initialRound = G.round; const revision = move.revision !== undefined || move.name === MoveName.ReopenPowering; if (revision) assert(canReviseChoice(G, move, playerNumber), 'This choice can no longer be changed.'); if (move.name === MoveName.ReopenPowering) { replacePoweringLog(G, playerNumber); restorePowering(G, playerNumber); player.lastMove = move; updateClocks(G, move.serverTime ?? move.time); G.currentPlayers.forEach((p) => (G.players[p].availableMoves = availableMoves(G, G.players[p]))); G.newTurn = true; return G; } const available = revision ? availableMoves(G, player)[move.name] : player.availableMoves?.[move.name]; updateGameState(G); assert(revision || G.currentPlayers.includes(playerNumber), 'It is not your turn!'); assert(available, 'You are not allowed to run the command ' + move.name); assert( available.some((x) => isEqual(x, move.data)), 'Wrong argument for the command ' + move.name ); rememberPowering(G, playerNumber); const { revision: ignoredRevision, ...recordedMove } = move; const replaceHiddenBid = () => { if (!revision) return; const index = G.hiddenLog.findIndex((entry) => entry.type === 'move' && entry.player === playerNumber); if (index >= 0 && index < G.hiddenLog.length - 1) { const previous = G.hiddenLog[index]; const replacement = G.hiddenLog.pop()!; if (previous.type === 'move' && replacement.type === 'move') { replacement.move.time = previous.move.time; replacement.move.serverTime = previous.move.serverTime; } G.hiddenLog[index] = replacement; } }; switch (move.name) { case MoveName.ChoosePowerPlant: { asserts(move); // Pull from actualMarket so regionalPowerPlants overrides survive // selection — canonical getPowerPlant() lookup would lose them. G.chosenPowerPlant = G.actualMarket.find((p) => p.number === move.data)!; G.auctioningPlayer = player.id; // Named card39Bought from the standard deck's plant; old games, which have // no powerPlantDeck, keep keying it on 39. if (phaseOutPlants((G.options.powerPlantDeck as PowerPlantDeck) ?? 'standard').includes(move.data)) { G.card39Bought = true; } if ( G.options.variant == 'recharged' && G.plantDiscountActive && G.chosenPowerPlant.number == G.actualMarket[0].number ) { G.minimunBid = 1; G.plantDiscountActive = false; move.usedPlantDiscount = true; } else { G.minimunBid = Math.floor(move.data); } const notPassed = G.players.filter((p) => !p.skipAuction && !p.isDropped); if (notPassed.length == 1) { G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} chooses Power Plant ${plantLabel(move.data)}.`, pretty: `${playerNameHTML(player)} chooses Power Plant ${plantLabel(move.data)}.`, }); const winningPlayer = notPassed[0]; endAuction(G, winningPlayer, G.minimunBid); if (G.discountBonusPlayer != undefined) { // Manhattan: the buyer of the discounted plant gets another // purchase. They are still the only eligible bidder, so re-prompt // them (they may also Pass to decline). Refill the market for the // plant they just took — UNLESS that purchase put them over the // plant limit: then they must discard first and the discard handler // does the refill, so refilling here too would draw a second plant // for one purchase and grow the market past 8. const overPlantLimit = countHeldPowerPlants(G, winningPlayer) > 4 || (G.players.length > 2 && countHeldPowerPlants(G, winningPlayer) > 3); if (!overPlantLimit) { refillMarketAfterPurchase(G); } setCurrentPlayer(G, G.discountBonusPlayer); } else if ( countHeldPowerPlants(G, winningPlayer) <= 3 || (G.players.length == 2 && countHeldPowerPlants(G, winningPlayer) == 4) ) { refillMarketAfterPurchase(G); toResourcesPhase(G); } } else { G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} chooses Power Plant ${plantLabel(move.data)} to initiate an auction.`, pretty: `${playerNameHTML(player)} chooses Power Plant ${plantLabel( move.data )} to initiate an auction.`, }); if (G.options.fastBid) { G.currentPlayers = G.playerOrder.filter( (p) => !G.players[p].skipAuction && !G.players[p].isDropped ); } } break; } case MoveName.Bid: { asserts(move); if (G.options.fastBid) { G.hiddenLog.push({ type: 'move', player: playerNumber, move: recordedMove, simple: `${player.name} bids $${move.data}.`, pretty: `${playerNameHTML(player)} bids $${move.data}.`, }); replaceHiddenBid(); fastAuction(G, player, move.data); } else { G.currentBid = player.bid = move.data; G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} bids $${move.data}.`, pretty: `${playerNameHTML(player)} bids $${move.data}.`, }); nextPlayerClockwise(G); } break; } case MoveName.ChooseRegion: { asserts(move); G.regionDraft!.picked.push(move.data); G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} chooses region ${move.data}.`, pretty: `${playerNameHTML(player)} chooses region ${move.data}.`, }); if (G.regionDraft!.picked.length >= G.regionDraft!.regionsNeeded) { finalizeRegions(G); } else { // Next picker, in seating order, wrapping around. const order = G.playerOrder; G.currentPlayers = [order[(order.indexOf(playerNumber) + 1) % order.length]]; } break; } case MoveName.ChooseColor: { asserts(move); player.color = move.data; G.colorDraft!.picked.push(move.data); G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} chooses ${move.data}.`, pretty: `${playerNameHTML(player)} chooses ${move.data}.`, }); advanceColorDraft(G); break; } case MoveName.Pass: { asserts(move); if (!G.options.fastBid || G.phase != Phase.Auction || !G.chosenPowerPlant) { G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} passes.`, pretty: `${playerNameHTML(player)} passes.`, }); } switch (G.phase) { case Phase.Auction: { if (G.chosenPowerPlant == undefined) { player.skipAuction = true; // Manhattan: passing here declines the discount bonus purchase. if (G.discountBonusPlayer == player.id) { G.discountBonusPlayer = undefined; } G.auctionSkips++; if (G.auctionSkips == 1 && G.map.name == 'Russia' && G.actualMarket?.length > 0) { G.log.push({ type: 'event', event: `First pass, removing lowest numbered Power Plant (${G.actualMarket[0].number}).`, }); G.actualMarket.shift(); addPowerPlant(G); } if (G.players.some((p) => !p.skipAuction && !p.isDropped)) { nextPlayerAuction(G); } else { if (G.auctionSkips == G.players.length && G.map.name == 'Baden-Württemberg') { // Baden-Württemberg: remove the two lowest plants when no one buys. // This applies regardless of variant. const removed: number[] = []; for (let i = 0; i < 2 && G.actualMarket.length > 0; i++) { removed.push(G.actualMarket[0].number); G.actualMarket.shift(); addPowerPlant(G); } G.log.push({ type: 'event', event: `Everyone passed, removing the two lowest numbered Power Plants (${removed.join( ', ' )}).`, }); } else if ( G.auctionSkips == G.players.length && G.options.variant == 'original' && G.map.name != 'China' && G.map.name != 'Russia' ) { G.log.push({ type: 'event', event: `Everyone passed, removing lowest numbered Power Plant (${G.actualMarket[0].number}).`, }); G.actualMarket.shift(); addPowerPlant(G); } toResourcesPhase(G); } } else { if (G.options.fastBid) { G.hiddenLog.push({ type: 'move', player: playerNumber, move: recordedMove, simple: `${player.name} passes.`, pretty: `${playerNameHTML(player)} passes.`, }); replaceHiddenBid(); fastAuction(G, player, 0); } else { player.passed = true; const notPassed = G.players.filter((p) => !p.passed && !p.skipAuction && !p.isDropped); if (notPassed.length == 1) { const winningPlayer = notPassed[0]; endAuction(G, winningPlayer, winningPlayer.bid); if ( (countHeldPowerPlants(G, winningPlayer) > 4 || (G.players.length > 2 && countHeldPowerPlants(G, winningPlayer) > 3)) && !winningPlayer.isDropped ) { setCurrentPlayer(G, winningPlayer.id); } else { refillMarketAfterPurchase(G); if (G.players.some((p) => !p.skipAuction && !p.isDropped)) { G.players.forEach((p) => { p.bid = 0; p.passed = p.isDropped; }); nextPlayerAuction(G, true); } else { toResourcesPhase(G); } } } else { nextPlayerClockwise(G); } } } break; } case Phase.Resources: { if (G.map.name == 'India') { if (G.chosenResource) { G.chosenResource = undefined; } else { player.passed = true; } } else { player.passed = true; } // Korea: end of this player's buying turn — clear the side lock // so the next player can pick freely. if (G.map.name == 'Korea' || G.map.name == 'Chile') { G.chosenSide = undefined; } if (G.players.filter((p) => !p.passed && !p.isDropped).length == 0) { G.players.forEach((p) => { p.passed = p.isDropped; }); G.phase = Phase.Building; if (G.map.name == 'India') { G.citiesBuiltInCurrentRound = 0; } setCurrentPlayer(G, G.playerOrder[G.players.length - 1]); if (G.players[G.currentPlayers[0]].isDropped && G.players.some((p) => !p.isDropped)) { G.players[G.currentPlayers[0]].passed = true; nextPlayerReverse(G); } } else { nextPlayerReverse(G); } if (G.map.name == 'India') { while (G.players[G.currentPlayers[0]].passed) { nextPlayerReverse(G); } } break; } case Phase.Building: { player.passed = true; if (G.players.filter((p) => !p.passed && !p.isDropped).length == 0) { const maxCities = Math.max(...G.players.map((p) => p.cities.length)); if (G.step == 1) { // Step 1 is single-occupancy, so the sum of player city // counts equals the number of cities with a house. When // that hits map.cities.length, every Step 1 slot is full // and Step 2 needs to fire even if no player reached // citiesToStep2 (can happen on small maps / small region // counts where the threshold is unreachable). const totalCitiesBuilt = G.players.reduce((sum, p) => sum + p.cities.length, 0); const allStep1HousesFilled = totalCitiesBuilt >= G.map.cities.length; if ( (maxCities >= G.citiesToStep2 || allStep1HousesFilled) && G.map.name != 'Middle East' && G.map.name != 'Manhattan' ) { beginStep2(G, 'Starting Step 2'); } } // A game nobody can finish ends anyway: in Step 3 (every city's capacity // at its last), if no player could ever build another house, the end // count can never be reached. UK & Ireland can get here: the island jump // needs an empty city, so a player locked on one island tops out at its // cities (13 or 12 on a 4-region board) once the other island fills. const stuck = !!G.options.stallEnds && maxCities < G.citiesToEndGame && G.step == 3 && noPlayerCanBuild(G); if (stuck) { G.log.push({ type: 'event', event: 'No player can build another city, so the game ends.', }); } if (maxCities >= G.citiesToEndGame || stuck) { G.phase = Phase.GameEnd; G.currentPlayers = []; calculateCitiesPowered(G); // Include payouts in phase 5 if there is a power outage in India. if (G.map.name == 'India' && G.citiesBuiltInCurrentRound! > G.players.length * 2) { G.players.forEach((player) => { const payment = G.paymentTable[player.citiesPowered] - 3 * player.cities.length; player.money += Math.max(payment, 0); player.totalIncome += Math.max(payment, 0); }); } // Australia: uranium income still pays in the final round // (unlike city income). Players sell in REVERSE turn order // (last player first); the market is not refilled afterwards. if (G.uraniumMineMarket) { [...G.playerOrder].reverse().forEach((idx) => { const seller = G.players[idx]; const sale = sellUraniumMine(G, seller); if (sale.mines > 0 && sale.income > 0) { const who = seller.name ?? `Player ${seller.id}`; G.log.push({ type: 'event', event: `${who} sells uranium for ${sale.income} Elektro (${sale.cities} cities × $${sale.price}, ${sale.mines} mine(s)).`, }); } }); } G.log.push({ type: 'event', event: 'Game Ended!' }); } else { G.players.forEach((p) => { p.passed = p.isDropped; p.powerPlantsNotUsed = p.powerPlants.map((pp) => pp.number); }); G.phase = Phase.Bureaucracy; G.currentPlayers = G.playerOrder.filter( (p) => !G.players[p].passed && !G.players[p].isDropped ); if (G.map.name == 'India') { // Compute the maximum number of cities each player can power. G.players.forEach( (player) => (player.targetCitiesPowered = calculateMaxCitiesPowered(G, player)) ); // Output log for power outage. if (G.citiesBuiltInCurrentRound! > G.players.length * 2) { G.log.push({ type: 'event', event: `Power outage! ${G.citiesBuiltInCurrentRound} built this round, which is more than twice the number of players.`, }); } } if (G.map.name == 'China' && G.step <= 2) { rebuildPlantMarketForChina(G); } else if (G.futureMarket.length == 0 && G.map.name != 'Manhattan') { // Manhattan never advances to Step 3 — its empty future // market is the all-buyable endgame stage, handled by the // market lifecycle, not a step change. startStep(G, 3); applyAustraliaStep3Shift(G); applyChileStep3(G); } } } else { nextPlayerReverse(G); } break; } case Phase.Bureaucracy: { player.passed = true; const payment = powerIncome(G, player); player.money += payment; if (G.options.trackTotalSpent) { player.totalIncome += payment; } player.citiesPowered = 0; if (G.map.name == 'India') { player.targetCitiesPowered = 0; } if (G.players.filter((p) => !p.passed && !p.isDropped).length == 0) { // Australia: the uranium-mine market resolves once everyone // has finished Bureaucracy. Players sell in REVERSE turn order // — the player last in turn order sells first, getting first // dibs on the highest empty price (the same trailing-player // advantage as the resource and building phases). Then the // resource refill removes tokens from the cheap end. if (G.uraniumMineMarket) { [...G.playerOrder].reverse().forEach((idx) => { const seller = G.players[idx]; const sale = sellUraniumMine(G, seller); if (sale.mines > 0) { const who = seller.name ?? `Player ${seller.id}`; G.log.push({ type: 'event', event: sale.income > 0 ? `${who} sells uranium for ${sale.income} Elektro (${sale.cities} cities × $${sale.price}, ${sale.mines} mine(s)).` : `${who}'s uranium mines earn nothing (market full).`, }); } }); const removed = resupplyUraniumMine( G, G.map.uraniumMineResupply![G.players.length - 2][G.step - 1] ); if (removed > 0) { G.log.push({ type: 'event', event: `Removing ${removed} uranium from the mine market (cheapest slots).`, }); } } // Resupply is also capped by remaining market capacity (the // prices array length minus current market size). Without // this, smaller markets like Korea's can overflow past the // number of slots and break price lookups. const coalCapSouth = (G.coalPrices?.length ?? prices[ResourceType.Coal].length) - G.coalMarket; const oilCapSouth = (G.oilPrices?.length ?? prices[ResourceType.Oil].length) - G.oilMarket; const garbageCapSouth = (G.garbagePrices?.length ?? prices[ResourceType.Garbage].length) - G.garbageMarket; const uraniumCapSouth = (G.uraniumPrices?.length ?? prices[ResourceType.Uranium].length) - G.uraniumMarket; // Korea: North restocks FIRST from the shared supply pool, then // South takes whatever remains. If supply runs short, South gets less. let coalResupplyNorthValue = 0; let oilResupplyNorthValue = 0; let garbageResupplyNorthValue = 0; if (G.coalResupplyNorth) { const coalCapNorth = G.coalPricesNorth!.length - G.coalMarketNorth!; const oilCapNorth = G.oilPricesNorth!.length - G.oilMarketNorth!; const garbageCapNorth = G.garbagePricesNorth!.length - G.garbageMarketNorth!; coalResupplyNorthValue = Math.min( G.coalSupply, G.coalResupplyNorth[G.players.length - 2][G.step - 1], coalCapNorth ); G.coalMarketNorth! += coalResupplyNorthValue; G.coalSupply -= coalResupplyNorthValue; oilResupplyNorthValue = Math.min( G.oilSupply, G.oilResupplyNorth![G.players.length - 2][G.step - 1], oilCapNorth ); G.oilMarketNorth! += oilResupplyNorthValue; G.oilSupply -= oilResupplyNorthValue; garbageResupplyNorthValue = Math.min( G.garbageSupply, G.garbageResupplyNorth![G.players.length - 2][G.step - 1], garbageCapNorth ); G.garbageMarketNorth! += garbageResupplyNorthValue; G.garbageSupply -= garbageResupplyNorthValue; } // South Africa pulls from coalStorage first, then coalSupply. // Other maps just pull from coalSupply. let coalResupplyValue: number; if (G.coalStorage !== undefined) { const wantCoal = Math.min(G.coalResupply![G.players.length - 2][G.step - 1], coalCapSouth); const fromStorage = Math.min(G.coalStorage, wantCoal); const fromSupply = Math.min(G.coalSupply, wantCoal - fromStorage); coalResupplyValue = fromStorage + fromSupply; G.coalMarket += coalResupplyValue; G.coalStorage -= fromStorage; G.coalSupply -= fromSupply; } else { coalResupplyValue = Math.min( G.coalSupply, G.coalResupply![G.players.length - 2][G.step - 1], coalCapSouth ); G.coalMarket += coalResupplyValue; G.coalSupply -= coalResupplyValue; } let oilResupplyValue: number; if (G.map.name == 'Middle East') { if (G.oilMarket == 0) { G.oilPrices = prices[ResourceType.Oil]; } oilResupplyValue = G.oilResupply![G.players.length - 2][G.step - 1]; for (let i = 0; i < oilResupplyValue; i++) { if (G.oilSupply > 0) { G.oilMarket++; G.oilSupply--; } else { // If we have more oil to stock than is in the supply, we shift the prices downward. G.oilPrices!.pop()!; G.oilPrices!.unshift(1); } } } else { oilResupplyValue = Math.min( G.oilSupply, G.oilResupply![G.players.length - 2][G.step - 1], oilCapSouth ); G.oilMarket += oilResupplyValue; G.oilSupply -= oilResupplyValue; } const garbageResupplyValue = Math.min( G.garbageSupply, G.garbageResupply![G.players.length - 2][G.step - 1], garbageCapSouth ); G.garbageMarket += garbageResupplyValue; G.garbageSupply -= garbageResupplyValue; let uraniumResupplyValue = 0; if ( G.options.variant != 'recharged' || (G.options.map != 'Germany' && G.options.map != 'Italy') || !G.card39Bought ) { uraniumResupplyValue = Math.min( G.uraniumSupply, G.uraniumResupply![G.players.length - 2][G.step - 1], uraniumCapSouth ); G.uraniumMarket += uraniumResupplyValue; G.uraniumSupply -= uraniumResupplyValue; } if (G.coalResupplyNorth) { G.log.push({ type: 'event', event: `Resupplying resources — North: [${coalResupplyNorthValue}, ${oilResupplyNorthValue}, ${garbageResupplyNorthValue}], ${ G.map.name == 'Chile' ? 'Central' : 'South' }: [${coalResupplyValue}, ${oilResupplyValue}, ${garbageResupplyValue}, ${uraniumResupplyValue}].`, }); } else { G.log.push({ type: 'event', event: `Resupplying resources: [${coalResupplyValue}, ${oilResupplyValue}, ${garbageResupplyValue}, ${uraniumResupplyValue}].`, }); } if (G.map.name == 'Manhattan') { // Manhattan's market is driven by deck depletions, not by a // step change. Its whole bureaucracy market behaviour lives in // the lifecycle helper, replacing the generic refill below. applyManhattanMarketLifecycle(G); } else if (G.map.name == 'Middle East' && G.step == 2 && G.futureMarket.length > 0) { // If we aren't about to enter step 3, discard top two plants instead of one. let powerPlantToPush: PowerPlant = G.futureMarket.pop()!; G.log.push({ type: 'event', event: `Putting Power Plant ${plantLabel( powerPlantToPush.number )} on the bottom of the deck.`, }); G.powerPlantsDeck.push(powerPlantToPush); addPowerPlant(G); // If Step 3 was drawn above, futureMarket will be empty so use actualMarket instead powerPlantToPush = G.futureMarket.length ? G.futureMarket.pop()! : G.actualMarket.pop()!; G.log.push({ type: 'event', event: `Putting Power Plant ${plantLabel( powerPlantToPush.number )} on the bottom of the deck.`, }); G.powerPlantsDeck.push(powerPlantToPush); addPowerPlant(G); } else if (G.futureMarket.length > 0) { if ( G.map.name == 'Benelux' && (G.options.variant == 'original' || G.discardSmallestPlant) ) { const removedPlant = G.actualMarket[0]; G.log.push({ type: 'event', event: `Removing smallest plant, Power Plant ${plantLabel(removedPlant.number)}.`, }); G.actualMarket.shift(); addPowerPlant(G); G.discardSmallestPlant = false; } let powerPlantToPush: PowerPlant | undefined; if (G.map.name == 'Quebec') { // For the Quebec map, ecological plants will never be put on the bottom of the deck. const nonEcoPlants = G.futureMarket.filter( (pp) => !isEcological(pp, G.options.fusionEcological) ); powerPlantToPush = nonEcoPlants.pop(); if (powerPlantToPush) { G.futureMarket = G.futureMarket.filter( (pp) => pp.number != powerPlantToPush?.number ); } else { const nonEcoPlants = G.actualMarket.filter( (pp) => !isEcological(pp, G.options.fusionEcological) ); powerPlantToPush = nonEcoPlants.pop(); if (powerPlantToPush) { G.actualMarket = G.actualMarket.filter( (pp) => pp.number != powerPlantToPush?.number ); } } } else { powerPlantToPush = G.futureMarket.pop()!; } // This check covers the rare case in which a Quebec game might have a futures market consisting of // all ecological plants. In that case, we do not draw a new plant. if (powerPlantToPush) { G.log.push({ type: 'event', event: `Putting Power Plant ${plantLabel( powerPlantToPush.number )} on the bottom of the deck.`, }); G.powerPlantsDeck.push(powerPlantToPush); addPowerPlant(G); } } else if (G.actualMarket.length > 0 && (G.map.name != 'China' || G.step == 3)) { G.log.push({ type: 'event', event: `Discarding Power Plant ${plantLabel(G.actualMarket[0].number)}.`, }); G.actualMarket.shift(); addPowerPlant(G); } G.round++; if (G.map.name != 'Baden-Württemberg') { // Baden-Württemberg: player order is determined AFTER the auction phase, not before it. setPlayerOrder(G); } G.players.forEach((p) => { p.passed = p.isDropped; }); G.auctionSkips = 0; if (G.actualMarket.length > 0) { G.phase = Phase.Auction; if (G.futureMarket.length == 0 && G.map.name != 'China' && G.map.name != 'Manhattan') { startStep(G, 3); applyAustraliaStep3Shift(G); applyChileStep3(G); } G.plantDiscountActive = G.options.variant == 'recharged' && !(G.options.endPlantDiscountAtStep3 && G.step === 3) && G.map.name != 'China' && G.map.name != 'Russia' && G.map.name != 'Baden-Württemberg'; setCurrentPlayer(G, G.playerOrder[0]); } else { toResourcesPhase(G); } } else { G.currentPlayers = G.playerOrder.filter((p) => !G.players[p].passed && !G.players[p].isDropped); } break; } } break; } case MoveName.DiscardPowerPlant: { asserts(move); if (!move.extra) { G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} discards Power Plant ${plantLabel(move.data)}.`, pretty: `${playerNameHTML(player)} discards Power Plant ${plantLabel(move.data)}.`, }); } const powerPlant = player.powerPlants.find((p) => p.number == move.data)!; player.powerPlants = player.powerPlants.filter((p) => p.number != move.data); updatePlayerCapacity(player); if (move.extra) { const discarded: string[] = []; switch (powerPlant.type) { case PowerPlantType.Coal: G.coalSupply += move.extra[0]; player.coalLeft -= move.extra[0]; discarded.push(move.extra[0] + ' Coal'); break; case PowerPlantType.Oil: G.oilSupply += move.extra[0]; player.oilLeft -= move.extra[0]; discarded.push(move.extra[0] + ' Oil'); break; case PowerPlantType.Garbage: G.garbageSupply += move.extra[0]; player.garbageLeft -= move.extra[0]; discarded.push(move.extra[0] + ' Garbage'); break; case PowerPlantType.Uranium: G.uraniumSupply += move.extra[0]; player.uraniumLeft -= move.extra[0]; discarded.push(move.extra[0] + ' Uranium'); break; case PowerPlantType.Hybrid: if (move.extra[0] > 0) { G.coalSupply += move.extra[0]; player.coalLeft -= move.extra[0]; discarded.push(move.extra[0] + ' Coal'); } if (move.extra[1] > 0) { G.oilSupply += move.extra[1]; player.oilLeft -= move.extra[1]; discarded.push(move.extra[1] + ' Oil'); } break; case PowerPlantType.GarbageOilHybrid: if (move.extra[0] > 0) { G.garbageSupply += move.extra[0]; player.garbageLeft -= move.extra[0]; discarded.push(move.extra[0] + ' Garbage'); } if (move.extra[1] > 0) { G.oilSupply += move.extra[1]; player.oilLeft -= move.extra[1]; discarded.push(move.extra[1] + ' Oil'); } break; } G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} discards Power Plant ${plantLabel(move.data)} and ${discarded.join(', ')}.`, pretty: `${playerNameHTML(player)} discards Power Plant ${plantLabel( move.data )} and ${discarded.join(', ')}.`, }); refillMarketAfterPurchase(G); G.players.forEach((p) => { p.bid = 0; p.passed = p.isDropped; }); if (G.players.some((p) => !p.skipAuction && !p.isDropped)) { nextPlayerAuction(G, true); } else { toResourcesPhase(G); } } else { const garbageOilHybrid = hasGarbageOilHybrid(player); const toDiscard: ResourceType[] = garbageOilHybrid ? fuelOverCapacity(player) : coalOilOverCapacity(player); // With a garbage/oil hybrid, overflow garbage can still fit in its tank, // so it is left to the fuelOverCapacity discard below. if (!garbageOilHybrid && player.garbageLeft > player.garbageCapacity) { G.garbageSupply += player.garbageLeft - player.garbageCapacity; player.garbageLeft = player.garbageCapacity; } if (player.uraniumLeft > player.uraniumCapacity) { G.uraniumSupply += player.uraniumLeft - player.uraniumCapacity; player.uraniumLeft = player.uraniumCapacity; } if (toDiscard.length == 1) { if (garbageOilHybrid) { discardDownToStorable(G, player, toDiscard[0]); } else { discardDownToCapacity(G, player, toDiscard[0]); } toDiscard.pop(); } if (toDiscard.length == 0) { refillMarketAfterPurchase(G); G.players.forEach((p) => { p.bid = 0; p.passed = p.isDropped; }); if (G.players.some((p) => !p.skipAuction && !p.isDropped)) { nextPlayerAuction(G, true); } else { toResourcesPhase(G); } } } break; } case MoveName.DiscardResources: { asserts(move); G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} discarded a ${move.data}.`, pretty: `${playerNameHTML(player)} discarded a ${move.data}.`, }); if (move.data == ResourceType.Coal) { player.coalLeft--; G.coalSupply++; } else if (move.data == ResourceType.Oil) { player.oilLeft--; G.oilSupply++; } else if (move.data == ResourceType.Garbage && hasGarbageOilHybrid(player)) { // Garbage is only offered as a discard to garbage/oil hybrid owners. player.garbageLeft--; G.garbageSupply++; } const garbageOilHybrid = hasGarbageOilHybrid(player); const toDiscard: ResourceType[] = garbageOilHybrid ? fuelOverCapacity(player) : coalOilOverCapacity(player); if (toDiscard.length == 1) { if (garbageOilHybrid) { discardDownToStorable(G, player, toDiscard[0]); } else { discardDownToCapacity(G, player, toDiscard[0]); } toDiscard.pop(); } if (toDiscard.length == 0) { refillMarketAfterPurchase(G); G.players.forEach((p) => { p.bid = 0; p.passed = p.isDropped; }); if (G.players.some((p) => !p.skipAuction && !p.isDropped)) { nextPlayerAuction(G, true); } else { toResourcesPhase(G); } } break; } case MoveName.BuyResource: { asserts(move); G.chosenResource = move.data.resource; // Korea: lock the player to the side of their first buy this turn. // Subsequent buys must come from the same side until they pass. if (move.data.side) { G.chosenSide = move.data.side; } const isNorth = move.data.side === 'north'; let price: number; switch (move.data.resource) { case ResourceType.Coal: { if (isNorth) { const coalPrices = G.coalPricesNorth!; price = coalPrices[coalPrices.length - G.coalMarketNorth!]; player.coalLeft++; G.coalMarketNorth!--; } else if (move.data.fromStorage) { // South Africa: $8 flat from the storage pool below the market. price = 8; player.coalLeft++; G.coalStorage!--; } else if (G.coalMarket == 0) { price = 8; player.coalLeft++; G.coalSupply--; move.fromSupply = true; } else { const coalPrices = G.coalPrices ?? prices[ResourceType.Coal]; price = coalPrices[coalPrices.length - G.coalMarket]; player.coalLeft++; G.coalMarket--; } break; } case ResourceType.Oil: { if (isNorth) { const oilPrices = G.oilPricesNorth!; price = oilPrices[oilPrices.length - G.oilMarketNorth!]; player.oilLeft++; G.oilMarketNorth!--; } else { const oilPrices = G.oilPrices ?? prices[ResourceType.Oil]; price = oilPrices[oilPrices.length - G.oilMarket]; player.oilLeft++; G.oilMarket--; } break; } case ResourceType.Garbage: { if (isNorth) { const garbagePrices = G.garbagePricesNorth!; price = garbagePrices[garbagePrices.length - G.garbageMarketNorth!]; player.garbageLeft++; G.garbageMarketNorth!--; } else { const garbagePrices = G.garbagePrices ?? prices[ResourceType.Garbage]; price = garbagePrice(G, player, garbagePrices[garbagePrices.length - G.garbageMarket]); player.garbageLeft++; G.garbageMarket--; } break; } case ResourceType.Uranium: { // Uranium is only available from the South market (or non-Korea maps). const uraniumPrices = G.uraniumPrices ?? prices[ResourceType.Uranium]; price = uraniumPrices[uraniumPrices.length - G.uraniumMarket]; player.uraniumLeft++; G.uraniumMarket--; break; } } player.money -= price; if (G.options.trackTotalSpent) { player.totalSpentResources += price; } G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} buys ${move.data.resource} for $${price}.`, pretty: `${playerNameHTML(player)} buys ${ move.data.resource } for $${price}.`, }); break; } case MoveName.Build: { asserts(move); // Japan: detect free jump. // Round 1: any second starting-city build uses the jump. // Round 2+: the player explicitly opted in via freeJump:true on the move. let isJapanFreeJump = false; if (G.map.name === 'Japan' && !player.usedFreeJump && player.cities.length >= 1) { if (G.round === 1 && G.map.startingCities) { const startingCities = new Set(G.map.startingCities); if (startingCities.has(move.data.name)) { isJapanFreeJump = true; } } else if (move.data.freeJump) { isJapanFreeJump = true; } } const position = G.players.filter((p) => p.cities.find((c) => c.name == move.data.name)).length; player.cities.push({ name: move.data.name, position }); player.money -= move.data.price; if (G.options.trackTotalSpent) { const cityData = G.map.cities.find((c) => c.name == move.data.name)!; if (cityData.singleOccupancy) { // SA cross-border: no house base — full price is "connection". Chile's Salta: its one slot cost. const house = cityData.slotCosts?.[0] ?? 0; player.totalSpentCities += house; player.totalSpentConnections += move.data.price - house; } else { player.totalSpentCities += 10 + position * 5; player.totalSpentConnections += move.data.price - (10 + position * 5); } } G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} builds on ${move.data.name} for $${move.data.price}.`, pretty: `${playerNameHTML(player)} builds on ${move.data.name} for $${ move.data.price }.`, }); if (isJapanFreeJump) { player.usedFreeJump = true; } if (G.map.name == 'India') { G.citiesBuiltInCurrentRound!++; } // Local round planning cannot know the replacement from the hidden deck. // That market change only matters for later rounds, outside its scope. if (updateBuildingMarket && G.options.variant == 'original') { if ( G.actualMarket.length > 0 && player.cities.length >= G.actualMarket[0].number && G.map.name != 'China' && G.map.name != 'Russia' && G.map.name != 'Baden-Württemberg' ) { G.actualMarket.shift(); addPowerPlant(G); } } break; } case MoveName.UsePowerPlant: { asserts(move); player.powerPlantsNotUsed = player.powerPlantsNotUsed.filter((x) => x != move.data.powerPlant); move.data.resourcesSpent.forEach((resourceType) => { switch (resourceType) { case ResourceType.Coal: player.coalLeft--; // SA: used coal returns to the separate storage pool below the market. if (G.coalStorage !== undefined) { G.coalStorage++; } else { G.coalSupply++; } break; case ResourceType.Oil: player.oilLeft--; G.oilSupply++; break; case ResourceType.Garbage: player.garbageLeft--; G.garbageSupply++; break; case ResourceType.Uranium: player.uraniumLeft--; G.uraniumSupply++; break; } }); player.citiesPowered += move.data.citiesPowered; G.log.push({ type: 'move', player: playerNumber, move, simple: `${player.name} uses Power Plant ${plantLabel(move.data.powerPlant)}.`, pretty: `${playerNameHTML(player)} uses Power Plant ${plantLabel(move.data.powerPlant)}.`, }); break; } } player.availableMoves = null; player.lastMove = revision ? ( G.hiddenLog.find( (entry) => entry.type === 'move' && entry.player === playerNumber ) as import('./log').LogMove )?.move ?? move : move; G.cardsLeft = G.powerPlantsDeck.length; G.nextCardWeak = nextCardIsPlug(G); updateClocks(G, move.poweringClock?.at ?? move.serverTime ?? move.time); if (move.poweringClock) { player.totalTimeUsed = move.poweringClock.totalTimeUsed; player.clockStartedAt = move.poweringClock.clockStartedAt; } if (initialPhase === Phase.Bureaucracy) finishPoweringLog(G, playerNumber, initialRound, move); G.currentPlayers.forEach((p) => (G.players[p].availableMoves = availableMoves(G, G.players[p]))); // Tentative-turn bookkeeping: the state is committed (`newTurn`) once the mover can // no longer undo. This mirrors the old undo-availability rule — undo was offered to // the mover while the last visible log entry was their own move, they were still a // current player, and that move was not a Pass. (The old rule's second branch — // Bureaucracy undo after ANOTHER player's move landed on top of yours — collapses // into this one under the turn-buffer model: a buffer always replays from a // committed state, so after the mover's move the last log entry is always theirs.) // // Consequences, phase by phase: // - Pass always commits (every turn ends with one, or with a move that hands // control elsewhere), including the simultaneous Bureaucracy phase where the // mover's own powering ends even though other players stay current. // - Moves followed by engine-pushed log EVENTS (market refills, auction // resolutions, upkeep...) commit, exactly like the old rule refused to undo // across them. // - Bids in a fastBid auction go to the hidden log; the mover leaves // currentPlayers, so they commit as single-move turns. // - Colour picks commit even if skipping custom-colour seats hands the same // player the next auction or region-selection turn. const lastLog = G.log[G.log.length - 1]; G.newTurn = !( move.name !== MoveName.ChooseColor && lastLog?.type === 'move' && lastLog.player === playerNumber && G.currentPlayers.includes(playerNumber) && player.lastMove?.name !== MoveName.Pass ); return G; } // A player's clock runs exactly while they are a current player. Called once after // each move has resolved (so currentPlayers is final): // - anyone whose clock was running but who is no longer current has that stretch // banked into totalTimeUsed (they either just moved or their window closed); // - anyone now current without a running clock starts one at this timestamp. // A player who is still current after moving (a multi-step turn) keeps their clock // running, so their whole turn accumulates as one stretch. // // During simultaneous phases every current player's clock runs independently, so // the sum of players' time can legitimately exceed the wall-clock time elapsed. // // `ts` comes from the move (its `serverTime`, else the client `time`) rather than the // system clock, so replaying a log reproduces identical times. It must be a stamp from // a single clock shared by every player: banking a stretch below subtracts one player's // stamp from another's, so mixing skewed per-client clocks would charge the skew to // whoever's clock runs fast. `wrapper.move` supplies `serverTime` for human turns and // `moveAI` stamps the server clock into `time`, so both resolve to the server clock. // Moves without any timestamp (dropped players) leave every clock untouched. function updateClocks(G: GameState, ts?: number) { if (ts == undefined) { return; } for (const player of G.players) { const isCurrent = G.currentPlayers.includes(player.id); if (!isCurrent && player.clockStartedAt != undefined) { // Math.max guards against clock skew between players' browsers, which // could otherwise make a stretch negative. player.totalTimeUsed = (player.totalTimeUsed ?? 0) + Math.max(0, ts - player.clockStartedAt); player.clockStartedAt = undefined; } else if (isCurrent && player.clockStartedAt == undefined) { player.clockStartedAt = ts; } } } export function moveAI(G: GameState, playerNumber: number): GameState { const player = G.players[playerNumber]; const availableMoves = player.availableMoves; let chosenMove: Move = { name: MoveName.Pass, data: true }; switch (G.phase) { case Phase.RegionSelection: { if (availableMoves?.ChooseRegion && availableMoves.ChooseRegion.length > 0) { chosenMove = { name: MoveName.ChooseRegion, data: chooseRandom(availableMoves.ChooseRegion) }; } break; } case Phase.ColorSelection: { if (availableMoves?.ChooseColor && availableMoves.ChooseColor.length > 0) { chosenMove = { name: MoveName.ChooseColor, data: chooseRandom(availableMoves.ChooseColor) }; } break; } case Phase.Auction: { if (availableMoves?.ChoosePowerPlant) { if ( !availableMoves.Pass || (Math.random() > 0.5 && player.money - availableMoves.ChoosePowerPlant[0] >= 20) ) { chosenMove = { name: MoveName.ChoosePowerPlant, data: chooseRandom(availableMoves.ChoosePowerPlant), }; } else { chosenMove = { name: MoveName.Pass, data: true }; } } else if (availableMoves?.Bid) { if ( !availableMoves.Pass || (availableMoves.Bid.length > 0 && Math.random() > 0.5 && player.money - availableMoves?.Bid[0] >= 15) ) { if (G.options.fastBid) { const bid = Math.floor((Math.random() * availableMoves.Bid.length) / 2); chosenMove = { name: MoveName.Bid, data: availableMoves?.Bid[bid] }; } else { chosenMove = { name: MoveName.Bid, data: availableMoves?.Bid[0] }; } } else { chosenMove = { name: MoveName.Pass, data: true }; } } else if (availableMoves?.DiscardPowerPlant) { // Pick from the offered candidates: the oldest-held plant can be an // Australia uranium mine or the just-bought plant, neither of which // is legal to discard — and dropPlayer() replays this prompt via // moveAI, so an illegal pick would throw inside the drop path. chosenMove = { name: MoveName.DiscardPowerPlant, data: Math.min(...availableMoves.DiscardPowerPlant) }; } else if (availableMoves?.DiscardResources) { chosenMove = { name: MoveName.DiscardResources, data: chooseRandom(availableMoves.DiscardResources) }; } break; } case Phase.Resources: { if (availableMoves?.BuyResource && player.money > 20) { const buyCoal = availableMoves.BuyResource.find((r) => r.resource == ResourceType.Coal); const buyOil = availableMoves.BuyResource.find((r) => r.resource == ResourceType.Oil); const buyGarbage = availableMoves.BuyResource.find((r) => r.resource == ResourceType.Garbage); const buyUranium = availableMoves.BuyResource.find((r) => r.resource == ResourceType.Uranium); const garbageOilTank = player.garbageOilHybridCapacity ?? 0; if (buyCoal && player.coalLeft < (player.coalCapacity + player.hybridCapacity) / 2) { chosenMove = { name: MoveName.BuyResource, data: buyCoal }; } else if ( buyOil && player.oilLeft < (player.oilCapacity + player.hybridCapacity + garbageOilTank) / 2 ) { chosenMove = { name: MoveName.BuyResource, data: buyOil }; } else if (buyGarbage && player.garbageLeft < (player.garbageCapacity + garbageOilTank) / 2) { chosenMove = { name: MoveName.BuyResource, data: buyGarbage }; } else if (buyUranium && player.uraniumLeft < player.uraniumCapacity / 2) { chosenMove = { name: MoveName.BuyResource, data: buyUranium }; } } break; } case Phase.Building: { // Seed the sum: reduce() with no initial value throws on an empty array, so a // plant-less player would crash the auto-play path instead of scoring zero capacity. const capacity = player.powerPlants.map((pp) => pp.citiesPowered).reduce((a, b) => a + b, 0); if (availableMoves?.Build && (player.money >= 30 || capacity > player.cities.length)) { // Sort a copy: `availableMoves` is `player.availableMoves`, which lives on the // game state and is served to clients — choosing a move must not reorder it. const minPrice = [...availableMoves.Build].sort((a, b) => a.price - b.price)[0].price; const cheapestCities = availableMoves.Build.filter((x) => x.price == minPrice); chosenMove = { name: MoveName.Build, data: chooseRandom(cheapestCities) }; } break; } case Phase.Bureaucracy: { if ( availableMoves?.UsePowerPlant && (!availableMoves.Pass || player.cities.length > player.citiesPowered) ) { chosenMove = { name: MoveName.UsePowerPlant, // Sorted on a copy, for the same reason as Build above. data: [...availableMoves.UsePowerPlant].sort((a, b) => a.citiesPowered - b.citiesPowered)[0], }; } break; } } // Stamp the move so the per-player clocks keep advancing across auto-played // turns (bots, and dropped players auto-played by dropPlayer). Without this the // clock chain breaks: no clock starts, so the next human to move sits on an // un-started clock showing zero. Reading the wall clock is safe here — moveAI is // a live move generator that is never re-run on replay (it is already // non-deterministic via chooseRandom), and the timestamp it produces is written // into the log, so replays re-apply the recorded value. return move(G, { ...chosenMove, time: Date.now() }, playerNumber); } function chooseRandom(moves: any[]) { return moves[Math.floor(Math.random() * moves.length)]; } // True when no active player has any city left they could build in, whatever their money: // every option is priced 9999 (taken, unreachable, or a jump into an occupied city). export function noPlayerCanBuild(G: GameState): boolean { return G.players.filter((p) => !p.isDropped).every((p) => buildOptions(G, p).every((c) => c.price >= 9999)); } export function ended(G: GameState): boolean { return G.phase == Phase.GameEnd; } export function scores(G: GameState): number[] { return ended(G) ? G.players.map((p) => p.citiesPowered) : G.players.map((_) => 0); } // A public reconstruction must replay observed draws across shuffles (not shuffle // those observations as though they were an unknown future deck). This ephemeral // queue never enters a saved state or a scenario snapshot. const publicReplayDraws = new WeakMap(); // The third argument is retained for callers that previously opted into public // draws. All secret-seed reconstructions now use that path. export function reconstructState(gameState: GameState, to?: number, _forcePublicDraws = false): GameState { const initialState = getBaseState(gameState); const G = cloneDeep(initialState); if (gameState.seed == 'secret') { if (gameState.knownPowerPlantDeck) { // Chile: the live map may already have its gates open, but the replay starts before Step 3. G.map = closeChileGates(gameState.map); G.blockedCities = gameState.blockedCities?.slice(); G.powerPlantsDeck = cloneDeep(gameState.knownPowerPlantDeck); // Split sizes must match the map's setup, not the standard 4+4: Russia // deals a 3+3 market, China numPlayers+0, etc. The base state was built // by the same setupDeck, so its market sizes are authoritative even // though the (seeded) contents are not. knownPowerPlantDeck is // actual-then-future at game start with the draws appended, so the // first actualN+futureN entries seed the market; the rest are draws. const actualN = initialState.actualMarket.length; const futureN = initialState.futureMarket.length; G.actualMarket = G.powerPlantsDeck.splice(0, actualN); G.futureMarket = G.powerPlantsDeck.splice(0, futureN); G.players[G.currentPlayers[0]].availableMoves = availableMoves(G, G.players[G.currentPlayers[0]]); G.knownPowerPlantDeck = G.actualMarket.concat(G.futureMarket); // Observations are a draw transcript, not a physical deck: they omit // unseen cards and include cards drawn again after being bottomed. // Keep setup's physical deck for its size and normal lifecycle (bottoming, // Step 3 shuffles, Manhattan recycling, Spain's held-back plants), but // take card identities from the transcript. Never replace the physical // deck with the Step 3 transcript, which may still be only a prefix. publicReplayDraws.set( G, cloneDeep( gameState.knownPowerPlantDeck .slice(actualN + futureN) .concat(gameState.knownPowerPlantDeckStep3 || []) ) ); G.powerPlantsDeck = cloneDeep(initialState.powerPlantsDeck); G.powerPlantDeckAfterStep3 = undefined; if (G.map.name === 'Middle East') { // Setup already filtered its randomly seeded market. Repeat that // filtering from the recorded initial market and an unfiltered deck. G.powerPlantsDeck = defaultSetupDeck( G.players.length, G.options.variant!, seedrandom('public-replay'), G.options.useNewRechargedSetup ?? true, gameDeck(G.options), G.map.name, 0, gameDeck(G.options) == 'shuffled' && G.options.shuffledSample === true ).powerPlantsDeck; // The placeholder setup's own filtering may have used up the discount. G.plantDiscountActive = G.options.variant == 'recharged'; removePlantsForMiddleEastStep1(G); G.players[G.currentPlayers[0]].availableMoves = availableMoves(G, G.players[G.currentPlayers[0]]); } G.cardsLeft = G.powerPlantsDeck.length; } } const log = to != null ? gameState.log.slice(0, to) : gameState.log; for (const item of log) { switch (item.type) { case 'event': { if (item.analysisMoney) { applyAnalysisMoney(G, item.analysisMoney); G.log.push(cloneDeep(item)); } if (item.event.endsWith('was dropped')) { const playerNum = +item.event.split(' ')[1]; G.players[playerNum].isDropped = true; } break; } case 'move': { move(G, item.move, item.player); break; } } } delete G.pendingMessages; return G; } export function updatePlayerCapacity(player: Player) { player.coalCapacity = player.oilCapacity = player.garbageCapacity = player.uraniumCapacity = player.hybridCapacity = 0; // Only alternate-deck players carry this field; leave everyone else's state as is. if (player.garbageOilHybridCapacity !== undefined) { player.garbageOilHybridCapacity = 0; } player.powerPlants.forEach((powerPlant) => { switch (powerPlant.type) { case PowerPlantType.Coal: { player.coalCapacity += powerPlant.cost * 2; break; } case PowerPlantType.Oil: { player.oilCapacity += powerPlant.cost * 2; break; } case PowerPlantType.Garbage: { if (powerPlant.storage) { // For the India map, garbage plants have cost one higher, but have no additional storage. player.garbageCapacity += powerPlant.storage; } else { player.garbageCapacity += powerPlant.cost * 2; } break; } case PowerPlantType.Uranium: { player.uraniumCapacity += powerPlant.cost * 2; break; } case PowerPlantType.Hybrid: { player.hybridCapacity += powerPlant.cost * 2; break; } case PowerPlantType.GarbageOilHybrid: { player.garbageOilHybridCapacity = (player.garbageOilHybridCapacity ?? 0) + powerPlant.cost * 2; break; } } }); } export function addPowerPlant(G: GameState): void { // Manhattan: the draw deck can run dry mid-round during an auction, not only at // Bureaucracy. Fire the depletion transition the instant it does, so the first // depletion's reshuffle (or the second depletion's whole-market-buyable collapse) // happens when it should instead of stalling the market until the end of the round // — the "3 in the top row, 4 in the bottom, the new deck should start but it // didn't" report on ManhattanJune28. The helper only reshuffles/collapses; the // draw below still owns the single post-buy refill, so the market can't double-fill. if (G.map.name == 'Manhattan' && G.powerPlantsDeck.length == 0) { applyManhattanDepletionOnEmptyDeck(G); } let powerPlant = G.powerPlantsDeck.shift(); if (publicReplayDraws.has(G)) powerPlant = publicReplayDraws.get(G)!.shift(); if (powerPlant) { if (G.step == 3) { if (G.knownPowerPlantDeckStep3) { G.knownPowerPlantDeckStep3.push(powerPlant); } } else { if (G.knownPowerPlantDeck) { G.knownPowerPlantDeck.push(powerPlant); } } if (G.options.variant == 'original' && G.map.name != 'China') { const maxCities = Math.max(...G.players.map((p) => p.cities.length)); while (Math.floor(powerPlant.number) <= maxCities) { G.log.push({ type: 'event', event: `Power plant ${plantLabel(powerPlant?.number)} discarded.`, }); if (G.powerPlantsDeck.length > 0) { powerPlant = G.powerPlantsDeck.shift()!; if (publicReplayDraws.has(G)) powerPlant = publicReplayDraws.get(G)!.shift()!; if (G.step == 3) { if (G.knownPowerPlantDeckStep3) { G.knownPowerPlantDeckStep3.push(powerPlant); } } else { if (G.knownPowerPlantDeck) { G.knownPowerPlantDeck.push(powerPlant); } } } else { break; } } } let skipAdd = false; if (powerPlant.number == 99) { // Drawn before Step 2 has begun: Step 2's setup comes first, then Step 3's. // In the auction it waits for the end of the phase, with Step 3's. if (G.phase != Phase.Auction && step2BeforeStep3(G)) { beginStep2(G, 'Starting Step 2 (the Step 3 card came first)'); } if (G.powerPlantDeckAfterStep3) { G.powerPlantsDeck = G.powerPlantDeckAfterStep3; } else if (G.map.name == 'China') { startStep(G, 3); } else { G.powerPlantsDeck = shuffle(G.powerPlantsDeck, G.seed); } if (G.phase != Phase.Auction) { if (G.map.name == 'Middle East' && G.step == 1) { // Add step 3 card to market, then trigger step 2 process. const market = [...G.actualMarket, ...G.futureMarket, powerPlant]; market.sort((a, b) => a.number - b.number); G.actualMarket = market.slice(0, 4); G.futureMarket = market.slice(4); enterStepTwoMiddleEast(G); skipAdd = true; } else { if (G.map.name == 'UK & Ireland' && G.step == 1) { // UK&I: Step 3 sits 3rd from last in the deck, so it can // surface before any player hits citiesToStep2. Fire Step // 2 here so its rules register before Step 3 takes over. startStep(G, 2, 'Starting Step 2 (Step 3 card drawn before Step 2 threshold).'); } const powerPlantDiscarded = G.actualMarket.shift(); G.log.push({ type: 'event', event: `Step 3 will begin next phase, Power Plant ${plantLabel( powerPlantDiscarded?.number )} discarded.`, }); const market = [...G.actualMarket, ...G.futureMarket]; market.sort((a, b) => a.number - b.number); G.actualMarket = market; G.futureMarket = []; } } } else { if (G.plantDiscountActive && powerPlant.number < G.actualMarket[0].number) { G.log.push({ type: 'event', event: `Power Plant ${plantLabel(powerPlant.number)} drawn from the deck and discarded.`, }); G.plantDiscountActive = false; addPowerPlant(G); return; } else { G.log.push({ type: 'event', event: `Power Plant ${plantLabel(powerPlant.number)} drawn from the deck.`, }); } } if (G.map.name == 'China' && powerPlant.type != PowerPlantType.Step3) { const market = [...G.actualMarket, powerPlant]; market.sort((a, b) => a.number - b.number); G.actualMarket = market; } else { if (!skipAdd) { const market = [...G.actualMarket, ...G.futureMarket, powerPlant]; market.sort((a, b) => a.number - b.number); if (G.futureMarket.length == 0) { if (G.map.name == 'Russia') { // Only four plants in market. G.actualMarket = market.slice(0, 4); G.futureMarket = []; } else { G.actualMarket = market.slice(0, 6); G.futureMarket = []; } } else { if (G.map.name == 'Russia') { // Only 3 plants in actual market and 3 in future market. G.actualMarket = market.slice(0, 3); G.futureMarket = market.slice(3); } else if (G.map.name == 'Benelux' && isEcological(market[4], G.options.fusionEcological)) { // Add extra ecological plant to actual market. G.actualMarket = market.slice(0, 5); G.futureMarket = market.slice(5); } else { G.actualMarket = market.slice(0, 4); G.futureMarket = market.slice(4); } } if (G.map.name == 'Middle East' && G.step == 1) { removePlantsForMiddleEastStep1(G); } } } } } // Manhattan never leaves Step 1, so the usual "future market empties → Step 3" // machinery is guarded off for it. Instead the power-plant market is driven by // DECK DEPLETIONS, in three stages tracked by G.manhattanDepletion (0 → 1 → 2). // This replaces the generic Bureaucracy market refill for Manhattan and runs once // per Bureaucracy round: // // Stage 0 — before the first depletion. The two highest-numbered future-market // plants are set aside each round on a SEPARATE recycle pile (not boxed: they // come back) and the market is refilled from the deck, steadily draining it. // First depletion (deck runs dry while a recycle pile exists): the recycle pile // is reshuffled into a fresh deck and the market refilled. → stage 1. // Stage 1 — between the depletions. One highest future plant rotates to the // bottom of the deck each round; the deck cycles through exactly once and is // drained the rest of the way by ordinary auction purchases. // Second depletion (deck runs dry again): the entire market collapses into the // actual market, so every plant becomes buyable (Step-3-like, but still // Step 1). → stage 2. // Stage 2 — endgame churn. The single cheapest market plant is removed from the // game each round, shrinking the market until it (or the game) ends. export function applyManhattanMarketLifecycle(G: GameState) { G.manhattanRecyclePile ??= []; G.manhattanDepletion ??= 0; // Pull the single highest-numbered plant out of the future market. const popBiggestFuture = (): PowerPlant | undefined => { if (G.futureMarket.length == 0) { return undefined; } G.futureMarket.sort((a, b) => a.number - b.number); return G.futureMarket.pop(); }; // Draw from the deck until the market holds `size` plants (or the deck is // empty), then keep it sorted with the cheapest four buyable (actual market) // and the rest visible-but-not-buyable (future market). const refillMarketTo = (size: number) => { while (G.actualMarket.length + G.futureMarket.length < size && G.powerPlantsDeck.length > 0) { const drawn = publicReplayDraws.has(G) ? (G.powerPlantsDeck.shift(), publicReplayDraws.get(G)!.shift()!) : G.powerPlantsDeck.shift()!; if (G.knownPowerPlantDeck) { G.knownPowerPlantDeck.push(drawn); } G.log.push({ type: 'event', event: `Power Plant ${plantLabel(drawn.number)} drawn from the deck.` }); G.futureMarket.push(drawn); } const market = [...G.actualMarket, ...G.futureMarket].sort((a, b) => a.number - b.number); G.actualMarket = market.slice(0, 4); G.futureMarket = market.slice(4); }; if (G.manhattanDepletion == 0) { for (let i = 0; i < 2; i++) { const plant = popBiggestFuture(); if (!plant) { break; } G.manhattanRecyclePile.push(plant); G.log.push({ type: 'event', event: `Setting Power Plant ${plantLabel(plant.number)} aside on the recycle pile.`, }); } refillMarketTo(8); if (G.powerPlantsDeck.length == 0 && G.manhattanRecyclePile.length > 0) { G.powerPlantsDeck = shuffle(G.manhattanRecyclePile, G.seed + ':manhattan-depletion-1'); G.manhattanRecyclePile = []; G.manhattanDepletion = 1; G.log.push({ type: 'event', event: 'The draw deck is empty — reshuffling the recycle pile into a new deck.', }); refillMarketTo(8); } } else if (G.manhattanDepletion == 1) { const plant = popBiggestFuture(); if (plant) { G.powerPlantsDeck.push(plant); G.log.push({ type: 'event', event: `Putting Power Plant ${plantLabel(plant.number)} on the bottom of the deck.`, }); } refillMarketTo(8); if (G.powerPlantsDeck.length == 0) { G.manhattanDepletion = 2; const market = [...G.actualMarket, ...G.futureMarket].sort((a, b) => a.number - b.number); G.actualMarket = market; G.futureMarket = []; G.log.push({ type: 'event', event: 'The deck is empty for the second time — the entire market is now buyable.', }); } } else { // Stage 2. Collapse any stragglers into the buyable market (defensive), then // box the single cheapest plant for the game's final churn. if (G.futureMarket.length > 0) { const market = [...G.actualMarket, ...G.futureMarket].sort((a, b) => a.number - b.number); G.actualMarket = market; G.futureMarket = []; } if (G.actualMarket.length > 0) { const removed = G.actualMarket.shift()!; G.log.push({ type: 'event', event: `Removing the smallest plant, Power Plant ${plantLabel(removed.number)}, from the game.`, }); } } } // Fire Manhattan's deck-depletion transition the instant the draw deck runs dry, // wherever that happens — in particular during an auction purchase's refill, not // only the Bureaucracy lifecycle above. It mirrors the two depletion arms of // applyManhattanMarketLifecycle: // • first depletion (stage 0, a recycle pile exists) → reshuffle that pile into a // fresh deck and advance to stage 1; the caller's own draw then refills the market. // • second depletion (stage 1) → collapse the whole market into the buyable (actual) // market, sorted, and advance to stage 2; no deck remains to draw from. // Unlike the Bureaucracy path it deliberately does NOT draw/refill — the sole caller // (addPowerPlant) owns the single post-buy draw, so the market cannot double-fill. export function applyManhattanDepletionOnEmptyDeck(G: GameState): void { if (G.map.name != 'Manhattan' || G.powerPlantsDeck.length > 0) { return; } G.manhattanRecyclePile ??= []; G.manhattanDepletion ??= 0; if (G.manhattanDepletion == 0 && G.manhattanRecyclePile.length > 0) { G.powerPlantsDeck = shuffle(G.manhattanRecyclePile, G.seed + ':manhattan-depletion-1'); G.manhattanRecyclePile = []; G.manhattanDepletion = 1; G.log.push({ type: 'event', event: 'The draw deck is empty — reshuffling the recycle pile into a new deck.', }); } else if (G.manhattanDepletion == 1) { G.manhattanDepletion = 2; const market = [...G.actualMarket, ...G.futureMarket].sort((a, b) => a.number - b.number); G.actualMarket = market; G.futureMarket = []; G.log.push({ type: 'event', event: 'The deck is empty for the second time — the entire market is now buyable.', }); } } // During step 1 for the Middle East map, we remove garbage and uranium plants from the actual market. // If the number is 6, 11, or 14, the plant is removed from the game. Otherwise, it's put under the deck. function removePlantsForMiddleEastStep1(G: GameState) { let plantToRemove: PowerPlant | undefined = G.actualMarket.find( (pp: PowerPlant) => pp.type == PowerPlantType.Garbage || pp.type == PowerPlantType.Uranium ); while (plantToRemove) { removePowerPlant(G, plantToRemove); // Plug plants leave the game (06, 11 and 14 in the standard deck); others go under. if (isPlugPlant(plantToRemove, gameDeck(G.options))) { G.log.push({ type: 'event', event: `Removing Power Plant ${plantLabel(plantToRemove.number)} from game.`, }); } else { G.powerPlantsDeck.push(plantToRemove); G.log.push({ type: 'event', event: `Sending Power Plant ${plantLabel(plantToRemove.number)} to the bottom of the deck.`, }); } // Prevent infinite loop cycling through power plants. const availableFuturePlants = G.futureMarket.filter( (pp) => pp.type != PowerPlantType.Garbage && pp.type != PowerPlantType.Uranium ); const nextFuturePlantNumber = availableFuturePlants.length > 0 ? availableFuturePlants[0].number : 100; if ( G.powerPlantsDeck.filter( (pp) => (pp.type != PowerPlantType.Garbage && pp.type != PowerPlantType.Uranium) || pp.number > nextFuturePlantNumber ).length == 0 ) { G.log.push({ type: 'event', event: 'No suitable power plants available to draw.', }); break; } addPowerPlant(G); plantToRemove = G.actualMarket.find( (pp: PowerPlant) => pp.type == PowerPlantType.Garbage || pp.type == PowerPlantType.Uranium ); } } // Maps with their own rule for an early Step 3 card: Middle East's second pass, // China's ordered deck, and Manhattan, which has no Step 3. const OWN_EARLY_STEP_3_MAPS = ['Middle East', 'China', 'Manhattan']; // The rulebooks: when the Step 3 card is drawn before Step 2 has begun, carry out // everything that begins Step 2, then prepare Step 3, back to back. Games created // before this rule was followed keep their old sequence (see reconstructionOptions). function step2BeforeStep3(G: GameState): boolean { return !!G.options.step2BeforeStep3 && G.step == 1 && !OWN_EARLY_STEP_3_MAPS.includes(G.map.name); } // Step 2's setup: the smallest plant leaves the market and a new one is drawn (Spain & // Portugal first puts its held-back plants on top). Also run, back to back with Step // 3's, when the Step 3 card is drawn before Step 2 has begun. function beginStep2(G: GameState, event: string) { const powerPlant = G.actualMarket.shift()!; startStep(G, 2, `${event}, Power Plant ${plantLabel(powerPlant?.number)} discarded.`); // Spain & Portugal: put the held-back plants on top (18, 22, 27 in the standard deck) if (G.map.name == 'Spain & Portugal' && G.heldPowerPlants) { // The shuffled decks: the held-back copies that were dealt, smallest on top. G.powerPlantsDeck.unshift(...cloneDeep(G.heldPowerPlants)); } else if (G.map.name == 'Spain & Portugal') { const deck = gameDeck(G.options) as SingleDeck; const held = SPAIN_STEP_2_PLANTS[deck]; if (!G.powerPlantsDeck.find((p) => p.number == held[0])) { G.powerPlantsDeck.unshift(...held.map((n) => getPowerPlant(n, G.map.name, deck))); } } if (G.map.name == 'Europe') { // Europe: do NOT draw a replacement from the deck. // The future market shrinks from 5 to 4; reorganize // the remaining 8 plants so actual stays at 4. const market = [...G.actualMarket, ...G.futureMarket]; market.sort((a, b) => a.number - b.number); G.actualMarket = market.slice(0, 4); G.futureMarket = market.slice(4); } else { addPowerPlant(G); } } function startStep(G: GameState, step: 2 | 3, event = `Starting Step ${step}.`) { if (G.step === step) return; G.step = step; if (step === 3 && G.options.variant === 'recharged' && G.options.endPlantDiscountAtStep3) { G.plantDiscountActive = false; } G.log.push({ type: 'event', event }); (G.pendingMessages ??= []).push(`Starting Step ${step}.`); } function enterStepTwoMiddleEast(G: GameState) { // Shuffle deck of remaining power plants and put step 3 card back underneath. const step3 = G.futureMarket.pop()!; G.powerPlantsDeck = shuffle(G.powerPlantsDeck, G.seed); G.powerPlantsDeck.push(step3); // Draw new plant to replace step 3 card. addPowerPlant(G); // Discard two lowest power plants from current market. startStep(G, 2, 'Step 2 will begin next phase, discarding two power plants.'); const powerPlantDiscarded1 = G.actualMarket.shift(); if (powerPlantDiscarded1) { G.log.push({ type: 'event', event: `Power Plant ${plantLabel(powerPlantDiscarded1.number)} discarded to start step 2.`, }); addPowerPlant(G); } const powerPlantDiscarded2 = G.actualMarket.shift(); if (powerPlantDiscarded2) { G.log.push({ type: 'event', event: `Power Plant ${plantLabel(powerPlantDiscarded2.number)} discarded to start step 2.`, }); addPowerPlant(G); } } // Australia CO2 tax: when Step 3 begins, the $1 and $2 resource spaces close for // the rest of the game and the six cheapest tokens of each resource move to the // new $9 and $10 spaces — i.e. the active price scale shifts up by $2. Because // the engine prices a market of M cubes as prices[length - M] (cubes sit at the // expensive end), swapping the price arrays to the $3–$10 scale reprices the // existing cubes exactly as the physical relocation does, for both full and // partially-depleted markets — the market counts and supply pools are untouched. // Idempotent and Australia-only, so it is safe to call at every Step 3 entry. export function applyAustraliaStep3Shift(G: GameState) { if (G.map.name !== 'Australia') { return; } if (G.coalPrices && G.coalPrices[0] === 3) { return; // already shifted this game } const shifted = [3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10]; G.coalPrices = [...shifted]; G.oilPrices = [...shifted]; G.garbagePrices = [...shifted]; G.log.push({ type: 'event', event: 'CO2 tax (Step 3): the $1 and $2 resource spaces close; the six cheapest tokens of each resource move to the new $9 and $10 spaces.', }); } /** * Chile, when Step 3 begins (after the usual shuffle), in this order: * 1. The two markets merge onto the standard 24-slot tracks, filling from the most expensive spaces; from now on * the main market restocks with its Step 3 column and the North market is gone. * 2. The gates open. * 3. The set-aside ecological plants go on top of the draw stack (33 on top, then 37, then 44). * 4. Every ecological plant powers one more city than printed, wherever it is: deck, market or a player's hand. * Fusion plant 50 is not boosted. Plants are replaced by boosted copies, never changed in place, so no shared * catalogue object is touched. * Runs once (the North market is gone afterwards), so it is safe at every Step 3 entry. */ /** Chile: the map as it was before Step 3, with any open gates back in step3Connections. Other maps unchanged. */ export function closeChileGates(map: GameMap): GameMap { if (map.name !== 'Chile' || map.step3Connections) { return map; } const chile = maps.find((m) => m.name == 'Chile')!; const isGate = (con: { nodes: string[] }) => chile.step3Connections!.some((gate) => gate.nodes.every((n) => con.nodes.includes(n))); return { ...map, connections: map.connections.filter((con) => !isGate(con)), step3Connections: map.connections.filter(isGate), }; } export function applyChileStep3(G: GameState) { if (G.map.name !== 'Chile' || G.coalMarketNorth === undefined) { return; } G.coalPrices = [...prices[ResourceType.Coal]]; G.oilPrices = [...prices[ResourceType.Oil]]; G.garbagePrices = [...prices[ResourceType.Garbage]]; G.coalMarket += G.coalMarketNorth; G.oilMarket += G.oilMarketNorth ?? 0; G.garbageMarket += G.garbageMarketNorth ?? 0; delete G.coalMarketNorth; delete G.oilMarketNorth; delete G.garbageMarketNorth; delete G.coalResupplyNorth; delete G.oilResupplyNorth; delete G.garbageResupplyNorth; delete G.coalPricesNorth; delete G.oilPricesNorth; delete G.garbagePricesNorth; delete G.resourceResupplyNorth; G.chosenSide = undefined; G.log.push({ type: 'event', event: 'Step 3: the North and Central resource markets merge into one.' }); const gates = G.map.step3Connections ?? []; G.map = { ...G.map, connections: [...G.map.connections, ...gates] }; delete G.map.step3Connections; if (gates.length > 0) { G.log.push({ type: 'event', event: 'Step 3: the gated connections open.' }); } if (G.setAsidePlants?.length) { // A replay from known draws (secret seed) already holds these plants in its Step 3 deck. if (G.powerPlantDeckAfterStep3 === undefined) G.powerPlantsDeck = [...G.setAsidePlants, ...G.powerPlantsDeck]; G.log.push({ type: 'event', event: `Step 3: power plants ${G.setAsidePlants.map((pp) => pp.number).join(', ')} go on top of the deck.`, }); } delete G.setAsidePlants; // From the printed value, so boosting twice can't add two. const boost = (plants: PowerPlant[]) => plants.map((pp) => pp.type == PowerPlantType.Wind ? { ...pp, citiesPowered: (plantCatalogue('Chile', gameDeck(G.options)).find((c) => c.number == pp.number) ?.citiesPowered ?? pp.citiesPowered) + 1, } : pp ); G.powerPlantsDeck = boost(G.powerPlantsDeck); G.actualMarket = boost(G.actualMarket); G.futureMarket = boost(G.futureMarket); G.players.forEach((player) => (player.powerPlants = boost(player.powerPlants))); if (G.chosenPowerPlant) G.chosenPowerPlant = boost([G.chosenPowerPlant])[0]; if (G.powerPlantDeckAfterStep3) G.powerPlantDeckAfterStep3 = boost(G.powerPlantDeckAfterStep3); G.log.push({ type: 'event', event: 'Step 3: every ecological power plant now powers one more city.' }); } export function rebuildPlantMarketForChina(G: GameState) { /*At the beginning of phase 5, the players fill the power plant market with new power plants. Depending on the number of players, the players always add a minimum of 1, 2, or 3 power plants to the market from the supply: with 2 and 3 players, add at least 1 power plant. with 4 and 5 players, add at least 2 power plants. with 6 players, add at least 3 power plants. The players add more than the minimum if the number of plants in the market is still more than 1 less than the number of players. Exception: with 2 players, add plants until there are 2 in the market.*/ let minPlantsToAdd = 0; if (G.players.length == 2 || G.players.length == 3) { minPlantsToAdd = 1; } else if (G.players.length == 4 || G.players.length == 5) { minPlantsToAdd = 2; } else if (G.players.length == 6) { minPlantsToAdd = 3; } const currentActualSize = G.actualMarket.length; const minSize = G.players.length - 1; const numPlantsToAdd = Math.max(minPlantsToAdd, minSize - currentActualSize); for (let i = 0; i < numPlantsToAdd; i++) { if (G.step == 3) { break; } else { addPowerPlant(G); } } // Special rule to move the market for two players // Viewer previews have a hidden (empty) deck: addPowerPlant cannot grow the market there. while (G.players.length == 2 && G.actualMarket.length < 2 && G.step != 3 && G.powerPlantsDeck.length > 0) { addPowerPlant(G); } if (G.step == 3) { G.actualMarket = G.actualMarket.filter((pp) => pp.type != PowerPlantType.Step3); // RIO 573: "in Step 3, there are always 4 power plants in the power plant market // regardless of the number of players." Steps 1-2 hold the market at players-1, // which is FIVE at six players, so the Step 3 branch has to trim down as well as // fill up. Smallest-first, matching the step 1/2 branch below and the rulebook's // "remove the smallest power plant from the market". while (G.actualMarket.length > 4) { G.actualMarket.shift(); } while (G.actualMarket.length < 4 && G.powerPlantsDeck.length > 0) { addPowerPlant(G); } } else { // Target size is one less than number of players, or 2 for a 2-player game. const targetSize = Math.max(2, G.players.length - 1); while (G.actualMarket.length > targetSize) { G.actualMarket.shift(); } } } /** * Discard just enough of one resource to fit the player's storage, returning the cubes * to the supply. * * The over-capacity test is `capacity + hybrid < held + otherOverflow`, so the largest * legal holding is `capacity + hybrid - otherOverflow`. The DiscardResources handler * used to step down by a single cube instead, which left a player who was over by two * still over by one — they get prompted again later, and that later discard used to * draw a spurious plant into the market (#67). It also dropped the cubes on the floor * rather than returning them to the supply. */ function discardDownToCapacity(G: GameState, player: Player, resource: ResourceType) { if (resource == ResourceType.Coal) { G.coalSupply += Math.max(0, player.coalLeft - player.coalCapacity); player.coalLeft = Math.min(player.coalLeft, player.coalCapacity); } else if (resource == ResourceType.Oil) { G.oilSupply += Math.max(0, player.oilLeft - player.oilCapacity); player.oilLeft = Math.min(player.oilLeft, player.oilCapacity); } } /** * Garbage/oil hybrid counterpart of discardDownToCapacity: discard just enough of one * resource for all of the player's fuel to fit, using the room left in the hybrid * tanks, and return the cubes to the supply. */ function discardDownToStorable(G: GameState, player: Player, resource: ResourceType) { const keep = maxStorable(player, resource); if (resource == ResourceType.Coal) { G.coalSupply += player.coalLeft - keep; player.coalLeft = keep; } else if (resource == ResourceType.Oil) { G.oilSupply += player.oilLeft - keep; player.oilLeft = keep; } else if (resource == ResourceType.Garbage) { G.garbageSupply += player.garbageLeft - keep; player.garbageLeft = keep; } } /** * Draw the replacement plant a completed purchase is owed — and only then. * * `endAuction` marks the purchase; whichever path finishes it consumes the mark. The * mark is what was missing: available-moves offers DiscardResources to ANY player over * capacity during the auction phase, not just a buyer, so a player merely carrying a * resource their plants can no longer hold used to complete a discard and pull a free * plant into the market. On China that surfaced as a fifth plant in a Step 3 market the * rulebook pins at four (#67); elsewhere it inflated the future market instead. */ function refillMarketAfterPurchase(G: GameState) { if (!G.pendingMarketRefill) { return; } G.pendingMarketRefill = false; // China refills once per round in Bureaucracy while in steps 1-2 (see // rebuildPlantMarketForChina), not per purchase. if (G.map.name != 'China' || G.step == 3) { addPowerPlant(G); } } function removePowerPlant(G: GameState, powerPlant: PowerPlant) { G.actualMarket.splice( G.actualMarket.findIndex((pp) => pp.number == powerPlant.number), 1 ); } export function getPowerPlant(num: number, mapName = '', deck: PowerPlantDeck = 'standard'): PowerPlant { return plantCatalogue(mapName, deck).find((p) => p.number == num)!; } // Options for rebuilding an existing game's starting state. Settings that new games // record explicitly must fall back to their old behaviour when absent, so games // created before them replay as they were played. A rule fixed without a stamp is // recognised from the game's own log instead: a $0 garbage buy can only have // happened under the old Wien discount. export function reconstructionOptions(options: GameOptions, log: LogItem[] = []): GameOptions { const wienFreeGarbage = options.wienFreeGarbage || log.some((item) => item.type == 'move' && /buys garbage for \$0\.$/.test(item.simple)); return { ...options, rechargedStartingResources: options.rechargedStartingResources ?? false, stallEnds: options.stallEnds ?? false, step2BeforeStep3: options.step2BeforeStep3 ?? false, shuffledSample: options.shuffledSample ?? false, legacyEcological: options.fusionEcological === undefined, ...(wienFreeGarbage ? { wienFreeGarbage: true } : {}), }; } function getBaseState(G: GameState): GameState { const baseState = setup( G.players.length, reconstructionOptions(G.options, G.log), G.seed, undefined, undefined, G.setupPlayerColors ); baseState.players.forEach((player, i) => { player.name = G.players[i].name; player.isAI = G.players[i].isAI; }); return baseState; } function playerNameHTML(player) { return `${player.name}`; } export function playersSortedByScore(G: GameState): Player[] { return cloneDeep(G.players) .sort((p1, p2) => { if (p1.citiesPowered == p2.citiesPowered) { if (p1.money == p2.money) { return p1.cities.length - p2.cities.length; } return p1.money - p2.money; } return p1.citiesPowered - p2.citiesPowered; }) .reverse(); } function calculateCitiesPowered(G: GameState) { G.players.forEach((player) => { player.citiesPowered = calculateMaxCitiesPowered(G, player); }); } export function calculateMaxCitiesPowered(G: GameState, player: Player) { // Australia's uranium mines never power cities, so they are excluded from the // powering combinations (this also keeps the 2^n permutation set smaller). const countablePlants = player.powerPlants.filter((pp) => !isUraniumMine(G, pp)); const permutations: PowerPlant[][] = []; for (let i = 0; i < Math.pow(2, countablePlants.length); i++) { const perm: PowerPlant[] = []; countablePlants.forEach((pp, index) => { if (i & Math.pow(2, index)) { perm.push(pp); } }); permutations.push(perm); } let max = 0; permutations.forEach((permutation) => { if (isValid(player, permutation)) { const citiesPowered = permutation.map((p) => p.citiesPowered).reduce((a, b) => a + b, 0); max = Math.max(max, citiesPowered); } }); return Math.min(player.cities.length, max); } function isValid(player: Player, powerPlants: PowerPlant[]) { const coalUsed = powerPlants .filter((pp) => pp.type == PowerPlantType.Coal) .map((pp) => pp.cost) .reduce((a, b) => a + b, 0); const oilUsed = powerPlants .filter((pp) => pp.type == PowerPlantType.Oil) .map((pp) => pp.cost) .reduce((a, b) => a + b, 0); const garbageUsed = powerPlants .filter((pp) => pp.type == PowerPlantType.Garbage) .map((pp) => pp.cost) .reduce((a, b) => a + b, 0); const uraniumUsed = powerPlants .filter((pp) => pp.type == PowerPlantType.Uranium) .map((pp) => pp.cost) .reduce((a, b) => a + b, 0); const hybridUsed = powerPlants .filter((pp) => pp.type == PowerPlantType.Hybrid) .map((pp) => pp.cost) .reduce((a, b) => a + b, 0); if ( coalUsed > player.coalLeft || oilUsed > player.oilLeft || garbageUsed > player.garbageLeft || uraniumUsed > player.uraniumLeft ) { return false; } if (hybridUsed > player.coalLeft - coalUsed + player.oilLeft - oilUsed) { return false; } // Garbage/oil hybrids draw on the leftover garbage and oil, sharing that oil with // any coal/oil hybrids: each kind must fit its own fuels, and both together must // fit everything that is left. const garbageOilUsed = powerPlants .filter((pp) => pp.type == PowerPlantType.GarbageOilHybrid) .map((pp) => pp.cost) .reduce((a, b) => a + b, 0); if (garbageOilUsed > 0) { const coalRemaining = player.coalLeft - coalUsed; const oilRemaining = player.oilLeft - oilUsed; const garbageRemaining = player.garbageLeft - garbageUsed; if ( garbageOilUsed > garbageRemaining + oilRemaining || hybridUsed + garbageOilUsed > coalRemaining + oilRemaining + garbageRemaining ) { return false; } } return true; } function toResourcesPhase(G: GameState) { if (G.map.name == 'Baden-Württemberg') { // Baden-Württemberg: player order is determined AFTER the auction phase, every round. setPlayerOrder(G); } else if (G.round == 1) { // Round 1 starts in seating order; once the auction ends, reorder by the // plants just bought (there are no cities yet). Done here — the single funnel // into the resources phase — so EVERY auction-completion path reorders, not // just the uncontested ChoosePowerPlant branch. Contested bids and Manhattan's // discount-bonus purchase otherwise reach this point with the seating order // intact (e.g. the first player keeps moving first regardless of plants bought). setPlayerOrder(G); } G.players.forEach((p) => { p.bid = 0; p.passed = p.isDropped; }); G.players.forEach((p) => { p.skipAuction = false; }); G.discountBonusPlayer = undefined; if (G.options.variant == 'recharged') { if (G.plantDiscountActive) { G.plantDiscountActive = false; if (G.actualMarket.length > 0) { G.log.push({ type: 'event', event: `Discarding Power Plant ${plantLabel(G.actualMarket[0].number)}.` }); G.actualMarket.shift(); addPowerPlant(G); } } else if (G.map.name == 'Benelux') { G.discardSmallestPlant = true; } } if (G.futureMarket.find((pp) => pp.number == 99)) { if (G.map.name == 'Middle East' && G.step == 1) { enterStepTwoMiddleEast(G); } else { if (step2BeforeStep3(G)) { // Step 2's setup, then Step 3's, which shuffles the draw pile again. beginStep2(G, 'Starting Step 2 (the Step 3 card came first)'); G.powerPlantsDeck = shuffle(G.powerPlantsDeck, G.seed); } const powerPlantDiscarded = G.actualMarket.shift(); G.futureMarket.pop(); startStep(G, 3, `Starting Step 3, Power Plant ${plantLabel(powerPlantDiscarded?.number)} discarded.`); applyAustraliaStep3Shift(G); applyChileStep3(G); G.actualMarket = [...G.actualMarket, ...G.futureMarket]; G.futureMarket = []; } } G.phase = Phase.Resources; setCurrentPlayer(G, G.playerOrder[G.players.length - 1]); } export function endAuction(G: GameState, winningPlayer: Player, bid: number) { winningPlayer.powerPlants.push(G.chosenPowerPlant!); winningPlayer.money -= bid; if (G.options.trackTotalSpent) { winningPlayer.totalSpentPlants += bid; } // Manhattan: buying the discounted power plant (whose minimum bid was lowered to // 1) lets the buyer purchase one more plant this phase. Keep them eligible // (skipAuction stays false) and remember they are owed the extra purchase, so // they may also decline it via Pass even in round 1 — when buying is otherwise // mandatory. minimunBid == 1 uniquely marks the discount auction (a normal bid // floor is the plant number, always >= 3). if ( G.map.name == 'Manhattan' && G.options.variant == 'recharged' && G.minimunBid == 1 && !winningPlayer.isDropped ) { G.discountBonusPlayer = winningPlayer.id; } else { winningPlayer.skipAuction = true; // If this win was the bonus purchase itself, the buyer is now done. if (G.discountBonusPlayer == winningPlayer.id) { G.discountBonusPlayer = undefined; } } updatePlayerCapacity(winningPlayer); G.log.push({ type: 'event', event: `${winningPlayer.name} wins the bid and pays ${bid}.`, pretty: `${playerNameHTML(winningPlayer)} wins the bid and pays $${bid}.`, }); removePowerPlant(G, G.chosenPowerPlant!); G.chosenPowerPlant = G.currentBid = undefined; // This purchase is owed exactly one replacement plant. Whichever path finishes the // purchase draws it — immediately, or after the buyer discards down to the plant // limit — and consumes the mark. See refillMarketAfterPurchase. G.pendingMarketRefill = true; } function setPlayerOrder(G: GameState) { G.playerOrder = cloneDeep(G.players) .sort((a, b) => { const citiesA = a.cities.length; const citiesB = b.cities.length; if (citiesA == citiesB) { return ( Math.max(...a.powerPlants.map((pp) => pp.number)) - Math.max(...b.powerPlants.map((pp) => pp.number)) ); } return citiesA - citiesB; }) .map((p) => p.id) .reverse(); } function setCurrentPlayer(G: GameState, playerNum: number) { G.currentPlayers = [playerNum]; if (G.players[playerNum].isDropped && G.players.some((p) => !p.isDropped)) { G.players[playerNum].passed = true; nextPlayer(G); } } function nextPlayer(G: GameState) { if (G.phase == Phase.Auction) { if (G.chosenPowerPlant == undefined) { nextPlayerAuction(G); } else { nextPlayerClockwise(G); } } else { nextPlayerReverse(G); } } function nextPlayerClockwise(G: GameState) { const index = G.currentPlayers[0]; G.currentPlayers = [(index + 1) % G.players.length]; if (G.players[G.currentPlayers[0]].isDropped && G.players.some((p) => !p.isDropped)) { G.players[G.currentPlayers[0]].passed = true; G.players[G.currentPlayers[0]].skipAuction = true; nextPlayerClockwise(G); } if ( (G.players[G.currentPlayers[0]].skipAuction || G.players[G.currentPlayers[0]].passed) && G.players.some((p) => !p.skipAuction && !p.passed && !p.isDropped) ) { nextPlayerClockwise(G); } } function nextPlayerReverse(G: GameState) { const index = G.playerOrder.indexOf(G.currentPlayers[0]); G.currentPlayers = [G.playerOrder[(index - 1 + G.players.length) % G.players.length]]; if (G.players[G.currentPlayers[0]].isDropped && G.players.some((p) => !p.isDropped)) { G.players[G.currentPlayers[0]].passed = true; nextPlayerReverse(G); } } function nextPlayerAuction(G: GameState, reset = false) { let playerNum: number; if (reset) { playerNum = G.playerOrder[0]; } else { const index = G.playerOrder.indexOf(G.currentPlayers[0]); playerNum = G.playerOrder[(index + 1) % G.players.length]; } G.currentPlayers = [playerNum]; const player = G.players[playerNum]; if (player.isDropped) { player.passed = true; player.skipAuction = true; } if ((player.skipAuction || player.passed) && G.players.some((p) => !p.skipAuction && !p.passed && !p.isDropped)) { nextPlayerAuction(G); } } function updateGameState(G: GameState) { if (!G.coalResupply) { const map = maps.find((map) => map.name === G.map.name); if (map?.resupply) { G.coalResupply = map.resupply[0]; G.oilResupply = map.resupply[1]; G.garbageResupply = map.resupply[2]; G.uraniumResupply = map.resupply[3]; } else { G.coalResupply = [ [3, 4, 3], [4, 5, 3], [5, 6, 4], [5, 7, 5], [7, 9, 6], ]; G.oilResupply = [ [2, 2, 4], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], ]; G.garbageResupply = [ [1, 2, 3], [1, 2, 3], [2, 3, 4], [3, 3, 5], [3, 5, 6], ]; G.uraniumResupply = [ [1, 1, 1], [1, 1, 1], [1, 2, 2], [2, 3, 2], [2, 3, 3], ]; } const p = G.players.length - 2; G.resourceResupply = [ `[${G.coalResupply[p][0]}, ${G.oilResupply[p][0]}, ${G.garbageResupply[p][0]}, ${G.uraniumResupply[p][0]}]`, `[${G.coalResupply[p][1]}, ${G.oilResupply[p][1]}, ${G.garbageResupply[p][1]}, ${G.uraniumResupply[p][1]}]`, `[${G.coalResupply[p][2]}, ${G.oilResupply[p][2]}, ${G.garbageResupply[p][2]}, ${G.uraniumResupply[p][2]}]`, ]; if (G.coalResupplyNorth && G.oilResupplyNorth && G.garbageResupplyNorth) { G.resourceResupplyNorth = [ `[${G.coalResupplyNorth[p][0]}, ${G.oilResupplyNorth[p][0]}, ${G.garbageResupplyNorth[p][0]}]`, `[${G.coalResupplyNorth[p][1]}, ${G.oilResupplyNorth[p][1]}, ${G.garbageResupplyNorth[p][1]}]`, `[${G.coalResupplyNorth[p][2]}, ${G.oilResupplyNorth[p][2]}, ${G.garbageResupplyNorth[p][2]}]`, ]; } } } function fastAuction(G: GameState, player: Player, bid: number) { player.bid = bid; G.currentPlayers = G.currentPlayers.filter((id) => id !== player.id); if (G.currentPlayers.length === 0) { // Reveal simultaneous responses in fixed seat order, not submission order // (nor the changing playerOrder used for auction nominations). G.log.push( ...G.hiddenLog.slice().sort((a, b) => (a.type === 'move' && b.type === 'move' ? a.player - b.player : 0)) ); G.hiddenLog = []; const bids = G.players.map((p) => p.bid).filter((b) => b > 0); let cost = G.minimunBid; const highestBid = Math.max(...bids); const highestBidders = G.players.filter((p) => !p.isDropped && p.bid === highestBid); let winnerId = highestBidders[0].id; if (bids.length > 1) { bids.splice(bids.indexOf(highestBid), 1); const secondHighestBid = Math.max(...bids); // In case of a tie, use turn order if (highestBidders.length > 1) { let index = G.auctioningPlayer!; while (!highestBidders.find((p) => p.id == index)) { index = (index + 1) % G.players.length; } cost = secondHighestBid; winnerId = index; } else { let index = G.auctioningPlayer!; cost = 0; while (cost == 0) { if (highestBidders[0].id == index) { cost = secondHighestBid; } else if (G.players[index].bid == secondHighestBid) { cost = secondHighestBid + 1; } index = (index + 1) % G.players.length; } } } const winningPlayer = G.players[winnerId]; endAuction(G, winningPlayer, cost); if ( (countHeldPowerPlants(G, winningPlayer) > 4 || (G.players.length > 2 && countHeldPowerPlants(G, winningPlayer) > 3)) && !winningPlayer.isDropped ) { setCurrentPlayer(G, winningPlayer.id); } else { refillMarketAfterPurchase(G); if (G.players.some((p) => !p.skipAuction && !p.isDropped)) { G.players.forEach((p) => { p.bid = 0; p.passed = p.isDropped; }); nextPlayerAuction(G, true); } else { toResourcesPhase(G); } } } } /** City income is shared with the current-round planner. Australia mine sales resolve separately. */ export function powerIncome(G: GameState, player: Player): number { let payment = G.paymentTable[Math.min(player.cities.length, player.citiesPowered)]; if (G.map.name === 'India' && G.citiesBuiltInCurrentRound! > G.players.length * 2) { payment = Math.max(0, payment - 3 * player.cities.length); } return payment; }