SA-MP Forums Archive
How to know where the player is the most closed to 5 point? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: How to know where the player is the most closed to 5 point? (/showthread.php?tid=75018)



How to know where the player is the most closed to 5 point? - Andom - 28.04.2009

Hello, i have 5 points in my map;
* one in San Fierro
* one in Los Santos
* one in Las Venturas
* one in The Desert
* one in The Middle Of Nowhere

How to know where a player is the closest to?


Re: How to know where the player is the most closed to 5 point? - MenaceX^ - 28.04.2009

Use PlayerToPoint.


Re: How to know where the player is the most closed to 5 point? - Andom - 01.05.2009

Quote:
Originally Posted by MenaceX^
Use PlayerToPoint.
That doesn't make any sence, because if i do that it just brink me to the point i first scripted, not the closest to.


Re: How to know where the player is the most closed to 5 point? - yom - 01.05.2009

It's a bit hard to explain with words, so i will just post basically the method i use in my gamemode:

Define points at the top of script:
pawn Код:
enum
{
  INVALID_POINT = -1,
  POINT_1,
  POINT_2,
  POINT_3
}

static Float:Points[][] =
{
  { 10.0, 10.0, 10.0 }, //POINT_1
  { 0.0, 0.0, 0.0 }, //POINT_2
  { -10.0, -10.0, -10.0 } //etc
};
Then, the main function, which return an index of the Points array, and you can get the distance in 'd':
pawn Код:
GetClosestPoint(Float:x, Float:y, Float:z, &Float:d = 0.0)
{
  new
    c = INVALID_POINT,
    Float:x2,
    Float:y2,
    Float:z2,
    Float:d2 = 70000.0
  ;

  for (new p = 0; p < sizeof Points; p ++)
  {
    x2 = x - Points[p][0];
    x2 *= x2;
       
    y2 = y - Points[p][1];
    y2 *= y2;
       
    z2 = z - Points[p][2];
    z2 *= z2;
       
    d = floatsqroot(x2 + y2 + z2);

    if (d < d2)
    {
      d2 = d;
      c = p;
    }
  }

  d = d2;

  return c;
}
pawn Код:
GetPlayerClosestPoint(playerid)
{
  new
    Float:x,
    Float:y,
    Float:z
  ;

  GetPlayerPos(playerid, x, y, z);

  return GetClosestPoint(x, y, z);
}
So basically now all you have to do is:
pawn Код:
switch (GetPlayerClosestPoint(playerid))
{
  case POINT_1 : //bla
  case POINT_2 : //etc
}