The AI script doesn't always work well when it reacts to being badly hurt. I've tried scripts now where the AI would use a healing potion whenever the character is hurt, and several times now, characters have died. Some enemies just deals too much damage per round.
I think that if you're going to make an AI script, it needs to be adapted to the difficulty level. On impossible, basically, you'd have to run away from every encounter unless you have magical protection or very high hp, like 100+ hp and helmet to avoid critical hits.
Example 1: Melee attack. A level 7 fighter with strength potion could make a critical hit with up to 40-50 damage on Core difficulty. That would be 80-100 damage on insane difficulty. One can halve that damage by wearing a helmet, but I don't think that everyone can wear helmet that protects against critical hits. My thief is usually unprotected from critical hits.
Another thing to consider is that there are several attacks in one round and there could be several enemies beating on a party member, and a script probably works best if it reacts to a round of beating and not a single blow, because then it could be too late to do anything.
Example 2: Ranged attacks. Ranged weapons deals much less damage, but you can't easily run away from missiles which makes these attacks still very dangerous.
Example 3. Magic attacks. A single fireball could do up to 60 damage on core difficulty at 30 yards. A skull trap does even more but at 20 yards. Basically, a character can never feel safe against casters unless they're protected from all elements and magic damage, and save vs all types of effects. If you install mods, the spell casters are also extremely dangerous because the AI has improved. Being too cautious however means half of the party will not become too defensive. It's do or die versus mages.
Example 4: Backstabs. There is no option to stay in combat and then to simply run away if one is too damaged, because one backstab can deal up to maybe 60-70 damage on core difficulty. A script can not do much other than to meet offense with offense. In a casual game, I would just summon something close by and keep my mages/bards in the front lines to absorb the melee damage. That's hard to do in a script, but it's easy to summon creatures.
In conclusion, in a script, it should be possible to react to being hurt or attacked, but only if the attacker uses physical attacks. Then there are at least 4 factors to consider: Damage, # attacks, THAC0 and AC.
I would calculate the max damage that an enemy does by checking how strong it is. because strength increases damage by up to +14 damage at 25 strength. Some weapons, proficiency skills, special abilities also add damage bonus, but most of the bonus comes from strength. In a script , 3 categories should be sufficient:
=0, <=7 and <=14 added damage from strength, i.e. <16 str, <20 str.and >=20 str.
Ranged attacks don't get a lot of damage bonus, so you could add maybe 4 damage to the missile that deals most damage, i.e. bolt, and expect up to 12 damage on core difficulty.
Then a "safe HP" can be calculated using the Binomial Distribution and its Expected value as a multiplier to max damage (but never below 1). Anyone who read a Probability course knows that E[X] = np where n=# of attacks and p=probability to hit. To calculate p we need THACO and AC. Unfortunately, Infinity Engine doesn't allow us calculate the probability, but by categorizing the AC and THAC0 by, say, 4 each we could do it beforehand.
The "safe HP" can then be reduced by armor of faith, Hardiness and barbarian bonus.
Finally, count the number of enemies attacking and increase the "safe HP" by their max damage, like above.
Post edited February 05, 2013 by potato_head