Complete refactor of the FFI to use handles instead of pointers.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-06-24 14:44:23 +02:00
parent 4c222cb753
commit 78bb91093b
76 changed files with 1510 additions and 1952 deletions

View File

@@ -1,10 +1,10 @@
use crate::static_data::EffectParameter;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use crate::StringKey;
use std::fmt::Debug;
use std::sync::Arc;
/// An ability is a passive effect in battle that is attached to a Pokemon.
pub trait Ability: Debug + ValueIdentifiable {
pub trait Ability: Debug {
/// The name of the ability.
fn name(&self) -> &StringKey;
/// The name of the script effect of the ability.
@@ -16,8 +16,6 @@ pub trait Ability: Debug + ValueIdentifiable {
/// An ability is a passive effect in battle that is attached to a Pokemon.
#[derive(Debug)]
pub struct AbilityImpl {
/// 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.
@@ -30,7 +28,6 @@ impl AbilityImpl {
/// Instantiates a new ability.
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<Arc<EffectParameter>>) -> Self {
Self {
identifier: Default::default(),
name: name.clone(),
effect: effect.clone(),
parameters,
@@ -53,12 +50,6 @@ impl Ability for AbilityImpl {
}
}
impl ValueIdentifiable for AbilityImpl {
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)]
@@ -84,10 +75,5 @@ pub(crate) mod tests {
fn effect(&self) -> &StringKey;
fn parameters(&self) -> &Vec<Arc<EffectParameter>>;
}
impl ValueIdentifiable for Ability {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}

View File

@@ -7,12 +7,12 @@ 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, ValueIdentifiable, ValueIdentifier};
use crate::Random;
use crate::{StringKey, VecExt};
/// A form is a variant of a specific species. A species always has at least one form, but can have
/// many more.
pub trait Form: ValueIdentifiable + Debug {
pub trait Form: Debug {
/// The name of the form.
fn name(&self) -> &StringKey;
/// The height of the form in meters.
@@ -31,7 +31,7 @@ pub trait Form: ValueIdentifiable + Debug {
fn hidden_abilities(&self) -> &Vec<StringKey>;
/// The moves a Pokemon with this form can learn.
fn moves(&self) -> &Box<dyn LearnableMoves>;
fn moves(&self) -> &Arc<dyn LearnableMoves>;
/// Arbitrary flags can be set on a form for scripting use.
fn flags(&self) -> &HashSet<StringKey>;
@@ -62,8 +62,6 @@ pub trait Form: ValueIdentifiable + Debug {
/// many more.
#[derive(Debug)]
pub struct FormImpl {
/// 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.
@@ -81,7 +79,7 @@ pub struct FormImpl {
/// The possible hidden abilities a Pokemon with this form can have.
hidden_abilities: Vec<StringKey>,
/// The moves a Pokemon with this form can learn.
moves: Box<dyn LearnableMoves>,
moves: Arc<dyn LearnableMoves>,
/// Arbitrary flags can be set on a form for scripting use.
flags: HashSet<StringKey>,
}
@@ -94,20 +92,19 @@ impl FormImpl {
weight: f32,
base_experience: u32,
types: Vec<TypeIdentifier>,
base_stats: StaticStatisticSet<u16>,
base_stats: Arc<StaticStatisticSet<u16>>,
abilities: Vec<StringKey>,
hidden_abilities: Vec<StringKey>,
moves: Box<dyn LearnableMoves>,
moves: Arc<dyn LearnableMoves>,
flags: HashSet<StringKey>,
) -> Self {
Self {
identifier: Default::default(),
name: name.clone(),
height,
weight,
base_experience,
types,
base_stats: Arc::new(base_stats),
base_stats,
abilities,
hidden_abilities,
moves,
@@ -151,7 +148,7 @@ impl Form for FormImpl {
}
/// The moves a Pokemon with this form can learn.
fn moves(&self) -> &Box<dyn LearnableMoves> {
fn moves(&self) -> &Arc<dyn LearnableMoves> {
&self.moves
}
/// Arbitrary flags can be set on a form for scripting use.
@@ -222,12 +219,6 @@ impl Form for FormImpl {
}
}
impl ValueIdentifiable for FormImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
#[allow(clippy::unwrap_used)]
@@ -246,7 +237,7 @@ pub(crate) mod tests {
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>>;
fn abilities(&self) -> &Vec<StringKey>;
fn hidden_abilities(&self) -> &Vec<StringKey>;
fn moves(&self) -> &Box<dyn LearnableMoves>;
fn moves(&self) -> &Arc<dyn LearnableMoves>;
fn flags(&self) -> &HashSet<StringKey>;
fn get_type(&self, index: usize) -> Result<TypeIdentifier>;
fn get_base_stat(&self, stat: Statistic) -> u16;
@@ -257,10 +248,5 @@ pub(crate) mod tests {
fn has_flag(&self, key: &StringKey) -> bool;
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
}
impl ValueIdentifiable for Form {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}

View File

@@ -1,5 +1,6 @@
use anyhow_ext::Result;
use indexmap::IndexSet;
use parking_lot::RwLock;
use std::fmt::Debug;
use crate::defines::LevelInt;
@@ -8,27 +9,27 @@ use crate::{StringKey, VecExt};
/// The storage of the moves a Pokemon can learn.
pub trait LearnableMoves: Debug {
/// Adds a new level move the Pokemon can learn.
fn add_level_move(&mut self, level: LevelInt, m: &StringKey) -> Result<()>;
fn add_level_move(&self, level: LevelInt, m: &StringKey) -> Result<()>;
/// Gets all moves a Pokemon can learn when leveling up to a specific level.
fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<StringKey>>;
fn get_learned_by_level(&self, level: LevelInt) -> Option<Vec<StringKey>>;
/// Gets the distinct moves a Pokemon can learn through leveling up.
fn get_distinct_level_moves(&self) -> &IndexSet<StringKey>;
fn get_distinct_level_moves(&self) -> IndexSet<StringKey>;
}
/// The storage of the moves a Pokemon can learn.
#[derive(PartialEq, Eq, Debug)]
#[derive(Debug)]
pub struct LearnableMovesImpl {
/// A map of the moves a Pokemon can learn per level.
learned_by_level: Vec<Vec<StringKey>>,
learned_by_level: RwLock<Vec<Vec<StringKey>>>,
/// A list of the distinct moves a Pokemon can learn through leveling up.
distinct_level_moves: IndexSet<StringKey>,
distinct_level_moves: RwLock<IndexSet<StringKey>>,
}
impl LearnableMovesImpl {
/// Instantiates a new Learnable Moves.
pub fn new(max_level: LevelInt) -> Self {
Self {
learned_by_level: vec![Vec::new(); (max_level + 1) as usize],
learned_by_level: RwLock::new(vec![Vec::new(); (max_level + 1) as usize]),
distinct_level_moves: Default::default(),
}
}
@@ -36,20 +37,23 @@ impl LearnableMovesImpl {
impl LearnableMoves for LearnableMovesImpl {
/// Adds a new level move the Pokemon can learn.
fn add_level_move(&mut self, level: LevelInt, m: &StringKey) -> Result<()> {
self.learned_by_level.get_mut_res(level as usize)?.push(m.clone());
self.distinct_level_moves.insert(m.clone());
fn add_level_move(&self, level: LevelInt, m: &StringKey) -> Result<()> {
self.learned_by_level
.write()
.get_mut_res(level as usize)?
.push(m.clone());
self.distinct_level_moves.write().insert(m.clone());
Ok(())
}
/// Gets all moves a Pokemon can learn when leveling up to a specific level.
fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<StringKey>> {
self.learned_by_level.get(level as usize)
fn get_learned_by_level(&self, level: LevelInt) -> Option<Vec<StringKey>> {
self.learned_by_level.read().get(level as usize).cloned()
}
/// Gets the distinct moves a Pokemon can learn through leveling up.
fn get_distinct_level_moves(&self) -> &IndexSet<StringKey> {
&self.distinct_level_moves
fn get_distinct_level_moves(&self) -> IndexSet<StringKey> {
self.distinct_level_moves.read().clone()
}
}
@@ -61,7 +65,7 @@ pub(crate) mod tests {
#[test]
fn adds_level_moves() {
let mut moves = LearnableMovesImpl::new(100);
let moves = LearnableMovesImpl::new(100);
moves.add_level_move(1, &"foo".into()).unwrap();
moves.add_level_move(1, &"bar".into()).unwrap();
@@ -73,7 +77,7 @@ pub(crate) mod tests {
#[test]
fn adds_two_same_moves_at_different_level() {
let mut moves = LearnableMovesImpl::new(100);
let moves = LearnableMovesImpl::new(100);
moves.add_level_move(1, &"foo".into()).unwrap();
moves.add_level_move(5, &"foo".into()).unwrap();

View File

@@ -8,11 +8,11 @@ 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.
pub trait Species: ValueIdentifiable + Debug {
pub trait Species: Debug {
/// The national dex identifier of the Pokemon.
fn id(&self) -> u16;
/// The name of the Pokemon.
@@ -47,8 +47,6 @@ pub trait Species: ValueIdentifiable + Debug {
/// The data belonging to a Pokemon with certain characteristics.
#[derive(Debug)]
pub struct SpeciesImpl {
/// 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.
@@ -88,7 +86,6 @@ impl SpeciesImpl {
let mut forms = HashMap::with_capacity(1);
forms.insert_unique_unchecked(get_default_key(), default_form);
Self {
identifier: Default::default(),
id,
name: name.clone(),
gender_rate,
@@ -176,12 +173,6 @@ impl Species for SpeciesImpl {
}
}
impl ValueIdentifiable for SpeciesImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
#[allow(clippy::unwrap_used)]
@@ -207,10 +198,5 @@ pub(crate) mod tests {
fn has_flag(&self, key: &StringKey) -> bool;
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
}
impl ValueIdentifiable for Species {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}