game-village-politics-frontend/app/utils/terrain.ts

66 lines
3.0 KiB
TypeScript

// The world map's land: lakes, rocky hills that rise to mountains where they bunch up, and
// forests, all from smooth noise of the coordinates. A field always looks the same, and the
// map and its overview read it from this one function, so the two never disagree. The land
// is decoration only: troops cross every field, and travel is a straight line.
export type Ground = 'grass' | 'forest' | 'water' | 'hill' | 'mountain'
/** 0..1 from a field's coordinates: the same field, the same value. */
export function fieldHash(x: number, y: number): number {
let h = (x * 73856093) ^ (y * 19349663)
h = Math.imul(h ^ (h >>> 13), 1274126177)
return ((h ^ (h >>> 16)) >>> 0) / 4294967296
}
const smooth = (t: number) => t * t * (3 - 2 * t)
/** Smooth value noise over patches of `size` fields: neighbouring fields get similar values. */
export function fieldNoise(x: number, y: number, size: number, seed: number): number {
const gx = x / size
const gy = y / size
const ix = Math.floor(gx)
const iy = Math.floor(gy)
const fx = smooth(gx - ix)
const fy = smooth(gy - iy)
const at = (a: number, b: number) => fieldHash(a * 7 + seed * 101, b * 13 - seed * 37)
const top = at(ix, iy) + (at(ix + 1, iy) - at(ix, iy)) * fx
const bottom = at(ix, iy + 1) + (at(ix + 1, iy + 1) - at(ix, iy + 1)) * fx
return top + (bottom - top) * fy
}
/** Patch size and seed of each kind of ground, and where its noise turns the field. */
export const TERRAIN = {
water: { size: 11, seed: 1, below: 0.17 },
/** Hills from `above`; their bunched-up cores, from `mountain`, are mountains. */
hill: { size: 8, seed: 2, above: 0.8, mountain: 0.9 },
/** Forest patches from `above`, and single trees scattered on `scattered` of the open land. */
forest: { size: 6, seed: 3, above: 0.6, scattered: 0.05 },
} as const
/** How deep in a forest a field lies, 0..1: deeper in, more and bigger trees. */
export const forestDepth = (x: number, y: number) => fieldNoise(x, y, TERRAIN.forest.size, TERRAIN.forest.seed)
/** The ground of a field. A village's field is always open land: the map draws it as grass. */
export function groundAt(x: number, y: number): Ground {
if (fieldNoise(x, y, TERRAIN.water.size, TERRAIN.water.seed) < TERRAIN.water.below) return 'water'
const hill = fieldNoise(x, y, TERRAIN.hill.size, TERRAIN.hill.seed)
if (hill > TERRAIN.hill.mountain) return 'mountain'
if (hill > TERRAIN.hill.above) return 'hill'
if (forestDepth(x, y) > TERRAIN.forest.above || fieldHash(x, y) < TERRAIN.forest.scattered) return 'forest'
return 'grass'
}
/** Flat colours of the map's ground: grass in four worn shades, the lake and its shore. */
export const GRASS = ['#6b7049', '#676c46', '#6f724c', '#73704b']
export const WATER = '#4a5b5d'
export const SHORE = '#7a6a50'
/** One colour per ground kind, for the overview where a field is a few pixels. */
export const GROUND_COLOURS: Record<Ground, string> = {
grass: '#656a45',
forest: '#3f4b31',
water: WATER,
hill: '#7b7769',
mountain: '#96918a',
}