Files
Battle-for-Tismo/shared/game_state/scenario.js
T

105 lines
3.9 KiB
JavaScript

// The opening order of battle and the reserves every civilisation starts a game
// with. The scenario is applied by the server once the world has been settled,
// not during world generation, so the economy's warm-up cannot spend the
// opening stores: a player joining the server sees exactly these figures.
import { STORABLE_RESOURCE_IDS } from "../data.js";
// The forces a nation is founded with. Infantry and artillery stand in every
// city, the launches and battleships of a city's port are launched onto its
// adjacent water, and the capital fields the air force.
export const STARTING_FORCES = {
infantryPerCity: 15,
artilleryPerCity: 5,
launchesPerPort: 5,
battleshipsPerPort: 2,
fightersPerCapital: 5,
bombersPerCapital: 5,
};
// Every region opens holding a year of what it consumes. Energy is a flow with
// no store, so only the four storable goods are stocked.
export const STARTING_RESERVE_DAYS = 365;
export const scenarioMethods = {
// Applies the whole opening scenario to a settled world. Any units the
// generated world seeded are cleared first, so the order of battle is exactly
// the one described here and never picks up a stray starting unit.
applyStartingScenario() {
this._clearStartingForces();
this._seedStartingBuildings();
this._seedStartingUnits();
this._seedStartingReserves();
this._emitChanged();
},
_clearStartingForces() {
for (const unit of this.units.slice()) this._destroyUnit(unit);
},
// Every city raises a barracks; the capital also an airport.
_seedStartingBuildings() {
const barracks = this.buildingIndexById("barracks");
const airport = this.buildingIndexById("airport");
for (const city of this.cities) {
if (barracks >= 0) {
city.buildings[barracks] = Math.max(1, city.buildings[barracks] || 0);
}
if (airport >= 0 && city.isCapital) {
city.buildings[airport] = Math.max(1, city.buildings[airport] || 0);
}
}
this._clearTileGdpCache();
this._touchModifiers();
},
_seedStartingUnits() {
const protoById = (id) => this.protoUnits.find((unit) => unit.id === id);
const infantry = protoById("modern_infantry");
const artillery = protoById("artillery");
const launch = protoById("launch");
const battleship = protoById("battleship");
const fighter = protoById("jet_fighter");
const bomber = protoById("bomber");
for (const city of this.cities) {
for (let i = 0; i < STARTING_FORCES.infantryPerCity; i++) {
this._spawnUnit(city.coords, city.civ, infantry, city.id);
}
for (let i = 0; i < STARTING_FORCES.artilleryPerCity; i++) {
this._spawnUnit(city.coords, city.civ, artillery, city.id);
}
if (!this.hasCityBuilding(city, "port")) continue;
for (let i = 0; i < STARTING_FORCES.launchesPerPort; i++) {
this._spawnUnit(this._spawnCoordsFor(city, launch), city.civ, launch, city.id);
}
for (let i = 0; i < STARTING_FORCES.battleshipsPerPort; i++) {
this._spawnUnit(this._spawnCoordsFor(city, battleship), city.civ, battleship, city.id);
}
}
for (const city of this.cities) {
if (!city.isCapital) continue;
for (let i = 0; i < STARTING_FORCES.fightersPerCapital; i++) {
this._spawnUnit(city.coords, city.civ, fighter, city.id);
}
for (let i = 0; i < STARTING_FORCES.bombersPerCapital; i++) {
this._spawnUnit(city.coords, city.civ, bomber, city.id);
}
}
},
// Fills every region's store with a year of its daily consumption, so a fresh
// nation can fight and build for a long while before its industry and its
// trade routes must carry it.
_seedStartingReserves() {
for (const city of this.cities) {
const consumption = this.getRegionResourceInfo(city).consumption;
const stock = this.getCityResourceStock(city);
for (const id of STORABLE_RESOURCE_IDS) {
stock[id] = (consumption[id] || 0) * STARTING_RESERVE_DAYS;
}
}
},
};