Rework of FFI, adding a value identifier, so we can keep knowledge of data even when data moves.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,7 +2,7 @@ use hashbrown::HashSet;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// An item category defines which bag slot items are stored in.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
@@ -48,6 +48,8 @@ pub enum BattleItemCategory {
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct Item {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The name of the item.
|
||||
name: StringKey,
|
||||
/// Which bag slot items are stored in.
|
||||
@@ -70,6 +72,7 @@ impl Item {
|
||||
flags: HashSet<StringKey>,
|
||||
) -> Item {
|
||||
Item {
|
||||
identifier: Default::default(),
|
||||
name: name.clone(),
|
||||
category,
|
||||
battle_category,
|
||||
@@ -104,3 +107,9 @@ impl Item {
|
||||
self.flags.contains(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for Item {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::Ability;
|
||||
use crate::static_data::DataLibrary;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A storage for all abilities that can be used in this data library.
|
||||
#[derive(Debug)]
|
||||
pub struct AbilityLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying map for the library.
|
||||
map: IndexMap<StringKey, Arc<Ability>>,
|
||||
}
|
||||
@@ -17,6 +19,7 @@ impl AbilityLibrary {
|
||||
/// Instantiates a new ability library.
|
||||
pub fn new(capacity: usize) -> AbilityLibrary {
|
||||
AbilityLibrary {
|
||||
identifier: Default::default(),
|
||||
map: IndexMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
@@ -31,18 +34,25 @@ impl DataLibrary<'_, Ability> for AbilityLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for AbilityLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::static_data::Ability;
|
||||
use crate::static_data::AbilityLibrary;
|
||||
use crate::static_data::DataLibrary;
|
||||
use crate::StringKey;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn build() -> AbilityLibrary {
|
||||
let mut lib = AbilityLibrary::new(1);
|
||||
lib.add(
|
||||
&StringKey::new("test_ability".into()),
|
||||
Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new()),
|
||||
Arc::new(Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new())),
|
||||
);
|
||||
// Drops borrow as mut
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ pub trait DataLibrary<'a, T: 'a> {
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<T>>;
|
||||
|
||||
/// Adds a new value to the library.
|
||||
fn add(&mut self, key: &StringKey, value: T) {
|
||||
self.get_modify().insert(key.clone(), Arc::new(value));
|
||||
fn add(&mut self, key: &StringKey, value: Arc<T>) {
|
||||
self.get_modify().insert(key.clone(), value);
|
||||
}
|
||||
|
||||
/// Removes a value from the library.
|
||||
|
||||
@@ -5,10 +5,12 @@ use hashbrown::HashMap;
|
||||
|
||||
use crate::defines::LevelInt;
|
||||
use crate::static_data::GrowthRate;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A library to store all growth rates.
|
||||
pub struct GrowthRateLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying data structure.
|
||||
growth_rates: HashMap<StringKey, Box<dyn GrowthRate>>,
|
||||
}
|
||||
@@ -17,6 +19,7 @@ impl GrowthRateLibrary {
|
||||
/// Instantiates a new growth rate library with a capacity.
|
||||
pub fn new(capacity: usize) -> GrowthRateLibrary {
|
||||
GrowthRateLibrary {
|
||||
identifier: Default::default(),
|
||||
growth_rates: HashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
@@ -36,6 +39,12 @@ impl GrowthRateLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for GrowthRateLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for GrowthRateLibrary {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("GrowthRateLibrary").finish()
|
||||
|
||||
@@ -4,12 +4,14 @@ use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::DataLibrary;
|
||||
use crate::static_data::Item;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A library to store all items.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct ItemLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying data structure.
|
||||
map: IndexMap<StringKey, Arc<Item>>,
|
||||
}
|
||||
@@ -18,6 +20,7 @@ impl ItemLibrary {
|
||||
/// Instantiates a new Item Library.
|
||||
pub fn new(capacity: usize) -> ItemLibrary {
|
||||
ItemLibrary {
|
||||
identifier: Default::default(),
|
||||
map: IndexMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
@@ -33,9 +36,16 @@ impl DataLibrary<'_, Item> for ItemLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for ItemLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use hashbrown::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::static_data::libraries::data_library::DataLibrary;
|
||||
use crate::static_data::libraries::item_library::ItemLibrary;
|
||||
@@ -57,7 +67,7 @@ pub mod tests {
|
||||
let m = build_item();
|
||||
// Borrow as mut so we can insert
|
||||
let w = &mut lib;
|
||||
w.add(&"foo".into(), m);
|
||||
w.add(&"foo".into(), Arc::new(m));
|
||||
// Drops borrow as mut
|
||||
|
||||
lib
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
use crate::defines::LevelInt;
|
||||
use crate::{ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// This library holds several misc settings for the library.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct LibrarySettings {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The highest level a Pokemon can be.
|
||||
maximum_level: LevelInt,
|
||||
}
|
||||
@@ -11,7 +14,10 @@ pub struct LibrarySettings {
|
||||
impl LibrarySettings {
|
||||
/// Creates a new settings library.
|
||||
pub fn new(maximum_level: LevelInt) -> Self {
|
||||
Self { maximum_level }
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
maximum_level,
|
||||
}
|
||||
}
|
||||
|
||||
/// The highest level a Pokemon can be.
|
||||
@@ -19,3 +25,9 @@ impl LibrarySettings {
|
||||
self.maximum_level
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for LibrarySettings {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::DataLibrary;
|
||||
use crate::static_data::MoveData;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A library to store all data for moves.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct MoveLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying map.
|
||||
map: IndexMap<StringKey, Arc<MoveData>>,
|
||||
}
|
||||
@@ -18,6 +20,7 @@ impl MoveLibrary {
|
||||
/// Instantiates a new Move Library.
|
||||
pub fn new(capacity: usize) -> MoveLibrary {
|
||||
MoveLibrary {
|
||||
identifier: Default::default(),
|
||||
map: IndexMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
@@ -32,9 +35,16 @@ impl DataLibrary<'_, MoveData> for MoveLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for MoveLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use hashbrown::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::static_data::libraries::data_library::DataLibrary;
|
||||
use crate::static_data::libraries::move_library::MoveLibrary;
|
||||
@@ -61,7 +71,7 @@ pub mod tests {
|
||||
let m = build_move();
|
||||
// Borrow as mut so we can insert
|
||||
let w = &mut lib;
|
||||
w.add(&StringKey::new("foo".into()), m);
|
||||
w.add(&StringKey::new("foo".into()), Arc::new(m));
|
||||
// Drops borrow as mut
|
||||
|
||||
lib
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use crate::static_data::Nature;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
use hashbrown::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A library of all natures that can be used, stored by their names.
|
||||
#[derive(Debug)]
|
||||
pub struct NatureLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying data structure.
|
||||
map: HashMap<StringKey, Arc<Nature>>,
|
||||
}
|
||||
@@ -14,13 +16,14 @@ impl NatureLibrary {
|
||||
/// Creates a new nature library with a given capacity.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
NatureLibrary {
|
||||
identifier: Default::default(),
|
||||
map: HashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a new nature with name to the library.
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
|
||||
self.map.insert(name, Arc::new(nature));
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Arc<Nature>) {
|
||||
self.map.insert(name, nature);
|
||||
}
|
||||
|
||||
/// Gets a nature by name.
|
||||
@@ -29,11 +32,11 @@ impl NatureLibrary {
|
||||
}
|
||||
|
||||
/// Finds a nature name by nature.
|
||||
pub fn get_nature_name(&self, nature: &Arc<Nature>) -> StringKey {
|
||||
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
|
||||
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 Arc::ptr_eq(kv.1, nature) {
|
||||
if std::ptr::eq(Arc::as_ptr(kv.1), nature) {
|
||||
return kv.0.clone();
|
||||
}
|
||||
}
|
||||
@@ -41,6 +44,12 @@ impl NatureLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for NatureLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::static_data::statistics::Statistic;
|
||||
|
||||
@@ -4,12 +4,14 @@ use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::DataLibrary;
|
||||
use crate::static_data::Species;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A library to store all data for Pokemon species.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct SpeciesLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying map.
|
||||
map: IndexMap<StringKey, Arc<Species>>,
|
||||
}
|
||||
@@ -18,6 +20,7 @@ impl SpeciesLibrary {
|
||||
/// Instantiates a new Species Library.
|
||||
pub fn new(capacity: usize) -> SpeciesLibrary {
|
||||
SpeciesLibrary {
|
||||
identifier: Default::default(),
|
||||
map: IndexMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
@@ -32,9 +35,16 @@ impl<'a> DataLibrary<'a, Species> for SpeciesLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for SpeciesLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use hashbrown::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::static_data::libraries::data_library::DataLibrary;
|
||||
use crate::static_data::libraries::species_library::SpeciesLibrary;
|
||||
@@ -50,7 +60,7 @@ pub mod tests {
|
||||
0.5,
|
||||
&"test_growthrate".into(),
|
||||
0,
|
||||
Form::new(
|
||||
Arc::new(Form::new(
|
||||
&"default".into(),
|
||||
0.0,
|
||||
0.0,
|
||||
@@ -61,7 +71,7 @@ pub mod tests {
|
||||
Vec::new(),
|
||||
LearnableMoves::new(),
|
||||
HashSet::new(),
|
||||
),
|
||||
)),
|
||||
HashSet::new(),
|
||||
)
|
||||
}
|
||||
@@ -71,7 +81,7 @@ pub mod tests {
|
||||
let species = build_species();
|
||||
// Borrow as mut so we can insert
|
||||
let w = &mut lib;
|
||||
w.add(&"foo".into(), species);
|
||||
w.add(&"foo".into(), Arc::new(species));
|
||||
// Drops borrow as mut
|
||||
|
||||
lib
|
||||
|
||||
@@ -6,11 +6,14 @@ use crate::static_data::MoveLibrary;
|
||||
use crate::static_data::NatureLibrary;
|
||||
use crate::static_data::SpeciesLibrary;
|
||||
use crate::static_data::TypeLibrary;
|
||||
use crate::{ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// The storage for all different libraries.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct StaticData {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// Several misc settings for the library.
|
||||
settings: LibrarySettings,
|
||||
/// All data for Pokemon species.
|
||||
@@ -33,6 +36,7 @@ impl StaticData {
|
||||
/// Instantiates a new data collection.
|
||||
pub fn new(settings: LibrarySettings) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
settings,
|
||||
species: SpeciesLibrary::new(0),
|
||||
moves: MoveLibrary::new(0),
|
||||
@@ -106,6 +110,12 @@ impl StaticData {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for StaticData {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use crate::static_data::libraries::library_settings::LibrarySettings;
|
||||
@@ -116,6 +126,7 @@ pub mod test {
|
||||
|
||||
pub fn build() -> StaticData {
|
||||
StaticData {
|
||||
identifier: Default::default(),
|
||||
settings: LibrarySettings::new(100),
|
||||
species: species_library::tests::build(),
|
||||
moves: move_library::tests::build(),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use atomig::Atom;
|
||||
use hashbrown::HashMap;
|
||||
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A unique key that can be used to store a reference to a type. Opaque reference to a byte
|
||||
/// internally.
|
||||
@@ -28,6 +28,8 @@ impl From<TypeIdentifier> for u8 {
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct TypeLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// A list of types
|
||||
types: HashMap<StringKey, TypeIdentifier>,
|
||||
/// The effectiveness of the different types against each other.
|
||||
@@ -38,6 +40,7 @@ impl TypeLibrary {
|
||||
/// Instantiates a new type library with a specific capacity.
|
||||
pub fn new(capacity: usize) -> TypeLibrary {
|
||||
TypeLibrary {
|
||||
identifier: Default::default(),
|
||||
types: HashMap::with_capacity(capacity),
|
||||
effectiveness: vec![],
|
||||
}
|
||||
@@ -93,6 +96,12 @@ impl TypeLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for TypeLibrary {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use assert_approx_eq::assert_approx_eq;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
#[doc(inline)]
|
||||
pub use growth_rates::*;
|
||||
#[doc(inline)]
|
||||
@@ -40,22 +40,57 @@ mod statistics;
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub enum EffectParameter {
|
||||
/// A boolean value.
|
||||
Bool(bool),
|
||||
Bool(ValueIdentifier, bool),
|
||||
/// An integer value. Stored as a 64 bit int to deal with potentially large numbers.
|
||||
Int(i64),
|
||||
Int(ValueIdentifier, i64),
|
||||
/// A float value. Stored as a 32 bit float.
|
||||
Float(f32),
|
||||
Float(ValueIdentifier, f32),
|
||||
/// A string value.
|
||||
String(StringKey),
|
||||
String(ValueIdentifier, StringKey),
|
||||
}
|
||||
|
||||
impl Into<EffectParameter> for bool {
|
||||
fn into(self) -> EffectParameter {
|
||||
EffectParameter::Bool(Default::default(), self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<EffectParameter> for i64 {
|
||||
fn into(self) -> EffectParameter {
|
||||
EffectParameter::Int(Default::default(), self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<EffectParameter> for f32 {
|
||||
fn into(self) -> EffectParameter {
|
||||
EffectParameter::Float(Default::default(), self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<EffectParameter> for StringKey {
|
||||
fn into(self) -> EffectParameter {
|
||||
EffectParameter::String(Default::default(), self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for EffectParameter {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
EffectParameter::Bool(v) => f.write_fmt(format_args!("EffectParameter::Bool({})", v)),
|
||||
EffectParameter::Int(v) => f.write_fmt(format_args!("EffectParameter::Int({})", v)),
|
||||
EffectParameter::Float(v) => f.write_fmt(format_args!("EffectParameter::Float({})", v)),
|
||||
EffectParameter::String(v) => f.write_fmt(format_args!("EffectParameter::String({})", v)),
|
||||
EffectParameter::Bool(_, v) => f.write_fmt(format_args!("EffectParameter::Bool({})", v)),
|
||||
EffectParameter::Int(_, v) => f.write_fmt(format_args!("EffectParameter::Int({})", v)),
|
||||
EffectParameter::Float(_, v) => f.write_fmt(format_args!("EffectParameter::Float({})", v)),
|
||||
EffectParameter::String(_, v) => f.write_fmt(format_args!("EffectParameter::String({})", v)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for EffectParameter {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
match self {
|
||||
EffectParameter::Bool(i, _) => *i,
|
||||
EffectParameter::Int(i, _) => *i,
|
||||
EffectParameter::Float(i, _) => *i,
|
||||
EffectParameter::String(i, _) => *i,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use hashbrown::HashSet;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::static_data::{SecondaryEffect, TypeIdentifier};
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// The move category defines what global kind of move this move is.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
@@ -63,6 +63,8 @@ pub enum MoveTarget {
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct MoveData {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The name of the move.
|
||||
name: StringKey,
|
||||
/// The attacking type of the move.
|
||||
@@ -101,6 +103,7 @@ impl MoveData {
|
||||
flags: HashSet<StringKey>,
|
||||
) -> MoveData {
|
||||
MoveData {
|
||||
identifier: Default::default(),
|
||||
name: name.clone(),
|
||||
move_type,
|
||||
category,
|
||||
@@ -163,3 +166,9 @@ impl MoveData {
|
||||
self.flags.contains::<u32>(&key_hash)
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for MoveData {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::static_data::EffectParameter;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A secondary effect is an effect on a move that happens after it hits.
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct SecondaryEffect {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The chance in percentages that the effect triggers. -1 to make it always trigger.
|
||||
chance: f32,
|
||||
/// The name of the effect.
|
||||
@@ -16,6 +18,7 @@ impl SecondaryEffect {
|
||||
/// Instantiates a new Secondary Effect.
|
||||
pub fn new(chance: f32, effect_name: StringKey, parameters: Vec<EffectParameter>) -> SecondaryEffect {
|
||||
SecondaryEffect {
|
||||
identifier: Default::default(),
|
||||
chance,
|
||||
effect_name,
|
||||
parameters,
|
||||
@@ -36,6 +39,12 @@ impl SecondaryEffect {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for SecondaryEffect {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use assert_approx_eq::assert_approx_eq;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
use crate::static_data::Statistic;
|
||||
use crate::{ValueIdentifiable, ValueIdentifier};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
|
||||
/// can have an increased statistic and a decreased statistic, or be neutral.
|
||||
#[derive(Debug)]
|
||||
pub struct Nature {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The stat that should receive the increased modifier.
|
||||
increase_stat: Statistic,
|
||||
/// The stat that should receive the decreased modifier.
|
||||
@@ -21,13 +25,14 @@ impl Nature {
|
||||
decrease_stat: Statistic,
|
||||
increase_modifier: f32,
|
||||
decrease_modifier: f32,
|
||||
) -> Self {
|
||||
Self {
|
||||
) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
identifier: Default::default(),
|
||||
increase_stat,
|
||||
decrease_stat,
|
||||
increase_modifier,
|
||||
decrease_modifier,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// The stat that should receive the increased modifier.
|
||||
@@ -52,3 +57,9 @@ impl Nature {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for Nature {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::static_data::EffectParameter;
|
||||
use crate::StringKey;
|
||||
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// An ability is a passive effect in battle that is attached to a Pokemon.
|
||||
#[derive(Debug)]
|
||||
pub struct Ability {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The name of the ability.
|
||||
name: StringKey,
|
||||
/// The name of the script effect of the ability.
|
||||
@@ -16,6 +18,7 @@ impl Ability {
|
||||
/// Instantiates a new ability.
|
||||
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<EffectParameter>) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
name: name.clone(),
|
||||
effect: effect.clone(),
|
||||
parameters,
|
||||
@@ -36,6 +39,12 @@ impl Ability {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for Ability {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
/// An ability index allows us to find an ability on a form. It combines a bool for whether the
|
||||
/// ability is hidden or not, and then an index of the ability.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
|
||||
@@ -4,13 +4,15 @@ use crate::static_data::Statistic;
|
||||
use crate::static_data::TypeIdentifier;
|
||||
use crate::static_data::{Ability, StaticStatisticSet};
|
||||
use crate::static_data::{AbilityIndex, LearnableMoves};
|
||||
use crate::Random;
|
||||
use crate::StringKey;
|
||||
use crate::{Random, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A form is a variant of a specific species. A species always has at least one form, but can have
|
||||
/// many more.
|
||||
#[derive(Debug)]
|
||||
pub struct Form {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The name of the form.
|
||||
name: StringKey,
|
||||
/// The height of the form in meters.
|
||||
@@ -48,6 +50,7 @@ impl Form {
|
||||
flags: HashSet<StringKey>,
|
||||
) -> Form {
|
||||
Form {
|
||||
identifier: Default::default(),
|
||||
name: name.clone(),
|
||||
height,
|
||||
weight,
|
||||
@@ -156,3 +159,9 @@ impl Form {
|
||||
self.flags.contains(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for Form {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
use std::sync::{Arc, LazyLock};
|
||||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use parking_lot::lock_api::RwLockReadGuard;
|
||||
use parking_lot::{RawRwLock, RwLock};
|
||||
|
||||
use crate::static_data::Form;
|
||||
use crate::static_data::Gender;
|
||||
use crate::Random;
|
||||
use crate::StringKey;
|
||||
use crate::{Random, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// The data belonging to a Pokemon with certain characteristics.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct Species {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The national dex identifier of the Pokemon.
|
||||
id: u16,
|
||||
/// The name of the Pokemon.
|
||||
@@ -23,7 +27,7 @@ pub struct Species {
|
||||
/// uncatchable.
|
||||
capture_rate: u8,
|
||||
/// The forms that belong to this Pokemon.
|
||||
forms: HashMap<StringKey, Arc<Form>>,
|
||||
forms: RwLock<HashMap<StringKey, Arc<Form>>>,
|
||||
/// The arbitrary flags that can be set on a Pokemon for script use.
|
||||
flags: HashSet<StringKey>,
|
||||
}
|
||||
@@ -44,18 +48,19 @@ impl Species {
|
||||
gender_rate: f32,
|
||||
growth_rate: &StringKey,
|
||||
capture_rate: u8,
|
||||
default_form: Form,
|
||||
default_form: Arc<Form>,
|
||||
flags: HashSet<StringKey>,
|
||||
) -> Species {
|
||||
let mut forms = HashMap::with_capacity(1);
|
||||
forms.insert_unique_unchecked(get_default_key(), Arc::new(default_form));
|
||||
forms.insert_unique_unchecked(get_default_key(), default_form);
|
||||
Species {
|
||||
identifier: Default::default(),
|
||||
id,
|
||||
name: name.clone(),
|
||||
gender_rate,
|
||||
growth_rate: growth_rate.clone(),
|
||||
capture_rate,
|
||||
forms,
|
||||
forms: RwLock::new(forms),
|
||||
flags,
|
||||
}
|
||||
}
|
||||
@@ -82,8 +87,8 @@ impl Species {
|
||||
self.capture_rate
|
||||
}
|
||||
/// The forms that belong to this Pokemon.
|
||||
pub fn forms(&self) -> &HashMap<StringKey, Arc<Form>> {
|
||||
&self.forms
|
||||
pub fn forms(&self) -> RwLockReadGuard<'_, RawRwLock, HashMap<StringKey, Arc<Form>>> {
|
||||
self.forms.read()
|
||||
}
|
||||
/// The arbitrary flags that can be set on a Pokemon for script use.
|
||||
pub fn flags(&self) -> &HashSet<StringKey> {
|
||||
@@ -91,18 +96,18 @@ impl Species {
|
||||
}
|
||||
|
||||
/// Adds a new form to the species.
|
||||
pub fn add_form(&mut self, id: StringKey, form: Form) {
|
||||
self.forms.insert(id, Arc::new(form));
|
||||
pub fn add_form(&self, id: StringKey, form: Arc<Form>) {
|
||||
self.forms.write().insert(id, form);
|
||||
}
|
||||
|
||||
/// Gets a form by name.
|
||||
pub fn get_form(&self, id: &StringKey) -> Option<&Arc<Form>> {
|
||||
self.forms.get(id)
|
||||
pub fn get_form(&self, id: &StringKey) -> Option<Arc<Form>> {
|
||||
self.forms.read().get(id).cloned()
|
||||
}
|
||||
|
||||
/// Gets the form the Pokemon will have by default, if no other form is specified.
|
||||
pub fn get_default_form(&self) -> &Arc<Form> {
|
||||
self.forms.get(&get_default_key()).unwrap()
|
||||
pub fn get_default_form(&self) -> Arc<Form> {
|
||||
self.forms.read().get(&get_default_key()).unwrap().clone()
|
||||
}
|
||||
|
||||
/// Gets a random gender.
|
||||
@@ -121,3 +126,9 @@ impl Species {
|
||||
self.flags.contains(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for Species {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use crate::{ValueIdentifiable, ValueIdentifier};
|
||||
use atomig::impls::{PrimitiveAtom, PrimitiveAtomInteger};
|
||||
use atomig::{Atom, AtomInteger, Atomic};
|
||||
use num_traits::{clamp, NumCast, PrimInt};
|
||||
@@ -20,6 +21,8 @@ where
|
||||
<T as Atom>::Repr: PrimitiveAtomInteger,
|
||||
T: AtomInteger,
|
||||
{
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The health point stat value.
|
||||
hp: Atomic<T>,
|
||||
/// The physical attack stat value.
|
||||
@@ -45,6 +48,7 @@ where
|
||||
/// Creates a new statistic set with given stats.
|
||||
pub fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
hp: Atomic::<T>::new(hp),
|
||||
attack: Atomic::<T>::new(attack),
|
||||
defense: Atomic::<T>::new(defense),
|
||||
@@ -128,6 +132,19 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ValueIdentifiable for StatisticSet<T>
|
||||
where
|
||||
T: PrimitiveAtom,
|
||||
T: Atom,
|
||||
T: PrimitiveAtomInteger,
|
||||
<T as Atom>::Repr: PrimitiveAtomInteger,
|
||||
T: AtomInteger,
|
||||
{
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
/// A collection of statistics that can not be modified after creation.
|
||||
///
|
||||
/// As no modifications happen, this struct does not use atomics.
|
||||
@@ -136,6 +153,8 @@ pub struct StaticStatisticSet<T>
|
||||
where
|
||||
T: PrimInt,
|
||||
{
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The health point stat value.
|
||||
hp: T,
|
||||
/// The physical attack stat value.
|
||||
@@ -155,8 +174,9 @@ where
|
||||
T: PrimInt,
|
||||
{
|
||||
/// Create a new static statistic set.
|
||||
pub const fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
|
||||
pub fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
hp,
|
||||
attack,
|
||||
defense,
|
||||
@@ -204,6 +224,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ValueIdentifiable for StaticStatisticSet<T>
|
||||
where
|
||||
T: PrimInt,
|
||||
{
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
/// A clamped statistic set holds the 6 normal stats for a Pokemon, but ensures it always remains
|
||||
/// between two values (inclusive on the two values).
|
||||
#[derive(Default, Debug)]
|
||||
@@ -218,6 +247,8 @@ where
|
||||
T: NumCast,
|
||||
T: PrimInt,
|
||||
{
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The health point stat value.
|
||||
hp: Atomic<T>,
|
||||
/// The physical attack stat value.
|
||||
@@ -264,6 +295,7 @@ where
|
||||
/// Instantiates a new clamped statistic set.
|
||||
pub fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
hp: Self::clamped_cast(hp),
|
||||
attack: Self::clamped_cast(attack),
|
||||
defense: Self::clamped_cast(defense),
|
||||
@@ -377,6 +409,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const MIN: i64, const MAX: i64> ValueIdentifiable for ClampedStatisticSet<T, MIN, MAX>
|
||||
where
|
||||
T: PrimitiveAtom,
|
||||
T: Atom,
|
||||
T: PrimitiveAtomInteger,
|
||||
<T as Atom>::Repr: PrimitiveAtomInteger,
|
||||
T: AtomInteger,
|
||||
T: NumCast,
|
||||
T: PrimInt,
|
||||
{
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user