From f034c32c384483f437156d2c343d7a0579951588 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Jun 2014 04:01:09 +1000 Subject: [PATCH] Collapsed the pager into a single file, removing lua hooks I need to re-add the lua hooks, but it'll be easy. --- common/map/map_allocator.cpp | 60 ---------------- common/map/map_allocator.hpp | 48 ------------- common/map/map_file_format.cpp | 66 ----------------- common/map/map_file_format.hpp | 62 ---------------- common/map/region_pager.cpp | 61 ++++++++++++++-- common/map/region_pager.hpp | 96 +++---------------------- common/network/serial/serial_region.cpp | 8 +-- common/script/map_api.cpp | 6 +- server/room_data.hpp | 4 +- server/room_manager.hpp | 1 - server/server_application.cpp | 1 + todo.txt | 1 - 12 files changed, 68 insertions(+), 346 deletions(-) delete mode 100644 common/map/map_allocator.cpp delete mode 100644 common/map/map_allocator.hpp delete mode 100644 common/map/map_file_format.cpp delete mode 100644 common/map/map_file_format.hpp diff --git a/common/map/map_allocator.cpp b/common/map/map_allocator.cpp deleted file mode 100644 index d57351c..0000000 --- a/common/map/map_allocator.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * 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 "map_allocator.hpp" - -#include - -void BlankAllocator::Create(Region** const ptr, int x, int y) { - (*ptr) = new Region(x, y); -} - -void BlankAllocator::Unload(Region* const ptr) { - delete ptr; -} - -void LuaAllocator::Create(Region** const ptr, int x, int y) { - //something to work on - (*ptr) = new Region(x, y); - - //API hook - lua_getglobal(state, "map"); - lua_getfield(state, -1, "create"); - lua_pushlightuserdata(state, *ptr); - if (lua_pcall(state, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); - } - lua_pop(state, 1); -} - -void LuaAllocator::Unload(Region* const ptr) { - //API hook - lua_getglobal(state, "map"); - lua_getfield(state, -1, "unload"); - lua_pushlightuserdata(state, ptr); - if (lua_pcall(state, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); - } - lua_pop(state, 1); - - //clean up the memory - delete ptr; -} diff --git a/common/map/map_allocator.hpp b/common/map/map_allocator.hpp deleted file mode 100644 index 1efeaf8..0000000 --- a/common/map/map_allocator.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * 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 MAPALLOCATOR_HPP_ -#define MAPALLOCATOR_HPP_ - -#include "region.hpp" - -#include "lua/lua.hpp" - -class BlankAllocator { -public: - void Create(Region** const, int x, int y); - void Unload(Region* const); -private: - // -}; - -class LuaAllocator { -public: - void Create(Region** const, int x, int y); - void Unload(Region* const); - - lua_State* SetLuaState(lua_State* L) { return state = L; } - lua_State* GetLuaState() { return state; } -private: - lua_State* state = nullptr; -}; - -#endif diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp deleted file mode 100644 index b4366e4..0000000 --- a/common/map/map_file_format.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * 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 "map_file_format.hpp" - -#include - -void DummyFormat::Load(Region** const ptr, int x, int y) { - //EMPTY -} - -void DummyFormat::Save(Region* const ptr) { - //EMPTY -} - -void LuaFormat::Load(Region** const ptr, int x, int y) { - //something to load into - - if (!(*ptr)) { - (*ptr) = new Region(x, y); - } - - //API hook - lua_getglobal(state, "map"); - lua_getfield(state, -1, "load"); - lua_pushlightuserdata(state, *ptr); - lua_pushstring(state, saveDir.c_str()); - if (lua_pcall(state, 2, 1, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); - } - if (lua_toboolean(state, -1) == false) { - delete (*ptr); - (*ptr) = nullptr; - } - lua_pop(state, 2); -} - -void LuaFormat::Save(Region* const ptr) { - //API hook - lua_getglobal(state, "map"); - lua_getfield(state, -1, "save"); - lua_pushlightuserdata(state, ptr); - lua_pushstring(state, saveDir.c_str()); - if (lua_pcall(state, 2, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); - } - lua_pop(state, 1); -} \ No newline at end of file diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp deleted file mode 100644 index 75f5204..0000000 --- a/common/map/map_file_format.hpp +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * 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 MAPFILEFORMAT_HPP_ -#define MAPFILEFORMAT_HPP_ - -#include "region.hpp" - -#include "lua/lua.hpp" - -#include - -//TODO: I'm unhappy with using this system, there needs to be a way to handle saving/loading better - -class DummyFormat { -public: - void Load(Region** const, int x, int y); - void Save(Region* const); - - std::string SetSaveDir(std::string s) { return saveDir = s; } - std::string GetSaveDir() { return saveDir; } -private: - std::string saveDir; -}; - -//TODO: verbose save file format -//TODO: compact save file format - -class LuaFormat { -public: - void Load(Region** const, int x, int y); - void Save(Region* const); - - std::string SetSaveDir(std::string s) { return saveDir = s; } - std::string GetSaveDir() { return saveDir; } - - lua_State* SetLuaState(lua_State* L) { return state = L; } - lua_State* GetLuaState() { return state; } -private: - std::string saveDir; - lua_State* state = nullptr; -}; - -#endif diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 90664dd..0fafd8f 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,17 +23,17 @@ #include "utility.hpp" -Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) { +Region::type_t RegionPager::SetTile(int x, int y, int z, Region::type_t v) { Region* ptr = GetRegion(x, y); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); } -Region::type_t RegionPagerBase::GetTile(int x, int y, int z) { +Region::type_t RegionPager::GetTile(int x, int y, int z) { Region* ptr = GetRegion(x, y); return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z); } -Region* RegionPagerBase::GetRegion(int x, int y) { +Region* RegionPager::GetRegion(int x, int y) { //snap the coords x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); @@ -47,7 +47,7 @@ Region* RegionPagerBase::GetRegion(int x, int y) { return CreateRegion(x, y); } -Region* RegionPagerBase::FindRegion(int x, int y) { +Region* RegionPager::FindRegion(int x, int y) { //snap the coords x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); @@ -61,7 +61,54 @@ Region* RegionPagerBase::FindRegion(int x, int y) { return nullptr; } -Region* RegionPagerBase::PushRegion(Region* ptr) { +Region* RegionPager::LoadRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //load the region if possible + //TODO: Load the region (lua) + return nullptr; +} + +Region* RegionPager::SaveRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //find & save the region + Region* ptr = FindRegion(x, y); + if (ptr) { + //TODO: save the region (lua) + } + return ptr; +} + +Region* RegionPager::CreateRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //create and push the object + Region* ptr = new Region(x, y); + //TODO: create the region (lua) regionList.push_front(ptr); - return regionList.front(); -} \ No newline at end of file + return ptr; +} + +void RegionPager::UnloadRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //custom loop, not FindRegion() + for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { + if ((*it)->GetX() == x && (*it)->GetY() == y) { + //TODO: unload the region (lua) + delete (*it); + it = regionList.erase(it); + continue; + } + ++it; + } +} diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 95864f0..6a1d091 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -23,14 +23,14 @@ #define REGIONPAGER_HPP_ #include "region.hpp" -#include "utility.hpp" #include -class RegionPagerBase { +//TODO: add lua interface? +class RegionPager { public: - RegionPagerBase() {}; - virtual ~RegionPagerBase() {}; + RegionPager() = default; + ~RegionPager() = default; //tile manipulation Region::type_t SetTile(int x, int y, int z, Region::type_t v); @@ -39,14 +39,12 @@ public: //region manipulation Region* GetRegion(int x, int y); Region* FindRegion(int x, int y); - Region* PushRegion(Region*); - //interface - virtual Region* LoadRegion(int x, int y) = 0; - virtual Region* SaveRegion(int x, int y) = 0; - virtual Region* CreateRegion(int x, int y) = 0; - virtual void UnloadRegion(int x, int y) = 0; - //TODO: delete existing regions + Region* LoadRegion(int x, int y); + Region* SaveRegion(int x, int y); + Region* CreateRegion(int x, int y); + void UnloadRegion(int x, int y); + void DeleteRegion(int x, int y); //accessors & mutators std::list* GetContainer() { return ®ionList; } @@ -54,80 +52,4 @@ protected: std::list regionList; }; -template -class RegionPager : public RegionPagerBase { -public: - RegionPager() {}; - ~RegionPager() { - UnloadAll(); - } - - Region* LoadRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //load the region if possible - Region* ptr = nullptr; - format.Load(&ptr, x, y); - if (ptr) { - return PushRegion(ptr); - } - return nullptr; - } - - Region* SaveRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //find & save the region - Region* ptr = FindRegion(x, y); - if (ptr) { - format.Save(ptr); - } - return ptr; - } - - Region* CreateRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //create and push the object - Region* ptr = nullptr; - allocator.Create(&ptr, x, y); - return PushRegion(ptr); - } - - void UnloadRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //custom loop - for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { - if ((*it)->GetX() == x && (*it)->GetY() == y) { - allocator.Unload(*it); - it = regionList.erase(it); - continue; - } - ++it; - } - } - void UnloadAll() { - for (auto& it : regionList) { - allocator.Unload(it); - } - regionList.clear(); - } - - //accessors - Allocator* GetAllocator() { return &allocator; } - FileFormat* GetFormat() { return &format; } -protected: - Allocator allocator; - FileFormat format; -}; - #endif diff --git a/common/network/serial/serial_region.cpp b/common/network/serial/serial_region.cpp index e0860ac..31145bd 100644 --- a/common/network/serial/serial_region.cpp +++ b/common/network/serial/serial_region.cpp @@ -23,8 +23,6 @@ #include "serial_util.hpp" -#include "map_allocator.hpp" - void serializeRegionFormat(RegionPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); @@ -71,11 +69,7 @@ void deserializeRegionContent(RegionPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->y, sizeof(int)); //an object to work on - BlankAllocator().Create( - &packet->region, - packet->x, - packet->y - ); + packet->region = new Region(packet->x, packet->y); //content for (register int i = 0; i < REGION_WIDTH; i++) { diff --git a/common/script/map_api.cpp b/common/script/map_api.cpp index 0bcaf80..20aa681 100644 --- a/common/script/map_api.cpp +++ b/common/script/map_api.cpp @@ -22,8 +22,6 @@ #include "map_api.hpp" //map headers -#include "map_allocator.hpp" -#include "map_file_format.hpp" #include "region_pager.hpp" //NOTE: When operating on a region, setTile() & getTile() *are not* zero indexed, but when operating on the entire map they *are* zero indexed. @@ -42,7 +40,7 @@ static int setTile(lua_State* L) { lua_gettable(L, LUA_REGISTRYINDEX); //assume the pager is using lua - RegionPager* pager = reinterpret_cast*>(lua_touserdata(L, -1)); + RegionPager* pager = reinterpret_cast(lua_touserdata(L, -1)); //balance the stack lua_pop(L, 1); @@ -65,7 +63,7 @@ static int getTile(lua_State* L) { lua_gettable(L, LUA_REGISTRYINDEX); //assume the pager is using lua - RegionPager* pager = reinterpret_cast*>(lua_touserdata(L, -1)); + RegionPager* pager = reinterpret_cast(lua_touserdata(L, -1)); //balance the stack lua_pop(L, 1); diff --git a/server/room_data.hpp b/server/room_data.hpp index c436d54..0787425 100644 --- a/server/room_data.hpp +++ b/server/room_data.hpp @@ -23,8 +23,6 @@ #define ROOMDATA_HPP_ //map system -#include "map_allocator.hpp" -#include "map_file_format.hpp" #include "region_pager.hpp" struct RoomData { @@ -37,7 +35,7 @@ struct RoomData { }; //members - RegionPager pager; + RegionPager pager; RoomType type; //TODO: collision map diff --git a/server/room_manager.hpp b/server/room_manager.hpp index 2caadaa..9b1dfc6 100644 --- a/server/room_manager.hpp +++ b/server/room_manager.hpp @@ -35,7 +35,6 @@ public: //public access methods //TODO - //TODO: setup the pagers and functors of each room object //accessors and mutators RoomData* GetRoom(int uid); diff --git a/server/server_application.cpp b/server/server_application.cpp index 44467a0..0cb011d 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -23,6 +23,7 @@ #include "sql_utility.hpp" #include "serial.hpp" +#include "utility.hpp" #include #include diff --git a/todo.txt b/todo.txt index 605e60a..20fbe42 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,3 @@ -TODO: MapLoader, in place of FileFormat TODO: Get the rooms working TODO: update the map API to handle multiple rooms