Example #1
0
void
SendControls ()
{
  static struct control c[5];
  int             i;
  for (i = 0; i < 5; i++)
    {
      if (control[i] && memcmp ((char *) &controls[i], (char *) &c[i], sizeof (controls[i])))
	{
	  memcpy ((char *) &c[i], (char *) &controls[i], sizeof (controls[i]));
	  switch (c[i].type)
	    {
	    case C_KEYBOARD:
	      SendKeys (i, c[i].mask);
	      break;
	    case C_RKEYBOARD:
	      SendRotation (i, c[i].mask);
	      break;
	    case C_MOUSE:
	      SendMouse (i, c[i].mx, c[i].my, c[i].mask);
	      break;
	    case C_JOYSTICK1:
	      SendMouse (i, c[i].jx, c[i].jy, c[i].mask);
	      break;
	    }
	}
    }
}
bool Engine::Network::CNetworkEngine::Update()
{
	if (m_bHost)
	{
		Engine::CActor* pcPlayer = m_pcWorld->GetActorByName("PlayerCube");
		
		float fNewLocationX = pcPlayer->GetMetricMember("LocationX");
		float fNewLocationY = pcPlayer->GetMetricMember("LocationY");
		float fNewLocationZ = pcPlayer->GetMetricMember("LocationZ");

		SendPosition(fNewLocationX, fNewLocationY, fNewLocationZ);

		float fNewRotationX = pcPlayer->GetMetricMember("RotationX");
		float fNewRotationY = pcPlayer->GetMetricMember("RotationY");
		float fNewRotationZ = pcPlayer->GetMetricMember("RotationZ");

		SendRotation(fNewRotationX, fNewRotationY, fNewRotationZ);

		//Engine::CActor* pcScoreZone1 = m_pcWorld->GetActorByName("ScoreZone1");

		//float fScore = pcScoreZone1->GetMetricMember("Score");

		//SendScore(fScore);

		float fStamina = pcPlayer->GetMetricMember("Stamina");

		SendStamina(fStamina);

		float fWalk = pcPlayer->GetMetricMember("SoundWalk");

		float fSurface = pcPlayer->GetMetricMember("LastHitSurface");

		SendSoundWalk(fWalk == 0.0f ? false : true, fSurface);

		float fSprint = pcPlayer->GetMetricMember("SoundSprint");

		SendSoundSprint(fSprint == 0.0f ? false : true);
	}
	else
	{
		Engine::CActor* pcPlayer2 = m_pcWorld->GetActorByName("PlayerCube2");

		float fNewLocationX = pcPlayer2->GetMetricMember("LocationX");
		float fNewLocationY = pcPlayer2->GetMetricMember("LocationY");
		float fNewLocationZ = pcPlayer2->GetMetricMember("LocationZ");

		SendPosition(fNewLocationX, fNewLocationY, fNewLocationZ);

		float fNewRotationX = pcPlayer2->GetMetricMember("RotationX");
		float fNewRotationY = pcPlayer2->GetMetricMember("RotationY");
		float fNewRotationZ = pcPlayer2->GetMetricMember("RotationZ");

		SendRotation(fNewRotationX, fNewRotationY, fNewRotationZ);

		//Engine::CActor* pcScoreZone2 = m_pcWorld->GetActorByName("ScoreZone2");

		//float fScore = pcScoreZone2->GetMetricMember("Score");

		//SendScore(fScore);

		float fStamina = pcPlayer2->GetMetricMember("Stamina");

		SendStamina(fStamina);

		float fWalk = pcPlayer2->GetMetricMember("SoundWalk");

		float fSurface = pcPlayer2->GetMetricMember("LastHitSurface");

		SendSoundWalk(fWalk == 0.0f ? false : true, fSurface);

		float fSprint = pcPlayer2->GetMetricMember("SoundSprint");

		SendSoundSprint(fSprint == 0.0f ? false : true);
	}

	for (m_pcPacket = m_pcPeer->Receive(); m_pcPacket; m_pcPeer->DeallocatePacket(m_pcPacket), m_pcPacket = m_pcPeer->Receive())
	{
		switch (m_pcPacket->data[0])
		{
		case ID_REMOTE_DISCONNECTION_NOTIFICATION:
			DEBUGPRINT("Another client has disconnected.\n");

			m_bConnected = false;

			break;
		case ID_REMOTE_CONNECTION_LOST:
			DEBUGPRINT("Another client has lost the connection.\n");
			break;
		case ID_REMOTE_NEW_INCOMING_CONNECTION:
			DEBUGPRINT("Another client has connected.\n");

			m_bConnected = true;

			break;
		case ID_CONNECTION_REQUEST_ACCEPTED:
		{
			DEBUGPRINT("Our connection request has been accepted.\n");

			m_bConnected = true;

			// Use a BitStream to write a custom user message
			// Bitstreams are easier to use than sending casted structures, and handle endian swapping automatically
			RakNet::BitStream bsOut;
			bsOut.Write((RakNet::MessageID)ID_GAME_VERSION);
			bsOut.Write(m_sztVersionNumber);
			m_pcPeer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, m_pcPacket->systemAddress, false);
		}
			break;
		case ID_NEW_INCOMING_CONNECTION:
			DEBUGPRINT("A connection is incoming.\n");

			m_bConnected = true;

			break;
		case ID_NO_FREE_INCOMING_CONNECTIONS:
			DEBUGPRINT("The server is full.\n");
			break;
		case ID_DISCONNECTION_NOTIFICATION:
			if (m_bHost)
			{
				DEBUGPRINT("A client has disconnected.\n");
			}
			else 
			{
				DEBUGPRINT("We have been disconnected.\n");
			}

			m_bConnected = false;

			break;
		case ID_CONNECTION_LOST:
			if (m_bHost)
			{
				DEBUGPRINT("A client lost the connection.\n");
			}
			else 
			{
				DEBUGPRINT("Connection lost.\n");
			}
			break;

		case ID_GAME_VERSION:
		{
			memcpy(m_pcSystemAddress, &(m_pcPacket->systemAddress), sizeof(RakNet::SystemAddress));

			size_t sztVersion = 0;
			RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
			bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
			bsIn.Read(sztVersion);

			if (m_sztVersionNumber != sztVersion)
			{
				if (m_bHost)
				{
					DEBUGPRINT("Server is using version %i.  Client had version %i.  Disconnecting client.\n", m_sztVersionNumber, sztVersion);
				}
				else
				{
					DEBUGPRINT("Connected to server with matching version of %u\n", sztVersion);
				}

				m_pcPeer->CloseConnection(RakNet::AddressOrGUID(m_pcPacket), true);
			}
			else
			{
				if (m_bHost)
				{
					DEBUGPRINT("Client connected to server with matching version of %u\n", m_sztVersionNumber);

					RakNet::BitStream bsOut;
					bsOut.Write((RakNet::MessageID)ID_GAME_VERSION);
					bsOut.Write(m_sztVersionNumber);

					m_pcPeer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, m_pcPacket->systemAddress, false);
				}
				else
				{
					DEBUGPRINT("Connected to server with matching version %i.\n", m_sztVersionNumber);

					size_t sztNetworkID = 1;

					RakNet::BitStream bsOut;
					bsOut.Write((RakNet::MessageID)ID_GAME_SETUP);
					bsOut.Write(sztNetworkID);

					m_pcPeer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, m_pcPacket->systemAddress, false);
				}
			}

			
		}

			break;
		case ID_GAME_SETUP:
		{
			if (m_bHost)
			{
				Engine::CActor* pcPlayer = m_pcWorld->GetActorByName("PlayerCube");
				Engine::CActor* pcPlayer2 = m_pcWorld->GetActorByName("PlayerCube2");
				
				pcPlayer->SetDataMember("Status", "Enabled");
				pcPlayer->SetDataMember("NetworkControl", "Enabled");
				pcPlayer->SetMetricMember("LocationX", 0.0f);
				pcPlayer->SetMetricMember("LocationY", 0.0f);
				pcPlayer->SetMetricMember("LocationZ", -500.0f);
				pcPlayer->SetMetricMember("RotationX", 0.0f);
				pcPlayer->SetMetricMember("RotationY", 0.0f);
				pcPlayer->SetMetricMember("RotationZ", 0.0f);

				pcPlayer2->SetDataMember("Status", "Enabled");
				pcPlayer2->SetDataMember("NetworkControl", "Disabled");
				pcPlayer2->SetMetricMember("LocationX", 0.0f);
				pcPlayer2->SetMetricMember("LocationY", 0.0f);
				pcPlayer2->SetMetricMember("LocationZ", 500.0f);
				pcPlayer2->SetMetricMember("RotationX", 0.0f);
				pcPlayer2->SetMetricMember("RotationY", 180.0f);
				pcPlayer2->SetMetricMember("RotationZ", 0.0f);

				Engine::CActor* pcPlayer2Forward = m_pcWorld->GetActorByName("Player2Forward");

				pcPlayer2Forward->SetDataMember("Status", "Enabled");

				Engine::CActor* pcCamera = m_pcWorld->GetActorByName("Camera");
				Engine::CActor* pcCamera2 = m_pcWorld->GetActorByName("Camera2");

				pcCamera->SetDataMember("CameraStatus", "Enabled");
				pcCamera2->SetDataMember("CameraStatus", "Disabled");
				pcCamera->SetDataMember("LookAtStatus", "Enabled");
				pcCamera2->SetDataMember("LookAtStatus", "Disabled");

				Engine::CActor* pcScoreZone1 = m_pcWorld->GetActorByName("ScoreZone1");
				Engine::CActor* pcScoreZone2 = m_pcWorld->GetActorByName("ScoreZone2");

				pcScoreZone1->SetMetricMember("Score", 0.0f);
				pcScoreZone2->SetMetricMember("Score", 0.0f);
				pcScoreZone1->SetDataMember("TextStatus", "Enabled");
				pcScoreZone2->SetDataMember("TextStatus", "Enabled");

				size_t sztNetworkID = 2;

				RakNet::BitStream bsOut;
				bsOut.Write((RakNet::MessageID)ID_GAME_SETUP);
				bsOut.Write(sztNetworkID);

				m_pcPeer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, m_pcPacket->systemAddress, false);
			}
			else
			{
				Engine::CActor* pcPlayer = m_pcWorld->GetActorByName("PlayerCube");
				Engine::CActor* pcPlayer2 = m_pcWorld->GetActorByName("PlayerCube2");

				pcPlayer->SetDataMember("Status", "Enabled");
				pcPlayer->SetDataMember("NetworkControl", "Disabled");
				pcPlayer->SetMetricMember("LocationX", 0.0f);
				pcPlayer->SetMetricMember("LocationY", 0.0f);
				pcPlayer->SetMetricMember("LocationZ", -500.0f);
				pcPlayer->SetMetricMember("RotationX", 0.0f);
				pcPlayer->SetMetricMember("RotationY", 0.0f);
				pcPlayer->SetMetricMember("RotationZ", 0.0f);

				pcPlayer2->SetDataMember("Status", "Enabled");
				pcPlayer2->SetDataMember("NetworkControl", "Enabled");
				pcPlayer2->SetDataMember("Status", "Enabled");
				pcPlayer2->SetMetricMember("LocationX", 0.0f);
				pcPlayer2->SetMetricMember("LocationY", 0.0f);
				pcPlayer2->SetMetricMember("LocationZ", 500.0f);
				pcPlayer2->SetMetricMember("RotationX", 0.0f);
				pcPlayer2->SetMetricMember("RotationY", 180.0f);
				pcPlayer2->SetMetricMember("RotationZ", 0.0f);

				pcPlayer2->SetInputMember("MovePositiveX", pcPlayer->GetInputMember("MovePositiveX"));
				pcPlayer2->SetInputMember("MovePositiveY", pcPlayer->GetInputMember("MovePositiveY"));
				pcPlayer2->SetInputMember("MovePositiveZ", pcPlayer->GetInputMember("MovePositiveZ"));
				pcPlayer2->SetInputMember("MoveNegativeX", pcPlayer->GetInputMember("MoveNegativeX"));
				pcPlayer2->SetInputMember("MoveNegativeY", pcPlayer->GetInputMember("MoveNegativeY"));
				pcPlayer2->SetInputMember("MoveNegativeZ", pcPlayer->GetInputMember("MoveNegativeZ"));
				pcPlayer2->SetInputMember("Run", pcPlayer->GetInputMember("Run"));

				Engine::CActor* pcCamera = m_pcWorld->GetActorByName("Camera");
				Engine::CActor* pcCamera2 = m_pcWorld->GetActorByName("Camera2");

				pcCamera->SetDataMember("CameraStatus", "Disabled");
				pcCamera2->SetDataMember("CameraStatus", "Enabled");
				pcCamera->SetDataMember("LookAtStatus", "Disabled");
				pcCamera2->SetDataMember("LookAtStatus", "Enabled");

				Engine::CActor* pcScoreZone1 = m_pcWorld->GetActorByName("ScoreZone1");
				Engine::CActor* pcScoreZone2 = m_pcWorld->GetActorByName("ScoreZone2");

				pcScoreZone1->SetMetricMember("Score", 0.0f);
				pcScoreZone2->SetMetricMember("Score", 0.0f);
				pcScoreZone1->SetDataMember("TextStatus", "Enabled");
				pcScoreZone2->SetDataMember("TextStatus", "Enabled");

				Engine::CActor* pcStaminaBar1 = m_pcWorld->GetActorByName("StaminaBar1");

				pcStaminaBar1->SetDataMember("ActiveState", "Game");

				Engine::CActor* pcStaminaBar2 = m_pcWorld->GetActorByName("StaminaBar2");

				pcStaminaBar2->SetDataMember("ActiveState", "Game");

				Engine::CActor* pcPlayer2Forward = m_pcWorld->GetActorByName("Player2Forward");
				pcPlayer2Forward->SetDataMember("Status", "Enabled");
			}
		}

			break;
		case ID_GAME_POSITION:
		{
			if (m_bHost)
			{
				float fX = 0.0f;
				float fY = 0.0f;
				float fZ = 0.0f;
				float fLastHit = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fX);
				bsIn.Read(fY);
				bsIn.Read(fZ);

				Engine::CActor* pcPlayer2 = m_pcWorld->GetActorByName("PlayerCube2");

				pcPlayer2->SetMetricMember("LocationX", fX);
				pcPlayer2->SetMetricMember("LocationY", fY);
				pcPlayer2->SetMetricMember("LocationZ", fZ);

				m_pcWorld->ResetTimer();
			}
			else
			{
				float fX = 0.0f;
				float fY = 0.0f;
				float fZ = 0.0f;
				float fPlaySound = -1.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fX);
				bsIn.Read(fY);
				bsIn.Read(fZ);

				Engine::CActor* pcPlayer = m_pcWorld->GetActorByName("PlayerCube");

				pcPlayer->SetMetricMember("LocationX", fX);
				pcPlayer->SetMetricMember("LocationY", fY);
				pcPlayer->SetMetricMember("LocationZ", fZ);

				m_pcWorld->ResetTimer();
			}
		}

			break;
		case ID_GAME_ROTATION:
		{
			if (m_bHost)
			{
				float fX = 0.0f;
				float fY = 0.0f;
				float fZ = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fX);
				bsIn.Read(fY);
				bsIn.Read(fZ);

				Engine::CActor* pcPlayer2 = m_pcWorld->GetActorByName("PlayerCube2");

				pcPlayer2->SetMetricMember("RotationX", fX);
				pcPlayer2->SetMetricMember("RotationY", fY);
				pcPlayer2->SetMetricMember("RotationZ", fZ);

				m_pcWorld->ResetTimer();
			}
			else
			{
				float fX = 0.0f;
				float fY = 0.0f;
				float fZ = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fX);
				bsIn.Read(fY);
				bsIn.Read(fZ);

				Engine::CActor* pcPlayer = m_pcWorld->GetActorByName("PlayerCube");

				pcPlayer->SetMetricMember("RotationX", fX);
				pcPlayer->SetMetricMember("RotationY", fY);
				pcPlayer->SetMetricMember("RotationZ", fZ);

				m_pcWorld->ResetTimer();
			}
		}

			break;
		case ID_GAME_SCORE:
		{
			if (m_bHost)
			{
				float fScore = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fScore);

				Engine::CActor* pcScoreZone2 = m_pcWorld->GetActorByName("ScoreZone2");

				pcScoreZone2->SetMetricMember("Score", fScore);

				m_pcWorld->ResetTimer();
			}
			else
			{
				float fScore = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fScore);

				Engine::CActor* pcScoreZone1 = m_pcWorld->GetActorByName("ScoreZone1");

				pcScoreZone1->SetMetricMember("Score", fScore);

				m_pcWorld->ResetTimer();
			}
		}

			break;
		case ID_GAME_STAMINA:
		{
			if (m_bHost)
			{
				float fStamina = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fStamina);

				Engine::CActor* pcPlayer2 = m_pcWorld->GetActorByName("PlayerCube2");

				pcPlayer2->SetMetricMember("Stamina", fStamina);

				m_pcWorld->ResetTimer();
			}
			else
			{
				float fStamina = 0.0f;

				RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				bsIn.Read(fStamina);

				Engine::CActor* pcPlayer = m_pcWorld->GetActorByName("PlayerCube");

				pcPlayer->SetMetricMember("Stamina", fStamina);

				m_pcWorld->ResetTimer();
			}
		}

			break;
		case ID_SOUND_SPRINT:
		{
			bool bSprint = false;

			RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
			bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
			bsIn.Read(bSprint);

			if (bSprint)
			{
				if (eae6320::UserSettings::IsSoundOn())
				{
					Engine::CActor* pcPlayer = nullptr;

					if (m_bHost)
					{
						pcPlayer = m_pcWorld->GetActorByName("PlayerCube2");
					}
					else
					{
						pcPlayer = m_pcWorld->GetActorByName("PlayerCube");
					}

					float fX = pcPlayer->GetMetricMember("LocationX");
					float fY = pcPlayer->GetMetricMember("LocationY");
					float fZ = pcPlayer->GetMetricMember("LocationZ");

					Shared::Math::CVector3 v3Position(fX, fY, fZ);
					Shared::Math::CVector3 v3Velocity(0.0f, 0.0f, 0.0f);

					m_pcSoundEngine->Play3D(m_pcSprintSound, m_pcSprintChannel, v3Position, v3Velocity);
					m_pcSoundEngine->SetVolume(50.0f * eae6320::UserSettings::GetSoundVolumeLevel(), m_pcSprintChannel);
				}
			}
			else
			{
				m_pcSoundEngine->Stop(m_pcSprintChannel);
			}

			m_pcWorld->ResetTimer();
		}
			break;
		case ID_SOUND_WALK:
		{
			float fSurface = -1.0f;
			bool bWalk = false;

			RakNet::BitStream bsIn(m_pcPacket->data, m_pcPacket->length, false);
			bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
			bsIn.Read(bWalk);
			bsIn.Read(fSurface);

			if (bWalk)
			{
				Engine::CActor* pcPlayer = nullptr;

				if (m_bHost)
				{
					pcPlayer = m_pcWorld->GetActorByName("PlayerCube2");
				}
				else
				{
					pcPlayer = m_pcWorld->GetActorByName("PlayerCube");
				}

				float fX = pcPlayer->GetMetricMember("LocationX");
				float fY = pcPlayer->GetMetricMember("LocationY");
				float fZ = pcPlayer->GetMetricMember("LocationZ");

				Shared::Math::CVector3 v3Position(fX, fY, fZ);
				Shared::Math::CVector3 v3Velocity(0.0f, 0.0f, 0.0f);

				switch (static_cast<int>(fSurface))
				{
				case 0:
					if (eae6320::UserSettings::IsSoundOn())
					{
						m_pcSoundEngine->Play3D(m_pcGroundSound, m_pcGroundChannel, v3Position, v3Velocity);
						m_pcSoundEngine->SetVolume(1.0f * eae6320::UserSettings::GetSoundVolumeLevel(), m_pcGroundChannel);
					}

					m_pcSoundEngine->Stop(m_pcStairChannel);

					break;
				case 1:
					if (eae6320::UserSettings::IsSoundOn())
					{
						m_pcSoundEngine->Play3D(m_pcStairSound, m_pcStairChannel, v3Position, v3Velocity);
						m_pcSoundEngine->SetVolume(1.0f * eae6320::UserSettings::GetSoundVolumeLevel(), m_pcStairChannel);
					}

					m_pcSoundEngine->Stop(m_pcGroundChannel);

					break;
				default:
					m_pcSoundEngine->Stop(m_pcGroundChannel);
					m_pcSoundEngine->Stop(m_pcStairChannel);
					break;
				}
			}
			else
			{
				m_pcSoundEngine->Stop(m_pcGroundChannel);
				m_pcSoundEngine->Stop(m_pcStairChannel);
			}

			m_pcWorld->ResetTimer();
		}

			break;
		default:
			DEBUGPRINT("Message with identifier %i has arrived.\n", m_pcPacket->data[0]);
			break;
		}
	}

	return true;
}