priortize ally ports and close ports (#335)

2x more likely to trade with ports belonging to an ally OR close.
3x more likely to trade with ports belonging to an ally AND close.
Add trading tests
This commit is contained in:
Ilan Schemoul
2025-03-28 00:09:58 +01:00
committed by GitHub
parent 665a8c3823
commit f2193edc7c
8 changed files with 102 additions and 22 deletions
+4
View File
@@ -85,6 +85,10 @@ export interface Config {
tilesPerTickUsed: number;
};
attackAmount(attacker: Player, defender: Player | TerraNullius): number;
radiusPortSpawn(): number;
// When computing likelihood of trading for any given port, the X closest port
// are twice more likely to be selected. X is determined below.
proximityBonusPortsNb(totalPorts: number): number;
maxPopulation(player: Player | PlayerView): number;
cityPopulationIncrease(): number;
boatAttackAmount(attacker: Player, defender: Player | TerraNullius): number;
+8
View File
@@ -474,6 +474,14 @@ export class DefaultConfig implements Config {
return Math.floor(attacker.troops() / 5);
}
radiusPortSpawn() {
return 20;
}
proximityBonusPortsNb(totalPorts: number) {
return within(totalPorts / 3, 4, totalPorts);
}
attackAmount(attacker: Player, defender: Player | TerraNullius) {
if (attacker.type() == PlayerType.Bot) {
return attacker.troops() / 20;
+11 -11
View File
@@ -38,7 +38,6 @@ export class PortExecution implements Execution {
tick(ticks: number): void {
if (this.port == null) {
// TODO: use canBuild
const tile = this.tile;
const player = this.mg.player(this._owner);
if (!player.canBuild(UnitType.Port, tile)) {
@@ -46,12 +45,15 @@ export class PortExecution implements Execution {
this.active = false;
return;
}
const spawns = Array.from(this.mg.bfs(tile, manhattanDistFN(tile, 20)))
.filter((t) => this.mg.isOceanShore(t) && this.mg.owner(t) == player)
.sort(
(a, b) =>
this.mg.manhattanDist(a, tile) - this.mg.manhattanDist(b, tile),
);
const spawns = Array.from(
this.mg.bfs(
tile,
manhattanDistFN(tile, this.mg.config().radiusPortSpawn()),
),
).sort(
(a, b) =>
this.mg.manhattanDist(a, tile) - this.mg.manhattanDist(b, tile),
);
if (spawns.length == 0) {
consolex.warn(`cannot find spawn for port`);
@@ -77,10 +79,8 @@ export class PortExecution implements Execution {
return;
}
const ports = this.mg
.players()
.filter((p) => p != this.port.owner() && p.canTrade(this.port.owner()))
.flatMap((p) => p.units(UnitType.Port));
const ports = this.owner().tradingPorts(this.port);
if (ports.length == 0) {
return;
}
+1
View File
@@ -377,6 +377,7 @@ export interface Player {
toUpdate(): PlayerUpdate;
playerProfile(): PlayerProfile;
canBoat(tile: TileRef): boolean;
tradingPorts(port: Unit): Unit[];
}
export interface Game extends GameMap {
+34
View File
@@ -953,4 +953,38 @@ export class PlayerImpl implements Player {
return false;
}
}
// 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[] {
let 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())
);
});
// 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]);
}
// 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;
}
}