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"); shutDownButton.SetText("Shut Down");
//load the tilesheet //load the tilesheet
//TODO: add the tilesheet to the map system?
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); 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 //create the server-side player object
//TODO: the login system needs an overhaul
NetworkPacket packet; NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::PLAYER_NEW; packet.meta.type = NetworkPacket::Type::PLAYER_NEW;
packet.playerInfo.clientIndex = clientIndex; packet.playerInfo.clientIndex = clientIndex;
@@ -143,7 +140,8 @@ void InWorld::RenderFrame() {
void InWorld::Render(SDL_Surface* const screen) { void InWorld::Render(SDL_Surface* const screen) {
//draw the map //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); tileSheet.DrawRegionTo(screen, *it, camera.x, camera.y);
} }
@@ -347,15 +345,15 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
void InWorld::HandleRegionContent(NetworkPacket packet) { void InWorld::HandleRegionContent(NetworkPacket packet) {
//replace existing regions //replace existing regions
if (mapPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) { if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
mapPager.UnloadRegion(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; packet.regionInfo.region = nullptr;
//debugging //debugging
cout << "Received region: " << packet.regionInfo.x << ", " << packet.regionInfo.y << endl; 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; cout << "Success" << endl;
} }
else { else {
@@ -421,7 +419,8 @@ void InWorld::RequestRegion(int x, int y) {
//------------------------- //-------------------------
int InWorld::CheckBufferDistance(Region* const region) { 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 * 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 * 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. * 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; int y = region->GetY() - camera.y;
//if the region is visible, return -1 //if the region is visible, return -1
if (x >= -mapPager.GetRegionWidth() * tileSheet.GetTileW() && x < camera.width && if (x >= -REGION_WIDTH * tileSheet.GetTileW() && x < camera.width &&
y >= -mapPager.GetRegionHeight() * tileSheet.GetTileH() && y < camera.height) { y >= -REGION_HEIGHT * tileSheet.GetTileH() && y < camera.height) {
return -1; return -1;
} }
//prune the screen's area from the algorithm; get the pseudo-indexes //prune the screen's area from the algorithm; get the pseudo-indexes
if (x < 0) x /= (mapPager.GetRegionWidth() * tileSheet.GetTileW()); if (x < 0) x /= (REGION_WIDTH * tileSheet.GetTileW());
if (y < 0) y /= (mapPager.GetRegionHeight() * tileSheet.GetTileH()); if (y < 0) y /= (REGION_HEIGHT * tileSheet.GetTileH());
if (x > 0) x = (x - camera.width) / (mapPager.GetRegionWidth() * tileSheet.GetTileW()) + 1; if (x > 0) x = (x - camera.width) / (REGION_WIDTH * tileSheet.GetTileW()) + 1;
if (y > 0) y = (y - camera.height) / (mapPager.GetRegionHeight() * tileSheet.GetTileH()) + 1; if (y > 0) y = (y - camera.height) / (REGION_HEIGHT * tileSheet.GetTileH()) + 1;
//return the pseudo-index with the greatest magnitude //return the pseudo-index with the greatest magnitude
return std::max(abs(x), abs(y)); return std::max(abs(x), abs(y));
@@ -471,28 +470,28 @@ int InWorld::CheckBufferDistance(Region* const region) {
//TODO: eew ugly //TODO: eew ugly
void InWorld::UpdateMap() { void InWorld::UpdateMap() {
//prune distant regions //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) { if (CheckBufferDistance(*it) > 2) {
//debugging //debugging
cout << "unloading: " << (*it)->GetX() << ", " << (*it)->GetY() << endl; cout << "unloading: " << (*it)->GetX() << ", " << (*it)->GetY() << endl;
mapPager.UnloadRegion((*it)->GetX(), (*it)->GetY()); regionPager.UnloadRegion((*it)->GetX(), (*it)->GetY());
//reset //reset
it = mapPager.GetContainer()->begin(); it = regionPager.GetContainer()->begin();
continue; continue;
} }
++it; ++it;
} }
//TODO: make the region units official //TODO: make the region units official
int regionUnitX = mapPager.GetRegionWidth() * tileSheet.GetTileW(); int regionUnitX = REGION_WIDTH * tileSheet.GetTileW();
int regionUnitY = mapPager.GetRegionHeight() * tileSheet.GetTileH(); int regionUnitY = REGION_HEIGHT * tileSheet.GetTileH();
//request empty regions, including buffers (-1 & +1 region unit) //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 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) { 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); RequestRegion(i, j);
} }
} }
+3 -2
View File
@@ -23,7 +23,7 @@
#define INWORLD_HPP_ #define INWORLD_HPP_
//maps //maps
#include "map_generator.hpp" #include "map_allocator.hpp"
#include "map_file_format.hpp" #include "map_file_format.hpp"
#include "region_pager.hpp" #include "region_pager.hpp"
@@ -101,11 +101,12 @@ protected:
TileSheet tileSheet; TileSheet tileSheet;
//map //map
RegionPager<BlankGenerator, DummyFormat> mapPager; RegionPager<BlankAllocator, DummyFormat> regionPager;
//UI //UI
Button disconnectButton; Button disconnectButton;
Button shutDownButton; Button shutDownButton;
//TODO: Fix the camera
struct { struct {
int x = 0, y = 0; int x = 0, y = 0;
int width = 0, height = 0; int width = 0, height = 0;
+11 -11
View File
@@ -29,8 +29,8 @@
class RegionPagerBase { class RegionPagerBase {
public: public:
RegionPagerBase() = default; RegionPagerBase() {};
virtual ~RegionPagerBase() = default; virtual ~RegionPagerBase() {};
//tile manipulation //tile manipulation
Region::type_t SetTile(int x, int y, int z, Region::type_t v); Region::type_t SetTile(int x, int y, int z, Region::type_t v);
@@ -54,10 +54,10 @@ protected:
std::list<Region*> regionList; std::list<Region*> regionList;
}; };
template<typename MapGenerator, typename MapFileFormat> template<typename Allocator, typename FileFormat>
class RegionPager : public RegionPagerBase { class RegionPager : public RegionPagerBase {
public: public:
RegionPager() = default; RegionPager() {};
~RegionPager() { ~RegionPager() {
UnloadAll(); UnloadAll();
} }
@@ -96,7 +96,7 @@ public:
//create and push the object //create and push the object
Region* ptr = nullptr; Region* ptr = nullptr;
generator.Create(&ptr, x, y); allocator.Create(&ptr, x, y);
return PushRegion(ptr); return PushRegion(ptr);
} }
@@ -108,7 +108,7 @@ public:
//custom loop //custom loop
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
if ((*it)->GetX() == x && (*it)->GetY() == y) { if ((*it)->GetX() == x && (*it)->GetY() == y) {
generator.Unload(*it); allocator.Unload(*it);
regionList.erase(it); regionList.erase(it);
//reset the loop, because of reasons //reset the loop, because of reasons
@@ -120,17 +120,17 @@ public:
} }
void UnloadAll() { void UnloadAll() {
for (auto& it : regionList) { for (auto& it : regionList) {
generator.Unload(it); allocator.Unload(it);
} }
regionList.clear(); regionList.clear();
} }
//accessors //accessors
MapGenerator* GetGenerator() { return &generator; } Allocator* GetAllocator() { return &allocator; }
MapFileFormat* GetFormat() { return &format; } FileFormat* GetFormat() { return &format; }
protected: protected:
MapGenerator generator; Allocator allocator;
MapFileFormat format; FileFormat format;
}; };
#endif #endif
+1
View File
@@ -167,6 +167,7 @@ void deserializeRegionContent(NetworkPacket* packet, char* buffer) {
memcpy(&packet->regionInfo.x, buffer, sizeof(int)); memcpy(&packet->regionInfo.x, buffer, sizeof(int));
buffer += sizeof(int); buffer += sizeof(int);
memcpy(&packet->regionInfo.y, buffer, sizeof(int)); memcpy(&packet->regionInfo.y, buffer, sizeof(int));
buffer += sizeof(int);
//content //content
BlankAllocator().Create( 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 * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * 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"} {"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"}
}); });
//setup the map
pager.SetRegionWidth(REGION_WIDTH);
pager.SetRegionHeight(REGION_HEIGHT);
pager.SetRegionDepth(REGION_DEPTH);
//debug //debug
tsheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); tsheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
for (int i = 0; i < REGION_WIDTH; i++) { 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 * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
@@ -30,7 +30,7 @@
#include "menu_bar.hpp" #include "menu_bar.hpp"
#include "region_pager.hpp" #include "region_pager.hpp"
#include "map_generator.hpp" #include "map_allocator.hpp"
#include "map_file_format.hpp" #include "map_file_format.hpp"
#include "tile_sheet.hpp" #include "tile_sheet.hpp"
@@ -73,7 +73,7 @@ protected:
int x = 0, y = 0; int x = 0, y = 0;
} camera; } camera;
RegionPager<BlankGenerator, DummyFormat> pager; RegionPager<BlankAllocator, DummyFormat> pager;
TileSheet tsheet; TileSheet tsheet;
}; };
+1 -4
View File
@@ -98,10 +98,7 @@ void ServerApplication::Init(int argc, char** argv) {
cout << "Initialized lua's setup script" << endl; cout << "Initialized lua's setup script" << endl;
//setup the map object //setup the map object
regionPager.SetRegionWidth(REGION_WIDTH); regionPager.GetAllocator()->SetLuaState(luaState);
regionPager.SetRegionHeight(REGION_HEIGHT);
regionPager.SetRegionDepth(REGION_DEPTH);
regionPager.GetGenerator()->SetLuaState(luaState);
regionPager.GetFormat()->SetLuaState(luaState); regionPager.GetFormat()->SetLuaState(luaState);
//TODO: config parameter //TODO: config parameter
regionPager.GetFormat()->SetSaveDir("save/mapname/"); regionPager.GetFormat()->SetSaveDir("save/mapname/");
+2 -2
View File
@@ -27,7 +27,7 @@
#include "player_entry.hpp" #include "player_entry.hpp"
//maps //maps
#include "map_generator.hpp" #include "map_allocator.hpp"
#include "map_file_format.hpp" #include "map_file_format.hpp"
#include "region_pager.hpp" #include "region_pager.hpp"
@@ -88,7 +88,7 @@ private:
//maps //maps
//TODO: I need to handle multiple map objects //TODO: I need to handle multiple map objects
RegionPager<LuaGenerator, LuaFormat> regionPager; RegionPager<LuaAllocator, LuaFormat> regionPager;
//misc //misc
bool running = true; bool running = true;