use crate::dynamic_data::{LearnedMove, Pokemon, TurnChoice}; use crate::script_implementations::wasm::export_registry::wasm_result::{ get_value, get_value_call_getter, get_value_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 anyhow_ext::Context; use wasmer::FunctionEnvMut; register! { fn turn_choice_get_user( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult> { let user = get_value_call_getter!(turn_choice.user(), &env); wasm_ok(ExternRef::func_new(&env, user)) } fn turn_choice_get_kind( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { let turn_choice = get_value!(turn_choice, env); wasm_ok(match turn_choice { 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 { wasm_ok(get_value_call_getter!(turn_choice.speed(), &env)) } fn turn_choice_has_failed( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmResult { wasm_ok(if get_value_call_getter!(turn_choice.has_failed(), &env) { 1 } else { 0 }) } fn turn_choice_fail( env: FunctionEnvMut, turn_choice: ExternRef, ) -> WasmVoidResult { let turn_choice = get_value_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(&env) { Ok(v) => { match v { 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(&env) { Ok(v) => { match v { TurnChoice::Move(m) => wasm_ok(ExternRef::::func_new(&env, m.used_move())), _ => 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(&env) { Ok(v) => { match v { 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(&env) { Ok(v) => { match v { 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(&env) { Ok(v) => { match v { 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), } } }