Fin de refactor lua_inter - add_worker et Worker en lua
This commit is contained in:
parent
105095efbc
commit
ac266206bc
3 changed files with 183 additions and 45 deletions
|
@ -9,6 +9,10 @@ machine.pos = pos
|
||||||
mtprint(pos.x)
|
mtprint(pos.x)
|
||||||
machine.pos.x = 50
|
machine.pos.x = 50
|
||||||
machine.storage[0] = 1
|
machine.storage[0] = 1
|
||||||
|
worker = add_worker(30, 10)
|
||||||
|
mtprint(worker.name)
|
||||||
|
worker.pos.x = 25
|
||||||
|
mtprint(worker.pos.x)
|
||||||
|
|
||||||
function on_tick(game)
|
function on_tick(game)
|
||||||
-- Ceci va drainer la sortie de la machine 2
|
-- Ceci va drainer la sortie de la machine 2
|
||||||
|
|
218
src/lua_inter.c
218
src/lua_inter.c
|
@ -42,7 +42,7 @@ void lua_index_custom(lua_State *L, struct TypeMap *map){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char tbuf[256];
|
char tbuf[256];
|
||||||
snprintf(tbuf, 256, "Invalid index for %s", map->type_name);
|
snprintf(tbuf, 256, "Invalid index for %s : \"%s\"", map->type_name, name);
|
||||||
luaL_error(L, tbuf);
|
luaL_error(L, tbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +61,15 @@ void lua_index_i32(lua_State *L, void *base, int offset){
|
||||||
lua_pushinteger(L, *ptr);
|
lua_pushinteger(L, *ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Index a V2d member of a struct != adressing into a V2d
|
||||||
|
void lua_index_V2d(lua_State *L, void *base, int offset){
|
||||||
|
V2d *ptr0 = (void*)(((char*)base) + offset);
|
||||||
|
V2d **ptr1 = lua_newuserdatauv(L, sizeof(void*), 0);
|
||||||
|
*ptr1 = ptr0;
|
||||||
|
luaL_newmetatable(L, "V2d");
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
}
|
||||||
|
|
||||||
void lua_newindex_u8(lua_State *L, void *base, int offset){
|
void lua_newindex_u8(lua_State *L, void *base, int offset){
|
||||||
uint8_t *ptr = (void*)(((char*)base) + offset);
|
uint8_t *ptr = (void*)(((char*)base) + offset);
|
||||||
*ptr = luaL_checkinteger(L, 3);
|
*ptr = luaL_checkinteger(L, 3);
|
||||||
|
@ -76,6 +85,13 @@ void lua_newindex_i32(lua_State *L, void *base, int offset){
|
||||||
*ptr = luaL_checkinteger(L, 3);
|
*ptr = luaL_checkinteger(L, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newindex a V2d member of a struct != adressing into a V2d
|
||||||
|
void lua_newindex_V2d(lua_State *L, void *base, int offset){
|
||||||
|
V2d *dst = (void*)(((char*)base) + offset);
|
||||||
|
V2d **ptr = luaL_checkudata(L, 3, "V2d");
|
||||||
|
memmove(dst, *ptr, sizeof(V2d));
|
||||||
|
}
|
||||||
|
|
||||||
static int lua_V2d_index(lua_State *L){
|
static int lua_V2d_index(lua_State *L){
|
||||||
V2d **ptr = luaL_checkudata(L, 1, "V2d");
|
V2d **ptr = luaL_checkudata(L, 1, "V2d");
|
||||||
V2d *u = *ptr;
|
V2d *u = *ptr;
|
||||||
|
@ -83,11 +99,11 @@ static int lua_V2d_index(lua_State *L){
|
||||||
if(name[1] != '\0')
|
if(name[1] != '\0')
|
||||||
return luaL_error(L, "Invalid index for V2d");
|
return luaL_error(L, "Invalid index for V2d");
|
||||||
if(name[0] == 'x'){
|
if(name[0] == 'x'){
|
||||||
lua_pushinteger(L, u->x);
|
lua_pushnumber(L, u->x);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if(name[0] == 'y'){
|
else if(name[0] == 'y'){
|
||||||
lua_pushinteger(L, u->y);
|
lua_pushnumber(L, u->y);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -99,7 +115,7 @@ static int lua_V2d_newindex(lua_State *L){
|
||||||
V2d **ptr = luaL_checkudata(L, 1, "V2d");
|
V2d **ptr = luaL_checkudata(L, 1, "V2d");
|
||||||
V2d *u = *ptr;
|
V2d *u = *ptr;
|
||||||
const char *name = luaL_checkstring(L, 2);
|
const char *name = luaL_checkstring(L, 2);
|
||||||
float val = luaL_checknumber(L, 3);
|
lua_Number val = luaL_checknumber(L, 3);
|
||||||
if(name[1] != '\0')
|
if(name[1] != '\0')
|
||||||
return luaL_error(L, "Invalid index for V2d");
|
return luaL_error(L, "Invalid index for V2d");
|
||||||
if(name[0] == 'x'){
|
if(name[0] == 'x'){
|
||||||
|
@ -140,22 +156,6 @@ static int lua_storage_newindex(lua_State *L){
|
||||||
return 0;
|
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 lua_machine_storage_index(lua_State *L, void *mach_, int offset){
|
||||||
(void)offset;
|
(void)offset;
|
||||||
Machine *mach = mach_;
|
Machine *mach = mach_;
|
||||||
|
@ -165,6 +165,22 @@ void lua_machine_storage_index(lua_State *L, void *mach_, int offset){
|
||||||
lua_setmetatable(L, -2);
|
lua_setmetatable(L, -2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lua_worker_aiint_index(lua_State *L, void *work_, int offset){
|
||||||
|
(void)offset;
|
||||||
|
Worker *work = work_;
|
||||||
|
AiInternal **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
|
||||||
|
*ptr = &work->ai;
|
||||||
|
luaL_newmetatable(L, "AiInternal");
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_worker_aiint_newindex(lua_State *L, void *work_, int offset){
|
||||||
|
(void)offset;
|
||||||
|
Worker *work = work_;
|
||||||
|
AiInternal **src = luaL_checkudata(L, 3, "AiInternal");
|
||||||
|
memmove(&work->ai, *src, sizeof(AiInternal));
|
||||||
|
}
|
||||||
|
|
||||||
#define OFFSET(field) (size_t)((char*)(BASE.field)-(char*)(BASE))
|
#define OFFSET(field) (size_t)((char*)(BASE.field)-(char*)(BASE))
|
||||||
|
|
||||||
Machine bmach;
|
Machine bmach;
|
||||||
|
@ -185,7 +201,7 @@ struct TypeMap machine_index_map = {
|
||||||
.n_fields = 6,
|
.n_fields = 6,
|
||||||
.field_names = mach_index_names,
|
.field_names = mach_index_names,
|
||||||
.field_affect = {
|
.field_affect = {
|
||||||
{lua_machine_pos_index, 0},
|
{lua_index_V2d, OFFSET(pos)},
|
||||||
{lua_index_u8, OFFSET(type)},
|
{lua_index_u8, OFFSET(type)},
|
||||||
{lua_index_u8, OFFSET(assign_workers)},
|
{lua_index_u8, OFFSET(assign_workers)},
|
||||||
{lua_index_u8, OFFSET(active)},
|
{lua_index_u8, OFFSET(active)},
|
||||||
|
@ -199,7 +215,7 @@ struct TypeMap machine_newindex_map = {
|
||||||
.n_fields = 6,
|
.n_fields = 6,
|
||||||
.field_names = mach_index_names,
|
.field_names = mach_index_names,
|
||||||
.field_affect = {
|
.field_affect = {
|
||||||
{lua_machine_pos_newindex, 0},
|
{lua_newindex_V2d, OFFSET(pos)},
|
||||||
{lua_newindex_u8, OFFSET(type)},
|
{lua_newindex_u8, OFFSET(type)},
|
||||||
{lua_newindex_u8, OFFSET(assign_workers)},
|
{lua_newindex_u8, OFFSET(assign_workers)},
|
||||||
{lua_newindex_u8, OFFSET(active)},
|
{lua_newindex_u8, OFFSET(active)},
|
||||||
|
@ -210,6 +226,100 @@ struct TypeMap machine_newindex_map = {
|
||||||
|
|
||||||
#undef BASE
|
#undef BASE
|
||||||
|
|
||||||
|
AiInternal baiint;
|
||||||
|
|
||||||
|
#define BASE &baiint
|
||||||
|
|
||||||
|
char *aiint_index_names[] = {
|
||||||
|
"status",
|
||||||
|
"carrying",
|
||||||
|
"carry_type",
|
||||||
|
"mtype",
|
||||||
|
"assigned_machine",
|
||||||
|
"assigned_machine1",
|
||||||
|
"dest"
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap aiint_index_map = {
|
||||||
|
.type_name = "AiInternal",
|
||||||
|
.n_fields = 7,
|
||||||
|
.field_names = aiint_index_names,
|
||||||
|
.field_affect = {
|
||||||
|
{lua_index_u8, OFFSET(status)},
|
||||||
|
{lua_index_u8, OFFSET(carrying)},
|
||||||
|
{lua_index_u8, OFFSET(carry_type)},
|
||||||
|
{lua_index_u8, OFFSET(mtype)},
|
||||||
|
{lua_index_u16, OFFSET(assigned_machine)},
|
||||||
|
{lua_index_u16, OFFSET(assigned_machine1)},
|
||||||
|
{lua_index_V2d, OFFSET(dest)}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap aiint_newindex_map = {
|
||||||
|
.type_name = "AiInternal",
|
||||||
|
.n_fields = 7,
|
||||||
|
.field_names = aiint_index_names,
|
||||||
|
.field_affect = {
|
||||||
|
{lua_newindex_u8, OFFSET(status)},
|
||||||
|
{lua_newindex_u8, OFFSET(carrying)},
|
||||||
|
{lua_newindex_u8, OFFSET(carry_type)},
|
||||||
|
{lua_newindex_u8, OFFSET(mtype)},
|
||||||
|
{lua_newindex_u16, OFFSET(assigned_machine)},
|
||||||
|
{lua_newindex_u16, OFFSET(assigned_machine1)},
|
||||||
|
{lua_newindex_V2d, OFFSET(dest)}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef BASE
|
||||||
|
|
||||||
|
Worker bwork;
|
||||||
|
|
||||||
|
#define BASE &bwork
|
||||||
|
|
||||||
|
char *work_index_names[] = {
|
||||||
|
"pos",
|
||||||
|
"ai",
|
||||||
|
"traits",
|
||||||
|
"anim_frame",
|
||||||
|
"assign_n",
|
||||||
|
"satisf",
|
||||||
|
"fatigue",
|
||||||
|
"name"
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap worker_index_map = {
|
||||||
|
.type_name = "Worker",
|
||||||
|
.n_fields = 8,
|
||||||
|
.field_names = work_index_names,
|
||||||
|
.field_affect = {
|
||||||
|
{lua_index_V2d, OFFSET(pos)},
|
||||||
|
{lua_worker_aiint_index, 0},
|
||||||
|
{lua_index_u16, OFFSET(traits)},
|
||||||
|
{lua_index_u8, OFFSET(anim_frame)},
|
||||||
|
{lua_index_u8, OFFSET(assign_n)},
|
||||||
|
{lua_index_u8, OFFSET(satisf)},
|
||||||
|
{lua_index_u8, OFFSET(fatigue)},
|
||||||
|
{lua_index_u8, OFFSET(name)}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap worker_newindex_map = {
|
||||||
|
.type_name = "Worker",
|
||||||
|
.n_fields = 8,
|
||||||
|
.field_names = work_index_names,
|
||||||
|
.field_affect = {
|
||||||
|
{lua_newindex_V2d, OFFSET(pos)},
|
||||||
|
{lua_worker_aiint_newindex, 0},
|
||||||
|
{lua_newindex_u16, OFFSET(traits)},
|
||||||
|
{lua_newindex_u8, OFFSET(anim_frame)},
|
||||||
|
{lua_newindex_u8, OFFSET(assign_n)},
|
||||||
|
{lua_newindex_u8, OFFSET(satisf)},
|
||||||
|
{lua_newindex_u8, OFFSET(fatigue)},
|
||||||
|
{lua_newindex_u8, OFFSET(name)}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef BASE
|
||||||
|
|
||||||
#undef OFFSET
|
#undef OFFSET
|
||||||
|
|
||||||
|
@ -223,15 +333,30 @@ static int lua_machine_newindex(lua_State *L){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int lua_aiint_index(lua_State *L){
|
||||||
|
lua_index_custom(L, &aiint_index_map);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int lua_aiint_newindex(lua_State *L){
|
||||||
|
lua_index_custom(L, &aiint_newindex_map);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int lua_worker_index(lua_State *L){
|
static int lua_worker_index(lua_State *L){
|
||||||
(void)L;
|
lua_index_custom(L, &worker_index_map);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int lua_worker_newindex(lua_State *L){
|
||||||
|
lua_index_custom(L, &worker_newindex_map);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add_machine(x, y)
|
// add_machine(x, y)
|
||||||
static int lua_add_machine(lua_State *L){
|
static int lua_add_machine(lua_State *L){
|
||||||
int x = luaL_checkinteger(L, 1);
|
lua_Number x = luaL_checknumber(L, 1);
|
||||||
int y = luaL_checkinteger(L, 2);
|
lua_Number y = luaL_checknumber(L, 2);
|
||||||
|
|
||||||
Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0);
|
Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0);
|
||||||
*buf = add_machine(&gl_game->machines, (V2d){.x=x,.y=y});
|
*buf = add_machine(&gl_game->machines, (V2d){.x=x,.y=y});
|
||||||
|
@ -258,6 +383,19 @@ static int lua_get_machine_from_id(lua_State *L){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add_worker(x, y)
|
||||||
|
static int lua_add_worker(lua_State *L){
|
||||||
|
lua_Number x = luaL_checknumber(L, 1);
|
||||||
|
lua_Number y = luaL_checknumber(L, 2);
|
||||||
|
Worker *work = add_worker(&gl_game->workers, (V2d){.x=x,.y=y});
|
||||||
|
if(!work)
|
||||||
|
return luaL_error(L, "(lua) Failed to add a worker");
|
||||||
|
Worker **buf = lua_newuserdatauv(L, sizeof(void*), 0);
|
||||||
|
*buf = work;
|
||||||
|
luaL_newmetatable(L, "Worker");
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
}
|
||||||
|
|
||||||
// mtprint(str)
|
// mtprint(str)
|
||||||
static int lua_mtprint(lua_State *L){
|
static int lua_mtprint(lua_State *L){
|
||||||
printf("(lua) %s\n", luaL_checkstring(L, 1));
|
printf("(lua) %s\n", luaL_checkstring(L, 1));
|
||||||
|
@ -274,6 +412,7 @@ struct LuaFn exported_funcs[] = {
|
||||||
{"mtprint", lua_mtprint},
|
{"mtprint", lua_mtprint},
|
||||||
{"add_machine", lua_add_machine},
|
{"add_machine", lua_add_machine},
|
||||||
{"get_machine_from_id", lua_get_machine_from_id},
|
{"get_machine_from_id", lua_get_machine_from_id},
|
||||||
|
{"add_worker", lua_add_worker},
|
||||||
};
|
};
|
||||||
|
|
||||||
int lstate_n;
|
int lstate_n;
|
||||||
|
@ -356,27 +495,22 @@ void lua_onmapload(Game *game, int map){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ADD_TYPE(name, index, newindex) \
|
||||||
|
luaL_newmetatable(L, (name)); \
|
||||||
|
lua_pushcfunction(L, (index)); \
|
||||||
|
lua_setfield(L, -2, "__index"); \
|
||||||
|
lua_pushcfunction(L, (newindex)); \
|
||||||
|
lua_setfield(L, -2, "__newindex")
|
||||||
|
|
||||||
void lua_mt_init(lua_State *L){
|
void lua_mt_init(lua_State *L){
|
||||||
for(size_t 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);
|
lua_register(L, exported_funcs[i].name, exported_funcs[i].fn);
|
||||||
|
|
||||||
luaL_newmetatable(L, "Machine");
|
ADD_TYPE("Machine", lua_machine_index, lua_machine_newindex);
|
||||||
lua_pushcfunction(L, lua_machine_index);
|
ADD_TYPE("V2d", lua_V2d_index, lua_V2d_newindex);
|
||||||
lua_setfield(L, -2, "__index");
|
ADD_TYPE("Storage", lua_storage_index, lua_storage_newindex);
|
||||||
lua_pushcfunction(L, lua_machine_newindex);
|
ADD_TYPE("AiInternal", lua_aiint_index, lua_aiint_newindex);
|
||||||
lua_setfield(L, -2, "__newindex");
|
ADD_TYPE("Worker", lua_worker_index, lua_worker_newindex);
|
||||||
|
|
||||||
luaL_newmetatable(L, "V2d");
|
|
||||||
lua_pushcfunction(L, lua_V2d_index);
|
|
||||||
lua_setfield(L, -2, "__index");
|
|
||||||
lua_pushcfunction(L, lua_V2d_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){
|
||||||
|
|
|
@ -57,11 +57,11 @@ typedef struct {
|
||||||
AiInternal ai;
|
AiInternal ai;
|
||||||
uint16_t traits;
|
uint16_t traits;
|
||||||
uint8_t anim_frame;
|
uint8_t anim_frame;
|
||||||
uint assign_n : 2; // To offset on machines
|
uint8_t assign_n; // To offset on machines
|
||||||
uint satisf : 6;
|
uint8_t satisf;
|
||||||
uint8_t fatigue;
|
uint8_t fatigue;
|
||||||
uint8_t name;
|
uint8_t name;
|
||||||
uint8_t __padding[2];
|
uint8_t __padding[1];
|
||||||
|
|
||||||
} Worker;
|
} Worker;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue