diff --git a/.vscode/launch.json b/.vscode/launch.json index baab0182..490847a1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,7 @@ "type": "node", "request": "launch", "program": "${workspaceRoot}/engine/node_modules/ts-node/dist/bin.js", - "args": ["${relativeFile}"], + "args": ["../${relativeFile}"], "cwd": "${workspaceRoot}/engine", "protocol": "inspector" }, @@ -24,7 +24,7 @@ "type": "node", "request": "launch", "program": "${workspaceRoot}/engine/node_modules/mocha/bin/_mocha", - "args": ["-r", "ts-node/register", "${relativeFile}"], + "args": ["-r", "ts-node/register", "../${relativeFile}"], "cwd": "${workspaceRoot}/engine", "protocol": "inspector" } diff --git a/engine/src/algorithms/spanning-tree.ts b/engine/src/algorithms/spanning-tree.ts index 94403545..44a5e489 100644 --- a/engine/src/algorithms/spanning-tree.ts +++ b/engine/src/algorithms/spanning-tree.ts @@ -1,7 +1,6 @@ -import { inRange, minBy, maxBy, uniq, difference, sortBy } from "lodash"; +import { inRange, minBy, maxBy, uniq, difference, sumBy } from "lodash"; import { Grid, Hex } from "hexagrid"; import shortestPath from "./shortest-path"; -import { sumBy } from "lodash"; import assert from "assert"; import minimumPathLength from "./minimum-path-length"; @@ -28,7 +27,7 @@ function spanningTreeWithHeuristic( destGroups: Hex[][], grid: Grid>, maxAdditional = -1, - costOf = (hex: Hex) => 1 + costOf = (hex: Hex) => 1 ): { path: Hex[]; cost: number } | undefined { type DestGroup = Hex[]; @@ -76,7 +75,8 @@ function spanningTreeWithHeuristic( if (destGroupsMap.has(hex)) { return destGroupsMap.get(hex); } else { - return [hex]; + // If it's an empty hex next to buildings, include those buildings + return uniq([hex, ...grid.neighbours(hex).map((nb) => destGroupsMap.get(nb) ?? [])].flat(1)); } }; @@ -90,7 +90,7 @@ function spanningTreeWithHeuristic( do { const hexSet = new Set>(hexes); - const path = shortestPath(hexes, toReach, grid, (hex) => (hexSet.has(hex) ? 0 : costOf(hex))); + const path = shortestPath(hexes, toReach, grid, (hex) => (hexSet.has(hex) ? 0 : costOf(hex))); if (!path) { hexes = undefined; diff --git a/engine/src/federation.ts b/engine/src/federation.ts index a0315345..ea635269 100644 --- a/engine/src/federation.ts +++ b/engine/src/federation.ts @@ -5,6 +5,7 @@ export interface FederationInfo { hexes: GaiaHex[]; planets: number; satellites: number; + newSatellites: number; } /** diff --git a/engine/src/player.spec.ts b/engine/src/player.spec.ts index 07308591..1d0722d4 100644 --- a/engine/src/player.spec.ts +++ b/engine/src/player.spec.ts @@ -1,11 +1,10 @@ import { expect } from "chai"; import "mocha"; import Player from "./player"; -import { BrainstoneArea, Building, Command, Faction, Operator, Planet, Player as PlayerEnum, Resource } from "./enums"; +import { Building, Faction, Operator, Planet, Player as PlayerEnum, Resource } from "./enums"; import Reward from "./reward"; import Event from "./events"; import { GaiaHex } from "./gaia-hex"; -import { Power } from "./player-data"; describe("Player", () => { describe("canBuild", () => { diff --git a/engine/src/player.ts b/engine/src/player.ts index 193638c9..cf469261 100644 --- a/engine/src/player.ts +++ b/engine/src/player.ts @@ -862,7 +862,10 @@ export default class Player extends EventEmitter { (hex) => !excluded.has(hex) && (!flexible || !flexibleExcluded.has(hex)) ); const workingGrid = new Grid>( - ...allHexes.map((hex) => new Hex(hex.q, hex.r, { cost: occupiedSet.has(hex) ? 0 : 1 })) + ...allHexes.map( + (hex) => + new Hex(hex.q, hex.r, { cost: occupiedSet.has(hex) || hex.belongsToFederationOf(this.player) ? 0 : 1 }) + ) ); const convertedDestGroups = destGroups.map((destGroup) => destGroup.map((hex) => workingGrid.get(hex))); let tree = spanningTree(convertedDestGroups, workingGrid, maxSatellites, "heuristic", (hex) => hex.data.cost); @@ -872,7 +875,10 @@ export default class Player extends EventEmitter { // as they don't add additional satellites const allHexes = [...map.grid.values()].filter((hex) => !excluded.has(hex) && !flexibleExcluded.has(hex)); const workingGrid = new Grid>( - ...allHexes.map((hex) => new Hex(hex.q, hex.r, { cost: occupiedSet.has(hex) ? 0 : 1 })) + ...allHexes.map( + (hex) => + new Hex(hex.q, hex.r, { cost: occupiedSet.has(hex) || hex.belongsToFederationOf(this.player) ? 0 : 1 }) + ) ); const convertedDestGroups = destGroups.map((destGroup) => destGroup.map((hex) => workingGrid.get(hex))); const treeWithoutOtherPlanets = spanningTree( @@ -926,7 +932,7 @@ export default class Player extends EventEmitter { } } - const feds = difference(fedsWithInfo, toRemove).filter((fed) => fed.satellites <= maxSatellites); + const feds = difference(fedsWithInfo, toRemove).filter((fed) => fed.newSatellites <= maxSatellites); this.federationCache = { availableSatellites: maxSatellites, @@ -955,6 +961,7 @@ export default class Player extends EventEmitter { return { hexes: federation, satellites: satellites.length, + newSatellites: satellites.filter((sat) => !sat.belongsToFederationOf(this.player)).length, planets: nPlanets, }; } @@ -1024,7 +1031,12 @@ export default class Player extends EventEmitter { const occupiedSet = new Set(this.data.occupied); const workingGrid = new Grid>( - ...allHexes.map((hex) => new Hex(hex.q, hex.r, { cost: occupiedSet.has(hex) ? 0 : 1 })) + ...allHexes.map( + (hex) => + new Hex(hex.q, hex.r, { + cost: occupiedSet.has(hex) || hex.belongsToFederationOf(this.player) ? 0 : 1, + }) + ) ); const allGroups = [ ...this.buildingGroups( diff --git a/engine/src/tiles/federations.spec.ts b/engine/src/tiles/federations.spec.ts index 2e656f1c..b625b185 100644 --- a/engine/src/tiles/federations.spec.ts +++ b/engine/src/tiles/federations.spec.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; import Engine from "../engine"; -import { Player } from "../enums"; +import { Command, Player } from "../enums"; const parseMoves = Engine.parseMoves; @@ -54,4 +54,210 @@ describe("Federation", () => { // Test other federation with the same buildings expect(() => new Engine([...moves, "p1 federation -1x2,-2x3,-3x3,-3x4,-4x2,-4x3 fed2"])).to.not.throw(); }); + + it("should offer one choice for Ivits in this game", () => { + const moves = [ + "init 4 2021-02-08-01", + "p4 rotate -2x-3 3 -1x8 2 -6x10 4", + "p1 faction ambas", + "p2 faction ivits", + "p3 faction terrans", + "p4 faction firaks", + "ambas build m 7A7", + "terrans build m 10A6", + "firaks build m 8B0", + "firaks build m 7A0", + "terrans build m 4A4", + "ambas build m 4B2", + "ivits build PI 10A5", + "firaks booster booster4", + "terrans booster booster1", + "ivits booster booster9", + "ambas booster booster5", + "ivits income 4pw. income 4pw", + "ambas special range+3. build m 10B1.", + "ivits charge 2pw", + "ivits action power3.", + "terrans build gf 1A9.", + "firaks build ts 7A0.", + "ambas charge 1pw", + "terrans charge 1pw", + "ambas build ts 4B2.", + "terrans charge 1pw", + "firaks charge 2pw", + "ivits build m 7B5.", + "firaks charge 2pw", + "ambas charge 1pw", + "terrans build ts 4A4.", + "firaks charge 2pw", + "ambas charge 2pw", + "firaks action power4.", + "ambas action power5.", + "ivits build ts 7B5.", + "firaks charge 2pw", + "ambas charge 1pw", + "terrans up gaia.", + "firaks build lab 7A0. tech free2. up terra.", + "ambas charge 2pw", + "ivits charge 2pw", + "terrans charge 2pw", + "ambas build lab 4B2. tech free2. up terra.", + "terrans charge 2pw", + "firaks charge 2pw", + "ivits build lab 7B5. tech free2. up terra.", + "firaks charge 2pw", + "ambas charge 1pw", + "terrans build PI 4A4.", + "firaks charge 2pw", + "ambas charge 2pw", + "firaks build ts 8B0.", + "ambas charge 1pw", + "ivits charge 3pw", + "terrans charge 1pw", + "ambas spend 3pw for 1o. burn 1. spend 1pw for 1c. build ac1 4B2. tech nav. up nav.", + "terrans charge 1pw", + "firaks charge 2pw", + "ivits special space-station. build sp 7C.", + "terrans spend 2pw for 2c. pass booster8", + "firaks spend 3pw for 1o. spend 3pw for 1o. build PI 8B0.", + "ambas charge 1pw", + "ivits charge 3pw", + "terrans charge 1pw", + "ambas up terra.", + "ivits spend 1pw for 1c. spend 6pw for 2o. build ac1 7B5. tech nav. up nav.", + "firaks charge 2pw", + "ambas charge 1pw", + "firaks special down-lab. build ts 7A0. up nav.", + "ambas charge 3pw", + "ivits charge 3pw", + "terrans charge 3pw", + "ambas pass booster10", + "ivits up terra.", + "firaks up nav.", + "ivits pass booster1", + "firaks spend 1q for 1o. build m 7B1.", + "ivits charge 3pw", + "firaks spend 1q for 1o. special step. build m 9A10.", + "firaks pass booster9", + "terrans income 1t", + "ivits income 4pw", + "firaks income 4pw. income 4pw", + "terrans spend 3tg for 1o. spend 3tg for 3c", + "terrans action power5.", + "ambas up terra.", + "ivits burn 2. action power4.", + "firaks action power3.", + "terrans build m 1A9.", + "firaks charge 2pw", + "ambas charge 1pw", + "ambas action power2. build m 1A11.", + "terrans charge 1pw", + "ivits build m 7B3.", + "firaks charge 1pw", + "ambas charge 1pw", + "firaks build lab 7A0. tech nav. up nav.", + "ambas charge 3pw", + "ivits charge 3pw", + "terrans charge 3pw", + "terrans build ts 10A6.", + "firaks charge 3pw", + "ivits charge 3pw", + "ambas build m 6A0.", + "ivits charge 1pw", + "ivits up terra. spend 3pw for 1o.", + "firaks spend 1pw for 1c. build m 9B0.", + "terrans build m 1B4.", + "firaks charge 2pw", + "ambas charge 1pw", + "ambas pass booster5", + "ivits build ts 7B3.", + "firaks charge 1pw", + "ambas charge 1pw", + "firaks special down-lab. build ts 7A0. up sci.", + "ambas decline 3pw", + "ivits charge 3pw", + "terrans charge 3pw", + "terrans up terra.", + "ivits spend 3pw for 1o. spend 2q for 2o. build lab 7B3. tech terra. up terra.", + "firaks charge 1pw", + "ambas charge 1pw", + "firaks spend 2pw for 2c. build m 9B2.", + "terrans spend 1pw for 1c. build lab 10A6. tech gaia. up gaia.", + "firaks decline 3pw", + "ivits charge 3pw", + "ivits special 4pw.", + "firaks pass booster10", + "terrans build gf 8A10.", + "ivits special space-station. build sp 10A4.", + "terrans spend 4pw for 4c. build gf 5A2.", + "ivits spend 3pw for 1o. spend 1pw for 1c. pass booster9", + "terrans pass booster1", + "firaks income 4pw", + "ivits income 4pw. income 4pw", + "terrans income 1t", + "terrans spend 3tg for 3c. spend 3tg for 1o. spend 2tg for 2c", + "ambas action power3.", + "firaks action power4.", + "ivits federation 10A4,10A5,7A9,7B3,7B5,7C fed5.", + "terrans build m 5A2.", + "ivits charge 3pw", + "ambas up nav.", + "firaks build ts 7B1.", + "ivits charge 1pw", + "ivits spend 3pw for 1o. up terra.", + "terrans build ts 1A9.", + "firaks decline 2pw", + "ambas charge 3pw", + "ambas special range+3. build m 1B2.", + "terrans charge 1pw", + "firaks federation 7A0,7A1,7A2,7B1,9A10,9A11,9B0,9B1,9B2 fed4.", + "ivits build ac2 7B3. tech adv-terra. cover free2. up nav.", + "firaks charge 2pw", + "ambas charge 1pw", + "terrans spend 3pw for 1o. spend 1pw for 1c. build lab 1A9. tech eco. up eco.", + "firaks charge 1pw", + "ambas charge 2pw", + "ambas spend 1pw for 1c. build ts 1A11.", + "terrans charge 2pw", + "firaks build lab 7B1. tech free3. up nav. spend 3pw for 1o.", + "ivits charge 3pw", + "ivits spend 1pw for 1c. special 4pw. spend 3pw for 1o.", + "terrans federation 1A9,1B4,4A4 fed4.", + "ambas build ts 1B2.", + "terrans charge 1pw", + "firaks up nav. lostPlanet 10A8.", + "terrans charge 2pw", + "ivits special space-station. build sp 8B5.", + "terrans up nav.", + "ambas action power7.", + "firaks up sci.", + "ivits build m 5A3.", + "terrans charge 2pw", + "terrans spend 2pw for 2c. build m 8A10.", + "firaks decline 3pw", + "ivits charge 3pw", + "ambas federation 1A10,1A11,1B2,1B5,1C,4A3,4B2 fed5.", + "firaks special down-lab. build ts 7B1. up sci.", + "ivits charge 3pw", + "ivits spend 3pw for 1o. spend 3pw for 1o. build m 8A8.", + "terrans charge 1pw", + "terrans build gf 6A9.", + "ambas build m 6B1.", + "firaks build m 2A6.", + "ivits special q.", + "terrans pass booster8", + "ambas build m 10B4.", + "ivits charge 3pw", + "terrans charge 2pw", + "firaks charge 1pw", + "firaks spend 2pw for 2c. pass booster7", + ]; + + const engine = new Engine(moves, { advancedRules: true }); + + engine.generateAvailableCommands(); + + expect(engine.findAvailableCommand(Player.Player2, Command.FormFederation)).to.not.be.undefined; + expect(engine.findAvailableCommand(Player.Player2, Command.FormFederation).data.federations.length).to.equal(1); + }); });