diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 2f72466..415c811 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 //------------------------- @@ -63,14 +58,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 +135,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,20 +340,11 @@ 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)) { - cout << "Success" << endl; - } - else { - cout << "Failure" << endl; - } } //------------------------- @@ -421,7 +405,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,45 +438,43 @@ 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)); } +//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(); + //TODO: make the region units official? + 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/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/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..18374aa 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -23,33 +23,20 @@ #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 8b2517a..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,12 +35,10 @@ 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 - - //TODO: revert this try/catch point Region* ptr = nullptr; ptr = FindRegion(x, y); if (ptr) return ptr; @@ -62,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..0bf035e 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -29,9 +29,8 @@ class RegionPagerBase { public: - RegionPagerBase() = default; - RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); - virtual ~RegionPagerBase(); + RegionPagerBase() {}; + virtual ~RegionPagerBase() {}; //tile manipulation Region::type_t SetTile(int x, int y, int z, Region::type_t v); @@ -50,86 +49,66 @@ 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; }; -template +template class RegionPager : public RegionPagerBase { public: - RegionPager() = default; - RegionPager(int w, int h, int d): - RegionPagerBase(w, h, d) - { - //EMPTY - } + RegionPager() {}; ~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; + allocator.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); + allocator.Unload(*it); regionList.erase(it); //reset the loop, because of reasons @@ -141,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/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..093c42d 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,25 @@ 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)); + 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/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/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 0ce7bd3..e8868cc 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,14 @@ #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(); - //TODO: 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; + //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 ClientEntry::uidCounter = 0; @@ -59,61 +42,56 @@ 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.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/"); - //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 // @@ -138,7 +116,7 @@ void ServerApplication::Loop() { } void ServerApplication::Quit() { - cout << "Shutting down" << endl; + std::cout << "Shutting down" << std::endl; //empty the members regionPager.UnloadAll(); @@ -148,7 +126,7 @@ void ServerApplication::Quit() { network.Close(); SDLNet_Quit(); SDL_Quit(); - cout << "Shutdown finished" << endl; + std::cout << "Shutdown finished" << std::endl; } //------------------------- @@ -186,7 +164,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; } } @@ -201,6 +179,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); @@ -223,7 +202,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) { @@ -257,7 +236,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) { @@ -292,7 +271,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) { @@ -331,6 +310,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")); } diff --git a/server/server_application.hpp b/server/server_application.hpp index 6609994..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,11 +23,12 @@ #define SERVERAPPLICATION_HPP_ //server specific stuff +#include "server_utility.hpp" #include "client_entry.hpp" #include "player_entry.hpp" //maps -#include "map_generator.hpp" +#include "map_allocator.hpp" #include "map_file_format.hpp" #include "region_pager.hpp" @@ -47,7 +48,6 @@ //STL #include -#include //The main application class class ServerApplication { @@ -88,7 +88,7 @@ private: //maps //TODO: I need to handle multiple map objects - RegionPager regionPager; + RegionPager regionPager; //misc bool running = true; 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