Merge branch 'GameServer' of https://github.com/dean11/Danbias into GameClient

This commit is contained in:
lindaandersson 2014-02-21 11:23:57 +01:00
commit 56e45f586e
16 changed files with 167 additions and 43 deletions

View File

@ -597,7 +597,7 @@ const GameClientState::NetEvent & GameState::DataRecieved( const GameClientState
return GameClientState::event_processed; return GameClientState::event_processed;
case protocol_Gameplay_ObjectDisconnectPlayer: case protocol_Gameplay_ObjectDisconnectPlayer:
{ {
//Removes //Remove the disconnected player
Protocol_ObjectDisconnectPlayer decoded(data); Protocol_ObjectDisconnectPlayer decoded(data);
auto object = this->privData->dynamicObjects->find( decoded.objectID ); auto object = this->privData->dynamicObjects->find( decoded.objectID );
if( object != this->privData->dynamicObjects->end() ) if( object != this->privData->dynamicObjects->end() )

View File

@ -2,37 +2,76 @@
#include "CollisionManager.h" #include "CollisionManager.h"
using namespace GameLogic; using namespace GameLogic;
using namespace Oyster::Math;
DynamicObject::DynamicObject() DynamicObject::DynamicObject()
:Object() :Object()
{ {
this->isReleased = false;
this->isActive = true;
} }
DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID) DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID)
:Object(rigidBody, EventOnCollision, type, objectID) :Object(rigidBody, EventOnCollision, type, objectID)
{ {
this->isReleased = false;
this->isActive = true;
} }
DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , Oyster::Physics::ICustomBody::SubscriptMessage (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID) DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , Oyster::Physics::ICustomBody::SubscriptMessage (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID)
:Object(rigidBody, EventOnCollision, type, objectID) :Object(rigidBody, EventOnCollision, type, objectID)
{ {
this->isReleased = false;
this->isActive = true;
} }
DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, Oyster::Math::Float extraDamageOnCollision) DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, Oyster::Math::Float extraDamageOnCollision)
:Object(rigidBody, EventOnCollision, type, objectID) :Object(rigidBody, EventOnCollision, type, objectID)
{ {
this->extraDamageOnCollision = extraDamageOnCollision; this->extraDamageOnCollision = extraDamageOnCollision;
this->isReleased = false;
this->isActive = true;
} }
DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , Oyster::Physics::ICustomBody::SubscriptMessage (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, Oyster::Math::Float extraDamageOnCollision) DynamicObject::DynamicObject(Oyster::Physics::ICustomBody *rigidBody , Oyster::Physics::ICustomBody::SubscriptMessage (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, Oyster::Math::Float extraDamageOnCollision)
:Object(rigidBody, EventOnCollision, type, objectID) :Object(rigidBody, EventOnCollision, type, objectID)
{ {
this->extraDamageOnCollision = extraDamageOnCollision; this->extraDamageOnCollision = extraDamageOnCollision;
this->isReleased = false;
this->isActive = true;
} }
DynamicObject::~DynamicObject(void) DynamicObject::~DynamicObject(void)
{ {
} }
void DynamicObject::ReleaseDynamicObject()
{
//TODO: Inactivate the physics object
if(this->isReleased) return;
this->isReleased = true;
this->isActive = false;
this->lookDirection = Float3::null;
this->forwardDirection = Float3::null;
this->scale = Float3::null;
this->extraDamageOnCollision = 0;
}
bool DynamicObject::IsReleased()
{
return this->isReleased;
}
bool DynamicObject::IsActive()
{
return this->isActive;
}
void DynamicObject::Inactivate()
{
this->isActive = false;
}
void DynamicObject::Activate()
{
this->isActive = true;
this->isReleased = false;
}

View File

@ -22,7 +22,15 @@ namespace GameLogic
~DynamicObject(void); ~DynamicObject(void);
void ReleaseDynamicObject();
bool IsReleased();
bool IsActive();
void Inactivate();
void Activate();
private: private:
bool isActive;
bool isReleased;
}; };

View File

@ -67,6 +67,17 @@ void Game::GetAllPlayerPositions() const
Game::PlayerData* Game::CreatePlayer() Game::PlayerData* Game::CreatePlayer()
{ {
//Se if there is a free player somewhere in our list
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i] && this->players[i]->player->IsReleased())
{
//We give the body to someone else
this->players[i]->player->Activate();
return this->players[i];
}
}
// Find a free space in array or insert at end // Find a free space in array or insert at end
int insert = InsertObject(this->players, (PlayerData*)0); int insert = InsertObject(this->players, (PlayerData*)0);
int freeID = 0; int freeID = 0;

View File

@ -43,8 +43,8 @@ namespace GameLogic
void Rotate(const Oyster::Math3D::Float3& lookDir, const Oyster::Math3D::Float3& right) override; void Rotate(const Oyster::Math3D::Float3& lookDir, const Oyster::Math3D::Float3& right) override;
void TurnLeft(Oyster::Math3D::Float deltaLeftRadians ) override; void TurnLeft(Oyster::Math3D::Float deltaLeftRadians ) override;
ObjectSpecialType GetObjectType() const override; ObjectSpecialType GetObjectType() const override;
void Inactivate() override;
void Release() override;
Player *player; Player *player;
}; };

View File

@ -106,6 +106,9 @@ namespace GameLogic
* @return The current player state * @return The current player state
********************************************************/ ********************************************************/
virtual PLAYER_STATE GetState() const = 0; virtual PLAYER_STATE GetState() const = 0;
virtual void Inactivate() = 0;
virtual void Release() = 0;
}; };
class ILevelData :public IObjectData class ILevelData :public IObjectData

View File

@ -94,3 +94,11 @@ void Game::PlayerData::TurnLeft(Oyster::Math3D::Float deltaLeftRadians )
{ {
this->player->TurnLeft(deltaLeftRadians); this->player->TurnLeft(deltaLeftRadians);
} }
void Game::PlayerData::Inactivate()
{
this->player->Inactivate();
}
void Game::PlayerData::Release()
{
this->player->ReleaseDynamicObject();
}

View File

@ -293,6 +293,11 @@ bool Player::IsIdle()
return (this->playerState == PLAYER_STATE::PLAYER_STATE_IDLE); return (this->playerState == PLAYER_STATE::PLAYER_STATE_IDLE);
} }
void Player::Inactivate()
{
//this->
}
Oyster::Math::Float3 Player::GetPosition() const Oyster::Math::Float3 Player::GetPosition() const
{ {
return (Oyster::Math::Float3) this->rigidBody->GetState().centerPos; return (Oyster::Math::Float3) this->rigidBody->GetState().centerPos;

View File

@ -63,6 +63,8 @@ namespace GameLogic
bool IsJumping(); bool IsJumping();
bool IsIdle(); bool IsIdle();
void Inactivate();
Oyster::Math::Float3 GetPosition() const; Oyster::Math::Float3 GetPosition() const;
Oyster::Math::Float3 GetLookDir() const; Oyster::Math::Float3 GetLookDir() const;
Oyster::Math::Float4x4 GetOrientation() const; Oyster::Math::Float4x4 GetOrientation() const;

View File

@ -292,6 +292,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectEnabled 356
struct Protocol_ObjectPositionRotation :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectPositionRotation :public Oyster::Network::CustomProtocolObject
{ {
short object_ID; short object_ID;
@ -366,7 +367,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectEnabled 356 //#define protocol_Gameplay_ObjectEnabled 357
struct Protocol_ObjectEnable :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectEnable :public Oyster::Network::CustomProtocolObject
{ {
int objectID; int objectID;
@ -399,7 +400,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectDisabled 357 //#define protocol_Gameplay_ObjectDisabled 358
struct Protocol_ObjectDisable :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectDisable :public Oyster::Network::CustomProtocolObject
{ {
int objectID; int objectID;
@ -439,7 +440,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectCreate 358 //#define protocol_Gameplay_ObjectCreate 359
struct Protocol_ObjectCreate :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectCreate :public Oyster::Network::CustomProtocolObject
{ {
//ObjectType type; //ie player, box or whatever //ObjectType type; //ie player, box or whatever
@ -543,7 +544,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectCreatePlayer 359 //#define protocol_Gameplay_ObjectCreatePlayer 360
struct Protocol_ObjectCreatePlayer :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectCreatePlayer :public Oyster::Network::CustomProtocolObject
{ {
/*1*/ int object_ID; /*1*/ int object_ID;
@ -673,7 +674,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectJoinTeam 360 //#define protocol_Gameplay_ObjectJoinTeam 361
struct Protocol_ObjectJoinTeam :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectJoinTeam :public Oyster::Network::CustomProtocolObject
{ {
int objectID; int objectID;
@ -713,7 +714,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectLeaveTeam 361 //#define protocol_Gameplay_ObjectLeaveTeam 362
struct Protocol_ObjectLeaveTeam :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectLeaveTeam :public Oyster::Network::CustomProtocolObject
{ {
int objectID; int objectID;
@ -745,7 +746,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectWeaponCooldown 362 //#define protocol_Gameplay_ObjectWeaponCooldown 363
struct Protocol_ObjectWeaponCooldown :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectWeaponCooldown :public Oyster::Network::CustomProtocolObject
{ {
float seconds; float seconds;
@ -777,7 +778,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectWeaponEnergy 363 //#define protocol_Gameplay_ObjectWeaponEnergy 364
struct Protocol_ObjectWeaponEnergy :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectWeaponEnergy :public Oyster::Network::CustomProtocolObject
{ {
float energy; float energy;
@ -809,7 +810,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectRespawn 364 //#define protocol_Gameplay_ObjectRespawn 365
struct Protocol_ObjectRespawn :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectRespawn :public Oyster::Network::CustomProtocolObject
{ {
int objectID; int objectID;
@ -861,7 +862,7 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol; Oyster::Network::CustomNetProtocol protocol;
}; };
//#define protocol_Gameplay_ObjectDie 365 //#define protocol_Gameplay_ObjectDie 366
struct Protocol_ObjectDie :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectDie :public Oyster::Network::CustomProtocolObject
{ {
int objectID; int objectID;

View File

@ -69,7 +69,6 @@
#define protocol_Gameplay_ObjectWeaponEnergy 364 #define protocol_Gameplay_ObjectWeaponEnergy 364
#define protocol_Gameplay_ObjectRespawn 365 #define protocol_Gameplay_ObjectRespawn 365
#define protocol_Gameplay_ObjectDie 366 #define protocol_Gameplay_ObjectDie 366
//Disconnect
#define protocol_Gameplay_ObjectDisconnectPlayer 367 #define protocol_Gameplay_ObjectDisconnectPlayer 367
#define protocol_GameplayMAX 399 #define protocol_GameplayMAX 399

View File

@ -55,6 +55,7 @@ namespace DanBias
GameLogic::IPlayerData* ReleasePlayer(); GameLogic::IPlayerData* ReleasePlayer();
Oyster::Network::NetClient ReleaseClient(); Oyster::Network::NetClient ReleaseClient();
bool IsInvalid();
void Invalidate(); void Invalidate();
int IncrementFailedProtocol(); int IncrementFailedProtocol();
void ResetFailedProtocolCount(); void ResetFailedProtocolCount();

View File

@ -25,12 +25,15 @@ GameClient::GameClient(Utility::DynamicMemory::SmartPointer<Oyster::Network::Net
} }
GameClient::~GameClient() GameClient::~GameClient()
{ {
this->client = 0; if(this->player)
this->player = 0; this->player->Inactivate();
this->isReady = false; this->isReady = false;
this->character = L"crate_colonists.dan"; this->character = L"crate_colonists.dan";
this->alias = L"Unknown"; this->alias = L"Unknown";
this->secondsSinceLastResponse = 0.0f; this->secondsSinceLastResponse = 0.0f;
this->client = 0;
this->player = 0;
} }
void GameClient::SetPlayer(GameLogic::IPlayerData* player) void GameClient::SetPlayer(GameLogic::IPlayerData* player)
@ -58,8 +61,14 @@ void GameClient::SetState(ClientState state)
this->state = state; this->state = state;
} }
bool GameClient::IsInvalid()
{
return this->isInvalid;
}
void GameClient::Invalidate() void GameClient::Invalidate()
{ {
this->player->Release();
this->player = 0;
this->isInvalid = true; this->isInvalid = true;
this->isReady = false; this->isReady = false;
this->state = ClientState_Invalid; this->state = ClientState_Invalid;

View File

@ -62,24 +62,31 @@ using namespace DanBias;
{ {
case NetworkClient::ClientEventArgs::EventType_Disconnect: case NetworkClient::ClientEventArgs::EventType_Disconnect:
{ {
//Send disconnect message to all the other players so the player can be removed from the client.
Protocol_ObjectDisconnectPlayer dp(cl->GetClient()->GetID());
for(int i = 0; i < this->gClients.Size(); i++)
{
if(this->gClients[i] && this->gClients[i] != cl)
{
this->gClients[i]->GetClient()->Send(dp);
}
}
printf("\t(%i : %s) - EventType_Disconnect\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str()); printf("\t(%i : %s) - EventType_Disconnect\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str());
Protocol_ObjectDisconnectPlayer prot(this->gClients[temp]->GetPlayer()->GetID());
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if(i != temp && this->gClients[i]) this->gClients[i]->GetClient()->Send(prot);
}
this->gClients[temp]->Invalidate(); this->gClients[temp]->Invalidate();
} }
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve: case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve:
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
{
if(this->gClients[temp]->IncrementFailedProtocol() >= 5/*client->threshold*/) if(this->gClients[temp]->IncrementFailedProtocol() >= 5/*client->threshold*/)
{
printf("\t(%i : %s) - EventType_Disconnect\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str());
Protocol_ObjectDisconnectPlayer prot(this->gClients[temp]->GetPlayer()->GetID());
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if(i != temp && this->gClients[i]) this->gClients[i]->GetClient()->Send(prot);
}
this->gClients[temp]->Invalidate(); this->gClients[temp]->Invalidate();
}
}
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
this->ParseProtocol(e.args.data.protocol, cl); this->ParseProtocol(e.args.data.protocol, cl);
@ -267,10 +274,17 @@ using namespace DanBias;
switch (p.status) switch (p.status)
{ {
case GameLogic::Protocol_General_Status::States_disconected: case GameLogic::Protocol_General_Status::States_disconected:
{
printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID());
//TODO: Tell other clients
//Protocol_ Protocol_ObjectDisconnectPlayer prot(c->GetPlayer()->GetID());
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if( this->gClients[i] && c->GetClient()->GetID() != this->gClients[i]->GetClient()->GetID() ) this->gClients[i]->GetClient()->Send(prot);
}
c->Invalidate();
this->Detach(c->GetClient()->GetID()); this->Detach(c->GetClient()->GetID());
}
break; break;
case GameLogic::Protocol_General_Status::States_idle: case GameLogic::Protocol_General_Status::States_idle:

View File

@ -214,14 +214,21 @@ bool GameSession::Join(gClient gameClient)
{ {
for (unsigned int i = 0; i < this->gClients.Size(); i++) for (unsigned int i = 0; i < this->gClients.Size(); i++)
{ {
if(this->gClients[i]) if(this->gClients[i] && !this->gClients[i]->IsInvalid())
{ {
IPlayerData* temp = this->gClients[i]->GetPlayer(); IPlayerData* temp = this->gClients[i]->GetPlayer();
Protocol_ObjectCreatePlayer oc( temp->GetPosition(), temp->GetRotation(), temp->GetScale(), Protocol_ObjectCreatePlayer p1( temp->GetPosition(), temp->GetRotation(), temp->GetScale(),
temp->GetID(), false, temp->GetTeamID(), temp->GetID(), false, temp->GetTeamID(),
Utility::String::WStringToString(this->gClients[i]->GetAlias(), std::string()), Utility::String::WStringToString(this->gClients[i]->GetAlias(), std::string()),
Utility::String::WStringToString(this->gClients[i]->GetCharacter(), std::string())); Utility::String::WStringToString(this->gClients[i]->GetCharacter(), std::string()));
nwClient->Send(oc); nwClient->Send(p1);
temp = playerData;
Protocol_ObjectCreatePlayer p2( temp->GetPosition(), temp->GetRotation(), temp->GetScale(),
temp->GetID(), false, temp->GetTeamID(),
Utility::String::WStringToString(gameClient->GetAlias(), std::string()),
Utility::String::WStringToString(gameClient->GetCharacter(), std::string()));
this->gClients[i]->GetClient()->Send(p2);
} }
} }
} }

View File

@ -22,16 +22,16 @@ namespace Oyster
{ {
struct BroadcastOptions struct BroadcastOptions
{ {
//bool broadcast; bool broadcast;
//float broadcastInterval; float broadcastInterval;
//std::wstring subnetToBroadcast; std::wstring subnetToBroadcast;
//CustomNetProtocol broadcastMessage; CustomNetProtocol broadcastMessage;
//BroadcastOptions() BroadcastOptions()
//{ {
// broadcast = true; broadcast = true;
// broadcastInterval = 1.0f; broadcastInterval = 1.0f;
// subnetToBroadcast = L"192.168.0.1"; subnetToBroadcast = L"192.168.0.1";
//} }
} broadcastOptions; } broadcastOptions;
struct MainOptions struct MainOptions
@ -117,6 +117,23 @@ namespace Oyster
*/ */
int NetworkServer::GetPort(); int NetworkServer::GetPort();
/***************************************
Broadcast functions
***************************************/
//Set broadcast settings.
void SetBroadcast(CustomNetProtocol& broadcastMessage, float interval = 1.0f, bool enable = true);
//Set broadcast settings.
void SetBroadcastMessage(CustomNetProtocol& broadcastMessage);
//Enable/disable broadcast.
void SetBroadcast(bool enable);
//Set interval between each broadcast message in seconds.
void SetBroadcastInterval(float interval);
private: private:
struct PrivateData; struct PrivateData;
PrivateData* privateData; PrivateData* privateData;