Would you be able to mount units on walls? That seems to be more common nowadays, and it is also very cool. But I would completely understand why you might decline. That is probably VERY complex.
One final consideration is the size of the breach. If it wants to attack with units larger than a 1x1, it should create a breach that's 2x2 in size. But now that gets into more details, because that should be different from one faction type to the next (and we're supposed to be designing this engine to work optimally with various mods). Thus, the AI has to determine which units it's attacking with and their max size to know how big of a breach it needs.
Please don't forget that right now the AI attacks all buildings, so you must change the AI in order for the AI to be competent in that way. You definitely don't want an AI held up on an irrelevant section of wall while enemy archers pick of their soldiers.
I have an idea for how the AI would build walls. You mention that they are built randomly, well here is a crude solution:
Some things you must understand first:
1. The AI must have a gate to go through, which would morph between closed and shut.
2. If units stationed on walls is implemented, there must be a stairwell that would allow access to the wall's ramparts, behind the crenellations.
Alright. Now, each wall would have to be built far from the town right? So each building, say one with a size of
x (in this case a building the size 3), must be a
y distance from the wall to allow expansion. So an equation for the AI to decide how far away to build the wall from the town, would be
y=2x+6. The
2x is how far away I think the wall should be built. And the y-intercept is how far away a wall must be no matter what size, because you can't have a 1 tile building with a wall three tiles away! The 6 would prevent this. So each town (concentration of buildings) would have an imaginary green footprint much larger than the towns size. And at the edge of this would go the walls. In this case, the 3-tile building would have a wall 12 tiles away. I think this is reasonable, but walls would not do well on a small map. I know that this is complicated and hard to understand, but I can't explain it better.