GetPlayerNearestVehicle - While you in vehicle. prob.
#1

Hi, so here's the code :
pawn Код:
COMMAND:loadcrates(playerid, params[])
{
    new hCar = GetPlayerVehicleID(playerid);
    if(!comps[hCar][wguns]) return SendClientError(playerid, "You don't have any crates on your forklift.");
    new Pcar = GetPlayerNearestVehicle(playerid);
    if(GetDistanceFromPlayerToVehicle(playerid, Pcar) > 8.0)
    comps[Pcar][wguns] += 10;
    comps[hCar][wguns] += -10;
    DestroyObject(GetPVarInt(playerid, "harrycrate"));
    Action(playerid, "places the crate in the back of the van.");
    format(iStr, sizeof(iStr), "has loaded a crate in a back of a van.", PlayerName(playerid));
    iEcho(iStr);
    return 1;
}
You type command "/loadcrates" when you allready in a car
pawn Код:
"hCar"
.
It should move 10 "wguns"
pawn Код:
comps[Pcar][wguns] += 10;
comps[hCar][wguns] += -10;
It does remove the "wguns" from the "hCar" but it wont fill the "Pcar" with "wguns"

I'm 99% precenet sure the probem is
pawn Код:
new Pcar = GetPlayerNearestVehicle(playerid);
    if(GetDistanceFromPlayerToVehicle(playerid, Pcar) > 8.0)
Because I'm in a vehicle already and the "NearestVehicleToPlayer" is the car im driving at.

Any ideas? Thanks !!!
Reply
#2

So from what I've gathered, you want to exclude the vehicle that the player is in? Edit the GetPlayerNearestVehicle function so that it skips the alliteration of the players vehicle ID if they are in a vehicle? If you don't know how, post the GetPlayerNearestVehicle function.
Reply
#3

Found.
pawn Код:
stock GetPlayerNearestVehicle(playerid)
{
    new closest = -1;
    if(IsPlayerInAnyVehicle(playerid)) return GetPlayerVehicleID(playerid);
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetDistanceFromPlayerToVehicle(playerid, i) < GetDistanceFromPlayerToVehicle(playerid, closest) && closest != -1 && IsVehicleConnected(i) && GetVehicleVirtualWorld(i) == GetPlayerVirtualWorld(playerid))
        {
            closest = i;
        }
        if(closest == -1 && IsVehicleConnected(i)) closest = i;
    }
    return closest;
}
Reply
#4

pawn Код:
stock GetPlayerNearestVehicle(playerid)
{
    new closest = -1, pvehid = INVALID_VEHICLE_ID;
    if(IsPlayerInAnyVehicle(playerid))
        pvehid = GetPlayerVehicleID(playerid);

    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetDistanceFromPlayerToVehicle(playerid, i) < GetDistanceFromPlayerToVehicle(playerid, closest) && closest != -1 && IsVehicleConnected(i) && GetVehicleVirtualWorld(i) == GetPlayerVirtualWorld(playerid) && i != pvehid)
        {
            closest = i;
        }
        if(closest == -1 && IsVehicleConnected(i)) closest = i;
    }
    return closest;
}
NOTE: Function now does not return player vehicle id at all.
Reply
#5

It wont mess the other script usage of "GetPlayerNearestVehicle" ?

For example : /friskcar - Which need to check the car ID. I hope you understand my point.

I was thinking of creating new stock "GetVehicleNearestVehicle".

pawn Код:
stock GetVehicleNearestVehicle(playerid)
{
    new closest = -1;
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetDistanceFromVehicleToVehicle(playerid, i) < GetDistanceFromVehicleToVehicle(playerid, closest) && closest != -1 && IsVehicleConnected(i) && GetVehicleVirtualWorld(i) == GetPlayerVirtualWorld(playerid))
        {
            closest = i;
        }
        if(closest == -1 && IsVehicleConnected(i)) closest = i;
    }
    return closest;
}
stock GetDistanceFromVehicleToVehicle(playerid, closestcarid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    GetVehiclePos(closestcarid,x2,y2,z2);
    tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
    return floatround(tmpdis);
}
Im giving it a test now.

// updating

Wont work it seems like the "wguns" wont go to the nearst vehicle.
Reply
#6

Quote:
Originally Posted by AdamCooper
Посмотреть сообщение
It wont mess the other script usage of "GetPlayerNearestVehicle" ?

For example : /friskcar - Which need to check the car ID. I hope you understand my point.

I was thinking of creating new stock "GetVehicleNearestVehicle".

pawn Код:
stock GetVehicleNearestVehicle(playerid)
{
    new closest = -1;
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetDistanceFromVehicleToVehicle(playerid, i) < GetDistanceFromVehicleToVehicle(playerid, closest) && closest != -1 && IsVehicleConnected(i) && GetVehicleVirtualWorld(i) == GetPlayerVirtualWorld(playerid))
        {
            closest = i;
        }
        if(closest == -1 && IsVehicleConnected(i)) closest = i;
    }
    return closest;
}
stock GetDistanceFromVehicleToVehicle(playerid, closestcarid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    GetVehiclePos(closestcarid,x2,y2,z2);
    tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
    return floatround(tmpdis);
}
Im giving it a test now.

// updating

Wont work it seems like the "wguns" wont go to the nearst vehicle.
That is a bad way to do it how about like this.

pawn Код:
stock GetVehicleNearestVehicle(playerid)
{
    new Float:closestdist = 99999.0;
    new Float:clostestvid;
    new Float:checkdist;
    new Float:x,Float:y,Float:z;
   
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        GetVehiclePos(i, x, y, z);
        checkdist = GetPlayerDistanceFromPoint(playerid, x, y, z);
        if(checkdist < closestdist && IsVehicleConnected(i) && GetVehicleVirtualWorld(i) == GetPlayerVirtualWorld(playerid))
        {
            clostestvid = i;
            checkdist = checkdist;
        }
    }
    return clostestvid;
}
Reply
#7

Ah much nicer. I will give it a try. Little question though :

How I gonna check the distance its obvious checkdist but i mean in the CMD, see below :

pawn Код:
COMMAND:loadcrates(playerid, params[])
{
    new hCar = GetPlayerVehicleID(playerid);
    if(!comps[hCar][wguns]) return SendClientError(playerid, "You don't have any crates on your forklift.");
    new Pcar = GetPlayerNearestVehicle(playerid);
    [B]if(GetDistanceFromPlayerToVehicle(playerid, Pcar) > 8.0)[/B]
-- this line



Updating :

warning 226: a variable is assigned to itself (symbol "checkdist") --
pawn Код:
checkdist = checkdist;
warning 213: tag mismatch --
pawn Код:
return clostestvid;
Tested in game. Not working too. It might be something else. The "wguns" needs to go to the ClosestVehicle.
Since tonight I'm on it.. Will keep trying
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)