Progrès sur l'interface lua
This commit is contained in:
parent
fd4321605b
commit
687fb81473
6 changed files with 238 additions and 36 deletions
|
@ -1,9 +1,19 @@
|
||||||
mtprint("hello")
|
mtprint("hello")
|
||||||
|
|
||||||
function on_tick()
|
machine = add_machine(10, 10)
|
||||||
mtprint("hello from tick")
|
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
|
end
|
||||||
|
|
||||||
function on_frame()
|
function on_frame(game)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "lua_inter.h"
|
||||||
|
|
||||||
MachineDef machine_dict[] = {
|
MachineDef machine_dict[] = {
|
||||||
{{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Coal, 4}, // MT_CoalMine
|
{{{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)
|
if(game->paused)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lua_onframe();
|
lua_onframe(game);
|
||||||
|
|
||||||
game->ttime += GetFrameTime();
|
game->ttime += GetFrameTime();
|
||||||
|
|
||||||
while(game->ttime > TPS_TIME){
|
while(game->ttime > TPS_TIME){
|
||||||
lua_ontick();
|
lua_ontick(game);
|
||||||
update_machines(game);
|
update_machines(game);
|
||||||
update_workers(game);
|
update_workers(game);
|
||||||
game->ttime -= TPS_TIME;
|
game->ttime -= TPS_TIME;
|
||||||
|
|
222
src/lua_inter.c
222
src/lua_inter.c
|
@ -9,12 +9,156 @@
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
#include "types.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;
|
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){
|
static int lua_mtprint(lua_State *L){
|
||||||
printf("(lua) %s\n", luaL_checkstring(L, 1));
|
printf("(lua) %s\n", luaL_checkstring(L, 1));
|
||||||
|
|
||||||
|
@ -28,40 +172,86 @@ struct LuaFn {
|
||||||
|
|
||||||
struct LuaFn exported_funcs[] = {
|
struct LuaFn exported_funcs[] = {
|
||||||
{"mtprint", lua_mtprint},
|
{"mtprint", lua_mtprint},
|
||||||
|
{"add_machine", lua_add_machine},
|
||||||
|
{"get_machine_from_id", lua_get_machine_from_id},
|
||||||
};
|
};
|
||||||
|
|
||||||
int lstate_n;
|
int lstate_n;
|
||||||
lua_State **lstates;
|
lua_State **lstates;
|
||||||
|
|
||||||
// TODO : Pass main as param
|
void lua_mkgame(lua_State *L, Game *game){
|
||||||
void lua_ontick(){
|
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++){
|
for(int i = 0; i < lstate_n; i++){
|
||||||
lua_State *L = lstates[i];
|
lua_State *L = lstates[i];
|
||||||
lua_getglobal(L, "on_tick");
|
lua_mkgame(L, game);
|
||||||
int ret = lua_pcall(L, 0, 0, 0);
|
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)
|
if(ret)
|
||||||
puts(lua_tostring(L, -1));
|
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(){
|
void lua_ontick(Game *game){
|
||||||
for(int i = 0; i < lstate_n; i++){
|
lua_callloop(game, "on_tick");
|
||||||
lua_State *L = lstates[i];
|
}
|
||||||
lua_getglobal(L, "on_frame");
|
|
||||||
int ret = lua_pcall(L, 0, 0, 0);
|
void lua_onframe(Game *game){
|
||||||
if(ret)
|
lua_callloop(game, "on_frame");
|
||||||
puts(lua_tostring(L, -1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua_mt_init(lua_State *L){
|
void lua_mt_init(lua_State *L){
|
||||||
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
for(int 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");
|
||||||
|
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);
|
FilePathList scripts = LoadDirectoryFilesEx("script",".lua",true);
|
||||||
lstate_n = scripts.count;
|
lstate_n = scripts.count;
|
||||||
lstates = malloc(sizeof(void*)*lstate_n);
|
lstates = malloc(sizeof(void*)*lstate_n);
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
void lua_ontick();
|
void lua_ontick(Game *game);
|
||||||
|
|
||||||
void lua_onframe();
|
void lua_onframe(Game *game);
|
||||||
|
|
||||||
void script_init();
|
void script_init();
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ int init(Game *game){
|
||||||
int err = init_draw();
|
int err = init_draw();
|
||||||
err |= init_game(game);
|
err |= init_game(game);
|
||||||
init_ui();
|
init_ui();
|
||||||
script_init();
|
script_init(game);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
src/types.h
24
src/types.h
|
@ -66,7 +66,7 @@ typedef struct {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
int worker_n;
|
int worker_n; // *
|
||||||
Worker *worker_stack;
|
Worker *worker_stack;
|
||||||
|
|
||||||
} Workers;
|
} Workers;
|
||||||
|
@ -97,7 +97,7 @@ typedef struct {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
int machine_n;
|
int machine_n; // *
|
||||||
int machine_id;
|
int machine_id;
|
||||||
Machine *machine_stack;
|
Machine *machine_stack;
|
||||||
|
|
||||||
|
@ -123,26 +123,26 @@ typedef struct {
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
uint8_t *coll;
|
uint8_t *coll;
|
||||||
int32_t width;
|
int32_t width; // *
|
||||||
int32_t height;
|
int32_t height; // *
|
||||||
Texture2D maptex;
|
Texture2D maptex;
|
||||||
|
|
||||||
} LoadedMap;
|
} LoadedMap;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
Machines machines;
|
Machines machines; // *
|
||||||
Workers workers;
|
Workers workers; // *
|
||||||
LoadedMap map;
|
LoadedMap map; // *
|
||||||
|
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
|
|
||||||
bool paused;
|
bool paused; // *
|
||||||
bool exit;
|
bool exit; // *
|
||||||
|
|
||||||
float ttime;
|
float ttime;
|
||||||
int t_per_frame;
|
int t_per_frame;
|
||||||
|
|
||||||
int64_t cash;
|
int64_t cash; // * - synced
|
||||||
|
|
||||||
} Game;
|
} Game;
|
||||||
|
|
Loading…
Add table
Reference in a new issue