From d815f174424f992d0d3c862196b0453eea1e0548 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 2 Jan 2015 23:25:59 +1100 Subject: [PATCH 1/8] Reimplemented the push/pop entity methods in RoomManager Some accessors in Entity had to be made const, as they were being called from lambdas with const parameters. --- server/entities/entity.cpp | 6 +++--- server/entities/entity.hpp | 6 +++--- server/rooms/room_manager.cpp | 31 +++++++++++++++++++++++++++++ server/rooms/room_manager.hpp | 4 ++++ server/server_character_methods.cpp | 18 +++++++++++++++++ server/server_methods.cpp | 3 +++ 6 files changed, 62 insertions(+), 6 deletions(-) diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index fcfa8aa..5587e81 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -33,14 +33,14 @@ Vector2 Entity::SetMotion(Vector2 v) { return motion = v; } -int Entity::GetRoomIndex() { +int Entity::GetRoomIndex() const { return roomIndex; } -Vector2 Entity::GetOrigin() { +Vector2 Entity::GetOrigin() const { return origin; } -Vector2 Entity::GetMotion() { +Vector2 Entity::GetMotion() const { return motion; } \ No newline at end of file diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index f71fbf9..6874a7e 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -32,9 +32,9 @@ public: Vector2 SetOrigin(Vector2 v); Vector2 SetMotion(Vector2 v); - int GetRoomIndex(); - Vector2 GetOrigin(); - Vector2 GetMotion(); + int GetRoomIndex() const; + Vector2 GetOrigin() const; + Vector2 GetMotion() const; protected: Entity() = default; diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 56a3f65..b173ed8 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -57,6 +57,37 @@ void RoomManager::UnloadIf(std::functionGetRoomIndex()); + + if (!room) { + throw(std::runtime_error("Failed to push an entity to a non-existant room")); + } + + room->entityList.push_back(entity); +} + +void RoomManager::PopEntity(Entity const* entity) { + //NOTE: to pop an entity from a room, the entity must first exist + if (!entity) { + throw(std::runtime_error("Failed to pop a null entity to a room")); + } + + RoomData* room = Get(entity->GetRoomIndex()); + + if (!room) { + throw(std::runtime_error("Failed to pop an entity to a non-existant room")); + } + + room->entityList.remove_if([entity](Entity* ptr) { + return entity == ptr; + }); +} + RoomData* RoomManager::Get(int uid) { std::map::iterator it = elementMap.find(uid); diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 8bf1cc6..17d8e53 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -22,6 +22,7 @@ #ifndef ROOMMANAGER_HPP_ #define ROOMMANAGER_HPP_ +#include "entity.hpp" #include "room_data.hpp" #include "singleton.hpp" @@ -42,6 +43,9 @@ public: void UnloadAll(); void UnloadIf(std::function)> fn); + void PushEntity(Entity* entity); + void PopEntity(Entity const* entity); + //accessors and mutators RoomData* Get(int uid); RoomData* Get(std::string name); diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index d123d8e..610ec2d 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -45,6 +45,9 @@ void ServerApplication::HandleCharacterCreate(CharacterPacket* const argPacket) return; } + //push to the rooms + roomMgr.PushEntity(characterMgr.Get(characterIndex)); + //pump this character to all clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, characterIndex); @@ -86,6 +89,9 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket) return; } + //pop from the rooms + roomMgr.PopEntity(characterMgr.Get(characterIndex)); + //delete the character characterMgr.Delete(characterIndex); @@ -119,6 +125,9 @@ void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) { return; } + //push to the rooms + roomMgr.PushEntity(characterMgr.Get(characterIndex)); + //pump this character to all clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, characterIndex); @@ -149,6 +158,9 @@ void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket) return; } + //pop from the rooms + roomMgr.PopEntity(characterData); + //unload the character characterMgr.Unload(argPacket->characterIndex); @@ -190,11 +202,17 @@ void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket) return; } + //pop from the old room + roomMgr.PopEntity(characterData); + //set the character's room, zero it's origin, zero it's motion characterData->SetRoomIndex(argPacket->roomIndex); characterData->SetOrigin({0, 0}); characterData->SetMotion({0, 0}); + //push to the new room + roomMgr.PushEntity(characterData); + //update the clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, argPacket->characterIndex); diff --git a/server/server_methods.cpp b/server/server_methods.cpp index e8f197f..7222ef0 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -149,6 +149,9 @@ void ServerApplication::FullCharacterUnload(int index) { return false; } + //pop from the rooms + roomMgr.PopEntity(&character.second); + //pump character unload CharacterPacket newPacket; newPacket.type = SerialPacketType::CHARACTER_DELETE; From 92eb75af7eecc177a9114e4ef055bcd3d935f8fc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 2 Jan 2015 23:51:00 +1100 Subject: [PATCH 2/8] Slight tweaks to waypoints --- server/waypoints/waypoint_data.cpp | 18 +++++++++++++++++- server/waypoints/waypoint_data.hpp | 13 +++++++++++-- server/waypoints/waypoint_manager.cpp | 12 ------------ server/waypoints/waypoint_manager.hpp | 3 --- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/server/waypoints/waypoint_data.cpp b/server/waypoints/waypoint_data.cpp index b720e1d..3225834 100644 --- a/server/waypoints/waypoint_data.cpp +++ b/server/waypoints/waypoint_data.cpp @@ -27,4 +27,20 @@ int WaypointData::SetTriggerReference(int i) { int WaypointData::GetTriggerReference() { return triggerRef; -} \ No newline at end of file +} + +BoundingBox WaypointData::SetBoundingBox(BoundingBox b) { + return bounds = b; +} + +BoundingBox WaypointData::GetBoundingBox() { + return bounds; +} + +Vector2 WaypointData::SetOrigin(Vector2 v) { + return origin = v; +} + +Vector2 WaypointData::GetOrigin() { + return origin; +} diff --git a/server/waypoints/waypoint_data.hpp b/server/waypoints/waypoint_data.hpp index 32aa7d0..f58555c 100644 --- a/server/waypoints/waypoint_data.hpp +++ b/server/waypoints/waypoint_data.hpp @@ -22,7 +22,8 @@ #ifndef WAYPOINTDATA_HPP_ #define WAYPOINTDATA_HPP_ -#include "entity.hpp" +#include "bounding_box.hpp" +#include "vector2.hpp" #if defined(__MINGW32__) #include "lua/lua.hpp" @@ -32,7 +33,7 @@ #include -class WaypointData: public Entity { +class WaypointData { public: WaypointData() = default; ~WaypointData() = default; @@ -40,10 +41,18 @@ public: int SetTriggerReference(int i); int GetTriggerReference(); + BoundingBox SetBoundingBox(BoundingBox b); + BoundingBox GetBoundingBox(); + + Vector2 SetOrigin(Vector2 v); + Vector2 GetOrigin(); + private: friend class WaypointManager; int triggerRef = LUA_NOREF; + BoundingBox bounds; + Vector2 origin; }; #endif \ No newline at end of file diff --git a/server/waypoints/waypoint_manager.cpp b/server/waypoints/waypoint_manager.cpp index a89165b..35101d1 100644 --- a/server/waypoints/waypoint_manager.cpp +++ b/server/waypoints/waypoint_manager.cpp @@ -25,22 +25,10 @@ int WaypointManager::Create() { //TODO } -int WaypointManager::Load() { - //TODO -} - -int WaypointManager::Save(int uid) { - //TODO -} - void WaypointManager::Unload(int uid) { //TODO } -void WaypointManager::Delete(int uid) { - //TODO -} - void WaypointManager::UnloadAll() { //TODO } diff --git a/server/waypoints/waypoint_manager.hpp b/server/waypoints/waypoint_manager.hpp index 38aea9e..bd1cc34 100644 --- a/server/waypoints/waypoint_manager.hpp +++ b/server/waypoints/waypoint_manager.hpp @@ -41,10 +41,7 @@ class WaypointManager: public Singleton { public: //common public methods int Create(); - int Load(); - int Save(int uid); void Unload(int uid); - void Delete(int uid); void UnloadAll(); void UnloadIf(std::function)> fn); From be679062188ed061ce66ef4477c1b70e0b0a7925 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 9 Jan 2015 12:41:24 +1100 Subject: [PATCH 3/8] Moved TileSheet to common/graphics Also deleted TileSheet's vestigial API. --- common/graphics/makefile | 2 +- common/{map => graphics}/tile_sheet.cpp | 0 common/{map => graphics}/tile_sheet.hpp | 0 common/map/makefile | 2 +- common/map/map_system_api.cpp | 2 - common/map/tile_sheet_api.cpp | 75 ------------------------- common/map/tile_sheet_api.hpp | 30 ---------- 7 files changed, 2 insertions(+), 109 deletions(-) rename common/{map => graphics}/tile_sheet.cpp (100%) rename common/{map => graphics}/tile_sheet.hpp (100%) delete mode 100644 common/map/tile_sheet_api.cpp delete mode 100644 common/map/tile_sheet_api.hpp diff --git a/common/graphics/makefile b/common/graphics/makefile index 9013447..afcde11 100644 --- a/common/graphics/makefile +++ b/common/graphics/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. +INCLUDES+=. ../map LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/map/tile_sheet.cpp b/common/graphics/tile_sheet.cpp similarity index 100% rename from common/map/tile_sheet.cpp rename to common/graphics/tile_sheet.cpp diff --git a/common/map/tile_sheet.hpp b/common/graphics/tile_sheet.hpp similarity index 100% rename from common/map/tile_sheet.hpp rename to common/graphics/tile_sheet.hpp diff --git a/common/map/makefile b/common/map/makefile index 24e7779..769709e 100644 --- a/common/map/makefile +++ b/common/map/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../graphics ../utilities +INCLUDES+=. ../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/map/map_system_api.cpp b/common/map/map_system_api.cpp index 1ab6984..7a980c7 100644 --- a/common/map/map_system_api.cpp +++ b/common/map/map_system_api.cpp @@ -24,7 +24,6 @@ //all map API headers #include "region_api.hpp" #include "region_pager_api.hpp" -#include "tile_sheet_api.hpp" //useful "globals" //... @@ -37,7 +36,6 @@ static const luaL_Reg funcs[] = { static const luaL_Reg libs[] = { {"Region", openRegionAPI}, {"RegionPager", openRegionPagerAPI}, -// {"TileSheet", openTileSheetAPI}, {nullptr, nullptr} }; diff --git a/common/map/tile_sheet_api.cpp b/common/map/tile_sheet_api.cpp deleted file mode 100644 index 141f200..0000000 --- a/common/map/tile_sheet_api.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2015 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#include "tile_sheet_api.hpp" - -#include "tile_sheet.hpp" - -static int load(lua_State* L) { - TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); - sheet->Load(lua_tostring(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); - return 0; -} - -static int unload(lua_State* L) { - TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); - sheet->Unload(); - return 0; -} - -static int getXCount(lua_State* L) { - TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); - lua_pushinteger(L, sheet->GetXCount()); - return 1; -} - -static int getYCount(lua_State* L) { - TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); - lua_pushinteger(L, sheet->GetYCount()); - return 1; -} - -static int getTileW(lua_State* L) { - TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); - lua_pushinteger(L, sheet->GetTileW()); - return 1; -} - -static int getTileH(lua_State* L) { - TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); - lua_pushinteger(L, sheet->GetTileH()); - return 1; -} - -static const luaL_Reg tileSheetLib[] = { - {"Load",load}, - {"Unload",unload}, - {"GetXCount",getXCount}, - {"GetYCount",getYCount}, - {"GetTileW",getTileW}, - {"GetTileH",getTileH}, - {nullptr, nullptr} -}; - -LUAMOD_API int openTileSheetAPI(lua_State* L) { - luaL_newlib(L, tileSheetLib); - return 1; -} \ No newline at end of file diff --git a/common/map/tile_sheet_api.hpp b/common/map/tile_sheet_api.hpp deleted file mode 100644 index 57c812e..0000000 --- a/common/map/tile_sheet_api.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2015 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#ifndef TILESHEETAPI_HPP_ -#define TILESHEETAPI_HPP_ - -#include "lua.hpp" - -#define TORTUGA_TILE_SHEET_NAME "tile_sheet" -LUAMOD_API int openTileSheetAPI(lua_State* L); - -#endif From b391dde08926e52c616d5ab59f0243c9fcc428cf Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 9 Jan 2015 13:21:09 +1100 Subject: [PATCH 4/8] 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 From 8ea667a0b5e213b925e5dae6110342cbef2c77bf Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 10 Jan 2015 14:03:13 +1100 Subject: [PATCH 5/8] Wrote the WaypointManager API skeleton --- server/waypoints/waypoint_manager.cpp | 19 +++++++++++++++++++ server/waypoints/waypoint_manager.hpp | 5 +++-- server/waypoints/waypoint_manager_api.cpp | 20 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/server/waypoints/waypoint_manager.cpp b/server/waypoints/waypoint_manager.cpp index e51dcd0..f81ad90 100644 --- a/server/waypoints/waypoint_manager.cpp +++ b/server/waypoints/waypoint_manager.cpp @@ -21,6 +21,25 @@ */ #include "waypoint_manager.hpp" +WaypointManager::WaypointManager() { + //EMPTY +} + +WaypointManager::~WaypointManager() { + UnloadAll(); +} + +int WaypointManager::Create() { + //implicitly creates the element + WaypointData& waypointData = elementMap[counter]; + + //no real values set + waypointData.origin = {0, 0}; + waypointData.bounds = {0, 0, 0, 0}; + + return counter++; +} + int WaypointManager::Create(Vector2 origin, BoundingBox bounds) { //implicitly creates the element WaypointData& waypointData = elementMap[counter]; diff --git a/server/waypoints/waypoint_manager.hpp b/server/waypoints/waypoint_manager.hpp index 140e1d4..91bc3d0 100644 --- a/server/waypoints/waypoint_manager.hpp +++ b/server/waypoints/waypoint_manager.hpp @@ -34,10 +34,11 @@ class WaypointManager { public: - WaypointManager() = default; - ~WaypointManager() = default; + WaypointManager(); + ~WaypointManager(); //common public methods + int Create(); int Create(Vector2 origin, BoundingBox bounds); void Unload(int uid); diff --git a/server/waypoints/waypoint_manager_api.cpp b/server/waypoints/waypoint_manager_api.cpp index c99782a..9703d71 100644 --- a/server/waypoints/waypoint_manager_api.cpp +++ b/server/waypoints/waypoint_manager_api.cpp @@ -23,7 +23,27 @@ #include "waypoint_manager.hpp" +static int create(lua_State* L) { + //TODO +} + +static int unload(lua_State* L) { + //TODO +} + +static int getWaypoint(lua_State* L) { + //TODO +} + +static int getLoadedCount(lua_State* L) { + //TODO +} + static const luaL_Reg waypointManagerLib[] = { + {"Create",create}, + {"Unload",unload}, + {"GetWaypoint",getWaypoint}, + {"GetCount",getLoadedCount}, {nullptr, nullptr} }; From 051ed0f14c8b98e8d906e404f19ae529ccaee0bb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 11 Jan 2015 19:08:31 +1100 Subject: [PATCH 6/8] Monster API clones from Entity API, read more This is my solution for handling inheritance via lua. The Entity class is only a base class, so the entity API is designed to be copied from, rather than used directly. linit.c: It should be noted that the Entity API must always be placed before the utilizing child APIs. I don't know about how lua handles things internally, but I'm assuming that this is the case. There's no real meat in the API code yet, since that's just busy-work. Right now I feel beter about writing the connective tissue. This case could aslo extend to the waypoint and monster APIs. The waypoint system had some API and class methods removed for brevity. --- rsc/scripts/setup_server.lua | 15 ++++++ server/linit.cpp | 5 ++ server/monsters/monster_api.cpp | 44 ++++++++++++++++ server/monsters/monster_data.cpp | 8 +-- server/monsters/monster_data.hpp | 4 +- server/monsters/monster_manager.cpp | 16 ------ server/monsters/monster_manager.hpp | 4 -- server/monsters/monster_system_api.cpp | 55 ++++++++++++++++++++ server/monsters/monster_system_api.hpp | 30 +++++++++++ server/waypoints/waypoint_api.cpp | 70 ++++---------------------- 10 files changed, 165 insertions(+), 86 deletions(-) create mode 100644 server/monsters/monster_system_api.cpp create mode 100644 server/monsters/monster_system_api.hpp diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 53c733f..605dcff 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -22,6 +22,21 @@ mapSystem.RegionPager.SetOnSave(roomSystem.Room.GetPager(overworld), mapSaver.Sa mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker.debugIsland) mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save) +--check the entity-monster cloning... +print("Checking monster-entity stuff") + +monsterSystem = require "monster_system" +entities = require "entity" + +dumpTable(entities) +dumpTable(monsterSystem) +dumpTable(monsterSystem.Monster) + +monsterSystem.Monster.GetRoomIndex = -1 + +dumpTable(entities) +dumpTable(monsterSystem.Monster) + print("Finished the lua script") --[[ diff --git a/server/linit.cpp b/server/linit.cpp index aaa449d..fe803b9 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -36,6 +36,9 @@ #include "lua.hpp" +#include "entity_api.hpp" +#include "monster_system_api.hpp" + #include "map_system_api.hpp" #include "room_system_api.hpp" #include "waypoint_system_api.hpp" @@ -59,6 +62,8 @@ static const luaL_Reg loadedlibs[] = { //these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { + {TORTUGA_ENTITY_API, openEntityAPI}, + {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_MAP_SYSTEM_API, openMapSystemAPI}, {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI}, {TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI}, diff --git a/server/monsters/monster_api.cpp b/server/monsters/monster_api.cpp index 3baec56..473e7b4 100644 --- a/server/monsters/monster_api.cpp +++ b/server/monsters/monster_api.cpp @@ -23,11 +23,55 @@ #include "monster_data.hpp" +#include "entity_api.hpp" + +static int setAvatar(lua_State* L) { + //TODO +} + +static int getAvatar(lua_State* L) { + //TODO +} + +static int setScript(lua_State* L) { + //TODO +} + +static int getScript(lua_State* L) { + //TODO +} + static const luaL_Reg monsterLib[] = { + {"SetAvatar", setAvatar}, + {"GetAvatar", getAvatar}, + {"SetScript", setScript}, + {"GetScript", getScript}, {nullptr, nullptr} }; LUAMOD_API int openMonsterAPI(lua_State* L) { + //the local table luaL_newlib(L, monsterLib); + + //get the parent table + luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); + + //clone the parent table into the local table + lua_pushnil(L); //first key + while(lua_next(L, -2)) { + //copy the key-value pair + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + + //push the copy to the local table + lua_settable(L, -6); + + //pop the original value before continuing + lua_pop(L, 1); + } + + //remove the parent table, leaving the expanded child table + lua_pop(L, 1); + return 1; } \ No newline at end of file diff --git a/server/monsters/monster_data.cpp b/server/monsters/monster_data.cpp index 7fda714..22f5252 100644 --- a/server/monsters/monster_data.cpp +++ b/server/monsters/monster_data.cpp @@ -25,14 +25,14 @@ std::string MonsterData::SetAvatar(std::string s) { return avatar = s; } -int MonsterData::SetScriptReference(int i) { - return scriptRef = i; -} - std::string MonsterData::GetAvatar() { return avatar; } +int MonsterData::SetScriptReference(int i) { + return scriptRef = i; +} + int MonsterData::GetScriptReference() { return scriptRef; } \ No newline at end of file diff --git a/server/monsters/monster_data.hpp b/server/monsters/monster_data.hpp index 034cebb..f02723b 100644 --- a/server/monsters/monster_data.hpp +++ b/server/monsters/monster_data.hpp @@ -32,9 +32,9 @@ public: ~MonsterData() = default; std::string SetAvatar(std::string); - int SetScriptReference(int); - std::string GetAvatar(); + + int SetScriptReference(int); int GetScriptReference(); private: diff --git a/server/monsters/monster_manager.cpp b/server/monsters/monster_manager.cpp index 0cc78ce..2031fe2 100644 --- a/server/monsters/monster_manager.cpp +++ b/server/monsters/monster_manager.cpp @@ -25,22 +25,10 @@ int MonsterManager::Create(std::string) { //TODO } -int MonsterManager::Load(std::string) { - //TODO -} - -int MonsterManager::Save(int uid) { - //TODO -} - void MonsterManager::Unload(int uid) { //TODO } -void MonsterManager::Delete(int uid) { - //TODO -} - void MonsterManager::UnloadAll() { //TODO } @@ -57,10 +45,6 @@ int MonsterManager::GetLoadedCount() { //TODO } -int MonsterManager::GetTotalCount() { - //TODO -} - std::map* MonsterManager::GetContainer() { //TODO } diff --git a/server/monsters/monster_manager.hpp b/server/monsters/monster_manager.hpp index 83e799a..07d9602 100644 --- a/server/monsters/monster_manager.hpp +++ b/server/monsters/monster_manager.hpp @@ -36,10 +36,7 @@ class MonsterManager: public Singleton { public: //common public methods int Create(std::string); - int Load(std::string); - int Save(int uid); void Unload(int uid); - void Delete(int uid); void UnloadAll(); void UnloadIf(std::function)> fn); @@ -47,7 +44,6 @@ public: //accessors & mutators MonsterData* Get(int uid); int GetLoadedCount(); - int GetTotalCount(); std::map* GetContainer(); //hooks diff --git a/server/monsters/monster_system_api.cpp b/server/monsters/monster_system_api.cpp new file mode 100644 index 0000000..2ecbf71 --- /dev/null +++ b/server/monsters/monster_system_api.cpp @@ -0,0 +1,55 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "monster_system_api.hpp" + +//all monster API headers +#include "monster_api.hpp" +#include "monster_manager_api.hpp" + +//useful "globals" +//... + +//This mimics linit.c to create a nested collection of all monster modules. +static const luaL_Reg funcs[] = { + {nullptr, nullptr} +}; + +static const luaL_Reg libs[] = { + {"Monster", openMonsterAPI}, + {"MonsterManager", openMonsterManagerAPI}, + {nullptr, nullptr} +}; + +int openMonsterSystemAPI(lua_State* L) { + //create the table + luaL_newlibtable(L, libs); + + //push the "global" functions + luaL_setfuncs(L, funcs, 0); + + //push the substable + for (const luaL_Reg* lib = libs; lib->func; lib++) { + lib->func(L); + lua_setfield(L, -2, lib->name); + } + return 1; +} diff --git a/server/monsters/monster_system_api.hpp b/server/monsters/monster_system_api.hpp new file mode 100644 index 0000000..843a5e7 --- /dev/null +++ b/server/monsters/monster_system_api.hpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef MONSTERSYSTEMAPI_HPP_ +#define MONSTERSYSTEMAPI_HPP_ + +#include "lua.hpp" + +#define TORTUGA_MONSTER_SYSTEM_API "monster_system" +LUAMOD_API int openMonsterSystemAPI(lua_State* L); + +#endif \ No newline at end of file diff --git a/server/waypoints/waypoint_api.cpp b/server/waypoints/waypoint_api.cpp index 34a6274..f769ddb 100644 --- a/server/waypoints/waypoint_api.cpp +++ b/server/waypoints/waypoint_api.cpp @@ -23,64 +23,26 @@ #include "waypoint_data.hpp" +//TODO: return multiple values, dummy + //origin -static int setOriginX(lua_State* L) { +static int setOrigin(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) { +static int getOrigin(lua_State* L) { //TODO return 0; } //bounds -static int setBoundingBoxX(lua_State* L) { +static int setBoundingBox(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) { +static int getBoundingBox(lua_State* L) { //TODO return 0; } @@ -97,24 +59,12 @@ static int getTriggerReference(lua_State* L) { } static const luaL_Reg waypointLib[] = { - //origin - {"SetOriginX",setOriginX}, - {"SetOriginY",setOriginY}, - {"GetOriginX",getOriginX}, - {"GetOriginY",getOriginY}, + {"SetOrigin",setOrigin}, + {"GetOrigin",getOrigin}, - //bounds - {"SetBoundsX",setBoundingBoxX}, - {"SetBoundsY",setBoundingBoxY}, - {"SetBoundsW",setBoundingBoxW}, - {"SetBoundsH",setBoundingBoxH}, + {"SetBounds",setBoundingBox}, + {"GetBounds",getBoundingBox}, - {"GetBoundsX",getBoundingBoxX}, - {"GetBoundsY",getBoundingBoxY}, - {"GetBoundsW",getBoundingBoxW}, - {"GetBoundsH",getBoundingBoxH}, - - //triggers {"SetTrigger",setTriggerReference}, {"GetTrigger",getTriggerReference}, {nullptr, nullptr} From d0b2f8e12fb1309432412d82c2bce3ef83b43234 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 11 Jan 2015 19:47:42 +1100 Subject: [PATCH 7/8] Removed MonsterManager's Singleton status, RoomData now has mgr members RoomData now has monsterMgr and waypointMgr members, so that it can compare it's characters to the monster & waypoints, etc. RoomManager has been updated. It now has a database reference, which is passed to the monsterMgr of new rooms. The room API also has functions which expose these managers to lua. --- server/main.cpp | 3 --- server/monsters/monster_manager.cpp | 24 ++++++++++++++++-------- server/monsters/monster_manager.hpp | 17 +++++++---------- server/rooms/makefile | 2 +- server/rooms/room_api.cpp | 18 +++++++++++++++++- server/rooms/room_data.cpp | 8 ++++++++ server/rooms/room_data.hpp | 6 ++++++ server/rooms/room_manager.cpp | 11 +++++++++++ server/rooms/room_manager.hpp | 4 ++++ server/server_application.hpp | 3 +-- server/server_logic.cpp | 2 +- 11 files changed, 72 insertions(+), 26 deletions(-) diff --git a/server/main.cpp b/server/main.cpp index befb95a..b7a8953 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -26,7 +26,6 @@ #include "character_manager.hpp" #include "client_manager.hpp" #include "config_utility.hpp" -#include "monster_manager.hpp" #include "room_manager.hpp" #include "udp_network_utility.hpp" #include "waypoint_manager.hpp" @@ -43,7 +42,6 @@ int main(int argc, char* argv[]) { CharacterManager::CreateSingleton(); ClientManager::CreateSingleton(); ConfigUtility::CreateSingleton(); - MonsterManager::CreateSingleton(); RoomManager::CreateSingleton(); UDPNetworkUtility::CreateSingleton(); @@ -62,7 +60,6 @@ int main(int argc, char* argv[]) { CharacterManager::DeleteSingleton(); ClientManager::DeleteSingleton(); ConfigUtility::DeleteSingleton(); - MonsterManager::DeleteSingleton(); RoomManager::DeleteSingleton(); UDPNetworkUtility::DeleteSingleton(); } diff --git a/server/monsters/monster_manager.cpp b/server/monsters/monster_manager.cpp index 2031fe2..5717e45 100644 --- a/server/monsters/monster_manager.cpp +++ b/server/monsters/monster_manager.cpp @@ -21,6 +21,14 @@ */ #include "monster_manager.hpp" +MonsterManager::MonsterManager() { + //EMPTY +} + +MonsterManager::~MonsterManager() { + UnloadAll(); +} + int MonsterManager::Create(std::string) { //TODO } @@ -49,14 +57,6 @@ std::map* MonsterManager::GetContainer() { //TODO } -sqlite3* MonsterManager::SetDatabase(sqlite3* db) { - //TODO -} - -sqlite3* MonsterManager::GetDatabase() { - //TODO -} - lua_State* MonsterManager::SetLuaState(lua_State* L) { //TODO } @@ -64,3 +64,11 @@ lua_State* MonsterManager::SetLuaState(lua_State* L) { lua_State* MonsterManager::GetLuaState() { //TODO } + +sqlite3* MonsterManager::SetDatabase(sqlite3* db) { + //TODO +} + +sqlite3* MonsterManager::GetDatabase() { + //TODO +} diff --git a/server/monsters/monster_manager.hpp b/server/monsters/monster_manager.hpp index 07d9602..5d74e4c 100644 --- a/server/monsters/monster_manager.hpp +++ b/server/monsters/monster_manager.hpp @@ -23,7 +23,6 @@ #define MONSTERMANAGER_HPP_ #include "monster_data.hpp" -#include "singleton.hpp" #include "lua.hpp" #include "sqlite3.h" @@ -32,8 +31,11 @@ #include #include -class MonsterManager: public Singleton { +class MonsterManager { public: + MonsterManager(); + ~MonsterManager(); + //common public methods int Create(std::string); void Unload(int uid); @@ -47,21 +49,16 @@ public: std::map* GetContainer(); //hooks - sqlite3* SetDatabase(sqlite3* db); - sqlite3* GetDatabase(); lua_State* SetLuaState(lua_State* L); lua_State* GetLuaState(); + sqlite3* SetDatabase(sqlite3* db); + sqlite3* GetDatabase(); private: - friend Singleton; - - MonsterManager() = default; - ~MonsterManager() = default; - //members std::map elementMap; - sqlite3* database = nullptr; lua_State* lua = nullptr; + sqlite3* database = nullptr; }; #endif \ No newline at end of file diff --git a/server/rooms/makefile b/server/rooms/makefile index 514e483..a0dda5d 100644 --- a/server/rooms/makefile +++ b/server/rooms/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../entities ../server_utilities ../../common/map ../../common/utilities +INCLUDES+=. ../entities ../monsters ../server_utilities ../waypoints ../../common/map ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index b6606f8..bee2689 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -53,6 +53,18 @@ static int getPager(lua_State* L) { return 1; } +static int getMonsterMgr(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushlightuserdata(L, reinterpret_cast(room->GetMonsterMgr()) ); + return 1; +} + +static int getWaypointMgr(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushlightuserdata(L, reinterpret_cast(room->GetWaypointMgr()) ); + return 1; +} + static int initialize(lua_State* L) { //set the members of the given room RoomData* room = static_cast(lua_touserdata(L, 1)); @@ -70,11 +82,15 @@ static int initialize(lua_State* L) { } static const luaL_Reg roomLib[] = { - {"GetPager",getPager}, {"SetName", setRoomName}, {"GetName", getRoomName}, {"SetTileset", setTilesetName}, {"GetTileset", getTilesetName}, + + {"GetPager",getPager}, + {"GetMonsterMgr",getMonsterMgr}, + {"GetWaypointMgr",getWaypointMgr}, + {"Initialize", initialize}, {nullptr, nullptr} }; diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 8919564..87a9502 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -41,6 +41,14 @@ RegionPagerLua* RoomData::GetPager() { return &pager; } +MonsterManager* RoomData::GetMonsterMgr() { + return &monsterMgr; +} + +WaypointManager* RoomData::GetWaypointMgr() { + return &waypointMgr; +} + std::list* RoomData::GetEntityList() { return &entityList; } diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index 8a8e094..b5d9d9f 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -23,7 +23,9 @@ #define ROOMDATA_HPP_ #include "entity.hpp" +#include "monster_manager.hpp" #include "region_pager_lua.hpp" +#include "waypoint_manager.hpp" #include "lua.hpp" @@ -43,6 +45,8 @@ public: std::string GetTileset(); RegionPagerLua* GetPager(); + MonsterManager* GetMonsterMgr(); + WaypointManager* GetWaypointMgr(); std::list* GetEntityList(); //TODO: triggers for unload, save, per-second, player enter, player exit, etc. @@ -55,6 +59,8 @@ private: std::string tilesetName; RegionPagerLua pager; + MonsterManager monsterMgr; + WaypointManager waypointMgr; std::list entityList; }; diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index b173ed8..65df378 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -36,6 +36,9 @@ int RoomManager::Create(std::string roomName, std::string tileset) { newRoom->SetTileset(tileset); newRoom->pager.SetLuaState(lua); + newRoom->monsterMgr.SetLuaState(lua); + newRoom->monsterMgr.SetDatabase(database); + newRoom->waypointMgr.SetLuaState(lua); //finish the routine return counter++; @@ -122,3 +125,11 @@ lua_State* RoomManager::SetLuaState(lua_State* L) { lua_State* RoomManager::GetLuaState() { return lua; } + +sqlite3* RoomManager::SetDatabase(sqlite3* db) { + return database = db; +} + +sqlite3* RoomManager::GetDatabase() { + return database; +} diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 7fe75a6..1b51c23 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -27,6 +27,7 @@ #include "singleton.hpp" #include "lua.hpp" +#include "sqlite3.h" #include #include @@ -51,6 +52,8 @@ public: //hooks lua_State* SetLuaState(lua_State* L); lua_State* GetLuaState(); + sqlite3* SetDatabase(sqlite3* db); + sqlite3* GetDatabase(); private: friend Singleton; @@ -61,6 +64,7 @@ private: //members std::map elementMap; lua_State* lua = nullptr; + sqlite3* database = nullptr; int counter = 0; }; diff --git a/server/server_application.hpp b/server/server_application.hpp index aa26406..2756be3 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -114,10 +114,9 @@ private: lua_State* luaState = nullptr; //ugly references; I hate this + ClientManager& clientMgr = ClientManager::GetSingleton(); AccountManager& accountMgr = AccountManager::GetSingleton(); CharacterManager& characterMgr = CharacterManager::GetSingleton(); - ClientManager& clientMgr = ClientManager::GetSingleton(); - MonsterManager& monsterMgr = MonsterManager::GetSingleton(); RoomManager& roomMgr = RoomManager::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton(); diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 8a3505b..d4d96fa 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -105,6 +105,7 @@ void ServerApplication::Init(int argc, char* argv[]) { characterMgr.SetDatabase(database); roomMgr.SetLuaState(luaState); + roomMgr.SetDatabase(database); std::cout << "Internal managers initialized" << std::endl; @@ -201,7 +202,6 @@ void ServerApplication::Quit() { accountMgr.UnloadAll(); characterMgr.UnloadAll(); clientMgr.UnloadAll(); - monsterMgr.UnloadAll(); roomMgr.UnloadAll(); //APIs From 9f3721247dfbee51bfdff36206d5b94b66396c77 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 11 Jan 2015 20:12:50 +1100 Subject: [PATCH 8/8] Replaced a block of code in setup_server.lua with one line --- client/scenes/in_world.cpp | 2 -- rsc/scripts/setup_server.lua | 65 +----------------------------------- server/linit.cpp | 5 ++- server/rooms/room_api.cpp | 4 +-- 4 files changed, 4 insertions(+), 72 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 19caef0..ec03408 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -589,8 +589,6 @@ void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) { //ignore if this character doesn't exist std::map::iterator characterIt = characterMap.find(argPacket->characterIndex); if (characterIt == characterMap.end()) { - //debug - std::cout << "Ignoring character deletion" << std::endl; return; } diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 605dcff..19c1abc 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -4,7 +4,6 @@ mapSystem = require "map_system" mapMaker = require "map_maker" mapSaver = require "map_saver" roomSystem = require "room_system" -waypointSystem = require "waypoint_system" local function dumpTable(t) print(t) @@ -15,68 +14,6 @@ end --NOTE: room 0 is the first that the client asks for, therefore it must exist local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") - ---NOTE: This is horrible; room initialization is important -mapSystem.RegionPager.SetOnLoad(roomSystem.Room.GetPager(overworld), mapSaver.Load) -mapSystem.RegionPager.SetOnSave(roomSystem.Room.GetPager(overworld), mapSaver.Save) -mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker.debugIsland) -mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save) - ---check the entity-monster cloning... -print("Checking monster-entity stuff") - -monsterSystem = require "monster_system" -entities = require "entity" - -dumpTable(entities) -dumpTable(monsterSystem) -dumpTable(monsterSystem.Monster) - -monsterSystem.Monster.GetRoomIndex = -1 - -dumpTable(entities) -dumpTable(monsterSystem.Monster) +roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.debugIsland, mapSaver.Save) print("Finished the lua script") - ---[[ -debugging test - -Ideal output: - -------------------------- -pager: userdata: [memory location] -Size 0: 0 -[debug output from load] -Size 1: 1 -[debug output from save] -Size 2: 0 -[debug output from load] -Size 3: 1 -[debug output from save] -Size 4: 0 -------------------------- - ---]-] - -print("-------------------------") -local pager = roomSystem.Room.GetPager(overworld) - -print("pager:", pager) - -print("Size 0:", mapSystem.RegionPager.ContainerSize(pager)) - -local regionFoo = mapSystem.RegionPager.GetRegion(pager, 0, 0) -print("Size 1:", mapSystem.RegionPager.ContainerSize(pager)) - -mapSystem.RegionPager.UnloadRegion(pager, regionFoo) -print("Size 2:", mapSystem.RegionPager.ContainerSize(pager)) - -local regionFoo = mapSystem.RegionPager.GetRegion(pager, 0, 0) -print("Size 3:", mapSystem.RegionPager.ContainerSize(pager)) - -mapSystem.RegionPager.UnloadRegion(pager, 0, 0) -print("Size 4:", mapSystem.RegionPager.ContainerSize(pager)) - -print("-------------------------") ---]] \ No newline at end of file diff --git a/server/linit.cpp b/server/linit.cpp index fe803b9..b460290 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -37,9 +37,8 @@ #include "lua.hpp" #include "entity_api.hpp" -#include "monster_system_api.hpp" - #include "map_system_api.hpp" +#include "monster_system_api.hpp" #include "room_system_api.hpp" #include "waypoint_system_api.hpp" @@ -63,8 +62,8 @@ static const luaL_Reg loadedlibs[] = { //these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { {TORTUGA_ENTITY_API, openEntityAPI}, - {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_MAP_SYSTEM_API, openMapSystemAPI}, + {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI}, {TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI}, {NULL, NULL} diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index bee2689..19b3a95 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -66,10 +66,8 @@ static int getWaypointMgr(lua_State* L) { } static int initialize(lua_State* L) { - //set the members of the given room + //TODO: This could fit into the room system's globals RoomData* room = static_cast(lua_touserdata(L, 1)); - room->SetName(lua_tostring(L, 2)); - room->SetTileset(lua_tostring(L, 3)); //set the refs of these parameters (backwards, since it pops from the top of the stack) room->GetPager()->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));