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, turn_choice: ExternRef, ) -> WasmResult> { 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, turn_choice: ExternRef, ) -> WasmResult { 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, turn_choice: ExternRef, ) -> WasmResult { let turn_choice = get_value_arc!(turn_choice, env); wasm_ok(turn_choice.speed()) } fn turn_choice_has_failed( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { 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, turn_choice: ExternRef, ) -> WasmVoidResult { let turn_choice = get_value_arc_void!(turn_choice, env); turn_choice.fail(); WasmVoidResult::ok() } fn turn_choice_move_priority( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { return match turn_choice.value_func_arc(&env) { Ok(v) => { match v.deref() { TurnChoice::Move(m) => wasm_ok(m.priority()), _ => wasm_err::(anyhow_ext::anyhow!("Invalid turn choice"), &env) } }, Err(e) => wasm_err::(e, &env), } } fn turn_choice_move_used_move( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult> { 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::>(anyhow_ext::anyhow!("Invalid turn choice"), &env) } }, Err(e) => wasm_err::>(e, &env), } } fn turn_choice_move_target_side( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { return match turn_choice.value_func_arc(&env) { Ok(v) => { match v.deref() { TurnChoice::Move(m) => wasm_ok(m.target_side()), _ => wasm_err::(anyhow_ext::anyhow!("Invalid turn choice"), &env) } }, Err(e) => wasm_err::(e, &env), } } fn turn_choice_move_target_index( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { return match turn_choice.value_func_arc(&env) { Ok(v) => { match v.deref() { TurnChoice::Move(m) => wasm_ok(m.target_index()), _ => wasm_err::(anyhow_ext::anyhow!("Invalid turn choice"), &env) } }, Err(e) => wasm_err::(e, &env), } } fn turn_choice_move_script( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { 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::(){ return wasm_ok(v.get_wasm_pointer()) } } } return wasm_ok(0); }, _ => wasm_err::(anyhow_ext::anyhow!("Invalid turn choice"), &env) } }, Err(e) => wasm_err::(e, &env), } } }