mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:40:42 +00:00
Unit count (#1402)
## Description: Use an iterative approach to counting units to reduce array allocations. ## 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 - [ ] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors
This commit is contained in:
@@ -187,7 +187,7 @@
|
||||
<territory-patterns-modal class="w-[20%] md:w-[15%]">
|
||||
<button
|
||||
id="territory-patterns-input-preview-button"
|
||||
class="w-full border p-[4px] rounded-lg flex cursor-pointer border-black/30 dark:border-gray-300/60 bg-white/70 dark:bg-[rgba(55,65,81,0.7)]"
|
||||
class="w-full border p-[4px] rounded-lg flex cursor-pointer border-black/30 dark:border-gray-300/60 bg-white/70 dark:bg-[rgba(55,65,81,0.7)] justify-center"
|
||||
title="Pick a pattern!"
|
||||
></button>
|
||||
</territory-patterns-modal>
|
||||
|
||||
@@ -75,7 +75,7 @@ export class PortExecution implements Execution {
|
||||
}
|
||||
|
||||
shouldSpawnTradeShip(): boolean {
|
||||
const numTradeShips = this.mg.units(UnitType.TradeShip).length;
|
||||
const numTradeShips = this.mg.unitCount(UnitType.TradeShip);
|
||||
const spawnRate = this.mg.config().tradeShipSpawnRate(numTradeShips);
|
||||
for (let i = 0; i < this.port!.level(); i++) {
|
||||
if (this.random.chance(spawnRate)) {
|
||||
|
||||
@@ -130,7 +130,7 @@ export class TradeShipExecution implements Execution {
|
||||
.config()
|
||||
.tradeShipGold(
|
||||
this.tilesTraveled,
|
||||
this.tradeShip!.owner().units(UnitType.Port).length,
|
||||
this.tradeShip!.owner().unitCount(UnitType.Port),
|
||||
);
|
||||
|
||||
if (this.wasCaptured) {
|
||||
|
||||
@@ -67,7 +67,7 @@ export class TransportShipExecution implements Execution {
|
||||
this.pathFinder = PathFinder.Mini(mg, 10_000, true, 10);
|
||||
|
||||
if (
|
||||
this.attacker.units(UnitType.TransportShip).length >=
|
||||
this.attacker.unitCount(UnitType.TransportShip) >=
|
||||
mg.config().boatMaxNumber()
|
||||
) {
|
||||
mg.displayMessage(
|
||||
|
||||
@@ -55,7 +55,7 @@ export class WarshipExecution implements Execution {
|
||||
this.warship.delete();
|
||||
return;
|
||||
}
|
||||
const hasPort = this.warship.owner().units(UnitType.Port).length > 0;
|
||||
const hasPort = this.warship.owner().unitCount(UnitType.Port) > 0;
|
||||
if (hasPort) {
|
||||
this.warship.modifyHealth(1);
|
||||
}
|
||||
@@ -75,7 +75,7 @@ export class WarshipExecution implements Execution {
|
||||
}
|
||||
|
||||
private findTargetUnit(): Unit | undefined {
|
||||
const hasPort = this.warship.owner().units(UnitType.Port).length > 0;
|
||||
const hasPort = this.warship.owner().unitCount(UnitType.Port) > 0;
|
||||
const patrolRangeSquared = this.mg.config().warshipPatrolRange() ** 2;
|
||||
|
||||
const ships = this.mg.nearbyUnits(
|
||||
|
||||
@@ -519,6 +519,7 @@ export interface Player {
|
||||
|
||||
// Units
|
||||
units(...types: UnitType[]): Unit[];
|
||||
unitCount(type: UnitType): number;
|
||||
unitsConstructed(type: UnitType): number;
|
||||
unitsOwned(type: UnitType): number;
|
||||
buildableUnits(tile: TileRef): BuildableUnit[];
|
||||
@@ -638,6 +639,7 @@ export interface Game extends GameMap {
|
||||
|
||||
// Units
|
||||
units(...types: UnitType[]): Unit[];
|
||||
unitCount(type: UnitType): number;
|
||||
unitInfo(type: UnitType): UnitInfo;
|
||||
hasUnitNearby(
|
||||
tile: TileRef,
|
||||
|
||||
@@ -209,9 +209,19 @@ export class GameImpl implements Game {
|
||||
units(...types: UnitType[]): Unit[] {
|
||||
return Array.from(this._players.values()).flatMap((p) => p.units(...types));
|
||||
}
|
||||
|
||||
unitCount(type: UnitType): number {
|
||||
let total = 0;
|
||||
for (const player of this._players.values()) {
|
||||
total += player.unitCount(type);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
unitInfo(type: UnitType): UnitInfo {
|
||||
return this.config().unitInfo(type);
|
||||
}
|
||||
|
||||
nations(): Nation[] {
|
||||
return this._nations;
|
||||
}
|
||||
|
||||
@@ -243,12 +243,23 @@ export class PlayerImpl implements Player {
|
||||
return total;
|
||||
}
|
||||
|
||||
// Count of units owned by the player, not including construction
|
||||
unitCount(type: UnitType): number {
|
||||
let total = 0;
|
||||
for (const unit of this._units) {
|
||||
if (unit.type() === type) {
|
||||
total += unit.level();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Count of units owned by the player, including construction
|
||||
unitsOwned(type: UnitType): number {
|
||||
let total = 0;
|
||||
for (const unit of this._units) {
|
||||
if (unit.type() === type) {
|
||||
total++;
|
||||
total += unit.level();
|
||||
continue;
|
||||
}
|
||||
if (unit.type() !== UnitType.Construction) continue;
|
||||
|
||||
@@ -9,7 +9,7 @@ export function canBuildTransportShip(
|
||||
tile: TileRef,
|
||||
): TileRef | false {
|
||||
if (
|
||||
player.units(UnitType.TransportShip).length >= game.config().boatMaxNumber()
|
||||
player.unitCount(UnitType.TransportShip) >= game.config().boatMaxNumber()
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user