Tactical AI
At first I want to start to improve the tactical AI, i.e. simply moving the AI in a way that their units get in advantageous positions or simply to get their endangered units out of the danger zones.
In order to avoid that the AI does its own unit movements we can set their unit movement to 0 at first, which is an easy task.
Now the first important step for this sub-project is creating a function that actually moves a unit from one field to the another. For this purpose we need a path-finding algorithm, because Firaxis did not provide us with a proper move-unit-to-function. The only proper way to move units is using
Code:
UnitManager.RequestOperation
but it can only be accessed for the local player, which it is not easy
to use for AI players .
Now there is another function called UnitManager.MoveUnit to move units. You can use it like this:
Code:
function MoveUnitToPlot(pUnit, pPlot)
UnitManager.RestoreMovementToFormation(pUnit)
UnitManager.RestoreUnitAttacks(pUnit)
UnitManager.MoveUnit(pUnit, pPlot)
end
However, in my tests this function often cannot find the right path. Also bugs can occur if you want to move units more than one tile away (e.g. the unit graphics can be displayed on a wrong spot).
Luckily the MoveUnit-function works very well, if you only move to an adjacent tile. So if we had a path finding algorithm like
the A* algorihm we would be able to use the MoveUnit-function even for long distances by moving units one tile after the other along the calculated path.
(Btw. my tests confirmed that MoveUnit is able to embark units with the right tech and move them together with ships. Only special operations like unit swapping or attacking cannot be done with MoveUnit.)
Another application of such an path algorithm is moving more than one unit in a formation:
When using our path finding algorithm we can also set which plots may be used and which not. Of course impassable plots like mountains cannot be used by default, but you can also set tiles of an existing path as impassable for any further paths to avoid path crossings.
In the path finding algorithm we can also give plots next to existing paths a higher priority to make units move in a closed formation. It should look something like this:
The orange hex fields in this image are the starting and the ending plot. The first path is the black one in this example. Then we create the two blue paths on the left and on the right side of the black one, and so on. Then we sent units to the starting points of these different paths one by one. Finally these units can start to move along their paths until they reach the enemy.
Only when there are too many obstacles (grey plots) we can allow paths to cross sometimes:
Of course there are more details to think of to make this work properly, but I think I will share my further thoughts on this later, once we actually have a good path finding algorithm.
So here are the first goals for this project:
- Implement a suitable path finding algorithm that does not cost too much performance .
- Decide which plots the AI needs to go to or which plots need to be avoided.
- Reuse the path algorithm in a way that allows units to move in a proper formation.
- Find paths for situations when the enemy is nearby: avoiding exposed units, retreating units, replacing low health units with fresh ones, etc.