From 105095efbca48dc038df5bbd6330fd9071bbb3dd Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Mon, 19 May 2025 21:05:29 +0200 Subject: [PATCH] =?UTF-8?q?R=C3=A9ecriture=20des=20types=20lua=20custom=20?= =?UTF-8?q?+=20fixs=20divers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/draw.c | 3 +- src/lua_inter.c | 219 ++++++++++++++++++++++++++++++++---------------- src/ui.c | 9 +- 3 files changed, 155 insertions(+), 76 deletions(-) diff --git a/src/draw.c b/src/draw.c index 4c7ff71..c6d8b4b 100644 --- a/src/draw.c +++ b/src/draw.c @@ -82,7 +82,8 @@ Texture2D mkmap_tex(Map *map){ Rectangle rectb = recta; rectb.x = x*32; rectb.y = y*32; - ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb, WHITE); + ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb, + WHITE); } } Texture2D maptex = LoadTextureFromImage(mapimg); diff --git a/src/lua_inter.c b/src/lua_inter.c index 820b035..f27360a 100644 --- a/src/lua_inter.c +++ b/src/lua_inter.c @@ -13,6 +13,69 @@ Game *gl_game; +typedef void (lua_affect_t)(lua_State*, void *, int); + +struct LuaAffect { + + lua_affect_t *func; + int arg; + +}; + +struct TypeMap { + + char *type_name; + + int n_fields; + char **field_names; + struct LuaAffect field_affect[]; + +}; + +void lua_index_custom(lua_State *L, struct TypeMap *map){ + void **ptr = luaL_checkudata(L, 1, map->type_name); + const char *name = luaL_checkstring(L, 2); + for(int i = 0; i < map->n_fields; i++){ + if(!strcmp(name,map->field_names[i])){ + map->field_affect[i].func(L, *((char**)(ptr)), map->field_affect[i].arg); + return; + } + } + char tbuf[256]; + snprintf(tbuf, 256, "Invalid index for %s", map->type_name); + luaL_error(L, tbuf); +} + +void lua_index_u8(lua_State *L, void *base, int offset){ + uint8_t *ptr = (void*)(((char*)base) + offset); + lua_pushinteger(L, *ptr); +} + +void lua_index_u16(lua_State *L, void *base, int offset){ + uint16_t *ptr = (void*)(((char*)base) + offset); + lua_pushinteger(L, *ptr); +} + +void lua_index_i32(lua_State *L, void *base, int offset){ + int32_t *ptr = (void*)(((char*)base) + offset); + lua_pushinteger(L, *ptr); +} + +void lua_newindex_u8(lua_State *L, void *base, int offset){ + uint8_t *ptr = (void*)(((char*)base) + offset); + *ptr = luaL_checkinteger(L, 3); +} + +void lua_newindex_u16(lua_State *L, void *base, int offset){ + uint16_t *ptr = (void*)(((char*)base) + offset); + *ptr = luaL_checkinteger(L, 3); +} + +void lua_newindex_i32(lua_State *L, void *base, int offset){ + uint16_t *ptr = (void*)(((char*)base) + offset); + *ptr = luaL_checkinteger(L, 3); +} + static int lua_V2d_index(lua_State *L){ V2d **ptr = luaL_checkudata(L, 1, "V2d"); V2d *u = *ptr; @@ -69,90 +132,102 @@ static int lua_storage_newindex(lua_State *L){ uint8_t *storage = *ptr; int pos = luaL_checkinteger(L, 2); int val = luaL_checkinteger(L, 3); - if(pos >= 0 && pos < 5){ + if(pos >= 0 && pos < 5) storage[pos] = val; - } else return luaL_error(L, "Invalid index for Storage"); return 0; } +void lua_machine_pos_index(lua_State *L, void *mach_, int offset){ + (void)offset; + Machine *mach = mach_; + V2d **ptr = lua_newuserdatauv(L, sizeof(void*), 0); + *ptr = &mach->pos; + luaL_newmetatable(L, "V2d"); + lua_setmetatable(L, -2); +} + +void lua_machine_pos_newindex(lua_State *L, void *mach_, int offset){ + (void)offset; + Machine *mach = mach_; + V2d **ptr = luaL_checkudata(L, 3, "V2d"); + memmove(&mach->pos, *ptr, sizeof(V2d)); +} + +void lua_machine_storage_index(lua_State *L, void *mach_, int offset){ + (void)offset; + Machine *mach = mach_; + uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0); + *ptr = mach->storage; + luaL_newmetatable(L, "Storage"); + lua_setmetatable(L, -2); +} + +#define OFFSET(field) (size_t)((char*)(BASE.field)-(char*)(BASE)) + +Machine bmach; + +#define BASE &bmach + +char *mach_index_names[] = { + "pos", + "type", + "assign_workers", + "active", + "storage", + "id" +}; + +struct TypeMap machine_index_map = { + .type_name = "Machine", + .n_fields = 6, + .field_names = mach_index_names, + .field_affect = { + {lua_machine_pos_index, 0}, + {lua_index_u8, OFFSET(type)}, + {lua_index_u8, OFFSET(assign_workers)}, + {lua_index_u8, OFFSET(active)}, + {lua_machine_storage_index, 0}, + {lua_index_u16, OFFSET(id)} + } +}; + +struct TypeMap machine_newindex_map = { + .type_name = "Machine", + .n_fields = 6, + .field_names = mach_index_names, + .field_affect = { + {lua_machine_pos_newindex, 0}, + {lua_newindex_u8, OFFSET(type)}, + {lua_newindex_u8, OFFSET(assign_workers)}, + {lua_newindex_u8, OFFSET(active)}, + {NULL, 0}, // Shouldn't happen - TODO : Fix when it happens + {lua_newindex_u16, OFFSET(id)} + } +}; + +#undef BASE + + +#undef OFFSET + static int lua_machine_index(lua_State *L){ - void **ptr = luaL_checkudata(L, 1, "Machine"); - Machine *mach = *ptr; - const char *name = luaL_checkstring(L, 2); - int ret; - if(!strcmp(name, "pos")){ - V2d **ptr = lua_newuserdatauv(L, sizeof(void*), 0); - *ptr = &mach->pos; - luaL_newmetatable(L, "V2d"); - lua_setmetatable(L, -2); - return 1; - } - else if(!strcmp(name, "type")){ - ret = mach->type; - } - else if(!strcmp(name, "assign_workers")){ - ret = mach->assign_workers; - } - else if(!strcmp(name, "active")){ - ret = mach->active; - } - else if(!strcmp(name, "storage")){ - uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0); - *ptr = mach->storage; - luaL_newmetatable(L, "Storage"); - lua_setmetatable(L, -2); - return 1; - } - else if(!strcmp(name, "id")){ - ret = mach->id; - } - else { - return luaL_error(L, "Invalid index for Machine"); - } - - lua_pushinteger(L, ret); - + lua_index_custom(L, &machine_index_map); return 1; } static int lua_machine_newindex(lua_State *L){ - void **ptr = luaL_checkudata(L, 1, "Machine"); - Machine *mach = *ptr; - const char *name = luaL_checkstring(L, 2); - int prm; - V2d *pos = NULL; - if(lua_isinteger(L, -1)) - prm = luaL_checkinteger(L, 3); - else{ - V2d **ptr = luaL_checkudata(L, 3, "V2d"); - pos = *ptr; - } - if(!strcmp(name, "pos") && pos){ - memmove(&mach->pos, pos, sizeof(V2d)); - return 1; - } - else if(!strcmp(name, "type")){ - mach->type = prm; - } - else if(!strcmp(name, "assign_workers")){ - mach->assign_workers = prm; - } - else if(!strcmp(name, "active")){ - mach->active = prm; - } - else if(!strcmp(name, "id")){ - mach->id = prm; - } - else { - return luaL_error(L, "Invalid index for Machine"); - } - + lua_index_custom(L, &machine_newindex_map); return 0; } +static int lua_worker_index(lua_State *L){ + (void)L; + return 1; +} + // add_machine(x, y) static int lua_add_machine(lua_State *L){ int x = luaL_checkinteger(L, 1); @@ -282,7 +357,7 @@ void lua_onmapload(Game *game, int map){ } void lua_mt_init(lua_State *L){ - for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++) + for(size_t i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++) lua_register(L, exported_funcs[i].name, exported_funcs[i].fn); luaL_newmetatable(L, "Machine"); @@ -308,7 +383,7 @@ void script_init(Game *game){ char tmpbuf[4096]; gl_game = game; - char *cwd = GetApplicationDirectory(); + const char *cwd = GetApplicationDirectory(); snprintf(tmpbuf, 4096, "%sscript", cwd); FilePathList scripts = LoadDirectoryFilesEx(tmpbuf,".lua",true); @@ -318,7 +393,7 @@ void script_init(Game *game){ puts("Fail 318"); return; } - for(int i = 0; i < scripts.count; i++){ + for(uint i = 0; i < scripts.count; i++){ lstates[i] = luaL_newstate(); lua_mt_init(lstates[i]); diff --git a/src/ui.c b/src/ui.c index f36f834..05cf52b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -263,9 +263,12 @@ void init_ui(){ // Android exclusive key -> no exit key SetExitKey(KEY_MENU); - gui_info = (GUIInfo){{&game_widget, &main_menu, &worker_widget, &machine_widget, &pause_menu}, - {false, true, false, false, false}, - NULL}; + gui_info = (GUIInfo){ + {&game_widget, &main_menu, &worker_widget, &machine_widget, &pause_menu}, + {false, true, false, false, false}, + NULL, + NULL + }; } void draw_widgets(Game *game){