// A close-up of one building, for its own screen: the building larger, the ground and // props of its kind around it, forest beyond, and its workers walking between the // building and the spots where they work (trees for a timber camp, pits for a clay pit, // rocks for an iron mine, fields for a farm, the wall walk for the wall). import type { BuildingType } from '~/types/game' import { box, DOOR, type Face, hash, ngon, plane, type Poly, project, pyramid, rect, scale, toPolys, type V2, type V3 } from './iso' import type { Tool } from './life' import { broadleaf, buildingModel, bush, C, conifer, rock } from './models' import { disc, ground, guardSpots, guardsAt, shadow, U, wallSegments } from './scene' export interface Closeup { viewBox: string width: number height: number terrain: Poly[] decals: Poly[] /** * Back to front: objects, and one entry per worker shown (a screen path walked there * and back, with what they carry), sorted in among the objects by depth. */ layers: ( | { key: string; polys: Poly[]; walk?: undefined; life?: undefined } | { key: string; walk: Walk; polys?: undefined; life?: undefined } | { key: string; life: { at: V2; facing: 1 | -1; tool: Tool | null; drill?: boolean; guard?: boolean }; polys?: undefined; walk?: undefined } )[] /** Sheep at a farm, chickens round the core buildings. */ herds: { kind: 'sheep' | 'chicken'; d: string; dur: number; begin: number }[] birds: { d: string; dur: number; begin: number }[] } export interface Walk { d: string /** Seconds one way and seconds of work at the spot. */ walk: number work: number begin: number tool: Tool | null carry: string | null /** Seconds at the start between trips, out of sight when they live in the building. */ rest?: number indoors?: boolean } /** What is going on at the building: an order running or waiting, units in training. */ export interface CloseupState { upgrading?: boolean demolishing?: boolean queued?: boolean training?: boolean /** Units standing in the village, for the guards at the door. */ garrison?: number } type Kind = 'timber' | 'clay' | 'iron' | 'farm' | 'wall' | 'core' const KIND: Partial> = { timber_camp: 'timber', clay_pit: 'clay', iron_mine: 'iron', farm: 'farm', wall: 'wall' } const GROUND: Record = { timber: '#76703f', clay: '#8d5a3b', iron: '#7c776a', farm: '#7f8f4b', wall: '#8c7350', core: '#a3885c', } /** What a worker carries home: logs, clay, ore, sheaves; core buildings move crates. */ const CARRY: Record = { timber: C.wood, clay: C.clay, iron: C.ore, farm: C.wheat, wall: null, core: C.plank } /** What workers work with at the spot; core buildings' workers carry crates. */ const TOOL: Record = { timber: 'axe', clay: 'shovel', iron: 'pick', farm: 'sickle', wall: 'spear', core: null } /** Workers drawn at most; more would crowd the scene. */ const MAX_WALKERS = 8 const S = U * 1.7 const deg = (d: number) => (d * Math.PI) / 180 const at = (a: number, r: number): V2 => [Math.cos(a) * r, Math.sin(a) * r] const screen = ([x, y]: V2, z = 1.2) => project([x, y, z]) const line = (pts: V2[]) => pts.map((p, i) => `${i ? 'L' : 'M'}${screen(p).map((v) => v.toFixed(1)).join(' ')}`).join(' ') export function buildCloseup(type: BuildingType, level: number, workers: number, aspect = 2, state: CloseupState = {}): Closeup { const kind = KIND[type] ?? 'core' const objects: { key: string; depth: number; polys: Poly[] }[] = [] const decals: Face[] = [plane(disc(0, 0, S * 2.3, 16, 7, 0.5), GROUND[kind])] const shadows: Face[] = [] const taken: { x: number; y: number; r: number }[] = [{ x: 0, y: 0, r: S * 1.5 }] /** Where workers go; each leaves the building on the side that faces their spot. */ const spots: V2[] = [] /** Just outside the building's door, if it has one. */ let door: V2 | null = null const put = (key: string, x: number, y: number, faces: Face[], r: number) => { objects.push({ key, depth: x + y, polys: toPolys(faces) }) taken.push({ x, y, r }) } if (kind === 'wall') { // A small fort: the wall round an empty yard, guards walking along it. const Rw = S * 2.2 wallSegments(Rw, level, [deg(45)]).forEach((seg, i) => objects.push({ key: `w${i}`, depth: seg.depth, polys: toPolys(seg.faces) })) taken[0] = { x: 0, y: 0, r: Rw + U } } else { const model = buildingModel(type, level, S) objects.push({ key: 'building', depth: 0, polys: toPolys(model.faces) }) const doorFace = model.faces.find((f) => f.color === DOOR) if (doorFace) door = [doorFace.pts.reduce((n, p) => n + p[0], 0) / doorFace.pts.length + U * 0.3, doorFace.pts.reduce((n, p) => n + p[1], 0) / doorFace.pts.length] shadows.push(shadow(0, 0, S * 0.95, scale(GROUND[kind], 0.8), 3)) } if (kind === 'timber') { for (let i = 0; i < 6; i++) { const [x, y] = at(deg(-80 + i * 34 + (hash(i, 1) - 0.5) * 14), S * (2.4 + 0.5 * hash(i, 2))) put(`tree${i}`, x, y, conifer(x, y, U * (1.4 + 0.4 * hash(i, 3)), i), U * 0.7) spots.push([x * 0.85, y * 0.85]) } } else if (kind === 'clay') { for (let i = 0; i < 4; i++) { const [x, y] = at(deg(-20 + i * 42), S * 2.2) decals.push(plane(disc(x, y, U * 0.75, 9, 20 + i, 0.8), C.clayWet)) spots.push([x, y]) } } else if (kind === 'iron') { for (let i = 0; i < 5; i++) { const [x, y] = at(deg(150 + i * 26), S * (2.5 + 0.3 * hash(i, 4))) put(`rock${i}`, x, y, rock(x, y, U * (1.5 + 0.6 * hash(i, 5)), 40 + i, i % 2 ? C.stoneDark : C.rock), U * 0.9) spots.push([x * 0.8, y * 0.8]) } } else if (kind === 'farm') { for (let i = 0; i < 4; i++) { const [x, y] = at(deg(-40 + i * 50), S * 2.5) decals.push(plane(rect(x, y, U * 2.2, U * 1.5).map(([px, py]): V3 => [px, py, 0.7]), i % 2 ? C.wheat : C.field)) taken.push({ x, y, r: U * 1.4 }) spots.push([x, y]) } } else if (kind === 'wall') { // Guards walk stretches of the wall walk. for (let i = 0; i < 4; i++) spots.push(at(deg(i * 90 + 20), S * 2.0)) } else { for (let i = 0; i < 4; i++) spots.push(at(deg(-10 + i * 55), S * 2.0)) } // Forest and bushes beyond, thicker further out. const R = S * 2.75 const f = U * 1.25 for (let i = -14; i <= 14; i++) { for (let j = -14; j <= 14; j++) { const x = (i + hash(i, j, 61) * 0.8) * f const y = (j + hash(i, j, 62) * 0.8) * f const r = Math.hypot(x, y) if (r < S * 2.6 || r > S * 7) continue if (taken.some((t) => Math.hypot(x - t.x, y - t.y) < t.r + U * 0.4)) continue if (Math.abs(Math.atan2(y, x) - deg(45)) < 0.25 && r < S * 5) continue const density = 0.15 + 0.6 * Math.min(1, (r - S * 2.6) / (S * 2.5)) if (hash(i, j, 63) > density) continue const kindRoll = hash(i, j, 64) const size = U * (1.0 + 0.5 * hash(i, j, 65)) const seed = i * 131 + j put(`t${i}:${j}`, x, y, kindRoll < 0.15 ? bush(x, y, size * 0.8, seed) : kindRoll < 0.7 ? conifer(x, y, size, seed) : broadleaf(x, y, size, seed), size * 0.3) } } // Frame: the building, its spots and a ring of land round them, widened to the stage. const pts: V2[] = [] for (let i = 0; i < 24; i++) pts.push(screen(at((i / 24) * Math.PI * 2, R), 0)) let minX = Math.min(...pts.map((p) => p[0])) let maxX = Math.max(...pts.map((p) => p[0])) let minY = Math.min(...pts.map((p) => p[1])) - S * 1.2 let maxY = Math.max(...pts.map((p) => p[1])) let w = maxX - minX let h = maxY - minY if (w / h < aspect) { minX -= (h * aspect - w) / 2 w = h * aspect } else { minY -= (w / aspect - h) / 2 h = w / aspect } maxX = minX + w maxY = minY + h const view = { x: minX, y: minY, w, h } // A worker sorts at the depth of the point of their path furthest back, so the building // hides those who work behind it and those in front walk over it. const shown = Math.min(MAX_WALKERS, Math.max(0, workers)) const walkers: { key: string; depth: number; walk: Walk }[] = [] const work = kind === 'core' ? 1.5 : kind === 'wall' ? 2.5 : 4.5 for (let i = 0; i < shown && spots.length; i++) { const spot = spots[i % spots.length]! const path: V2[] = kind === 'wall' ? [0, 15, 30, 45, 60, 75, 90].map((d) => at(deg(i * 90 + d), S * 2.0)) : [at(Math.atan2(spot[1], spot[0]), S * 1.5), spot] const length = path.slice(1).reduce((n, p, k) => n + Math.hypot(p[0] - path[k]![0], p[1] - path[k]![1]), 0) const walk = Math.max(3.5, (length / U) * 1.2) walkers.push({ key: `walker${i}`, depth: Math.min(...path.map(([x, y]) => x + y)), walk: { d: line(path), walk, work, begin: -(i * (walk * 2 + work)) / Math.max(1, shown), tool: TOOL[kind], carry: CARRY[kind] }, }) } // A builder works at the building while an order runs: a hammer to build, a pick to tear // down. Any order for it piles materials in front, and a porter brings more. const life: { key: string; depth: number; life: { at: V2; facing: 1 | -1; tool: Tool | null; drill?: boolean; guard?: boolean } }[] = [] const person = (key: string, [x, y]: V2, facing: 1 | -1, tool: Tool | null, drill = false, guard = false) => life.push({ key, depth: x + y, life: { at: screen([x, y]), facing, tool, drill, guard } }) // Soldiers stand guard at the door, as in the village. if (door) guardSpots(type, door, guardsAt(type, state.garrison ?? 0), U * 1.6).forEach((p, k) => person(`guard${k}`, p, 1, 'spear', false, true)) if ((state.upgrading || state.demolishing) && kind !== 'wall') person('builder', at(deg(15), S * 1.35), -1, state.demolishing ? 'pick' : 'hammer') if ((state.queued || state.upgrading) && kind !== 'wall') { const pile = at(deg(80), S * 1.45) objects.push({ key: 'pile', depth: pile[0] + pile[1], polys: toPolys([ ...box(pile[0], pile[1], U * 0.9, U * 0.4, 0, U * 0.13, C.plank, C.wood), ...box(pile[0] + U * 0.05, pile[1] - U * 0.05, U * 0.85, U * 0.36, U * 0.13, U * 0.13, C.wood, C.plank), ...pyramid(ngon(pile[0] + U * 0.7, pile[1] + U * 0.2, U * 0.26, 5), 0, U * 0.26, C.stone), ]), }) const from = at(deg(100), S * 3.4) const to: V2 = [pile[0] - U * 0.2, pile[1] + U * 0.5] const walk = Math.hypot(to[0] - from[0], to[1] - from[1]) / U walkers.push({ key: 'porter', depth: Math.max(from[0] + from[1], to[0] + to[1]), walk: { d: line([from, to]), walk, work: 1.4, begin: -2, tool: null, carry: C.plank, rest: 2, indoors: true } }) } // Units in training drill in front, an instructor facing them. if (state.training) { for (let k = 0; k < 3; k++) person(`drill${k}`, [S * 1.95, -S * 0.55 + k * U * 0.85], 1, 'spear', true) person('instructor', [S * 1.95 + U * 1.5, S * 0.1], -1, null) } // Someone steps out of the door now and then, stands a moment, and goes back in. if (door && kind === 'core') { const out = at(deg(25), S * 2.1) walkers.push({ key: 'visitor', depth: Math.max(door[0] + door[1], out[0] + out[1]), walk: { d: line([door, out]), walk: Math.hypot(out[0] - door[0], out[1] - door[1]) / U * 0.9, work: 3, begin: -4, tool: null, carry: null, rest: 7, indoors: true }, }) } // Animals and birds. const loop = (cx: number, cy: number, r: number, seed: number) => line([0, 1, 2, 3, 0].map((i): V2 => { const a = (i / 4) * Math.PI * 2 + hash(seed, 1) * 6 return [cx + Math.cos(a) * r * (0.7 + 0.5 * hash(seed, i + 2)), cy + Math.sin(a) * r] })) const herds: Closeup['herds'] = [] if (kind === 'farm') { for (let k = 0; k < 3; k++) { const [x, y] = at(deg(200 + k * 30), S * 2.1) herds.push({ kind: 'sheep', d: loop(x, y, U * 0.8, 400 + k), dur: 22 + k * 5, begin: -k * 6 }) } } else if (kind === 'core') { for (let k = 0; k < 2; k++) { const [x, y] = at(deg(100 + k * 40), S * 1.8) herds.push({ kind: 'chicken', d: loop(x, y, U * 0.5, 420 + k), dur: 12 + k * 4, begin: -k * 5 }) } } const layers = [...objects, ...walkers, ...life].sort((a, b) => a.depth - b.depth) return { viewBox: `${view.x.toFixed(1)} ${view.y.toFixed(1)} ${w.toFixed(1)} ${h.toFixed(1)}`, width: w, height: h, terrain: ground(view), decals: [...decals, ...shadows].flatMap((face) => toPolys([face])), layers: layers.map((l) => ('walk' in l ? { key: l.key, walk: l.walk } : 'life' in l ? { key: l.key, life: l.life } : { key: l.key, polys: l.polys })), herds, birds: [0, 1].map((k) => { const y0 = view.y + h * (0.1 + 0.12 * k) return { d: `M${(view.x - U).toFixed(1)} ${y0.toFixed(1)} L${(view.x + w + U).toFixed(1)} ${(y0 - h * 0.1).toFixed(1)}`, dur: 16 + k * 7, begin: -k * 9 } }), } }