Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/shared/game/entities/Settlement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ export function createSettlement(
* - Available units (RURE occupation) do not count.
*/
export function calculatePopulation(settlement: Settlement): number {
return settlement.units.filter((u) => {
const occ = u.occupation;
// ⚡ Bolt: Use a simple loop with a counter instead of filter().length to avoid intermediate array allocation.
let count = 0;
for (const unit of settlement.units) {
const occ = unit.occupation;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!occ) return false;
if (!occ) continue;
// JobType is a string
if (typeof occ === 'string') return true;
// FieldWork and Rure are objects
return occ.kind === 'FIELD_WORK';
}).length;
if (typeof occ === 'string') {
count++;
} else if (occ.kind === 'FIELD_WORK') {
// FieldWork is an object
count++;
}
}
return count;
}
8 changes: 6 additions & 2 deletions src/shared/game/systems/TurnEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export interface TurnEngineResult {
readonly effects: readonly GameEffect[];
}

const BUILDING_TYPES_SET = new Set<string>(Object.values(BuildingType));
const UNIT_TYPES_SET = new Set<string>(Object.values(UnitType));

/* eslint-disable-next-line @typescript-eslint/no-extraneous-class */
export class TurnEngine {
private constructor() {
Expand Down Expand Up @@ -155,8 +158,9 @@ export class TurnEngine {
return currentNamingStats;
}

const isBuilding = Object.values(BuildingType).includes(currentItem as BuildingType);
const isUnit = Object.values(UnitType).includes(currentItem as UnitType);
// ⚡ Bolt: Use pre-calculated Sets for O(1) lookup instead of creating new arrays with Object.values()
const isBuilding = BUILDING_TYPES_SET.has(currentItem);
const isUnit = UNIT_TYPES_SET.has(currentItem);
const cost = this.getProductionCost(currentItem, isBuilding);

if (!this.canAffordConstruction(settlement, cost)) {
Expand Down