Files
OpenFrontIO/src/core/execution/PortExecution.ts
T
evanpelle 0f60825b9f reduce number of trade ships by 50%, double gold per trade ship (#1934)
## Description:

v25 increased the number of trade ships from v24, causing lag
complaints. On my chromebook pathfinding is using over 60% of cpu.

So reduced the number of trade ships roughly by half, but double the
gold per trade ship so it should balance out,

World v25: 
* 2 mins: ~ 60 trade ships 
* 5 mins: ~ 140 trade ships

World on this branch:
* 2 mins: ~ 35 trade ships
* 5 mins: ~ 70 trade ships


Also increased the probably of trade ship spawn for players with under 2
tradeships to prevent larger players from starving out smaller players.

## 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
2025-08-25 16:13:54 -07:00

107 lines
2.8 KiB
TypeScript

import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { PseudoRandom } from "../PseudoRandom";
import { TradeShipExecution } from "./TradeShipExecution";
import { TrainStationExecution } from "./TrainStationExecution";
export class PortExecution implements Execution {
private active = true;
private mg: Game;
private port: Unit | null = null;
private random: PseudoRandom;
private checkOffset: number;
constructor(
private player: Player,
private tile: TileRef,
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
this.random = new PseudoRandom(mg.ticks());
this.checkOffset = mg.ticks() % 10;
}
tick(ticks: number): void {
if (this.mg === null || this.random === null || this.checkOffset === null) {
throw new Error("Not initialized");
}
if (this.port === null) {
const tile = this.tile;
const spawn = this.player.canBuild(UnitType.Port, tile);
if (spawn === false) {
console.warn(
`player ${this.player.id()} cannot build port at ${this.tile}`,
);
this.active = false;
return;
}
this.port = this.player.buildUnit(UnitType.Port, spawn, {});
this.createStation();
}
if (!this.port.isActive()) {
this.active = false;
return;
}
if (this.player.id() !== this.port.owner().id()) {
this.player = this.port.owner();
}
// Only check every 10 ticks for performance.
if ((this.mg.ticks() + this.checkOffset) % 10 !== 0) {
return;
}
if (!this.shouldSpawnTradeShip()) {
return;
}
const ports = this.player.tradingPorts(this.port);
if (ports.length === 0) {
return;
}
const port = this.random.randElement(ports);
this.mg.addExecution(new TradeShipExecution(this.player, this.port, port));
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
shouldSpawnTradeShip(): boolean {
const numTradeShips = this.mg.unitCount(UnitType.TradeShip);
const numPlayerPorts = this.player.unitCount(UnitType.Port);
const numPlayerTradeShips = this.player.unitCount(UnitType.TradeShip);
const spawnRate = this.mg
.config()
.tradeShipSpawnRate(numTradeShips, numPlayerPorts, numPlayerTradeShips);
for (let i = 0; i < this.port!.level(); i++) {
if (this.random.chance(spawnRate)) {
return true;
}
}
return false;
}
createStation(): void {
if (this.port !== null) {
const nearbyFactory = this.mg.hasUnitNearby(
this.port.tile()!,
this.mg.config().trainStationMaxRange(),
UnitType.Factory,
);
if (nearbyFactory) {
this.mg.addExecution(new TrainStationExecution(this.port));
}
}
}
}