So I have been wondering: where in the code is the part where the Enemy AI decides that the “player” is too far away to shoot, so it will decide to move closer? I have been searching for awhile; I might have just forgotten where or just missed it. I just needed a bit of clarification.
This is actually the side effect of a number of sections of code.
First, on each Action installed on the player is asked for GetBestEnemyAIAction. This method is actually contained in BaseAction
public EnemyAIAction GetBestEnemyAIAction()
{
List<EnemyAIAction> enemyAIActionList = new List<EnemyAIAction>();
List<GridPosition> validActionGridPositionList = GetValidActionGridPositionList();
foreach (GridPosition gridPosition in validActionGridPositionList)
{
EnemyAIAction enemyAIAction = GetEnemyAIAction(gridPosition);
enemyAIActionList.Add(enemyAIAction);
}
if (enemyAIActionList.Count > 0)
{
enemyAIActionList.Sort((EnemyAIAction a, EnemyAIAction b) => b.actionValue - a.actionValue);
if (enemyAIActionList[0].actionValue <= 0)
{
return enemyAIActionList[Random.Range(0, enemyAIActionList.Count)];
}
return enemyAIActionList[0];
} else
{
// No possible Enemy AI Actions
return null;
}
}
What this method does is to move through the Action’s GetValidActinoPositionList and assign each position a score. The AI polls all Actions on the Unit, regardless of whether or not the Action is the best action available, so whether a GridPosition can be shot or not, the MoveAction will evaluate all spots and get scores for each valid location it can move to.
So if ShootAction doesn’t return any actions worth shooting at (i.e. no targets in range), then MoveAction may have a location queued up that could allow a shot, and that would be the selected action instead.
Thank you very much for the clarification!
The only issue is when Enemy is too far too shoot and to get in a shoot range in a single turn. Then it just spins.