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.
This commit is contained in:
Kayne Ruse
2014-04-20 03:31:37 +10:00
parent ac27fb0ca7
commit c5a627004a
10 changed files with 60 additions and 159 deletions
+1
View File
@@ -468,6 +468,7 @@ int InWorld::CheckBufferDistance(Region* const region) {
return std::max(abs(x), abs(y)); return std::max(abs(x), abs(y));
} }
//TODO: eew ugly
void InWorld::UpdateMap() { void InWorld::UpdateMap() {
//prune distant regions //prune distant regions
for (auto it = mapPager.GetContainer()->begin(); it != mapPager.GetContainer()->end(); /* EMPTY */) { for (auto it = mapPager.GetContainer()->begin(); it != mapPager.GetContainer()->end(); /* EMPTY */) {
@@ -19,29 +19,21 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "map_generator.hpp" #include "map_allocator.hpp"
#include <stdexcept> #include <stdexcept>
void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { void BlankAllocator::Create(Region** const ptr, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y); (*ptr) = new Region(x, y);
} }
void BlankGenerator::Unload(Region* const ptr) { void BlankAllocator::Unload(Region* const ptr) {
delete 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) { void LuaAllocator::Create(Region** const ptr, int x, int y) {
delete ptr;
}
*/
void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
//something to work on //something to work on
(*ptr) = new Region(width, height, depth, x, y); (*ptr) = new Region(x, y);
//API hook //API hook
lua_getglobal(state, "Region"); lua_getglobal(state, "Region");
@@ -53,7 +45,7 @@ void LuaGenerator::Create(Region** const ptr, int width, int height, int depth,
lua_pop(state, 1); lua_pop(state, 1);
} }
void LuaGenerator::Unload(Region* const ptr) { void LuaAllocator::Unload(Region* const ptr) {
//API hook //API hook
lua_getglobal(state, "Region"); lua_getglobal(state, "Region");
lua_getfield(state, -1, "Unload"); lua_getfield(state, -1, "Unload");
@@ -19,32 +19,24 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef MAPGENERATOR_HPP_ #ifndef MAPALLOCATOR_HPP_
#define MAPGENERATOR_HPP_ #define MAPALLOCATOR_HPP_
#include "region.hpp" #include "region.hpp"
#include "lua/lua.hpp" #include "lua/lua.hpp"
class BlankGenerator { class BlankAllocator {
public: 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); void Unload(Region* const);
private: private:
// //
}; };
/*
class PerlinGenerator { class LuaAllocator {
public: 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 LuaGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const); void Unload(Region* const);
lua_State* SetLuaState(lua_State* L) { return state = L; } lua_State* SetLuaState(lua_State* L) { return state = L; }
+5 -19
View File
@@ -23,33 +23,19 @@
#include <stdexcept> #include <stdexcept>
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 //EMPTY
} }
void DummyFormat::Save(Region* const ptr) { void DummyFormat::Save(Region* const ptr) {
//EMPTY //EMPTY
} }
/*
void VerboseFormat::Load(Region** const ptr, int x, int y) {
//TODO
}
void VerboseFormat::Save(Region* const ptr) { void LuaFormat::Load(Region** const ptr, int x, int y) {
//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) {
//something to load into //something to load into
(*ptr) = new Region(width, height, depth, x, y); if (!ptr) {
(*ptr) = new Region(x, y);
}
//API hook //API hook
lua_getglobal(state, "Region"); lua_getglobal(state, "Region");
+4 -23
View File
@@ -30,18 +30,7 @@
class DummyFormat { class DummyFormat {
public: 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; }
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 Save(Region* const); void Save(Region* const);
std::string SetSaveDir(std::string s) { return saveDir = s; } std::string SetSaveDir(std::string s) { return saveDir = s; }
@@ -50,20 +39,12 @@ private:
std::string saveDir; std::string saveDir;
}; };
class CompactFormat { //TODO: verbose save file format
public: //TODO: compact save file format
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 LuaFormat { class LuaFormat {
public: 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); void Save(Region* const);
std::string SetSaveDir(std::string s) { return saveDir = s; } std::string SetSaveDir(std::string s) { return saveDir = s; }
+3 -23
View File
@@ -21,35 +21,15 @@
*/ */
#include "region.hpp" #include "region.hpp"
Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): Region::Region(int argX, int argY):
width(argWidth),
height(argHeight),
depth(argDepth),
x(argX), x(argX),
y(argY) y(argY)
{ {
tiles = new type_t**[width]; for (register int i = 0; i < REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH; ++i) {
for (register int i = 0; i < width; ++i) { *(reinterpret_cast<type_t*>(tiles) + i) = 0;
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;
}
}
} }
} }
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) { Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
return tiles[x][y][z] = v; return tiles[x][y][z] = v;
} }
+3 -10
View File
@@ -22,7 +22,6 @@
#ifndef REGION_HPP_ #ifndef REGION_HPP_
#define REGION_HPP_ #define REGION_HPP_
//temporary?
#define REGION_WIDTH 20 #define REGION_WIDTH 20
#define REGION_HEIGHT 20 #define REGION_HEIGHT 20
#define REGION_DEPTH 3 #define REGION_DEPTH 3
@@ -32,26 +31,20 @@ public:
typedef unsigned short type_t; typedef unsigned short type_t;
Region() = delete; Region() = delete;
Region(int width, int height, int depth, int x, int y); Region(int x, int y);
~Region(); ~Region() = default;
type_t SetTile(int x, int y, int z, type_t v); type_t SetTile(int x, int y, int z, type_t v);
type_t GetTile(int x, int y, int z); type_t GetTile(int x, int y, int z);
//accessors //accessors
int GetWidth() const { return width; }
int GetHeight() const { return height; }
int GetDepth() const { return depth; }
int GetX() const { return x; } int GetX() const { return x; }
int GetY() const { return y; } int GetY() const { return y; }
private: private:
const int width;
const int height;
const int depth;
const int x; const int x;
const int y; const int y;
type_t*** tiles = nullptr; type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH];
}; };
#endif #endif
+6 -14
View File
@@ -23,18 +23,6 @@
#include "utility.hpp" #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::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
Region* ptr = GetRegion(x, y); Region* ptr = GetRegion(x, y);
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); 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) { Region* RegionPagerBase::GetRegion(int x, int y) {
//snap the coords //snap the coords
x = snapToBase(regionWidth, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(regionHeight, y); y = snapToBase(REGION_HEIGHT, y);
//get the region by various means //get the region by various means
Region* ptr = nullptr; Region* ptr = nullptr;
@@ -60,6 +48,10 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
} }
Region* RegionPagerBase::FindRegion(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 //find the region
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) { for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
if ((*it)->GetX() == x && (*it)->GetY() == y) { if ((*it)->GetX() == x && (*it)->GetY() == y) {
+18 -39
View File
@@ -30,8 +30,7 @@
class RegionPagerBase { class RegionPagerBase {
public: public:
RegionPagerBase() = default; RegionPagerBase() = default;
RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); virtual ~RegionPagerBase() = default;
virtual ~RegionPagerBase();
//tile manipulation //tile manipulation
Region::type_t SetTile(int x, int y, int z, Region::type_t v); Region::type_t SetTile(int x, int y, int z, Region::type_t v);
@@ -50,20 +49,8 @@ public:
//TODO: delete? //TODO: delete?
//accessors & mutators //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<Region*>* GetContainer() { return &regionList; } std::list<Region*>* GetContainer() { return &regionList; }
protected: protected:
int regionWidth;
int regionHeight;
int regionDepth;
std::list<Region*> regionList; std::list<Region*> regionList;
}; };
@@ -71,62 +58,54 @@ template<typename MapGenerator, typename MapFileFormat>
class RegionPager : public RegionPagerBase { class RegionPager : public RegionPagerBase {
public: public:
RegionPager() = default; RegionPager() = default;
RegionPager(int w, int h, int d):
RegionPagerBase(w, h, d)
{
//EMPTY
}
~RegionPager() { ~RegionPager() {
UnloadAll(); UnloadAll();
} }
Region* LoadRegion(int x, int y) { Region* LoadRegion(int x, int y) {
//snap the coords //snap the coords
x = snapToBase(regionWidth, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(regionHeight, y); y = snapToBase(REGION_HEIGHT, y);
//load the region if possible //load the region if possible
Region* ptr = nullptr; Region* ptr = nullptr;
format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y); format.Load(&ptr, x, y);
if (ptr) { if (ptr) {
regionList.push_back(ptr); return PushRegion(ptr);
return ptr;
} }
return nullptr; return nullptr;
} }
Region* SaveRegion(int x, int y) { Region* SaveRegion(int x, int y) {
//snap the coords //snap the coords
x = snapToBase(regionWidth, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(regionHeight, y); y = snapToBase(REGION_HEIGHT, y);
//find & save the region //find & save the region
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) { Region* ptr = FindRegion(x, y);
if ((*it)->GetX() == x && (*it)->GetY() == y) { if (ptr) {
format.Save(*it); format.Save(ptr);
return *it;
}
} }
return nullptr; return ptr;
} }
Region* CreateRegion(int x, int y) { Region* CreateRegion(int x, int y) {
//snap the coords //snap the coords
x = snapToBase(regionWidth, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(regionHeight, y); y = snapToBase(REGION_HEIGHT, y);
//create and push the object //create and push the object
Region* ptr = nullptr; Region* ptr = nullptr;
generator.Create(&ptr, regionWidth, regionHeight, regionDepth, x, y); generator.Create(&ptr, x, y);
regionList.push_back(ptr); return PushRegion(ptr);
return ptr;
} }
void UnloadRegion(int x, int y) { void UnloadRegion(int x, int y) {
//snap the coords //snap the coords
x = snapToBase(regionWidth, x); x = snapToBase(REGION_WIDTH, x);
y = snapToBase(regionHeight, y); y = snapToBase(REGION_HEIGHT, y);
//custom loop
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
if ((*it)->GetX() == x && (*it)->GetY() == y) { if ((*it)->GetX() == x && (*it)->GetY() == y) {
generator.Unload(*it); generator.Unload(*it);
+6 -1
View File
@@ -1,6 +1,11 @@
#These were partially scraped from the TODO utility #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 4. in_world.cpp:487 make the region units official
5. region_pager.hpp:50 delete? 5. region_pager.hpp:50 delete?
16. editor_scene.cpp:99 skip the out-of-bounds regions 16. editor_scene.cpp:99 skip the out-of-bounds regions