From 93480be68544f935503aad73636d3bf087055a1a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 2 Jul 2014 00:47:37 +1000 Subject: [PATCH] Solid data is moving from the server to the client, read more The APIs have access to the solid data, and I fixed a bug: Basically, the template parameter for std::bitset expects an integer representing the number of bits to hold, but I initially misread it as the number of bytes. This has been corrected. I've also added a sandy beach to the generated island. I'm tempted to start working on some simple generators soon. --- common/map/region.cpp | 1 + common/map/region.hpp | 12 +++++++----- common/map/region_api.cpp | 16 ++++++++++++++++ common/map/region_pager_api.cpp | 16 ++++++++++++++++ common/network/serial/serial_region.cpp | 10 ++++++---- rsc/scripts/setup_server.lua | 9 ++++++--- server/server_application.cpp | 2 +- 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/common/map/region.cpp b/common/map/region.cpp index 1f75df8..c4fb712 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -35,6 +35,7 @@ Region::Region(int argX, int argY): x(argX), y(argY) { Region::Region(Region const& rhs): x(rhs.x), y(rhs.y) { memcpy(tiles, rhs.tiles, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t)); + solid = rhs.solid; } Region::type_t Region::SetTile(int x, int y, int z, type_t v) { diff --git a/common/map/region.hpp b/common/map/region.hpp index 3617482..0571c09 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -26,12 +26,12 @@ #include //the region's storage format -constexpr int REGION_WIDTH = 21; -constexpr int REGION_HEIGHT = 21; +constexpr int REGION_WIDTH = 20; +constexpr int REGION_HEIGHT = 20; constexpr int REGION_DEPTH = 3; //the size of the solid map -constexpr int REGION_SOLID = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); +constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); class Region { public: @@ -51,15 +51,17 @@ public: //accessors int GetX() const { return x; } int GetY() const { return y; } + + std::bitset* GetSolidBitset() { return &solid; } private: const int x; const int y; type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH]; - std::bitset solid; + 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; +constexpr int REGION_FOOTPRINT = REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + REGION_SOLID_FOOTPRINT; #endif diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index ead6ef2..014a873 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -37,6 +37,20 @@ static int getTile(lua_State* L) { return 1; } +static int setSolid(lua_State* L) { + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + bool ret = region->SetSolid(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_toboolean(L, 4)); + lua_pushboolean(L, ret); + return 1; +} + +static int getSolid(lua_State* L) { + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + bool ret = region->GetSolid(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1); + lua_pushboolean(L, ret); + return 1; +} + static int getX(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); lua_pushinteger(L, region->GetX()); @@ -89,6 +103,8 @@ static int onUnload(lua_State* L) { static const luaL_Reg regionLib[] = { {"SetTile",setTile}, {"GetTile",getTile}, + {"SetSolid",setSolid}, + {"GetSolid",getSolid}, {"GetX",getX}, {"GetY",getY}, {"GetWidth",getWidth}, diff --git a/common/map/region_pager_api.cpp b/common/map/region_pager_api.cpp index f38d1e2..40c4d76 100644 --- a/common/map/region_pager_api.cpp +++ b/common/map/region_pager_api.cpp @@ -43,6 +43,20 @@ static int getTile(lua_State* L) { return 1; } +static int setSolid(lua_State* L) { + RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); + bool ret = pager->SetSolid(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_toboolean(L, 4)); + lua_pushboolean(L, ret); + return 1; +} + +static int getSolid(lua_State* L) { + RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); + bool ret = pager->GetSolid(lua_tointeger(L, 2), lua_tointeger(L, 3)); + lua_pushboolean(L, ret); + return 1; +} + static int getRegion(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); @@ -94,6 +108,8 @@ static int unloadRegion(lua_State* L) { static const luaL_Reg pagerlib[] = { {"SetTile", setTile}, {"GetTile", getTile}, + {"SetSolid", setSolid}, + {"GetSolid", getSolid}, {"GetRegion", getRegion}, {"SetDirectory", setDirectory}, {"GetDirectory", getDirectory}, diff --git a/common/network/serial/serial_region.cpp b/common/network/serial/serial_region.cpp index d4dc563..5aec454 100644 --- a/common/network/serial/serial_region.cpp +++ b/common/network/serial/serial_region.cpp @@ -40,7 +40,7 @@ void serializeRegionContent(RegionPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->x, sizeof(int)); SERIALIZE(buffer, &packet->y, sizeof(int)); - //content + //tiles for (register int i = 0; i < REGION_WIDTH; i++) { for (register int j = 0; j < REGION_HEIGHT; j++) { for (register int k = 0; k < REGION_DEPTH; k++) { @@ -50,7 +50,8 @@ void serializeRegionContent(RegionPacket* packet, void* buffer) { } } - //TODO: serialize collision map + //solids + SERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); } void deserializeRegionFormat(RegionPacket* packet, void* buffer) { @@ -73,7 +74,7 @@ void deserializeRegionContent(RegionPacket* packet, void* buffer) { //an object to work on packet->region = new Region(packet->x, packet->y); - //content + //tiles for (register int i = 0; i < REGION_WIDTH; i++) { for (register int j = 0; j < REGION_HEIGHT; j++) { for (register int k = 0; k < REGION_DEPTH; k++) { @@ -83,5 +84,6 @@ void deserializeRegionContent(RegionPacket* packet, void* buffer) { } } - //TODO: deserialize collision map + //solids + DESERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); } \ No newline at end of file diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index e589a18..582cacf 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -19,10 +19,13 @@ Region.OnCreate = function(region) local ret = Region.hcOnCreate(region) --best practices for i = 1, Region.GetWidth() do for j = 1, Region.GetHeight() do - if distance(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1) > 10 then - Region.SetTile(region, i, j, 1, water) - else + local dist = distance(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1) + if dist < 10 then Region.SetTile(region, i, j, 1, plains) + elseif dist < 12 then + Region.SetTile(region, i, j, 1, sand) + else + Region.SetTile(region, i, j, 1, water) end end end diff --git a/server/server_application.cpp b/server/server_application.cpp index 41671bf..7396cda 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -124,7 +124,7 @@ void ServerApplication::Init(int argc, char** argv) { DEBUG_OUTPUT_VAR(REGION_WIDTH); DEBUG_OUTPUT_VAR(REGION_HEIGHT); DEBUG_OUTPUT_VAR(REGION_DEPTH); - DEBUG_OUTPUT_VAR(REGION_SOLID); + DEBUG_OUTPUT_VAR(REGION_SOLID_FOOTPRINT); DEBUG_OUTPUT_VAR(REGION_FOOTPRINT); DEBUG_OUTPUT_VAR(PACKET_BUFFER_SIZE); DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE);