From b391dde08926e52c616d5ab59f0243c9fcc428cf Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 9 Jan 2015 13:21:09 +1100 Subject: [PATCH] WaypointManager is no longer a Singleton, wrote waypoint API outline I'm planning on giving each room it's own waypoint manager, so it can compare it's waypoints against the characters in that room alone. If it turns out to be a good pattern, I'll do thae same for monsters. --- server/main.cpp | 2 - server/server_application.hpp | 2 - server/server_logic.cpp | 2 - server/waypoints/waypoint_api.cpp | 94 ++++++++++++++++++++++++++- server/waypoints/waypoint_data.hpp | 12 ++-- server/waypoints/waypoint_manager.cpp | 49 ++++++++++---- server/waypoints/waypoint_manager.hpp | 22 +++---- todo.txt | 1 + 8 files changed, 146 insertions(+), 38 deletions(-) diff --git a/server/main.cpp b/server/main.cpp index a86f4f0..befb95a 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -46,7 +46,6 @@ int main(int argc, char* argv[]) { MonsterManager::CreateSingleton(); RoomManager::CreateSingleton(); UDPNetworkUtility::CreateSingleton(); - WaypointManager::CreateSingleton(); //call the server's routines ServerApplication::CreateSingleton(); @@ -66,7 +65,6 @@ int main(int argc, char* argv[]) { MonsterManager::DeleteSingleton(); RoomManager::DeleteSingleton(); UDPNetworkUtility::DeleteSingleton(); - WaypointManager::DeleteSingleton(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; diff --git a/server/server_application.hpp b/server/server_application.hpp index f8c6d32..aa26406 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -28,7 +28,6 @@ #include "client_manager.hpp" #include "monster_manager.hpp" #include "room_manager.hpp" -#include "waypoint_manager.hpp" //utilities #include "config_utility.hpp" @@ -120,7 +119,6 @@ private: ClientManager& clientMgr = ClientManager::GetSingleton(); MonsterManager& monsterMgr = MonsterManager::GetSingleton(); RoomManager& roomMgr = RoomManager::GetSingleton(); - WaypointManager& waypointMgr = WaypointManager::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton(); UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); diff --git a/server/server_logic.cpp b/server/server_logic.cpp index ab3eb2b..8a3505b 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -105,7 +105,6 @@ void ServerApplication::Init(int argc, char* argv[]) { characterMgr.SetDatabase(database); roomMgr.SetLuaState(luaState); - waypointMgr.SetLuaState(luaState); std::cout << "Internal managers initialized" << std::endl; @@ -204,7 +203,6 @@ void ServerApplication::Quit() { clientMgr.UnloadAll(); monsterMgr.UnloadAll(); roomMgr.UnloadAll(); - waypointMgr.UnloadAll(); //APIs lua_close(luaState); diff --git a/server/waypoints/waypoint_api.cpp b/server/waypoints/waypoint_api.cpp index 6059f69..34a6274 100644 --- a/server/waypoints/waypoint_api.cpp +++ b/server/waypoints/waypoint_api.cpp @@ -23,8 +23,100 @@ #include "waypoint_data.hpp" -//TODO: Can I alias the entity API for this? +//origin +static int setOriginX(lua_State* L) { + //TODO + return 0; +} + +static int setOriginY(lua_State* L) { + //TODO + return 0; +} + +static int getOriginX(lua_State* L) { + //TODO + return 0; +} + +static int getOriginY(lua_State* L) { + //TODO + return 0; +} + +//bounds +static int setBoundingBoxX(lua_State* L) { + //TODO + return 0; +} + +static int setBoundingBoxY(lua_State* L) { + //TODO + return 0; +} + +static int setBoundingBoxW(lua_State* L) { + //TODO + return 0; +} + +static int setBoundingBoxH(lua_State* L) { + //TODO + return 0; +} + +static int getBoundingBoxX(lua_State* L) { + //TODO + return 0; +} + +static int getBoundingBoxY(lua_State* L) { + //TODO + return 0; +} + +static int getBoundingBoxW(lua_State* L) { + //TODO + return 0; +} + +static int getBoundingBoxH(lua_State* L) { + //TODO + return 0; +} + +//triggers +static int setTriggerReference(lua_State* L) { + //TODO + return 0; +} + +static int getTriggerReference(lua_State* L) { + //TODO + return 0; +} + static const luaL_Reg waypointLib[] = { + //origin + {"SetOriginX",setOriginX}, + {"SetOriginY",setOriginY}, + {"GetOriginX",getOriginX}, + {"GetOriginY",getOriginY}, + + //bounds + {"SetBoundsX",setBoundingBoxX}, + {"SetBoundsY",setBoundingBoxY}, + {"SetBoundsW",setBoundingBoxW}, + {"SetBoundsH",setBoundingBoxH}, + + {"GetBoundsX",getBoundingBoxX}, + {"GetBoundsY",getBoundingBoxY}, + {"GetBoundsW",getBoundingBoxW}, + {"GetBoundsH",getBoundingBoxH}, + + //triggers + {"SetTrigger",setTriggerReference}, + {"GetTrigger",getTriggerReference}, {nullptr, nullptr} }; diff --git a/server/waypoints/waypoint_data.hpp b/server/waypoints/waypoint_data.hpp index b10faff..6104847 100644 --- a/server/waypoints/waypoint_data.hpp +++ b/server/waypoints/waypoint_data.hpp @@ -34,21 +34,21 @@ public: WaypointData() = default; ~WaypointData() = default; - int SetTriggerReference(int i); - int GetTriggerReference(); + Vector2 SetOrigin(Vector2 v); + Vector2 GetOrigin(); BoundingBox SetBoundingBox(BoundingBox b); BoundingBox GetBoundingBox(); - Vector2 SetOrigin(Vector2 v); - Vector2 GetOrigin(); + int SetTriggerReference(int i); + int GetTriggerReference(); private: friend class WaypointManager; - int triggerRef = LUA_NOREF; - BoundingBox bounds; Vector2 origin; + BoundingBox bounds; + int triggerRef = LUA_NOREF; }; #endif \ No newline at end of file diff --git a/server/waypoints/waypoint_manager.cpp b/server/waypoints/waypoint_manager.cpp index 35101d1..e51dcd0 100644 --- a/server/waypoints/waypoint_manager.cpp +++ b/server/waypoints/waypoint_manager.cpp @@ -21,34 +21,59 @@ */ #include "waypoint_manager.hpp" -int WaypointManager::Create() { - //TODO +int WaypointManager::Create(Vector2 origin, BoundingBox bounds) { + //implicitly creates the element + WaypointData& waypointData = elementMap[counter]; + + waypointData.origin = origin; + waypointData.bounds = bounds; + + return counter++; } void WaypointManager::Unload(int uid) { - //TODO + elementMap.erase(uid); } void WaypointManager::UnloadAll() { - //TODO + elementMap.clear(); } void WaypointManager::UnloadIf(std::function)> fn) { - //TODO + std::map::iterator it = elementMap.begin(); + while (it != elementMap.end()) { + if (fn(*it)) { + it = elementMap.erase(it); + } + else { + ++it; + } + } } WaypointData* WaypointManager::Get(int uid) { - //TODO + std::map::iterator it = elementMap.find(uid); + + if (it == elementMap.end()) { + return nullptr; + } + + return &it->second; } int WaypointManager::GetLoadedCount() { - //TODO -} - -int WaypointManager::GetTotalCount() { - //TODO + return elementMap.size(); } std::map* WaypointManager::GetContainer() { - //TODO + return &elementMap; } + +//hooks +lua_State* WaypointManager::SetLuaState(lua_State* L) { + return lua = L; +} + +lua_State* WaypointManager::GetLuaState() { + return lua; +} \ No newline at end of file diff --git a/server/waypoints/waypoint_manager.hpp b/server/waypoints/waypoint_manager.hpp index dfb3b5e..140e1d4 100644 --- a/server/waypoints/waypoint_manager.hpp +++ b/server/waypoints/waypoint_manager.hpp @@ -22,9 +22,9 @@ #ifndef WAYPOINTMANAGER_HPP_ #define WAYPOINTMANAGER_HPP_ -#include "waypoint_data.hpp" -#include "singleton.hpp" +#include "bounding_box.hpp" #include "vector2.hpp" +#include "waypoint_data.hpp" #include "lua.hpp" @@ -32,11 +32,13 @@ #include #include -//TODO: should waypoints be managed on a per-room basis? -class WaypointManager: public Singleton { +class WaypointManager { public: + WaypointManager() = default; + ~WaypointManager() = default; + //common public methods - int Create(); + int Create(Vector2 origin, BoundingBox bounds); void Unload(int uid); void UnloadAll(); @@ -45,19 +47,13 @@ public: //accessors & mutators WaypointData* Get(int uid); int GetLoadedCount(); - int GetTotalCount(); std::map* GetContainer(); //hooks - lua_State* SetLuaState(lua_State* L) { return lua = L; } - lua_State* GetLuaState() { return lua; } + lua_State* SetLuaState(lua_State* L); + lua_State* GetLuaState(); private: - friend Singleton; - - WaypointManager() = default; - ~WaypointManager() = default; - //members std::map elementMap; lua_State* lua = nullptr; diff --git a/todo.txt b/todo.txt index 2a51e6b..3e19264 100644 --- a/todo.txt +++ b/todo.txt @@ -17,3 +17,4 @@ TODO: Make a way for the server owner to control the server directly TODO: The TileSheet class should implement the surface itself TODO: Time delay for requesting region packets TODO: A proper logging system +TODO: Fix the const-ness of accessors \ No newline at end of file