Ok, I was tired yesterday. Took me much less time to unravel some parts of AI coding. And myself I've learnt new stuff...very interesting stuff that can be used in practice...
Once again, I recall that unit fortification bonuses has nothing to do with it and again, that bug I called sheep's clothing is prolly behing the AI missing an easy retaliation.
If I DoW and land next to the city without capturing/killing any unit, then I get what is expected: a retaliation. Not from the UNITAI_ATTACK archer, but from the UNITAI_CITY_DEFENSE archer. IIRC,
Sirian, one of the dev behind CIV, said that CIV AI compared to CivIII AI is less prone to let human pillage everything. Along his colleagues, AI units defending cities are implemented in such way they react to weak pillagings units, but remain behind the city walls when strong forces like stacks come along. For attacking weak unit, looking at the precise script for UNITAI_CITY_DEFENSE, leaving a city for getting rid of a weak pest is high in defense archer priorities (see the bolded function):
Adding to that, Shaka has his units to be pretty courageous (unit courage; yes, it is a real parameter), thus indulging less favorable battles compared to typical AI's like Freddy, Ramesses, Hatshe, etc. My article lists unit courage btw.
BTW, AI units actions, in the code POW, is simply a list of conditional structure read from top to bottom and according to the situation, the first corresponding to the circumstance accepted by the unit is taken and proceeded during this turns (or many turns if it happens to be a mission).
Now, why that UNITAI_ATTACK archer moves to and fro around the city; well, I haven't found the exact explanation yet, but it seems MISSIONAI_EXPLORE (to be an explorer mission) is attributed to the archer, when given the city is needing 2 defenders, thus the attacking archer is called back.
Indeed, the units logistics work in term of need.
Exploration units, workers and defenders for instance, are listed in terms of needed/have. If the city needs some worker/defenders/exploration units, the city either builds them or call a nearby existing unit to do the job.
What attracted my attention while looking at the AI shuffling its units is at some point, that UNITAI_ATTACK archer quits the city for exploration. My first instinct: the city no longer needs two minimal defenders.
And thus comes the concept of minimum defenses in a city. In the code, the function dealing with this is CvCityAI::
AI_neededDefenders() and I got interesting insights about it:
- Minimum of base is indeed 1 defender.
- Min # of defenders is also era dependant. Ancient AI needs only 1 defenfer in its cities and one more each era. This an AI gunning IW will move the next era faster and get its cities more defended. And by min defenses, we mean defenses that won't move out.
- Coastal cities are a bit more defended.
- A capital has one more defender by default.
- Cities with an active WW will have one more defender.
- Cities, for the first 9 turns, will have a needed min defense of the default+era#+one more to defend recent cities.
- More conditions I won't lay down unless asked.
What I saw, after 10 turns, that UNITAI_ATTACK was free to explore because he was no longer bound to the city needed 2 defenders since the 9 turns counter has expired.
The capital still needed 2 min defenders for being a capital.
So basically, that UNITAI_ATTACK archer did not return to the city because of the recent invasion of yours, but because he was bound to the city until the counter of 9 turns finished.
I let a bunch of images to show some imagery.
N=Needed defenders.
The city needed two defenders to protect a recent city.
That attack archer returns given the city needs. A: attack. D: defense.
The 9 turns counter has expired (now 10 turns since the city foundation), thus that UNITAI_ATTACK archer is free. See, the city no longer needs 2 defenders, but only one now.
I show you Shaka explorations. By some internal calculi, the AI deems exploration is insufficient (btw, the AI settles its cities thanks to explorations, it doesn't know the map as it has some omniscience; thus exploration is important for an impotent AI) and thus that leads that UNITAI_ATTACK archer to exploration that fog of war near us.
The capital is nonetheless founded for over 9 turns. Why it still needs two defenders? Well, it's a capital.
EDIT: I'll come with the pictures later. An error occured.