273 lines
12 KiB
JavaScript
273 lines
12 KiB
JavaScript
import { TestCase } from "./framework/test_case.js";
|
|
import { smallState } from "./framework/helpers.js";
|
|
import { parseKey } from "../shared/hex.js";
|
|
|
|
const BIG_BUDGET = 1.0e15;
|
|
|
|
function buildingIndex(state, id) {
|
|
return state.protoBuildings.findIndex((building) => building.id === id);
|
|
}
|
|
|
|
function protoIndex(state, id) {
|
|
return state.protoUnits.findIndex((unit) => unit.id === id);
|
|
}
|
|
|
|
function protoUnit(state, id) {
|
|
return state.protoUnits.find((unit) => unit.id === id);
|
|
}
|
|
|
|
function coastalCity(state, civ) {
|
|
return state.cities.find((city) => city.civ === civ && state.isCoastalCity(city)) || null;
|
|
}
|
|
|
|
function inlandCity(state, civ) {
|
|
return state.cities.find((city) => city.civ === civ && !state.isCoastalCity(city)) || null;
|
|
}
|
|
|
|
function giveBuilding(state, city, id, level = 1) {
|
|
const index = buildingIndex(state, id);
|
|
city.buildings[index] = level;
|
|
state._gdpPerCapitaCache.clear();
|
|
}
|
|
|
|
function firstLandNeighbour(state, coords) {
|
|
return state.topology.neighbours(coords.x, coords.y).find((n) => state._isLand(n)) || null;
|
|
}
|
|
|
|
function freeAdjacentLand(state) {
|
|
for (const cell of state.landCells) {
|
|
if (state.cityAt(cell) || state.unitAt(cell)) continue;
|
|
for (const neighbour of state.topology.neighbours(cell.x, cell.y)) {
|
|
if (!state._isLand(neighbour) || state.cityAt(neighbour) || state.unitAt(neighbour)) continue;
|
|
return [cell, neighbour];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function farTile(state, from, minDistance) {
|
|
for (const k in state.tiles) {
|
|
const coords = parseKey(k);
|
|
if (state.topology.tileDistance(from, coords) <= minDistance) continue;
|
|
if (state.cityAt(coords) || state.unitAt(coords)) continue;
|
|
return coords;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export class AirTest extends TestCase {
|
|
test_port_can_only_be_raised_in_a_coastal_city() {
|
|
const state = smallState();
|
|
state.budgets.set(0, BIG_BUDGET);
|
|
const port = buildingIndex(state, "port");
|
|
const inland = inlandCity(state, 0);
|
|
const coastal = coastalCity(state, 0);
|
|
this.assertNotNull(inland, "the fixture has an inland city");
|
|
this.assertNotNull(coastal, "the fixture has a coastal city");
|
|
this.assertFalse(state.requestBuild(inland.id, port), "an inland city cannot build a port");
|
|
this.assertTrue(state.requestBuild(coastal.id, port), "a coastal city can build a port");
|
|
}
|
|
|
|
test_shipyard_needs_a_coastal_city_and_a_port() {
|
|
const state = smallState();
|
|
state.budgets.set(0, BIG_BUDGET);
|
|
const shipyard = buildingIndex(state, "shipyard");
|
|
const city = coastalCity(state, 0);
|
|
this.assertFalse(state.requestBuild(city.id, shipyard), "no port, no shipyard");
|
|
giveBuilding(state, city, "port");
|
|
this.assertTrue(state.requestBuild(city.id, shipyard), "a port unlocks the shipyard");
|
|
|
|
// Even with a port, an inland city has no water to launch from.
|
|
const inland = inlandCity(state, 0);
|
|
this.assertNotNull(inland, "the fixture has an inland city");
|
|
giveBuilding(state, inland, "port");
|
|
this.assertFalse(state.requestBuild(inland.id, shipyard), "a shipyard is coastal-only");
|
|
}
|
|
|
|
test_airport_and_shipyard_gate_training() {
|
|
const state = smallState();
|
|
state.budgets.set(0, BIG_BUDGET);
|
|
const city = coastalCity(state, 0);
|
|
const jet = protoIndex(state, "jet_fighter");
|
|
const battleship = protoIndex(state, "battleship");
|
|
this.assertFalse(state.requestTrain(city.id, jet), "no airport, no aircraft");
|
|
this.assertFalse(state.requestTrain(city.id, battleship), "no shipyard, no warship");
|
|
giveBuilding(state, city, "airport");
|
|
giveBuilding(state, city, "shipyard");
|
|
this.assertTrue(state.requestTrain(city.id, jet), "the airport unlocks aircraft");
|
|
this.assertTrue(state.requestTrain(city.id, battleship), "the shipyard unlocks warships");
|
|
}
|
|
|
|
test_trained_aircraft_gets_its_home_airport() {
|
|
const state = smallState();
|
|
state.budgets.set(0, BIG_BUDGET);
|
|
const city = coastalCity(state, 0);
|
|
giveBuilding(state, city, "airport");
|
|
const jet = protoIndex(state, "jet_fighter");
|
|
this.assertTrue(state.requestTrain(city.id, jet), "the airport lets the order through");
|
|
for (let i = 0; i < 100000 && state.training.size > 0; i++) state.advanceHour();
|
|
const unit = state.units.find((u) => u.proto === jet && u.civ === 0);
|
|
this.assertNotNull(unit, "the aircraft was produced");
|
|
this.assertEqual(unit.homeCityId, city.id, "it is based at the training city");
|
|
this.assertFalse(unit.airborne, "it is parked on the runway");
|
|
}
|
|
|
|
test_battleships_launch_onto_an_adjacent_sea_tile() {
|
|
const state = smallState();
|
|
const city = coastalCity(state, 0);
|
|
const battleship = protoUnit(state, "battleship");
|
|
const unit = state._spawnTrainedUnit(city, battleship);
|
|
const tile = state.tiles[`${unit.coords.x},${unit.coords.y}`];
|
|
this.assertEqual(tile.terrainClass, "Sea", "the hull is launched on water");
|
|
this.assertTrue(
|
|
state.topology.tileDistance(unit.coords, city.coords) === 1,
|
|
"the launch tile neighbours the city"
|
|
);
|
|
}
|
|
|
|
test_aircraft_fly_home_and_refuel_after_a_sortie() {
|
|
const state = smallState();
|
|
const city = coastalCity(state, 0);
|
|
giveBuilding(state, city, "airport");
|
|
const jet = protoUnit(state, "jet_fighter");
|
|
const unit = state._spawnUnit(city.coords, 0, jet, city.id);
|
|
const goal = firstLandNeighbour(state, city.coords);
|
|
this.assertNotNull(goal, "there is somewhere to fly");
|
|
this.assertTrue(state.requestGroupMove([unit.id], goal), "the sortie is accepted");
|
|
|
|
state.advanceHour();
|
|
this.assertEqual(unit.coords.x, goal.x, "it reached the target");
|
|
this.assertTrue(unit.airborne, "it is airborne away from the airport");
|
|
this.assertLess(unit.airHours, jet.enduranceHours, "the sortie burned fuel");
|
|
|
|
state.advanceHour();
|
|
this.assertEqual(unit.coords.x, city.coords.x, "it returned to its airport");
|
|
this.assertEqual(unit.coords.y, city.coords.y);
|
|
this.assertFalse(unit.airborne, "it is parked again");
|
|
this.assertApprox(unit.airHours, jet.enduranceHours, 1e-9, "it refuelled");
|
|
}
|
|
|
|
test_aircraft_crash_when_no_airport_can_reach_them() {
|
|
const state = smallState();
|
|
const jet = protoUnit(state, "jet_fighter");
|
|
const spot = freeAdjacentLand(state);
|
|
const unit = state._spawnUnit(spot[0], 0, jet, null);
|
|
this.assertNull(state._nearestFriendlyAirport(unit), "no airport exists yet");
|
|
state.advanceHour();
|
|
this.assertNotNull(state.findUnit(unit.id), "still flying after one hour");
|
|
state.advanceHour();
|
|
this.assertNull(state.findUnit(unit.id), "out of fuel, it crashed");
|
|
}
|
|
|
|
test_aircraft_cross_every_terrain_at_their_listed_speed() {
|
|
const state = smallState();
|
|
const jet = protoUnit(state, "jet_fighter");
|
|
const unit = state._spawnUnit(state.landCells[0], 0, jet, null);
|
|
const expected = 1 / jet.speed;
|
|
let checked = 0;
|
|
for (const k in state.tiles) {
|
|
this.assertApprox(
|
|
state._tileTravelHours(unit, parseKey(k)),
|
|
expected,
|
|
1e-9,
|
|
`flat flight time over ${state.tiles[k].terrainType}`
|
|
);
|
|
checked += 1;
|
|
if (checked >= 12) break;
|
|
}
|
|
this.assertGreater(checked, 0);
|
|
}
|
|
|
|
test_aircraft_reach_the_edge_of_their_radius_and_return() {
|
|
const state = smallState();
|
|
const city = coastalCity(state, 0);
|
|
giveBuilding(state, city, "airport");
|
|
const jet = protoUnit(state, "jet_fighter");
|
|
const unit = state._spawnUnit(city.coords, 0, jet, city.id);
|
|
let goal = null;
|
|
for (const k in state.tiles) {
|
|
const coords = parseKey(k);
|
|
if (state.topology.tileDistance(city.coords, coords) !== jet.missionRange) continue;
|
|
if (state.cityAt(coords) || state.unitAt(coords)) continue;
|
|
goal = coords;
|
|
break;
|
|
}
|
|
this.assertNotNull(goal, "a tile sits on the radius edge");
|
|
this.assertTrue(state.requestGroupMove([unit.id], goal), "the edge is reachable");
|
|
state.advanceHour();
|
|
this.assertEqual(unit.coords.x, goal.x, "it reached the edge");
|
|
this.assertEqual(unit.coords.y, goal.y);
|
|
state.advanceHour();
|
|
this.assertEqual(unit.coords.x, city.coords.x, "the round trip closed");
|
|
this.assertEqual(unit.coords.y, city.coords.y);
|
|
this.assertApprox(unit.airHours, jet.enduranceHours, 1e-9, "it landed and refuelled");
|
|
}
|
|
|
|
test_aircraft_cannot_be_ordered_beyond_their_range() {
|
|
const state = smallState();
|
|
const city = coastalCity(state, 0);
|
|
giveBuilding(state, city, "airport");
|
|
const jet = protoUnit(state, "jet_fighter");
|
|
const unit = state._spawnUnit(city.coords, 0, jet, city.id);
|
|
const target = farTile(state, city.coords, jet.missionRange);
|
|
this.assertNotNull(target, "the map has tiles beyond the radius");
|
|
this.assertFalse(state.requestGroupMove([unit.id], target), "the order is refused");
|
|
this.assertEmpty(unit.path, "no route was stored");
|
|
}
|
|
|
|
test_bombers_strike_from_afar_without_joining_the_battle() {
|
|
const state = smallState();
|
|
const bomber = protoUnit(state, "bomber");
|
|
const infantry = protoUnit(state, "modern_infantry");
|
|
const pair = freeAdjacentLand(state);
|
|
const attacker = state._spawnUnit(pair[0], 0, bomber, null);
|
|
const defender = state._spawnUnit(pair[1], 1, infantry);
|
|
this.assertTrue(state.requestStrike([attacker.id], defender.coords), "the strike is accepted");
|
|
this.assertLess(defender.hp, defender.maxHp, "the bomb damaged the target");
|
|
this.assertEqual(attacker.coords.x, pair[0].x, "the bomber never moved");
|
|
this.assertEqual(attacker.coords.y, pair[0].y);
|
|
this.assertEmpty(state._battleTiles(), "no melee battle was started");
|
|
}
|
|
|
|
test_ground_ranged_units_strike_from_afar_like_aircraft() {
|
|
const state = smallState();
|
|
const artillery = protoUnit(state, "artillery");
|
|
const infantry = protoUnit(state, "modern_infantry");
|
|
const pair = freeAdjacentLand(state);
|
|
const attacker = state._spawnUnit(pair[0], 0, artillery);
|
|
const defender = state._spawnUnit(pair[1], 1, infantry);
|
|
this.assertTrue(state._isStrikeUnit(attacker), "artillery counts as a ranged attacker");
|
|
this.assertTrue(state.requestStrike([attacker.id], defender.coords), "the battery opens fire");
|
|
this.assertLess(defender.hp, defender.maxHp, "the shell damaged the target");
|
|
this.assertEqual(attacker.coords.x, pair[0].x, "the artillery never moved");
|
|
this.assertEqual(attacker.coords.y, pair[0].y);
|
|
this.assertEmpty(state._battleTiles(), "no melee battle was started");
|
|
}
|
|
|
|
test_ranged_attack_beyond_reach_is_refused() {
|
|
const state = smallState();
|
|
const artillery = protoUnit(state, "artillery");
|
|
const infantry = protoUnit(state, "modern_infantry");
|
|
const from = state.landCells.find((c) => !state.unitAt(c) && !state.cityAt(c));
|
|
const attacker = state._spawnUnit(from, 0, artillery);
|
|
const goal = farTile(state, from, artillery.range);
|
|
this.assertNotNull(goal, "the map has a tile beyond the battery's range");
|
|
const defender = state._spawnUnit(goal, 1, infantry);
|
|
this.assertFalse(state.requestStrike([attacker.id], goal), "a target out of reach is refused");
|
|
this.assertEqual(defender.hp, defender.maxHp, "the target is untouched");
|
|
}
|
|
|
|
test_only_air_or_long_reach_units_are_ranged() {
|
|
const state = smallState();
|
|
const spot = state.landCells.find((c) => !state.unitAt(c) && !state.cityAt(c));
|
|
const infantry = state._spawnUnit(spot, 0, protoUnit(state, "modern_infantry"), null);
|
|
const worker = state._spawnUnit(spot, 0, protoUnit(state, "worker"), null);
|
|
const artillery = state._spawnUnit(spot, 0, protoUnit(state, "artillery"), null);
|
|
const jet = state._spawnUnit(spot, 0, protoUnit(state, "jet_fighter"), null);
|
|
this.assertFalse(state._isStrikeUnit(infantry), "infantry is melee");
|
|
this.assertFalse(state._isStrikeUnit(worker), "a worker is not a ranged attacker");
|
|
this.assertTrue(state._isStrikeUnit(artillery), "artillery outranges one tile");
|
|
this.assertTrue(state._isStrikeUnit(jet), "an aircraft attacks from the air");
|
|
}
|
|
}
|