From 6d32d44fa312d7b262884c8d736c36a5433ae7b5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 15 Apr 2014 23:21:28 +1000 Subject: [PATCH] Reverted some of the changes, stable --- common/network/network_packet.hpp | 5 +- .../{player_entity.cpp => client_entry.cpp} | 4 +- server/{client.hpp => client_entry.hpp} | 6 +- server/entity.cpp | 29 ---- server/entity.hpp | 47 ------ server/{client.cpp => player_entry.cpp} | 9 +- .../{player_entity.hpp => player_entry.hpp} | 16 +- server/server_application.cpp | 145 +++++++----------- server/server_application.hpp | 10 +- 9 files changed, 84 insertions(+), 187 deletions(-) rename server/{player_entity.cpp => client_entry.cpp} (93%) rename server/{client.hpp => client_entry.hpp} (93%) delete mode 100644 server/entity.cpp delete mode 100644 server/entity.hpp rename server/{client.cpp => player_entry.cpp} (83%) rename server/{player_entity.hpp => player_entry.hpp} (85%) diff --git a/common/network/network_packet.hpp b/common/network/network_packet.hpp index 4933591..68b91ee 100644 --- a/common/network/network_packet.hpp +++ b/common/network/network_packet.hpp @@ -31,7 +31,7 @@ #pragma pack(push, 0) -//TODO: update the code here to match the entity code +//TODO: rename to serial packet union NetworkPacket { //types of packets enum class Type { @@ -85,6 +85,7 @@ union NetworkPacket { }serverInfo; //information about the client + //TODO: login credentials struct ClientInformation { Metadata meta; int index; @@ -95,6 +96,8 @@ union NetworkPacket { Metadata meta; int clientIndex; int playerIndex; + //TODO: should probably move these into the client info + //TODO: these might actually do better during the login system char handle[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE]; Vector2 position; diff --git a/server/player_entity.cpp b/server/client_entry.cpp similarity index 93% rename from server/player_entity.cpp rename to server/client_entry.cpp index ccc49b3..7708423 100644 --- a/server/player_entity.cpp +++ b/server/client_entry.cpp @@ -19,6 +19,6 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "player_entity.hpp" +#include "client_entry.hpp" -unsigned int PlayerEntity::uidCounter; +unsigned int ClientEntry::uidCounter; diff --git a/server/client.hpp b/server/client_entry.hpp similarity index 93% rename from server/client.hpp rename to server/client_entry.hpp index cf4105e..42225b8 100644 --- a/server/client.hpp +++ b/server/client_entry.hpp @@ -19,12 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef CLIENT_HPP_ -#define CLIENT_HPP_ +#ifndef CLIENTENTRY_HPP_ +#define CLIENTENTRY_HPP_ #include "SDL/SDL_net.h" -struct Client { +struct ClientEntry { IPaddress address; static unsigned int uidCounter; }; diff --git a/server/entity.cpp b/server/entity.cpp deleted file mode 100644 index bfc6061..0000000 --- a/server/entity.cpp +++ /dev/null @@ -1,29 +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 "entity.hpp" - -#include - -//This is explicitly a POD -static_assert(std::is_pod::value, "Entity is not a POD"); - -unsigned int Entity::uidCounter; diff --git a/server/entity.hpp b/server/entity.hpp deleted file mode 100644 index 59296ba..0000000 --- a/server/entity.hpp +++ /dev/null @@ -1,47 +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 ENTITY_HPP_ -#define ENTITY_HPP_ - -//POD members -#include "vector2.hpp" -#include "bbox.hpp" - -struct Entity { - enum Type { - PLAYER, - PORTAL, - ITEMS, - CHEST, - DOOR, - }; - - Type type; - int mapIndex; - Vector2 position; - Vector2 motion; - BBox bbox; - unsigned int externalID; - static unsigned int uidCounter; -}; - -#endif diff --git a/server/client.cpp b/server/player_entry.cpp similarity index 83% rename from server/client.cpp rename to server/player_entry.cpp index 7321482..11cfdfc 100644 --- a/server/client.cpp +++ b/server/player_entry.cpp @@ -19,11 +19,6 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "client.hpp" +#include "player_entry.hpp" -#include - -//This is explicitly a POD -static_assert(std::is_pod::value, "Client is not a POD"); - -unsigned int Client::uidCounter; +unsigned int PlayerEntry::uidCounter; diff --git a/server/player_entity.hpp b/server/player_entry.hpp similarity index 85% rename from server/player_entity.hpp rename to server/player_entry.hpp index 2a5a6e6..17dca99 100644 --- a/server/player_entity.hpp +++ b/server/player_entry.hpp @@ -19,17 +19,27 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef PLAYERENTITY_HPP_ -#define PLAYERENTITY_HPP_ +#ifndef PLAYERENTRY_HPP_ +#define PLAYERENTRY_HPP_ + +//POD members +#include "bbox.hpp" +#include "vector2.hpp" #include -struct PlayerEntity { +struct PlayerEntry { //metadata int clientIndex; std::string handle; std::string avatar; + //world position + int mapIndex; + Vector2 position; + Vector2 motion; + BBox bbox; + //statistics int level; int exp; diff --git a/server/server_application.cpp b/server/server_application.cpp index f90fffe..0ce7bd3 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -53,9 +53,8 @@ void ServerApplication::Init(int argc, char** argv) { cout << "Beginning startup" << endl; //initial setup - Client::uidCounter = 0; - Entity::uidCounter = 0; - PlayerEntity::uidCounter = 0; + ClientEntry::uidCounter = 0; + PlayerEntry::uidCounter = 0; config.Load("rsc\\config.cfg"); //Init SDL @@ -209,19 +208,21 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) { void ServerApplication::HandleJoinRequest(NetworkPacket packet) { //register the new client - clientMap[Client::uidCounter] = {packet.meta.srcAddress}; + ClientEntry newClient; + newClient.address = packet.meta.srcAddress; + clientMap[ClientEntry::uidCounter] = newClient; //send the client their index char buffer[PACKET_BUFFER_SIZE]; packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE; - packet.clientInfo.index = Client::uidCounter; + packet.clientInfo.index = ClientEntry::uidCounter; serialize(&packet, buffer); //bounce this packet - network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); + network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE); //finished this routine - Client::uidCounter++; + ClientEntry::uidCounter++; cout << "Connect, total: " << clientMap.size() << endl; } @@ -239,19 +240,14 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) { delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE; //TODO: can this use DeletePlayer() instead? - //delete PlayerEntity, Entity, and client side players - erase_if(playerMap, [&](std::pair playerIter) -> bool { + //delete server and client side players + erase_if(playerMap, [&](std::pair it) -> bool { //find the internal players to delete - if (playerIter.second.clientIndex == packet.clientInfo.index) { + if (it.second.clientIndex == packet.clientInfo.index) { //send the delete player command to all clients - delPacket.playerInfo.playerIndex = playerIter.first; + delPacket.playerInfo.playerIndex = it.first; PumpPacket(delPacket); - //erase the corresponding Entity - erase_if(entityMap, [&](std::pair entityIter) -> bool { - return entityIter.second.type == Entity::Type::PLAYER && entityIter.second.externalID == playerIter.first; - }); - //delete this player object return true; } @@ -273,32 +269,15 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) { //TODO: map? - //entities - for (auto& it : entityMap) { - //what are we sending? - switch(it.second.type) { - case Entity::Type::PLAYER: - //TODO: update the network code to match the entity code - newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; - newPacket.playerInfo.playerIndex = it.first; - snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", playerMap[it.second.externalID].handle.c_str()); - snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", playerMap[it.second.externalID].avatar.c_str()); - newPacket.playerInfo.position = it.second.position; - newPacket.playerInfo.motion = it.second.motion; - break; - case Entity::Type::PORTAL: - //TODO - break; - case Entity::Type::ITEMS: - //TODO - break; - case Entity::Type::CHEST: - //TODO - break; - case Entity::Type::DOOR: - //TODO - break; - } + //players + newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; + for (auto& it : playerMap) { + //TODO: update this for the expanded PlayerEntry structure + newPacket.playerInfo.playerIndex = it.first; + snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str()); + snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str()); + newPacket.playerInfo.position = it.second.position; + newPacket.playerInfo.motion = it.second.motion; serialize(&newPacket, buffer); network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE); } @@ -317,52 +296,42 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) { } void ServerApplication::HandlePlayerNew(NetworkPacket packet) { - //register the new Entity - entityMap[Entity::uidCounter] = { - Entity::Type::PLAYER, - 0, - {0, 0}, - {0, 0}, - {0, 0, 0, 0}, - PlayerEntity::uidCounter - }; + //register the new PlayerEntry + //NOTE: assigning each field one-by-one so adding or moving a field doesn't break this code + PlayerEntry newPlayer; - //register the new PlayerEntity - playerMap[PlayerEntity::uidCounter] = { - packet.playerInfo.clientIndex, - packet.playerInfo.handle, - packet.playerInfo.avatar, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0.0, - 0.0, - 0.0 - }; + //metadata + newPlayer.clientIndex = packet.playerInfo.clientIndex; + newPlayer.handle = packet.playerInfo.handle; + newPlayer.avatar = packet.playerInfo.avatar; + + //position + newPlayer.mapIndex = 0; + newPlayer.position = {0,0}; + newPlayer.motion = {0,0}; + newPlayer.bbox = {0, 0, 0, 0}; + + //stats + //TODO + + //push this player + playerMap[PlayerEntry::uidCounter] = newPlayer; //send the client their info - packet.playerInfo.playerIndex = PlayerEntity::uidCounter; - packet.playerInfo.position = entityMap[Entity::uidCounter].position; - packet.playerInfo.motion = entityMap[Entity::uidCounter].motion; + packet.playerInfo.playerIndex = PlayerEntry::uidCounter; + packet.playerInfo.position = newPlayer.position; + packet.playerInfo.motion = newPlayer.motion; //actually send to everyone PumpPacket(packet); //finish this routine - Entity::uidCounter++; - PlayerEntity::uidCounter++; + PlayerEntry::uidCounter++; } void ServerApplication::HandlePlayerDelete(NetworkPacket packet) { //TODO: remove this? - if (entityMap.find(packet.playerInfo.playerIndex) == entityMap.end()) { + if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) { throw(std::runtime_error("Cannot delete a non-existant player")); } @@ -370,32 +339,30 @@ void ServerApplication::HandlePlayerDelete(NetworkPacket packet) { NetworkPacket delPacket; delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE; - //delete the specified Entity, PlayerEntity - erase_if(entityMap, [&](std::pair entityIter) -> bool { - //find the specified Entity - if (entityIter.first == packet.playerInfo.playerIndex) { + //delete the specified playerEntry + erase_if(playerMap, [&](std::pair it) -> bool { + //find the specified PlayerEntry + if (it.first == packet.playerInfo.playerIndex) { //send to all - delPacket.playerInfo.playerIndex = entityIter.first; + delPacket.playerInfo.playerIndex = it.first; PumpPacket(delPacket); - //erase matching PlayerEntity - erase_if(playerMap, [&](std::pair playerIter) -> bool { - return playerIter.first == entityIter.second.externalID; - }); + + //delete this player return true; } + //skip this player return false; }); } void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) { - //TODO: Lookup the reference once, and operate on that instead of looking it up 3 times - if (entityMap.find(packet.playerInfo.playerIndex) == entityMap.end()) { + if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) { throw(std::runtime_error("Cannot update a non-existant player")); } //TODO: the server needs it's own movement system too - entityMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position; - entityMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion; + playerMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position; + playerMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion; PumpPacket(packet); } diff --git a/server/server_application.hpp b/server/server_application.hpp index 4b19910..6609994 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -23,9 +23,8 @@ #define SERVERAPPLICATION_HPP_ //server specific stuff -#include "client.hpp" -#include "entity.hpp" -#include "player_entity.hpp" +#include "client_entry.hpp" +#include "player_entry.hpp" //maps #include "map_generator.hpp" @@ -84,9 +83,8 @@ private: lua_State* luaState = nullptr; //server tables - std::map clientMap; - std::map entityMap; - std::map playerMap; + std::map clientMap; + std::map playerMap; //maps //TODO: I need to handle multiple map objects