@typeonce/ritmo-math 0.14.0 → 0.15.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/CHANGELOG.md +104 -0
- package/README.md +17 -12
- package/dist/Angle.d.ts +44 -0
- package/dist/Angle.d.ts.map +1 -0
- package/dist/Angle.js +62 -0
- package/dist/Angle.js.map +1 -0
- package/dist/Box2.d.ts +16 -4
- package/dist/Box2.d.ts.map +1 -1
- package/dist/Box2.js +16 -1
- package/dist/Box2.js.map +1 -1
- package/dist/Collide2.d.ts +11 -1
- package/dist/Collide2.d.ts.map +1 -1
- package/dist/Collide2.js +16 -1
- package/dist/Collide2.js.map +1 -1
- package/dist/FlowField.d.ts +24 -38
- package/dist/FlowField.d.ts.map +1 -1
- package/dist/FlowField.js +18 -5
- package/dist/FlowField.js.map +1 -1
- package/dist/Scalar.d.ts +19 -166
- package/dist/Scalar.d.ts.map +1 -1
- package/dist/Scalar.js +51 -51
- package/dist/Scalar.js.map +1 -1
- package/dist/Vec2.d.ts +47 -7
- package/dist/Vec2.d.ts.map +1 -1
- package/dist/Vec2.js +56 -8
- package/dist/Vec2.js.map +1 -1
- package/dist/index.d.ts +9 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -22
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/Angle.ts +60 -0
- package/src/Box2.ts +20 -4
- package/src/Collide2.ts +18 -2
- package/src/FlowField.ts +34 -10
- package/src/Scalar.ts +43 -165
- package/src/Vec2.ts +58 -8
- package/src/index.ts +9 -22
- package/dist/Aabb.d.ts +0 -182
- package/dist/Aabb.d.ts.map +0 -1
- package/dist/Aabb.js +0 -109
- package/dist/Aabb.js.map +0 -1
- package/dist/InputAxis.d.ts +0 -44
- package/dist/InputAxis.d.ts.map +0 -1
- package/dist/InputAxis.js +0 -71
- package/dist/InputAxis.js.map +0 -1
- package/dist/Ops.d.ts +0 -209
- package/dist/Ops.d.ts.map +0 -1
- package/dist/Ops.js +0 -210
- package/dist/Ops.js.map +0 -1
- package/dist/Random.d.ts +0 -124
- package/dist/Random.d.ts.map +0 -1
- package/dist/Random.js +0 -126
- package/dist/Random.js.map +0 -1
- package/dist/Size2.d.ts +0 -118
- package/dist/Size2.d.ts.map +0 -1
- package/dist/Size2.js +0 -54
- package/dist/Size2.js.map +0 -1
- package/dist/SpatialGrid.d.ts +0 -68
- package/dist/SpatialGrid.d.ts.map +0 -1
- package/dist/SpatialGrid.js +0 -112
- package/dist/SpatialGrid.js.map +0 -1
- package/dist/Vector2.d.ts +0 -214
- package/dist/Vector2.d.ts.map +0 -1
- package/dist/Vector2.js +0 -134
- package/dist/Vector2.js.map +0 -1
- package/dist/internal/aabb.d.ts +0 -27
- package/dist/internal/aabb.d.ts.map +0 -1
- package/dist/internal/aabb.js +0 -52
- package/dist/internal/aabb.js.map +0 -1
- package/dist/internal/codec.d.ts +0 -9
- package/dist/internal/codec.d.ts.map +0 -1
- package/dist/internal/codec.js +0 -16
- package/dist/internal/codec.js.map +0 -1
- package/dist/internal/fdlibm.d.ts +0 -43
- package/dist/internal/fdlibm.d.ts.map +0 -1
- package/dist/internal/fdlibm.js +0 -1291
- package/dist/internal/fdlibm.js.map +0 -1
- package/dist/internal/inputAxis.d.ts +0 -11
- package/dist/internal/inputAxis.d.ts.map +0 -1
- package/dist/internal/inputAxis.js +0 -13
- package/dist/internal/inputAxis.js.map +0 -1
- package/dist/internal/scalar.d.ts +0 -11
- package/dist/internal/scalar.d.ts.map +0 -1
- package/dist/internal/scalar.js +0 -68
- package/dist/internal/scalar.js.map +0 -1
- package/dist/internal/size2.d.ts +0 -22
- package/dist/internal/size2.d.ts.map +0 -1
- package/dist/internal/size2.js +0 -38
- package/dist/internal/size2.js.map +0 -1
- package/dist/internal/vector2.d.ts +0 -39
- package/dist/internal/vector2.d.ts.map +0 -1
- package/dist/internal/vector2.js +0 -78
- package/dist/internal/vector2.js.map +0 -1
- package/src/Aabb.ts +0 -180
- package/src/InputAxis.ts +0 -81
- package/src/Ops.ts +0 -232
- package/src/Random.ts +0 -225
- package/src/Size2.ts +0 -122
- package/src/SpatialGrid.ts +0 -172
- package/src/Vector2.ts +0 -216
- package/src/internal/aabb.ts +0 -81
- package/src/internal/codec.ts +0 -21
- package/src/internal/fdlibm.ts +0 -1256
- package/src/internal/inputAxis.ts +0 -21
- package/src/internal/scalar.ts +0 -104
- package/src/internal/size2.ts +0 -54
- package/src/internal/vector2.ts +0 -121
package/dist/Ops.d.ts
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Math functions that return the same bits on every JavaScript engine and
|
|
3
|
-
* CPU, so a simulation replays exactly on any device.
|
|
4
|
-
*
|
|
5
|
-
* The spec lets engines approximate `Math.sin`, `cos`, `tan`, `asin`, `acos`,
|
|
6
|
-
* `atan`, `atan2`, `exp`, `expm1`, `log`, `log2`, `log10`, `log1p`, `sinh`,
|
|
7
|
-
* `cosh`, `tanh`, `asinh`, `acosh`, `atanh`, `cbrt`, `pow`, `hypot`, and
|
|
8
|
-
* `a ** b`, and they do: Chrome, Safari, macOS, and Linux disagree in the
|
|
9
|
-
* last bit, and a simulation grows that bit into a different run. Use these
|
|
10
|
-
* in simulation code instead; each is a pure-JavaScript port of fdlibm (musl
|
|
11
|
-
* libm) built only from operations every engine rounds exactly.
|
|
12
|
-
*
|
|
13
|
-
* Keep using `Math.abs`, `sign`, `min`, `max`, `floor`, `ceil`, `round`,
|
|
14
|
-
* `trunc`, `sqrt`, `imul`, `fround`, `+ - * /`, and `x ** 2`: they are
|
|
15
|
-
* already exact everywhere. Use `Random` for randomness and `Clock` for time.
|
|
16
|
-
* Code that only draws (rendering, effects) can use `Math` freely. The
|
|
17
|
-
* `ritmo/use-ops-for-math` rule (`@typeonce/oxlint-plugin-ritmo`) points
|
|
18
|
-
* simulation code here.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* // Inside a system: aim, then move along the heading.
|
|
23
|
-
* const heading = Ops.atan2(target.y - position.y, target.x - position.x)
|
|
24
|
-
* const step = { x: Ops.cos(heading) * speed * dt, y: Ops.sin(heading) * speed * dt }
|
|
25
|
-
* const damping = Ops.exp(-friction * dt)
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @module Ops
|
|
29
|
-
* @docGroup helpers
|
|
30
|
-
*
|
|
31
|
-
* @categoryDescription Trigonometry
|
|
32
|
-
* Sine, cosine, tangent, and their inverses, in radians.
|
|
33
|
-
*
|
|
34
|
-
* @categoryDescription Exponentials
|
|
35
|
-
* Exponentials, logarithms, powers, and roots.
|
|
36
|
-
*
|
|
37
|
-
* @categoryDescription Hyperbolic
|
|
38
|
-
* Hyperbolic functions and their inverses.
|
|
39
|
-
*
|
|
40
|
-
* @categoryDescription Lengths
|
|
41
|
-
* Euclidean length without overflow.
|
|
42
|
-
*/
|
|
43
|
-
/**
|
|
44
|
-
* Sine of `x` radians. Same bits on every engine (within 1 ulp of the exact
|
|
45
|
-
* value); use instead of `Math.sin` in simulation code.
|
|
46
|
-
*
|
|
47
|
-
* @category Trigonometry
|
|
48
|
-
*/
|
|
49
|
-
export declare const sin: (x: number) => number;
|
|
50
|
-
/**
|
|
51
|
-
* Cosine of `x` radians. Same bits on every engine (within 1 ulp of the
|
|
52
|
-
* exact value); use instead of `Math.cos` in simulation code.
|
|
53
|
-
*
|
|
54
|
-
* @category Trigonometry
|
|
55
|
-
*/
|
|
56
|
-
export declare const cos: (x: number) => number;
|
|
57
|
-
/**
|
|
58
|
-
* Tangent of `x` radians. Same bits on every engine (within 1 ulp of the
|
|
59
|
-
* exact value); use instead of `Math.tan` in simulation code.
|
|
60
|
-
*
|
|
61
|
-
* @category Trigonometry
|
|
62
|
-
*/
|
|
63
|
-
export declare const tan: (x: number) => number;
|
|
64
|
-
/**
|
|
65
|
-
* Arcsine of `x` in radians, in [-π/2, π/2]; NaN outside [-1, 1]. Same bits
|
|
66
|
-
* on every engine; use instead of `Math.asin` in simulation code.
|
|
67
|
-
*
|
|
68
|
-
* @category Trigonometry
|
|
69
|
-
*/
|
|
70
|
-
export declare const asin: (x: number) => number;
|
|
71
|
-
/**
|
|
72
|
-
* Arccosine of `x` in radians, in [0, π]; NaN outside [-1, 1]. Same bits on
|
|
73
|
-
* every engine; use instead of `Math.acos` in simulation code.
|
|
74
|
-
*
|
|
75
|
-
* @category Trigonometry
|
|
76
|
-
*/
|
|
77
|
-
export declare const acos: (x: number) => number;
|
|
78
|
-
/**
|
|
79
|
-
* Arctangent of `x` in radians, in (-π/2, π/2). Same bits on every engine;
|
|
80
|
-
* use instead of `Math.atan` in simulation code. For the angle of a vector,
|
|
81
|
-
* use `atan2`.
|
|
82
|
-
*
|
|
83
|
-
* @category Trigonometry
|
|
84
|
-
*/
|
|
85
|
-
export declare const atan: (x: number) => number;
|
|
86
|
-
/**
|
|
87
|
-
* The angle of the vector (x, y) in radians, in [-π, π] (note the argument
|
|
88
|
-
* order: `y` first, like `Math.atan2`). Same bits on every engine; use
|
|
89
|
-
* instead of `Math.atan2` in simulation code.
|
|
90
|
-
*
|
|
91
|
-
* @category Trigonometry
|
|
92
|
-
*/
|
|
93
|
-
export declare const atan2: (y: number, x: number) => number;
|
|
94
|
-
/**
|
|
95
|
-
* e raised to `x`. Same bits on every engine (within 1 ulp of the exact
|
|
96
|
-
* value); use instead of `Math.exp` in simulation code, for example for
|
|
97
|
-
* frame-rate independent damping `exp(-rate * dt)`.
|
|
98
|
-
*
|
|
99
|
-
* @category Exponentials
|
|
100
|
-
*/
|
|
101
|
-
export declare const exp: (x: number) => number;
|
|
102
|
-
/**
|
|
103
|
-
* Hyperbolic tangent of `x`, in [-1, 1]. Same bits on every engine (within 2
|
|
104
|
-
* ulp of the exact value, like the native one); use instead of `Math.tanh` in
|
|
105
|
-
* simulation code.
|
|
106
|
-
*
|
|
107
|
-
* @category Hyperbolic
|
|
108
|
-
*/
|
|
109
|
-
export declare const tanh: (x: number) => number;
|
|
110
|
-
/**
|
|
111
|
-
* e raised to `x`, minus 1, accurate for `x` near 0 (where `exp(x) - 1` loses
|
|
112
|
-
* digits). Same bits on every engine; use instead of `Math.expm1` in
|
|
113
|
-
* simulation code.
|
|
114
|
-
*
|
|
115
|
-
* @category Exponentials
|
|
116
|
-
*/
|
|
117
|
-
export declare const expm1: (x: number) => number;
|
|
118
|
-
/**
|
|
119
|
-
* Natural logarithm of `x`: -Infinity at 0, NaN below 0. Same bits on every
|
|
120
|
-
* engine (within 1 ulp of the exact value); use instead of `Math.log` in
|
|
121
|
-
* simulation code.
|
|
122
|
-
*
|
|
123
|
-
* @category Exponentials
|
|
124
|
-
*/
|
|
125
|
-
export declare const log: (x: number) => number;
|
|
126
|
-
/**
|
|
127
|
-
* Base-2 logarithm of `x`: -Infinity at 0, NaN below 0, exact for powers of
|
|
128
|
-
* two. Same bits on every engine; use instead of `Math.log2` in simulation
|
|
129
|
-
* code.
|
|
130
|
-
*
|
|
131
|
-
* @category Exponentials
|
|
132
|
-
*/
|
|
133
|
-
export declare const log2: (x: number) => number;
|
|
134
|
-
/**
|
|
135
|
-
* Base-10 logarithm of `x`: -Infinity at 0, NaN below 0. Same bits on every
|
|
136
|
-
* engine; use instead of `Math.log10` in simulation code.
|
|
137
|
-
*
|
|
138
|
-
* @category Exponentials
|
|
139
|
-
*/
|
|
140
|
-
export declare const log10: (x: number) => number;
|
|
141
|
-
/**
|
|
142
|
-
* Natural logarithm of `1 + x`, accurate for `x` near 0 (where `log(1 + x)`
|
|
143
|
-
* loses digits). Same bits on every engine; use instead of `Math.log1p` in
|
|
144
|
-
* simulation code.
|
|
145
|
-
*
|
|
146
|
-
* @category Exponentials
|
|
147
|
-
*/
|
|
148
|
-
export declare const log1p: (x: number) => number;
|
|
149
|
-
/**
|
|
150
|
-
* Cube root of `x`, negative for negative `x`. Same bits on every engine;
|
|
151
|
-
* use instead of `Math.cbrt` and `x ** (1 / 3)` in simulation code.
|
|
152
|
-
*
|
|
153
|
-
* @category Exponentials
|
|
154
|
-
*/
|
|
155
|
-
export declare const cbrt: (x: number) => number;
|
|
156
|
-
/**
|
|
157
|
-
* `base` raised to `exponent`, with the results of `base ** exponent` for
|
|
158
|
-
* every special case (NaN, zeros, infinities, negative bases). Same bits on
|
|
159
|
-
* every engine (within 1 ulp of the exact value); use instead of `Math.pow`
|
|
160
|
-
* and `**` in simulation code. `x * x` and `x ** 2` are already exact.
|
|
161
|
-
*
|
|
162
|
-
* @category Exponentials
|
|
163
|
-
*/
|
|
164
|
-
export declare const pow: (base: number, exponent: number) => number;
|
|
165
|
-
/**
|
|
166
|
-
* Hyperbolic sine of `x`. Same bits on every engine (within 1 ulp of the
|
|
167
|
-
* exact value); use instead of `Math.sinh` in simulation code.
|
|
168
|
-
*
|
|
169
|
-
* @category Hyperbolic
|
|
170
|
-
*/
|
|
171
|
-
export declare const sinh: (x: number) => number;
|
|
172
|
-
/**
|
|
173
|
-
* Hyperbolic cosine of `x`, at least 1. Same bits on every engine (within 1
|
|
174
|
-
* ulp of the exact value); use instead of `Math.cosh` in simulation code.
|
|
175
|
-
*
|
|
176
|
-
* @category Hyperbolic
|
|
177
|
-
*/
|
|
178
|
-
export declare const cosh: (x: number) => number;
|
|
179
|
-
/**
|
|
180
|
-
* Inverse hyperbolic sine of `x`. Same bits on every engine; use instead of
|
|
181
|
-
* `Math.asinh` in simulation code.
|
|
182
|
-
*
|
|
183
|
-
* @category Hyperbolic
|
|
184
|
-
*/
|
|
185
|
-
export declare const asinh: (x: number) => number;
|
|
186
|
-
/**
|
|
187
|
-
* Inverse hyperbolic cosine of `x`: NaN below 1. Same bits on every engine;
|
|
188
|
-
* use instead of `Math.acosh` in simulation code.
|
|
189
|
-
*
|
|
190
|
-
* @category Hyperbolic
|
|
191
|
-
*/
|
|
192
|
-
export declare const acosh: (x: number) => number;
|
|
193
|
-
/**
|
|
194
|
-
* Inverse hyperbolic tangent of `x`: ±Infinity at ±1, NaN beyond. Same bits
|
|
195
|
-
* on every engine; use instead of `Math.atanh` in simulation code.
|
|
196
|
-
*
|
|
197
|
-
* @category Hyperbolic
|
|
198
|
-
*/
|
|
199
|
-
export declare const atanh: (x: number) => number;
|
|
200
|
-
/**
|
|
201
|
-
* The length of the vector (x, y), `sqrt(x² + y²)` without overflow or
|
|
202
|
-
* underflow. Same bits on every engine (within 1 ulp of the exact value);
|
|
203
|
-
* use instead of `Math.hypot` in simulation code. Takes exactly two numbers;
|
|
204
|
-
* for vectors use `Vec2.length` and `Vec2.distance`.
|
|
205
|
-
*
|
|
206
|
-
* @category Lengths
|
|
207
|
-
*/
|
|
208
|
-
export declare const hypot: (x: number, y: number) => number;
|
|
209
|
-
//# sourceMappingURL=Ops.d.ts.map
|
package/dist/Ops.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Ops.d.ts","sourceRoot":"","sources":["../src/Ops.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAIH;;;;;GAKG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAmB,CAAA;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAmB,CAAA;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAmB,CAAA;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAoB,CAAA;AAEtD;;;;;GAKG;AACH,eAAO,MAAM,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAoB,CAAA;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAoB,CAAA;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,EAAE,CAAC,CAAC,EAAE,MA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AAK,MAAqB,CAAA;AAExD;;;;;GAKG;AACH,eAAO,MAAM,KAAK,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAqB,CAAA;AAExD;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAqB,CAAA"}
|
package/dist/Ops.js
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Math functions that return the same bits on every JavaScript engine and
|
|
3
|
-
* CPU, so a simulation replays exactly on any device.
|
|
4
|
-
*
|
|
5
|
-
* The spec lets engines approximate `Math.sin`, `cos`, `tan`, `asin`, `acos`,
|
|
6
|
-
* `atan`, `atan2`, `exp`, `expm1`, `log`, `log2`, `log10`, `log1p`, `sinh`,
|
|
7
|
-
* `cosh`, `tanh`, `asinh`, `acosh`, `atanh`, `cbrt`, `pow`, `hypot`, and
|
|
8
|
-
* `a ** b`, and they do: Chrome, Safari, macOS, and Linux disagree in the
|
|
9
|
-
* last bit, and a simulation grows that bit into a different run. Use these
|
|
10
|
-
* in simulation code instead; each is a pure-JavaScript port of fdlibm (musl
|
|
11
|
-
* libm) built only from operations every engine rounds exactly.
|
|
12
|
-
*
|
|
13
|
-
* Keep using `Math.abs`, `sign`, `min`, `max`, `floor`, `ceil`, `round`,
|
|
14
|
-
* `trunc`, `sqrt`, `imul`, `fround`, `+ - * /`, and `x ** 2`: they are
|
|
15
|
-
* already exact everywhere. Use `Random` for randomness and `Clock` for time.
|
|
16
|
-
* Code that only draws (rendering, effects) can use `Math` freely. The
|
|
17
|
-
* `ritmo/use-ops-for-math` rule (`@typeonce/oxlint-plugin-ritmo`) points
|
|
18
|
-
* simulation code here.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* // Inside a system: aim, then move along the heading.
|
|
23
|
-
* const heading = Ops.atan2(target.y - position.y, target.x - position.x)
|
|
24
|
-
* const step = { x: Ops.cos(heading) * speed * dt, y: Ops.sin(heading) * speed * dt }
|
|
25
|
-
* const damping = Ops.exp(-friction * dt)
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @module Ops
|
|
29
|
-
* @docGroup helpers
|
|
30
|
-
*
|
|
31
|
-
* @categoryDescription Trigonometry
|
|
32
|
-
* Sine, cosine, tangent, and their inverses, in radians.
|
|
33
|
-
*
|
|
34
|
-
* @categoryDescription Exponentials
|
|
35
|
-
* Exponentials, logarithms, powers, and roots.
|
|
36
|
-
*
|
|
37
|
-
* @categoryDescription Hyperbolic
|
|
38
|
-
* Hyperbolic functions and their inverses.
|
|
39
|
-
*
|
|
40
|
-
* @categoryDescription Lengths
|
|
41
|
-
* Euclidean length without overflow.
|
|
42
|
-
*/
|
|
43
|
-
import * as fdlibm from "./internal/fdlibm.js";
|
|
44
|
-
/**
|
|
45
|
-
* Sine of `x` radians. Same bits on every engine (within 1 ulp of the exact
|
|
46
|
-
* value); use instead of `Math.sin` in simulation code.
|
|
47
|
-
*
|
|
48
|
-
* @category Trigonometry
|
|
49
|
-
*/
|
|
50
|
-
export const sin = fdlibm.sin;
|
|
51
|
-
/**
|
|
52
|
-
* Cosine of `x` radians. Same bits on every engine (within 1 ulp of the
|
|
53
|
-
* exact value); use instead of `Math.cos` in simulation code.
|
|
54
|
-
*
|
|
55
|
-
* @category Trigonometry
|
|
56
|
-
*/
|
|
57
|
-
export const cos = fdlibm.cos;
|
|
58
|
-
/**
|
|
59
|
-
* Tangent of `x` radians. Same bits on every engine (within 1 ulp of the
|
|
60
|
-
* exact value); use instead of `Math.tan` in simulation code.
|
|
61
|
-
*
|
|
62
|
-
* @category Trigonometry
|
|
63
|
-
*/
|
|
64
|
-
export const tan = fdlibm.tan;
|
|
65
|
-
/**
|
|
66
|
-
* Arcsine of `x` in radians, in [-π/2, π/2]; NaN outside [-1, 1]. Same bits
|
|
67
|
-
* on every engine; use instead of `Math.asin` in simulation code.
|
|
68
|
-
*
|
|
69
|
-
* @category Trigonometry
|
|
70
|
-
*/
|
|
71
|
-
export const asin = fdlibm.asin;
|
|
72
|
-
/**
|
|
73
|
-
* Arccosine of `x` in radians, in [0, π]; NaN outside [-1, 1]. Same bits on
|
|
74
|
-
* every engine; use instead of `Math.acos` in simulation code.
|
|
75
|
-
*
|
|
76
|
-
* @category Trigonometry
|
|
77
|
-
*/
|
|
78
|
-
export const acos = fdlibm.acos;
|
|
79
|
-
/**
|
|
80
|
-
* Arctangent of `x` in radians, in (-π/2, π/2). Same bits on every engine;
|
|
81
|
-
* use instead of `Math.atan` in simulation code. For the angle of a vector,
|
|
82
|
-
* use `atan2`.
|
|
83
|
-
*
|
|
84
|
-
* @category Trigonometry
|
|
85
|
-
*/
|
|
86
|
-
export const atan = fdlibm.atan;
|
|
87
|
-
/**
|
|
88
|
-
* The angle of the vector (x, y) in radians, in [-π, π] (note the argument
|
|
89
|
-
* order: `y` first, like `Math.atan2`). Same bits on every engine; use
|
|
90
|
-
* instead of `Math.atan2` in simulation code.
|
|
91
|
-
*
|
|
92
|
-
* @category Trigonometry
|
|
93
|
-
*/
|
|
94
|
-
export const atan2 = fdlibm.atan2;
|
|
95
|
-
/**
|
|
96
|
-
* e raised to `x`. Same bits on every engine (within 1 ulp of the exact
|
|
97
|
-
* value); use instead of `Math.exp` in simulation code, for example for
|
|
98
|
-
* frame-rate independent damping `exp(-rate * dt)`.
|
|
99
|
-
*
|
|
100
|
-
* @category Exponentials
|
|
101
|
-
*/
|
|
102
|
-
export const exp = fdlibm.exp;
|
|
103
|
-
/**
|
|
104
|
-
* Hyperbolic tangent of `x`, in [-1, 1]. Same bits on every engine (within 2
|
|
105
|
-
* ulp of the exact value, like the native one); use instead of `Math.tanh` in
|
|
106
|
-
* simulation code.
|
|
107
|
-
*
|
|
108
|
-
* @category Hyperbolic
|
|
109
|
-
*/
|
|
110
|
-
export const tanh = fdlibm.tanh;
|
|
111
|
-
/**
|
|
112
|
-
* e raised to `x`, minus 1, accurate for `x` near 0 (where `exp(x) - 1` loses
|
|
113
|
-
* digits). Same bits on every engine; use instead of `Math.expm1` in
|
|
114
|
-
* simulation code.
|
|
115
|
-
*
|
|
116
|
-
* @category Exponentials
|
|
117
|
-
*/
|
|
118
|
-
export const expm1 = fdlibm.expm1;
|
|
119
|
-
/**
|
|
120
|
-
* Natural logarithm of `x`: -Infinity at 0, NaN below 0. Same bits on every
|
|
121
|
-
* engine (within 1 ulp of the exact value); use instead of `Math.log` in
|
|
122
|
-
* simulation code.
|
|
123
|
-
*
|
|
124
|
-
* @category Exponentials
|
|
125
|
-
*/
|
|
126
|
-
export const log = fdlibm.log;
|
|
127
|
-
/**
|
|
128
|
-
* Base-2 logarithm of `x`: -Infinity at 0, NaN below 0, exact for powers of
|
|
129
|
-
* two. Same bits on every engine; use instead of `Math.log2` in simulation
|
|
130
|
-
* code.
|
|
131
|
-
*
|
|
132
|
-
* @category Exponentials
|
|
133
|
-
*/
|
|
134
|
-
export const log2 = fdlibm.log2;
|
|
135
|
-
/**
|
|
136
|
-
* Base-10 logarithm of `x`: -Infinity at 0, NaN below 0. Same bits on every
|
|
137
|
-
* engine; use instead of `Math.log10` in simulation code.
|
|
138
|
-
*
|
|
139
|
-
* @category Exponentials
|
|
140
|
-
*/
|
|
141
|
-
export const log10 = fdlibm.log10;
|
|
142
|
-
/**
|
|
143
|
-
* Natural logarithm of `1 + x`, accurate for `x` near 0 (where `log(1 + x)`
|
|
144
|
-
* loses digits). Same bits on every engine; use instead of `Math.log1p` in
|
|
145
|
-
* simulation code.
|
|
146
|
-
*
|
|
147
|
-
* @category Exponentials
|
|
148
|
-
*/
|
|
149
|
-
export const log1p = fdlibm.log1p;
|
|
150
|
-
/**
|
|
151
|
-
* Cube root of `x`, negative for negative `x`. Same bits on every engine;
|
|
152
|
-
* use instead of `Math.cbrt` and `x ** (1 / 3)` in simulation code.
|
|
153
|
-
*
|
|
154
|
-
* @category Exponentials
|
|
155
|
-
*/
|
|
156
|
-
export const cbrt = fdlibm.cbrt;
|
|
157
|
-
/**
|
|
158
|
-
* `base` raised to `exponent`, with the results of `base ** exponent` for
|
|
159
|
-
* every special case (NaN, zeros, infinities, negative bases). Same bits on
|
|
160
|
-
* every engine (within 1 ulp of the exact value); use instead of `Math.pow`
|
|
161
|
-
* and `**` in simulation code. `x * x` and `x ** 2` are already exact.
|
|
162
|
-
*
|
|
163
|
-
* @category Exponentials
|
|
164
|
-
*/
|
|
165
|
-
export const pow = fdlibm.pow;
|
|
166
|
-
/**
|
|
167
|
-
* Hyperbolic sine of `x`. Same bits on every engine (within 1 ulp of the
|
|
168
|
-
* exact value); use instead of `Math.sinh` in simulation code.
|
|
169
|
-
*
|
|
170
|
-
* @category Hyperbolic
|
|
171
|
-
*/
|
|
172
|
-
export const sinh = fdlibm.sinh;
|
|
173
|
-
/**
|
|
174
|
-
* Hyperbolic cosine of `x`, at least 1. Same bits on every engine (within 1
|
|
175
|
-
* ulp of the exact value); use instead of `Math.cosh` in simulation code.
|
|
176
|
-
*
|
|
177
|
-
* @category Hyperbolic
|
|
178
|
-
*/
|
|
179
|
-
export const cosh = fdlibm.cosh;
|
|
180
|
-
/**
|
|
181
|
-
* Inverse hyperbolic sine of `x`. Same bits on every engine; use instead of
|
|
182
|
-
* `Math.asinh` in simulation code.
|
|
183
|
-
*
|
|
184
|
-
* @category Hyperbolic
|
|
185
|
-
*/
|
|
186
|
-
export const asinh = fdlibm.asinh;
|
|
187
|
-
/**
|
|
188
|
-
* Inverse hyperbolic cosine of `x`: NaN below 1. Same bits on every engine;
|
|
189
|
-
* use instead of `Math.acosh` in simulation code.
|
|
190
|
-
*
|
|
191
|
-
* @category Hyperbolic
|
|
192
|
-
*/
|
|
193
|
-
export const acosh = fdlibm.acosh;
|
|
194
|
-
/**
|
|
195
|
-
* Inverse hyperbolic tangent of `x`: ±Infinity at ±1, NaN beyond. Same bits
|
|
196
|
-
* on every engine; use instead of `Math.atanh` in simulation code.
|
|
197
|
-
*
|
|
198
|
-
* @category Hyperbolic
|
|
199
|
-
*/
|
|
200
|
-
export const atanh = fdlibm.atanh;
|
|
201
|
-
/**
|
|
202
|
-
* The length of the vector (x, y), `sqrt(x² + y²)` without overflow or
|
|
203
|
-
* underflow. Same bits on every engine (within 1 ulp of the exact value);
|
|
204
|
-
* use instead of `Math.hypot` in simulation code. Takes exactly two numbers;
|
|
205
|
-
* for vectors use `Vec2.length` and `Vec2.distance`.
|
|
206
|
-
*
|
|
207
|
-
* @category Lengths
|
|
208
|
-
*/
|
|
209
|
-
export const hypot = fdlibm.hypot;
|
|
210
|
-
//# sourceMappingURL=Ops.js.map
|
package/dist/Ops.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Ops.js","sourceRoot":"","sources":["../src/Ops.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAA;AAE9C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,GAAG,GAA0B,MAAM,CAAC,GAAG,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,GAAG,GAA0B,MAAM,CAAC,GAAG,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,GAAG,GAA0B,MAAM,CAAC,GAAG,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,IAAI,GAA0B,MAAM,CAAC,IAAI,CAAA;AAEtD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,IAAI,GAA0B,MAAM,CAAC,IAAI,CAAA;AAEtD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,IAAI,GAA0B,MAAM,CAAC,IAAI,CAAA;AAEtD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,KAAK,GAAqC,MAAM,CAAC,KAAK,CAAA;AAEnE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,GAAG,GAA0B,MAAM,CAAC,GAAG,CAAA;AAEpD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|
-
*
|
|
5
|
-
* Keep the state in a resource, so a run replays exactly and saves with the
|
|
6
|
-
* world: declare it with `Random.codec` as its schema, start it with
|
|
7
|
-
* `seed`, and draw in systems through `draw` over its write cell, which writes
|
|
8
|
-
* every draw back (rolled back with a failed system). Two generators made
|
|
9
|
-
* from the same state draw the same sequence on every platform. `make` is for
|
|
10
|
-
* a state that lives outside the world (a level generator seeded once).
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const Rng = Descriptor.ConstructedResource(Random.codec)("Game/Rng")
|
|
15
|
-
* const runtime = Game.Runtime.make({ services, resources: { Rng: Random.seed(0x5eed) } })
|
|
16
|
-
*
|
|
17
|
-
* Game.System("Game/Spawn", { resources: { rng: Game.System.writeResource(Rng) } }, ({ resources }) => {
|
|
18
|
-
* const random = Random.draw(resources.rng)
|
|
19
|
-
* const angle = random.range(0, Math.PI * 2)
|
|
20
|
-
* const kind = random.pick(kinds) ?? "grunt"
|
|
21
|
-
* })
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @module Random
|
|
25
|
-
* @docGroup helpers
|
|
26
|
-
*
|
|
27
|
-
* @categoryDescription Random Types
|
|
28
|
-
* The generator interface.
|
|
29
|
-
*
|
|
30
|
-
* @categoryDescription Construction
|
|
31
|
-
* Making a generator from a stored state.
|
|
32
|
-
*
|
|
33
|
-
* @categoryDescription Resource
|
|
34
|
-
* Keeping the state in a world resource.
|
|
35
|
-
*/
|
|
36
|
-
import type * as Brand from "@typeonce/ritmo/Brand";
|
|
37
|
-
import type { WriteCell } from "@typeonce/ritmo/Query";
|
|
38
|
-
import type { StandardSchema } from "@typeonce/ritmo/Descriptor";
|
|
39
|
-
import * as Result from "@typeonce/ritmo/Result";
|
|
40
|
-
/**
|
|
41
|
-
* A generator. Every draw advances `state`.
|
|
42
|
-
*
|
|
43
|
-
* @category Random Types
|
|
44
|
-
*/
|
|
45
|
-
export interface Random {
|
|
46
|
-
/** The current state: an unsigned 32-bit integer. Store it to continue the sequence later. */
|
|
47
|
-
readonly state: number;
|
|
48
|
-
/** Uniform in [0, 1). */
|
|
49
|
-
next(): number;
|
|
50
|
-
/** Uniform in [min, max), or `min` when they are equal. Throws when `min > max` or a bound is not finite. */
|
|
51
|
-
range(min: number, max: number): number;
|
|
52
|
-
/** A uniform integer in [min, maxExclusive). Throws unless both bounds are integers with min < maxExclusive. */
|
|
53
|
-
integer(min: number, maxExclusive: number): number;
|
|
54
|
-
/** A uniform element of `items`, or `undefined` when it is empty (no draw is made then). */
|
|
55
|
-
pick<A>(items: ReadonlyArray<A>): A | undefined;
|
|
56
|
-
/** A shuffled copy of `items` (Fisher–Yates). */
|
|
57
|
-
shuffle<A>(items: ReadonlyArray<A>): Array<A>;
|
|
58
|
-
/**
|
|
59
|
-
* An element of `items` chosen with probability proportional to `weight`,
|
|
60
|
-
* or `undefined` when no element has a positive finite weight (no draw is
|
|
61
|
-
* made then). Elements with zero, negative, or non-finite weight are never
|
|
62
|
-
* chosen. `weight` is called once per element.
|
|
63
|
-
*/
|
|
64
|
-
weighted<A>(items: ReadonlyArray<A>, weight: (item: A) => number): A | undefined;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* A generator continuing from `state`. Any number is accepted and reduced to
|
|
68
|
-
* an unsigned 32-bit integer (`state >>> 0`), so a stored state round-trips
|
|
69
|
-
* exactly.
|
|
70
|
-
*
|
|
71
|
-
* @category Construction
|
|
72
|
-
*/
|
|
73
|
-
export declare const make: (state: number) => Random;
|
|
74
|
-
/**
|
|
75
|
-
* A generator's state as a resource holds it: an unsigned 32-bit integer.
|
|
76
|
-
* Only `seed`, `result`, and `codec` make one, so `draw` takes the cell of a
|
|
77
|
-
* random resource and nothing else.
|
|
78
|
-
*
|
|
79
|
-
* @category Resource
|
|
80
|
-
*/
|
|
81
|
-
export type State = Brand.Branded<number, "Random/State">;
|
|
82
|
-
/** Why a value is not a generator state. @category Resource */
|
|
83
|
-
export interface InvalidState {
|
|
84
|
-
readonly _tag: "InvalidRandomState";
|
|
85
|
-
readonly value: unknown;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Validates a state, for game code that builds one from a number.
|
|
89
|
-
*
|
|
90
|
-
* @category Resource
|
|
91
|
-
*/
|
|
92
|
-
export declare const result: (raw: number) => Result.Result<State, InvalidState>;
|
|
93
|
-
/**
|
|
94
|
-
* Whether any value is a generator state: a guard, also for building a
|
|
95
|
-
* state field in your own schema (`Schema.declare(Random.is)`).
|
|
96
|
-
*
|
|
97
|
-
* @category Resource
|
|
98
|
-
*/
|
|
99
|
-
export declare const is: (raw: unknown) => raw is State;
|
|
100
|
-
/**
|
|
101
|
-
* The codec of a random resource's state (a Standard Schema, see
|
|
102
|
-
* `Descriptor.Codec`): `Descriptor.ConstructedResource(Random.codec)`. Its
|
|
103
|
-
* saved form is the number itself, so it needs no `encode`.
|
|
104
|
-
*
|
|
105
|
-
* @category Resource
|
|
106
|
-
*/
|
|
107
|
-
export declare const codec: StandardSchema<number, State>;
|
|
108
|
-
/**
|
|
109
|
-
* The starting state for a seed, reduced to an unsigned 32-bit integer like
|
|
110
|
-
* `make`: the value to give a random resource in `Game.Runtime.make`.
|
|
111
|
-
*
|
|
112
|
-
* @category Resource
|
|
113
|
-
*/
|
|
114
|
-
export declare const seed: (value: number) => State;
|
|
115
|
-
/**
|
|
116
|
-
* A generator over a random resource's write cell: every draw continues from
|
|
117
|
-
* the stored state and writes the new one back, so the system needs no
|
|
118
|
-
* `set` of its own, a failed run rolls the draws back, and generators made
|
|
119
|
-
* from the same cell in one run continue one sequence.
|
|
120
|
-
*
|
|
121
|
-
* @category Resource
|
|
122
|
-
*/
|
|
123
|
-
export declare const draw: (cell: WriteCell<State>) => Random;
|
|
124
|
-
//# sourceMappingURL=Random.d.ts.map
|
package/dist/Random.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Random.d.ts","sourceRoot":"","sources":["../src/Random.ts"],"names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,CAAC,IAAI,EAAE,oBAAoB,CAAA;IACnC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CACxB;AAKD;;;;GAIG;AACH,eAAO,MAAM,MAAM,QAAS,MAAM,KAAG,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,YAAY,CAC2B,CAAA;AAEjG;;;;;GAKG;AACH,eAAO,MAAM,EAAE,QAAS,OAAO,KAAG,GAAG,IAAI,KAAqB,CAAA;AAE9D;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,EAAE,cAAc,CAAC,MAAM,EAAE,KAAK,CAG/C,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,IAAI,UAAW,MAAM,KAAG,KAA+B,CAAA;AAmBpE;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,SAAU,SAAS,CAAC,KAAK,CAAC,KAAG,MAA8B,CAAA"}
|
package/dist/Random.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import * as Result from "@typeonce/ritmo/Result";
|
|
2
|
-
import { codecOf } from "./internal/codec.js";
|
|
3
|
-
const usable = (weight) => weight > 0 && weight < Infinity;
|
|
4
|
-
class Mulberry32 {
|
|
5
|
-
state;
|
|
6
|
-
constructor(state) {
|
|
7
|
-
this.state = state >>> 0;
|
|
8
|
-
}
|
|
9
|
-
next() {
|
|
10
|
-
this.state = (this.state + 0x6d2b79f5) >>> 0;
|
|
11
|
-
let t = this.state;
|
|
12
|
-
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
13
|
-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
14
|
-
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
15
|
-
}
|
|
16
|
-
range(min, max) {
|
|
17
|
-
if (!(Number.isFinite(min) && Number.isFinite(max) && min <= max)) {
|
|
18
|
-
throw new Error(`Random.range needs finite bounds with min <= max, got [${min}, ${max})`);
|
|
19
|
-
}
|
|
20
|
-
return min + (max - min) * this.next();
|
|
21
|
-
}
|
|
22
|
-
integer(min, maxExclusive) {
|
|
23
|
-
if (!(Number.isSafeInteger(min) && Number.isSafeInteger(maxExclusive) && min < maxExclusive)) {
|
|
24
|
-
throw new Error(`Random.integer needs whole bounds with min below maxExclusive, got [${min}, ${maxExclusive})`);
|
|
25
|
-
}
|
|
26
|
-
return min + Math.floor(this.next() * (maxExclusive - min));
|
|
27
|
-
}
|
|
28
|
-
pick(items) {
|
|
29
|
-
return items.length === 0 ? undefined : items[Math.floor(this.next() * items.length)];
|
|
30
|
-
}
|
|
31
|
-
shuffle(items) {
|
|
32
|
-
const shuffled = items.slice();
|
|
33
|
-
for (let index = shuffled.length - 1; index > 0; index--) {
|
|
34
|
-
const other = Math.floor(this.next() * (index + 1));
|
|
35
|
-
const swap = shuffled[index];
|
|
36
|
-
shuffled[index] = shuffled[other];
|
|
37
|
-
shuffled[other] = swap;
|
|
38
|
-
}
|
|
39
|
-
return shuffled;
|
|
40
|
-
}
|
|
41
|
-
weighted(items, weight) {
|
|
42
|
-
// `weight` is called once per item: it may be costly or not pure.
|
|
43
|
-
const weights = items.map(weight);
|
|
44
|
-
let total = 0;
|
|
45
|
-
for (const value of weights) {
|
|
46
|
-
if (usable(value))
|
|
47
|
-
total += value;
|
|
48
|
-
}
|
|
49
|
-
if (!usable(total))
|
|
50
|
-
return undefined;
|
|
51
|
-
let roll = this.next() * total;
|
|
52
|
-
let last;
|
|
53
|
-
for (let index = 0; index < items.length; index++) {
|
|
54
|
-
const value = weights[index];
|
|
55
|
-
if (!usable(value))
|
|
56
|
-
continue;
|
|
57
|
-
last = items[index];
|
|
58
|
-
roll -= value;
|
|
59
|
-
if (roll < 0)
|
|
60
|
-
return items[index];
|
|
61
|
-
}
|
|
62
|
-
// Rounding can leave a sliver of the roll past the last bucket.
|
|
63
|
-
return last;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* A generator continuing from `state`. Any number is accepted and reduced to
|
|
68
|
-
* an unsigned 32-bit integer (`state >>> 0`), so a stored state round-trips
|
|
69
|
-
* exactly.
|
|
70
|
-
*
|
|
71
|
-
* @category Construction
|
|
72
|
-
*/
|
|
73
|
-
export const make = (state) => new Mulberry32(state);
|
|
74
|
-
const isState = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 0xffffffff;
|
|
75
|
-
/**
|
|
76
|
-
* Validates a state, for game code that builds one from a number.
|
|
77
|
-
*
|
|
78
|
-
* @category Resource
|
|
79
|
-
*/
|
|
80
|
-
export const result = (raw) => isState(raw) ? Result.success(raw) : Result.failure({ _tag: "InvalidRandomState", value: raw });
|
|
81
|
-
/**
|
|
82
|
-
* Whether any value is a generator state: a guard, also for building a
|
|
83
|
-
* state field in your own schema (`Schema.declare(Random.is)`).
|
|
84
|
-
*
|
|
85
|
-
* @category Resource
|
|
86
|
-
*/
|
|
87
|
-
export const is = (raw) => isState(raw);
|
|
88
|
-
/**
|
|
89
|
-
* The codec of a random resource's state (a Standard Schema, see
|
|
90
|
-
* `Descriptor.Codec`): `Descriptor.ConstructedResource(Random.codec)`. Its
|
|
91
|
-
* saved form is the number itself, so it needs no `encode`.
|
|
92
|
-
*
|
|
93
|
-
* @category Resource
|
|
94
|
-
*/
|
|
95
|
-
export const codec = codecOf((raw) => isState(raw) ? Result.success(raw) : Result.failure({ _tag: "InvalidRandomState", value: raw }), "a generator state: an unsigned 32-bit integer");
|
|
96
|
-
/**
|
|
97
|
-
* The starting state for a seed, reduced to an unsigned 32-bit integer like
|
|
98
|
-
* `make`: the value to give a random resource in `Game.Runtime.make`.
|
|
99
|
-
*
|
|
100
|
-
* @category Resource
|
|
101
|
-
*/
|
|
102
|
-
export const seed = (value) => (value >>> 0);
|
|
103
|
-
class CellRandom extends Mulberry32 {
|
|
104
|
-
#cell;
|
|
105
|
-
constructor(cell) {
|
|
106
|
-
super(cell.get());
|
|
107
|
-
this.#cell = cell;
|
|
108
|
-
}
|
|
109
|
-
next() {
|
|
110
|
-
// Read the cell each time, so two generators over one cell share one sequence.
|
|
111
|
-
this.state = this.#cell.get();
|
|
112
|
-
const value = super.next();
|
|
113
|
-
this.#cell.set(this.state);
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* A generator over a random resource's write cell: every draw continues from
|
|
119
|
-
* the stored state and writes the new one back, so the system needs no
|
|
120
|
-
* `set` of its own, a failed run rolls the draws back, and generators made
|
|
121
|
-
* from the same cell in one run continue one sequence.
|
|
122
|
-
*
|
|
123
|
-
* @category Resource
|
|
124
|
-
*/
|
|
125
|
-
export const draw = (cell) => new CellRandom(cell);
|
|
126
|
-
//# sourceMappingURL=Random.js.map
|