Progrès sur l'interface lua

This commit is contained in:
attilavs2 2025-04-11 22:22:37 +02:00
parent fd4321605b
commit 687fb81473
6 changed files with 238 additions and 36 deletions

View file

@ -1,9 +1,19 @@
mtprint("hello")
function on_tick()
mtprint("hello from tick")
machine = add_machine(10, 10)
mtprint(machine.id)
machine.type = 2
mtprint(machine.type)
pos = machine.pos
machine.pos = pos
mtprint(pos.x)
machine.pos.x = 50
machine.storage[0] = 1
function on_tick(game)
end
function on_frame()
function on_frame(game)
end

View file

@ -11,6 +11,7 @@
#include "types.h"
#include "map.h"
#include "game.h"
#include "lua_inter.h"
MachineDef machine_dict[] = {
{{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Coal, 4}, // MT_CoalMine
@ -325,12 +326,12 @@ void update(Game *game){
if(game->paused)
return;
lua_onframe();
lua_onframe(game);
game->ttime += GetFrameTime();
while(game->ttime > TPS_TIME){
lua_ontick();
lua_ontick(game);
update_machines(game);
update_workers(game);
game->ttime -= TPS_TIME;

View file

@ -9,12 +9,156 @@
#include <lauxlib.h>
#include "types.h"
#include "game.h"
static int lua_add_machine(lua_State *L){
Game *gl_game;
static int lua_V2d_index(lua_State *L){
V2d **ptr = luaL_checkudata(L, 1, "V2d");
V2d *u = *ptr;
const char *name = luaL_checkstring(L, 2);
if(name[1] != '\0')
return luaL_error(L, "Invalid index for V2d");
if(name[0] == 'x'){
lua_pushinteger(L, u->x);
return 1;
}
else if(name[0] == 'y'){
lua_pushinteger(L, u->y);
return 1;
}
else {
return luaL_error(L, "Invalid index for V2d");
}
}
static int lua_V2d_newindex(lua_State *L){
V2d **ptr = luaL_checkudata(L, 1, "V2d");
V2d *u = *ptr;
const char *name = luaL_checkstring(L, 2);
float val = luaL_checknumber(L, 3);
if(name[1] != '\0')
return luaL_error(L, "Invalid index for V2d");
if(name[0] == 'x'){
u->x = val;
}
else if(name[0] == 'y'){
u->y = val;
}
else {
return luaL_error(L, "Invalid index for V2d");
}
return 0;
}
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")){
lua_createtable(L, 5, 0);
for(int i = 0; i < 5; i++){
lua_pushinteger(L, mach->storage[i]);
lua_seti(L, -2, i);
}
return 1;
}
else if(!strcmp(name, "id")){
ret = mach->id;
}
else {
return luaL_error(L, "Invalid index for Machine");
}
lua_pushinteger(L, ret);
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");
}
return 0;
}
// add_machine(x, y)
static int lua_add_machine(lua_State *L){
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0);
*buf = add_machine(&gl_game->machines, (V2d){.x=x,.y=y});
if(!(*buf))
return luaL_error(L, "(lua) Failed to add a machine");
luaL_newmetatable(L, "Machine");
lua_setmetatable(L, -2);
return 1;
}
// get_machine_from_id(id)
static int lua_get_machine_from_id(lua_State *L){
int id = luaL_checkinteger(L, 1) & 0xFFFF;
Machine *mach = get_machine_from_id(&gl_game->machines, id);
if(!mach)
return 0;
Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0);
*buf = mach;
luaL_newmetatable(L, "Machine");
lua_setmetatable(L, -2);
return 1;
}
// mtprint(str)
static int lua_mtprint(lua_State *L){
printf("(lua) %s\n", luaL_checkstring(L, 1));
@ -28,40 +172,86 @@ struct LuaFn {
struct LuaFn exported_funcs[] = {
{"mtprint", lua_mtprint},
{"add_machine", lua_add_machine},
{"get_machine_from_id", lua_get_machine_from_id},
};
int lstate_n;
lua_State **lstates;
// TODO : Pass main as param
void lua_ontick(){
void lua_mkgame(lua_State *L, Game *game){
lua_createtable(L, 0, 9); // game
lua_createtable(L, 0, 1); // machines
lua_pushinteger(L,game->machines.machine_n);
lua_setfield(L, -2, "machine_n");
lua_setfield(L, -2, "machines");
lua_createtable(L, 0, 1); // workers
lua_pushinteger(L,game->workers.worker_n);
lua_setfield(L, -2, "worker_n");
lua_setfield(L, -2, "workers");
lua_createtable(L, 0, 2); // map
lua_pushinteger(L,game->map.width);
lua_setfield(L, -2, "width");
lua_pushinteger(L,game->map.height);
lua_setfield(L, -2, "height");
lua_setfield(L, -2, "map");
lua_pushinteger(L,game->paused);
lua_setfield(L, -2, "paused");
lua_pushinteger(L,game->exit);
lua_setfield(L, -2, "exit");
lua_pushinteger(L,game->cash);
lua_setfield(L, -2, "cash");
}
void lua_callloop(Game *game, char *fnname){
gl_game = game;
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);
lua_mkgame(L, game);
if(lua_getglobal(L, fnname) == LUA_TNIL){
lua_pop(L, 2);
continue;
}
lua_pushvalue(L, -2);
int ret = lua_pcall(L, 1, 0, 0);
if(ret)
puts(lua_tostring(L, -1));
else{
lua_pushliteral(L, "cash");
lua_gettable(L, -2);
if(lua_isinteger(L, -1))
game->cash = lua_tointeger(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_ontick(Game *game){
lua_callloop(game, "on_tick");
}
void lua_onframe(Game *game){
lua_callloop(game, "on_frame");
}
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);
luaL_newmetatable(L, "Machine");
lua_pushcfunction(L, lua_machine_index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_machine_newindex);
lua_setfield(L, -2, "__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");
}
void script_init(){
void script_init(Game *game){
gl_game = game;
FilePathList scripts = LoadDirectoryFilesEx("script",".lua",true);
lstate_n = scripts.count;
lstates = malloc(sizeof(void*)*lstate_n);

View file

@ -1,10 +1,11 @@
#include <lua.h>
#include "types.h"
#pragma once
void lua_ontick();
void lua_ontick(Game *game);
void lua_onframe();
void lua_onframe(Game *game);
void script_init();

View file

@ -29,7 +29,7 @@ int init(Game *game){
int err = init_draw();
err |= init_game(game);
init_ui();
script_init();
script_init(game);
return err;
}

View file

@ -66,7 +66,7 @@ typedef struct {
typedef struct {
int worker_n;
int worker_n; // *
Worker *worker_stack;
} Workers;
@ -97,7 +97,7 @@ typedef struct {
typedef struct {
int machine_n;
int machine_n; // *
int machine_id;
Machine *machine_stack;
@ -123,26 +123,26 @@ typedef struct {
typedef struct {
uint8_t *coll;
int32_t width;
int32_t height;
int32_t width; // *
int32_t height; // *
Texture2D maptex;
} LoadedMap;
typedef struct {
Machines machines;
Workers workers;
LoadedMap map;
Machines machines; // *
Workers workers; // *
LoadedMap map; // *
Camera2D camera;
bool paused;
bool exit;
bool paused; // *
bool exit; // *
float ttime;
int t_per_frame;
float ttime;
int t_per_frame;
int64_t cash;
int64_t cash; // * - synced
} Game;