Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances, roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map, alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js. Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push, bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
398 lines
17 KiB
JavaScript
398 lines
17 KiB
JavaScript
'use strict';
|
|
/*
|
|
* The canvas map and nothing else (window.MapRender.MapView): camera, pan / pinch / wheel / keyboard, tile culling,
|
|
* drawing. It knows no API. The screen gives it a `source` with the loaded data and callbacks:
|
|
*
|
|
* source.forEachSpecial(x0, y0, x1, y1, fn(special)) rural + incumbent tiles in the rect
|
|
* source.townsInRect(x0, y0, x1, y1) -> [town] POPs in the rect
|
|
* source.isOwn(town) -> bool
|
|
* onTap(x, y) a finger lift / click that was not a drag or a pinch (act on lift)
|
|
* onMoveEnd(view) debounced after the camera stopped; view = { x0, y0, x1, y1, cx, cy, zoom }
|
|
*
|
|
* Coordinates are tile units, y grows downward; tile (x, y) is the square [x-.5, x+.5] x [y-.5, y+.5].
|
|
* Touch: one finger pans, two fingers pinch-zoom around their midpoint and never trigger a tap. A tap is only
|
|
* reported when no second finger was ever down and the finger moved less than a few pixels.
|
|
*/
|
|
(function () {
|
|
const COLORS = {
|
|
void: '#04070e', land: '#0d1626', land2: '#0f192b', grid: 'rgba(120,160,220,0.10)', grid10: 'rgba(120,160,220,0.22)', border: 'rgba(77,227,255,0.55)',
|
|
fiber: '#4de3ff', hardware: '#ff9f5a', capital: '#5be08f', incumbent: '#b06cff', incumbentFill: '#241338',
|
|
own: '#4de3ff', ownFill: '#0f4b63', select: '#ffd24d', text: '#e4f0ff', halo: 'rgba(4,7,14,0.9)', reach: 'rgba(77,227,255,0.35)',
|
|
};
|
|
|
|
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
|
|
|
// A stable colour per operator id so rivals are told apart but never confused with you (cyan) or incumbents (violet).
|
|
function rivalColors(id) {
|
|
const hues = [8, 28, 48, 86, 150, 330, 355, 120]; // warm and green hues, no cyan/blue/violet
|
|
const hue = hues[Math.abs(id | 0) % hues.length];
|
|
return { fill: 'hsl(' + hue + ',45%,24%)', stroke: 'hsl(' + hue + ',70%,62%)' };
|
|
}
|
|
|
|
class MapView {
|
|
constructor(canvas, source, opts) {
|
|
this.canvas = canvas;
|
|
this.ctx = canvas.getContext('2d');
|
|
this.src = source;
|
|
this.opts = opts || {};
|
|
this.radius = 50;
|
|
this.cx = 0; this.cy = 0; this.zoom = 24;
|
|
this.w = 0; this.h = 0; this.dpr = 1;
|
|
this.selected = null;
|
|
this.pointers = new Map();
|
|
this.drag = null;
|
|
this.pinch = null;
|
|
this.raf = 0;
|
|
this.moveTimer = 0;
|
|
this.labelFn = null;
|
|
this.reachTiles = 2;
|
|
this._bind();
|
|
}
|
|
|
|
setWorld(radius) { this.radius = radius; this._limits(); this.invalidate(); }
|
|
setSelected(x, y) { this.selected = x === null ? null : { x, y }; this.invalidate(); }
|
|
|
|
resize() {
|
|
const r = this.canvas.getBoundingClientRect();
|
|
const dpr = Math.min(window.devicePixelRatio || 1, 3);
|
|
if (!r.width || !r.height) return false;
|
|
this.w = r.width; this.h = r.height; this.dpr = dpr;
|
|
this.canvas.width = Math.round(r.width * dpr);
|
|
this.canvas.height = Math.round(r.height * dpr);
|
|
this._limits();
|
|
this.zoom = clamp(this.zoom, this.minZoom, this.maxZoom);
|
|
this._clampCenter();
|
|
this.invalidate();
|
|
this._scheduleMoveEnd(0);
|
|
return true;
|
|
}
|
|
|
|
_limits() {
|
|
const span = this.radius * 2 + 3;
|
|
this.minZoom = clamp(Math.min(this.w || 300, this.h || 300) / span, 2.5, 20);
|
|
this.maxZoom = 90;
|
|
}
|
|
|
|
_clampCenter() {
|
|
this.cx = clamp(this.cx, -this.radius, this.radius);
|
|
this.cy = clamp(this.cy, -this.radius, this.radius);
|
|
}
|
|
|
|
setView(cx, cy, zoom) {
|
|
this.cx = cx; this.cy = cy;
|
|
if (zoom) this.zoom = clamp(zoom, this.minZoom, this.maxZoom);
|
|
this._clampCenter();
|
|
this.invalidate();
|
|
this._scheduleMoveEnd(0);
|
|
}
|
|
|
|
// screen px (relative to the canvas) <-> tile coordinates
|
|
toTile(px, py) { return { x: (px - this.w / 2) / this.zoom + this.cx, y: (py - this.h / 2) / this.zoom + this.cy }; }
|
|
toScreen(x, y) { return { x: (x - this.cx) * this.zoom + this.w / 2, y: (y - this.cy) * this.zoom + this.h / 2 }; }
|
|
|
|
// Visible tiles clamped to the world.
|
|
view() {
|
|
const a = this.toTile(0, 0), b = this.toTile(this.w, this.h), r = this.radius;
|
|
return {
|
|
x0: clamp(Math.floor(a.x), -r, r), y0: clamp(Math.floor(a.y), -r, r),
|
|
x1: clamp(Math.ceil(b.x), -r, r), y1: clamp(Math.ceil(b.y), -r, r),
|
|
cx: this.cx, cy: this.cy, zoom: this.zoom,
|
|
};
|
|
}
|
|
|
|
// Zoom keeping the world point under screen (px, py) fixed.
|
|
zoomAt(px, py, factor) {
|
|
const before = this.toTile(px, py);
|
|
this.zoom = clamp(this.zoom * factor, this.minZoom, this.maxZoom);
|
|
this.cx = before.x - (px - this.w / 2) / this.zoom;
|
|
this.cy = before.y - (py - this.h / 2) / this.zoom;
|
|
this._clampCenter();
|
|
this.invalidate();
|
|
this._scheduleMoveEnd();
|
|
}
|
|
|
|
panBy(dxPx, dyPx) {
|
|
this.cx -= dxPx / this.zoom; this.cy -= dyPx / this.zoom;
|
|
this._clampCenter();
|
|
this.invalidate();
|
|
this._scheduleMoveEnd();
|
|
}
|
|
|
|
_scheduleMoveEnd(delay) {
|
|
clearTimeout(this.moveTimer);
|
|
this.moveTimer = setTimeout(() => { if (this.opts.onMoveEnd && this.w) this.opts.onMoveEnd(this.view()); }, delay === undefined ? 200 : delay);
|
|
}
|
|
|
|
invalidate() {
|
|
if (this.raf) return;
|
|
this.raf = requestAnimationFrame(() => { this.raf = 0; this.draw(); });
|
|
}
|
|
|
|
// ------------------------------------------------------------------ input
|
|
_bind() {
|
|
const c = this.canvas;
|
|
const rel = (e) => { const r = c.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; };
|
|
|
|
c.addEventListener('pointerdown', (e) => {
|
|
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
|
try { c.setPointerCapture(e.pointerId); } catch (err) { /* synthetic */ }
|
|
const p = rel(e);
|
|
this.pointers.set(e.pointerId, { x: p.x, y: p.y });
|
|
if (this.pointers.size === 1) {
|
|
this.drag = { startX: p.x, startY: p.y, lastX: p.x, lastY: p.y, moved: false, pinched: false, t: e.timeStamp, touch: e.pointerType !== 'mouse' };
|
|
} else if (this.pointers.size === 2 && this.drag) {
|
|
this.drag.pinched = true;
|
|
this._startPinch();
|
|
}
|
|
});
|
|
|
|
c.addEventListener('pointermove', (e) => {
|
|
const cur = this.pointers.get(e.pointerId);
|
|
if (!cur || !this.drag) return;
|
|
const p = rel(e);
|
|
cur.x = p.x; cur.y = p.y;
|
|
if (this.pointers.size >= 2 && this.pinch) { this._movePinch(); return; }
|
|
if (this.pointers.size === 1 && !this.drag.pinched) {
|
|
const slop = this.drag.touch ? 9 : 5;
|
|
if (!this.drag.moved && Math.hypot(p.x - this.drag.startX, p.y - this.drag.startY) > slop) this.drag.moved = true;
|
|
if (this.drag.moved) {
|
|
this.panBy(p.x - this.drag.lastX, p.y - this.drag.lastY);
|
|
c.classList.add('grabbing');
|
|
}
|
|
this.drag.lastX = p.x; this.drag.lastY = p.y;
|
|
}
|
|
});
|
|
|
|
const end = (e, cancelled) => {
|
|
if (!this.pointers.has(e.pointerId)) return;
|
|
const p = rel(e);
|
|
this.pointers.delete(e.pointerId);
|
|
try { c.releasePointerCapture(e.pointerId); } catch (err) { /* already released */ }
|
|
const d = this.drag;
|
|
if (this.pointers.size === 1 && d) {
|
|
// one finger left after a pinch: continue as a pan from where that finger is, never as a tap
|
|
const [rest] = this.pointers.values();
|
|
d.lastX = rest.x; d.lastY = rest.y; d.startX = rest.x; d.startY = rest.y; d.moved = true;
|
|
this.pinch = null;
|
|
}
|
|
if (this.pointers.size === 0 && d) {
|
|
const tap = !cancelled && !d.moved && !d.pinched && e.timeStamp - d.t < 700;
|
|
this.drag = null; this.pinch = null;
|
|
c.classList.remove('grabbing');
|
|
if (tap && this.opts.onTap) {
|
|
const t = this.toTile(p.x, p.y);
|
|
this.opts.onTap(Math.round(t.x), Math.round(t.y));
|
|
}
|
|
this._scheduleMoveEnd(60);
|
|
}
|
|
};
|
|
c.addEventListener('pointerup', (e) => end(e, false));
|
|
c.addEventListener('pointercancel', (e) => end(e, true));
|
|
c.addEventListener('lostpointercapture', (e) => { if (this.pointers.has(e.pointerId)) end(e, true); });
|
|
|
|
c.addEventListener('wheel', (e) => {
|
|
e.preventDefault();
|
|
const p = rel(e);
|
|
const dy = e.deltaMode === 1 ? e.deltaY * 16 : e.deltaY;
|
|
this.zoomAt(p.x, p.y, Math.exp(-dy * 0.0016));
|
|
}, { passive: false });
|
|
|
|
c.addEventListener('keydown', (e) => {
|
|
const step = Math.max(24, this.w * 0.2);
|
|
const k = e.key;
|
|
if (k === 'ArrowLeft') this.panBy(step, 0);
|
|
else if (k === 'ArrowRight') this.panBy(-step, 0);
|
|
else if (k === 'ArrowUp') this.panBy(0, step);
|
|
else if (k === 'ArrowDown') this.panBy(0, -step);
|
|
else if (k === '+' || k === '=') this.zoomAt(this.w / 2, this.h / 2, 1.3);
|
|
else if (k === '-' || k === '_') this.zoomAt(this.w / 2, this.h / 2, 1 / 1.3);
|
|
else if ((k === 'Enter' || k === ' ') && this.opts.onTap) this.opts.onTap(Math.round(this.cx), Math.round(this.cy));
|
|
else return;
|
|
e.preventDefault();
|
|
});
|
|
}
|
|
|
|
_pair() { const a = Array.from(this.pointers.values()); return [a[0], a[1]]; }
|
|
|
|
_startPinch() {
|
|
const [a, b] = this._pair();
|
|
const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
this.pinch = { dist: Math.max(10, Math.hypot(a.x - b.x, a.y - b.y)), zoom: this.zoom, world: this.toTile(mid.x, mid.y) };
|
|
}
|
|
|
|
_movePinch() {
|
|
const [a, b] = this._pair();
|
|
const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
const k = Math.hypot(a.x - b.x, a.y - b.y) / this.pinch.dist;
|
|
this.zoom = clamp(this.pinch.zoom * k, this.minZoom, this.maxZoom);
|
|
// keep the world point that was under the fingers' midpoint under it (also pans with the midpoint)
|
|
this.cx = this.pinch.world.x - (mid.x - this.w / 2) / this.zoom;
|
|
this.cy = this.pinch.world.y - (mid.y - this.h / 2) / this.zoom;
|
|
this._clampCenter();
|
|
this.invalidate();
|
|
this._scheduleMoveEnd();
|
|
}
|
|
|
|
// ------------------------------------------------------------------ drawing
|
|
draw() {
|
|
const { ctx, dpr, w, h, zoom: z } = this;
|
|
if (!w || !h) return;
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
ctx.fillStyle = COLORS.void;
|
|
ctx.fillRect(0, 0, w, h);
|
|
|
|
const r = this.radius;
|
|
const tl = this.toScreen(-r - 0.5, -r - 0.5), br = this.toScreen(r + 0.5, r + 0.5);
|
|
ctx.fillStyle = COLORS.land;
|
|
ctx.fillRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
|
|
|
|
const v = this.view();
|
|
// land texture + grid
|
|
if (z >= 9) {
|
|
ctx.fillStyle = COLORS.land2;
|
|
for (let y = v.y0; y <= v.y1; y++) {
|
|
for (let x = v.x0 + (((v.x0 + y) & 1) ? 1 : 0); x <= v.x1; x += 2) {
|
|
const s = this.toScreen(x - 0.5, y - 0.5);
|
|
ctx.fillRect(s.x, s.y, z, z);
|
|
}
|
|
}
|
|
}
|
|
ctx.lineWidth = 1;
|
|
if (z >= 14) {
|
|
ctx.strokeStyle = COLORS.grid;
|
|
ctx.beginPath();
|
|
for (let x = v.x0; x <= v.x1 + 1; x++) { const s = this.toScreen(x - 0.5, 0).x; ctx.moveTo(Math.round(s) + 0.5, Math.max(0, tl.y)); ctx.lineTo(Math.round(s) + 0.5, Math.min(h, br.y)); }
|
|
for (let y = v.y0; y <= v.y1 + 1; y++) { const s = this.toScreen(0, y - 0.5).y; ctx.moveTo(Math.max(0, tl.x), Math.round(s) + 0.5); ctx.lineTo(Math.min(w, br.x), Math.round(s) + 0.5); }
|
|
ctx.stroke();
|
|
}
|
|
// every 10 tiles, so a zoomed-out map still reads as a map
|
|
ctx.strokeStyle = COLORS.grid10;
|
|
ctx.beginPath();
|
|
for (let x = Math.ceil(v.x0 / 10) * 10; x <= v.x1; x += 10) { const s = this.toScreen(x, 0).x; ctx.moveTo(Math.round(s) + 0.5, Math.max(0, tl.y)); ctx.lineTo(Math.round(s) + 0.5, Math.min(h, br.y)); }
|
|
for (let y = Math.ceil(v.y0 / 10) * 10; y <= v.y1; y += 10) { const s = this.toScreen(0, y).y; ctx.moveTo(Math.max(0, tl.x), Math.round(s) + 0.5); ctx.lineTo(Math.min(w, br.x), Math.round(s) + 0.5); }
|
|
ctx.stroke();
|
|
|
|
// specials: rural areas and incumbent sites
|
|
this.src.forEachSpecial(v.x0, v.y0, v.x1, v.y1, (sp) => {
|
|
const s = this.toScreen(sp.x, sp.y);
|
|
if (sp.type === 'rural') this._rural(s.x, s.y, sp.bonus);
|
|
else this._incumbent(s.x, s.y);
|
|
});
|
|
|
|
const towns = this.src.townsInRect(v.x0 - 1, v.y0 - 1, v.x1 + 1, v.y1 + 1);
|
|
// faint rural reach rings around your own POPs
|
|
if (z >= 10) {
|
|
ctx.save();
|
|
ctx.setLineDash([4, 5]);
|
|
ctx.strokeStyle = COLORS.reach;
|
|
for (const t of towns) {
|
|
if (!this.src.isOwn(t)) continue;
|
|
const s = this.toScreen(t.x, t.y);
|
|
ctx.beginPath(); ctx.arc(s.x, s.y, this.reachTiles * z, 0, Math.PI * 2); ctx.stroke();
|
|
}
|
|
ctx.restore();
|
|
}
|
|
for (const t of towns) this._pop(t);
|
|
|
|
// world border
|
|
ctx.strokeStyle = COLORS.border;
|
|
ctx.lineWidth = 2;
|
|
ctx.strokeRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
|
|
|
|
// selection
|
|
if (this.selected) this._selection(this.selected);
|
|
}
|
|
|
|
_rural(x, y, bonus) {
|
|
const { ctx, zoom: z } = this;
|
|
const col = COLORS[bonus] || COLORS.capital;
|
|
if (z < 6) { ctx.fillStyle = col; ctx.globalAlpha = 0.8; ctx.fillRect(x - Math.max(1, z / 2), y - Math.max(1, z / 2), Math.max(2, z), Math.max(2, z)); ctx.globalAlpha = 1; return; }
|
|
ctx.globalAlpha = 0.2; ctx.fillStyle = col;
|
|
ctx.beginPath(); ctx.arc(x, y, z * 0.46, 0, Math.PI * 2); ctx.fill();
|
|
ctx.globalAlpha = 1; ctx.strokeStyle = col; ctx.lineWidth = Math.max(1, z * 0.06);
|
|
ctx.beginPath(); ctx.arc(x, y, z * 0.46, 0, Math.PI * 2); ctx.stroke();
|
|
if (z >= 12) {
|
|
// "unconnected": a signal glyph with a cut line through it
|
|
ctx.lineWidth = Math.max(1, z * 0.07); ctx.lineCap = 'round';
|
|
for (let i = 1; i <= 2; i++) { ctx.beginPath(); ctx.arc(x, y + z * 0.14, z * 0.1 * i + z * 0.04, Math.PI * 1.2, Math.PI * 1.8); ctx.stroke(); }
|
|
ctx.fillStyle = col; ctx.beginPath(); ctx.arc(x, y + z * 0.14, z * 0.05, 0, Math.PI * 2); ctx.fill();
|
|
}
|
|
}
|
|
|
|
_incumbent(x, y) {
|
|
const { ctx, zoom: z } = this;
|
|
const s = z * 0.78;
|
|
ctx.fillStyle = COLORS.incumbentFill; ctx.strokeStyle = COLORS.incumbent; ctx.lineWidth = Math.max(1, z * 0.07);
|
|
ctx.fillRect(x - s / 2, y - s / 2, s, s);
|
|
ctx.strokeRect(x - s / 2, y - s / 2, s, s);
|
|
if (z >= 14) {
|
|
ctx.fillStyle = COLORS.incumbent;
|
|
const n = 3, nw = s / 7;
|
|
for (let i = 0; i < n; i++) ctx.fillRect(x - s / 2 + s * 0.12 + i * (s * 0.3), y - s / 2 - nw * 0.0 + 1, nw * 1.3, nw * 1.3);
|
|
ctx.fillRect(x - s * 0.12, y + s * 0.02, s * 0.24, s * 0.28);
|
|
}
|
|
}
|
|
|
|
_hex(x, y, rad) {
|
|
const { ctx } = this;
|
|
ctx.beginPath();
|
|
for (let i = 0; i < 6; i++) {
|
|
const a = Math.PI / 6 + i * Math.PI / 3;
|
|
const px = x + Math.cos(a) * rad, py = y + Math.sin(a) * rad;
|
|
if (i) ctx.lineTo(px, py); else ctx.moveTo(px, py);
|
|
}
|
|
ctx.closePath();
|
|
}
|
|
|
|
_pop(t) {
|
|
const { ctx, zoom: z } = this;
|
|
const s = this.toScreen(t.x, t.y);
|
|
if (s.x < -40 || s.y < -40 || s.x > this.w + 40 || s.y > this.h + 40) return;
|
|
const own = this.src.isOwn(t);
|
|
const col = own ? { fill: COLORS.ownFill, stroke: COLORS.own } : rivalColors(t.owner.id);
|
|
const rad = Math.max(3, z * (own ? 0.5 : 0.42));
|
|
if (own) {
|
|
ctx.save();
|
|
ctx.shadowColor = COLORS.own; ctx.shadowBlur = Math.min(22, 6 + z * 0.35);
|
|
this._hex(s.x, s.y, rad); ctx.fillStyle = col.fill; ctx.fill();
|
|
ctx.restore();
|
|
}
|
|
this._hex(s.x, s.y, rad);
|
|
ctx.fillStyle = col.fill; ctx.fill();
|
|
ctx.strokeStyle = col.stroke; ctx.lineWidth = own ? Math.max(2, z * 0.1) : Math.max(1.2, z * 0.07); ctx.stroke();
|
|
if (z >= 10) {
|
|
ctx.fillStyle = col.stroke;
|
|
ctx.beginPath(); ctx.arc(s.x, s.y, Math.max(1.5, rad * (t.isCapital ? 0.38 : 0.22)), 0, Math.PI * 2); ctx.fill();
|
|
if (t.isCapital) { ctx.strokeStyle = col.stroke; ctx.lineWidth = 1; this._hex(s.x, s.y, rad * 0.68); ctx.stroke(); }
|
|
}
|
|
if (z >= 18 && t.owner.protected) {
|
|
ctx.strokeStyle = COLORS.select; ctx.lineWidth = Math.max(1, z * 0.05); ctx.setLineDash([3, 3]);
|
|
this._hex(s.x, s.y, rad + z * 0.12); ctx.stroke(); ctx.setLineDash([]);
|
|
}
|
|
if (z >= 24 || (own && z >= 12)) {
|
|
const label = this.labelFn ? this.labelFn(t, own) : t.name;
|
|
ctx.font = (own ? '700 ' : '600 ') + Math.round(clamp(z * 0.34, 10, 14)) + 'px system-ui, sans-serif';
|
|
ctx.textAlign = 'center'; ctx.textBaseline = 'top';
|
|
ctx.lineWidth = 3; ctx.lineJoin = 'round'; ctx.strokeStyle = COLORS.halo;
|
|
ctx.strokeText(label, s.x, s.y + rad + 3);
|
|
ctx.fillStyle = own ? COLORS.own : COLORS.text;
|
|
ctx.fillText(label, s.x, s.y + rad + 3);
|
|
}
|
|
}
|
|
|
|
_selection(sel) {
|
|
const { ctx, zoom: z } = this;
|
|
const s = this.toScreen(sel.x, sel.y);
|
|
const half = Math.max(5, z * 0.52), len = Math.max(3, half * 0.5);
|
|
ctx.strokeStyle = COLORS.select; ctx.lineWidth = 2.2; ctx.lineCap = 'round'; ctx.lineJoin = 'round';
|
|
ctx.beginPath();
|
|
for (const [sx, sy] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {
|
|
const px = s.x + sx * half, py = s.y + sy * half;
|
|
ctx.moveTo(px - sx * len, py); ctx.lineTo(px, py); ctx.lineTo(px, py - sy * len);
|
|
}
|
|
ctx.stroke();
|
|
}
|
|
}
|
|
|
|
window.MapRender = { MapView, COLORS };
|
|
}());
|