Skip to content

Commit d3e60b3

Browse files
mia-pi-gitZarel
andauthored
Sim: Use a CSPRNG (#10806)
* Sim: Use a CSPRNG * Add test * fix test prng * move prng test to others * fix slight hack * tf? * Fuck this * fucking lol * fix crap * i'm going to kill someone * i hate state * fix test * Good work genius * typo * Fix exportinputlog * Refactor for inputlog backwards compatibility This is a pretty major refactor which is mostly unrelated to the feature, but it does make the code a lot simpler. * Readability pass * Readability (again) * Remove sodium-native dependency * Refactor to serialize seeds in hex strings (Also removes the Buffer dependency from PRNG, and slightly improves comments.) * Apparently << is 32-bit signed * Readability --------- Co-authored-by: Guangcong Luo <[email protected]>
1 parent 66792c9 commit d3e60b3

File tree

27 files changed

+276
-107
lines changed

27 files changed

+276
-107
lines changed

data/cg-teams.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ export default class TeamGenerator {
10281028

10291029
const totalWeight = weights.reduce((a, b) => a + b, 0);
10301030

1031-
let randomWeight = this.prng.next(0, totalWeight);
1031+
let randomWeight = this.prng.random(0, totalWeight);
10321032
for (let i = 0; i < choices.length; i++) {
10331033
randomWeight -= weights[i];
10341034
if (randomWeight < 0) {
@@ -1043,6 +1043,6 @@ export default class TeamGenerator {
10431043
}
10441044

10451045
setSeed(seed: PRNGSeed) {
1046-
this.prng.seed = seed;
1046+
this.prng.setSeed(seed);
10471047
}
10481048
}

data/mods/gen2/moves.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ export const Moves: import('../../../sim/dex-moves').ModdedMoveDataTable = {
590590
this.debug('Pursuit start');
591591
let alreadyAdded = false;
592592
for (const source of this.effectState.sources) {
593-
if (source.speed < pokemon.speed || (source.speed === pokemon.speed && this.random(2) === 0)) {
593+
if (source.speed < pokemon.speed || (source.speed === pokemon.speed && this.randomChance(1, 2))) {
594594
// Destiny Bond ends if the switch action "outspeeds" the attacker, regardless of host
595595
pokemon.removeVolatile('destinybond');
596596
}

data/mods/gen5/conditions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export const Conditions: import('../../../sim/dex-conditions').ModdedConditionDa
3232
// However, just in case, use 1 if it is undefined.
3333
const counter = this.effectState.counter || 1;
3434
if (counter >= 256) {
35-
// 2^32 - special-cased because Battle.random(n) can't handle n > 2^16 - 1
36-
return (this.random() * 4294967296 < 1);
35+
return this.randomChance(1, 2 ** 32);
3736
}
3837
this.debug("Success chance: " + Math.round(100 / counter) + "%");
3938
return this.randomChance(1, counter);

data/mods/gen9ssb/moves.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ export const Moves: import('../../../sim/dex-moves').ModdedMoveDataTable = {
12481248
target.clearBoosts();
12491249
this.add('-clearboost', target);
12501250
target.addVolatile('protect');
1251-
const set = Math.floor(Math.random() * 4);
1251+
const set = this.random(4);
12521252
const newMoves = [];
12531253
let role = '';
12541254
switch (set) {
@@ -2608,7 +2608,7 @@ export const Moves: import('../../../sim/dex-moves').ModdedMoveDataTable = {
26082608
const spd = target.getStat('spd', false, true);
26092609
const physical = Math.floor(Math.floor(Math.floor(Math.floor(2 * pokemon.level / 5 + 2) * 90 * atk) / def) / 50);
26102610
const special = Math.floor(Math.floor(Math.floor(Math.floor(2 * pokemon.level / 5 + 2) * 90 * spa) / spd) / 50);
2611-
if (physical > special || (physical === special && this.random(2) === 0)) {
2611+
if (physical > special || (physical === special && this.randomChance(1, 2))) {
26122612
move.category = 'Physical';
26132613
move.flags.contact = 1;
26142614
}

data/moves.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7352,7 +7352,7 @@ export const Moves: import('../sim/dex-moves').MoveDataTable = {
73527352
isMax: "Snorlax",
73537353
self: {
73547354
onHit(source) {
7355-
if (this.random(2) === 0) return;
7355+
if (this.randomChance(1, 2)) return;
73567356
for (const pokemon of source.alliesAndSelf()) {
73577357
if (pokemon.item) continue;
73587358

@@ -7448,12 +7448,12 @@ export const Moves: import('../sim/dex-moves').MoveDataTable = {
74487448
isMax: "Grimmsnarl",
74497449
onHit(target) {
74507450
if (target.status || !target.runStatusImmunity('slp')) return;
7451-
if (this.random(2) === 0) return;
7451+
if (this.randomChance(1, 2)) return;
74527452
target.addVolatile('yawn');
74537453
},
74547454
onAfterSubDamage(damage, target) {
74557455
if (target.status || !target.runStatusImmunity('slp')) return;
7456-
if (this.random(2) === 0) return;
7456+
if (this.randomChance(1, 2)) return;
74577457
target.addVolatile('yawn');
74587458
},
74597459
secondary: null,
@@ -16812,7 +16812,7 @@ export const Moves: import('../sim/dex-moves').MoveDataTable = {
1681216812
const spd = target.getStat('spd', false, true);
1681316813
const physical = Math.floor(Math.floor(Math.floor(Math.floor(2 * pokemon.level / 5 + 2) * 90 * atk) / def) / 50);
1681416814
const special = Math.floor(Math.floor(Math.floor(Math.floor(2 * pokemon.level / 5 + 2) * 90 * spa) / spd) / 50);
16815-
if (physical > special || (physical === special && this.random(2) === 0)) {
16815+
if (physical > special || (physical === special && this.randomChance(1, 2))) {
1681616816
move.category = 'Physical';
1681716817
move.flags.contact = 1;
1681816818
}

data/random-battles/gen1/teams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class RandomGen1Teams extends RandomGen2Teams {
107107
this.enforceNoDirectCustomBanlistChanges();
108108

109109
// Get what we need ready.
110-
const seed = this.prng.seed;
110+
const seed = this.prng.getSeed();
111111
const ruleTable = this.dex.formats.getRuleTable(this.format);
112112
const pokemon: RandomTeamsTypes.RandomSet[] = [];
113113

data/random-battles/gen3/teams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ export class RandomGen3Teams extends RandomGen4Teams {
639639
randomTeam() {
640640
this.enforceNoDirectCustomBanlistChanges();
641641

642-
const seed = this.prng.seed;
642+
const seed = this.prng.getSeed();
643643
const ruleTable = this.dex.formats.getRuleTable(this.format);
644644
const pokemon: RandomTeamsTypes.RandomSet[] = [];
645645

data/random-battles/gen5/teams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ export class RandomGen5Teams extends RandomGen6Teams {
846846
randomTeam() {
847847
this.enforceNoDirectCustomBanlistChanges();
848848

849-
const seed = this.prng.seed;
849+
const seed = this.prng.getSeed();
850850
const ruleTable = this.dex.formats.getRuleTable(this.format);
851851
const pokemon: RandomTeamsTypes.RandomSet[] = [];
852852

data/random-battles/gen7/teams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ export class RandomGen7Teams extends RandomGen8Teams {
11821182
randomTeam() {
11831183
this.enforceNoDirectCustomBanlistChanges();
11841184

1185-
const seed = this.prng.seed;
1185+
const seed = this.prng.getSeed();
11861186
const ruleTable = this.dex.formats.getRuleTable(this.format);
11871187
const pokemon: RandomTeamsTypes.RandomSet[] = [];
11881188

data/random-battles/gen8/teams.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export class RandomGen8Teams {
270270
}
271271

272272
random(m?: number, n?: number) {
273-
return this.prng.next(m, n);
273+
return this.prng.random(m, n);
274274
}
275275

276276
/**
@@ -2479,7 +2479,7 @@ export class RandomGen8Teams {
24792479
randomTeam() {
24802480
this.enforceNoDirectCustomBanlistChanges();
24812481

2482-
const seed = this.prng.seed;
2482+
const seed = this.prng.getSeed();
24832483
const ruleTable = this.dex.formats.getRuleTable(this.format);
24842484
const pokemon: RandomTeamsTypes.RandomSet[] = [];
24852485

@@ -3112,7 +3112,7 @@ export class RandomGen8Teams {
31123112
for (const speciesName of pokemonPool) {
31133113
const sortObject = {
31143114
speciesName: speciesName,
3115-
score: Math.pow(this.prng.next(), 1 / this.randomBSSFactorySets[speciesName].usage),
3115+
score: Math.pow(this.prng.random(), 1 / this.randomBSSFactorySets[speciesName].usage),
31163116
};
31173117
shuffledSpecies.push(sortObject);
31183118
}

0 commit comments

Comments
 (0)