Page 1 of 1

Shooting nearest target

PostPosted: Thu Jul 31, 2014 11:11 pm
by sportmaster
I don't know how to do this. Please help me.
draw.png

Re: Shooting nearest target

PostPosted: Fri Aug 01, 2014 12:26 am
by skydereign
You need to loop through all the blue clones. In the current build there isn't a built in way to do this, so you should create a variable called highestBlueCloneindex, and in the blue clone actor's create actor event, have this code.
Code: Select all
highestBlueCloneindex = cloneindex;

You want to track this number as you'll need to loop through every clone, starting from the lowest indexed to the highest. The reason the above code works to track the highest indexed clone is that the newest clone is always the one with the highest cloneindex. And we don't need to track the lowest cloneindex because the default actor struct can be used to get the lowest.

You'll also need to put this in global code, so you can use cloneindex to get references to the clones via cloneindex.
Code: Select all
Actor *getclone2(char* cname, int cindex)
{
    char buffer[30];
    sprintf(buffer,"%s.%d", cname, cindex);
    return(getclone(buffer));
}


That way whenever you want to get the angle to the closest clone you can do this.
Code: Select all
int i;
Actor* closest = NULL;
float closest_dist = 0;

for(i=blueClone.cloneindex; i<=highestBlueCloneindex; i++)
{
    Actor* i_clone = getclone2("blueClone", i);

    if(i_clone->cloneindex!=-1) // the clone's cloneindex is -1 when the clone does not exist, this checks only if the clone exists
    {
        float i_dist = distance(x, y, i_clone->x, i_clone->y);
        if(closest == NULL || closest_dist > i_dist) // either there is no closest assigned yet, or the current one is closer
        {
            closest_dist = i_dist; // update to new distance
            closest = i_clone; // store it
        }
    }
}

if(closest != NULL)
{
    // than closest is the blueClone that is closest to the event actor
    float ang = direction(x, y, closest->x, closest->y); // this is the angle you want
}

Let me know if you have any questions.