Beispiel #1
0
void DownloadFinished(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{

	// Read the player name
	RakNet::RakString _strName;
	pBitStream->Read(_strName);
	CString strName(_strName.C_String());

	// Read the player serial
	RakNet::RakString _strSerial;
	pBitStream->Read(_strSerial);
	CString strSerial(_strSerial.C_String());

	// Is the nickname already in use?
	// TODO: check is nick in use

	// Is the player banned?
	// TODO: check is banned

	// Add the player to the manager
	// TODO: add to player manager
	CPlayerEntity * pPlayer = new CPlayerEntity();

	pPlayer->SetName(strName);

	// Do we need the id; maybe internal for easier sync but definetly not public to the scripting engine
	pPlayer->SetId(CServer::GetInstance()->GetPlayerManager()->Add(pPlayer));
	EntityId playerId = pPlayer->GetId();
	srand(time(NULL));
	pPlayer->SetColor(CColor(rand() % 256, rand() % 256, rand() % 256).dwHexColor); //generate random color

	CLogFile::Printf("[join] %s (%i) has connected to the server. (%s)", strName.Get(), playerId, strSerial.Get());

	CScriptArguments args;
	CScriptPlayer * pScriptPlayer = new CScriptPlayer();
	pScriptPlayer->SetEntity(pPlayer);
	pPlayer->SetScriptPlayer(pScriptPlayer);
	args.push(pScriptPlayer);
	CEvents::GetInstance()->Call("playerJoin", &args, CEventHandler::eEventType::NATIVE_EVENT, 0);

	// Construct a new bitstream
	RakNet::BitStream bitStream;

	// Write the player id
	bitStream.Write(playerId);

	// Write the player colour
	bitStream.Write(pPlayer->GetColor());

	// Write the server name
	bitStream.Write(RakNet::RakString("IV:Network DEV Server"));

	// Write the max player count
	bitStream.Write(CVAR_GET_INTEGER("maxplayers"));

	// Write the port
	bitStream.Write(CVAR_GET_INTEGER("port"));

	// Send it back to the player
	CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_INITIAL_DATA), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, playerId, false);

	for (EntityId i = 0; i < CServer::GetInstance()->GetPlayerManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetPlayerManager()->DoesExists(i) && i != playerId)
		{
			bitStream.Reset();
			bitStream.Write(i);
			bitStream.Write(CServer::GetInstance()->GetPlayerManager()->GetAt(i)->GetName().Get());
			bitStream.Write(CServer::GetInstance()->GetPlayerManager()->GetAt(i)->GetColor());
			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_NEW_PLAYER), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, playerId, false);
		}
	}

	for (EntityId i = 0; i < CServer::GetInstance()->GetPlayerManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetPlayerManager()->DoesExists(i) && i != playerId)
		{
			bitStream.Reset();
			bitStream.Write(playerId);
			bitStream.Write(pPlayer->GetName().Get());
			bitStream.Write(pPlayer->GetColor());
			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_NEW_PLAYER), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, i, false);
		}
	}


	for (EntityId i = 0; i < CServer::GetInstance()->GetVehicleManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetVehicleManager()->DoesExists(i))
		{
			bitStream.Reset();
			CVehicleEntity * pVehicle = CServer::GetInstance()->GetVehicleManager()->GetAt(i);
			bitStream.Write(pVehicle->GetId());
			bitStream.Write(pVehicle->GetModelId());

			CVector3 vecPosition;
			pVehicle->GetPosition(vecPosition);
			bitStream.Write(vecPosition);

			CVector3 vecRotation;
			pVehicle->GetRotation(vecRotation);
			bitStream.Write(vecRotation.fX);

			bitStream.Write(pVehicle->GetColor(1));
			bitStream.Write(pVehicle->GetColor(2));
			bitStream.Write(pVehicle->GetColor(3));
			bitStream.Write(pVehicle->GetColor(4));
			bitStream.Write(pVehicle->GetColor(5));

			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_CREATE_VEHICLE), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, playerId, false);
		}
	}

	for (EntityId i = 0; i < CServer::GetInstance()->GetCheckpointManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetCheckpointManager()->DoesExists(i))
		{
			bitStream.Reset();
			CCheckpointEntity * pCheckpoint = CServer::GetInstance()->GetCheckpointManager()->GetAt(i);
			bitStream.Write(pCheckpoint->GetId());
			bitStream.Write(pCheckpoint->GetType());

			CVector3 vecPosition;
			pCheckpoint->GetPosition(vecPosition);
			bitStream.Write(vecPosition);

			CVector3 vecTargetPosition;
			pCheckpoint->GetTargetPosition(vecTargetPosition);
			bitStream.Write(vecTargetPosition);

			bitStream.Write(pCheckpoint->GetRadius());

			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_CREATE_CHECKPOINT), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, playerId, false);


			// Checkpoint visibility
			if (!pCheckpoint->GetVisible())
			{
				bitStream.Reset();

				bitStream.Write(pCheckpoint->GetId());
				CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_CHECKPOINT_HIDE), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, playerId, false);

			}
		}
	}
}
Beispiel #2
0
bool CClientCommands::HandleUserInput(CString strCommand, CString strParameters)
{
	if(strCommand == "q"  || strCommand == "quit")
	{
		// Are we connected to the network?
		if(g_pCore->GetNetworkManager()->IsConnected())
		{
			// Disconnect and shutdown RakNet
			g_pCore->GetNetworkManager()->Shutdown(500, true);
		}
		
		TerminateProcess(GetCurrentProcess(), 0);
		return true;
	}
	else if(strCommand == "qq" || strCommand == "quickquit")
	{
		TerminateProcess(GetCurrentProcess(), 0);
		return true;
	}
	else if (strCommand == "saveposition" || strCommand == "save")
	{
		FILE * file = fopen(SharedUtility::GetAbsolutePath("multiplayer//SavePositions.log"), "a");
		if (!file)
		{
			g_pCore->GetChat()->Print("Failed to open 'SavePositions.log'");
			return true;
		}

		CVector3 vecPosition;

		// Get our local player
		CLocalPlayer * pLocalPlayer = g_pCore->GetGame()->GetLocalPlayer();

		if (pLocalPlayer->IsInVehicle())
		{
			CVehicleEntity * pVehicle = pLocalPlayer->GetVehicleEntity();

			if (pVehicle)
			{
				pVehicle->GetPosition(vecPosition);
				CVector3 vecRotation;
				pVehicle->GetRotation(vecRotation);
				DWORD dwColors[5];
				pVehicle->GetColors(dwColors[0], dwColors[1], dwColors[2], dwColors[3], dwColors[4]);
				fprintf_s(file, "createVehicle(%d, %f, %f, %f, %f, %f, %f, %d, %d, %d, %d, %d);%s%s\n", CIVModelManager::ModelHashToVehicleId(pVehicle->GetModelInfo()->GetHash()), vecPosition.fX, vecPosition.fY, vecPosition.fZ, vecRotation.fX, vecRotation.fY, vecRotation.fZ, dwColors[0], dwColors[1], dwColors[2], dwColors[3], dwColors[4], strParameters.GetLength() > 0 ? " // " : "", strParameters.GetLength() > 0 ? strParameters.Get() : "");
			}
		}
		else
		{
			pLocalPlayer->GetPosition(vecPosition);
			int iModelId = pLocalPlayer->GetPlayerPed()->GetModelIndex();
			fprintf_s(file, "PlayerData(%d, %f, %f, %f, %f);%s%s\n", iModelId, vecPosition.fX, vecPosition.fY, vecPosition.fZ, pLocalPlayer->GetHeading(), strParameters.GetLength() > 0 ? " // " : "", strParameters.GetLength() > 0 ? strParameters.Get() : "");
		}

		fclose(file);
		g_pCore->GetChat()->Print("Position data saved to 'SavePositions.log'");
		return true;
	}
#ifdef _DEBUG
	else if(strCommand == "cv")
	{
		CVector3 vecCreatePos; 
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecCreatePos);
		vecCreatePos.fX += 4;

		CVehicleEntity * pVehicle = new CVehicleEntity(atoi(strParameters.Get()), vecCreatePos, 0.0f, 0x000000, 0x000000, 0x000000, 0x000000, 0xFFFFFF);
		if(pVehicle) {
			// Add our vehicle
			pVehicle->SetId(g_pCore->GetGame()->GetVehicleManager()->Add(pVehicle));
			pVehicle->Create();
			g_pCore->GetGame()->GetLocalPlayer()->WarpIntoVehicle(pVehicle);
		}
		return true;
	}
	else if(strCommand == "respawn")
	{
		g_pCore->GetGame()->GetLocalPlayer()->Respawn();
		return true;
	}
	else if(strCommand == "cp")
	{
		CVector3 vecCreatePos; 
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecCreatePos);

		CPlayerEntity * pPlayer = new CPlayerEntity(false);
		if(pPlayer) {
			pPlayer->SetModel(7);
			pPlayer->Create();
			pPlayer->Teleport(vecCreatePos);
		}
		return true;
	}
	else if(strCommand == "engine") 
	{
		if(g_pCore->GetGame()->GetLocalPlayer()->GetVehicleEntity() != NULL)
			g_pCore->GetGame()->GetLocalPlayer()->GetVehicleEntity()->SetEngineState(!g_pCore->GetGame()->GetLocalPlayer()->GetVehicleEntity()->GetEngineState());
		
		return true;
	}
	else if(strCommand == "giveweapon")
	{
		CIVScript::GiveWeaponToChar(g_pCore->GetGame()->GetLocalPlayer()->GetScriptingHandle(), (CIVScript::eWeapon)atoi(strParameters.Get()), 100, true);
		return true;
	}
	else if(strCommand == "xaxis")
	{
		CVector3 vecPositon;
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecPositon);

		vecPositon.fX += atoi(strParameters.Get());
		g_pCore->GetGame()->GetLocalPlayer()->SetPosition(vecPositon);
		return true;
	}
	else if(strCommand == "yaxis")
	{
		CVector3 vecPositon;
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecPositon);

		vecPositon.fY += atoi(strParameters.Get());
		g_pCore->GetGame()->GetLocalPlayer()->SetPosition(vecPositon);
		return true;
	}
	else if(strCommand == "zaxis")
	{
		CVector3 vecPositon;
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecPositon);

		vecPositon.fZ += atoi(strParameters.Get());
		g_pCore->GetGame()->GetLocalPlayer()->SetPosition(vecPositon);
		return true;
	}
	else if(strCommand == "port")
	{
		CVector3 vecPositon;
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecPositon);
		vecPositon.fX = 900;
		vecPositon.fY = -71;
		vecPositon.fZ += 20;
		g_pCore->GetGame()->GetLocalPlayer()->SetPosition(vecPositon);
		return true;
	}
	else if(strCommand == "time")
	{
		g_pCore->GetTimeManagementInstance()->SetTime(atoi(strParameters.Get()), 0);
		CGameFunction::SetTimeOfDay(atoi(strParameters.Get()), 0);
		return true;
	}
	else if(strCommand == "setmodel")
	{
		g_pCore->GetGame()->GetLocalPlayer()->SetModel(atoi(strParameters.Get()));
		return true;
	}
	else if(strCommand == "setclothes")
	{
		CString strParameter = CString("%s", strParameters.Get());
		g_pCore->GetChat()->Print(strParameter.Get());

		// Get the end of the command
		size_t sCommandEnd = strParameter.Find(" "); 

		// If we don't have a valid end use the end of the string
		if (sCommandEnd == std::string::npos)
		{
			sCommandEnd = strParameter.GetLength();
		}

		// Get the command name
		std::string strCommand2 = strParameter.Substring(0, (sCommandEnd));

		// Get the command parameters
		std::string strParams;

		// Do we have any parameters?
		if(sCommandEnd < strParameter.GetLength())
		{
			strParams = strParameter.Substring((sCommandEnd + 1), strParameter.GetLength());
		}

		g_pCore->GetChat()->Print(CString("Setting clothes part %d to %d", atoi(strCommand2.c_str()), atoi(strParams.c_str())));
		g_pCore->GetGame()->GetLocalPlayer()->SetPedClothes(atoi(strCommand2.c_str()), atoi(strParams.c_str()));

		return true;
	}
	else if(strCommand == "bahama")
	{
		g_pCore->GetGame()->GetLocalPlayer()->CPlayerEntity::SetPosition(CVector3(-15.9453f, -13.5865f, -11.7456f));
		return true;
	}
	else if(strCommand == "spawnvehs")
	{
		CVector3 vecPos;
		PTR_LOCALPLAYER->GetPosition(vecPos);

		for (int i = 0; i < 179; i++)
		{
			vecPos.fX += 5;
			CVehicleEntity * pVehicle = new CVehicleEntity(i, vecPos, 0.0f, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF);
			if(pVehicle)
			{
				// Add our vehicle
				g_pCore->GetGame()->GetVehicleManager()->Add(pVehicle);
				pVehicle->SetId(g_pCore->GetGame()->GetVehicleManager()->FindFreeSlot());
				pVehicle->Create();
				pVehicle->SetPosition(vecPos, true);
			}
		}
		return true;
	}
	else if(strCommand == "getvehpos")
	{
		int iVehicle = atoi(strParameters.Get());

		if(g_pCore->GetGame()->GetVehicleManager()->DoesExists(iVehicle)) {
			CVector3 vecPosition;
			g_pCore->GetGame()->GetVehicleManager()->GetAt(iVehicle)->GetPosition(vecPosition);
			PTR_CHAT->Print(CString("Position of vehicle %d: %f, %f,%f", iVehicle, vecPosition.fX, vecPosition.fY, vecPosition.fZ));
		}
		return true;
	}
	else if(strCommand == "ivhelp")
	{
		PTR_CHAT->Print("List of Default IV:N Commands...");
		PTR_CHAT->Print("** /cv /respawn /debug /weapon /cp /spawn /engine /save /giveweapon /xaxis /time");
		PTR_CHAT->Print("** /setmodel /testweapon /ready /parachute /bahama /spawnvehicles /getvehpos");
		return true;
	}
	else if(strCommand == "createtrain")
	{
		g_pCore->GetGame()->GetIVManagement()->CreateTrain(g_pCore->GetGame()->GetLocalPlayer()->GetPosition(), 3, 20.0f, 0);
		return true;
	}
	else if(strCommand == "blip")
	{
		unsigned int uiBlip;
		CIVScript_NativeInvoke::Invoke<unsigned int>(CIVScript::NATIVE_ADD_BLIP_FOR_COORD, 0, 0, 0,&uiBlip); 
		CIVScript_NativeInvoke::Invoke<unsigned int>(CIVScript::NATIVE_CHANGE_BLIP_SPRITE, 10);
		return true;
	}
	else if(strCommand == "sethealth")
	{
		g_pCore->GetGame()->GetLocalPlayer()->SetHealth(atoi(strParameters.Get()));
		return true;
	}
	else if (strCommand == "setarmour")
	{
		g_pCore->GetGame()->GetLocalPlayer()->SetArmour(atoi(strParameters.Get()));
		return true;
	}
#endif
#ifdef SYNC_TEST
	else if (strCommand == "syncTest")
	{
		// Add the player
		CPlayerEntity * pPlayer = new CPlayerEntity;
		pPlayer->SetModel(0); // Set temporary to nico lol
		pPlayer->Create();
		pPlayer->SetNick("Keks");
		pPlayer->SetId(1);
		pPlayer->SetColor(0xFFFFFFFF);
		g_pCore->GetGame()->GetPlayerManager()->Add(1, pPlayer);

		CVector3 vecPosition;
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecPosition);
		vecPosition.fX += 2.0f;
		pPlayer->SetPosition(vecPosition);
	}
	else if (strCommand == "getP")
	{
		CVector3 vecPosition;
		g_pCore->GetGame()->GetLocalPlayer()->GetPosition(vecPosition);
		vecPosition.fX += 2.0f;
		g_pCore->GetGame()->GetPlayerManager()->GetAt(1)->SetPosition(vecPosition);
	}
	else if (strCommand = "vehicleSync")
	{
		g_pCore->GetGame()->GetLocalPlayer()->PutInVehicle(g_pCore->GetGame()->GetVehicleManager()->GetAt(0), 0);
		CVector3 vecPosition;
		g_pCore->GetGame()->GetPlayerManager()->GetAt(1)->GetPosition(vecPosition);
		CVehicleEntity * pVehicle = new CVehicleEntity(90, vecPosition, 90, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
		if (pVehicle)
		{
			//	// Add our vehicle
			g_pCore->GetGame()->GetVehicleManager()->Add(1, pVehicle);
			pVehicle->SetId(1);
			pVehicle->Create();
			pVehicle->SetPosition(vecPosition, true);
		}
		//g_pCore->GetGame()->GetPlayerManager()->GetAt(1)->PutInVehicle(g_pCore->GetGame()->GetVehicleManager()->GetAt(0), 0);

		//CVehicleEntity * pVehicle = new CVehicleEntity(90, vecPosition, 90, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
		//if (pVehicle)
		//{
		//	//	// Add our vehicle
		//	g_pCore->GetGame()->GetVehicleManager()->Add(vehicleId, pVehicle);
		//	pVehicle->SetId(vehicleId);
		//	pVehicle->Create();
		//	pVehicle->SetPosition(vecPosition, true);
		//}
	}
#endif
	return false;
}
Beispiel #3
0
void InitialData(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	// Get the playerid
	EntityId playerId = (EntityId) pPacket->guid.systemIndex;

	// Read the player version
	DWORD dwVersion;
	pBitStream->Read(dwVersion);

	// Read the player name
	RakNet::RakString _strName;
	pBitStream->Read(_strName);
	CString strName(_strName.C_String());



	// Read the player serial
	RakNet::RakString _strSerial;
	pBitStream->Read(_strSerial);
	CString strSerial(_strSerial.C_String());

	// Is the network version invalid?
	if (dwVersion != (DWORD)/*NETWORK_VERSION*/0x0)
	{
		// TODO
	}

	

	// Is the nickname already in use?
	// TODO: check is nick in use

	// Is the player banned?
	// TODO: check is banned

	// Add the player to the manager
	// TODO: add to player manager
	CPlayerEntity * pPlayer = new CPlayerEntity();

	pPlayer->SetName(strName);

	// Do we need the id; maybe internal for easier sync but definetly not public to the scripting engine
	pPlayer->SetId(CServer::GetInstance()->GetPlayerManager()->Add(pPlayer));
	playerId = pPlayer->GetId();

	CLogFile::Printf("[join] %s (%i) has connected to the server. (%s)", strName.Get(), playerId, strSerial.Get());

	CScriptArguments args;
	CScriptPlayer * pScriptPlayer = new CScriptPlayer();
	pScriptPlayer->SetEntity(pPlayer);
	pPlayer->SetScriptPlayer(pScriptPlayer);
	args.push(pScriptPlayer);
	CEvents::GetInstance()->Call("playerJoin", &args, CEventHandler::eEventType::GLOBAL_EVENT, 0);

	// Construct a new bitstream
	RakNet::BitStream bitStream;

	// Write the player id
	bitStream.WriteCompressed(playerId);

	// Write the player colour
	bitStream.Write(0xFFFFFF/*CServer::GetInstance()->GetPlayerManager()->GetAt(playerId)->GetColor()*/);

	// Write the server name
	bitStream.Write(RakNet::RakString("IV:Network DEV Server"));

	// Write the max player count
	bitStream.Write(CVAR_GET_INTEGER("maxplayers"));

	// Write the port
	bitStream.Write(CVAR_GET_INTEGER("port"));

	// Send it back to the player
	CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_INITIAL_DATA), &bitStream, HIGH_PRIORITY, RELIABLE, playerId, false);

	CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_START_GAME), &bitStream, HIGH_PRIORITY, RELIABLE, playerId, false);

	for (EntityId i = 0; i < CServer::GetInstance()->GetPlayerManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetPlayerManager()->DoesExists(i) && i != playerId)
		{
			bitStream.Reset();
			bitStream.WriteCompressed(i);
			bitStream.Write(CServer::GetInstance()->GetPlayerManager()->GetAt(i)->GetName().Get());
			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_NEW_PLAYER), &bitStream, HIGH_PRIORITY, RELIABLE, playerId, false);

		}
	}

	for (EntityId i = 0; i < CServer::GetInstance()->GetPlayerManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetPlayerManager()->DoesExists(i) && i != playerId)
		{
			bitStream.Reset();
			bitStream.WriteCompressed(playerId);
			bitStream.Write(strName.Get());
			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_NEW_PLAYER), &bitStream, HIGH_PRIORITY, RELIABLE, i, false);
		}
	}


	for (EntityId i = 0; i < CServer::GetInstance()->GetVehicleManager()->GetMax(); ++i)
	{
		if (CServer::GetInstance()->GetVehicleManager()->DoesExists(i))
		{
			bitStream.Reset();
			CVehicleEntity * pVehicle = CServer::GetInstance()->GetVehicleManager()->GetAt(i);
			bitStream.Write(pVehicle->GetId());
			bitStream.Write(/*pVehicle->GetModelId()*/90);

			CVector3 vecPosition;
			pVehicle->GetPosition(vecPosition);
			bitStream.Write(vecPosition);
			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_CREATE_VEHICLE), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, -1, true);
		}
	}

	bitStream.Reset();

	bitStream.WriteCompressed(playerId);
	bitStream.Write(7);
	bitStream.Write(3);
	bitStream.Write(3);
	bitStream.Write(1);
}