GetClosestCar
#1

When I try to refill my car it keeps saying you are not near a vehicle, Has someone a better GetClosestCar solution?

pawn Код:
command(refill, playerid, params[])
{
    #pragma unused params
    if(IsAtGasStation(playerid))
    {
        new PCar = GetClosestVehicle(playerid);
        new engine,lights,alarm,doors,bonnet,boot,objective;
        GetVehicleParamsEx(PCar,engine,lights,alarm,doors,bonnet,boot,objective);
        if(IsPlayerConnected(playerid))
        {
            if(!IsPlayerInAnyVehicle(playerid))
            {
                if(PCar == 0)
                {
                    if(engine == 0)
                    {
                        if(Vehicles[PCar][Fuel] != 100)
                        {
                            TogglePlayerControllable(playerid,0);
                            ShowMenuForPlayer(Menu:Gas, playerid);
                        }
                        else
                        {
                            SCM(playerid, ADMINBLUE, "Your car is already full!");
                        }
                    }
                    else
                    {
                        SCM(playerid, YELLOW,"Your vehicle engine is still on!");
                    }
                }
                else
                {
                    SCM(playerid, YELLOW,"You are not near a vehicle!");
                }
            }
            else
            {
                SCM(playerid, YELLOW,"You are still in a vehicle!");
            }
        }
    }
    else
    {
       SCM(playerid,0xFFFF00AA,".:: [ERROR]: You are not at a Gas Station! ::.");
    }
    return 1;
}

//found on SAMP forums
stock GetClosestVehicle(playerid)
{
    new Float:DefaultDistance = 99999999999.9;
    new Float:Distance,VehID;
    new Float:X,Float:Y,Float:Z;
    for(new d=1; d < MAX_VEHICLES; d++)
    {
        if(GetVehicleModel(d))
        {
            GetPlayerPos(playerid,X,Y,Z);
            Distance = GetVehicleDistanceFromPoint(d,X,Y,Z);
            if(Distance < DefaultDistance)
            {
                VehID = d;
                DefaultDistance = Distance;
            }
        }
    }
    return VehID;
}
Reply
#2

Try
Код:
stock GetClosestVehicle(playerid)
{
    new vehicleid = INVALID_VEHICLE_ID;
    new Float:distance = 99999.0;
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(playerid, X, Y, Z);
    for(i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetVehicleDistanceFromPoint(i, X, Y, Z) < distance && distance < 8.0) //you can change this value
        {
            vehicleid = i;
            distance = GetVehicleDistanceFromPoint(i, X, Y, Z);
        }
    }
    return vehicleid;
}
Reply
#3

Nope keeps saying You are not near a vehicle
Reply
#4

pawn Код:
new PCar = GetClosestVehicle(playerid);
if(PCar == 0)
As you can probably guess, the GetClosetVehicle stock will return the vehicle id, and you check if the vehicle id is 0...?

If you use now TheStreetsRP's stock, I think you should change the if statement to:
pawn Код:
if(PCar != INVALID_VEHICLE_ID)
Reply
#5

Quote:
Originally Posted by zxc1
Посмотреть сообщение
pawn Код:
new PCar = GetClosestVehicle(playerid);
if(PCar == 0)
As you can probably guess, the GetClosetVehicle stock will return the vehicle id, and you check if the vehicle id is 0...?

If you use now TheStreetsRP's stock, I think you should change the if statement to:
pawn Код:
if(PCar != INVALID_VEHICLE_ID)
Let Me test that.
Reply
#6

It's untested, but it should work fine...

pawn Код:
stock GetClosestVehicle(playerid)
{
    new
        Float:fPos[3],
        Float:distance = 30,
        Float:curdistance,
        currentVehicle;
   
    for(new v = 0; v < MAX_VEHICLES; v++)
    {
        GetVehiclePos(v, fPos[0], fPos[1], fPos[2]);       
        curdistance = GetPlayerDistanceFromPoint(playerid, fPos[0], fPos[1], fPos[2]);
       
        if(curdistance < distance)
        {
            currentVehicle = v;
            distance = curdistance;
        }
    }
    return currentVehicle;
}
Reply
#7

Quote:
Originally Posted by TheStreetsRP
Посмотреть сообщение
Try
Код:
stock GetClosestVehicle(playerid)
{
    new vehicleid = INVALID_VEHICLE_ID;
    new Float:distance = 99999.0;
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(playerid, X, Y, Z);
    for(i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetVehicleDistanceFromPoint(i, X, Y, Z) < distance && distance < 8.0) //you can change this value
        {
            vehicleid = i;
            distance = GetVehicleDistanceFromPoint(i, X, Y, Z);
        }
    }
    return vehicleid;
}
you set distance to 99999, how is that going to be under 8?
Reply
#8

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
It's untested, but it should work fine...

pawn Код:
stock GetClosestVehicle(playerid)
{
    new
        Float:fPos[3],
        Float:distance = 30,
        Float:curdistance,
        currentVehicle;
   
    for(new v = 0; v < MAX_VEHICLES; v++)
    {
        GetVehiclePos(v, fPos[0], fPos[1], fPos[2]);       
        curdistance = GetPlayerDistanceFromPoint(playerid, fPos[0], fPos[1], fPos[2]);
       
        if(curdistance < distance)
        {
            currentVehicle = v;
            distance = curdistance;
        }
    }
    return currentVehicle;
}
Worked, tyvm
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)