From fd4321605badfbf7842e14939bc090101da10bb0 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Fri, 11 Apr 2025 16:17:46 +0200 Subject: [PATCH] Interface Lua complete --- script/test.lua | 10 +++++++- src/game.c | 3 +++ src/lua_inter.c | 68 ++++++++++++++++++++++++++++++++++--------------- src/lua_inter.h | 4 +++ 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/script/test.lua b/script/test.lua index 11b15b1..c8d7c72 100644 --- a/script/test.lua +++ b/script/test.lua @@ -1 +1,9 @@ -print("hello") +mtprint("hello") + +function on_tick() + mtprint("hello from tick") +end + +function on_frame() + +end diff --git a/src/game.c b/src/game.c index 08e437f..d607654 100644 --- a/src/game.c +++ b/src/game.c @@ -325,9 +325,12 @@ void update(Game *game){ if(game->paused) return; + lua_onframe(); + game->ttime += GetFrameTime(); while(game->ttime > TPS_TIME){ + lua_ontick(); update_machines(game); update_workers(game); game->ttime -= TPS_TIME; diff --git a/src/lua_inter.c b/src/lua_inter.c index ebe9183..f5b18a7 100644 --- a/src/lua_inter.c +++ b/src/lua_inter.c @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -9,14 +10,6 @@ #include "types.h" -void lua_ontick(){ - -} - -void lua_onframe(){ - -} - static int lua_add_machine(lua_State *L){ return 0; @@ -34,27 +27,60 @@ struct LuaFn { }; struct LuaFn exported_funcs[] = { - {"mtprint", lua_mtprint} + {"mtprint", lua_mtprint}, + }; -struct load_dat { - char *str; - int pos; - int len; -}; +int lstate_n; +lua_State **lstates; -lua_State *gl_L; +// TODO : Pass main as param +void lua_ontick(){ + for(int i = 0; i < lstate_n; i++){ + lua_State *L = lstates[i]; + lua_getglobal(L, "on_tick"); + int ret = lua_pcall(L, 0, 0, 0); + if(ret) + puts(lua_tostring(L, -1)); + } +} + +void lua_onframe(){ + for(int i = 0; i < lstate_n; i++){ + lua_State *L = lstates[i]; + lua_getglobal(L, "on_frame"); + int ret = lua_pcall(L, 0, 0, 0); + if(ret) + puts(lua_tostring(L, -1)); + } +} + +void lua_mt_init(lua_State *L){ + for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++) + lua_register(L, exported_funcs[i].name, exported_funcs[i].fn); +} void script_init(){ - gl_L = luaL_newstate(); - for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++) - lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn); + FilePathList scripts = LoadDirectoryFilesEx("script",".lua",true); + lstate_n = scripts.count; + lstates = malloc(sizeof(void*)*lstate_n); + if(!lstates){ + return; + } + for(int i = 0; i < scripts.count; i++){ + lstates[i] = luaL_newstate(); + lua_mt_init(lstates[i]); - luaL_loadstring(gl_L, "mtprint(\"hello world\")\n"); - lua_pcall(gl_L, 0, 0, 0); + printf("Loading script \"%s\"...\n", scripts.paths[i]); + int ret = luaL_dofile(lstates[i], scripts.paths[i]); + if(ret) + puts(lua_tostring(lstates[i], -1)); + } } void script_clean(){ - lua_close(gl_L); + for(int i = 0; i < lstate_n; i++) + lua_close(lstates[i]); + free(lstates); } diff --git a/src/lua_inter.h b/src/lua_inter.h index 332d323..c21e438 100644 --- a/src/lua_inter.h +++ b/src/lua_inter.h @@ -2,6 +2,10 @@ #pragma once +void lua_ontick(); + +void lua_onframe(); + void script_init(); void script_clean();