Page 1 of 1

Getting the closest target

PostPosted: Wed Nov 23, 2011 9:42 pm
by Hblade
How would I go about finding the closest enemy?

Re: Getting the closest target

PostPosted: Wed Nov 23, 2011 10:20 pm
by jimmynewguy
did a sort of demo thing on this a pretty long time ago. Should do the trick!
viewtopic.php?f=4&t=6654&p=48310&hilit=+closest#p48310

Re: Getting the closest target

PostPosted: Wed Nov 23, 2011 10:25 pm
by Hblade
Thanks Ill check it out. +1

Re: Getting the closest target

PostPosted: Thu Nov 24, 2011 5:24 am
by Hblade
Can you pin-point and explain how this is done? ^^

Re: Getting the closest target

PostPosted: Thu Nov 24, 2011 12:29 pm
by jimmynewguy
Code: Select all
int n;
double mindist = 10000;//set closest distance to out of range so the first check will work
Actor *actors, *closest = NULL;// NULL since 0 can be an actor technically and the check will fail
actors = getAllActorsInCollision("Event Actor", &n);// get all actors in the collision
// actors is the actors, n is the number of actors

if(actors)// if we have actors
{
  int i;
  for(i = 0; i < n; i++)// cycle through them
  {
    if(strcmp(actors[i].name, "a") == 0 ||// js it actor a
    strcmp(actors[i].name, "c") == 0)// or is it actor c
 
    {
       double d = distance(parent.x, parent.y, actors[i].x, actors[i].y);// d is the distance of this actor from the player
       if(d < mindist)// if the actor is closer than the the previous closest actor
       {
           closest = (actors + i);//make the closest actor
           mindist = d;//new mindist
       }
    }
  }
}
if(closest)//if we have a closest actor
{
MoveTo("Parent Actor", 0.000000, 0.000000, 2.000000, closest->clonename, "");// move our parent to the closest actor
}

This code in the range actor does it all. It checks all the actors in it's collision when the timer goes. The number of actors is n and the name of the actor is actors. If there are actors [if(actors)] it does a loop through the clones to check if the actors' name is "a" or "c". If yes, then it gets the distance and sets this to d. d is then checked to see if it is less than the mindist variable (which is set to 10000 in the beginning (aka out of range)) if d is less than the mindist than the mindist will now be d and the closest actor will now be the actor that was just checked. Once the loop is done we check if there is a closest actor and move our parent to it. [if(closest)]

Re: Getting the closest target

PostPosted: Thu Nov 24, 2011 1:39 pm
by Hblade
thanks =)