From 7e500027e338980144144399347c29b5762686ea Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 15 Mar 2014 19:15:58 +1100 Subject: [PATCH 01/10] Loosened the requirements for constructing a MapPager This is so that I can configure the size of the pages in the config.cfg file. --- common/map/region_pager.cpp | 4 ---- common/map/region_pager.hpp | 23 +++++++++++++---------- editor/editor_scene.cpp | 9 +++++++-- rsc/config.cfg | 5 +++++ server/server_application.cpp | 9 +++++++++ server/server_application.hpp | 16 ++++++++++++---- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index a251ee8..11baea9 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -62,7 +62,3 @@ Region* RegionPagerBase::GetRegion(int x, int y) { if (ptr) return ptr; return CreateRegion(x, y); } - -void RegionPagerBase::Update() { - //TODO -} diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 30c6f64..9759a69 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -29,15 +29,13 @@ class RegionPagerBase { public: - RegionPagerBase() = delete; + RegionPagerBase() = default; RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); virtual ~RegionPagerBase(); int SetTile(int x, int y, int z, int v); int GetTile(int x, int y, int z); - void Update(); - Region* GetRegion(int x, int y); //interface @@ -47,20 +45,25 @@ public: virtual void UnloadRegion(int x, int y) = 0; //accessors - int GetRegionWidth() { return regionWidth; } - int GetRegionHeight() { return regionHeight; } - int GetRegionDepth() { return regionDepth; } + //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; } protected: - const int regionWidth; - const int regionHeight; - const int regionDepth; + int regionWidth; + int regionHeight; + int regionDepth; std::list regionList; }; template class RegionPager : public RegionPagerBase { public: - RegionPager() = delete; + RegionPager() = default; RegionPager(int w, int h, int d): RegionPagerBase(w, h, d) { diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 07403bf..816c304 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -34,8 +34,7 @@ using namespace std; //------------------------- EditorScene::EditorScene(ConfigUtility* const arg1): - config(*arg1), - pager(20, 20, 3) + config(*arg1) { //create the debugging "window" debugInfo.CreateSurface(256, 256); @@ -56,6 +55,11 @@ EditorScene::EditorScene(ConfigUtility* const arg1): {"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"} }); + //setup the map + pager.SetRegionWidth(config.Int("map.pager.width")); + pager.SetRegionHeight(config.Int("map.pager.height")); + pager.SetRegionDepth(config.Int("map.pager.depth")); + //debug tsheet.Load("rsc\\graphics\\tilesets\\sand.bmp", 12, 3); } @@ -85,6 +89,7 @@ void EditorScene::Render(SDL_Surface* const screen) { for (int i = 0; i < pager.GetRegionWidth()*2; i++) { for (int j = 0; j < pager.GetRegionHeight()*2; j++) { for (int k = 0; k < pager.GetRegionDepth(); k++) { + //TODO: skip the out-of-bounds regions tsheet.DrawTo( screen, i*tsheet.GetTileW()-camera.x, diff --git a/rsc/config.cfg b/rsc/config.cfg index 7790217..0b5fec0 100644 --- a/rsc/config.cfg +++ b/rsc/config.cfg @@ -17,6 +17,11 @@ dir.tilesets = rsc/graphics/tilesets/ dir.interface = rsc/graphics/interface/ dir.scripts = rsc/scripts/ +#map system +map.pager.width = 20 +map.pager.height = 20 +map.pager.depth = 3 + #player options player.handle = username player.avatar = elliot2.bmp diff --git a/server/server_application.cpp b/server/server_application.cpp index 56d1e4f..6c11873 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -96,6 +96,15 @@ void ServerApplication::Init(int argc, char** argv) { } cout << "Initialized lua's setup script" << endl; + //setup the map object + mapPager.SetRegionWidth(config.Int("map.pager.width")); + mapPager.SetRegionHeight(config.Int("map.pager.height")); + mapPager.SetRegionDepth(config.Int("map.pager.depth")); + //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; + //finalize the startup cout << "Startup completed successfully" << endl; } diff --git a/server/server_application.hpp b/server/server_application.hpp index c131958..3d43ca4 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -22,20 +22,25 @@ #ifndef SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_ +//maps +#include "map_generator.hpp" +#include "map_file_format.hpp" +#include "region_pager.hpp" + //networking #include "network_packet.hpp" #include "udp_network_utility.hpp" #include "serial.hpp" +//common +#include "config_utility.hpp" +#include "vector2.hpp" + //APIs #include "lua/lua.hpp" #include "sqlite3/sqlite3.h" #include "SDL/SDL.h" -//common -#include "config_utility.hpp" -#include "vector2.hpp" - //STL #include #include @@ -79,6 +84,9 @@ private: void PumpPacket(NetworkPacket); + //maps + RegionPager mapPager; + //networking UDPNetworkUtility network; From d5b551cec3d17f4be65b2db5524ab0c9a62443a7 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 15 Mar 2014 22:59:57 +1100 Subject: [PATCH 02/10] Reduced the footprint of the tiles Also prepping for serializing the regions. --- common/map/region.cpp | 20 ++++++++++---------- common/map/region.hpp | 23 +++++++++++++++-------- common/map/region_pager.cpp | 4 ++-- common/map/region_pager.hpp | 4 ++-- common/network/makefile | 2 +- common/network/network_packet.hpp | 14 +++++++++++--- editor/editor_scene.cpp | 8 ++++---- server/server_application.cpp | 7 ++++--- 8 files changed, 49 insertions(+), 33 deletions(-) diff --git a/common/map/region.cpp b/common/map/region.cpp index 9f4f68e..8dd701b 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -28,12 +28,12 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): x(argX), y(argY) { - tiles = new int**[width]; - for (int i = 0; i < width; ++i) { - tiles[i] = new int*[height]; - for (int j = 0; j < height; ++j) { - tiles[i][j] = new int[depth]; - for (int k = 0; k < depth; ++k) { + 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; } } @@ -41,8 +41,8 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): } Region::~Region() { - for (int i = 0; i < width; ++i) { - for (int j = 0; j < height; j++) { + for (register int i = 0; i < width; ++i) { + for (register int j = 0; j < height; j++) { delete tiles[i][j]; } delete tiles[i]; @@ -50,10 +50,10 @@ Region::~Region() { delete tiles; } -int Region::SetTile(int x, int y, int z, int v) { +Region::type_t Region::SetTile(int x, int y, int z, type_t v) { return tiles[x][y][z] = v; } -int Region::GetTile(int x, int y, int z) { +Region::type_t Region::GetTile(int x, int y, int z) { return tiles[x][y][z]; } diff --git a/common/map/region.hpp b/common/map/region.hpp index 4d083ad..382c3dd 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -22,21 +22,28 @@ #ifndef REGION_HPP_ #define REGION_HPP_ +//temporary? +#define REGION_WIDTH 20 +#define REGION_HEIGHT 20 +#define REGION_DEPTH 3 + class Region { public: + typedef unsigned short type_t; + Region() = delete; Region(int width, int height, int depth, int x, int y); ~Region(); - int SetTile(int x, int y, int z, int v); - int GetTile(int x, int y, int z); + type_t SetTile(int x, int y, int z, type_t v); + type_t GetTile(int x, int y, int z); //accessors - int GetWidth() { return width; } - int GetHeight() { return height; } - int GetDepth() { return depth; } - int GetX() { return x; } - int GetY() { return y; } + 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; @@ -44,7 +51,7 @@ private: const int x; const int y; - int*** tiles = nullptr; + type_t*** tiles = nullptr; }; #endif diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 11baea9..bbef22d 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -35,12 +35,12 @@ RegionPagerBase::~RegionPagerBase() { //EMPTY } -int RegionPagerBase::SetTile(int x, int y, int z, int v) { +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); } -int RegionPagerBase::GetTile(int x, int y, int z) { +Region::type_t RegionPagerBase::GetTile(int x, int y, int z) { Region* ptr = GetRegion(x, y); return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z); } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 9759a69..46fc612 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -33,8 +33,8 @@ public: RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); virtual ~RegionPagerBase(); - int SetTile(int x, int y, int z, int v); - int GetTile(int x, int y, int z); + Region::type_t SetTile(int x, int y, int z, Region::type_t v); + Region::type_t GetTile(int x, int y, int z); Region* GetRegion(int x, int y); diff --git a/common/network/makefile b/common/network/makefile index fb50dee..c1b2927 100644 --- a/common/network/makefile +++ b/common/network/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. +INCLUDES+=. .. ../map LIBS+= CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index a031f00..35801d5 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -22,9 +22,10 @@ #ifndef NETWORKPACKET_HPP_ #define NETWORKPACKET_HPP_ -#include "SDL/SDL_net.h" - #include "vector2.hpp" +#include "region.hpp" + +#include "SDL/SDL_net.h" #define PACKET_STRING_SIZE 100 @@ -61,6 +62,9 @@ union NetworkPacket { PLAYER_NEW = 10, PLAYER_DELETE = 11, PLAYER_UPDATE = 12, + + //map data + REGION_CONTENT = 13, }; //metadata on the packet itself @@ -75,6 +79,7 @@ union NetworkPacket { //TODO: version info char name[PACKET_STRING_SIZE]; //TODO: player count + //TODO: map format }serverInfo; //information about the client @@ -95,7 +100,10 @@ union NetworkPacket { }playerInfo; //map data - //... + struct MapInformation { + Metadata meta; + Region* region; + }mapInfo; //defaults NetworkPacket() { diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 816c304..19d414b 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -56,12 +56,12 @@ EditorScene::EditorScene(ConfigUtility* const arg1): }); //setup the map - pager.SetRegionWidth(config.Int("map.pager.width")); - pager.SetRegionHeight(config.Int("map.pager.height")); - pager.SetRegionDepth(config.Int("map.pager.depth")); + pager.SetRegionWidth(REGION_WIDTH); + pager.SetRegionHeight(REGION_HEIGHT); + pager.SetRegionDepth(REGION_DEPTH); //debug - tsheet.Load("rsc\\graphics\\tilesets\\sand.bmp", 12, 3); + tsheet.Load(config["dir.tilesets"] + "sand.bmp", 12, 3); } EditorScene::~EditorScene() { diff --git a/server/server_application.cpp b/server/server_application.cpp index 6c11873..84ff4ea 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -97,13 +97,14 @@ void ServerApplication::Init(int argc, char** argv) { cout << "Initialized lua's setup script" << endl; //setup the map object - mapPager.SetRegionWidth(config.Int("map.pager.width")); - mapPager.SetRegionHeight(config.Int("map.pager.height")); - mapPager.SetRegionDepth(config.Int("map.pager.depth")); + mapPager.SetRegionWidth(REGION_WIDTH); + mapPager.SetRegionHeight(REGION_HEIGHT); + mapPager.SetRegionDepth(REGION_DEPTH); //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; //finalize the startup cout << "Startup completed successfully" << endl; From 9db86c19f678afde3406f84aa252f621b719d41d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 15 Mar 2014 23:36:31 +1100 Subject: [PATCH 03/10] Implemented a macro for the serial buffer size --- client/client_application.cpp | 2 +- client/scenes/in_world.cpp | 18 +++++++++--------- client/scenes/lobby_menu.cpp | 8 ++++---- common/network/network_packet.hpp | 4 ++++ common/network/serial.cpp | 26 ++++++++++++++++++++++++++ server/server_application.cpp | 23 ++++++++++++----------- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/client/client_application.cpp b/client/client_application.cpp index faac031..2bf9203 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -68,7 +68,7 @@ void ClientApplication::Init() { if (SDLNet_Init()) { throw(std::runtime_error("Failed to initialize SDL_net")); } - network.Open(0, sizeof(NetworkPacket)); + network.Open(0, PACKET_BUFFER_SIZE); } void ClientApplication::Proc() { diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index b156064..2ad41a5 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -65,14 +65,14 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet packet.playerInfo.motion = {0,0}; //send it - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); + network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); //request a sync packet.meta.type = NetworkPacket::Type::SYNCHRONIZE; serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); + network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } InWorld::~InWorld() { @@ -290,7 +290,7 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) { void InWorld::SendState() { NetworkPacket packet; - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; //pack the packet packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE; @@ -302,27 +302,27 @@ void InWorld::SendState() { packet.playerInfo.motion = localCharacter->GetMotion(); serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); + network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } void InWorld::RequestDisconnect() { NetworkPacket packet; - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; //send a disconnect request packet.meta.type = NetworkPacket::Type::DISCONNECT; packet.clientInfo.index = clientIndex; serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); + network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } void InWorld::RequestShutDown() { NetworkPacket packet; - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; //send a shutdown request packet.meta.type = NetworkPacket::Type::SHUTDOWN; packet.clientInfo.index = clientIndex; serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); + network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } \ No newline at end of file diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 4b1c5fb..b7827c6 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -127,12 +127,12 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (search.MouseButtonUp(button) == Button::State::HOVER) { //the vars NetworkPacket packet; - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; //broadcast to the network, or a specific server packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST; serialize(&packet, buffer); - network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, sizeof(NetworkPacket)); + network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE); //reset the server list serverInfo.clear(); @@ -142,12 +142,12 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr) { //the vars NetworkPacket packet; - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; //join the selected server packet.meta.type = NetworkPacket::Type::JOIN_REQUEST; serialize(&packet, buffer); - network.Send(&selection->address, buffer, sizeof(NetworkPacket)); + network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE); selection = nullptr; } diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index 35801d5..8b5ea51 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -27,6 +27,8 @@ #include "SDL/SDL_net.h" +#include + #define PACKET_STRING_SIZE 100 #pragma pack(push, 0) @@ -114,4 +116,6 @@ union NetworkPacket { #pragma pack(pop) +#define PACKET_BUFFER_SIZE std::max(sizeof(NetworkPacket), REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(NetworkPacket::Metadata)) + #endif diff --git a/common/network/serial.cpp b/common/network/serial.cpp index c6f263b..bb2cf1a 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -76,6 +76,14 @@ void serializePlayer(NetworkPacket* packet, char* buffer) { memcpy(buffer, &packet->playerInfo.motion.y, sizeof(double)); } +void serializeRegion(NetworkPacket* packet, char* buffer) { +// cout << "serializeRegion" << endl; + memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); + buffer += sizeof(NetworkPacket::Type); + + //TODO +} + //------------------------- //internal deserialization functions //------------------------- @@ -126,6 +134,14 @@ void deserializePlayer(NetworkPacket* packet, char* buffer) { memcpy(&packet->playerInfo.motion.y, buffer, sizeof(double)); } +void deserializeRegion(NetworkPacket* packet, char* buffer) { +// cout << "deserializeRegion" << endl; + memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); + buffer += sizeof(NetworkPacket::Type); + + //TODO +} + //------------------------- //the interface functions //------------------------- @@ -160,6 +176,11 @@ void serialize(NetworkPacket* packet, void* buffer) { case NetworkPacket::Type::PLAYER_UPDATE: serializePlayer(packet, reinterpret_cast(buffer)); break; + + //map info + case NetworkPacket::Type::REGION_CONTENT: + serializeRegion(packet, reinterpret_cast(buffer)); + break; } // for (int i = 0; i < sizeof(NetworkPacket); i++) { // cout << ((char*)(buffer))[i]; @@ -199,6 +220,11 @@ void deserialize(NetworkPacket* packet, void* buffer) { case NetworkPacket::Type::PLAYER_UPDATE: deserializePlayer(packet, reinterpret_cast(buffer)); break; + + //map info + case NetworkPacket::Type::REGION_CONTENT: + serializeRegion(packet, reinterpret_cast(buffer)); + break; } // for (int i = 0; i < sizeof(NetworkPacket); i++) { // cout << ((char*)(buffer))[i]; diff --git a/server/server_application.cpp b/server/server_application.cpp index 84ff4ea..a126280 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -66,7 +66,7 @@ void ServerApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(runtime_error("Failed to initialize SDL_net")); } - network.Open(config.Int("server.port"), sizeof(NetworkPacket)); + network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE); cout << "Initialized SDL_net" << endl; //Init SQL @@ -105,6 +105,7 @@ void ServerApplication::Init(int argc, char** argv) { // 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; //finalize the startup cout << "Startup completed successfully" << endl; @@ -189,9 +190,9 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) { //TODO: version info snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str()); //TODO: player count - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; serialize(&packet, buffer); - network.Send(&packet.meta.srcAddress, buffer, sizeof(NetworkPacket)); + network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); } void ServerApplication::HandleJoinRequest(NetworkPacket packet) { @@ -201,13 +202,13 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { clientMap[clientCounter] = c; //send the client their info - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE; packet.clientInfo.index = clientCounter; serialize(&packet, buffer); - network.Send(&clientMap[clientCounter].address, buffer, sizeof(NetworkPacket)); + network.Send(&clientMap[clientCounter].address, buffer, PACKET_BUFFER_SIZE); //finished this routine clientCounter++; @@ -217,9 +218,9 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { void ServerApplication::HandleDisconnect(NetworkPacket packet) { //disconnect the specified client //TODO: authenticate who is disconnecting/kicking - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; serialize(&packet, buffer); - network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket)); + network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE); clientMap.erase(packet.clientInfo.index); //delete players from all clients @@ -245,7 +246,7 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) { //send all the server's data to this client //TODO: compensate for large distances NetworkPacket newPacket; - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; //players newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; @@ -256,7 +257,7 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) { newPacket.playerInfo.position = it.second.position; newPacket.playerInfo.motion = it.second.motion; serialize(&newPacket, buffer); - network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket)); + network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE); } } @@ -334,9 +335,9 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) { void ServerApplication::PumpPacket(NetworkPacket packet) { //I don't really like this, but it'll do for now - char buffer[sizeof(NetworkPacket)]; + char buffer[PACKET_BUFFER_SIZE]; serialize(&packet, buffer); for (auto& it : clientMap) { - network.Send(&it.second.address, buffer, sizeof(NetworkPacket)); + network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE); } } From e4bfbfb906ee034de77c6fb18979601e6437ad15 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 16 Mar 2014 00:28:19 +1100 Subject: [PATCH 04/10] Minor code tweaks, this needs a lot of forethought --- client/scenes/in_world.cpp | 12 ++++++------ client/scenes/in_world.hpp | 32 ++++++++++++++++++++++++++----- common/network/network_packet.hpp | 2 +- common/network/serial.cpp | 10 +++++++++- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 2ad41a5..1c25bf6 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -35,21 +35,21 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet clientIndex(*argClientIndex) { //setup the utility objects - image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); - image.SetClipH(image.GetClipH()/3); + buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); + buttonImage.SetClipH(buttonImage.GetClipH()/3); font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); //pass the utility objects - disconnectButton.SetImage(&image); + disconnectButton.SetImage(&buttonImage); disconnectButton.SetFont(&font); - shutDownButton.SetImage(&image); + shutDownButton.SetImage(&buttonImage); shutDownButton.SetFont(&font); //set the button positions disconnectButton.SetX(50); - disconnectButton.SetY(50 + image.GetClipH() * 0); + disconnectButton.SetY(50 + buttonImage.GetClipH() * 0); shutDownButton.SetX(50); - shutDownButton.SetY(50 + image.GetClipH() * 1); + shutDownButton.SetY(50 + buttonImage.GetClipH() * 1); //set the button texts disconnectButton.SetText("Disconnect"); diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index b3b74f9..7fb14c4 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -22,17 +22,29 @@ #ifndef INWORLD_HPP_ #define INWORLD_HPP_ -#include "base_scene.hpp" +//maps +#include "map_generator.hpp" +#include "map_file_format.hpp" +#include "region_pager.hpp" -#include "config_utility.hpp" +//networking #include "udp_network_utility.hpp" #include "network_packet.hpp" #include "serial.hpp" + +//graphics #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" + +//common +#include "config_utility.hpp" + +//client +#include "base_scene.hpp" #include "player_character.hpp" +//STL #include class InWorld : public BaseScene { @@ -66,16 +78,26 @@ protected: void RequestDisconnect(); void RequestShutDown(); - //global + //globals ConfigUtility& config; UDPNetworkUtility& network; int& clientIndex; - //members - Image image; + //graphics + Image buttonImage; RasterFont font; + + //map + RegionPager mapPager; + + //UI Button disconnectButton; Button shutDownButton; + struct { + int x = 0, y = 0; + } camera; + + //game std::map playerCharacters; PlayerCharacter* localCharacter = nullptr; int playerIndex = -1; diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index 8b5ea51..aed56d8 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -116,6 +116,6 @@ union NetworkPacket { #pragma pack(pop) -#define PACKET_BUFFER_SIZE std::max(sizeof(NetworkPacket), REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(NetworkPacket::Metadata)) +#define PACKET_BUFFER_SIZE std::max(sizeof(NetworkPacket), REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 2 + sizeof(NetworkPacket::Metadata)) #endif diff --git a/common/network/serial.cpp b/common/network/serial.cpp index bb2cf1a..de97b46 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -81,7 +81,15 @@ void serializeRegion(NetworkPacket* packet, char* buffer) { memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); - //TODO + //TODO: incomplete +/* for (register int i = 0; i < packet->mapInfo.region->GetWidth(); i++) { + for (register int j = 0; j < packet->mapInfo.region->GetHeight(); j++) { + for (register int k = 0; k < packet->mapInfo.region->GetDepth(); k++) { + memcpy(buffer, &packet->mapInfo.region->GetTile(i, j, k), sizeof(Region::type_t)); + buffer += sizeof(Region::type_t); + } + } + }*/ } //------------------------- From 47684380a9a411a53edd3db670c5d4ff1774e76d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 26 Mar 2014 00:30:10 +1100 Subject: [PATCH 05/10] I'm having trouble with the sequence of events --- common/network/network_packet.hpp | 12 ++++++--- common/network/serial.cpp | 44 ++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index aed56d8..ce0c112 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -66,7 +66,8 @@ union NetworkPacket { PLAYER_UPDATE = 12, //map data - REGION_CONTENT = 13, + REGION_REQUEST = 13, + REGION_CONTENT = 14, }; //metadata on the packet itself @@ -102,10 +103,11 @@ union NetworkPacket { }playerInfo; //map data - struct MapInformation { + struct RegionInformation { Metadata meta; + int x, y; Region* region; - }mapInfo; + }regionInfo; //defaults NetworkPacket() { @@ -116,6 +118,10 @@ union NetworkPacket { #pragma pack(pop) +/* content: width * height * depth * sizoeof(type) + * map size: sizeof(int) * 2 + * metadata: sizeof(metadata) +*/ #define PACKET_BUFFER_SIZE std::max(sizeof(NetworkPacket), REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 2 + sizeof(NetworkPacket::Metadata)) #endif diff --git a/common/network/serial.cpp b/common/network/serial.cpp index de97b46..2b7abe1 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -76,11 +76,7 @@ void serializePlayer(NetworkPacket* packet, char* buffer) { memcpy(buffer, &packet->playerInfo.motion.y, sizeof(double)); } -void serializeRegion(NetworkPacket* packet, char* buffer) { -// cout << "serializeRegion" << endl; - memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); - buffer += sizeof(NetworkPacket::Type); - +void serializeRegionContent(NetworkPacket* packet, char* buffer) { //TODO: incomplete /* for (register int i = 0; i < packet->mapInfo.region->GetWidth(); i++) { for (register int j = 0; j < packet->mapInfo.region->GetHeight(); j++) { @@ -92,6 +88,22 @@ void serializeRegion(NetworkPacket* packet, char* buffer) { }*/ } +void serializeRegion(NetworkPacket* packet, char* buffer) { +// cout << "serializeRegion" << endl; + memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); + buffer += sizeof(NetworkPacket::Type); + + //x & y + memcpy(buffer, &packet->regionInfo.x, sizeof(int)); + buffer += sizeof(int); + memcpy(buffer, &packet->regionInfo.y, sizeof(int)); + buffer += sizeof(int); + + if (packet->meta.type == NetworkPacket::Type::REGION_CONTENT) { + serializeRegionContent(packet, buffer); + } +} + //------------------------- //internal deserialization functions //------------------------- @@ -142,12 +154,24 @@ void deserializePlayer(NetworkPacket* packet, char* buffer) { memcpy(&packet->playerInfo.motion.y, buffer, sizeof(double)); } +void deserializeRegionContent(NetworkPacket* packet, char* buffer) { + //TODO +} + void deserializeRegion(NetworkPacket* packet, char* buffer) { // cout << "deserializeRegion" << endl; memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); - //TODO + //x & y + memcpy(&packet->regionInfo.x, buffer, sizeof(int)); + buffer += sizeof(int); + memcpy(&packet->regionInfo.y, buffer, sizeof(int)); + buffer += sizeof(int); + + if (packet->meta.type == NetworkPacket::Type::REGION_CONTENT) { + deserializeRegionContent(packet, buffer); + } } //------------------------- @@ -185,7 +209,8 @@ void serialize(NetworkPacket* packet, void* buffer) { serializePlayer(packet, reinterpret_cast(buffer)); break; - //map info + //region info + case NetworkPacket::Type::REGION_REQUEST: case NetworkPacket::Type::REGION_CONTENT: serializeRegion(packet, reinterpret_cast(buffer)); break; @@ -207,7 +232,7 @@ void deserialize(NetworkPacket* packet, void* buffer) { case NetworkPacket::Type::BROADCAST_REQUEST: case NetworkPacket::Type::JOIN_REQUEST: case NetworkPacket::Type::SYNCHRONIZE: - // + //NOTHING break; //Server info @@ -230,8 +255,9 @@ void deserialize(NetworkPacket* packet, void* buffer) { break; //map info + case NetworkPacket::Type::REGION_REQUEST: case NetworkPacket::Type::REGION_CONTENT: - serializeRegion(packet, reinterpret_cast(buffer)); + deserializeRegion(packet, reinterpret_cast(buffer)); break; } // for (int i = 0; i < sizeof(NetworkPacket); i++) { From 38b603fc8fc15dfe47f1f9946d54b908c399dcfd Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Mar 2014 03:24:14 +1100 Subject: [PATCH 06/10] Began working on the lua API for the map The basic framework is done. --- client/scenes/in_world.hpp | 2 +- common/makefile | 1 + common/map/map_file_format.cpp | 21 +++++++++- common/map/map_file_format.hpp | 18 ++++++++- common/map/map_generator.cpp | 25 +++++++++++- common/map/map_generator.hpp | 23 ++++++++++- common/script/linit.cpp | 72 ++++++++++++++++++++++++++++++++++ common/script/makefile | 43 ++++++++++++++++++++ common/script/region_api.cpp | 27 +++++++++++++ common/script/region_api.hpp | 30 ++++++++++++++ editor/editor_scene.hpp | 2 +- editor/makefile | 2 +- server/makefile | 2 +- server/server_application.cpp | 7 ++-- server/server_application.hpp | 2 +- 15 files changed, 263 insertions(+), 14 deletions(-) create mode 100644 common/script/linit.cpp create mode 100644 common/script/makefile create mode 100644 common/script/region_api.cpp create mode 100644 common/script/region_api.hpp diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 7fb14c4..c6ecd72 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -88,7 +88,7 @@ protected: RasterFont font; //map - RegionPager mapPager; + RegionPager mapPager; //UI Button disconnectButton; diff --git a/common/makefile b/common/makefile index c1e25c2..645ac13 100644 --- a/common/makefile +++ b/common/makefile @@ -22,6 +22,7 @@ all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) $(MAKE) -C graphics $(MAKE) -C map + $(MAKE) -C script $(MAKE) -C network $(MAKE) -C ui diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index ee7f58d..ac1b79e 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -21,10 +21,27 @@ */ #include "map_file_format.hpp" -void MapFileFormat::Load(Region** const ptr, int x, int y) { +void DummyFormat::Load(Region** const ptr, int x, int y) { //TODO } -void MapFileFormat::Save(Region* const ptr) { +void DummyFormat::Save(Region* const ptr) { //TODO } +/* +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 +} +*/ \ No newline at end of file diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp index 546003d..576dc50 100644 --- a/common/map/map_file_format.hpp +++ b/common/map/map_file_format.hpp @@ -24,7 +24,15 @@ #include "region.hpp" -class MapFileFormat { +class DummyFormat { +public: + void Load(Region** const, int x, int y); + void Save(Region* const); +private: + // +}; +/* +class VerboseFormat { public: void Load(Region** const, int x, int y); void Save(Region* const); @@ -32,4 +40,12 @@ private: // }; +class CompactFormat { +public: + void Load(Region** const, int x, int y); + void Save(Region* const); +private: + // +}; +*/ #endif diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp index 2668ca5..d0fbbca 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -21,10 +21,31 @@ */ #include "map_generator.hpp" -void MapGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { +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 MapGenerator::Unload(Region* const ptr) { +void BlankGenerator::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) { + (*ptr) = new Region(width, height, depth, x, y); + + //generate the lua-driven maps + lua_getglobal(state, "CreateRegion"); + lua_pushlightuserdata(state, ptr); + lua_pcall(state, 1, 0, 0); +} + +void LuaGenerator::Unload(Region* const ptr) { delete ptr; } diff --git a/common/map/map_generator.hpp b/common/map/map_generator.hpp index 85121b9..f3eba56 100644 --- a/common/map/map_generator.hpp +++ b/common/map/map_generator.hpp @@ -24,12 +24,33 @@ #include "region.hpp" -class MapGenerator { +#include "lua/lua.hpp" + +class BlankGenerator { public: void Create(Region** const, int width, int height, int depth, int x, int y); void Unload(Region* const); private: // }; +/* +class PerlinGenerator { +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 Unload(Region* const); + + lua_State* SetLuaState(lua_State* L) { return state = L; } + lua_State* GetLuaState() { return state; } +private: + lua_State* state = nullptr; +}; #endif diff --git a/common/script/linit.cpp b/common/script/linit.cpp new file mode 100644 index 0000000..c03a812 --- /dev/null +++ b/common/script/linit.cpp @@ -0,0 +1,72 @@ +/* +** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ +** Initialization of libraries for lua.c and other clients +** See Copyright Notice in lua.h +*/ + +/* Modified for use in Tortuga, renamed to linit.cpp +*/ + + +/* +** If you embed Lua in your program and need to open the standard +** libraries, call luaL_openlibs in your program. If you need a +** different set of libraries, copy this file to your project and edit +** it to suit your needs. +*/ + + +#define linit_c +#define LUA_LIB + +#include "lua/lua.hpp" +#include "region_api.hpp" + + +/* +** these libs are loaded by lua.c and are readily available to any Lua +** program +*/ +static const luaL_Reg loadedlibs[] = { + /* Standard libs */ + {"_G", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_COLIBNAME, luaopen_coroutine}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_IOLIBNAME, luaopen_io}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_BITLIBNAME, luaopen_bit32}, + {LUA_MATHLIBNAME, luaopen_math}, + {LUA_DBLIBNAME, luaopen_debug}, + + /* custom libs */ + {LUA_REGIONLIBNAME, luaopen_regionapi}, + + {NULL, NULL} +}; + + +/* +** these libs are preloaded and must be required before used +*/ +static const luaL_Reg preloadedlibs[] = { + {NULL, NULL} +}; + + +LUALIB_API void luaL_openlibs (lua_State *L) { + const luaL_Reg *lib; + /* call open functions from 'loadedlibs' and set results to global table */ + for (lib = loadedlibs; lib->func; lib++) { + luaL_requiref(L, lib->name, lib->func, 1); + lua_pop(L, 1); /* remove lib */ + } + /* add open functions from 'preloadedlibs' into 'package.preload' table */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); + for (lib = preloadedlibs; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); + } + lua_pop(L, 1); /* remove _PRELOAD table */ +} \ No newline at end of file diff --git a/common/script/makefile b/common/script/makefile new file mode 100644 index 0000000..c1b2927 --- /dev/null +++ b/common/script/makefile @@ -0,0 +1,43 @@ +#config +INCLUDES+=. .. ../map +LIBS+= +CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) +CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) +CSRC=$(wildcard *.c) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) +OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) + +#output +OUTDIR=../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +$(OBJDIR)/%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp new file mode 100644 index 0000000..d378835 --- /dev/null +++ b/common/script/region_api.cpp @@ -0,0 +1,27 @@ +/* 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 "region_api.hpp" + +LUAMOD_API int luaopen_regionapi(lua_State* L) { + //TODO: stuff + return 1; +} \ No newline at end of file diff --git a/common/script/region_api.hpp b/common/script/region_api.hpp new file mode 100644 index 0000000..310074d --- /dev/null +++ b/common/script/region_api.hpp @@ -0,0 +1,30 @@ +/* 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 REGIONAPI_HPP_ +#define REGIONAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_REGIONLIBNAME "region" +LUAMOD_API int luaopen_regionapi(lua_State* L); + +#endif diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index e86fa52..ecfc291 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -73,7 +73,7 @@ protected: int x = 0, y = 0; } camera; - RegionPager pager; + RegionPager pager; TileSheet tsheet; }; diff --git a/editor/makefile b/editor/makefile index 7343b2e..f785faa 100644 --- a/editor/makefile +++ b/editor/makefile @@ -1,6 +1,6 @@ #config INCLUDES+=../common ../common/graphics ../common/map ../common/ui -LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL +LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL -llua CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/server/makefile b/server/makefile index 6af6c60..9686a1c 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../common ../common/map ../common/network +INCLUDES+=. ../common ../common/map ../common/script ../common/network LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/server/server_application.cpp b/server/server_application.cpp index a126280..9aeed88 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -100,6 +100,7 @@ void ServerApplication::Init(int argc, char** argv) { mapPager.SetRegionWidth(REGION_WIDTH); mapPager.SetRegionHeight(REGION_HEIGHT); mapPager.SetRegionDepth(REGION_DEPTH); + mapPager.GetGenerator()->SetLuaState(luaState); //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. @@ -212,7 +213,7 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { //finished this routine clientCounter++; - cout << "connect, total: " << clientMap.size() << endl; + cout << "Connect, total: " << clientMap.size() << endl; } void ServerApplication::HandleDisconnect(NetworkPacket packet) { @@ -239,7 +240,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) { }); //finished this routine - cout << "disconnect, total: " << clientMap.size() << endl; + cout << "Disconnect, total: " << clientMap.size() << endl; } void ServerApplication::HandleSynchronize(NetworkPacket packet) { @@ -270,7 +271,7 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) { PumpPacket(packet); //finished this routine - cout << "shutting down" << endl; + cout << "Shutdown signal accepted" << endl; } void ServerApplication::HandlePlayerNew(NetworkPacket packet) { diff --git a/server/server_application.hpp b/server/server_application.hpp index 3d43ca4..4d969a0 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -85,7 +85,7 @@ private: void PumpPacket(NetworkPacket); //maps - RegionPager mapPager; + RegionPager mapPager; //networking UDPNetworkUtility network; From 4cff57fe71577c85adfd287a61008b09da9d2e1b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Mar 2014 04:11:34 +1100 Subject: [PATCH 07/10] Established a connection between the Region objects and lua --- common/map/map_generator.cpp | 2 +- common/script/region_api.cpp | 48 ++++++++++++++++++++++++++++++++++- common/script/region_api.hpp | 2 +- rsc/scripts/setup_server.lua | 6 ++++- server/server_application.cpp | 4 +++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp index d0fbbca..8f7a9ad 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -42,7 +42,7 @@ void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, //generate the lua-driven maps lua_getglobal(state, "CreateRegion"); - lua_pushlightuserdata(state, ptr); + lua_pushlightuserdata(state, *ptr); lua_pcall(state, 1, 0, 0); } diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index d378835..2f5a3c1 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -21,7 +21,53 @@ */ #include "region_api.hpp" +#include "region.hpp" + +static int setTile(lua_State* L) { +// Region* ptr = lua_touserdata(L, 1); +// ptr->SetTile +} + +static int getTile(lua_State* L) { + //TODO +} + +static int getWidth(lua_State* L) { + //TODO +} + +static int getHeight(lua_State* L) { + //TODO +} + +static int getDepth(lua_State* L) { + //TODO +} + +static int getX(lua_State* L) { + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetX()); + return 1; +} + +static int getY(lua_State* L) { + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetY()); + return 1; +} + +static const luaL_Reg regionlib[] = { + {"SetTile",setTile}, + {"GetTile",getTile}, + {"GetWidth",getWidth}, + {"GetHeight",getHeight}, + {"GetDepth",getDepth}, + {"GetX",getX}, + {"GetY",getY}, + {nullptr, nullptr} +}; + LUAMOD_API int luaopen_regionapi(lua_State* L) { - //TODO: stuff + luaL_newlib(L, regionlib); return 1; } \ No newline at end of file diff --git a/common/script/region_api.hpp b/common/script/region_api.hpp index 310074d..f193950 100644 --- a/common/script/region_api.hpp +++ b/common/script/region_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define LUA_REGIONLIBNAME "region" +#define LUA_REGIONLIBNAME "Region" LUAMOD_API int luaopen_regionapi(lua_State* L); #endif diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index a2cf14a..448b25f 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1 +1,5 @@ -print("Lua script check OK") \ No newline at end of file +print("Lua script check OK (./rsc)") + +function CreateRegion(r) + print(Region.GetX(r), Region.GetY(r)) +end \ No newline at end of file diff --git a/server/server_application.cpp b/server/server_application.cpp index 9aeed88..d39223b 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -110,6 +110,10 @@ void ServerApplication::Init(int argc, char** argv) { //finalize the startup cout << "Startup completed successfully" << endl; + + //debugging + mapPager.GetRegion(0,0); + mapPager.GetRegion(128,256); } void ServerApplication::Loop() { From a5b68cf1fd092fabf03a4f0817cb9aeb4f333d4b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 31 Mar 2014 01:26:45 +1100 Subject: [PATCH 08/10] Expaneded the lua API for Regions I've added lua hooks for both pager functor classes. Hopefully, I haven't missed any corner cases, because it took me a while to hunt everything down. One issue is that the map's save directory needs to be set in the Format class, but it'll do for now. I'll review this again when I've got more than one map running at one time. There should be enough here for a lua-driven map generator to be implemented, even if it's a bit rough. I think I'll test this out in the editor eventually, but getting the base branch's network map code going comes first. The current process is extremely convulted, so I need to document everything that I've done so far, including C++ and lua functions. --- common/map/map_file_format.cpp | 36 ++++++++++++++++++++++++++++-- common/map/map_file_format.hpp | 40 +++++++++++++++++++++++++++++----- common/map/map_generator.cpp | 23 ++++++++++++++++--- common/map/region_pager.hpp | 13 +++++++++-- common/script/region_api.cpp | 30 ++++++++++++++++++++----- rsc/scripts/setup_server.lua | 20 ++++++++++++++--- server/server_application.cpp | 6 +++-- server/server_application.hpp | 2 +- 8 files changed, 145 insertions(+), 25 deletions(-) diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index ac1b79e..879d14e 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -21,7 +21,9 @@ */ #include "map_file_format.hpp" -void DummyFormat::Load(Region** const ptr, int x, int y) { +#include + +void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { //TODO } @@ -44,4 +46,34 @@ void CompactFormat::Load(Region** const ptr, int x, int y) { void CompactFormat::Save(Region* const ptr) { //TODO } -*/ \ No newline at end of file +*/ +void LuaFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { + //something to load into + (*ptr) = new Region(width, height, depth, x, y); + + //API hook + lua_getglobal(state, "Region"); + lua_getfield(state, -1, "Load"); + lua_pushlightuserdata(state, *ptr); + lua_pushstring(state, saveDir.c_str()); + if (lua_pcall(state, 2, 1, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); + } + if (lua_toboolean(state, -1) == false) { + delete (*ptr); + (*ptr) = nullptr; + } + lua_pop(state, 2); +} + +void LuaFormat::Save(Region* const ptr) { + //API hook + lua_getglobal(state, "Region"); + lua_getfield(state, -1, "Save"); + lua_pushlightuserdata(state, ptr); + lua_pushstring(state, saveDir.c_str()); + if (lua_pcall(state, 2, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); + } + lua_pop(state, 1); +} \ No newline at end of file diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp index 576dc50..50345e5 100644 --- a/common/map/map_file_format.hpp +++ b/common/map/map_file_format.hpp @@ -24,28 +24,56 @@ #include "region.hpp" +#include "lua/lua.hpp" + +#include + class DummyFormat { public: - void Load(Region** const, int x, int y); + 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 x, int y); + 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 CompactFormat { public: - void Load(Region** const, int x, int y); + 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 LuaFormat { +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; } + + lua_State* SetLuaState(lua_State* L) { return state = L; } + lua_State* GetLuaState() { return state; } +private: + std::string saveDir; + lua_State* state = nullptr; +}; + #endif diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp index 8f7a9ad..eef4b16 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -21,6 +21,8 @@ */ #include "map_generator.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); } @@ -38,14 +40,29 @@ void PerlinGenerator::Unload(Region* const ptr) { } */ void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { + //something to work on (*ptr) = new Region(width, height, depth, x, y); - //generate the lua-driven maps - lua_getglobal(state, "CreateRegion"); + //API hook + lua_getglobal(state, "Region"); + lua_getfield(state, -1, "Create"); lua_pushlightuserdata(state, *ptr); - lua_pcall(state, 1, 0, 0); + if (lua_pcall(state, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); + } + lua_pop(state, 1); } void LuaGenerator::Unload(Region* const ptr) { + //API hook + lua_getglobal(state, "Region"); + lua_getfield(state, -1, "Unload"); + lua_pushlightuserdata(state, ptr); + if (lua_pcall(state, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) )); + } + lua_pop(state, 1); + + //clean up the memory delete ptr; } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 46fc612..bbf1d42 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -43,6 +43,7 @@ public: virtual Region* SaveRegion(int x, int y) = 0; virtual Region* CreateRegion(int x, int y) = 0; virtual void UnloadRegion(int x, int y) = 0; + virtual void UnloadAll() = 0; //accessors //NOTE: don't change the sizes mid-program, it will cause issues @@ -69,7 +70,9 @@ public: { //EMPTY } - ~RegionPager() = default; + ~RegionPager() { + UnloadAll(); + } Region* LoadRegion(int x, int y) { //snap the coords @@ -78,7 +81,7 @@ public: //load the region if possible Region* ptr = nullptr; - format.Load(&ptr, x, y); + format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y); if (ptr) { regionList.push_back(ptr); return ptr; @@ -130,6 +133,12 @@ public: ++it; } } + void UnloadAll() { + for (auto& it : regionList) { + generator.Unload(it); + } + regionList.clear(); + } //accessors MapGenerator* GetGenerator() { return &generator; } diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index 2f5a3c1..48380d3 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -24,24 +24,34 @@ #include "region.hpp" static int setTile(lua_State* L) { -// Region* ptr = lua_touserdata(L, 1); -// ptr->SetTile + Region* ptr = (Region*)lua_touserdata(L, 1); + ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); + return 0; } static int getTile(lua_State* L) { - //TODO + Region* ptr = (Region*)lua_touserdata(L, 1); + int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); + lua_pushnumber(L, ret); + return 1; } static int getWidth(lua_State* L) { - //TODO + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetWidth()); + return 1; } static int getHeight(lua_State* L) { - //TODO + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetHeight()); + return 1; } static int getDepth(lua_State* L) { - //TODO + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetDepth()); + return 1; } static int getX(lua_State* L) { @@ -56,6 +66,10 @@ static int getY(lua_State* L) { return 1; } +static int dummy(lua_State* L) { + return 0; +} + static const luaL_Reg regionlib[] = { {"SetTile",setTile}, {"GetTile",getTile}, @@ -64,6 +78,10 @@ static const luaL_Reg regionlib[] = { {"GetDepth",getDepth}, {"GetX",getX}, {"GetY",getY}, + {"Create", dummy}, + {"Unload", dummy}, + {"Load", dummy}, + {"Save", dummy}, {nullptr, nullptr} }; diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 448b25f..4daf1f4 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,5 +1,19 @@ print("Lua script check OK (./rsc)") -function CreateRegion(r) - print(Region.GetX(r), Region.GetY(r)) -end \ No newline at end of file +function Region.Create(r) + print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")") +end + +function Region.Unload(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), ")") + return false +end + +function Region.Save(r, saveDir) + 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 d39223b..9466ce8 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -101,6 +101,8 @@ void ServerApplication::Init(int argc, char** argv) { mapPager.SetRegionHeight(REGION_HEIGHT); mapPager.SetRegionDepth(REGION_DEPTH); mapPager.GetGenerator()->SetLuaState(luaState); + mapPager.GetFormat()->SetLuaState(luaState); + mapPager.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. @@ -112,8 +114,7 @@ void ServerApplication::Init(int argc, char** argv) { cout << "Startup completed successfully" << endl; //debugging - mapPager.GetRegion(0,0); - mapPager.GetRegion(128,256); + // } void ServerApplication::Loop() { @@ -136,6 +137,7 @@ void ServerApplication::Loop() { void ServerApplication::Quit() { cout << "Shutting down" << endl; //empty the members + mapPager.UnloadAll(); //TODO: player manager //TODO: client manager diff --git a/server/server_application.hpp b/server/server_application.hpp index 4d969a0..d3d30fa 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -85,7 +85,7 @@ private: void PumpPacket(NetworkPacket); //maps - RegionPager mapPager; + RegionPager mapPager; //networking UDPNetworkUtility network; From 60000cb0cf6ad238c4e4511da37ba9e6c482f788 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 31 Mar 2014 03:38:05 +1100 Subject: [PATCH 09/10] Minor tweak --- common/map/region_pager.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index bbf1d42..016cab9 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -43,7 +43,6 @@ public: virtual Region* SaveRegion(int x, int y) = 0; virtual Region* CreateRegion(int x, int y) = 0; virtual void UnloadRegion(int x, int y) = 0; - virtual void UnloadAll() = 0; //accessors //NOTE: don't change the sizes mid-program, it will cause issues From 4410ab892f54208fe9dbaf57a685ff47247fdc74 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 31 Mar 2014 21:40:50 +1100 Subject: [PATCH 10/10] Finished the region serial code --- common/network/network_packet.hpp | 10 +-- common/network/serial.cpp | 126 +++++++++++++++++++----------- common/network/serial.hpp | 7 ++ 3 files changed, 87 insertions(+), 56 deletions(-) diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index ce0c112..f9b4894 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -27,8 +27,6 @@ #include "SDL/SDL_net.h" -#include - #define PACKET_STRING_SIZE 100 #pragma pack(push, 0) @@ -105,7 +103,7 @@ union NetworkPacket { //map data struct RegionInformation { Metadata meta; - int x, y; + int width, height, depth, x, y; Region* region; }regionInfo; @@ -118,10 +116,4 @@ union NetworkPacket { #pragma pack(pop) -/* content: width * height * depth * sizoeof(type) - * map size: sizeof(int) * 2 - * metadata: sizeof(metadata) -*/ -#define PACKET_BUFFER_SIZE std::max(sizeof(NetworkPacket), REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 2 + sizeof(NetworkPacket::Metadata)) - #endif diff --git a/common/network/serial.cpp b/common/network/serial.cpp index 2b7abe1..2aa88cb 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -21,36 +21,31 @@ */ #include "serial.hpp" -#include -//#include +#include "map_generator.hpp" -//using namespace std; +#include //------------------------- //internal serialization functions //------------------------- void serializeType(NetworkPacket* packet, char* buffer) { -// cout << "serializeType" << endl; memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); } void serializeServer(NetworkPacket* packet, char* buffer) { -// cout << "serializeServer" << endl; memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); memcpy(buffer, packet->serverInfo.name, PACKET_STRING_SIZE); } void serializeClient(NetworkPacket* packet, char* buffer) { -// cout << "serializeClient" << endl; memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); memcpy(buffer, &packet->clientInfo.index, sizeof(int)); } void serializePlayer(NetworkPacket* packet, char* buffer) { -// cout << "serializePlayer" << endl; memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); @@ -76,31 +71,51 @@ void serializePlayer(NetworkPacket* packet, char* buffer) { memcpy(buffer, &packet->playerInfo.motion.y, sizeof(double)); } -void serializeRegionContent(NetworkPacket* packet, char* buffer) { - //TODO: incomplete -/* for (register int i = 0; i < packet->mapInfo.region->GetWidth(); i++) { - for (register int j = 0; j < packet->mapInfo.region->GetHeight(); j++) { - for (register int k = 0; k < packet->mapInfo.region->GetDepth(); k++) { - memcpy(buffer, &packet->mapInfo.region->GetTile(i, j, k), sizeof(Region::type_t)); - buffer += sizeof(Region::type_t); - } - } - }*/ -} - -void serializeRegion(NetworkPacket* packet, char* buffer) { -// cout << "serializeRegion" << endl; +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); memcpy(buffer, &packet->regionInfo.y, sizeof(int)); +} + +void serializeRegionContent(NetworkPacket* packet, char* buffer) { + //format + 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); - if (packet->meta.type == NetworkPacket::Type::REGION_CONTENT) { - serializeRegionContent(packet, buffer); + //x & y + *reinterpret_cast(buffer) = packet->regionInfo.region->GetX(); + buffer += sizeof(int); + *reinterpret_cast(buffer) = packet->regionInfo.region->GetY(); + 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++) { + *reinterpret_cast(buffer) = packet->regionInfo.region->GetTile(i, j, k); + buffer += sizeof(Region::type_t); + } + } } } @@ -109,26 +124,22 @@ void serializeRegion(NetworkPacket* packet, char* buffer) { //------------------------- void deserializeType(NetworkPacket* packet, char* buffer) { -// cout << "deserializeType" << endl; memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); } void deserializeServer(NetworkPacket* packet, char* buffer) { -// cout << "deserializeServer" << endl; memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); memcpy(packet->serverInfo.name, buffer, PACKET_STRING_SIZE); } void deserializeClient(NetworkPacket* packet, char* buffer) { -// cout << "deserializeClient" << endl; memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); memcpy(&packet->clientInfo.index, buffer, sizeof(int)); } void deserializePlayer(NetworkPacket* packet, char* buffer) { -// cout << "deserializePlayer" << endl; memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); buffer += sizeof(NetworkPacket::Type); @@ -154,23 +165,46 @@ void deserializePlayer(NetworkPacket* packet, char* buffer) { memcpy(&packet->playerInfo.motion.y, buffer, sizeof(double)); } -void deserializeRegionContent(NetworkPacket* packet, char* buffer) { - //TODO -} - -void deserializeRegion(NetworkPacket* packet, char* buffer) { -// cout << "deserializeRegion" << endl; +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); memcpy(&packet->regionInfo.y, buffer, sizeof(int)); - buffer += sizeof(int); +} - if (packet->meta.type == NetworkPacket::Type::REGION_CONTENT) { - deserializeRegionContent(packet, buffer); +void deserializeRegionContent(NetworkPacket* packet, char* buffer) { + //format + deserializeRegionFormat(packet, buffer); + buffer += sizeof(int) * 5 + sizeof(NetworkPacket::Type); + + //content + BlankGenerator().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++) { + packet->regionInfo.region->SetTile(i, j, k, *reinterpret_cast(buffer)); + buffer += sizeof(Region::type_t); + } + } } } @@ -211,14 +245,13 @@ void serialize(NetworkPacket* packet, void* buffer) { //region info case NetworkPacket::Type::REGION_REQUEST: + serializeRegionFormat(packet, reinterpret_cast(buffer)); + break; + case NetworkPacket::Type::REGION_CONTENT: - serializeRegion(packet, reinterpret_cast(buffer)); + serializeRegionContent(packet, reinterpret_cast(buffer)); break; } -// for (int i = 0; i < sizeof(NetworkPacket); i++) { -// cout << ((char*)(buffer))[i]; -// } -// cout << endl; } void deserialize(NetworkPacket* packet, void* buffer) { @@ -254,14 +287,13 @@ void deserialize(NetworkPacket* packet, void* buffer) { deserializePlayer(packet, reinterpret_cast(buffer)); break; - //map info + //region info case NetworkPacket::Type::REGION_REQUEST: + deserializeRegionFormat(packet, reinterpret_cast(buffer)); + break; + case NetworkPacket::Type::REGION_CONTENT: - deserializeRegion(packet, reinterpret_cast(buffer)); + deserializeRegionContent(packet, reinterpret_cast(buffer)); break; } -// for (int i = 0; i < sizeof(NetworkPacket); i++) { -// cout << ((char*)(buffer))[i]; -// } -// cout << endl; } \ No newline at end of file diff --git a/common/network/serial.hpp b/common/network/serial.hpp index ef0c291..8680c8a 100644 --- a/common/network/serial.hpp +++ b/common/network/serial.hpp @@ -24,6 +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 + * metadata: sizeof(metadata) +*/ +#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 5 + sizeof(NetworkPacket::Metadata) + void serialize(NetworkPacket* const, void*); void deserialize(NetworkPacket* const, void*);