Files
Battle-for-Tismo/shared/terrain_stats.js
T
2026-09-16 19:59:18 +02:00

44 lines
1.3 KiB
JavaScript

// Reads the per-terrain figures baked into generated tile dictionaries. A
// movement multiplier of 0 (an unauthored layer, or a non-land tile) falls back
// to a normal cost so a unit is never frozen.
export const DEFAULT_MOVEMENT_COST_MULTIPLIER = 1.0;
export class TerrainStats {
constructor(tiles) {
this._tiles = tiles;
}
_value(coords, field) {
const tile = this._tiles[coords.x + "," + coords.y];
return tile ? (tile[field] || 0) : 0;
}
populationMultiplier(coords) {
return this._value(coords, "populationMultiplier");
}
gdpMultiplier(coords) {
return this._value(coords, "gdpMultiplier");
}
movementCostMultiplier(coords) {
const value = this._value(coords, "movementCostMultiplier");
return value > 0 ? value : DEFAULT_MOVEMENT_COST_MULTIPLIER;
}
// Cheapest authored movement cost across every tile in use, so the A*
// heuristic never overestimates a path.
minMovementCostMultiplier() {
let cheapest = Infinity;
for (const k in this._tiles) {
const value = this.movementCostMultiplier({
x: Number(k.slice(0, k.indexOf(","))),
y: Number(k.slice(k.indexOf(",") + 1)),
});
if (value < cheapest) cheapest = value;
}
return cheapest < Infinity ? cheapest : DEFAULT_MOVEMENT_COST_MULTIPLIER;
}
}