Reverted some of the changes, stable

This commit is contained in:
Kayne Ruse
2014-04-15 23:21:28 +10:00
parent 9bacfb1424
commit 6d32d44fa3
9 changed files with 84 additions and 187 deletions
+4 -1
View File
@@ -31,7 +31,7 @@
#pragma pack(push, 0) #pragma pack(push, 0)
//TODO: update the code here to match the entity code //TODO: rename to serial packet
union NetworkPacket { union NetworkPacket {
//types of packets //types of packets
enum class Type { enum class Type {
@@ -85,6 +85,7 @@ union NetworkPacket {
}serverInfo; }serverInfo;
//information about the client //information about the client
//TODO: login credentials
struct ClientInformation { struct ClientInformation {
Metadata meta; Metadata meta;
int index; int index;
@@ -95,6 +96,8 @@ union NetworkPacket {
Metadata meta; Metadata meta;
int clientIndex; int clientIndex;
int playerIndex; 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 handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE];
Vector2 position; Vector2 position;
@@ -19,6 +19,6 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "player_entity.hpp" #include "client_entry.hpp"
unsigned int PlayerEntity::uidCounter; unsigned int ClientEntry::uidCounter;
@@ -19,12 +19,12 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef CLIENT_HPP_ #ifndef CLIENTENTRY_HPP_
#define CLIENT_HPP_ #define CLIENTENTRY_HPP_
#include "SDL/SDL_net.h" #include "SDL/SDL_net.h"
struct Client { struct ClientEntry {
IPaddress address; IPaddress address;
static unsigned int uidCounter; static unsigned int uidCounter;
}; };
-29
View File
@@ -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 <type_traits>
//This is explicitly a POD
static_assert(std::is_pod<Entity>::value, "Entity is not a POD");
unsigned int Entity::uidCounter;
-47
View File
@@ -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
@@ -19,11 +19,6 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "client.hpp" #include "player_entry.hpp"
#include <type_traits> unsigned int PlayerEntry::uidCounter;
//This is explicitly a POD
static_assert(std::is_pod<Client>::value, "Client is not a POD");
unsigned int Client::uidCounter;
@@ -19,17 +19,27 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef PLAYERENTITY_HPP_ #ifndef PLAYERENTRY_HPP_
#define PLAYERENTITY_HPP_ #define PLAYERENTRY_HPP_
//POD members
#include "bbox.hpp"
#include "vector2.hpp"
#include <string> #include <string>
struct PlayerEntity { struct PlayerEntry {
//metadata //metadata
int clientIndex; int clientIndex;
std::string handle; std::string handle;
std::string avatar; std::string avatar;
//world position
int mapIndex;
Vector2 position;
Vector2 motion;
BBox bbox;
//statistics //statistics
int level; int level;
int exp; int exp;
+56 -89
View File
@@ -53,9 +53,8 @@ void ServerApplication::Init(int argc, char** argv) {
cout << "Beginning startup" << endl; cout << "Beginning startup" << endl;
//initial setup //initial setup
Client::uidCounter = 0; ClientEntry::uidCounter = 0;
Entity::uidCounter = 0; PlayerEntry::uidCounter = 0;
PlayerEntity::uidCounter = 0;
config.Load("rsc\\config.cfg"); config.Load("rsc\\config.cfg");
//Init SDL //Init SDL
@@ -209,19 +208,21 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) {
void ServerApplication::HandleJoinRequest(NetworkPacket packet) { void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
//register the new client //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 //send the client their index
char buffer[PACKET_BUFFER_SIZE]; char buffer[PACKET_BUFFER_SIZE];
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE; packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = Client::uidCounter; packet.clientInfo.index = ClientEntry::uidCounter;
serialize(&packet, buffer); serialize(&packet, buffer);
//bounce this packet //bounce this packet
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE);
//finished this routine //finished this routine
Client::uidCounter++; ClientEntry::uidCounter++;
cout << "Connect, total: " << clientMap.size() << endl; cout << "Connect, total: " << clientMap.size() << endl;
} }
@@ -239,19 +240,14 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE; delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
//TODO: can this use DeletePlayer() instead? //TODO: can this use DeletePlayer() instead?
//delete PlayerEntity, Entity, and client side players //delete server and client side players
erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntity> playerIter) -> bool { erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
//find the internal players to delete //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 //send the delete player command to all clients
delPacket.playerInfo.playerIndex = playerIter.first; delPacket.playerInfo.playerIndex = it.first;
PumpPacket(delPacket); PumpPacket(delPacket);
//erase the corresponding Entity
erase_if(entityMap, [&](std::pair<unsigned int, Entity> entityIter) -> bool {
return entityIter.second.type == Entity::Type::PLAYER && entityIter.second.externalID == playerIter.first;
});
//delete this player object //delete this player object
return true; return true;
} }
@@ -273,32 +269,15 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) {
//TODO: map? //TODO: map?
//entities //players
for (auto& it : entityMap) { newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
//what are we sending? for (auto& it : playerMap) {
switch(it.second.type) { //TODO: update this for the expanded PlayerEntry structure
case Entity::Type::PLAYER: newPacket.playerInfo.playerIndex = it.first;
//TODO: update the network code to match the entity code snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
newPacket.playerInfo.playerIndex = it.first; newPacket.playerInfo.position = it.second.position;
snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", playerMap[it.second.externalID].handle.c_str()); newPacket.playerInfo.motion = it.second.motion;
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;
}
serialize(&newPacket, buffer); serialize(&newPacket, buffer);
network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE); 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) { void ServerApplication::HandlePlayerNew(NetworkPacket packet) {
//register the new Entity //register the new PlayerEntry
entityMap[Entity::uidCounter] = { //NOTE: assigning each field one-by-one so adding or moving a field doesn't break this code
Entity::Type::PLAYER, PlayerEntry newPlayer;
0,
{0, 0},
{0, 0},
{0, 0, 0, 0},
PlayerEntity::uidCounter
};
//register the new PlayerEntity //metadata
playerMap[PlayerEntity::uidCounter] = { newPlayer.clientIndex = packet.playerInfo.clientIndex;
packet.playerInfo.clientIndex, newPlayer.handle = packet.playerInfo.handle;
packet.playerInfo.handle, newPlayer.avatar = packet.playerInfo.avatar;
packet.playerInfo.avatar,
0, //position
0, newPlayer.mapIndex = 0;
0, newPlayer.position = {0,0};
0, newPlayer.motion = {0,0};
0, newPlayer.bbox = {0, 0, 0, 0};
0,
0, //stats
0, //TODO
0,
0, //push this player
0.0, playerMap[PlayerEntry::uidCounter] = newPlayer;
0.0,
0.0
};
//send the client their info //send the client their info
packet.playerInfo.playerIndex = PlayerEntity::uidCounter; packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
packet.playerInfo.position = entityMap[Entity::uidCounter].position; packet.playerInfo.position = newPlayer.position;
packet.playerInfo.motion = entityMap[Entity::uidCounter].motion; packet.playerInfo.motion = newPlayer.motion;
//actually send to everyone //actually send to everyone
PumpPacket(packet); PumpPacket(packet);
//finish this routine //finish this routine
Entity::uidCounter++; PlayerEntry::uidCounter++;
PlayerEntity::uidCounter++;
} }
void ServerApplication::HandlePlayerDelete(NetworkPacket packet) { void ServerApplication::HandlePlayerDelete(NetworkPacket packet) {
//TODO: remove this? //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")); throw(std::runtime_error("Cannot delete a non-existant player"));
} }
@@ -370,32 +339,30 @@ void ServerApplication::HandlePlayerDelete(NetworkPacket packet) {
NetworkPacket delPacket; NetworkPacket delPacket;
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE; delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
//delete the specified Entity, PlayerEntity //delete the specified playerEntry
erase_if(entityMap, [&](std::pair<unsigned int, Entity> entityIter) -> bool { erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
//find the specified Entity //find the specified PlayerEntry
if (entityIter.first == packet.playerInfo.playerIndex) { if (it.first == packet.playerInfo.playerIndex) {
//send to all //send to all
delPacket.playerInfo.playerIndex = entityIter.first; delPacket.playerInfo.playerIndex = it.first;
PumpPacket(delPacket); PumpPacket(delPacket);
//erase matching PlayerEntity
erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntity> playerIter) -> bool { //delete this player
return playerIter.first == entityIter.second.externalID;
});
return true; return true;
} }
//skip this player
return false; return false;
}); });
} }
void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) { void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
//TODO: Lookup the reference once, and operate on that instead of looking it up 3 times if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
if (entityMap.find(packet.playerInfo.playerIndex) == entityMap.end()) {
throw(std::runtime_error("Cannot update a non-existant player")); throw(std::runtime_error("Cannot update a non-existant player"));
} }
//TODO: the server needs it's own movement system too //TODO: the server needs it's own movement system too
entityMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position; playerMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position;
entityMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion; playerMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion;
PumpPacket(packet); PumpPacket(packet);
} }
+4 -6
View File
@@ -23,9 +23,8 @@
#define SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_
//server specific stuff //server specific stuff
#include "client.hpp" #include "client_entry.hpp"
#include "entity.hpp" #include "player_entry.hpp"
#include "player_entity.hpp"
//maps //maps
#include "map_generator.hpp" #include "map_generator.hpp"
@@ -84,9 +83,8 @@ private:
lua_State* luaState = nullptr; lua_State* luaState = nullptr;
//server tables //server tables
std::map<unsigned int, Client> clientMap; std::map<unsigned int, ClientEntry> clientMap;
std::map<unsigned int, Entity> entityMap; std::map<unsigned int, PlayerEntry> playerMap;
std::map<unsigned int, PlayerEntity> playerMap;
//maps //maps
//TODO: I need to handle multiple map objects //TODO: I need to handle multiple map objects