Switched the hack for the bitset

I've also added acessors and mutators to the Region and RegionPagerBase
classes.
This commit is contained in:
Kayne Ruse
2014-07-01 22:55:43 +10:00
parent 8df1ecd804
commit 8ed308e89a
6 changed files with 59 additions and 29 deletions
+10 -2
View File
@@ -30,11 +30,11 @@ Region::Region(int argX, int argY): x(argX), y(argY) {
if (x != snapToBase(REGION_WIDTH, x) || y != snapToBase(REGION_HEIGHT, y)) { if (x != snapToBase(REGION_WIDTH, x) || y != snapToBase(REGION_HEIGHT, y)) {
throw(std::invalid_argument("Region location is off grid")); throw(std::invalid_argument("Region location is off grid"));
} }
memset(tiles, 0, REGION_WIDTH*REGION_HEIGHT*(REGION_DEPTH+1)*sizeof(type_t)); memset(tiles, 0, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t));
} }
Region::Region(Region const& rhs): x(rhs.x), y(rhs.y) { Region::Region(Region const& rhs): x(rhs.x), y(rhs.y) {
memcpy(tiles, rhs.tiles, REGION_WIDTH*REGION_HEIGHT*(REGION_DEPTH+1)*sizeof(type_t)); memcpy(tiles, rhs.tiles, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t));
} }
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) {
@@ -44,3 +44,11 @@ Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
Region::type_t Region::GetTile(int x, int y, int z) { Region::type_t Region::GetTile(int x, int y, int z) {
return tiles[x][y][z]; return tiles[x][y][z];
} }
bool Region::SetSolid(int x, int y, bool b) {
return solid[x * REGION_WIDTH + y] = b;
}
bool Region::GetSolid(int x, int y) {
return solid[x * REGION_WIDTH + y];
}
+15 -1
View File
@@ -22,10 +22,17 @@
#ifndef REGION_HPP_ #ifndef REGION_HPP_
#define REGION_HPP_ #define REGION_HPP_
#include <bitset>
#include <cmath>
//the region's storage format
constexpr int REGION_WIDTH = 21; constexpr int REGION_WIDTH = 21;
constexpr int REGION_HEIGHT = 21; constexpr int REGION_HEIGHT = 21;
constexpr int REGION_DEPTH = 3; constexpr int REGION_DEPTH = 3;
//the size of the solid map
constexpr int REGION_SOLID = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0);
class Region { class Region {
public: public:
typedef unsigned char type_t; typedef unsigned char type_t;
@@ -38,6 +45,9 @@ public:
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);
bool SetSolid(int x, int y, bool b);
bool GetSolid(int x, int y);
//accessors //accessors
int GetX() const { return x; } int GetX() const { return x; }
int GetY() const { return y; } int GetY() const { return y; }
@@ -45,7 +55,11 @@ private:
const int x; const int x;
const int y; const int y;
type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH+1]; type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH];
std::bitset<REGION_SOLID> solid;
}; };
//the memory footprint of the tile and solid data; not including any metadata
constexpr int REGION_FOOTPRINT = REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + REGION_SOLID;
#endif #endif
+10
View File
@@ -36,6 +36,16 @@ Region::type_t RegionPagerBase::GetTile(int x, int y, int z) {
return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z); return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z);
} }
bool RegionPagerBase::SetSolid(int x, int y, int b) {
Region* ptr = GetRegion(x, y);
return ptr->SetSolid(x - ptr->GetX(), y - ptr->GetY(), b);
}
bool RegionPagerBase::GetSolid(int x, int y) {
Region* ptr = GetRegion(x, y);
return ptr->GetSolid(x - ptr->GetX(), y - ptr->GetY());
}
Region* RegionPagerBase::GetRegion(int x, int y) { Region* RegionPagerBase::GetRegion(int x, int y) {
//get the region by various means //get the region by various means
Region* ptr = nullptr; Region* ptr = nullptr;
+4
View File
@@ -35,6 +35,10 @@ public:
virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v); virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v);
virtual Region::type_t GetTile(int x, int y, int z); virtual Region::type_t GetTile(int x, int y, int z);
//solid manipulation
virtual bool SetSolid(int x, int y, int b);
virtual bool GetSolid(int x, int y);
//region manipulation //region manipulation
virtual Region* GetRegion(int x, int y); virtual Region* GetRegion(int x, int y);
virtual Region* FindRegion(int x, int y); virtual Region* FindRegion(int x, int y);
+8 -17
View File
@@ -27,8 +27,6 @@
#include "region.hpp" #include "region.hpp"
#include "statistics.hpp" #include "statistics.hpp"
#include <cmath>
//Primary interface functions //Primary interface functions
void serializePacket(SerialPacketBase*, void* dest); void serializePacket(SerialPacketBase*, void* dest);
void deserializePacket(SerialPacketBase*, void* src); void deserializePacket(SerialPacketBase*, void* src);
@@ -55,22 +53,15 @@ void deserializeRegionContent(RegionPacket*, void*);
void deserializeServer(ServerPacket*, void*); void deserializeServer(ServerPacket*, void*);
void deserializeStatistics(Statistics*, void*); void deserializeStatistics(Statistics*, void*);
/* DOCS: Keep these macros, including PACKET_BUFFER_SIZE up to date /* DOCS: Keep PACKET_BUFFER_SIZE up to date
* DOCS: REGION_CONTENT is currently the largest type of packet, read more * DOCS: SerialPacketType::REGION_CONTENT is currently the largest type of packet, read more
* REGION_FORMAT * The metadata used are:
* This is the space for the region's metadata; the X and Y position, the map index and the SerialPacketType * SerialPacketType
* REGION_FOOTPRINT * room index
* This is the theoretical size of the map's tile data in memory, but it doesn't take the collision map into account * X & Y positon
* COLLISION_FOOTPRINT * The rest is taken up by the Regions's content.
* This is the space in the packet for the collision map, stored as a bit array
* PACKET_BUFFER_SIZE
* This is the tital size of the packet being sent between the server and the clients
*/ */
constexpr int REGION_FORMAT = sizeof(int) * 3 + sizeof(SerialPacketType); constexpr int PACKET_BUFFER_SIZE = sizeof(SerialPacketType) + sizeof(int) * 3 + REGION_FOOTPRINT;
constexpr int REGION_FOOTPRINT = REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t);
constexpr int COLLISION_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0);
constexpr int PACKET_BUFFER_SIZE = REGION_FORMAT + REGION_FOOTPRINT + COLLISION_FOOTPRINT;
#endif #endif
+12 -9
View File
@@ -114,19 +114,22 @@ void ServerApplication::Init(int argc, char** argv) {
//------------------------- //-------------------------
//TODO: put these outputs into the client too //TODO: put these outputs into the client too
#define OUTPUT_VAR(x) std::cout << "\t" << #x << ": " << x << std::endl; //TODO: enable/disable these with a switch
#define DEBUG_OUTPUT_VAR(x) std::cout << "\t" << #x << ": " << x << std::endl;
std::cout << "Internal sizes:" << std::endl; std::cout << "Internal sizes:" << std::endl;
OUTPUT_VAR(sizeof(Region::type_t)); DEBUG_OUTPUT_VAR(sizeof(Region::type_t));
OUTPUT_VAR(sizeof(Region)); DEBUG_OUTPUT_VAR(sizeof(Region));
OUTPUT_VAR(REGION_FORMAT); DEBUG_OUTPUT_VAR(REGION_WIDTH);
OUTPUT_VAR(REGION_FOOTPRINT); DEBUG_OUTPUT_VAR(REGION_HEIGHT);
OUTPUT_VAR(COLLISION_FOOTPRINT); DEBUG_OUTPUT_VAR(REGION_DEPTH);
OUTPUT_VAR(PACKET_BUFFER_SIZE); DEBUG_OUTPUT_VAR(REGION_SOLID);
OUTPUT_VAR(MAX_PACKET_SIZE); DEBUG_OUTPUT_VAR(REGION_FOOTPRINT);
DEBUG_OUTPUT_VAR(PACKET_BUFFER_SIZE);
DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE);
#undef OUTPUT_VAR #undef DEBUG_OUTPUT_VAR
//------------------------- //-------------------------
//finalize the startup //finalize the startup