If you have trouble reading that (As do I), essentially it checks that it is actually under fire (no point retreating if it isn't) then it splits into the 3 supported bot classes: Sniper on left, which always retreats if it has <75% shield, machine gun in the middle, which retreats at 25% shield when HP is 75% or less, then 50% shield when HP is 50% or less then 75% shield when HP is 25% or less. This has the effect that when the bot is more damaged it is much more cautious and therefore less likely to take additional damage, while simultaneously standing it's ground as much as possible. The assault retreats at 50% shield when HP is >50% and at 75% shield when HP is < 50%.
It is mostly built for assault and machine guns, in fact there is not support for Shotguns at all in this version. I just got the game literally about 12 hours ago but this bit of code has greatly improved my combat. Let me know what you think about my version or any problems, and also do you use a similar block of code as well or do you have some other strategy to decide when to retreat?