Large amounts of work on Rune
Some checks failed
continuous-integration/drone/push Build is failing

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

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::RuneScriptType;
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
/// we will not execute its methods. This holds the number of suppressions on the script.
suppressed_count: AtomicUsize,
/// The owner of this script (where the script is attached to)
owner: ScriptOwnerData,
script_type: Arc<RuneScriptType>,
@@ -39,7 +37,6 @@ impl RuneScript {
pub fn new(
name: StringKey,
object: Shared<Object>,
owner: ScriptOwnerData,
script_type: Arc<RuneScriptType>,
runtime: Arc<RuntimeContext>,
unit: Arc<Unit>,
@@ -49,7 +46,6 @@ impl RuneScript {
state: RwLock::new(object),
marked_for_deletion: Default::default(),
suppressed_count: Default::default(),
owner,
script_type,
runtime,
unit,
@@ -82,39 +78,10 @@ impl Script for RuneScript {
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<()> {
if let Some(hash) = self.script_type.fn_change_speed {
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 state = read_lock.deref();
@@ -132,7 +99,35 @@ impl Script for RuneScript {
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(())
}

View File

@@ -1,5 +1,6 @@
use crate::dynamic_data::{ItemScript, Script, ScriptCategory, ScriptOwnerData, ScriptResolver};
use crate::script_implementations::rune::script::RuneScript;
use crate::script_implementations::rune::wrappers::RuneWrapper;
use crate::script_implementations::rune::RuneScriptType;
use crate::static_data::Item;
use crate::StringKey;
@@ -10,6 +11,7 @@ use rune::diagnostics::Diagnostic;
use rune::runtime::{RuntimeContext, Shared};
use rune::{Context, Diagnostics, Options, Source, Sources, Unit};
use std::any::Any;
use std::convert::TryFrom;
use std::path::Path;
use std::sync::Arc;
@@ -38,11 +40,22 @@ impl ScriptResolver for RuneScriptResolver {
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(
script_key.clone(),
state,
owner,
script_type.clone(),
self.runtime.clone(),
self.unit.clone(),

View File

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

View File

@@ -1,7 +1,9 @@
use rune::runtime::{AnyObj, Shared};
use rune::runtime::{AnyObj, Protocol, Shared, VmResult};
use rune::{Any, Value};
use std::num::Saturating;
mod dynamic_data;
mod parameters;
mod static_data;
pub trait RuneWrapper {
@@ -11,40 +13,57 @@ pub trait RuneWrapper {
pub fn module() -> anyhow::Result<rune::Module> {
let mut module = rune::Module::new();
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)?;
static_data::register(&mut 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))?)?))
}
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 = obj.take()?;
let obj = match obj.downcast_borrow_ref::<RuneValueIntWrapper>() {
Some(obj) => obj,
None => return Err(anyhow::anyhow!("Value is not a RuneValueIntWrapper")),
};
Ok(obj.value())
Ok(obj.as_int())
}
#[derive(Any, Clone)]
struct RuneValueIntWrapper {
#[rune(get, set)]
value: i64,
}
impl RuneValueIntWrapper {
pub fn new(value: i64) -> Self { Self { value } }
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() }
pub fn get_bool_reference_value(value: Value) -> anyhow::Result<bool> {
let obj = value.into_any().into_result()?;
let obj = obj.take()?;
let obj = match obj.downcast_borrow_ref::<RuneValueBoolWrapper>() {
Some(obj) => obj,
None => return Err(anyhow::anyhow!("Value is not a RuneValueBoolWrapper")),
};
Ok(obj.as_bool())
}
macro_rules! impl_rune_wrapper {
@@ -57,4 +76,76 @@ macro_rules! impl_rune_wrapper {
};
}
use crate::StringKey;
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 species;
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<()> {
module.ty::<TimeOfDay>()?;
module.ty::<Statistic>()?;
module.ty::<Gender>()?;
statistic_set::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(())
}

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<()> {
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(())
}
#[derive(Debug, Any)]
pub struct RuneNature(Arc<dyn Nature>);
pub struct RuneNature(pub Arc<dyn Nature>);
impl_rune_wrapper!(&Arc<dyn Nature>, RuneNature);
impl RuneNature {
#[rune::function]
pub fn increased_stat(&self) -> Statistic { self.0.increased_stat() }
fn increased_stat(&self) -> Statistic { self.0.increased_stat() }
#[rune::function]
pub fn decreased_stat(&self) -> Statistic { self.0.decreased_stat() }
fn decreased_stat(&self) -> Statistic { self.0.decreased_stat() }
#[rune::function]
pub fn increased_modifier(&self) -> f32 { self.0.increased_modifier() }
fn increased_modifier(&self) -> f32 { self.0.increased_modifier() }
#[rune::function]
pub fn decreased_modifier(&self) -> f32 { self.0.decreased_modifier() }
fn decreased_modifier(&self) -> f32 { self.0.decreased_modifier() }
#[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<()> {
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.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(())
}
@@ -15,28 +32,28 @@ pub struct RuneStatisticSet(Arc<StatisticSet<u32>>);
impl_rune_wrapper!(&Arc<StatisticSet<u32>>, RuneStatisticSet);
impl RuneStatisticSet {
#[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]
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]
pub fn hp(&self) -> u32 { self.0.hp() }
fn hp(&self) -> u32 { self.0.hp() }
#[rune::function]
pub fn attack(&self) -> u32 { self.0.attack() }
fn attack(&self) -> u32 { self.0.attack() }
#[rune::function]
pub fn defense(&self) -> u32 { self.0.defense() }
fn defense(&self) -> u32 { self.0.defense() }
#[rune::function]
pub fn special_attack(&self) -> u32 { self.0.special_attack() }
fn special_attack(&self) -> u32 { self.0.special_attack() }
#[rune::function]
pub fn special_defense(&self) -> u32 { self.0.special_defense() }
fn special_defense(&self) -> u32 { self.0.special_defense() }
#[rune::function]
pub fn speed(&self) -> u32 { self.0.speed() }
fn speed(&self) -> u32 { self.0.speed() }
}
#[derive(Debug, Any)]
@@ -46,22 +63,22 @@ impl_rune_wrapper!(&Arc<StaticStatisticSet<u16>>, RuneStaticStatisticSet);
impl RuneStaticStatisticSet {
#[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]
pub fn hp(&self) -> u16 { self.0.hp() }
fn hp(&self) -> u16 { self.0.hp() }
#[rune::function]
pub fn attack(&self) -> u16 { self.0.attack() }
fn attack(&self) -> u16 { self.0.attack() }
#[rune::function]
pub fn defense(&self) -> u16 { self.0.defense() }
fn defense(&self) -> u16 { self.0.defense() }
#[rune::function]
pub fn special_attack(&self) -> u16 { self.0.special_attack() }
fn special_attack(&self) -> u16 { self.0.special_attack() }
#[rune::function]
pub fn special_defense(&self) -> u16 { self.0.special_defense() }
fn special_defense(&self) -> u16 { self.0.special_defense() }
#[rune::function]
pub fn speed(&self) -> u16 { self.0.speed() }