Added some structures to SerialPacket, and serial.cpp

I've also created the Statistics structure for simplicity.
This commit is contained in:
Kayne Ruse
2014-05-26 00:49:31 +10:00
parent c575ee9ce1
commit 1dd8042d3d
8 changed files with 243 additions and 80 deletions
+95
View File
@@ -22,6 +22,7 @@
#include "serial.hpp"
#include "map_allocator.hpp"
#include "statistics.hpp"
#include <cstring>
@@ -59,6 +60,7 @@ void serializeClient(SerialPacket* packet, char* buffer) {
//texts
SERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE);
//TODO: password
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
}
@@ -91,6 +93,35 @@ void serializeRegionContent(SerialPacket* packet, char* buffer) {
}
}
void serializeCombat(SerialPacket* packet, char* buffer) {
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
//integers
SERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int));
SERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int));
//TODO: more comabat info
}
void serializeStatistics(Statistics* stats, char* 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 serializeCharacter(SerialPacket* packet, char* buffer) {
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
@@ -108,6 +139,22 @@ void serializeCharacter(SerialPacket* packet, char* buffer) {
SERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double));
SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
//stats structure
serializeStatistics(&packet->characterInfo.stats, buffer);
buffer += sizeof(Statistics);
}
void serializeEnemy(SerialPacket* packet, char* buffer) {
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
//texts
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
//stats structure
serializeStatistics(&packet->characterInfo.stats, buffer);
buffer += sizeof(Statistics);
}
//-------------------------
@@ -137,6 +184,7 @@ void deserializeClient(SerialPacket* packet, char* buffer) {
//texts
DESERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE);
//TODO: password
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
}
@@ -176,6 +224,37 @@ void deserializeRegionContent(SerialPacket* packet, char* buffer) {
}
}
void deserializeCombat(SerialPacket* packet, char* buffer) {
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
//integers
DESERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int));
DESERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int));
//TODO: more comabat info
}
void deserializeStatistics(Statistics* stats, char* 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));
}
void deserializeCharacter(SerialPacket* packet, char* buffer) {
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
@@ -193,6 +272,22 @@ void deserializeCharacter(SerialPacket* packet, char* buffer) {
DESERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double));
DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
//stats structure
deserializeStatistics(&packet->characterInfo.stats, buffer);
buffer += sizeof(Statistics);
}
void deserializeEnemy(SerialPacket* packet, char* buffer) {
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
//texts
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
//stats structure
deserializeStatistics(&packet->characterInfo.stats, buffer);
buffer += sizeof(Statistics);
}
//-------------------------
+4 -4
View File
@@ -28,11 +28,11 @@
* NOTE: REGION_CONTENT is currently the largest type of packet
* map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t)
* map format: sizeof(int) * 3
* metadata: sizeof(metadata)
* metadata: sizeof(SerialPacket::Type)
*/
#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Metadata)
#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Type)
void serialize(SerialPacket* const, void*);
void deserialize(SerialPacket* const, void*);
void serialize(SerialPacket* const, void* dest);
void deserialize(SerialPacket* const, void* src);
#endif
+73 -29
View File
@@ -24,53 +24,80 @@
#include "vector2.hpp"
#include "region.hpp"
#include "statistics.hpp"
#include "SDL/SDL_net.h"
#define NETWORK_VERSION 20140512
#define PACKET_STRING_SIZE 100
#define NETWORK_VERSION 20140526
#pragma pack(push, 0)
//maximum string size; don't use std::string
#define PACKET_STRING_SIZE 100
union SerialPacket {
//types of packets
//TODO: readd the value definitions
enum class Type {
//default: there is something wrong
NONE = 0,
//not used
PING = 1,
PONG = 2,
//keep alive
PING,
PONG,
//TODO: rejection message
//Searching for a server to join
BROADCAST_REQUEST = 3,
BROADCAST_RESPONSE = 4,
//searching for a server to join
BROADCAST_REQUEST,
BROADCAST_RESPONSE,
BROADCAST_REJECTION,
//try to join the server
JOIN_REQUEST = 5,
JOIN_RESPONSE = 6,
JOIN_REQUEST,
JOIN_RESPONSE,
JOIN_REJECTION,
//mass update
SYNCHRONIZE = 7,
SYNCHRONIZE,
//disconnect from the server
DISCONNECT = 8,
DISCONNECT,
//shut down the server
SHUTDOWN = 9,
SHUTDOWN,
//map data
REGION_REQUEST = 10,
REGION_CONTENT = 11,
REGION_REQUEST,
REGION_CONTENT,
REGION_REJECTION,
//Character movement, etc.
CHARACTER_NEW = 12,
CHARACTER_DELETE = 13,
CHARACTER_UPDATE = 14,
//character data
CHARACTER_NEW,
CHARACTER_DELETE,
CHARACTER_UPDATE,
//TODO: combat packets
CHARACTER_STATS_REQUEST,
CHARACTER_STATS_RESPONSE,
CHARACTER_REJECTION,
//enemy data
ENEMY_NEW,
ENEMY_DELETE,
ENEMY_UPDATE,
ENEMY_STATS_REQUEST,
ENEMY_STATS_RESPONSE,
ENEMY_REJECTION,
//combat data
COMBAT_ENTER,
COMBAT_EXIT,
COMBAT_REJECTION,
//more packet types go here
//not used
LAST,
};
//metadata on the packet itself
@@ -79,7 +106,7 @@ union SerialPacket {
IPaddress srcAddress;
}meta;
//information about the server
//info about the server
struct ServerInformation {
Metadata meta;
int networkVersion;
@@ -87,18 +114,19 @@ union SerialPacket {
int playerCount;
}serverInfo;
//information about the client
//info about the client
struct ClientInformation {
Metadata meta;
int clientIndex;
int accountIndex;
int characterIndex;
char username[PACKET_STRING_SIZE];
//TODO: password
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
}clientInfo;
//map data
//info about a region
struct RegionInformation {
Metadata meta;
int mapIndex;
@@ -106,7 +134,16 @@ union SerialPacket {
Region* region;
}regionInfo;
//information about a character
//info about a combat scenario
struct CombatInformation {
Metadata meta;
int combatIndex;
int difficulty;
//TODO: background image, based on terrain type
//TODO: array of combatants
}combatInfo;
//info about a character
struct CharacterInformation {
Metadata meta;
int clientIndex;
@@ -117,8 +154,17 @@ union SerialPacket {
int mapIndex;
Vector2 position;
Vector2 motion;
Statistics stats;
}characterInfo;
//info about an enemy
struct EnemyInformation {
Metadata meta;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
Statistics stats;
}enemyInfo;
//defaults
SerialPacket() {
meta.type = Type::NONE;
@@ -126,6 +172,4 @@ union SerialPacket {
}
};
#pragma pack(pop)
#endif
+42
View File
@@ -0,0 +1,42 @@
/* 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 STATISTICS_HPP_
#define STATISTICS_HPP_
struct Statistics {
int level = 0;
int exp = 0;
int maxHP = 0;
int health = 0;
int maxMP = 0;
int mana = 0;
int attack = 0;
int defence = 0;
int intelligence = 0;
int resistance = 0;
int speed = 0;
float accuracy = 0.0;
float evasion = 0.0;
float luck = 0.0;
};
#endif
+2 -14
View File
@@ -25,6 +25,7 @@
//POD members
#include "bbox.hpp"
#include "vector2.hpp"
#include "statistics.hpp"
#include <string>
@@ -40,20 +41,7 @@ struct CharacterData {
Vector2 motion = {0.0,0.0};
//base statistics
int level = 0;
int exp = 0;
int maxHP = 0;
int health = 0;
int maxMP = 0;
int mana = 0;
int attack = 0;
int defence = 0;
int intelligence = 0;
int resistance = 0;
int speed = 0;
float accuracy = 0.0;
float evasion = 0.0;
float luck = 0.0;
Statistics stats;
//TODO: equipment
//TODO: items
+16 -14
View File
@@ -29,6 +29,7 @@
//Define the queries
//-------------------------
//TODO: save and load the statistics
static const char* CREATE_CHARACTER = "INSERT INTO PlayerCharacters (owner, handle, avatar) VALUES (?, ?, ?);";
static const char* LOAD_CHARACTER = "SELECT * FROM PlayerCharacters WHERE handle = ?;";
static const char* SAVE_CHARACTER = "UPDATE OR FAIL PlayerCharacters SET mapIndex = ?2, positionX = ?3, positionY = ?4 WHERE uid = ?1;";
@@ -38,6 +39,7 @@ static const char* DELETE_CHARACTER = "DELETE FROM PlayerCharacters WHERE uid =
//Define the methods
//-------------------------
//TODO: default stats as a parameter
int ServerApplication::CreateCharacter(int owner, std::string handle, std::string avatar) {
//Create the character, failing if it exists
sqlite3_stmt* statement = nullptr;
@@ -121,20 +123,20 @@ int ServerApplication::LoadCharacter(int owner, std::string handle, std::string
newChar.position.y = (double)sqlite3_column_int(statement, 7);
//statistics
newChar.level = sqlite3_column_int(statement, 8);
newChar.exp = sqlite3_column_int(statement, 9);
newChar.maxHP = sqlite3_column_int(statement, 10);
newChar.health = sqlite3_column_int(statement, 11);
newChar.maxMP = sqlite3_column_int(statement, 12);
newChar.mana = sqlite3_column_int(statement, 13);
newChar.attack = sqlite3_column_int(statement, 14);
newChar.defence = sqlite3_column_int(statement, 15);
newChar.intelligence = sqlite3_column_int(statement, 16);
newChar.resistance = sqlite3_column_int(statement, 17);
newChar.speed = sqlite3_column_int(statement, 18);
newChar.accuracy = sqlite3_column_double(statement, 19);
newChar.evasion = sqlite3_column_double(statement, 20);
newChar.luck = sqlite3_column_double(statement, 21);
newChar.stats.level = sqlite3_column_int(statement, 8);
newChar.stats.exp = sqlite3_column_int(statement, 9);
newChar.stats.maxHP = sqlite3_column_int(statement, 10);
newChar.stats.health = sqlite3_column_int(statement, 11);
newChar.stats.maxMP = sqlite3_column_int(statement, 12);
newChar.stats.mana = sqlite3_column_int(statement, 13);
newChar.stats.attack = sqlite3_column_int(statement, 14);
newChar.stats.defence = sqlite3_column_int(statement, 15);
newChar.stats.intelligence = sqlite3_column_int(statement, 16);
newChar.stats.resistance = sqlite3_column_int(statement, 17);
newChar.stats.speed = sqlite3_column_int(statement, 18);
newChar.stats.accuracy = sqlite3_column_double(statement, 19);
newChar.stats.evasion = sqlite3_column_double(statement, 20);
newChar.stats.luck = sqlite3_column_double(statement, 21);
//TODO: equipment
+4 -15
View File
@@ -22,6 +22,8 @@
#ifndef ENEMYDATA_HPP_
#define ENEMYDATA_HPP_
#include "statistics.hpp"
#include <string>
struct EnemyData {
@@ -29,21 +31,8 @@ struct EnemyData {
std::string handle;
std::string avatar;
//statistics
int level = 0;
int exp = 0;
int maxHP = 0;
int health = 0;
int maxMP = 0;
int mana = 0;
int attack = 0;
int defence = 0;
int intelligence = 0;
int resistance = 0;
int speed = 0;
float accuracy = 0.0;
float evasion = 0.0;
float luck = 0.0;
//gameplay
Statistics stats;
//TODO: equipment
//TODO: items
+7 -4
View File
@@ -1,7 +1,10 @@
* I need to keep the documentation up to date. Namely, the GDD is getting out of date.
* How many lookups is the map system using?
* Hook the serial packet to the network utility
* I completely forgot about status ailments
TODO: I need to keep the documentation up to date. Namely, the GDD is getting out of date.
TODO: How many lookups is the map system using?
TODO: Hook the serial packet to the network utility
TODO: I completely forgot about status ailments
TODO: Time delay for requesting region packets
TODO: command line parameters overriding config.cfg settings
TODO: inplementing SerialPacket in NetworkUtility
--Battle System--