1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-04-20 14:47:45 +00:00

Add a more extensible way to add actions

You now reigster actions with registerAction under Global.lua. This should make
it extremely easy to add new actions later.
This commit is contained in:
Joshua Goins 2025-03-30 09:02:52 -04:00
parent da431cbb02
commit d8627c646e
4 changed files with 58 additions and 15 deletions

View file

@ -3,7 +3,4 @@ function onBeginLogin(player)
player:send_message("Welcome to Kawari!") player:send_message("Welcome to Kawari!")
end end
function doAction(player) registerAction(3, "actions/Sprint.lua")
-- give sprint
player:give_status_effect(50, 5.0)
end

View file

@ -0,0 +1,4 @@
function doAction(player)
-- give sprint
player:give_status_effect(50, 5.0)
end

View file

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use kawari::common::custom_ipc::{CustomIpcData, CustomIpcSegment, CustomIpcType}; use kawari::common::custom_ipc::{CustomIpcData, CustomIpcSegment, CustomIpcType};
@ -33,6 +34,11 @@ use physis::gamedata::GameData;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tokio::net::TcpListener; use tokio::net::TcpListener;
#[derive(Default)]
struct ExtraLuaState {
action_scripts: HashMap<u32, String>,
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
@ -50,7 +56,22 @@ async fn main() {
{ {
let lua = lua.lock().unwrap(); let lua = lua.lock().unwrap();
let file_name = format!("{}/test.lua", &config.world.scripts_location);
let register_action_func = lua
.create_function(|lua, (action_id, action_script): (u32, String)| {
tracing::info!("Registering {action_id} with {action_script}!");
let mut state = lua.app_data_mut::<ExtraLuaState>().unwrap();
let _ = state.action_scripts.insert(action_id, action_script);
Ok(())
})
.unwrap();
lua.set_app_data(ExtraLuaState::default());
lua.globals()
.set("registerAction", register_action_func)
.unwrap();
let file_name = format!("{}/Global.lua", &config.world.scripts_location);
lua.load(std::fs::read(&file_name).expect("Failed to locate scripts directory!")) lua.load(std::fs::read(&file_name).expect("Failed to locate scripts directory!"))
.set_name("@".to_string() + &file_name) .set_name("@".to_string() + &file_name)
.exec() .exec()
@ -757,21 +778,41 @@ async fn main() {
.await; .await;
} }
// run action script
{ {
let lua = lua.lock().unwrap(); let lua = lua.lock().unwrap();
lua.scope(|scope| { let state =
let connection_data = scope lua.app_data_ref::<ExtraLuaState>().unwrap();
.create_userdata_ref_mut(&mut lua_player)
if let Some(action_script) =
state.action_scripts.get(&request.action_id)
{
lua.scope(|scope| {
let connection_data = scope
.create_userdata_ref_mut(&mut lua_player)
.unwrap();
let file_name = format!(
"{}/{}",
&config.world.scripts_location,
action_script
);
lua.load(std::fs::read(&file_name).expect(
"Failed to locate scripts directory!",
))
.set_name("@".to_string() + &file_name)
.exec()
.unwrap(); .unwrap();
let func: Function = let func: Function =
lua.globals().get("doAction").unwrap(); lua.globals().get("doAction").unwrap();
func.call::<()>(connection_data).unwrap(); func.call::<()>(connection_data).unwrap();
Ok(()) Ok(())
}) })
.unwrap(); .unwrap();
}
} }
// tell them the action results // tell them the action results

View file

@ -5,7 +5,8 @@ use rusqlite::Connection;
use crate::{ use crate::{
common::Position, common::Position,
lobby::{ lobby::{
ipc::{CharacterDetails, CharacterFlag}, CharaMake, ClientSelectData, RemakeMode CharaMake, ClientSelectData, RemakeMode,
ipc::{CharacterDetails, CharacterFlag},
}, },
}; };