Skip to content

Commit 5f72ab2

Browse files
committed
Add filters and reduce to one generation function
1 parent cf3ac23 commit 5f72ab2

File tree

5 files changed

+230
-581
lines changed

5 files changed

+230
-581
lines changed

src/filter.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
use crate::{Advance, Pokemon};
2+
3+
#[derive(Default)]
4+
pub struct Filter {
5+
pub shiny: bool,
6+
pub species: Option<u16>,
7+
pub min_ivs: [u8; 6],
8+
pub max_ivs: [u8; 6],
9+
pub ability: Option<u8>,
10+
pub nature: Option<u8>,
11+
pub item: Option<u16>,
12+
pub egg_move: Option<u16>,
13+
pub gender: Option<u8>,
14+
}
15+
16+
impl Filter {
17+
pub fn shiny(mut self, shiny: bool) -> Filter {
18+
self.shiny = shiny;
19+
self
20+
}
21+
22+
pub fn species(mut self, species: u16) -> Filter {
23+
self.species = Some(species);
24+
self
25+
}
26+
27+
pub fn min_ivs(mut self, min_ivs: [u8; 6]) -> Filter {
28+
self.min_ivs = min_ivs;
29+
self
30+
}
31+
32+
pub fn max_ivs(mut self, max_ivs: [u8; 6]) -> Filter {
33+
self.max_ivs = max_ivs;
34+
self
35+
}
36+
37+
pub fn ability(mut self, ability: u8) -> Filter {
38+
self.ability = Some(ability);
39+
self
40+
}
41+
42+
pub fn nature(mut self, nature: u8) -> Filter {
43+
self.nature = Some(nature);
44+
self
45+
}
46+
47+
pub fn item(mut self, item: u16) -> Filter {
48+
self.item = Some(item);
49+
self
50+
}
51+
52+
pub fn egg_move(mut self, egg_move: u16) -> Filter {
53+
self.egg_move = Some(egg_move);
54+
self
55+
}
56+
57+
pub fn gender(mut self, gender: u8) -> Filter {
58+
self.gender = Some(gender);
59+
self
60+
}
61+
62+
pub fn passes_filter(&self, advance: &Advance) -> bool {
63+
for pokemon in advance.regular_pokemon.iter() {
64+
if self.check_pokemon(pokemon) {
65+
return true;
66+
}
67+
}
68+
69+
if let Some(pokemon) = &advance.rare_pokemon {
70+
if self.check_pokemon(pokemon) {
71+
return true;
72+
}
73+
}
74+
75+
false
76+
}
77+
78+
fn check_pokemon(&self, pokemon: &Pokemon) -> bool {
79+
if let Some(species) = self.species {
80+
if pokemon.species != species {
81+
return false;
82+
}
83+
}
84+
85+
if self.shiny && !pokemon.shiny {
86+
return false;
87+
}
88+
89+
let mut passes_ivs = true;
90+
for (i, iv) in pokemon.ivs.iter().enumerate() {
91+
if !(self.min_ivs[i]..=self.max_ivs[i]).contains(iv) {
92+
passes_ivs = false;
93+
break;
94+
}
95+
}
96+
97+
if !passes_ivs {
98+
return false;
99+
}
100+
101+
if let Some(ability) = self.ability {
102+
if pokemon.ability != ability {
103+
return false;
104+
}
105+
}
106+
107+
if let Some(nature) = self.nature {
108+
if pokemon.nature != nature {
109+
return false;
110+
}
111+
}
112+
113+
if let Some(item) = self.item {
114+
if pokemon.item != item {
115+
return false;
116+
}
117+
}
118+
119+
if pokemon.egg_move != self.egg_move {
120+
return false;
121+
}
122+
123+
if let Some(gender) = self.gender {
124+
if pokemon.gender != gender {
125+
return false;
126+
}
127+
}
128+
129+
true
130+
}
131+
}

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
mod flag_util;
2-
mod personal_info;
3-
mod personal_info_bdsp;
4-
mod personal_table;
2+
pub mod personal_info;
3+
pub mod personal_info_bdsp;
4+
pub mod personal_table;
55
mod resource_util;
6-
mod run_print;
76
mod run_results;
87
mod xorshift;
8+
mod filter;
99

10-
pub use run_print::run_print;
1110
pub use run_results::*;
11+
pub use filter::*;
1212

1313
use crate::personal_info::PersonalInfo;
1414
use crate::xorshift::XorShift;

src/main.rs

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![allow(unused)]
22

3-
use bdsp_ug_generator::{run_print, RoomType, Version};
3+
use bdsp_ug_generator::{run_results, Pokemon, RoomType, Version, ABILITIES_EN, GENDER_SYMBOLS, ITEMS_EN, MOVES_EN, NATURES_EN, SPECIES_EN, personal_table, Filter};
44
use clap::Parser;
5+
use std::fmt::Write;
56

67
#[derive(Parser)]
78
struct Cli {
@@ -13,13 +14,44 @@ struct Cli {
1314
story_flag: u8,
1415
#[clap(short = 's', long)]
1516
shiny_only: bool,
17+
#[clap(long, default_value = "0/0/0/0/0/0")]
18+
min_ivs: String,
19+
#[clap(long, default_value = "31/31/31/31/31/31")]
20+
max_ivs: String,
21+
#[clap(long)]
22+
species: Option<u16>,
23+
#[clap(long)]
24+
nature: Option<u8>,
25+
#[clap(long)]
26+
ability: Option<u8>,
27+
#[clap(long)]
28+
item: Option<u16>,
29+
#[clap(long)]
30+
egg_move: Option<u16>,
31+
#[clap(long)]
32+
gender: Option<u8>,
1633
advances: u32,
1734
s0: String,
1835
s1: String,
1936
s2: String,
2037
s3: String,
2138
}
2239

40+
fn write_pokemon(pokemon: &Pokemon, string: &mut String) {
41+
let personal_info = personal_table::BDSP.get_form_entry(pokemon.species as usize, 0);
42+
let ability = match pokemon.ability {
43+
0 => personal_info.get_ability_1(),
44+
_ => personal_info.get_ability_2()
45+
};
46+
writeln!(string, "Species: {}\nPID: {:08X} EC: {:08X} Shiny: {}\nIVs: {:?} Ability: {} Gender: {}\nNature: {} Item: {}{}\n", SPECIES_EN[pokemon.species as usize], pokemon.pid, pokemon.ec, pokemon.shiny, pokemon.ivs, ABILITIES_EN[ability], GENDER_SYMBOLS[pokemon.gender as usize], NATURES_EN[pokemon.nature as usize].trim(),
47+
ITEMS_EN[pokemon.item as usize].trim(),
48+
if let Some(no) = pokemon.egg_move {
49+
format!(" Egg Move: {}", MOVES_EN[no as usize].trim())
50+
} else {
51+
"".to_string()
52+
}).unwrap();
53+
}
54+
2355
fn main() {
2456
let cli: Cli = Cli::parse();
2557

@@ -38,7 +70,38 @@ fn main() {
3870
println!("s3: {:#08X}", s3);
3971
println!();
4072

41-
run_print(
73+
let min_split = cli.min_ivs.split('/');
74+
let max_split = cli.max_ivs.split('/');
75+
76+
let mut min_ivs = [0, 0, 0, 0, 0, 0];
77+
78+
for (i, val) in min_split.take(6).enumerate() {
79+
if !val.is_empty() {
80+
min_ivs[i] = val.parse::<u8>().expect(&format!("Failed to parse min iv {}", i));
81+
}
82+
}
83+
84+
let mut max_ivs = [31, 31, 31, 31, 31, 31];
85+
86+
for (i, val) in max_split.take(6).enumerate() {
87+
if !val.is_empty() {
88+
max_ivs[i] = val.parse::<u8>().expect(&format!("Failed to parse max iv {}", i));
89+
}
90+
}
91+
92+
let filter = Filter {
93+
shiny: cli.shiny_only,
94+
species: cli.species,
95+
min_ivs,
96+
max_ivs,
97+
ability: cli.ability,
98+
nature: cli.nature,
99+
item: cli.item,
100+
egg_move: cli.egg_move,
101+
gender: cli.gender
102+
};
103+
104+
let results = run_results(
42105
cli.advances,
43106
s0,
44107
s1,
@@ -47,6 +110,26 @@ fn main() {
47110
cli.version,
48111
cli.story_flag,
49112
cli.room,
50-
cli.shiny_only,
113+
filter
51114
);
115+
116+
let mut print = String::new();
117+
118+
for (advance, result) in results.iter().enumerate() {
119+
writeln!(
120+
print,
121+
"-------------------------------------------\nAdvances: {}",
122+
advance
123+
)
124+
.unwrap();
125+
for pokemon in result.regular_pokemon.iter() {
126+
write_pokemon(pokemon, &mut print);
127+
}
128+
129+
if let Some(pokemon) = &result.rare_pokemon {
130+
write_pokemon(pokemon, &mut print);
131+
}
132+
}
133+
134+
println!("{}", print);
52135
}

0 commit comments

Comments
 (0)