Further massive amounts of work
This commit is contained in:
@@ -33,4 +33,9 @@ pub enum Event<'a> {
|
||||
index: u8,
|
||||
pokemon: Option<Arc<RwLock<Pokemon<'a>>>>,
|
||||
},
|
||||
Swap {
|
||||
side_index: u8,
|
||||
index_a: u8,
|
||||
index_b: u8,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,3 +1,80 @@
|
||||
pub trait BattleStatCalculator {
|
||||
//fn is_critical(attack: &ExecutingMove, target: &Pokemon, hit: u8);
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 derive_getters::Getters;
|
||||
use crate::PkmnResult;
|
||||
|
||||
#[derive(Getters, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub struct DynamicLibrary<'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)]
|
||||
pub mod test {
|
||||
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
|
||||
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
|
||||
use crate::static_data::libraries::static_data;
|
||||
|
||||
pub fn build<'a>() -> DynamicLibrary<'a> {
|
||||
DynamicLibrary {
|
||||
static_data: static_data::test::build(),
|
||||
stat_calculator: BattleStatCalculator {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod battle_stat_calculator;
|
||||
pub mod dynamic_library;
|
||||
pub mod script_resolver;
|
||||
|
||||
12
src/dynamic_data/libraries/script_resolver.rs
Normal file
12
src/dynamic_data/libraries/script_resolver.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
pub trait ScriptResolver {}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ScriptCategory {
|
||||
Move,
|
||||
Ability,
|
||||
Status,
|
||||
Pokemon,
|
||||
Battle,
|
||||
Side,
|
||||
ItemBattleTrigger,
|
||||
}
|
||||
@@ -12,8 +12,9 @@ use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Getters, Debug)]
|
||||
pub struct Battle<'a> {
|
||||
#[getter(skip)]
|
||||
library: &'a DynamicLibrary<'a>,
|
||||
parties: Vec<BattleParty>,
|
||||
parties: Vec<BattleParty<'a>>,
|
||||
can_flee: bool,
|
||||
number_of_sides: u8,
|
||||
pokemon_per_side: u8,
|
||||
@@ -33,7 +34,7 @@ pub struct Battle<'a> {
|
||||
impl<'a> Battle<'a> {
|
||||
pub fn new(
|
||||
library: &'a DynamicLibrary<'a>,
|
||||
parties: Vec<BattleParty>,
|
||||
parties: Vec<BattleParty<'a>>,
|
||||
can_flee: bool,
|
||||
number_of_sides: u8,
|
||||
pokemon_per_side: u8,
|
||||
@@ -59,7 +60,7 @@ impl<'a> Battle<'a> {
|
||||
event_hook: Default::default(),
|
||||
history_holder: Box::new(HistoryHolder {}),
|
||||
current_turn: 0,
|
||||
volatile: ScriptSet {},
|
||||
volatile: ScriptSet::default(),
|
||||
last_turn_time: 0,
|
||||
}));
|
||||
|
||||
@@ -77,4 +78,8 @@ impl<'a> Battle<'a> {
|
||||
pub fn can_slot_be_filled(&self) -> bool {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn library(&self) -> &'a DynamicLibrary<'a> {
|
||||
self.library
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,28 @@
|
||||
use crate::dynamic_data::models::pokemon_party::PokemonParty;
|
||||
|
||||
#[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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,16 @@ impl BattleRandom {
|
||||
pub fn get_rng(&self) -> &Mutex<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 {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
use crate::dynamic_data::choices::TurnChoice;
|
||||
use crate::dynamic_data::event_hooks::event_hook::Event;
|
||||
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::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::script_hook;
|
||||
use crate::{script_hook, PkmnResult};
|
||||
use derive_getters::Getters;
|
||||
use std::sync::{Arc, RwLock, Weak};
|
||||
|
||||
@@ -16,7 +20,8 @@ pub struct BattleSide<'a> {
|
||||
fillable_slots: Vec<bool>,
|
||||
choices_set: u8,
|
||||
battle: Weak<RwLock<Battle<'a>>>,
|
||||
has_fled: bool,
|
||||
has_fled_battle: bool,
|
||||
volatile_scripts: Arc<RwLock<ScriptSet>>,
|
||||
}
|
||||
|
||||
impl<'a> BattleSide<'a> {
|
||||
@@ -39,7 +44,8 @@ impl<'a> BattleSide<'a> {
|
||||
fillable_slots,
|
||||
choices_set: 0,
|
||||
battle,
|
||||
has_fled: false,
|
||||
has_fled_battle: false,
|
||||
volatile_scripts: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,6 +142,113 @@ impl<'a> BattleSide<'a> {
|
||||
}
|
||||
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> {
|
||||
|
||||
@@ -5,3 +5,4 @@ pub mod battle_result;
|
||||
pub mod battle_side;
|
||||
pub mod learned_move;
|
||||
pub mod pokemon;
|
||||
pub mod pokemon_party;
|
||||
|
||||
@@ -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");
|
||||
|
||||
80
src/dynamic_data/models/pokemon_party.rs
Normal file
80
src/dynamic_data/models/pokemon_party.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
||||
use std::sync::Weak;
|
||||
|
||||
pub mod script;
|
||||
pub mod script_set;
|
||||
pub mod volatile_scripts;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! script_hook {
|
||||
@@ -24,20 +26,20 @@ pub trait ScriptSource {
|
||||
fn get_script_count(&self);
|
||||
}
|
||||
|
||||
pub enum ScriptWrapper<'a> {
|
||||
Script(&'a Box<dyn Script>),
|
||||
Set(&'a ScriptSet),
|
||||
pub enum ScriptWrapper {
|
||||
Script(Weak<Box<dyn Script>>),
|
||||
Set(Weak<ScriptSet>),
|
||||
}
|
||||
|
||||
pub struct ScriptAggregator<'a> {
|
||||
scripts: Vec<Option<ScriptWrapper<'a>>>,
|
||||
pub struct ScriptAggregator {
|
||||
scripts: Vec<Option<ScriptWrapper>>,
|
||||
size: i32,
|
||||
index: i32,
|
||||
set_index: i32,
|
||||
}
|
||||
|
||||
impl<'a> ScriptAggregator<'a> {
|
||||
pub fn new(scripts: Vec<Option<ScriptWrapper<'a>>>) -> Self {
|
||||
impl ScriptAggregator {
|
||||
pub fn new(scripts: Vec<Option<ScriptWrapper>>) -> Self {
|
||||
let len = scripts.len();
|
||||
Self {
|
||||
scripts,
|
||||
@@ -51,11 +53,13 @@ impl<'a> ScriptAggregator<'a> {
|
||||
if self.index != -1 {
|
||||
if let Some(wrapper) = &self.scripts[self.index as usize] {
|
||||
if let ScriptWrapper::Set(set) = wrapper {
|
||||
self.set_index += 1;
|
||||
if self.set_index as usize >= set.count() {
|
||||
self.set_index = -1;
|
||||
} else {
|
||||
return true;
|
||||
if let Some(set) = set.upgrade() {
|
||||
self.set_index += 1;
|
||||
if self.set_index as usize >= set.count() {
|
||||
self.set_index = -1;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,10 +68,16 @@ impl<'a> ScriptAggregator<'a> {
|
||||
for index in self.index..self.size {
|
||||
self.index = index;
|
||||
if let Some(wrapper) = &self.scripts[index as usize] {
|
||||
if let ScriptWrapper::Set(..) = wrapper {
|
||||
self.set_index = 0;
|
||||
if let ScriptWrapper::Set(s) = wrapper {
|
||||
if let Some(..) = s.upgrade() {
|
||||
self.set_index = 0;
|
||||
return true;
|
||||
}
|
||||
} else if let ScriptWrapper::Script(script) = wrapper {
|
||||
if let Some(..) = script.upgrade() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,8 +89,13 @@ impl<'a> ScriptAggregator<'a> {
|
||||
return None;
|
||||
}
|
||||
return match self.scripts[self.index as usize].as_ref().unwrap() {
|
||||
ScriptWrapper::Script(script) => Some(script),
|
||||
ScriptWrapper::Set(set) => Some(set.at(self.set_index as usize)),
|
||||
// We can make this unsafe as we know there is a strong reference. This is validated in
|
||||
// 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())
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
pub trait Script {
|
||||
fn name(&self) -> &str;
|
||||
|
||||
fn is_suppressed(&self) -> bool {
|
||||
self.get_suppressed_count() > 0
|
||||
}
|
||||
|
||||
@@ -1,14 +1,69 @@
|
||||
use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::PkmnResult;
|
||||
use indexmap::IndexMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ScriptSet {}
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ScriptSet {
|
||||
scripts: IndexMap<String, Arc<Box<dyn Script>>>,
|
||||
}
|
||||
|
||||
impl ScriptSet {
|
||||
pub fn count(&self) -> usize {
|
||||
todo!()
|
||||
pub fn add(&mut self, script: Box<dyn Script>) -> Arc<Box<dyn Script>> {
|
||||
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> {
|
||||
todo!()
|
||||
pub fn stack_or_add<'b, F>(
|
||||
&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()
|
||||
}
|
||||
}
|
||||
|
||||
30
src/dynamic_data/script_handling/volatile_scripts.rs
Normal file
30
src/dynamic_data/script_handling/volatile_scripts.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user