Further massive amounts of work

This commit is contained in:
2022-06-06 13:54:59 +02:00
parent df662ce6b5
commit ce33ec0649
33 changed files with 848 additions and 80 deletions

View File

@@ -4,18 +4,21 @@ 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::StatisticSet;
use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
use crate::static_data::statistics::Statistic;
use crate::utils::random::Random;
use crate::{PkmnResult, ScriptCategory};
use derive_getters::Getters;
use std::collections::HashSet;
use std::sync::{RwLock, Weak};
use std::sync::{Arc, RwLock, Weak};
#[derive(Debug)]
pub struct PokemonBattleData<'a> {
@@ -45,6 +48,7 @@ impl<'a> PokemonBattleData<'a> {
pub struct Pokemon<'a> {
library: &'a DynamicLibrary<'a>,
species: &'a Species<'a>,
#[getter(skip)]
form: &'a Form<'a>,
display_species: Option<&'a Species<'a>>,
@@ -56,14 +60,18 @@ pub struct Pokemon<'a> {
gender: Gender,
coloring: u8,
held_item: Option<&'a Item>,
health: u32,
current_health: u32,
weight: f32,
height: f32,
stat_boost: StatisticSet<i8>,
flat_stats: StatisticSet<u16>,
boosted_stats: StatisticSet<u16>,
stat_boost: ClampedStatisticSet<i8, -6, 6>,
flat_stats: StatisticSet<u32>,
boosted_stats: StatisticSet<u32>,
individual_values: ClampedStatisticSet<u8, 0, 31>,
effort_values: ClampedStatisticSet<u8, 0, 252>,
#[getter(skip)]
nature: &'a Nature,
nickname: Option<String>,
@@ -80,7 +88,7 @@ pub struct Pokemon<'a> {
ability_script: Option<Box<dyn Script>>,
status_script: Option<Box<dyn Script>>,
volatile: ScriptSet,
volatile: Arc<RwLock<ScriptSet>>,
}
impl<'a> Pokemon<'a> {
@@ -93,6 +101,7 @@ impl<'a> Pokemon<'a> {
unique_identifier: u32,
gender: Gender,
coloring: u8,
nature: String,
) -> Pokemon<'a> {
// Calculate experience from the level for the specified growth rate.
let experience = library
@@ -102,7 +111,12 @@ impl<'a> Pokemon<'a> {
let health = form.get_base_stat(Statistic::HP) as u32;
let weight = *form.weight();
let height = *form.height();
Pokemon {
let nature = library
.static_data()
.natures()
.get_nature(&nature)
.expect("Unknown nature name was given.");
let mut pokemon = Pokemon {
library,
species,
form,
@@ -114,12 +128,15 @@ impl<'a> Pokemon<'a> {
gender,
coloring,
held_item: None,
health,
current_health: health,
weight,
height,
stat_boost: Default::default(),
flat_stats: *form.base_stats(),
boosted_stats: *form.base_stats(),
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,
@@ -130,8 +147,26 @@ impl<'a> Pokemon<'a> {
types: form.types().to_vec(),
ability_script: None,
status_script: None,
volatile: ScriptSet {},
}
volatile: Default::default(),
};
pokemon.recalculate_flat_stats();
pokemon
}
pub fn form(&self) -> &'a Form<'a> {
self.form
}
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) {
@@ -188,7 +223,7 @@ impl<'a> Pokemon<'a> {
if let Some(data) = &mut self.battle_data {
data.on_battle_field = value;
if !value {
self.reset_active_scripts();
self.volatile.write().unwrap().clear();
self.weight = *self.form.weight();
self.height = *self.form.height();
}
@@ -201,15 +236,19 @@ impl<'a> Pokemon<'a> {
}
}
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, 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> {
@@ -218,6 +257,16 @@ impl<'a> ScriptSource for Pokemon<'a> {
}
}
impl<'a> VolatileScripts<'a> for Pokemon<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile
}
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>> {
self.library.load_script(ScriptCategory::Pokemon, key)
}
}
#[cfg(test)]
pub mod test {
use crate::dynamic_data::libraries::dynamic_library;
@@ -244,6 +293,7 @@ pub mod test {
0,
Gender::Male,
0,
"test_nature".to_string(),
);
assert_eq!(pokemon.species.name(), "foo");
assert_eq!(pokemon.form.name(), "default");