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
+18 -39
View File
@@ -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<Region*>* GetContainer() { return &regionList; }
protected:
int regionWidth;
int regionHeight;
int regionDepth;
std::list<Region*> regionList;
};
@@ -71,62 +58,54 @@ template<typename MapGenerator, typename MapFileFormat>
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<Region*>::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<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
if ((*it)->GetX() == x && (*it)->GetY() == y) {
generator.Unload(*it);