From c5a627004aca63d9ccb5dd22a8ef1c115dd70a34 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 03:31:37 +1000 Subject: [PATCH] Refactored the map system (read more) The region's width, height and depth are all defined by preprocessor macros. The rest of the map system has been updated to match. The programs proper need to be updated as well. It would be a good idea to include the macros' values as part of the initial communication protocols, so that the clients don't connect to a server that is using the wrong sized regions. --- client/scenes/in_world.cpp | 1 + .../{map_generator.cpp => map_allocator.cpp} | 22 +++---- .../{map_generator.hpp => map_allocator.hpp} | 22 +++---- common/map/map_file_format.cpp | 24 ++------ common/map/map_file_format.hpp | 27 ++------- common/map/region.cpp | 26 +-------- common/map/region.hpp | 13 +---- common/map/region_pager.cpp | 20 ++----- common/map/region_pager.hpp | 57 ++++++------------- todo.txt | 7 ++- 10 files changed, 60 insertions(+), 159 deletions(-) rename common/map/{map_generator.cpp => map_allocator.cpp} (70%) rename common/map/{map_generator.hpp => map_allocator.hpp} (74%) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 2f72466..1890699 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -468,6 +468,7 @@ int InWorld::CheckBufferDistance(Region* const region) { return std::max(abs(x), abs(y)); } +//TODO: eew ugly void InWorld::UpdateMap() { //prune distant regions for (auto it = mapPager.GetContainer()->begin(); it != mapPager.GetContainer()->end(); /* EMPTY */) { diff --git a/common/map/map_generator.cpp b/common/map/map_allocator.cpp similarity index 70% rename from common/map/map_generator.cpp rename to common/map/map_allocator.cpp index eef4b16..a625b4e 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_allocator.cpp @@ -19,29 +19,21 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "map_generator.hpp" +#include "map_allocator.hpp" #include -void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { - (*ptr) = new Region(width, height, depth, x, y); +void BlankAllocator::Create(Region** const ptr, int x, int y) { + (*ptr) = new Region(x, y); } -void BlankGenerator::Unload(Region* const ptr) { +void BlankAllocator::Unload(Region* const ptr) { delete ptr; } -/* -void PerlinGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { - (*ptr) = new Region(width, height, depth, x, y); -} -void PerlinGenerator::Unload(Region* const ptr) { - delete ptr; -} -*/ -void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { +void LuaAllocator::Create(Region** const ptr, int x, int y) { //something to work on - (*ptr) = new Region(width, height, depth, x, y); + (*ptr) = new Region(x, y); //API hook lua_getglobal(state, "Region"); @@ -53,7 +45,7 @@ void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, lua_pop(state, 1); } -void LuaGenerator::Unload(Region* const ptr) { +void LuaAllocator::Unload(Region* const ptr) { //API hook lua_getglobal(state, "Region"); lua_getfield(state, -1, "Unload"); diff --git a/common/map/map_generator.hpp b/common/map/map_allocator.hpp similarity index 74% rename from common/map/map_generator.hpp rename to common/map/map_allocator.hpp index f3eba56..1efeaf8 100644 --- a/common/map/map_generator.hpp +++ b/common/map/map_allocator.hpp @@ -19,32 +19,24 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef MAPGENERATOR_HPP_ -#define MAPGENERATOR_HPP_ +#ifndef MAPALLOCATOR_HPP_ +#define MAPALLOCATOR_HPP_ #include "region.hpp" #include "lua/lua.hpp" -class BlankGenerator { +class BlankAllocator { public: - void Create(Region** const, int width, int height, int depth, int x, int y); + void Create(Region** const, int x, int y); void Unload(Region* const); private: // }; -/* -class PerlinGenerator { + +class LuaAllocator { public: - void Create(Region** const, int width, int height, int depth, int x, int y); - void Unload(Region* const); -private: - // -}; -*/ -class LuaGenerator { -public: - void Create(Region** const, int width, int height, int depth, int x, int y); + void Create(Region** const, int x, int y); void Unload(Region* const); lua_State* SetLuaState(lua_State* L) { return state = L; } diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index cc9aa7f..d27c664 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -23,33 +23,19 @@ #include -void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { +void DummyFormat::Load(Region** const ptr, int x, int y) { //EMPTY } void DummyFormat::Save(Region* const ptr) { //EMPTY } -/* -void VerboseFormat::Load(Region** const ptr, int x, int y) { - //TODO -} -void VerboseFormat::Save(Region* const ptr) { - //TODO -} - -void CompactFormat::Load(Region** const ptr, int x, int y) { - //TODO -} - -void CompactFormat::Save(Region* const ptr) { - //TODO -} -*/ -void LuaFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { +void LuaFormat::Load(Region** const ptr, int x, int y) { //something to load into - (*ptr) = new Region(width, height, depth, x, y); + if (!ptr) { + (*ptr) = new Region(x, y); + } //API hook lua_getglobal(state, "Region"); diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp index 50345e5..92f0c8d 100644 --- a/common/map/map_file_format.hpp +++ b/common/map/map_file_format.hpp @@ -30,18 +30,7 @@ class DummyFormat { public: - void Load(Region** const, int width, int height, int depth, 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; -}; -/* -class VerboseFormat { -public: - void Load(Region** const, int width, int height, int depth, int x, int y); + void Load(Region** const, int x, int y); void Save(Region* const); std::string SetSaveDir(std::string s) { return saveDir = s; } @@ -50,20 +39,12 @@ private: std::string saveDir; }; -class CompactFormat { -public: - void Load(Region** const, int width, int height, int depth, int x, int y); - void Save(Region* const); +//TODO: verbose save file format +//TODO: compact save file format - std::string SetSaveDir(std::string s) { return saveDir = s; } - std::string GetSaveDir() { return saveDir; } -private: - std::string saveDir; -}; -*/ class LuaFormat { public: - void Load(Region** const, int width, int height, int depth, int x, int y); + void Load(Region** const, int x, int y); void Save(Region* const); std::string SetSaveDir(std::string s) { return saveDir = s; } diff --git a/common/map/region.cpp b/common/map/region.cpp index 8dd701b..ffdbdf1 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -21,35 +21,15 @@ */ #include "region.hpp" -Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): - width(argWidth), - height(argHeight), - depth(argDepth), +Region::Region(int argX, int argY): x(argX), y(argY) { - tiles = new type_t**[width]; - for (register int i = 0; i < width; ++i) { - tiles[i] = new type_t*[height]; - for (register int j = 0; j < height; ++j) { - tiles[i][j] = new type_t[depth]; - for (register int k = 0; k < depth; ++k) { - tiles[i][j][k] = 0; - } - } + for (register int i = 0; i < REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH; ++i) { + *(reinterpret_cast(tiles) + i) = 0; } } -Region::~Region() { - for (register int i = 0; i < width; ++i) { - for (register int j = 0; j < height; j++) { - delete tiles[i][j]; - } - delete tiles[i]; - } - delete tiles; -} - Region::type_t Region::SetTile(int x, int y, int z, type_t v) { return tiles[x][y][z] = v; } diff --git a/common/map/region.hpp b/common/map/region.hpp index 382c3dd..d45628f 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -22,7 +22,6 @@ #ifndef REGION_HPP_ #define REGION_HPP_ -//temporary? #define REGION_WIDTH 20 #define REGION_HEIGHT 20 #define REGION_DEPTH 3 @@ -32,26 +31,20 @@ public: typedef unsigned short type_t; Region() = delete; - Region(int width, int height, int depth, int x, int y); - ~Region(); + Region(int x, int y); + ~Region() = default; type_t SetTile(int x, int y, int z, type_t v); type_t GetTile(int x, int y, int z); //accessors - int GetWidth() const { return width; } - int GetHeight() const { return height; } - int GetDepth() const { return depth; } int GetX() const { return x; } int GetY() const { return y; } private: - const int width; - const int height; - const int depth; const int x; const int y; - type_t*** tiles = nullptr; + type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH]; }; #endif diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index f0275be..90664dd 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,18 +23,6 @@ #include "utility.hpp" -RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): - regionWidth(argWidth), - regionHeight(argHeight), - regionDepth(argDepth) -{ - //EMPTY -} - -RegionPagerBase::~RegionPagerBase() { - //EMPTY -} - Region::type_t RegionPagerBase::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); @@ -47,8 +35,8 @@ Region::type_t RegionPagerBase::GetTile(int x, int y, int z) { Region* RegionPagerBase::GetRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //get the region by various means Region* ptr = nullptr; @@ -60,6 +48,10 @@ Region* RegionPagerBase::GetRegion(int x, int y) { } Region* RegionPagerBase::FindRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + //find the region for (std::list::iterator it = regionList.begin(); it != regionList.end(); it++) { if ((*it)->GetX() == x && (*it)->GetY() == y) { diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index bc48505..8414165 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -30,8 +30,7 @@ class RegionPagerBase { public: RegionPagerBase() = default; - RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); - virtual ~RegionPagerBase(); + virtual ~RegionPagerBase() = default; //tile manipulation Region::type_t SetTile(int x, int y, int z, Region::type_t v); @@ -50,20 +49,8 @@ public: //TODO: delete? //accessors & mutators - //NOTE: don't change the sizes mid-program, it will cause issues - int SetRegionWidth(int i) { return regionWidth = i; } - int SetRegionHeight(int i) { return regionHeight = i; } - int SetRegionDepth(int i) { return regionDepth = i; } - - int GetRegionWidth() const { return regionWidth; } - int GetRegionHeight() const { return regionHeight; } - int GetRegionDepth() const { return regionDepth; } - std::list* GetContainer() { return ®ionList; } protected: - int regionWidth; - int regionHeight; - int regionDepth; std::list regionList; }; @@ -71,62 +58,54 @@ template class RegionPager : public RegionPagerBase { public: RegionPager() = default; - RegionPager(int w, int h, int d): - RegionPagerBase(w, h, d) - { - //EMPTY - } ~RegionPager() { UnloadAll(); } Region* LoadRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //load the region if possible Region* ptr = nullptr; - format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y); + format.Load(&ptr, x, y); if (ptr) { - regionList.push_back(ptr); - return ptr; + return PushRegion(ptr); } return nullptr; } Region* SaveRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //find & save the region - for (std::list::iterator it = regionList.begin(); it != regionList.end(); it++) { - if ((*it)->GetX() == x && (*it)->GetY() == y) { - format.Save(*it); - return *it; - } + Region* ptr = FindRegion(x, y); + if (ptr) { + format.Save(ptr); } - return nullptr; + return ptr; } Region* CreateRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //create and push the object Region* ptr = nullptr; - generator.Create(&ptr, regionWidth, regionHeight, regionDepth, x, y); - regionList.push_back(ptr); - return ptr; + generator.Create(&ptr, x, y); + return PushRegion(ptr); } void UnloadRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + 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) { generator.Unload(*it); diff --git a/todo.txt b/todo.txt index 5e0beac..f60a8bc 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,11 @@ #These were partially scraped from the TODO utility -1. Implement the preprocessor values throughout the map system +*. Implement the preprocessor values throughout the map system +*. Rename MapGenerator to MapAllocator +-. Does the format need to be a functor too? +-. Can the format functor go inside the allocator? +5. lua API + 4. in_world.cpp:487 make the region units official 5. region_pager.hpp:50 delete? 16. editor_scene.cpp:99 skip the out-of-bounds regions