hivedesk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/FIRST-RUN.md +14 -0
  3. package/PROTOCOL.md +16 -3
  4. package/README.md +50 -6
  5. package/assets/illustrations/hive-city.svg +113 -0
  6. package/assets/video-poster.webp +0 -0
  7. package/i18n/README.pt-BR.md +44 -6
  8. package/i18n/de.json +174 -0
  9. package/i18n/en.json +174 -0
  10. package/i18n/es.json +174 -0
  11. package/i18n/fr.json +174 -0
  12. package/i18n/it.json +174 -0
  13. package/i18n/ja.json +174 -0
  14. package/i18n/ko.json +174 -0
  15. package/i18n/pt-BR.json +174 -0
  16. package/i18n/ru.json +174 -0
  17. package/i18n/zh-CN.json +174 -0
  18. package/package.json +1 -1
  19. package/src/bus.js +7 -1
  20. package/src/city/demo.js +75 -0
  21. package/src/city/hops.js +95 -0
  22. package/src/city/index.js +1507 -0
  23. package/src/city/router.js +53 -0
  24. package/src/city/state.js +313 -0
  25. package/src/city/store.js +91 -0
  26. package/src/cli.js +3 -0
  27. package/src/crew/engine.js +40 -3
  28. package/src/demo.js +30 -2
  29. package/src/index.js +23 -2
  30. package/src/server.js +4 -1
  31. package/ui/city/city.css +204 -0
  32. package/ui/city/city.js +933 -0
  33. package/ui/city/collab.js +64 -0
  34. package/ui/city/layout.js +142 -0
  35. package/ui/city/mock.js +358 -0
  36. package/ui/city/model.js +195 -0
  37. package/ui/city/scene.js +2033 -0
  38. package/ui/js/main.js +3 -1
  39. package/ui/lend/collab/roles.js +8 -2
  40. package/ui/office/office.js +51 -21
  41. package/ui/office/scene.js +3 -0
  42. package/ui/screenshots/city-agent-card-1440.png +0 -0
  43. package/ui/screenshots/city-agent-card-1920.png +0 -0
  44. package/ui/screenshots/city-bridge-gpu-borrow-1440.png +0 -0
  45. package/ui/screenshots/city-bridge-gpu-borrow-1920.png +0 -0
  46. package/ui/screenshots/city-friend-rising-1440.png +0 -0
  47. package/ui/screenshots/city-friend-rising-1920.png +0 -0
  48. package/ui/screenshots/city-overview-1440.png +0 -0
  49. package/ui/screenshots/city-overview-1920.png +0 -0
  50. package/ui/screenshots/city-review-room-1440.png +0 -0
  51. package/ui/screenshots/city-review-room-1920.png +0 -0
@@ -0,0 +1,2033 @@
1
+ // Hive City: an isometric little city of floating honeycomb islands, drawn on one canvas with
2
+ // the Office art. Your building shows its rooms as an open dollhouse, friend machines rise as
3
+ // towers on their own islands, and bridges light up when tasks or GPU turns travel.
4
+ // Nothing here moves by itself: every walk, construction and glow follows a change in the city
5
+ // state (ui/city/model.js), which only changes from engine events.
6
+
7
+ import { beeSvg } from '/js/bees.js';
8
+ import {
9
+ nightFactor, goldenFactor, iconJar, roundRect, hexPath, hexScreen, wrap, drawNameTag, drawBubble, drawPill,
10
+ iconClock, iconCheck, iconX, iconDots, iconMag, iconFlask, iconPause, drawNoteIcon, drawParcel, mixHex, shade, alpha, rgb,
11
+ } from '/office/scene.js';
12
+ import {
13
+ HOME, FRIEND_R, ROOMS, BUILDING, ENTRANCE, PLAZA, WALK_Z, SEAT_Z, roomCenter, spotFor, hexCorners, friendCenter, bridgeTo,
14
+ bridgePoint, towerOf, roomPath, exitPath, roomAt, CORRIDOR_Y,
15
+ } from './layout.js';
16
+ import { ROOM_COLOR, ROOM_KINDS, borrowOf } from './model.js';
17
+
18
+ const FONT = '"Segoe UI Variable Text","Segoe UI",system-ui,-apple-system,sans-serif';
19
+ const MONO = 'ui-monospace,"Cascadia Code",Consolas,monospace';
20
+ const OUT = '#24160C';
21
+ const ZK = 1.12;
22
+ const WALL = 2.25;
23
+ const PART = 0.36;
24
+ const DW = 1.5;
25
+ const DD = 0.72;
26
+ const DH = 0.55;
27
+ const FLOOR_H = 1.25;
28
+ const TANK = { x: -1.6, y: -1.4, z: 3.2 };
29
+ const WALK_SPEED = 3.1;
30
+
31
+ const clamp = (v, a = 0, b = 1) => (v < a ? a : v > b ? b : v);
32
+ const lerp = (a, b, t) => a + (b - a) * t;
33
+ const easeInOut = (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
34
+ const easeOut = (t) => 1 - Math.pow(1 - t, 3);
35
+ const easeOutBack = (t) => { const c1 = 1.70158; const c3 = c1 + 1; return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2); };
36
+ const rnd = (a, b) => a + Math.random() * (b - a);
37
+ const pick = (arr) => arr[(Math.random() * arr.length) | 0];
38
+ const hash = (s) => { let h = 0; for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0; return Math.abs(h); };
39
+
40
+ const GLYPHS = {
41
+ design: ['#', 'px', 'rem', 'rgb', 'em', ':root'],
42
+ code: ['{ }', '</>', '=>', '( )', ';', 'fn', '++'],
43
+ review: ['+', '-', 'diff', '?', 'ok'],
44
+ test: ['ok?', 'assert', '==', 'test'],
45
+ ops: ['git', 'push', 'tag', 'v1'],
46
+ library: ['Aa', 'md', 'mem', '...'],
47
+ lobby: ['plan', '1.', '2.', '3.'],
48
+ };
49
+
50
+ /* ---------- day and night ---------- */
51
+ const DAY = {
52
+ grassA: '#A3C463', grassB: '#93B758', grassC: '#B0CD6E', grout: '#7E9E48', lip: '#6F8F3E',
53
+ path: '#EBD5A6', pathEdge: '#C4A675',
54
+ soil: '#7A5636', rockA: '#9A7656', rockB: '#7A5C44', rockC: '#57412F', rockD: '#3E2E22',
55
+ plinthTop: '#DCCAA6', plinthL: '#B69E78', plinthR: '#9D8562',
56
+ wallL: '#F0E3C9', wallR: '#E6D6B8', trim: '#7A5636', comb: '#C9A979',
57
+ floorA: '#8C6743', floorB: '#835F3D', floorC: '#946E48', grout2: '#634629', corridor: '#A88456',
58
+ deskTop: '#ECD6AA', deskL: '#D2B07A', deskR: '#B99560', panel: '#94724A',
59
+ part: '#EADBBF', partL: '#CDB993', partR: '#B8A37C',
60
+ deck: '#C79A62', deckB: '#B88B55', deckSide: '#7E5A36', rail: '#6B4A2C',
61
+ tree: '#6FA65A', treeB: '#5A8F4A', treeC: '#83B866', trunk: '#7A5636',
62
+ tower: '#E9DCC2', towerL: '#CDBB98', towerR: '#B6A27E',
63
+ };
64
+ const NIGHT_TINT = '#131A2E';
65
+ const DUSK_TINT = '#FF9A55';
66
+
67
+ export class CityScene {
68
+ /**
69
+ * @param {HTMLCanvasElement} canvas
70
+ * @param {{ getCity: () => object, reduced?: () => boolean, hour?: () => number, t: (k: string, p?: object) => string,
71
+ * onFrame?: (scene: CityScene) => void, onSound?: (name: string) => void }} opts
72
+ */
73
+ constructor(canvas, opts) {
74
+ this.canvas = canvas;
75
+ this.ctx = canvas.getContext('2d', { alpha: false });
76
+ this.opts = opts;
77
+ this.t = opts.t || ((k) => k);
78
+ this.getCity = opts.getCity;
79
+ this.reducedFn = opts.reduced || (() => false);
80
+ this.hourFn = opts.hour || (() => { const d = new Date(); return d.getHours() + d.getMinutes() / 60; });
81
+ this.sound = opts.onSound || (() => {});
82
+ this.w = 0; this.h = 0; this.dpr = 1;
83
+ this.now = 0;
84
+ this.running = false;
85
+ this.loop = this.loop.bind(this);
86
+ this.cam = { x: HOME.cx + 2, y: HOME.cy + 1, z: 0.6, A: 26 };
87
+ this.camT = { ...this.cam };
88
+ this.camSet = false;
89
+ this.focus = null; // { kind: 'room', id } | { kind: 'machine', id }
90
+ this.highlight = null; // { kind, id }
91
+ this.dropTarget = null;
92
+ this.actors = new Map();
93
+ this.machines = new Map(); // id -> { id, slot, center, bridge, born, glow }
94
+ this.notes = new Map();
95
+ this.parts = [];
96
+ this.couriers = [];
97
+ this.trafficSeen = new Set();
98
+ this.sprites = new Map();
99
+ this.hits = [];
100
+ this.machineHits = [];
101
+ this.synced = false;
102
+ this.stars = Array.from({ length: 90 }, (_, i) => ({ x: ((i * 7919) % 1000) / 1000, y: ((i * 4157) % 1000) / 1000, r: 0.5 + ((i * 13) % 7) / 6, ph: i * 1.7 }));
103
+ this.clouds = Array.from({ length: 14 }, (_, i) => ({ x: ((i * 397) % 1000) / 1000, y: 0.08 + ((i * 211) % 1000) / 1000 * 0.5, s: 0.6 + ((i * 53) % 10) / 12, v: 0.004 + ((i * 17) % 7) / 1400 }));
104
+ this.palAt = -1;
105
+ this.buildPaths();
106
+ }
107
+
108
+ reduced() { return this.reducedFn(); }
109
+
110
+ /* ---------- lifecycle ---------- */
111
+ start() {
112
+ if (this.running) return;
113
+ this.running = true;
114
+ this.last = performance.now();
115
+ this.lastDraw = 0;
116
+ this.raf = requestAnimationFrame(this.loop);
117
+ }
118
+
119
+ stop() { this.running = false; cancelAnimationFrame(this.raf); }
120
+
121
+ resize(w, h) {
122
+ if (!w || !h) return;
123
+ this.w = w; this.h = h;
124
+ let dpr = Math.min(window.devicePixelRatio || 1, 2);
125
+ if (w * h * dpr * dpr > 4.2e6) dpr = Math.max(1, Math.sqrt(4.2e6 / (w * h)));
126
+ this.dpr = dpr;
127
+ this.canvas.width = Math.round(w * dpr);
128
+ this.canvas.height = Math.round(h * dpr);
129
+ this.skyGrad = null;
130
+ if (!this.camSet) { this.fit(true); }
131
+ else if (this.focus?.kind === 'room') this.focusRoom(this.focus.id, true);
132
+ if (!this.running) this.draw();
133
+ }
134
+
135
+ loop(ts) {
136
+ if (!this.running) return;
137
+ this.raf = requestAnimationFrame(this.loop);
138
+ const calm = this.isCalm();
139
+ if (calm && ts - this.lastDraw < 1000 / 30 - 2) return;
140
+ const dt = Math.min(0.1, (ts - this.last) / 1000);
141
+ this.last = ts;
142
+ this.lastDraw = ts;
143
+ this.now += dt;
144
+ this.update(dt);
145
+ this.draw();
146
+ this.opts.onFrame?.(this);
147
+ }
148
+
149
+ isCalm() {
150
+ if (this.parts.length || this.couriers.length) return false;
151
+ if (Math.abs(this.cam.A - this.camT.A) > 0.05 || Math.abs(this.cam.x - this.camT.x) > 0.01 || Math.abs(this.cam.y - this.camT.y) > 0.01) return false;
152
+ for (const a of this.actors.values()) if (a.path.length || a.mode === 'working') return false;
153
+ for (const m of this.machines.values()) if (this.now - m.born < 6) return false;
154
+ return true;
155
+ }
156
+
157
+ /* ---------- camera ---------- */
158
+ get A() { return this.cam.A; }
159
+ P(x, y, z = 0) {
160
+ const A = this.cam.A; const B = A / 2; const C = A * ZK;
161
+ return [this.ox + (x - y) * A, this.oy + (x + y) * B - z * C];
162
+ }
163
+ computeOrigin() {
164
+ const A = this.cam.A; const B = A / 2; const C = A * ZK;
165
+ this.ox = this.w / 2 - (this.cam.x - this.cam.y) * A;
166
+ this.oy = this.h / 2 - (this.cam.x + this.cam.y) * B + this.cam.z * C;
167
+ }
168
+ /** World point on the ground (z = 0) under a screen point. */
169
+ unproject(sx, sy) {
170
+ const A = this.cam.A; const B = A / 2;
171
+ const u = (sx - this.ox) / A; const v = (sy - this.oy) / B;
172
+ return { x: (u + v) / 2, y: (v - u) / 2 };
173
+ }
174
+
175
+ /** Screen size needed for a world box (iso bounding box) at zoom 1. */
176
+ isoBox(pts) {
177
+ let x0 = Infinity; let x1 = -Infinity; let y0 = Infinity; let y1 = -Infinity;
178
+ for (const [x, y, z = 0] of pts) {
179
+ const sx = x - y; const sy = (x + y) / 2 - z * ZK;
180
+ x0 = Math.min(x0, sx); x1 = Math.max(x1, sx); y0 = Math.min(y0, sy); y1 = Math.max(y1, sy);
181
+ }
182
+ return { x0, x1, y0, y1 };
183
+ }
184
+
185
+ /** Frames a set of world points. */
186
+ frame(pts, margin = { l: 60, r: 60, t: 90, b: 120 }, instant = false, maxA = 140) {
187
+ const b = this.isoBox(pts);
188
+ const A = clamp(Math.min((this.w - margin.l - margin.r) / Math.max(1, b.x1 - b.x0), (this.h - margin.t - margin.b) / Math.max(1, b.y1 - b.y0)), 6, maxA);
189
+ // center of the box in iso space, back to a ground point
190
+ const cxs = (b.x0 + b.x1) / 2 + ((margin.l - margin.r) / 2) / -A;
191
+ const cys = (b.y0 + b.y1) / 2 + ((margin.t - margin.b) / 2) / -A;
192
+ // ground point (z = 0) whose iso is (cxs, cys): x - y = cxs, (x + y) / 2 = cys
193
+ this.camT = { x: cys + cxs / 2, y: cys - cxs / 2, z: 0, A };
194
+ this.camSet = true;
195
+ if (instant || this.reduced()) this.cam = { ...this.camT };
196
+ }
197
+
198
+ fit(instant = false) {
199
+ this.focus = null;
200
+ this.userMoved = false;
201
+ const pts = [];
202
+ for (const [x, y] of hexCorners(HOME.cx, HOME.cy, HOME.r)) pts.push([x, y, 0], [x, y, -2]);
203
+ pts.push([0, 0, WALL + 3.2]);
204
+ for (const m of this.machines.values()) {
205
+ for (const [x, y] of hexCorners(m.center.x, m.center.y, FRIEND_R)) pts.push([x, y, 0], [x, y, -1.5]);
206
+ pts.push([m.center.x, m.center.y, 9]);
207
+ }
208
+ const narrow = this.w < 900;
209
+ this.frame(pts, narrow ? { l: 16, r: 16, t: 120, b: 150 } : { l: this.w < 1100 ? 30 : 330, r: this.panelRight ? 410 : 40, t: 100, b: 110 }, instant, 46);
210
+ }
211
+
212
+ focusRoom(id, instant = false) {
213
+ const r = ROOMS[id];
214
+ if (!r) return;
215
+ this.focus = { kind: 'room', id };
216
+ const pts = [[r.x0, r.y0, 0], [r.x1, r.y0, 0], [r.x1, r.y1, 0], [r.x0, r.y1, 0], [r.x0, r.y0, r.row === 'back' ? WALL + 0.4 : 2.2], [r.x1, r.y0, r.row === 'back' ? WALL + 0.4 : 2.2]];
217
+ const narrow = this.w < 900;
218
+ this.frame(pts, narrow ? { l: 16, r: 16, t: 140, b: 260 } : { l: 340, r: 420, t: 110, b: 150 }, instant, 170);
219
+ }
220
+
221
+ focusMachine(id, instant = false) {
222
+ const m = this.machines.get(id);
223
+ const city = this.getCity();
224
+ if (!m || id === city.selfId) { this.fit(instant); return; }
225
+ this.focus = { kind: 'machine', id };
226
+ const pts = [];
227
+ for (const [x, y] of hexCorners(m.center.x, m.center.y, FRIEND_R)) pts.push([x, y, 0]);
228
+ pts.push([m.center.x, m.center.y, this.towerHeight(m) + 3], [m.bridge.a.x, m.bridge.a.y, 0], [PLAZA.x, PLAZA.y, 0]);
229
+ this.frame(pts, { l: 330, r: 420, t: 110, b: 150 }, instant, 60);
230
+ }
231
+
232
+ zoomAt(factor, sx, sy) {
233
+ const before = this.unproject(sx, sy);
234
+ const A = clamp(this.camT.A * factor, 8, 190);
235
+ // keep the point under the cursor fixed
236
+ const cur = this.cam;
237
+ this.cam = { ...this.camT, A };
238
+ this.computeOrigin();
239
+ const after = this.unproject(sx, sy);
240
+ this.cam = cur;
241
+ this.camT = { ...this.camT, A, x: this.camT.x + before.x - after.x, y: this.camT.y + before.y - after.y };
242
+ this.userMoved = true;
243
+ if (this.reduced()) this.cam = { ...this.camT };
244
+ if (this.focus && A < 40) this.focus = null;
245
+ }
246
+
247
+ panBy(dx, dy) {
248
+ this.userMoved = true;
249
+ const A = this.camT.A; const B = A / 2;
250
+ const u = -dx / A; const v = -dy / B;
251
+ this.camT.x += (u + v) / 2; this.camT.y += (v - u) / 2;
252
+ this.cam.x += (u + v) / 2; this.cam.y += (v - u) / 2;
253
+ }
254
+
255
+ /* ---------- state sync: diffs drive every animation ---------- */
256
+ sync() {
257
+ const c = this.getCity();
258
+ if (!c?.ready) return;
259
+ const first = !this.synced;
260
+ // machines: a new friend raises a building
261
+ const friendsList = Object.values(c.machines).filter((m) => m.id !== c.selfId).sort((a, b) => String(a.joinedAt || '').localeCompare(String(b.joinedAt || '')) || a.id.localeCompare(b.id));
262
+ const seenM = new Set();
263
+ for (const m of friendsList) {
264
+ seenM.add(m.id);
265
+ let s = this.machines.get(m.id);
266
+ if (!s) {
267
+ const used = new Set([...this.machines.values()].map((x) => x.slot));
268
+ let slot = 0; while (used.has(slot)) slot++;
269
+ const center = friendCenter(slot);
270
+ s = { id: m.id, slot, center, bridge: bridgeTo(center), born: first || this.reduced() ? -99 : this.now, glow: 0, m };
271
+ this.machines.set(m.id, s);
272
+ if (!first) { this.sound('build'); this.dust(s); if (!this.userMoved && !this.focus) this.later(0.05, () => this.fit()); }
273
+ }
274
+ s.m = m;
275
+ }
276
+ for (const [id, s] of this.machines) if (!seenM.has(id)) { if (!s.leaving) s.leaving = this.now; if (this.now - s.leaving > 1.5 || first) this.machines.delete(id); }
277
+ // agents
278
+ const byPlace = new Map();
279
+ const agents = Object.values(c.agents).sort((a, b) => a.id.localeCompare(b.id));
280
+ const seenA = new Set();
281
+ for (const ag of agents) {
282
+ seenA.add(ag.id);
283
+ const place = this.placeOf(c, ag, byPlace);
284
+ let a = this.actors.get(ag.id);
285
+ if (!a) {
286
+ a = this.addActor(ag, place, first);
287
+ } else {
288
+ a.ag = ag;
289
+ // a walk is never cut short: the newest place waits until the bee arrives and has stayed a moment
290
+ if (a.placeKey !== place.key) { if (a.path.length || this.now < (a.dwellUntil || 0)) a.next = place; else this.walk(a, place); }
291
+ else a.next = null;
292
+ if (ag.level > a.level) { this.label(a, this.t('city.fx.level', { n: ag.level }), '#FFD072', 1.6); this.sparkle(a, 16); this.sound('level'); }
293
+ else if (ag.xp > a.xp && ag.level === a.level) this.label(a, this.t('city.fx.xp', { n: ag.xp - a.xp }), '#BFE3C6', 1.1);
294
+ if (a.status !== ag.status && ag.status === 'idle' && a.status !== 'idle') a.idleSince = this.now;
295
+ }
296
+ a.level = ag.level; a.xp = ag.xp; a.status = ag.status;
297
+ }
298
+ for (const [id, a] of this.actors) if (!seenA.has(id) && !a.leaving) this.retire(a);
299
+ // tasks: notes on the room boards, flying when they change room
300
+ const seenT = new Set();
301
+ for (const t of Object.values(c.tasks)) {
302
+ seenT.add(t.id);
303
+ const room = t.status === 'done' || !ROOMS[t.room] ? 'lobby' : t.room;
304
+ let n = this.notes.get(t.id);
305
+ if (!n) { n = { id: t.id, room, t, born: first ? -99 : this.now, fly: null }; this.notes.set(t.id, n); if (!first) this.sound('stick'); }
306
+ else if (n.room !== room) {
307
+ const from = this.notePos(n);
308
+ n.room = room;
309
+ n.fly = { from, t0: this.now };
310
+ if (t.status === 'done') { const a = t.assignee && this.actors.get(t.assignee); if (a) { this.sparkle(a, 12); this.jar(a); } this.sound('pass'); }
311
+ }
312
+ if (n.t.status !== t.status && t.status === 'done' && n.room === room) { const a = this.actors.get(t.assignee); if (a) this.sparkle(a, 10); }
313
+ n.t = t;
314
+ }
315
+ for (const [id, n] of this.notes) if (!seenT.has(id)) { if (!n.gone) n.gone = this.now; if (this.now - n.gone > 0.6) this.notes.delete(id); }
316
+ this.layoutBoards();
317
+ // bridge traffic: tasks and GPU turns travel as glowing couriers
318
+ for (const tr of c.traffic || []) {
319
+ if (this.trafficSeen.has(tr.id)) continue;
320
+ this.trafficSeen.add(tr.id);
321
+ if (first) continue;
322
+ const friend = tr.from === c.selfId ? tr.to : tr.from;
323
+ const m = this.machines.get(friend);
324
+ if (!m) continue;
325
+ m.glow = Math.min(1.6, m.glow + 0.7);
326
+ if (this.hostGlow) this.hostGlow.glow = Math.min(1.6, this.hostGlow.glow + 0.5);
327
+ if (this.reduced()) continue;
328
+ this.couriers.push({ m, kind: tr.kind, dir: tr.from === c.selfId ? 1 : -1, t0: this.now, dur: tr.kind === 'turn' ? 1.1 : 1.6, color: tr.kind === 'turn' ? '#7FE3FF' : '#FFD072' });
329
+ if (tr.kind === 'turn') this.sound('blip');
330
+ }
331
+ if (this.trafficSeen.size > 400) this.trafficSeen = new Set([...this.trafficSeen].slice(-200));
332
+ this.synced = true;
333
+ }
334
+
335
+ /** Where an agent should be: a room spot, a friend's roof (borrowing a GPU) or a friend's terrace. */
336
+ placeOf(c, ag, byPlace) {
337
+ const b = borrowOf(c, ag.id);
338
+ if (b && (b.to === c.selfId || this.machines.has(b.to))) return { key: `roof:${b.to}`, kind: 'roof', machine: b.to, host: b.to === c.selfId };
339
+ const home = ag.home || c.selfId;
340
+ // a friend's agent stays on its own island until it holds a task in your building
341
+ if (home !== c.selfId && this.machines.has(home) && (!ag.taskId || c.machines[home]?.online === false)) {
342
+ const k = `terrace:${home}`;
343
+ const i = byPlace.get(k) || 0; byPlace.set(k, i + 1);
344
+ return { key: `${k}:${i}`, kind: 'terrace', machine: home, i };
345
+ }
346
+ const room = ROOMS[ag.room] ? ag.room : 'lobby';
347
+ const i = byPlace.get(room) || 0; byPlace.set(room, i + 1);
348
+ return { key: `room:${room}:${i}`, kind: 'room', room, i };
349
+ }
350
+
351
+ placePoint(place) {
352
+ if (place.kind === 'room') { const s = spotFor(place.room, place.i); return { x: s.x, y: s.y, z: s.desk ? SEAT_Z : WALK_Z, desk: s.desk }; }
353
+ if (place.kind === 'roof' && place.host) return { x: TANK.x + 0.9, y: TANK.y + 0.9, z: TANK.z + 2.2 };
354
+ const m = this.machines.get(place.machine);
355
+ if (!m) return { x: ENTRANCE.x, y: ENTRANCE.y, z: WALK_Z };
356
+ if (place.kind === 'roof') { const tw = towerOf(m.center); return { x: tw.x1 - 0.9, y: tw.y1 - 0.9, z: this.towerHeight(m) + 0.55 }; }
357
+ const fx = m.center.x + 3.7; const fy = m.center.y + 3.7;
358
+ const off = (place.i % 4) - 1.5;
359
+ return { x: fx + off * 0.9, y: fy - off * 0.9, z: WALK_Z };
360
+ }
361
+
362
+ addActor(ag, place, first) {
363
+ const p = this.placePoint(place);
364
+ const a = {
365
+ id: ag.id, ag, placeKey: place.key, place, pos: { x: p.x, y: p.y, z: p.z }, desk: p.desk, path: [], alpha: 1, born: first ? -99 : this.now,
366
+ ph: Math.random() * 6.28, blinkAt: this.now + rnd(1, 4), level: ag.level, xp: ag.xp, status: ag.status, idleSince: this.now - 30,
367
+ nextGlyph: 0, nextZ: 0, squashAt: -9, hopAt: -9, bubble: null, sx: 0, sy: 0, facing: 1,
368
+ };
369
+ // a friend's agent steps out once its building is up
370
+ const mb = place.machine && this.machines.get(place.machine);
371
+ if (!first && mb && this.now - mb.born < 4) a.born = mb.born + 4;
372
+ if (!first && !this.reduced() && place.kind === 'room') {
373
+ a.pos = { x: PLAZA.x, y: PLAZA.y + 1.5, z: WALK_Z };
374
+ a.alpha = 0;
375
+ this.walk(a, place, true);
376
+ this.sound('build');
377
+ }
378
+ this.actors.set(ag.id, a);
379
+ return a;
380
+ }
381
+
382
+ retire(a) {
383
+ if (this.reduced()) { this.actors.delete(a.id); return; }
384
+ a.leaving = this.now;
385
+ const from = a.place.kind === 'room' ? a.place.room : 'lobby';
386
+ a.path = [...roomPath(from, a.pos, 'lobby', { x: 8, y: 8.6 }), { ...ENTRANCE, z: WALK_Z }, { ...PLAZA, z: WALK_Z }, { x: PLAZA.x, y: PLAZA.y + 2, z: 2.5 }].map((p) => ({ z: WALK_Z, ...p }));
387
+ a.onDone = () => this.actors.delete(a.id);
388
+ }
389
+
390
+ /** Builds the walking path between two places and starts the walk. */
391
+ walk(a, place, entering = false) {
392
+ const to = this.placePoint(place);
393
+ const prev = a.place;
394
+ a.place = place;
395
+ a.placeKey = place.key;
396
+ a.desk = to.desk;
397
+ if (this.reduced()) { a.pos = { ...to }; a.path = []; return; }
398
+ let pts = [];
399
+ const roomOfPos = roomAt(a.pos.x, a.pos.y) || (prev?.kind === 'room' ? prev.room : 'lobby');
400
+ const up = WALL + 1.8;
401
+ if (entering) pts = [{ ...ENTRANCE }, { x: 8, y: 8.6 }, ...roomPath('lobby', null, place.room, to)];
402
+ else if (place.kind === 'roof' && place.host) pts = [{ x: a.pos.x, y: a.pos.y, z: up }, { x: TANK.x + 1.2, y: TANK.y + 1.2, z: TANK.z + 2.8 }, to];
403
+ else if (prev?.kind === 'roof' && prev.host && place.kind === 'room') pts = [{ x: a.pos.x + 0.4, y: a.pos.y + 0.4, z: TANK.z + 2.8 }, { x: to.x, y: to.y, z: up }, to];
404
+ else if (place.kind === 'room' && prev?.kind === 'room') pts = roomPath(roomOfPos, a.pos, place.room, to);
405
+ else if (place.kind === 'room' && prev && prev.kind !== 'room') {
406
+ // back over the bridge, in through the entrance
407
+ const m = this.machines.get(prev.machine);
408
+ pts = m ? [...this.friendLeave(m, a.pos), ...exitPath('lobby', m.bridge).reverse(), ...roomPath('lobby', null, place.room, to)] : roomPath('lobby', null, place.room, to);
409
+ } else if (place.kind !== 'room') {
410
+ const m = this.machines.get(place.machine);
411
+ if (prev?.kind === 'room' || !prev) pts = [...exitPath(roomOfPos, m.bridge), ...this.friendArrive(m, place, to)];
412
+ else pts = [to];
413
+ }
414
+ if (!pts.length) pts = [to];
415
+ pts[pts.length - 1] = { ...to };
416
+ a.path = pts.map((p) => ({ x: p.x, y: p.y, z: p.z != null ? p.z : WALK_Z, bridge: p.z != null && p !== to }));
417
+ a.path[a.path.length - 1].final = true;
418
+ a.squashAt = this.now;
419
+ this.sound('whoosh');
420
+ }
421
+
422
+ /** From the end of the bridge around the tower to its terrace, or up to the roof. */
423
+ friendArrive(m, place, to) {
424
+ const c = m.center;
425
+ const end = m.bridge.b;
426
+ const ang0 = Math.atan2(end.y - c.y, end.x - c.x);
427
+ const ang1 = Math.PI / 4;
428
+ const pts = [];
429
+ let d = ang1 - ang0;
430
+ while (d > Math.PI) d -= Math.PI * 2;
431
+ while (d < -Math.PI) d += Math.PI * 2;
432
+ const steps = Math.max(1, Math.round(Math.abs(d) / 0.5));
433
+ for (let i = 1; i <= steps; i++) { const an = ang0 + (d * i) / steps; pts.push({ x: c.x + Math.cos(an) * 4.4, y: c.y + Math.sin(an) * 4.4, z: WALK_Z }); }
434
+ if (place.kind === 'roof') {
435
+ const h = this.towerHeight(m);
436
+ pts.push({ x: to.x + 0.8, y: to.y + 0.8, z: h * 0.5 }, { x: to.x + 0.3, y: to.y + 0.3, z: h + 1.2 });
437
+ }
438
+ pts.push(to);
439
+ return pts;
440
+ }
441
+
442
+ friendLeave(m, pos) {
443
+ const c = m.center;
444
+ const end = m.bridge.b;
445
+ const pts = [];
446
+ if (pos.z > 2) pts.push({ x: pos.x + 0.5, y: pos.y + 0.5, z: pos.z + 0.8 }, { x: c.x + 3.4, y: c.y + 3.4, z: 2 });
447
+ const ang0 = Math.atan2(pos.y - c.y, pos.x - c.x);
448
+ const ang1 = Math.atan2(end.y - c.y, end.x - c.x);
449
+ let d = ang1 - ang0;
450
+ while (d > Math.PI) d -= Math.PI * 2;
451
+ while (d < -Math.PI) d += Math.PI * 2;
452
+ const steps = Math.max(1, Math.round(Math.abs(d) / 0.5));
453
+ for (let i = 1; i <= steps; i++) { const an = ang0 + (d * i) / steps; pts.push({ x: c.x + Math.cos(an) * 4.4, y: c.y + Math.sin(an) * 4.4, z: WALK_Z }); }
454
+ return pts;
455
+ }
456
+
457
+ towerHeight(m) {
458
+ const vram = m.m?.gpu?.vramBytes || 4 * 1024 ** 3;
459
+ const floors = clamp(Math.round(vram / (4 * 1024 ** 3)) + 1, 2, 5);
460
+ return floors * FLOOR_H + 0.4;
461
+ }
462
+
463
+ /* ---------- task boards ---------- */
464
+ boardOf(room) {
465
+ // back rooms: on the back wall; the Lobby and Ops: a standing board; the Library: the left wall
466
+ const r = ROOMS[room];
467
+ if (room.startsWith('m:')) {
468
+ const m = this.machines.get(room.slice(2));
469
+ if (!m) return null;
470
+ const tw = towerOf(m.center);
471
+ return { kind: 'stand', x0: tw.x1 + 0.35, x1: tw.x1 + 2.25, y: tw.y1 + 0.8, z0: 0.5, z1: 1.75, cols: 2, color: m.m?.color || '#7DC49B' };
472
+ }
473
+ if (!r) return null;
474
+ if (r.row === 'back') return { kind: 'wallR', x0: r.x0 + 0.35, x1: r.x0 + 2.05, y: 0, z0: 0.62, z1: 1.72, cols: 2, color: ROOM_COLOR[room] };
475
+ if (room === 'library') return { kind: 'wallL', y0: 5.75, y1: 7.5, x: 0, z0: 0.62, z1: 1.72, cols: 2, color: ROOM_COLOR[room] };
476
+ if (room === 'lobby') return { kind: 'stand', x0: 9.2, x1: 11.7, y: 5.9, z0: 0.48, z1: 1.9, cols: 3, color: ROOM_COLOR[room] };
477
+ return { kind: 'stand', x0: 12.4, x1: 13.9, y: 5.85, z0: 0.48, z1: 1.55, cols: 2, color: ROOM_COLOR[room] };
478
+ }
479
+
480
+ layoutBoards() {
481
+ const by = new Map();
482
+ for (const n of this.notes.values()) {
483
+ if (n.gone) continue;
484
+ if (!by.has(n.room)) by.set(n.room, []);
485
+ by.get(n.room).push(n);
486
+ }
487
+ for (const [room, list] of by) {
488
+ list.sort((a, b) => (a.t.status === 'done') - (b.t.status === 'done') || a.id.localeCompare(b.id));
489
+ const bd = this.boardOf(room);
490
+ if (!bd) continue;
491
+ list.forEach((n, i) => { n.slot = i; n.board = bd; n.hidden = i >= bd.cols * 3; });
492
+ by.get(room).count = list.length;
493
+ }
494
+ this.boardCounts = by;
495
+ }
496
+
497
+ /** World position of a note's center on its board. */
498
+ notePos(n, slot = n.slot ?? 0, bd = n.board || this.boardOf(n.room)) {
499
+ if (!bd) return { x: ENTRANCE.x, y: ENTRANCE.y, z: 1 };
500
+ const col = slot % bd.cols; const row = Math.floor(slot / bd.cols) % 3;
501
+ const zt = bd.z1 - 0.32 - row * 0.34;
502
+ if (bd.kind === 'wallL') { const w = (bd.y1 - bd.y0) / bd.cols; return { x: 0.02, y: bd.y1 - (col + 0.5) * w, z: zt }; }
503
+ const w = (bd.x1 - bd.x0) / bd.cols;
504
+ return { x: bd.x0 + (col + 0.5) * w, y: bd.y + (bd.kind === 'stand' ? -0.06 : 0.02), z: zt };
505
+ }
506
+
507
+ /* ---------- effects ---------- */
508
+ spawn(p) {
509
+ if (this.reduced() && p.kind !== 'jar') return;
510
+ if (this.parts.length > 360) this.parts.shift();
511
+ p.age = 0;
512
+ if (p.max == null) p.max = p.life;
513
+ this.parts.push(p);
514
+ }
515
+ sparkle(a, n) {
516
+ const k = this.k;
517
+ for (let i = 0; i < n; i++) this.spawn({ kind: 'spark', x: a.sx + rnd(-26, 26) * k, y: a.sy + rnd(-34, 8) * k, vx: rnd(-30, 30), vy: rnd(-60, -10), g: 40, life: rnd(0.5, 1.1), size: rnd(2, 4.5), color: pick(['#FFD072', '#FFFFFF', a.ag.color]) });
518
+ }
519
+ label(a, text, color, life = 1.4) {
520
+ this.spawn({ kind: 'label', x: a.sx + rnd(-6, 6), y: a.sy - 52 * this.k, vy: -22, text, color, life, max: life });
521
+ }
522
+ jar(a) { this.spawn({ kind: 'jarpop', x: a.sx, y: a.sy - 40 * this.k, vy: -30, life: 1.3 }); }
523
+ dust(m) {
524
+ for (let i = 0; i < 26; i++) this.later(rnd(0, 2.8), () => {
525
+ const an = rnd(0, 6.28); const r = rnd(1, FRIEND_R * 0.8);
526
+ const p = this.P(m.center.x + Math.cos(an) * r, m.center.y + Math.sin(an) * r, 0);
527
+ this.spawn({ kind: 'puff', x: p[0], y: p[1], vx: rnd(-20, 20), vy: rnd(-24, -6), life: rnd(0.8, 1.5), size: rnd(6, 14) * this.cam.A / 30 });
528
+ });
529
+ }
530
+ later(sec, fn) { (this.timers ||= []).push({ at: this.now + sec, fn }); }
531
+
532
+ get k() { return (this.cam.A * 1.32) / 64; }
533
+
534
+ /* ---------- per frame update ---------- */
535
+ update(dt) {
536
+ const reduced = this.reduced();
537
+ // camera eases toward its target
538
+ const kc = reduced ? 1 : 1 - Math.pow(0.0009, dt);
539
+ this.cam.x = lerp(this.cam.x, this.camT.x, kc);
540
+ this.cam.y = lerp(this.cam.y, this.camT.y, kc);
541
+ this.cam.z = lerp(this.cam.z, this.camT.z, kc);
542
+ this.cam.A = Math.exp(lerp(Math.log(this.cam.A), Math.log(this.camT.A), kc));
543
+ if (this.timers?.length) {
544
+ for (let i = this.timers.length - 1; i >= 0; i--) if (this.now >= this.timers[i].at) { const t = this.timers[i]; this.timers.splice(i, 1); t.fn(); }
545
+ }
546
+ const c = this.getCity();
547
+ for (const a of this.actors.values()) {
548
+ if (a.path.length) {
549
+ const to = a.path[0];
550
+ const dx = to.x - a.pos.x; const dy = to.y - a.pos.y; const dz = to.z - a.pos.z;
551
+ const dist = Math.hypot(dx, dy, dz * 0.7);
552
+ const step = WALK_SPEED * (a.path.length > 14 ? 2.6 : a.path.length > 6 ? 1.3 : 1) * dt;
553
+ if (Math.abs(dx - dy) > 0.01) a.facing = dx - dy > 0 ? 1 : -1;
554
+ if (dist <= step) {
555
+ a.pos = { x: to.x, y: to.y, z: to.z };
556
+ a.path.shift();
557
+ if (!a.path.length) { a.squashAt = this.now; a.onDone?.(); a.onDone = null; a.dwellUntil = this.now + (a.place.kind === 'room' && a.ag.taskId ? 2.5 : 0.6); }
558
+ } else {
559
+ a.pos.x += (dx / dist) * step; a.pos.y += (dy / dist) * step; a.pos.z += (dz / dist) * step;
560
+ }
561
+ }
562
+ if (a.next && !a.path.length && this.now >= (a.dwellUntil || 0)) { const nx = a.next; a.next = null; if (nx.key !== a.placeKey) this.walk(a, nx); }
563
+ if (a.born > 0) a.alpha = clamp((this.now - a.born) / 0.6);
564
+ if (a.leaving) a.alpha = 1 - clamp((this.now - a.leaving - 1.2) / 1.2);
565
+ const st = a.ag.status;
566
+ a.mode = a.path.length ? 'walk' : st === 'working' ? 'working' : st === 'thinking' ? 'thinking' : st === 'waiting' ? 'waiting' : st === 'reviewing' ? 'reviewing' : st === 'testing' ? 'testing' : st === 'sleeping' || st === 'offline' ? 'sleep' : (this.now - a.idleSince > 14 ? 'sleep' : 'idle');
567
+ if (a.place.kind === 'roof' && !a.path.length) a.mode = 'borrow';
568
+ if (!reduced && !a.path.length) {
569
+ if (a.mode === 'working' && this.now > a.nextGlyph && this.cam.A > 16) { this.glyph(a); a.nextGlyph = this.now + rnd(0.18, 0.34); }
570
+ else if (a.mode === 'sleep' && this.now > a.nextZ && this.cam.A > 16) { this.spawn({ kind: 'z', x: a.sx + 10 * this.k, y: a.sy - 20 * this.k, vx: rnd(6, 14), vy: -14, life: 2.2, size: clamp(this.cam.A * 0.32, 9, 17) }); a.nextZ = this.now + 1.2; }
571
+ else if (a.mode === 'testing' && this.now > a.nextGlyph && this.cam.A > 16) { this.spawn({ kind: 'bubble', x: a.sx + rnd(-8, 8) + 26 * this.k, y: a.sy - 46 * this.k, vx: rnd(-6, 6), vy: rnd(-34, -20), life: rnd(0.7, 1.2), size: rnd(1.5, 3.2), color: '#7DC49B' }); a.nextGlyph = this.now + rnd(0.1, 0.22); }
572
+ else if (a.mode === 'borrow' && this.now > a.nextGlyph) { this.spawn({ kind: 'dot', x: a.sx + rnd(-12, 12) * this.k, y: a.sy + rnd(-10, 10) * this.k, vx: rnd(-10, 10), vy: rnd(-40, -20), life: rnd(0.6, 1), size: rnd(1.5, 3), color: '#7FE3FF' }); a.nextGlyph = this.now + 0.12; }
573
+ }
574
+ if (this.now > a.blinkAt + 0.14) a.blinkAt = this.now + rnd(2.2, 5.5);
575
+ if (a.bubble && this.now > a.bubble.until) a.bubble = null;
576
+ }
577
+ for (const m of this.machines.values()) m.glow = Math.max(borrowTo(c, m.id) ? 0.6 : 0, m.glow - dt * 0.35);
578
+ if (this.hostGlow) this.hostGlow.glow = Math.max(0, this.hostGlow.glow - dt * 0.35);
579
+ for (let i = this.couriers.length - 1; i >= 0; i--) if (this.now - this.couriers[i].t0 > this.couriers[i].dur) this.couriers.splice(i, 1);
580
+ for (let i = this.parts.length - 1; i >= 0; i--) {
581
+ const p = this.parts[i];
582
+ p.age += dt;
583
+ if (p.age >= p.max) { this.parts.splice(i, 1); continue; }
584
+ p.vy = (p.vy || 0) + (p.g || 0) * dt;
585
+ p.x += (p.vx || 0) * dt;
586
+ p.y += (p.vy || 0) * dt;
587
+ }
588
+ }
589
+
590
+ glyph(a) {
591
+ const room = a.place.kind === 'room' ? a.place.room : 'code';
592
+ const base = [a.sx, a.sy - 6 * this.k];
593
+ this.spawn({ kind: 'glyph', x: base[0] + rnd(-16, 16) * this.k, y: base[1] - rnd(0, 6), vx: rnd(-10, 10), vy: rnd(-46, -30), life: rnd(1.1, 1.6), text: pick(GLYPHS[room] || GLYPHS.code), color: a.ag.color, size: clamp(this.cam.A * 0.3, 9, 20) });
594
+ }
595
+
596
+ /* ---------- palette ---------- */
597
+ palette() {
598
+ const h = this.hourFn();
599
+ const key = Math.round(h * 12);
600
+ if (key === this.palAt && this.pal) return this.pal;
601
+ this.palAt = key;
602
+ const night = nightFactor(h);
603
+ const gold = goldenFactor(h) * (1 - night * 0.6);
604
+ const out = { night, gold, hour: h };
605
+ for (const [k, v] of Object.entries(DAY)) {
606
+ let c = mixHex(v, NIGHT_TINT, night * 0.55);
607
+ if (gold) c = mixHex(c, DUSK_TINT, gold * 0.12);
608
+ out[k] = c;
609
+ }
610
+ const skyTop = mixHex(mixHex('#6FA6D8', '#06091A', night), '#3E3470', gold * 0.75);
611
+ const skyMid = mixHex(mixHex('#A9CDE8', '#121836', night), '#C0627A', gold * 0.6);
612
+ const skyBot = mixHex(mixHex('#E9E6D6', '#1C2245', night), '#F7A65E', gold * 0.85);
613
+ Object.assign(out, { skyTop, skyMid, skyBot });
614
+ this.pal = out;
615
+ this.skyGrad = null;
616
+ return out;
617
+ }
618
+
619
+ buildPaths() {
620
+ // hexagon ground tiles around the origin, three shades
621
+ const mk = (r, span) => {
622
+ const hw = Math.sqrt(3) * r; const vh = 1.5 * r;
623
+ const fill = [new Path2D(), new Path2D(), new Path2D()];
624
+ const grout = new Path2D();
625
+ for (let row = -Math.ceil(span / vh) - 1; row * vh < span + r; row++) {
626
+ for (let col = -Math.ceil(span / hw) - 1; col * hw < span + hw; col++) {
627
+ const cx = col * hw + (row & 1 ? hw / 2 : 0); const cy = row * vh;
628
+ const q = col - (row - (row & 1)) / 2;
629
+ const idx = (((q - row) % 3) + 3) % 3;
630
+ const p = new Path2D();
631
+ for (let i = 0; i < 6; i++) { const an = (Math.PI / 180) * (60 * i - 30); const px = cx + r * Math.cos(an); const py = cy + r * Math.sin(an); if (i === 0) p.moveTo(px, py); else p.lineTo(px, py); }
632
+ p.closePath();
633
+ fill[idx].addPath(p); grout.addPath(p);
634
+ }
635
+ }
636
+ return { fill, grout };
637
+ };
638
+ this.grassTiles = mk(0.95, 16);
639
+ this.floorTiles = mk(0.42, 18);
640
+ const comb = new Path2D(); const wr = 0.2; const whw = Math.sqrt(3) * wr;
641
+ for (let row = 0; row * 1.5 * wr < WALL + 0.3; row++) {
642
+ for (let col = -1; col * whw < 18; col++) {
643
+ const cx = col * whw + (row & 1 ? whw / 2 : 0); const cy = row * 1.5 * wr;
644
+ for (let i = 0; i < 6; i++) { const an = (Math.PI / 180) * (60 * i - 30); const px = cx + wr * 0.9 * Math.cos(an); const py = cy + wr * 0.9 * Math.sin(an); if (i === 0) comb.moveTo(px, py); else comb.lineTo(px, py); }
645
+ comb.closePath();
646
+ }
647
+ }
648
+ this.comb = comb;
649
+ }
650
+
651
+ /* ---------- transforms ---------- */
652
+ base() { this.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0); }
653
+ floorT(z = 0) { const d = this.dpr; const A = this.cam.A; this.ctx.setTransform(d * A, d * A / 2, -d * A, d * A / 2, d * this.ox, d * (this.oy - z * A * ZK)); }
654
+ /** A plane along x at y = y0 (a wall facing the lower left): u = x, t = down from zTop. */
655
+ wallRT(y0, zTop, k = 1) { const d = this.dpr; const A = this.cam.A; const [px, py] = this.P(0, y0, zTop); this.ctx.setTransform((d * A) / k, (d * A / 2) / k, 0, (d * A * ZK) / k, d * px, d * py); }
656
+ /** A plane along y at x = x0 (a wall facing the lower right): u = yEnd - y, t = down from zTop. */
657
+ wallLT(x0, yEnd, zTop, k = 1) { const d = this.dpr; const A = this.cam.A; const [px, py] = this.P(x0, yEnd, zTop); this.ctx.setTransform((d * A) / k, (-d * A / 2) / k, 0, (d * A * ZK) / k, d * px, d * py); }
658
+
659
+ poly(pts, fill, stroke, lw = 1) {
660
+ const ctx = this.ctx;
661
+ ctx.beginPath();
662
+ ctx.moveTo(pts[0][0], pts[0][1]);
663
+ for (let i = 1; i < pts.length; i++) ctx.lineTo(pts[i][0], pts[i][1]);
664
+ ctx.closePath();
665
+ if (fill) { ctx.fillStyle = fill; ctx.fill(); }
666
+ if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = lw; ctx.stroke(); }
667
+ }
668
+
669
+ box(x, y, z, dx, dy, dz, top, left, right, edge = 'rgba(36,22,12,.3)') {
670
+ const P = (X, Y, Z) => this.P(X, Y, Z);
671
+ this.poly([P(x, y + dy, z), P(x + dx, y + dy, z), P(x + dx, y + dy, z + dz), P(x, y + dy, z + dz)], left, edge);
672
+ this.poly([P(x + dx, y, z), P(x + dx, y + dy, z), P(x + dx, y + dy, z + dz), P(x + dx, y, z + dz)], right, edge);
673
+ this.poly([P(x, y, z + dz), P(x + dx, y, z + dz), P(x + dx, y + dy, z + dz), P(x, y + dy, z + dz)], top, edge);
674
+ }
675
+
676
+ cyl(x, y, z, r, hgt, body, top) {
677
+ const ctx = this.ctx;
678
+ const [bx, by] = this.P(x, y, z);
679
+ const [, ty] = this.P(x, y, z + hgt);
680
+ const rx = r * this.cam.A * 1.414; const ry = rx / 2;
681
+ ctx.fillStyle = body;
682
+ ctx.beginPath(); ctx.ellipse(bx, by, rx, ry, 0, 0, Math.PI); ctx.lineTo(bx - rx, ty); ctx.ellipse(bx, ty, rx, ry, 0, Math.PI, 0, true); ctx.closePath(); ctx.fill();
683
+ ctx.strokeStyle = 'rgba(36,22,12,.32)'; ctx.lineWidth = 1; ctx.stroke();
684
+ ctx.fillStyle = top; ctx.beginPath(); ctx.ellipse(bx, ty, rx, ry, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
685
+ }
686
+
687
+ /** True when a world box is (partly) on screen. */
688
+ visible(x0, y0, x1, y1, zTop = 3, zBot = -3) {
689
+ const b = this.isoBox([[x0, y0, zTop], [x1, y0, zTop], [x0, y1, zTop], [x1, y1, zTop], [x0, y0, zBot], [x1, y1, zBot], [x1, y0, zBot], [x0, y1, zBot]]);
690
+ const A = this.cam.A;
691
+ const sx0 = this.ox + b.x0 * A; const sx1 = this.ox + b.x1 * A; const sy0 = this.oy + b.y0 * A; const sy1 = this.oy + b.y1 * A;
692
+ return sx1 > -40 && sx0 < this.w + 40 && sy1 > -40 && sy0 < this.h + 40;
693
+ }
694
+
695
+ /* ---------- drawing ---------- */
696
+ draw() {
697
+ if (!this.w) return;
698
+ const ctx = this.ctx;
699
+ const pal = this.palette();
700
+ this.computeOrigin();
701
+ this.anchorParticles();
702
+ this.base();
703
+ this.hits = [];
704
+ this.machineHits = [];
705
+ this.drawSky(pal);
706
+ const c = this.getCity();
707
+ const islands = [{ home: true, cx: HOME.cx, cy: HOME.cy, r: HOME.r }];
708
+ for (const m of this.machines.values()) islands.push({ m, cx: m.center.x, cy: m.center.y, r: FRIEND_R });
709
+ islands.sort((p, q) => (p.cx + p.cy) - (q.cx + q.cy));
710
+ for (const is of islands) this.drawIsland(is, pal);
711
+ // bridges and the tank behind the building go first, so its walls hide them
712
+ const behind = (m) => m.center.x + m.center.y < HOME.cx + HOME.cy - 4;
713
+ for (const m of this.machines.values()) if (behind(m)) this.drawBridge(m, pal, c);
714
+ if (this.visible(TANK.x - 2, TANK.y - 2, TANK.x + 2, TANK.y + 2, 8, 0)) this.drawTank(TANK.x, TANK.y, pal);
715
+ this.drawBuildingGround(pal);
716
+ for (const m of this.machines.values()) if (!behind(m)) this.drawBridge(m, pal, c);
717
+
718
+ // depth sorted things
719
+ const list = [];
720
+ this.buildingItems(list, pal);
721
+ for (const m of this.machines.values()) {
722
+ if (!this.visible(m.center.x - FRIEND_R, m.center.y - FRIEND_R, m.center.x + FRIEND_R, m.center.y + FRIEND_R, 10, -1)) continue;
723
+ list.push({ k: m.center.x + m.center.y, f: () => this.drawTower(m, pal, c) });
724
+ for (const d of this.friendDecor(m)) list.push(d);
725
+ }
726
+ for (const d of this.homeDecor(pal)) list.push(d);
727
+ for (const a of this.actors.values()) {
728
+ if (!this.visible(a.pos.x - 1, a.pos.y - 1, a.pos.x + 1, a.pos.y + 1, a.pos.z + 2, a.pos.z - 1)) { a.sx = -999; continue; }
729
+ list.push({ k: a.pos.x + a.pos.y + (a.desk && !a.path.length ? -0.05 : 0.02), f: () => this.drawBee(a) });
730
+ }
731
+ list.sort((p, q) => p.k - q.k);
732
+ for (const it of list) it.f();
733
+
734
+ this.drawCouriers(c);
735
+ this.drawLights(pal);
736
+ this.drawFocus();
737
+ this.drawOverlay(pal, c);
738
+ this.drawParticles();
739
+ this.base();
740
+ }
741
+
742
+ /** Particles live in screen space: when the camera moves, move them with the world. */
743
+ anchorParticles() {
744
+ const o = this.lastO;
745
+ this.lastO = { x: this.ox, y: this.oy, A: this.cam.A };
746
+ if (!o || (o.x === this.ox && o.y === this.oy && o.A === this.cam.A)) return;
747
+ const k = this.cam.A / o.A;
748
+ const mx = (x) => this.ox + (x - o.x) * k;
749
+ const my = (y) => this.oy + (y - o.y) * k;
750
+ for (const p of this.parts) {
751
+ p.x = mx(p.x); p.y = my(p.y);
752
+ if (k !== 1) { p.vx = (p.vx || 0) * k; p.vy = (p.vy || 0) * k; }
753
+ }
754
+ }
755
+
756
+ drawSky(pal) {
757
+ const ctx = this.ctx;
758
+ if (!this.skyGrad) {
759
+ const g = ctx.createLinearGradient(0, 0, 0, this.h);
760
+ g.addColorStop(0, pal.skyTop); g.addColorStop(0.55, pal.skyMid); g.addColorStop(1, pal.skyBot);
761
+ this.skyGrad = g;
762
+ }
763
+ ctx.fillStyle = this.skyGrad;
764
+ ctx.fillRect(0, 0, this.w, this.h);
765
+ // stars
766
+ if (pal.night > 0.3) {
767
+ ctx.fillStyle = '#FFF8E6';
768
+ for (const s of this.stars) {
769
+ const tw = this.reduced() ? 0.8 : 0.55 + 0.45 * Math.sin(this.now * 1.4 + s.ph);
770
+ ctx.globalAlpha = (pal.night - 0.3) * 1.4 * tw;
771
+ ctx.beginPath(); ctx.arc(s.x * this.w, s.y * this.h * 0.7, s.r, 0, 6.28); ctx.fill();
772
+ }
773
+ ctx.globalAlpha = 1;
774
+ }
775
+ // sun or moon, the height follows the clock
776
+ const h = pal.hour;
777
+ const day = h >= 6 && h < 19.5;
778
+ const arc = day ? clamp((h - 6) / 13.5) : (((h + 24 - 19.5) % 24) / 10.5);
779
+ const sx = this.w * (0.12 + arc * 0.76);
780
+ const sy = this.h * (0.1 + Math.pow(Math.abs(arc - 0.5) * 2, 2) * 0.26);
781
+ if (day) {
782
+ const g = ctx.createRadialGradient(sx, sy, 4, sx, sy, 150);
783
+ g.addColorStop(0, pal.gold > 0.3 ? 'rgba(255,200,120,.55)' : 'rgba(255,246,210,.55)'); g.addColorStop(1, 'rgba(255,230,180,0)');
784
+ ctx.fillStyle = g; ctx.fillRect(sx - 150, sy - 150, 300, 300);
785
+ ctx.fillStyle = pal.gold > 0.3 ? '#FFC27A' : '#FFF4C8'; ctx.beginPath(); ctx.arc(sx, sy, 26, 0, 6.28); ctx.fill();
786
+ } else {
787
+ const g = ctx.createRadialGradient(sx, sy, 4, sx, sy, 110);
788
+ g.addColorStop(0, 'rgba(220,230,255,.22)'); g.addColorStop(1, 'rgba(220,230,255,0)');
789
+ ctx.fillStyle = g; ctx.fillRect(sx - 110, sy - 110, 220, 220);
790
+ ctx.fillStyle = '#F4EBCF'; ctx.beginPath(); ctx.arc(sx, sy, 20, 0, 6.28); ctx.fill();
791
+ ctx.fillStyle = pal.skyTop; ctx.beginPath(); ctx.arc(sx + 9, sy - 5, 17, 0, 6.28); ctx.fill();
792
+ }
793
+ // far islands, a soft silhouette layer that drifts with the camera
794
+ const par = 0.18;
795
+ const bx = -((this.cam.x - this.cam.y) * this.cam.A * par) % (this.w * 1.4);
796
+ ctx.fillStyle = mixHex(pal.skyMid, pal.skyTop, 0.35);
797
+ ctx.globalAlpha = 0.55;
798
+ for (let i = 0; i < 5; i++) {
799
+ const x = ((bx + i * this.w * 0.33 + this.w * 2.8) % (this.w * 1.4)) - this.w * 0.2;
800
+ const y = this.h * (0.3 + ((i * 37) % 10) / 40);
801
+ const s = 40 + ((i * 53) % 5) * 14;
802
+ ctx.beginPath(); ctx.ellipse(x, y, s, s * 0.22, 0, Math.PI, 0); ctx.lineTo(x + s * 0.4, y + s * 0.5); ctx.lineTo(x - s * 0.3, y + s * 0.45); ctx.closePath(); ctx.fill();
803
+ }
804
+ ctx.globalAlpha = 1;
805
+ // clouds
806
+ const drift = this.reduced() ? 0 : this.now;
807
+ for (const cl of this.clouds) {
808
+ const x = (((cl.x + drift * cl.v) % 1.3) - 0.15) * this.w;
809
+ const y = cl.y * this.h + 40;
810
+ const s = cl.s * Math.min(this.w, 1400) * 0.07;
811
+ ctx.fillStyle = pal.night > 0.5 ? `rgba(150,160,200,${0.08 + (1 - pal.night) * 0.2})` : `rgba(255,255,255,${0.42 + pal.gold * 0.1})`;
812
+ if (pal.gold > 0.3) ctx.fillStyle = `rgba(255,214,190,${0.45})`;
813
+ ctx.beginPath();
814
+ ctx.ellipse(x, y, s, s * 0.32, 0, 0, 6.28);
815
+ ctx.ellipse(x + s * 0.5, y - s * 0.14, s * 0.55, s * 0.3, 0, 0, 6.28);
816
+ ctx.ellipse(x - s * 0.45, y + s * 0.02, s * 0.45, s * 0.22, 0, 0, 6.28);
817
+ ctx.fill();
818
+ }
819
+ // a sea of clouds below the islands
820
+ const seaY = this.h * 0.86;
821
+ const g2 = ctx.createLinearGradient(0, seaY - 60, 0, this.h);
822
+ g2.addColorStop(0, 'rgba(255,255,255,0)');
823
+ g2.addColorStop(1, pal.night > 0.5 ? 'rgba(60,70,110,.5)' : pal.gold > 0.3 ? 'rgba(255,200,170,.55)' : 'rgba(255,255,255,.6)');
824
+ ctx.fillStyle = g2; ctx.fillRect(0, seaY - 60, this.w, this.h - seaY + 60);
825
+ }
826
+
827
+ drawIsland(is, pal) {
828
+ const r = is.r; const cx = is.cx; const cy = is.cy;
829
+ let rise = 0; let al = 1;
830
+ if (is.m) {
831
+ const t = this.now - is.m.born;
832
+ if (t < 1.4) { const u = easeOut(clamp(t / 1.4)); rise = (1 - u) * -9; al = clamp(t / 0.5); }
833
+ if (is.m.leaving) al = 1 - clamp((this.now - is.m.leaving) / 1.2);
834
+ }
835
+ if (!this.visible(cx - r, cy - r, cx + r, cy + r, 1, -r * 1.2 + rise)) return;
836
+ const ctx = this.ctx;
837
+ ctx.globalAlpha = al;
838
+ const corners = hexCorners(cx, cy, r);
839
+ const depth = is.home ? 2.3 : 1.7;
840
+ const P = (x, y, z) => this.P(x, y, z + rise);
841
+ // underside: a rocky cone under the island
842
+ const tip = P(cx + 0.6, cy + 0.6, -r * 0.95);
843
+ const bottom = corners.map(([x, y]) => P(x, y, -depth));
844
+ for (let i = 0; i < 6; i++) {
845
+ const p = bottom[i]; const q = bottom[(i + 1) % 6];
846
+ this.poly([p, q, tip], [pal.rockC, pal.rockD, pal.rockD, pal.rockD, pal.rockC, pal.rockB][i], 'rgba(20,12,6,.35)');
847
+ }
848
+ // hanging rocks
849
+ for (let i = 0; i < (is.home ? 5 : 3); i++) {
850
+ const an = 0.3 + i * 1.1; const rr = r * (0.35 + (i % 3) * 0.15);
851
+ const x = cx + Math.cos(an) * rr; const y = cy + Math.sin(an) * rr;
852
+ const top = P(x, y, -depth - r * 0.25); const tp = P(x + 0.2, y + 0.2, -depth - r * (0.55 + (i % 2) * 0.2));
853
+ this.poly([[top[0] - this.cam.A * 0.9, top[1]], [top[0] + this.cam.A * 0.9, top[1]], tp], pal.rockC, 'rgba(20,12,6,.3)');
854
+ }
855
+ // sides of the island that face the viewer: edges 0, 1 and 5
856
+ for (const i of [5, 0, 1]) {
857
+ const [x0, y0] = corners[i]; const [x1, y1] = corners[(i + 1) % 6];
858
+ const tone = i === 1 ? 0 : i === 0 ? -0.08 : -0.18;
859
+ this.poly([P(x0, y0, 0), P(x1, y1, 0), P(x1, y1, -depth), P(x0, y0, -depth)], shade(pal.rockA, tone), 'rgba(20,12,6,.35)');
860
+ this.poly([P(x0, y0, 0), P(x1, y1, 0), P(x1, y1, -0.42), P(x0, y0, -0.42)], shade(pal.soil, tone));
861
+ // strata
862
+ ctx.strokeStyle = 'rgba(20,12,6,.18)'; ctx.lineWidth = Math.max(1, this.cam.A * 0.04);
863
+ for (const zz of [-0.95, -1.5]) {
864
+ if (-zz > depth) continue;
865
+ const a = P(x0, y0, zz); const b = P(x1, y1, zz + 0.12);
866
+ ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.stroke();
867
+ }
868
+ // grass lip
869
+ this.poly([P(x0, y0, 0), P(x1, y1, 0), P(x1, y1, -0.14), P(x0, y0, -0.14)], pal.lip);
870
+ }
871
+ // top: honeycomb meadow tiles, clipped to the hexagon
872
+ const d = this.dpr; const A = this.cam.A;
873
+ const o = this.P(cx, cy, rise);
874
+ ctx.setTransform(d * A, d * A / 2, -d * A, d * A / 2, d * o[0], d * o[1]);
875
+ ctx.save();
876
+ ctx.beginPath();
877
+ hexCorners(0, 0, r).forEach(([x, y], i) => (i ? ctx.lineTo(x, y) : ctx.moveTo(x, y)));
878
+ ctx.closePath(); ctx.clip();
879
+ ctx.fillStyle = pal.grassA; ctx.fill(this.grassTiles.fill[0]);
880
+ ctx.fillStyle = pal.grassB; ctx.fill(this.grassTiles.fill[1]);
881
+ ctx.fillStyle = pal.grassC; ctx.fill(this.grassTiles.fill[2]);
882
+ ctx.strokeStyle = alpha(pal.grout, 0.75); ctx.lineWidth = 0.05; ctx.stroke(this.grassTiles.grout);
883
+ // stone paths: home plaza to every bridge, friend tower to its bridge
884
+ ctx.lineCap = 'round';
885
+ const paths = [];
886
+ if (is.home) { for (const m of this.machines.values()) paths.push([[PLAZA.x - cx, PLAZA.y - cy], [m.bridge.a.x - cx, m.bridge.a.y - cy]]); paths.push([[PLAZA.x - cx, PLAZA.y - cy], [ENTRANCE.x - cx, ENTRANCE.y - cy]]); }
887
+ else { paths.push([[0, 0], [is.m.bridge.b.x - cx, is.m.bridge.b.y - cy]]); paths.push([[0, 0], [3.7, 3.7]]); }
888
+ for (const [w, col] of [[1.55, pal.pathEdge], [1.25, pal.path]]) {
889
+ ctx.strokeStyle = col; ctx.lineWidth = w;
890
+ for (const [p0, p1] of paths) { ctx.beginPath(); ctx.moveTo(p0[0], p0[1]); ctx.lineTo(p1[0], p1[1]); ctx.stroke(); }
891
+ }
892
+ if (is.home) {
893
+ // the plaza: a honey hexagon
894
+ ctx.fillStyle = pal.pathEdge; ctx.beginPath(); hexPath(ctx, PLAZA.x - cx, PLAZA.y - cy + 0.4, 2.6); ctx.fill();
895
+ ctx.fillStyle = pal.path; ctx.beginPath(); hexPath(ctx, PLAZA.x - cx, PLAZA.y - cy + 0.4, 2.35); ctx.fill();
896
+ ctx.strokeStyle = alpha('#E8A93A', 0.45); ctx.lineWidth = 0.06; ctx.beginPath(); hexPath(ctx, PLAZA.x - cx, PLAZA.y - cy + 0.4, 1.4); ctx.stroke();
897
+ } else {
898
+ ctx.fillStyle = pal.pathEdge; ctx.beginPath(); hexPath(ctx, 0, 0, 3.6); ctx.fill();
899
+ ctx.fillStyle = pal.path; ctx.beginPath(); hexPath(ctx, 0, 0, 3.35); ctx.fill();
900
+ }
901
+ ctx.lineCap = 'butt';
902
+ ctx.restore();
903
+ this.base();
904
+ ctx.globalAlpha = 1;
905
+ }
906
+
907
+ /** Plinth, floors, corridor and the two tall back walls of your building. */
908
+ drawBuildingGround(pal) {
909
+ if (!this.visible(-1, -1, 17, 11, WALL + 3, -1)) return;
910
+ const ctx = this.ctx;
911
+ const P = (x, y, z) => this.P(x, y, z);
912
+ const { x0, x1, y0, y1 } = BUILDING;
913
+ const pz = 0.3;
914
+ // plinth
915
+ this.poly([P(x0 - 0.3, y1 + 0.3, 0), P(x1 + 0.3, y1 + 0.3, 0), P(x1 + 0.3, y1 + 0.3, pz), P(x0 - 0.3, y1 + 0.3, pz)], pal.plinthL, 'rgba(36,22,12,.35)');
916
+ this.poly([P(x1 + 0.3, y0 - 0.3, 0), P(x1 + 0.3, y1 + 0.3, 0), P(x1 + 0.3, y1 + 0.3, pz), P(x1 + 0.3, y0 - 0.3, pz)], pal.plinthR, 'rgba(36,22,12,.35)');
917
+ this.poly([P(x0 - 0.3, y0 - 0.3, pz), P(x1 + 0.3, y0 - 0.3, pz), P(x1 + 0.3, y1 + 0.3, pz), P(x0 - 0.3, y1 + 0.3, pz)], pal.plinthTop, 'rgba(36,22,12,.35)');
918
+ // entrance steps
919
+ for (let i = 0; i < 2; i++) this.box(ENTRANCE.x - 1.1 + i * 0.15, y1 + 0.3 + (1 - i) * 0.35, 0, 2.2 - i * 0.3, 0.35, pz * (i + 1) / 2, pal.plinthTop, pal.plinthL, pal.plinthR);
920
+ // floors per room, tinted by the room color
921
+ const d = this.dpr; const A = this.cam.A;
922
+ for (const [k, r] of Object.entries(ROOMS)) {
923
+ const o = this.P(0, 0, pz);
924
+ ctx.setTransform(d * A, d * A / 2, -d * A, d * A / 2, d * o[0], d * o[1]);
925
+ ctx.save();
926
+ ctx.beginPath(); ctx.rect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); ctx.clip();
927
+ const tint = (c) => mixHex(c, ROOM_COLOR[k], 0.13);
928
+ ctx.fillStyle = tint(pal.floorA); ctx.fill(this.floorTiles.fill[0]);
929
+ ctx.fillStyle = tint(pal.floorB); ctx.fill(this.floorTiles.fill[1]);
930
+ ctx.fillStyle = tint(pal.floorC); ctx.fill(this.floorTiles.fill[2]);
931
+ ctx.strokeStyle = pal.grout2; ctx.lineWidth = 0.03; ctx.stroke(this.floorTiles.grout);
932
+ // a rug in the room color
933
+ ctx.fillStyle = alpha(ROOM_COLOR[k], this.highlightRoom(k) ? 0.34 : 0.14);
934
+ ctx.beginPath(); hexPath(ctx, (r.x0 + r.x1) / 2, (r.y0 + r.y1) / 2 + 0.25, Math.min(r.x1 - r.x0, r.y1 - r.y0) * 0.36); ctx.fill();
935
+ ctx.strokeStyle = alpha(ROOM_COLOR[k], 0.45); ctx.lineWidth = 0.035; ctx.setLineDash([0.1, 0.08]); ctx.stroke(); ctx.setLineDash([]);
936
+ if (this.highlightRoom(k)) { ctx.strokeStyle = alpha('#FFD072', 0.9); ctx.lineWidth = 0.08; ctx.strokeRect(r.x0 + 0.08, r.y0 + 0.08, r.x1 - r.x0 - 0.16, r.y1 - r.y0 - 0.16); }
937
+ ctx.restore();
938
+ }
939
+ // corridor runner
940
+ const o = this.P(0, 0, pz);
941
+ ctx.setTransform(d * A, d * A / 2, -d * A, d * A / 2, d * o[0], d * o[1]);
942
+ ctx.fillStyle = pal.corridor; ctx.fillRect(0, 4.6, 16, 0.8);
943
+ ctx.fillStyle = alpha('#E8A93A', 0.5); ctx.fillRect(0.3, 4.92, 15.4, 0.16);
944
+ this.base();
945
+ // the two tall back walls with windows, wallpaper in each room color, signs and boards
946
+ this.drawBackWalls(pal);
947
+ }
948
+
949
+ drawBackWalls(pal) {
950
+ const ctx = this.ctx;
951
+ const P = (x, y, z) => this.P(x, y, z);
952
+ const zb = 0.3; const zt = WALL + 0.3;
953
+ // left wall x = 0 (Design and Library), right wall y = 0 (the four pipeline rooms)
954
+ this.poly([P(0, 0, zb), P(0, 10, zb), P(0, 10, zt), P(0, 0, zt)], pal.wallL);
955
+ this.poly([P(0, 0, zb), P(16, 0, zb), P(16, 0, zt), P(0, 0, zt)], pal.wallR);
956
+ for (const side of ['L', 'R']) {
957
+ const len = side === 'L' ? 10 : 16;
958
+ if (side === 'L') this.wallLT(0, 10, zt); else this.wallRT(0, zt);
959
+ ctx.save();
960
+ ctx.beginPath(); ctx.rect(0, 0, len, WALL - 0.65); ctx.clip();
961
+ // wallpaper tinted per room
962
+ for (const [k, r] of Object.entries(ROOMS)) {
963
+ let u0; let u1;
964
+ if (side === 'R') { if (r.row !== 'back') continue; u0 = r.x0; u1 = r.x1; } else { if (r.x0 !== 0) continue; u0 = 10 - r.y1; u1 = 10 - r.y0; }
965
+ ctx.fillStyle = alpha(ROOM_COLOR[k], 0.1); ctx.fillRect(u0, 0, u1 - u0, WALL);
966
+ }
967
+ ctx.strokeStyle = alpha(pal.comb, 0.55); ctx.lineWidth = 0.012; ctx.stroke(this.comb);
968
+ ctx.restore();
969
+ ctx.fillStyle = side === 'L' ? mixHex(pal.trim, '#ffffff', 0.35) : mixHex(pal.trim, '#ffffff', 0.25);
970
+ ctx.fillRect(0, WALL - 0.65, len, 0.65);
971
+ ctx.fillStyle = pal.trim; ctx.fillRect(0, WALL - 0.69, len, 0.05); ctx.fillRect(0, WALL - 0.08, len, 0.08);
972
+ ctx.strokeStyle = 'rgba(36,22,12,.12)'; ctx.lineWidth = 0.02; ctx.beginPath();
973
+ for (let u = 0.5; u < len; u += 0.5) { ctx.moveTo(u, WALL - 0.63); ctx.lineTo(u, WALL - 0.08); }
974
+ ctx.stroke();
975
+ // the corridor gap on the left wall
976
+ if (side === 'L') { ctx.fillStyle = 'rgba(36,22,12,.22)'; ctx.fillRect(10 - 5.4, 0, 0.8, WALL); }
977
+ const g = ctx.createLinearGradient(side === 'L' ? len : 0, 0, side === 'L' ? len - 3 : 3, 0);
978
+ g.addColorStop(0, 'rgba(36,22,12,.2)'); g.addColorStop(1, 'rgba(36,22,12,0)');
979
+ ctx.fillStyle = g; ctx.fillRect(0, 0, len, WALL);
980
+ }
981
+ this.base();
982
+ // windows: one per back room, the sky outside follows the clock
983
+ const night = this.pal.night;
984
+ const top = mixHex(mixHex('#78B9E6', '#0B1430', night), '#4A3F7E', this.pal.gold * 0.7);
985
+ const bot = mixHex(mixHex('#D6EEF8', '#1E2B4E', night), '#F5A766', this.pal.gold * 0.85);
986
+ const win = (u0, u1, side) => {
987
+ if (side === 'L') this.wallLT(0, 10, zt); else this.wallRT(0, zt);
988
+ const t0 = 0.28; const t1 = 1.08;
989
+ const g = ctx.createLinearGradient(0, t0, 0, t1); g.addColorStop(0, top); g.addColorStop(1, bot);
990
+ ctx.fillStyle = g; ctx.fillRect(u0, t0, u1 - u0, t1 - t0);
991
+ if (night > 0.5) { ctx.fillStyle = 'rgba(255,248,220,.8)'; for (let i = 0; i < 4; i++) ctx.fillRect(u0 + 0.1 + ((i * 37) % 9) / 10 * (u1 - u0 - 0.2), t0 + 0.1 + ((i * 23) % 5) / 12, 0.025, 0.025); }
992
+ ctx.strokeStyle = shade(this.pal.trim, 0.1); ctx.lineWidth = 0.06; ctx.strokeRect(u0, t0, u1 - u0, t1 - t0);
993
+ ctx.lineWidth = 0.035; ctx.beginPath(); ctx.moveTo((u0 + u1) / 2, t0); ctx.lineTo((u0 + u1) / 2, t1); ctx.stroke();
994
+ ctx.fillStyle = shade(this.pal.trim, 0.2); ctx.fillRect(u0 - 0.06, t1, u1 - u0 + 0.12, 0.06);
995
+ };
996
+ for (const k of ['design', 'code', 'review', 'test']) { const r = ROOMS[k]; win(r.x1 - 1.6, r.x1 - 0.45, 'R'); }
997
+ win(10 - 3.4, 10 - 1.9, 'L');
998
+ win(10 - 9.1, 10 - 7.6, 'L');
999
+ this.base();
1000
+ // wall caps
1001
+ const T = 0.16;
1002
+ this.poly([P(0, 0, zt), P(0, 10, zt), P(-T, 10, zt), P(-T, -T, zt), P(16, -T, zt), P(16, 0, zt)], shade(pal.trim, 0.25), 'rgba(36,22,12,.5)');
1003
+ this.poly([P(-T, 10, zb), P(0, 10, zb), P(0, 10, zt), P(-T, 10, zt)], shade(pal.trim, -0.1), 'rgba(36,22,12,.5)');
1004
+ this.poly([P(16, -T, zb), P(16, 0, zb), P(16, 0, zt), P(16, -T, zt)], shade(pal.trim, -0.25), 'rgba(36,22,12,.5)');
1005
+ // room plaques on the back walls
1006
+ for (const k of ['design', 'code', 'review', 'test']) { const r = ROOMS[k]; this.plaque(k, 'R', r.x0 + 1.2, pal); }
1007
+ this.plaque('library', 'L', 10 - 9.4 + 0.9, pal);
1008
+ // room boards on the walls
1009
+ for (const k of ['design', 'code', 'review', 'test', 'library']) this.drawBoard(k, pal);
1010
+ // wall decor per room (drawn flat on the walls)
1011
+ this.wallDecor(pal);
1012
+ }
1013
+
1014
+ plaque(k, side, u, pal) {
1015
+ if (this.cam.A < 44) return;
1016
+ const ctx = this.ctx;
1017
+ const zt = WALL + 0.3;
1018
+ if (side === 'L') this.wallLT(0, 10, zt, 100); else this.wallRT(0, zt, 100);
1019
+ const name = this.t(`city.room.${k}`).toUpperCase();
1020
+ ctx.font = `800 15px ${FONT}`;
1021
+ const tw = ctx.measureText(name).width;
1022
+ const x = u * 100; const y = 14;
1023
+ ctx.fillStyle = 'rgba(36,22,12,.25)'; roundRect(ctx, x - 10 + 2, y + 3, tw + 34, 26, 6); ctx.fill();
1024
+ ctx.fillStyle = mixHex('#3A2A1C', NIGHT_TINT, pal.night * 0.3); roundRect(ctx, x - 10, y, tw + 34, 26, 6); ctx.fill();
1025
+ ctx.fillStyle = ROOM_COLOR[k]; ctx.beginPath(); hexScreen(ctx, x + 2, y + 13, 6); ctx.fill();
1026
+ ctx.fillStyle = '#F7EAD3'; ctx.fillText(name, x + 14, y + 18.5);
1027
+ this.base();
1028
+ }
1029
+
1030
+ wallDecor(pal) {
1031
+ const ctx = this.ctx;
1032
+ const zt = WALL + 0.3;
1033
+ this.wallRT(0, zt);
1034
+ // Design: color swatches
1035
+ ['#FF7AB6', '#6CC4FF', '#FFD072', '#7DC49B', '#C9A0E8'].forEach((c, i) => { ctx.fillStyle = c; ctx.fillRect(2.25 + i * 0.13, 0.62, 0.1, 0.32); });
1036
+ // Review: a wall screen with a diff
1037
+ ctx.fillStyle = '#1D2230'; ctx.fillRect(10.1, 0.25, 0.0, 0);
1038
+ // Test: a status light
1039
+ const ok = (Math.floor(this.now * 0.5) % 4) !== 3;
1040
+ ctx.fillStyle = '#2A2420'; ctx.fillRect(14.1, 0.5, 0.22, 0.5);
1041
+ ctx.fillStyle = ok ? '#5DB27E' : '#3C6B4E'; ctx.beginPath(); ctx.arc(14.21, 0.62, 0.06, 0, 6.28); ctx.fill();
1042
+ ctx.fillStyle = ok ? '#4A3A2B' : '#E8B45C'; ctx.beginPath(); ctx.arc(14.21, 0.8, 0.06, 0, 6.28); ctx.fill();
1043
+ ctx.fillStyle = '#4A3A2B'; ctx.beginPath(); ctx.arc(14.21, 0.96, 0.05, 0, 6.28); ctx.fill();
1044
+ this.base();
1045
+ void pal;
1046
+ }
1047
+
1048
+ /** A task board with its notes, on a wall or standing. */
1049
+ drawBoard(room, pal) {
1050
+ const bd = this.boardOf(room);
1051
+ if (!bd || this.cam.A < 9) return;
1052
+ const ctx = this.ctx;
1053
+ let u0; let u1;
1054
+ if (bd.kind === 'wallR') { this.wallRT(0, bd.z1 + 0.3 - 0.3); u0 = bd.x0; u1 = bd.x1; }
1055
+ else if (bd.kind === 'wallL') { this.wallLT(0, 10, bd.z1); u0 = 10 - bd.y1; u1 = 10 - bd.y0; }
1056
+ else { this.wallRT(bd.y, bd.z1); u0 = bd.x0; u1 = bd.x1; }
1057
+ const hgt = bd.z1 - bd.z0;
1058
+ ctx.fillStyle = 'rgba(36,22,12,.25)'; ctx.fillRect(u0 + 0.05, 0.06, u1 - u0, hgt);
1059
+ ctx.fillStyle = pal.trim; ctx.fillRect(u0 - 0.06, -0.06, u1 - u0 + 0.12, hgt + 0.12);
1060
+ ctx.fillStyle = mixHex('#FBF6EA', NIGHT_TINT, pal.night * 0.35); ctx.fillRect(u0, 0, u1 - u0, hgt);
1061
+ ctx.fillStyle = bd.color; ctx.fillRect(u0, 0, u1 - u0, 0.07);
1062
+ // notes
1063
+ const list = [...this.notes.values()].filter((n) => n.room === room && !n.gone && !n.hidden && !(n.fly && this.now - n.fly.t0 < 0.9));
1064
+ for (const n of list) {
1065
+ const col = n.slot % bd.cols; const row = Math.floor(n.slot / bd.cols);
1066
+ const w = (u1 - u0) / bd.cols;
1067
+ const x = bd.kind === 'wallL' ? u1 - (col + 1) * w : u0 + col * w;
1068
+ const y = 0.16 + row * 0.34;
1069
+ const pop = this.reduced() ? 1 : easeOutBack(clamp((this.now - Math.max(n.born, n.fly ? n.fly.t0 + 0.9 : -99)) / 0.35));
1070
+ const t = n.t;
1071
+ const color = t.status === 'blocked' ? '#F6B3A8' : t.status === 'done' ? '#C6E8CD' : ['in_progress', 'in_review', 'in_test', 'claimed'].includes(t.status) ? '#FFD488' : '#FFEB9C';
1072
+ ctx.save();
1073
+ ctx.translate(x + w / 2, y + 0.14);
1074
+ ctx.scale(pop, pop);
1075
+ ctx.rotate(((hash(n.id) % 7) - 3) * 0.015);
1076
+ ctx.fillStyle = 'rgba(36,22,12,.18)'; ctx.fillRect(-w / 2 + 0.07, -0.12, w - 0.1, 0.27);
1077
+ ctx.fillStyle = color; ctx.fillRect(-w / 2 + 0.05, -0.14, w - 0.1, 0.27);
1078
+ const a = t.assignee && this.actors.get(t.assignee);
1079
+ ctx.fillStyle = a?.ag.color || '#FFB531'; ctx.fillRect(-w / 2 + 0.05, -0.14, 0.04, 0.27);
1080
+ if (this.dragTask === n.id || this.highlight?.id === n.id) { ctx.strokeStyle = '#E8951E'; ctx.lineWidth = 0.03; ctx.strokeRect(-w / 2 + 0.03, -0.16, w - 0.06, 0.31); }
1081
+ if (this.cam.A > 34) {
1082
+ ctx.scale(0.01, 0.01);
1083
+ ctx.fillStyle = '#2B1D12';
1084
+ ctx.font = `650 ${this.cam.A > 80 ? 9.5 : 11}px ${FONT}`;
1085
+ const line = wrap(ctx, t.title || '', (w - 0.24) * 100, 1)[0] || '';
1086
+ ctx.fillText(line, (-w / 2 + 0.13) * 100, 0.04 * 100);
1087
+ if (t.status === 'done') { ctx.fillStyle = '#3F8A5E'; ctx.font = `800 11px ${FONT}`; ctx.fillText('✓', (w / 2 - 0.2) * 100, 0.04 * 100); }
1088
+ } else {
1089
+ ctx.fillStyle = 'rgba(43,29,18,.4)'; ctx.fillRect(-w / 2 + 0.14, -0.06, w * 0.5, 0.04); ctx.fillRect(-w / 2 + 0.14, 0.02, w * 0.32, 0.04);
1090
+ }
1091
+ ctx.restore();
1092
+ }
1093
+ const more = (this.boardCounts?.get(room)?.length || 0) - bd.cols * 3;
1094
+ if (more > 0 && this.cam.A > 24) {
1095
+ ctx.save(); ctx.scale(0.01, 0.01);
1096
+ ctx.fillStyle = 'rgba(43,29,18,.6)'; ctx.font = `700 10px ${FONT}`;
1097
+ ctx.fillText(`+${more}`, (u1 - 0.3) * 100, (hgt - 0.06) * 100);
1098
+ ctx.restore();
1099
+ }
1100
+ this.base();
1101
+ }
1102
+
1103
+ drawBoardItem(room, pal) {
1104
+ const bd = this.boardOf(room);
1105
+ if (!bd) return;
1106
+ // legs of a standing board
1107
+ const ctx = this.ctx;
1108
+ for (const x of [bd.x0 + 0.12, bd.x1 - 0.12]) {
1109
+ const a = this.P(x, bd.y + 0.02, 0.3 * (room.startsWith('m:') ? 0 : 1)); const b = this.P(x, bd.y + 0.02, bd.z0 + 0.05);
1110
+ ctx.strokeStyle = pal.trim; ctx.lineWidth = Math.max(1.5, this.cam.A * 0.06);
1111
+ ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.stroke();
1112
+ }
1113
+ this.drawBoard(room, pal);
1114
+ }
1115
+
1116
+ /** Every piece of your building that needs depth sorting: partitions, furniture, the gate. */
1117
+ buildingItems(list, pal) {
1118
+ if (!this.visible(-1, -1, 17, 11, WALL + 3, -1)) return;
1119
+ const pz = 0.3;
1120
+ // low partitions with door gaps, split into short pieces so bees sort well against them
1121
+ const walls = [];
1122
+ const gap = (x) => [x - 0.65, x + 0.65];
1123
+ const along = (y, xa, xb, gaps) => { // wall along x at y
1124
+ let x = xa;
1125
+ const cuts = [...gaps].sort((p, q) => p[0] - q[0]);
1126
+ for (const [g0, g1] of [...cuts, [xb, xb]]) {
1127
+ for (let s = x; s < g0 - 0.01; s += 1) walls.push({ kind: 'x', y, x0: s, x1: Math.min(g0, s + 1) });
1128
+ x = g1;
1129
+ }
1130
+ };
1131
+ const across = (x, ya, yb) => { for (let s = ya; s < yb - 0.01; s += 1) walls.push({ kind: 'y', x, y0: s, y1: Math.min(yb, s + 1) }); };
1132
+ along(4.6, 0, 16, ['design', 'code', 'review', 'test'].map((k) => gap(ROOMS[k].x0 + 2)));
1133
+ along(5.4, 0, 16, [gap(2), gap(6.4), gap(14)]);
1134
+ for (const x of [4, 8, 12]) across(x, 0, 4.6);
1135
+ for (const x of [4, 12]) across(x, 5.4, 10);
1136
+ // the front parapet and the right side, with the entrance gap
1137
+ along(10, 0, 16, [[6.9, 9.1]]);
1138
+ across(16, 0, 4.6); across(16, 5.4, 10);
1139
+ for (const wpc of walls) {
1140
+ const hgt = wpc.kind === 'x' && (wpc.y === 10) ? 0.26 : wpc.kind === 'y' && wpc.x === 16 ? 0.26 : PART;
1141
+ if (wpc.kind === 'x') list.push({ k: (wpc.x0 + wpc.x1) / 2 + wpc.y + 0.08, f: () => this.box(wpc.x0, wpc.y - 0.05, pz, wpc.x1 - wpc.x0, 0.1, hgt, pal.part, pal.partL, pal.partR, 'rgba(36,22,12,.22)') });
1142
+ else list.push({ k: wpc.x + (wpc.y0 + wpc.y1) / 2 + 0.08, f: () => this.box(wpc.x - 0.05, wpc.y0, pz, 0.1, wpc.y1 - wpc.y0, hgt, pal.part, pal.partL, pal.partR, 'rgba(36,22,12,.22)') });
1143
+ }
1144
+ // desks and chairs at the seats
1145
+ for (const k of Object.keys(ROOMS)) {
1146
+ for (let i = 0; i < 4; i++) {
1147
+ const s = spotFor(k, i);
1148
+ if (!s.desk) continue;
1149
+ list.push({ k: s.x + s.y + 0.55, f: () => this.drawDesk(k, s, pal) });
1150
+ list.push({ k: s.x + s.y - 0.3, f: () => this.drawChair(k, s, pal) });
1151
+ }
1152
+ }
1153
+ // room props
1154
+ const add = (x, y, f) => list.push({ k: x + y, f });
1155
+ // Design: an easel and a plant
1156
+ add(3.3, 1.2, () => this.drawEasel(3.3, 1.2, pal));
1157
+ add(0.6, 4.0, () => this.drawPlant(0.6, 4.0, 0.8, pz));
1158
+ // Code: a coffee bar and a small server
1159
+ add(7.55, 0.55, () => { this.box(6.9, 0.25, pz, 1.0, 0.55, 0.62, pal.deskTop, pal.deskL, pal.deskR); this.cyl(7.15, 0.5, pz + 0.62, 0.12, 0.26, '#5A4836', '#3B2414'); this.cyl(7.6, 0.55, pz + 0.62, 0.07, 0.12, '#F2E9DA', '#3B2414'); });
1160
+ // Review: a sofa and a tall wall screen
1161
+ add(10, 0.5, () => this.drawScreen(8.7, 11.3, pal));
1162
+ add(11.4, 3.95, () => this.drawPlant(11.5, 4.0, 0.7, pz));
1163
+ // Test: a lab bench with flasks
1164
+ add(14, 0.6, () => { this.box(12.3, 0.25, pz, 3.3, 0.6, 0.58, pal.deskTop, pal.deskL, pal.deskR); ['#7DC49B', '#FFB531', '#86A9D6', '#FF7AB6', '#7DC49B'].forEach((c, i) => this.cyl(12.6 + i * 0.32, 0.55, pz + 0.58, 0.06, 0.3 + (i % 2) * 0.08, alpha(c, 0.85), '#F2E9DA')); });
1165
+ // Library: shelves along the left wall, an armchair, memory jars
1166
+ add(0.3, 6.8, () => this.drawShelves(0.05, 5.6, 1.5, pal));
1167
+ add(0.3, 9.1, () => this.drawShelves(0.05, 8.2, 1.6, pal));
1168
+ add(2.0, 8.2, () => this.drawArmchair(2.0, 7.95, pal));
1169
+ // Lobby: Mel's desk, benches, the honey fountain, plants
1170
+ add(8, 7.6, () => this.drawReception(8, 7.6, pal));
1171
+ add(8, 9.0, () => this.drawFountain(8.0, 8.95, pal));
1172
+ add(4.6, 9.5, () => this.drawPlant(4.6, 9.5, 0.9, pz));
1173
+ add(11.4, 9.5, () => this.drawPlant(11.4, 9.5, 0.9, pz));
1174
+ add(5.6, 6.15, () => this.drawBench(5.0, 6.0, pal));
1175
+ add(10.45, 5.95, () => this.drawBoardItem('lobby', pal));
1176
+ // Ops: server racks and a little rocket
1177
+ add(15.4, 6.0, () => this.drawRack(15.15, 5.7, pal));
1178
+ add(15.4, 7.1, () => this.drawRack(15.15, 6.8, pal));
1179
+ add(13.1, 6.0, () => this.drawBoardItem('ops', pal));
1180
+ add(12.8, 9.3, () => this.drawRocket(12.8, 9.3, pal));
1181
+ // the entrance gate over the Lobby
1182
+ add(9.2, 10.05, () => this.drawGate(pal));
1183
+ }
1184
+
1185
+ highlightRoom(k) { return (this.highlight?.kind === 'room' && this.highlight.id === k) || (this.dropTarget?.kind === 'room' && this.dropTarget.id === k); }
1186
+
1187
+ drawDesk(room, s, pal) {
1188
+ const ctx = this.ctx;
1189
+ const pz = 0.3;
1190
+ const x = s.x - DW / 2; const y = s.y + 0.18;
1191
+ this.box(x + 0.05, y + 0.06, pz, 0.08, DD - 0.12, DH - 0.06, pal.panel, shade(pal.panel, -0.1), shade(pal.panel, -0.2));
1192
+ this.box(x + DW - 0.13, y + 0.06, pz, 0.08, DD - 0.12, DH - 0.06, pal.panel, shade(pal.panel, -0.1), shade(pal.panel, -0.2));
1193
+ this.box(x + 0.13, y + DD - 0.12, pz + 0.12, DW - 0.26, 0.05, DH - 0.2, pal.panel, shade(pal.panel, -0.18), shade(pal.panel, -0.25));
1194
+ this.box(x, y, pz + DH - 0.06, DW, DD, 0.07, pal.deskTop, pal.deskL, pal.deskR);
1195
+ const z = pz + DH + 0.01;
1196
+ const col = ROOM_COLOR[room];
1197
+ const sitter = [...this.actors.values()].find((a) => !a.path.length && a.place.kind === 'room' && a.place.room === room && Math.abs(a.pos.x - s.x) < 0.2 && Math.abs(a.pos.y - s.y) < 0.2);
1198
+ const active = sitter && ['working', 'thinking', 'reviewing', 'testing'].includes(sitter.mode);
1199
+ // laptop facing the bee (the bee sits behind the desk)
1200
+ const lx = s.x - 0.35; const ly = y + 0.22;
1201
+ this.box(lx, ly, z, 0.7, 0.42, 0.035, '#D8D2C6', '#B9B2A5', '#A39C90');
1202
+ this.box(lx, ly - 0.02, z + 0.035, 0.7, 0.04, 0.42, '#C9C2B6', '#CFC8BC', '#9F988C');
1203
+ const lc = this.P(lx + 0.35, ly + 0.02, z + 0.25);
1204
+ ctx.save();
1205
+ ctx.beginPath(); hexScreen(ctx, lc[0], lc[1], this.cam.A * 0.09);
1206
+ ctx.fillStyle = active ? (sitter.ag.color || col) : shade(col, -0.4);
1207
+ if (active) { ctx.shadowColor = sitter.ag.color; ctx.shadowBlur = 8; }
1208
+ ctx.fill();
1209
+ ctx.restore();
1210
+ // a lamp at the corner
1211
+ const lpx = x + 0.18; const lpy = y + 0.2;
1212
+ this.cyl(lpx, lpy, z, 0.06, 0.03, '#4A3A2B', '#5A4836');
1213
+ const s0 = this.P(lpx, lpy, z + 0.03); const s1 = this.P(lpx, lpy, z + 0.48);
1214
+ ctx.strokeStyle = '#4A3A2B'; ctx.lineWidth = Math.max(1, this.cam.A * 0.03); ctx.beginPath(); ctx.moveTo(s0[0], s0[1]); ctx.lineTo(s1[0], s1[1]); ctx.stroke();
1215
+ const sw = this.cam.A * 0.17;
1216
+ ctx.fillStyle = this.pal.night > 0.3 ? '#FFE3A1' : '#F0B54A';
1217
+ ctx.beginPath(); ctx.moveTo(s1[0] - sw * 0.45, s1[1] - this.cam.A * 0.1); ctx.lineTo(s1[0] + sw * 0.45, s1[1] - this.cam.A * 0.1); ctx.lineTo(s1[0] + sw, s1[1] + this.cam.A * 0.035); ctx.lineTo(s1[0] - sw, s1[1] + this.cam.A * 0.035); ctx.closePath(); ctx.fill();
1218
+ (this.lamps ||= []).push([s1[0], s1[1] + this.cam.A * 0.04]);
1219
+ // a room prop on the desk
1220
+ const px = x + DW - 0.3; const py = y + DD - 0.25;
1221
+ if (room === 'design') ['#FF7AB6', '#6CC4FF', '#FFD072'].forEach((c, i) => { const p = this.P(px - 0.25 + i * 0.1, py, z + 0.01); ctx.fillStyle = c; ctx.beginPath(); ctx.ellipse(p[0], p[1], this.cam.A * 0.04, this.cam.A * 0.022, 0, 0, 6.28); ctx.fill(); });
1222
+ else if (room === 'test') this.cyl(px, py - 0.05, z, 0.05, 0.24, alpha('#7DC49B', 0.85), '#F2E9DA');
1223
+ else if (room === 'review') { const p = this.P(px - 0.1, py, z + 0.08); ctx.strokeStyle = '#4A3A2B'; ctx.lineWidth = Math.max(1, this.cam.A * 0.025); ctx.beginPath(); ctx.arc(p[0], p[1], this.cam.A * 0.07, 0, 6.28); ctx.stroke(); }
1224
+ else this.cyl(px, py, z, 0.08, 0.15, '#F2E9DA', '#3B2414');
1225
+ }
1226
+
1227
+ drawChair(room, s, pal) {
1228
+ const x = s.x - 0.25; const y = s.y - 0.3; const pz = 0.3;
1229
+ const c = ROOM_COLOR[room];
1230
+ this.cyl(x + 0.25, y + 0.22, pz + 0.02, 0.05, 0.36, '#3A2F25', '#3A2F25');
1231
+ this.box(x, y - 0.03, pz + 0.38, 0.5, 0.5, 0.08, shade(c, -0.2), shade(c, -0.4), shade(c, -0.5));
1232
+ this.box(x, y - 0.08, pz + 0.46, 0.5, 0.07, 0.56, shade(c, -0.25), shade(c, -0.35), shade(c, -0.5));
1233
+ void pal;
1234
+ }
1235
+
1236
+ drawEasel(x, y, pal) {
1237
+ const ctx = this.ctx; const pz = 0.3;
1238
+ const a = this.P(x - 0.3, y + 0.15, pz); const b = this.P(x, y, pz + 1.5); const c = this.P(x + 0.3, y - 0.15, pz);
1239
+ ctx.strokeStyle = pal.trim; ctx.lineWidth = Math.max(1.4, this.cam.A * 0.05);
1240
+ ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.lineTo(c[0], c[1]); ctx.stroke();
1241
+ this.poly([this.P(x - 0.45, y + 0.12, pz + 0.55), this.P(x + 0.45, y + 0.12, pz + 0.55), this.P(x + 0.45, y + 0.12, pz + 1.3), this.P(x - 0.45, y + 0.12, pz + 1.3)], '#FFFDF6', 'rgba(36,22,12,.4)');
1242
+ const m = this.P(x, y + 0.12, pz + 0.92);
1243
+ ctx.fillStyle = '#FF7AB6'; ctx.beginPath(); ctx.arc(m[0] - this.cam.A * 0.1, m[1] - this.cam.A * 0.05, this.cam.A * 0.12, 0, 6.28); ctx.fill();
1244
+ ctx.fillStyle = '#6CC4FF'; ctx.fillRect(m[0], m[1] - this.cam.A * 0.02, this.cam.A * 0.2, this.cam.A * 0.14);
1245
+ }
1246
+
1247
+ drawScreen(u0, u1, pal) {
1248
+ // a big screen on the Review wall with a diff that scrolls
1249
+ const ctx = this.ctx;
1250
+ this.wallRT(0.02, WALL + 0.3);
1251
+ const t0 = 0.35; const t1 = 1.25;
1252
+ ctx.fillStyle = '#2A2420'; ctx.fillRect(u0 + 0.85, t0 - 0.05, u1 - u0 - 1.65, t1 - t0 + 0.1);
1253
+ ctx.fillStyle = '#171B26'; ctx.fillRect(u0 + 0.9, t0, u1 - u0 - 1.75, t1 - t0);
1254
+ const off = this.reduced() ? 0 : Math.floor(this.now * 1.5);
1255
+ for (let i = 0; i < 8; i++) {
1256
+ const k = (i + off) % 11;
1257
+ const add = k % 4 === 1; const del = k % 5 === 3;
1258
+ ctx.fillStyle = add ? 'rgba(93,178,126,.85)' : del ? 'rgba(229,123,110,.85)' : 'rgba(200,190,170,.4)';
1259
+ ctx.fillRect(u0 + 1.0, t0 + 0.07 + i * 0.1, 0.25 + ((k * 7) % 5) * 0.12, 0.05);
1260
+ }
1261
+ this.base();
1262
+ void pal;
1263
+ }
1264
+
1265
+ drawShelves(x, y, len, pal) {
1266
+ const ctx = this.ctx; const pz = 0.3;
1267
+ this.box(x, y, pz, 0.45, len, 1.6, pal.deskTop, pal.panel, shade(pal.panel, -0.2));
1268
+ const colors = ['#E57B6E', '#86A9D6', '#FFD072', '#7DC49B', '#C9A0E8', '#F2E9DA'];
1269
+ for (let row = 0; row < 3; row++) {
1270
+ const zz = pz + 0.12 + row * 0.5;
1271
+ for (let i = 0; i < Math.floor(len / 0.22) - 1; i++) {
1272
+ const yy = y + 0.1 + i * 0.22;
1273
+ const glow = (i + row) % 4 === 1;
1274
+ if (glow) {
1275
+ // a glowing memory jar
1276
+ const p = this.P(x + 0.45, yy + 0.08, zz + 0.14);
1277
+ ctx.fillStyle = '#FFB531'; ctx.globalAlpha = 0.9;
1278
+ roundRect(ctx, p[0] - this.cam.A * 0.07, p[1] - this.cam.A * 0.12, this.cam.A * 0.14, this.cam.A * 0.2, this.cam.A * 0.04); ctx.fill();
1279
+ ctx.globalAlpha = 1;
1280
+ (this.jarLights ||= []).push(p);
1281
+ } else this.poly([this.P(x + 0.45, yy, zz), this.P(x + 0.45, yy + 0.16, zz), this.P(x + 0.45, yy + 0.16, zz + 0.3 + ((i + row) % 3) * 0.05), this.P(x + 0.45, yy, zz + 0.3 + ((i + row) % 3) * 0.05)], colors[(i * 2 + row) % colors.length], 'rgba(36,22,12,.3)');
1282
+ }
1283
+ }
1284
+ }
1285
+
1286
+ drawArmchair(x, y, pal) {
1287
+ const pz = 0.3; const c = '#B5654A';
1288
+ this.box(x - 0.45, y - 0.35, pz, 0.9, 0.75, 0.32, shade(c, 0.1), shade(c, -0.15), shade(c, -0.3));
1289
+ this.box(x - 0.45, y - 0.42, pz + 0.32, 0.9, 0.16, 0.55, shade(c, 0.05), shade(c, -0.2), shade(c, -0.35));
1290
+ this.box(x - 0.55, y - 0.35, pz + 0.2, 0.12, 0.75, 0.35, shade(c, 0), shade(c, -0.2), shade(c, -0.35));
1291
+ this.box(x + 0.45, y - 0.35, pz + 0.2, 0.12, 0.75, 0.35, shade(c, 0), shade(c, -0.2), shade(c, -0.35));
1292
+ void pal;
1293
+ }
1294
+
1295
+ drawReception(x, y, pal) {
1296
+ // a hexagonal desk for Mel
1297
+ const ctx = this.ctx; const pz = 0.3; const r = 0.7; const hgt = 0.62;
1298
+ const ring = [];
1299
+ for (let i = 0; i < 6; i++) { const an = (Math.PI / 3) * i + Math.PI / 6; ring.push([x + r * Math.cos(an), y + r * Math.sin(an)]); }
1300
+ for (let i = 0; i < 6; i++) {
1301
+ const p = ring[i]; const q = ring[(i + 1) % 6];
1302
+ const nx = (p[0] + q[0]) / 2 - x; const ny = (p[1] + q[1]) / 2 - y;
1303
+ if (nx + ny <= 0.01) continue;
1304
+ this.poly([this.P(p[0], p[1], pz), this.P(q[0], q[1], pz), this.P(q[0], q[1], pz + hgt), this.P(p[0], p[1], pz + hgt)], nx > ny ? pal.deskR : pal.deskL, 'rgba(36,22,12,.35)');
1305
+ }
1306
+ this.poly(ring.map((p) => this.P(p[0], p[1], pz + hgt)), pal.deskTop, 'rgba(36,22,12,.35)');
1307
+ const c = this.P(x, y, pz + hgt);
1308
+ ctx.fillStyle = '#E8951E'; ctx.beginPath(); ctx.ellipse(c[0], c[1], this.cam.A * 0.32, this.cam.A * 0.16, 0, 0, 6.28); ctx.fill();
1309
+ ctx.fillStyle = '#FFD072'; ctx.beginPath(); hexScreen(ctx, c[0], c[1] - this.cam.A * 0.02, this.cam.A * 0.1); ctx.fill();
1310
+ }
1311
+
1312
+ drawFountain(x, y, pal) {
1313
+ const ctx = this.ctx; const pz = 0.3;
1314
+ this.cyl(x, y, pz, 0.62, 0.26, pal.plinthL, pal.plinthTop);
1315
+ const t = this.P(x, y, pz + 0.26);
1316
+ ctx.fillStyle = '#F3A42A'; ctx.beginPath(); ctx.ellipse(t[0], t[1], this.cam.A * 0.72, this.cam.A * 0.36, 0, 0, 6.28); ctx.fill();
1317
+ ctx.fillStyle = 'rgba(255,240,190,.5)';
1318
+ const w = this.reduced() ? 0 : this.now;
1319
+ for (let i = 0; i < 3; i++) { const ph = ((w * 0.6 + i / 3) % 1); ctx.globalAlpha = 1 - ph; ctx.strokeStyle = '#FFE7A8'; ctx.lineWidth = 1; ctx.beginPath(); ctx.ellipse(t[0], t[1], this.cam.A * 0.7 * ph, this.cam.A * 0.35 * ph, 0, 0, 6.28); ctx.stroke(); }
1320
+ ctx.globalAlpha = 1;
1321
+ this.cyl(x, y, pz + 0.26, 0.1, 0.45, pal.plinthL, pal.plinthTop);
1322
+ const s = this.P(x, y, pz + 0.75);
1323
+ if (!this.reduced()) for (let i = 0; i < 2; i++) { const ph = ((w * 1.4 + i * 0.5) % 1); ctx.fillStyle = `rgba(255,200,90,${1 - ph})`; ctx.beginPath(); ctx.arc(s[0], s[1] - ph * this.cam.A * 0.3, this.cam.A * 0.05, 0, 6.28); ctx.fill(); }
1324
+ }
1325
+
1326
+ drawBench(x, y, pal) {
1327
+ const pz = 0.3;
1328
+ this.box(x, y, pz + 0.3, 1.3, 0.42, 0.08, pal.deskTop, pal.deskL, pal.deskR);
1329
+ this.box(x + 0.08, y + 0.08, pz, 0.08, 0.3, 0.3, pal.panel, pal.panel, shade(pal.panel, -0.2));
1330
+ this.box(x + 1.14, y + 0.08, pz, 0.08, 0.3, 0.3, pal.panel, pal.panel, shade(pal.panel, -0.2));
1331
+ }
1332
+
1333
+ drawRack(x, y, pal) {
1334
+ const ctx = this.ctx; const pz = 0.3;
1335
+ this.box(x, y, pz, 0.62, 0.85, 1.55, '#3A332D', '#2A2420', '#221D19');
1336
+ for (let i = 0; i < 6; i++) {
1337
+ for (let j = 0; j < 3; j++) {
1338
+ const on = this.reduced() ? (i + j) % 2 : (Math.sin(this.now * (3 + j) + i * 1.7 + x) > 0.2);
1339
+ const p = this.P(x + 0.62, y + 0.18 + j * 0.24, pz + 0.25 + i * 0.22);
1340
+ ctx.fillStyle = on ? ['#7DC49B', '#FFB531', '#6CC4FF'][j] : '#3C3530';
1341
+ ctx.fillRect(p[0] - 1, p[1] - 1, Math.max(2, this.cam.A * 0.05), Math.max(1.5, this.cam.A * 0.03));
1342
+ }
1343
+ }
1344
+ void pal;
1345
+ }
1346
+
1347
+ drawRocket(x, y, pal) {
1348
+ const ctx = this.ctx; const pz = 0.3;
1349
+ this.cyl(x, y, pz, 0.32, 0.08, '#4A3A2B', '#5A4836');
1350
+ const b = this.P(x, y, pz + 0.1); const t = this.P(x, y, pz + 1.1);
1351
+ const w = this.cam.A * 0.2;
1352
+ ctx.fillStyle = '#F2E9DA'; ctx.beginPath(); ctx.moveTo(b[0] - w, b[1]); ctx.lineTo(b[0] - w, t[1] + w); ctx.quadraticCurveTo(t[0], t[1] - w * 0.8, b[0] + w, t[1] + w); ctx.lineTo(b[0] + w, b[1]); ctx.closePath(); ctx.fill();
1353
+ ctx.strokeStyle = 'rgba(36,22,12,.5)'; ctx.lineWidth = 1; ctx.stroke();
1354
+ ctx.fillStyle = '#F08A5D'; ctx.beginPath(); ctx.moveTo(b[0] - w, b[1]); ctx.lineTo(b[0] - w * 1.7, b[1] + w * 0.3); ctx.lineTo(b[0] - w, b[1] - w * 1.2); ctx.fill();
1355
+ ctx.beginPath(); ctx.moveTo(b[0] + w, b[1]); ctx.lineTo(b[0] + w * 1.7, b[1] + w * 0.3); ctx.lineTo(b[0] + w, b[1] - w * 1.2); ctx.fill();
1356
+ ctx.fillStyle = '#6CC4FF'; ctx.beginPath(); ctx.arc(b[0], (b[1] + t[1]) / 2, w * 0.45, 0, 6.28); ctx.fill();
1357
+ void pal;
1358
+ }
1359
+
1360
+ drawGate(pal) {
1361
+ const ctx = this.ctx; const pz = 0.3; const y = 10.05;
1362
+ // see through the gate when the Lobby is zoomed in
1363
+ const fade = this.focus?.kind === 'room' && ROOMS[this.focus.id]?.row === 'front';
1364
+ if (fade) { ctx.save(); ctx.globalAlpha = 0.28; }
1365
+ this.drawGateBody(pal, pz, y);
1366
+ if (fade) ctx.restore();
1367
+ }
1368
+
1369
+ drawGateBody(pal, pz, y) {
1370
+ const ctx = this.ctx;
1371
+ this.box(6.75, y - 0.12, pz, 0.26, 0.26, 2.0, pal.trim, shade(pal.trim, -0.1), shade(pal.trim, -0.25));
1372
+ this.box(9.0, y - 0.12, pz, 0.26, 0.26, 2.0, pal.trim, shade(pal.trim, -0.1), shade(pal.trim, -0.25));
1373
+ this.box(6.55, y - 0.15, pz + 2.0, 2.9, 0.3, 0.42, shade(pal.trim, 0.2), shade(pal.trim, 0.05), shade(pal.trim, -0.2));
1374
+ if (this.cam.A < 12) return;
1375
+ this.wallRT(y + 0.16, pz + 2.42, 100);
1376
+ ctx.fillStyle = '#FFD072'; ctx.font = `800 26px ${FONT}`;
1377
+ ctx.textAlign = 'center'; ctx.fillText('hivedesk', 8.0 * 100, 0.31 * 100); ctx.textAlign = 'left';
1378
+ this.base();
1379
+ }
1380
+
1381
+ drawTank(x, y, pal) {
1382
+ // a honey water tower that gives the building its silhouette, with your GPU core on top
1383
+ const ctx = this.ctx;
1384
+ for (const [dx, dy] of [[-0.6, -0.6], [0.6, -0.6], [-0.6, 0.6], [0.6, 0.6]]) {
1385
+ const a = this.P(x + dx, y + dy, 0); const b = this.P(x + dx * 0.6, y + dy * 0.6, TANK.z);
1386
+ ctx.strokeStyle = pal.rail; ctx.lineWidth = Math.max(1.5, this.cam.A * 0.07); ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.stroke();
1387
+ }
1388
+ this.cyl(x, y, TANK.z, 0.95, 1.4, '#E79A2A', '#C9862A');
1389
+ const p = this.P(x, y, TANK.z + 0.75);
1390
+ ctx.fillStyle = 'rgba(255,240,200,.45)'; ctx.fillRect(p[0] - this.cam.A * 0.95, p[1] - this.cam.A * 0.35, this.cam.A * 0.22, this.cam.A * 0.95);
1391
+ ctx.fillStyle = '#FFF1CF'; ctx.beginPath(); hexScreen(ctx, p[0] + this.cam.A * 0.2, p[1], this.cam.A * 0.3); ctx.fill();
1392
+ const c = this.getCity();
1393
+ const self = c.machines?.[c.selfId] || {};
1394
+ const hostGlow = this.hostGlow || (this.hostGlow = { glow: 0 });
1395
+ this.drawCore(hostGlow, x, y, TANK.z + 1.45, self, '#FFB531', false);
1396
+ }
1397
+
1398
+ drawPlant(x, y, s, z = 0) {
1399
+ const ctx = this.ctx;
1400
+ this.cyl(x, y, z, 0.2 * s, 0.32 * s, '#C46A3A', '#4A2E1A');
1401
+ const c = this.P(x, y, z + 0.32 * s);
1402
+ const t = this.reduced() ? 0 : Math.sin(this.now * 0.8 + x) * 0.04;
1403
+ for (let i = 0; i < 7; i++) {
1404
+ const an = -Math.PI / 2 + (i - 3) * 0.42 + t;
1405
+ const len = this.cam.A * (0.42 + (i % 3) * 0.1) * s;
1406
+ ctx.fillStyle = i % 2 ? '#4E8F5E' : '#63A874';
1407
+ ctx.beginPath(); ctx.ellipse(c[0] + Math.cos(an) * len * 0.5, c[1] + Math.sin(an) * len * 0.5, len * 0.5, len * 0.16, an, 0, 6.28); ctx.fill();
1408
+ }
1409
+ }
1410
+
1411
+ drawTree(x, y, s, pal, z = 0) {
1412
+ const ctx = this.ctx;
1413
+ const b = this.P(x, y, z); const t = this.P(x, y, z + 1.1 * s);
1414
+ ctx.fillStyle = 'rgba(20,30,10,.25)'; ctx.beginPath(); ctx.ellipse(b[0] + this.cam.A * 0.3, b[1], this.cam.A * 0.9 * s, this.cam.A * 0.42 * s, 0, 0, 6.28); ctx.fill();
1415
+ ctx.strokeStyle = pal.trunk; ctx.lineWidth = Math.max(2, this.cam.A * 0.14 * s); ctx.lineCap = 'round';
1416
+ ctx.beginPath(); ctx.moveTo(b[0], b[1]); ctx.lineTo(t[0], t[1]); ctx.stroke(); ctx.lineCap = 'butt';
1417
+ const sway = this.reduced() ? 0 : Math.sin(this.now * 0.7 + x * 0.3) * this.cam.A * 0.04;
1418
+ const r = this.cam.A * 0.78 * s;
1419
+ const cx = t[0] + sway; const cy = t[1] - r * 0.55;
1420
+ ctx.fillStyle = pal.treeB; ctx.beginPath(); ctx.arc(cx + r * 0.2, cy + r * 0.25, r * 0.85, 0, 6.28); ctx.fill();
1421
+ ctx.fillStyle = pal.tree; ctx.beginPath(); ctx.arc(cx - r * 0.12, cy, r * 0.8, 0, 6.28); ctx.fill();
1422
+ ctx.fillStyle = pal.treeC; ctx.beginPath(); ctx.arc(cx - r * 0.35, cy - r * 0.3, r * 0.4, 0, 6.28); ctx.fill();
1423
+ ctx.strokeStyle = 'rgba(30,40,10,.35)'; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(cx - r * 0.12, cy, r * 0.8, 0, 6.28); ctx.stroke();
1424
+ }
1425
+
1426
+ drawLamp(x, y, pal) {
1427
+ const ctx = this.ctx;
1428
+ const b = this.P(x, y, 0); const t = this.P(x, y, 1.9);
1429
+ ctx.strokeStyle = '#3A2F25'; ctx.lineWidth = Math.max(1.5, this.cam.A * 0.07);
1430
+ ctx.beginPath(); ctx.moveTo(b[0], b[1]); ctx.lineTo(t[0], t[1]); ctx.stroke();
1431
+ const on = this.pal.night > 0.25;
1432
+ ctx.fillStyle = on ? '#FFE3A1' : '#C9B48A';
1433
+ ctx.beginPath(); hexScreen(ctx, t[0], t[1] - this.cam.A * 0.12, this.cam.A * 0.16); ctx.fill();
1434
+ ctx.strokeStyle = 'rgba(36,22,12,.6)'; ctx.lineWidth = 1; ctx.stroke();
1435
+ if (on) (this.lamps ||= []).push([t[0], t[1] - this.cam.A * 0.12, 2.4]);
1436
+ void pal;
1437
+ }
1438
+
1439
+ /** Trees, lamps and flowers around your building, kept clear of the bridges. */
1440
+ homeDecor(pal) {
1441
+ const out = [];
1442
+ const spots = [[-3.4, 2.6, 1.1], [-2.8, 7.4, 1.0], [-0.8, 12.4, 0.9], [3.2, 14.2, 1.0], [12.8, 14.6, 1.05], [17.6, 11.6, 0.9], [19.4, 6.4, 1.15], [18.4, 1.0, 1.0], [14.6, -3.4, 0.95], [5.0, -3.6, 1.0], [9.8, -4.6, 0.85], [-1.4, -3.0, 0.8], [1.6, 11.6, 0.7], [15.2, 13.0, 0.75]];
1443
+ const clear = (x, y) => {
1444
+ for (const m of this.machines.values()) {
1445
+ const br = m.bridge;
1446
+ const vx = br.b.x - br.a.x; const vy = br.b.y - br.a.y; const L = vx * vx + vy * vy;
1447
+ // distance to the line from the plaza to the bridge start, and to the bridge
1448
+ for (const [ax, ay, bx, by] of [[PLAZA.x, PLAZA.y, br.a.x, br.a.y], [br.a.x, br.a.y, br.b.x, br.b.y]]) {
1449
+ const wx = bx - ax; const wy = by - ay; const LL = wx * wx + wy * wy || 1;
1450
+ const u = clamp(((x - ax) * wx + (y - ay) * wy) / LL);
1451
+ if (Math.hypot(ax + wx * u - x, ay + wy * u - y) < 2.4) return false;
1452
+ }
1453
+ void L;
1454
+ }
1455
+ return true;
1456
+ };
1457
+ for (const [x, y, s] of spots) if (clear(x, y)) out.push({ k: x + y, f: () => this.drawTree(x, y, s, pal) });
1458
+ for (const [x, y] of [[5.4, 12.0], [10.6, 12.0], [5.8, 15.0], [10.2, 14.8]]) if (clear(x, y) || y < 12.5) out.push({ k: x + y, f: () => this.drawLamp(x, y, pal) });
1459
+ // flower beds by the steps
1460
+ for (const [x, y] of [[5.9, 10.7], [10.1, 10.7]]) out.push({ k: x + y, f: () => this.drawFlowers(x, y) });
1461
+ return out;
1462
+ }
1463
+
1464
+ drawFlowers(x, y) {
1465
+ const ctx = this.ctx;
1466
+ const colors = ['#FF7AB6', '#FFD072', '#FFFFFF', '#C9A0E8'];
1467
+ for (let i = 0; i < 9; i++) {
1468
+ const p = this.P(x + ((i * 37) % 10) / 10 - 0.5, y + ((i * 53) % 10) / 22 - 0.2, 0.05);
1469
+ ctx.fillStyle = '#4E8F5E'; ctx.fillRect(p[0] - 0.5, p[1] - this.cam.A * 0.12, 1, this.cam.A * 0.12);
1470
+ ctx.fillStyle = colors[i % 4]; ctx.beginPath(); ctx.arc(p[0], p[1] - this.cam.A * 0.14, Math.max(1.4, this.cam.A * 0.06), 0, 6.28); ctx.fill();
1471
+ }
1472
+ }
1473
+
1474
+ friendDecor(m) {
1475
+ const out = [];
1476
+ const c = m.center;
1477
+ const pal = this.pal;
1478
+ const born = this.now - m.born;
1479
+ const show = born > 3.6 || born < -50;
1480
+ if (!show) return out;
1481
+ for (const [dx, dy, s] of [[-5.2, 1.2, 0.9], [1.6, -5.4, 0.85], [-3.2, -4.0, 0.8], [5.2, -1.4, 0.95]]) {
1482
+ const x = c.x + dx; const y = c.y + dy;
1483
+ const br = m.bridge;
1484
+ const vx = br.b.x - c.x; const vy = br.b.y - c.y; const L = Math.hypot(vx, vy) || 1;
1485
+ const u = clamp(((x - c.x) * vx + (y - c.y) * vy) / (L * L));
1486
+ if (Math.hypot(c.x + vx * u - x, c.y + vy * u - y) < 2.2) continue;
1487
+ out.push({ k: x + y, f: () => this.drawTree(x, y, s, pal) });
1488
+ }
1489
+ out.push({ k: c.x + c.y + 6.6, f: () => this.drawLamp(c.x + 3.0, c.y + 4.6, pal) });
1490
+ return out;
1491
+ }
1492
+
1493
+ /* ---------- friend towers ---------- */
1494
+ drawTower(m, pal, city) {
1495
+ const ctx = this.ctx;
1496
+ const tw = towerOf(m.center);
1497
+ const H = this.towerHeight(m);
1498
+ const floors = Math.round((H - 0.4) / FLOOR_H);
1499
+ const born = this.now - m.born;
1500
+ const mm = m.m || {};
1501
+ const col = mm.color || '#7DC49B';
1502
+ const offline = mm.online === false;
1503
+ const build = born < -50 ? 1 : clamp((born - 1.0) / 2.6);
1504
+ const w = tw.x1 - tw.x0;
1505
+ // foundation
1506
+ this.box(tw.x0 - 0.3, tw.y0 - 0.3, 0, w + 0.6, w + 0.6, 0.3, pal.plinthTop, pal.plinthL, pal.plinthR);
1507
+ // floors stack up one by one while it is being built
1508
+ let topZ = 0.3;
1509
+ for (let i = 0; i < floors; i++) {
1510
+ const fs = clamp(build * floors - i);
1511
+ if (fs <= 0) break;
1512
+ const sc = this.reduced() ? 1 : easeOutBack(fs);
1513
+ const z0 = 0.3 + i * FLOOR_H;
1514
+ const hgt = FLOOR_H * Math.min(1, sc);
1515
+ const inset = i === floors - 1 ? 0.2 : 0;
1516
+ const wallL = offline ? '#9A938A' : mixHex(pal.towerL, col, 0.12);
1517
+ const wallR = offline ? '#837C74' : mixHex(pal.towerR, col, 0.12);
1518
+ this.box(tw.x0 + inset, tw.y0 + inset, z0, w - inset * 2, w - inset * 2, hgt, pal.tower, wallL, wallR);
1519
+ // a colored band per floor
1520
+ this.poly([this.P(tw.x0 + inset, tw.y1 - inset, z0 + hgt - 0.12), this.P(tw.x1 - inset, tw.y1 - inset, z0 + hgt - 0.12), this.P(tw.x1 - inset, tw.y1 - inset, z0 + hgt), this.P(tw.x0 + inset, tw.y1 - inset, z0 + hgt)], shade(col, -0.1));
1521
+ this.poly([this.P(tw.x1 - inset, tw.y0 + inset, z0 + hgt - 0.12), this.P(tw.x1 - inset, tw.y1 - inset, z0 + hgt - 0.12), this.P(tw.x1 - inset, tw.y1 - inset, z0 + hgt), this.P(tw.x1 - inset, tw.y0 + inset, z0 + hgt)], shade(col, -0.3));
1522
+ // windows with frames and a glint; warm light at night or when the GPU works hard
1523
+ if (fs >= 1) {
1524
+ const lit = !offline && (pal.night > 0.3 || (mm.load || 0) > 0.5);
1525
+ const glass = mixHex(mixHex('#7FA8C9', '#2B3A5C', pal.night * 0.7), DUSK_TINT, pal.gold * 0.25);
1526
+ const ww = (w - inset * 2 - 0.9) / 3;
1527
+ for (let j = 0; j < 3; j++) {
1528
+ for (const side of ['L', 'R']) {
1529
+ if (i === 0 && side === 'L' && j === 1) continue; // the door
1530
+ const on = lit && ((i * 3 + j + (side === 'L' ? 1 : 0)) % 3 !== 0);
1531
+ const zz = z0 + 0.34; const wh = 0.58;
1532
+ const u0 = (side === 'L' ? tw.x0 : tw.y0) + inset + 0.45 + j * ww + ww * 0.12; const u1 = u0 + ww * 0.76;
1533
+ const pt = (u, z) => (side === 'L' ? this.P(u, tw.y1 - inset, z) : this.P(tw.x1 - inset, u, z));
1534
+ this.poly([pt(u0 - 0.06, zz - 0.06), pt(u1 + 0.06, zz - 0.06), pt(u1 + 0.06, zz + wh + 0.06), pt(u0 - 0.06, zz + wh + 0.06)], shade(col, -0.25));
1535
+ this.poly([pt(u0, zz), pt(u1, zz), pt(u1, zz + wh), pt(u0, zz + wh)], on ? '#FFD98A' : glass);
1536
+ if (!on) this.poly([pt(u0 + (u1 - u0) * 0.15, zz + wh * 0.25), pt(u0 + (u1 - u0) * 0.35, zz + wh * 0.25), pt(u0 + (u1 - u0) * 0.6, zz + wh), pt(u0 + (u1 - u0) * 0.4, zz + wh)], 'rgba(255,255,255,.22)');
1537
+ const mid = (u0 + u1) / 2;
1538
+ const a = pt(mid, zz); const b = pt(mid, zz + wh);
1539
+ this.ctx.strokeStyle = shade(col, -0.25); this.ctx.lineWidth = Math.max(1, this.cam.A * 0.03);
1540
+ this.ctx.beginPath(); this.ctx.moveTo(a[0], a[1]); this.ctx.lineTo(b[0], b[1]); this.ctx.stroke();
1541
+ this.poly([pt(u0 - 0.1, zz - 0.06), pt(u1 + 0.1, zz - 0.06), pt(u1 + 0.1, zz - 0.13), pt(u0 - 0.1, zz - 0.13)], pal.tower);
1542
+ if (on) (this.windowLights ||= []).push(pt(mid, zz + wh / 2));
1543
+ }
1544
+ }
1545
+ }
1546
+ topZ = z0 + hgt;
1547
+ }
1548
+ // roof rim, a little garden and an antenna once it is built
1549
+ if (build >= 1 || born < -50) {
1550
+ const r0 = tw.x0 + 0.2; const r1 = tw.x1 - 0.2; const s0 = tw.y0 + 0.2; const s1 = tw.y1 - 0.2; const t = 0.14;
1551
+ this.box(r0, s0, topZ, r1 - r0, t, 0.22, pal.tower, pal.towerL, pal.towerR);
1552
+ this.box(r0, s0, topZ, t, s1 - s0, 0.22, pal.tower, pal.towerL, pal.towerR);
1553
+ this.box(r0, s1 - t, topZ, r1 - r0, t, 0.22, shade(pal.tower, 0.05), mixHex(pal.towerL, col, 0.25), pal.towerR);
1554
+ this.box(r1 - t, s0, topZ, t, s1 - s0, 0.22, shade(pal.tower, 0.05), pal.towerL, mixHex(pal.towerR, col, 0.25));
1555
+ this.drawPlant(r0 + 0.55, s1 - 0.55, 0.55, topZ);
1556
+ const an0 = this.P(r0 + 0.5, s0 + 0.5, topZ); const an1 = this.P(r0 + 0.5, s0 + 0.5, topZ + 1.5);
1557
+ this.ctx.strokeStyle = '#5A4836'; this.ctx.lineWidth = Math.max(1, this.cam.A * 0.04);
1558
+ this.ctx.beginPath(); this.ctx.moveTo(an0[0], an0[1]); this.ctx.lineTo(an1[0], an1[1]); this.ctx.stroke();
1559
+ const blink = this.reduced() || Math.sin(this.now * 3 + m.slot) > 0;
1560
+ this.ctx.fillStyle = blink ? '#FF6B5A' : '#7A3A30'; this.ctx.beginPath(); this.ctx.arc(an1[0], an1[1], Math.max(2, this.cam.A * 0.07), 0, 6.28); this.ctx.fill();
1561
+ }
1562
+ // the door with an awning in the owner's color
1563
+ if (build > 0.2) {
1564
+ const d0 = tw.x0 + (w - 1) / 2; const d1 = d0 + 1;
1565
+ this.poly([this.P(d0 - 0.08, tw.y1, 0.3), this.P(d1 + 0.08, tw.y1, 0.3), this.P(d1 + 0.08, tw.y1, 1.36), this.P(d0 - 0.08, tw.y1, 1.36)], shade(col, -0.3));
1566
+ this.poly([this.P(d0, tw.y1, 0.3), this.P(d1, tw.y1, 0.3), this.P(d1, tw.y1, 1.28), this.P(d0, tw.y1, 1.28)], '#8A5A2E', 'rgba(36,22,12,.5)');
1567
+ this.poly([this.P(d0 - 0.25, tw.y1, 1.5), this.P(d1 + 0.25, tw.y1, 1.5), this.P(d1 + 0.25, tw.y1 + 0.55, 1.28), this.P(d0 - 0.25, tw.y1 + 0.55, 1.28)], col, 'rgba(36,22,12,.4)');
1568
+ }
1569
+ // scaffolding and a crane while it is being built
1570
+ if (build < 1 && !this.reduced()) {
1571
+ ctx.strokeStyle = 'rgba(122,86,54,.85)'; ctx.lineWidth = Math.max(1, this.cam.A * 0.04);
1572
+ const top = 0.3 + floors * FLOOR_H;
1573
+ for (const [x, y] of [[tw.x0 - 0.2, tw.y1 + 0.2], [tw.x1 + 0.2, tw.y1 + 0.2], [tw.x1 + 0.2, tw.y0 - 0.2]]) {
1574
+ const a = this.P(x, y, 0.3); const b = this.P(x, y, top);
1575
+ ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.stroke();
1576
+ }
1577
+ for (let z = 0.3; z <= top; z += FLOOR_H / 2) {
1578
+ const p = [this.P(tw.x0 - 0.2, tw.y1 + 0.2, z), this.P(tw.x1 + 0.2, tw.y1 + 0.2, z), this.P(tw.x1 + 0.2, tw.y0 - 0.2, z)];
1579
+ ctx.beginPath(); ctx.moveTo(p[0][0], p[0][1]); ctx.lineTo(p[1][0], p[1][1]); ctx.lineTo(p[2][0], p[2][1]); ctx.stroke();
1580
+ }
1581
+ // the crane
1582
+ const cz = top + 1.4;
1583
+ const mast0 = this.P(tw.x0 + 0.4, tw.y0 + 0.4, topZ); const mast1 = this.P(tw.x0 + 0.4, tw.y0 + 0.4, cz);
1584
+ ctx.strokeStyle = '#E8A93A'; ctx.lineWidth = Math.max(1.5, this.cam.A * 0.07);
1585
+ ctx.beginPath(); ctx.moveTo(mast0[0], mast0[1]); ctx.lineTo(mast1[0], mast1[1]); ctx.stroke();
1586
+ const swing = Math.sin(this.now * 1.6) * 0.6;
1587
+ const jib = this.P(tw.x0 + 0.4 + Math.cos(swing) * 4, tw.y0 + 0.4 + Math.sin(swing) * 4, cz);
1588
+ ctx.beginPath(); ctx.moveTo(mast1[0], mast1[1]); ctx.lineTo(jib[0], jib[1]); ctx.stroke();
1589
+ ctx.strokeStyle = 'rgba(36,22,12,.7)'; ctx.lineWidth = 1;
1590
+ const hook = [jib[0], jib[1] + this.cam.A * 1.4];
1591
+ ctx.beginPath(); ctx.moveTo(jib[0], jib[1]); ctx.lineTo(hook[0], hook[1]); ctx.stroke();
1592
+ ctx.fillStyle = '#FFD072'; ctx.fillRect(hook[0] - this.cam.A * 0.2, hook[1], this.cam.A * 0.4, this.cam.A * 0.25);
1593
+ if (Math.random() < 0.25) { const p = this.P(rnd(tw.x0, tw.x1), tw.y1 + 0.2, topZ); this.spawn({ kind: 'puff', x: p[0], y: p[1], vx: rnd(-14, 14), vy: rnd(-20, -6), life: rnd(0.6, 1), size: rnd(4, 9) * this.cam.A / 30 }); }
1594
+ }
1595
+ // the GPU core on the roof: a glowing hexagon crystal that spins with the load
1596
+ const coreOn = build >= 1 || born < -50;
1597
+ if (coreOn) {
1598
+ this.box((tw.x0 + tw.x1) / 2 - 0.7, (tw.y0 + tw.y1) / 2 - 0.7, topZ, 1.4, 1.4, 0.25, '#4A3A2B', '#3A2F25', '#2A2420');
1599
+ this.drawCore(m, (tw.x0 + tw.x1) / 2, (tw.y0 + tw.y1) / 2, topZ + 0.05, mm, col, offline);
1600
+ if (born > 3.5 && born < 3.9 && !m.flashed && !this.reduced()) { m.flashed = true; const [cx, cy] = m.coreAt; for (let i = 0; i < 18; i++) this.spawn({ kind: 'spark', x: cx + rnd(-30, 30), y: cy + rnd(-30, 20), vx: rnd(-50, 50), vy: rnd(-70, -10), g: 40, life: rnd(0.6, 1.2), size: rnd(2, 4.5), color: pick(['#7FE3FF', '#FFFFFF', col]) }); this.sound('level'); }
1601
+ }
1602
+ // hit box for the machine panel
1603
+ const pts = [this.P(tw.x0 - 0.3, tw.y1 + 0.3, 0), this.P(tw.x1 + 0.3, tw.y1 + 0.3, 0), this.P(tw.x1 + 0.3, tw.y0 - 0.3, 0), this.P(tw.x0, tw.y0, H + 1.6)];
1604
+ const xs = pts.map((p) => p[0]); const ys = pts.map((p) => p[1]);
1605
+ this.machineHits.push({ id: m.id, x: Math.min(...xs), y: Math.min(...ys), w: Math.max(...xs) - Math.min(...xs), h: Math.max(...ys) - Math.min(...ys) });
1606
+ void city;
1607
+ }
1608
+
1609
+
1610
+ /** A glowing hexagon crystal: the GPU of a machine. It spins faster with the load and flares with traffic. */
1611
+ drawCore(holder, x, y, z, mm, col, offline) {
1612
+ const ctx = this.ctx;
1613
+ const c = this.P(x, y, z);
1614
+ const load = clamp(mm.load || 0);
1615
+ const lit = offline ? 0 : 0.35 + load * 0.65 + clamp(holder.glow || 0) * 0.4;
1616
+ const spin = this.reduced() ? 0 : this.now * (0.6 + load * 3.2);
1617
+ const R = this.cam.A * 0.62;
1618
+ const cy = c[1] - this.cam.A * 0.9 - (this.reduced() ? 0 : Math.sin(this.now * 2) * this.cam.A * 0.06);
1619
+ const g = ctx.createRadialGradient(c[0], cy, 2, c[0], cy, R * 3.2);
1620
+ g.addColorStop(0, `rgba(127,227,255,${0.5 * lit})`); g.addColorStop(1, 'rgba(127,227,255,0)');
1621
+ ctx.fillStyle = g; ctx.fillRect(c[0] - R * 3.2, cy - R * 3.2, R * 6.4, R * 6.4);
1622
+ ctx.save(); ctx.translate(c[0], cy);
1623
+ ctx.scale(0.55 + 0.45 * Math.abs(Math.cos(spin)), 1);
1624
+ ctx.beginPath(); hexScreen(ctx, 0, 0, R);
1625
+ const cg = ctx.createLinearGradient(0, -R, 0, R);
1626
+ cg.addColorStop(0, offline ? '#8A8A8A' : '#CFF6FF'); cg.addColorStop(1, offline ? '#555555' : mixHex('#4FB6E0', col, 0.25));
1627
+ ctx.fillStyle = cg; ctx.fill();
1628
+ ctx.strokeStyle = 'rgba(20,40,60,.7)'; ctx.lineWidth = 1.2; ctx.stroke();
1629
+ ctx.fillStyle = 'rgba(255,255,255,.55)'; ctx.beginPath(); hexScreen(ctx, -R * 0.2, -R * 0.25, R * 0.3); ctx.fill();
1630
+ ctx.restore();
1631
+ ctx.strokeStyle = `rgba(127,227,255,${0.35 + lit * 0.5})`; ctx.lineWidth = Math.max(1.5, this.cam.A * 0.06);
1632
+ ctx.beginPath(); ctx.ellipse(c[0], c[1] - this.cam.A * 0.05, R * 1.5, R * 0.75, 0, -Math.PI / 2, -Math.PI / 2 + Math.PI * 2 * Math.max(0.02, load)); ctx.stroke();
1633
+ holder.coreAt = [c[0], cy, R];
1634
+ }
1635
+
1636
+ /* ---------- bridges ---------- */
1637
+ drawBridge(m, pal, city) {
1638
+ const br = m.bridge;
1639
+ const born = this.now - m.born;
1640
+ const ext = born < -50 ? 1 : clamp((born - 1.8) / 2.0);
1641
+ if (ext <= 0) return;
1642
+ if (!this.visible(Math.min(br.a.x, br.b.x) - 1, Math.min(br.a.y, br.b.y) - 1, Math.max(br.a.x, br.b.x) + 1, Math.max(br.a.y, br.b.y) + 1, 4, -1)) return;
1643
+ const ctx = this.ctx;
1644
+ const n = 26;
1645
+ const px = -br.uy * 0.75; const py = br.ux * 0.75;
1646
+ const segs = Math.max(1, Math.round(n * ext));
1647
+ const pt = (u, side, dz = 0) => { const p = bridgePoint(br, u); return this.P(p.x + px * side, p.y + py * side, p.z + dz); };
1648
+ // the side of the deck that faces the viewer
1649
+ const front = (px + py) > 0 ? 1 : -1;
1650
+ for (let i = 0; i < segs; i++) {
1651
+ const u0 = i / n; const u1 = (i + 1) / n;
1652
+ this.poly([pt(u0, front, 0), pt(u1, front, 0), pt(u1, front, -0.28), pt(u0, front, -0.28)], pal.deckSide);
1653
+ this.poly([pt(u0, -1), pt(u1, -1), pt(u1, 1), pt(u0, 1)], i % 2 ? pal.deck : pal.deckB, 'rgba(36,22,12,.25)');
1654
+ }
1655
+ // under arch supports
1656
+ ctx.strokeStyle = pal.rail; ctx.lineWidth = Math.max(1, this.cam.A * 0.05);
1657
+ ctx.beginPath();
1658
+ for (let i = 0; i <= segs; i++) { const p = pt(i / n, front, -0.28 - Math.sin(Math.PI * i / n) * 0.6); if (i) ctx.lineTo(p[0], p[1]); else ctx.moveTo(p[0], p[1]); }
1659
+ ctx.stroke();
1660
+ // railings and posts
1661
+ const glow = clamp(m.glow);
1662
+ const borrowing = borrowTo(city, m.id);
1663
+ for (const side of [-1, 1]) {
1664
+ ctx.strokeStyle = pal.rail; ctx.lineWidth = Math.max(1, this.cam.A * 0.05);
1665
+ for (let i = 0; i <= segs; i += 2) { const a = pt(i / n, side); const b = pt(i / n, side, 0.55); ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.stroke(); }
1666
+ ctx.beginPath();
1667
+ for (let i = 0; i <= segs; i++) { const p = pt(i / n, side, 0.55); if (i) ctx.lineTo(p[0], p[1]); else ctx.moveTo(p[0], p[1]); }
1668
+ ctx.stroke();
1669
+ }
1670
+ // the light cable along the deck: it glows when tasks or GPU turns travel
1671
+ const cablePts = [];
1672
+ for (let i = 0; i <= segs; i++) cablePts.push(pt(i / n, 0, 0.04));
1673
+ ctx.lineCap = 'round';
1674
+ ctx.strokeStyle = alpha('#FFD072', 0.25 + 0.15 * (1 - this.pal.night));
1675
+ ctx.lineWidth = Math.max(1.2, this.cam.A * 0.06);
1676
+ ctx.beginPath(); cablePts.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.stroke();
1677
+ if (glow > 0.02 || borrowing) {
1678
+ ctx.save();
1679
+ ctx.globalCompositeOperation = 'lighter';
1680
+ const g = Math.max(glow, borrowing ? 0.55 : 0);
1681
+ ctx.strokeStyle = borrowing ? `rgba(127,227,255,${0.55 * g})` : `rgba(255,200,90,${0.6 * g})`;
1682
+ ctx.lineWidth = Math.max(3, this.cam.A * 0.24);
1683
+ ctx.shadowColor = borrowing ? '#7FE3FF' : '#FFC24A'; ctx.shadowBlur = 16 * g;
1684
+ ctx.beginPath(); cablePts.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.stroke();
1685
+ if (borrowing && !this.reduced()) {
1686
+ ctx.setLineDash([this.cam.A * 0.4, this.cam.A * 0.6]);
1687
+ ctx.lineDashOffset = -this.now * this.cam.A * 3;
1688
+ ctx.strokeStyle = 'rgba(220,250,255,.9)'; ctx.lineWidth = Math.max(1.5, this.cam.A * 0.08); ctx.shadowBlur = 0;
1689
+ ctx.beginPath(); cablePts.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.stroke();
1690
+ ctx.setLineDash([]);
1691
+ }
1692
+ ctx.restore();
1693
+ }
1694
+ ctx.lineCap = 'butt';
1695
+ // lanterns
1696
+ for (const u of [0.15, 0.5, 0.85]) {
1697
+ if (u > ext) continue;
1698
+ const p = pt(u, front, 0.8);
1699
+ const on = this.pal.night > 0.25 || glow > 0.1 || borrowing;
1700
+ ctx.fillStyle = on ? (borrowing ? '#BFF3FF' : '#FFE3A1') : '#C9B48A';
1701
+ ctx.beginPath(); hexScreen(ctx, p[0], p[1], Math.max(2.5, this.cam.A * 0.13)); ctx.fill();
1702
+ ctx.strokeStyle = 'rgba(36,22,12,.6)'; ctx.lineWidth = 1; ctx.stroke();
1703
+ if (on) (this.lamps ||= []).push([p[0], p[1], borrowing ? 1.6 : 1.3, borrowing ? '127,227,255' : null]);
1704
+ }
1705
+ m.cable = cablePts;
1706
+ }
1707
+
1708
+ drawCouriers() {
1709
+ const ctx = this.ctx;
1710
+ for (const cr of this.couriers) {
1711
+ const br = cr.m.bridge;
1712
+ const u0 = clamp((this.now - cr.t0) / cr.dur);
1713
+ const e = easeInOut(u0);
1714
+ // home side: from the plaza onto the bridge; friend side: from the tower
1715
+ const u = cr.dir > 0 ? e : 1 - e;
1716
+ const p0 = bridgePoint(br, u);
1717
+ const p = this.P(p0.x, p0.y, p0.z + 0.6);
1718
+ ctx.save();
1719
+ ctx.globalCompositeOperation = 'lighter';
1720
+ for (let i = 1; i <= 6; i++) {
1721
+ const uu = clamp(u - cr.dir * i * 0.018);
1722
+ const q0 = bridgePoint(br, uu); const q = this.P(q0.x, q0.y, q0.z + 0.6);
1723
+ ctx.fillStyle = alpha(cr.color, 0.28 * (1 - i / 7));
1724
+ ctx.beginPath(); ctx.arc(q[0], q[1], Math.max(2, this.cam.A * 0.16) * (1 - i / 9), 0, 6.28); ctx.fill();
1725
+ }
1726
+ const g = ctx.createRadialGradient(p[0], p[1], 1, p[0], p[1], this.cam.A * 0.8);
1727
+ g.addColorStop(0, alpha(cr.color, 0.9)); g.addColorStop(1, alpha(cr.color, 0));
1728
+ ctx.fillStyle = g; ctx.fillRect(p[0] - this.cam.A, p[1] - this.cam.A, this.cam.A * 2, this.cam.A * 2);
1729
+ ctx.restore();
1730
+ if (cr.kind === 'task') drawNoteIcon(ctx, p[0], p[1] - this.cam.A * 0.25, Math.max(10, this.cam.A * 0.5), '#FFE08A', '#FFB531');
1731
+ else { ctx.fillStyle = '#E8FBFF'; ctx.beginPath(); hexScreen(ctx, p[0], p[1], Math.max(3, this.cam.A * 0.14)); ctx.fill(); }
1732
+ }
1733
+ }
1734
+
1735
+ /* ---------- bees ---------- */
1736
+ drawBee(a) {
1737
+ const ctx = this.ctx;
1738
+ const now = this.now;
1739
+ const reduced = this.reduced();
1740
+ const k = this.k;
1741
+ let bob = 0; let tilt = 0; let sx = 1; let sy = 1; let wingRate = 22; let wingAmp = 0.28; let eyes = false;
1742
+ const m = a.mode;
1743
+ if (!reduced) {
1744
+ switch (m) {
1745
+ case 'walk': {
1746
+ wingRate = 40; wingAmp = 0.5;
1747
+ bob = Math.abs(Math.sin(now * 9 + a.ph)) * 0.12;
1748
+ tilt = 0.1 * a.facing;
1749
+ break;
1750
+ }
1751
+ case 'working': bob = Math.sin(now * 16 + a.ph) * 0.018; wingRate = 26; break;
1752
+ case 'thinking': bob = Math.sin(now * 3 + a.ph) * 0.05; break;
1753
+ case 'waiting': { const beat = (now * 2.2 + a.ph) % 1; bob = beat < 0.2 ? -Math.sin((beat / 0.2) * Math.PI) * 0.05 : 0; wingRate = 14; wingAmp = 0.18; break; }
1754
+ case 'reviewing': bob = Math.sin(now * 2 + a.ph) * 0.03; tilt = Math.sin(now * 1.2) * 0.05; break;
1755
+ case 'testing': bob = Math.sin(now * 5 + a.ph) * 0.03; break;
1756
+ case 'borrow': bob = Math.sin(now * 3 + a.ph) * 0.08; wingRate = 34; wingAmp = 0.42; break;
1757
+ case 'sleep': bob = Math.sin(now * 1.1 + a.ph) * 0.012; tilt = 0.24; eyes = true; wingRate = 0; wingAmp = 0; break;
1758
+ default: bob = Math.sin(now * 2.2 + a.ph) * 0.05;
1759
+ }
1760
+ const sq = now - a.squashAt;
1761
+ if (sq < 0.5) { const d = Math.exp(-sq * 9) * Math.cos(sq * 22) * 0.16; sy *= 1 - d; sx *= 1 + d * 0.8; }
1762
+ } else if (m === 'sleep') { tilt = 0.18; eyes = true; wingAmp = 0; } else { wingRate = 0; wingAmp = 0.2; }
1763
+ if (now >= a.blinkAt && now < a.blinkAt + 0.13) eyes = true;
1764
+ const p = this.P(a.pos.x, a.pos.y, a.pos.z + (a.place.kind === 'room' && !a.path.length ? 0.3 : 0) + bob);
1765
+ a.sx = p[0]; a.sy = p[1];
1766
+ // shadow on the ground
1767
+ const groundZ = a.path.length && a.path[0].bridge ? bridgeGroundZ(a) : (a.place.kind === 'room' && roomAt(a.pos.x, a.pos.y) ? 0.3 : a.pos.z > 2 ? this.towerHeight(this.machines.get(a.place.machine) || {}) : 0);
1768
+ const sp = this.P(a.pos.x, a.pos.y, Math.min(groundZ, a.pos.z));
1769
+ ctx.fillStyle = `rgba(20,12,6,${0.26 * a.alpha})`;
1770
+ ctx.beginPath(); ctx.ellipse(sp[0], sp[1], 15 * k, 7 * k, 0, 0, 6.28); ctx.fill();
1771
+ const img = this.sprite(a.ag);
1772
+ ctx.save();
1773
+ ctx.globalAlpha = a.alpha;
1774
+ ctx.translate(p[0], p[1]);
1775
+ ctx.translate(0, 28 * k);
1776
+ ctx.rotate(tilt);
1777
+ ctx.scale(sx, sy);
1778
+ ctx.translate(-32 * k, -72 * k);
1779
+ const flap = wingRate ? Math.sin(now * wingRate + a.ph) * wingAmp : -0.25;
1780
+ ctx.fillStyle = 'rgba(255,253,245,.62)'; ctx.strokeStyle = 'rgba(36,22,12,.5)'; ctx.lineWidth = Math.max(0.6, k);
1781
+ for (const [wx, dir] of [[13.5, -1], [50.5, 1]]) {
1782
+ ctx.save(); ctx.translate((wx + dir * -6) * k, 30 * k); ctx.rotate(dir * (0.56 - flap));
1783
+ ctx.beginPath(); ctx.ellipse(dir * 7 * k, -4 * k, 10.5 * k, 6 * k * (1 - Math.abs(flap) * 0.35), 0, 0, 6.28); ctx.fill(); ctx.stroke();
1784
+ ctx.restore();
1785
+ }
1786
+ if (img) ctx.drawImage(img, -4 * k, -2 * k, 72 * k, 76 * k);
1787
+ else { ctx.fillStyle = a.ag.color; ctx.beginPath(); ctx.arc(32 * k, 32 * k, 17 * k, 0, 6.28); ctx.fill(); }
1788
+ if (eyes) {
1789
+ ctx.fillStyle = '#FCC041'; ctx.strokeStyle = OUT; ctx.lineWidth = Math.max(0.8, 1.3 * k); ctx.lineCap = 'round';
1790
+ for (const ex of [25.6, 38.4]) { ctx.beginPath(); ctx.ellipse(ex * k, 31.6 * k, 4.3 * k, 5.1 * k, 0, 0, 6.28); ctx.fill(); ctx.beginPath(); ctx.moveTo((ex - 3) * k, 32 * k); ctx.quadraticCurveTo(ex * k, 34.8 * k, (ex + 3) * k, 32 * k); ctx.stroke(); }
1791
+ ctx.lineCap = 'butt';
1792
+ }
1793
+ ctx.restore();
1794
+ // a task note in its hands while it walks to a new room
1795
+ const task = a.ag.taskId && this.getCity().tasks[a.ag.taskId];
1796
+ if (a.mode === 'walk' && task && a.place.kind === 'room') drawNoteIcon(ctx, p[0], p[1] + 24 * k, 15 * k * 1.2, '#FFE08A', a.ag.color);
1797
+ else if (a.mode === 'walk' && a.place.kind === 'roof') drawParcel(ctx, p[0], p[1] + 24 * k, 14 * k, '#7FE3FF');
1798
+ this.hits.push({ id: a.id, x: p[0] - 26 * k, y: p[1] - 38 * k, w: 52 * k, h: 66 * k });
1799
+ }
1800
+
1801
+ sprite(ag) {
1802
+ const raw = Math.max(16, this.k * 72 * this.dpr);
1803
+ const px = Math.round(Math.pow(2, Math.round(Math.log2(raw) * 3) / 3));
1804
+ const key = `${ag.color}|${ag.role}|${ag.name}|${px}`;
1805
+ const sp = this.sprites.get(ag.id);
1806
+ if (sp && sp.key === key) return sp.ready ? sp.img : sp.prev;
1807
+ const img = new Image();
1808
+ const svg = beeSvg({ id: ag.id, name: ag.name, role: ag.role, color: ag.color }, { title: '' })
1809
+ .replace(/<g opacity="\.95">[\s\S]*?<\/g>/, '')
1810
+ .replace('viewBox="0 0 64 64"', `viewBox="-4 -2 72 76" width="${px}" height="${Math.round((px * 76) / 72)}"`);
1811
+ const next = { key, img, ready: false, prev: sp ? (sp.ready ? sp.img : sp.prev) : null };
1812
+ img.onload = () => { next.ready = true; if (!this.running) this.draw(); };
1813
+ img.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
1814
+ this.sprites.set(ag.id, next);
1815
+ return next.prev;
1816
+ }
1817
+
1818
+ /* ---------- lights, focus, overlay ---------- */
1819
+ drawLights(pal) {
1820
+ const ctx = this.ctx;
1821
+ const lamps = this.lamps || []; this.lamps = [];
1822
+ const wins = this.windowLights || []; this.windowLights = [];
1823
+ const jars = this.jarLights || []; this.jarLights = [];
1824
+ ctx.save();
1825
+ ctx.globalCompositeOperation = 'lighter';
1826
+ const al = clamp((pal.night - 0.2) * 0.5);
1827
+ if (al > 0) {
1828
+ for (const [x, y, s = 1, rgbs] of lamps) {
1829
+ const r = this.cam.A * 1.4 * s;
1830
+ const g = ctx.createRadialGradient(x, y + this.cam.A * 0.2, 1, x, y + this.cam.A * 0.2, r);
1831
+ g.addColorStop(0, `rgba(${rgbs || '255,190,90'},${al * 0.8})`); g.addColorStop(1, `rgba(${rgbs || '255,190,90'},0)`);
1832
+ ctx.fillStyle = g; ctx.fillRect(x - r, y - r, r * 2, r * 2.2);
1833
+ }
1834
+ for (const [x, y] of wins) {
1835
+ const r = this.cam.A * 0.8;
1836
+ const g = ctx.createRadialGradient(x, y, 1, x, y, r);
1837
+ g.addColorStop(0, `rgba(255,210,130,${al * 0.5})`); g.addColorStop(1, 'rgba(255,210,130,0)');
1838
+ ctx.fillStyle = g; ctx.fillRect(x - r, y - r, r * 2, r * 2);
1839
+ }
1840
+ }
1841
+ for (const [x, y] of jars) {
1842
+ const r = this.cam.A * 0.5;
1843
+ const g = ctx.createRadialGradient(x, y, 1, x, y, r);
1844
+ g.addColorStop(0, `rgba(255,190,80,${0.25 + al * 0.4})`); g.addColorStop(1, 'rgba(255,190,80,0)');
1845
+ ctx.fillStyle = g; ctx.fillRect(x - r, y - r, r * 2, r * 2);
1846
+ }
1847
+ // screens glow on busy bees
1848
+ for (const a of this.actors.values()) {
1849
+ if (!['working', 'testing', 'reviewing', 'thinking'].includes(a.mode) || a.sx < -100) continue;
1850
+ const r = this.cam.A * 0.7;
1851
+ const [r0, g0, b0] = rgb(a.ag.color);
1852
+ const g = ctx.createRadialGradient(a.sx, a.sy + this.cam.A * 0.3, 1, a.sx, a.sy + this.cam.A * 0.3, r);
1853
+ g.addColorStop(0, `rgba(${r0},${g0},${b0},${0.14 + pal.night * 0.14})`); g.addColorStop(1, `rgba(${r0},${g0},${b0},0)`);
1854
+ ctx.fillStyle = g; ctx.fillRect(a.sx - r, a.sy - r * 0.6, r * 2, r * 2);
1855
+ }
1856
+ ctx.restore();
1857
+ // a soft night wash over everything outside the light
1858
+ if (pal.night > 0.4) { ctx.fillStyle = `rgba(10,14,32,${(pal.night - 0.4) * 0.18})`; ctx.fillRect(0, 0, this.w, this.h); }
1859
+ }
1860
+
1861
+ drawFocus() {
1862
+ const f = this.focus;
1863
+ if (!f || f.kind !== 'room') return;
1864
+ const r = ROOMS[f.id];
1865
+ if (!r) return;
1866
+ const ctx = this.ctx;
1867
+ const pz = 0.3;
1868
+ const h = r.row === 'back' ? WALL + 0.3 : 2.4;
1869
+ const pts = [this.P(r.x0 - 0.15, r.y0 - 0.15, pz), this.P(r.x0 - 0.15, r.y0 - 0.15, h + 0.6), this.P(r.x1 + 0.15, r.y0 - 0.15, h + 0.6), this.P(r.x1 + 0.15, r.y0 - 0.15, pz), this.P(r.x1 + 0.15, r.y1 + 0.15, pz), this.P(r.x0 - 0.15, r.y1 + 0.15, pz)];
1870
+ ctx.save();
1871
+ ctx.beginPath(); ctx.rect(0, 0, this.w, this.h);
1872
+ ctx.moveTo(pts[0][0], pts[0][1]);
1873
+ for (let i = pts.length - 1; i > 0; i--) ctx.lineTo(pts[i][0], pts[i][1]);
1874
+ ctx.closePath();
1875
+ ctx.fillStyle = 'rgba(12,10,8,.5)';
1876
+ ctx.fill('evenodd');
1877
+ ctx.restore();
1878
+ }
1879
+
1880
+ drawOverlay(pal, c) {
1881
+ const ctx = this.ctx;
1882
+ const now = this.now;
1883
+ const k = this.k;
1884
+ const reduced = this.reduced();
1885
+ const A = this.cam.A;
1886
+ // room name pills over the front rooms and over every room when zoomed out
1887
+ if (A >= 10 && A < 44 && !(this.focus?.kind === 'room')) {
1888
+ for (const key of ROOM_KINDS) {
1889
+ const r = ROOMS[key];
1890
+ const cn = roomCenter(key);
1891
+ const z = r.row === 'back' ? WALL + 1.1 : 2.1;
1892
+ const p = this.P(cn.x, r.row === 'back' ? r.y0 + 0.4 : r.y0 + 0.6, z);
1893
+ const n = [...this.notes.values()].filter((x) => x.room === key && !x.gone && x.t.status !== 'done').length;
1894
+ const label = this.t(`city.room.${key}`);
1895
+ drawRoomPill(ctx, p[0], p[1], label, ROOM_COLOR[key], n, clamp(A * 0.42, 10, 13.5), this.highlightRoom(key));
1896
+ }
1897
+ }
1898
+ // machine signs
1899
+ for (const m of this.machines.values()) {
1900
+ const born = now - m.born;
1901
+ if (born < 3 && born > -50) continue;
1902
+ const tw = towerOf(m.center);
1903
+ const p = this.P((tw.x0 + tw.x1) / 2, (tw.y0 + tw.y1) / 2, this.towerHeight(m) + 2.4);
1904
+ const drop = reduced || born < -50 ? 1 : easeOutBack(clamp((born - 3) / 0.6));
1905
+ ctx.globalAlpha = clamp(drop);
1906
+ drawMachineSign(ctx, p[0], p[1] - (1 - drop) * 30, m.m?.name || m.id, m.m?.color || '#7DC49B', m.m?.online === false ? this.t('city.machine.offline') : null, clamp(A * 0.45, 11, 14), this.highlight?.kind === 'machine' && this.highlight.id === m.id);
1907
+ ctx.globalAlpha = 1;
1908
+ }
1909
+ // your building sign
1910
+ {
1911
+ const p = this.P(-1.6, -1.4, 5.9);
1912
+ drawMachineSign(ctx, p[0], p[1], c.machines[c.selfId]?.name || this.t('city.machine.you'), '#FFB531', null, clamp(A * 0.45, 11, 14), this.highlight?.kind === 'machine' && this.highlight.id === c.selfId);
1913
+ }
1914
+ // bees: name tags, status icons, bubbles
1915
+ for (const a of this.actors.values()) {
1916
+ if (a.sx < -100) continue;
1917
+ const headY = a.sy - 34 * k;
1918
+ const ix = a.sx + 22 * k; const iy = headY - 6 * k;
1919
+ const sz = clamp(A * 0.62, 12, 26);
1920
+ ctx.globalAlpha = a.alpha;
1921
+ if (A >= 15) {
1922
+ const wob = reduced ? 0 : Math.sin(now * 3 + a.ph) * 2;
1923
+ if (a.mode === 'waiting') iconClock(ctx, ix, iy + wob, sz, reduced ? 0 : now);
1924
+ else if (a.mode === 'thinking') iconDots(ctx, ix, iy + wob, sz, reduced ? 0 : now);
1925
+ else if (a.mode === 'reviewing') iconMag(ctx, ix, iy + wob, sz, reduced ? 0 : now);
1926
+ else if (a.mode === 'testing') iconFlask(ctx, ix, iy + wob, sz, reduced ? 0 : now);
1927
+ else if (a.ag.status === 'offline') iconPause(ctx, ix, iy, sz);
1928
+ else if (a.mode === 'borrow') drawPill(ctx, a.sx, headY - sz * 0.9 + wob, this.t('city.fx.borrowing'), '#7FE3FF', clamp(A * 0.4, 10, 12));
1929
+ }
1930
+ if (A >= 17 || this.highlight?.id === a.id) {
1931
+ const fs = clamp(A * 0.38, 9.5, 12.5);
1932
+ drawNameTag(ctx, a.sx, a.sy + 30 * k, a.ag.name, a.ag.level, a.ag.color, this.highlight?.id === a.id || this.dropTarget?.id === a.id, fs);
1933
+ }
1934
+ if (this.highlight?.id === a.id || this.dropTarget?.id === a.id) {
1935
+ ctx.strokeStyle = 'rgba(255,208,114,.95)'; ctx.lineWidth = 2; ctx.setLineDash([4, 4]);
1936
+ ctx.beginPath(); ctx.ellipse(a.sx, a.sy + 28 * k, 26 * k, 10 * k, 0, 0, 6.28); ctx.stroke(); ctx.setLineDash([]);
1937
+ }
1938
+ if (a.bubble && A >= 20) {
1939
+ const age = now - a.bubble.at;
1940
+ ctx.globalAlpha = Math.min(clamp(age / 0.2), clamp((a.bubble.until - now) / 0.35)) * a.alpha;
1941
+ drawBubble(ctx, a.sx, headY - 10 * k, a.bubble.text, clamp(A * 0.4, 10.5, 13), this.w, a.ag.color);
1942
+ }
1943
+ ctx.globalAlpha = 1;
1944
+ }
1945
+ // flying notes between boards
1946
+ for (const n of this.notes.values()) {
1947
+ if (!n.fly) continue;
1948
+ const u = clamp((now - n.fly.t0) / 0.9);
1949
+ if (u >= 1) { n.fly = null; continue; }
1950
+ const to = this.notePos(n);
1951
+ const e = easeInOut(u);
1952
+ const x = lerp(n.fly.from.x, to.x, e); const y = lerp(n.fly.from.y, to.y, e); const z = lerp(n.fly.from.z, to.z, e) + Math.sin(Math.PI * u) * 2.2;
1953
+ const p = this.P(x, y, z);
1954
+ drawNoteIcon(ctx, p[0], p[1], Math.max(10, A * 0.55), n.t.status === 'done' ? '#C6E8CD' : '#FFE08A', this.actors.get(n.t.assignee)?.ag.color);
1955
+ }
1956
+ void pal;
1957
+ }
1958
+
1959
+ drawParticles() {
1960
+ const ctx = this.ctx;
1961
+ for (const p of this.parts) {
1962
+ const life = 1 - p.age / p.max;
1963
+ ctx.globalAlpha = clamp(life * 1.6);
1964
+ switch (p.kind) {
1965
+ case 'glyph': ctx.font = `700 ${p.size}px ${MONO}`; ctx.fillStyle = p.color; ctx.fillText(p.text, p.x, p.y); break;
1966
+ case 'z': ctx.font = `800 ${Math.min(22, p.size * (1 + p.age * 0.3))}px ${FONT}`; ctx.fillStyle = '#E8DDF7'; ctx.fillText('z', p.x, p.y); break;
1967
+ case 'dot': case 'bubble':
1968
+ ctx.fillStyle = p.color; ctx.beginPath(); ctx.arc(p.x, p.y, p.size, 0, 6.28); ctx.fill();
1969
+ if (p.kind === 'bubble') { ctx.strokeStyle = 'rgba(255,255,255,.6)'; ctx.lineWidth = 1; ctx.stroke(); }
1970
+ break;
1971
+ case 'spark': {
1972
+ ctx.fillStyle = p.color;
1973
+ const r = p.size * (0.6 + life * 0.6);
1974
+ ctx.beginPath(); ctx.moveTo(p.x, p.y - r * 2); ctx.lineTo(p.x + r * 0.5, p.y - r * 0.5); ctx.lineTo(p.x + r * 2, p.y); ctx.lineTo(p.x + r * 0.5, p.y + r * 0.5); ctx.lineTo(p.x, p.y + r * 2); ctx.lineTo(p.x - r * 0.5, p.y + r * 0.5); ctx.lineTo(p.x - r * 2, p.y); ctx.lineTo(p.x - r * 0.5, p.y - r * 0.5); ctx.closePath(); ctx.fill();
1975
+ break;
1976
+ }
1977
+ case 'puff': ctx.fillStyle = 'rgba(242,233,218,.55)'; ctx.beginPath(); ctx.arc(p.x, p.y, p.size * (1 + p.age * 1.5), 0, 6.28); ctx.fill(); break;
1978
+ case 'label': {
1979
+ ctx.font = `700 ${clamp(this.cam.A * 0.42, 10, 13)}px ${FONT}`;
1980
+ const tw = ctx.measureText(p.text).width;
1981
+ ctx.fillStyle = 'rgba(20,16,12,.85)'; roundRect(ctx, p.x - tw / 2 - 7, p.y - 13, tw + 14, 20, 10); ctx.fill();
1982
+ ctx.fillStyle = p.color; ctx.textAlign = 'center'; ctx.fillText(p.text, p.x, p.y + 1.5); ctx.textAlign = 'left';
1983
+ break;
1984
+ }
1985
+ case 'jarpop': ctx.globalAlpha = clamp(life * 2); iconJar(ctx, p.x, p.y, clamp(this.cam.A * 0.6, 12, 22)); break;
1986
+ default: break;
1987
+ }
1988
+ }
1989
+ ctx.globalAlpha = 1;
1990
+ }
1991
+ }
1992
+
1993
+ /* ---------- helpers ---------- */
1994
+ function borrowTo(c, machineId) { return !!c && Object.values(c.borrows || {}).some((b) => b.to === machineId || b.from === machineId); }
1995
+
1996
+ function bridgeGroundZ(a) { return Math.max(0, a.pos.z - WALK_Z); }
1997
+
1998
+ function drawRoomPill(ctx, x, y, text, color, count, fs, on) {
1999
+ ctx.font = `750 ${fs}px ${FONT}`;
2000
+ const tw = ctx.measureText(text).width;
2001
+ const cw = count ? fs * 1.5 : 0;
2002
+ const w = tw + 26 + cw; const h = fs + 11;
2003
+ const x0 = x - w / 2;
2004
+ ctx.fillStyle = 'rgba(0,0,0,.25)'; roundRect(ctx, x0 + 1, y - h / 2 + 2, w, h, h / 2); ctx.fill();
2005
+ ctx.fillStyle = on ? 'rgba(52,40,26,.96)' : 'rgba(24,19,14,.86)'; roundRect(ctx, x0, y - h / 2, w, h, h / 2); ctx.fill();
2006
+ ctx.strokeStyle = on ? 'rgba(255,208,114,.9)' : alpha(color, 0.5); ctx.lineWidth = 1; ctx.stroke();
2007
+ ctx.fillStyle = color; ctx.beginPath(); hexScreen(ctx, x0 + 11, y, fs * 0.36); ctx.fill();
2008
+ ctx.fillStyle = '#F2E9DA'; ctx.fillText(text, x0 + 20, y + fs * 0.36);
2009
+ if (count) {
2010
+ ctx.fillStyle = alpha(color, 0.25); roundRect(ctx, x0 + w - cw - 5, y - fs * 0.55, cw, fs * 1.1, fs * 0.55); ctx.fill();
2011
+ ctx.fillStyle = color; ctx.font = `800 ${fs * 0.82}px ${FONT}`; ctx.textAlign = 'center';
2012
+ ctx.fillText(String(count), x0 + w - cw / 2 - 5, y + fs * 0.3); ctx.textAlign = 'left';
2013
+ }
2014
+ }
2015
+
2016
+ function drawMachineSign(ctx, x, y, text, color, sub, fs, on) {
2017
+ ctx.font = `800 ${fs}px ${FONT}`;
2018
+ const tw = ctx.measureText(text).width;
2019
+ ctx.font = `600 ${fs * 0.8}px ${FONT}`;
2020
+ const sw = sub ? ctx.measureText(sub).width + 10 : 0;
2021
+ const w = tw + 34 + sw; const h = fs + 14;
2022
+ const x0 = x - w / 2;
2023
+ ctx.strokeStyle = 'rgba(36,22,12,.6)'; ctx.lineWidth = 1.5;
2024
+ ctx.beginPath(); ctx.moveTo(x, y + h / 2); ctx.lineTo(x, y + h / 2 + 10); ctx.stroke();
2025
+ ctx.fillStyle = 'rgba(0,0,0,.3)'; roundRect(ctx, x0 + 2, y - h / 2 + 3, w, h, 8); ctx.fill();
2026
+ ctx.fillStyle = on ? '#3A2C1C' : '#241C14'; roundRect(ctx, x0, y - h / 2, w, h, 8); ctx.fill();
2027
+ ctx.strokeStyle = on ? '#FFD072' : alpha(color, 0.8); ctx.lineWidth = 1.5; ctx.stroke();
2028
+ ctx.fillStyle = color; ctx.beginPath(); hexScreen(ctx, x0 + 13, y, fs * 0.42); ctx.fill();
2029
+ ctx.fillStyle = '#FFF4DE'; ctx.font = `800 ${fs}px ${FONT}`; ctx.fillText(text, x0 + 24, y + fs * 0.36);
2030
+ if (sub) { ctx.fillStyle = '#E57B6E'; ctx.font = `600 ${fs * 0.8}px ${FONT}`; ctx.fillText(sub, x0 + 30 + tw, y + fs * 0.3); }
2031
+ }
2032
+
2033
+ export { WALL, ROOMS, CORRIDOR_Y };