From 8ed308e89aa0c7dc3ea0e23c529fbdcce38db02d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 1 Jul 2014 22:55:43 +1000 Subject: [PATCH] Switched the hack for the bitset I've also added acessors and mutators to the Region and RegionPagerBase classes. --- common/map/region.cpp | 12 ++++++++++-- common/map/region.hpp | 16 +++++++++++++++- common/map/region_pager_base.cpp | 10 ++++++++++ common/map/region_pager_base.hpp | 4 ++++ common/network/serial/serial.hpp | 25 ++++++++----------------- server/server_application.cpp | 21 ++++++++++++--------- 6 files changed, 59 insertions(+), 29 deletions(-) diff --git a/common/map/region.cpp b/common/map/region.cpp index 7fbe38c..1f75df8 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -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)) { 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) { - 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) { @@ -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) { 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]; +} \ No newline at end of file diff --git a/common/map/region.hpp b/common/map/region.hpp index 7b578fa..3617482 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -22,10 +22,17 @@ #ifndef REGION_HPP_ #define REGION_HPP_ +#include +#include + +//the region's storage format constexpr int REGION_WIDTH = 21; constexpr int REGION_HEIGHT = 21; constexpr int REGION_DEPTH = 3; +//the size of the solid map +constexpr int REGION_SOLID = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); + class Region { public: typedef unsigned char type_t; @@ -38,6 +45,9 @@ public: type_t SetTile(int x, int y, int z, type_t v); type_t GetTile(int x, int y, int z); + bool SetSolid(int x, int y, bool b); + bool GetSolid(int x, int y); + //accessors int GetX() const { return x; } int GetY() const { return y; } @@ -45,7 +55,11 @@ private: const int x; const int y; - type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH+1]; + type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH]; + std::bitset 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 diff --git a/common/map/region_pager_base.cpp b/common/map/region_pager_base.cpp index a8ad76f..fbe633d 100644 --- a/common/map/region_pager_base.cpp +++ b/common/map/region_pager_base.cpp @@ -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); } +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) { //get the region by various means Region* ptr = nullptr; diff --git a/common/map/region_pager_base.hpp b/common/map/region_pager_base.hpp index 004faa4..53bb097 100644 --- a/common/map/region_pager_base.hpp +++ b/common/map/region_pager_base.hpp @@ -35,6 +35,10 @@ public: 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); + //solid manipulation + virtual bool SetSolid(int x, int y, int b); + virtual bool GetSolid(int x, int y); + //region manipulation virtual Region* GetRegion(int x, int y); virtual Region* FindRegion(int x, int y); diff --git a/common/network/serial/serial.hpp b/common/network/serial/serial.hpp index f69743a..2a948bd 100644 --- a/common/network/serial/serial.hpp +++ b/common/network/serial/serial.hpp @@ -27,8 +27,6 @@ #include "region.hpp" #include "statistics.hpp" -#include - //Primary interface functions void serializePacket(SerialPacketBase*, void* dest); void deserializePacket(SerialPacketBase*, void* src); @@ -55,22 +53,15 @@ void deserializeRegionContent(RegionPacket*, void*); void deserializeServer(ServerPacket*, void*); void deserializeStatistics(Statistics*, void*); -/* DOCS: Keep these macros, including PACKET_BUFFER_SIZE up to date - * DOCS: REGION_CONTENT is currently the largest type of packet, read more - * REGION_FORMAT - * This is the space for the region's metadata; the X and Y position, the map index and the SerialPacketType - * REGION_FOOTPRINT - * This is the theoretical size of the map's tile data in memory, but it doesn't take the collision map into account - * COLLISION_FOOTPRINT - * 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 +/* DOCS: Keep PACKET_BUFFER_SIZE up to date + * DOCS: SerialPacketType::REGION_CONTENT is currently the largest type of packet, read more + * The metadata used are: + * SerialPacketType + * room index + * X & Y positon + * The rest is taken up by the Regions's content. */ -constexpr int REGION_FORMAT = sizeof(int) * 3 + sizeof(SerialPacketType); -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; +constexpr int PACKET_BUFFER_SIZE = sizeof(SerialPacketType) + sizeof(int) * 3 + REGION_FOOTPRINT; #endif \ No newline at end of file diff --git a/server/server_application.cpp b/server/server_application.cpp index 16bb8e4..41671bf 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -114,19 +114,22 @@ void ServerApplication::Init(int argc, char** argv) { //------------------------- //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; - OUTPUT_VAR(sizeof(Region::type_t)); - OUTPUT_VAR(sizeof(Region)); - OUTPUT_VAR(REGION_FORMAT); - OUTPUT_VAR(REGION_FOOTPRINT); - OUTPUT_VAR(COLLISION_FOOTPRINT); - OUTPUT_VAR(PACKET_BUFFER_SIZE); - OUTPUT_VAR(MAX_PACKET_SIZE); + DEBUG_OUTPUT_VAR(sizeof(Region::type_t)); + DEBUG_OUTPUT_VAR(sizeof(Region)); + DEBUG_OUTPUT_VAR(REGION_WIDTH); + DEBUG_OUTPUT_VAR(REGION_HEIGHT); + DEBUG_OUTPUT_VAR(REGION_DEPTH); + DEBUG_OUTPUT_VAR(REGION_SOLID); + 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