From ebd8e54725133bd4b00eee80d7622cfb2aa87299 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 1 Jan 2015 02:50:52 +1100 Subject: [PATCH] Added PushEntity() & PopEntity() to RoomManager Calling them from server_character_methods.cpp. --- rsc/scripts/map_maker.lua | 8 ++++++++ rsc/scripts/setup_server.lua | 4 ++++ server/rooms/room_api.cpp | 2 -- server/rooms/room_manager.cpp | 27 +++++++++++++++++++++++++++ server/rooms/room_manager.hpp | 4 ++++ server/server_character_methods.cpp | 14 ++++++++++++++ 6 files changed, 57 insertions(+), 2 deletions(-) diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index ca9ebc8..b92863c 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -36,4 +36,12 @@ function mapMaker.debugIsland(region) end end +function mapMaker.dirtLand(region) + for i = 1, mapSystem.Region.GetWidth(region) do + for j = 1, mapSystem.Region.GetHeight(region) do + mapSystem.Region.SetTile(region, i, j, 1, mapMaker.dirt) + end + end +end + return mapMaker \ No newline at end of file diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 53c733f..9321a59 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -22,6 +22,10 @@ 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) +--Dirt Land +local dirtLand = roomSystem.RoomManager.CreateRoom("dirt land", "overworld.bmp") +roomSystem.Room.Initialize(dirtLand, mapSaver.Load, mapSaver.Save, mapMaker.dirtLand, mapSaver.Save) + print("Finished the lua script") --[[ diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index b6606f8..f10ceaa 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -56,8 +56,6 @@ static int getPager(lua_State* L) { static int initialize(lua_State* L) { //set the members of the given room 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)); diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 4ce2dff..d31a597 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -24,6 +24,7 @@ #include "room_api.hpp" #include +#include //------------------------- //public access methods @@ -41,6 +42,32 @@ int RoomManager::Create(std::string roomName, std::string tileset) { return counter++; } +void RoomManager::PushEntity(Entity* entity) { + std::map::iterator it = elementMap.find(entity->GetRoomIndex()); + + if (it == elementMap.end()) { + std::ostringstream msg; + msg << "Failed to push entity; Room index not found: " << entity->GetRoomIndex() << std::endl; + throw(std::runtime_error(msg.str())); + } + + it->second.entityList.push_back(entity); +} + +void RoomManager::PopEntity(Entity* entity) { + std::map::iterator it = elementMap.find(entity->GetRoomIndex()); + + if (it == elementMap.end()) { + std::ostringstream msg; + msg << "Failed to pop entity; Room index not found: " << entity->GetRoomIndex() << std::endl; + throw(std::runtime_error(msg.str())); + } + + it->second.entityList.remove_if([&](Entity* ptr) -> bool { + return ptr == entity; + }); +} + void RoomManager::UnloadAll() { elementMap.clear(); } diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 9f9f37c..ef87106 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" @@ -39,6 +40,9 @@ public: //common public methods int Create(std::string name, std::string tileset); + void PushEntity(Entity* entity); + void PopEntity(Entity* entity); + void UnloadAll(); void UnloadIf(std::function)> fn); diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index d123d8e..dbdb903 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 this character to the rooms + roomMgr.PushEntity(characterMgr.Get(characterIndex)); + //pump this character to all clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, characterIndex); @@ -87,6 +90,7 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket) } //delete the character + roomMgr.PopEntity(characterMgr.Get(characterIndex)); characterMgr.Delete(characterIndex); //pump character delete @@ -119,6 +123,9 @@ void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) { return; } + //push this character to the rooms + roomMgr.PushEntity(characterMgr.Get(characterIndex)); + //pump this character to all clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, characterIndex); @@ -150,6 +157,7 @@ void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket) } //unload the character + roomMgr.PopEntity(characterData); characterMgr.Unload(argPacket->characterIndex); //pump character delete @@ -190,11 +198,17 @@ void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket) return; } + //pop from the rooms + 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 rooms + roomMgr.PushEntity(characterData); + //update the clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, argPacket->characterIndex);