The purpose of the AI is to live as long as possible while gathering points. Points can be gathered by reaching checkpoints, picking up items and defeating enemies. The AI makes its decisions using a behavior tree.
The world where the AI lives in is random generated world. Houses are visualized with green lines placed in a rectangular form. Enemies are indicated with a red color and the AI with a green color. In the world there are yellow dots. These yellow dots represent items. The items included are a medkit (restores health), food (restores energy), a pistol (shoot at enemies) and garbage (no effects).
Actions:
- Pick up / Use / Remove items
- Evade / Shoot enemies
- Search houses for items
- memorize items
- Emergency item search
Code:
Github – Source code
House searching algorithm:
When the AI enters a house it calculates the location of the corners. It then decides which corner is the closest to its current location and after that it decides which corner is the furthest. This is then done again ignoring the past corners to determine the order the AI needs to visit the corners.
Evading enemies:
If an enemy enters the field of view of the AI then the AI tries to evade the enemy. This is done by calculating the angle between the AI and the enemy. After calculating the angle the AI sets a target location with the opposite angle at the end of its FOV.
//Calculate the angle between the forward and the directionToEnemy
auto forward = agent.LinearVelocity.GetNormalized();
auto directionToEnemy = (enemy.Location - agent.Position).GetNormalized();
//Make sure the evade is always atleast 30degree to the other side
auto minEvadeAngle = _Pi / 180.0f * minEvadeAngleInDegree;
auto angle = atan2(forward.x*directionToEnemy.y - forward.y*directionToEnemy.x, forward.x*directionToEnemy.x + forward.y*directionToEnemy.y);
//Add a minumum dodge angle
angle += angle > 0 ? minEvadeAngle : -minEvadeAngle;
//Set the angle as max to the FOV angle
if (abs(angle) > agent.FOV_Angle)
{
if (angle < 0)
{
angle = agent.FOV_Angle / 2;
angle *= -1;
}
else
{
angle = agent.FOV_Angle / 2;
}
}
//Invert the angle so we dodge to the other side of the incoming vector
angle *= -1;
//Determine a minimum radius for the target distance
auto minRadius = agent.FOV_Range / 4;
//Set the new target as the forward rotated with the dodge angle
Elite::Vector2 newTarget = { agent.Position.x + (minRadius * forward.x * cos(angle) - minRadius * forward.y * sin(angle)), agent.Position.y + (minRadius * forward.x * sin(angle) + minRadius * forward.y * cos(angle)) };
