Detecting which is the closest
#1

So I am scripting the Trucker job and erm, I wanted to detect if the coordinates for the delivery is close to San Fierro or Los Santos.

For instance, The coordinates are set to Commerence, It will automatically detect that it's in Los Santos and if it is set to Downtown San Fierro, It will automatically detect that it's in San Fierro. If the coordinates are located in a county-side, It will get the closest city in distance.

I have an idea though, I will get the coordinates for LS/SF and then use this function to do it.

PHP код:
// From SCRP
stock GetClosestHospital(playerid)
{
    new
        
Float:fDistance[2] = {99999.00.0},
        
iIndex = -1
    
;
    for (new 
0sizeof(arrHospitalSpawns); ++)
    {
        
fDistance[1] = GetPlayerDistanceFromPoint(playeridarrHospitalSpawns[i][0], arrHospitalSpawns[i][1], arrHospitalSpawns[i][2]);
        if (
fDistance[1] < fDistance[0])
        {
            
fDistance[0] = fDistance[1];
            
iIndex i;
        }
    }
    return 
iIndex;

Though I am clueless on how am I gonna do it.
Reply
#2

Here's an example:
PHP код:
// ** INCLUDES
#include <a_samp>
#include <zcmd>
// ** ARRAYS AND ENUMERATORS
static const Float:aHospitalLocations[][] =
{
    {
1606.79001821.856710.8203}, // Las Venturas
    
{1184.4950, -1323.946813.5744// Los Santos
};
// ** MAIN
main()
{
    print(
"Loaded \"get_closest_location_to_player.amx\".");
}
// ** CALLBACKS
public OnGameModeInit()
{
    return 
1;
}
public 
OnGameModeExit()
{
    return 
1;
}
// ** COMMANDS
CMD:closesthospital(playerid)
{
    new 
string[144], Float:xFloat:yFloat:z;
    
GetClosestHospitalToPlayer(playeridxyz);
    
format(stringsizeof(string), "%f, %f, %f"xyz);
    
SendClientMessage(playerid, -1string);
    return 
1;
}
// ** FUNCTIONS
stock GetClosestHospitalToPlayer(playerid, &Float:x, &Float:y, &Float:z)
{
    new 
Float:distance[2], index;
    
distance[0] = GetPlayerDistanceFromPoint(playeridaHospitalLocations[0][0], aHospitalLocations[0][1], aHospitalLocations[0][2]);
    for(new 
1sizeof(aHospitalLocations); ++)
    {
        
distance[1] = GetPlayerDistanceFromPoint(playeridaHospitalLocations[i][0], aHospitalLocations[i][1], aHospitalLocations[i][2]);
        if(
distance[1] < distance[0])
        {
            
distance[0] = distance[1];
            
index i;
        }
    }
    
aHospitalLocations[index][0];
    
aHospitalLocations[index][1];
    
aHospitalLocations[index][2];
    return 
1;

Reply
#3

Will try this out, cheers m8.

EDIT: How can I determine if the player is in LS or LV (in this case SF) though?
Reply
#4

There are various ways, depends on which one you choose.

- Create 3 areas with the streamer plugin, one for each city
- Specify which city each location/zone is in (array -> {"Commerce", x, y, z, "Los Santos"},)
- Etc., etc.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)