@flare-engine/simulation 0.2.1 → 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/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +2 -2
- package/dist/index.d.mts +137 -132
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +137 -132
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +602 -637
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +595 -604
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,47 +1,129 @@
|
|
|
1
|
-
import { SeededRNG } from
|
|
2
|
-
|
|
1
|
+
import { SeededRNG } from "@flare-engine/math";
|
|
2
|
+
//#region src/field/scalar-field.d.ts
|
|
3
|
+
interface ScalarField {
|
|
4
|
+
readonly w: number;
|
|
5
|
+
readonly h: number;
|
|
6
|
+
readonly cols: number;
|
|
7
|
+
readonly rows: number;
|
|
8
|
+
readonly cell: number;
|
|
9
|
+
readonly edge: 'toroidal' | 'clamp';
|
|
10
|
+
/** Bilinear read in px space. */
|
|
11
|
+
sample(x: number, y: number): number;
|
|
12
|
+
/** Central-difference gradient ∇ of the field at (x,y) — one cell apart, edge-aware. */
|
|
13
|
+
gradient(x: number, y: number, out: {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
}): void;
|
|
17
|
+
/** Additive pheromone splat into the nearest cell. */
|
|
18
|
+
deposit(x: number, y: number, amount: number): void;
|
|
19
|
+
/** Multiply cells within radiusPx of (x,y) by `mul` (0..1). */
|
|
20
|
+
erode(x: number, y: number, radiusPx: number, mul: number): void;
|
|
21
|
+
/** Whole-field update: separable 3×3 box blur lerped by diffuseRate, then × decayRate.
|
|
22
|
+
* Each cell is clamped to maxVal (use Infinity for no clamp). */
|
|
23
|
+
step(diffuseRate: number, decayRate: number, maxVal: number): void;
|
|
24
|
+
/** Strided mean over the whole field. */
|
|
25
|
+
meanValue(stride: number): number;
|
|
26
|
+
/** Strided max over the whole field. */
|
|
27
|
+
maxValue(stride: number): number;
|
|
28
|
+
/** Mean field value in a disc around (x,y). */
|
|
29
|
+
localMean(x: number, y: number, radiusPx: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Pack the field into an RGBA byte buffer (length cols*rows*4) with a palette-binned ramp:
|
|
32
|
+
* low→mid→hi by value, knot reserved for the brightest cells; brightness folded into rgb
|
|
33
|
+
* so dim cells add nothing under additive Plus. `out` is caller-owned and reused.
|
|
34
|
+
*
|
|
35
|
+
* Palette channels (low/mid/hi/knot) are normalized [0,1] fractions (not byte values).
|
|
36
|
+
* ScalarField values are assumed non-negative; negative values are clamped to 0 in the
|
|
37
|
+
* output byte channels.
|
|
38
|
+
*/
|
|
39
|
+
toRGBA(out: Uint8Array, norm: number, low: readonly number[], mid: readonly number[], hi: readonly number[], knot: readonly number[]): void;
|
|
40
|
+
resize(width: number, height: number): void;
|
|
41
|
+
}
|
|
42
|
+
declare function createScalarField(width: number, height: number, cellSize: number, edgeMode?: 'toroidal' | 'clamp'): ScalarField;
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/agent/stigmergy.d.ts
|
|
45
|
+
interface SenseConfig {
|
|
46
|
+
/** rad — angular offset of the two side sensors. */
|
|
47
|
+
sensorAngle: number;
|
|
48
|
+
/** px — how far ahead the sensors read. */
|
|
49
|
+
sensorDist: number;
|
|
50
|
+
/** rad — turn applied per step toward the brightest sensor. */
|
|
51
|
+
rotateAngle: number;
|
|
52
|
+
}
|
|
53
|
+
declare const SENSE_DEFAULTS: SenseConfig;
|
|
54
|
+
/** Read the field at one sensor: `offset` rad off the agent heading, `dist` px ahead. */
|
|
55
|
+
declare function senseAt(field: ScalarField, x: number, y: number, heading: number, offset: number, dist: number): number;
|
|
56
|
+
/** Jones turn decision from three sensor readings: −1 turn left, 0 straight, +1 turn right.
|
|
57
|
+
* `rand` ∈ [0,1) breaks the symmetric "both sides brighter" case. */
|
|
58
|
+
declare function decideTurn(left: number, center: number, right: number, rand: number): -1 | 0 | 1;
|
|
59
|
+
interface StigmergyConfig {
|
|
60
|
+
count: number;
|
|
61
|
+
sensorAngle: number;
|
|
62
|
+
sensorDist: number;
|
|
63
|
+
rotateAngle: number;
|
|
64
|
+
stepSize: number;
|
|
65
|
+
depositAmount: number;
|
|
66
|
+
chaos: number;
|
|
67
|
+
/** Returns an extra heading delta (rad) applied before moving. */
|
|
68
|
+
onSense?: (index: number, x: number, y: number, heading: number) => number;
|
|
69
|
+
/** Post-move notify. */
|
|
70
|
+
onMove?: (index: number, x: number, y: number, heading: number) => void;
|
|
71
|
+
}
|
|
72
|
+
interface AgentField {
|
|
73
|
+
readonly count: number;
|
|
74
|
+
readonly px: Float32Array;
|
|
75
|
+
readonly py: Float32Array;
|
|
76
|
+
readonly heading: Float32Array;
|
|
77
|
+
update(): void;
|
|
78
|
+
forEachAgent(cb: (x: number, y: number) => void, stride?: number): void;
|
|
79
|
+
resize(width: number, height: number): void;
|
|
80
|
+
}
|
|
81
|
+
declare function createStigmergy(field: ScalarField, config: StigmergyConfig, rng: SeededRNG): AgentField;
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/integrate/nbody.d.ts
|
|
3
84
|
interface ClosestPair {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
85
|
+
i: number;
|
|
86
|
+
j: number;
|
|
87
|
+
sep: number;
|
|
7
88
|
}
|
|
8
89
|
interface NBodySystem {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
90
|
+
/** Backing-array capacity (max bodies). */
|
|
91
|
+
readonly capacity: number;
|
|
92
|
+
/** Live body count. */
|
|
93
|
+
readonly count: number;
|
|
94
|
+
readonly px: Float32Array;
|
|
95
|
+
readonly py: Float32Array;
|
|
96
|
+
readonly vx: Float32Array;
|
|
97
|
+
readonly vy: Float32Array;
|
|
98
|
+
readonly mass: Float32Array;
|
|
99
|
+
/** Generic caller tag carried per body (e.g. a planet/color id); swapped through `absorb`. */
|
|
100
|
+
readonly tag: Float32Array;
|
|
101
|
+
/** Largest mass currently present. */
|
|
102
|
+
maxMass(): number;
|
|
103
|
+
/** Add a body; returns its index, or -1 if at capacity. `tag` is an opaque per-body scalar. */
|
|
104
|
+
add(x: number, y: number, vx: number, vy: number, m: number, tag?: number): number;
|
|
105
|
+
/** One velocity-Verlet step under Plummer-softened gravity. `gr`>0 adds a relativistic 1/r⁴
|
|
106
|
+
* precession pull on bodies i≥1 toward body 0 (rosette orbits); `rs` is its GR length scale. */
|
|
107
|
+
step(dt: number, g: number, softening: number, gr?: number, rs?: number): void;
|
|
108
|
+
/** Total mechanical energy KE + PE (softened, incl. the optional GR term) — bounded ⇒ stable. */
|
|
109
|
+
totalEnergy(g: number, softening: number, gr?: number, rs?: number): number;
|
|
110
|
+
/** Center-of-mass position written into `out` (for keeping the system framed). */
|
|
111
|
+
centerOfMass(out: {
|
|
112
|
+
x: number;
|
|
113
|
+
y: number;
|
|
114
|
+
}): void;
|
|
115
|
+
/** Translate every body by (dx,dy) — used to re-center after a resize. */
|
|
116
|
+
translate(dx: number, dy: number): void;
|
|
117
|
+
/** Merge `victim` into `into` (momentum- and mass-conserving inelastic coalescence), then
|
|
118
|
+
* swap-remove `victim` so the array stays packed. In practice `into` is the central mass
|
|
119
|
+
* (index 0), which is never the moved last slot. */
|
|
120
|
+
absorb(into: number, victim: number): void;
|
|
121
|
+
/** Closest body pair + their separation (sep = Infinity with fewer than two bodies). */
|
|
122
|
+
closestPair(out: ClosestPair): void;
|
|
42
123
|
}
|
|
43
124
|
declare function createNBodySystem(capacity: number): NBodySystem;
|
|
44
|
-
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/integrate/verlet.d.ts
|
|
45
127
|
/**
|
|
46
128
|
* AccelFn fills ax[i]/ay[i] for i in [0, count) from the current SoA position/velocity state.
|
|
47
129
|
* Callers own the ax/ay scratch arrays (length >= count).
|
|
@@ -61,100 +143,23 @@ declare function leapfrogInto(count: number, px: Float32Array, py: Float32Array,
|
|
|
61
143
|
* Symplectic => bounded energy for conservative forces.
|
|
62
144
|
*/
|
|
63
145
|
declare function verletStep(count: number, px: Float32Array, py: Float32Array, vx: Float32Array, vy: Float32Array, dt: number, accel: AccelFn, ax: Float32Array, ay: Float32Array, axPrev: Float32Array, ayPrev: Float32Array): void;
|
|
64
|
-
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/wave/wave-packet-pool.d.ts
|
|
65
148
|
interface RipplePool {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
149
|
+
readonly capacity: number;
|
|
150
|
+
/** Live wavefront count. */
|
|
151
|
+
readonly count: number;
|
|
152
|
+
/** Emit an expanding wavefront from (x,y) born at sim time `now`. Overwrites the oldest slot
|
|
153
|
+
* when full so a long run never starves. */
|
|
154
|
+
emit(x: number, y: number, now: number, amp: number, speed: number, wavelength: number, life: number): void;
|
|
155
|
+
/** Summed transverse height (px, signed) at (x,y) from all live fronts at time `now`. */
|
|
156
|
+
heightAt(x: number, y: number, now: number): number;
|
|
157
|
+
/** Retire spent wavefronts (call once/frame). */
|
|
158
|
+
prune(now: number): void;
|
|
159
|
+
/** Drop all wavefronts (e.g. on resize). */
|
|
160
|
+
clear(): void;
|
|
78
161
|
}
|
|
79
162
|
declare function createRipplePool(capacity: number): RipplePool;
|
|
80
|
-
|
|
81
|
-
interface ScalarField {
|
|
82
|
-
readonly w: number;
|
|
83
|
-
readonly h: number;
|
|
84
|
-
readonly cols: number;
|
|
85
|
-
readonly rows: number;
|
|
86
|
-
readonly cell: number;
|
|
87
|
-
readonly edge: 'toroidal' | 'clamp';
|
|
88
|
-
/** Bilinear read in px space. */
|
|
89
|
-
sample(x: number, y: number): number;
|
|
90
|
-
/** Central-difference gradient ∇ of the field at (x,y) — one cell apart, edge-aware. */
|
|
91
|
-
gradient(x: number, y: number, out: {
|
|
92
|
-
x: number;
|
|
93
|
-
y: number;
|
|
94
|
-
}): void;
|
|
95
|
-
/** Additive pheromone splat into the nearest cell. */
|
|
96
|
-
deposit(x: number, y: number, amount: number): void;
|
|
97
|
-
/** Multiply cells within radiusPx of (x,y) by `mul` (0..1). */
|
|
98
|
-
erode(x: number, y: number, radiusPx: number, mul: number): void;
|
|
99
|
-
/** Whole-field update: separable 3×3 box blur lerped by diffuseRate, then × decayRate.
|
|
100
|
-
* Each cell is clamped to maxVal (use Infinity for no clamp). */
|
|
101
|
-
step(diffuseRate: number, decayRate: number, maxVal: number): void;
|
|
102
|
-
/** Strided mean over the whole field. */
|
|
103
|
-
meanValue(stride: number): number;
|
|
104
|
-
/** Strided max over the whole field. */
|
|
105
|
-
maxValue(stride: number): number;
|
|
106
|
-
/** Mean field value in a disc around (x,y). */
|
|
107
|
-
localMean(x: number, y: number, radiusPx: number): number;
|
|
108
|
-
/**
|
|
109
|
-
* Pack the field into an RGBA byte buffer (length cols*rows*4) with a palette-binned ramp:
|
|
110
|
-
* low→mid→hi by value, knot reserved for the brightest cells; brightness folded into rgb
|
|
111
|
-
* so dim cells add nothing under additive Plus. `out` is caller-owned and reused.
|
|
112
|
-
*
|
|
113
|
-
* Palette channels (low/mid/hi/knot) are normalized [0,1] fractions (not byte values).
|
|
114
|
-
* ScalarField values are assumed non-negative; negative values are clamped to 0 in the
|
|
115
|
-
* output byte channels.
|
|
116
|
-
*/
|
|
117
|
-
toRGBA(out: Uint8Array, norm: number, low: readonly number[], mid: readonly number[], hi: readonly number[], knot: readonly number[]): void;
|
|
118
|
-
resize(width: number, height: number): void;
|
|
119
|
-
}
|
|
120
|
-
declare function createScalarField(width: number, height: number, cellSize: number, edgeMode?: 'toroidal' | 'clamp'): ScalarField;
|
|
121
|
-
|
|
122
|
-
interface SenseConfig {
|
|
123
|
-
/** rad — angular offset of the two side sensors. */
|
|
124
|
-
sensorAngle: number;
|
|
125
|
-
/** px — how far ahead the sensors read. */
|
|
126
|
-
sensorDist: number;
|
|
127
|
-
/** rad — turn applied per step toward the brightest sensor. */
|
|
128
|
-
rotateAngle: number;
|
|
129
|
-
}
|
|
130
|
-
declare const SENSE_DEFAULTS: SenseConfig;
|
|
131
|
-
/** Read the field at one sensor: `offset` rad off the agent heading, `dist` px ahead. */
|
|
132
|
-
declare function senseAt(field: ScalarField, x: number, y: number, heading: number, offset: number, dist: number): number;
|
|
133
|
-
/** Jones turn decision from three sensor readings: −1 turn left, 0 straight, +1 turn right.
|
|
134
|
-
* `rand` ∈ [0,1) breaks the symmetric "both sides brighter" case. */
|
|
135
|
-
declare function decideTurn(left: number, center: number, right: number, rand: number): -1 | 0 | 1;
|
|
136
|
-
interface StigmergyConfig {
|
|
137
|
-
count: number;
|
|
138
|
-
sensorAngle: number;
|
|
139
|
-
sensorDist: number;
|
|
140
|
-
rotateAngle: number;
|
|
141
|
-
stepSize: number;
|
|
142
|
-
depositAmount: number;
|
|
143
|
-
chaos: number;
|
|
144
|
-
/** Returns an extra heading delta (rad) applied before moving. */
|
|
145
|
-
onSense?: (index: number, x: number, y: number, heading: number) => number;
|
|
146
|
-
/** Post-move notify. */
|
|
147
|
-
onMove?: (index: number, x: number, y: number, heading: number) => void;
|
|
148
|
-
}
|
|
149
|
-
interface AgentField {
|
|
150
|
-
readonly count: number;
|
|
151
|
-
readonly px: Float32Array;
|
|
152
|
-
readonly py: Float32Array;
|
|
153
|
-
readonly heading: Float32Array;
|
|
154
|
-
update(): void;
|
|
155
|
-
forEachAgent(cb: (x: number, y: number) => void, stride?: number): void;
|
|
156
|
-
resize(width: number, height: number): void;
|
|
157
|
-
}
|
|
158
|
-
declare function createStigmergy(field: ScalarField, config: StigmergyConfig, rng: SeededRNG): AgentField;
|
|
159
|
-
|
|
163
|
+
//#endregion
|
|
160
164
|
export { type AccelFn, type AgentField, type ClosestPair, type NBodySystem, type RipplePool, SENSE_DEFAULTS, type ScalarField, type SenseConfig, type StigmergyConfig, createNBodySystem, createRipplePool, createScalarField, createStigmergy, decideTurn, leapfrogInto, senseAt, verletStep };
|
|
165
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/field/scalar-field.ts","../src/agent/stigmergy.ts","../src/integrate/nbody.ts","../src/integrate/verlet.ts","../src/wave/wave-packet-pool.ts"],"mappings":";;UAWiB;WACN;WACA;WACA;WACA;WACA;WACA;;EAET,OAAO,WAAW;;EAElB,SAAS,WAAW,WAAW;IAAO;IAAW;;;EAEjD,QAAQ,WAAW,WAAW;;EAE9B,MAAM,WAAW,WAAW,kBAAkB;;;EAG9C,KAAK,qBAAqB,mBAAmB;;EAE7C,UAAU;;EAEV,SAAS;;EAET,UAAU,WAAW,WAAW;;;;;;;;;;EAUhC,OACE,KAAK,YACL,cACA,wBACA,wBACA,uBACA;EAEF,OAAO,eAAe;;iBAGR,kBACd,eACA,gBACA,kBACA,kCACC;;;UCjDc;;EAEf;;EAEA;;EAEA;;cAGW,gBAAgB;;iBAOb,QACd,OAAO,aACP,WACA,WACA,iBACA,gBACA;;;iBAQc,WAAW,cAAc,gBAAgB,eAAe;UASvD;EACf;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA,WAAW,eAAe,WAAW,WAAW;;EAEhD,UAAU,eAAe,WAAW,WAAW;;UAGhC;WACN;WACA,IAAI;WACJ,IAAI;WACJ,SAAS;EAClB;EACA,aAAa,KAAK,WAAW,oBAAoB;EACjD,OAAO,eAAe;;iBAGR,gBACd,OAAO,aACP,QAAQ,iBACR,KAAK,YACJ;;;UC7Dc;EACf;EACA;EACA;;UAGe;;WAEN;;WAEA;WAEA,IAAI;WACJ,IAAI;WACJ,IAAI;WACJ,IAAI;WACJ,MAAM;;WAEN,KAAK;;EAEd;;EAEA,IAAI,WAAW,WAAW,YAAY,YAAY,WAAW;;;EAG7D,KAAK,YAAY,WAAW,mBAAmB,aAAa;;EAE5D,YAAY,WAAW,mBAAmB,aAAa;;EAEvD,aAAa;IAAO;IAAW;;;EAE/B,UAAU,YAAY;;;;EAItB,OAAO,cAAc;;EAErB,YAAY,KAAK;;iBAGH,kBAAkB,mBAAmB;;;;;;;KCrDzC,WACV,eACA,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,IAAI;;;;;;iBAQU,aACd,eACA,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,YACA,OAAO,SACP,IAAI,cACJ,IAAI;;;;;;;;iBAyBU,WACd,eACA,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,IAAI,cACJ,YACA,OAAO,SACP,IAAI,cACJ,IAAI,cACJ,QAAQ,cACR,QAAQ;;;UCpDO;WACN;;WAEA;;;EAGT,KACE,WACA,WACA,aACA,aACA,eACA,oBACA;;EAGF,SAAS,WAAW,WAAW;;EAE/B,MAAM;;EAEN;;iBAGc,iBAAiB,mBAAmB"}
|