2022-06-30 15:34:57 +00:00
|
|
|
use std::ffi::c_void;
|
2022-06-19 19:34:08 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
|
2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::libraries::damage_library::DamageLibrary;
|
|
|
|
use crate::dynamic_data::libraries::misc_library::MiscLibrary;
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::libraries::script_resolver::ScriptCategory;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::Script;
|
2022-06-30 15:34:57 +00:00
|
|
|
use crate::dynamic_data::{ItemScript, ScriptResolver};
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::static_data::Item;
|
|
|
|
use crate::static_data::StaticData;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::{PkmnResult, StringKey};
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The dynamic library stores a static data library, as well as holding different libraries and
|
|
|
|
/// calculators that might be customized between different generations and implementations.
|
2022-06-06 11:54:59 +00:00
|
|
|
#[derive(Debug)]
|
2022-06-16 15:59:33 +00:00
|
|
|
pub struct DynamicLibrary {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The static data is the immutable storage data for this library.
|
2022-06-17 17:53:33 +00:00
|
|
|
static_data: StaticData,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
|
|
|
|
/// Pokemons attributes.
|
2022-06-19 19:34:08 +00:00
|
|
|
stat_calculator: Box<dyn BattleStatCalculator>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The damage calculator deals with the calculation of things relating to damage.
|
2022-06-16 15:59:33 +00:00
|
|
|
damage_calculator: Box<dyn DamageLibrary>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
|
|
|
|
/// calculators.
|
2022-06-16 15:59:33 +00:00
|
|
|
misc_library: Box<dyn MiscLibrary<'static>>,
|
2022-06-30 15:34:57 +00:00
|
|
|
|
|
|
|
/// The script resolver deals with how to resolve the scripts from specific unique key combinations.
|
|
|
|
script_resolver: Box<dyn ScriptResolver>,
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
unsafe impl Sync for DynamicLibrary {}
|
|
|
|
unsafe impl Send for DynamicLibrary {}
|
|
|
|
|
|
|
|
impl DynamicLibrary {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Instantiates a new DynamicLibrary with given parameters.
|
2022-06-17 17:53:33 +00:00
|
|
|
pub fn new(
|
|
|
|
static_data: StaticData,
|
2022-06-19 19:34:08 +00:00
|
|
|
stat_calculator: Box<dyn BattleStatCalculator>,
|
2022-06-17 17:53:33 +00:00
|
|
|
damage_calculator: Box<dyn DamageLibrary>,
|
|
|
|
misc_library: Box<dyn MiscLibrary<'static>>,
|
2022-06-30 15:34:57 +00:00
|
|
|
script_resolver: Box<dyn ScriptResolver>,
|
2022-06-17 17:53:33 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
static_data,
|
|
|
|
stat_calculator,
|
|
|
|
damage_calculator,
|
|
|
|
misc_library,
|
2022-06-30 15:34:57 +00:00
|
|
|
script_resolver,
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The static data is the immutable storage data for this library.
|
2022-06-17 17:53:33 +00:00
|
|
|
pub fn static_data(&self) -> &StaticData {
|
2022-06-06 11:54:59 +00:00
|
|
|
&self.static_data
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
|
|
|
|
/// Pokemons attributes.
|
2022-06-19 19:34:08 +00:00
|
|
|
pub fn stat_calculator(&self) -> &dyn BattleStatCalculator {
|
|
|
|
self.stat_calculator.deref()
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The damage calculator deals with the calculation of things relating to damage.
|
2022-06-18 14:06:54 +00:00
|
|
|
pub fn damage_calculator(&self) -> &dyn DamageLibrary {
|
|
|
|
self.damage_calculator.deref()
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
|
|
|
|
/// calculators.
|
2022-06-18 14:06:54 +00:00
|
|
|
pub fn misc_library(&self) -> &dyn MiscLibrary<'static> {
|
|
|
|
self.misc_library.deref()
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Loads a standard script with a given unique combination of category and key. If no script
|
|
|
|
/// can be created with this combination, returns None.
|
|
|
|
pub fn load_script(
|
|
|
|
&self,
|
|
|
|
owner: *const c_void,
|
|
|
|
_category: ScriptCategory,
|
|
|
|
_key: &StringKey,
|
|
|
|
) -> PkmnResult<Option<Arc<dyn Script>>> {
|
|
|
|
self.script_resolver.load_script(owner, _category, _key)
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Loads an item script with the given unique key. If no script can be created with this
|
|
|
|
/// combinations, returns None. Note that ItemScripts are immutable, as their script should be
|
|
|
|
/// shared between all different usages.
|
|
|
|
pub fn load_item_script(&self, _key: &Item) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
|
2022-06-06 11:54:59 +00:00
|
|
|
todo!()
|
|
|
|
}
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test {
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::libraries::battle_stat_calculator::Gen7BattleStatCalculator;
|
2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::libraries::damage_library::Gen7DamageLibrary;
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
|
2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::libraries::misc_library::Gen7MiscLibrary;
|
2022-06-30 15:34:57 +00:00
|
|
|
use crate::dynamic_data::EmptyScriptResolver;
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-06-18 14:06:54 +00:00
|
|
|
pub fn build() -> DynamicLibrary {
|
2021-01-31 16:31:22 +00:00
|
|
|
DynamicLibrary {
|
2022-06-19 19:34:08 +00:00
|
|
|
static_data: crate::static_data::libraries::static_data::test::build(),
|
|
|
|
stat_calculator: Box::new(Gen7BattleStatCalculator {}),
|
2022-06-16 15:59:33 +00:00
|
|
|
damage_calculator: Box::new(Gen7DamageLibrary::new(false)),
|
|
|
|
misc_library: Box::new(Gen7MiscLibrary::new()),
|
2022-06-30 15:34:57 +00:00
|
|
|
script_resolver: Box::new(EmptyScriptResolver {}),
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|