2021-01-31 16:31:22 +00:00
|
|
|
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;
|
2022-06-03 14:35:18 +00:00
|
|
|
use crate::dynamic_data::script_handling::ScriptSource;
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::static_data::items::item::Item;
|
|
|
|
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::StatisticSet;
|
|
|
|
use crate::static_data::statistics::Statistic;
|
2022-06-03 14:35:18 +00:00
|
|
|
use crate::utils::random::Random;
|
2021-01-31 16:31:22 +00:00
|
|
|
use derive_getters::Getters;
|
|
|
|
use std::collections::HashSet;
|
2022-06-04 10:40:32 +00:00
|
|
|
use std::sync::{RwLock, Weak};
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
#[derive(Debug)]
|
2021-01-31 16:31:22 +00:00
|
|
|
pub struct PokemonBattleData<'a> {
|
2022-06-04 10:40:32 +00:00
|
|
|
battle: Weak<RwLock<Battle<'a>>>,
|
2022-06-03 14:35:18 +00:00
|
|
|
battle_side_index: u8,
|
|
|
|
index: u8,
|
2021-01-31 16:31:22 +00:00
|
|
|
on_battle_field: bool,
|
2022-06-03 14:35:18 +00:00
|
|
|
seen_opponents: HashSet<u32>,
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
impl<'a> PokemonBattleData<'a> {
|
2022-06-04 10:40:32 +00:00
|
|
|
pub fn battle(&'a mut self) -> &'a mut Weak<RwLock<Battle<'a>>> {
|
2022-06-03 14:35:18 +00:00
|
|
|
&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 HashSet<u32> {
|
|
|
|
&mut self.seen_opponents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Getters, Debug)]
|
2021-01-31 16:31:22 +00:00
|
|
|
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>,
|
|
|
|
health: u32,
|
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
weight: f32,
|
|
|
|
height: f32,
|
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
stat_boost: StatisticSet<i8>,
|
|
|
|
flat_stats: StatisticSet<u16>,
|
|
|
|
boosted_stats: StatisticSet<u16>,
|
|
|
|
|
|
|
|
nickname: Option<String>,
|
|
|
|
|
|
|
|
ability_index: AbilityIndex,
|
|
|
|
is_ability_overridden: bool,
|
|
|
|
overridden_ability_name: String,
|
|
|
|
|
|
|
|
battle_data: Option<PokemonBattleData<'a>>,
|
|
|
|
|
|
|
|
moves: [Option<LearnedMove>; MAX_MOVES],
|
|
|
|
allowed_experience: bool,
|
|
|
|
|
|
|
|
types: Vec<u8>,
|
|
|
|
|
|
|
|
ability_script: Option<Box<dyn Script>>,
|
|
|
|
status_script: Option<Box<dyn Script>>,
|
|
|
|
volatile: ScriptSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
) -> 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;
|
2022-06-03 14:35:18 +00:00
|
|
|
let weight = *form.weight();
|
|
|
|
let height = *form.height();
|
2021-01-31 16:31:22 +00:00
|
|
|
Pokemon {
|
|
|
|
library,
|
|
|
|
species,
|
|
|
|
form,
|
|
|
|
display_species: None,
|
|
|
|
display_form: None,
|
|
|
|
level,
|
|
|
|
experience,
|
|
|
|
unique_identifier,
|
|
|
|
gender,
|
|
|
|
coloring,
|
|
|
|
held_item: None,
|
|
|
|
health,
|
2022-06-03 14:35:18 +00:00
|
|
|
weight,
|
|
|
|
height,
|
2021-01-31 16:31:22 +00:00
|
|
|
stat_boost: Default::default(),
|
|
|
|
flat_stats: *form.base_stats(),
|
|
|
|
boosted_stats: *form.base_stats(),
|
|
|
|
nickname: None,
|
|
|
|
ability_index: ability,
|
|
|
|
is_ability_overridden: false,
|
|
|
|
overridden_ability_name: "".to_string(),
|
|
|
|
battle_data: None,
|
|
|
|
moves: [None, None, None, None],
|
|
|
|
allowed_experience: false,
|
|
|
|
types: form.types().to_vec(),
|
|
|
|
ability_script: None,
|
|
|
|
status_script: None,
|
|
|
|
volatile: ScriptSet {},
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
|
|
|
|
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() {
|
2022-06-04 10:40:32 +00:00
|
|
|
let battle_data = self.battle_data.as_mut().unwrap();
|
2022-06-03 14:35:18 +00:00
|
|
|
self.gender = species.get_random_gender(
|
2022-06-04 10:40:32 +00:00
|
|
|
&mut battle_data
|
2022-06-03 14:35:18 +00:00
|
|
|
.battle
|
|
|
|
.upgrade()
|
|
|
|
.unwrap()
|
2022-06-04 10:40:32 +00:00
|
|
|
.read()
|
2022-06-03 14:35:18 +00:00
|
|
|
.unwrap()
|
|
|
|
.random()
|
2022-06-04 10:40:32 +00:00
|
|
|
.get_rng()
|
|
|
|
.lock()
|
|
|
|
.unwrap(),
|
2022-06-03 14:35:18 +00:00
|
|
|
);
|
|
|
|
} 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!()
|
|
|
|
}
|
|
|
|
|
2022-06-04 10:40:32 +00:00
|
|
|
pub fn set_battle_data(&mut self, battle: Weak<RwLock<Battle<'a>>>, battle_side_index: u8) {
|
2022-06-03 14:35:18 +00:00
|
|
|
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.reset_active_scripts();
|
|
|
|
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 mark_opponent_as_seen(&mut self, unique_identifier: u32) {
|
|
|
|
if let Some(battle_data) = &mut self.battle_data {
|
|
|
|
battle_data.seen_opponents.insert(unique_identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset_active_scripts(&mut self) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ScriptSource for Pokemon<'a> {
|
|
|
|
fn get_script_count(&self) {
|
|
|
|
todo!()
|
|
|
|
}
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
);
|
|
|
|
assert_eq!(pokemon.species.name(), "foo");
|
|
|
|
assert_eq!(pokemon.form.name(), "default");
|
|
|
|
}
|
|
|
|
}
|