1
2
3
4
5
6
7
8
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use ::smrng::drops::{analysis::DropAnalysis, Drop, DropSet};
use ::smrng::*;
use serde::Serialize;
use clap::{builder::BoolishValueParser, Parser, Subcommand};
use std::{collections::HashMap, num::ParseIntError, process::exit};
#[derive(Parser, Debug)]
struct Args {
/// Whether to simulate RNG behavior in an XBA room.
#[arg(short, long, num_args = 0..=1, default_missing_value = "true", value_parser = BoolishValueParser::new(), global = true)]
xba: Option<bool>,
/// How many RNG calls to simulate per frame.
#[arg(short = 'n', long, default_value = "1", global = true)]
calls_per_frame: usize,
/// The initial seed value. Can be a number, or 'reset', 'beetom', 'sidehopper', or 'polyp'.
/// Defaults to 'reset'.
#[arg(short = 'i', long, value_parser = parse_seed, global = true)]
seed: Option<Rng>,
/// Output in JSON format.
#[arg(short, long, global = true)]
json: bool,
#[command(subcommand)]
command: Command,
}
fn parse_seed(seed: &str) -> Result<Rng, ParseIntError> {
match seed.to_lowercase().as_str() {
"reset" => Ok(Rng::RESET),
s if s.starts_with("power") => Ok(Rng::RESET),
"beetom" => Ok(Rng::BEETOM),
"sidehopper" | "hopper" => Ok(Rng::SIDEHOPPER),
"polyp" => Ok(Rng::POLYP),
n if n.starts_with("0x") => Ok(Rng {
seed: u16::from_str_radix(&n[2..], 16)?,
..Rng::RESET
}),
n => Ok(Rng {
seed: n.parse()?,
..Rng::RESET
}),
}
}
impl Args {
fn rng(&self) -> Rng {
let mut rng = self.seed.clone().unwrap_or(Rng::RESET);
rng.calls_per_frame = self.calls_per_frame;
rng.xba = self.xba.unwrap_or(rng.xba);
rng
}
}
#[derive(Subcommand, Debug)]
enum Command {
/// Print information about RNG loops and branches.
Loops,
/// Print generated random numbers to standard output.
Dump {
#[arg(
short,
long = "loop",
name = "loop",
default_missing_value = "0",
num_args = 0..=1
)]
/// Dump only the values that are part of an RNG loop.
/// You can optionally specify a loop ID as returned by `rng loops`.
loop_id: Option<usize>,
/// Dump only the values that are part of branch <BRANCH>, as returned by `rng loops`.
#[arg(short, long, conflicts_with = "loop")]
branch: Option<usize>,
/// Output numbers in hexadecimal.
#[arg(long, conflicts_with = "json")]
hex: bool,
},
/// Print drop chances for an enemy
Drops {
/// How many of the enemy are killed with a single shot.
#[arg(short, long, default_value = "1")]
count: u32,
/// Whether to ignore correlation between consecutive RNG calls.
#[arg(long)]
uncorrelated: bool,
/// Whether to pretend the game generates drops uniformly, instead of using a PRNG.
#[arg(long, conflicts_with = "uncorrelated")]
ideal: bool,
/// Output a histogram of drop chances instead of probabilities.
#[arg(long, conflicts_with = "uncorrelated", conflicts_with = "ideal")]
histogram: bool,
/// Only consider RNG seeds that are part of a loop.
/// You can optionally specify a loop ID as returned by `rng loops`.
///
/// If no seed is specified, loop 0 of the reset seed is assumed automatically (the 2280 loop).
#[arg(
short,
long = "loop",
name = "loop",
default_missing_value = "0",
num_args = 0..=1
)]
loop_id: Option<usize>,
/// Only consider RNG seeds that are part of branch <BRANCH>, as returned by `rng loops`.
#[arg(short, long, conflicts_with = "loop")]
branch: Option<usize>,
/// Consider all 65536 RNG seeds, rather than just descendents of a given seed.
///
/// In most circumstances, you want `--all_seeds` when you use `--xba`.
#[arg(short, long, conflicts_with = "branch", conflicts_with = "loop")]
all_seeds: bool,
/// The player is full on energy.
#[arg(short = 'e')]
full_energy: bool,
/// The player is full on missiles.
#[arg(short = 'm', long)]
full_missiles: bool,
/// The player is full on super missiles.
#[arg(short = 's', long)]
full_supers: bool,
/// The player is full on power bombs.
#[arg(short = 'p', long)]
full_pbs: bool,
/// [histogram mode] Only include energy drops in the output.
#[arg(short = 'E', long, requires = "histogram")]
filter_energy: bool,
/// [histogram mode] Only include missiles drops in the output.
#[arg(short = 'M', long, requires = "histogram")]
filter_missiles: bool,
/// [histogram mode] Only include super missile drops in the output.
#[arg(short = 'S', long, requires = "histogram")]
filter_supers: bool,
/// [histogram mode] Only include power bomb drops in the output.
#[arg(short = 'P', long, requires = "histogram")]
filter_pbs: bool,
/// The enemy name.
enemy: String,
},
}
fn main() {
let args = Args::parse();
match args.command {
Command::Loops => {
let analysis = args.rng().analyze();
if args.json {
serde_json::to_writer(std::io::stdout(), &analysis).unwrap();
} else {
analysis.print();
}
}
Command::Dump {
loop_id,
branch,
hex,
} => {
let mut output = Vec::new();
if let Some(loop_id) = loop_id {
let analysis = args.rng().analyze();
let Some(l) = analysis.loops.get(loop_id) else {
eprintln!("Loop index out of range 0..={}", analysis.loops.len());
exit(2);
};
output = l.seeds.to_vec();
} else if let Some(branch_id) = branch {
let analysis = args.rng().analyze();
let Some(b) = analysis.branches.get(branch_id) else {
eprintln!("Branch index out of range 0..={}", analysis.branches.len());
exit(2);
};
output = b.seeds.to_vec();
} else {
let mut seen = vec![false; 0x10000];
let mut rng = args.rng();
while !seen[rng.seed as usize] {
output.push(rng.seed);
seen[rng.seed as usize] = true;
rng.frame_advance();
}
}
if args.json {
serde_json::to_writer(std::io::stdout(), &output).unwrap();
} else {
for seed in output {
if hex {
println!("{seed:#06x}");
} else {
println!("{seed}");
}
}
}
}
Command::Drops {
count,
uncorrelated,
ideal,
histogram,
mut loop_id,
branch,
all_seeds,
ref enemy,
full_energy,
full_missiles,
full_supers,
full_pbs,
filter_energy,
filter_missiles,
filter_supers,
filter_pbs,
} => {
let Some(drop_table) = drops::ENEMY_DROPS.get(enemy) else {
eprintln!("Unknown enemy {enemy}");
exit(2)
};
if loop_id.is_none() && branch.is_none() && !all_seeds && args.seed.is_none() {
loop_id = Some(0);
}
let rng = args.rng();
let seeds: Vec<u16> = if all_seeds {
(0..=u16::MAX).collect()
} else if let Some(loop_id) = loop_id {
let mut analysis = args.rng().analyze();
let Some(l) = analysis.loops.get_mut(loop_id) else {
eprintln!("Loop index out of range 0..={}", analysis.loops.len());
exit(2);
};
std::mem::take(&mut l.seeds)
} else if let Some(branch_id) = branch {
let mut analysis = args.rng().analyze();
let Some(b) = analysis.branches.get_mut(branch_id) else {
eprintln!("Branch index out of range 0..={}", analysis.branches.len());
exit(2);
};
std::mem::take(&mut b.seeds)
} else {
rng.seeds_until_loop().collect()
};
let mut possible_drops = DropSet::ALL;
if full_energy {
possible_drops -= &DropSet::from_iter([Drop::SmallEnergy, Drop::BigEnergy]);
}
if full_missiles {
possible_drops -= &DropSet::from_iter([Drop::Missile]);
}
if full_supers {
possible_drops -= &DropSet::from_iter([Drop::SuperMissile]);
}
if full_pbs {
possible_drops -= &DropSet::from_iter([Drop::PowerBomb]);
}
if histogram {
let no_filters =
!filter_energy && !filter_missiles && !filter_pbs && !filter_supers;
let include_energy = no_filters || filter_energy;
let include_missiles = no_filters || filter_missiles;
let include_supers = no_filters || filter_supers;
let include_pbs = no_filters || filter_pbs;
let mut histogram = HashMap::<DropAnalysis, u32>::new();
for &seed in &seeds {
let mut analysis = drops::analysis::analyze_correlated(
drop_table,
&possible_drops,
count,
rng.clone(),
std::iter::once(seed),
);
analysis.nothing = 0;
if !include_energy {
analysis.small_energy = 0;
analysis.big_energy = 0;
}
if !include_missiles {
analysis.missile = 0;
}
if !include_supers {
analysis.super_missile = 0;
}
if !include_pbs {
analysis.power_bomb = 0;
}
*histogram.entry(analysis).or_default() += 1;
}
let mut histogram: Vec<_> = histogram
.into_iter()
.map(|(entry, count)| DropAnalysis {
seeds: count,
..entry
})
.collect();
histogram.sort_by_key(|DropAnalysis { seeds, .. }| u32::MAX - *seeds);
if args.json {
serde_json::to_writer(std::io::stdout(), &histogram).unwrap();
} else {
print!("# ");
if include_energy {
print!("| Small E| Big E");
}
if include_missiles {
print!("| Missile");
}
if include_supers {
print!("| Super");
}
if include_pbs {
print!("| PB");
}
println!();
print!("-------------");
if include_energy {
print!("+--------+--------");
}
if include_missiles {
print!("+--------");
}
if include_supers {
print!("+--------");
}
if include_pbs {
print!("+--------");
}
println!();
for entry in histogram {
print!(
"{:>5} ({}%)",
entry.seeds,
format_percentage(entry.seeds, seeds.len() as u32),
);
if include_energy {
print!("|{:>8}|{:>8}", entry.small_energy, entry.big_energy);
}
if include_missiles {
print!("|{:>8}", entry.missile);
}
if include_supers {
print!("|{:>8}", entry.super_missile);
}
if include_pbs {
print!("|{:>8}", entry.power_bomb);
}
println!();
}
}
} else if ideal {
let get_stat = |drop| drop_table.ideal_drops_per_farm(drop, &possible_drops, count);
if args.json {
#[derive(Serialize)]
struct Output {
small_energy: f32,
big_energy: f32,
missile: f32,
super_missile: f32,
power_bomb: f32,
}
let output = Output {
small_energy: get_stat(Drop::SmallEnergy),
big_energy: get_stat(Drop::BigEnergy),
missile: get_stat(Drop::Missile),
super_missile: get_stat(Drop::SuperMissile),
power_bomb: get_stat(Drop::PowerBomb),
};
serde_json::to_writer_pretty(std::io::stdout(), &output).unwrap();
} else {
let print_stat = |name, drop| println!("{name:>8} | {:.3}", get_stat(drop));
println!("Resource | Drops");
println!("---------+------");
print_stat("Small E", Drop::SmallEnergy);
print_stat("Big E", Drop::BigEnergy);
print_stat("Missile", Drop::Missile);
print_stat("Super", Drop::SuperMissile);
print_stat("PB", Drop::PowerBomb);
};
} else {
let analysis = if uncorrelated {
drops::analysis::analyze_uncorrelated(drop_table, &possible_drops, count, seeds)
} else {
drops::analysis::analyze_correlated(
drop_table,
&possible_drops,
count,
rng.clone(),
seeds,
)
};
if args.json {
serde_json::to_writer_pretty(std::io::stdout(), &analysis).unwrap();
} else {
let print_stat = |name, stat| {
println!("{name:>8} | {:.3}", stat as f32 / analysis.seeds as f32)
};
println!("Resource | Drops");
println!("---------+------");
print_stat("Small E", analysis.small_energy);
print_stat("Big E", analysis.big_energy);
print_stat("Missile", analysis.missile);
print_stat("Super", analysis.super_missile);
print_stat("PB", analysis.power_bomb);
}
}
}
}
}
fn format_percentage(num: u32, denom: u32) -> String {
let percentage = (num as f32) / (denom as f32) * 100.;
let digits_before_decimal = (percentage.floor() + 0.1).log10().max(1.).ceil() as usize;
let digits_after_decimal = 3 - digits_before_decimal;
format!(
"{:b$.a$}",
percentage,
b = digits_before_decimal,
a = digits_after_decimal
)
}