IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  HPPercentLT(Myself,40)
  HaveSpell(CLERIC_CURE_SERIOUS_WOUNDS)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(Myself,CLERIC_CURE_SERIOUS_WOUNDS)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  HPPercentLT(Myself,60)
  HaveSpell(PALADIN_LAY_ON_HANDS)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(Myself,PALADIN_LAY_ON_HANDS)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  HPPercentLT(Myself,60)
  HaveSpell(CLERIC_CURE_MEDIUM_WOUNDS)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(Myself,CLERIC_CURE_MEDIUM_WOUNDS)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(NearestMyGroupOfType)
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HPPercentLT(LastSeenBy(Myself),30)
  HaveSpell(CLERIC_CURE_SERIOUS_WOUNDS)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(LastSeenBy(Myself),CLERIC_CURE_SERIOUS_WOUNDS)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(NearestMyGroupOfType)
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HPPercentLT(LastSeenBy(Myself),50)
  HaveSpell(PALADIN_LAY_ON_HANDS)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(LastSeenBy(Myself),PALADIN_LAY_ON_HANDS)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(NearestMyGroupOfType)
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HPPercentLT(LastSeenBy(Myself),50)
  HaveSpell(CLERIC_CURE_MEDIUM_WOUNDS)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(LastSeenBy(Myself),CLERIC_CURE_MEDIUM_WOUNDS)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(NearestEnemyOf(Myself))
  HaveSpell(CLERIC_ARMOR_OF_FAITH)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(Myself,CLERIC_ARMOR_OF_FAITH)
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(NearestEnemyOf(Myself))
  HaveSpell(CLERIC_DRAW_UPON_HOLY_MIGHT)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(Myself,CLERIC_DRAW_UPON_HOLY_MIGHT)
END

IF
  RandomNum(6,1)
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(NearestEnemyOf(Myself))
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HaveSpell(CLERIC_DOOM)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(NearestEnemyOf(Myself),CLERIC_DOOM)
END

IF
  RandomNum(6,2)
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(SecondNearestEnemyOf(Myself))
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HaveSpell(CLERIC_DOOM)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(SecondNearestEnemyOf(Myself),CLERIC_DOOM)
END

IF
  RandomNum(6,3)
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(ThirdNearestEnemyOf(Myself))
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HaveSpell(CLERIC_DOOM)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(ThirdNearestEnemyOf(Myself),CLERIC_DOOM)
END

IF
  RandomNum(6,4)
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(FourthNearestEnemyOf(Myself))
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HaveSpell(CLERIC_DOOM)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(FourthNearestEnemyOf(Myself),CLERIC_DOOM)
END

IF
  RandomNum(6,5)
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(FifthNearestEnemyOf(Myself))
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HaveSpell(CLERIC_DOOM)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(FifthNearestEnemyOf(Myself),CLERIC_DOOM)
END

IF
  RandomNum(6,6)
  !GlobalTimerNotExpired("Cast","LOCALS")
  See(TenthNearestEnemyOf(Myself))
  !StateCheck(LastSeenBy(Myself),STATE_IMPROVEDINVISIBILITY)
  HaveSpell(CLERIC_DOOM)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    Spell(TenthNearestEnemyOf(Myself),CLERIC_DOOM)
END

IF
  !GlobalTimerNotExpired("Turning","LOCALS")
  !GlobalTimerNotExpired("Cast","LOCALS")
  See([GOODCUTOFF.UNDEAD])
  Allegiance(Myself,ENEMY)
  !InWeaponRange(NearestEnemyOf(Myself))
  !AttackedBy([ANYONE],DEFAULT)
  !HitBy([ANYONE],CRUSHING)
THEN
  RESPONSE #100
    SetGlobalTimer("Turning","LOCALS",6)
    SetGlobalTimer("Cast","LOCALS",6)
    Turn()
END

IF
  !GlobalTimerNotExpired("Turning","LOCALS")
  !GlobalTimerNotExpired("Cast","LOCALS")
  See([EVILCUTOFF.UNDEAD])
  Allegiance(Myself,GOODCUTOFF)
  !InWeaponRange(NearestEnemyOf(Myself))
  !AttackedBy([ANYONE],DEFAULT)
  !HitBy([ANYONE],CRUSHING)
THEN
  RESPONSE #100
    SetGlobalTimer("Turning","LOCALS",6)
    SetGlobalTimer("Cast","LOCALS",6)
    Turn()
END

IF
  OR(3)
    !See(LastAttackerOf(Myself))
    StateCheck(LastAttackerOf(Myself),STATE_HELPLESS)
    RandomNumGT(6,3)
  See(NearestEnemyOf(Myself))
  OR(2)
    StateCheck(NearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,3)
  See(SecondNearestEnemyOf(Myself))
  OR(2)
    StateCheck(SecondNearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,4)
  See(ThirdNearestEnemyOf(Myself))
  OR(2)
    StateCheck(ThirdNearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,5)
  See(FourthNearestEnemyOf(Myself))
  OR(2)
    StateCheck(FourthNearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,3)
  See(FifthNearestEnemyOf(Myself))
  OR(2)
    StateCheck(FifthNearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,3)
  See(SixthNearestEnemyOf(Myself))
  OR(2)
    StateCheck(SixthNearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,3)
  See(TenthNearestEnemyOf(Myself))
  OR(2)
    StateCheck(TenthNearestEnemyOf(Myself),STATE_HELPLESS)
    RandomNumGT(10,3)
  !See(NearestEnemyOfType([GOODCUTOFF.0.0.SORCERER]))
  !See(NearestEnemyOfType([GOODCUTOFF.0.0.LONG_BOW]))
  !See(NearestEnemyOfType([GOODCUTOFF.0.0.CLERIC_ALL]))
  !See(NearestEnemyOfType([GOODCUTOFF.0.0.BARD_ALL]))
  !See(NearestEnemyOfType([GOODCUTOFF.0.0.DRUID_ALL]))
  See(NearestEnemyOf(Myself))
  False()
THEN
  RESPONSE #100
    Continue()
END

IF
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  !InWeaponRange(LastSeenBy(Myself))
THEN
  RESPONSE #100
    EquipRanged()
    Continue()
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  InWeaponRange(LastSeenBy(Myself))
THEN
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",0)
    Attack(LastSeenBy(Myself))
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",0)
    Continue()
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  OR(2)
    !Range(LastSeenBy(Myself),5)
    Range(LastSeenBy(Myself),5)
  OR(2)
    Range(NearestEnemyOf(Myself),5)
    Range(LastSeenBy(Myself),5)
THEN
  RESPONSE #100
    EquipMostDamagingMelee()
    Attack(NearestEnemyOf(Myself))
  RESPONSE #100
    Continue()
END

IF
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  RandomNum(4,1)
  Global("D0_MOVE","LOCALS",0)
  GlobalTimerExpired("D0_ATTACK","LOCALS")
THEN
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",1)
    RandomWalkContinuous()
END

IF
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  RandomNum(4,2)
  Global("D0_MOVE","LOCALS",0)
  GlobalTimerExpired("D0_ATTACK","LOCALS")
THEN
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",1)
    RandomWalkContinuous()
END

IF
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  RandomNum(4,3)
  Global("D0_MOVE","LOCALS",0)
  GlobalTimerExpired("D0_ATTACK","LOCALS")
THEN
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",1)
    RandomWalkContinuous()
END

IF
  See(LastSeenBy(Myself))
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  RandomNum(4,4)
  Global("D0_MOVE","LOCALS",0)
  GlobalTimerExpired("D0_ATTACK","LOCALS")
THEN
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",1)
    RandomWalkContinuous()
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  !HaveSpell(CLERIC_DOOM)
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  See(LastSeenBy(Myself))
  !Range(NearestEnemyOf(Myself),7)
THEN
  RESPONSE #100
    SetGlobal("D0_MOVE","LOCALS",0)
    EquipRanged()
    Attack(LastSeenBy(Myself))
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  !HaveSpell(CLERIC_DOOM)
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  See(LastSeenBy(Myself))
  Range(LastSeenBy(Myself),7)
THEN
  RESPONSE #100
    EquipMostDamagingMelee()
    Attack(LastSeenBy(Myself))
END

IF
  !GlobalTimerNotExpired("Cast","LOCALS")
  HaveSpell(CLERIC_DOOM)
  !GlobalGT("D0HasNotCast","LOCALS",2)
THEN
  RESPONSE #100
    IncrementGlobal("D0HasNotCast","LOCALS",1)
END

IF
  OR(2)
    HaveSpell(CLERIC_DOOM)
    GlobalTimerNotExpired("Turning","LOCALS")
  !GlobalGT("D0HasNotCast","LOCALS",2)
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  See(NearestEnemyOf(Myself))
  RandomNum(4,1)
THEN
  RESPONSE #100
    NoAction()
  RESPONSE #100
    RandomWalk()
END

IF
  OR(2)
    HaveSpell(CLERIC_DOOM)
    GlobalTimerNotExpired("Turning","LOCALS")
  !GlobalGT("D0HasNotCast","LOCALS",2)
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  See(NearestEnemyOf(Myself))
  RandomNum(4,2)
THEN
  RESPONSE #100
    NoAction()
  RESPONSE #100
    RandomWalk()
END

IF
  OR(2)
    HaveSpell(CLERIC_DOOM)
    GlobalTimerNotExpired("Turning","LOCALS")
  !GlobalGT("D0HasNotCast","LOCALS",2)
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  See(NearestEnemyOf(Myself))
  RandomNum(4,3)
THEN
  RESPONSE #100
    NoAction()
  RESPONSE #100
    RandomWalk()
END

IF
  OR(2)
    HaveSpell(CLERIC_DOOM)
    GlobalTimerNotExpired("Turning","LOCALS")
  !GlobalGT("D0HasNotCast","LOCALS",2)
  Allegiance(Myself,ENEMY)
  Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  See(NearestEnemyOf(Myself))
  RandomNum(4,4)
THEN
  RESPONSE #100
    NoAction()
  RESPONSE #100
    RandomWalk()
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  See(NearestEnemyOf(Myself))
  !Range(NearestEnemyOf(Myself),5)
THEN
  RESPONSE #100
    EquipRanged()
    AttackReevaluate(NearestEnemyOf(Myself),30)
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  See(NearestEnemyOf(Myself))
  Range(NearestEnemyOf(Myself),5)
THEN
  RESPONSE #100
    EquipMostDamagingMelee()
    AttackReevaluate(NearestEnemyOf(Myself),30)
END

IF
  OR(5)
    InWeaponRange(NearestEnemyOf(Myself))
    AttackedBy([ANYONE],DEFAULT)
    HitBy([ANYONE],CRUSHING)
    !GlobalTimerNotExpired("Turning","LOCALS")
    !See([0.UNDEAD])
  AttackedBy([ANYONE],DEFAULT)
THEN
  RESPONSE #100
    AttackReevaluate(LastAttackerOf(Myself),30)
END
