PkmnLib_rs/src/script_implementations/wasm/export_registry/dynamic_data/turn_choice.rs

149 lines
5.2 KiB
Rust
Executable File

use crate::dynamic_data::{LearnedMove, Pokemon, TurnChoice};
use crate::script_implementations::wasm::export_registry::wasm_result::{get_value_arc, get_value_arc_void, wasm_err};
use crate::script_implementations::wasm::export_registry::{
register, wasm_ok, WasmResult, WasmVoidResult, WasmVoidResultExtension,
};
use crate::script_implementations::wasm::extern_ref::ExternRef;
use crate::script_implementations::wasm::script::WebAssemblyScript;
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
use std::ops::Deref;
use wasmer::FunctionEnvMut;
register! {
fn turn_choice_get_user(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<ExternRef<Pokemon>> {
let turn_choice = get_value_arc!(turn_choice, env);
wasm_ok(ExternRef::func_new(&env, turn_choice.user().into()))
}
fn turn_choice_get_kind(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
let turn_choice = get_value_arc!(turn_choice, env);
wasm_ok(match turn_choice.deref() {
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>,
) -> WasmResult<u32> {
let turn_choice = get_value_arc!(turn_choice, env);
wasm_ok(turn_choice.speed())
}
fn turn_choice_has_failed(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
let turn_choice = get_value_arc!(turn_choice, env);
wasm_ok(if turn_choice.has_failed() { 1 } else { 0 })
}
fn turn_choice_fail(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmVoidResult {
let turn_choice = get_value_arc_void!(turn_choice, env);
turn_choice.fail();
WasmVoidResult::ok()
}
fn turn_choice_move_priority(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<i8> {
return match turn_choice.value_func_arc(&env) {
Ok(v) => {
match v.deref() {
TurnChoice::Move(m) => wasm_ok(m.priority()),
_ => wasm_err::<i8>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<i8>(e, &env),
}
}
fn turn_choice_move_used_move(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<ExternRef<LearnedMove>> {
return match turn_choice.value_func_arc(&env) {
Ok(v) => {
match v.deref() {
TurnChoice::Move(m) => wasm_ok(ExternRef::func_new(&env, m.used_move().into())),
_ => wasm_err::<ExternRef<LearnedMove>>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<ExternRef<LearnedMove>>(e, &env),
}
}
fn turn_choice_move_target_side(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
return match turn_choice.value_func_arc(&env) {
Ok(v) => {
match v.deref() {
TurnChoice::Move(m) => wasm_ok(m.target_side()),
_ => wasm_err::<u8>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<u8>(e, &env),
}
}
fn turn_choice_move_target_index(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
return match turn_choice.value_func_arc(&env) {
Ok(v) => {
match v.deref() {
TurnChoice::Move(m) => wasm_ok(m.target_index()),
_ => wasm_err::<u8>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<u8>(e, &env),
}
}
fn turn_choice_move_script(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u32> {
return match turn_choice.value_func_arc(&env) {
Ok(v) => {
match v.deref() {
TurnChoice::Move(d) => {
if let Some(script) = d.script().get() {
let read_lock = script.read();
if let Some(script) = read_lock.as_ref() {
if let Some(v) = script.as_any().downcast_ref::<WebAssemblyScript>(){
return wasm_ok(v.get_wasm_pointer())
}
}
}
return wasm_ok(0);
},
_ => wasm_err::<u32>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<u32>(e, &env),
}
}
}