Shortest Distance
#1

Ok, i made a new command for my server to make it give the players the nearest mission, but i need it to figure out which value is the smallest distance and give them that mission.

Heres the command so far:

pawn Code:
COMMAND:work(playerid,params[])
{
    if(isworking[playerid] == 1) return SendClientMessage(playerid, COLOR_RED, "You are already working!");
    new vehiclemodel = GetVehicleModel(GetPlayerVehicleID(playerid));
    if (vehiclemodel == 431 || vehiclemodel == 437)
    {
        TextDrawShowForPlayer(playerid,Textdraw0);
        new DistanceAngleP, DistanceBayside, DistanceDill, DistanceElQue, DistanceFortC, DistanceLasB, DistanceLasP, DistanceMont, DistancePC;
        DistanceAngleP = GetPlayerDistanceToPointEx(playerid,-2169.75, -2305.75, 30.46);
        DistanceBayside = GetPlayerDistanceToPointEx(playerid,-2565.75,2315.25,4.83);
        DistanceDill = GetPlayerDistanceToPointEx(playerid,736.00, -527.50, 16.18);
        DistanceElQue = GetPlayerDistanceToPointEx(playerid,-1467.00, 2674.00, 55.68);
        DistanceFortC = GetPlayerDistanceToPointEx(playerid,-45.37, 1200.68, 19.21);
        DistanceLasB = GetPlayerDistanceToPointEx(playerid,-2565.75, 2315.25, 4.83);
        DistanceLasP = GetPlayerDistanceToPointEx(playerid,-269.00, 2625.75, 62.64);
        DistanceMont = GetPlayerDistanceToPointEx(playerid,1288.50, 329.00, 19.40);
        DistancePC = GetPlayerDistanceToPointEx(playerid,2263.75, -30.75, 26.33);
    }
    else
    {
        SendClientMessage(playerid, 0xFF0000AA, "You need a Bus Or Coach to work!");
    }
    return 1;
}
Now, i need it to be like if DistanceAngleP is the shortest it calls MissionAngleP(playerid); or if DistanceFortC is the shortest then it calls MissionFortC(playerid);, how do i do that?
Reply
#2

Put them all in an array instead of single variables, then you can use this function to get the index of the shortest distance (just writing it, cant guarantee for it to be bug-free )

pawn Code:
stock GetSmallestIndex(v[], len = sizeof(v))
{
    new min_index, min_value = -1;
    for(new i = 0; i < len; i ++)
    {
        if(v[i] < min_value || minvalue == -1)
        {
            min_index = i;
            min_value = v[i];
        }
    }
    return min_index;
}

// Use it like this then:
new values[8];
values[0] = GetPlayerDistance...

switch(GetSmallestIndex(values))
{
    case 0: MissionFortC(playerid); break;
    case 1: //...
}
Reply
#3

TY
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)