Exemple #1
0
void CheckpointExit(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	// Get the player id
	EntityId playerId = (EntityId)pPacket->guid.systemIndex;

	// Read the checkpoint id
	EntityId checkpointId;
	pBitStream->Read(checkpointId);

	// Get the player instance
	CPlayerEntity * pPlayer = CServer::GetInstance()->GetPlayerManager()->GetAt(playerId);

	// Get the checkpoint instance
	CCheckpointEntity * pCheckpoint = CServer::GetInstance()->GetCheckpointManager()->GetAt(checkpointId);

	// Is the player instance valid?
	if (pPlayer)
	{
		CScriptArguments args;
		args.push(pPlayer->GetScriptPlayer());
		args.push(pCheckpoint->GetScriptCheckpoint());
		CEvents::GetInstance()->Call("playerExitCheckpoint", &args, CEventHandler::eEventType::NATIVE_EVENT, 0);
	}

	CLogFile::Printf("RPC_EXIT_CHECKPOINT");
}
Exemple #2
0
void returnValue(CScriptVM * pVM, CVector3 v)
{
	CScriptArguments vec;
	vec.push(v.fX);
	vec.push(v.fY);
	vec.push(v.fZ);
	pVM->PushArray(vec);
}
Exemple #3
0
CScriptArguments CEvents::Call(CString strName, CScriptArguments* pArguments, CEventHandler::eEventType EventType, IScriptVM * pVM)
{
	CScriptArguments returnArguments;
	auto itEvent = m_Events.find(strName);
	if(itEvent != m_Events.end())
	{
		CScriptArgument ret;
		for(auto pEvent : itEvent->second)
		{
			if(EventType == CEventHandler::eEventType::GLOBAL_EVENT
				&& pEvent->GetType() == CEventHandler::GLOBAL_EVENT)
			{
				pEvent->Call(pArguments, &ret);
				returnArguments.push(ret);
			}
			else if(EventType == CEventHandler::eEventType::RESOURCE_EVENT
				&& pEvent->GetType() == CEventHandler::RESOURCE_EVENT
				&& pEvent->GetVM() == pVM)
			{
				pEvent->Call(pArguments, &ret);
				returnArguments.push(ret);
			}
			else if (EventType == CEventHandler::eEventType::NATIVE_EVENT
				&& pEvent->GetType() == CEventHandler::eEventType::RESOURCE_EVENT
				&& pVM == nullptr)
			{
				//Pruebas
				if (strName.Compare(CString("playerCommand")) == 0){
					CLogFile::Printf("Resource name: %s", pEvent->GetVM()->GetResource()->GetName().Get());
				}
				else
					CLogFile::Printf("playerCommand");
				pEvent->Call(pArguments, &ret);
				returnArguments.push(ret);
			}
			else if (EventType == CEventHandler::eEventType::NATIVE_EVENT
				&& pEvent->GetType() == CEventHandler::eEventType::RESOURCE_EVENT
				&& pEvent->GetVM() == pVM)
			{
				pEvent->Call(pArguments, &ret);
				returnArguments.push(ret);
			}
			else if (EventType == CEventHandler::REMOTE_EVENT
				&& pEvent->GetType() == CEventHandler::REMOTE_EVENT)
			{
				pEvent->Call(pArguments, &ret);
				returnArguments.push(ret);
			}
		}
	}
	return returnArguments;
}
Exemple #4
0
void VehicleExit(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	// Get the player id
	EntityId playerId = (EntityId) pPacket->guid.systemIndex;

	// Read the vehicle id
	EntityId vehicleId;
	pBitStream->Read(vehicleId);

	// Read the seat
	byte byteSeat;
	pBitStream->Read(byteSeat);

	// Get the player instance
	CPlayerEntity * pPlayer = CServer::GetInstance()->GetPlayerManager()->GetAt(playerId);

	// Is the player instance valid?
	if (pPlayer)
	{
		// Handle this with the player
		RakNet::BitStream bitStream;
		bitStream.Write(playerId);
		bitStream.Write(vehicleId);
		bitStream.Write(byteSeat);
		CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_EXIT_VEHICLE), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, -1, true);
		CScriptArguments args;
		args.push(pPlayer->GetScriptPlayer());
		CEvents::GetInstance()->Call("playerExitVehicle", &args, CEventHandler::eEventType::NATIVE_EVENT, 0);
	}

	CLogFile::Printf("RPC_EXIT_VEHICLE - Player: %d, Vehicle: %d, Seat: %d", playerId, vehicleId, byteSeat);
}
Exemple #5
0
void PlayerRequestSpawn(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	CLogFile::Printf("%s", __FUNCTION__);

	// Get the player id
	EntityId playerId = (EntityId) pPacket->guid.systemIndex;

	// Get a pointer to the player
	CPlayerEntity * pPlayer = CServer::GetInstance()->GetPlayerManager()->GetAt(playerId);

	// Is the player pointer valid?
	if (pPlayer)
	{
		if (CEvents::GetInstance()->IsEventRegistered("playerRequestSpawn"))
		{
			CScriptArguments args;
			args.push(pPlayer->GetScriptPlayer());
			CEvents::GetInstance()->Call("playerRequestSpawn", &args, CEventHandler::eEventType::NATIVE_EVENT, 0);
		}
		else
		{
			RakNet::BitStream bitStream;
			bitStream.Write(CVector3(DEFAULT_SPAWN_POSITION)); //spawnPos
			bitStream.Write(0.0f); //fHeading
			CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_PLAYER_SPAWN), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, playerId, false);
		}
		CLogFile::Printf("[spawn] %s has requestred a spawn.", pPlayer->GetName().Get());
	}
}
bool CScriptingManager::Unload(CScript * pScript)
{
	if(pScript)
	{
		CEvents * pEvents = CEvents::GetInstance();
		pEvents->Call("scriptExit", pScript);

		CScriptArguments pArguments;
		pArguments.push(pScript->GetMetaFile().ToMetaAndFileString());
		pEvents->Call("scriptUnload", &pArguments);
		pEvents->RemoveScript(pScript->GetVM());

#ifdef _SERVER
		if(g_pModuleManager)
			g_pModuleManager->ScriptUnload(pScript->GetVM());
#endif

		pScript->Unload();
		m_scripts.remove(pScript);
		delete pScript;
		return true;
	}

	return false;
}
Exemple #7
0
void PlayerChat(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	// Get the playerid
	EntityId playerId = (EntityId) pPacket->guid.systemIndex;

	// Read if this is a command
	bool bIsCommand = pBitStream->ReadBit();

	// Read the input
	RakNet::RakString strInput;
	pBitStream->Read(strInput);

	// Is the player active?
	if (CServer::GetInstance()->GetPlayerManager()->DoesExists(playerId))
	{
		// Get a pointer to the player
		CPlayerEntity * pPlayer = CServer::GetInstance()->GetPlayerManager()->GetAt(playerId);

		// Is the pointer valid?
		if (pPlayer)
		{
			// Is this not a command?
			if (!bIsCommand)
			{
				CLogFile::Printf("[chat] %s: %s", pPlayer->GetName().Get(), strInput.C_String());

				// Send the RPC back to other players
				RakNet::BitStream bitStream;
				bitStream.Write(playerId);
				bitStream.Write(strInput);
				CServer::GetInstance()->GetNetworkModule()->Call(GET_RPC_CODEX(RPC_PLAYER_CHAT), &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, -1, true);
			}
			else
			{
				CLogFile::Printf("[command] %s: %s", pPlayer->GetName().Get(), strInput.C_String());

				CScriptArguments args;
				args.push(strInput);
				args.push(pPlayer->GetScriptPlayer());
				CEvents::GetInstance()->Call("playerCommand", &args, CEventHandler::eEventType::NATIVE_EVENT, nullptr);
			}
		}
	}
}
Exemple #8
0
void CVehicle::StoreInVehicleSync(InVehicleSyncData * syncPacket)
{
	m_vecPosition = syncPacket->vecPos;
	m_vecRotation = syncPacket->vecRotation;
	if(m_uiHealth != syncPacket->uiHealth || m_fPetrolTankHealth != syncPacket->fPetrolHealth)
	{
		CScriptArguments pArguments;
		pArguments.push(m_vehicleId);
		pArguments.push((int)m_uiHealth);
		pArguments.push((int)syncPacket->uiHealth);
		pArguments.push(m_fPetrolTankHealth);
		pArguments.push(syncPacket->fPetrolHealth);

		g_pEvents->Call("vehicleDamage", &pArguments);
	}
	m_uiHealth = syncPacket->uiHealth;
	m_fPetrolTankHealth = syncPacket->fPetrolHealth;
	memcpy(m_byteColors, syncPacket->byteColors, sizeof(m_byteColors));
	memcpy(&m_vecTurnSpeed, &syncPacket->vecTurnSpeed, sizeof(CVector3));
	memcpy(&m_vecMoveSpeed, &syncPacket->vecMoveSpeed, sizeof(CVector3));
	m_fDirtLevel = syncPacket->fDirtLevel;
	m_bSirenState = syncPacket->bSirenState;
	m_bEngineStatus = syncPacket->bEngineStatus;
	m_bLights = syncPacket->bLights;
	m_fDoor[0] = syncPacket->fDoor[0];
	m_fDoor[1] = syncPacket->fDoor[1];
	m_fDoor[2] = syncPacket->fDoor[2];
	m_fDoor[3] = syncPacket->fDoor[3];
	m_fDoor[4] = syncPacket->fDoor[4];
	m_fDoor[5] = syncPacket->fDoor[5];
	m_bWindow[0] = syncPacket->bWindow[0];
	m_bWindow[1] = syncPacket->bWindow[1];
	m_bWindow[2] = syncPacket->bWindow[2];
	m_bWindow[3] = syncPacket->bWindow[3];
	m_bTaxiLight = syncPacket->bTaxiLights;
	m_bTyre[0] = syncPacket->bTyre[0];
	m_bTyre[1] = syncPacket->bTyre[1];
	m_bTyre[2] = syncPacket->bTyre[2];
	m_bTyre[3] = syncPacket->bTyre[3];
	m_bTyre[4] = syncPacket->bTyre[4];
	m_bTyre[5] = syncPacket->bTyre[5];
	m_bGpsState = syncPacket->bGpsState;
}
bool CPlayerManager::Remove(EntityId playerId, BYTE byteReason)
{
	if(!DoesExist(playerId))
		return false;

	CScriptArguments pArguments;
	pArguments.push(playerId);
	pArguments.push(byteReason);
	g_pEvents->Call("playerDisconnect", &pArguments);

	// Clear player variables:
	g_pGlobalsManager->Remove(playerId);

	// Delete player blip
	g_pBlipManager->DeleteForPlayer(playerId);

	m_pPlayers[playerId]->SetState(STATE_TYPE_DISCONNECT);
	m_pPlayers[playerId]->DeleteForWorld();

	// Mark player as false
	m_bActive[playerId] = false;

	String strReason = "None";

	if(byteReason == 0)
		strReason = "Disconnected";
	else if(byteReason == 1)
		strReason = "Lost Connection";
	else if(byteReason == 3)
		strReason = "Classremove";

	CLogFile::Printf("[Quit] %s (%d) left the server (%s).", m_pPlayers[playerId]->GetName().Get(), playerId, strReason.Get());

	// Only works on windows, if we have linux don't use it!
#ifdef _WIN32
	SAFE_DELETE( m_pPlayers[playerId] );
#else
	SAFE_DELETE( m_pPlayers[playerId] ); 
#endif
	return true;
}
bool CResourceServerScript::Start()
{
	if (m_resource->GetVM()->LoadScript(m_strShortName))
	{
		// Call the scripting event
		CScriptArguments args;
		args.push(m_strShortName.Get());
		CEvents::GetInstance()->Call("scriptLoaded", &args, CEventHandler::eEventType::NATIVE_EVENT, m_resource->GetVM());
		return true;
	}
	return false;
}
CScript * CScriptingManager::Load(CMetaFile & mf)
{
	CScript * pScript = new CScript();

	if(!pScript->Load(mf))
	{
		delete pScript;
		return NULL;
	}

	for(auto i = m_funcs.begin(); i != m_funcs.end(); i++)
		pScript->RegisterFunction((*i)->strName, (*i)->pfnFunction, (*i)->iParameterCount, (*i)->strTemplate);

	for(auto i = m_classes.begin(); i != m_classes.end(); i++)
		pScript->RegisterClass(*i);

	for(auto i = m_constants.begin(); i != m_constants.end(); i++)
		pScript->RegisterConstant((*i)->strName, (*i)->value);

#ifdef _SERVER
	if(g_pModuleManager)
		g_pModuleManager->ScriptLoad(pScript->GetVM());
#endif

	// not a very good way , but leave it as is for now
	for(auto i = m_scripts.begin(); i != m_scripts.end(); i++)
	{
		if((*i)->GetMetaFile().m_Meta == "common" && (*i)->GetMetaFile().m_Type == mf.m_Type)
		{
			pScript->AddExternal((*i)->GetMetaFile());
		}
	}

	if(!pScript->Execute())
	{
		delete pScript;
		CLogFile::Printf("Failed to execute script %s", mf.ToMetaAndFileString().Get());
		return NULL;
	}

	m_scripts.push_back(pScript);

	CEvents * pEvents = CEvents::GetInstance();
	pEvents->Call("scriptInit", pScript);

	CScriptArguments arguments;
	arguments.push(mf.ToMetaAndFileString());
	pEvents->Call("scriptLoad", &arguments);
	return pScript;
}
Exemple #12
0
void CInput::ProcessInput(CString strInput)
{
	// Get the command and parameters
	size_t sSplit = strInput.Find(' ', 0);
	CString strCommand = strInput.Substring(0, sSplit++);
	CString strParameters = strInput.Substring(sSplit, (strInput.GetLength() - sSplit));

	if(strCommand.IsEmpty())
		return;
	else if(strCommand == "quit" || strCommand == "Quit" || strCommand == "exit") {
		CLogFile::Print("[Server] Server is going to shutdown NOW ....");
		g_bClose = true;
		return;

	} else if(strCommand == "help" || strCommand == "?" || strCommand == "--usage") {
		printf("========== Available console commands: ==========\n");
		printf("say <text>\n");
		printf("uptime\n");
		printf("resources\n");
		printf("players\n");
		printf("loadresource <name>\n");
		printf("reloadresource <name>\n");
		printf("unloadresource <name>\n");
		printf("exit\n");
		return;
	}
	else if (strCommand == "setSyncRate") {
		int rate = atoi(strParameters.Get());
		CServer::GetInstance()->SetSyncRate(rate);
	}
	else if (strCommand == "setMaxFPS") {
		int fps = atoi(strParameters.Get());
		CServer::GetInstance()->SetMaximumFPS(fps);
	}
	else if (strCommand == "test") {
		CScriptArguments args;
		CScriptPlayer* player = new CScriptPlayer();
		player->SetEntity(new CPlayerEntity());
		args.push(player);
		CEvents::GetInstance()->Call("Test", &args, CEventHandler::eEventType::GLOBAL_EVENT, 0);
		delete player;
	}
}
Exemple #13
0
void PlayerDeath(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	CLogFile::Printf("%s", __FUNCTION__);

	// Get the player id
	EntityId playerId = (EntityId) pPacket->guid.systemIndex;

	// Read the killer id
	EntityId killerId;
	pBitStream->Read(killerId);

	// Get a pointer to the player
	CPlayerEntity * pPlayer = CServer::GetInstance()->GetPlayerManager()->GetAt(playerId);

	// Is the player pointer valid?
	if (pPlayer)
	{
		// Kill the player
		//pPlayer->KillForWorld();

		// Is the killer a player?
		if (killerId != INVALID_ENTITY_ID)
		{
			// Find the killer
			CPlayerEntity* pKiller = CServer::GetInstance()->GetPlayerManager()->GetAt(killerId);

			// Is the killer valid?
			if (pKiller)
				CLogFile::Printf("[death] %s has been killed by %s!", pPlayer->GetName().Get(), pKiller->GetName().Get());
			else
				CLogFile::Printf("[death] %s has been killed!", pPlayer->GetName().Get());
		}
		else
		{
			CLogFile::Printf("[death] %s has died!", pPlayer->GetName().Get());
		}

		CScriptArguments args;
		args.push(pPlayer->GetScriptPlayer());
		CEvents::GetInstance()->Call("playerDeath", &args, CEventHandler::eEventType::NATIVE_EVENT, 0);
	}
}
Exemple #14
0
void PlayerSpawn(RakNet::BitStream * pBitStream, RakNet::Packet * pPacket)
{
	// Get the player id
	EntityId playerId = (EntityId) pPacket->guid.systemIndex;

	// Get a pointer to the player
	CPlayerEntity * pPlayer = CServer::GetInstance()->GetPlayerManager()->GetAt(playerId);

	// Is the player pointer valid?
	if (pPlayer)
	{
		// Spawn the player
		//pPlayer->SpawnForWorld();

		// Spawn everyone else connected for this player
		//CServer::GetInstance()->GetPlayerManager()->HandlePlayerSpawn(playerId);
		CScriptArguments args;
		args.push(pPlayer->GetScriptPlayer());
		CEvents::GetInstance()->Call("onSpawn", &args, CEventHandler::eEventType::GLOBAL_EVENT, 0);
		CLogFile::Printf("[spawn] %s has spawned.", pPlayer->GetName().Get());
	}
	CLogFile::Printf("%s", __FUNCTION__);
}
Exemple #15
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);

			}
		}
	}
}
Exemple #16
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);
}