Working on the client side map code (read more)

I've also added in some debug code to the map classes, because I was
hunting down a std::bad_alloc beingthrown. Turns out I forgot to set the
map sizes in the client's InWorld constructor. I'm committing the fix, and
the debug code.
This commit is contained in:
Kayne Ruse
2014-04-06 02:25:55 +11:00
parent 962f3f5dd0
commit 27bda5dc28
7 changed files with 113 additions and 24 deletions
+2 -2
View File
@@ -24,11 +24,11 @@
#include <stdexcept>
void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) {
//TODO
//EMPTY
}
void DummyFormat::Save(Region* const ptr) {
//TODO
//EMPTY
}
/*
void VerboseFormat::Load(Region** const ptr, int x, int y) {
+9 -3
View File
@@ -21,6 +21,12 @@
*/
#include "region.hpp"
#include <stdexcept>
#include <iostream>
//decorator
#define TRY(x) try { x } catch(std::exception& e) { throw std::runtime_error(std::string() + e.what() + ": " + #x); }
Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
width(argWidth),
height(argHeight),
@@ -28,11 +34,11 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
x(argX),
y(argY)
{
tiles = new type_t**[width];
TRY(tiles = new type_t**[width];)
for (register int i = 0; i < width; ++i) {
tiles[i] = new type_t*[height];
TRY(tiles[i] = new type_t*[height];)
for (register int j = 0; j < height; ++j) {
tiles[i][j] = new type_t[depth];
TRY(tiles[i][j] = new type_t[depth];)
for (register int k = 0; k < depth; ++k) {
tiles[i][j][k] = 0;
}
+41 -5
View File
@@ -23,6 +23,11 @@
#include "utility.hpp"
#include <stdexcept>
#include <iostream>
using namespace std;
RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
regionWidth(argWidth),
regionHeight(argHeight),
@@ -50,15 +55,46 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y);
//get the region by various means
//TODO: revert this try/catch point
Region* ptr = nullptr;
try {
ptr = FindRegion(x, y);
if (ptr) return ptr;
}
catch(exception& e) {
cerr << "FindRegion Error: " << e.what() << endl;
}
try {
ptr = LoadRegion(x, y);
if (ptr) return ptr;
}
catch(exception& e) {
cerr << "LoadRegion Error: " << e.what() << endl;
}
try {
return CreateRegion(x, y);
}
catch(exception& e) {
cerr << "CreateRegion Error: " << e.what() << endl;
}
return nullptr;
}
Region* RegionPagerBase::FindRegion(int x, int y) {
//find the region
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
if ((*it)->GetX() == x && (*it)->GetY() == y) {
return *it;
}
}
//get the region by other means
Region* ptr = LoadRegion(x, y);
if (ptr) return ptr;
return CreateRegion(x, y);
return nullptr;
}
Region* RegionPagerBase::PushRegion(Region* ptr) {
regionList.push_front(ptr);
return regionList.front();
}
+6 -1
View File
@@ -33,18 +33,23 @@ public:
RegionPagerBase(int regionWidth, int regionHeight, int regionDepth);
virtual ~RegionPagerBase();
//tile manipulation
Region::type_t SetTile(int x, int y, int z, Region::type_t v);
Region::type_t GetTile(int x, int y, int z);
//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?
//accessors
//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; }