Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-27-2009, 10:57 PM
Prozac Prozac is offline
one of the good guys
Prozac's Avatar
Join Date: Jan 2006
Posts: 245
Prozac is on a distinguished road
Send a message via AIM to Prozac
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;

Reply With Quote
  #2  
Old 06-27-2009, 11:11 PM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
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;
  }

__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #3  
Old 06-28-2009, 02:43 AM
Prozac Prozac is offline
one of the good guys
Prozac's Avatar
Join Date: Jan 2006
Posts: 245
Prozac is on a distinguished road
Send a message via AIM to Prozac
thanks! that works quite nicely
Reply With Quote
  #4  
Old 06-28-2009, 09:21 AM
Robin Robin is offline
The secret of NIMH
Robin's Avatar
Join Date: Apr 2005
Location: Wales, UK
Posts: 515
Robin will become famous soon enough
Send a message via AIM to Robin
findareanpcs(float, float, float, float) - returns object - returns an array of all npcs at the specified rectangle (x,y,width,height)
__________________

Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:47 PM.


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