// getVehicleLocked(vehicleid)
int CVehicleModuleNatives::GetLocked(EntityId vehicleId)
{
    CVehicle * pVehicle = g_pVehicleManager->GetAt(vehicleId);

    if(pVehicle)
    {
        return pVehicle->GetLocked();
    }

    return -1;
}
void CServerRPCHandler::VehicleEnterExit(CBitStream * pBitStream, CPlayerSocket * pSenderSocket)
{
	// Ensure we have a valid bit stream
	if(!pBitStream)
		return;

	EntityId playerId;
	if(!pBitStream->ReadCompressed(playerId))
		return;

	// Check the host
	if(playerId != pSenderSocket->GetPlayerId())
		return;

	// Get the player pointer
	CPlayer * pPlayer = g_pPlayerManager->GetAt(playerId);

	// Is the player pointer valid?
	if(pPlayer)
	{
		// Read the vehicle entry/exit type
		BYTE byteVehicleEntryExitType;

		if(!pBitStream->Read(byteVehicleEntryExitType))
			return;

		// Read the vehicle id
		EntityId vehicleId;

		if(!pBitStream->ReadCompressed(vehicleId))
			return;

		// Get the vehicle
		CVehicle * pVehicle = g_pVehicleManager->GetAt(vehicleId);

		// Does the vehicle not exist?
		if(!pVehicle)
			return;

		// Is this an entry request?
		if(byteVehicleEntryExitType == VEHICLE_ENTRY_REQUEST)
		{
			// Read the seat id
			BYTE byteSeatId;

			if(!pBitStream->Read(byteSeatId))
				return;

			bool bReply = true;

			// Is the vehicle fully locked?
			if(pVehicle->GetLocked() == 1)
				bReply = false;
			else
			{
				// Get the reply
				CSquirrelArguments arguments;
				arguments.push(playerId);
				arguments.push(vehicleId);
				arguments.push(byteSeatId);
				bReply = (g_pEvents->Call("vehicleEntryRequest", &arguments).GetInteger() == 1);
			}

			// Reply to the vehicle entry request
			CBitStream bitStream;
			bitStream.WriteCompressed(playerId);
			bitStream.WriteBit(bReply);

			//-Was the reply ok?
			if(bReply)
			{
				bitStream.Write((BYTE)VEHICLE_ENTRY_RETURN);
				bitStream.Write(vehicleId);
				bitStream.Write(byteSeatId);

				// Set the player state
				pPlayer->SetState(STATE_TYPE_ENTERVEHICLE);
			}

			g_pNetworkManager->RPC(RPC_VehicleEnterExit, &bitStream, PRIORITY_HIGH, RELIABILITY_RELIABLE, INVALID_ENTITY_ID, true);
		}
		// Is this an entry cancellation?
		if(byteVehicleEntryExitType == VEHICLE_ENTRY_CANCELLED)
		{
			// Read the seat id
			BYTE byteSeatId;

			if(!pBitStream->Read(byteSeatId))
				return;

			// Call the event
			CSquirrelArguments arguments;
			arguments.push(playerId);
			arguments.push(vehicleId);
			arguments.push(byteSeatId);
			g_pEvents->Call("vehicleEntryCancelled", &arguments);

			CBitStream bitStream;
			bitStream.WriteCompressed(playerId);
			bitStream.WriteBit(true);
			bitStream.Write((BYTE)VEHICLE_ENTRY_CANCELLED);
			bitStream.Write(vehicleId);
			g_pNetworkManager->RPC(RPC_VehicleEnterExit, &bitStream, PRIORITY_HIGH, RELIABILITY_RELIABLE, pSenderSocket->playerId, true);

			// Set the player state
			pPlayer->SetState(STATE_TYPE_ONFOOT);
		}
		// Is this an entry completion?
		else if(byteVehicleEntryExitType == VEHICLE_ENTRY_COMPLETE)
		{
			// Read the seat id
			BYTE byteSeatId;

			if(!pBitStream->Read(byteSeatId))
				return;

			// Call the event
			CSquirrelArguments arguments;
			arguments.push(playerId);
			arguments.push(vehicleId);
			arguments.push(byteSeatId);
			g_pEvents->Call("vehicleEntryComplete", &arguments);

			// Set the player vehicle and seat id
			pPlayer->SetVehicle(pVehicle);
			pPlayer->SetVehicleSeatId(byteSeatId);

			// Set the vehicle occupant
			pVehicle->SetOccupant(byteSeatId, pPlayer);

			// Set the player state
			pPlayer->SetState(STATE_TYPE_INVEHICLE);
		}
		// Is this an exit request?
		else if(byteVehicleEntryExitType == VEHICLE_EXIT_REQUEST)
		{
			// Get the reply
			CSquirrelArguments arguments;
			arguments.push(playerId);
			arguments.push(vehicleId);
			arguments.push(pPlayer->GetVehicleSeatId());
			bool bReply = (g_pEvents->Call("vehicleExitRequest", &arguments).GetInteger() == 1);

			// Reply to the vehicle exit request
			CBitStream bitStream;
			bitStream.WriteCompressed(playerId);
			bitStream.WriteBit(bReply);

			// Was the reply ok?
			if(bReply)
			{
				bitStream.Write((BYTE)VEHICLE_EXIT_RETURN);
				bitStream.Write(vehicleId);

				// Set the player state
				pPlayer->SetState(STATE_TYPE_EXITVEHICLE);
			}

			g_pNetworkManager->RPC(RPC_VehicleEnterExit, &bitStream, PRIORITY_HIGH, RELIABILITY_RELIABLE, INVALID_ENTITY_ID, true);
		}
		// Is this an exit completion?
		else if(byteVehicleEntryExitType == VEHICLE_EXIT_COMPLETE)
		{
			// Call the event
			CSquirrelArguments arguments;
			arguments.push(playerId);
			arguments.push(vehicleId);
			arguments.push(pPlayer->GetVehicleSeatId());
			g_pEvents->Call("vehicleExitComplete", &arguments);

			// Reset the vehicle occupant
			pVehicle->SetOccupant(pPlayer->GetVehicleSeatId(), NULL);

			// Reset the player vehicle and seat id
			pPlayer->SetVehicle(NULL);
			pPlayer->SetVehicleSeatId(0);

			// Set the player state
			pPlayer->SetState(STATE_TYPE_ONFOOT);
		}
		// Is this a forceful exit?
		else if(byteVehicleEntryExitType == VEHICLE_EXIT_FORCEFUL)
		{
			// Call the event
			CSquirrelArguments arguments;
			arguments.push(playerId);
			arguments.push(vehicleId);
			arguments.push(pPlayer->GetVehicleSeatId());
			g_pEvents->Call("vehicleForcefulExit", &arguments);

			// Reset the vehicle occupant
			pVehicle->SetOccupant(pPlayer->GetVehicleSeatId(), NULL);

			// Reset the player vehicle and seat id
			pPlayer->SetVehicle(NULL);
			pPlayer->SetVehicleSeatId(0);

			// Set the player state
			pPlayer->SetState(STATE_TYPE_ONFOOT);
		}
	}
}