Discourage trading with nearby ports (#2381)

## Description:

The **proximityBonusPortsNb** function increased the likelihood a
tradeship would go to a nearby port. But now that trade gold is nerfed
from nearby ports, we shouldn't encourage trading with ports that are
too close. So now add another factor **tradeShipShortRangeDebuff** That
cancels out the proximity bonus if the port is too close.

Now tradeships are encouraged to go to ports that are close, but not too
close.

Also move tradingPorts method to the PortExecution class because that's
the only place it's used.

## 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

## Please put your Discord username so you can be contacted if a bug or
regression is found:

evan
This commit is contained in:
Evan
2025-11-04 11:52:52 -08:00
committed by GitHub
parent 4d1911ae1b
commit 4470c3d58a
7 changed files with 137 additions and 50 deletions
+1
View File
@@ -135,6 +135,7 @@ export interface Config {
deleteUnitCooldown(): Tick;
defaultDonationAmount(sender: Player): number;
unitInfo(type: UnitType): UnitInfo;
tradeShipShortRangeDebuff(): number;
tradeShipGold(dist: number, numPorts: number): Gold;
tradeShipSpawnRate(
numTradeShips: number,
+7 -2
View File
@@ -371,9 +371,10 @@ export class DefaultConfig implements Config {
}
tradeShipGold(dist: number, numPorts: number): Gold {
// Sigmoid: concave start, sharp S-curve middle, linear end - heavily punishes trades under 200
// Sigmoid: concave start, sharp S-curve middle, linear end - heavily punishes trades under range debuff.
const debuff = this.tradeShipShortRangeDebuff();
const baseGold =
100_000 / (1 + Math.exp(-0.03 * (dist - 200))) + 100 * dist;
100_000 / (1 + Math.exp(-0.03 * (dist - debuff))) + 100 * dist;
const numPortBonus = numPorts - 1;
// Hyperbolic decay, midpoint at 5 ports, 3x bonus max.
const bonus = 1 + 2 * (numPortBonus / (numPortBonus + 5));
@@ -777,6 +778,10 @@ export class DefaultConfig implements Config {
return 20;
}
tradeShipShortRangeDebuff(): number {
return 200;
}
proximityBonusPortsNb(totalPorts: number) {
return within(totalPorts / 3, 4, totalPorts);
}
+37 -1
View File
@@ -58,7 +58,7 @@ export class PortExecution implements Execution {
return;
}
const ports = this.player.tradingPorts(this.port);
const ports = this.tradingPorts();
if (ports.length === 0) {
return;
@@ -103,4 +103,40 @@ export class PortExecution implements Execution {
}
}
}
// It's a probability list, so if an element appears twice it's because it's
// twice more likely to be picked later.
tradingPorts(): Unit[] {
const ports = this.mg
.players()
.filter((p) => p !== this.port!.owner() && p.canTrade(this.port!.owner()))
.flatMap((p) => p.units(UnitType.Port))
.sort((p1, p2) => {
return (
this.mg.manhattanDist(this.port!.tile(), p1.tile()) -
this.mg.manhattanDist(this.port!.tile(), p2.tile())
);
});
const weightedPorts: Unit[] = [];
for (const [i, otherPort] of ports.entries()) {
const expanded = new Array(otherPort.level()).fill(otherPort);
weightedPorts.push(...expanded);
const tooClose =
this.mg.manhattanDist(this.port!.tile(), otherPort.tile()) <
this.mg.config().tradeShipShortRangeDebuff();
const closeBonus =
i < this.mg.config().proximityBonusPortsNb(ports.length);
if (!tooClose && closeBonus) {
// If the port is close, but not too close, add it again
// to increase the chances of trading with it.
weightedPorts.push(...expanded);
}
if (!tooClose && this.port!.owner().isFriendly(otherPort.owner())) {
weightedPorts.push(...expanded);
}
}
return weightedPorts;
}
}
-1
View File
@@ -648,7 +648,6 @@ export interface Player {
// Misc
toUpdate(): PlayerUpdate;
playerProfile(): PlayerProfile;
tradingPorts(port: Unit): Unit[];
// WARNING: this operation is expensive.
bestTransportShipSpawn(tile: TileRef): TileRef | false;
}
-30
View File
@@ -1214,34 +1214,4 @@ export class PlayerImpl implements Player {
bestTransportShipSpawn(targetTile: TileRef): TileRef | false {
return bestShoreDeploymentSource(this.mg, this, targetTile);
}
// It's a probability list, so if an element appears twice it's because it's
// twice more likely to be picked later.
tradingPorts(port: Unit): Unit[] {
const ports = this.mg
.players()
.filter((p) => p !== port.owner() && p.canTrade(port.owner()))
.flatMap((p) => p.units(UnitType.Port))
.sort((p1, p2) => {
return (
this.mg.manhattanDist(port.tile(), p1.tile()) -
this.mg.manhattanDist(port.tile(), p2.tile())
);
});
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);
}
}
return weightedPorts;
}
}
-16
View File
@@ -83,22 +83,6 @@ 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);
});
test("Can't send alliance requests when dead", () => {
// conquer other
const otherTiles = other.tiles();
+92
View File
@@ -0,0 +1,92 @@
import { PortExecution } from "../src/core/execution/PortExecution";
import {
Game,
Player,
PlayerInfo,
PlayerType,
UnitType,
} from "../src/core/game/Game";
import { setup } from "./util/Setup";
let game: Game;
let player: Player;
let other: Player;
describe("PortExecution", () => {
beforeEach(async () => {
game = await setup(
"half_land_half_ocean",
{
instantBuild: true,
},
[
new PlayerInfo("player", PlayerType.Human, null, "player_id"),
new PlayerInfo("other", PlayerType.Human, null, "other_id"),
],
);
while (game.inSpawnPhase()) {
game.executeNextTick();
}
player = game.player("player_id");
player.addGold(BigInt(1000000));
other = game.player("other_id");
game.config().structureMinDist = () => 10;
});
test("Destination ports chances scale with level", () => {
game.config().proximityBonusPortsNb = () => 0;
game.config().tradeShipShortRangeDebuff = () => 0;
player.conquer(game.ref(7, 10));
const execution = new PortExecution(player, game.ref(7, 10));
execution.init(game, 0);
execution.tick(0);
other.conquer(game.ref(0, 0));
const otherPort = other.buildUnit(UnitType.Port, game.ref(0, 0), {});
otherPort.increaseLevel();
otherPort.increaseLevel();
const ports = execution.tradingPorts();
expect(ports.length).toBe(3);
});
test("Trade ship proximity bonus", () => {
game.config().proximityBonusPortsNb = () => 10;
game.config().tradeShipShortRangeDebuff = () => 0;
player.conquer(game.ref(7, 10));
const execution = new PortExecution(player, game.ref(7, 10));
execution.init(game, 0);
execution.tick(0);
other.conquer(game.ref(0, 0));
other.buildUnit(UnitType.Port, game.ref(0, 0), {});
const ports = execution.tradingPorts();
expect(ports.length).toBe(2);
});
test("Trade ship short range debuff", () => {
game.config().proximityBonusPortsNb = () => 10;
// Short range debuff cancels out the proximity bonus.
game.config().tradeShipShortRangeDebuff = () => 100;
player.conquer(game.ref(7, 10));
const execution = new PortExecution(player, game.ref(7, 10));
execution.init(game, 0);
execution.tick(0);
other.conquer(game.ref(0, 0));
other.buildUnit(UnitType.Port, game.ref(0, 0), {});
const ports = execution.tradingPorts();
expect(ports.length).toBe(1);
});
});