Here are some simple functions I wrote that are both useful within scripts and for becoming more acquainted with Squirrel.
Distance
This function returns the distance, in metres, between point (x1, y1) and (x2,y2). It uses simple Pythagoras.
function Distance(x1, y1, x2, y2)
{
local dist = sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));
return dist;
}
InRadius
This function also uses Pythagoras. It returns whether point (x2,y2) is within the specified radius (rad, in metres) of point (x1,y1).
function InRadius(x1, y1, x2, y2, rad)
{
//where 1 = centre coord and 2 = player.pos
if (sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))) < rad)
{
return true;
}
else if (sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))) > rad)
{
return false;
}
}
Direction
This function returns the direction (North/East/South/West or in between) that point (x2,y2) is in from point (x1,y1).
function Direction(x1, y1, x2, y2)
{
//all values must be floats.
x1 = x1.tofloat();
x2 = x2.tofloat();
y1 = y1.tofloat();
y2 = y2.tofloat();
// Added those ^ just in case you forget :P
local m = (y2-y1)/(x2-x1);
if ((m >= 6) || (m <= -6))
{
if (y2 > y1) return "North";
else return "South";
}
if ((m < 6) && (m >= 0.5))
{
if (y2 > y1) return "North East";
else return "South West";
}
else if ((m < 0.5) && (m > -0.5))
{
if (x2 > x1) return "East";
else return "West";
}
else if ((m <= -0.5) && (m > -6))
{
if (y2 > y1) return "North West";
else return "South East";
}
}