use crate::defines::{LevelInt, MAX_MOVES}; use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary; use crate::dynamic_data::models::battle::Battle; use crate::dynamic_data::models::learned_move::LearnedMove; use crate::dynamic_data::script_handling::script::Script; use crate::dynamic_data::script_handling::script_set::ScriptSet; use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts; use crate::dynamic_data::script_handling::ScriptSource; use crate::static_data::items::item::Item; use crate::static_data::natures::Nature; use crate::static_data::species_data::ability_index::AbilityIndex; use crate::static_data::species_data::form::Form; use crate::static_data::species_data::gender::Gender; use crate::static_data::species_data::species::Species; use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet}; use crate::static_data::statistics::Statistic; use crate::utils::random::Random; use crate::{PkmnResult, ScriptCategory}; use std::sync::{Arc, RwLock, Weak}; #[derive(Debug)] pub struct PokemonBattleData<'a> { battle: Weak>>, battle_side_index: u8, index: u8, on_battle_field: bool, seen_opponents: Vec>>>, } impl<'a> PokemonBattleData<'a> { pub fn battle(&'a mut self) -> &'a mut Weak>> { &mut self.battle } pub fn battle_side_index(&self) -> u8 { self.battle_side_index } pub fn on_battle_field(&'a mut self) -> &mut bool { &mut self.on_battle_field } pub fn seen_opponents(&'a mut self) -> &'a mut Vec>>> { &mut self.seen_opponents } } #[derive(Debug)] pub struct Pokemon<'a> { library: &'a DynamicLibrary<'a>, species: &'a Species<'a>, form: &'a Form<'a>, display_species: Option<&'a Species<'a>>, display_form: Option<&'a Form<'a>>, level: LevelInt, experience: u32, unique_identifier: u32, gender: Gender, coloring: u8, held_item: Option<&'a Item>, current_health: u32, weight: f32, height: f32, stat_boost: ClampedStatisticSet, flat_stats: StatisticSet, boosted_stats: StatisticSet, individual_values: ClampedStatisticSet, effort_values: ClampedStatisticSet, nature: &'a Nature, nickname: Option, ability_index: AbilityIndex, is_ability_overridden: bool, battle_data: Option>, moves: [Option; MAX_MOVES], allowed_experience: bool, types: Vec, ability_script: Option>, status_script: Option>, volatile: Arc>, } impl<'a> Pokemon<'a> { pub fn new( library: &'a DynamicLibrary, species: &'a Species, form: &'a Form, ability: AbilityIndex, level: LevelInt, unique_identifier: u32, gender: Gender, coloring: u8, nature: String, ) -> Pokemon<'a> { // Calculate experience from the level for the specified growth rate. let experience = library .static_data() .growth_rates() .calculate_experience(species.growth_rate(), level); let health = form.get_base_stat(Statistic::HP) as u32; let weight = form.weight(); let height = form.height(); let nature = library .static_data() .natures() .get_nature(&nature) .expect("Unknown nature name was given."); let mut pokemon = Pokemon { library, species, form, display_species: None, display_form: None, level, experience, unique_identifier, gender, coloring, held_item: None, current_health: health, weight, height, stat_boost: Default::default(), flat_stats: Default::default(), boosted_stats: Default::default(), individual_values: Default::default(), effort_values: Default::default(), nature, nickname: None, ability_index: ability, is_ability_overridden: false, battle_data: None, moves: [None, None, None, None], allowed_experience: false, types: form.types().to_vec(), ability_script: None, status_script: None, volatile: Default::default(), }; pokemon.recalculate_flat_stats(); pokemon } pub fn library(&self) -> &'a DynamicLibrary<'a> { self.library } pub fn species(&self) -> &'a Species<'a> { self.species } pub fn form(&self) -> &'a Form<'a> { self.form } pub fn display_species(&self) -> &'a Species<'a> { if let Some(v) = self.display_species { v } else { self.species } } pub fn display_form(&self) -> &'a Form<'a> { if let Some(v) = self.display_form { v } else { self.form } } pub fn level(&self) -> LevelInt { self.level } pub fn experience(&self) -> u32 { self.experience } pub fn unique_identifier(&self) -> u32 { self.unique_identifier } pub fn gender(&self) -> Gender { self.gender } pub fn coloring(&self) -> u8 { self.coloring } pub fn held_item(&self) -> Option<&'a Item> { self.held_item } pub fn current_health(&self) -> u32 { self.current_health } pub fn max_health(&self) -> u32 { self.boosted_stats.hp() } pub fn weight(&self) -> f32 { self.weight } pub fn height(&self) -> f32 { self.height } pub fn nickname(&self) -> &Option { &self.nickname } pub fn real_ability(&self) -> &AbilityIndex { &self.ability_index } pub fn types(&self) -> &Vec { &self.types } pub fn learned_moves(&self) -> &[Option; MAX_MOVES] { &self.moves } pub fn status(&self) -> &Option> { &self.status_script } pub fn flat_stats(&self) -> &StatisticSet { &self.flat_stats } pub fn boosted_stats(&self) -> &StatisticSet { &self.boosted_stats } pub fn stat_boost(&self) -> &ClampedStatisticSet { &self.stat_boost } pub fn individual_values(&self) -> &ClampedStatisticSet { &self.individual_values } pub fn effort_values(&self) -> &ClampedStatisticSet { &self.effort_values } pub fn get_battle(&self) -> Option<&Weak>>> { if let Some(data) = &self.battle_data { Some(&data.battle) } else { None } } pub fn get_battle_side_index(&self) -> Option { self.battle_data.as_ref().map(|data| data.battle_side_index) } pub fn get_battle_index(&self) -> Option { self.battle_data.as_ref().map(|data| data.index) } pub fn is_ability_overriden(&self) -> bool { self.is_ability_overridden } pub fn active_ability(&self) -> &Option> { &self.ability_script } pub fn seen_opponents(&self) -> Option<&Vec>>>> { if let Some(data) = &self.battle_data { Some(&data.seen_opponents) } else { None } } pub fn allowed_experience_gain(&self) -> bool { self.allowed_experience } pub fn nature(&self) -> &'a Nature { self.nature } pub fn recalculate_flat_stats(&mut self) { self.flat_stats = self.library.stat_calculator().calculate_flat_stats(self); self.recalculate_boosted_stats(); } pub fn recalculate_boosted_stats(&mut self) { self.boosted_stats = self.library.stat_calculator().calculate_boosted_stats(self); } pub fn change_species(&mut self, species: &'a Species, form: &'a Form) { self.species = species; self.form = form; // If the pokemon is genderless, but it's new species is not, we want to set its gender if self.gender != Gender::Genderless && species.gender_rate() < 0.0 { if self.battle_data.is_some() { let battle_data = self.battle_data.as_mut().unwrap(); self.gender = species.get_random_gender( &mut battle_data .battle .upgrade() .unwrap() .read() .unwrap() .random() .get_rng() .lock() .unwrap(), ); } else { self.gender = species.get_random_gender(&mut Random::default()); } } // Else if the new species is genderless, but the pokemon has a gender, make the creature genderless. else if species.gender_rate() < 0.0 && self.gender != Gender::Genderless { self.gender = Gender::Genderless; } // TODO: Battle Event trigger } pub fn is_usable(&self) -> bool { todo!() } pub fn set_battle_data(&mut self, battle: Weak>>, battle_side_index: u8) { if let Some(battle_data) = &mut self.battle_data { battle_data.battle = battle; battle_data.battle_side_index = battle_side_index; } else { self.battle_data = Some(PokemonBattleData { battle, battle_side_index, index: 0, on_battle_field: false, seen_opponents: Default::default(), }) } } pub fn set_on_battlefield(&mut self, value: bool) { if let Some(data) = &mut self.battle_data { data.on_battle_field = value; if !value { self.volatile.write().unwrap().clear(); self.weight = self.form.weight(); self.height = self.form.height(); } } } pub fn set_battle_index(&mut self, index: u8) { if let Some(data) = &mut self.battle_data { data.index = index; } } pub fn is_on_battlefield(&self) -> bool { if let Some(data) = &self.battle_data { data.on_battle_field } else { false } } pub fn mark_opponent_as_seen(&mut self, pokemon: Weak>>) { if let Some(battle_data) = &mut self.battle_data { for seen_opponent in &battle_data.seen_opponents { if seen_opponent.ptr_eq(&pokemon) { return; } } battle_data.seen_opponents.push(pokemon); } } } impl<'a> ScriptSource for Pokemon<'a> { fn get_script_count(&self) { todo!() } } impl<'a> VolatileScripts<'a> for Pokemon<'a> { fn volatile_scripts(&self) -> &Arc> { &self.volatile } fn load_volatile_script(&self, key: &str) -> PkmnResult> { self.library.load_script(ScriptCategory::Pokemon, key) } } #[cfg(test)] pub mod test { use crate::dynamic_data::libraries::dynamic_library; use crate::dynamic_data::models::pokemon::Pokemon; use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::species_data::ability_index::AbilityIndex; use crate::static_data::species_data::gender::Gender; #[test] fn construct_pokemon() { let lib = dynamic_library::test::build(); let species = lib.static_data().species().get("foo").unwrap(); let form = species.get_form("default").unwrap(); let pokemon = Pokemon::new( &lib, species, form, AbilityIndex { hidden: false, index: 0, }, 10, 0, Gender::Male, 0, "test_nature".to_string(), ); assert_eq!(pokemon.species.name(), "foo"); assert_eq!(pokemon.form.name(), "default"); } }