Script: Get the Direction [0,1,2,3] Facing any Relative X,Y | faceoffdir = f(x,y)

[for the impatient]

//-And heres the context-free application of this concept, simplified.
//-Its a nice little skeleton for scripting directional logic into 
// more complicated player/npc 'reactions' to elements with an (x,y) origin. 
//////////////
//ACTUAL SCRIPT BELOW#!

function faceOffDirToObject() {
  angletoobj =  REPLACE_WITH_ANGLE_FUNCTION; //try not to get a NaN here
  dir = int(.637 * angletoobj + .5) + 3 % 4;
    //*now do something with that dir
}
  

So I figured I’d throw my share into commune pile to help along with playerworld development.

I’ve been converting my weaponset to a click interface for my 5 year old nephew who cant yet grasp the W,A,S,D,<,^,>,v two hand harmony.

Thanks goes to the Reborn Team for keepin it alive and in the spirit of oleskool graal, and yes for saving me, friends and family $$ / month.

//FaceOffDirection by Strider
//////////////////
//-originally I just wanted the player to face the mouse simply like: 

//  anglefromplayer = f (mousex,mousey)  
         //*calculate angle from player to the
         // mouse (probably with the trusty
         // getangle(dx,dy) command)

//  playerdir = g (anglefromplayer) 
         //*yield the correct direction from that angle.

//  I'm defining 'correct direction' as:
//    NAME    DIR    RANGE(degrees)
//     up      0      [45,135)
//     left    1      [135,225)
//     down    2      [225,315)
//     right   3      [0,45) U [315,360)  <=non-continuous issue

//  So heres what I had:
  if (created) {timeout =.05}
  if (timeout) {
    dx = mousex-(playerx+1.5);
    dy = mousey-(playery+1.5);
    angle = getangle(dx,dy);

    pi = 3.14;
    anglewithoffset = angle + (pi / 4); 
         //*we have to offset the angle, so 
         // that our RANGE for DIR = 3 becomes continuous 

    serial = int( anglewithoffset / (pi / 2) );  
         //*this way, if we divide the 
         // offset angle by (pi/2), then round down, 
         // we will get an number from a consecutive series

    playerdir = serial + 3 % 4;  
         //*and finally we use mod to translate/tweak 
         //the series into the range we need ([0,1,2,3])
  }

//-Admittedly, the example application above will yield a NaN when:
// dx = dy = 0, from the (trusty) getangle(dx,dy) command.
//-Its really up to the individual how he/she wants to deal with this.
// eg. you could just leave it and take whatever you get from the function

//-And heres the context-free application of this concept, simplified.
//-Its a nice little skeleton for scripting directional logic into 
// more complicated player/npc 'reactions' to elements with an (x,y) origin. 
//////////////
//ACTUAL SCRIPT BELOW#!

function faceOffDirToObject() {
  angletoobj =  REPLACE_WITH_ANGLE_FUNCTION; //try not to get a NaN here
  dir = int(.637 * angletoobj + .5) + 3 % 4;
    //*now do something with that dir
}
  

Alot of possible applications for this script based on a combination of how you define the angle function, what you do with the dir when you get it (setting a .gani would be the start), or even some hacking of dir function itself if you’re savy.

And with all the great things being done with triggeraction on Gserver +/- NPC-Server, a lot of interesting things you can do if you’re creative…and can config params.

eg. making all players or npc(with dir) near you to turn to notice you, then run off into the other direction, because you’re too badaz for this and that reason

-Cheers

Thanks for the contribution, especially starting your first one as a good one.

Hope many more come after.

I’m pretty sure Pi is already set to half a radian.

wow nice reference guide, should be handy for something :slight_smile:

Isn’t there getdir in the older versions?

I forget, but I always just did a getangle with a - pi/2 offset, then divide it by a quarter of pi, then int( ) to get an integer 0~3. Most likely one in this code but I just don’t feel like reading it at this time, wall of text for a small formula to me o_o

faceOffDirToObject() is never called either.

yeah you got me, i was trying to secretly show people math :D.
but then again I did kinda play with the numbers at the end, turning [1,2,3,4] into [0,1,2,3].

I was trying to get that 0~3 without +/-/%ing after the int( ), but I couldnt figure it out, atleast not with any negative offsets. in the end I chose a + pi/4 offset because it let me consider the range for dir = 3 as continuous range. and eh, theres some more math involved in the why but i wont get into it.

function actionFaceOffDirToObject($#%**&(&(#||$#*%&
#@$%^#@%%@#@$%@#$%^^#&&$#&$#$*$#*#*#$&
#$^$===your favorite params syntax=====@#^@^&&$
$^#$^$#&$#&#$&$%@!!@#$!@$%^^&&@$&$*$#^$#&
  angletoobj =  REPLACE_WITH_ANGLE_FUNCTION; //try not to get a NaN here
  dir = int(.637 * angletoobj + .5) + 3 % 4;
    //*now do something with that dir
}  

there, now you can only trigger it, still wont work without replacing something…

___Merged doublepost__________________

thanks, though i easily spent way more time typing out the breakdown than the actual coding. Still, it was things like this that helped me out alot when I was young scriptin

Actions aren’t done like that in GS1. Unless Joey’s NPC-Server uses functions like that. (If that’s the case then don’t even read the rest of the post.)

triggeraction x,y,bullshit,params;
if (actionbullshit) doshit;

And functions need to be called like this:

bullshit();
function bullshit() doshit;