More work on Rune
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2024-04-13 10:47:40 +02:00
parent 67b0abe59f
commit 4bc76b0ee4
18 changed files with 166 additions and 67 deletions

View File

@@ -1,15 +1,11 @@
use crate::dynamic_data::{Battle, DamageSource, ExecutingMove, Pokemon, TurnChoice};
use crate::static_data::{Item, Statistic, TypeIdentifier};
use crate::StringKey;
use rune::Hash;
use std::sync::Arc;
mod script;
pub mod script_resolver;
pub(self) mod wrappers;
mod wrappers;
#[derive(Debug, Default)]
pub(self) struct RuneScriptType {
struct RuneScriptType {
pub fn_on_initialize: Option<Hash>,
pub fn_on_stack: Option<Hash>,
pub fn_on_remove: Option<Hash>,

View File

@@ -6,9 +6,8 @@ use crate::StringKey;
use hashbrown::HashMap;
use parking_lot::RwLock;
use rune::runtime::{Object, RuntimeContext, Shared, VmError, VmResult};
use rune::{Any, Unit, Value};
use rune::{Unit, Value};
use std::convert::TryFrom;
use std::error::Error;
use std::ops::Deref;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::Arc;
@@ -73,7 +72,7 @@ impl Script for RuneScript {
if pars.is_empty() {
return Ok(());
}
let mut write_lock = self.state.write();
let write_lock = self.state.write();
for par in pars {
let key = rune::alloc::string::String::try_from(par.0.str())?;
write_lock

View File

@@ -4,12 +4,11 @@ use crate::script_implementations::rune::RuneScriptType;
use crate::static_data::Item;
use crate::StringKey;
use hashbrown::HashMap;
use parking_lot::RwLock;
use rune::compile::meta::AssociatedKind;
use rune::compile::{ComponentRef, MetaError};
use rune::diagnostics::Diagnostic;
use rune::runtime::{RuntimeContext, Shared};
use rune::{Context, Diagnostics, Hash, Options, Source, Sources, Unit, Vm};
use rune::{Context, Diagnostics, Options, Source, Sources, Unit};
use std::any::Any;
use std::path::Path;
use std::sync::Arc;
@@ -128,11 +127,13 @@ impl rune::compile::CompileVisitor for FindScriptTypeVisitor {
if meta.item.iter().count() < 2 {
return Ok(());
}
#[allow(clippy::unwrap_used)] // We know that the first element exists
let mod_name = meta.item.iter().nth(0).unwrap();
let category = match get_mod_category(mod_name) {
Ok(value) => value,
Err(value) => return value,
};
#[allow(clippy::unwrap_used)] // We know that the last element exists
let name = meta.item.last().unwrap();
self.script_types
.insert((category, name.to_string().as_str().into()), RuneScriptType::default());

View File

@@ -10,17 +10,15 @@ pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
}
#[derive(Debug, Any)]
pub struct RuneExecutingMove {
inner: Arc<ExecutingMove>,
}
pub struct RuneExecutingMove(Arc<ExecutingMove>);
impl RuneExecutingMove {
#[rune::function]
pub fn target_count(&self) -> usize { self.inner.target_count() }
pub fn target_count(&self) -> usize { self.0.target_count() }
#[rune::function]
pub fn number_of_hits(&self) -> u8 { self.inner.number_of_hits() }
pub fn number_of_hits(&self) -> u8 { self.0.number_of_hits() }
#[rune::function]
pub fn user(&self) -> Shared<AnyObj> { self.inner.user().wrap() }
pub fn user(&self) -> Shared<AnyObj> { self.0.user().wrap() }
}
impl_rune_wrapper!(&Arc<ExecutingMove>, RuneExecutingMove);

View File

@@ -0,0 +1,10 @@
mod executing_move;
mod pokemon;
mod turn_choice;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
executing_move::register(module)?;
pokemon::register(module)?;
turn_choice::register(module)?;
Ok(())
}

View File

@@ -1,6 +1,6 @@
use crate::defines::LevelInt;
use crate::dynamic_data::Pokemon;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneWrapper};
use crate::script_implementations::rune::wrappers::impl_rune_wrapper;
use rune::Any;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
@@ -10,13 +10,11 @@ pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
}
#[derive(Any)]
pub struct RunePokemon {
inner: Pokemon,
}
pub struct RunePokemon(Pokemon);
impl RunePokemon {
#[rune::function]
fn level(&self) -> LevelInt { self.inner.level() }
fn level(&self) -> LevelInt { self.0.level() }
}
impl_rune_wrapper!(&Pokemon, RunePokemon);

View File

@@ -1,5 +1,5 @@
use crate::dynamic_data::TurnChoice;
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneExecutingMove, RuneWrapper};
use crate::script_implementations::rune::wrappers::{impl_rune_wrapper, RuneWrapper};
use rune::{Any, Value};
use std::sync::Arc;
@@ -11,16 +11,14 @@ pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
}
#[derive(Any)]
pub struct RuneTurnChoice {
inner: Arc<TurnChoice>,
}
pub struct RuneTurnChoice(Arc<TurnChoice>);
impl RuneTurnChoice {
#[rune::function]
fn speed(&self) -> u32 { self.inner.speed() }
fn speed(&self) -> u32 { self.0.speed() }
#[rune::function]
fn user(&self) -> Value { Value::from(self.inner.user().wrap()) }
fn user(&self) -> Value { Value::from(self.0.user().wrap()) }
}
impl_rune_wrapper!(&Arc<TurnChoice>, RuneTurnChoice);

View File

@@ -1,27 +1,18 @@
use rune::runtime::Protocol;
use rune::runtime::{AnyObj, Shared};
use rune::{Any, Value};
use std::ops::Deref;
mod executing_move;
mod pokemon;
mod turn_choice;
pub use executing_move::*;
pub use pokemon::*;
mod dynamic_data;
mod static_data;
pub trait RuneWrapper {
#[inline]
fn wrap(self) -> Shared<AnyObj>;
}
pub fn module() -> anyhow::Result<rune::Module> {
let mut module = rune::Module::new();
module.ty::<RuneValueIntWrapper>()?;
turn_choice::register(&mut module)?;
pokemon::register(&mut module)?;
executing_move::register(&mut module)?;
dynamic_data::register(&mut module)?;
static_data::register(&mut module)?;
Ok(module)
}
@@ -32,7 +23,10 @@ pub fn wrap_value_reference(value: i64) -> anyhow::Result<Value> {
pub fn get_value_reference(value: Value) -> anyhow::Result<i64> {
let obj = value.into_any().into_result()?;
let obj = obj.take()?;
let obj = obj.downcast_borrow_ref::<RuneValueIntWrapper>().unwrap();
let obj = match obj.downcast_borrow_ref::<RuneValueIntWrapper>() {
Some(obj) => obj,
None => return Err(anyhow::anyhow!("Value is not a RuneValueIntWrapper")),
};
Ok(obj.value())
}
@@ -57,11 +51,10 @@ macro_rules! impl_rune_wrapper {
($t:ty, $wrapped_type:ident) => {
impl crate::script_implementations::rune::wrappers::RuneWrapper for $t {
fn wrap(self) -> rune::runtime::Shared<rune::runtime::AnyObj> {
rune::runtime::Shared::new(rune::runtime::AnyObj::new($wrapped_type { inner: self.clone() }).unwrap())
.unwrap()
rune::runtime::Shared::new(rune::runtime::AnyObj::new($wrapped_type(self.clone())).unwrap()).unwrap()
}
}
};
}
pub(self) use impl_rune_wrapper;
use impl_rune_wrapper;

View File

@@ -0,0 +1,12 @@
mod nature;
mod statistic_set;
use crate::static_data::{Statistic, TimeOfDay};
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<TimeOfDay>()?;
module.ty::<Statistic>()?;
statistic_set::register(module)?;
nature::register(module)?;
Ok(())
}

View File

@@ -0,0 +1,31 @@
use crate::script_implementations::rune::wrappers::impl_rune_wrapper;
use crate::static_data::{Nature, Statistic};
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneNature>()?;
Ok(())
}
#[derive(Debug, Any)]
pub struct RuneNature(Arc<dyn Nature>);
impl_rune_wrapper!(&Arc<dyn Nature>, RuneNature);
impl RuneNature {
#[rune::function]
pub fn increased_stat(&self) -> Statistic { self.0.increased_stat() }
#[rune::function]
pub fn decreased_stat(&self) -> Statistic { self.0.decreased_stat() }
#[rune::function]
pub fn increased_modifier(&self) -> f32 { self.0.increased_modifier() }
#[rune::function]
pub 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) }
}

View File

@@ -0,0 +1,68 @@
use crate::script_implementations::rune::wrappers::impl_rune_wrapper;
use crate::static_data::{StaticStatisticSet, Statistic, StatisticSet};
use rune::Any;
use std::sync::Arc;
pub fn register(module: &mut rune::Module) -> anyhow::Result<()> {
module.ty::<RuneStatisticSet>()?;
module.ty::<RuneStaticStatisticSet>()?;
Ok(())
}
#[derive(Debug, Any)]
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) }
#[rune::function]
pub fn set(&mut self, stat: Statistic, value: u32) { self.0.set_stat(stat, value) }
#[rune::function]
pub fn hp(&self) -> u32 { self.0.hp() }
#[rune::function]
pub fn attack(&self) -> u32 { self.0.attack() }
#[rune::function]
pub fn defense(&self) -> u32 { self.0.defense() }
#[rune::function]
pub fn special_attack(&self) -> u32 { self.0.special_attack() }
#[rune::function]
pub fn special_defense(&self) -> u32 { self.0.special_defense() }
#[rune::function]
pub fn speed(&self) -> u32 { self.0.speed() }
}
#[derive(Debug, Any)]
pub struct RuneStaticStatisticSet(Arc<StaticStatisticSet<u16>>);
impl_rune_wrapper!(&Arc<StaticStatisticSet<u16>>, RuneStaticStatisticSet);
impl RuneStaticStatisticSet {
#[rune::function]
pub fn get(&self, stat: Statistic) -> u16 { self.0.get_stat(stat) }
#[rune::function]
pub fn hp(&self) -> u16 { self.0.hp() }
#[rune::function]
pub fn attack(&self) -> u16 { self.0.attack() }
#[rune::function]
pub fn defense(&self) -> u16 { self.0.defense() }
#[rune::function]
pub fn special_attack(&self) -> u16 { self.0.special_attack() }
#[rune::function]
pub fn special_defense(&self) -> u16 { self.0.special_defense() }
#[rune::function]
pub fn speed(&self) -> u16 { self.0.speed() }
}