diff --git a/Makefile b/Makefile index 2bbe7d2..3edeec2 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ OUTNAME = mtycoon -CFLAGS = -O0 -Wall -Wextra -g -I./raylib/include -I./lua/include -pipe +CFLAGS = -O2 -Wall -Wextra -g -I./raylib/include -I./lua/include -pipe CC = gcc diff --git a/map/converter b/map/converter index 1dc8042..b555b45 100755 Binary files a/map/converter and b/map/converter differ diff --git a/script/test.lua b/script/test.lua index 824dc05..179d947 100644 --- a/script/test.lua +++ b/script/test.lua @@ -22,3 +22,8 @@ end function on_frame(game) end + +function on_load(game, map) + mtprint("loaded map :") + mtprint(map) +end diff --git a/src/game.c b/src/game.c index 2de06e9..fb27e54 100644 --- a/src/game.c +++ b/src/game.c @@ -58,8 +58,6 @@ int init_game(Game *game){ return 1; } - load_map(game, 0); - int c_worker = 0; for(int k = 0; k < 16; k++){ diff --git a/src/lua_inter.c b/src/lua_inter.c index 63ffbc3..c85b0d7 100644 --- a/src/lua_inter.c +++ b/src/lua_inter.c @@ -258,6 +258,29 @@ void lua_onframe(Game *game){ lua_callloop(game, "on_frame"); } +void lua_onmapload(Game *game, int map){ + gl_game = game; + for(int i = 0; i < lstate_n; i++){ + lua_State *L = lstates[i]; + lua_mkgame(L, game); + if(lua_getglobal(L, "on_load") == LUA_TNIL){ + lua_pop(L, 2); + continue; + } + lua_pushvalue(L, -2); + lua_pushinteger(L, map); + int ret = lua_pcall(L, 2, 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_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); diff --git a/src/lua_inter.h b/src/lua_inter.h index e91f19e..3a9982d 100644 --- a/src/lua_inter.h +++ b/src/lua_inter.h @@ -7,6 +7,8 @@ void lua_ontick(Game *game); void lua_onframe(Game *game); +void lua_onmapload(Game *game, int map); + void script_init(); void script_clean(); diff --git a/src/main.c b/src/main.c index 8ef6829..94fb078 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ int init(Game *game){ err |= init_game(game); init_ui(); script_init(game); + load_map(game, 0); return err; } diff --git a/src/map.c b/src/map.c index 8d21738..dba4539 100644 --- a/src/map.c +++ b/src/map.c @@ -10,6 +10,7 @@ #include "types.h" #include "draw.h" +#include "lua_inter.h" extern MapCollec mapcollec; @@ -39,6 +40,9 @@ int load_map(Game *game, int mapn){ return 1; if(generate_coll(game, mapn)) return 1; + + lua_onmapload(game, mapn); + return 0; }