From a221fee92146caaefdee8a287e341a18da11716b Mon Sep 17 00:00:00 2001 From: evanpelle Date: Thu, 17 Jul 2025 19:21:27 -0700 Subject: [PATCH] Have port destination likelihood scale with level (#1473) ## Description: Having a high level port would increase the number of outgoing tradeships, but not the number of incoming tradeships. Now the the chances of a tradeship landing on a port is scaled with the port's level ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I have read and accepted the CLA aggreement (only required once). ## Please put your Discord username so you can be contacted if a bug or regression is found: evan --- src/core/game/PlayerImpl.ts | 28 ++++++++++++---------------- tests/PlayerImpl.test.ts | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index 4a019c983..20222e5a3 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -1165,23 +1165,19 @@ export class PlayerImpl implements Player { ); }); - // Make close ports twice more likely by putting them again - for ( - let i = 0; - i < this.mg.config().proximityBonusPortsNb(ports.length); - i++ - ) { - ports.push(ports[i]); + const weightedPorts: Unit[] = []; + + for (const [i, otherPort] of ports.entries()) { + const expanded = new Array(otherPort.level()).fill(otherPort); + weightedPorts.push(...expanded); + if (i < this.mg.config().proximityBonusPortsNb(ports.length)) { + weightedPorts.push(...expanded); + } + if (port.owner().isFriendly(otherPort.owner())) { + weightedPorts.push(...expanded); + } } - // Make ally ports twice more likely by putting them again - this.mg - .players() - .filter((p) => p !== port.owner() && p.canTrade(port.owner())) - .filter((p) => p.isAlliedWith(port.owner())) - .flatMap((p) => p.units(UnitType.Port)) - .forEach((p) => ports.push(p)); - - return ports; + return weightedPorts; } } diff --git a/tests/PlayerImpl.test.ts b/tests/PlayerImpl.test.ts index a877993da..24b02e735 100644 --- a/tests/PlayerImpl.test.ts +++ b/tests/PlayerImpl.test.ts @@ -9,6 +9,7 @@ import { setup } from "./util/Setup"; let game: Game; let player: Player; +let other: Player; describe("PlayerImpl", () => { beforeEach(async () => { @@ -17,7 +18,10 @@ describe("PlayerImpl", () => { { instantBuild: true, }, - [new PlayerInfo("player", PlayerType.Human, null, "player_id")], + [ + new PlayerInfo("player", PlayerType.Human, null, "player_id"), + new PlayerInfo("other", PlayerType.Human, null, "other_id"), + ], ); while (game.inSpawnPhase()) { @@ -26,6 +30,7 @@ describe("PlayerImpl", () => { player = game.player("player_id"); player.addGold(BigInt(1000000)); + other = game.player("other_id"); game.config().structureMinDist = () => 10; }); @@ -77,4 +82,20 @@ describe("PlayerImpl", () => { ); expect(cityToUpgrade).toBe(false); }); + + test("Destination ports chances scale with level", () => { + game.config().proximityBonusPortsNb = () => 0; + + player.conquer(game.ref(10, 10)); + const playerPort = player.buildUnit(UnitType.Port, game.ref(10, 10), {}); + + other.conquer(game.ref(0, 0)); + const otherPort = other.buildUnit(UnitType.Port, game.ref(0, 0), {}); + otherPort.increaseLevel(); + otherPort.increaseLevel(); + + const ports = player.tradingPorts(playerPort); + + expect(ports.length).toBe(3); + }); });