@flare-engine/simulation 0.2.0 → 0.3.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.
package/dist/index.js CHANGED
@@ -1,644 +1,614 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/agent/stigmergy.ts
3
+ const TWO_PI$1 = Math.PI * 2;
4
+ const SENSE_DEFAULTS = {
5
+ sensorAngle: .5,
6
+ sensorDist: 9,
7
+ rotateAngle: .45
9
8
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
9
+ /** Read the field at one sensor: `offset` rad off the agent heading, `dist` px ahead. */
10
+ function senseAt(field, x, y, heading, offset, dist) {
11
+ const a = heading + offset;
12
+ return field.sample(x + Math.cos(a) * dist, y + Math.sin(a) * dist);
13
+ }
14
+ /** Jones turn decision from three sensor readings: −1 turn left, 0 straight, +1 turn right.
15
+ * `rand` ∈ [0,1) breaks the symmetric "both sides brighter" case. */
16
+ function decideTurn(left, center, right, rand) {
17
+ if (center > left && center > right) return 0;
18
+ if (center < left && center < right) return rand < .5 ? -1 : 1;
19
+ if (left < right) return 1;
20
+ return -1;
21
+ }
22
+ function createStigmergy(field, config, rng) {
23
+ const { count } = config;
24
+ const px = new Float32Array(count);
25
+ const py = new Float32Array(count);
26
+ const heading = new Float32Array(count);
27
+ const fw = field.w;
28
+ const fh = field.h;
29
+ for (let i = 0; i < count; i++) {
30
+ px[i] = rng.float(0, fw);
31
+ py[i] = rng.float(0, fh);
32
+ heading[i] = rng.float(0, TWO_PI$1);
33
+ }
34
+ function wrapX(x, w) {
35
+ if (field.edge === "toroidal") return (x % w + w) % w;
36
+ return x < 0 ? 0 : x >= w ? w - 1 : x;
37
+ }
38
+ function wrapY(y, h) {
39
+ if (field.edge === "toroidal") return (y % h + h) % h;
40
+ return y < 0 ? 0 : y >= h ? h - 1 : y;
41
+ }
42
+ return {
43
+ count,
44
+ px,
45
+ py,
46
+ heading,
47
+ update() {
48
+ const w = field.w;
49
+ const h = field.h;
50
+ const sa = config.sensorAngle;
51
+ const sd = config.sensorDist;
52
+ const turnRate = config.rotateAngle;
53
+ const chaos = config.chaos;
54
+ const stepSize = config.stepSize;
55
+ const deposit = config.depositAmount;
56
+ for (let i = 0; i < count; i++) {
57
+ const x = px[i] ?? 0;
58
+ const y = py[i] ?? 0;
59
+ let hd = heading[i] ?? 0;
60
+ const cF = senseAt(field, x, y, hd, 0, sd);
61
+ const cL = senseAt(field, x, y, hd, -sa, sd);
62
+ const cR = senseAt(field, x, y, hd, sa, sd);
63
+ hd += decideTurn(cL, cF, cR, rng.float(0, 1)) * turnRate;
64
+ hd += (rng.float(0, 1) * 2 - 1) * chaos;
65
+ if (config.onSense) hd += config.onSense(i, x, y, hd);
66
+ const nx = wrapX(x + Math.cos(hd) * stepSize, w);
67
+ const ny = wrapY(y + Math.sin(hd) * stepSize, h);
68
+ px[i] = nx;
69
+ py[i] = ny;
70
+ heading[i] = hd;
71
+ field.deposit(nx, ny, deposit);
72
+ if (config.onMove) config.onMove(i, nx, ny, hd);
73
+ }
74
+ },
75
+ forEachAgent(cb, stride = 1) {
76
+ const s = Math.max(1, Math.floor(stride));
77
+ for (let i = 0; i < count; i += s) cb(px[i] ?? 0, py[i] ?? 0);
78
+ },
79
+ resize(width, height) {
80
+ const newW = Math.max(1, width);
81
+ const newH = Math.max(1, height);
82
+ for (let i = 0; i < count; i++) {
83
+ px[i] = wrapX(px[i] ?? 0, newW);
84
+ py[i] = wrapY(py[i] ?? 0, newH);
85
+ }
86
+ }
87
+ };
88
+ }
89
+ //#endregion
90
+ //#region src/field/scalar-field.ts
91
+ const clamp01 = (v) => v < 0 ? 0 : v > 1 ? 1 : v;
92
+ const smooth = (t) => {
93
+ const c = clamp01(t);
94
+ return c * c * (3 - 2 * c);
17
95
  };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- SENSE_DEFAULTS: () => SENSE_DEFAULTS,
24
- createNBodySystem: () => createNBodySystem,
25
- createRipplePool: () => createRipplePool,
26
- createScalarField: () => createScalarField,
27
- createStigmergy: () => createStigmergy,
28
- decideTurn: () => decideTurn,
29
- leapfrogInto: () => leapfrogInto,
30
- senseAt: () => senseAt,
31
- verletStep: () => verletStep
32
- });
33
- module.exports = __toCommonJS(index_exports);
34
-
35
- // src/integrate/nbody.ts
96
+ function createScalarField(width, height, cellSize, edgeMode = "toroidal") {
97
+ const cell = Math.max(1, cellSize);
98
+ let w = Math.max(1, width);
99
+ let h = Math.max(1, height);
100
+ let cols = Math.max(1, Math.ceil(w / cell));
101
+ let rows = Math.max(1, Math.ceil(h / cell));
102
+ let a = new Float32Array(cols * rows);
103
+ let b = new Float32Array(cols * rows);
104
+ let wrapCol;
105
+ let wrapRow;
106
+ function rebuildEdgeFns() {
107
+ if (edgeMode === "toroidal") {
108
+ wrapCol = (c) => (c % cols + cols) % cols;
109
+ wrapRow = (r) => (r % rows + rows) % rows;
110
+ } else {
111
+ wrapCol = (c) => c < 0 ? 0 : c >= cols ? cols - 1 : c;
112
+ wrapRow = (r) => r < 0 ? 0 : r >= rows ? rows - 1 : r;
113
+ }
114
+ }
115
+ rebuildEdgeFns();
116
+ const field = {
117
+ get w() {
118
+ return w;
119
+ },
120
+ get h() {
121
+ return h;
122
+ },
123
+ get cols() {
124
+ return cols;
125
+ },
126
+ get rows() {
127
+ return rows;
128
+ },
129
+ get cell() {
130
+ return cell;
131
+ },
132
+ get edge() {
133
+ return edgeMode;
134
+ },
135
+ sample(x, y) {
136
+ const gx = x / cell - .5;
137
+ const gy = y / cell - .5;
138
+ const x0 = Math.floor(gx);
139
+ const y0 = Math.floor(gy);
140
+ const fx = gx - x0;
141
+ const fy = gy - y0;
142
+ const xa = wrapCol(x0);
143
+ const xb = wrapCol(x0 + 1);
144
+ const ya = wrapRow(y0);
145
+ const yb = wrapRow(y0 + 1);
146
+ const a00 = a[ya * cols + xa] ?? 0;
147
+ const a10 = a[ya * cols + xb] ?? 0;
148
+ const a01 = a[yb * cols + xa] ?? 0;
149
+ const a11 = a[yb * cols + xb] ?? 0;
150
+ const top = a00 + (a10 - a00) * fx;
151
+ return top + (a01 + (a11 - a01) * fx - top) * fy;
152
+ },
153
+ gradient(x, y, out) {
154
+ out.x = field.sample(x + cell, y) - field.sample(x - cell, y);
155
+ out.y = field.sample(x, y + cell) - field.sample(x, y - cell);
156
+ },
157
+ deposit(x, y, amount) {
158
+ const cx = wrapCol(Math.floor(x / cell));
159
+ const i = wrapRow(Math.floor(y / cell)) * cols + cx;
160
+ a[i] = (a[i] ?? 0) + amount;
161
+ },
162
+ erode(x, y, radiusPx, mul) {
163
+ const cr = Math.max(1, Math.ceil(radiusPx / cell));
164
+ const ccx = Math.floor(x / cell);
165
+ const ccy = Math.floor(y / cell);
166
+ const r2 = cr * cr;
167
+ for (let dy = -cr; dy <= cr; dy++) for (let dx = -cr; dx <= cr; dx++) {
168
+ if (dx * dx + dy * dy > r2) continue;
169
+ const i = wrapRow(ccy + dy) * cols + wrapCol(ccx + dx);
170
+ a[i] = (a[i] ?? 0) * mul;
171
+ }
172
+ },
173
+ step(diffuseRate, decayRate, maxVal) {
174
+ for (let r = 0; r < rows; r++) {
175
+ const base = r * cols;
176
+ for (let c = 0; c < cols; c++) {
177
+ const cl = edgeMode === "toroidal" ? c === 0 ? cols - 1 : c - 1 : c === 0 ? 0 : c - 1;
178
+ const cr2 = edgeMode === "toroidal" ? c === cols - 1 ? 0 : c + 1 : c === cols - 1 ? cols - 1 : c + 1;
179
+ b[base + c] = ((a[base + cl] ?? 0) + (a[base + c] ?? 0) + (a[base + cr2] ?? 0)) / 3;
180
+ }
181
+ }
182
+ for (let r = 0; r < rows; r++) {
183
+ const ru = edgeMode === "toroidal" ? r === 0 ? rows - 1 : r - 1 : r === 0 ? 0 : r - 1;
184
+ const rd = edgeMode === "toroidal" ? r === rows - 1 ? 0 : r + 1 : r === rows - 1 ? rows - 1 : r + 1;
185
+ const base = r * cols;
186
+ const upBase = ru * cols;
187
+ const dnBase = rd * cols;
188
+ for (let c = 0; c < cols; c++) {
189
+ const blurred = ((b[upBase + c] ?? 0) + (b[base + c] ?? 0) + (b[dnBase + c] ?? 0)) / 3;
190
+ const orig = a[base + c] ?? 0;
191
+ const next = (orig + (blurred - orig) * diffuseRate) * decayRate;
192
+ a[base + c] = next > maxVal ? maxVal : next;
193
+ }
194
+ }
195
+ },
196
+ meanValue(stride) {
197
+ const s = Math.max(1, Math.floor(stride));
198
+ let sum = 0;
199
+ let n = 0;
200
+ for (let i = 0; i < a.length; i += s) {
201
+ sum += a[i] ?? 0;
202
+ n++;
203
+ }
204
+ return n > 0 ? sum / n : 0;
205
+ },
206
+ maxValue(stride) {
207
+ const s = Math.max(1, Math.floor(stride));
208
+ if (Math.ceil(a.length / s) === 0) return 0;
209
+ let m = Number.NEGATIVE_INFINITY;
210
+ for (let i = 0; i < a.length; i += s) {
211
+ const v = a[i] ?? 0;
212
+ if (v > m) m = v;
213
+ }
214
+ return m;
215
+ },
216
+ localMean(x, y, radiusPx) {
217
+ const cr = Math.max(1, Math.ceil(radiusPx / cell));
218
+ const ccx = Math.floor(x / cell);
219
+ const ccy = Math.floor(y / cell);
220
+ let sum = 0;
221
+ let n = 0;
222
+ for (let dy = -cr; dy <= cr; dy++) for (let dx = -cr; dx <= cr; dx++) {
223
+ sum += a[wrapRow(ccy + dy) * cols + wrapCol(ccx + dx)] ?? 0;
224
+ n++;
225
+ }
226
+ return n > 0 ? sum / n : 0;
227
+ },
228
+ toRGBA(out, norm, low, mid, hi, knot) {
229
+ const inv = norm > 1e-6 ? 1 / norm : 0;
230
+ const lr = low[0] ?? 0;
231
+ const lg = low[1] ?? 0;
232
+ const lb = low[2] ?? 0;
233
+ const mr = mid[0] ?? 0;
234
+ const mg = mid[1] ?? 0;
235
+ const mb = mid[2] ?? 0;
236
+ const hr = hi[0] ?? 0;
237
+ const hg = hi[1] ?? 0;
238
+ const hb = hi[2] ?? 0;
239
+ const kr = knot[0] ?? 0;
240
+ const kg = knot[1] ?? 0;
241
+ const kb = knot[2] ?? 0;
242
+ for (let i = 0; i < a.length; i++) {
243
+ const v = smooth((a[i] ?? 0) * inv);
244
+ let r;
245
+ let g;
246
+ let bl;
247
+ if (v < .5) {
248
+ const t = v * 2;
249
+ r = lr + (mr - lr) * t;
250
+ g = lg + (mg - lg) * t;
251
+ bl = lb + (mb - lb) * t;
252
+ } else if (v < .85) {
253
+ const t = (v - .5) / .35;
254
+ r = mr + (hr - mr) * t;
255
+ g = mg + (hg - mg) * t;
256
+ bl = mb + (hb - mb) * t;
257
+ } else {
258
+ const t = (v - .85) / .15;
259
+ r = hr + (kr - hr) * t;
260
+ g = hg + (kg - hg) * t;
261
+ bl = hb + (kb - hb) * t;
262
+ }
263
+ const o = i << 2;
264
+ const rv = r * v * 255;
265
+ const gv = g * v * 255;
266
+ const bv = bl * v * 255;
267
+ out[o] = rv < 0 ? 0 : rv > 255 ? 255 : rv | 0;
268
+ out[o + 1] = gv < 0 ? 0 : gv > 255 ? 255 : gv | 0;
269
+ out[o + 2] = bv < 0 ? 0 : bv > 255 ? 255 : bv | 0;
270
+ out[o + 3] = 255;
271
+ }
272
+ },
273
+ resize(width2, height2) {
274
+ w = Math.max(1, width2);
275
+ h = Math.max(1, height2);
276
+ cols = Math.max(1, Math.ceil(w / cell));
277
+ rows = Math.max(1, Math.ceil(h / cell));
278
+ a = new Float32Array(cols * rows);
279
+ b = new Float32Array(cols * rows);
280
+ rebuildEdgeFns();
281
+ }
282
+ };
283
+ return field;
284
+ }
285
+ //#endregion
286
+ //#region src/integrate/nbody.ts
36
287
  function createNBodySystem(capacity) {
37
- const cap = Math.max(1, Math.floor(capacity));
38
- const px = new Float32Array(cap);
39
- const py = new Float32Array(cap);
40
- const vx = new Float32Array(cap);
41
- const vy = new Float32Array(cap);
42
- const mass = new Float32Array(cap);
43
- const tag = new Float32Array(cap);
44
- const ax = new Float32Array(cap);
45
- const ay = new Float32Array(cap);
46
- const axOld = new Float32Array(cap);
47
- const ayOld = new Float32Array(cap);
48
- let count = 0;
49
- let primed = false;
50
- const computeAccel = (g, soft, gr, rs) => {
51
- const soft2 = soft * soft;
52
- for (let i = 0; i < count; i++) {
53
- ax[i] = 0;
54
- ay[i] = 0;
55
- }
56
- for (let i = 0; i < count; i++) {
57
- const xi = px[i] ?? 0;
58
- const yi = py[i] ?? 0;
59
- const mi = mass[i] ?? 0;
60
- for (let j = i + 1; j < count; j++) {
61
- const dx = (px[j] ?? 0) - xi;
62
- const dy = (py[j] ?? 0) - yi;
63
- const d2 = dx * dx + dy * dy + soft2;
64
- const invD = 1 / Math.sqrt(d2);
65
- const invD3 = g * invD * invD * invD;
66
- const mj = mass[j] ?? 0;
67
- ax[i] = (ax[i] ?? 0) + dx * invD3 * mj;
68
- ay[i] = (ay[i] ?? 0) + dy * invD3 * mj;
69
- ax[j] = (ax[j] ?? 0) - dx * invD3 * mi;
70
- ay[j] = (ay[j] ?? 0) - dy * invD3 * mi;
71
- }
72
- }
73
- if (gr > 0 && count > 1) {
74
- const cxp = px[0] ?? 0;
75
- const cyp = py[0] ?? 0;
76
- const beta = gr * g * (mass[0] ?? 0) * rs * rs;
77
- for (let i = 1; i < count; i++) {
78
- const dx = (px[i] ?? 0) - cxp;
79
- const dy = (py[i] ?? 0) - cyp;
80
- const r2 = dx * dx + dy * dy + soft2;
81
- const invr2 = 1 / r2;
82
- const k = beta * invr2 * invr2 * Math.sqrt(invr2);
83
- ax[i] = (ax[i] ?? 0) - dx * k;
84
- ay[i] = (ay[i] ?? 0) - dy * k;
85
- }
86
- }
87
- };
88
- return {
89
- capacity: cap,
90
- get count() {
91
- return count;
92
- },
93
- px,
94
- py,
95
- vx,
96
- vy,
97
- mass,
98
- tag,
99
- maxMass() {
100
- let m = 0;
101
- for (let i = 0; i < count; i++) {
102
- const v = mass[i] ?? 0;
103
- if (v > m) m = v;
104
- }
105
- return m;
106
- },
107
- add(x, y, vx0, vy0, m, t = 0) {
108
- if (count >= cap) return -1;
109
- const i = count++;
110
- px[i] = x;
111
- py[i] = y;
112
- vx[i] = vx0;
113
- vy[i] = vy0;
114
- mass[i] = m;
115
- tag[i] = t;
116
- primed = false;
117
- return i;
118
- },
119
- step(dt, g, soft, gr = 0, rs = 0) {
120
- if (count === 0 || dt <= 0) return;
121
- if (!primed) {
122
- computeAccel(g, soft, gr, rs);
123
- primed = true;
124
- }
125
- for (let i = 0; i < count; i++) {
126
- const axi = ax[i] ?? 0;
127
- const ayi = ay[i] ?? 0;
128
- px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + 0.5 * axi * dt * dt;
129
- py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + 0.5 * ayi * dt * dt;
130
- axOld[i] = axi;
131
- ayOld[i] = ayi;
132
- }
133
- computeAccel(g, soft, gr, rs);
134
- for (let i = 0; i < count; i++) {
135
- vx[i] = (vx[i] ?? 0) + 0.5 * ((axOld[i] ?? 0) + (ax[i] ?? 0)) * dt;
136
- vy[i] = (vy[i] ?? 0) + 0.5 * ((ayOld[i] ?? 0) + (ay[i] ?? 0)) * dt;
137
- }
138
- },
139
- totalEnergy(g, soft, gr = 0, rs = 0) {
140
- const soft2 = soft * soft;
141
- let ke = 0;
142
- for (let i = 0; i < count; i++) {
143
- const m = mass[i] ?? 0;
144
- const vxi = vx[i] ?? 0;
145
- const vyi = vy[i] ?? 0;
146
- ke += 0.5 * m * (vxi * vxi + vyi * vyi);
147
- }
148
- let pe = 0;
149
- for (let i = 0; i < count; i++) {
150
- for (let j = i + 1; j < count; j++) {
151
- const dx = (px[j] ?? 0) - (px[i] ?? 0);
152
- const dy = (py[j] ?? 0) - (py[i] ?? 0);
153
- const d = Math.sqrt(dx * dx + dy * dy + soft2);
154
- pe -= g * (mass[i] ?? 0) * (mass[j] ?? 0) / d;
155
- }
156
- }
157
- if (gr > 0 && count > 1) {
158
- const cxp = px[0] ?? 0;
159
- const cyp = py[0] ?? 0;
160
- const beta = gr * g * (mass[0] ?? 0) * rs * rs;
161
- for (let i = 1; i < count; i++) {
162
- const dx = (px[i] ?? 0) - cxp;
163
- const dy = (py[i] ?? 0) - cyp;
164
- const u = dx * dx + dy * dy + soft2;
165
- pe -= beta * (mass[i] ?? 0) / (3 * u * Math.sqrt(u));
166
- }
167
- }
168
- return ke + pe;
169
- },
170
- centerOfMass(out) {
171
- let mx = 0;
172
- let my = 0;
173
- let mt = 0;
174
- for (let i = 0; i < count; i++) {
175
- const m = mass[i] ?? 0;
176
- mx += (px[i] ?? 0) * m;
177
- my += (py[i] ?? 0) * m;
178
- mt += m;
179
- }
180
- out.x = mt > 0 ? mx / mt : 0;
181
- out.y = mt > 0 ? my / mt : 0;
182
- },
183
- translate(dx, dy) {
184
- for (let i = 0; i < count; i++) {
185
- px[i] = (px[i] ?? 0) + dx;
186
- py[i] = (py[i] ?? 0) + dy;
187
- }
188
- },
189
- absorb(into, victim) {
190
- if (into === victim || into < 0 || victim < 0 || into >= count || victim >= count) return;
191
- const mi = mass[into] ?? 0;
192
- const mj = mass[victim] ?? 0;
193
- const M = mi + mj;
194
- if (M <= 0) return;
195
- px[into] = ((px[into] ?? 0) * mi + (px[victim] ?? 0) * mj) / M;
196
- py[into] = ((py[into] ?? 0) * mi + (py[victim] ?? 0) * mj) / M;
197
- vx[into] = ((vx[into] ?? 0) * mi + (vx[victim] ?? 0) * mj) / M;
198
- vy[into] = ((vy[into] ?? 0) * mi + (vy[victim] ?? 0) * mj) / M;
199
- mass[into] = M;
200
- const last = count - 1;
201
- if (victim !== last) {
202
- px[victim] = px[last] ?? 0;
203
- py[victim] = py[last] ?? 0;
204
- vx[victim] = vx[last] ?? 0;
205
- vy[victim] = vy[last] ?? 0;
206
- mass[victim] = mass[last] ?? 0;
207
- tag[victim] = tag[last] ?? 0;
208
- }
209
- count--;
210
- primed = false;
211
- },
212
- closestPair(out) {
213
- out.i = -1;
214
- out.j = -1;
215
- out.sep = Number.POSITIVE_INFINITY;
216
- for (let i = 0; i < count; i++) {
217
- for (let j = i + 1; j < count; j++) {
218
- const dx = (px[j] ?? 0) - (px[i] ?? 0);
219
- const dy = (py[j] ?? 0) - (py[i] ?? 0);
220
- const d = Math.sqrt(dx * dx + dy * dy);
221
- if (d < out.sep) {
222
- out.sep = d;
223
- out.i = i;
224
- out.j = j;
225
- }
226
- }
227
- }
228
- }
229
- };
288
+ const cap = Math.max(1, Math.floor(capacity));
289
+ const px = new Float32Array(cap);
290
+ const py = new Float32Array(cap);
291
+ const vx = new Float32Array(cap);
292
+ const vy = new Float32Array(cap);
293
+ const mass = new Float32Array(cap);
294
+ const tag = new Float32Array(cap);
295
+ const ax = new Float32Array(cap);
296
+ const ay = new Float32Array(cap);
297
+ const axOld = new Float32Array(cap);
298
+ const ayOld = new Float32Array(cap);
299
+ let count = 0;
300
+ let primed = false;
301
+ const computeAccel = (g, soft, gr, rs) => {
302
+ const soft2 = soft * soft;
303
+ for (let i = 0; i < count; i++) {
304
+ ax[i] = 0;
305
+ ay[i] = 0;
306
+ }
307
+ for (let i = 0; i < count; i++) {
308
+ const xi = px[i] ?? 0;
309
+ const yi = py[i] ?? 0;
310
+ const mi = mass[i] ?? 0;
311
+ for (let j = i + 1; j < count; j++) {
312
+ const dx = (px[j] ?? 0) - xi;
313
+ const dy = (py[j] ?? 0) - yi;
314
+ const d2 = dx * dx + dy * dy + soft2;
315
+ const invD = 1 / Math.sqrt(d2);
316
+ const invD3 = g * invD * invD * invD;
317
+ const mj = mass[j] ?? 0;
318
+ ax[i] = (ax[i] ?? 0) + dx * invD3 * mj;
319
+ ay[i] = (ay[i] ?? 0) + dy * invD3 * mj;
320
+ ax[j] = (ax[j] ?? 0) - dx * invD3 * mi;
321
+ ay[j] = (ay[j] ?? 0) - dy * invD3 * mi;
322
+ }
323
+ }
324
+ if (gr > 0 && count > 1) {
325
+ const cxp = px[0] ?? 0;
326
+ const cyp = py[0] ?? 0;
327
+ const beta = gr * g * (mass[0] ?? 0) * rs * rs;
328
+ for (let i = 1; i < count; i++) {
329
+ const dx = (px[i] ?? 0) - cxp;
330
+ const dy = (py[i] ?? 0) - cyp;
331
+ const invr2 = 1 / (dx * dx + dy * dy + soft2);
332
+ const k = beta * invr2 * invr2 * Math.sqrt(invr2);
333
+ ax[i] = (ax[i] ?? 0) - dx * k;
334
+ ay[i] = (ay[i] ?? 0) - dy * k;
335
+ }
336
+ }
337
+ };
338
+ return {
339
+ capacity: cap,
340
+ get count() {
341
+ return count;
342
+ },
343
+ px,
344
+ py,
345
+ vx,
346
+ vy,
347
+ mass,
348
+ tag,
349
+ maxMass() {
350
+ let m = 0;
351
+ for (let i = 0; i < count; i++) {
352
+ const v = mass[i] ?? 0;
353
+ if (v > m) m = v;
354
+ }
355
+ return m;
356
+ },
357
+ add(x, y, vx0, vy0, m, t = 0) {
358
+ if (count >= cap) return -1;
359
+ const i = count++;
360
+ px[i] = x;
361
+ py[i] = y;
362
+ vx[i] = vx0;
363
+ vy[i] = vy0;
364
+ mass[i] = m;
365
+ tag[i] = t;
366
+ primed = false;
367
+ return i;
368
+ },
369
+ step(dt, g, soft, gr = 0, rs = 0) {
370
+ if (count === 0 || dt <= 0) return;
371
+ if (!primed) {
372
+ computeAccel(g, soft, gr, rs);
373
+ primed = true;
374
+ }
375
+ for (let i = 0; i < count; i++) {
376
+ const axi = ax[i] ?? 0;
377
+ const ayi = ay[i] ?? 0;
378
+ px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + .5 * axi * dt * dt;
379
+ py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + .5 * ayi * dt * dt;
380
+ axOld[i] = axi;
381
+ ayOld[i] = ayi;
382
+ }
383
+ computeAccel(g, soft, gr, rs);
384
+ for (let i = 0; i < count; i++) {
385
+ vx[i] = (vx[i] ?? 0) + .5 * ((axOld[i] ?? 0) + (ax[i] ?? 0)) * dt;
386
+ vy[i] = (vy[i] ?? 0) + .5 * ((ayOld[i] ?? 0) + (ay[i] ?? 0)) * dt;
387
+ }
388
+ },
389
+ totalEnergy(g, soft, gr = 0, rs = 0) {
390
+ const soft2 = soft * soft;
391
+ let ke = 0;
392
+ for (let i = 0; i < count; i++) {
393
+ const m = mass[i] ?? 0;
394
+ const vxi = vx[i] ?? 0;
395
+ const vyi = vy[i] ?? 0;
396
+ ke += .5 * m * (vxi * vxi + vyi * vyi);
397
+ }
398
+ let pe = 0;
399
+ for (let i = 0; i < count; i++) for (let j = i + 1; j < count; j++) {
400
+ const dx = (px[j] ?? 0) - (px[i] ?? 0);
401
+ const dy = (py[j] ?? 0) - (py[i] ?? 0);
402
+ const d = Math.sqrt(dx * dx + dy * dy + soft2);
403
+ pe -= g * (mass[i] ?? 0) * (mass[j] ?? 0) / d;
404
+ }
405
+ if (gr > 0 && count > 1) {
406
+ const cxp = px[0] ?? 0;
407
+ const cyp = py[0] ?? 0;
408
+ const beta = gr * g * (mass[0] ?? 0) * rs * rs;
409
+ for (let i = 1; i < count; i++) {
410
+ const dx = (px[i] ?? 0) - cxp;
411
+ const dy = (py[i] ?? 0) - cyp;
412
+ const u = dx * dx + dy * dy + soft2;
413
+ pe -= beta * (mass[i] ?? 0) / (3 * u * Math.sqrt(u));
414
+ }
415
+ }
416
+ return ke + pe;
417
+ },
418
+ centerOfMass(out) {
419
+ let mx = 0;
420
+ let my = 0;
421
+ let mt = 0;
422
+ for (let i = 0; i < count; i++) {
423
+ const m = mass[i] ?? 0;
424
+ mx += (px[i] ?? 0) * m;
425
+ my += (py[i] ?? 0) * m;
426
+ mt += m;
427
+ }
428
+ out.x = mt > 0 ? mx / mt : 0;
429
+ out.y = mt > 0 ? my / mt : 0;
430
+ },
431
+ translate(dx, dy) {
432
+ for (let i = 0; i < count; i++) {
433
+ px[i] = (px[i] ?? 0) + dx;
434
+ py[i] = (py[i] ?? 0) + dy;
435
+ }
436
+ },
437
+ absorb(into, victim) {
438
+ if (into === victim || into < 0 || victim < 0 || into >= count || victim >= count) return;
439
+ const mi = mass[into] ?? 0;
440
+ const mj = mass[victim] ?? 0;
441
+ const M = mi + mj;
442
+ if (M <= 0) return;
443
+ px[into] = ((px[into] ?? 0) * mi + (px[victim] ?? 0) * mj) / M;
444
+ py[into] = ((py[into] ?? 0) * mi + (py[victim] ?? 0) * mj) / M;
445
+ vx[into] = ((vx[into] ?? 0) * mi + (vx[victim] ?? 0) * mj) / M;
446
+ vy[into] = ((vy[into] ?? 0) * mi + (vy[victim] ?? 0) * mj) / M;
447
+ mass[into] = M;
448
+ const last = count - 1;
449
+ if (victim !== last) {
450
+ px[victim] = px[last] ?? 0;
451
+ py[victim] = py[last] ?? 0;
452
+ vx[victim] = vx[last] ?? 0;
453
+ vy[victim] = vy[last] ?? 0;
454
+ mass[victim] = mass[last] ?? 0;
455
+ tag[victim] = tag[last] ?? 0;
456
+ }
457
+ count--;
458
+ primed = false;
459
+ },
460
+ closestPair(out) {
461
+ out.i = -1;
462
+ out.j = -1;
463
+ out.sep = Number.POSITIVE_INFINITY;
464
+ for (let i = 0; i < count; i++) for (let j = i + 1; j < count; j++) {
465
+ const dx = (px[j] ?? 0) - (px[i] ?? 0);
466
+ const dy = (py[j] ?? 0) - (py[i] ?? 0);
467
+ const d = Math.sqrt(dx * dx + dy * dy);
468
+ if (d < out.sep) {
469
+ out.sep = d;
470
+ out.i = i;
471
+ out.j = j;
472
+ }
473
+ }
474
+ }
475
+ };
230
476
  }
231
-
232
- // src/integrate/verlet.ts
477
+ //#endregion
478
+ //#region src/integrate/verlet.ts
479
+ /**
480
+ * Self-contained kick-drift-kick leapfrog (velocity-Verlet-equivalent). Advances pos/vel in place
481
+ * under accel; symplectic => bounded energy for conservative forces. ax/ay are caller scratch
482
+ * (length >= count). Evals accel TWICE per step. Zero-alloc.
483
+ */
233
484
  function leapfrogInto(count, px, py, vx, vy, dt, accel, ax, ay) {
234
- accel(count, px, py, vx, vy, ax, ay);
235
- for (let i = 0; i < count; i++) {
236
- vx[i] = (vx[i] ?? 0) + 0.5 * (ax[i] ?? 0) * dt;
237
- vy[i] = (vy[i] ?? 0) + 0.5 * (ay[i] ?? 0) * dt;
238
- }
239
- for (let i = 0; i < count; i++) {
240
- px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt;
241
- py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt;
242
- }
243
- accel(count, px, py, vx, vy, ax, ay);
244
- for (let i = 0; i < count; i++) {
245
- vx[i] = (vx[i] ?? 0) + 0.5 * (ax[i] ?? 0) * dt;
246
- vy[i] = (vy[i] ?? 0) + 0.5 * (ay[i] ?? 0) * dt;
247
- }
485
+ accel(count, px, py, vx, vy, ax, ay);
486
+ for (let i = 0; i < count; i++) {
487
+ vx[i] = (vx[i] ?? 0) + .5 * (ax[i] ?? 0) * dt;
488
+ vy[i] = (vy[i] ?? 0) + .5 * (ay[i] ?? 0) * dt;
489
+ }
490
+ for (let i = 0; i < count; i++) {
491
+ px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt;
492
+ py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt;
493
+ }
494
+ accel(count, px, py, vx, vy, ax, ay);
495
+ for (let i = 0; i < count; i++) {
496
+ vx[i] = (vx[i] ?? 0) + .5 * (ax[i] ?? 0) * dt;
497
+ vy[i] = (vy[i] ?? 0) + .5 * (ay[i] ?? 0) * dt;
498
+ }
248
499
  }
500
+ /**
501
+ * Velocity-Verlet step reusing a cached a(t) — the nbody scheme, ONE accel eval/step. On ENTRY
502
+ * ax/ay must hold a(t) for the current positions; on RETURN they hold a(t+dt) (feed into the next
503
+ * step). axPrev/ayPrev are caller scratch (length >= count). Zero-alloc.
504
+ *
505
+ * Symplectic => bounded energy for conservative forces.
506
+ */
249
507
  function verletStep(count, px, py, vx, vy, dt, accel, ax, ay, axPrev, ayPrev) {
250
- for (let i = 0; i < count; i++) {
251
- px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + 0.5 * (ax[i] ?? 0) * dt * dt;
252
- py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + 0.5 * (ay[i] ?? 0) * dt * dt;
253
- axPrev[i] = ax[i] ?? 0;
254
- ayPrev[i] = ay[i] ?? 0;
255
- }
256
- accel(count, px, py, vx, vy, ax, ay);
257
- for (let i = 0; i < count; i++) {
258
- vx[i] = (vx[i] ?? 0) + 0.5 * ((axPrev[i] ?? 0) + (ax[i] ?? 0)) * dt;
259
- vy[i] = (vy[i] ?? 0) + 0.5 * ((ayPrev[i] ?? 0) + (ay[i] ?? 0)) * dt;
260
- }
508
+ for (let i = 0; i < count; i++) {
509
+ px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + .5 * (ax[i] ?? 0) * dt * dt;
510
+ py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + .5 * (ay[i] ?? 0) * dt * dt;
511
+ axPrev[i] = ax[i] ?? 0;
512
+ ayPrev[i] = ay[i] ?? 0;
513
+ }
514
+ accel(count, px, py, vx, vy, ax, ay);
515
+ for (let i = 0; i < count; i++) {
516
+ vx[i] = (vx[i] ?? 0) + .5 * ((axPrev[i] ?? 0) + (ax[i] ?? 0)) * dt;
517
+ vy[i] = (vy[i] ?? 0) + .5 * ((ayPrev[i] ?? 0) + (ay[i] ?? 0)) * dt;
518
+ }
261
519
  }
262
-
263
- // src/wave/wave-packet-pool.ts
264
- var TWO_PI = Math.PI * 2;
265
- var PACKET_WIDTH = 1.3;
266
- var ATTEN = 320;
267
- var LIFE_CUTOFF = 5;
520
+ //#endregion
521
+ //#region src/wave/wave-packet-pool.ts
522
+ const TWO_PI = Math.PI * 2;
523
+ const PACKET_WIDTH = 1.3;
524
+ const ATTEN = 320;
525
+ const LIFE_CUTOFF = 5;
268
526
  function createRipplePool(capacity) {
269
- const cap = Math.max(1, Math.floor(capacity));
270
- const ox = new Float32Array(cap);
271
- const oy = new Float32Array(cap);
272
- const t0 = new Float32Array(cap);
273
- const amp = new Float32Array(cap);
274
- const speed = new Float32Array(cap);
275
- const wavelength = new Float32Array(cap);
276
- const life = new Float32Array(cap);
277
- const active = new Uint8Array(cap);
278
- let live = 0;
279
- const freeSlot = () => {
280
- for (let i = 0; i < cap; i++) if (active[i] === 0) return i;
281
- let oldest = 0;
282
- let best = Number.POSITIVE_INFINITY;
283
- for (let i = 0; i < cap; i++) {
284
- const t = t0[i] ?? 0;
285
- if (t < best) {
286
- best = t;
287
- oldest = i;
288
- }
289
- }
290
- return oldest;
291
- };
292
- return {
293
- capacity: cap,
294
- get count() {
295
- return live;
296
- },
297
- emit(x, y, now, a, sp, wl, lf) {
298
- const i = freeSlot();
299
- if (active[i] === 0) live++;
300
- ox[i] = x;
301
- oy[i] = y;
302
- t0[i] = now;
303
- amp[i] = a;
304
- speed[i] = sp;
305
- wavelength[i] = Math.max(1, wl);
306
- life[i] = Math.max(0.01, lf);
307
- active[i] = 1;
308
- },
309
- heightAt(x, y, now) {
310
- let h = 0;
311
- for (let i = 0; i < cap; i++) {
312
- if (active[i] === 0) continue;
313
- const age = now - (t0[i] ?? 0);
314
- if (age < 0) continue;
315
- const wl = wavelength[i] ?? 1;
316
- const R = (speed[i] ?? 0) * age;
317
- const dx = x - (ox[i] ?? 0);
318
- const dy = y - (oy[i] ?? 0);
319
- const d = Math.sqrt(dx * dx + dy * dy);
320
- const front = d - R;
321
- const width = wl * PACKET_WIDTH;
322
- const env = Math.exp(-(front * front) / (2 * width * width));
323
- if (env < 1e-3) continue;
324
- const tdecay = Math.exp(-age / (life[i] ?? 1));
325
- const sdecay = 1 / Math.sqrt(1 + R / ATTEN);
326
- const k = TWO_PI / wl;
327
- h += (amp[i] ?? 0) * env * tdecay * sdecay * Math.cos(k * front);
328
- }
329
- return h;
330
- },
331
- prune(now) {
332
- for (let i = 0; i < cap; i++) {
333
- if (active[i] === 0) continue;
334
- if (now - (t0[i] ?? 0) > (life[i] ?? 1) * LIFE_CUTOFF) {
335
- active[i] = 0;
336
- live--;
337
- }
338
- }
339
- },
340
- clear() {
341
- active.fill(0);
342
- live = 0;
343
- }
344
- };
527
+ const cap = Math.max(1, Math.floor(capacity));
528
+ const ox = new Float32Array(cap);
529
+ const oy = new Float32Array(cap);
530
+ const t0 = new Float32Array(cap);
531
+ const amp = new Float32Array(cap);
532
+ const speed = new Float32Array(cap);
533
+ const wavelength = new Float32Array(cap);
534
+ const life = new Float32Array(cap);
535
+ const active = new Uint8Array(cap);
536
+ let live = 0;
537
+ const freeSlot = () => {
538
+ for (let i = 0; i < cap; i++) if (active[i] === 0) return i;
539
+ let oldest = 0;
540
+ let best = Number.POSITIVE_INFINITY;
541
+ for (let i = 0; i < cap; i++) {
542
+ const t = t0[i] ?? 0;
543
+ if (t < best) {
544
+ best = t;
545
+ oldest = i;
546
+ }
547
+ }
548
+ return oldest;
549
+ };
550
+ return {
551
+ capacity: cap,
552
+ get count() {
553
+ return live;
554
+ },
555
+ emit(x, y, now, a, sp, wl, lf) {
556
+ const i = freeSlot();
557
+ if (active[i] === 0) live++;
558
+ ox[i] = x;
559
+ oy[i] = y;
560
+ t0[i] = now;
561
+ amp[i] = a;
562
+ speed[i] = sp;
563
+ wavelength[i] = Math.max(1, wl);
564
+ life[i] = Math.max(.01, lf);
565
+ active[i] = 1;
566
+ },
567
+ heightAt(x, y, now) {
568
+ let h = 0;
569
+ for (let i = 0; i < cap; i++) {
570
+ if (active[i] === 0) continue;
571
+ const age = now - (t0[i] ?? 0);
572
+ if (age < 0) continue;
573
+ const wl = wavelength[i] ?? 1;
574
+ const R = (speed[i] ?? 0) * age;
575
+ const dx = x - (ox[i] ?? 0);
576
+ const dy = y - (oy[i] ?? 0);
577
+ const front = Math.sqrt(dx * dx + dy * dy) - R;
578
+ const width = wl * PACKET_WIDTH;
579
+ const env = Math.exp(-(front * front) / (2 * width * width));
580
+ if (env < .001) continue;
581
+ const tdecay = Math.exp(-age / (life[i] ?? 1));
582
+ const sdecay = 1 / Math.sqrt(1 + R / ATTEN);
583
+ const k = TWO_PI / wl;
584
+ h += (amp[i] ?? 0) * env * tdecay * sdecay * Math.cos(k * front);
585
+ }
586
+ return h;
587
+ },
588
+ prune(now) {
589
+ for (let i = 0; i < cap; i++) {
590
+ if (active[i] === 0) continue;
591
+ if (now - (t0[i] ?? 0) > (life[i] ?? 1) * LIFE_CUTOFF) {
592
+ active[i] = 0;
593
+ live--;
594
+ }
595
+ }
596
+ },
597
+ clear() {
598
+ active.fill(0);
599
+ live = 0;
600
+ }
601
+ };
345
602
  }
603
+ //#endregion
604
+ exports.SENSE_DEFAULTS = SENSE_DEFAULTS;
605
+ exports.createNBodySystem = createNBodySystem;
606
+ exports.createRipplePool = createRipplePool;
607
+ exports.createScalarField = createScalarField;
608
+ exports.createStigmergy = createStigmergy;
609
+ exports.decideTurn = decideTurn;
610
+ exports.leapfrogInto = leapfrogInto;
611
+ exports.senseAt = senseAt;
612
+ exports.verletStep = verletStep;
346
613
 
347
- // src/field/scalar-field.ts
348
- var clamp01 = (v) => v < 0 ? 0 : v > 1 ? 1 : v;
349
- var smooth = (t) => {
350
- const c = clamp01(t);
351
- return c * c * (3 - 2 * c);
352
- };
353
- function createScalarField(width, height, cellSize, edgeMode = "toroidal") {
354
- const cell = Math.max(1, cellSize);
355
- let w = Math.max(1, width);
356
- let h = Math.max(1, height);
357
- let cols = Math.max(1, Math.ceil(w / cell));
358
- let rows = Math.max(1, Math.ceil(h / cell));
359
- let a = new Float32Array(cols * rows);
360
- let b = new Float32Array(cols * rows);
361
- let wrapCol;
362
- let wrapRow;
363
- function rebuildEdgeFns() {
364
- if (edgeMode === "toroidal") {
365
- wrapCol = (c) => (c % cols + cols) % cols;
366
- wrapRow = (r) => (r % rows + rows) % rows;
367
- } else {
368
- wrapCol = (c) => c < 0 ? 0 : c >= cols ? cols - 1 : c;
369
- wrapRow = (r) => r < 0 ? 0 : r >= rows ? rows - 1 : r;
370
- }
371
- }
372
- rebuildEdgeFns();
373
- const field = {
374
- get w() {
375
- return w;
376
- },
377
- get h() {
378
- return h;
379
- },
380
- get cols() {
381
- return cols;
382
- },
383
- get rows() {
384
- return rows;
385
- },
386
- get cell() {
387
- return cell;
388
- },
389
- get edge() {
390
- return edgeMode;
391
- },
392
- sample(x, y) {
393
- const gx = x / cell - 0.5;
394
- const gy = y / cell - 0.5;
395
- const x0 = Math.floor(gx);
396
- const y0 = Math.floor(gy);
397
- const fx = gx - x0;
398
- const fy = gy - y0;
399
- const xa = wrapCol(x0);
400
- const xb = wrapCol(x0 + 1);
401
- const ya = wrapRow(y0);
402
- const yb = wrapRow(y0 + 1);
403
- const a00 = a[ya * cols + xa] ?? 0;
404
- const a10 = a[ya * cols + xb] ?? 0;
405
- const a01 = a[yb * cols + xa] ?? 0;
406
- const a11 = a[yb * cols + xb] ?? 0;
407
- const top = a00 + (a10 - a00) * fx;
408
- const bot = a01 + (a11 - a01) * fx;
409
- return top + (bot - top) * fy;
410
- },
411
- gradient(x, y, out) {
412
- out.x = field.sample(x + cell, y) - field.sample(x - cell, y);
413
- out.y = field.sample(x, y + cell) - field.sample(x, y - cell);
414
- },
415
- deposit(x, y, amount) {
416
- const cx = wrapCol(Math.floor(x / cell));
417
- const cy = wrapRow(Math.floor(y / cell));
418
- const i = cy * cols + cx;
419
- a[i] = (a[i] ?? 0) + amount;
420
- },
421
- erode(x, y, radiusPx, mul) {
422
- const cr = Math.max(1, Math.ceil(radiusPx / cell));
423
- const ccx = Math.floor(x / cell);
424
- const ccy = Math.floor(y / cell);
425
- const r2 = cr * cr;
426
- for (let dy = -cr; dy <= cr; dy++) {
427
- for (let dx = -cr; dx <= cr; dx++) {
428
- if (dx * dx + dy * dy > r2) continue;
429
- const i = wrapRow(ccy + dy) * cols + wrapCol(ccx + dx);
430
- a[i] = (a[i] ?? 0) * mul;
431
- }
432
- }
433
- },
434
- step(diffuseRate, decayRate, maxVal) {
435
- for (let r = 0; r < rows; r++) {
436
- const base = r * cols;
437
- for (let c = 0; c < cols; c++) {
438
- const cl = edgeMode === "toroidal" ? c === 0 ? cols - 1 : c - 1 : c === 0 ? 0 : c - 1;
439
- const cr2 = edgeMode === "toroidal" ? c === cols - 1 ? 0 : c + 1 : c === cols - 1 ? cols - 1 : c + 1;
440
- b[base + c] = ((a[base + cl] ?? 0) + (a[base + c] ?? 0) + (a[base + cr2] ?? 0)) / 3;
441
- }
442
- }
443
- for (let r = 0; r < rows; r++) {
444
- const ru = edgeMode === "toroidal" ? r === 0 ? rows - 1 : r - 1 : r === 0 ? 0 : r - 1;
445
- const rd = edgeMode === "toroidal" ? r === rows - 1 ? 0 : r + 1 : r === rows - 1 ? rows - 1 : r + 1;
446
- const base = r * cols;
447
- const upBase = ru * cols;
448
- const dnBase = rd * cols;
449
- for (let c = 0; c < cols; c++) {
450
- const blurred = ((b[upBase + c] ?? 0) + (b[base + c] ?? 0) + (b[dnBase + c] ?? 0)) / 3;
451
- const orig = a[base + c] ?? 0;
452
- const next = (orig + (blurred - orig) * diffuseRate) * decayRate;
453
- a[base + c] = next > maxVal ? maxVal : next;
454
- }
455
- }
456
- },
457
- meanValue(stride) {
458
- const s = Math.max(1, Math.floor(stride));
459
- let sum = 0;
460
- let n = 0;
461
- for (let i = 0; i < a.length; i += s) {
462
- sum += a[i] ?? 0;
463
- n++;
464
- }
465
- return n > 0 ? sum / n : 0;
466
- },
467
- maxValue(stride) {
468
- const s = Math.max(1, Math.floor(stride));
469
- let m = 0;
470
- for (let i = 0; i < a.length; i += s) {
471
- const v = a[i] ?? 0;
472
- if (v > m) m = v;
473
- }
474
- return m;
475
- },
476
- localMean(x, y, radiusPx) {
477
- const cr = Math.max(1, Math.ceil(radiusPx / cell));
478
- const ccx = Math.floor(x / cell);
479
- const ccy = Math.floor(y / cell);
480
- let sum = 0;
481
- let n = 0;
482
- for (let dy = -cr; dy <= cr; dy++) {
483
- for (let dx = -cr; dx <= cr; dx++) {
484
- sum += a[wrapRow(ccy + dy) * cols + wrapCol(ccx + dx)] ?? 0;
485
- n++;
486
- }
487
- }
488
- return n > 0 ? sum / n : 0;
489
- },
490
- toRGBA(out, norm, low, mid, hi, knot) {
491
- const inv = norm > 1e-6 ? 1 / norm : 0;
492
- const lr = low[0] ?? 0;
493
- const lg = low[1] ?? 0;
494
- const lb = low[2] ?? 0;
495
- const mr = mid[0] ?? 0;
496
- const mg = mid[1] ?? 0;
497
- const mb = mid[2] ?? 0;
498
- const hr = hi[0] ?? 0;
499
- const hg = hi[1] ?? 0;
500
- const hb = hi[2] ?? 0;
501
- const kr = knot[0] ?? 0;
502
- const kg = knot[1] ?? 0;
503
- const kb = knot[2] ?? 0;
504
- for (let i = 0; i < a.length; i++) {
505
- const v = smooth((a[i] ?? 0) * inv);
506
- let r;
507
- let g;
508
- let bl;
509
- if (v < 0.5) {
510
- const t = v * 2;
511
- r = lr + (mr - lr) * t;
512
- g = lg + (mg - lg) * t;
513
- bl = lb + (mb - lb) * t;
514
- } else if (v < 0.85) {
515
- const t = (v - 0.5) / 0.35;
516
- r = mr + (hr - mr) * t;
517
- g = mg + (hg - mg) * t;
518
- bl = mb + (hb - mb) * t;
519
- } else {
520
- const t = (v - 0.85) / 0.15;
521
- r = hr + (kr - hr) * t;
522
- g = hg + (kg - hg) * t;
523
- bl = hb + (kb - hb) * t;
524
- }
525
- const o = i << 2;
526
- out[o] = r * v * 255 | 0;
527
- out[o + 1] = g * v * 255 | 0;
528
- out[o + 2] = bl * v * 255 | 0;
529
- out[o + 3] = 255;
530
- }
531
- },
532
- resize(width2, height2) {
533
- w = Math.max(1, width2);
534
- h = Math.max(1, height2);
535
- cols = Math.max(1, Math.ceil(w / cell));
536
- rows = Math.max(1, Math.ceil(h / cell));
537
- a = new Float32Array(cols * rows);
538
- b = new Float32Array(cols * rows);
539
- rebuildEdgeFns();
540
- }
541
- };
542
- return field;
543
- }
544
-
545
- // src/agent/stigmergy.ts
546
- var TWO_PI2 = Math.PI * 2;
547
- var SENSE_DEFAULTS = {
548
- sensorAngle: 0.5,
549
- sensorDist: 9,
550
- rotateAngle: 0.45
551
- };
552
- function senseAt(field, x, y, heading, offset, dist) {
553
- const a = heading + offset;
554
- return field.sample(x + Math.cos(a) * dist, y + Math.sin(a) * dist);
555
- }
556
- function decideTurn(left, center, right, rand) {
557
- if (center > left && center > right) return 0;
558
- if (center < left && center < right) return rand < 0.5 ? -1 : 1;
559
- if (left < right) return 1;
560
- return -1;
561
- }
562
- function createStigmergy(field, config, rng) {
563
- const { count } = config;
564
- const px = new Float32Array(count);
565
- const py = new Float32Array(count);
566
- const heading = new Float32Array(count);
567
- const fw = field.w;
568
- const fh = field.h;
569
- for (let i = 0; i < count; i++) {
570
- px[i] = rng.float(0, fw);
571
- py[i] = rng.float(0, fh);
572
- heading[i] = rng.float(0, TWO_PI2);
573
- }
574
- function wrapX(x, w) {
575
- if (field.edge === "toroidal") return (x % w + w) % w;
576
- return x < 0 ? 0 : x >= w ? w - 1 : x;
577
- }
578
- function wrapY(y, h) {
579
- if (field.edge === "toroidal") return (y % h + h) % h;
580
- return y < 0 ? 0 : y >= h ? h - 1 : y;
581
- }
582
- const af = {
583
- count,
584
- px,
585
- py,
586
- heading,
587
- update() {
588
- const w = field.w;
589
- const h = field.h;
590
- const sa = config.sensorAngle;
591
- const sd = config.sensorDist;
592
- const turnRate = config.rotateAngle;
593
- const chaos = config.chaos;
594
- const stepSize = config.stepSize;
595
- const deposit = config.depositAmount;
596
- for (let i = 0; i < count; i++) {
597
- const x = px[i] ?? 0;
598
- const y = py[i] ?? 0;
599
- let hd = heading[i] ?? 0;
600
- const cF = senseAt(field, x, y, hd, 0, sd);
601
- const cL = senseAt(field, x, y, hd, -sa, sd);
602
- const cR = senseAt(field, x, y, hd, sa, sd);
603
- hd += decideTurn(cL, cF, cR, rng.float(0, 1)) * turnRate;
604
- hd += (rng.float(0, 1) * 2 - 1) * chaos;
605
- if (config.onSense) hd += config.onSense(i, x, y, hd);
606
- const nx = wrapX(x + Math.cos(hd) * stepSize, w);
607
- const ny = wrapY(y + Math.sin(hd) * stepSize, h);
608
- px[i] = nx;
609
- py[i] = ny;
610
- heading[i] = hd;
611
- field.deposit(nx, ny, deposit);
612
- if (config.onMove) config.onMove(i, nx, ny, hd);
613
- }
614
- },
615
- forEachAgent(cb, stride = 1) {
616
- const s = Math.max(1, Math.floor(stride));
617
- for (let i = 0; i < count; i += s) {
618
- cb(px[i] ?? 0, py[i] ?? 0);
619
- }
620
- },
621
- resize(width, height) {
622
- const newW = Math.max(1, width);
623
- const newH = Math.max(1, height);
624
- for (let i = 0; i < count; i++) {
625
- px[i] = wrapX(px[i] ?? 0, newW);
626
- py[i] = wrapY(py[i] ?? 0, newH);
627
- }
628
- }
629
- };
630
- return af;
631
- }
632
- // Annotate the CommonJS export names for ESM import in node:
633
- 0 && (module.exports = {
634
- SENSE_DEFAULTS,
635
- createNBodySystem,
636
- createRipplePool,
637
- createScalarField,
638
- createStigmergy,
639
- decideTurn,
640
- leapfrogInto,
641
- senseAt,
642
- verletStep
643
- });
644
614
  //# sourceMappingURL=index.js.map