From be4a8311d5f4a267eb8b9f9e93a4ec0155bdebdb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 02:39:29 +1000 Subject: [PATCH 1/8] Adjusted a few comments --- common/map/region_pager.cpp | 2 -- server/server_application.cpp | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 8b2517a..f0275be 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -51,8 +51,6 @@ Region* RegionPagerBase::GetRegion(int x, int y) { y = snapToBase(regionHeight, y); //get the region by various means - - //TODO: revert this try/catch point Region* ptr = nullptr; ptr = FindRegion(x, y); if (ptr) return ptr; diff --git a/server/server_application.cpp b/server/server_application.cpp index 0ce7bd3..b170355 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -38,7 +38,7 @@ int runSQLScript(sqlite3* db, std::string fname) { string script; getline(is, script, '\0'); is.close(); - //TODO: flesh out this error if needed + //NOTE: flesh out this error if needed if (sqlite3_exec(db, script.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { return -2; } @@ -201,6 +201,7 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) { //TODO: version info snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str()); //TODO: player count + //TODO: map format char buffer[PACKET_BUFFER_SIZE]; serialize(&packet, buffer); network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); @@ -331,6 +332,7 @@ void ServerApplication::HandlePlayerNew(NetworkPacket packet) { void ServerApplication::HandlePlayerDelete(NetworkPacket packet) { //TODO: remove this? + //TODO: authenticate who is deleting this player if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) { throw(std::runtime_error("Cannot delete a non-existant player")); } From ac27fb0ca78adc14886449e39370c8cf4bf7cd9b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 02:40:06 +1000 Subject: [PATCH 2/8] Found a list of TODO comments --- todo.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 todo.txt diff --git a/todo.txt b/todo.txt new file mode 100644 index 0000000..5e0beac --- /dev/null +++ b/todo.txt @@ -0,0 +1,12 @@ +#These were partially scraped from the TODO utility + +1. Implement the preprocessor values throughout the map system +4. in_world.cpp:487 make the region units official +5. region_pager.hpp:50 delete? +16. editor_scene.cpp:99 skip the out-of-bounds regions +24. server_application.cpp:106 config parameter +25. server_application.cpp:108 pass args to the generator & format as needed +31. server_application.cpp:264 compensate for large distances +32. server_application.cpp:270 map? +36. server_application.hpp:77 a function that sends to players in a certain proximity +37. server_application.hpp:90 I need to handle multiple map objects From c5a627004aca63d9ccb5dd22a8ef1c115dd70a34 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 03:31:37 +1000 Subject: [PATCH 3/8] Refactored the map system (read more) The region's width, height and depth are all defined by preprocessor macros. The rest of the map system has been updated to match. The programs proper need to be updated as well. It would be a good idea to include the macros' values as part of the initial communication protocols, so that the clients don't connect to a server that is using the wrong sized regions. --- client/scenes/in_world.cpp | 1 + .../{map_generator.cpp => map_allocator.cpp} | 22 +++---- .../{map_generator.hpp => map_allocator.hpp} | 22 +++---- common/map/map_file_format.cpp | 24 ++------ common/map/map_file_format.hpp | 27 ++------- common/map/region.cpp | 26 +-------- common/map/region.hpp | 13 +---- common/map/region_pager.cpp | 20 ++----- common/map/region_pager.hpp | 57 ++++++------------- todo.txt | 7 ++- 10 files changed, 60 insertions(+), 159 deletions(-) rename common/map/{map_generator.cpp => map_allocator.cpp} (70%) rename common/map/{map_generator.hpp => map_allocator.hpp} (74%) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 2f72466..1890699 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -468,6 +468,7 @@ int InWorld::CheckBufferDistance(Region* const region) { return std::max(abs(x), abs(y)); } +//TODO: eew ugly void InWorld::UpdateMap() { //prune distant regions for (auto it = mapPager.GetContainer()->begin(); it != mapPager.GetContainer()->end(); /* EMPTY */) { diff --git a/common/map/map_generator.cpp b/common/map/map_allocator.cpp similarity index 70% rename from common/map/map_generator.cpp rename to common/map/map_allocator.cpp index eef4b16..a625b4e 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_allocator.cpp @@ -19,29 +19,21 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "map_generator.hpp" +#include "map_allocator.hpp" #include -void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { - (*ptr) = new Region(width, height, depth, x, y); +void BlankAllocator::Create(Region** const ptr, int x, int y) { + (*ptr) = new Region(x, y); } -void BlankGenerator::Unload(Region* const ptr) { +void BlankAllocator::Unload(Region* const ptr) { delete ptr; } -/* -void PerlinGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { - (*ptr) = new Region(width, height, depth, x, y); -} -void PerlinGenerator::Unload(Region* const ptr) { - delete ptr; -} -*/ -void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { +void LuaAllocator::Create(Region** const ptr, int x, int y) { //something to work on - (*ptr) = new Region(width, height, depth, x, y); + (*ptr) = new Region(x, y); //API hook lua_getglobal(state, "Region"); @@ -53,7 +45,7 @@ void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, lua_pop(state, 1); } -void LuaGenerator::Unload(Region* const ptr) { +void LuaAllocator::Unload(Region* const ptr) { //API hook lua_getglobal(state, "Region"); lua_getfield(state, -1, "Unload"); diff --git a/common/map/map_generator.hpp b/common/map/map_allocator.hpp similarity index 74% rename from common/map/map_generator.hpp rename to common/map/map_allocator.hpp index f3eba56..1efeaf8 100644 --- a/common/map/map_generator.hpp +++ b/common/map/map_allocator.hpp @@ -19,32 +19,24 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef MAPGENERATOR_HPP_ -#define MAPGENERATOR_HPP_ +#ifndef MAPALLOCATOR_HPP_ +#define MAPALLOCATOR_HPP_ #include "region.hpp" #include "lua/lua.hpp" -class BlankGenerator { +class BlankAllocator { public: - void Create(Region** const, int width, int height, int depth, int x, int y); + void Create(Region** const, int x, int y); void Unload(Region* const); private: // }; -/* -class PerlinGenerator { + +class LuaAllocator { public: - void Create(Region** const, int width, int height, int depth, int x, int y); - void Unload(Region* const); -private: - // -}; -*/ -class LuaGenerator { -public: - void Create(Region** const, int width, int height, int depth, int x, int y); + void Create(Region** const, int x, int y); void Unload(Region* const); lua_State* SetLuaState(lua_State* L) { return state = L; } diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index cc9aa7f..d27c664 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -23,33 +23,19 @@ #include -void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { +void DummyFormat::Load(Region** const ptr, int x, int y) { //EMPTY } void DummyFormat::Save(Region* const ptr) { //EMPTY } -/* -void VerboseFormat::Load(Region** const ptr, int x, int y) { - //TODO -} -void VerboseFormat::Save(Region* const ptr) { - //TODO -} - -void CompactFormat::Load(Region** const ptr, int x, int y) { - //TODO -} - -void CompactFormat::Save(Region* const ptr) { - //TODO -} -*/ -void LuaFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { +void LuaFormat::Load(Region** const ptr, int x, int y) { //something to load into - (*ptr) = new Region(width, height, depth, x, y); + if (!ptr) { + (*ptr) = new Region(x, y); + } //API hook lua_getglobal(state, "Region"); diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp index 50345e5..92f0c8d 100644 --- a/common/map/map_file_format.hpp +++ b/common/map/map_file_format.hpp @@ -30,18 +30,7 @@ class DummyFormat { public: - void Load(Region** const, int width, int height, int depth, int x, int y); - void Save(Region* const); - - std::string SetSaveDir(std::string s) { return saveDir = s; } - std::string GetSaveDir() { return saveDir; } -private: - std::string saveDir; -}; -/* -class VerboseFormat { -public: - void Load(Region** const, int width, int height, int depth, int x, int y); + void Load(Region** const, int x, int y); void Save(Region* const); std::string SetSaveDir(std::string s) { return saveDir = s; } @@ -50,20 +39,12 @@ private: std::string saveDir; }; -class CompactFormat { -public: - void Load(Region** const, int width, int height, int depth, int x, int y); - void Save(Region* const); +//TODO: verbose save file format +//TODO: compact save file format - std::string SetSaveDir(std::string s) { return saveDir = s; } - std::string GetSaveDir() { return saveDir; } -private: - std::string saveDir; -}; -*/ class LuaFormat { public: - void Load(Region** const, int width, int height, int depth, int x, int y); + void Load(Region** const, int x, int y); void Save(Region* const); std::string SetSaveDir(std::string s) { return saveDir = s; } diff --git a/common/map/region.cpp b/common/map/region.cpp index 8dd701b..ffdbdf1 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -21,35 +21,15 @@ */ #include "region.hpp" -Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): - width(argWidth), - height(argHeight), - depth(argDepth), +Region::Region(int argX, int argY): x(argX), y(argY) { - tiles = new type_t**[width]; - for (register int i = 0; i < width; ++i) { - tiles[i] = new type_t*[height]; - for (register int j = 0; j < height; ++j) { - tiles[i][j] = new type_t[depth]; - for (register int k = 0; k < depth; ++k) { - tiles[i][j][k] = 0; - } - } + for (register int i = 0; i < REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH; ++i) { + *(reinterpret_cast(tiles) + i) = 0; } } -Region::~Region() { - for (register int i = 0; i < width; ++i) { - for (register int j = 0; j < height; j++) { - delete tiles[i][j]; - } - delete tiles[i]; - } - delete tiles; -} - Region::type_t Region::SetTile(int x, int y, int z, type_t v) { return tiles[x][y][z] = v; } diff --git a/common/map/region.hpp b/common/map/region.hpp index 382c3dd..d45628f 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -22,7 +22,6 @@ #ifndef REGION_HPP_ #define REGION_HPP_ -//temporary? #define REGION_WIDTH 20 #define REGION_HEIGHT 20 #define REGION_DEPTH 3 @@ -32,26 +31,20 @@ public: typedef unsigned short type_t; Region() = delete; - Region(int width, int height, int depth, int x, int y); - ~Region(); + Region(int x, int y); + ~Region() = default; type_t SetTile(int x, int y, int z, type_t v); type_t GetTile(int x, int y, int z); //accessors - int GetWidth() const { return width; } - int GetHeight() const { return height; } - int GetDepth() const { return depth; } int GetX() const { return x; } int GetY() const { return y; } private: - const int width; - const int height; - const int depth; const int x; const int y; - type_t*** tiles = nullptr; + type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH]; }; #endif diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index f0275be..90664dd 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,18 +23,6 @@ #include "utility.hpp" -RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): - regionWidth(argWidth), - regionHeight(argHeight), - regionDepth(argDepth) -{ - //EMPTY -} - -RegionPagerBase::~RegionPagerBase() { - //EMPTY -} - Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) { Region* ptr = GetRegion(x, y); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); @@ -47,8 +35,8 @@ Region::type_t RegionPagerBase::GetTile(int x, int y, int z) { Region* RegionPagerBase::GetRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //get the region by various means Region* ptr = nullptr; @@ -60,6 +48,10 @@ Region* RegionPagerBase::GetRegion(int x, int y) { } Region* RegionPagerBase::FindRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + //find the region for (std::list::iterator it = regionList.begin(); it != regionList.end(); it++) { if ((*it)->GetX() == x && (*it)->GetY() == y) { diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index bc48505..8414165 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -30,8 +30,7 @@ class RegionPagerBase { public: RegionPagerBase() = default; - RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); - virtual ~RegionPagerBase(); + virtual ~RegionPagerBase() = default; //tile manipulation Region::type_t SetTile(int x, int y, int z, Region::type_t v); @@ -50,20 +49,8 @@ public: //TODO: delete? //accessors & mutators - //NOTE: don't change the sizes mid-program, it will cause issues - int SetRegionWidth(int i) { return regionWidth = i; } - int SetRegionHeight(int i) { return regionHeight = i; } - int SetRegionDepth(int i) { return regionDepth = i; } - - int GetRegionWidth() const { return regionWidth; } - int GetRegionHeight() const { return regionHeight; } - int GetRegionDepth() const { return regionDepth; } - std::list* GetContainer() { return ®ionList; } protected: - int regionWidth; - int regionHeight; - int regionDepth; std::list regionList; }; @@ -71,62 +58,54 @@ template class RegionPager : public RegionPagerBase { public: RegionPager() = default; - RegionPager(int w, int h, int d): - RegionPagerBase(w, h, d) - { - //EMPTY - } ~RegionPager() { UnloadAll(); } Region* LoadRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //load the region if possible Region* ptr = nullptr; - format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y); + format.Load(&ptr, x, y); if (ptr) { - regionList.push_back(ptr); - return ptr; + return PushRegion(ptr); } return nullptr; } Region* SaveRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //find & save the region - for (std::list::iterator it = regionList.begin(); it != regionList.end(); it++) { - if ((*it)->GetX() == x && (*it)->GetY() == y) { - format.Save(*it); - return *it; - } + Region* ptr = FindRegion(x, y); + if (ptr) { + format.Save(ptr); } - return nullptr; + return ptr; } Region* CreateRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); //create and push the object Region* ptr = nullptr; - generator.Create(&ptr, regionWidth, regionHeight, regionDepth, x, y); - regionList.push_back(ptr); - return ptr; + generator.Create(&ptr, x, y); + return PushRegion(ptr); } void UnloadRegion(int x, int y) { //snap the coords - x = snapToBase(regionWidth, x); - y = snapToBase(regionHeight, y); + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + //custom loop for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { if ((*it)->GetX() == x && (*it)->GetY() == y) { generator.Unload(*it); diff --git a/todo.txt b/todo.txt index 5e0beac..f60a8bc 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,11 @@ #These were partially scraped from the TODO utility -1. Implement the preprocessor values throughout the map system +*. Implement the preprocessor values throughout the map system +*. Rename MapGenerator to MapAllocator +-. Does the format need to be a functor too? +-. Can the format functor go inside the allocator? +5. lua API + 4. in_world.cpp:487 make the region units official 5. region_pager.hpp:50 delete? 16. editor_scene.cpp:99 skip the out-of-bounds regions From fba183fa27ffeb9da765b7e9b514bbbfdab9862f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 03:55:52 +1000 Subject: [PATCH 4/8] Brought the common/ directory up to date with the region's preprocessors I really hope the serialization code still works. --- common/graphics/tile_sheet.cpp | 6 ++-- common/network/network_packet.hpp | 2 +- common/network/serial.cpp | 53 +++++++++---------------------- common/network/serial.hpp | 9 +++--- common/script/region_api.cpp | 9 ++---- todo.txt | 2 +- 6 files changed, 28 insertions(+), 53 deletions(-) diff --git a/common/graphics/tile_sheet.cpp b/common/graphics/tile_sheet.cpp index cc96ec5..e1b9ea2 100644 --- a/common/graphics/tile_sheet.cpp +++ b/common/graphics/tile_sheet.cpp @@ -44,9 +44,9 @@ void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t til void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY) { Region::type_t tile = 0; - for (register int i = 0; i < region->GetWidth(); ++i) { - for (register int j = 0; j < region->GetHeight(); ++j) { - for (register int k = 0; k < region->GetDepth(); ++k) { + 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) { tile = region->GetTile(i, j, k); //0 is invisible if (tile == 0) continue; diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index 68b91ee..79c9af2 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -107,7 +107,7 @@ union NetworkPacket { //map data struct RegionInformation { Metadata meta; - int width, height, depth, x, y; + int x, y; Region* region; }regionInfo; diff --git a/common/network/serial.cpp b/common/network/serial.cpp index 2aa88cb..5130841 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -21,7 +21,7 @@ */ #include "serial.hpp" -#include "map_generator.hpp" +#include "map_allocator.hpp" #include @@ -75,14 +75,6 @@ void serializeRegionFormat(NetworkPacket* packet, char* buffer) { memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); - //size - memcpy(buffer, &packet->regionInfo.width, sizeof(int)); - buffer += sizeof(int); - memcpy(buffer, &packet->regionInfo.height, sizeof(int)); - buffer += sizeof(int); - memcpy(buffer, &packet->regionInfo.depth, sizeof(int)); - buffer += sizeof(int); - //x & y memcpy(buffer, &packet->regionInfo.x, sizeof(int)); buffer += sizeof(int); @@ -94,14 +86,6 @@ void serializeRegionContent(NetworkPacket* packet, char* buffer) { memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); - //size - *reinterpret_cast(buffer) = packet->regionInfo.region->GetWidth(); - buffer += sizeof(int); - *reinterpret_cast(buffer) = packet->regionInfo.region->GetHeight(); - buffer += sizeof(int); - *reinterpret_cast(buffer) = packet->regionInfo.region->GetDepth(); - buffer += sizeof(int); - //x & y *reinterpret_cast(buffer) = packet->regionInfo.region->GetX(); buffer += sizeof(int); @@ -109,9 +93,9 @@ void serializeRegionContent(NetworkPacket* packet, char* buffer) { buffer += sizeof(int); //content - for (register int i = 0; i < packet->regionInfo.region->GetWidth(); i++) { - for (register int j = 0; j < packet->regionInfo.region->GetHeight(); j++) { - for (register int k = 0; k < packet->regionInfo.region->GetDepth(); k++) { + 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++) { *reinterpret_cast(buffer) = packet->regionInfo.region->GetTile(i, j, k); buffer += sizeof(Region::type_t); } @@ -169,14 +153,6 @@ void deserializeRegionFormat(NetworkPacket* packet, char* buffer) { memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); - //size - memcpy(&packet->regionInfo.width, buffer, sizeof(int)); - buffer += sizeof(int); - memcpy(&packet->regionInfo.height, buffer, sizeof(int)); - buffer += sizeof(int); - memcpy(&packet->regionInfo.depth, buffer, sizeof(int)); - buffer += sizeof(int); - //x & y memcpy(&packet->regionInfo.x, buffer, sizeof(int)); buffer += sizeof(int); @@ -184,23 +160,24 @@ void deserializeRegionFormat(NetworkPacket* packet, char* buffer) { } void deserializeRegionContent(NetworkPacket* packet, char* buffer) { - //format - deserializeRegionFormat(packet, buffer); - buffer += sizeof(int) * 5 + sizeof(NetworkPacket::Type); + memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); + buffer += sizeof(NetworkPacket::Type); + + //x & y + memcpy(&packet->regionInfo.x, buffer, sizeof(int)); + buffer += sizeof(int); + memcpy(&packet->regionInfo.y, buffer, sizeof(int)); //content - BlankGenerator().Create( + BlankAllocator().Create( &packet->regionInfo.region, - packet->regionInfo.width, - packet->regionInfo.height, - packet->regionInfo.depth, packet->regionInfo.x, packet->regionInfo.y ); - for (register int i = 0; i < packet->regionInfo.region->GetWidth(); i++) { - for (register int j = 0; j < packet->regionInfo.region->GetHeight(); j++) { - for (register int k = 0; k < packet->regionInfo.region->GetDepth(); k++) { + 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++) { packet->regionInfo.region->SetTile(i, j, k, *reinterpret_cast(buffer)); buffer += sizeof(Region::type_t); } diff --git a/common/network/serial.hpp b/common/network/serial.hpp index 8680c8a..f2534dc 100644 --- a/common/network/serial.hpp +++ b/common/network/serial.hpp @@ -24,12 +24,13 @@ #include "network_packet.hpp" -/* Sending regions are the largest type of packet - * content: width * height * depth * sizoeof(type) - * map format: sizeof(int) * 5 +/* TODO: Keep the PACKET_BUFFER_SIZE up to date + * NOTE: REGION_CONTENT is currently the largest type of packet + * map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t) + * map format: sizeof(int) * 2 * metadata: sizeof(metadata) */ -#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 5 + sizeof(NetworkPacket::Metadata) +#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 2 + sizeof(NetworkPacket::Metadata) void serialize(NetworkPacket* const, void*); void deserialize(NetworkPacket* const, void*); diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index 48380d3..e71831f 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -37,20 +37,17 @@ static int getTile(lua_State* L) { } static int getWidth(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetWidth()); + lua_pushinteger(L, REGION_WIDTH); return 1; } static int getHeight(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetHeight()); + lua_pushinteger(L, REGION_HEIGHT); return 1; } static int getDepth(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetDepth()); + lua_pushinteger(L, REGION_DEPTH); return 1; } diff --git a/todo.txt b/todo.txt index f60a8bc..7e60edd 100644 --- a/todo.txt +++ b/todo.txt @@ -4,7 +4,7 @@ *. Rename MapGenerator to MapAllocator -. Does the format need to be a functor too? -. Can the format functor go inside the allocator? -5. lua API +*. lua API 4. in_world.cpp:487 make the region units official 5. region_pager.hpp:50 delete? From eb0b18af6fb6ae6eea06871b9019b86493eeaa3f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 04:27:28 +1000 Subject: [PATCH 5/8] 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. --- client/scenes/in_world.cpp | 45 +++++++++++++++++------------------ client/scenes/in_world.hpp | 5 ++-- common/map/region_pager.hpp | 22 ++++++++--------- common/network/serial.cpp | 1 + editor/editor_scene.cpp | 7 +----- editor/editor_scene.hpp | 6 ++--- server/server_application.cpp | 5 +--- server/server_application.hpp | 4 ++-- 8 files changed, 44 insertions(+), 51 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 1890699..38b39c1 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -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); } } diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 278c007..62261bc 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -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 mapPager; + RegionPager regionPager; //UI Button disconnectButton; Button shutDownButton; + //TODO: Fix the camera struct { int x = 0, y = 0; int width = 0, height = 0; diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 8414165..0bf035e 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -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 regionList; }; -template +template 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::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 diff --git a/common/network/serial.cpp b/common/network/serial.cpp index 5130841..093c42d 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -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( diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index af3a42c..c0d0d73 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -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++) { diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index ecfc291..0ba8734 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -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 pager; + RegionPager pager; TileSheet tsheet; }; diff --git a/server/server_application.cpp b/server/server_application.cpp index b170355..56a9b24 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -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/"); diff --git a/server/server_application.hpp b/server/server_application.hpp index 6609994..8a10aa5 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -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 regionPager; + RegionPager regionPager; //misc bool running = true; From 894b46c5db5342fb5f21480b768bc69b7e5f881e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 05:14:25 +1000 Subject: [PATCH 6/8] Found the error in the format functor I shouldn't automatically guess where an error is, since this project is so complex. --- common/map/map_file_format.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index d27c664..18374aa 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -33,7 +33,8 @@ void DummyFormat::Save(Region* const ptr) { void LuaFormat::Load(Region** const ptr, int x, int y) { //something to load into - if (!ptr) { + + if (!(*ptr)) { (*ptr) = new Region(x, y); } From 35d463d4ba8d184184845a074b471c6944d8d0c5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 05:30:08 +1000 Subject: [PATCH 7/8] Removed some annoying debugging messages --- client/scenes/in_world.cpp | 17 --------- rsc/scripts/setup_server.lua | 10 +++--- server/server_application.cpp | 66 +++++++++++++---------------------- server/server_application.hpp | 4 +-- server/server_utility.cpp | 40 +++++++++++++++++++++ server/server_utility.hpp | 31 ++++++++++++++++ 6 files changed, 102 insertions(+), 66 deletions(-) create mode 100644 server/server_utility.cpp create mode 100644 server/server_utility.hpp diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 38b39c1..62ad57c 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -27,11 +27,6 @@ #include #include -//debugging -#include -using std::cout; -using std::endl; - //------------------------- //Public access members //------------------------- @@ -350,15 +345,6 @@ void InWorld::HandleRegionContent(NetworkPacket packet) { } regionPager.PushRegion(packet.regionInfo.region); packet.regionInfo.region = nullptr; - - //debugging - cout << "Received region: " << packet.regionInfo.x << ", " << packet.regionInfo.y << endl; - if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) { - cout << "Success" << endl; - } - else { - cout << "Failure" << endl; - } } //------------------------- @@ -472,9 +458,6 @@ void InWorld::UpdateMap() { //prune distant regions for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) { if (CheckBufferDistance(*it) > 2) { - //debugging - cout << "unloading: " << (*it)->GetX() << ", " << (*it)->GetY() << endl; - regionPager.UnloadRegion((*it)->GetX(), (*it)->GetY()); //reset diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index c201054..1a7cc8a 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,7 +1,7 @@ print("Lua script check OK (./rsc)") function Region.Create(r) - print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")") +-- print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")") for i = 1, Region.GetWidth(r) do for j = 1, Region.GetHeight(r) do if math.abs(i) == math.abs(j) then @@ -11,19 +11,19 @@ function Region.Create(r) end end end - print("done") +-- print("done") end function Region.Unload(r) - print("Region:Unload(r", Region.GetX(r), Region.GetY(r), ")") +-- print("Region:Unload(r", Region.GetX(r), Region.GetY(r), ")") end --return true if file loaded, otherwise return false function Region.Load(r, saveDir) - print("Region:Load(r,", saveDir, Region.GetX(r), Region.GetY(r), ")") +-- print("Region:Load(r,", saveDir, Region.GetX(r), Region.GetY(r), ")") return false end function Region.Save(r, saveDir) - print("Region:Save(r,", saveDir, Region.GetX(r), Region.GetY(r), ")") +-- print("Region:Save(r,", saveDir, Region.GetX(r), Region.GetY(r), ")") end diff --git a/server/server_application.cpp b/server/server_application.cpp index 56a9b24..78e42b7 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -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 @@ -26,31 +26,13 @@ #include #include #include -#include - -using namespace std; - -int runSQLScript(sqlite3* db, std::string fname) { - ifstream is(fname); - if (!is.is_open()) { - return -1; - } - string script; - getline(is, script, '\0'); - is.close(); - //NOTE: flesh out this error if needed - if (sqlite3_exec(db, script.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { - return -2; - } - return 0; -} //------------------------- //Define the public members //------------------------- void ServerApplication::Init(int argc, char** argv) { - cout << "Beginning startup" << endl; + std::cout << "Beginning startup" << std::endl; //initial setup ClientEntry::uidCounter = 0; @@ -59,43 +41,43 @@ void ServerApplication::Init(int argc, char** argv) { //Init SDL if (SDL_Init(0)) { - throw(runtime_error("Failed to initialize SDL")); + throw(std::runtime_error("Failed to initialize SDL")); } - cout << "Initialized SDL" << endl; + std::cout << "Initialized SDL" << std::endl; //Init SDL_net if (SDLNet_Init()) { - throw(runtime_error("Failed to initialize SDL_net")); + throw(std::runtime_error("Failed to initialize SDL_net")); } network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE); - cout << "Initialized SDL_net" << endl; + std::cout << "Initialized SDL_net" << std::endl; //Init SQL int ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr); if (ret != SQLITE_OK || !database) { - throw(runtime_error(string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) )); + throw(std::runtime_error(std::string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) )); } - cout << "Initialized SQL" << endl; + std::cout << "Initialized SQL" << std::endl; //setup the database if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) { - throw(runtime_error("Failed to initialize SQL's setup script")); + throw(std::runtime_error("Failed to initialize SQL's setup script")); } - cout << "Initialized SQL's setup script" << endl; + std::cout << "Initialized SQL's setup script" << std::endl; //lua luaState = luaL_newstate(); if (!luaState) { - throw(runtime_error("Failed to initialize lua")); + throw(std::runtime_error("Failed to initialize lua")); } luaL_openlibs(luaState); - cout << "Initialized lua" << endl; + std::cout << "Initialized lua" << std::endl; //run the startup script if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) { - throw(runtime_error(string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) )); + throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) )); } - cout << "Initialized lua's setup script" << endl; + std::cout << "Initialized lua's setup script" << std::endl; //setup the map object regionPager.GetAllocator()->SetLuaState(luaState); @@ -105,12 +87,12 @@ void ServerApplication::Init(int argc, char** argv) { //TODO: pass args to the generator & format as needed //NOTE: I might need to rearrange the init process so that lua & SQL can interact // with the map system as needed. - cout << "Initialized the map system" << endl; - cout << "\tsizeof(NetworkPacket): " << sizeof(NetworkPacket) << endl; - cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << endl; + std::cout << "Initialized the map system" << std::endl; + std::cout << "\tsizeof(NetworkPacket): " << sizeof(NetworkPacket) << std::endl; + std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl; //finalize the startup - cout << "Startup completed successfully" << endl; + std::cout << "Startup completed successfully" << std::endl; //debugging // @@ -135,7 +117,7 @@ void ServerApplication::Loop() { } void ServerApplication::Quit() { - cout << "Shutting down" << endl; + std::cout << "Shutting down" << std::endl; //empty the members regionPager.UnloadAll(); @@ -145,7 +127,7 @@ void ServerApplication::Quit() { network.Close(); SDLNet_Quit(); SDL_Quit(); - cout << "Shutdown finished" << endl; + std::cout << "Shutdown finished" << std::endl; } //------------------------- @@ -183,7 +165,7 @@ void ServerApplication::HandlePacket(NetworkPacket packet) { break; //handle errors default: - throw(runtime_error("Unknown NetworkPacket::Type encountered")); + throw(std::runtime_error("Unknown NetworkPacket::Type encountered")); break; } } @@ -221,7 +203,7 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { //finished this routine ClientEntry::uidCounter++; - cout << "Connect, total: " << clientMap.size() << endl; + std::cout << "Connect, total: " << clientMap.size() << std::endl; } void ServerApplication::HandleDisconnect(NetworkPacket packet) { @@ -255,7 +237,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) { }); //finished this routine - cout << "Disconnect, total: " << clientMap.size() << endl; + std::cout << "Disconnect, total: " << clientMap.size() << std::endl; } void ServerApplication::HandleSynchronize(NetworkPacket packet) { @@ -290,7 +272,7 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) { PumpPacket(packet); //finished this routine - cout << "Shutdown signal accepted" << endl; + std::cout << "Shutdown signal accepted" << std::endl; } void ServerApplication::HandlePlayerNew(NetworkPacket packet) { diff --git a/server/server_application.hpp b/server/server_application.hpp index 8a10aa5..8aa7211 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -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 @@ -23,6 +23,7 @@ #define SERVERAPPLICATION_HPP_ //server specific stuff +#include "server_utility.hpp" #include "client_entry.hpp" #include "player_entry.hpp" @@ -47,7 +48,6 @@ //STL #include -#include //The main application class class ServerApplication { diff --git a/server/server_utility.cpp b/server/server_utility.cpp new file mode 100644 index 0000000..ebecbbd --- /dev/null +++ b/server/server_utility.cpp @@ -0,0 +1,40 @@ +/* Copyright: (c) Kayne Ruse 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 + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "server_utility.hpp" + +#include + + +int runSQLScript(sqlite3* db, std::string fname) { + std::ifstream is(fname); + if (!is.is_open()) { + return -1; + } + std::string script; + getline(is, script, '\0'); + is.close(); + //NOTE: flesh out this error if needed + if (sqlite3_exec(db, script.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { + return -2; + } + return 0; +} \ No newline at end of file diff --git a/server/server_utility.hpp b/server/server_utility.hpp new file mode 100644 index 0000000..c0fe9b9 --- /dev/null +++ b/server/server_utility.hpp @@ -0,0 +1,31 @@ +/* Copyright: (c) Kayne Ruse 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 + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef SERVERUTILITY_HPP_ +#define SERVERUTILITY_HPP_ + +#include "sqlite3/sqlite3.h" + +#include + +int runSQLScript(sqlite3* db, std::string fname); + +#endif From 69765de4330f9bf66d9a02e20dd904ef2b6166d2 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 20 Apr 2014 05:34:15 +1000 Subject: [PATCH 8/8] Removed the TODO file; I think it's fairly stable again --- client/scenes/in_world.cpp | 2 +- server/server_application.cpp | 5 ++--- todo.txt | 17 ----------------- 3 files changed, 3 insertions(+), 21 deletions(-) delete mode 100644 todo.txt diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 62ad57c..415c811 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -467,7 +467,7 @@ void InWorld::UpdateMap() { ++it; } - //TODO: make the region units official + //TODO: make the region units official? int regionUnitX = REGION_WIDTH * tileSheet.GetTileW(); int regionUnitY = REGION_HEIGHT * tileSheet.GetTileH(); diff --git a/server/server_application.cpp b/server/server_application.cpp index 78e42b7..e8868cc 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -32,6 +32,7 @@ //------------------------- void ServerApplication::Init(int argc, char** argv) { + //NOTE: I might need to rearrange the init process so that lua & SQL can interact with the map system as needed. std::cout << "Beginning startup" << std::endl; //initial setup @@ -84,9 +85,7 @@ void ServerApplication::Init(int argc, char** argv) { regionPager.GetFormat()->SetLuaState(luaState); //TODO: config parameter regionPager.GetFormat()->SetSaveDir("save/mapname/"); - //TODO: pass args to the generator & format as needed - //NOTE: I might need to rearrange the init process so that lua & SQL can interact - // with the map system as needed. + std::cout << "Initialized the map system" << std::endl; std::cout << "\tsizeof(NetworkPacket): " << sizeof(NetworkPacket) << std::endl; std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl; diff --git a/todo.txt b/todo.txt deleted file mode 100644 index 7e60edd..0000000 --- a/todo.txt +++ /dev/null @@ -1,17 +0,0 @@ -#These were partially scraped from the TODO utility - -*. Implement the preprocessor values throughout the map system -*. Rename MapGenerator to MapAllocator --. Does the format need to be a functor too? --. Can the format functor go inside the allocator? -*. lua API - -4. in_world.cpp:487 make the region units official -5. region_pager.hpp:50 delete? -16. editor_scene.cpp:99 skip the out-of-bounds regions -24. server_application.cpp:106 config parameter -25. server_application.cpp:108 pass args to the generator & format as needed -31. server_application.cpp:264 compensate for large distances -32. server_application.cpp:270 map? -36. server_application.hpp:77 a function that sends to players in a certain proximity -37. server_application.hpp:90 I need to handle multiple map objects