int lua_mapgen(map * m, std::string terrain_type, mapgendata md, int t, float d, const std::string & scr) { lua_State* L = lua_state; { map** map_userdata = (map**) lua_newuserdata(L, sizeof(map*)); *map_userdata = m; luah_setmetatable(L, "map_metatable"); luah_setglobal(L, "map", -1); } int err = luaL_loadstring(L, scr.c_str() ); if(err) { // Error handling. const char* error = lua_tostring(L, -1); debugmsg("Error loading lua mapgen: %s", error); return err; } // int function_index = luaL_ref(L, LUA_REGISTRYINDEX); // todo; make use of this // lua_rawgeti(L, LUA_REGISTRYINDEX, function_index); lua_pushstring(L, terrain_type.c_str()); lua_setglobal(L, "tertype"); lua_pushinteger(L, t); lua_setglobal(L, "turn"); err=lua_pcall(L, 0 , LUA_MULTRET, 0); if(err) { // Error handling. const char* error = lua_tostring(L, -1); debugmsg("Error running lua mapgen: %s", error); } // luah_remove_from_registry(L, function_index); // todo: make use of this return err; }
int lua_mapgen(map *m, std::string terrain_type, mapgendata, int t, float, const std::string &scr) { if( lua_state == nullptr ) { return 0; } lua_State *L = lua_state; { map **map_userdata = (map **) lua_newuserdata(L, sizeof(map *)); *map_userdata = m; luah_setmetatable(L, "map_metatable"); luah_setglobal(L, "map", -1); } int err = luaL_loadstring(L, scr.c_str() ); if( lua_report_error( L, err, scr.c_str() ) ) { return err; } // int function_index = luaL_ref(L, LUA_REGISTRYINDEX); // todo; make use of this // lua_rawgeti(L, LUA_REGISTRYINDEX, function_index); lua_pushstring(L, terrain_type.c_str()); lua_setglobal(L, "tertype"); lua_pushinteger(L, t); lua_setglobal(L, "turn"); err = lua_pcall(L, 0 , LUA_MULTRET, 0); lua_report_error( L, err, scr.c_str() ); // luah_remove_from_registry(L, function_index); // todo: make use of this return err; }
void update_globals(lua_State *L) { // Make sure the player reference is up to date. { player** player_userdata = (player**) lua_newuserdata(L, sizeof(player*)); *player_userdata = &g->u; // Set the metatable for the player. luah_setmetatable(L, "player_metatable"); luah_setglobal(L, "player", -1); } // Make sure the map reference is up to date. { map** map_userdata = (map**) lua_newuserdata(L, sizeof(map*)); *map_userdata = &g->m; // Set the metatable for the player. luah_setmetatable(L, "map_metatable"); luah_setglobal(L, "map", -1); } }