Reverted some of the changes, stable
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
@@ -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
|
||||
* distribution.
|
||||
*/
|
||||
#include "client.hpp"
|
||||
#include "player_entry.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
//This is explicitly a POD
|
||||
static_assert(std::is_pod<Client>::value, "Client is not a POD");
|
||||
|
||||
unsigned int Client::uidCounter;
|
||||
unsigned int PlayerEntry::uidCounter;
|
||||
@@ -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 <string>
|
||||
|
||||
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;
|
||||
@@ -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<unsigned int, PlayerEntity> playerIter) -> bool {
|
||||
//delete server and client side players
|
||||
erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> 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<unsigned int, Entity> 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<unsigned int, Entity> entityIter) -> bool {
|
||||
//find the specified Entity
|
||||
if (entityIter.first == packet.playerInfo.playerIndex) {
|
||||
//delete the specified playerEntry
|
||||
erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> 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<unsigned int, PlayerEntity> 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);
|
||||
}
|
||||
|
||||
@@ -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<unsigned int, Client> clientMap;
|
||||
std::map<unsigned int, Entity> entityMap;
|
||||
std::map<unsigned int, PlayerEntity> playerMap;
|
||||
std::map<unsigned int, ClientEntry> clientMap;
|
||||
std::map<unsigned int, PlayerEntry> playerMap;
|
||||
|
||||
//maps
|
||||
//TODO: I need to handle multiple map objects
|
||||
|
||||
Reference in New Issue
Block a user