@wildshard/shard-format 0.14.0 → 0.16.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.
@@ -0,0 +1,111 @@
1
+ /* oxlint-disable capitalized-comments, curly, id-length, import/exports-last, import/group-exports, no-bitwise, no-inline-comments, no-magic-numbers, no-plusplus, no-use-before-define, typescript/explicit-member-accessibility, typescript/no-unsafe-type-assertion, typescript/prefer-readonly-parameter-types, unicorn/number-literal-case, unicorn/numeric-separators-style, unicorn/prefer-at -- verbatim port of the engine's law (SDK-SPLIT SP4); bit-exact output is pinned by golden vectors, so it keeps the engine's form */
2
+ /** SDK-SPLIT SP4a (Q35): the seeded RNG both the bake and the engine draw from, so their draws agree bit for bit.
3
+ * Moved from engine `shared/engine/core/rng.ts` (its named-stream service and page salt stay in the engine, which
4
+ * builds them on this). `seeded-rng.golden.json` holds the engine's own draws as float64 bits. */
5
+
6
+ /** Mulberry32 continuation, including fork seed and the legacy scrambled-fork mode. */
7
+ export interface RngState {
8
+ version: number
9
+ state: number
10
+ initial: number
11
+ scrambledFork: boolean
12
+ }
13
+ /** Public seeded draws and continuation; independent of a generator's private storage. */
14
+ export interface RngPort {
15
+ readonly next: () => number
16
+ readonly range: (a: number, b: number) => number
17
+ readonly int: (a: number, b: number) => number
18
+ readonly pick: <T>(arr: readonly T[]) => T
19
+ readonly chance: (p: number) => boolean
20
+ readonly weighted: <T>(pairs: readonly (readonly [T, number])[]) => T
21
+ readonly fork: (salt: number | string) => RngPort
22
+ readonly snapshot: () => RngState
23
+ readonly restore: (state: RngState) => void
24
+ }
25
+ const uint32 = (value: number): boolean =>
26
+ Number.isInteger(value) && value >= 0 && value <= 0xffffffff
27
+ /** Throws a RangeError unless `state` is a version-1 continuation of uint32 words. */
28
+ export function validateRngState(state: RngState): void {
29
+ if (
30
+ state.version !== 1 ||
31
+ !uint32(state.state) ||
32
+ !uint32(state.initial) ||
33
+ typeof state.scrambledFork !== 'boolean'
34
+ )
35
+ throw new RangeError('Invalid RNG snapshot')
36
+ }
37
+ /** Deterministic PRNG (mulberry32) so every playtest sees the same chunk. */
38
+ export class Rng implements RngPort {
39
+ private s: number
40
+ private initial: number
41
+ private scrambledFork = false
42
+ constructor(seed: number) {
43
+ this.s = seed >>> 0
44
+ this.initial = this.s
45
+ }
46
+ snapshot(): RngState {
47
+ return {
48
+ version: 1,
49
+ state: this.s,
50
+ initial: this.initial,
51
+ scrambledFork: this.scrambledFork,
52
+ }
53
+ }
54
+ restore(state: RngState): void {
55
+ validateRngState(state)
56
+ this.s = state.state
57
+ this.initial = state.initial
58
+ this.scrambledFork = state.scrambledFork
59
+ }
60
+ /** Preserve the facade grammar's multiplicative constructor and numeric forks. */
61
+ static scrambled(seed: number): Rng {
62
+ const rng = new Rng((seed * 2654435761) >>> 0)
63
+ rng.scrambledFork = true
64
+ return rng
65
+ }
66
+ next(): number {
67
+ this.s = (this.s + 0x6d2b79f5) >>> 0
68
+ let t = this.s
69
+ t = Math.imul(t ^ (t >>> 15), t | 1)
70
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
71
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296
72
+ }
73
+ range(a: number, b: number): number {
74
+ return a + (b - a) * this.next()
75
+ }
76
+ int(a: number, b: number): number {
77
+ return Math.floor(this.range(a, b + 1))
78
+ }
79
+ pick<T>(arr: readonly T[]): T {
80
+ return arr[Math.floor(this.next() * arr.length)] as T
81
+ } // an empty list yields undefined, as it always did
82
+ chance(p: number): boolean {
83
+ return this.next() < p
84
+ }
85
+ weighted<T>(pairs: readonly (readonly [T, number])[]): T {
86
+ let sum = 0
87
+ for (const [, weight] of pairs) sum += weight
88
+ let r = this.next() * sum
89
+ for (const [item, weight] of pairs) {
90
+ r -= weight
91
+ if (r <= 0) return item
92
+ }
93
+ const last = pairs[pairs.length - 1]
94
+ if (last === undefined) throw new Error('Rng.weighted on an empty list')
95
+ return last[0]
96
+ }
97
+ fork(salt: number | string): Rng {
98
+ if (typeof salt === 'string')
99
+ return new Rng(fnv1a32(`${String(this.initial)}:${salt}`))
100
+ const seed = (this.s ^ Math.imul(salt + 1, 0x9e3779b1)) >>> 0
101
+ return this.scrambledFork ? Rng.scrambled(seed) : new Rng(seed)
102
+ }
103
+ }
104
+
105
+ /** 32-bit FNV-1a over a string's code points (the string-fork and named-stream seed hash). */
106
+ export function fnv1a32(text: string): number {
107
+ let hash = 0x811c9dc5
108
+ for (let i = 0; i < text.length; i++)
109
+ hash = Math.imul(hash ^ (text.codePointAt(i) ?? 0), 0x01000193)
110
+ return hash >>> 0
111
+ }
@@ -0,0 +1,57 @@
1
+ {
2
+ "about": "SDK-SPLIT SP4f1: engine host/headlessSections.ts, grid/frameModel.ts ROAD_SKY, grid/farView.ts FAR_CLIFF_FLOOR and plugin/clientChannelView.ts moverChannelValues over small-laws.golden-specs.ts",
3
+ "sectionNames": [
4
+ "actors/species-programs",
5
+ "view/modules",
6
+ "sim/scripts",
7
+ "actors/spawn-scripts",
8
+ "actors/return-scripts",
9
+ "actors/moves",
10
+ "actors/strikes",
11
+ "world/targets",
12
+ "sim/runtime-plan",
13
+ "sim/bodies"
14
+ ],
15
+ "sections": [
16
+ [{ "ok": [] }, { "ok": [] }],
17
+ [
18
+ { "ok": [["world/targets", { "targets": [], "spots": [] }]] },
19
+ { "ok": [["world/targets", { "targets": [], "spots": [] }]] }
20
+ ],
21
+ [
22
+ { "error": "Invalid type: Expected Object but received \"nope\"" },
23
+ { "error": "Invalid type: Expected Object but received \"nope\"" }
24
+ ]
25
+ ],
26
+ "roadSky": {
27
+ "air": [0.95, 0.56, 0.44],
28
+ "mid": [0.58, 0.46, 0.8],
29
+ "zenith": [0.1, 0.24, 0.72],
30
+ "line": [0.25, 1.1, 1.4],
31
+ "grid": [0.3, 0.85, 1.1],
32
+ "gridStrength": 0.3,
33
+ "void": [0.17, 0.17, 0.27],
34
+ "sun": {
35
+ "azimuth": 70,
36
+ "elevation": 13,
37
+ "colour": [1, 0.8, 0.66],
38
+ "intensity": 2.4,
39
+ "glow": [1, 0.62, 0.42]
40
+ },
41
+ "hemi": {
42
+ "sky": [0.6, 0.62, 0.86],
43
+ "ground": [0.42, 0.36, 0.34],
44
+ "intensity": 1.1
45
+ },
46
+ "env": 0.7,
47
+ "fogSun": [1, 0.72, 0.56]
48
+ },
49
+ "farCliffFloor": 0.8,
50
+ "mover": [
51
+ { "ok": [1, 1.5, -2, 3.25, 0.1, -0.2, 0.3] },
52
+ { "ok": [0.5, -7, 0] },
53
+ { "error": "Missing copied mover field" },
54
+ { "error": "Missing copied mover field" },
55
+ { "error": "Missing copied mover field" }
56
+ ]
57
+ }