Research Article

Artificial Intelligence in Video Games: Towards a Unified Framework

Algorithm 6

Avoiding enemy attacks by staying out of range. This function can be used for kiting.
void  stay_safe(Unit  u, UnitList  enemies)
{
UnitList threats;
UnitList::iterator e;
 //Iterate on enemies to detect immediate threats
for  (e = enemies.begin(); e != enemies.end(); ++e)
 {
  //Ignore enemies that can't be outrun
  if  (u->maxspeed() > (e)->maxspeed() &&
   distance(u->position(), (e)->position() + (e)
    ->velocity()) <= (e)->maxrange())
   threats.add(e);
 }
 //Get the center of the threats
Vector c = center(threats);
 //If the unit is located at the center, drop one of the threats
if  (c == u->position())
  c = center(remove_weakest(threats));
 //Create a force that pulls the unit away from the center
Vector dir = u->position() − c;
 //Add a steering force of maximum magnitude
u->addforce(diru->maxforce()/dir.norm());
}