Phantom Tactics AI
The general AI routine is to construct a Dijkstra map for the entire map, then use that for each unit to decide on a "best" tile. Dijkstra map means more or less assigning a value to each possible destination that represents how much you want to move there. You start by assigning values to particularly valuable destinations (for example, enemies or allies), then you flood-fill the rest of the area with decreasing numbers, representing that it's more desirable to get closer to good things.
In this particular case, I use two Dijkstra maps: one representing distance to nearest ally and one representing distance to nearest enemy. To store these maps, Phantom Tactics uses a maptile layer with an empty tileset. This frees up your globals for something else, plus you can hold several pathmaps in memory at the same time if you want (I don't). The disadvantage is that you can only use values from 0 to 255; luckily, I don't really care about values higher than 255. Incidentally, this is the step during which "Thinking" is displayed onscreen.
With these maps stored, each unit uses a heuristic to determine its preferred tile. As an easy example, a Cleric will gravitate towards its allies, since it can't attack. A more complicated example is that when a unit is deciding how to group up with its allies, it will prefer to support an ally who is within attack range of an enemy. And the most complicated thing is deciding how to attack.
Due to the combat mechanics of Phantom Tactics, you don't want to move your attacker into position until your supporters are already in place. If a unit is within attack range and the AI decides it's a good idea to attack, the first step is to figure out which unit is the best attacker. There are a lot of considerations here: Who's going to benefit the most from attacking directly? Who's going to take a lot of damage as a direct attacker? How much support will each unit be able to receive if it's the attacker? Will the attacker die? And so on. The AI is very aggressive and will usually fight even when the conditions are unfavorable, but it won't attempt an attack that doesn't damage the target.
Once the attacker is decided, he can't move yet. The supporters need to be brought into position. There's a similar level of complexity here, and even more considerations that the AI doesn't bother with; notably, should we save this unit to perform an attack later. The AI always prefers to stack support units. Fortunately, it will stack the units that are best at supporting: bowmen, for example. This means that if there are extra attackers after this combat resolves, they're more likely to be front-line units anyway. During this phase, the AI is forbidden to move next to an enemy unit -- that would start another combat.
After everyone who's going to fight has fought, the rest of the movement is resolved. The AI prefers to group its units, but if a unit is already grouped with other units that haven't moved yet, it will approach the enemy. The AI will hesitate to enter enemy attack range, preferring to stay just outside unless it's already near the line.
In order for a unit to move to the destination tile, the game calculates the unit's movement range as if it were already there. As discussed in the A* thread, this creates a map of descending values from the point of origin. The unit then traverses that map using the highest-valued adjacent tile.
And now you know everything* you need to know to reproduce the Phantom Tactics AI
*Note: assumes you have taken several years of college-level programming courses
Mega Tact v1.1
Super Penguin Chef
Wizard Blocks