Brought the programs into line, (BUG, read more)

It seems that the serialization code has a bug in it. I was expecting
something like this. When the server tries to send the region content, it
exits. I'll try and find the cause of the error, but I'm committing my
changes anyway.
This commit is contained in:
Kayne Ruse
2014-04-20 04:27:28 +10:00
parent fba183fa27
commit eb0b18af6f
8 changed files with 44 additions and 51 deletions
+22 -23
View File
@@ -63,14 +63,11 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
shutDownButton.SetText("Shut Down");
//load the tilesheet
//TODO: add the tilesheet to the map system?
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
//setup the map object
mapPager.SetRegionWidth(REGION_WIDTH);
mapPager.SetRegionHeight(REGION_HEIGHT);
mapPager.SetRegionDepth(REGION_DEPTH);
//create the server-side player object
//TODO: the login system needs an overhaul
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::PLAYER_NEW;
packet.playerInfo.clientIndex = clientIndex;
@@ -143,7 +140,8 @@ void InWorld::RenderFrame() {
void InWorld::Render(SDL_Surface* const screen) {
//draw the map
for (auto it = mapPager.GetContainer()->begin(); it != mapPager.GetContainer()->end(); it++) {
//TODO: figure out something to fix the region container access
for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
tileSheet.DrawRegionTo(screen, *it, camera.x, camera.y);
}
@@ -347,15 +345,15 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
void InWorld::HandleRegionContent(NetworkPacket packet) {
//replace existing regions
if (mapPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
mapPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
}
mapPager.PushRegion(packet.regionInfo.region);
regionPager.PushRegion(packet.regionInfo.region);
packet.regionInfo.region = nullptr;
//debugging
cout << "Received region: " << packet.regionInfo.x << ", " << packet.regionInfo.y << endl;
if (mapPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
cout << "Success" << endl;
}
else {
@@ -421,7 +419,8 @@ void InWorld::RequestRegion(int x, int y) {
//-------------------------
int InWorld::CheckBufferDistance(Region* const region) {
/* DOCUMENTATION
/* TODO: Remove InWorld::CheckBufferDistance(), and replace it with a simpler system
* DOCUMENTATION
* This algorithm is extremely complex and involed, but initial tests show
* that it gives the right answers. The purpose is to determine how far off screen
* a certain region is, so that it can be unloaded when necessary.
@@ -453,16 +452,16 @@ int InWorld::CheckBufferDistance(Region* const region) {
int y = region->GetY() - camera.y;
//if the region is visible, return -1
if (x >= -mapPager.GetRegionWidth() * tileSheet.GetTileW() && x < camera.width &&
y >= -mapPager.GetRegionHeight() * tileSheet.GetTileH() && y < camera.height) {
if (x >= -REGION_WIDTH * tileSheet.GetTileW() && x < camera.width &&
y >= -REGION_HEIGHT * tileSheet.GetTileH() && y < camera.height) {
return -1;
}
//prune the screen's area from the algorithm; get the pseudo-indexes
if (x < 0) x /= (mapPager.GetRegionWidth() * tileSheet.GetTileW());
if (y < 0) y /= (mapPager.GetRegionHeight() * tileSheet.GetTileH());
if (x > 0) x = (x - camera.width) / (mapPager.GetRegionWidth() * tileSheet.GetTileW()) + 1;
if (y > 0) y = (y - camera.height) / (mapPager.GetRegionHeight() * tileSheet.GetTileH()) + 1;
if (x < 0) x /= (REGION_WIDTH * tileSheet.GetTileW());
if (y < 0) y /= (REGION_HEIGHT * tileSheet.GetTileH());
if (x > 0) x = (x - camera.width) / (REGION_WIDTH * tileSheet.GetTileW()) + 1;
if (y > 0) y = (y - camera.height) / (REGION_HEIGHT * tileSheet.GetTileH()) + 1;
//return the pseudo-index with the greatest magnitude
return std::max(abs(x), abs(y));
@@ -471,28 +470,28 @@ int InWorld::CheckBufferDistance(Region* const region) {
//TODO: eew ugly
void InWorld::UpdateMap() {
//prune distant regions
for (auto it = mapPager.GetContainer()->begin(); it != mapPager.GetContainer()->end(); /* EMPTY */) {
for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) {
if (CheckBufferDistance(*it) > 2) {
//debugging
cout << "unloading: " << (*it)->GetX() << ", " << (*it)->GetY() << endl;
mapPager.UnloadRegion((*it)->GetX(), (*it)->GetY());
regionPager.UnloadRegion((*it)->GetX(), (*it)->GetY());
//reset
it = mapPager.GetContainer()->begin();
it = regionPager.GetContainer()->begin();
continue;
}
++it;
}
//TODO: make the region units official
int regionUnitX = mapPager.GetRegionWidth() * tileSheet.GetTileW();
int regionUnitY = mapPager.GetRegionHeight() * tileSheet.GetTileH();
int regionUnitX = REGION_WIDTH * tileSheet.GetTileW();
int regionUnitY = REGION_HEIGHT * tileSheet.GetTileH();
//request empty regions, including buffers (-1 & +1 region unit)
for (int i = snapToBase(regionUnitX, camera.x - regionUnitX); i <= snapToBase(regionUnitX, camera.x + camera.width + regionUnitX); i += regionUnitX) {
for (int j = snapToBase(regionUnitY, camera.y - regionUnitY); j <= snapToBase(regionUnitY, camera.y + camera.height + regionUnitY); j += regionUnitY) {
if (!mapPager.FindRegion(i, j)) {
if (!regionPager.FindRegion(i, j)) {
RequestRegion(i, j);
}
}
+3 -2
View File
@@ -23,7 +23,7 @@
#define INWORLD_HPP_
//maps
#include "map_generator.hpp"
#include "map_allocator.hpp"
#include "map_file_format.hpp"
#include "region_pager.hpp"
@@ -101,11 +101,12 @@ protected:
TileSheet tileSheet;
//map
RegionPager<BlankGenerator, DummyFormat> mapPager;
RegionPager<BlankAllocator, DummyFormat> regionPager;
//UI
Button disconnectButton;
Button shutDownButton;
//TODO: Fix the camera
struct {
int x = 0, y = 0;
int width = 0, height = 0;
+11 -11
View File
@@ -29,8 +29,8 @@
class RegionPagerBase {
public:
RegionPagerBase() = default;
virtual ~RegionPagerBase() = default;
RegionPagerBase() {};
virtual ~RegionPagerBase() {};
//tile manipulation
Region::type_t SetTile(int x, int y, int z, Region::type_t v);
@@ -54,10 +54,10 @@ protected:
std::list<Region*> regionList;
};
template<typename MapGenerator, typename MapFileFormat>
template<typename Allocator, typename FileFormat>
class RegionPager : public RegionPagerBase {
public:
RegionPager() = default;
RegionPager() {};
~RegionPager() {
UnloadAll();
}
@@ -96,7 +96,7 @@ public:
//create and push the object
Region* ptr = nullptr;
generator.Create(&ptr, x, y);
allocator.Create(&ptr, x, y);
return PushRegion(ptr);
}
@@ -108,7 +108,7 @@ public:
//custom loop
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
if ((*it)->GetX() == x && (*it)->GetY() == y) {
generator.Unload(*it);
allocator.Unload(*it);
regionList.erase(it);
//reset the loop, because of reasons
@@ -120,17 +120,17 @@ public:
}
void UnloadAll() {
for (auto& it : regionList) {
generator.Unload(it);
allocator.Unload(it);
}
regionList.clear();
}
//accessors
MapGenerator* GetGenerator() { return &generator; }
MapFileFormat* GetFormat() { return &format; }
Allocator* GetAllocator() { return &allocator; }
FileFormat* GetFormat() { return &format; }
protected:
MapGenerator generator;
MapFileFormat format;
Allocator allocator;
FileFormat format;
};
#endif
+1
View File
@@ -167,6 +167,7 @@ void deserializeRegionContent(NetworkPacket* packet, char* buffer) {
memcpy(&packet->regionInfo.x, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&packet->regionInfo.y, buffer, sizeof(int));
buffer += sizeof(int);
//content
BlankAllocator().Create(
+1 -6
View File
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2013
/* Copyright: (c) Kayne Ruse 2013, 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
@@ -55,11 +55,6 @@ EditorScene::EditorScene(ConfigUtility* const arg1):
{"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"}
});
//setup the map
pager.SetRegionWidth(REGION_WIDTH);
pager.SetRegionHeight(REGION_HEIGHT);
pager.SetRegionDepth(REGION_DEPTH);
//debug
tsheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
for (int i = 0; i < REGION_WIDTH; i++) {
+3 -3
View File
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2013
/* Copyright: (c) Kayne Ruse 2013, 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
@@ -30,7 +30,7 @@
#include "menu_bar.hpp"
#include "region_pager.hpp"
#include "map_generator.hpp"
#include "map_allocator.hpp"
#include "map_file_format.hpp"
#include "tile_sheet.hpp"
@@ -73,7 +73,7 @@ protected:
int x = 0, y = 0;
} camera;
RegionPager<BlankGenerator, DummyFormat> pager;
RegionPager<BlankAllocator, DummyFormat> pager;
TileSheet tsheet;
};
+1 -4
View File
@@ -98,10 +98,7 @@ void ServerApplication::Init(int argc, char** argv) {
cout << "Initialized lua's setup script" << endl;
//setup the map object
regionPager.SetRegionWidth(REGION_WIDTH);
regionPager.SetRegionHeight(REGION_HEIGHT);
regionPager.SetRegionDepth(REGION_DEPTH);
regionPager.GetGenerator()->SetLuaState(luaState);
regionPager.GetAllocator()->SetLuaState(luaState);
regionPager.GetFormat()->SetLuaState(luaState);
//TODO: config parameter
regionPager.GetFormat()->SetSaveDir("save/mapname/");
+2 -2
View File
@@ -27,7 +27,7 @@
#include "player_entry.hpp"
//maps
#include "map_generator.hpp"
#include "map_allocator.hpp"
#include "map_file_format.hpp"
#include "region_pager.hpp"
@@ -88,7 +88,7 @@ private:
//maps
//TODO: I need to handle multiple map objects
RegionPager<LuaGenerator, LuaFormat> regionPager;
RegionPager<LuaAllocator, LuaFormat> regionPager;
//misc
bool running = true;