feat: map villages as miniatures of the village diorama: one demo village per tier painted once per session in a worker, worn for barbarians; relation as a flag (a barbarian's dot) at the field's top-left, white corners round the current village

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-03 10:59:27 +01:00
parent aba98d7b1c
commit 45886713c3
9 changed files with 385 additions and 512 deletions

View File

@ -25,7 +25,7 @@ const { current } = useCurrentWorld()
</dd>
<dt class="text-ink-500">{{ $t('map.info.relation') }}</dt>
<dd class="inline-flex items-center gap-1.5">
<MapLegendSwatch :colour="RELATION_COLOURS[village.relation]" />{{ $t(`map.legend.${village.relation}`) }}
<MapLegendSwatch :colour="RELATION_COLOURS[village.relation]" :dot="village.relation === 'barbarian'" />{{ $t(`map.legend.${village.relation}`) }}
</dd>
<dt class="text-ink-500">{{ $t('map.info.points') }}</dt>
<dd class="tabular-nums">{{ village.points }}</dd>

View File

@ -1,20 +1,25 @@
<script setup lang="ts">
defineProps<{
/** The relation colour of the plot's edge. */
/** The relation colour of the marker. */
colour: string
/** A crown fort: the gold crown on the plot. */
/** A barbarian village's dot instead of a player's flag. */
dot?: boolean
/** A crown fort: the gold crown. */
crown?: boolean
}>()
</script>
<!-- A village on the map in small: the round plot of earth, its edge in the relation colour. -->
<!-- A village's marker on the map in small: the flag (or a barbarian's dot) in the relation colour, the crown of a crown fort. -->
<template>
<svg viewBox="-50 -50 100 100" class="size-4 shrink-0" aria-hidden="true">
<circle r="49" fill="#2b2118" />
<circle r="36" fill="#6c5940" :stroke="colour" stroke-width="22" />
<svg viewBox="0 0 20 20" class="size-4 shrink-0" aria-hidden="true">
<g v-if="crown">
<polygon points="-24,14 -24,-14 -12,0 0,-22 12,0 24,-14 24,14" fill="#d9b44a" />
<rect x="-24" y="6" width="48" height="8" fill="#a8842e" />
<polygon points="2,16 2,6 6.5,10.5 10,3 13.5,10.5 18,6 18,16" fill="#d9b44a" stroke="#6b4f16" stroke-width="1.2" stroke-linejoin="round" />
<rect x="2" y="13" width="16" height="3" fill="#a8842e" />
</g>
<circle v-else-if="dot" cx="10" cy="10" r="6" :fill="colour" stroke="#2b2118" stroke-width="1.6" />
<g v-else>
<rect x="3" y="2" width="2.2" height="17" fill="#2b2118" />
<polygon points="5.2,2.5 18,7.5 5.2,12.5" :fill="colour" stroke="#2b2118" stroke-width="1.4" stroke-linejoin="round" />
</g>
</svg>
</template>

View File

@ -1,22 +1,32 @@
<script setup lang="ts">
import { MAP_VILLAGE_LOOKS } from '~/diorama/mapVillage'
import { MAP_LOOKS } from '~/diorama/mapTiles'
// Every village look once, for <use> on the map: the picture of its demo village (painted
// once per session, useMapTiles), and its marker at the field's top-left in the relation
// colour (currentColor, set on each <use>): a flag for a player's village, a dot for a
// barbarian's, the gold crown on a crown fort. A second layout shows the first until it is
// painted; before any, a bare clearing.
const urls = useMapTiles()
const href = (id: string) => urls[id] ?? urls[id.replace(/-1$/, '-0')]
const looks = MAP_LOOKS.map((l) => ({ id: l.id, crown: l.tier === 'crown', dot: l.worn }))
</script>
<!--
Every village picture once, for <use> on the map: the round plot seen from above with its
edge and flag in the relation colour (currentColor, set on each <use>).
-->
<template>
<symbol v-for="l in MAP_VILLAGE_LOOKS" :id="l.id" :key="l.id" v-once viewBox="-50 -50 100 100" overflow="visible" stroke-linejoin="round" stroke-linecap="round">
<path
v-for="(s, i) in l.shapes"
:key="i"
:d="s.d"
:fill="s.fill ?? 'none'"
:stroke="s.stroke"
:stroke-width="s.width"
:stroke-dasharray="s.dash"
:stroke-linecap="s.dash ? 'butt' : undefined"
/>
<symbol v-for="l in looks" :id="l.id" :key="l.id" viewBox="0 0 100 100" overflow="visible">
<image v-if="href(l.id)" :href="href(l.id)" width="100" height="100" />
<g v-else>
<ellipse cx="50" cy="60" rx="40" ry="19" fill="#6c6f48" />
<ellipse cx="50" cy="60" rx="34" ry="16" fill="#6d6b48" />
<ellipse cx="50" cy="60" rx="29" ry="13" fill="#6d6748" />
</g>
<circle v-if="l.dot" cx="10" cy="10" r="5" fill="currentColor" stroke="#2b2118" stroke-width="1.4" />
<g v-else>
<rect x="5.5" y="3" width="2.4" height="25" fill="#2b2118" />
<polygon points="7.9,3.5 26,9.5 7.9,15.5" fill="currentColor" stroke="#2b2118" stroke-width="1.4" stroke-linejoin="round" />
</g>
<g v-if="l.crown">
<polygon points="78,14 78,4 82.5,8.5 87,2 91.5,8.5 96,4 96,14" fill="#d9b44a" stroke="#6b4f16" stroke-width="1" stroke-linejoin="round" />
<rect x="78" y="11" width="18" height="3" fill="#a8842e" />
</g>
</symbol>
</template>

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import { mapVillageSymbol } from '~/diorama/mapVillage'
import { mapLookOf } from '~/diorama/mapTiles'
import type { MapMovement, MapVillage, MapView } from '~/types/map'
import type { MarkerRelation } from '~/utils/relations'
@ -88,7 +88,17 @@ const villages = computed(() => {
return (props.view?.villages ?? [])
.filter((v) => v.x >= r.x0 && v.x < r.x1 && v.y >= r.y0 && v.y < r.y1)
.sort((a, b) => a.y - b.y || a.x - b.x)
.map((v) => ({ id: v.id, x: v.x, y: v.y, look: mapVillageSymbol(v), colour: RELATION_COLOURS[relation(v)] }))
.map((v) => ({ id: v.id, x: v.x, y: v.y, look: `#${mapLookOf(v)}`, colour: RELATION_COLOURS[relation(v)] }))
})
/** White corners round the field of the village the player is in, when it is in the region. */
const currentCorners = computed(() => {
const v = villages.value.find((v) => v.id === props.current)
if (!v) return null
const F = props.field
const [x0, y0, x1, y1] = [v.x * F + 2, v.y * F + 2, (v.x + 1) * F - 2, (v.y + 1) * F - 2]
const k = F * 0.24
return `M${x0} ${y0 + k}V${y0}H${x0 + k}M${x1 - k} ${y0}H${x1}V${y0 + k}M${x1} ${y1 - k}V${y1}H${x1 - k}M${x0 + k} ${y1}H${x0}V${y1 - k}`
})
// Groups on the march: a line from village to village and the group where it is now,
@ -273,6 +283,10 @@ function onKey(e: KeyboardEvent) {
/>
<MapVillages :villages="villages" :field="field" />
<g v-if="currentCorners" fill="none" stroke-linecap="square" pointer-events="none">
<path :d="currentCorners" stroke="#1f150c" stroke-width="4.5" />
<path :d="currentCorners" :stroke="RELATION_COLOURS.current" stroke-width="2" />
</g>
<!-- Groups on the march, over the villages; the line and the group open the group's card. -->
<g v-for="g in marches" :key="`m${g.m.id}`">

View File

@ -0,0 +1,60 @@
import { MAP_LOOKS, mapTileDrawing, paintTile } from '~/diorama/mapTiles'
/** Pixels per side of a village picture: twice the map's biggest field, so it stays crisp. */
const TILE_PX = 160
/** Image URLs by look id, filled in as the pictures are painted; kept for the whole session. */
const urls = shallowReactive<Record<string, string>>({})
let started = false
// The first layout of every look first, so each kind shows its own picture soon; the
// other layouts after, standing in with the first until they come.
const ORDER = [...MAP_LOOKS.filter((l) => l.variant === 0), ...MAP_LOOKS.filter((l) => l.variant === 1)].map((l) => l.id)
/**
* The map's village pictures: each look painted once from its demo village's scene, in a
* worker so the map's first paint never waits; without one, a look per idle moment.
*/
export function useMapTiles() {
if (!started && import.meta.client) {
started = true
performance.mark('map-tiles:start')
if (typeof Worker !== 'undefined' && typeof OffscreenCanvas !== 'undefined') inWorker()
else onMainThread(ORDER)
}
return urls
}
function done(id: string, url: string) {
urls[id] = url
if (Object.keys(urls).length === ORDER.length) performance.mark('map-tiles:done')
}
function inWorker() {
const worker = new Worker(new URL('../workers/mapTiles.worker.ts', import.meta.url), { type: 'module' })
worker.onmessage = (e: MessageEvent<{ id: string; blob: Blob }>) => {
done(e.data.id, URL.createObjectURL(e.data.blob))
if (Object.keys(urls).length === ORDER.length) worker.terminate()
}
worker.onerror = () => {
worker.terminate()
onMainThread(ORDER.filter((id) => !urls[id]))
}
worker.postMessage({ ids: ORDER, size: TILE_PX })
}
function onMainThread(ids: string[]) {
const next = () => {
const id = ids.shift()
if (!id) return
const canvas = document.createElement('canvas')
canvas.width = TILE_PX
canvas.height = TILE_PX
paintTile(canvas.getContext('2d')!, mapTileDrawing(MAP_LOOKS.find((l) => l.id === id)!), TILE_PX)
done(id, canvas.toDataURL('image/png'))
later(next)
}
later(next)
}
const later = (fn: () => void) => (typeof requestIdleCallback === 'function' ? requestIdleCallback(fn, { timeout: 500 }) : setTimeout(fn, 16))

254
app/diorama/mapTiles.ts Normal file
View File

@ -0,0 +1,254 @@
// Villages on the world map as miniatures of the village diorama itself, like the little
// village pictures on the Die Stämme map: one fixed demo village per points tier (which
// buildings at which levels), drawn by the very same scene as the village view, cropped
// round its core on a clearing of trodden ground that steps out into the map's grass.
// Barbarian villages are the same villages worn: colours faded and darkened, no banners, no
// fire. The crown fort is a fortress: a stone keep for a headquarters behind a high stone wall.
// Pure: the pictures are painted on a canvas once per look (useMapTiles), never per village.
import type { Building, BuildingType, VillageState } from '~/types/game'
import { hash, mix, type Poly, project, scale } from './iso'
import { buildScene, type Scene, U } from './scene'
export type MapTier = 1 | 2 | 3 | 4 | 5 | 6
/** Lowest points of tiers 2 to 6; below the first, tier 1. Die Stämme's bands, its wide 3000–8999 split at 6000. */
export const MAP_TIER_POINTS = [300, 1000, 3000, 6000, 10000] as const
/** Six sizes by points, like the village pictures on the Die Stämme map. */
export const mapTier = (points: number): MapTier => (MAP_TIER_POINTS.findIndex((p) => points < p) + 1 || 6) as MapTier
/** A picture: its tier (or the crown fort), barbarian or not, and the layout variant. */
export interface MapLook {
id: string
tier: MapTier | 'crown'
worn: boolean
variant: 0 | 1
}
export const MAP_LOOKS: MapLook[] = [
...[false, true].flatMap((worn) =>
([1, 2, 3, 4, 5, 6] as MapTier[]).flatMap((tier) => ([0, 1] as const).map((variant) => ({ id: `mv-${worn ? 'barbarian' : 'player'}-${tier}-${variant}`, tier, worn, variant }))),
),
{ id: 'mv-crown', tier: 'crown', worn: true, variant: 0 },
{ id: 'mv-crown-player', tier: 'crown', worn: false, variant: 0 },
]
/** The look a village wears: its kind and tier, the variant from its id. */
export function mapLookOf(v: { id: number; points: number; crown: boolean; player: unknown }): string {
if (v.crown) return v.player ? 'mv-crown-player' : 'mv-crown'
return `mv-${v.player ? 'player' : 'barbarian'}-${mapTier(v.points)}-${Math.abs(v.id) % 2}`
}
// The demo villages. Farms and camps stand outside in their zones; everything else in the
// core, round the headquarters, inside the wall from the second tier.
const VILLAGES: Record<MapTier | 'crown', [BuildingType, number][]> = {
1: [['headquarters', 3], ['storage', 2], ['farm', 3], ['timber_camp', 3], ['clay_pit', 2], ['iron_mine', 1], ['rally_point', 1], ['hiding_place', 1]],
2: [['headquarters', 7], ['storage', 6], ['farm', 6], ['farm', 3], ['timber_camp', 5], ['timber_camp', 3], ['clay_pit', 5], ['iron_mine', 4], ['rally_point', 1], ['hiding_place', 3], ['barracks', 3], ['wall', 3]],
3: [
['headquarters', 12], ['storage', 10], ['storage', 4], ['farm', 9], ['farm', 6], ['timber_camp', 8], ['timber_camp', 6], ['clay_pit', 8], ['iron_mine', 7],
['rally_point', 1], ['hiding_place', 5], ['barracks', 7], ['stable', 3], ['wall', 8], ['market', 4],
],
4: [
['headquarters', 17], ['storage', 15], ['storage', 9], ['farm', 13], ['farm', 10], ['timber_camp', 12], ['timber_camp', 10], ['clay_pit', 12], ['clay_pit', 8], ['iron_mine', 11],
['rally_point', 1], ['hiding_place', 6], ['barracks', 12], ['stable', 8], ['workshop', 3], ['wall', 13], ['market', 9], ['hospital', 4], ['watchtower', 3],
],
5: [
['headquarters', 22], ['storage', 19], ['storage', 14], ['farm', 17], ['farm', 15], ['timber_camp', 16], ['timber_camp', 14], ['clay_pit', 16], ['clay_pit', 12], ['iron_mine', 15], ['iron_mine', 10],
['rally_point', 1], ['hiding_place', 8], ['barracks', 16], ['stable', 12], ['workshop', 7], ['wall', 17], ['market', 13], ['hospital', 8], ['watchtower', 8], ['academy', 1],
],
6: [
['headquarters', 28], ['storage', 25], ['storage', 20], ['farm', 22], ['farm', 20], ['timber_camp', 22], ['timber_camp', 20], ['clay_pit', 22], ['clay_pit', 18], ['iron_mine', 21], ['iron_mine', 16],
['rally_point', 1], ['hiding_place', 10], ['barracks', 22], ['barracks', 12], ['stable', 18], ['workshop', 12], ['wall', 20], ['market', 18], ['hospital', 12], ['watchtower', 14], ['academy', 3],
],
crown: [
['headquarters', 30], ['wall', 20], ['storage', 20], ['rally_point', 1], ['barracks', 20], ['stable', 15], ['workshop', 10], ['hospital', 10], ['watchtower', 20], ['hiding_place', 10],
],
}
const SPACE: Partial<Record<BuildingType, number>> = {
headquarters: 9, storage: 4, farm: 4, timber_camp: 4, clay_pit: 4, iron_mine: 4, hiding_place: 1, wall: 0, rally_point: 1, watchtower: 2, barracks: 6, stable: 6, workshop: 6, hospital: 4, market: 4, academy: 9,
}
/** A demo village as the scene reads it; variant 1 lays it out in the other order, the headquarters staying first. */
function demo(tier: MapTier | 'crown', variant: 0 | 1): VillageState {
const all = VILLAGES[tier]
const levels = variant ? [all[0]!, ...all.slice(1).reverse()] : all
const buildings = levels.map(([type, level], i): Building => ({
id: i + 1, type, level, space: SPACE[type] ?? 4, priority: 5,
workers: { assigned: 0, required: 0 }, staffing: 1, production: null, upgrade: null, demolish: null, effects: [], next_effects: null, instance_share: 1,
}))
const points = tier === 'crown' ? 12000 : [150, 600, 2000, 4500, 8000, 12000][tier - 1]!
return { id: 0, points, buildings, queue: [], troops: {}, support: { here: [], away: [] }, military: [], buildable: [], movements: [] } as unknown as VillageState
}
/** A picture to paint: back to front, in scene units; `clip` limits the ground under it to the clearing. */
export interface MapTileDrawing {
/** The clearing and the steps out to the grass, outermost first. */
steps: { points: string; fill: string }[]
/** The innermost step: the village's ground is cut to it. */
clip: string
ground: Poly[]
objects: Poly[]
/** Scene units that span the field: the clearing fills more of it as the village grows. */
units: number
}
/** How much of the field's width the clearing takes, per tier. */
const FILL: Record<MapTier | 'crown', number> = { 1: 0.8, 2: 0.86, 3: 0.9, 4: 0.94, 5: 0.97, 6: 1, crown: 1 }
/** The map's grass the clearing steps out into. */
const MEADOW = '#6b7049'
const CLEARING = '#6d6748'
/** A ring round the village in scene units, wandering in and out, projected. */
function ring(r: number, seed: number): [number, number][] {
const n = 28
return Array.from({ length: n }, (_, i) => {
const a = (i / n) * Math.PI * 2
const k = r * (0.92 + 0.14 * hash(i, seed) + 0.05 * Math.sin(a * 3 + seed))
return project([Math.cos(a) * k, Math.sin(a) * k, 0])
})
}
const pts = (p: [number, number][]) => p.map(([x, y]) => `${x.toFixed(1)},${y.toFixed(1)}`).join(' ')
/** Faded and darkened, for a barbarian village: what the weather and neglect do. */
function worn(color: string): string {
const n = Number.parseInt(color.slice(1), 16)
const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255]
const grey = Math.round(r * 0.3 + g * 0.59 + b * 0.11)
const hex = `#${[grey, grey, grey].map((v) => v.toString(16).padStart(2, '0')).join('')}`
return scale(mix(color, hex, 0.5), 0.82)
}
/** The scene's roads and their ruts (flat, so lit at 1.02): at the map's size they would read as the spokes of a wheel. */
const ROADS = new Set([scale('#6b563f', 1.02), scale(mix('#6b563f', '#4e3f2f', 0.6), 1.02)])
/** Banners and fire: deep reds and the warm glow, which a barbarian village has none of. */
function bannerOrFire(color: string): boolean {
const n = Number.parseInt(color.slice(1), 16)
const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255]
return r > 60 && r > g * 1.8 && r > b * 1.8
}
/** The middle of an object's lowest edge on screen. */
function baseOf(polys: Poly[]): [number, number] {
let minX = Infinity
let maxX = -Infinity
let maxY = -Infinity
for (const p of polys) {
for (const xy of p.points.split(' ')) {
const [x, y] = xy.split(',').map(Number) as [number, number]
if (x < minX) minX = x
if (x > maxX) maxX = x
if (y > maxY) maxY = y
}
}
return [(minX + maxX) / 2, maxY]
}
/** Back on the ground plane: the world point a screen point shows. */
const groundOf = ([X, Y]: [number, number]): [number, number] => [Y + X / 2, Y - X / 2]
/** An object drawn bigger round where it stands, so a building still reads as one at the map's size. */
function enlarge(polys: Poly[], [ax, ay]: [number, number], k: number): Poly[] {
const at = (xy: string) => {
const i = xy.indexOf(',')
return `${(ax + (Number(xy.slice(0, i)) - ax) * k).toFixed(1)},${(ay + (Number(xy.slice(i + 1)) - ay) * k).toFixed(1)}`
}
return polys.map((p) => ({ ...p, points: p.points.split(' ').map(at).join(' ') }))
}
/** How much bigger buildings, trees and props draw than in the village, the wall staying where it is. */
const ENLARGE = 2.3
/** Scenes by tier and layout: a player's and a barbarian's picture share one. */
const scenes = new Map<string, Scene>()
function sceneOf(tier: MapTier | 'crown', variant: 0 | 1): Scene {
const key = `${tier}-${variant}`
let scene = scenes.get(key)
if (!scene) scenes.set(key, (scene = buildScene(demo(tier, variant), 1)))
return scene
}
/** The picture of a look, cut out of the scene of its demo village. */
export function mapTileDrawing(look: MapLook): MapTileDrawing {
const scene = sceneOf(look.tier, look.variant)
// The village's own ground (its first decal) is a disc just outside the wall: the clearing
// reaches past it to the first fields and camps, then steps out into the grass.
const disc = scene.decals[0]!.points.split(' ').map((xy) => xy.split(',').map(Number) as [number, number])
const halfWidth = Math.max(...disc.map(([x]) => Math.abs(x)))
const Rv = halfWidth / Math.SQRT2 - U * 0.25
const Rc = Rv + U * 0.15
const recolor = (p: Poly): Poly => (look.worn ? { ...p, fill: worn(p.fill), stroke: worn(p.stroke) } : p)
const keep = (p: Poly) => !(look.worn && bannerOrFire(p.fill))
const inside = (r: number) => r <= Rc * 0.98
// The steps fade into the grass whatever the village; only its own ground wears.
const clearing = look.worn ? mix(CLEARING, worn(CLEARING), 0.6) : CLEARING
const steps = [
{ r: Rc + U * 0.7, fill: mix(clearing, MEADOW, 0.7) },
{ r: Rc + U * 0.35, fill: mix(clearing, MEADOW, 0.38) },
{ r: Rc, fill: clearing },
].map((s, i) => ({ points: pts(ring(s.r, 11 + i)), fill: s.fill }))
const objects = scene.objects
.filter((o) => o.polys.length && !o.life && !o.walker)
.flatMap((o) => {
const base = baseOf(o.polys)
const [x, y] = groundOf(base)
if (!inside(Math.hypot(x, y))) return []
const polys = o.key.startsWith('wall-') ? o.polys : enlarge(o.polys, base, ENLARGE)
return polys.filter(keep).map(recolor)
})
return {
steps,
clip: steps[2]!.points,
ground: scene.decals.filter((p) => keep(p) && !ROADS.has(p.fill)).map(recolor),
objects,
units: (Math.max(...ring(Rc + U * 0.7, 11).map(([x]) => Math.abs(x))) * 2) / FILL[look.tier],
}
}
/** Paints a picture on a canvas of `size` × `size` pixels covering one field. */
export function paintTile(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, d: MapTileDrawing, size: number) {
const k = size / d.units
ctx.setTransform(k, 0, 0, k, size / 2, size * 0.6)
const path = (points: string) => {
ctx.beginPath()
let first = true
for (const xy of points.split(' ')) {
const i = xy.indexOf(',')
const x = Number(xy.slice(0, i))
const y = Number(xy.slice(i + 1))
if (first) ctx.moveTo(x, y)
else ctx.lineTo(x, y)
first = false
}
ctx.closePath()
}
ctx.lineJoin = 'round'
// A hair of each face's own colour round it closes the seams, as in the village view.
ctx.lineWidth = 0.9
for (const s of d.steps) {
path(s.points)
ctx.fillStyle = s.fill
ctx.fill()
}
ctx.save()
path(d.clip)
ctx.clip()
for (const p of d.ground) {
path(p.points)
ctx.fillStyle = p.fill
ctx.fill()
ctx.strokeStyle = p.stroke
ctx.stroke()
}
ctx.restore()
for (const p of d.objects) {
path(p.points)
ctx.fillStyle = p.fill
ctx.fill()
ctx.strokeStyle = p.stroke
ctx.stroke()
}
}

View File

@ -1,484 +0,0 @@
// Villages on the world map, seen from above like the village pictures on the Die Stämme
// map: a predefined picture per points tier (six, like its v1 to v6), hand-placed, not
// generated from counts. A round plot of trodden earth fills the field, its edge in the
// relation colour; on it the roofs seen from above (two halves either side of the ridge, or
// four facets of a hipped roof, lit from the upper left like the village, a flat shadow to
// the lower right), the yard, paths to the gate and gardens; a stake palisade from the third
// tier, a stone wall with towers from the fifth, the headquarters the biggest roof in the
// middle with the flag. Barbarian villages have the same layouts worn: darker, roofs gone
// grey and broken, a house burnt down, weeds in the yard, gaps in the palisade, no flag.
// Crown forts are a stone keep in a walled ring. Each look is drawn once as an SVG <symbol>
// (`MapVillageDefs`) and placed with <use>; the relation colour (the plot's edge and the flag)
// is `currentColor`, set per village. Two variants per tier, mirrored, picked by the village
// id. Units: a field is 100 × 100, the plot's centre at 0|0.
import { mix, scale } from './iso'
import { C } from './models'
export type MapTier = 1 | 2 | 3 | 4 | 5 | 6
/** One path of a look: the shapes of one colour and layer, merged into one element. */
export interface MapVillageShape {
d: string
fill?: string
stroke?: string
width?: number
dash?: string
}
export interface MapVillageLook {
id: string
shapes: MapVillageShape[]
}
/** Lowest points of tiers 2 to 6; below the first, tier 1. */
export const MAP_TIER_POINTS = [300, 1000, 3000, 6000, 10000] as const
/** Six sizes by points, like the village pictures on the Die Stämme map. */
export const mapTier = (points: number): MapTier => (MAP_TIER_POINTS.findIndex((p) => points < p) + 1 || 6) as MapTier
/** The relation colour, set on each <use>. */
const OWNER = 'currentColor'
/** Stands for the shadow colour of the plot's earth until the look is built. */
const SHADOW = 'shadow'
/** The plot's dark rim, so every relation colour stands off the grass. */
const RIM = '#2b2118'
const EARTH = '#6c5940'
const EARTH_WORN = '#574a3a'
/** Paths, the plaza and the yard: trodden lighter. */
const PATH = '#806a4c'
const PATH_WORN = '#655542'
const WEEDS = '#575c3c'
/** Weathered grey that a barbarian village's roofs fade to. */
const WEATHER = '#77726a'
// Light from the upper left, as in the village: what faces it keeps its colour, what faces
// away drops to two thirds and cools a little, the rest in between.
const LIGHT: [number, number] = [-0.6, -0.8]
function tone(base: string, nx: number, ny: number): string {
const d = nx * LIGHT[0] + ny * LIGHT[1]
return d > 0.35 ? scale(base, 1.05) : d < -0.35 ? mix(scale(base, 0.64), '#2a3038', 0.12) : scale(base, 0.84)
}
type Pt = [number, number]
const f = (n: number) => (Math.round(n * 10) / 10).toString()
const poly = (pts: Pt[]) => `M${pts.map(([x, y]) => `${f(x)} ${f(y)}`).join('L')}Z`
const line = (pts: Pt[]) => `M${pts.map(([x, y]) => `${f(x)} ${f(y)}`).join('L')}`
const circle = (cx: number, cy: number, r: number) => `M${f(cx - r)} ${f(cy)}a${f(r)} ${f(r)} 0 1 0 ${f(2 * r)} 0a${f(r)} ${f(r)} 0 1 0 ${f(-2 * r)} 0Z`
const box = (cx: number, cy: number, hw: number, hh: number) => poly([[cx - hw, cy - hh], [cx + hw, cy - hh], [cx + hw, cy + hh], [cx - hw, cy + hh]])
/** An arc round the plot's centre (or moved by dx|dy, for its shadow) from angle a0 to a1 (radians, clockwise on screen). */
function arc(r: number, a0: number, a1: number, dx = 0, dy = 0) {
const p = (a: number) => `${f(dx + Math.cos(a) * r)} ${f(dy + Math.sin(a) * r)}`
return `M${p(a0)}A${f(r)} ${f(r)} 0 ${a1 - a0 > Math.PI ? 1 : 0} 1 ${p(a1)}`
}
const rotate = ([x, y]: Pt, a: number): Pt => [x * Math.cos(a) - y * Math.sin(a), x * Math.sin(a) + y * Math.cos(a)]
// Painter's layers, bottom to top. Shapes within a layer never overlap, so each colour of a
// layer can be one path.
const enum L {
Base,
Ground,
Furrows,
Shadow,
Edge,
Wall,
Roof,
Ridge,
Tower,
Top,
}
/** Collects a look's shapes, merged by layer and style. */
class Picture {
private parts = new Map<string, { layer: number; order: number; style: Omit<MapVillageShape, 'd'>; d: string }>()
constructor(
/** Variant 1 is the mirror image of variant 0, lit the same way. */
readonly mirror: boolean,
readonly worn: boolean,
) {}
x = (x: number) => (this.mirror ? -x : x)
add(layer: L, style: Omit<MapVillageShape, 'd'>, d: string) {
if (!d) return
const key = `${layer}|${style.fill}|${style.stroke}|${style.width}|${style.dash}`
const part = this.parts.get(key)
if (part) part.d += d
else this.parts.set(key, { layer, order: this.parts.size, style, d })
}
shapes(shadow: string): MapVillageShape[] {
return [...this.parts.values()]
.sort((a, b) => a.layer - b.layer || a.order - b.order)
.map((p) => ({ d: p.d, ...p.style, ...(p.style.fill === SHADOW ? { fill: shadow } : {}), ...(p.style.stroke === SHADOW ? { stroke: shadow } : {}) }))
}
}
type Material = 'thatch' | 'tile' | 'shingle' | 'slate'
const MATERIAL: Record<Material, string> = { thatch: C.thatch, tile: C.roofRed, shingle: C.shingle, slate: C.slate }
interface RoofSpec {
/** Centre on the plot (mirrored in variant 1). */
at: Pt
/** Size along the ridge and across it. */
w: number
d: number
/** Turn in degrees, clockwise. */
turn?: number
material: Material
/** Gable: two halves; hip: four facets. */
hip?: boolean
/** How high it stands, for its shadow. */
height?: number
/** In a barbarian village: burnt down to its walls. */
burnt?: boolean
/** In a barbarian village: a hole in the roof. */
hole?: boolean
}
/** A roof seen from above, its shadow on the ground and its ridge. */
function roof(p: Picture, s: RoofSpec) {
const a = ((p.mirror ? -(s.turn ?? 0) : (s.turn ?? 0)) * Math.PI) / 180
const cx = p.x(s.at[0])
const cy = s.at[1]
const at = (x: number, y: number): Pt => {
const [rx, ry] = rotate([x, y], a)
return [cx + rx, cy + ry]
}
const hw = s.w / 2
const hd = s.d / 2
const h = s.height ?? Math.min(s.w, s.d) * 0.6
const corners = [at(-hw, -hd), at(hw, -hd), at(hw, hd), at(-hw, hd)]
p.add(L.Shadow, { fill: SHADOW }, poly(corners.map(([x, y]) => [x + h * 0.42, y + h * 0.3])))
if (p.worn && s.burnt) {
// Charred walls round the ash, a fallen beam or two across.
p.add(L.Roof, { fill: C.char }, poly(corners))
p.add(L.Ridge, { fill: C.ash }, poly([at(-hw + 1.6, -hd + 1.6), at(hw - 1.6, -hd + 1.6), at(hw - 1.6, hd - 1.6), at(-hw + 1.6, hd - 1.6)]))
p.add(L.Tower, { stroke: C.char, width: 1.2 }, line([at(-hw + 1, -hd * 0.3), at(hw - 2, hd * 0.5)]) + line([at(-hw * 0.3, -hd + 1), at(hw * 0.1, hd - 1)]))
return
}
const base = p.worn ? scale(mix(MATERIAL[s.material], WEATHER, 0.5), 0.88) : MATERIAL[s.material]
const facet = (pts: Pt[], nx: number, ny: number) => {
const [ex, ey] = rotate([nx, ny], a)
p.add(L.Roof, { fill: tone(base, ex, ey) }, poly(pts))
}
const ridge = scale(base, 0.56)
if (s.hip) {
const r = Math.max(0, hw - hd)
facet([at(-hw, -hd), at(hw, -hd), at(r, 0), at(-r, 0)], 0, -1)
facet([at(-hw, hd), at(hw, hd), at(r, 0), at(-r, 0)], 0, 1)
facet([at(-hw, -hd), at(-r, 0), at(-hw, hd)], -1, 0)
facet([at(hw, -hd), at(r, 0), at(hw, hd)], 1, 0)
if (r > 0) p.add(L.Ridge, { stroke: ridge, width: 0.9 }, line([at(-r, 0), at(r, 0)]))
} else {
facet([at(-hw, -hd), at(hw, -hd), at(hw, 0), at(-hw, 0)], 0, -1)
facet([at(-hw, 0), at(hw, 0), at(hw, hd), at(-hw, hd)], 0, 1)
p.add(L.Ridge, { stroke: ridge, width: 0.9 }, line([at(-hw, 0), at(hw, 0)]))
}
if (p.worn && s.hole) {
// Fallen in: the rafters show over the dark inside.
p.add(L.Ridge, { fill: C.char }, poly([at(-hw * 0.1, hd * 0.15), at(hw * 0.55, hd * 0.15), at(hw * 0.45, hd * 0.8), at(-hw * 0.05, hd * 0.75)]))
p.add(L.Tower, { stroke: scale(C.wood, 0.8), width: 0.7 }, line([at(hw * 0.12, hd * 0.15), at(hw * 0.12, hd * 0.78)]) + line([at(hw * 0.32, hd * 0.15), at(hw * 0.3, hd * 0.8)]))
}
}
/** A round tower or a tree seen from above: a cone or a crown, lit half and shadowed half. */
function round(p: Picture, layer: L, at: Pt, r: number, base: string, height: number) {
const cx = p.x(at[0])
const cy = at[1]
p.add(L.Shadow, { fill: SHADOW }, circle(cx + height * 0.42, cy + height * 0.3, r))
// The line between the halves runs across the light.
const dx = 0.8 * r
const dy = -0.6 * r
const half = (sweep: 0 | 1) => `M${f(cx + dx)} ${f(cy + dy)}A${f(r)} ${f(r)} 0 0 ${sweep} ${f(cx - dx)} ${f(cy - dy)}Z`
p.add(layer, { fill: tone(base, -0.6, -0.8) }, half(0))
p.add(layer, { fill: tone(base, 0.6, 0.8) }, half(1))
}
const SLATE = (p: Picture) => (p.worn ? mix(C.slate, WEATHER, 0.4) : C.slate)
const tree = (p: Picture, at: Pt, r: number) => round(p, L.Roof, at, r, p.worn ? C.broadleafDark : C.broadleaf, r * 1.6)
const hay = (p: Picture, at: Pt, r: number) => round(p, L.Roof, at, r, p.worn ? mix(C.hay, WEATHER, 0.5) : C.hay, r)
/** Paths trodden into the yard, each a run of points. */
function paths(p: Picture, width: number, ...runs: Pt[][]) {
p.add(L.Ground, { stroke: p.worn ? PATH_WORN : PATH, width }, runs.map((r) => line(r.map(([x, y]) => [p.x(x), y]))).join(''))
}
/** The plaza: a trodden patch in front of the headquarters. */
const plaza = (p: Picture, at: Pt, r: number) => p.add(L.Ground, { fill: p.worn ? PATH_WORN : PATH }, circle(p.x(at[0]), at[1], r))
/** A garden or a little field: furrows across a patch of greens or wheat; weeds in a barbarian village. */
function garden(p: Picture, at: Pt, w: number, d: number, turn: number, wheat = false) {
const a = ((p.mirror ? -turn : turn) * Math.PI) / 180
const cx = p.x(at[0])
const cy = at[1]
const pt = (x: number, y: number): Pt => {
const [rx, ry] = rotate([x, y], a)
return [cx + rx, cy + ry]
}
p.add(L.Ground, { fill: p.worn ? WEEDS : wheat ? C.wheat : C.field }, poly([pt(-w / 2, -d / 2), pt(w / 2, -d / 2), pt(w / 2, d / 2), pt(-w / 2, d / 2)]))
let rows = ''
for (let y = -d / 2 + 2; y < d / 2 - 1; y += 2.6) rows += line([pt(-w / 2 + 1, y), pt(w / 2 - 1, y)])
p.add(L.Furrows, { stroke: C.furrow, width: 0.8 }, rows)
}
/** A well on the plaza: a stone ring round the dark shaft. */
function well(p: Picture, at: Pt) {
p.add(L.Roof, { fill: C.stone }, circle(p.x(at[0]), at[1], 2.6))
p.add(L.Ridge, { fill: C.dark }, circle(p.x(at[0]), at[1], 1.3))
}
/** Weeds grown over the yard of a barbarian village. */
function weeds(p: Picture, ...spots: [number, number, number][]) {
if (p.worn) p.add(L.Furrows, { fill: WEEDS }, spots.map(([x, y, r]) => circle(p.x(x), y, r)).join(''))
}
/** A few graves behind the chapel, the newest still open. */
function graves(p: Picture, ...at: Pt[]) {
p.add(L.Ridge, { fill: C.stoneDark }, at.slice(1).map(([x, y]) => box(p.x(x), y, 0.8, 1.3)).join(''))
p.add(L.Ridge, { fill: C.dark }, box(p.x(at[0]![0]), at[0]![1], 0.9, 1.6))
}
/** Half the gate's width at the bottom of a ring, in radians. */
const GATE = 0.2
const TURN = Math.PI * 2
/** Runs of a ring from the gate round to the gate, without the gaps (angles, mirrored in variant 1). */
function ringRuns(p: Picture, gaps: [number, number][]): [number, number][] {
const from = Math.PI / 2 + GATE
const to = from + TURN - GATE * 2
const runs: [number, number][] = []
let a = from
const norm = (g: number) => (g < from ? g + TURN : g)
for (const [g0, g1] of gaps.map(([g0, g1]) => (p.mirror ? [Math.PI - g1, Math.PI - g0] : [g0, g1])).map(([g0, g1]) => [norm(g0!), norm(g1!)]).sort((x, y) => x[0]! - y[0]!)) {
runs.push([a, g0!])
a = g1!
}
runs.push([a, to])
return runs
}
/** A stake palisade round the plot, the gate at the bottom: posts, or wooden gate towers. A barbarian's has gaps. */
function palisade(p: Picture, r: number, towers: boolean) {
const runs = ringRuns(p, p.worn ? [[0.5, 0.75], [2.7, 2.85], [4.3, 4.55]] : [])
const d = runs.map(([a0, a1]) => arc(r, a0, a1)).join('')
p.add(L.Shadow, { stroke: SHADOW, width: 3.2 }, runs.map(([a0, a1]) => arc(r, a0, a1, 1.8, 1.3)).join(''))
p.add(L.Wall, { stroke: p.worn ? mix(C.woodDark, WEATHER, 0.3) : scale(C.woodDark, 0.85), width: 3.4 }, d)
// The stakes' tops, lit.
p.add(L.Ridge, { stroke: p.worn ? mix(C.plank, WEATHER, 0.4) : C.plank, width: 2.2, dash: '1.5 0.9' }, d)
for (const s of [-1, 1]) {
const g = Math.PI / 2 + s * (GATE + 0.05)
// Either side of the gate, the same in both variants.
const at: Pt = [Math.cos(g) * r, Math.sin(g) * r]
if (towers) roof(p, { at, w: 6.5, d: 6.5, material: 'shingle', hip: true, height: 9 })
else p.add(L.Wall, { fill: C.woodDark }, box(at[0], at[1], 1.7, 1.7))
}
}
/** A stone wall round the plot with battlements, round towers and a gate at the bottom. */
function stoneWall(p: Picture, r: number, towers: number, gatehouse: boolean) {
const stone = p.worn ? scale(mix(C.stone, WEATHER, 0.3), 0.9) : C.stone
const from = Math.PI / 2 + GATE
const to = from + TURN - GATE * 2
const d = arc(r, from, to)
p.add(L.Shadow, { stroke: SHADOW, width: 4.4 }, arc(r, from + 0.05, to - 0.05, 2, 1.5))
p.add(L.Wall, { stroke: scale(stone, 0.72), width: 4.6 }, d)
p.add(L.Wall, { stroke: stone, width: 2.8 }, arc(r - 0.5, from, to))
// Merlons along the outer edge.
p.add(L.Ridge, { stroke: scale(stone, 0.58), width: 1.2, dash: '1.6 1.4' }, arc(r + 1.4, from, to))
for (let k = 0; k < towers; k++) {
const a = Math.PI / 2 + ((k + 0.5) / towers) * TURN
// Spread evenly from the gate, so the same in both variants.
round(p, L.Tower, [Math.cos(a) * r, Math.sin(a) * r], 4.4, SLATE(p), 12)
}
for (const s of [-1, 1]) {
const g = Math.PI / 2 + s * (GATE + 0.03)
round(p, L.Tower, [Math.cos(g) * r, Math.sin(g) * r], gatehouse ? 4.8 : 3.8, SLATE(p), 13)
}
if (gatehouse) roof(p, { at: [0, r - 0.5], w: 8, d: 6, material: 'slate', hip: true, height: 10 })
}
/** The village's flag on a pole at the headquarters, in the relation colour, and its shadow. */
function flag(p: Picture, at: Pt) {
if (p.worn) return
const x = p.x(at[0])
const y = at[1]
p.add(L.Top, { fill: SHADOW }, poly([[x + 4, y + 3], [x + 15, y + 5], [x + 5, y + 9.5]]))
p.add(L.Top, { fill: C.woodDark }, circle(x, y, 1.3))
p.add(L.Top, { fill: OWNER }, poly([[x, y - 1.2], [x + 12, y + 2], [x + 0.6, y + 6.6]]))
}
// The six pictures. Roofs never overlap, nor trees and roofs, so the layers can merge by colour.
function tier1(p: Picture) {
plaza(p, [0, 7], 8)
paths(p, 4, [[0, 41], [0, 8]], [[0, 9], [-19, 10]], [[0, 9], [16, 16]])
garden(p, [-14, 27], 15, 10, -12)
weeds(p, [10, 27, 5], [-25, -6, 4])
roof(p, { at: [0, -8], w: 27, d: 14, material: 'thatch', height: 10, hole: true })
roof(p, { at: [-21, 7], w: 15, d: 11, turn: 82, material: 'thatch', height: 7, burnt: true })
roof(p, { at: [18, 18], w: 14, d: 10, turn: -8, material: 'thatch', height: 7 })
tree(p, [-19, -22], 6.5)
tree(p, [21, -20], 5.5)
hay(p, [26, 3], 3.4)
flag(p, [12, -15])
}
function tier2(p: Picture) {
plaza(p, [0, 7], 10)
paths(p, 4, [[0, 43], [0, 8]], [[0, 8], [-21, 6]], [[0, 8], [21, 9]], [[-6, 14], [-15, 23]], [[6, 14], [16, 25]])
garden(p, [-24, -21], 14, 10, 32, true)
garden(p, [25, -20], 13, 9, -30)
weeds(p, [-8, 31, 5], [28, 0, 4])
well(p, [5, 9])
roof(p, { at: [0, -9], w: 29, d: 15, material: 'thatch', height: 11 })
roof(p, { at: [-23, 6], w: 15, d: 11, turn: 84, material: 'thatch', height: 7, hole: true })
roof(p, { at: [23, 8], w: 14, d: 11, turn: -84, material: 'shingle', height: 7 })
roof(p, { at: [-16, 25], w: 14, d: 9, turn: -12, material: 'thatch', height: 6, burnt: true })
roof(p, { at: [17, 26], w: 13, d: 9, turn: 14, material: 'thatch', height: 6 })
tree(p, [-6, -30], 5.5)
tree(p, [9, -31], 5)
hay(p, [33, -5], 3)
flag(p, [13, -16])
}
function tier3(p: Picture) {
plaza(p, [0, 7], 10)
paths(p, 4, [[0, 45], [0, 8]], [[-26, 8], [26, 8]])
garden(p, [-24, -20], 12, 10, 38, true)
weeds(p, [24, 27, 4], [-4, -29, 4])
well(p, [6, 10])
roof(p, { at: [0, -9], w: 28, d: 16, material: 'tile', hip: true, height: 12 })
roof(p, { at: [-22, -3], w: 15, d: 11, turn: 86, material: 'thatch', height: 7, hole: true })
roof(p, { at: [23, -2], w: 15, d: 11, turn: -86, material: 'tile', height: 7 })
roof(p, { at: [-17, 21], w: 15, d: 10, turn: -14, material: 'thatch', height: 7, burnt: true })
roof(p, { at: [17, 21], w: 14, d: 10, turn: 14, material: 'shingle', height: 7 })
roof(p, { at: [13, -27], w: 17, d: 9, turn: -20, material: 'shingle', height: 7 })
tree(p, [-8, -29], 5)
hay(p, [-31, 9], 3)
palisade(p, 39.5, false)
flag(p, [12, -17])
}
function tier4(p: Picture) {
plaza(p, [0, 8], 11)
paths(p, 4.4, [[0, 46], [0, 9]], [[-28, 9], [28, 9]], [[-14, 9], [-22, 28]], [[14, 9], [22, 28]])
garden(p, [-26, -16], 11, 9, 50, true)
weeds(p, [-2, 31, 4], [28, -14, 3.5])
well(p, [6, 11])
roof(p, { at: [0, -8], w: 30, d: 17, material: 'tile', hip: true, height: 13, hole: true })
// The chapel: a long slate nave behind the headquarters, its tower at the end.
roof(p, { at: [15, -27], w: 20, d: 9, turn: -22, material: 'slate', height: 10 })
roof(p, { at: [4, -33], w: 7, d: 7, turn: -22, material: 'slate', hip: true, height: 16 })
roof(p, { at: [-23, -1], w: 15, d: 11, turn: 86, material: 'thatch', height: 7 })
roof(p, { at: [24, -2], w: 15, d: 11, turn: -86, material: 'tile', height: 7, hole: true })
roof(p, { at: [-8, 24], w: 13, d: 9, turn: -64, material: 'tile', height: 7 })
roof(p, { at: [8, 24], w: 13, d: 9, turn: 64, material: 'thatch', height: 7, burnt: true })
roof(p, { at: [-29, 18], w: 12, d: 9, turn: 30, material: 'shingle', height: 6 })
roof(p, { at: [29, 18], w: 12, d: 9, turn: -30, material: 'thatch', height: 6 })
roof(p, { at: [-12, -28], w: 13, d: 9, turn: 24, material: 'thatch', height: 6 })
palisade(p, 40.5, true)
flag(p, [13, -17])
}
function tier5(p: Picture) {
plaza(p, [0, 8], 11)
paths(p, 4.4, [[0, 47], [0, 9]], [[-28, 9], [28, 9]], [[-14, 9], [-21, 28]], [[14, 9], [21, 28]])
weeds(p, [0, 31, 4], [-28, -12, 3.5])
well(p, [6, 11])
// The headquarters a stone hall with a slate roof; the chapel and its graves behind it.
roof(p, { at: [0, -8], w: 31, d: 17, material: 'slate', hip: true, height: 14 })
roof(p, { at: [16, -26], w: 20, d: 9, turn: -24, material: 'slate', height: 10, hole: true })
roof(p, { at: [5, -33], w: 7, d: 7, turn: -24, material: 'slate', hip: true, height: 17 })
graves(p, [24, -13], [27, -15], [21, -16], [28, -11])
roof(p, { at: [-23, -2], w: 15, d: 11, turn: 86, material: 'tile', height: 8 })
roof(p, { at: [-14, -25], w: 14, d: 9, turn: 26, material: 'tile', height: 7 })
roof(p, { at: [-8, 24], w: 13, d: 9, turn: -64, material: 'tile', height: 7, burnt: true })
roof(p, { at: [8, 24], w: 13, d: 9, turn: 64, material: 'shingle', height: 7 })
roof(p, { at: [-28, 18], w: 12, d: 9, turn: 34, material: 'thatch', height: 6, hole: true })
roof(p, { at: [28, 18], w: 12, d: 9, turn: -34, material: 'tile', height: 6 })
roof(p, { at: [29, -1], w: 11, d: 9, turn: -88, material: 'thatch', height: 6 })
stoneWall(p, 41, 4, false)
flag(p, [14, -17])
}
function tier6(p: Picture) {
plaza(p, [0, 9], 10)
paths(p, 4.4, [[0, 47], [0, 10]], [[-30, 10], [30, 10]], [[-15, 10], [-23, 29]], [[15, 10], [23, 29]], [[-19, 10], [-30, -10]], [[19, 10], [30, -10]])
weeds(p, [0, 32, 4], [-31, 0, 3])
well(p, [6, 12])
// The headquarters a stone hall round a small court with two towers: the biggest roofs of the town.
roof(p, { at: [0, -14], w: 32, d: 9, material: 'slate', height: 15 })
roof(p, { at: [-12, -2], w: 13, d: 8, turn: 90, material: 'slate', height: 14 })
roof(p, { at: [12, -2], w: 13, d: 8, turn: 90, material: 'slate', height: 14, hole: true })
p.add(L.Ground, { fill: p.worn ? PATH_WORN : PATH }, box(0, -3, 7.5, 6.5))
round(p, L.Tower, [-16, -18], 3.8, SLATE(p), 18)
round(p, L.Tower, [16, -18], 3.8, SLATE(p), 18)
roof(p, { at: [10, -31], w: 18, d: 8, turn: -16, material: 'slate', height: 10 })
roof(p, { at: [-12, -31], w: 13, d: 8, turn: 18, material: 'tile', height: 7, burnt: true })
roof(p, { at: [-26, -16], w: 12, d: 8, turn: 52, material: 'tile', height: 7 })
roof(p, { at: [26, -16], w: 12, d: 8, turn: -52, material: 'tile', height: 7 })
roof(p, { at: [-28, 2], w: 11, d: 8, turn: 86, material: 'shingle', height: 7, hole: true })
roof(p, { at: [28, 2], w: 11, d: 8, turn: -86, material: 'tile', height: 7 })
roof(p, { at: [-8, 24], w: 13, d: 8, turn: -64, material: 'tile', height: 7 })
roof(p, { at: [8, 24], w: 13, d: 8, turn: 64, material: 'tile', height: 7 })
roof(p, { at: [-28, 20], w: 11, d: 8, turn: 36, material: 'thatch', height: 6 })
roof(p, { at: [28, 20], w: 11, d: 8, turn: -36, material: 'shingle', height: 6 })
stoneWall(p, 42, 6, true)
flag(p, [16, -24])
}
/** The crown fort: a square stone keep with battlements and corner turrets in a walled ring, the gold crown over it. */
function crownFort(p: Picture, banner: boolean) {
plaza(p, [0, 8], 17)
paths(p, 5, [[0, 47], [0, 18]])
weeds(p, [-24, 14, 4], [22, -22, 3.5])
const stone = p.worn ? scale(mix(C.stone, WEATHER, 0.3), 0.9) : C.stone
const s = 30
const [kx, ky] = [0, -5]
p.add(L.Shadow, { fill: SHADOW }, box(kx + 9, ky + 7, s / 2, s / 2))
// The keep's flat roof: the wall walk round it lit, the roof inside a step down, merlons on the edge.
p.add(L.Roof, { fill: stone }, box(kx, ky, s / 2, s / 2))
p.add(L.Ridge, { fill: scale(stone, 0.72) }, box(kx, ky, s / 2 - 4, s / 2 - 4))
p.add(L.Ridge, { stroke: scale(stone, 0.58), width: 1.3, dash: '2 2' }, box(kx, ky, s / 2 - 0.7, s / 2 - 0.7))
p.add(L.Tower, { fill: C.dark }, box(p.x(kx + 6), ky + 6, 1.6, 1.6))
for (const [cx, cy] of [[-1, -1], [1, -1], [1, 1], [-1, 1]] as const) round(p, L.Tower, [kx + (cx * s) / 2, ky + (cy * s) / 2], 4.4, SLATE(p), 20)
roof(p, { at: [-27, 7], w: 13, d: 8, turn: 80, material: 'tile', height: 6, hole: true })
roof(p, { at: [27, 7], w: 13, d: 8, turn: -80, material: 'shingle', height: 6 })
roof(p, { at: [-18, 26], w: 12, d: 8, turn: -40, material: 'tile', height: 6, burnt: true })
stoneWall(p, 42, 6, true)
// A player's flag on the keep's wall walk, clear of the crown.
if (banner) flag(p, [kx + 6, ky - 13])
const y = ky + 3
p.add(L.Top, { fill: '#d9b44a' }, poly([[-8, y], [-8, y - 9], [-4, y - 4.5], [0, y - 11], [4, y - 4.5], [8, y - 9], [8, y]]))
p.add(L.Top, { fill: '#a8842e' }, box(0, y - 1.3, 8, 1.3))
}
const PICTURES: Record<MapTier, (p: Picture) => void> = { 1: tier1, 2: tier2, 3: tier3, 4: tier4, 5: tier5, 6: tier6 }
/** The plot's radius per tier: it fills most of the field, more of it as the village grows. */
const RADIUS: Record<MapTier, number> = { 1: 42, 2: 44, 3: 45.5, 4: 46.5, 5: 47, 6: 47 }
function build(id: string, r: number, worn: boolean, mirror: boolean, draw: (p: Picture) => void): MapVillageLook {
const p = new Picture(mirror, worn)
const earth = worn ? EARTH_WORN : EARTH
p.add(L.Base, { fill: RIM }, circle(0, 0, r + 2.5))
p.add(L.Base, { fill: earth }, circle(0, 0, r))
// The edge in the relation colour lies over the ground and the shadows, under what stands.
p.add(L.Edge, { stroke: OWNER, width: 5 }, circle(0, 0, r - 0.5))
draw(p)
return { id, shapes: p.shapes(scale(earth, 0.74)) }
}
const TIER_LIST: MapTier[] = [1, 2, 3, 4, 5, 6]
/** Every look, built once: each tier for players and barbarians in two variants, and the crown forts. */
export const MAP_VILLAGE_LOOKS: MapVillageLook[] = [
...(['player', 'barbarian'] as const).flatMap((kind) =>
TIER_LIST.flatMap((tier) => [0, 1].map((v) => build(`mv-${kind}-${tier}-${v}`, RADIUS[tier], kind === 'barbarian', v === 1, PICTURES[tier]))),
),
build('mv-crown', 47, true, false, (p) => crownFort(p, false)),
build('mv-crown-player', 47, false, false, (p) => crownFort(p, true)),
]
/** The symbol a village wears: its kind and tier, the variant from its id. */
export function mapVillageSymbol(v: { id: number; points: number; crown: boolean; player: unknown }): string {
if (v.crown) return v.player ? '#mv-crown-player' : '#mv-crown'
return `#mv-${v.player ? 'player' : 'barbarian'}-${mapTier(v.points)}-${Math.abs(v.id) % 2}`
}

View File

@ -415,7 +415,7 @@ const continent = computed(() => (rounded.value ? continentOf(rounded.value.x, r
</li>
<template v-for="r in LEGEND" :key="r">
<li v-if="r === 'own' || r === 'none' || r === 'barbarian' || counts[r]" class="flex items-center gap-2">
<MapLegendSwatch :colour="RELATION_COLOURS[r]" /> {{ $t(`map.legend.${r}`) }}
<MapLegendSwatch :colour="RELATION_COLOURS[r]" :dot="r === 'barbarian'" /> {{ $t(`map.legend.${r}`) }}
<span class="ml-auto tabular-nums text-ink-500">{{ counts[r] }}</span>
</li>
</template>

View File

@ -0,0 +1,14 @@
// Paints the map's village pictures off the main thread, one look at a time, and hands each
// back as a PNG the moment it is done (useMapTiles).
import { MAP_LOOKS, mapTileDrawing, paintTile } from '../diorama/mapTiles'
self.onmessage = async (e: MessageEvent<{ ids: string[]; size: number }>) => {
const { ids, size } = e.data
for (const id of ids) {
const look = MAP_LOOKS.find((l) => l.id === id)
if (!look) continue
const canvas = new OffscreenCanvas(size, size)
paintTile(canvas.getContext('2d')!, mapTileDrawing(look), size)
self.postMessage({ id, blob: await canvas.convertToBlob({ type: 'image/png' }) })
}
}