Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   sorting for lowest array values (https://forums.graalonline.com/forums/showthread.php?t=86576)

Prozac 06-27-2009 10:57 PM

sorting for lowest array values
 
so, what I want to do is detect the closest baddy to an npcs x and y location. here is what I have so far .. how can i sort the 2 arrays better?

PHP Code:

// NPC made by Prozac
if (created) {timeout=.05;}
if (
timeout)
  {
  if (
compuscount>0)
    {
    
setarray badx,compuscount;
    
setarray bady,compuscount;
    for(
i=0i<compuscounti++;)  
      {
      
badx[i]=abs(compus[i].x-x);
      
bady[i]=abs(compus[i].y-y);
    }
    
    
close=0//close = the index of the baddy closest to npc
    
for(j=0j<arraylen(badx); j++;)
      {
      if (
j>1)
        {
        if (
badx[j-1]>badx[j] || bady[j-1]>bady[j]) {close=j;}
      }
    }
    
showimg 1,chest.png,compus[close].x,compus[close].y;
    
// it will do more interesting stuff but showimg is to see which baddy
  
}
  
timeout=.05;



napo_p2p 06-27-2009 11:11 PM

You could do the distance calculation while scanning through the baddies:

PHP Code:

temp.close = -1;  //index of closest baddy
temp.smallestdist 9999;  //distance from closest baddy
for (temp.0temp.compuscounttemp.i++) {
  
temp.dist = ((compus[temp.i].x)^+ (compus[temp.i].y)^2) ^ 0.5;
  if (
temp.dist temp.smallestdist) {
    
//This baddy is the closest, so update
    
temp.smallestdist temp.dist;
    
temp.close temp.i;
  }



Prozac 06-28-2009 02:43 AM

thanks! that works quite nicely

Robin 06-28-2009 09:21 AM

findareanpcs(float, float, float, float) - returns object - returns an array of all npcs at the specified rectangle (x,y,width,height)


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

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