February 24, 2018, 04:08:41 am

Author Topic: Useful Small Functions/Snippets  (Read 1937 times)

0 Members and 1 Guest are viewing this topic.

Charley

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 4065
  • Country: 00
    • View Profile
    • Awards
Useful Small Functions/Snippets
« on: July 25, 2011, 11:08:40 am »
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.

Code: [Select]
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).

Code: [Select]
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).

Code: [Select]
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";
}
}
« Last Edit: May 09, 2012, 09:17:59 am by Charley »
Winner Winner x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Writer of excessively long posts


Skirmant

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 676
  • Country: il
  • Ignorance is bliss.
    • View Profile
    • Awards
Re: Distance, InRadius, Direction
« Reply #1 on: November 07, 2011, 08:30:26 pm »
Nice. Especially the direction one  ;D

I'll just point out a few extra ones for people that suck at math. I heard Squirrel allows function overloading so I think there should be no problem in giving them the same names as long as the parameters differ.

1D Distance

Code: [Select]
function Distance(x1, x2) {
 local dis = x1 - x2;
 if (dis < 0) dis = -dis;
 return dis;
}

3D Distance

Code: [Select]
function Distance(x1, y1, z1, x2, y2, z2 ) {
return sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)) + ((z2 - z1)*(z2 - z1)));
}

3D Radius

Code: [Select]
function InRadius(x1, y1, z1, x2, y2, z2, rad) {
if (sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)) + ((z2 - z1)*(z2 - z1))) < rad) return true;
else return false;
}
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

"C++ is a horrible language" - Linus Torvalds, creator of Linux

Quote
<SLC> nope. changed my mind. squirrel still sux dix
<SLC> it's just a piece of sh!t
* SLC has quit (Connection closed)

aXXo

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • *
  • Posts: 2377
  • Country: in
    • View Profile
    • Awards
Re: Distance, InRadius, Direction
« Reply #2 on: January 08, 2012, 01:03:10 pm »
Code: [Select]
//Function to get the coordinates of where a player is looking at.
//range represents the distance ahead of the player at which you want the point to be placed.
function GetPlayer2DLookAtPos( player, range )
{
local angle = player.Angle + 90; //To point North at 90 degrees (It is at 0 degree by default)

if ( angle > 360 )
angle -= 360;

local slope = tan( angle * ( pi/180 ) );

local x2 = null, y2 = null;

if ( angle >= 135 && angle <= 225 )
x2 = player.Pos.x - range;
else if ( angle > 225 && angle <= 315 )
y2 = player.Pos.y - range;
else if ( angle > 315 || angle <= 45 )
x2 = player.Pos.x + range;
else if ( angle >= 45 && angle < 135 )
y2 = player.Pos.y + range;

if ( !y2 )
y2 = ( ( x2 - player.Pos.x ) * slope ) + player.Pos.y;

else if ( !x2 )
x2 = (( y2 - player.Pos.y )/slope) + player.Pos.x;

return Vector( x2, y2, player.Pos.z);
}

Example Usage:
Code: [Select]
GetPlayer2DLookAtPos( player, 2 )Returns coordinates of a point 2 steps "ahead" of a player.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor


Thijn

  • Forum Administrator
  • Crazy Man
  • *
  • *
  • *
  • Posts: 2801
  • Country: nl
    • View Profile
    • Awards
Re: Distance, InRadius, Direction
« Reply #3 on: January 09, 2012, 07:18:35 am »
Ow thats a nifty function axxo
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

I'm not totally useless,
I can be used as an bad example ;)

"Never do Today, What you can do Tomorrow"



Charley

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 4065
  • Country: 00
    • View Profile
    • Awards
Re: Distance, InRadius, Direction
« Reply #4 on: January 09, 2012, 11:54:33 am »
I'm looking forward to testing that aXXo, trig ftw :p

One small thing for anyone that's new,

Code: [Select]
local x2 = null, y2 = null;
can just be
Code: [Select]
local x2, y2;
Makes no difference in functionality, just looks a bit neater imo.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Writer of excessively long posts


Charley

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 4065
  • Country: 00
    • View Profile
    • Awards
Re: Useful Small Functions/Snippets
« Reply #5 on: May 09, 2012, 09:18:31 am »
I've changed the name of this thread so that anyone with useful small functions or snippets can post them. If they're deemed useful enough and working then they'll stay.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Writer of excessively long posts