Large amounts of work on Rune
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2024-05-08 15:46:09 +02:00
parent 4bc76b0ee4
commit 4ec07ca049
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
43 changed files with 979 additions and 397 deletions

View File

@ -68,9 +68,7 @@ impl ChoiceQueue {
} }
/// Check if we have any choices remaining. /// Check if we have any choices remaining.
pub fn has_next(&self) -> bool { pub fn has_next(&self) -> bool { self.current.load(Ordering::Relaxed) < self.queue.read().len() }
self.current.load(Ordering::Relaxed) < self.queue.read().len()
}
/// This resorts the yet to be executed choices. This can be useful for dealing with situations /// This resorts the yet to be executed choices. This can be useful for dealing with situations
/// such as Pokemon changing forms just after the very start of a turn, when turn order has /// such as Pokemon changing forms just after the very start of a turn, when turn order has
@ -163,7 +161,6 @@ mod tests {
use crate::defines::LevelInt; use crate::defines::LevelInt;
use crate::dynamic_data::{DynamicLibrary, PassChoice}; use crate::dynamic_data::{DynamicLibrary, PassChoice};
use crate::static_data::{AbilityIndex, Gender}; use crate::static_data::{AbilityIndex, Gender};
use std::sync::Arc;
#[test] #[test]
fn create_empty_queue() { fn create_empty_queue() {
let queue = ChoiceQueue::new(Vec::new()); let queue = ChoiceQueue::new(Vec::new());

View File

@ -1,9 +1,5 @@
#[doc(inline)] #[doc(inline)] pub use choice_queue::*;
pub use choice_queue::*; #[doc(inline)] pub use target_resolver::*;
#[doc(inline)]
pub use target_resolver::*;
#[doc(inline)]
pub use turn_runner::*;
/// Data for enqueueing and retrieving choices. /// Data for enqueueing and retrieving choices.
mod choice_queue; mod choice_queue;

View File

@ -4,6 +4,7 @@ use crate::static_data::{EvolutionMethod, TimeOfDay};
/// A library for handling the checking of evolution requirements. /// A library for handling the checking of evolution requirements.
pub trait EvolutionLibrary { pub trait EvolutionLibrary {
/// Checks if the given Pokemon fulfills the given evolution conditions. /// Checks if the given Pokemon fulfills the given evolution conditions.
#[allow(dead_code)]
fn pokemon_fulfills_evolution_conditions(&self, pokemon: &Pokemon, method: &EvolutionMethod) -> bool; fn pokemon_fulfills_evolution_conditions(&self, pokemon: &Pokemon, method: &EvolutionMethod) -> bool;
} }

View File

@ -125,57 +125,31 @@ impl Battle {
} }
/// The library the battle uses for handling. /// The library the battle uses for handling.
pub fn library(&self) -> &Arc<dyn DynamicLibrary> { pub fn library(&self) -> &Arc<dyn DynamicLibrary> { &self.data.library }
&self.data.library
}
/// A list of all different parties in the battle. /// A list of all different parties in the battle.
pub fn parties(&self) -> &Vec<Arc<BattleParty>> { pub fn parties(&self) -> &Vec<Arc<BattleParty>> { &self.data.parties }
&self.data.parties
}
/// Whether or not Pokemon can flee from the battle. /// Whether or not Pokemon can flee from the battle.
pub fn can_flee(&self) -> bool { pub fn can_flee(&self) -> bool { self.data.can_flee }
self.data.can_flee
}
/// The number of sides in the battle. Typically 2. /// The number of sides in the battle. Typically 2.
pub fn number_of_sides(&self) -> u8 { pub fn number_of_sides(&self) -> u8 { self.data.number_of_sides }
self.data.number_of_sides
}
/// The number of Pokemon that can be on each side. /// The number of Pokemon that can be on each side.
pub fn pokemon_per_side(&self) -> u8 { pub fn pokemon_per_side(&self) -> u8 { self.data.pokemon_per_side }
self.data.pokemon_per_side
}
/// A list of all sides in the battle. /// A list of all sides in the battle.
pub fn sides(&self) -> &Vec<BattleSide> { pub fn sides(&self) -> &Vec<BattleSide> { &self.data.sides }
&self.data.sides
}
/// The RNG used for the battle. /// The RNG used for the battle.
pub fn random(&self) -> &Arc<BattleRandom> { pub fn random(&self) -> &Arc<BattleRandom> { &self.data.random }
&self.data.random
}
/// Whether or not the battle has ended. /// Whether or not the battle has ended.
pub fn has_ended(&self) -> bool { pub fn has_ended(&self) -> bool { self.data.has_ended.load(Ordering::Relaxed) }
self.data.has_ended.load(Ordering::Relaxed)
}
/// The eventual result of the battle. Inconclusive until the battle is ended. /// The eventual result of the battle. Inconclusive until the battle is ended.
pub fn result(&self) -> BattleResult { pub fn result(&self) -> BattleResult { *self.data.result.read() }
*self.data.result.read()
}
/// The handler to send all events to. /// The handler to send all events to.
pub fn event_hook(&self) -> &EventHook { pub fn event_hook(&self) -> &EventHook { &self.data.event_hook }
&self.data.event_hook
}
/// The index of the current turn. 0 until all choices /// The index of the current turn. 0 until all choices
pub fn current_turn(&self) -> u32 { pub fn current_turn(&self) -> u32 { self.data.current_turn.load(Ordering::Relaxed) }
self.data.current_turn.load(Ordering::Relaxed)
}
/// The time in nanoseconds the last turn took to run. Defaults to 0. /// The time in nanoseconds the last turn took to run. Defaults to 0.
pub fn last_turn_time(&self) -> u64 { pub fn last_turn_time(&self) -> u64 { self.data.last_turn_time.load(Ordering::Relaxed) }
self.data.last_turn_time.load(Ordering::Relaxed)
}
/// A queue of the yet to be executed choices in a turn. /// A queue of the yet to be executed choices in a turn.
pub fn current_turn_queue(&self) -> &RwLock<Option<Arc<ChoiceQueue>>> { pub fn current_turn_queue(&self) -> &RwLock<Option<Arc<ChoiceQueue>>> { &self.data.current_turn_queue }
&self.data.current_turn_queue
}
/// Get a Pokemon on the battlefield, on a specific side and an index on that side. /// Get a Pokemon on the battlefield, on a specific side and an index on that side.
pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> { pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> {
@ -379,28 +353,21 @@ impl Battle {
} }
/// Gets the inner pointer to the reference counted data. /// Gets the inner pointer to the reference counted data.
pub fn as_ptr(&self) -> *const c_void { pub fn as_ptr(&self) -> *const c_void { Arc::as_ptr(&self.data) as *const c_void }
Arc::as_ptr(&self.data) as *const c_void
}
} }
impl PartialEq for Battle { impl PartialEq for Battle {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.data, &other.data) }
Arc::ptr_eq(&self.data, &other.data)
}
} }
impl WeakBattleReference { impl WeakBattleReference {
/// Attempts to upgrade the weak reference to a strong reference. If the strong reference has /// Attempts to upgrade the weak reference to a strong reference. If the strong reference has
/// been dropped, this returns None. /// been dropped, this returns None.
pub fn upgrade(&self) -> Option<Battle> { pub fn upgrade(&self) -> Option<Battle> { self.data.upgrade().map(|battle| Battle { data: battle }) }
self.data.upgrade().map(|battle| Battle { data: battle })
}
/// Gets the inner pointer to the reference counted data. /// Gets the inner pointer to the reference counted data.
pub(crate) fn as_ptr(&self) -> *const c_void { #[cfg(feature = "wasm")]
self.data.as_ptr() as *const c_void pub(crate) fn as_ptr(&self) -> *const c_void { self.data.as_ptr() as *const c_void }
}
} }
unsafe impl Send for WeakBattleReference {} unsafe impl Send for WeakBattleReference {}
@ -408,17 +375,13 @@ unsafe impl Send for WeakBattleReference {}
unsafe impl Sync for WeakBattleReference {} unsafe impl Sync for WeakBattleReference {}
impl PartialEq for WeakBattleReference { impl PartialEq for WeakBattleReference {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool { self.data.ptr_eq(&other.data) }
self.data.ptr_eq(&other.data)
}
} }
impl Eq for WeakBattleReference {} impl Eq for WeakBattleReference {}
impl VolatileScriptsOwner for Battle { impl VolatileScriptsOwner for Battle {
fn volatile_scripts(&self) -> &Arc<ScriptSet> { fn volatile_scripts(&self) -> &Arc<ScriptSet> { &self.data.volatile_scripts }
&self.data.volatile_scripts
}
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> { fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
self.data.library.load_script(self.into(), ScriptCategory::Battle, key) self.data.library.load_script(self.into(), ScriptCategory::Battle, key)
@ -426,13 +389,9 @@ impl VolatileScriptsOwner for Battle {
} }
impl ScriptSource for Battle { impl ScriptSource for Battle {
fn get_script_count(&self) -> Result<usize> { fn get_script_count(&self) -> Result<usize> { Ok(1) }
Ok(1)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { &self.data.script_source_data }
&self.data.script_source_data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) { fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.data.weather).into()); scripts.push((&self.data.weather).into());
@ -457,7 +416,5 @@ pub enum BattleResult {
impl BattleResult { impl BattleResult {
/// Whether or not the battle has a winner. /// Whether or not the battle has a winner.
pub fn is_conclusive(&self) -> bool { pub fn is_conclusive(&self) -> bool { matches!(self, Self::Conclusive(_)) }
matches!(self, Self::Conclusive(_))
}
} }

View File

@ -101,30 +101,18 @@ impl BattleSide {
} }
/// The index of the side on the battle. /// The index of the side on the battle.
pub fn index(&self) -> u8 { pub fn index(&self) -> u8 { self.data.index }
self.data.index
}
/// The number of Pokemon that can be on the side. /// The number of Pokemon that can be on the side.
pub fn pokemon_per_side(&self) -> u8 { pub fn pokemon_per_side(&self) -> u8 { self.data.pokemon_per_side }
self.data.pokemon_per_side
}
/// A list of pokemon currently on the battlefield. /// A list of pokemon currently on the battlefield.
pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Pokemon>>> { pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Pokemon>>> { self.data.pokemon.read() }
self.data.pokemon.read()
}
/// The currently set choices for all Pokemon on the battlefield. Cleared when the turn starts. /// The currently set choices for all Pokemon on the battlefield. Cleared when the turn starts.
pub fn choices(&self) -> &RwLock<Vec<Option<Arc<TurnChoice>>>> { pub fn choices(&self) -> &RwLock<Vec<Option<Arc<TurnChoice>>>> { &self.data.choices }
&self.data.choices
}
/// The slots on the side that can still be filled. Once all slots are set to false, this side /// The slots on the side that can still be filled. Once all slots are set to false, this side
/// has lost the battle. /// has lost the battle.
pub fn fillable_slots(&self) -> &Vec<AtomicBool> { pub fn fillable_slots(&self) -> &Vec<AtomicBool> { &self.data.fillable_slots }
&self.data.fillable_slots
}
/// The number of choices that are set. /// The number of choices that are set.
pub fn choices_set(&self) -> u8 { pub fn choices_set(&self) -> u8 { self.data.choices_set.load(Ordering::SeqCst) }
self.data.choices_set.load(Ordering::SeqCst)
}
/// A reference to the battle we're part of. /// A reference to the battle we're part of.
pub fn battle(&self) -> Result<Battle> { pub fn battle(&self) -> Result<Battle> {
self.data self.data
@ -133,18 +121,12 @@ impl BattleSide {
.ok_or(anyhow!("Battle was not set, but requested")) .ok_or(anyhow!("Battle was not set, but requested"))
} }
/// Whether or not this side has fled. /// Whether or not this side has fled.
pub fn has_fled_battle(&self) -> bool { pub fn has_fled_battle(&self) -> bool { self.data.has_fled_battle.load(Ordering::SeqCst) }
self.data.has_fled_battle.load(Ordering::SeqCst)
}
/// The volatile scripts that are attached to the side. /// The volatile scripts that are attached to the side.
pub fn volatile_scripts(&self) -> &Arc<ScriptSet> { pub fn volatile_scripts(&self) -> &Arc<ScriptSet> { &self.data.volatile_scripts }
&self.data.volatile_scripts
}
/// Whether every Pokemon on this side has its choices /// Whether every Pokemon on this side has its choices
pub fn all_choices_set(&self) -> bool { pub fn all_choices_set(&self) -> bool { self.choices_set() == self.data.pokemon_per_side }
self.choices_set() == self.data.pokemon_per_side
}
/// Returns true if there are slots that need to be filled with a new pokemon, that have parties /// Returns true if there are slots that need to be filled with a new pokemon, that have parties
/// responsible for them. Returns false if all slots are filled with usable pokemon, or slots are /// responsible for them. Returns false if all slots are filled with usable pokemon, or slots are
@ -190,9 +172,7 @@ impl BattleSide {
} }
/// Forcibly removes a Pokemon from the field. /// Forcibly removes a Pokemon from the field.
pub fn force_clear_pokemon(&mut self, index: u8) { pub fn force_clear_pokemon(&mut self, index: u8) { self.data.pokemon.write().get_mut(index as usize).take(); }
self.data.pokemon.write().get_mut(index as usize).take();
}
/// Switches out a spot on the field for a different Pokemon. /// Switches out a spot on the field for a different Pokemon.
pub fn set_pokemon(&self, index: u8, pokemon: Option<Pokemon>) -> Result<()> { pub fn set_pokemon(&self, index: u8, pokemon: Option<Pokemon>) -> Result<()> {
@ -296,9 +276,7 @@ impl BattleSide {
} }
/// Mark the side as fled. /// Mark the side as fled.
pub fn mark_as_fled(&mut self) { pub fn mark_as_fled(&mut self) { self.data.has_fled_battle.store(true, Ordering::SeqCst); }
self.data.has_fled_battle.store(true, Ordering::SeqCst);
}
/// Gets a random Pokemon on the given side. /// Gets a random Pokemon on the given side.
pub fn get_random_creature_index(&self) -> Result<u8> { pub fn get_random_creature_index(&self) -> Result<u8> {
@ -364,28 +342,21 @@ impl BattleSide {
} }
/// Gets the inner pointer to the reference counted data. /// Gets the inner pointer to the reference counted data.
pub fn as_ptr(&self) -> *const c_void { pub fn as_ptr(&self) -> *const c_void { Arc::as_ptr(&self.data) as *const c_void }
Arc::as_ptr(&self.data) as *const c_void
}
} }
impl PartialEq for BattleSide { impl PartialEq for BattleSide {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.data, &other.data) }
Arc::ptr_eq(&self.data, &other.data)
}
} }
impl WeakBattleSideReference { impl WeakBattleSideReference {
/// Upgrades the weak reference to a strong reference, returning `None` if the side has been /// Upgrades the weak reference to a strong reference, returning `None` if the side has been
/// dropped. /// dropped.
pub fn upgrade(&self) -> Option<BattleSide> { pub fn upgrade(&self) -> Option<BattleSide> { self.data.upgrade().map(|data| BattleSide { data }) }
self.data.upgrade().map(|data| BattleSide { data })
}
/// Gets the underlying pointer to the data of the side. /// Gets the underlying pointer to the data of the side.
pub(crate) fn as_ptr(&self) -> *const c_void { #[cfg(feature = "wasm")]
self.data.as_ptr() as *const c_void pub(crate) fn as_ptr(&self) -> *const c_void { self.data.as_ptr() as *const c_void }
}
} }
unsafe impl Send for WeakBattleSideReference {} unsafe impl Send for WeakBattleSideReference {}
@ -393,15 +364,11 @@ unsafe impl Send for WeakBattleSideReference {}
unsafe impl Sync for WeakBattleSideReference {} unsafe impl Sync for WeakBattleSideReference {}
impl PartialEq for WeakBattleSideReference { impl PartialEq for WeakBattleSideReference {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool { self.data.ptr_eq(&other.data) }
self.data.ptr_eq(&other.data)
}
} }
impl VolatileScriptsOwner for BattleSide { impl VolatileScriptsOwner for BattleSide {
fn volatile_scripts(&self) -> &Arc<ScriptSet> { fn volatile_scripts(&self) -> &Arc<ScriptSet> { &self.data.volatile_scripts }
&self.data.volatile_scripts
}
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> { fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
self.battle()? self.battle()?
@ -411,17 +378,11 @@ impl VolatileScriptsOwner for BattleSide {
} }
impl ScriptSource for BattleSide { impl ScriptSource for BattleSide {
fn get_script_count(&self) -> Result<usize> { fn get_script_count(&self) -> Result<usize> { Ok(self.battle()?.get_script_count()? + 1) }
Ok(self.battle()?.get_script_count()? + 1)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { &self.data.script_source_data }
&self.data.script_source_data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) { fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) { scripts.push((&self.data.volatile_scripts).into()); }
scripts.push((&self.data.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> { fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.get_own_scripts(scripts); self.get_own_scripts(scripts);

View File

@ -941,6 +941,7 @@ impl WeakPokemonReference {
} }
/// Gets the pointer to the underlying data. /// Gets the pointer to the underlying data.
#[cfg(feature = "wasm")]
pub(crate) fn as_ptr(&self) -> *const c_void { self.data.as_ptr() as *const c_void } pub(crate) fn as_ptr(&self) -> *const c_void { self.data.as_ptr() as *const c_void }
} }

View File

@ -169,15 +169,11 @@ pub enum ScriptWrapper {
} }
impl From<&ScriptContainer> for ScriptWrapper { impl From<&ScriptContainer> for ScriptWrapper {
fn from(c: &ScriptContainer) -> Self { fn from(c: &ScriptContainer) -> Self { ScriptWrapper::Script(Arc::downgrade(c.arc())) }
ScriptWrapper::Script(Arc::downgrade(c.arc()))
}
} }
impl From<&Arc<ScriptSet>> for ScriptWrapper { impl From<&Arc<ScriptSet>> for ScriptWrapper {
fn from(c: &Arc<ScriptSet>) -> Self { fn from(c: &Arc<ScriptSet>) -> Self { ScriptWrapper::Set(Arc::downgrade(c)) }
ScriptWrapper::Set(Arc::downgrade(c))
}
} }
/// This struct allows for the iteration over scripts. /// This struct allows for the iteration over scripts.
@ -291,7 +287,6 @@ mod tests {
use std::any::Any; use std::any::Any;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use crate::dynamic_data::script_handling::script::ScriptContainer;
use crate::StringKey; use crate::StringKey;
use super::*; use super::*;
@ -323,17 +318,11 @@ mod tests {
} }
impl Script for TestScript { impl Script for TestScript {
fn name(&self) -> Result<&StringKey> { fn name(&self) -> Result<&StringKey> { Ok(&self.name) }
Ok(&self.name)
}
fn get_marked_for_deletion(&self) -> &AtomicBool { fn get_marked_for_deletion(&self) -> &AtomicBool { &self.is_marked_for_deletion }
&self.is_marked_for_deletion
}
fn get_suppressed_count(&self) -> &AtomicUsize { fn get_suppressed_count(&self) -> &AtomicUsize { &self.suppressed_count }
&self.suppressed_count
}
fn add_suppression(&self) {} fn add_suppression(&self) {}
@ -344,13 +333,9 @@ mod tests {
Ok(()) Ok(())
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any { self }
self
}
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn Any { self }
self
}
} }
#[test] #[test]
@ -551,17 +536,11 @@ mod tests {
} }
impl ScriptSource for TestScriptSource { impl ScriptSource for TestScriptSource {
fn get_script_count(&self) -> Result<usize> { fn get_script_count(&self) -> Result<usize> { Ok(1) }
Ok(1)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> { &self.data }
&self.data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) { fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) { scripts.push((&self.script).into()); }
scripts.push((&self.script).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> { fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.get_own_scripts(scripts); self.get_own_scripts(scripts);

View File

@ -1,4 +1,4 @@
use crate::dynamic_data::{DynamicLibrary, ExecutingMove, Pokemon, Script, ScriptOwnerData, TurnChoice}; use crate::dynamic_data::{DynamicLibrary, ExecutingMove, Pokemon, Script, TurnChoice};
use crate::script_implementations::rune::wrappers::*; use crate::script_implementations::rune::wrappers::*;
use crate::script_implementations::rune::RuneScriptType; use crate::script_implementations::rune::RuneScriptType;
use crate::static_data::Parameter; use crate::static_data::Parameter;
@ -22,8 +22,6 @@ pub struct RuneScript {
/// A script can be suppressed by other scripts. If a script is suppressed by at least one script /// A script can be suppressed by other scripts. If a script is suppressed by at least one script
/// we will not execute its methods. This holds the number of suppressions on the script. /// we will not execute its methods. This holds the number of suppressions on the script.
suppressed_count: AtomicUsize, suppressed_count: AtomicUsize,
/// The owner of this script (where the script is attached to)
owner: ScriptOwnerData,
script_type: Arc<RuneScriptType>, script_type: Arc<RuneScriptType>,
@ -39,7 +37,6 @@ impl RuneScript {
pub fn new( pub fn new(
name: StringKey, name: StringKey,
object: Shared<Object>, object: Shared<Object>,
owner: ScriptOwnerData,
script_type: Arc<RuneScriptType>, script_type: Arc<RuneScriptType>,
runtime: Arc<RuntimeContext>, runtime: Arc<RuntimeContext>,
unit: Arc<Unit>, unit: Arc<Unit>,
@ -49,7 +46,6 @@ impl RuneScript {
state: RwLock::new(object), state: RwLock::new(object),
marked_for_deletion: Default::default(), marked_for_deletion: Default::default(),
suppressed_count: Default::default(), suppressed_count: Default::default(),
owner,
script_type, script_type,
runtime, runtime,
unit, unit,
@ -82,39 +78,10 @@ impl Script for RuneScript {
Ok(()) Ok(())
} }
fn block_critical(
&self,
move_data: &Arc<ExecutingMove>,
target: &Pokemon,
hit: u8,
block_critical: &mut bool,
) -> anyhow::Result<()> {
if let Some(hash) = self.script_type.fn_block_critical {
let mut vm = rune::runtime::Vm::new(self.runtime.clone(), self.unit.clone());
todo!()
// let block_critical_handle = RuneValueWrapper::new_mut(block_critical);
// let read_lock = self.state.read();
// let state = read_lock.deref();
//
// vm.execute(
// hash,
// vec![
// Value::Object(state.clone()),
// Value::from(move_data.wrap()?),
// Value::from(target.wrap()?),
// Value::from(hit),
// Value::from(block_critical_handle.clone().wrap()?),
// ],
// )?;
// *block_critical = block_critical_handle.value();
}
Ok(())
}
fn change_speed(&self, choice: &Arc<TurnChoice>, speed: &mut u32) -> anyhow::Result<()> { fn change_speed(&self, choice: &Arc<TurnChoice>, speed: &mut u32) -> anyhow::Result<()> {
if let Some(hash) = self.script_type.fn_change_speed { if let Some(hash) = self.script_type.fn_change_speed {
let mut vm = rune::runtime::Vm::new(self.runtime.clone(), self.unit.clone()); let mut vm = rune::runtime::Vm::new(self.runtime.clone(), self.unit.clone());
let speed_handle = wrap_value_reference(*speed as i64)?; let speed_handle = wrap_int_reference(*speed as i64)?;
let read_lock = self.state.read(); let read_lock = self.state.read();
let state = read_lock.deref(); let state = read_lock.deref();
@ -132,7 +99,35 @@ impl Script for RuneScript {
return Err(anyhow::anyhow!("Error executing script: {}", e)); return Err(anyhow::anyhow!("Error executing script: {}", e));
} }
*speed = get_value_reference(speed_handle)? as u32; *speed = get_int_reference_value(speed_handle)? as u32;
}
Ok(())
}
fn block_critical(
&self,
move_data: &Arc<ExecutingMove>,
target: &Pokemon,
hit: u8,
block_critical: &mut bool,
) -> anyhow::Result<()> {
if let Some(hash) = self.script_type.fn_block_critical {
let mut vm = rune::runtime::Vm::new(self.runtime.clone(), self.unit.clone());
let block_critical_handle = wrap_bool_reference(*block_critical)?;
let read_lock = self.state.read();
let state = read_lock.deref();
vm.execute(
hash,
vec![
Value::Object(state.clone()),
Value::from(move_data.wrap()),
Value::from(target.wrap()),
Value::from(hit),
block_critical_handle.clone(),
],
)?;
*block_critical = get_bool_reference_value(block_critical_handle)?;
} }
Ok(()) Ok(())
} }

View File

@ -1,5 +1,6 @@
use crate::dynamic_data::{ItemScript, Script, ScriptCategory, ScriptOwnerData, ScriptResolver}; use crate::dynamic_data::{ItemScript, Script, ScriptCategory, ScriptOwnerData, ScriptResolver};
use crate::script_implementations::rune::script::RuneScript; use crate::script_implementations::rune::script::RuneScript;
use crate::script_implementations::rune::wrappers::RuneWrapper;
use crate::script_implementations::rune::RuneScriptType; use crate::script_implementations::rune::RuneScriptType;
use crate::static_data::Item; use crate::static_data::Item;
use crate::StringKey; use crate::StringKey;
@ -10,6 +11,7 @@ use rune::diagnostics::Diagnostic;
use rune::runtime::{RuntimeContext, Shared}; use rune::runtime::{RuntimeContext, Shared};
use rune::{Context, Diagnostics, Options, Source, Sources, Unit}; use rune::{Context, Diagnostics, Options, Source, Sources, Unit};
use std::any::Any; use std::any::Any;
use std::convert::TryFrom;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
@ -38,11 +40,22 @@ impl ScriptResolver for RuneScriptResolver {
return Ok(None); return Ok(None);
}; };
let state = Shared::new(rune::runtime::Object::new())?; let mut o = rune::runtime::Object::new();
let owner_obj = match owner {
ScriptOwnerData::Pokemon(p) => Some(p.upgrade().unwrap().wrap()),
ScriptOwnerData::BattleSide(_) => None, //FIXME
ScriptOwnerData::Battle(_) => None, //FIXME
ScriptOwnerData::None => None,
};
if let Some(owner_obj) = owner_obj {
o.insert(rune::alloc::String::try_from("owner")?, owner_obj.into())?;
}
let state = Shared::new(o)?;
let script = Arc::new(RuneScript::new( let script = Arc::new(RuneScript::new(
script_key.clone(), script_key.clone(),
state, state,
owner,
script_type.clone(), script_type.clone(),
self.runtime.clone(), self.runtime.clone(),
self.unit.clone(), self.unit.clone(),

View File

@ -6,6 +6,10 @@ use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> { pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneExecutingMove>()?; module.ty::<RuneExecutingMove>()?;
module.function_meta(RuneExecutingMove::target_count)?;
module.function_meta(RuneExecutingMove::number_of_hits)?;
module.function_meta(RuneExecutingMove::user)?;
Ok(()) Ok(())
} }
@ -14,11 +18,11 @@ pub struct RuneExecutingMove(Arc<ExecutingMove>);
impl RuneExecutingMove { impl RuneExecutingMove {
#[rune::function] #[rune::function]
pub fn target_count(&self) -> usize { self.0.target_count() } fn target_count(&self) -> usize { self.0.target_count() }
#[rune::function] #[rune::function]
pub fn number_of_hits(&self) -> u8 { self.0.number_of_hits() } fn number_of_hits(&self) -> u8 { self.0.number_of_hits() }
#[rune::function] #[rune::function]
pub fn user(&self) -> Shared<AnyObj> { self.0.user().wrap() } fn user(&self) -> Shared<AnyObj> { self.0.user().wrap() }
} }
impl_rune_wrapper!(&Arc<ExecutingMove>, RuneExecutingMove); impl_rune_wrapper!(&Arc<ExecutingMove>, RuneExecutingMove);

View File

@ -1,7 +1,9 @@
use rune::runtime::{AnyObj, Shared}; use rune::runtime::{AnyObj, Protocol, Shared, VmResult};
use rune::{Any, Value}; use rune::{Any, Value};
use std::num::Saturating;
mod dynamic_data; mod dynamic_data;
mod parameters;
mod static_data; mod static_data;
pub trait RuneWrapper { pub trait RuneWrapper {
@ -11,40 +13,57 @@ pub trait RuneWrapper {
pub fn module() -> anyhow::Result<rune::Module> { pub fn module() -> anyhow::Result<rune::Module> {
let mut module = rune::Module::new(); let mut module = rune::Module::new();
module.ty::<RuneValueIntWrapper>()?; module.ty::<RuneValueIntWrapper>()?;
module.function_meta(RuneValueIntWrapper::rn_as_int)?;
module.function_meta(RuneValueIntWrapper::set_value)?;
module.associated_function(Protocol::ADD_ASSIGN, RuneValueIntWrapper::add_assign)?;
module.associated_function(Protocol::SUB_ASSIGN, RuneValueIntWrapper::sub_assign)?;
module.associated_function(Protocol::DIV_ASSIGN, RuneValueIntWrapper::div_assign)?;
module.associated_function(Protocol::MUL_ASSIGN, RuneValueIntWrapper::mul_assign)?;
module.associated_function(Protocol::PARTIAL_EQ, RuneValueIntWrapper::partial_eq)?;
module.associated_function(Protocol::EQ, RuneValueIntWrapper::eq)?;
module.associated_function(Protocol::STRING_DISPLAY, RuneValueIntWrapper::string_display)?;
module.ty::<RuneValueBoolWrapper>()?;
module.function_meta(RuneValueBoolWrapper::rn_as_bool)?;
module.function_meta(RuneValueBoolWrapper::set_value)?;
module.associated_function(Protocol::EQ, RuneValueBoolWrapper::eq)?;
module.associated_function(Protocol::STRING_DISPLAY, RuneValueBoolWrapper::string_display)?;
module.ty::<RuneStringKey>()?;
module.associated_function(Protocol::STRING_DISPLAY, RuneStringKey::string_display)?;
parameters::register(&mut module)?;
dynamic_data::register(&mut module)?; dynamic_data::register(&mut module)?;
static_data::register(&mut module)?; static_data::register(&mut module)?;
Ok(module) Ok(module)
} }
pub fn wrap_value_reference(value: i64) -> anyhow::Result<Value> { pub fn wrap_int_reference(value: i64) -> anyhow::Result<Value> {
Ok(Value::Any(Shared::new(AnyObj::new(RuneValueIntWrapper::new(value))?)?)) Ok(Value::Any(Shared::new(AnyObj::new(RuneValueIntWrapper::new(value))?)?))
} }
pub fn get_value_reference(value: Value) -> anyhow::Result<i64> { pub fn wrap_bool_reference(value: bool) -> anyhow::Result<Value> {
Ok(Value::Any(Shared::new(AnyObj::new(RuneValueBoolWrapper::new(value))?)?))
}
pub fn get_int_reference_value(value: Value) -> anyhow::Result<i64> {
let obj = value.into_any().into_result()?; let obj = value.into_any().into_result()?;
let obj = obj.take()?; let obj = obj.take()?;
let obj = match obj.downcast_borrow_ref::<RuneValueIntWrapper>() { let obj = match obj.downcast_borrow_ref::<RuneValueIntWrapper>() {
Some(obj) => obj, Some(obj) => obj,
None => return Err(anyhow::anyhow!("Value is not a RuneValueIntWrapper")), None => return Err(anyhow::anyhow!("Value is not a RuneValueIntWrapper")),
}; };
Ok(obj.value()) Ok(obj.as_int())
} }
#[derive(Any, Clone)] pub fn get_bool_reference_value(value: Value) -> anyhow::Result<bool> {
struct RuneValueIntWrapper { let obj = value.into_any().into_result()?;
#[rune(get, set)] let obj = obj.take()?;
value: i64, let obj = match obj.downcast_borrow_ref::<RuneValueBoolWrapper>() {
} Some(obj) => obj,
None => return Err(anyhow::anyhow!("Value is not a RuneValueBoolWrapper")),
impl RuneValueIntWrapper { };
pub fn new(value: i64) -> Self { Self { value } } Ok(obj.as_bool())
pub fn value(&self) -> i64 { self.value }
pub fn set_value(&mut self, value: i64) { self.value = value; }
}
impl RuneWrapper for RuneValueIntWrapper {
fn wrap(self) -> Shared<AnyObj> { Shared::new(AnyObj::new(self).unwrap()).unwrap() }
} }
macro_rules! impl_rune_wrapper { macro_rules! impl_rune_wrapper {
@ -57,4 +76,76 @@ macro_rules! impl_rune_wrapper {
}; };
} }
use crate::StringKey;
use impl_rune_wrapper; use impl_rune_wrapper;
#[derive(Any, Clone)]
struct RuneValueIntWrapper {
value: Saturating<i64>,
}
impl RuneValueIntWrapper {
pub fn new(value: i64) -> Self {
Self {
value: Saturating(value),
}
}
pub fn as_int(&self) -> i64 { self.value.0 }
#[rune::function(path = RuneValueIntWrapper::as_int)]
fn rn_as_int(&self) -> i64 { self.value.0 }
#[rune::function]
fn set_value(&mut self, value: i64) { self.value.0 = value; }
fn add_assign(&mut self, other: i64) { self.value += other; }
fn sub_assign(&mut self, other: i64) { self.value -= other; }
fn div_assign(&mut self, other: i64) { self.value /= other; }
fn mul_assign(&mut self, other: i64) { self.value *= other; }
fn partial_eq(&self, b: i64) -> VmResult<bool> { VmResult::Ok(self.value.0 == b) }
fn eq(&self, b: i64) -> VmResult<bool> { VmResult::Ok(self.value.0 == b) }
fn string_display(&self) -> VmResult<String> { VmResult::Ok(format!("{}", self.value.0)) }
}
#[derive(Any, Clone)]
struct RuneValueBoolWrapper {
value: bool,
}
impl RuneValueBoolWrapper {
pub fn new(value: bool) -> Self { Self { value } }
pub fn as_bool(&self) -> bool { self.value }
#[rune::function(path = RuneValueIntWrapper::as_bool)]
fn rn_as_bool(&self) -> bool { self.value }
#[rune::function]
fn set_value(&mut self, value: bool) { self.value = value; }
fn string_display(&self) -> VmResult<String> { VmResult::Ok(format!("{}", self.value)) }
fn eq(&self, b: bool) -> VmResult<bool> { VmResult::Ok(self.value == b) }
}
#[derive(Any, Clone, Debug)]
pub(super) struct RuneStringKey(StringKey);
impl RuneStringKey {
pub fn new(value: StringKey) -> Self { Self(value) }
fn string_display(&self) -> VmResult<String> { VmResult::Ok(format!("{}", self.0)) }
}
impl RuneWrapper for &StringKey {
fn wrap(self) -> Shared<AnyObj> {
Shared::new(AnyObj::new(RuneStringKey(self.clone())).unwrap()).unwrap()
}
}

View File

@ -0,0 +1,31 @@
use crate::script_implementations::rune::wrappers::{RuneStringKey, RuneWrapper};
use crate::static_data::Parameter;
use rune::runtime::{AnyObj, Shared};
use rune::Any;
use std::ops::Deref;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneParameter>()?;
Ok(())
}
#[derive(Debug, Any)]
pub(super) enum RuneParameter {
Int(#[rune(get)] i32),
Float(#[rune(get)] f32),
String(#[rune(get)] RuneStringKey),
Bool(#[rune(get)] bool),
}
impl RuneWrapper for &Arc<Parameter> {
fn wrap(self) -> Shared<AnyObj> {
let p = match self.deref() {
Parameter::Bool(b) => RuneParameter::Bool(*b),
Parameter::Int(i) => RuneParameter::Int(*i as i32),
Parameter::Float(f) => RuneParameter::Float(*f),
Parameter::String(s) => RuneParameter::String(RuneStringKey::new(s.clone())),
};
Shared::new(AnyObj::new(p).unwrap()).unwrap()
}
}

View File

@ -0,0 +1,34 @@
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::Ability;
use rune::runtime::{AnyObj, Shared};
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneAbility>()?;
module.function_meta(RuneAbility::name)?;
module.function_meta(RuneAbility::effect)?;
module.function_meta(RuneAbility::get_parameter)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneAbility(Arc<dyn Ability>);
impl_rune_wrapper!(&Arc<dyn Ability>, RuneAbility);
impl RuneAbility {
#[rune::function]
fn name(&self) -> Shared<AnyObj> { self.0.name().wrap() }
#[rune::function]
fn effect(&self) -> Shared<AnyObj> { self.0.effect().wrap() }
#[rune::function(vm_result)]
fn get_parameter(&self, key: RuneStringKey) -> Option<Shared<AnyObj>> {
match self.0.parameters().get(&key.0) {
None => None,
Some(p) => Some(p.wrap()),
}
}
}

View File

@ -0,0 +1,67 @@
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::{AbilityIndex, Form, Statistic};
use rune::runtime::{AnyObj, Shared};
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneForm>()?;
module.function_meta(RuneForm::name)?;
module.function_meta(RuneForm::height)?;
module.function_meta(RuneForm::weight)?;
module.function_meta(RuneForm::base_experience)?;
module.function_meta(RuneForm::types)?;
module.function_meta(RuneForm::base_stats)?;
module.function_meta(RuneForm::abilities)?;
module.function_meta(RuneForm::hidden_abilities)?;
module.function_meta(RuneForm::has_flag)?;
module.function_meta(RuneForm::get_type)?;
module.function_meta(RuneForm::get_base_stat)?;
module.function_meta(RuneForm::get_ability)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneForm(Arc<dyn Form>);
impl_rune_wrapper!(&Arc<dyn Form>, RuneForm);
impl RuneForm {
#[rune::function]
fn name(&self) -> Shared<AnyObj> { self.0.name().wrap() }
#[rune::function]
fn height(&self) -> f32 { self.0.height() }
#[rune::function]
fn weight(&self) -> f32 { self.0.weight() }
#[rune::function]
fn base_experience(&self) -> u32 { self.0.base_experience() }
#[rune::function]
fn types(&self) -> Vec<u8> { self.0.types().iter().map(|t| u8::from(*t)).collect() }
#[rune::function]
fn base_stats(&self) -> Shared<AnyObj> { self.0.base_stats().wrap() }
#[rune::function]
fn abilities(&self) -> Vec<Shared<AnyObj>> { self.0.abilities().iter().map(|a| a.wrap()).collect() }
#[rune::function]
fn hidden_abilities(&self) -> Vec<Shared<AnyObj>> { self.0.hidden_abilities().iter().map(|a| a.wrap()).collect() }
#[rune::function]
fn has_flag(&self, key: &RuneStringKey) -> bool { self.0.has_flag(&key.0) }
#[rune::function]
fn get_type(&self, index: usize) -> anyhow::Result<u8> { self.0.get_type(index).map(|t| u8::from(t)) }
#[rune::function]
fn get_base_stat(&self, statistic: Statistic) -> u16 { self.0.get_base_stat(statistic) }
#[rune::function]
fn get_ability(&self, hidden: bool, index: u8) -> anyhow::Result<Shared<AnyObj>> {
self.0.get_ability(AbilityIndex { hidden, index }).map(|a| a.wrap())
}
}

View File

@ -0,0 +1,27 @@
use crate::defines::LevelInt;
use crate::script_implementations::rune::wrappers::impl_rune_wrapper;
use crate::static_data::GrowthRate;
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneGrowthRate>()?;
module.function_meta(RuneGrowthRate::calculate_level)?;
module.function_meta(RuneGrowthRate::calculate_experience)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneGrowthRate(Arc<dyn GrowthRate>);
impl_rune_wrapper!(&Arc<dyn GrowthRate>, RuneGrowthRate);
impl RuneGrowthRate {
#[rune::function]
fn calculate_level(&self, experience: u32) -> i32 { self.0.calculate_level(experience) as i32 }
#[rune::function]
fn calculate_experience(&self, level: LevelInt) -> Result<u32, String> {
self.0.calculate_experience(level).map_err(|e| e.to_string())
}
}

View File

@ -0,0 +1,47 @@
use std::sync::Arc;
use rune::runtime::{AnyObj, Shared};
use rune::Any;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::{BattleItemCategory, Item, ItemCategory};
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<ItemCategory>()?;
module.ty::<BattleItemCategory>()?;
module.ty::<RuneItem>()?;
module.function_meta(RuneItem::name)?;
module.function_meta(RuneItem::category)?;
module.function_meta(RuneItem::battle_category)?;
module.function_meta(RuneItem::price)?;
module.function_meta(RuneItem::flags)?;
module.function_meta(RuneItem::has_flag)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneItem(Arc<dyn Item>);
impl_rune_wrapper!(&Arc<dyn Item>, RuneItem);
impl RuneItem {
#[rune::function]
fn name(&self) -> Shared<AnyObj> { self.0.name().clone().wrap() }
#[rune::function]
fn category(&self) -> ItemCategory { self.0.category() }
#[rune::function]
fn battle_category(&self) -> BattleItemCategory { self.0.battle_category() }
#[rune::function]
fn price(&self) -> i32 { self.0.price() }
#[rune::function]
fn flags(&self) -> Vec<Shared<AnyObj>> { self.0.flags().iter().map(|s| s.clone().wrap()).collect() }
#[rune::function]
fn has_flag(&self, key: &RuneStringKey) -> bool { self.0.has_flag(&key.0) }
}

View File

@ -0,0 +1,40 @@
use crate::defines::LevelInt;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::LearnableMoves;
use rune::runtime::{AnyObj, Shared};
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneLearnableMoves>()?;
module.function_meta(RuneLearnableMoves::get_learned_by_level)?;
module.function_meta(RuneLearnableMoves::get_distinct_level_moves)?;
module.function_meta(RuneLearnableMoves::learns_move)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneLearnableMoves(Arc<dyn LearnableMoves>);
impl_rune_wrapper!(&Arc<dyn LearnableMoves>, RuneLearnableMoves);
impl RuneLearnableMoves {
#[rune::function]
fn get_learned_by_level(&self, level: LevelInt) -> Option<Vec<Shared<AnyObj>>> {
self.0
.get_learned_by_level(level)
.map(|v| v.into_iter().map(|s| s.wrap()).collect())
}
#[rune::function]
fn get_distinct_level_moves(&self) -> Vec<Shared<AnyObj>> {
self.0
.get_distinct_level_moves()
.into_iter()
.map(|s| s.wrap())
.collect()
}
#[rune::function]
fn learns_move(&self, moveName: &RuneStringKey) -> bool { self.0.get_distinct_level_moves().contains(&moveName.0) }
}

View File

@ -0,0 +1,29 @@
use crate::defines::LevelInt;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey};
use crate::static_data::GrowthRateLibrary;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneGrowthRateLibrary>()?;
module.function_meta(RuneGrowthRateLibrary::calculate_level)?;
module.function_meta(RuneGrowthRateLibrary::calculate_experience)?;
Ok(())
}
#[derive(Debug, rune::Any)]
struct RuneGrowthRateLibrary(Arc<dyn GrowthRateLibrary>);
impl_rune_wrapper!(&Arc<dyn GrowthRateLibrary>, RuneGrowthRateLibrary);
impl RuneGrowthRateLibrary {
#[rune::function]
fn calculate_level(&self, growth_rate: &RuneStringKey, experience: u32) -> anyhow::Result<LevelInt> {
self.0.calculate_level(&growth_rate.0, experience)
}
#[rune::function]
fn calculate_experience(&self, growth_rate: &RuneStringKey, level: LevelInt) -> anyhow::Result<u32> {
self.0.calculate_experience(&growth_rate.0, level)
}
}

View File

@ -0,0 +1,23 @@
use crate::script_implementations::rune::wrappers::impl_rune_wrapper;
use crate::static_data::LibrarySettings;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneLibrarySettings>()?;
module.function_meta(RuneLibrarySettings::maximum_level)?;
module.function_meta(RuneLibrarySettings::shiny_rate)?;
Ok(())
}
#[derive(Debug, rune::Any)]
struct RuneLibrarySettings(Arc<dyn LibrarySettings>);
impl_rune_wrapper!(&Arc<dyn LibrarySettings>, RuneLibrarySettings);
impl RuneLibrarySettings {
#[rune::function]
fn maximum_level(&self) -> i64 { self.0.maximum_level() as i64 }
#[rune::function]
fn shiny_rate(&self) -> i64 { self.0.shiny_rate() as i64 }
}

View File

@ -0,0 +1,68 @@
mod growth_rate_library;
mod library_settings;
mod nature_library;
mod static_data;
mod type_library;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::{AbilityLibrary, ItemLibrary, MoveLibrary, SpeciesLibrary};
use rune::runtime::{AnyObj, Shared};
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneAbilityLibrary>()?;
module.function_meta(RuneAbilityLibrary::get)?;
module.function_meta(RuneAbilityLibrary::len)?;
module.function_meta(RuneAbilityLibrary::get_key_by_index)?;
module.ty::<RuneItemLibrary>()?;
module.function_meta(RuneItemLibrary::get)?;
module.function_meta(RuneItemLibrary::len)?;
module.function_meta(RuneItemLibrary::get_key_by_index)?;
module.ty::<RuneMoveLibrary>()?;
module.function_meta(RuneMoveLibrary::get)?;
module.function_meta(RuneMoveLibrary::len)?;
module.function_meta(RuneMoveLibrary::get_key_by_index)?;
module.ty::<RuneSpeciesLibrary>()?;
module.function_meta(RuneSpeciesLibrary::get)?;
module.function_meta(RuneSpeciesLibrary::len)?;
module.function_meta(RuneSpeciesLibrary::get_key_by_index)?;
growth_rate_library::register(module)?;
library_settings::register(module)?;
nature_library::register(module)?;
type_library::register(module)?;
static_data::register(module)?;
Ok(())
}
macro_rules! impl_rune_data_library_wrapper {
($t:ident, $wrapped_type:ty) => {
#[derive(Debug, rune::Any)]
struct $t($wrapped_type);
impl_rune_wrapper!(&$wrapped_type, $t);
impl $t {
#[rune::function]
fn get(&self, key: &RuneStringKey) -> Option<Shared<AnyObj>> { self.0.get(&key.0).map(|v| v.wrap()) }
#[rune::function]
fn len(&self) -> usize { self.0.len() }
#[rune::function]
fn get_key_by_index(&self, index: usize) -> Option<Shared<AnyObj>> {
self.0.get_key_by_index(index).map(|v| v.wrap())
}
}
};
}
impl_rune_data_library_wrapper!(RuneAbilityLibrary, Arc<dyn AbilityLibrary>);
impl_rune_data_library_wrapper!(RuneItemLibrary, Arc<dyn ItemLibrary>);
impl_rune_data_library_wrapper!(RuneMoveLibrary, Arc<dyn MoveLibrary>);
impl_rune_data_library_wrapper!(RuneSpeciesLibrary, Arc<dyn SpeciesLibrary>);

View File

@ -0,0 +1,27 @@
use crate::script_implementations::rune::wrappers::static_data::nature::RuneNature;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::NatureLibrary;
use rune::runtime::{AnyObj, Shared};
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneNatureLibrary>()?;
module.function_meta(RuneNatureLibrary::get_nature)?;
module.function_meta(RuneNatureLibrary::get_nature_name)?;
Ok(())
}
#[derive(Debug, rune::Any)]
struct RuneNatureLibrary(Arc<dyn NatureLibrary>);
impl_rune_wrapper!(&Arc<dyn NatureLibrary>, RuneNatureLibrary);
impl RuneNatureLibrary {
#[rune::function]
fn get_nature(&self, key: RuneStringKey) -> Option<Shared<AnyObj>> { self.0.get_nature(&key.0).map(|v| v.wrap()) }
#[rune::function]
fn get_nature_name(&self, nature: &RuneNature) -> RuneStringKey {
RuneStringKey(self.0.get_nature_name(&nature.0).unwrap())
}
}

View File

@ -0,0 +1,48 @@
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneWrapper};
use crate::static_data::StaticData;
use rune::runtime::{AnyObj, Shared};
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneStaticData>()?;
module.function_meta(RuneStaticData::settings)?;
module.function_meta(RuneStaticData::species)?;
module.function_meta(RuneStaticData::moves)?;
module.function_meta(RuneStaticData::items)?;
module.function_meta(RuneStaticData::growth_rates)?;
module.function_meta(RuneStaticData::types)?;
module.function_meta(RuneStaticData::natures)?;
module.function_meta(RuneStaticData::abilities)?;
Ok(())
}
#[derive(Debug, rune::Any)]
struct RuneStaticData(Arc<dyn StaticData>);
impl_rune_wrapper!(&Arc<dyn StaticData>, RuneStaticData);
impl RuneStaticData {
#[rune::function]
fn settings(&self) -> Shared<AnyObj> { self.0.settings().wrap() }
#[rune::function]
fn species(&self) -> Shared<AnyObj> { self.0.species().wrap() }
#[rune::function]
fn moves(&self) -> Shared<AnyObj> { self.0.moves().wrap() }
#[rune::function]
fn items(&self) -> Shared<AnyObj> { self.0.items().wrap() }
#[rune::function]
fn growth_rates(&self) -> Shared<AnyObj> { self.0.growth_rates().wrap() }
#[rune::function]
fn types(&self) -> Shared<AnyObj> { self.0.types().wrap() }
#[rune::function]
fn natures(&self) -> Shared<AnyObj> { self.0.natures().wrap() }
#[rune::function]
fn abilities(&self) -> Shared<AnyObj> { self.0.abilities().wrap() }
}

View File

@ -0,0 +1,39 @@
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::TypeLibrary;
use rune::runtime::{AnyObj, Shared};
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneTypeLibrary>()?;
module.function_meta(RuneTypeLibrary::get_type_id)?;
module.function_meta(RuneTypeLibrary::get_type_name)?;
module.function_meta(RuneTypeLibrary::get_single_effectiveness)?;
module.function_meta(RuneTypeLibrary::get_effectiveness)?;
Ok(())
}
#[derive(Debug, rune::Any)]
struct RuneTypeLibrary(Arc<dyn TypeLibrary>);
impl_rune_wrapper!(&Arc<dyn TypeLibrary>, RuneTypeLibrary);
impl RuneTypeLibrary {
#[rune::function]
fn get_type_id(&self, key: &RuneStringKey) -> Option<u8> { self.0.get_type_id(&key.0).map(|v| v.into()) }
#[rune::function]
fn get_type_name(&self, t: u8) -> Option<Shared<AnyObj>> { self.0.get_type_name(t.into()).map(|v| v.wrap()) }
#[rune::function]
fn get_single_effectiveness(&self, attacking: u8, defending: u8) -> anyhow::Result<f32> {
self.0.get_single_effectiveness(attacking.into(), defending.into())
}
#[rune::function]
fn get_effectiveness(&self, attacking: u8, defending: &[u8]) -> anyhow::Result<f32> {
self.0.get_effectiveness(
attacking.into(),
&defending.iter().map(|v| (*v).into()).collect::<Vec<_>>(),
)
}
}

View File

@ -1,12 +1,30 @@
mod ability;
mod form;
mod growth_rate;
mod item;
mod learnable_moves;
mod libraries;
mod move_data;
mod nature; mod nature;
mod species;
mod statistic_set; mod statistic_set;
use crate::static_data::{Statistic, TimeOfDay}; use crate::static_data::{Gender, Statistic, TimeOfDay};
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> { pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<TimeOfDay>()?; module.ty::<TimeOfDay>()?;
module.ty::<Statistic>()?; module.ty::<Statistic>()?;
module.ty::<Gender>()?;
statistic_set::register(module)?; statistic_set::register(module)?;
nature::register(module)?; nature::register(module)?;
item::register(module)?;
growth_rate::register(module)?;
form::register(module)?;
ability::register(module)?;
learnable_moves::register(module)?;
species::register(module)?;
move_data::register(module)?;
libraries::register(module)?;
Ok(()) Ok(())
} }

View File

@ -0,0 +1,89 @@
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::{MoveCategory, MoveData, MoveTarget, SecondaryEffect};
use rune::runtime::{AnyObj, Object, Shared};
use rune::{Any, Value};
use std::convert::TryFrom;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<MoveCategory>()?;
module.ty::<MoveTarget>()?;
module.ty::<RuneMoveData>()?;
module.function_meta(RuneMoveData::name)?;
module.function_meta(RuneMoveData::move_type)?;
module.function_meta(RuneMoveData::category)?;
module.function_meta(RuneMoveData::base_power)?;
module.function_meta(RuneMoveData::accuracy)?;
module.function_meta(RuneMoveData::base_usages)?;
module.function_meta(RuneMoveData::target)?;
module.function_meta(RuneMoveData::priority)?;
module.function_meta(RuneMoveData::secondary_effect)?;
module.function_meta(RuneMoveData::has_flag)?;
module.ty::<RuneSecondaryEffect>()?;
module.function_meta(RuneSecondaryEffect::chance)?;
module.function_meta(RuneSecondaryEffect::effect_name)?;
module.function_meta(RuneSecondaryEffect::parameters)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneMoveData(Arc<dyn MoveData>);
impl_rune_wrapper!(&Arc<dyn MoveData>, RuneMoveData);
impl RuneMoveData {
#[rune::function]
fn name(&self) -> Shared<AnyObj> { self.0.name().wrap() }
#[rune::function]
fn move_type(&self) -> u8 { u8::from(self.0.move_type()) }
#[rune::function]
fn category(&self) -> MoveCategory { self.0.category() }
#[rune::function]
fn base_power(&self) -> u8 { self.0.base_power() }
#[rune::function]
fn accuracy(&self) -> u8 { self.0.accuracy() }
#[rune::function]
fn base_usages(&self) -> u8 { self.0.base_usages() }
#[rune::function]
fn target(&self) -> MoveTarget { self.0.target() }
#[rune::function]
fn priority(&self) -> i8 { self.0.priority() }
#[rune::function]
fn secondary_effect(&self) -> Option<Shared<AnyObj>> { self.0.secondary_effect().as_ref().map(|x| x.wrap()) }
#[rune::function]
fn has_flag(&self, flag: RuneStringKey) -> bool { self.0.has_flag(&flag.0) }
}
#[derive(Debug, Any)]
pub struct RuneSecondaryEffect(Arc<dyn SecondaryEffect>);
impl_rune_wrapper!(&Arc<dyn SecondaryEffect>, RuneSecondaryEffect);
impl RuneSecondaryEffect {
#[rune::function]
fn chance(&self) -> f32 { self.0.chance() }
#[rune::function]
fn effect_name(&self) -> Shared<AnyObj> { self.0.effect_name().wrap() }
#[rune::function]
fn parameters(&self) -> anyhow::Result<Object> {
let pars = self.0.parameters();
let mut o = Object::with_capacity(pars.len())?;
for (key, value) in pars.iter() {
o.insert(rune::alloc::String::try_from(key.str())?, Value::from(value.wrap()))?;
}
Ok(o)
}
}

View File

@ -5,27 +5,32 @@ use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> { pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneNature>()?; module.ty::<RuneNature>()?;
module.function_meta(RuneNature::increased_stat)?;
module.function_meta(RuneNature::decreased_stat)?;
module.function_meta(RuneNature::increased_modifier)?;
module.function_meta(RuneNature::decreased_modifier)?;
module.function_meta(RuneNature::get_stat_modifier)?;
Ok(()) Ok(())
} }
#[derive(Debug, Any)] #[derive(Debug, Any)]
pub struct RuneNature(Arc<dyn Nature>); pub struct RuneNature(pub Arc<dyn Nature>);
impl_rune_wrapper!(&Arc<dyn Nature>, RuneNature); impl_rune_wrapper!(&Arc<dyn Nature>, RuneNature);
impl RuneNature { impl RuneNature {
#[rune::function] #[rune::function]
pub fn increased_stat(&self) -> Statistic { self.0.increased_stat() } fn increased_stat(&self) -> Statistic { self.0.increased_stat() }
#[rune::function] #[rune::function]
pub fn decreased_stat(&self) -> Statistic { self.0.decreased_stat() } fn decreased_stat(&self) -> Statistic { self.0.decreased_stat() }
#[rune::function] #[rune::function]
pub fn increased_modifier(&self) -> f32 { self.0.increased_modifier() } fn increased_modifier(&self) -> f32 { self.0.increased_modifier() }
#[rune::function] #[rune::function]
pub fn decreased_modifier(&self) -> f32 { self.0.decreased_modifier() } fn decreased_modifier(&self) -> f32 { self.0.decreased_modifier() }
#[rune::function] #[rune::function]
pub fn get_stat_modifier(&self, stat: Statistic) -> f32 { self.0.get_stat_modifier(stat) } fn get_stat_modifier(&self, stat: Statistic) -> f32 { self.0.get_stat_modifier(stat) }
} }

View File

@ -0,0 +1,56 @@
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneStringKey, RuneWrapper};
use crate::static_data::Species;
use rune::runtime::{AnyObj, Shared};
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneSpecies>()?;
module.function_meta(RuneSpecies::id)?;
module.function_meta(RuneSpecies::name)?;
module.function_meta(RuneSpecies::gender_rate)?;
module.function_meta(RuneSpecies::growth_rate)?;
module.function_meta(RuneSpecies::capture_rate)?;
module.function_meta(RuneSpecies::base_happiness)?;
module.function_meta(RuneSpecies::get_form)?;
module.function_meta(RuneSpecies::get_default_form)?;
module.function_meta(RuneSpecies::has_flag)?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneSpecies(Arc<dyn Species>);
impl_rune_wrapper!(&Arc<dyn Species>, RuneSpecies);
impl RuneSpecies {
#[rune::function]
fn id(&self) -> u16 { self.0.id() }
#[rune::function]
fn name(&self) -> Shared<AnyObj> { self.0.name().wrap() }
#[rune::function]
fn gender_rate(&self) -> f32 { self.0.gender_rate() }
#[rune::function]
fn growth_rate(&self) -> Shared<AnyObj> { self.0.growth_rate().wrap() }
#[rune::function]
fn capture_rate(&self) -> u8 { self.0.capture_rate() }
#[rune::function]
fn base_happiness(&self) -> u8 { self.0.base_happiness() }
#[rune::function]
fn get_form(&self, name: &RuneStringKey) -> Option<Shared<AnyObj>> {
self.0.get_form(&name.0).map(|form| form.wrap())
}
#[rune::function]
fn get_default_form(&self) -> anyhow::Result<Shared<AnyObj>> { self.0.get_default_form().map(|v| v.wrap()) }
#[rune::function]
fn has_flag(&self, key: &RuneStringKey) -> bool { self.0.has_flag(&key.0) }
}

View File

@ -5,7 +5,24 @@ use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> { pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneStatisticSet>()?; module.ty::<RuneStatisticSet>()?;
module.function_meta(RuneStatisticSet::get)?;
module.function_meta(RuneStatisticSet::set)?;
module.function_meta(RuneStatisticSet::hp)?;
module.function_meta(RuneStatisticSet::attack)?;
module.function_meta(RuneStatisticSet::defense)?;
module.function_meta(RuneStatisticSet::special_attack)?;
module.function_meta(RuneStatisticSet::special_defense)?;
module.function_meta(RuneStatisticSet::speed)?;
module.ty::<RuneStaticStatisticSet>()?; module.ty::<RuneStaticStatisticSet>()?;
module.function_meta(RuneStaticStatisticSet::get)?;
module.function_meta(RuneStaticStatisticSet::hp)?;
module.function_meta(RuneStaticStatisticSet::attack)?;
module.function_meta(RuneStaticStatisticSet::defense)?;
module.function_meta(RuneStaticStatisticSet::special_attack)?;
module.function_meta(RuneStaticStatisticSet::special_defense)?;
module.function_meta(RuneStaticStatisticSet::speed)?;
Ok(()) Ok(())
} }
@ -15,28 +32,28 @@ pub struct RuneStatisticSet(Arc<StatisticSet<u32>>);
impl_rune_wrapper!(&Arc<StatisticSet<u32>>, RuneStatisticSet); impl_rune_wrapper!(&Arc<StatisticSet<u32>>, RuneStatisticSet);
impl RuneStatisticSet { impl RuneStatisticSet {
#[rune::function] #[rune::function]
pub fn get(&self, stat: Statistic) -> u32 { self.0.get_stat(stat) } fn get(&self, stat: Statistic) -> u32 { self.0.get_stat(stat) }
#[rune::function] #[rune::function]
pub fn set(&mut self, stat: Statistic, value: u32) { self.0.set_stat(stat, value) } fn set(&mut self, stat: Statistic, value: u32) { self.0.set_stat(stat, value) }
#[rune::function] #[rune::function]
pub fn hp(&self) -> u32 { self.0.hp() } fn hp(&self) -> u32 { self.0.hp() }
#[rune::function] #[rune::function]
pub fn attack(&self) -> u32 { self.0.attack() } fn attack(&self) -> u32 { self.0.attack() }
#[rune::function] #[rune::function]
pub fn defense(&self) -> u32 { self.0.defense() } fn defense(&self) -> u32 { self.0.defense() }
#[rune::function] #[rune::function]
pub fn special_attack(&self) -> u32 { self.0.special_attack() } fn special_attack(&self) -> u32 { self.0.special_attack() }
#[rune::function] #[rune::function]
pub fn special_defense(&self) -> u32 { self.0.special_defense() } fn special_defense(&self) -> u32 { self.0.special_defense() }
#[rune::function] #[rune::function]
pub fn speed(&self) -> u32 { self.0.speed() } fn speed(&self) -> u32 { self.0.speed() }
} }
#[derive(Debug, Any)] #[derive(Debug, Any)]
@ -46,22 +63,22 @@ impl_rune_wrapper!(&Arc<StaticStatisticSet<u16>>, RuneStaticStatisticSet);
impl RuneStaticStatisticSet { impl RuneStaticStatisticSet {
#[rune::function] #[rune::function]
pub fn get(&self, stat: Statistic) -> u16 { self.0.get_stat(stat) } fn get(&self, stat: Statistic) -> u16 { self.0.get_stat(stat) }
#[rune::function] #[rune::function]
pub fn hp(&self) -> u16 { self.0.hp() } fn hp(&self) -> u16 { self.0.hp() }
#[rune::function] #[rune::function]
pub fn attack(&self) -> u16 { self.0.attack() } fn attack(&self) -> u16 { self.0.attack() }
#[rune::function] #[rune::function]
pub fn defense(&self) -> u16 { self.0.defense() } fn defense(&self) -> u16 { self.0.defense() }
#[rune::function] #[rune::function]
pub fn special_attack(&self) -> u16 { self.0.special_attack() } fn special_attack(&self) -> u16 { self.0.special_attack() }
#[rune::function] #[rune::function]
pub fn special_defense(&self) -> u16 { self.0.special_defense() } fn special_defense(&self) -> u16 { self.0.special_defense() }
#[rune::function] #[rune::function]
pub fn speed(&self) -> u16 { self.0.speed() } pub fn speed(&self) -> u16 { self.0.speed() }

View File

@ -2,9 +2,10 @@ use crate::defines::LevelInt;
use crate::VecExt; use crate::VecExt;
use anyhow::Result; use anyhow::Result;
use anyhow_ext::ensure; use anyhow_ext::ensure;
use std::fmt::Debug;
/// A growth rate defines how much experience is required per level. /// A growth rate defines how much experience is required per level.
pub trait GrowthRate { pub trait GrowthRate: Debug {
/// Calculate the level something with this growth rate would have at a certain experience. /// Calculate the level something with this growth rate would have at a certain experience.
fn calculate_level(&self, experience: u32) -> LevelInt; fn calculate_level(&self, experience: u32) -> LevelInt;
/// Calculate the experience something with this growth rate would have at a certain level. /// Calculate the experience something with this growth rate would have at a certain level.
@ -12,6 +13,7 @@ pub trait GrowthRate {
} }
/// An implementation of the growth rate that uses a lookup table for experience. /// An implementation of the growth rate that uses a lookup table for experience.
#[derive(Debug)]
pub struct LookupGrowthRate { pub struct LookupGrowthRate {
/// The lookup Vec. /// The lookup Vec.
experience: Vec<u32>, experience: Vec<u32>,
@ -20,9 +22,7 @@ pub struct LookupGrowthRate {
impl LookupGrowthRate { impl LookupGrowthRate {
/// Instantiates a new lookup growth rate. The experience vec should be the amount of experience /// Instantiates a new lookup growth rate. The experience vec should be the amount of experience
/// required per level, with the first element being the experience required for level 1 (generally 0). /// required per level, with the first element being the experience required for level 1 (generally 0).
pub fn new(experience: Vec<u32>) -> LookupGrowthRate { pub fn new(experience: Vec<u32>) -> LookupGrowthRate { LookupGrowthRate { experience } }
LookupGrowthRate { experience }
}
} }
impl GrowthRate for LookupGrowthRate { impl GrowthRate for LookupGrowthRate {

View File

@ -1,6 +1,5 @@
use hashbrown::HashSet; use hashbrown::HashSet;
#[cfg(feature = "serde")] #[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use std::any::Any; use std::any::Any;
use std::fmt::Debug; use std::fmt::Debug;
@ -9,6 +8,7 @@ use crate::StringKey;
/// An item category defines which bag slot items are stored in. /// An item category defines which bag slot items are stored in.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "rune", derive(rune::Any))]
#[repr(u8)] #[repr(u8)]
pub enum ItemCategory { pub enum ItemCategory {
/// This is where most items should go. /// This is where most items should go.
@ -32,6 +32,7 @@ pub enum ItemCategory {
/// A battle item category defines how the item is categorized when in battle. /// A battle item category defines how the item is categorized when in battle.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "rune", derive(rune::Any))]
#[repr(u8)] #[repr(u8)]
pub enum BattleItemCategory { pub enum BattleItemCategory {
/// This item can't be used in battle. /// This item can't be used in battle.
@ -99,30 +100,18 @@ impl ItemImpl {
impl Item for ItemImpl { impl Item for ItemImpl {
/// The name of the item. /// The name of the item.
fn name(&self) -> &StringKey { fn name(&self) -> &StringKey { &self.name }
&self.name
}
/// Which bag slot items are stored in. /// Which bag slot items are stored in.
fn category(&self) -> ItemCategory { fn category(&self) -> ItemCategory { self.category }
self.category
}
/// How the item is categorized when in battle. /// How the item is categorized when in battle.
fn battle_category(&self) -> BattleItemCategory { fn battle_category(&self) -> BattleItemCategory { self.battle_category }
self.battle_category
}
/// The buying value of the item. /// The buying value of the item.
fn price(&self) -> i32 { fn price(&self) -> i32 { self.price }
self.price
}
/// A set of arbitrary flags that can be set on the item. /// A set of arbitrary flags that can be set on the item.
fn flags(&self) -> &HashSet<StringKey> { fn flags(&self) -> &HashSet<StringKey> { &self.flags }
&self.flags
}
/// Checks whether the item has a specific flag. /// Checks whether the item has a specific flag.
fn has_flag(&self, key: &StringKey) -> bool { fn has_flag(&self, key: &StringKey) -> bool { self.flags.contains(key) }
self.flags.contains(key)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,5 @@
use anyhow_ext::Result; use anyhow_ext::Result;
use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
use indexmap::IndexMap; use indexmap::IndexMap;
@ -8,7 +9,7 @@ use crate::StringKey;
/// A data library is a collection of methods to set up a default library, where values are stored /// A data library is a collection of methods to set up a default library, where values are stored
/// by both key, while keeping their insertion order. /// by both key, while keeping their insertion order.
pub trait DataLibrary<T: ?Sized> { pub trait DataLibrary<T: ?Sized>: Debug {
/// Returns the underlying map. /// Returns the underlying map.
fn map(&self) -> &IndexMap<StringKey, Arc<T>>; fn map(&self) -> &IndexMap<StringKey, Arc<T>>;
/// Returns the underlying map in mutable manner. /// Returns the underlying map in mutable manner.
@ -25,32 +26,22 @@ pub trait DataLibrary<T: ?Sized> {
fn remove(&self, key: &StringKey) { fn remove(&self, key: &StringKey) {
#[allow(clippy::unwrap_used)] // We know this cant fail. #[allow(clippy::unwrap_used)] // We know this cant fail.
let self_mut = unsafe { (self as *const Self as *mut Self).as_mut() }.unwrap(); let self_mut = unsafe { (self as *const Self as *mut Self).as_mut() }.unwrap();
self_mut.get_modify().remove(key); self_mut.get_modify().swap_remove(key);
} }
/// Gets a value from the library. /// Gets a value from the library.
fn get(&self, key: &StringKey) -> Option<Arc<T>> { fn get(&self, key: &StringKey) -> Option<Arc<T>> { self.map().get::<StringKey>(key).cloned() }
self.map().get::<StringKey>(key).cloned()
}
/// Gets a value from the library. /// Gets a value from the library.
fn get_by_hash(&self, key: u32) -> Option<Arc<T>> { fn get_by_hash(&self, key: u32) -> Option<Arc<T>> { self.map().get::<u32>(&key).cloned() }
self.map().get::<u32>(&key).cloned()
}
/// Gets a value from the library by the index where it is stored. /// Gets a value from the library by the index where it is stored.
fn get_key_by_index(&self, index: usize) -> Option<StringKey> { fn get_key_by_index(&self, index: usize) -> Option<StringKey> { self.map().get_index(index).map(|a| a.0.clone()) }
self.map().get_index(index).map(|a| a.0.clone())
}
/// Gets the amount of values in the library. /// Gets the amount of values in the library.
fn len(&self) -> usize { fn len(&self) -> usize { self.map().len() }
self.map().len()
}
/// Returns whether the library has no values. /// Returns whether the library has no values.
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool { self.map().is_empty() }
self.map().is_empty()
}
/// Gets a random value from the library. /// Gets a random value from the library.
fn random_value(&self, rand: &mut Random) -> Result<&Arc<T>> { fn random_value(&self, rand: &mut Random) -> Result<&Arc<T>> {

View File

@ -61,9 +61,7 @@ impl GrowthRateLibrary for GrowthRateLibraryImpl {
} }
impl Debug for GrowthRateLibraryImpl { impl Debug for GrowthRateLibraryImpl {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("GrowthRateLibrary").finish() }
f.debug_struct("GrowthRateLibrary").finish()
}
} }
#[cfg(test)] #[cfg(test)]
@ -72,8 +70,6 @@ impl Debug for GrowthRateLibraryImpl {
pub mod tests { pub mod tests {
use super::*; use super::*;
use crate::static_data::growth_rates::LookupGrowthRate; use crate::static_data::growth_rates::LookupGrowthRate;
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary;
use crate::static_data::GrowthRateLibraryImpl;
pub fn build() -> GrowthRateLibraryImpl { pub fn build() -> GrowthRateLibraryImpl {
let mut lib = GrowthRateLibraryImpl::new(1); let mut lib = GrowthRateLibraryImpl::new(1);

View File

@ -38,14 +38,10 @@ impl NatureLibraryImpl {
impl NatureLibrary for NatureLibraryImpl { impl NatureLibrary for NatureLibraryImpl {
/// Adds a new nature with name to the library. /// Adds a new nature with name to the library.
fn load_nature(&self, name: StringKey, nature: Arc<dyn Nature>) { fn load_nature(&self, name: StringKey, nature: Arc<dyn Nature>) { self.map.write().insert(name, nature); }
self.map.write().insert(name, nature);
}
/// Gets a nature by name. /// Gets a nature by name.
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>> { fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>> { self.map.read().get(key).cloned() }
self.map.read().get(key).cloned()
}
fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>> { fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>> {
let map = self.map.read(); let map = self.map.read();
@ -80,7 +76,7 @@ impl NatureLibrary for NatureLibraryImpl {
pub mod tests { pub mod tests {
use super::*; use super::*;
use crate::static_data::statistics::Statistic; use crate::static_data::statistics::Statistic;
use crate::static_data::{NatureImpl, NatureLibrary, NatureLibraryImpl}; use crate::static_data::NatureImpl;
pub fn build() -> NatureLibraryImpl { pub fn build() -> NatureLibraryImpl {
let lib = NatureLibraryImpl::new(2); let lib = NatureLibraryImpl::new(2);

View File

@ -11,27 +11,18 @@ use crate::{PkmnError, StringKey};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Atom)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Atom)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)] #[repr(transparent)]
pub struct TypeIdentifier { pub struct TypeIdentifier(u8);
/// The unique internal value.
val: u8,
}
impl From<u8> for TypeIdentifier { impl From<u8> for TypeIdentifier {
fn from(val: u8) -> Self { fn from(val: u8) -> Self { Self(val) }
Self { val }
}
} }
impl From<TypeIdentifier> for u8 { impl From<TypeIdentifier> for u8 {
fn from(id: TypeIdentifier) -> Self { fn from(id: TypeIdentifier) -> Self { id.0 }
id.val
}
} }
impl Display for TypeIdentifier { impl Display for TypeIdentifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "TypeId({})", self.0) }
write!(f, "TypeId({})", self.val)
}
} }
/// All data related to types and effectiveness. /// All data related to types and effectiveness.
@ -87,18 +78,16 @@ impl TypeLibraryImpl {
defending: TypeIdentifier, defending: TypeIdentifier,
) -> Result<f32> { ) -> Result<f32> {
Ok(*lock Ok(*lock
.get((attacking.val - 1) as usize) .get((attacking.0 - 1) as usize)
.ok_or(PkmnError::InvalidTypeIdentifier { type_id: attacking })? .ok_or(PkmnError::InvalidTypeIdentifier { type_id: attacking })?
.get((defending.val - 1) as usize) .get((defending.0 - 1) as usize)
.ok_or(PkmnError::InvalidTypeIdentifier { type_id: defending })?) .ok_or(PkmnError::InvalidTypeIdentifier { type_id: defending })?)
} }
} }
impl TypeLibrary for TypeLibraryImpl { impl TypeLibrary for TypeLibraryImpl {
/// Gets the type identifier for a type with a name. /// Gets the type identifier for a type with a name.
fn get_type_id(&self, key: &StringKey) -> Option<TypeIdentifier> { fn get_type_id(&self, key: &StringKey) -> Option<TypeIdentifier> { self.types.read().get(key).cloned() }
self.types.read().get(key).cloned()
}
/// Gets the type name from the type identifier. /// Gets the type name from the type identifier.
fn get_type_name(&self, t: TypeIdentifier) -> Option<StringKey> { fn get_type_name(&self, t: TypeIdentifier) -> Option<StringKey> {
@ -133,13 +122,11 @@ impl TypeLibrary for TypeLibraryImpl {
let mut types_write_lock = self.types.write(); let mut types_write_lock = self.types.write();
let mut effectiveness_write_lock = self.effectiveness.write(); let mut effectiveness_write_lock = self.effectiveness.write();
let id = TypeIdentifier { let id = TypeIdentifier((types_write_lock.len() + 1) as u8);
val: (types_write_lock.len() + 1) as u8,
};
types_write_lock.insert(name.clone(), id); types_write_lock.insert(name.clone(), id);
effectiveness_write_lock.resize((id.val) as usize, vec![]); effectiveness_write_lock.resize((id.0) as usize, vec![]);
for effectiveness in &mut effectiveness_write_lock.iter_mut() { for effectiveness in &mut effectiveness_write_lock.iter_mut() {
effectiveness.resize((id.val) as usize, 1.0) effectiveness.resize((id.0) as usize, 1.0)
} }
id id
} }
@ -154,9 +141,9 @@ impl TypeLibrary for TypeLibraryImpl {
*self *self
.effectiveness .effectiveness
.write() .write()
.get_mut((attacking.val - 1) as usize) .get_mut((attacking.0 - 1) as usize)
.ok_or(PkmnError::InvalidTypeIdentifier { type_id: attacking })? .ok_or(PkmnError::InvalidTypeIdentifier { type_id: attacking })?
.get_mut((defending.val - 1) as usize) .get_mut((defending.0 - 1) as usize)
.ok_or(PkmnError::InvalidTypeIdentifier { type_id: defending })? = effectiveness; .ok_or(PkmnError::InvalidTypeIdentifier { type_id: defending })? = effectiveness;
Ok(()) Ok(())
} }
@ -169,7 +156,6 @@ pub mod tests {
use assert_approx_eq::assert_approx_eq; use assert_approx_eq::assert_approx_eq;
use super::*; use super::*;
use crate::static_data::libraries::type_library::TypeLibrary;
pub fn build() -> TypeLibraryImpl { pub fn build() -> TypeLibraryImpl {
let mut lib = TypeLibraryImpl::new(2); let mut lib = TypeLibraryImpl::new(2);

View File

@ -24,12 +24,6 @@ use std::fmt::{Display, Formatter};
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
#[doc(inline)] #[doc(inline)]
pub use growth_rates::tests::*;
#[doc(inline)]
pub use items::tests::*;
#[doc(inline)]
pub use libraries::tests::*;
#[doc(inline)]
pub use moves::tests::*; pub use moves::tests::*;
#[doc(inline)] #[doc(inline)]
pub use natures::tests::*; pub use natures::tests::*;
@ -71,27 +65,19 @@ pub enum Parameter {
} }
impl From<bool> for Parameter { impl From<bool> for Parameter {
fn from(b: bool) -> Self { fn from(b: bool) -> Self { Parameter::Bool(b) }
Parameter::Bool(b)
}
} }
impl From<i64> for Parameter { impl From<i64> for Parameter {
fn from(i: i64) -> Self { fn from(i: i64) -> Self { Parameter::Int(i) }
Parameter::Int(i)
}
} }
impl From<f32> for Parameter { impl From<f32> for Parameter {
fn from(f: f32) -> Self { fn from(f: f32) -> Self { Parameter::Float(f) }
Parameter::Float(f)
}
} }
impl From<StringKey> for Parameter { impl From<StringKey> for Parameter {
fn from(s: StringKey) -> Self { fn from(s: StringKey) -> Self { Parameter::String(s) }
Parameter::String(s)
}
} }
impl Display for Parameter { impl Display for Parameter {

View File

@ -1,16 +1,11 @@
#[doc(inline)] #[doc(inline)] pub use move_data::*;
pub use move_data::*; #[doc(inline)] pub use secondary_effect::*;
#[doc(inline)]
pub use secondary_effect::*;
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
#[doc(inline)] #[doc(inline)] pub use move_data::tests::*;
pub use move_data::tests::*;
#[doc(inline)]
pub use secondary_effect::tests::*;
} }
/// The data belonging to a certain move. /// The data belonging to a certain move.

View File

@ -1,6 +1,5 @@
use hashbrown::HashSet; use hashbrown::HashSet;
#[cfg(feature = "serde")] #[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
@ -11,6 +10,7 @@ use crate::StringKey;
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] #[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[cfg_attr(feature = "rune", derive(rune::Any))]
#[repr(u8)] #[repr(u8)]
pub enum MoveCategory { pub enum MoveCategory {
/// A physical move uses the physical attack stats and physical defense stats to calculate damage. /// A physical move uses the physical attack stats and physical defense stats to calculate damage.
@ -24,6 +24,7 @@ pub enum MoveCategory {
/// The move target defines what kind of targets the move can touch. /// The move target defines what kind of targets the move can touch.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "rune", derive(rune::Any))]
#[repr(u8)] #[repr(u8)]
pub enum MoveTarget { pub enum MoveTarget {
/// Adjacent allows a move to target any Pokemon that is either directly to the left or right of /// Adjacent allows a move to target any Pokemon that is either directly to the left or right of
@ -149,54 +150,32 @@ impl MoveDataImpl {
impl MoveData for MoveDataImpl { impl MoveData for MoveDataImpl {
/// The name of the move. /// The name of the move.
fn name(&self) -> &StringKey { fn name(&self) -> &StringKey { &self.name }
&self.name
}
/// The attacking type of the move. /// The attacking type of the move.
fn move_type(&self) -> TypeIdentifier { fn move_type(&self) -> TypeIdentifier { self.move_type }
self.move_type
}
/// The category of the move. /// The category of the move.
fn category(&self) -> MoveCategory { fn category(&self) -> MoveCategory { self.category }
self.category
}
/// The base power, not considering any modifiers, the move has. /// The base power, not considering any modifiers, the move has.
fn base_power(&self) -> u8 { fn base_power(&self) -> u8 { self.base_power }
self.base_power
}
/// The accuracy of the move in percentage. Should be 255 for moves that always hit. /// The accuracy of the move in percentage. Should be 255 for moves that always hit.
fn accuracy(&self) -> u8 { fn accuracy(&self) -> u8 { self.accuracy }
self.accuracy
}
/// The number of times the move can be used. This can be modified on actually learned moves using /// The number of times the move can be used. This can be modified on actually learned moves using
/// PP-Ups /// PP-Ups
fn base_usages(&self) -> u8 { fn base_usages(&self) -> u8 { self.base_usages }
self.base_usages
}
/// How the move handles targets. /// How the move handles targets.
fn target(&self) -> MoveTarget { fn target(&self) -> MoveTarget { self.target }
self.target
}
/// The priority of the move. A higher priority means the move should go before other moves. /// The priority of the move. A higher priority means the move should go before other moves.
fn priority(&self) -> i8 { fn priority(&self) -> i8 { self.priority }
self.priority
}
/// The optional secondary effect the move has. /// The optional secondary effect the move has.
fn secondary_effect(&self) -> &Option<Arc<dyn SecondaryEffect>> { fn secondary_effect(&self) -> &Option<Arc<dyn SecondaryEffect>> { &self.secondary_effect }
&self.secondary_effect
}
/// Checks if the move has a specific flag. /// Checks if the move has a specific flag.
fn has_flag(&self, key: &StringKey) -> bool { fn has_flag(&self, key: &StringKey) -> bool { self.flags.contains::<StringKey>(key) }
self.flags.contains::<StringKey>(key)
}
/// Checks if the move has a specific flag. /// Checks if the move has a specific flag.
fn has_flag_by_hash(&self, key_hash: u32) -> bool { fn has_flag_by_hash(&self, key_hash: u32) -> bool { self.flags.contains::<u32>(&key_hash) }
self.flags.contains::<u32>(&key_hash)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -52,9 +52,6 @@ pub(crate) mod tests {
use super::*; use super::*;
use assert_approx_eq::assert_approx_eq; use assert_approx_eq::assert_approx_eq;
use crate::static_data::moves::secondary_effect::SecondaryEffect;
use crate::static_data::SecondaryEffectImpl;
mockall::mock! { mockall::mock! {
#[derive(Debug)] #[derive(Debug)]
pub SecondaryEffect{} pub SecondaryEffect{}

View File

@ -4,6 +4,7 @@
/// that allows for a more progressive gender system for those that want it? /// that allows for a more progressive gender system for those that want it?
#[derive(Eq, PartialEq, Copy, Clone, Debug)] #[derive(Eq, PartialEq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr))] #[cfg_attr(feature = "serde", derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr))]
#[cfg_attr(feature = "rune", derive(rune::Any))]
#[repr(u8)] #[repr(u8)]
pub enum Gender { pub enum Gender {
/// The Pokemon has no gender. /// The Pokemon has no gender.

View File

@ -13,9 +13,7 @@ pub use species::*;
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
pub use super::ability::tests::*;
pub use super::form::tests::*; pub use super::form::tests::*;
pub use super::learnable_moves::tests::*;
pub use super::species::tests::*; pub use super::species::tests::*;
} }

View File

@ -1,5 +1,4 @@
#[cfg(feature = "serde")] #[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
/// Stats are numerical values on Pokemon that are used in battle. /// Stats are numerical values on Pokemon that are used in battle.
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]

View File

@ -6,7 +6,8 @@ impl TestMove {
pub fn change_speed(self, choice, speed) { pub fn change_speed(self, choice, speed) {
println(`change_speed: ${choice.speed()}`); println(`change_speed: ${choice.speed()}`);
println(`user level: ${choice.user().level()}`); println(`user level: ${choice.user().level()}`);
speed.value = 100; println(`owner: ${self.owner.level()}`);
speed += 100;
} }
} }

View File

@ -51,8 +51,16 @@ fn validate_library_load() {
fn rune_test() { fn rune_test() {
let result = library_loader::load_library(); let result = library_loader::load_library();
let library = result.library; let library = result.library;
let p1 = PokemonBuilder::new(library.clone(), "charizard".into(), 100)
.build()
.unwrap();
let script = library let script = library
.load_script(ScriptOwnerData::None, ScriptCategory::Move, &"TestMove".into()) .load_script(
ScriptOwnerData::Pokemon(p1.weak()),
ScriptCategory::Move,
&"TestMove".into(),
)
.unwrap() .unwrap()
.unwrap(); .unwrap();
assert_eq!(script.name().unwrap().str(), "TestMove"); assert_eq!(script.name().unwrap().str(), "TestMove");