multiple targets(enemies)

Posted By: Blobfist

multiple targets(enemies) - 04/21/18 02:18

I am puzzling around with this for quite a while now.

My AI should target the nearest suitable enemy.
The ENTITY of it's enemy is locally stored in the definition "my.enemy".


Code:
function find_enemy()
{
	for(you=ent_next(NULL); you!=NULL; you=ent_next(you))
	{
		if ((you != my) && (you.team != my.team) && (you.health>0))// go through all models with the correct specs
		{
//somehow determine the nearest suitable enemy
			my.enemy = you;//
		}
//////////
//somehow repeat the function, so that the AI can find new targets, if one happens to die or if another is closer.
	}
}

Posted By: Quad

Re: multiple targets(enemies) - 04/22/18 17:58

Assuming the code you posted is working right now (except for nearest part),

what you should do is simple.
set my.enemy to null at the start of the function.

then before the my.enemy = you like check 2 things.

1- if my.enemy is already null set my.enemy to you.
2- if my.enemy is not null, you are going to check if current my.enemey is farther away from new "you". you can use if(vec_dist(me.x,my.enemy.x)>vec_dist(me.x,you.x)) .
if that is true then entity stored in my.enemy is not closer, the one in you is closer so you set my.enemy = you.
Posted By: Blobfist

Re: multiple targets(enemies) - 04/23/18 17:21

Including "my.enemy = NULL;" is what I was missing.

This code works perfectly smoothly tongue

Code:
function find_enemy()
{
	my.enemy = NULL;
	//////////
	ENTITY* Entenemy;
	var next_target;
	//////////
	for(you=ent_next(NULL); you!=NULL; you=ent_next(you))
	{
		if ((you != my) && (you.team != my.team) && (you.health>0))
		{
			if(my.enemy == NULL)
			{  
				my.enemy = you;
				Entenemy = my.enemy;
				next_target = vec_dist(my.x, you.x);
			}
			else
			{
				if(vec_dist(my.x, you.x) < next_target)
				{
					my.enemy = you;
					Entenemy= my.enemy;
					next_target = vec_dist(my.x, you.x);
				}
			}
		}
	}
}




Thank you!
© 2024 lite-C Forums