1import * as THREE from '../vendor/three.module.js'; 2import { HEX, hexToWorld, hexDistance } from './hex.js'; 3import { BIOMES, DEEP, ROAD, SNOW, TRUNK_PAINT, BURNT, PRIMER } from './paint.js'; 4import { sway } from './life.js'; 5import { CANOPY, SNOW_TIPS, TRUNK, TREE_TRUNK, MOUNTAIN, MOUNTAIN_CAP, ROCKS, TUFT } from './flora.js'; 6 7const SHAPE = { DEEP: 0, SHALLOW: 1, PLAIN: 2, HILL: 3, MOUNTAIN: 4, PEAK: 5 }; 8const FLAG = { FOREST: 1, ROAD: 2 }; 9const STEP = 0.12; 10const BASE = 0.5; 11const WATER_TOP = [-0.14, 0.03]; 12const PEAK_HEIGHT = { [SHAPE.MOUNTAIN]: 0.75, [SHAPE.PEAK]: 1.3 }; 13const TREES = 3; 14const SEEN = 0.55; 15const HIDDEN = 0.2; 16const LOW = 0.14; 17const RISE = 0.5; 18const SIDE = 0.62; 19const BEYOND = 6; 20const DIM = 0.32; 21 22export const decode = (b64) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)); 23 24function random(col, row, k) { 25 const x = Math.sin(col * 12.9898 + row * 78.233 + k * 37.719) * 43758.5453; 26 return x - Math.floor(x); 27} 28 29function prism() { 30 const g = new THREE.CylinderGeometry(HEX * 0.97, HEX * 0.97, 1, 6).translate(0, 0.5, 0).toNonIndexed(); 31 const n = g.attributes.normal; 32 const c = new Float32Array(n.count * 3).map((_, i) => (n.getY(Math.floor(i / 3)) > 0.5 ? 1 : SIDE)); 33 g.setAttribute('color', new THREE.BufferAttribute(c, 3)); 34 return g; 35} 36 37const UP = new THREE.Vector3(0, 1, 0); 38const m4 = new THREE.Matrix4(); 39const q = new THREE.Quaternion(); 40const v = new THREE.Vector3(); 41const s = new THREE.Vector3(); 42const ZERO = new THREE.Matrix4().makeScale(0, 0, 0); 43const IDENT = new THREE.Quaternion(); 44const sunk = (it, h) => m4.compose(v.set(it.x, -BASE, it.z), IDENT, s.set(1, h, 1)); 45 46export function createBoard(map) { 47 const W = map.width; 48 const H = map.height; 49 const bytes = decode(map.hexes); 50 const look = (c, r) => bytes.subarray((r * W + c) * 4, (r * W + c) * 4 + 4); 51 const top = (c, r) => { 52 const [shape, , h] = look(c, r); 53 return shape <= SHAPE.SHALLOW ? WATER_TOP[shape] : h * STEP; 54 }; 55 const center = hexToWorld(W - 1, H - 1).multiplyScalar(0.5); 56 const pos = (c, r) => hexToWorld(c, r).sub(center).setY(top(c, r)); 57 const walkable = (c, r) => c >= 0 && r >= 0 && c < W && r < H && look(c, r)[0] !== SHAPE.DEEP && look(c, r)[0] !== SHAPE.PEAK; 58 59 const layers = []; 60 const layer = (geometry, opts = {}) => { 61 const l = { geometry, items: [], ...opts }; 62 layers.push(l); 63 return l; 64 }; 65 const tiles = layer(prism(), { pick: true, vertexColors: true }); 66 const outerTiles = layer(tiles.geometry, { outer: true, vertexColors: true }); 67 const outerPeaks = layer(MOUNTAIN, { outer: true, cast: true }); 68 const mount = layer(MOUNTAIN, { cast: true }); 69 const snow = layer(MOUNTAIN_CAP, { cast: true }); 70 const trunks = layer(TRUNK, { cast: true }); 71 const tips = layer(SNOW_TIPS, { cast: true, sway: 0.05 }); 72 const tufts = layer(TUFT, { sway: 0.12 }); 73 const canopies = Object.fromEntries(Object.entries(CANOPY).map(([k, g]) => [k, layer(g, { cast: true, sway: 0.05 })])); 74 const rocks = Object.fromEntries(Object.entries(ROCKS).map(([k, g]) => [k, layer(g, { cast: true })])); 75 const put = (l, hex, x, y, z, angle, sx, sy, color, shade) => { 76 m4.compose(v.set(x, y, z), q.setFromAxisAngle(UP, angle), s.set(sx, sy, sx)); 77 const paint = new THREE.Color(color).offsetHSL(0, 0, (random(hex, l.items.length, 91) - 0.5) * 2 * shade); 78 l.items.push({ hex, matrix: m4.clone(), paint, x, z, h: sy }); 79 }; 80 81 for (let r = 0; r < H; r++) { 82 for (let c = 0; c < W; c++) { 83 const i = r * W + c; 84 const [shape, biome, , flags] = look(c, r); 85 const b = BIOMES[biome]; 86 const t = top(c, r); 87 const { x, z } = pos(c, r); 88 const ground = shape === SHAPE.DEEP ? DEEP : shape === SHAPE.SHALLOW ? b.water : flags & FLAG.ROAD ? ROAD : [, , b.ground, b.hill, b.rock, b.rock][shape]; 89 put(tiles, i, x, -BASE, z, 0, 1, t + BASE, ground, 0.03); 90 const at = (a, d) => [x + Math.cos(a) * d * HEX, z + Math.sin(a) * d * HEX]; 91 if (PEAK_HEIGHT[shape]) { 92 const k = (0.8 + 0.25 * random(c, r, 2)) * HEX; 93 const a = random(c, r, 1) * Math.PI; 94 put(mount, i, x, t, z, a, k, PEAK_HEIGHT[shape] * k * 1.6, b.rock, 0.05); 95 put(snow, i, x, t, z, a, k, PEAK_HEIGHT[shape] * k * 1.6, SNOW, 0.02); 96 } 97 const open = shape === SHAPE.PLAIN || shape === SHAPE.HILL; 98 if (open && flags & FLAG.FOREST) { 99 const kind = b.canopy; 100 const base = random(c, r, 3) * Math.PI * 2; 101 for (let n = 0; n < TREES; n++) { 102 const [px, pz] = at(base + (n / TREES) * Math.PI * 2, 0.3 + 0.3 * random(c, r, 30 + n)); 103 const tall = (kind === 'round' || kind === 'bog' ? 0.6 : kind === 'dead' ? 0.95 : 0.78) + 0.35 * random(c, r, 4 + n); 104 const a = random(c, r, 9 + n) * 7; 105 const k = tall * HEX; 106 put(trunks, i, px, t, pz, a, HEX, TREE_TRUNK[kind] * k, kind === 'dead' ? BURNT : TRUNK_PAINT, 0.04); 107 put(canopies[kind], i, px, t, pz, a, k, k, b.tree, 0.07); 108 if (kind === 'frostpine') put(tips, i, px, t, pz, a, k, k, SNOW, 0.02); 109 } 110 } else if (open && !(flags & FLAG.ROAD)) { 111 if (random(c, r, 20) < 0.12) { 112 const [px, pz] = at(random(c, r, 21) * 7, 0.15 + 0.4 * random(c, r, 22)); 113 const k = (0.5 + 0.3 * random(c, r, 23)) * HEX; 114 put(rocks[b.rocks], i, px, t, pz, random(c, r, 24) * 7, k, k, b.rock, 0.06); 115 } 116 if (b.tufts) { 117 for (let n = 0; n < 3; n++) { 118 const [px, pz] = at(random(c, r, 40 + n) * 7, 0.2 + 0.55 * random(c, r, 50 + n)); 119 put(tufts, i, px, t, pz, random(c, r, 60 + n) * 7, (1.1 + random(c, r, 70 + n)) * HEX, (1 + random(c, r, 80 + n) * 0.8) * HEX, new THREE.Color(b.tree).offsetHSL(0, 0, 0.08), 0.06); 120 } 121 } 122 } 123 } 124 } 125 126 for (let r = -BEYOND; r < H + BEYOND; r++) { 127 for (let c = -BEYOND; c < W + BEYOND; c++) { 128 if (c >= 0 && r >= 0 && c < W && r < H) continue; 129 const ec = Math.max(0, Math.min(W - 1, c)); 130 const er = Math.max(0, Math.min(H - 1, r)); 131 const [shape, biome, h] = look(ec, er); 132 const { x, z } = hexToWorld(c, r).sub(center); 133 const far = Math.max(ec - c, c - ec, er - r, r - er); 134 if (shape <= SHAPE.SHALLOW) { 135 put(outerTiles, -1, x, -BASE, z, 0, 1, WATER_TOP[0] + BASE - far * 0.02, DEEP, 0.03); 136 continue; 137 } 138 const b = BIOMES[biome]; 139 const t = (h + 1 + Math.floor(random(c, r, 5) * 3)) * STEP; 140 put(outerTiles, -1, x, -BASE, z, 0, 1, t + BASE, b.rock, 0.04); 141 const k = (0.85 + 0.35 * random(c, r, 2)) * HEX; 142 put(outerPeaks, -1, x, t, z, random(c, r, 1) * Math.PI, k, PEAK_HEIGHT[SHAPE.PEAK] * k * (1.4 + 0.6 * random(c, r, 6)), b.rock, 0.06); 143 } 144 } 145 146 const group = new THREE.Group(); 147 const byHex = Array.from({ length: W * H }, () => []); 148 for (const l of layers) { 149 const material = new THREE.MeshStandardMaterial({ color: '#ffffff', roughness: 0.9, flatShading: true, vertexColors: !!l.vertexColors }); 150 if (l.sway) sway(material, l.sway); 151 const mesh = new THREE.InstancedMesh(l.geometry, material, Math.max(1, l.items.length)); 152 mesh.count = l.items.length; 153 mesh.castShadow = !!l.cast; 154 mesh.frustumCulled = false; 155 mesh.receiveShadow = true; 156 if (!l.pick) mesh.raycast = () => {}; 157 l.items.forEach((it, k) => { 158 if (l.outer) { 159 mesh.setMatrixAt(k, it.matrix); 160 mesh.setColorAt(k, it.paint.multiplyScalar(DIM)); 161 return; 162 } 163 mesh.setMatrixAt(k, l === tiles ? sunk(it, LOW) : ZERO); 164 mesh.setColorAt(k, l === tiles ? new THREE.Color(PRIMER).multiplyScalar(HIDDEN) : it.paint); 165 byHex[it.hex].push([l, k]); 166 }); 167 l.mesh = mesh; 168 group.add(mesh); 169 } 170 171 const rising = []; 172 const state = new Uint8Array(W * H); 173 const explored = new Uint8Array(W * H); 174 const primer = new THREE.Color(PRIMER); 175 const tint = new THREE.Color(); 176 let kept = []; 177 const up = new Uint8Array(W * H); 178 const stand = (i, on) => { 179 for (const [l, k] of byHex[i] || []) { 180 if (l === tiles) continue; 181 l.mesh.setMatrixAt(k, on && up[i] ? l.items[k].matrix : ZERO); 182 l.mesh.instanceMatrix.needsUpdate = true; 183 } 184 }; 185 function reveal(px, py, radius) { 186 const touched = new Set(); 187 for (let r = 0; r < H; r++) { 188 for (let c = 0; c < W; c++) { 189 const i = r * W + c; 190 const near = hexDistance(c, r, px, py) <= radius; 191 if (near) explored[i] = 1; 192 const next = near ? 2 : explored[i]; 193 if (next === state[i]) continue; 194 if (!state[i]) rising.push({ i, t0: null }); 195 state[i] = next; 196 for (const [l, k] of byHex[i]) { 197 const it = l.items[k]; 198 l.mesh.setColorAt(k, next === 2 ? it.paint : tint.copy(it.paint).lerp(primer, SEEN)); 199 touched.add(l); 200 } 201 } 202 } 203 for (const l of touched) { 204 l.mesh.instanceMatrix.needsUpdate = true; 205 l.mesh.instanceColor.needsUpdate = true; 206 } 207 } 208 209 function shelter(list) { 210 for (const i of kept) if (!list.includes(i)) stand(i, !cleared.includes(i)); 211 kept = list; 212 for (const i of kept) stand(i, false); 213 } 214 215 function update(now) { 216 for (let n = rising.length - 1; n >= 0; n--) { 217 const r = rising[n]; 218 r.t0 ??= now + Math.random() * 0.15; 219 const k = Math.max(0, Math.min(1, (now - r.t0) / RISE)); 220 const e = 1 + 2.2 * Math.pow(k - 1, 3) + 1.2 * Math.pow(k - 1, 2); 221 for (const [l, j] of byHex[r.i]) if (l === tiles) l.mesh.setMatrixAt(j, sunk(l.items[j], LOW + (l.items[j].h - LOW) * e)); 222 tiles.mesh.instanceMatrix.needsUpdate = true; 223 if (k < 1) continue; 224 rising.splice(n, 1); 225 up[r.i] = 1; 226 stand(r.i, !kept.includes(r.i) && !cleared.includes(r.i)); 227 } 228 } 229 230 let cleared = []; 231 function arena(px, py, radius) { 232 for (const i of cleared) stand(i, !kept.includes(i)); 233 cleared = []; 234 if (px == null) return; 235 for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) if (hexDistance(c, r, px, py) <= radius) cleared.push(r * W + c); 236 for (const i of cleared) stand(i, false); 237 } 238 239 const hexOf = (instanceId) => { 240 const i = tiles.items[instanceId].hex; 241 return { x: i % W, y: Math.floor(i / W) }; 242 }; 243 const seen = (c, r) => explored[r * W + c] === 1; 244 return { group, pick: tiles.mesh, hexOf, pos, walkable, reveal, shelter, arena, seen, center, look, update }; 245}