Files
Battle-for-Tismo/tests/coast_test.js
T

59 lines
2.4 KiB
JavaScript

import { TestCase } from "./framework/test_case.js";
import { MapTopology, key } from "../shared/hex.js";
import { coastDistanceField } from "../client/js/map_view/coast.js";
function fixture(terrainAt, size = 5) {
const topology = new MapTopology({ x: size, y: size }, false);
const tiles = {};
for (let y = topology.originY; y < topology.originY + size; y++) {
for (let x = topology.originX; x < topology.originX + size; x++) {
tiles[key(x, y)] = { terrainType: terrainAt(x, y), col: 0 };
}
}
return { seed: 1, topology, tiles };
}
function at(field, view, x, y) {
const i = (y - view.topology.originY) * field.width + (x - view.topology.originX);
return field.data[i];
}
export class CoastTest extends TestCase {
test_no_world_has_no_field() {
this.assertNull(coastDistanceField({ topology: null, tiles: {} }, 4));
this.assertNull(coastDistanceField(fixture(() => "Sea"), 0), "a zero range is no field");
}
test_land_is_the_source_and_the_sea_climbs_away() {
const view = fixture((x, y) => (x === 0 && y === 0 ? "Land" : "Sea"));
const field = coastDistanceField(view, 3);
this.assertEqual(field.width, 5);
this.assertEqual(field.height, 5);
this.assertEqual(at(field, view, 0, 0), 0, "the land is the coast");
this.assertEqual(at(field, view, 1, 0), Math.round(255 / 3), "one tile out");
this.assertEqual(at(field, view, 2, -2), 255, "three tiles out is the range");
this.assertEqual(at(field, view, -2, 2), 255, "beyond the range is clamped");
}
test_a_landlocked_sea_reads_as_coast() {
// A one-tile lake is a single sea tile ringed by land: every direction is
// one step from a source, so its whole field is the first band.
const view = fixture((x, y) => (x === 0 && y === 0 ? "Sea" : "Land"));
const field = coastDistanceField(view, 4);
this.assertEqual(at(field, view, 0, 0), Math.round(255 / 4), "the lake is all shore");
}
test_ice_counts_as_land() {
const view = fixture((x, y) => (x === 0 && y === 0 ? "Ice" : "Sea"));
const field = coastDistanceField(view, 4);
this.assertEqual(at(field, view, 0, 0), 0, "ice is a coast");
this.assertEqual(at(field, view, 1, 0), Math.round(255 / 4), "the water beside it");
}
test_an_all_sea_map_has_no_coast() {
const view = fixture(() => "Sea");
const field = coastDistanceField(view, 4);
for (const value of field.data) this.assertEqual(value, 255, "everything is far offshore");
}
}