Improved Action selection

Hi, while I understand that this course is not focused on AI there were a couple of things that bothered me about the current setup.

  1. I do not feel that the Unit should move to the position with the most targets, rather it should move to the position with the least number of targets greater than zero(Try to fight one on one if you can).
    My solution is
    public override EnemyAiAction GetEnemyAiAction(GridPosition gridPosition)
    {
        int targetsAtPosition = unit.GetShootAction().GetTargetCountAtPosition(gridPosition);
        if (targetsAtPosition == 0)
        {
            return new EnemyAiAction
            {
                gridPosition = gridPosition,
                actionValue = 0

            };
        }
        else
        {
            return new EnemyAiAction
            {
                gridPosition = gridPosition,
                actionValue = 50 / targetsAtPosition

            };
        }
    }
  1. Given multiple actions with an equal actionScore the unit will always select the action on the position closest to 0,0 which results in any unit that has no target within range and no move available that will put a target in range it will just move towards 0,0 I would rather select randomly from a list of equally desirable actions like this
    public EnemyAiAction GetBestEnemyAiAction()
    {
        List<EnemyAiAction> enemyAiActionList = new List<EnemyAiAction>();
        List<EnemyAiAction> bestEnemyAiActionList = new List<EnemyAiAction>();

        List<GridPosition> validGridPositionList = GetValidActionGridPositionsList();
        foreach (GridPosition position in validGridPositionList)
        {
            EnemyAiAction enemyAiAction = GetEnemyAiAction(position);
            enemyAiActionList.Add(enemyAiAction);
        }

        if (enemyAiActionList.Count > 0)
        {
            enemyAiActionList.Sort((EnemyAiAction a, EnemyAiAction b) => b.actionValue - a.actionValue);
            foreach(EnemyAiAction enemyAiAction in enemyAiActionList)
            {
                if(enemyAiAction.actionValue == enemyAiActionList[0].actionValue)
                {
                    bestEnemyAiActionList.Add(enemyAiAction);
                }
            }
            if (bestEnemyAiActionList.Count > 0)
            {
                return bestEnemyAiActionList[UnityEngine.Random.Range(0, bestEnemyAiActionList.Count - 1)];
            }
            else { return null; }
        }
        else { return null; }
    }

This seems to work well and results in a random movement as opposed to heading straight to 0,0

Thoughts ???

1 Like

Privacy & Terms