Skip to content

Commit a55ad86

Browse files
refactor: update PRNG & seed handling in platform adapters
1 parent cfb5acf commit a55ad86

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

packages/adapter-editart/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const SUPPORTED_TYPES = ["choice", "range", "toggle", "weighted"];
2525

2626
const {
2727
math: { clamp, round, fit, mix },
28-
prng: { defPRNG, sfc32 },
28+
prng: { SFC32 },
2929
utils: { isString, hashString },
3030
} = $genart;
3131

@@ -48,6 +48,7 @@ export class EditArtAdapter implements PlatformAdapter {
4848
protected _selectedParamIDs?: string[];
4949
protected _cache: Record<string, string> = {};
5050
protected _prng!: PRNG;
51+
protected _seed!: string;
5152
protected _screen: ScreenConfig;
5253

5354
constructor() {
@@ -112,6 +113,10 @@ export class EditArtAdapter implements PlatformAdapter {
112113
return this._prng;
113114
}
114115

116+
get seed() {
117+
return this._seed;
118+
}
119+
115120
configure(opts: Partial<EditArtAdapterOpts>) {
116121
if (opts.params) {
117122
if (opts.params.length > MAX_PARAMS) {
@@ -223,7 +228,8 @@ export class EditArtAdapter implements PlatformAdapter {
223228
for (let i = 0; i < MAX_PARAMS; i++) {
224229
seedStr += this._searchParams.get("m" + i) || "0.5";
225230
}
226-
this._prng = defPRNG(seedStr, hashString(seedStr), sfc32);
231+
this._seed = seedStr;
232+
this._prng = new SFC32(hashString(seedStr));
227233
}
228234

229235
protected serializeParam(spec: Param<any>) {

packages/adapter-fxhash/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const UPDATE_MAP: Record<ParamUpdateBehavior, FxParam["update"]> = {
5858
};
5959

6060
const {
61-
prng: { defPRNG, sfc32 },
61+
prng: { SFC32 },
6262
utils: { equiv, isString, hashString },
6363
} = $genart;
6464

@@ -172,10 +172,11 @@ export class FxhashAdapter implements PlatformAdapter {
172172
}
173173

174174
get prng() {
175-
return (
176-
this._prng ||
177-
(this._prng = defPRNG($fx.hash, hashString($fx.hash), sfc32))
178-
);
175+
return this._prng || (this._prng = new SFC32(hashString($fx.hash)));
176+
}
177+
178+
get seed() {
179+
return $fx.hash;
179180
}
180181

181182
get collector() {

packages/adapter-layer/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const TYPE_MAP: Record<
4343
};
4444

4545
const {
46-
prng: { defPRNG, sfc32 },
46+
prng: { SFC32 },
4747
utils: { equiv, isString, parseUUID },
4848
} = $genart;
4949

@@ -122,10 +122,11 @@ class LayerAdapter implements PlatformAdapter {
122122
}
123123

124124
get prng() {
125-
return (
126-
this._prng ||
127-
(this._prng = defPRNG($layer.uuid, parseUUID($layer.uuid), sfc32))
128-
);
125+
return this._prng || (this._prng = new SFC32(parseUUID($layer.uuid)));
126+
}
127+
128+
get seed() {
129+
return $layer.uuid;
129130
}
130131

131132
async updateParam(id: string, _: Param<any>) {

packages/adapter-urlparams/src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { compressBytes, decompressBytes } from "./compress.js";
1717

1818
const {
1919
math: { clamp01, parseNum },
20-
prng: { defPRNG, sfc32, randomBigInt },
20+
prng: { SFC32, randomBigInt },
2121
utils: { formatValuePrec, parseBigInt128, stringifyBigInt },
2222
} = $genart;
2323

@@ -35,6 +35,7 @@ class URLParamsAdapter implements PlatformAdapter {
3535
protected params: URLSearchParams;
3636
protected cache: Record<string, string> = {};
3737
protected _prng!: PRNG;
38+
protected _seed!: string;
3839
protected _screen: ScreenConfig;
3940

4041
constructor() {
@@ -116,6 +117,10 @@ class URLParamsAdapter implements PlatformAdapter {
116117
return this._prng;
117118
}
118119

120+
get seed() {
121+
return this._seed;
122+
}
123+
119124
augmentParams(params: ParamSpecs) {
120125
const group = this.id;
121126
return {
@@ -127,7 +132,7 @@ class URLParamsAdapter implements PlatformAdapter {
127132
desc: "Manually defined seed value",
128133
min: 0n,
129134
max: MAX_SEED - 1n,
130-
default: BigInt(this._prng.seed),
135+
default: BigInt(this._seed),
131136
update: "reload",
132137
}),
133138
[WIDTH]: $genart.params.range({
@@ -284,11 +289,8 @@ class URLParamsAdapter implements PlatformAdapter {
284289
protected initPRNG() {
285290
const seedParam = this.params.get(SEED);
286291
const seed = seedParam ? BigInt(seedParam) : randomBigInt(MAX_SEED);
287-
this._prng = defPRNG(
288-
stringifyBigInt(seed),
289-
parseBigInt128(seed),
290-
sfc32
291-
);
292+
this._seed = stringifyBigInt(seed);
293+
this._prng = new SFC32(parseBigInt128(seed));
292294
}
293295
}
294296

0 commit comments

Comments
 (0)