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

This commit is contained in:
2022-10-08 13:15:04 +02:00
parent 84ddf0307d
commit 41b40ef98e
38 changed files with 582 additions and 230 deletions

View File

@@ -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)]

View File

@@ -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
}
}

View File

@@ -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
}
}