Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Simple Poly Line (to mouse) (https://forums.graalonline.com/forums/showthread.php?t=134261559)

scriptless 01-06-2011 12:53 AM

Simple Poly Line (to mouse)
 
Okay this is an easy one, but for some reason I cannot get a line to draw to the players mouse from a given x/y of a npc.. Any suggestions here, i know its top left, top right, bottom right, and bottom left for the order of cords.. or am i wrong here?

PHP Code:

//#CLIENTSIDE
function onMouseDown() {
  
with findImg4000)) {  
    
polygon = {
      
mousexmousey,
      
mousex-(2/16), mousey-(2/16),
      
xy,
      
x-(2/16),y-(2/16)
    };
    
layer 3;
  }


All I am trying to do is make a line draw from the npc to the mouse.. =/

It seems to be twisting, but i cant get the cords working right, grr !!

fowlplay4 01-06-2011 12:59 AM

Try swapping x, y with x-(2/16), y - (2/16)

and your explanation sounds misunderstood, it's from point to point to point.

scriptless 01-06-2011 01:03 AM

ohh.. guess your right on point-to-point.. hmm
line kinds dissapears at certain angles.. is this normal tho?

fowlplay4 01-06-2011 01:05 AM

Quote:

Originally Posted by scriptless (Post 1620595)
ohh.. guess your right on point-to-point.. hmm
line kinds dissapears at certain angles.. is this normal tho?

Well you're using layer 0-3, so you'd get better/pixel accuracy if you used layer 4 and up with mousescreenx and mousescreeny.

scriptless 01-06-2011 01:06 AM

Quote:

Originally Posted by fowlplay4 (Post 1620597)
Well you're using layer 0-3, so you'd get better/pixel accuracy if you used layer 4 and up with mousescreenx and mousescreeny.

Was not aware layers had a difference lol.. im trying to make this script plot a position on the level and draw a line to it.. so i can see the NPC's path of movement.. the npc will move along the line and pick another destination but i wanted to see it's path, you know..

12171217 01-06-2011 01:07 AM

Quote:

Originally Posted by fowlplay4 (Post 1620597)
Well you're using layer 0-3, so you'd get better/pixel accuracy if you used layer 4 and up with mousescreenx and mousescreeny.

It's a polygon, not a line with a width/height. Defining the points like that will only result in a proper "thickness" at two angles. The angle has to be compensated for using some trig.

Crow 01-06-2011 01:08 AM

http://forums.graalonline.com/forums...ight=draw+line

scriptless 01-06-2011 01:09 AM

So why does this NOT work?

PHP Code:

//#CLIENTSIDE
function onMouseDown() {
  
this.angle random(0,360); // angle
  
this.distance random(5,7); // distance
  
this.nx sin(this.angle) * this.distance;
  
this.ny cos(this.angle) * this.distance;
  
with findImg4000)) {  
    
polygon = {
      
this.nxthis.ny,
      
this.nx-(2/16), this.ny-(2/16),
      
x-(2/16),y-(2/16),
      
xy
    
};
    
layer 3;
  }
  
chat this.nx SPC this.ny;


The NPC chats the correct cords but does not move to draw poly properly..

12171217 01-06-2011 01:13 AM

Quote:

Originally Posted by Crow (Post 1620602)

Also doesn't take into account the direction of the line. Again, needs trig.

scriptless 01-06-2011 01:14 AM

Quote:

Originally Posted by 12171217 (Post 1620606)
Also doesn't take into account the direction of the line. Again, needs trig.

I noticed that.. Downsider is the trig simple or no? I guess the line is not completly important but it's not drawing to the right cords when i used the above script.. the line is just for visual debugging so I guess it doesnt have to be perfect.

scriptless 01-06-2011 01:16 AM

Nvm, problem solved. I used the DrawLine() custom function and it worked perfectly.. hmm now to redo the other script I had and use this new-found information.

Thanks everyone for the help.

fowlplay4 01-06-2011 01:31 AM

Well just using 2 (ox, oy, tx, ty) points works fine as well.

cbk1994 01-06-2011 01:31 AM

Quote:

Originally Posted by 12171217 (Post 1620606)
Also doesn't take into account the direction of the line. Again, needs trig.

Sales made one a while back.


Quote:

Originally Posted by salesman (Post 1474047)
I came across this thread when trying to find out if there was a built in drawLine() function. However, I needed the width of the line to be the same regardless of angle and made this:

PHP Code:

//DrawLine( img_index, start_x, start_y, target_x, target_y, width_in_tiles, red, green, blue, alpha)
public function DrawLineindsxsytxtywidrgbalph ) {
  
temp.ang getangle(tx sxty sy) + (pi/2);
  
temp.wid = ( wid == NULL .1 wid );

  
with findImg(ind) ) {
    
polygon = {
      
sxsy,
      
txty,
      
tx cos(ang)*widty sin(ang)*wid,
      
sx cos(ang)*widsy sin(ang)*wid
    
};
    
red = ( || );
    
green = ( || );
    
blue = ( || );
    
alpha = ( alph <= || alph alph);
    
layer 3;
  }


An example:
PHP Code:

DrawLine(20025254545);
//Draws a black line of width .1 tiles from 25,25 to 45,45 

or if you want to specify color, width and alpha:
PHP Code:

DrawLine(20025254545.3100.5);
//Draws a semi-transparent red line of width .3 tiles from 25,25 to 45,45 



fowlplay4 01-06-2011 01:39 AM

What's the reasoning for adding pi/2, getangle a fraud?

scriptless 01-06-2011 01:41 AM

This doesnt seem to be choosing the random correctly..

PHP Code:

//#CLIENTSIDE
public function DrawLineindsxsytxtywidrgbalph ) { 
  
temp.ang getangle(tx sxty sy) + (pi/2); 
  
temp.wid = ( wid == NULL .1 wid ); 

  
with findImg(ind) ) { 
    
polygon = { 
      
sxsy
      
txty
      
tx cos(ang)*widty sin(ang)*wid
      
sx cos(ang)*widsy sin(ang)*wid 
    
}; 
    
red = ( || ); 
    
green = ( || ); 
    
blue = ( || ); 
    
alpha = ( alph <= || alph alph); 
    
layer 3
  } 
}  

function 
onMouseDown() {
    
this.angle random(this.angle-15,this.angle+15); // angle
    
this.distance random(5,7); // distance
    
this.nx sin(this.angle) * this.distance;
    
this.ny cos(this.angle) * this.distance;
    
DrawLine(2000this.nxthis.nyxy);


The angles are still wrong.. it's supose to pick a random angle within 15 degrees of current angle.. o_O but it's still 360 =/

cbk1994 01-06-2011 01:42 AM

It's in radians, not degrees.

PHP Code:

this.angle random(this.angle-0.261,this.angle+0.261

Google "# degrees to radians" if you need help converting.

You can also use degtorad but it's not recommended to use degrees in scripting anyway.

fowlplay4 01-06-2011 01:43 AM

edit: been said, but there is functionality in Graal to convert to and from it.

Functions:

degtorad(degrees)
radtodeg(degrees)

I.e:

this.angle = random(this.angle - degtorad(15), this.angle + degtorad(15));

scriptless 01-06-2011 01:50 AM

oh, i guess i learn somethign every day XD

12171217 01-06-2011 02:03 AM

Quote:

Originally Posted by fowlplay4 (Post 1620621)
What's the reasoning for adding pi/2, getangle a fraud?

You need the points to the left and the right of the line, not in front of it, so you have to subtract 90 degrees, or pi/2 radians.

WhiteDragon 01-06-2011 02:50 AM

PHP Code:

function degtorad(temp.d) {
  return 
temp.d*pi/180;
}

function 
radtodeg(temp.r) {
  return 
temp.r*180/pi;


For anyone interested.

A degree is a value between 0 and 360, which we can write as [0,360].
A radian is a value between 0 and 2*pi, which we can write as [0,2*pi].

This is how degtorad would work:
  • Start with [0,360].
  • Divide by 360, and get to [0,1].
  • Multiple by 2pi, and get to [0,2*pi].

(The way I wrote it in the code was divide by 180 then multiple by pi. Same thing.)

radtodeg would just be the opposite.

Also worth noting, 0 points to the right, and positive values rotate counterclockwise.

fowlplay4 01-06-2011 02:54 AM

Someone really needs to go through forum posts, categorize them, and organize them into a tutorial/documentation.

scriptless 01-06-2011 03:20 AM

New problem, move() only works first time then stops.. any particular reason behind this?

PHP Code:

function onMouseDown() {
  
this.nx x;
  
this.ny y;
  for ( 
i=0i<3i++ ) {
    
this.angle random(this.angle-0.522,this.angle+0.522); // angle
    
this.distance random(2,5); // distance
    
this.nx2 this.nx sin(this.angle) * this.distance;
    
this.ny2 this.ny cos(this.angle) * this.distance;
    
move(xyx+5y+5);
    
DrawLine(2000+ithis.nxthis.nythis.nx2this.ny2);
    
this.nx += sin(this.angle) * this.distance;
    
this.ny += cos(this.angle) * this.distance;
  }



cbk1994 01-06-2011 03:22 AM

Quote:

Originally Posted by scriptless (Post 1620649)
New problem, move() only works first time then stops.. any particular reason behind this?

PHP Code:

function onMouseDown() {
  
this.nx x;
  
this.ny y;
  for ( 
i=0i<3i++ ) {
    
this.angle random(this.angle-0.522,this.angle+0.522); // angle
    
this.distance random(2,5); // distance
    
this.nx2 this.nx sin(this.angle) * this.distance;
    
this.ny2 this.ny cos(this.angle) * this.distance;
    
move(xyx+5y+5);
    
DrawLine(2000+ithis.nxthis.nythis.nx2this.ny2);
    
this.nx += sin(this.angle) * this.distance;
    
this.ny += cos(this.angle) * this.distance;
  }



You can't stack moves on top of eachother like that. Use the option that gives an event when finished (8? check scripthelp) and in the onMovementFinished (if I recall correctly) event call move again.

scriptless 01-06-2011 03:24 AM

so something like

PHP Code:

function newmove() {
  if ( 
this.moved == false move(..);
  
this.moved true;
}

function 
onMovementFinished() {
  
this.moved false;
  
newmove();


is what your saying?

fowlplay4 01-06-2011 03:25 AM

move doesn't work like that.

TServerNPC.move(float, float, float, int)
- moves the npc smoothly
- parameters:
delta x,
delta y,
time
options: cache type (0, 1-cache, 2-append) + blockcheck(4) + eventwhendone(8) + applydir(16)

move(dx, dy, time, flags);

I.e:

PHP Code:

//#CLIENTSIDE
function moveTo(txty) {
  
// Delta = Final - Initial
  
temp.dx tx this.x;
  
temp.dy ty this.y;
  
temp.speed 1// Tiles per second
  
temp.time getDistance(this.xthis.ytxty) / temp.speed;
  
move(temp.dxtemp.dytemp.time8); 
}

function 
onMovementFinished() {
  
// Your other code..
}

function 
GetDistance(temp.x1temp.y1temp.x2temp.y2) {
  
temp.toreturn = (temp.x2 temp.x1) ^ + (temp.y2 temp.y1) ^ 2;
  return (
temp.toreturn) ^ .5;



scriptless 01-06-2011 03:28 AM

Quote:

Originally Posted by fowlplay4 (Post 1620652)
move doesn't work like that.

TServerNPC.move(float, float, float, int)
- moves the npc smoothly
- parameters:
delta x,
delta y,
time
options: cache type (0, 1-cache, 2-append) + blockcheck(4) + eventwhendone(8) + applydir(16)

move(dx, dy, time, flags);

I.e:

PHP Code:

//#CLIENTSIDE
function moveTo(txty) {
  
// Delta = Final - Initial
  
temp.dx tx this.x;
  
temp.dy ty this.y;
  
temp.speed 1// Tiles per second
  
temp.time getDistance(this.xthis.ytxty) / temp.speed;
  
move(temp.dxtemp.dytemp.time8); 
}

function 
onMovementFinished() {
  
// Your other code..
}

function 
GetDistance(temp.x1temp.y1temp.x2temp.y2) {
  
temp.toreturn = (temp.x2 temp.x1) ^ + (temp.y2 temp.y1) ^ 2;
  return (
temp.toreturn) ^ .5;



hmm.. interesting i should have looked up syntax earlier lol.. i thought i was doing it strange.. but still would i do similar to how i posted before because of using move() wrong without the check if movement ended.. ?

WhiteDragon 01-06-2011 03:36 AM

Quote:

Originally Posted by scriptless (Post 1620653)
hmm.. interesting i should have looked up syntax earlier lol.. i thought i was doing it strange.. but still would i do similar to how i posted before because of using move() wrong without the check if movement ended.. ?

Your earlier code looks to me like it'd go on forever.

newmove -> onMovementFinished -> newmove -> onMovementFinished -> newmove -> ...

If you only want to do it a certain amount of times, you could track it in a this variable which you start at say, 3, and then decrease every time the movement is finished.

scriptless 01-06-2011 03:40 AM

it looks like i have alot more scripting to do haha .. oh well im bored anyways

yeah it's only suspose to plan 3 moves and then move the paths.. then im gonna need onwall checks .. gah


tho technically it's supose to continiously move.. maybe randomly pause a second or 2.. lol

12171217 01-06-2011 03:55 AM

move() is the worst interpolation system EVER created.

When I get my main CPU back, I'll write a REAL interpolation system. I've already written one for Delteria, and it looks lightyears ahead of "move" and reduces server strain a lot. A .25 timeout looks just as good as a .05, and you don't have to deal with using that ****ty move() command where if you change your direction during the middle of the movement, it jumps around. You just do movement like normal, and set a certain attr to a certain value. Easy.

salesman 01-06-2011 04:01 AM

Quote:

Originally Posted by fowlplay4 (Post 1620621)
What's the reasoning for adding pi/2, getangle a fraud?

because thickness is perpendicular to the angle of the line


All times are GMT +2. The time now is 06:49 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.