Further massive amounts of work

This commit is contained in:
Deukhoofd 2022-06-06 13:54:59 +02:00
parent df662ce6b5
commit ce33ec0649
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
33 changed files with 848 additions and 80 deletions

View File

@ -50,3 +50,5 @@ maplit = "1.0.2"
failure = "0.1.8" failure = "0.1.8"
failure_derive = "0.1.8" failure_derive = "0.1.8"
lazy_static = "1.4.0" lazy_static = "1.4.0"
hashbrown = "0.12.1"
indexmap = "1.8.2"

View File

@ -33,4 +33,9 @@ pub enum Event<'a> {
index: u8, index: u8,
pokemon: Option<Arc<RwLock<Pokemon<'a>>>>, pokemon: Option<Arc<RwLock<Pokemon<'a>>>>,
}, },
Swap {
side_index: u8,
index_a: u8,
index_b: u8,
},
} }

View File

@ -1,3 +1,80 @@
pub trait BattleStatCalculator { use crate::dynamic_data::models::pokemon::Pokemon;
//fn is_critical(attack: &ExecutingMove, target: &Pokemon, hit: u8); use crate::static_data::statistic_set::StatisticSet;
use crate::static_data::statistics::Statistic;
#[derive(Debug)]
pub struct BattleStatCalculator {}
impl BattleStatCalculator {
pub fn calculate_flat_stats(&self, pokemon: &Pokemon) -> StatisticSet<u32> {
StatisticSet::<u32>::new(
self.calculate_health_stat(pokemon),
self.calculate_other_stat(pokemon, Statistic::Attack),
self.calculate_other_stat(pokemon, Statistic::Defense),
self.calculate_other_stat(pokemon, Statistic::SpecialAttack),
self.calculate_other_stat(pokemon, Statistic::SpecialDefense),
self.calculate_other_stat(pokemon, Statistic::Speed),
)
}
pub fn calculate_flat_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32 {
if stat == Statistic::HP {
self.calculate_health_stat(pokemon)
} else {
self.calculate_other_stat(pokemon, stat)
}
}
pub fn calculate_boosted_stats(&self, pokemon: &Pokemon) -> StatisticSet<u32> {
StatisticSet::<u32>::new(
self.calculate_boosted_stat(pokemon, Statistic::HP),
self.calculate_boosted_stat(pokemon, Statistic::Attack),
self.calculate_boosted_stat(pokemon, Statistic::Defense),
self.calculate_boosted_stat(pokemon, Statistic::SpecialAttack),
self.calculate_boosted_stat(pokemon, Statistic::SpecialDefense),
self.calculate_boosted_stat(pokemon, Statistic::Speed),
)
}
pub fn calculate_boosted_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32 {
(self.calculate_flat_stat(pokemon, stat) as f32
* self.get_stat_boost_modifier(pokemon, stat)) as u32
}
fn calculate_health_stat(&self, pokemon: &Pokemon) -> u32 {
let base = pokemon.form().get_base_stat(Statistic::HP) as u32;
let iv = *pokemon.individual_values().hp() as u32;
let ev = *pokemon.effort_values().hp() as u32;
let level = *pokemon.level() as u32;
(((2 * base + iv + (ev / 4)) * level) / 100) + level + 10
}
fn calculate_other_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32 {
let base = pokemon.form().get_base_stat(stat) as u32;
let iv = pokemon.individual_values().get_stat(stat) as u32;
let ev = pokemon.effort_values().get_stat(stat) as u32;
let level = *pokemon.level() as u32;
let unmodified = (((2 * base + iv + (ev / 4)) * level) / 100) + 5;
return (unmodified as f32 * pokemon.nature().get_stat_modifier(stat)) as u32;
}
fn get_stat_boost_modifier(&self, pokemon: &Pokemon, stat: Statistic) -> f32 {
let boost = pokemon.stat_boost().get_stat(stat);
match boost {
-6 => 2.0 / 8.0,
-5 => 2.0 / 7.0,
-4 => 2.0 / 6.0,
-3 => 2.0 / 5.0,
-2 => 2.0 / 4.0,
-1 => 2.0 / 3.0,
0 => 1.0,
1 => 3.0 / 2.0,
2 => 4.0 / 2.0,
3 => 5.0 / 2.0,
4 => 6.0 / 2.0,
5 => 7.0 / 2.0,
6 => 8.0 / 2.0,
_ => panic!("Stat boost was out of expected range of -6 to 6"),
}
}
} }

View File

@ -1,19 +1,42 @@
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
use crate::dynamic_data::libraries::script_resolver::ScriptCategory;
use crate::dynamic_data::script_handling::script::Script;
use crate::static_data::libraries::static_data::StaticData; use crate::static_data::libraries::static_data::StaticData;
use derive_getters::Getters; use crate::PkmnResult;
#[derive(Getters, Debug)] #[derive(Debug)]
pub struct DynamicLibrary<'a> { pub struct DynamicLibrary<'a> {
static_data: StaticData<'a>, static_data: StaticData<'a>,
stat_calculator: BattleStatCalculator,
}
impl<'a> DynamicLibrary<'a> {
pub fn static_data(&self) -> &StaticData<'a> {
&self.static_data
}
pub fn stat_calculator(&self) -> &BattleStatCalculator {
&self.stat_calculator
}
pub fn load_script(
&self,
_category: ScriptCategory,
_key: &str,
) -> PkmnResult<Box<dyn Script>> {
todo!()
}
} }
#[cfg(test)] #[cfg(test)]
pub mod test { pub mod test {
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary; use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::static_data::libraries::static_data; use crate::static_data::libraries::static_data;
pub fn build<'a>() -> DynamicLibrary<'a> { pub fn build<'a>() -> DynamicLibrary<'a> {
DynamicLibrary { DynamicLibrary {
static_data: static_data::test::build(), static_data: static_data::test::build(),
stat_calculator: BattleStatCalculator {},
} }
} }
} }

View File

@ -1,2 +1,3 @@
pub mod battle_stat_calculator; pub mod battle_stat_calculator;
pub mod dynamic_library; pub mod dynamic_library;
pub mod script_resolver;

View File

@ -0,0 +1,12 @@
pub trait ScriptResolver {}
#[derive(Debug, Clone)]
pub enum ScriptCategory {
Move,
Ability,
Status,
Pokemon,
Battle,
Side,
ItemBattleTrigger,
}

View File

@ -12,8 +12,9 @@ use std::sync::{Arc, RwLock};
#[derive(Getters, Debug)] #[derive(Getters, Debug)]
pub struct Battle<'a> { pub struct Battle<'a> {
#[getter(skip)]
library: &'a DynamicLibrary<'a>, library: &'a DynamicLibrary<'a>,
parties: Vec<BattleParty>, parties: Vec<BattleParty<'a>>,
can_flee: bool, can_flee: bool,
number_of_sides: u8, number_of_sides: u8,
pokemon_per_side: u8, pokemon_per_side: u8,
@ -33,7 +34,7 @@ pub struct Battle<'a> {
impl<'a> Battle<'a> { impl<'a> Battle<'a> {
pub fn new( pub fn new(
library: &'a DynamicLibrary<'a>, library: &'a DynamicLibrary<'a>,
parties: Vec<BattleParty>, parties: Vec<BattleParty<'a>>,
can_flee: bool, can_flee: bool,
number_of_sides: u8, number_of_sides: u8,
pokemon_per_side: u8, pokemon_per_side: u8,
@ -59,7 +60,7 @@ impl<'a> Battle<'a> {
event_hook: Default::default(), event_hook: Default::default(),
history_holder: Box::new(HistoryHolder {}), history_holder: Box::new(HistoryHolder {}),
current_turn: 0, current_turn: 0,
volatile: ScriptSet {}, volatile: ScriptSet::default(),
last_turn_time: 0, last_turn_time: 0,
})); }));
@ -77,4 +78,8 @@ impl<'a> Battle<'a> {
pub fn can_slot_be_filled(&self) -> bool { pub fn can_slot_be_filled(&self) -> bool {
todo!() todo!()
} }
pub fn library(&self) -> &'a DynamicLibrary<'a> {
self.library
}
} }

View File

@ -1,2 +1,28 @@
use crate::dynamic_data::models::pokemon_party::PokemonParty;
#[derive(Debug)] #[derive(Debug)]
pub struct BattleParty {} pub struct BattleParty<'a> {
party: &'a PokemonParty<'a>,
responsible_indices: Vec<(u8, u8)>,
}
impl<'a> BattleParty<'a> {
pub fn is_responsible_for_index(&self, side: u8, index: u8) -> bool {
for responsible_index in &self.responsible_indices {
if responsible_index.0 == side && responsible_index.1 == index {
return true;
}
}
false
}
pub fn has_pokemon_not_in_field(&self) -> bool {
for pokemon in self.party.pokemon().iter().flatten() {
let pokemon = pokemon.read().unwrap();
if pokemon.is_usable() && !pokemon.is_on_battlefield() {
return true;
}
}
false
}
}

View File

@ -17,6 +17,16 @@ impl BattleRandom {
pub fn get_rng(&self) -> &Mutex<Random> { pub fn get_rng(&self) -> &Mutex<Random> {
&self.random &self.random
} }
pub fn get(&self) -> i32 {
return self.get_rng().lock().unwrap().get();
}
pub fn get_max(&self, max: i32) -> i32 {
return self.get_rng().lock().unwrap().get_max(max);
}
pub fn get_between(&self, min: i32, max: i32) -> i32 {
return self.get_rng().lock().unwrap().get_between(min, max);
}
} }
impl Debug for BattleRandom { impl Debug for BattleRandom {

View File

@ -1,9 +1,13 @@
use crate::dynamic_data::choices::TurnChoice; use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::event_hooks::event_hook::Event; use crate::dynamic_data::event_hooks::event_hook::Event;
use crate::dynamic_data::models::battle::Battle; use crate::dynamic_data::models::battle::Battle;
use crate::dynamic_data::models::battle_party::BattleParty;
use crate::dynamic_data::models::pokemon::Pokemon; use crate::dynamic_data::models::pokemon::Pokemon;
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::dynamic_data::script_handling::ScriptSource;
use crate::script_hook; use crate::{script_hook, PkmnResult};
use derive_getters::Getters; use derive_getters::Getters;
use std::sync::{Arc, RwLock, Weak}; use std::sync::{Arc, RwLock, Weak};
@ -16,7 +20,8 @@ pub struct BattleSide<'a> {
fillable_slots: Vec<bool>, fillable_slots: Vec<bool>,
choices_set: u8, choices_set: u8,
battle: Weak<RwLock<Battle<'a>>>, battle: Weak<RwLock<Battle<'a>>>,
has_fled: bool, has_fled_battle: bool,
volatile_scripts: Arc<RwLock<ScriptSet>>,
} }
impl<'a> BattleSide<'a> { impl<'a> BattleSide<'a> {
@ -39,7 +44,8 @@ impl<'a> BattleSide<'a> {
fillable_slots, fillable_slots,
choices_set: 0, choices_set: 0,
battle, battle,
has_fled: false, has_fled_battle: false,
volatile_scripts: Default::default(),
} }
} }
@ -136,6 +142,113 @@ impl<'a> BattleSide<'a> {
} }
false false
} }
pub fn mark_slot_as_unfillable(&mut self, pokemon: Arc<Pokemon<'a>>) {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
self.fillable_slots[i] = false;
return;
}
}
}
}
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
return self.fillable_slots[i];
}
}
}
false
}
pub fn is_defeated(&self) -> bool {
for fillable_slot in &self.fillable_slots {
if *fillable_slot {
return false;
}
}
true
}
pub fn has_fled(&self) -> bool {
self.has_fled_battle
}
pub fn mark_as_fled(&mut self) {
self.has_fled_battle = true;
}
pub fn get_random_creature_index(&self) -> u8 {
// TODO: Consider adding parameter to only get index for available creatures.
self.battle
.upgrade()
.unwrap()
.read()
.unwrap()
.random()
.get_max(self.pokemon_per_side as i32) as u8
}
pub fn swap_positions(&mut self, a: u8, b: u8) -> bool {
// If out of range, don't allow swapping.
if a >= self.pokemon_per_side || b >= self.pokemon_per_side {
return false;
}
// If the two indices are the same, don't allow swapping.
if a == b {
return false;
}
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
// Fetch parties for the two indices.
let mut party_a = None;
let mut party_b = None;
for party in battle.parties() {
if party.is_responsible_for_index(self.index, a) {
party_a = Some(party);
}
if party.is_responsible_for_index(self.index, b) {
party_b = Some(party);
}
}
// If either of the parties does not exist, fail.
if party_a.is_none() || party_b.is_none() {
return false;
}
// Don't allow swapping if different parties are responsible for the indices.
if party_a.unwrap() as *const BattleParty != party_b.unwrap() as *const BattleParty {
return false;
}
self.pokemon.swap(a as usize, b as usize);
battle.event_hook().trigger(Event::Swap {
side_index: self.index,
index_a: a,
index_b: b,
});
true
}
}
impl<'a> VolatileScripts<'a> for BattleSide<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile_scripts
}
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>> {
self.battle
.upgrade()
.unwrap()
.read()
.unwrap()
.library()
.load_script(crate::ScriptCategory::Side, key)
}
} }
impl<'a> ScriptSource for BattleSide<'a> { impl<'a> ScriptSource for BattleSide<'a> {

View File

@ -5,3 +5,4 @@ pub mod battle_result;
pub mod battle_side; pub mod battle_side;
pub mod learned_move; pub mod learned_move;
pub mod pokemon; pub mod pokemon;
pub mod pokemon_party;

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::models::learned_move::LearnedMove;
use crate::dynamic_data::script_handling::script::Script; use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet; 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::dynamic_data::script_handling::ScriptSource;
use crate::static_data::items::item::Item; 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::ability_index::AbilityIndex;
use crate::static_data::species_data::form::Form; use crate::static_data::species_data::form::Form;
use crate::static_data::species_data::gender::Gender; use crate::static_data::species_data::gender::Gender;
use crate::static_data::species_data::species::Species; 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::static_data::statistics::Statistic;
use crate::utils::random::Random; use crate::utils::random::Random;
use crate::{PkmnResult, ScriptCategory};
use derive_getters::Getters; use derive_getters::Getters;
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::{RwLock, Weak}; use std::sync::{Arc, RwLock, Weak};
#[derive(Debug)] #[derive(Debug)]
pub struct PokemonBattleData<'a> { pub struct PokemonBattleData<'a> {
@ -45,6 +48,7 @@ impl<'a> PokemonBattleData<'a> {
pub struct Pokemon<'a> { pub struct Pokemon<'a> {
library: &'a DynamicLibrary<'a>, library: &'a DynamicLibrary<'a>,
species: &'a Species<'a>, species: &'a Species<'a>,
#[getter(skip)]
form: &'a Form<'a>, form: &'a Form<'a>,
display_species: Option<&'a Species<'a>>, display_species: Option<&'a Species<'a>>,
@ -56,14 +60,18 @@ pub struct Pokemon<'a> {
gender: Gender, gender: Gender,
coloring: u8, coloring: u8,
held_item: Option<&'a Item>, held_item: Option<&'a Item>,
health: u32, current_health: u32,
weight: f32, weight: f32,
height: f32, height: f32,
stat_boost: StatisticSet<i8>, stat_boost: ClampedStatisticSet<i8, -6, 6>,
flat_stats: StatisticSet<u16>, flat_stats: StatisticSet<u32>,
boosted_stats: StatisticSet<u16>, boosted_stats: StatisticSet<u32>,
individual_values: ClampedStatisticSet<u8, 0, 31>,
effort_values: ClampedStatisticSet<u8, 0, 252>,
#[getter(skip)]
nature: &'a Nature,
nickname: Option<String>, nickname: Option<String>,
@ -80,7 +88,7 @@ pub struct Pokemon<'a> {
ability_script: Option<Box<dyn Script>>, ability_script: Option<Box<dyn Script>>,
status_script: Option<Box<dyn Script>>, status_script: Option<Box<dyn Script>>,
volatile: ScriptSet, volatile: Arc<RwLock<ScriptSet>>,
} }
impl<'a> Pokemon<'a> { impl<'a> Pokemon<'a> {
@ -93,6 +101,7 @@ impl<'a> Pokemon<'a> {
unique_identifier: u32, unique_identifier: u32,
gender: Gender, gender: Gender,
coloring: u8, coloring: u8,
nature: String,
) -> Pokemon<'a> { ) -> Pokemon<'a> {
// Calculate experience from the level for the specified growth rate. // Calculate experience from the level for the specified growth rate.
let experience = library let experience = library
@ -102,7 +111,12 @@ impl<'a> Pokemon<'a> {
let health = form.get_base_stat(Statistic::HP) as u32; let health = form.get_base_stat(Statistic::HP) as u32;
let weight = *form.weight(); let weight = *form.weight();
let height = *form.height(); 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, library,
species, species,
form, form,
@ -114,12 +128,15 @@ impl<'a> Pokemon<'a> {
gender, gender,
coloring, coloring,
held_item: None, held_item: None,
health, current_health: health,
weight, weight,
height, height,
stat_boost: Default::default(), stat_boost: Default::default(),
flat_stats: *form.base_stats(), flat_stats: Default::default(),
boosted_stats: *form.base_stats(), boosted_stats: Default::default(),
individual_values: Default::default(),
effort_values: Default::default(),
nature,
nickname: None, nickname: None,
ability_index: ability, ability_index: ability,
is_ability_overridden: false, is_ability_overridden: false,
@ -130,8 +147,26 @@ impl<'a> Pokemon<'a> {
types: form.types().to_vec(), types: form.types().to_vec(),
ability_script: None, ability_script: None,
status_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) { 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 { if let Some(data) = &mut self.battle_data {
data.on_battle_field = value; data.on_battle_field = value;
if !value { if !value {
self.reset_active_scripts(); self.volatile.write().unwrap().clear();
self.weight = *self.form.weight(); self.weight = *self.form.weight();
self.height = *self.form.height(); 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) { pub fn mark_opponent_as_seen(&mut self, unique_identifier: u32) {
if let Some(battle_data) = &mut self.battle_data { if let Some(battle_data) = &mut self.battle_data {
battle_data.seen_opponents.insert(unique_identifier); battle_data.seen_opponents.insert(unique_identifier);
} }
} }
pub fn reset_active_scripts(&mut self) {
todo!()
}
} }
impl<'a> ScriptSource for Pokemon<'a> { 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)] #[cfg(test)]
pub mod test { pub mod test {
use crate::dynamic_data::libraries::dynamic_library; use crate::dynamic_data::libraries::dynamic_library;
@ -244,6 +293,7 @@ pub mod test {
0, 0,
Gender::Male, Gender::Male,
0, 0,
"test_nature".to_string(),
); );
assert_eq!(pokemon.species.name(), "foo"); assert_eq!(pokemon.species.name(), "foo");
assert_eq!(pokemon.form.name(), "default"); assert_eq!(pokemon.form.name(), "default");

View File

@ -0,0 +1,80 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use std::sync::{Arc, RwLock};
#[derive(Debug)]
pub struct PokemonParty<'a> {
pokemon: Vec<Option<Arc<RwLock<Pokemon<'a>>>>>,
}
impl<'a> PokemonParty<'a> {
pub fn new(size: usize) -> Self {
let mut pokemon = Vec::with_capacity(size);
for _i in 0..size {
pokemon.push(None);
}
Self { pokemon }
}
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'a>>>> {
let opt = self.pokemon.get(index);
if let Some(v) = opt {
v
} else {
&None
}
}
pub fn switch(&mut self, a: usize, b: usize) {
self.pokemon.swap(a, b);
}
pub fn swap_into(
&mut self,
index: usize,
pokemon: Option<Arc<RwLock<Pokemon<'a>>>>,
) -> Option<Arc<RwLock<Pokemon<'a>>>> {
if index >= self.pokemon.len() {
return pokemon;
}
let old = self.pokemon[index].as_ref().cloned();
self.pokemon[index] = pokemon;
old
}
pub fn has_usable_pokemon(&self) -> bool {
for pokemon in self.pokemon.iter().flatten() {
if pokemon.read().unwrap().is_usable() {
return true;
}
}
false
}
pub fn length(&self) -> usize {
self.pokemon.len()
}
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'a>>>>> {
&self.pokemon
}
pub fn pack_party(&mut self) {
let mut first_empty = None;
let mut i = 0;
loop {
if self.pokemon[i].is_none() {
if first_empty.is_none() {
first_empty = Some(i)
}
} else if first_empty.is_some() {
self.pokemon.swap(first_empty.unwrap(), i);
i = first_empty.unwrap();
first_empty = None;
}
i += 1;
if i >= self.pokemon.len() {
break;
}
}
}
}

View File

@ -1,8 +1,10 @@
use crate::dynamic_data::script_handling::script::Script; use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet; use crate::dynamic_data::script_handling::script_set::ScriptSet;
use std::sync::Weak;
pub mod script; pub mod script;
pub mod script_set; pub mod script_set;
pub mod volatile_scripts;
#[macro_export] #[macro_export]
macro_rules! script_hook { macro_rules! script_hook {
@ -24,20 +26,20 @@ pub trait ScriptSource {
fn get_script_count(&self); fn get_script_count(&self);
} }
pub enum ScriptWrapper<'a> { pub enum ScriptWrapper {
Script(&'a Box<dyn Script>), Script(Weak<Box<dyn Script>>),
Set(&'a ScriptSet), Set(Weak<ScriptSet>),
} }
pub struct ScriptAggregator<'a> { pub struct ScriptAggregator {
scripts: Vec<Option<ScriptWrapper<'a>>>, scripts: Vec<Option<ScriptWrapper>>,
size: i32, size: i32,
index: i32, index: i32,
set_index: i32, set_index: i32,
} }
impl<'a> ScriptAggregator<'a> { impl ScriptAggregator {
pub fn new(scripts: Vec<Option<ScriptWrapper<'a>>>) -> Self { pub fn new(scripts: Vec<Option<ScriptWrapper>>) -> Self {
let len = scripts.len(); let len = scripts.len();
Self { Self {
scripts, scripts,
@ -51,6 +53,7 @@ impl<'a> ScriptAggregator<'a> {
if self.index != -1 { if self.index != -1 {
if let Some(wrapper) = &self.scripts[self.index as usize] { if let Some(wrapper) = &self.scripts[self.index as usize] {
if let ScriptWrapper::Set(set) = wrapper { if let ScriptWrapper::Set(set) = wrapper {
if let Some(set) = set.upgrade() {
self.set_index += 1; self.set_index += 1;
if self.set_index as usize >= set.count() { if self.set_index as usize >= set.count() {
self.set_index = -1; self.set_index = -1;
@ -60,15 +63,22 @@ impl<'a> ScriptAggregator<'a> {
} }
} }
} }
}
self.index += 1; self.index += 1;
for index in self.index..self.size { for index in self.index..self.size {
self.index = index; self.index = index;
if let Some(wrapper) = &self.scripts[index as usize] { if let Some(wrapper) = &self.scripts[index as usize] {
if let ScriptWrapper::Set(..) = wrapper { if let ScriptWrapper::Set(s) = wrapper {
if let Some(..) = s.upgrade() {
self.set_index = 0; self.set_index = 0;
}
return true; return true;
} }
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(..) = script.upgrade() {
return true;
}
}
}
} }
false false
@ -79,8 +89,13 @@ impl<'a> ScriptAggregator<'a> {
return None; return None;
} }
return match self.scripts[self.index as usize].as_ref().unwrap() { return match self.scripts[self.index as usize].as_ref().unwrap() {
ScriptWrapper::Script(script) => Some(script), // We can make this unsafe as we know there is a strong reference. This is validated in
ScriptWrapper::Set(set) => Some(set.at(self.set_index as usize)), // increment_to_next_value
ScriptWrapper::Script(script) => unsafe { Some(&*script.as_ptr()) },
ScriptWrapper::Set(set) => unsafe {
let r = (&*set.as_ptr()).at(self.set_index as usize);
Some(r.as_ref())
},
}; };
} }
} }

View File

@ -2,6 +2,8 @@ use crate::dynamic_data::models::pokemon::Pokemon;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
pub trait Script { pub trait Script {
fn name(&self) -> &str;
fn is_suppressed(&self) -> bool { fn is_suppressed(&self) -> bool {
self.get_suppressed_count() > 0 self.get_suppressed_count() > 0
} }

View File

@ -1,14 +1,69 @@
use crate::dynamic_data::script_handling::script::Script; use crate::dynamic_data::script_handling::script::Script;
use crate::PkmnResult;
use indexmap::IndexMap;
use std::sync::Arc;
#[derive(Debug)] #[derive(Debug, Default)]
pub struct ScriptSet {} pub struct ScriptSet {
scripts: IndexMap<String, Arc<Box<dyn Script>>>,
}
impl ScriptSet { impl ScriptSet {
pub fn count(&self) -> usize { pub fn add(&mut self, script: Box<dyn Script>) -> Arc<Box<dyn Script>> {
todo!() if let Some(existing) = self.scripts.get(script.name()) {
existing.stack();
return existing.clone();
}
let arc = Arc::new(script);
self.scripts.insert(arc.name().to_string(), arc.clone());
self.scripts.last().unwrap().1.clone()
} }
pub fn at(&self, _index: usize) -> &Box<dyn Script> { pub fn stack_or_add<'b, F>(
todo!() &mut self,
key: &str,
instantiation: &'b F,
) -> PkmnResult<Arc<Box<dyn Script>>>
where
F: Fn() -> PkmnResult<Box<dyn Script>>,
{
if let Some(existing) = self.scripts.get(key) {
existing.stack();
return Ok(existing.clone());
}
let script = instantiation()?;
let arc = Arc::new(script);
self.scripts.insert(arc.name().to_string(), arc.clone());
Ok(self.scripts.last().unwrap().1.clone())
}
pub fn get(&self, key: &str) -> Option<&Arc<Box<dyn Script>>> {
self.scripts.get(key)
}
pub fn remove(&mut self, key: &str) {
let value = self.scripts.shift_remove(key);
if let Some(script) = value {
script.on_remove();
}
}
pub fn clear(&mut self) {
for script in &self.scripts {
script.1.on_remove();
}
self.scripts.clear();
}
pub fn has(&self, key: &str) -> bool {
self.scripts.contains_key(key)
}
pub fn at(&self, index: usize) -> &Arc<Box<dyn Script>> {
&self.scripts[index]
}
pub fn count(&self) -> usize {
self.scripts.len()
} }
} }

View File

@ -0,0 +1,30 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use crate::PkmnResult;
use std::sync::{Arc, RwLock};
pub trait VolatileScripts<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>>;
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>>;
fn has_volatile_script(&self, key: &str) -> bool {
self.volatile_scripts().read().unwrap().has(key)
}
fn get_volatile_script(&self, key: &str) -> Option<Arc<Box<dyn Script>>> {
let scripts = self.volatile_scripts().read().unwrap();
let s = scripts.get(key);
s.cloned()
}
fn add_volatile_script(&mut self, key: &str) -> PkmnResult<Arc<Box<dyn Script>>> {
self.volatile_scripts()
.write()
.unwrap()
.stack_or_add(key, &|| self.load_volatile_script(key))
}
fn remove_volatile_script(&mut self, key: &str) {
self.volatile_scripts().write().unwrap().remove(key)
}
}

View File

@ -4,16 +4,26 @@
#![feature(bench_black_box)] #![feature(bench_black_box)]
#![feature(let_chains)] #![feature(let_chains)]
#[macro_use]
extern crate maplit;
#[cfg(feature = "c_interface")] #[cfg(feature = "c_interface")]
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
use crate::dynamic_data::libraries::script_resolver::ScriptCategory;
mod c_interface; mod c_interface;
pub mod defines; pub mod defines;
pub mod dynamic_data; pub mod dynamic_data;
pub mod static_data; pub mod static_data;
pub mod utils; pub mod utils;
#[derive(Debug, Clone)]
pub enum PokemonError {
ScriptNotFound {
category: ScriptCategory,
name: String,
},
MiscError,
}
pub type PkmnResult<T> = std::result::Result<T, PokemonError>;

View File

@ -22,6 +22,10 @@ impl GrowthRate for LookupGrowthRate {
} }
fn calculate_experience(&self, level: LevelInt) -> u32 { fn calculate_experience(&self, level: LevelInt) -> u32 {
if level >= self.experience.len() as LevelInt {
*self.experience.last().unwrap()
} else {
self.experience[(level - 1) as usize] self.experience[(level - 1) as usize]
} }
} }
}

View File

@ -1,5 +1,5 @@
use crate::utils::random::Random; use crate::utils::random::Random;
use std::collections::HashMap; use hashbrown::HashMap;
pub trait DataLibrary<'a, T: 'a> { pub trait DataLibrary<'a, T: 'a> {
fn map(&self) -> &HashMap<String, T>; fn map(&self) -> &HashMap<String, T>;

View File

@ -1,6 +1,6 @@
use crate::defines::LevelInt; use crate::defines::LevelInt;
use crate::static_data::growth_rates::growth_rate::GrowthRate; use crate::static_data::growth_rates::growth_rate::GrowthRate;
use std::collections::HashMap; use hashbrown::HashMap;
use std::fmt; use std::fmt;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
@ -42,7 +42,10 @@ pub mod tests {
// Borrow as mut so we can insert // Borrow as mut so we can insert
let w = &mut lib; let w = &mut lib;
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100]))); w.add_growth_rate(
"test_growthrate",
Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])),
);
// Drops borrow as mut // Drops borrow as mut
lib lib
@ -51,14 +54,14 @@ pub mod tests {
#[test] #[test]
fn add_growth_rate_to_library_and_calculate_level() { fn add_growth_rate_to_library_and_calculate_level() {
let lib = build(); let lib = build();
assert_eq!(lib.calculate_level("foo", 3), 1); assert_eq!(lib.calculate_level("test_growthrate", 3), 1);
assert_eq!(lib.calculate_level("foo", 50), 3); assert_eq!(lib.calculate_level("test_growthrate", 50), 3);
} }
#[test] #[test]
fn add_growth_rate_to_library_and_calculate_experience() { fn add_growth_rate_to_library_and_calculate_experience() {
let lib = build(); let lib = build();
assert_eq!(lib.calculate_experience("foo", 1), 0); assert_eq!(lib.calculate_experience("test_growthrate", 1), 0);
assert_eq!(lib.calculate_experience("foo", 3), 10); assert_eq!(lib.calculate_experience("test_growthrate", 3), 10);
} }
} }

View File

@ -1,6 +1,6 @@
use crate::static_data::items::item::Item; use crate::static_data::items::item::Item;
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::data_library::DataLibrary;
use std::collections::HashMap; use hashbrown::HashMap;
#[derive(Debug)] #[derive(Debug)]
pub struct ItemLibrary { pub struct ItemLibrary {

View File

@ -1,6 +1,6 @@
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::moves::move_data::MoveData; use crate::static_data::moves::move_data::MoveData;
use std::collections::HashMap; use hashbrown::HashMap;
#[derive(Debug)] #[derive(Debug)]
pub struct MoveLibrary { pub struct MoveLibrary {

View File

@ -1,6 +1,6 @@
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::species_data::species::Species; use crate::static_data::species_data::species::Species;
use std::collections::HashMap; use hashbrown::HashMap;
#[derive(Debug)] #[derive(Debug)]
pub struct SpeciesLibrary<'a> { pub struct SpeciesLibrary<'a> {
@ -39,14 +39,14 @@ pub mod tests {
use crate::static_data::species_data::learnable_moves::LearnableMoves; use crate::static_data::species_data::learnable_moves::LearnableMoves;
use crate::static_data::species_data::species::Species; use crate::static_data::species_data::species::Species;
use crate::static_data::statistic_set::StatisticSet; use crate::static_data::statistic_set::StatisticSet;
use std::collections::HashSet; use hashbrown::HashSet;
fn build_species<'a>() -> Species<'a> { fn build_species<'a>() -> Species<'a> {
Species::new( Species::new(
0, 0,
"foo", "foo",
0.5, 0.5,
"", "test_growthrate",
0, 0,
Form::new( Form::new(
"default", "default",

View File

@ -4,6 +4,7 @@ use crate::static_data::libraries::library_settings::LibrarySettings;
use crate::static_data::libraries::move_library::MoveLibrary; use crate::static_data::libraries::move_library::MoveLibrary;
use crate::static_data::libraries::species_library::SpeciesLibrary; use crate::static_data::libraries::species_library::SpeciesLibrary;
use crate::static_data::libraries::type_library::TypeLibrary; use crate::static_data::libraries::type_library::TypeLibrary;
use crate::static_data::natures::NatureLibrary;
use derive_getters::Getters; use derive_getters::Getters;
#[derive(Getters, Debug)] #[derive(Getters, Debug)]
@ -14,6 +15,7 @@ pub struct StaticData<'a> {
items: ItemLibrary, items: ItemLibrary,
growth_rates: GrowthRateLibrary, growth_rates: GrowthRateLibrary,
types: TypeLibrary, types: TypeLibrary,
natures: NatureLibrary,
} }
#[cfg(test)] #[cfg(test)]
@ -23,6 +25,7 @@ pub mod test {
use crate::static_data::libraries::{ use crate::static_data::libraries::{
growth_rate_library, item_library, move_library, species_library, type_library, growth_rate_library, item_library, move_library, species_library, type_library,
}; };
use crate::static_data::natures;
pub fn build<'a>() -> StaticData<'a> { pub fn build<'a>() -> StaticData<'a> {
StaticData { StaticData {
@ -32,6 +35,7 @@ pub mod test {
items: item_library::tests::build(), items: item_library::tests::build(),
growth_rates: growth_rate_library::tests::build(), growth_rates: growth_rate_library::tests::build(),
types: type_library::tests::build(), types: type_library::tests::build(),
natures: natures::tests::build(),
} }
} }
} }

View File

@ -1,4 +1,4 @@
use std::collections::HashMap; use hashbrown::HashMap;
#[derive(Debug)] #[derive(Debug)]
pub struct TypeLibrary { pub struct TypeLibrary {

View File

@ -2,6 +2,7 @@ pub mod growth_rates;
pub mod items; pub mod items;
pub mod libraries; pub mod libraries;
pub mod moves; pub mod moves;
pub mod natures;
pub mod species_data; pub mod species_data;
pub mod statistic_set; pub mod statistic_set;
pub mod statistics; pub mod statistics;

131
src/static_data/natures.rs Normal file
View File

@ -0,0 +1,131 @@
use crate::static_data::statistics::Statistic;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct Nature {
increase_stat: Statistic,
decrease_stat: Statistic,
increase_modifier: f32,
decrease_modifier: f32,
}
impl Nature {
pub fn new(
increase_stat: Statistic,
decrease_stat: Statistic,
increase_modifier: f32,
decrease_modifier: f32,
) -> Self {
Self {
increase_stat,
decrease_stat,
increase_modifier,
decrease_modifier,
}
}
pub fn increased_stat(&self) -> Statistic {
self.increase_stat
}
pub fn decreased_stat(&self) -> Statistic {
self.decrease_stat
}
pub fn get_stat_modifier(&self, stat: Statistic) -> f32 {
if stat == self.increase_stat {
self.increase_modifier
} else if stat == self.decrease_stat {
self.decrease_modifier
} else {
1.0
}
}
}
#[derive(Debug)]
pub struct NatureLibrary {
map: HashMap<String, Nature>,
}
impl NatureLibrary {
pub fn new(capacity: usize) -> Self {
NatureLibrary {
map: HashMap::with_capacity(capacity),
}
}
pub fn load_nature(&mut self, name: &str, nature: Nature) {
self.map.insert(name.to_string(), nature);
}
pub fn get_nature(&self, key: &str) -> Option<&Nature> {
self.map.get(key)
}
pub fn get_nature_name(&self, nature: &Nature) -> String {
for kv in &self.map {
// As natures can't be copied, and should always be the same reference as the value
// in the map, we just compare by reference.
if (kv.1 as *const Nature) == (nature as *const Nature) {
return kv.0.to_string();
}
}
panic!("No name was found for the given nature. This should never happen.");
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::natures::{Nature, NatureLibrary};
use crate::static_data::statistics::Statistic;
pub fn build() -> NatureLibrary {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"test_nature",
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib
}
#[test]
fn create_nature_library_insert_and_retrieve() {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"foo",
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib.load_nature(
"bar",
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature("foo").expect("Nature was not found");
assert_eq!(n1.increase_stat, Statistic::HP);
assert_eq!(n1.decrease_stat, Statistic::Attack);
assert_eq!(n1.increase_modifier, 1.1);
assert_eq!(n1.decrease_modifier, 0.9);
}
#[test]
fn create_nature_library_insert_and_get_name() {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"foo",
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib.load_nature(
"bar",
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature("foo").expect("Nature was not found");
let name = lib.get_nature_name(n1);
assert_eq!(name, "foo");
let n2 = lib.get_nature("bar").expect("Nature was not found");
let name2 = lib.get_nature_name(n2);
assert_eq!(name2, "bar");
}
}

View File

@ -4,7 +4,7 @@ use crate::static_data::statistic_set::StatisticSet;
use crate::static_data::statistics::Statistic; use crate::static_data::statistics::Statistic;
use crate::utils::random::Random; use crate::utils::random::Random;
use derive_getters::Getters; use derive_getters::Getters;
use std::collections::HashSet; use hashbrown::HashSet;
#[derive(Getters, Debug)] #[derive(Getters, Debug)]
pub struct Form<'a> { pub struct Form<'a> {

View File

@ -1,7 +1,7 @@
use crate::defines::LevelInt; use crate::defines::LevelInt;
use crate::static_data::moves::move_data::MoveData; use crate::static_data::moves::move_data::MoveData;
use std::collections::hash_map::Entry::{Occupied, Vacant}; use hashbrown::hash_map::Entry::{Occupied, Vacant};
use std::collections::HashMap; use hashbrown::HashMap;
#[derive(Default, PartialEq, Debug)] #[derive(Default, PartialEq, Debug)]
pub struct LearnableMoves<'a> { pub struct LearnableMoves<'a> {
@ -45,7 +45,6 @@ mod tests {
#[test] #[test]
fn adds_level_moves() { fn adds_level_moves() {
let mut moves = LearnableMoves::new();
let move1 = MoveData::new( let move1 = MoveData::new(
"foo", "foo",
0, 0,
@ -70,6 +69,8 @@ mod tests {
SecondaryEffect::empty(), SecondaryEffect::empty(),
Default::default(), Default::default(),
); );
let mut moves = LearnableMoves::new();
moves.add_level_move(1, &move1); moves.add_level_move(1, &move1);
moves.add_level_move(1, &move2); moves.add_level_move(1, &move2);
@ -81,7 +82,6 @@ mod tests {
#[test] #[test]
fn adds_two_same_moves_at_different_level() { fn adds_two_same_moves_at_different_level() {
let mut moves = LearnableMoves::new();
let move1 = MoveData::new( let move1 = MoveData::new(
"foo", "foo",
0, 0,
@ -94,6 +94,8 @@ mod tests {
SecondaryEffect::empty(), SecondaryEffect::empty(),
Default::default(), Default::default(),
); );
let mut moves = LearnableMoves::new();
moves.add_level_move(1, &move1); moves.add_level_move(1, &move1);
moves.add_level_move(5, &move1); moves.add_level_move(5, &move1);

View File

@ -2,7 +2,7 @@ use self::super::form::Form;
use crate::static_data::species_data::gender::Gender; use crate::static_data::species_data::gender::Gender;
use crate::utils::random::Random; use crate::utils::random::Random;
use derive_getters::Getters; use derive_getters::Getters;
use std::collections::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};
#[derive(Debug, Getters)] #[derive(Debug, Getters)]
pub struct Species<'a> { pub struct Species<'a> {
@ -25,15 +25,15 @@ impl<'a> Species<'a> {
default_form: Form<'a>, default_form: Form<'a>,
flags: HashSet<String>, flags: HashSet<String>,
) -> Species<'a> { ) -> Species<'a> {
let mut forms = HashMap::with_capacity(1);
forms.insert("default".to_string(), default_form);
Species { Species {
id, id,
name: name.to_string(), name: name.to_string(),
gender_rate, gender_rate,
growth_rate: growth_rate.to_string(), growth_rate: growth_rate.to_string(),
capture_rate, capture_rate,
forms: hashmap! { forms,
"default".to_string() => default_form,
},
flags, flags,
} }
} }

View File

@ -19,6 +19,24 @@ impl<T> StatisticSet<T>
where where
T: PrimInt, T: PrimInt,
{ {
pub fn new(
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
) -> Self {
Self {
hp,
attack,
defense,
special_attack,
special_defense,
speed,
}
}
pub const fn get_stat(&self, stat: Statistic) -> T { pub const fn get_stat(&self, stat: Statistic) -> T {
match stat { match stat {
Statistic::HP => self.hp, Statistic::HP => self.hp,
@ -63,3 +81,91 @@ where
} }
} }
} }
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug, Getters)]
pub struct ClampedStatisticSet<T, const MIN: i64, const MAX: i64>
where
T: PrimInt,
{
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
}
impl<T, const MIN: i64, const MAX: i64> ClampedStatisticSet<T, MIN, MAX>
where
T: PrimInt,
{
pub const fn get_stat(&self, stat: Statistic) -> T {
match stat {
Statistic::HP => self.hp,
Statistic::Attack => self.attack,
Statistic::Defense => self.defense,
Statistic::SpecialAttack => self.special_attack,
Statistic::SpecialDefense => self.special_defense,
Statistic::Speed => self.speed,
}
}
pub fn set_stat(&mut self, stat: Statistic, mut value: T) {
if value < T::from(MIN).unwrap() {
value = T::from(MIN).unwrap();
} else if value > T::from(MAX).unwrap() {
value = T::from(MAX).unwrap();
}
match stat {
Statistic::HP => self.hp = value,
Statistic::Attack => self.attack = value,
Statistic::Defense => self.defense = value,
Statistic::SpecialAttack => self.special_attack = value,
Statistic::SpecialDefense => self.special_defense = value,
Statistic::Speed => self.speed = value,
}
}
fn change_stat(mut new_value: T, original_value: &mut T) -> bool {
if new_value < T::from(MIN).unwrap() {
new_value = T::from(MIN).unwrap();
} else if new_value > T::from(MAX).unwrap() {
new_value = T::from(MAX).unwrap();
}
if *original_value == new_value {
return false;
}
*original_value = new_value;
true
}
pub fn increase_stat(&mut self, stat: Statistic, value: T) -> bool {
match stat {
Statistic::HP => Self::change_stat(self.hp + value, &mut self.hp),
Statistic::Attack => Self::change_stat(self.attack + value, &mut self.attack),
Statistic::Defense => Self::change_stat(self.defense + value, &mut self.defense),
Statistic::SpecialAttack => {
Self::change_stat(self.special_attack + value, &mut self.special_attack)
}
Statistic::SpecialDefense => {
Self::change_stat(self.special_defense + value, &mut self.special_defense)
}
Statistic::Speed => Self::change_stat(self.speed + value, &mut self.speed),
}
}
pub fn decrease_stat(&mut self, stat: Statistic, value: T) -> bool {
match stat {
Statistic::HP => Self::change_stat(self.hp - value, &mut self.hp),
Statistic::Attack => Self::change_stat(self.attack - value, &mut self.attack),
Statistic::Defense => Self::change_stat(self.defense - value, &mut self.defense),
Statistic::SpecialAttack => {
Self::change_stat(self.special_attack - value, &mut self.special_attack)
}
Statistic::SpecialDefense => {
Self::change_stat(self.special_defense - value, &mut self.special_defense)
}
Statistic::Speed => Self::change_stat(self.speed - value, &mut self.speed),
}
}
}

View File

@ -1,4 +1,4 @@
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Copy, Clone)]
pub enum Statistic { pub enum Statistic {
HP, HP,
Attack, Attack,