Petit fix dans l'interface lua

This commit is contained in:
attilavs2 2025-04-12 13:57:32 +02:00
parent 687fb81473
commit efd52dae7d
2 changed files with 42 additions and 6 deletions

View file

@ -11,7 +11,12 @@ machine.pos.x = 50
machine.storage[0] = 1 machine.storage[0] = 1
function on_tick(game) function on_tick(game)
-- Ceci va drainer la sortie de la machine 2
-- (Le premier fourneau)
local mach = get_machine_from_id(2)
if mach.storage[4] > 0 then
mach.storage[4] = mach.storage[4]-1
end
end end
function on_frame(game) function on_frame(game)

View file

@ -52,6 +52,32 @@ static int lua_V2d_newindex(lua_State *L){
return 0; return 0;
} }
static int lua_storage_index(lua_State *L){
uint8_t **ptr = luaL_checkudata(L, 1, "Storage");
uint8_t *storage = *ptr;
int pos = luaL_checkinteger(L, 2);
if(pos >= 0 && pos < 5){
lua_pushinteger(L, storage[pos]);
return 1;
}
else
return 0;
}
static int lua_storage_newindex(lua_State *L){
uint8_t **ptr = luaL_checkudata(L, 1, "Storage");
uint8_t *storage = *ptr;
int pos = luaL_checkinteger(L, 2);
int val = luaL_checkinteger(L, 3);
if(pos >= 0 && pos < 5){
storage[pos] = val;
}
else
return luaL_error(L, "Invalid index for Storage");
return 0;
}
static int lua_machine_index(lua_State *L){ static int lua_machine_index(lua_State *L){
void **ptr = luaL_checkudata(L, 1, "Machine"); void **ptr = luaL_checkudata(L, 1, "Machine");
Machine *mach = *ptr; Machine *mach = *ptr;
@ -74,11 +100,10 @@ static int lua_machine_index(lua_State *L){
ret = mach->active; ret = mach->active;
} }
else if(!strcmp(name, "storage")){ else if(!strcmp(name, "storage")){
lua_createtable(L, 5, 0); uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
for(int i = 0; i < 5; i++){ *ptr = mach->storage;
lua_pushinteger(L, mach->storage[i]); luaL_newmetatable(L, "Storage");
lua_seti(L, -2, i); lua_setmetatable(L, -2);
}
return 1; return 1;
} }
else if(!strcmp(name, "id")){ else if(!strcmp(name, "id")){
@ -248,6 +273,12 @@ void lua_mt_init(lua_State *L){
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_V2d_newindex); lua_pushcfunction(L, lua_V2d_newindex);
lua_setfield(L, -2, "__newindex"); lua_setfield(L, -2, "__newindex");
luaL_newmetatable(L, "Storage");
lua_pushcfunction(L, lua_storage_index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_storage_newindex);
lua_setfield(L, -2, "__newindex");
} }
void script_init(Game *game){ void script_init(Game *game){