From e19b6fbc23974ea17f39add6ddcb56caa3747cd8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 19:02:43 +1000 Subject: [PATCH] Updated RoomManager and the API --- server/rooms/room_manager.cpp | 67 +++++++++++++++++++++++------------ server/rooms/room_manager.hpp | 7 ++-- server/rooms/room_mgr_api.cpp | 19 ++++++++-- 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 8f60ba4..cc0b330 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -21,37 +21,64 @@ */ #include "room_manager.hpp" +//the generator types +#include "overworld_generator.hpp" +#include "ruins_generator.hpp" +#include "towers_generator.hpp" +#include "forests_generator.hpp" +#include "caves_generator.hpp" + #include //------------------------- //public access methods //------------------------- -RoomData* RoomManager::CreateRoom(int uid) { - //don't overwrite existing rooms - std::map::iterator it = roomMap.find(uid); - if (it != roomMap.end()) { - throw(std::runtime_error("Cannot overwrite an existing room")); - } - roomMap[uid] = new RoomData(); - //TODO: create room in the API +int RoomManager::CreateRoom(MapType mapType) { + //create the room + RoomData* newRoom = new RoomData(); + + //set the state if (luaState) { - roomMap[uid]->pager.SetLuaState(luaState); + newRoom->pager.SetLuaState(luaState); } - return roomMap[uid]; + + //create the generator + newRoom->generator = [mapType]() -> BaseGenerator* { + switch(mapType) { + case MapType::NONE: //use overworld as a default + case MapType::OVERWORLD: return new OverworldGenerator(); + case MapType::RUINS: return new RuinsGenerator(); + case MapType::TOWERS: return new TowersGenerator(); + case MapType::FORESTS: return new ForestsGenerator(); + case MapType::CAVES: return new CavesGenerator(); + } + throw(std::runtime_error("Failed to set the room's generator")); + }(); + + //finish the routine + roomMap[counter] = newRoom; + return counter++; } -RoomData* RoomManager::UnloadRoom(int uid) { - //TODO: unload room in the API - delete roomMap[uid]; +int RoomManager::UnloadRoom(int uid) { + RoomData* room = FindRoom(uid); + if (!room) { + return -1; + } + + delete room->generator; + delete room; roomMap.erase(uid); + + return 0; } RoomData* RoomManager::GetRoom(int uid) { RoomData* ptr = FindRoom(uid); if (ptr) return ptr; - ptr = CreateRoom(uid); - return ptr; + int newIndex = CreateRoom(MapType::NONE); + return FindRoom(newIndex); } RoomData* RoomManager::FindRoom(int uid) { @@ -62,11 +89,7 @@ RoomData* RoomManager::FindRoom(int uid) { return it->second; } -RoomData* RoomManager::PushRoom(int uid, RoomData* room) { - //unload existing rooms with this index - std::map::iterator it = roomMap.find(uid); - if (it != roomMap.end()) { - UnloadRoom(uid); - } - roomMap[uid] = room; +int RoomManager::PushRoom(RoomData* room) { + roomMap[counter] = room; + return counter++; } diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 80480f7..30129c1 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -36,12 +36,12 @@ public: ~RoomManager() = default; //public access methods - RoomData* CreateRoom(int uid); - RoomData* UnloadRoom(int uid); + int CreateRoom(MapType); + int UnloadRoom(int uid); RoomData* GetRoom(int uid); RoomData* FindRoom(int uid); - RoomData* PushRoom(int uid, RoomData*); + int PushRoom(RoomData*); //accessors and mutators std::map* GetContainer() { return &roomMap; } @@ -52,6 +52,7 @@ public: private: std::map roomMap; lua_State* luaState = nullptr; + int counter = 0; }; #endif \ No newline at end of file diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp index 6539002..0e58729 100644 --- a/server/rooms/room_mgr_api.cpp +++ b/server/rooms/room_mgr_api.cpp @@ -24,6 +24,8 @@ #include "room_manager.hpp" #include "room_data.hpp" +#include + static int getRoom(lua_State* L) { //get the room manager lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); @@ -41,9 +43,22 @@ static int createRoom(lua_State* L) { lua_gettable(L, LUA_REGISTRYINDEX); RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); - //TODO: create room + //determine the specified room type + MapType mapType = [L]() -> MapType { + if (std::string("overworld") == lua_tostring(L, -2)) return MapType::OVERWORLD; + if (std::string("ruins") == lua_tostring(L, -2)) return MapType::RUINS; + if (std::string("towers") == lua_tostring(L, -2)) return MapType::TOWERS; + if (std::string("forests") == lua_tostring(L, -2)) return MapType::FORESTS; + if (std::string("caves") == lua_tostring(L, -2)) return MapType::CAVES; + return MapType::NONE; + }(); - return 0; + //create the room + int newIndex = roomMgr->CreateRoom(mapType); + + //return the index + lua_pushinteger(L, newIndex); + return 1; } static int unloadRoom(lua_State* L) {