2026-10-29 13:38:34 +00:00
|
|
|
// 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 { type Face, hash, plane, type Poly, project, rect, scale, toPolys, type V2, type V3 } from './iso'
|
2026-10-29 14:50:07 +00:00
|
|
|
import type { Tool } from './life'
|
2026-10-29 13:38:34 +00:00
|
|
|
import { broadleaf, buildingModel, bush, C, conifer, rock } from './models'
|
|
|
|
|
import { disc, ground, shadow, U, wallSegments } from './scene'
|
|
|
|
|
|
|
|
|
|
export interface Closeup {
|
|
|
|
|
viewBox: string
|
|
|
|
|
width: number
|
|
|
|
|
height: number
|
|
|
|
|
terrain: Poly[]
|
|
|
|
|
decals: Poly[]
|
2026-10-29 14:27:40 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-10-29 14:50:07 +00:00
|
|
|
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 }; 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 }[]
|
2026-10-29 14:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Walk {
|
|
|
|
|
d: string
|
2026-10-29 14:50:07 +00:00
|
|
|
/** Seconds one way and seconds of work at the spot. */
|
|
|
|
|
walk: number
|
|
|
|
|
work: number
|
|
|
|
|
begin: number
|
|
|
|
|
tool: Tool | null
|
2026-10-29 14:27:40 +00:00
|
|
|
carry: string | null
|
2026-10-29 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Kind = 'timber' | 'clay' | 'iron' | 'farm' | 'wall' | 'core'
|
|
|
|
|
const KIND: Partial<Record<BuildingType, Kind>> = { timber_camp: 'timber', clay_pit: 'clay', iron_mine: 'iron', farm: 'farm', wall: 'wall' }
|
|
|
|
|
const GROUND: Record<Kind, string> = {
|
|
|
|
|
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<Kind, string | null> = { timber: C.wood, clay: C.clay, iron: C.ore, farm: C.wheat, wall: null, core: C.plank }
|
2026-10-29 14:50:07 +00:00
|
|
|
/** What workers work with at the spot; core buildings' workers carry crates. */
|
|
|
|
|
const TOOL: Record<Kind, Tool | null> = { timber: 'axe', clay: 'shovel', iron: 'pick', farm: 'sickle', wall: 'spear', core: null }
|
2026-10-29 13:38:34 +00:00
|
|
|
/** 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(' ')
|
|
|
|
|
|
2026-10-29 14:50:07 +00:00
|
|
|
export function buildCloseup(type: BuildingType, level: number, workers: number, aspect = 2, upgrading = false): Closeup {
|
2026-10-29 13:38:34 +00:00
|
|
|
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 }]
|
2026-10-29 14:27:40 +00:00
|
|
|
/** Where workers go; each leaves the building on the side that faces their spot. */
|
2026-10-29 13:38:34 +00:00
|
|
|
const spots: V2[] = []
|
|
|
|
|
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) })
|
|
|
|
|
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 }
|
|
|
|
|
|
2026-10-29 14:27:40 +00:00
|
|
|
// 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.
|
2026-10-29 13:38:34 +00:00
|
|
|
const shown = Math.min(MAX_WALKERS, Math.max(0, workers))
|
2026-10-29 14:27:40 +00:00
|
|
|
const walkers: { key: string; depth: number; walk: Walk }[] = []
|
2026-10-29 14:50:07 +00:00
|
|
|
const work = kind === 'core' ? 1.5 : kind === 'wall' ? 2.5 : 4.5
|
2026-10-29 13:38:34 +00:00
|
|
|
for (let i = 0; i < shown && spots.length; i++) {
|
|
|
|
|
const spot = spots[i % spots.length]!
|
2026-10-29 14:27:40 +00:00
|
|
|
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)
|
2026-10-29 14:50:07 +00:00
|
|
|
const walk = Math.max(3.5, (length / U) * 1.2)
|
2026-10-29 14:27:40 +00:00
|
|
|
walkers.push({
|
|
|
|
|
key: `walker${i}`,
|
|
|
|
|
depth: Math.min(...path.map(([x, y]) => x + y)),
|
2026-10-29 14:50:07 +00:00
|
|
|
walk: { d: line(path), walk, work, begin: -(i * (walk * 2 + work)) / Math.max(1, shown), tool: TOOL[kind], carry: CARRY[kind] },
|
2026-10-29 14:27:40 +00:00
|
|
|
})
|
2026-10-29 13:38:34 +00:00
|
|
|
}
|
2026-10-29 14:50:07 +00:00
|
|
|
// A builder hammers at the building while it is being upgraded.
|
|
|
|
|
const life: { key: string; depth: number; life: { at: V2; facing: 1 | -1; tool: Tool | null } }[] = []
|
|
|
|
|
if (upgrading && kind !== 'wall') {
|
|
|
|
|
const [x, y] = at(deg(15), S * 1.35)
|
|
|
|
|
life.push({ key: 'builder', depth: x + y, life: { at: screen([x, y]), facing: -1, tool: 'hammer' } })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 })
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-29 13:38:34 +00:00
|
|
|
|
2026-10-29 14:50:07 +00:00
|
|
|
const layers = [...objects, ...walkers, ...life].sort((a, b) => a.depth - b.depth)
|
2026-10-29 13:38:34 +00:00
|
|
|
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])),
|
2026-10-29 14:50:07 +00:00
|
|
|
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 }
|
|
|
|
|
}),
|
2026-10-29 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
}
|