diff --git a/client/client_application.cpp b/client/client_application.cpp index a5426be..3c5ae4c 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -21,7 +21,7 @@ */ #include "client_application.hpp" -#include "serial.hpp" +#include "serial_packet.hpp" #include "config_utility.hpp" #include diff --git a/client/makefile b/client/makefile index f318d52..c8ca863 100644 --- a/client/makefile +++ b/client/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/ui ../common/utilities +INCLUDES+=. scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities LIBS+=client.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index e263bc8..517b86a 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -27,6 +27,7 @@ //networking #include "udp_network_utility.hpp" +#include "serial_packet.hpp" //graphics #include "image.hpp" diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index 03160c2..e8a5d1c 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -31,6 +31,7 @@ //utilities #include "config_utility.hpp" #include "udp_network_utility.hpp" +#include "serial_packet.hpp" //client #include "base_scene.hpp" diff --git a/client/scenes/makefile b/client/scenes/makefile index 4e050c7..f8d8358 100644 --- a/client/scenes/makefile +++ b/client/scenes/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet ../../common/network/serial ../../common/ui ../../common/utilities +INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/network/makefile b/common/network/makefile index 618d925..2caa8d4 100644 --- a/common/network/makefile +++ b/common/network/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. packet serial ../gameplay ../map ../utilities +INCLUDES+=. packet_types ../gameplay ../map ../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) @@ -17,8 +17,7 @@ OUT=$(addprefix $(OUTDIR)/,libcommon.a) #targets all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) - $(MAKE) -C packet - $(MAKE) -C serial + $(MAKE) -C packet_types $(OBJ): | $(OBJDIR) diff --git a/common/network/packet/enemy_packet.hpp b/common/network/packet/enemy_packet.hpp deleted file mode 100644 index d3fb7c5..0000000 --- a/common/network/packet/enemy_packet.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/* 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 - * 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 ENEMYPACKET_HPP_ -#define ENEMYPACKET_HPP_ - -#include "serial_packet_base.hpp" - -struct EnemyPacket : SerialPacketBase { - //identify the enemy - int enemyIndex; - char handle[PACKET_STRING_SIZE]; - char avatar[PACKET_STRING_SIZE]; - - //gameplay - Statistics stats; - - //TODO: gameplay components: equipment, items, buffs, debuffs, rewards -}; - -#endif \ No newline at end of file diff --git a/common/network/packet/serial_packet_type.hpp b/common/network/packet/serial_packet_type.hpp deleted file mode 100644 index 9142dcc..0000000 --- a/common/network/packet/serial_packet_type.hpp +++ /dev/null @@ -1,113 +0,0 @@ -/* 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 - * 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 SERIALPACKETTYPE_HPP_ -#define SERIALPACKETTYPE_HPP_ - -/* Key for the comments: - * request => response -*/ - -enum class SerialPacketType { - //default: there is something wrong - NONE = 0, - - //keep alive - //ping => pong - PING = 1, - PONG = 2, - - //search for the server list - //none => server name, player count, version info (and source address) - BROADCAST_REQUEST = 3, - BROADCAST_RESPONSE = 4, - - //try to join the server - //username, and password => client index, account index - JOIN_REQUEST = 5, - JOIN_RESPONSE = 6, - JOIN_REJECTION = 7, - - //mass update of all surrounding content - //character.x, character.y => packet barrage - SYNCHRONIZE = 8, - - //disconnect from the server - //autentication, account index => disconnect that account - DISCONNECT = 9, - - //shut down the server - //autentication => disconnect, shutdown - SHUTDOWN = 10, - - //map data - //room index, region.x, region.y => room index, region.x, region.y, region content - REGION_REQUEST = 11, - REGION_CONTENT = 12, - - //combat data - //TODO: system incomplete - COMBAT_NEW = 13, - COMBAT_DELETE = 14, - COMBAT_UPDATE = 15, - - COMBAT_ENTER_REQUEST = 16, - COMBAT_ENTER_RESPONSE = 17, - - COMBAT_EXIT_REQUEST = 18, - COMBAT_EXIT_RESPONSE = 19, - - //TODO: COMBAT info - - COMBAT_REJECTION = 20, - - //character data - //character data => etc. - CHARACTER_NEW = 21, - CHARACTER_DELETE = 22, - CHARACTER_UPDATE = 23, - - //authentication, character index => character stats - CHARACTER_STATS_REQUEST= 24, - CHARACTER_STATS_RESPONSE = 25, - - //character new => character rejection, disconnect? - CHARACTER_REJECTION = 26, - - //enemy data - //enemy data => etc. - ENEMY_NEW = 27, - ENEMY_DELETE = 28, - ENEMY_UPDATE = 29, - - ENEMY_STATS_REQUEST = 30, - ENEMY_STATS_RESPONSE = 31, - - //enemy index => enemy doens't exist - ENEMY_REJECTION= 32, - - //NOTE: more packet types go here - - //not used - LAST -}; - -#endif \ No newline at end of file diff --git a/common/network/packet_types/character_packet.cpp b/common/network/packet_types/character_packet.cpp new file mode 100644 index 0000000..a66b330 --- /dev/null +++ b/common/network/packet_types/character_packet.cpp @@ -0,0 +1,74 @@ +/* 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 "character_packet.hpp" + +#include "serial_utility.hpp" + +#include "serial_statistics.hpp" + +void serializeCharacter(void* buffer, CharacterPacket* packet) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + //identify the character + serialCopy(&buffer, &packet->characterIndex, sizeof(int)); + serialCopy(&buffer, packet->handle, PACKET_STRING_SIZE); + serialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE); + + //the owner + serialCopy(&buffer, &packet->accountIndex, sizeof(int)); + + //location + serialCopy(&buffer, &packet->roomIndex, sizeof(int)); + serialCopy(&buffer, &packet->origin.x, sizeof(double)); + serialCopy(&buffer, &packet->origin.y, sizeof(double)); + serialCopy(&buffer, &packet->motion.x, sizeof(double)); + serialCopy(&buffer, &packet->motion.y, sizeof(double)); + + //stats structure + serializeStatistics(&buffer, &packet->stats); + + //TODO: gameplay components: equipment, items, buffs, debuffs +} + +void deserializeCharacter(void* buffer, CharacterPacket* packet) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + //identify the character + deserialCopy(&buffer, &packet->characterIndex, sizeof(int)); + deserialCopy(&buffer, packet->handle, PACKET_STRING_SIZE); + deserialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE); + + //the owner + deserialCopy(&buffer, &packet->accountIndex, sizeof(int)); + + //location + deserialCopy(&buffer, &packet->roomIndex, sizeof(int)); + deserialCopy(&buffer, &packet->origin.x, sizeof(double)); + deserialCopy(&buffer, &packet->origin.y, sizeof(double)); + deserialCopy(&buffer, &packet->motion.x, sizeof(double)); + deserialCopy(&buffer, &packet->motion.y, sizeof(double)); + + //stats structure + deserializeStatistics(&buffer, &packet->stats); + + //TODO: gameplay components: equipment, items, buffs, debuffs +} diff --git a/common/network/packet/character_packet.hpp b/common/network/packet_types/character_packet.hpp similarity index 91% rename from common/network/packet/character_packet.hpp rename to common/network/packet_types/character_packet.hpp index b6042f3..72a6fc4 100644 --- a/common/network/packet/character_packet.hpp +++ b/common/network/packet_types/character_packet.hpp @@ -47,4 +47,7 @@ struct CharacterPacket : SerialPacketBase { //TODO: gameplay components: equipment, items, buffs, debuffs }; +void serializeCharacter(void* buffer, CharacterPacket* packet); +void deserializeCharacter(void* buffer, CharacterPacket* packet); + #endif \ No newline at end of file diff --git a/common/network/packet_types/client_packet.cpp b/common/network/packet_types/client_packet.cpp new file mode 100644 index 0000000..77da2e0 --- /dev/null +++ b/common/network/packet_types/client_packet.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 "client_packet.hpp" + +#include "serial_utility.hpp" + +void serializeClient(void* buffer, ClientPacket* packet) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + serialCopy(&buffer, &packet->clientIndex, sizeof(int)); + serialCopy(&buffer, &packet->accountIndex, sizeof(int)); + serialCopy(&buffer, packet->username, PACKET_STRING_SIZE); +} + +void deserializeClient(void* buffer, ClientPacket* packet) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + deserialCopy(&buffer, &packet->clientIndex, sizeof(int)); + deserialCopy(&buffer, &packet->accountIndex, sizeof(int)); + deserialCopy(&buffer, packet->username, PACKET_STRING_SIZE); +} diff --git a/common/network/packet/client_packet.hpp b/common/network/packet_types/client_packet.hpp similarity index 90% rename from common/network/packet/client_packet.hpp rename to common/network/packet_types/client_packet.hpp index f191b11..bd19c02 100644 --- a/common/network/packet/client_packet.hpp +++ b/common/network/packet_types/client_packet.hpp @@ -28,7 +28,9 @@ struct ClientPacket : SerialPacketBase { int clientIndex; int accountIndex; char username[PACKET_STRING_SIZE]; -// char password[PACKET_STRING_SIZE]; //hashed, not currently used }; +void serializeClient(void* buffer, ClientPacket* packet); +void deserializeClient(void* buffer, ClientPacket* packet); + #endif \ No newline at end of file diff --git a/common/network/packet/makefile b/common/network/packet_types/makefile similarity index 89% rename from common/network/packet/makefile rename to common/network/packet_types/makefile index f993f8b..21223de 100644 --- a/common/network/packet/makefile +++ b/common/network/packet_types/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../../gameplay ../../map ../../utilities +INCLUDES+=. .. ../../gameplay ../../map ../../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/network/serial/serial_region.cpp b/common/network/packet_types/region_packet.cpp similarity index 56% rename from common/network/serial/serial_region.cpp rename to common/network/packet_types/region_packet.cpp index 54c4b6b..e5f2c44 100644 --- a/common/network/serial/serial_region.cpp +++ b/common/network/packet_types/region_packet.cpp @@ -19,26 +19,21 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial.hpp" +#include "region_packet.hpp" -#include "serial_util.hpp" +#include "serial_utility.hpp" -void serializeRegionFormat(RegionPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void serializeRegionContent(void* buffer, RegionPacket* packet) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); //format - SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - SERIALIZE(buffer, &packet->x, sizeof(int)); - SERIALIZE(buffer, &packet->y, sizeof(int)); -} + serialCopy(&buffer, &packet->roomIndex, sizeof(int)); + serialCopy(&buffer, &packet->x, sizeof(int)); + serialCopy(&buffer, &packet->y, sizeof(int)); -void serializeRegionContent(RegionPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //format - SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - SERIALIZE(buffer, &packet->x, sizeof(int)); - SERIALIZE(buffer, &packet->y, sizeof(int)); + if (packet->type != SerialPacketType::REGION_CONTENT) { + return; + } //tiles for (int i = 0; i < REGION_WIDTH; i++) { @@ -51,25 +46,20 @@ void serializeRegionContent(RegionPacket* packet, void* buffer) { } //solids - SERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); + serialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); } -void deserializeRegionFormat(RegionPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void deserializeRegionContent(void* buffer, RegionPacket* packet) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); //format - DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->x, sizeof(int)); - DESERIALIZE(buffer, &packet->y, sizeof(int)); -} + deserialCopy(&buffer, &packet->roomIndex, sizeof(int)); + deserialCopy(&buffer, &packet->x, sizeof(int)); + deserialCopy(&buffer, &packet->y, sizeof(int)); -void deserializeRegionContent(RegionPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //format - DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->x, sizeof(int)); - DESERIALIZE(buffer, &packet->y, sizeof(int)); + if (packet->type != SerialPacketType::REGION_CONTENT) { + return; + } //an object to work on packet->region = new Region(packet->x, packet->y); @@ -85,5 +75,5 @@ void deserializeRegionContent(RegionPacket* packet, void* buffer) { } //solids - DESERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); + deserialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); } \ No newline at end of file diff --git a/common/network/packet/combat_packet.hpp b/common/network/packet_types/region_packet.hpp similarity index 59% rename from common/network/packet/combat_packet.hpp rename to common/network/packet_types/region_packet.hpp index 6b068df..0c4d717 100644 --- a/common/network/packet/combat_packet.hpp +++ b/common/network/packet_types/region_packet.hpp @@ -19,28 +19,30 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef COMBATPACKET_HPP_ -#define COMBATPACKET_HPP_ +#ifndef REGIONPACKET_HPP_ +#define REGIONPACKET_HPP_ #include "serial_packet_base.hpp" -#include "combat_defines.hpp" +#include "region.hpp" -struct CombatPacket : SerialPacketBase { - //identify the combat instance - int combatIndex; - int difficulty; - TerrainType terrainType; +#include - //combatants - int characterArray[COMBAT_MAX_CHARACTERS]; - int enemyArray[COMBAT_MAX_ENEMIES]; +//define the memory footprint for the region's members +constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH; +constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); +constexpr int REGION_METADATA_FOOTPRINT = sizeof(int) * 3; - //location - int mapIndex; - Vector2 origin; +struct RegionPacket : SerialPacketBase { + //location/identify the region + int roomIndex; + int x, y; - //TODO: gameplay components: rewards + //the data + Region* region; }; +void serializeRegion(void* buffer, RegionPacket* packet); +void deserializeRegion(void* buffer, RegionPacket* packet); + #endif \ No newline at end of file diff --git a/common/network/packet/region_packet.hpp b/common/network/packet_types/serial_packet_base.cpp similarity index 77% rename from common/network/packet/region_packet.hpp rename to common/network/packet_types/serial_packet_base.cpp index 8a9085e..c8803b0 100644 --- a/common/network/packet/region_packet.hpp +++ b/common/network/packet_types/serial_packet_base.cpp @@ -1,4 +1,4 @@ -/* Copyright: (c) Kayne Ruse 2013, 2014 +/* 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 @@ -19,20 +19,6 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef REGIONPACKET_HPP_ -#define REGIONPACKET_HPP_ - #include "serial_packet_base.hpp" -#include "region.hpp" - -struct RegionPacket : SerialPacketBase { - //location/identify the region - int roomIndex; - int x, y; - - //the data - Region* region; -}; - -#endif \ No newline at end of file +//sanity check \ No newline at end of file diff --git a/common/network/packet/serial_packet_base.hpp b/common/network/packet_types/serial_packet_base.hpp similarity index 87% rename from common/network/packet/serial_packet_base.hpp rename to common/network/packet_types/serial_packet_base.hpp index 2f82092..364e645 100644 --- a/common/network/packet/serial_packet_base.hpp +++ b/common/network/packet_types/serial_packet_base.hpp @@ -22,15 +22,10 @@ #ifndef SERIALPACKETBASE_HPP_ #define SERIALPACKETBASE_HPP_ -#ifndef SERIALPACKET_HPP_ - #error Cannot include this file without 'serial_packet.hpp' -#endif - #include "serial_packet_type.hpp" #include "SDL/SDL_net.h" -constexpr int NETWORK_VERSION = 20140701; constexpr int PACKET_STRING_SIZE = 100; struct SerialPacketBase { @@ -41,6 +36,4 @@ struct SerialPacketBase { virtual ~SerialPacketBase() {}; }; -typedef SerialPacketBase SerialPacket; - #endif diff --git a/common/network/packet_types/serial_statistics.cpp b/common/network/packet_types/serial_statistics.cpp new file mode 100644 index 0000000..fae799e --- /dev/null +++ b/common/network/packet_types/serial_statistics.cpp @@ -0,0 +1,64 @@ +/* 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 "serial_statistics.hpp" + +#include "serial_utility.hpp" + +void serializeStatistics(void** buffer, Statistics* stats) { + //integers + serialCopy(buffer, &stats->level, sizeof(int)); + serialCopy(buffer, &stats->exp, sizeof(int)); + serialCopy(buffer, &stats->maxHP, sizeof(int)); + serialCopy(buffer, &stats->health, sizeof(int)); + serialCopy(buffer, &stats->maxMP, sizeof(int)); + serialCopy(buffer, &stats->mana, sizeof(int)); + serialCopy(buffer, &stats->attack, sizeof(int)); + serialCopy(buffer, &stats->defence, sizeof(int)); + serialCopy(buffer, &stats->intelligence, sizeof(int)); + serialCopy(buffer, &stats->resistance, sizeof(int)); + serialCopy(buffer, &stats->speed, sizeof(int)); + + //floats + serialCopy(buffer, &stats->accuracy, sizeof(float)); + serialCopy(buffer, &stats->evasion, sizeof(float)); + serialCopy(buffer, &stats->luck, sizeof(float)); +} + +void deserializeStatistics(void** buffer, Statistics* stats) { + //integers + deserialCopy(buffer, &stats->level, sizeof(int)); + deserialCopy(buffer, &stats->exp, sizeof(int)); + deserialCopy(buffer, &stats->maxHP, sizeof(int)); + deserialCopy(buffer, &stats->health, sizeof(int)); + deserialCopy(buffer, &stats->maxMP, sizeof(int)); + deserialCopy(buffer, &stats->mana, sizeof(int)); + deserialCopy(buffer, &stats->attack, sizeof(int)); + deserialCopy(buffer, &stats->defence, sizeof(int)); + deserialCopy(buffer, &stats->intelligence, sizeof(int)); + deserialCopy(buffer, &stats->resistance, sizeof(int)); + deserialCopy(buffer, &stats->speed, sizeof(int)); + + //floats + deserialCopy(buffer, &stats->accuracy, sizeof(float)); + deserialCopy(buffer, &stats->evasion, sizeof(float)); + deserialCopy(buffer, &stats->luck, sizeof(float)); +} diff --git a/common/network/packet/sanity_check.cpp b/common/network/packet_types/serial_statistics.hpp similarity index 74% rename from common/network/packet/sanity_check.cpp rename to common/network/packet_types/serial_statistics.hpp index f250d69..2f4243f 100644 --- a/common/network/packet/sanity_check.cpp +++ b/common/network/packet_types/serial_statistics.hpp @@ -1,4 +1,4 @@ -/* Copyright: (c) Kayne Ruse 2013, 2014 +/* 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 @@ -19,10 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial_packet.hpp" +#ifndef SERIALSTATISTICS_HPP_ +#define SERIALSTATISTICS_HPP_ -/* DOCS: Sanity check, read more - * Since most/all of the files in this directory are header files, I've created - * this source file as a "sanity check", to ensure that the above header files - * are written correctly via make. -*/ +#include "statistics.hpp" + +void serializeStatistics(void** buffer, Statistics* stats); +void deserializeStatistics(void** buffer, Statistics* stats); + +#endif \ No newline at end of file diff --git a/common/network/serial/serial_server.cpp b/common/network/packet_types/server_packet.cpp similarity index 59% rename from common/network/serial/serial_server.cpp rename to common/network/packet_types/server_packet.cpp index 12a2f34..85ec493 100644 --- a/common/network/serial/serial_server.cpp +++ b/common/network/packet_types/server_packet.cpp @@ -19,24 +19,24 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial.hpp" +#include "server_packet.hpp" -#include "serial_util.hpp" +#include "serial_utility.hpp" -void serializeServer(ServerPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void serializeServer(void* buffer, ServerPacket* packet) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); //identify the server - SERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->playerCount, sizeof(int)); - SERIALIZE(buffer, &packet->version, sizeof(int)); + serialCopy(&buffer, packet->name, PACKET_STRING_SIZE); + serialCopy(&buffer, &packet->playerCount, sizeof(int)); + serialCopy(&buffer, &packet->version, sizeof(int)); } -void deserializeServer(ServerPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void deserializeServer(void* buffer, ServerPacket* packet) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); //identify the server - DESERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->playerCount, sizeof(int)); - DESERIALIZE(buffer, &packet->version, sizeof(int)); + deserialCopy(&buffer, packet->name, PACKET_STRING_SIZE); + deserialCopy(&buffer, &packet->playerCount, sizeof(int)); + deserialCopy(&buffer, &packet->version, sizeof(int)); } diff --git a/common/network/packet/server_packet.hpp b/common/network/packet_types/server_packet.hpp similarity index 90% rename from common/network/packet/server_packet.hpp rename to common/network/packet_types/server_packet.hpp index fcd4a49..3de5a2d 100644 --- a/common/network/packet/server_packet.hpp +++ b/common/network/packet_types/server_packet.hpp @@ -31,4 +31,7 @@ struct ServerPacket : SerialPacketBase { int version; }; +void serializeServer(void* buffer, ServerPacket* packet); +void deserializeServer(void* buffer, ServerPacket* packet); + #endif \ No newline at end of file diff --git a/common/network/serial/makefile b/common/network/serial/makefile deleted file mode 100644 index 564b228..0000000 --- a/common/network/serial/makefile +++ /dev/null @@ -1,37 +0,0 @@ -#config -INCLUDES+=. ../packet ../../gameplay ../../map ../../utilities -LIBS+= -CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) - -#source -CXXSRC=$(wildcard *.cpp) - -#objects -OBJDIR=obj -OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.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 $@ $< - -clean: - $(RM) *.o *.a *.exe - -rebuild: clean all diff --git a/common/network/serial/serial.cpp b/common/network/serial/serial.cpp deleted file mode 100644 index 45c9c32..0000000 --- a/common/network/serial/serial.cpp +++ /dev/null @@ -1,182 +0,0 @@ -/* 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 "serial.hpp" - -#include "serial_util.hpp" - -//simple type functions -void serializeType(SerialPacketBase* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); -} - -void deserializeType(SerialPacketBase* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); -} - -//main switch functions -void serializePacket(SerialPacketBase* packet, void* buffer) { - switch(packet->type) { - //no extra data - case SerialPacketType::NONE: - case SerialPacketType::PING: - case SerialPacketType::PONG: - case SerialPacketType::BROADCAST_REQUEST: - - //all rejections - case SerialPacketType::JOIN_REJECTION: - case SerialPacketType::CHARACTER_REJECTION: - case SerialPacketType::ENEMY_REJECTION: - case SerialPacketType::COMBAT_REJECTION: - - serializeType(packet, buffer); - break; - - //character info - case SerialPacketType::CHARACTER_NEW: - case SerialPacketType::CHARACTER_DELETE: - case SerialPacketType::CHARACTER_UPDATE: - case SerialPacketType::CHARACTER_STATS_REQUEST: - case SerialPacketType::CHARACTER_STATS_RESPONSE: - serializeCharacter(static_cast(packet), buffer); - break; - - //client info - case SerialPacketType::JOIN_REQUEST: - case SerialPacketType::JOIN_RESPONSE: - case SerialPacketType::SYNCHRONIZE: - case SerialPacketType::DISCONNECT: - case SerialPacketType::SHUTDOWN: - serializeClient(static_cast(packet), buffer); - break; - - //combat info - case SerialPacketType::COMBAT_NEW: - case SerialPacketType::COMBAT_DELETE: - case SerialPacketType::COMBAT_UPDATE: - - case SerialPacketType::COMBAT_ENTER_REQUEST: - case SerialPacketType::COMBAT_ENTER_RESPONSE: - case SerialPacketType::COMBAT_EXIT_REQUEST: - case SerialPacketType::COMBAT_EXIT_RESPONSE: - - serializeCombat(static_cast(packet), buffer); - break; - - //enemy info - case SerialPacketType::ENEMY_NEW: - case SerialPacketType::ENEMY_DELETE: - case SerialPacketType::ENEMY_UPDATE: - case SerialPacketType::ENEMY_STATS_REQUEST: - case SerialPacketType::ENEMY_STATS_RESPONSE: - serializeEnemy(static_cast(packet), buffer); - break; - - //region info - case SerialPacketType::REGION_REQUEST: - serializeRegionFormat(static_cast(packet), buffer); - break; - - case SerialPacketType::REGION_CONTENT: - serializeRegionContent(static_cast(packet), buffer); - break; - - //server info - case SerialPacketType::BROADCAST_RESPONSE: - serializeServer(static_cast(packet), buffer); - break; - } -} - -void deserializePacket(SerialPacketBase* packet, void* buffer) { - //find the type, so that you can actually deserialize the packet! - deserializeType(packet, buffer); - switch(packet->type) { - //no extra data - case SerialPacketType::NONE: - case SerialPacketType::PING: - case SerialPacketType::PONG: - case SerialPacketType::BROADCAST_REQUEST: - - //all rejections - case SerialPacketType::JOIN_REJECTION: - case SerialPacketType::CHARACTER_REJECTION: - case SerialPacketType::ENEMY_REJECTION: - case SerialPacketType::COMBAT_REJECTION: - - //NOTHING - break; - - //character info - case SerialPacketType::CHARACTER_NEW: - case SerialPacketType::CHARACTER_DELETE: - case SerialPacketType::CHARACTER_UPDATE: - case SerialPacketType::CHARACTER_STATS_REQUEST: - case SerialPacketType::CHARACTER_STATS_RESPONSE: - deserializeCharacter(static_cast(packet), buffer); - break; - - //client info - case SerialPacketType::JOIN_REQUEST: - case SerialPacketType::JOIN_RESPONSE: - case SerialPacketType::SYNCHRONIZE: - case SerialPacketType::DISCONNECT: - case SerialPacketType::SHUTDOWN: - deserializeClient(static_cast(packet), buffer); - break; - - //combat info - case SerialPacketType::COMBAT_NEW: - case SerialPacketType::COMBAT_DELETE: - case SerialPacketType::COMBAT_UPDATE: - - case SerialPacketType::COMBAT_ENTER_REQUEST: - case SerialPacketType::COMBAT_ENTER_RESPONSE: - case SerialPacketType::COMBAT_EXIT_REQUEST: - case SerialPacketType::COMBAT_EXIT_RESPONSE: - - serializeCombat(static_cast(packet), buffer); - break; - - //enemy info - case SerialPacketType::ENEMY_NEW: - case SerialPacketType::ENEMY_DELETE: - case SerialPacketType::ENEMY_UPDATE: - case SerialPacketType::ENEMY_STATS_REQUEST: - case SerialPacketType::ENEMY_STATS_RESPONSE: - serializeEnemy(static_cast(packet), buffer); - break; - - //region info - case SerialPacketType::REGION_REQUEST: - deserializeRegionFormat(static_cast(packet), buffer); - break; - - case SerialPacketType::REGION_CONTENT: - deserializeRegionContent(static_cast(packet), buffer); - break; - - //server info - case SerialPacketType::BROADCAST_RESPONSE: - deserializeServer(static_cast(packet), buffer); - break; - } -} \ No newline at end of file diff --git a/common/network/serial/serial.hpp b/common/network/serial/serial.hpp deleted file mode 100644 index f218a24..0000000 --- a/common/network/serial/serial.hpp +++ /dev/null @@ -1,73 +0,0 @@ -/* 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 SERIALIZE_HPP_ -#define SERIALIZE_HPP_ - -#include "serial_packet.hpp" - -#include "region.hpp" -#include "statistics.hpp" - -#include - -//Primary interface functions -void serializePacket(SerialPacketBase*, void* dest); -void deserializePacket(SerialPacketBase*, void* src); - -void serializeType(SerialPacketBase*, void*); -void deserializeType(SerialPacketBase*, void*); - -//utility functions, exposed -void serializeCharacter(CharacterPacket*, void*); -void serializeClient(ClientPacket*, void*); -void serializeCombat(CombatPacket*, void*); -void serializeEnemy(EnemyPacket*, void*); -void serializeRegionFormat(RegionPacket*, void*); -void serializeRegionContent(RegionPacket*, void*); -void serializeServer(ServerPacket*, void*); -void serializeStatistics(Statistics*, void*); - -void deserializeCharacter(CharacterPacket*, void*); -void deserializeClient(ClientPacket*, void*); -void deserializeCombat(CombatPacket*, void*); -void deserializeEnemy(EnemyPacket*, void*); -void deserializeRegionFormat(RegionPacket*, void*); -void deserializeRegionContent(RegionPacket*, void*); -void deserializeServer(ServerPacket*, void*); -void deserializeStatistics(Statistics*, void*); - -/* DOCS: PACKET_BUFFER_SIZE is the memory required to store serialized data - * DOCS: SerialPacketType::REGION_CONTENT is currently the largest packet type - * Serialized packet structure: - * SerialPacketType - * room index - * X & Y position - * tile data (3 layers) - * solid data (bitset) - * The constants declared here are used for networking ONLY -*/ - -constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH; -constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); -constexpr int PACKET_BUFFER_SIZE = sizeof(SerialPacketType) + sizeof(int) * 3 + REGION_TILE_FOOTPRINT + REGION_SOLID_FOOTPRINT; - -#endif \ No newline at end of file diff --git a/common/network/serial/serial_character.cpp b/common/network/serial/serial_character.cpp deleted file mode 100644 index 2754f56..0000000 --- a/common/network/serial/serial_character.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* 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 "serial.hpp" - -#include "serial_util.hpp" - -void serializeCharacter(CharacterPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the character - SERIALIZE(buffer, &packet->characterIndex, sizeof(int)); - SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); - - //the owner - SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); - - //location - SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - SERIALIZE(buffer, &packet->origin.x, sizeof(double)); - SERIALIZE(buffer, &packet->origin.y, sizeof(double)); - SERIALIZE(buffer, &packet->motion.x, sizeof(double)); - SERIALIZE(buffer, &packet->motion.y, sizeof(double)); - - //stats structure - serializeStatistics(&packet->stats, buffer); - buffer = reinterpret_cast(buffer) + sizeof(Statistics); - - //TODO: gameplay components: equipment, items, buffs, debuffs -} - -void deserializeCharacter(CharacterPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the character - DESERIALIZE(buffer, &packet->characterIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); - - //the owner - DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); - - //location - DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->origin.x, sizeof(double)); - DESERIALIZE(buffer, &packet->origin.y, sizeof(double)); - DESERIALIZE(buffer, &packet->motion.x, sizeof(double)); - DESERIALIZE(buffer, &packet->motion.y, sizeof(double)); - - //stats structure - deserializeStatistics(&packet->stats, buffer); - buffer = reinterpret_cast(buffer) + sizeof(Statistics); - - //TODO: gameplay components: equipment, items, buffs, debuffs -} diff --git a/common/network/serial/serial_client.cpp b/common/network/serial/serial_client.cpp deleted file mode 100644 index 96bc881..0000000 --- a/common/network/serial/serial_client.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* 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 "serial.hpp" - -#include "serial_util.hpp" - -void serializeClient(ClientPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - SERIALIZE(buffer, &packet->clientIndex, sizeof(int)); - SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); - SERIALIZE(buffer, &packet->username, PACKET_STRING_SIZE); -// SERIALIZE(buffer, &packet->password, PACKET_STRING_SIZE); -} - -void deserializeClient(ClientPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - DESERIALIZE(buffer, &packet->clientIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->username, PACKET_STRING_SIZE); -// DESERIALIZE(buffer, &packet->password, PACKET_STRING_SIZE); -} diff --git a/common/network/serial/serial_combat.cpp b/common/network/serial/serial_combat.cpp deleted file mode 100644 index 318779f..0000000 --- a/common/network/serial/serial_combat.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* 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 "serial.hpp" - -#include "serial_util.hpp" - -void serializeCombat(CombatPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the combat instance - SERIALIZE(buffer, &packet->combatIndex, sizeof(int)); - SERIALIZE(buffer, &packet->difficulty, sizeof(int)); - SERIALIZE(buffer, &packet->terrainType, sizeof(TerrainType)); - - //combatants - SERIALIZE(buffer, &packet->characterArray, sizeof(int) * COMBAT_MAX_CHARACTERS); - SERIALIZE(buffer, &packet->enemyArray, sizeof(int) * COMBAT_MAX_ENEMIES); - - //location - SERIALIZE(buffer, &packet->mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->origin.x, sizeof(double)); - SERIALIZE(buffer, &packet->origin.y, sizeof(double)); - - //TODO: gameplay components: rewards -} - -void deserializeCombat(CombatPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the combat instance - DESERIALIZE(buffer, &packet->combatIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->difficulty, sizeof(int)); - DESERIALIZE(buffer, &packet->terrainType, sizeof(TerrainType)); - - //combatants - DESERIALIZE(buffer, &packet->characterArray, sizeof(int) * COMBAT_MAX_CHARACTERS); - DESERIALIZE(buffer, &packet->enemyArray, sizeof(int) * COMBAT_MAX_ENEMIES); - - //location - DESERIALIZE(buffer, &packet->mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->origin.x, sizeof(double)); - DESERIALIZE(buffer, &packet->origin.y, sizeof(double)); - - //TODO: gameplay components: rewards -} diff --git a/common/network/serial/serial_enemy.cpp b/common/network/serial/serial_enemy.cpp deleted file mode 100644 index 4861d83..0000000 --- a/common/network/serial/serial_enemy.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* 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 "serial.hpp" - -#include "serial_util.hpp" - -void serializeEnemy(EnemyPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the enemy - SERIALIZE(buffer, &packet->enemyIndex, sizeof(int)); - SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); - - //gameplay - - //stats structure - serializeStatistics(&packet->stats, buffer); - buffer = reinterpret_cast(buffer) + sizeof(Statistics); - - //TODO: gameplay components: equipment, items, buffs, debuffs, rewards -} - -void deserializeEnemy(EnemyPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the enemy - DESERIALIZE(buffer, &packet->enemyIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); - - //stats structure - deserializeStatistics(&packet->stats, buffer); - buffer = reinterpret_cast(buffer) + sizeof(Statistics); - - //TODO: gameplay components: equipment, items, buffs, debuffs, rewards -} diff --git a/common/network/serial/serial_statistics.cpp b/common/network/serial/serial_statistics.cpp deleted file mode 100644 index 6a594a8..0000000 --- a/common/network/serial/serial_statistics.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* 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 "serial.hpp" - -#include "serial_util.hpp" - -void serializeStatistics(Statistics* stats, void* buffer) { - //integers - SERIALIZE(buffer, &stats->level, sizeof(int)); - SERIALIZE(buffer, &stats->exp, sizeof(int)); - SERIALIZE(buffer, &stats->maxHP, sizeof(int)); - SERIALIZE(buffer, &stats->health, sizeof(int)); - SERIALIZE(buffer, &stats->maxMP, sizeof(int)); - SERIALIZE(buffer, &stats->mana, sizeof(int)); - SERIALIZE(buffer, &stats->attack, sizeof(int)); - SERIALIZE(buffer, &stats->defence, sizeof(int)); - SERIALIZE(buffer, &stats->intelligence, sizeof(int)); - SERIALIZE(buffer, &stats->resistance, sizeof(int)); - SERIALIZE(buffer, &stats->speed, sizeof(int)); - - //floats - SERIALIZE(buffer, &stats->accuracy, sizeof(float)); - SERIALIZE(buffer, &stats->evasion, sizeof(float)); - SERIALIZE(buffer, &stats->luck, sizeof(float)); -} - - -void deserializeStatistics(Statistics* stats, void* buffer) { - //integers - DESERIALIZE(buffer, &stats->level, sizeof(int)); - DESERIALIZE(buffer, &stats->exp, sizeof(int)); - DESERIALIZE(buffer, &stats->maxHP, sizeof(int)); - DESERIALIZE(buffer, &stats->health, sizeof(int)); - DESERIALIZE(buffer, &stats->maxMP, sizeof(int)); - DESERIALIZE(buffer, &stats->mana, sizeof(int)); - DESERIALIZE(buffer, &stats->attack, sizeof(int)); - DESERIALIZE(buffer, &stats->defence, sizeof(int)); - DESERIALIZE(buffer, &stats->intelligence, sizeof(int)); - DESERIALIZE(buffer, &stats->resistance, sizeof(int)); - DESERIALIZE(buffer, &stats->speed, sizeof(int)); - - //floats - DESERIALIZE(buffer, &stats->accuracy, sizeof(float)); - DESERIALIZE(buffer, &stats->evasion, sizeof(float)); - DESERIALIZE(buffer, &stats->luck, sizeof(float)); -} diff --git a/common/network/packet/serial_packet.hpp b/common/network/serial_packet.hpp similarity index 61% rename from common/network/packet/serial_packet.hpp rename to common/network/serial_packet.hpp index 44198aa..8d8982e 100644 --- a/common/network/packet/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -22,23 +22,41 @@ #ifndef SERIALPACKET_HPP_ #define SERIALPACKET_HPP_ +#include "serial_packet_base.hpp" #include "character_packet.hpp" #include "client_packet.hpp" -#include "combat_packet.hpp" -#include "enemy_packet.hpp" #include "region_packet.hpp" #include "server_packet.hpp" -//NOTE: SerialPacket is defined in serial_packet_base.hpp +//SerialPacketBase is defined in serial_packet_base.hpp +typedef SerialPacketBase SerialPacket; + +//DOCS: NETWORK_VERSION is used to discern compatible servers and clients +constexpr int NETWORK_VERSION = -1; union MaxPacket { CharacterPacket a; ClientPacket b; - CombatPacket c; - EnemyPacket d; - RegionPacket e; - ServerPacket f; + RegionPacket c; + ServerPacket d; }; + constexpr int MAX_PACKET_SIZE = sizeof(MaxPacket); +/* DOCS: PACKET_BUFFER_SIZE is the memory required to store serialized data + * DOCS: SerialPacketType::REGION_CONTENT is currently the largest packet type + * Serialized RegionPacket structure: + * SerialPacketType + * room index (int) + * X & Y position (int) + * tile data (3 layers) + * solid data (bitset) +*/ + +constexpr int PACKET_BUFFER_SIZE = + sizeof(SerialPacketType) + + REGION_METADATA_FOOTPRINT + + REGION_TILE_FOOTPRINT + + REGION_SOLID_FOOTPRINT; + #endif diff --git a/common/network/serial_packet_type.hpp b/common/network/serial_packet_type.hpp new file mode 100644 index 0000000..cf9a4dd --- /dev/null +++ b/common/network/serial_packet_type.hpp @@ -0,0 +1,98 @@ +/* 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 + * 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 SERIALPACKETTYPE_HPP_ +#define SERIALPACKETTYPE_HPP_ + +/* DOCS: The headers indicate what packet type is used for each message + * different messages under the same header will carry different amounts of + * valid data, but it will still be carried in that packet's format. +*/ + +enum class SerialPacketType { + //default: there is something wrong + NONE = 0, + + //------------------------- + //ServerPacket + // name, player count, version + //------------------------- + + //heartbeat + PING, + PONG, + + //Used for finding available servers + BROADCAST_REQUEST, + BROADCAST_RESPONSE, + + //------------------------- + //ClientPacket + // client index, account index, character index + //------------------------- + + //Connecting to a server as a client + JOIN_REQUEST, + JOIN_RESPONSE, + JOIN_REJECTION, + + //client requests all information from the server + SYNCHRONIZE, + + //disconnect from the server + DISCONNECT, + + //shut down the server + SHUTDOWN, + + //------------------------- + //RegionPacket + // room index, x, y, raw data + //------------------------- + + //map data + REGION_REQUEST, + REGION_CONTENT, + + //------------------------- + //CharacterPacket + // handle, avatar, character index, account index, + // room index, origin, motion + // statistics + //------------------------- + + //controlling characters + CHARACTER_NEW, + CHARACTER_DELETE, + CHARACTER_UPDATE, + + //authentication, character index => character stats + CHARACTER_STATS_REQUEST, + CHARACTER_STATS_RESPONSE, + + //reject a character request + CHARACTER_REJECTION, + + //not used + LAST +}; + +#endif \ No newline at end of file diff --git a/common/network/serial_utility.cpp b/common/network/serial_utility.cpp new file mode 100644 index 0000000..a3aa205 --- /dev/null +++ b/common/network/serial_utility.cpp @@ -0,0 +1,65 @@ +/* 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 "serial_utility.hpp" + +//packet types +#include "character_packet.hpp" +#include "client_packet.hpp" +#include "region_packet.hpp" +#include "server_packet.hpp" + +#include + +//raw memory copy +void serialCopy(void** buffer, void* data, int size) { + memcpy(*buffer, data, size); + *buffer = static_cast(*buffer) + size; +} + +void deserialCopy(void** buffer, void* data, int size) { + memcpy(data, *buffer, size); + *buffer = static_cast(*buffer) + size; +} + +//simple type functions +void serializeType(SerialPacketBase* packet, void* buffer) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); +} + +void deserializeType(SerialPacketBase* packet, void* buffer) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); +} + +//main switch functions +void serializePacket(SerialPacketBase* packet, void* buffer) { + switch(packet->type) { + //TODO: implement the switch statement + } +} + +void deserializePacket(SerialPacketBase* packet, void* buffer) { + //find the type, so that you can actually deserialize the packet! + deserializeType(packet, buffer); + switch(packet->type) { + //TODO: implement the switch statement + } +} \ No newline at end of file diff --git a/common/network/serial/serial_util.hpp b/common/network/serial_utility.hpp similarity index 71% rename from common/network/serial/serial_util.hpp rename to common/network/serial_utility.hpp index a0910b8..7d3766e 100644 --- a/common/network/serial/serial_util.hpp +++ b/common/network/serial_utility.hpp @@ -19,13 +19,19 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef SERIALIZEUTIL_HPP_ -#define SERIALIZEUTIL_HPP_ +#ifndef SERIALIZEUTILITY_HPP_ +#define SERIALIZEUTILITY_HPP_ + +#include "serial_packet_base.hpp" #include -//NOTE: The strange assignments here used in order to move the void* parameter -#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer = reinterpret_cast(buffer) + size; -#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer = reinterpret_cast(buffer) + size; +//raw memory copy +void serialCopy(void** buffer, void* data, int size); +void deserialCopy(void** buffer, void* data, int size); + +//primary functions +void serializePacket(SerialPacketBase* packet, void* buffer); +void deserializePacket(SerialPacketBase* packet, void* buffer); #endif diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index ad97a1c..8145a05 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -21,12 +21,13 @@ */ #include "udp_network_utility.hpp" -#include "serial.hpp" +#include "serial_packet.hpp" +#include "serial_utility.hpp" #include //BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network -//NOTE: don't confuse SerialPacket with UDPpacket +//NOTE: don't confuse SerialPacketBase with UDPpacket void UDPNetworkUtility::Open(int port) { socket = SDLNet_UDP_Open(port); @@ -152,10 +153,10 @@ int UDPNetworkUtility::Receive() { } //------------------------- -//send a SerialPacket +//send a SerialPacketBase //------------------------- -int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPacket) { +int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serialPacket) { IPaddress add; if (SDLNet_ResolveHost(&add, ip, port) == -1) { throw(std::runtime_error("Failed to resolve a host")); @@ -164,7 +165,7 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPack SendTo(&add, serialPacket); } -int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { +int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacketBase* serialPacket) { memset(packet->data, 0, packet->maxlen); serializePacket(serialPacket, packet->data); packet->len = PACKET_BUFFER_SIZE; @@ -179,7 +180,7 @@ int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { return ret; } -int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { +int UDPNetworkUtility::SendTo(int channel, SerialPacketBase* serialPacket) { memset(packet->data, 0, packet->maxlen); serializePacket(serialPacket, packet->data); packet->len = PACKET_BUFFER_SIZE; @@ -193,7 +194,7 @@ int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { return ret; } -int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { +int UDPNetworkUtility::SendToAllChannels(SerialPacketBase* serialPacket) { memset(packet->data, 0, packet->maxlen); serializePacket(serialPacket, packet->data); packet->len = PACKET_BUFFER_SIZE; @@ -210,7 +211,7 @@ int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { return sent; } -int UDPNetworkUtility::Receive(SerialPacket* serialPacket) { +int UDPNetworkUtility::Receive(SerialPacketBase* serialPacket) { memset(packet->data, 0, packet->maxlen); int ret = SDLNet_UDP_Recv(socket, packet); deserializePacket(serialPacket, packet->data); diff --git a/common/network/udp_network_utility.hpp b/common/network/udp_network_utility.hpp index d812ad7..3928ff5 100644 --- a/common/network/udp_network_utility.hpp +++ b/common/network/udp_network_utility.hpp @@ -23,7 +23,7 @@ #define UDPNETWORKUTILITY_HPP_ //common -#include "serial_packet.hpp" +#include "serial_packet_base.hpp" #include "singleton.hpp" //APIs @@ -50,12 +50,12 @@ public: int SendToAllChannels(void* data, int len); int Receive(); - //send a SerialPacket - int SendTo(const char* ip, int port, SerialPacket* serialPacket); - int SendTo(IPaddress* add, SerialPacket* serialPacket); - int SendTo(int channel, SerialPacket* serialPacket); - int SendToAllChannels(SerialPacket* serialPacket); - int Receive(SerialPacket* serialPacket); + //send a SerialPacketBase + int SendTo(const char* ip, int port, SerialPacketBase* serialPacket); + int SendTo(IPaddress* add, SerialPacketBase* serialPacket); + int SendTo(int channel, SerialPacketBase* serialPacket); + int SendToAllChannels(SerialPacketBase* serialPacket); + int Receive(SerialPacketBase* serialPacket); //accessors UDPpacket* GetPacket() const { diff --git a/server/makefile b/server/makefile index b096f70..e3d7530 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. accounts characters rooms ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities +INCLUDES+=. accounts characters rooms ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet_types ../common/utilities LIBS+=server.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/server_application.hpp b/server/server_application.hpp index 372549b..ecd08ab 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -30,6 +30,7 @@ //common utilities #include "udp_network_utility.hpp" +#include "serial_packet.hpp" #include "config_utility.hpp" #include "singleton.hpp" diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 114c7ff..94cc258 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -21,9 +21,6 @@ */ #include "server_application.hpp" -//for PACKET_BUFFER_SIZE -#include "serial.hpp" - //utility functions #include "sql_utility.hpp" #include "utility.hpp"