113 lines
3.5 KiB
Rust
Executable File
113 lines
3.5 KiB
Rust
Executable File
use std::ops::Deref;
|
|
|
|
use crate::dynamic_data::{LearnedMove, Pokemon, TurnChoice};
|
|
use crate::script_implementations::wasm::export_registry::register;
|
|
use crate::script_implementations::wasm::extern_ref::ExternRef;
|
|
use crate::script_implementations::wasm::script::WebAssemblyScript;
|
|
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
|
|
use wasmer::FunctionEnvMut;
|
|
|
|
register! {
|
|
|
|
fn turn_choice_get_user(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> ExternRef<Pokemon> {
|
|
let turn_choice = turn_choice.value_func(&env).unwrap();
|
|
ExternRef::func_new(&env, turn_choice.user().as_ref().deref())
|
|
}
|
|
|
|
fn turn_choice_get_kind(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> u8 {
|
|
match turn_choice.value_func(&env).unwrap() {
|
|
TurnChoice::Move(_) => 0,
|
|
TurnChoice::Item(_) => 1,
|
|
TurnChoice::Switch(_) => 2,
|
|
TurnChoice::Flee(_) => 3,
|
|
TurnChoice::Pass(_) => 4,
|
|
}
|
|
}
|
|
|
|
fn turn_choice_get_speed(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> u32 {
|
|
turn_choice.value_func(&env).unwrap().speed()
|
|
}
|
|
|
|
fn turn_choice_has_failed(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> u8 {
|
|
if turn_choice.value_func(&env).unwrap().has_failed() { 1 } else { 0 }
|
|
}
|
|
|
|
fn turn_choice_fail(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) {
|
|
turn_choice.value_func(&env).unwrap().fail();
|
|
}
|
|
|
|
|
|
fn turn_choice_move_priority(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> i8 {
|
|
if let TurnChoice::Move(d) = turn_choice.value_func(&env).unwrap() {
|
|
return d.priority()
|
|
}
|
|
panic!("Invalid turn choice");
|
|
}
|
|
|
|
fn turn_choice_move_used_move(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> ExternRef<LearnedMove> {
|
|
if let TurnChoice::Move(d) = turn_choice.value_func(&env).unwrap() {
|
|
return ExternRef::func_new(&env, d.used_move().as_ref());
|
|
}
|
|
panic!("Invalid turn choice");
|
|
}
|
|
|
|
fn turn_choice_move_target_side(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> u8 {
|
|
if let TurnChoice::Move(d) = turn_choice.value_func(&env).unwrap() {
|
|
return d.target_side();
|
|
}
|
|
panic!("Invalid turn choice");
|
|
}
|
|
|
|
fn turn_choice_move_target_index(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> u8 {
|
|
if let TurnChoice::Move(d) = turn_choice.value_func(&env).unwrap() {
|
|
return d.target_index();
|
|
}
|
|
panic!("Invalid turn choice");
|
|
}
|
|
|
|
fn turn_choice_move_script(
|
|
env: FunctionEnvMut<WebAssemblyEnv>,
|
|
turn_choice: ExternRef<TurnChoice>,
|
|
) -> u32 {
|
|
if let TurnChoice::Move(d) = turn_choice.value_func(&env).unwrap() {
|
|
if let Some(script) = d.script().get() {
|
|
let read_lock = script.read();
|
|
if let Some(script) = read_lock.as_ref() {
|
|
return script.as_any().downcast_ref::<WebAssemblyScript>().unwrap().get_wasm_pointer()
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
panic!("Invalid turn choice");
|
|
}
|
|
|
|
|
|
}
|