All current lua hooks are being called

This commit is contained in:
Kayne Ruse
2014-06-23 04:10:21 +10:00
parent 64baa63d12
commit 46ed196bf4
4 changed files with 55 additions and 11 deletions
+1
View File
@@ -41,6 +41,7 @@ public:
void UnloadAll() override; void UnloadAll() override;
//accessors & mutators
std::string SetDirectory(std::string s) { return directory = s; } std::string SetDirectory(std::string s) { return directory = s; }
std::string GetDirectory() { return directory; } std::string GetDirectory() { return directory; }
+51 -10
View File
@@ -38,12 +38,7 @@ int RoomManager::CreateRoom(MapType mapType) {
//create the room //create the room
RoomData* newRoom = new RoomData(); RoomData* newRoom = new RoomData();
//set the state //create the generator, use a lambda because I'm lazy
if (luaState) {
newRoom->pager.SetLuaState(luaState);
}
//create the generator
newRoom->generator = [mapType]() -> BaseGenerator* { newRoom->generator = [mapType]() -> BaseGenerator* {
switch(mapType) { switch(mapType) {
case MapType::NONE: //use overworld as a default case MapType::NONE: //use overworld as a default
@@ -56,17 +51,47 @@ int RoomManager::CreateRoom(MapType mapType) {
throw(std::runtime_error("Failed to set the room's generator")); throw(std::runtime_error("Failed to set the room's generator"));
}(); }();
//set the state
if (luaState) {
newRoom->pager.SetLuaState(luaState);
newRoom->generator->SetLuaState(luaState);
}
//register the room
roomMap[counter++] = newRoom;
//TODO: pass the room's index to the lua hooks?
//API hook
lua_getglobal(luaState, "Room");
lua_getfield(luaState, -1, "OnCreate");
lua_pushlightuserdata(luaState, newRoom);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
}
lua_pop(luaState, 1);
//finish the routine //finish the routine
roomMap[counter] = newRoom; return counter;
return counter++;
} }
int RoomManager::UnloadRoom(int uid) { int RoomManager::UnloadRoom(int uid) {
//find the room
RoomData* room = FindRoom(uid); RoomData* room = FindRoom(uid);
if (!room) { if (!room) {
return -1; return -1;
} }
//API hook
lua_getglobal(luaState, "Room");
lua_getfield(luaState, -1, "OnUnload");
lua_pushlightuserdata(luaState, room);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
}
lua_pop(luaState, 1);
//free the memory
delete room->generator; delete room->generator;
delete room; delete room;
roomMap.erase(uid); roomMap.erase(uid);
@@ -90,6 +115,22 @@ RoomData* RoomManager::FindRoom(int uid) {
} }
int RoomManager::PushRoom(RoomData* room) { int RoomManager::PushRoom(RoomData* room) {
roomMap[counter] = room; roomMap[counter++] = room;
return counter++; return counter;
}
void RoomManager::UnloadAll() {
lua_getglobal(luaState, "Room");
for (auto& it : roomMap) {
//API hook
lua_getfield(luaState, -1, "OnUnload");
lua_pushlightuserdata(luaState, it.second);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
}
}
lua_pop(luaState, 1);
roomMap.clear();
} }
+2
View File
@@ -43,6 +43,8 @@ public:
RoomData* FindRoom(int uid); RoomData* FindRoom(int uid);
int PushRoom(RoomData*); int PushRoom(RoomData*);
void UnloadAll();
//accessors and mutators //accessors and mutators
std::map<int, RoomData*>* GetContainer() { return &roomMap; } std::map<int, RoomData*>* GetContainer() { return &roomMap; }
+1 -1
View File
@@ -157,7 +157,7 @@ void ServerApplication::Quit() {
characterMgr.UnloadAll(); characterMgr.UnloadAll();
//TODO: unload combats //TODO: unload combats
//TODO: unload enemies //TODO: unload enemies
//TODO: unload rooms roomMgr.UnloadAll();
//APIs //APIs
lua_close(luaState); lua_close(luaState);