fix(engine): allow valid Lost Fleet custom-map rematches
This commit is contained in:
parent
de287bb96e
commit
5a7fdec4a0
4 changed files with 209 additions and 8 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@gaia-project/engine",
|
||||
"version": "4.17.10",
|
||||
"version": "4.17.11",
|
||||
"description": "Javascript engine for project gaia",
|
||||
"main": "dist/index.js",
|
||||
"types": "index.ts",
|
||||
|
|
|
|||
|
|
@ -158,6 +158,165 @@ describe("Map", () => {
|
|||
expect(() => map.isValid()).to.not.throw();
|
||||
});
|
||||
|
||||
for (const nbPlayers of [1, 2, 3, 4]) {
|
||||
it(`should reuse a generated ${nbPlayers}-player Lost Fleet map in a rematch`, () => {
|
||||
const seed = `rematch-lost-fleet-${nbPlayers}`;
|
||||
const original = new Engine([`init ${nbPlayers} ${seed}`], { lostFleet: true });
|
||||
const configuration = JSON.parse(JSON.stringify(original.options.map));
|
||||
const rematch = new Engine([`init ${nbPlayers} ${seed}`], { lostFleet: true, map: configuration });
|
||||
expect(rematch.options.map).to.deep.equal(original.options.map);
|
||||
expect(rematch.map.toJSON().map((hex) => hex.toJSON())).to.deep.equal(
|
||||
original.map.toJSON().map((hex) => hex.toJSON())
|
||||
);
|
||||
|
||||
// A new seed changes only the unrecorded Interspace/Deep Space tiles, not Space Sectors.
|
||||
const newSeed = `${seed}-new`;
|
||||
const next = new Engine([`init ${nbPlayers} ${newSeed}`], { lostFleet: true, map: configuration });
|
||||
const generated = new SpaceMap(nbPlayers, newSeed, false, "standard", true);
|
||||
const extras = (map: SpaceMap) =>
|
||||
map
|
||||
.toJSON()
|
||||
.filter((hex) => /^(IS|DS)/.test(hex.data.sector))
|
||||
.map((hex) => hex.toJSON());
|
||||
expect(next.options.map).to.deep.equal(configuration);
|
||||
expect(next.map.grid.size).to.equal(original.map.grid.size);
|
||||
expect(extras(next.map)).to.deep.equal(extras(generated));
|
||||
expect(next.map.isValid()).to.equal(true);
|
||||
for (const hex of next.map.grid.values()) {
|
||||
expect(next.map.getS(hex.toString())).to.equal(hex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it("should not apply legacy base-game mirroring to a Lost Fleet rematch", () => {
|
||||
const original = new Engine(["init 4 randomSeed"], { lostFleet: true });
|
||||
const rematch = new Engine(["init 4 randomSeed"], {
|
||||
lostFleet: true,
|
||||
map: JSON.parse(JSON.stringify(original.options.map)),
|
||||
});
|
||||
expect(rematch.map.toJSON().map((hex) => hex.toJSON())).to.deep.equal(
|
||||
original.map.toJSON().map((hex) => hex.toJSON())
|
||||
);
|
||||
});
|
||||
|
||||
it("should place sectors by their explicit centers, regardless of array order", () => {
|
||||
const original = new Engine(["init 4 reordered-rematch"], { lostFleet: true });
|
||||
const conf: MapConfiguration = JSON.parse(JSON.stringify(original.options.map));
|
||||
conf.sectors.reverse();
|
||||
const rematch = new Engine(["init 4 reordered-rematch"], { lostFleet: true, map: conf });
|
||||
expect(rematch.map.grid.size).to.equal(original.map.grid.size);
|
||||
for (const hex of original.map.grid.values()) {
|
||||
expect(rematch.map.grid.get(hex).toJSON()).to.deep.equal(hex.toJSON());
|
||||
}
|
||||
});
|
||||
|
||||
const invalidConfigurations: Array<[string, (conf: MapConfiguration) => void, string]> = [
|
||||
["missing sectors", (conf) => conf.sectors.pop(), "sector count"],
|
||||
[
|
||||
"empty sectors",
|
||||
(conf) => {
|
||||
conf.sectors = [];
|
||||
},
|
||||
"sector count",
|
||||
],
|
||||
[
|
||||
"duplicate sectors",
|
||||
(conf) => {
|
||||
conf.sectors[0].sector = conf.sectors[1].sector;
|
||||
},
|
||||
"duplicate Lost Fleet sector",
|
||||
],
|
||||
[
|
||||
"unknown sectors",
|
||||
(conf) => {
|
||||
conf.sectors[0].sector = "99";
|
||||
},
|
||||
"Invalid or duplicate Lost Fleet sector",
|
||||
],
|
||||
[
|
||||
"wrong sector face",
|
||||
(conf) => {
|
||||
conf.sectors.find((s) => s.sector === "5A").sector = "5B";
|
||||
},
|
||||
"Invalid or duplicate Lost Fleet sector",
|
||||
],
|
||||
[
|
||||
"missing centers",
|
||||
(conf) => {
|
||||
delete conf.sectors[0].center;
|
||||
},
|
||||
"sector center",
|
||||
],
|
||||
[
|
||||
"duplicate centers",
|
||||
(conf) => {
|
||||
conf.sectors[0].center = conf.sectors[1].center;
|
||||
},
|
||||
"sector center",
|
||||
],
|
||||
[
|
||||
"base-game centers",
|
||||
(conf) => {
|
||||
conf.sectors[0].center = { q: 5, r: -2, s: -3 };
|
||||
},
|
||||
"sector center",
|
||||
],
|
||||
[
|
||||
"inconsistent cube coordinates",
|
||||
(conf) => {
|
||||
conf.sectors[0].center.s += 1;
|
||||
},
|
||||
"sector center",
|
||||
],
|
||||
...[-1, 6, 0.5, NaN].map((rotation): [string, (conf: MapConfiguration) => void, string] => [
|
||||
`rotation ${rotation}`,
|
||||
(conf) => {
|
||||
conf.sectors[0].rotation = rotation;
|
||||
},
|
||||
"sector rotation",
|
||||
]),
|
||||
];
|
||||
for (const [name, invalidate, error] of invalidConfigurations) {
|
||||
it(`should reject Lost Fleet maps with ${name}`, () => {
|
||||
const conf: MapConfiguration = JSON.parse(
|
||||
JSON.stringify(new SpaceMap(4, "invalid-custom-map", false, "standard", true).placement)
|
||||
);
|
||||
invalidate(conf);
|
||||
expect(() => new Engine(["init 4 invalid-custom-map"], { lostFleet: true, map: conf })).to.throw(error);
|
||||
});
|
||||
}
|
||||
|
||||
it("should validate adjacency when loading custom Lost Fleet sectors", () => {
|
||||
const map = new SpaceMap(2, "lost-fleet-space-map", false, "standard", true);
|
||||
map.placement.sectors[0].rotation = (map.placement.sectors[0].rotation + 3) % 6;
|
||||
expect(() => new Engine(["init 2 lost-fleet-space-map"], { lostFleet: true, map: map.placement })).to.throw(
|
||||
"Map is invalid with two planets for the same type being near each other"
|
||||
);
|
||||
});
|
||||
|
||||
it("should honor the official-center restriction for supplied Lost Fleet maps", () => {
|
||||
const original = new Engine(["init 4 official-rematch"], { lostFleet: true, officialCenterSectors: true });
|
||||
expect(
|
||||
() =>
|
||||
new Engine(["init 4 official-rematch"], {
|
||||
lostFleet: true,
|
||||
officialCenterSectors: true,
|
||||
map: original.options.map,
|
||||
})
|
||||
).to.not.throw();
|
||||
const placements = original.options.map.sectors;
|
||||
const outer = placements.find((s) => s.sector === "10");
|
||||
[placements[0].sector, outer.sector] = [outer.sector, placements[0].sector];
|
||||
expect(
|
||||
() =>
|
||||
new Engine(["init 4 official-rematch"], {
|
||||
lostFleet: true,
|
||||
officialCenterSectors: true,
|
||||
map: original.options.map,
|
||||
})
|
||||
).to.throw("Lost Fleet center sectors must be drawn from sectors 1-4");
|
||||
});
|
||||
|
||||
it("should reject a legacy custom map configuration with Lost Fleet", () => {
|
||||
expect(
|
||||
() =>
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import seedrandom from "seedrandom";
|
|||
import { EngineOptions } from "./engine";
|
||||
import { Faction, Planet, Player } from "./enums";
|
||||
import { GaiaHex, reverseSuffixes } from "./gaia-hex";
|
||||
import { generateLostFleetBoard } from "./lost-fleet-board";
|
||||
import { generateLostFleetBoard, lostFleetSectorTiles } from "./lost-fleet-board";
|
||||
import { classifySectorId, lostFleetSectorCenters, LostFleetSectorType } from "./lost-fleet-map";
|
||||
import Sector from "./sector";
|
||||
import assert from "./utils/assert";
|
||||
|
|
@ -181,7 +181,11 @@ export default class SpaceMap {
|
|||
} while (!this.isValid(germanRules));
|
||||
}
|
||||
|
||||
load(conf: MapConfiguration) {
|
||||
load(conf: MapConfiguration, officialCenterSectors = false) {
|
||||
if (this.lostFleet) {
|
||||
this.loadLostFleet(conf, officialCenterSectors);
|
||||
return;
|
||||
}
|
||||
const centers = conf.sectors.length === 7 ? smallCenters : bigCenters;
|
||||
|
||||
// Legacy map generation, to keep old tests valid
|
||||
|
|
@ -223,6 +227,47 @@ export default class SpaceMap {
|
|||
this.placement = conf;
|
||||
}
|
||||
|
||||
/** Load Space Sectors without discarding the generated Interspace/Deep Space tiles.
|
||||
* Placement only records Space Sectors: the other tiles remain determined by the init seed.
|
||||
* Require the Lost Fleet geometry/pool, rather than falling back to legacy base-game centers.
|
||||
*/
|
||||
private loadLostFleet(conf: MapConfiguration, officialCenterSectors: boolean) {
|
||||
const centers = lostFleetSectorCenters(this.nbPlayers);
|
||||
const remainingTiles = new Set(lostFleetSectorTiles(this.nbPlayers).map((tile) => tile.name));
|
||||
const remainingCenters = new Set(centers.map((center) => `${center.q}x${center.r}`));
|
||||
assert(Array.isArray(conf.sectors) && conf.sectors.length === centers.length, "Invalid Lost Fleet sector count");
|
||||
const grids = conf.sectors.map((placement) => {
|
||||
assert(placement && remainingTiles.delete(placement.sector), "Invalid or duplicate Lost Fleet sector");
|
||||
const { center, rotation, sector } = placement;
|
||||
assert(
|
||||
center &&
|
||||
Number.isInteger(center.q) &&
|
||||
Number.isInteger(center.r) &&
|
||||
center.s === -center.q - center.r &&
|
||||
remainingCenters.delete(`${center.q}x${center.r}`),
|
||||
"Invalid or duplicate Lost Fleet sector center"
|
||||
);
|
||||
assert(Number.isInteger(rotation) && rotation >= 0 && rotation < 6, "Invalid Lost Fleet sector rotation");
|
||||
if (
|
||||
officialCenterSectors &&
|
||||
centers.slice(0, this.nbPlayers === 4 ? 2 : 1).some((c) => c.q === center.q && c.r === center.r)
|
||||
) {
|
||||
assert(["1", "2", "3", "4"].includes(sector), "Lost Fleet center sectors must be drawn from sectors 1-4");
|
||||
}
|
||||
const definition = (conf.mirror ? rSectors : sectors)[sector];
|
||||
return Sector.create(definition.map, sector, center).rotateRight(rotation, center);
|
||||
});
|
||||
const extraHexes = [...this.grid.values()].filter(
|
||||
(hex) => classifySectorId(hex.data.sector) !== LostFleetSectorType.Space
|
||||
);
|
||||
const candidate = new SpaceMap();
|
||||
candidate.grid = grids[0].merge(...grids.slice(1), new Grid(...extraHexes));
|
||||
assert(candidate.isValid(), "Map is invalid with two planets for the same type being near each other");
|
||||
this.grid = candidate.grid;
|
||||
this.placement = conf;
|
||||
this.distanceCache = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the map is correct (no two HOME planets of the same color side by side - following german rules)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -22,10 +22,7 @@ export function moveInit(engine: Engine, players: number, seed: string) {
|
|||
assert(players >= 1 && players <= 5, "Invalid number of players");
|
||||
const solo = players === 1;
|
||||
if (solo) initAutoma(engine);
|
||||
assert(
|
||||
!(engine.options.lostFleet && engine.options.map?.sectors),
|
||||
"A custom map configuration cannot be combined with the Lost Fleet expansion"
|
||||
);
|
||||
|
||||
if (engine.options.auction === AuctionVariant.PreferenceSplit) {
|
||||
// Checked at the earliest possible moment (a game that reached its bid phase before anyone
|
||||
// noticed would have no legal way forward). The faction count needs no check of its own: the
|
||||
|
|
@ -52,7 +49,7 @@ export function moveInit(engine: Engine, players: number, seed: string) {
|
|||
);
|
||||
|
||||
if (engine.options.map?.sectors) {
|
||||
engine.map.load(engine.options.map);
|
||||
engine.map.load(engine.options.map, engine.options.officialCenterSectors);
|
||||
}
|
||||
engine.options.map = engine.map.placement;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue