AI scripts have very little control over military units and how they fight. There is a separate built-in tactical AI that can't be changed that handles the details of what each unit actually does. Here are the things that an AI script can control with regards to military units:
1. sn-number-explore-groups: It can tell units to explore. It can't choose which units will do the exploring, only the number; it seems to always choose the fastest unit available, usually a scout/light cavalry and then knights. Once a unit is told to explore, it can't be told to stop exploring.
2. sn-task-ungrouped-soldiers: It can tell all units to spread out so that their lines of sight don't overlap.
3. sn-percent-attack-soldiers & attack-now: It can tell a certain percentage of the units to attack the enemy. It can't choose which units will be sent to attack, and it can't choose which enemy to attack if there are more than one.
4. sn-percent-enemy-sighted-response & sn-enemy-sighted-response-distance: It can tell units up to a certain distance away from an ally being attacked by an enemy to help out in the fight. (A military unit will always attack an enemy unit that enters its line of sight.)
"If the units are tasked to a certain area and they meet enemies on the way then they will attack that enemy before continuing on their way." - ES has specifically made it impossible to do this. Sandyman has said that it was designed on purpose to ignore attacks when the goal is to move or attack somewhere else.
"Also, if they are tasked to destroy a building etc but are being attacked then they will battle the enemies units before finishing their original task." - You can do this one by simply tasking the units to move next to the enemy building instead of tasking it to attack the building. They will automatically attack the building once they get there if you leave them on aggressive stance. Since they are not tasked to attack the building, the tactical AI will prefer to attack enemy units over the building but will return to attacking the building whenever no enemy units are in sight.
"If one or two are being attacked then other nearby units will come to their aid." - This is #4 above. The default of sn-enemy-sighted-response-distance is 50 which is also the maximum. The default for sn-percent-enemy-sighted-response is 50, so you might want to change this to 100 to be assured of a response.------------------
Bingmann's Age of Kings AI Scripting Kitchen Party
http://www.crl.com/~xhounds/aok/index.html