"/* ----- The simulation API of Bio7 (the JavaScript version of com.eco.bio7.simulation) ----- */\nclass SimRandom {\n constructor(seed) { this.state = seed | 0; }\n nextDouble() {\n this.state = (this.state + 0x6D2B79F5) | 0;\n let t = this.state;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n }\n nextInt(n) { return Math.trunc(this.nextDouble() * n); }\n}\nconst Bio7Sim = {\n array(sizes, leaf) {\n const make = function (d) {\n const n = sizes[d];\n if (d === sizes.length - 1) {\n if (leaf === \'null\') return new Array(n).fill(null);\n if (leaf === \'false\') return new Array(n).fill(false);\n return new (globalThis[leaf])(n);\n }\n const a = new Array(n);\n for (let i = 0; i < n; i++) a[i] = make(d + 1);\n return a;\n };\n return make(0);\n },\n remove(list, o) { const i = list.indexOf(o); if (i < 0) return false; list.splice(i, 1); return true; },\n removeIf(list, f) { let n = 0; for (let i = list.length - 1; i >= 0; i--) if (f(list[i])) { list.splice(i, 1); n++; } return n > 0; },\n getOrDefault(map, k, d) { return map.has(k) ? map.get(k) : d; },\n str(x) {\n if (typeof x !== \'number\') return String(x);\n if (!isFinite(x)) return String(x);\n if (x === 0) return Object.is(x, -0) ? \'-0.0\' : \'0.0\';\n const a = Math.abs(x);\n if (a >= 1e7 || a < 1e-3) { const e = x.toExponential().split(\'e\'); return (e[0].includes(\'.\') ? e[0] : e[0] + \'.0\') + \'E\' + parseInt(e[1], 10); }\n const s = String(x);\n return s.includes(\'.\') ? s : s + \'.0\';\n },\n floorMod(a, b) { return ((a % b) + b) % b; },\n rint(x) { const f = Math.floor(x), r = x - f; return r > 0.5 ? f + 1 : r < 0.5 ? f : (f % 2 === 0 ? f : f + 1); },\n arraycopy(src, sp, dst, dp, n) { if (src === dst && sp < dp) { for (let i = n - 1; i >= 0; i--) dst[dp + i] = src[sp + i]; } else { for (let i = 0; i < n; i++) dst[dp + i] = src[sp + i]; } },\n copyOf(a, n) { const b = new a.constructor(n); for (let i = 0; i < Math.min(n, a.length); i++) b[i] = a[i]; return b; },\n /* IntStream.range(a, b) (also .parallel(), it runs as a loop in the browser) */\n intRange(from, to) {\n return { parallel() { return this; }, sequential() { return this; },\n forEach(f) { for (let i = from; i < to; i++) f(i); },\n sum() { let s = 0; for (let i = from; i < to; i++) s += i; return s; } };\n },\n shuffle(list, model) { for (let i = list.length - 1; i > 0; i--) { const j = model.randomInt(i + 1), t = list[i]; list[i] = list[j]; list[j] = t; } },\n format(f, ...args) {\n let k = 0;\n return f.replace(/%(\\.(\\d+))?([dsf%n])/g, function (m, p, digits, c) {\n if (c === \'%\') return \'%\'; if (c === \'n\') return \'\\n\';\n const v = args[k++];\n if (c === \'f\') return Number(v).toFixed(digits === undefined ? 6 : parseInt(digits, 10));\n if (c === \'d\') return String(Math.trunc(v));\n return String(v);\n });\n }\n};\nclass Simulation {\n constructor() { this.t = 0; this._random = new SimRandom(42); this._sim = null; }\n init() {}\n step() {}\n seed(s) { this._random = new SimRandom(s); this._seed = s | 0; }\n randomAt(index, k = 0) {\n let h = Math.imul(this._seed === undefined ? 42 : this._seed, 0x9E3779B1) ^ Math.imul(this.t, 0x85EBCA77) ^ Math.imul(index, 0xC2B2AE3D) ^ Math.imul(k, 0x27D4EB2F);\n h ^= h >>> 16; h = Math.imul(h, 0x7FEB352D); h ^= h >>> 15; h = Math.imul(h, 0x846CA68B); h ^= h >>> 16;\n return (h >>> 0) / 4294967296;\n }\n random(min, max) { const r = this._random.nextDouble(); return min === undefined ? r : min + r * (max - min); }\n randomInt(a, b) { return b === undefined ? this._random.nextInt(a) : a + this._random.nextInt(b - a); }\n chance(p) { return this._random.nextDouble() < p; }\n randomGaussian(mean, sd) {\n let u, v, s;\n do { u = 2 * this._random.nextDouble() - 1; v = 2 * this._random.nextDouble() - 1; s = u * u + v * v; } while (s >= 1 || s === 0);\n const g = u * Math.sqrt(-2 * Simulation._log(s) / s);\n return mean === undefined ? g : mean + sd * g;\n }\n randomExponential(rate) { return -Simulation._log(1 - this._random.nextDouble()) / rate; }\n randomPoisson(lambda) {\n if (lambda > 30) return Math.max(0, Math.round(lambda + Math.sqrt(lambda) * this.randomGaussian()));\n const limit = Simulation._exp(-lambda); let p = 1, k = 0;\n do { k++; p *= this._random.nextDouble(); } while (p > limit);\n return k - 1;\n }\n randomElement(list) { return list.length === 0 ? null : list[this._random.nextInt(list.length)]; }\n shuffle(list) { for (let i = list.length - 1; i > 0; i--) { const j = this._random.nextInt(i + 1), t = list[i]; list[i] = list[j]; list[j] = t; } }\n static _log(x) {\n if (!(x > 0)) return x === 0 ? -Infinity : NaN;\n if (x === Infinity) return x;\n let e = 0, m = x;\n while (m >= 1.4142135623730951) { m *= 0.5; e++; }\n while (m < 0.7071067811865476) { m *= 2; e--; }\n const z = (m - 1) / (m + 1), z2 = z * z; let t = 0;\n for (let k = 27; k >= 1; k -= 2) t = t * z2 + 1.0 / k;\n return 2 * z * t + e * 0.6931471805599453;\n }\n static _exp(x) {\n if (x !== x) return x;\n if (x > 709.7) return Infinity;\n if (x < -745.2) return 0;\n const k = Math.floor(x / 0.6931471805599453 + 0.5), r = (x - k * 0.6931471803691238) - k * 1.9082149292705877e-10;\n let t = 1;\n for (let n = 20; n >= 1; n--) t = 1 + t * r / n;\n return t * Math.pow(2, k);\n }\n static clamp(v, min, max) { return Math.max(min, Math.min(max, v)); }\n static lerp(a, b, t) { return a + (b - a) * t; }\n static mapRange(v, inMin, inMax, outMin, outMax) { return outMin + (v - inMin) * (outMax - outMin) / (inMax - inMin); }\n static wrapAround(v, size) { return ((v % size) + size) % size; }\n static sq(v) { return v * v; }\n static distance(x1, y1, x2, y2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); }\n static distance3(x1, y1, z1, x2, y2, z2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); }\n static logistic(x) { return 1 / (1 + Simulation._exp(-x)); }\n static logisticGrowth(n, r, k) { return r * n * (1 - n / k); }\n static sum(a) { let s = 0; for (let i = 0; i < a.length; i++) s += a[i]; return s; }\n static mean(a) { return a.length === 0 ? 0 : Simulation.sum(a) / a.length; }\n static variance(a) { const n = a.length; if (n < 2) return 0; const m = Simulation.mean(a); let s = 0; for (let i = 0; i < n; i++) s += (a[i] - m) * (a[i] - m); return s / (n - 1); }\n static sd(a) { return Math.sqrt(Simulation.variance(a)); }\n static min(a) { let m = Infinity; for (let i = 0; i < a.length; i++) m = Math.min(m, a[i]); return m; }\n static max(a) { let m = -Infinity; for (let i = 0; i < a.length; i++) m = Math.max(m, a[i]); return m; }\n static rk4(f, t, y, dt) {\n const n = y.length, k1 = f(t, y), y2 = new Float64Array(n), y3 = new Float64Array(n), y4 = new Float64Array(n), r = new Float64Array(n);\n for (let i = 0; i < n; i++) y2[i] = y[i] + dt / 2 * k1[i];\n const k2 = f(t + dt / 2, y2);\n for (let i = 0; i < n; i++) y3[i] = y[i] + dt / 2 * k2[i];\n const k3 = f(t + dt / 2, y3);\n for (let i = 0; i < n; i++) y4[i] = y[i] + dt * k3[i];\n const k4 = f(t + dt, y4);\n for (let i = 0; i < n; i++) r[i] = y[i] + dt / 6 * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]);\n return r;\n }\n chart(a, b, c) { if (!this._sim) return; if (c === undefined) this._sim.chart(a, b); else this._sim.chartLine(a, b, this.t, c); }\n chartXY(chart, name, x, y) { if (this._sim) this._sim.chartXY(chart, name, x, y); }\n chartBars(chart, labels, values) { if (this._sim) this._sim.chartBars(chart, Array.from(labels), Array.from(values)); }\n stop() { this._stopped = true; }\n isStopped() { return !!this._stopped; }\n record(name, value) { (this._records || (this._records = {}))[name] = value; }\n _shape(k, p, s, f, t, w, a, z) {\n /* 3D shapes in the 2D view of a grid: not drawn, a message once (as in Bio7) */\n if (!this._warned3d && [\'box\', \'sphere\', \'point3\', \'path3\', \'polygon3\', \'pillar\'].indexOf(k) >= 0 && !(this instanceof AgentModel) && !(this instanceof GeoAgentModel)) {\n this._warned3d = true; console.warn(\'The model draws a 3D shape (\' + k + \'): 3D shapes are drawn only in the 3D view of an agent model (the 2D view of a grid ignores them)\');\n }\n const d = { k: k, p: p, s: s || null, f: f || null, t: t === undefined ? null : t, w: w === undefined ? 1 : w, a: a || 0 }; if (z) d.z = z; (this._drawing || (this._drawing = [])).push(d); }\n drawLine(x1, y1, x2, y2, color, width, z) { this._shape(\'line\', [x1, y1, x2, y2], color, null, null, width, 0, z); }\n drawLine3(x1, y1, z1, x2, y2, z2, color, width) { this._shape(\'line3\', [x1, y1, z1, x2, y2, z2], color, null, null, width); }\n drawPath(xs, ys, color, width, z) { const p = []; for (let i = 0; i < Math.min(xs.length, ys.length); i++) p.push(xs[i], ys[i]); this._shape(\'path\', p, color, null, null, width, 0, z); }\n drawPolygon(xs, ys, fill, stroke, z) { const p = []; for (let i = 0; i < Math.min(xs.length, ys.length); i++) p.push(xs[i], ys[i]); this._shape(\'polygon\', p, stroke, fill, null, 1, 0, z); }\n drawCircle(x, y, radius, fill, stroke, z) { this._shape(\'circle\', [x, y, radius], stroke, fill, null, 1, 0, z); }\n drawRect(x, y, w, h, fill, stroke, z) { this._shape(\'rect\', [x, y, w, h], stroke, fill, null, 1, 0, z); }\n drawText(x, y, text, color, size, z) { this._shape(\'text\', [x, y], color, null, String(text), size, 0, z); }\n drawImage(name, x, y, size, angle, z) { this._shape(\'image\', [x, y], null, null, name, size, angle, z); }\n drawPoint3(x, y, z, size, color) { this._shape(\'point3\', [x, y, z, size], null, color, null, 1); }\n drawPath3(xs, ys, zs, color, width) { const p = []; for (let i = 0; i < Math.min(xs.length, ys.length, zs.length); i++) p.push(xs[i], ys[i], zs[i]); this._shape(\'path3\', p, color, null, null, width); }\n drawBox(x, y, z, w, h, d, fill, stroke) { this._shape(\'box\', [x, y, z, w, h, d], stroke, fill, null, 1); }\n drawSphere(x, y, z, radius, fill) { this._shape(\'sphere\', [x, y, z, radius], null, fill, null, 1); }\n drawPolygon3(xs, ys, zs, fill, stroke) { const p = []; for (let i = 0; i < Math.min(xs.length, ys.length, zs.length); i++) p.push(xs[i], ys[i], zs[i]); this._shape(\'polygon3\', p, stroke, fill, null, 1); }\n drawPillar(x, y, radius, height, color) { this._shape(\'pillar\', [x, y, radius, height], null, color, null, 1); }\n loadImage(name, file) {}\n loadModel(name, file) {}\n randomBinomial(n, p) { let k = 0; for (let i = 0; i < n; i++) if (this.random() < p) k++; return k; }\n randomWeighted(w) {\n let total = 0; for (let i = 0; i < w.length; i++) total += Math.max(0, w[i]);\n if (total <= 0) return -1;\n let r = this.random() * total;\n for (let i = 0; i < w.length; i++) { r -= Math.max(0, w[i]); if (r < 0) return i; }\n return w.length - 1;\n }\n static dot(a, b) { let s = 0; for (let i = 0; i < Math.min(a.length, b.length); i++) s += a[i] * b[i]; return s; }\n static norm(a) { return Math.sqrt(Simulation.dot(a, a)); }\n static matMul(a, b) { const n = a.length, m = b[0].length, k = b.length, r = []; for (let i = 0; i < n; i++) { r.push(new Float64Array(m)); for (let j = 0; j < m; j++) { let s = 0; for (let q = 0; q < k; q++) s += a[i][q] * b[q][j]; r[i][j] = s; } } return r; }\n static matVec(a, v) { const r = new Float64Array(a.length); for (let i = 0; i < a.length; i++) r[i] = Simulation.dot(a[i], v); return r; }\n static transpose(a) { const r = []; for (let j = 0; j < a[0].length; j++) { r.push(new Float64Array(a.length)); for (let i = 0; i < a.length; i++) r[j][i] = a[i][j]; } return r; }\n static solve(a, b) {\n const n = b.length, m = [];\n for (let i = 0; i < n; i++) { const row = new Float64Array(n + 1); for (let j = 0; j < n; j++) row[j] = a[i][j]; row[n] = b[i]; m.push(row); }\n for (let c = 0; c < n; c++) {\n let p = c; for (let r = c + 1; r < n; r++) if (Math.abs(m[r][c]) > Math.abs(m[p][c])) p = r;\n const tmp = m[c]; m[c] = m[p]; m[p] = tmp;\n if (m[c][c] === 0) return new Float64Array(n).fill(NaN);\n for (let r = c + 1; r < n; r++) { const f = m[r][c] / m[c][c]; for (let j = c; j <= n; j++) m[r][j] -= f * m[c][j]; }\n }\n const x = new Float64Array(n);\n for (let i = n - 1; i >= 0; i--) { let s = m[i][n]; for (let j = i + 1; j < n; j++) s -= m[i][j] * x[j]; x[i] = s / m[i][i]; }\n return x;\n }\n static inverse(a) { const n = a.length, r = []; for (let i = 0; i < n; i++) r.push(new Float64Array(n)); for (let j = 0; j < n; j++) { const e = new Float64Array(n); e[j] = 1; const col = Simulation.solve(a, e); for (let i = 0; i < n; i++) r[i][j] = col[i]; } return r; }\n static determinant(a) {\n const n = a.length, m = a.map(function (r) { return Float64Array.from(r); }); let det = 1;\n for (let c = 0; c < n; c++) {\n let p = c; for (let r = c + 1; r < n; r++) if (Math.abs(m[r][c]) > Math.abs(m[p][c])) p = r;\n if (m[p][c] === 0) return 0;\n if (p !== c) { const tmp = m[c]; m[c] = m[p]; m[p] = tmp; det = -det; }\n det *= m[c][c];\n for (let r = c + 1; r < n; r++) { const f = m[r][c] / m[c][c]; for (let j = c; j < n; j++) m[r][j] -= f * m[c][j]; }\n }\n return det;\n }\n static linearRegression(x, y) {\n const n = Math.min(x.length, y.length); let mx = 0, my = 0;\n for (let i = 0; i < n; i++) { mx += x[i]; my += y[i]; }\n mx /= n; my /= n; let sxy = 0, sxx = 0, syy = 0;\n for (let i = 0; i < n; i++) { sxy += (x[i] - mx) * (y[i] - my); sxx += (x[i] - mx) * (x[i] - mx); syy += (y[i] - my) * (y[i] - my); }\n const b = sxx === 0 ? 0 : sxy / sxx;\n return Float64Array.of(my - b * mx, b, sxx === 0 || syy === 0 ? 0 : sxy * sxy / (sxx * syy));\n }\n static correlation(x, y) { const r = Simulation.linearRegression(x, y); return r[1] < 0 ? -Math.sqrt(r[2]) : Math.sqrt(r[2]); }\n static quantile(values, p) {\n if (values.length === 0) return NaN;\n const v = Float64Array.from(values).sort(), h = (v.length - 1) * Math.max(0, Math.min(1, p)), lo = Math.floor(h), hi = Math.min(v.length - 1, lo + 1);\n return v[lo] + (h - lo) * (v[hi] - v[lo]);\n }\n static median(values) { return Simulation.quantile(values, 0.5); }\n static normalPdf(x, mean, sd) { const z = (x - mean) / sd; return Simulation._exp(-0.5 * z * z) / (sd * 2.5066282746310002); }\n static normalCdf(x, mean, sd) {\n const z = (x - mean) / (sd * 1.4142135623730951), a = Math.abs(z), t = 1 / (1 + 0.5 * a);\n const erfc = t * Simulation._exp(-a * a - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 + t * (-0.18628806 + t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 + t * (-0.82215223 + t * 0.17087277)))))))));\n return z >= 0 ? 1 - 0.5 * erfc : 0.5 * erfc;\n }\n static _corner(x, y) { let h = (Math.imul(x, 374761393) + Math.imul(y, 668265263)) | 0; h = Math.imul(h ^ (h >>> 13), 1274126177); h = h ^ (h >>> 16); return (h & 0xffffff) / 16777216; }\n static noise(x, y) {\n const ix = Math.floor(x) | 0, iy = Math.floor(y) | 0, fx = x - ix, fy = y - iy, sx = fx * fx * (3 - 2 * fx), sy = fy * fy * (3 - 2 * fy);\n const a = Simulation._corner(ix, iy), b = Simulation._corner(ix + 1, iy), c = Simulation._corner(ix, iy + 1), d = Simulation._corner(ix + 1, iy + 1);\n return a + (b - a) * sx + (c - a) * sy + (a - b - c + d) * sx * sy;\n }\n static fractalNoise(x, y, octaves) {\n let sum = 0, amplitude = 1, total = 0, f = 1;\n for (let k = 0; k < octaves; k++) { sum += amplitude * Simulation.noise(x * f + k * 17.3, y * f + k * 11.1); total += amplitude; amplitude *= 0.5; f *= 2; }\n return sum / total;\n }\n static cosDeg(d) { return GeoAgentModel.cosDeg(d); }\n static sinDeg(d) { return GeoAgentModel.sinDeg(d); }\n static headingOf(dx, dy) { const h = Math.atan2(dx, dy) * 57.29577951308232; return h < 0 ? h + 360 : h; }\n /* The shapes of the step and (on top) the agents of a grid */\n _drawn() { const d = this._drawing || []; return this._gridAgents ? d.concat(Bio7GridAgents.shapes(this.agents)) : d; }\n _prepare() {}\n _show(sim) {}\n}\n/* An agent on a grid (as GridAgent of Java) */\nclass GridAgent {\n constructor(x, y) { this.x = x === undefined ? 0.5 : x; this.y = y === undefined ? 0.5 : y; this.heading = 0; this.color = \'#e53935\'; this.image = null; this.size = 0.8; this.shape = \'circle\'; }\n cellX() { return Math.floor(this.x); }\n cellY() { return Math.floor(this.y); }\n}\n/* The agents of the grid models (as GridAgents of Java: the same numbers) */\nconst Bio7GridAgents = {\n keep(a, w, h, wrap) { if (wrap) { a.x = ((a.x % w) + w) % w; a.y = ((a.y % h) + h) % h; } else { a.x = Math.max(0, Math.min(w - 1e-9, a.x)); a.y = Math.max(0, Math.min(h - 1e-9, a.y)); } },\n delta(d, size, wrap) { return wrap ? d - size * Bio7GridAgents.rint(d / size) : d; },\n rint(v) { const f = Math.floor(v), r = v - f; return r > 0.5 ? f + 1 : r < 0.5 ? f : (f % 2 === 0 ? f : f + 1); },\n shapes(agents) {\n const out = [];\n for (const a of agents) {\n if (!a) continue;\n const size = a.size > 0 ? a.size : 0.8;\n if (a.image) { out.push({ k: \'image\', p: [a.x, a.y], s: null, f: null, t: a.image, w: size, a: a.heading }); continue; }\n const color = a.color || \'#e53935\', shape = a.shape || \'circle\';\n if (shape === \'arrow\') {\n const r = size / 2, d = [0, 140, 180, 220], f = [1, 1, 0.35, 1], p = [];\n for (let k = 0; k < 4; k++) p.push(a.x + r * f[k] * Simulation.sinDeg(a.heading + d[k]), a.y - r * f[k] * Simulation.cosDeg(a.heading + d[k]));\n out.push({ k: \'polygon\', p: p, s: \'#00000066\', f: color, t: null, w: 1, a: 0 });\n } else if (shape === \'square\') out.push({ k: \'rect\', p: [a.x - size / 2, a.y - size / 2, size, size], s: \'#00000066\', f: color, t: null, w: 1, a: 0 });\n else out.push({ k: \'circle\', p: [a.x, a.y, size / 2], s: \'#00000066\', f: color, t: null, w: 1, a: 0 });\n }\n return out;\n },\n methods: {\n addAgent(a) { this.agents.push(a); return a; },\n removeAgent(a) { const i = this.agents.indexOf(a); if (i >= 0) this.agents.splice(i, 1); },\n forward(a, d) { a.x += d * Simulation.sinDeg(a.heading); a.y -= d * Simulation.cosDeg(a.heading); Bio7GridAgents.keep(a, this.width, this.height, this.wrap); },\n moveTo(a, x, y) { a.x = x; a.y = y; Bio7GridAgents.keep(a, this.width, this.height, this.wrap); },\n face(a, x, y) { const dx = Bio7GridAgents.delta(x - a.x, this.width, this.wrap), dy = Bio7GridAgents.delta(y - a.y, this.height, this.wrap); if (dx !== 0 || dy !== 0) a.heading = Simulation.headingOf(dx, -dy); },\n agentDistance(a, b) { const dx = Bio7GridAgents.delta(b.x - a.x, this.width, this.wrap), dy = Bio7GridAgents.delta(b.y - a.y, this.height, this.wrap); return Math.sqrt(dx * dx + dy * dy); },\n agentsAt(x, y) { return this.agents.filter(function (a) { return a.cellX() === x && a.cellY() === y; }); },\n countAgentsAt(x, y) { return this.agentsAt(x, y).length; },\n agentsNear(a, r) { const self = this; return this.agents.filter(function (o) { return o !== a && self.agentDistance(a, o) <= r; }); }\n }\n};\nclass GridModel extends Simulation {\n constructor() { super(); this.width = 0; this.height = 0; this.cells = new Int32Array(0); this.next = new Int32Array(0); this.wrap = true; this._palette = [\'#ffffff\', \'#000000\']; this.agents = []; this._gridAgents = true; }\n _prepare() { this.agents = []; }\n size(w, h) { this.width = w; this.height = h; this.cells = new Int32Array(w * h); this.next = new Int32Array(w * h); }\n palette(...colors) { this._palette = colors; }\n get(x, y) {\n const w = this.width, h = this.height;\n if (this.wrap) { x = ((x % w) + w) % w; y = ((y % h) + h) % h; }\n else if (x < 0 || y < 0 || x >= w || y >= h) return 0;\n return this.cells[y * w + x];\n }\n set(x, y, state) { this.cells[y * this.width + x] = state; }\n setNext(x, y, state) { this.next[y * this.width + x] = state; }\n swap() { const t = this.cells; this.cells = this.next; this.next = t; }\n countNeighbors(x, y, state) {\n let n = 0;\n for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) if ((dx !== 0 || dy !== 0) && this.get(x + dx, y + dy) === state) n++;\n return n;\n }\n count4(x, y, state) { return (this.get(x - 1, y) === state ? 1 : 0) + (this.get(x + 1, y) === state ? 1 : 0) + (this.get(x, y - 1) === state ? 1 : 0) + (this.get(x, y + 1) === state ? 1 : 0); }\n count(state) { let n = 0; for (let i = 0; i < this.cells.length; i++) if (this.cells[i] === state) n++; return n; }\n _show(sim) { sim.width = this.width; sim.height = this.height; sim.cells = this.cells; sim.palette = this._palette; }\n}\nclass ValueGridModel extends Simulation {\n constructor() { super(); this.width = 0; this.height = 0; this.values = new Float64Array(0); this.next = new Float64Array(0); this.wrap = true; this._min = 0; this._max = 1; this._ramp = [\'#440154\', \'#3b528b\', \'#21908c\', \'#5dc963\', \'#fde725\']; this.agents = []; this._gridAgents = true; }\n _prepare() { this.agents = []; }\n size(w, h) { this.width = w; this.height = h; this.values = new Float64Array(w * h); this.next = new Float64Array(w * h); }\n range(min, max) { this._min = min; this._max = max; }\n colorMap(...colors) { if (colors.length >= 2) this._ramp = colors; }\n get(x, y) {\n const w = this.width, h = this.height;\n if (this.wrap) { x = ((x % w) + w) % w; y = ((y % h) + h) % h; }\n else if (x < 0 || y < 0 || x >= w || y >= h) return 0;\n return this.values[y * w + x];\n }\n set(x, y, value) { this.values[y * this.width + x] = value; }\n setNext(x, y, value) { this.next[y * this.width + x] = value; }\n swap() { const t = this.values; this.values = this.next; this.next = t; }\n sum4(x, y) { return this.get(x - 1, y) + this.get(x + 1, y) + this.get(x, y - 1) + this.get(x, y + 1); }\n laplacian(x, y) { return this.sum4(x, y) - 4 * this.get(x, y); }\n sum8(x, y) { return this.sum4(x, y) + this.get(x - 1, y - 1) + this.get(x + 1, y - 1) + this.get(x - 1, y + 1) + this.get(x + 1, y + 1); }\n mean() { let s = 0; for (let i = 0; i < this.values.length; i++) s += this.values[i]; return this.values.length === 0 ? 0 : s / this.values.length; }\n _show(sim) { sim.width = this.width; sim.height = this.height; sim.cells = this.values; sim.palette = null; sim.min = this._min; sim.max = this._max; sim.ramp = this._ramp; }\n}\nSimulation.DEFAULT_SEED = 42;\nValueGridModel.VIRIDIS = [\'#440154\', \'#3b528b\', \'#21908c\', \'#5dc963\', \'#fde725\'];\nObject.assign(GridModel.prototype, Bio7GridAgents.methods);\nObject.assign(ValueGridModel.prototype, Bio7GridAgents.methods);\nclass GeoGridModel extends GridModel {\n constructor() { super(); this.originX = 0; this.originY = 0; this.cellWidth = 1; this.cellHeight = -1; this.cellSizeMeters = 1; this.south = 0; this.west = 0; this.north = 0; this.east = 0; this.crs = \'\'; this._work = {}; this._shown = null; }\n _prepare() {\n this.agents = [];\n const g = GEO;\n [\'originX\', \'originY\', \'cellWidth\', \'cellHeight\', \'cellSizeMeters\', \'south\', \'west\', \'north\', \'east\'].forEach(k => { this[k] = g[k]; });\n this.size(g.width, g.height);\n this._work = {};\n for (const k in g.layers) this._work[k] = Float64Array.from(g.layers[k]);\n this._shown = null;\n }\n layer(name) { return this._work[name] || null; }\n showValues(values, min, max, ...colors) { this._shown = { values: values, min: min, max: max, colors: colors.length >= 2 ? colors : ValueGridModel.VIRIDIS }; }\n showStates() { this._shown = null; }\n _inside(v, n) { return v >= 0 && v < n ? v : -1; }\n colAt(lon, lat) {\n const M = 20037508.342789244;\n if (GEO.mode === \'mercator\') return this._inside(Math.floor((lon * M / 180 - this.originX) / this.cellWidth), this.width);\n if (GEO.mode === \'geographic\') return this._inside(Math.floor((lon - this.originX) / this.cellWidth), this.width);\n return this._inside(Math.floor((lon - this.west) / (this.east - this.west) * this.width), this.width);\n }\n rowAt(lon, lat) {\n const M = 20037508.342789244;\n if (GEO.mode === \'mercator\') return this._inside(Math.floor((Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180) * M / 180 - this.originY) / this.cellHeight), this.height);\n if (GEO.mode === \'geographic\') return this._inside(Math.floor((lat - this.originY) / this.cellHeight), this.height);\n return this._inside(Math.floor((this.north - lat) / (this.north - this.south) * this.height), this.height);\n }\n lon(x) {\n const M = 20037508.342789244;\n if (GEO.mode === \'mercator\') return (this.originX + (x + 0.5) * this.cellWidth) / M * 180;\n if (GEO.mode === \'geographic\') return this.originX + (x + 0.5) * this.cellWidth;\n return this.west + (x + 0.5) / this.width * (this.east - this.west);\n }\n lat(y) {\n const M = 20037508.342789244;\n if (GEO.mode === \'mercator\') { const my = (this.originY + (y + 0.5) * this.cellHeight) / M * 180; return 180 / Math.PI * (2 * Math.atan(Math.exp(my * Math.PI / 180)) - Math.PI / 2); }\n if (GEO.mode === \'geographic\') return this.originY + (y + 0.5) * this.cellHeight;\n return this.north - (y + 0.5) / this.height * (this.north - this.south);\n }\n value(layer, x, y) { x = Math.max(0, Math.min(this.width - 1, x)); y = Math.max(0, Math.min(this.height - 1, y)); return layer[y * this.width + x]; }\n gradient(layer, x, y) {\n const dzdx = (this.value(layer, x + 1, y) - this.value(layer, x - 1, y)) / (2 * this.cellSizeMeters);\n const dzdy = (this.value(layer, x, y + 1) - this.value(layer, x, y - 1)) / (2 * this.cellSizeMeters);\n return Math.sqrt(dzdx * dzdx + dzdy * dzdy);\n }\n slopeDegrees(layer, x, y) { return Math.atan(this.gradient(layer, x, y)) * 180 / Math.PI; }\n _show(sim) {\n if (!this._shown) { super._show(sim); sim.ramp = null; return; }\n sim.width = this.width; sim.height = this.height; sim.cells = this._shown.values; sim.palette = null; sim.min = this._shown.min; sim.max = this._shown.max; sim.ramp = this._shown.colors;\n }\n}\n/* The layers of a geospatial model (base64 of little endian doubles) */\nfunction bio7Doubles(text) { const bin = atob(text), bytes = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); return new Float64Array(bytes.buffer); }\nclass GeoAgent {\n constructor(lon = 0, lat = 0) { this.lon = lon; this.lat = lat; this.color = null; this.size = 0; this.label = null; this.altitude = 0; this.heading = NaN; this.image = null; this.model = null; this.animation = null; }\n}\nclass GeoAgentModel extends Simulation {\n constructor() { super(); this.agents = []; this.south = -60; this.west = -180; this.north = 75; this.east = 180; this.agentSize = 4; this.agentColor = \'#d62728\'; this.agentShape = \'point\'; this.agentSizeMeters = 300; this.viewPitch = 0; }\n static cosDeg(deg) {\n let d = deg % 360;\n if (d > 180) d -= 360; else if (d < -180) d += 360;\n d = Math.abs(d);\n return d <= 90 ? GeoAgentModel.cosLat(d) : -GeoAgentModel.cosLat(180 - d);\n }\n static sinDeg(deg) { return GeoAgentModel.cosDeg(90 - deg); }\n static _compass(dlon, dlat, lat) { const h = Math.atan2(dlon * Math.cos(lat * Math.PI / 180), dlat) * 180 / Math.PI; return h < 0 ? h + 360 : h; }\n area(south, west, north, east) { this.south = south; this.west = west; this.north = north; this.east = east; }\n distanceKm(a, b, c, d) {\n if (c === undefined) return GeoAgentModel.distanceKm(a.lon, a.lat, b.lon, b.lat);\n return GeoAgentModel.distanceKm(a, b, c, d);\n }\n static distanceKm(lon1, lat1, lon2, lat2) {\n const p1 = lat1 * (Math.PI / 180), p2 = lat2 * (Math.PI / 180), dp = p2 - p1, dl = (lon2 - lon1) * (Math.PI / 180);\n const h = Math.sin(dp / 2) * Math.sin(dp / 2) + Math.cos(p1) * Math.cos(p2) * Math.sin(dl / 2) * Math.sin(dl / 2);\n return 2 * GeoAgentModel.EARTH_RADIUS * Math.asin(Math.min(1, Math.sqrt(h)));\n }\n static cosLat(latDeg) {\n const x = latDeg * (Math.PI / 180), x2 = x * x, C = GeoAgentModel.COS;\n let c = C[11];\n for (let k = 10; k >= 0; k--) c = c * x2 + C[k];\n return c;\n }\n move(a, eastKm, northKm) {\n a.lat += northKm / 110.574;\n a.lon += eastKm / (111.320 * GeoAgentModel.cosLat(a.lat));\n if (a.lon > 180) a.lon -= 360; else if (a.lon < -180) a.lon += 360;\n a.lat = Math.max(-89.9, Math.min(89.9, a.lat));\n }\n moveTowards(a, lon, lat, km) {\n const eastKm = (lon - a.lon) * 111.320 * GeoAgentModel.cosLat((lat + a.lat) / 2), northKm = (lat - a.lat) * 110.574;\n const d = Math.sqrt(eastKm * eastKm + northKm * northKm);\n if (d <= km) { a.lon = lon; a.lat = lat; return true; }\n this.move(a, eastKm / d * km, northKm / d * km);\n return false;\n }\n neighborsKm(agent, km) { const found = []; for (const o of this.agents) if (o !== agent && this.distanceKm(agent, o) <= km) found.push(o); return found; }\n _prepare() { this.agents = []; }\n _show(sim) {\n const n = this.agents.length, g = { lon: new Array(n), lat: new Array(n), colors: new Array(n), sizes: new Array(n), labels: new Array(n) };\n for (let i = 0; i < n; i++) {\n const a = this.agents[i];\n g.lon[i] = a.lon; g.lat[i] = a.lat; g.colors[i] = a.color || this.agentColor; g.sizes[i] = a.size > 0 ? a.size : this.agentSize; g.labels[i] = a.label;\n /* An image (and its heading: given or the direction of the last movement) */\n if (a.image || a.model) { if (!g.images) { g.images = new Array(n).fill(null); g.headings = new Array(n).fill(0); } g.images[i] = a.image; if (a._plon !== undefined && (a._plon !== a.lon || a._plat !== a.lat)) a._h = GeoAgentModel._compass(a.lon - a._plon, a.lat - a._plat, a.lat); g.headings[i] = isNaN(a.heading) ? (a._h || 0) : a.heading; }\n a._plon = a.lon; a._plat = a.lat;\n }\n sim.cells = null; sim.geoAgents = g; sim.mapBounds = [[this.south, this.west], [this.north, this.east]];\n }\n}\nGeoAgentModel.EARTH_RADIUS = 6371.0088;\nGeoAgentModel.COS = [1.0, -0.5, 0.041666666666666664, -0.001388888888888889, 2.48015873015873e-05, -2.755731922398589e-07, 2.08767569878681e-09, -1.1470745597729725e-11, 4.779477332387385e-14, -1.5619206968586225e-16, 4.110317623312165e-19, -8.896791392450574e-22];\nclass Agent {\n constructor(x = 0, y = 0, z = 0) { this.x = x; this.y = y; this.z = z; this.color = null; this.image = null; this.model = null; this.animation = null; this.heading = NaN; this.size = 0; }\n}\n/* The spatial index of the agents (as SpatialIndex of Java: the same results in the same order) */\nclass SpatialIndex {\n constructor(agents, cellSize) {\n this.agents = agents.slice(); this.cell = cellSize > 0 ? cellSize : 1; const n = this.agents.length;\n this.x = new Float64Array(n); this.y = new Float64Array(n); this.z = new Float64Array(n); this.cells = new Map(); this.position = new Map();\n for (let i = 0; i < n; i++) {\n const a = this.agents[i]; this.x[i] = a.x; this.y[i] = a.y; this.z[i] = a.z;\n if (!this.position.has(a)) this.position.set(a, i);\n const k = this._key(this._c(a.x), this._c(a.y), this._c(a.z)); let list = this.cells.get(k); if (!list) { list = []; this.cells.set(k, list); } list.push(i);\n }\n }\n _c(v) { return Math.floor(v / this.cell); }\n _key(cx, cy, cz) { return cx + \',\' + cy + \',\' + cz; }\n _search(px, py, pz, radius, skip) {\n const r2 = radius * radius, found = [];\n const x0 = this._c(px - radius), x1 = this._c(px + radius), y0 = this._c(py - radius), y1 = this._c(py + radius), z0 = this._c(pz - radius), z1 = this._c(pz + radius);\n for (let cx = x0; cx <= x1; cx++) for (let cy = y0; cy <= y1; cy++) for (let cz = z0; cz <= z1; cz++) {\n const list = this.cells.get(this._key(cx, cy, cz)); if (!list) continue;\n for (const i of list) { if (i === skip) continue; const dx = this.x[i] - px, dy = this.y[i] - py, dz = this.z[i] - pz; if (dx * dx + dy * dy + dz * dz <= r2) found.push(i); }\n }\n return found.sort(function (a, b) { return a - b; });\n }\n _index(agent) { const i = this.position.get(agent); return i === undefined ? -1 : i; }\n near(px, py, pz, radius) { const ag = this.agents; return this._search(px, py, pz, radius, -1).map(function (i) { return ag[i]; }); }\n neighbors(agent, radius) { const i = this._index(agent), ag = this.agents; return (i < 0 ? this._search(agent.x, agent.y, agent.z, radius, -1) : this._search(this.x[i], this.y[i], this.z[i], radius, i)).map(function (k) { return ag[k]; }); }\n nearest(agent, radius) {\n const self = this._index(agent), px = self < 0 ? agent.x : this.x[self], py = self < 0 ? agent.y : this.y[self], pz = self < 0 ? agent.z : this.z[self];\n let best = -1, bestD = Number.MAX_VALUE;\n for (const i of this._search(px, py, pz, radius, self)) { const dx = this.x[i] - px, dy = this.y[i] - py, dz = this.z[i] - pz, d2 = dx * dx + dy * dy + dz * dz; if (d2 < bestD) { bestD = d2; best = i; } }\n return best < 0 ? null : this.agents[best];\n }\n forEachPair(radius, action) { for (let i = 0; i < this.agents.length; i++) for (const j of this._search(this.x[i], this.y[i], this.z[i], radius, i)) if (j > i) action(this.agents[i], this.agents[j]); }\n size() { return this.agents.length; }\n}\nclass AgentModel extends Simulation {\n constructor() { super(); this.agents = []; this.boxWidth = 100; this.boxHeight = 100; this.boxDepth = 100; this.agentRadius = 0; this.agentColor = \'#1f77b4\';\n this.agentShape = \'sphere\'; this.floor = \'none\'; this.shadows = false; this.followAgent = -1; this._scenery = []; this.linkColor = \'#8a94a6\'; this.linkWidth = 1; this._links = new Map(); }\n index(cellSize) { return new SpatialIndex(this.agents, cellSize); }\n /* The network (as AgentModel of Java): an agent -> its linked agents and the color of the link */\n link(a, b, color) {\n if (!a || !b || a === b) return;\n if (!this._links.has(a)) this._links.set(a, new Map()); if (!this._links.has(b)) this._links.set(b, new Map());\n this._links.get(a).set(b, color === undefined ? null : color); this._links.get(b).set(a, color === undefined ? null : color);\n }\n unlink(a, b) { const la = this._links.get(a), lb = this._links.get(b); if (la) la.delete(b); if (lb) lb.delete(a); }\n removeLinks(a) { const la = this._links.get(a); this._links.delete(a); if (la) for (const o of la.keys()) { const lo = this._links.get(o); if (lo) lo.delete(a); } }\n clearLinks() { this._links = new Map(); }\n linked(a, b) { const la = this._links.get(a); return !!la && la.has(b); }\n linkNeighbors(a) { const la = this._links.get(a); return la ? Array.from(la.keys()) : []; }\n degree(a) { const la = this._links.get(a); return la ? la.size : 0; }\n linkColorOf(a, b) { const la = this._links.get(a); return la && la.has(b) ? la.get(b) : null; }\n setLinkColor(a, b, color) { if (this.linked(a, b)) this.link(a, b, color); }\n _indices() { const idx = new Map(); for (let i = 0; i < this.agents.length; i++) if (!idx.has(this.agents[i])) idx.set(this.agents[i], i); return idx; }\n getLinks() {\n const idx = this._indices(), out = [];\n for (let i = 0; i < this.agents.length; i++) { const la = this._links.get(this.agents[i]); if (!la) continue; for (const o of la.keys()) { const j = idx.get(o); if (j !== undefined && j > i) out.push(i, j); } }\n return out;\n }\n linkCount() { return this.getLinks().length / 2; }\n links() { return this.getLinks(); }\n cameraFollow(index) { this.followAgent = index < 0 ? -1 : index; }\n neighborPairs(distance) {\n const idx = this._indices(), pairs = [];\n this.index(distance).forEachPair(distance, function (a, b) { const i = idx.get(a), j = idx.get(b); pairs.push([Math.min(i, j), Math.max(i, j)]); });\n pairs.sort(function (p, q) { return p[0] !== q[0] ? p[0] - q[0] : p[1] - q[1]; });\n const out = []; for (const p of pairs) out.push(p[0], p[1]); return out;\n }\n linkRandom(p) { const n = this.agents.length; for (let i = 0; i < n; i++) for (let j = i + 1; j < n; j++) if (this.random() < p) this.link(this.agents[i], this.agents[j]); }\n linkSmallWorld(k, p) {\n const n = this.agents.length, half = Math.max(1, Math.trunc(k / 2)); if (n < 2) return;\n for (let i = 0; i < n; i++) for (let d = 1; d <= half; d++) this.link(this.agents[i], this.agents[(i + d) % n]);\n for (let i = 0; i < n; i++) for (let d = 1; d <= half; d++) {\n const a = this.agents[i], b = this.agents[(i + d) % n];\n if (this.random() < p && this.linked(a, b)) { for (let t = 0; t < n; t++) { const c = this.agents[this.randomInt(n)]; if (c !== a && !this.linked(a, c)) { this.unlink(a, b); this.link(a, c); break; } } }\n }\n }\n linkScaleFree(m) {\n const n = this.agents.length; m = Math.max(1, m); const ends = [], start = Math.min(n, m + 1);\n for (let i = 0; i < start; i++) for (let j = i + 1; j < start; j++) { this.link(this.agents[i], this.agents[j]); ends.push(i, j); }\n for (let i = start; i < n; i++) {\n const chosen = [];\n while (chosen.length < Math.min(m, i)) { const j = ends.length === 0 ? this.randomInt(i) : ends[this.randomInt(ends.length)]; if (chosen.indexOf(j) < 0) chosen.push(j); }\n for (const j of chosen) { this.link(this.agents[i], this.agents[j]); ends.push(i, j); }\n }\n }\n linkNearby(radius) { const self = this; this.index(radius).forEachPair(radius, function (a, b) { self.link(a, b); }); }\n components() {\n const out = [], seen = new Set(), idx = this._indices();\n for (const a of this.agents) {\n if (seen.has(a)) continue;\n const group = [], queue = [a]; seen.add(a);\n while (queue.length) { const c = queue.shift(); group.push(idx.get(c)); for (const o of this.linkNeighbors(c)) if (idx.has(o) && !seen.has(o)) { seen.add(o); queue.push(o); } }\n group.sort(function (x, y) { return x - y; }); out.push(group.map((i) => this.agents[i]));\n }\n return out;\n }\n shortestPath(a, b) {\n if (a === b) return 0;\n const dist = new Map([[a, 0]]), queue = [a];\n while (queue.length) { const c = queue.shift(); for (const o of this.linkNeighbors(c)) if (!dist.has(o)) { dist.set(o, dist.get(c) + 1); if (o === b) return dist.get(o); queue.push(o); } }\n return -1;\n }\n clustering(a) {\n const nb = this.linkNeighbors(a), k = nb.length; if (k < 2) return 0;\n let closed = 0; for (let i = 0; i < k; i++) for (let j = i + 1; j < k; j++) if (this.linked(nb[i], nb[j])) closed++;\n return 2.0 * closed / (k * (k - 1.0));\n }\n layoutCircle() {\n const n = this.agents.length, cx = this.boxWidth / 2, cy = this.boxHeight / 2, r = Math.min(this.boxWidth, this.boxHeight) * 0.42;\n for (let i = 0; i < n; i++) { const a = 360.0 * i / Math.max(1, n), ag = this.agents[i]; ag.x = cx + r * Simulation.cosDeg(a); ag.y = cy + r * Simulation.sinDeg(a); ag.z = this.boxDepth / 2; }\n }\n layoutSpring(iterations) {\n const n = this.agents.length; if (n === 0) return;\n const w = this.boxWidth, h = this.boxHeight, k = Math.sqrt(w * h / n), pairs = this.getLinks(), px = new Float64Array(n), py = new Float64Array(n);\n for (let i = 0; i < n; i++) { px[i] = this.agents[i].x; py[i] = this.agents[i].y; }\n for (let it = 0; it < iterations; it++) {\n const temp = Math.max(w, h) * 0.1 * (1 - it / iterations), dx = new Float64Array(n), dy = new Float64Array(n);\n for (let i = 0; i < n; i++) for (let j = i + 1; j < n; j++) {\n let ex = px[i] - px[j], ey = py[i] - py[j], d = Math.sqrt(ex * ex + ey * ey);\n if (d < 1e-9) { ex = 0.01 * k * Simulation.cosDeg(37.0 * (i + j)); ey = 0.01 * k * Simulation.sinDeg(37.0 * (i + j)); d = 0.01 * k; }\n const f = k * k / d; dx[i] += ex / d * f; dy[i] += ey / d * f; dx[j] -= ex / d * f; dy[j] -= ey / d * f;\n }\n for (let p = 0; p + 1 < pairs.length; p += 2) {\n const i = pairs[p], j = pairs[p + 1], ex = px[i] - px[j], ey = py[i] - py[j], d = Math.max(1e-9, Math.sqrt(ex * ex + ey * ey)), f = d * d / k;\n dx[i] -= ex / d * f; dy[i] -= ey / d * f; dx[j] += ex / d * f; dy[j] += ey / d * f;\n }\n for (let i = 0; i < n; i++) { const d = Math.sqrt(dx[i] * dx[i] + dy[i] * dy[i]); if (d > 0) { const s = Math.min(d, temp) / d; px[i] = Math.max(0, Math.min(w, px[i] + dx[i] * s)); py[i] = Math.max(0, Math.min(h, py[i] + dy[i] * s)); } }\n }\n for (let i = 0; i < n; i++) { this.agents[i].x = px[i]; this.agents[i].y = py[i]; }\n }\n box(w, h, d) { this.boxWidth = w; this.boxHeight = h; this.boxDepth = d; }\n /* The objects of the environment (3D models which stay until they are removed) */\n placeModel(model, x, y, z, heading, size, animation) { this._scenery.push([model, x, y, z, heading || 0, size || 0, animation || null]); return this._scenery.length - 1; }\n removeModel(index) { if (index >= 0 && index < this._scenery.length) this._scenery.splice(index, 1); }\n clearModels() { this._scenery = []; }\n distance(a, b) { return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)); }\n agentDistance(a, b) { return this.distance(a, b); }\n neighbors(agent, radius) {\n const found = [], r2 = radius * radius;\n for (const o of this.agents) { if (o !== agent) { const dx = o.x - agent.x, dy = o.y - agent.y, dz = o.z - agent.z; if (dx * dx + dy * dy + dz * dz <= r2) found.push(o); } }\n return found;\n }\n nearest(agent, radius) {\n let best = null, r2 = radius * radius;\n for (const o of this.agents) { if (o !== agent) { const dx = o.x - agent.x, dy = o.y - agent.y, dz = o.z - agent.z, d2 = dx * dx + dy * dy + dz * dz; if (d2 <= r2) { r2 = d2; best = o; } } }\n return best;\n }\n keepInBox(a) { a.x = Math.max(0, Math.min(this.boxWidth, a.x)); a.y = Math.max(0, Math.min(this.boxHeight, a.y)); a.z = Math.max(0, Math.min(this.boxDepth, a.z)); }\n wrapInBox(a) { a.x = ((a.x % this.boxWidth) + this.boxWidth) % this.boxWidth; a.y = ((a.y % this.boxHeight) + this.boxHeight) % this.boxHeight; a.z = Math.max(0, Math.min(this.boxDepth, a.z)); }\n _prepare() { this.agents = []; this._scenery = []; this._links = new Map(); }\n _show(sim) {\n /* The shape, the floor and the shadows of the model; the objects of the environment */\n sim.applyModelScene(this.agentShape, this.floor, this.shadows ? 1 : 0);\n sim.scenery = sim.sceneryOf(this._scenery.map(function (p) { return p[0]; }), [].concat.apply([], this._scenery.map(function (p) { return p.slice(1, 6); })), this._scenery.map(function (p) { return p[6]; }));\n const n = this.agents.length, xyz = new Array(n * 3), colors = new Array(n * 3), dirs = new Array(n * 3);\n let own = false;\n const base = this.agentColor;\n for (let i = 0; i < n; i++) {\n const a = this.agents[i];\n xyz[i * 3] = a.x; xyz[i * 3 + 1] = a.y; xyz[i * 3 + 2] = a.z;\n /* The direction of the movement (for cones and gliders): the last move of the agent */\n if (a._px !== undefined) { const dx = a.x - a._px, dy = a.y - a._py, dz = a.z - a._pz; if (dx * dx + dy * dy + dz * dz > 1e-18 && Math.abs(dx) + Math.abs(dy) < Math.max(this.boxWidth, this.boxHeight) / 2) { a._dx = dx; a._dy = dy; a._dz = dz; } }\n a._px = a.x; a._py = a.y; a._pz = a.z;\n dirs[i * 3] = a._dx || 0; dirs[i * 3 + 1] = a._dy || 0; dirs[i * 3 + 2] = a._dz || 0;\n const c = a.color || base;\n if (a.color) own = true;\n colors[i * 3] = parseInt(c.substr(1, 2), 16) / 255; colors[i * 3 + 1] = parseInt(c.substr(3, 2), 16) / 255; colors[i * 3 + 2] = parseInt(c.substr(5, 2), 16) / 255;\n }\n const size = Math.max(this.boxWidth, this.boxHeight, this.boxDepth);\n sim.box = [this.boxWidth, this.boxHeight, this.boxDepth];\n sim.points = { xyz: xyz, dirs: dirs, colors: own ? colors : null, color: base, radius: this.agentRadius > 0 ? this.agentRadius : size * 0.01 };\n /* Images and 3D models (glTF) of the agents */\n if (this.agents.some(function (a) { return a.image || a.model; })) {\n const p = sim.points; p.images = []; p.models = []; p.animations = []; p.headings = []; p.sizes = [];\n for (const a of this.agents) { p.images.push(a.image); p.models.push(a.model); p.animations.push(a.animation); p.headings.push(isNaN(a.heading) ? NaN : a.heading); p.sizes.push(a.size); }\n }\n /* The links of the network */\n const pairs = this.getLinks(), lc = [];\n for (let k = 0; k < pairs.length; k += 2) lc.push(this.linkColorOf(this.agents[pairs[k]], this.agents[pairs[k + 1]]) || \'\');\n sim.links = pairs.length ? { pairs: pairs, colors: lc, color: this.linkColor || \'\', width: this.linkWidth } : null;\n }\n}\n"