IF
  HPLT(Myself,20)
  Global("Death","LOCALS",0)
THEN
  RESPONSE #100
    ClearAllActions()
    Wait(1)
    SetGlobal("Death","LOCALS",1)
    StartDialogNoSet(Player1)
END

IF
  OnCreation()
THEN
  RESPONSE #100
    Face(0)
END

IF
  See(NearestEnemyOf(Myself))
  !GlobalTimerNotExpired("Prepare2","LOCALS")
  HPGT(Myself,1)
THEN
  RESPONSE #100
    SetGlobalTimer("Prepare2","LOCALS",1000)
    DisplayString(Myself,73139)
    SetGlobalTimer("ElectrifiedFloorTimer","AR6005",6)
    SetGlobalTimer("TailSlap","AR6005",12)
    ApplySpell(Myself,DRAGON_STONE_SKIN)
    ReallyForceSpell(Myself,BEHOLDER_SPELL_TURNING)
    ReallyForceSpell(NearestEnemyOf(Myself),2302)
END

IF
  See(NearestEnemyOf(Myself))
  !GlobalTimerNotExpired("SpellTrigger1","LOCALS")
  HPGT(Myself,1)
THEN
  RESPONSE #100
    SetGlobalTimer("SpellTrigger1","LOCALS",1000)
    DisplayString(Myself,39968)
    ReallyForceSpell(Myself,WIZARD_IMPROVED_HASTE)
    ReallyForceSpell(Myself,HEAL_NO_VISUAL)
    ReallyForceSpell(Myself,PROTECTION_MAGIC_WEAPON_NOEFF)
    ReallyForceSpell(Myself,SHADOW_DRAGON_SHADOWS)
END

IF
  See(NearestEnemyOf(Myself))
  !GlobalTimerNotExpired("DRAGONFEAR","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("DRAGONFEAR","LOCALS",100)
    PlaySound("ABAZIG02")
    ReallyForceSpell(NearestEnemyOf(Myself),DRAGON_FEAR)
    ReallyForceSpell(NearestEnemyOf(Myself),DRAGON_LOWER_ELECTRICAL)
    ForceSpell(LastSeenBy(Myself),DRAGON_WING_BUFFET)
END

IF
  OR(3)
    SpellCast([GOODCUTOFF],WIZARD_CLOUDKILL)
    SpellCast([GOODCUTOFF],WIZARD_INCENDIARY_CLOUD)
    SpellCast([GOODCUTOFF],WIZARD_DEATH_FOG)
  !GlobalTimerNotExpired("WINGBUFFET","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("WINGBUFFET","LOCALS",8)
    ForceSpell(NearestEnemyOf(Myself),DRAGON_WING_BUFFET)
    Wait(1)
    ApplySpell(Myself,CLERIC_ZONE_OF_SWEET_AIR)
END

IF
  Allegiance(Myself,ENEMY)
  !GlobalTimerNotExpired("DragonStoneSkin","LOCALS")
  RandomNum(2,1)
THEN
  RESPONSE #100
    SetGlobalTimer("DragonStoneSkin","LOCALS",30)
    ForceSpell(Myself,DRAGON_STONE_SKIN)
END

IF
  See(NearestEnemyOf(Myself))
  CheckStatLT(Myself,50,RESISTMAGIC)
  RandomNum(3,1)
  !GlobalTimerNotExpired("RegainMagic","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("RegainMagic","LOCALS",15)
    ApplySpell(Myself,WARRIOR_RESIST_MAGIC)
END

IF
  !HasBounceEffects(Myself)
  OR(3)
    See([PC.0.0.BARD_ALL])
    See([PC.0.0.MAGE_ALL])
    See([PC.0.0.SORCERER])
  !StateCheck(LastSeenBy(Myself),STATE_SILENCED)
  RandomNumLT(4,3)
  !GlobalTimerNotExpired("SpellTurn","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("SpellTurn","LOCALS",15)
    ForceSpell(Myself,BEHOLDER_SPELL_TURNING)
END

IF
  !Allegiance(Myself,ENEMY)
THEN
  RESPONSE #100
END

IF
  See(NearestEnemyOf(Myself))
  HPGT(LastSeenBy(Myself),0)
THEN
  RESPONSE #100
    Continue()
END

IF
  RandomNumLT(31,21)
  OR(6)
    See(NearestEnemyOf(Myself))
    See(WeakestOf)
    See(WorstAC)
    See(MostDamagedOf)
    See(LastAttackerOf(Myself))
    See(LastHitter(Myself))
  RandomNumLT(28,22)
  OR(4)
    See([GOODCUTOFF.0.0.0.0.SUMMONED_DEMON])
    See([GOODCUTOFF.0.0.0.0.SUMMONED])
    See([GOODCUTOFF.0.0.0.207])
    See([GOODCUTOFF.0.0.0.0.ILLUSIONARY])
  RandomNumLT(29,23)
  OR(7)
    See([PC.0.0.RANGER_ALL])
    See([PC.0.0.PALADIN_ALL])
    See([PC.0.0.BARD_ALL])
    See([PC.0.0.DRUID_ALL])
    See([PC.0.0.CLERIC_ALL])
    See([PC.0.0.MAGE_ALL])
    See([PC.0.0.SORCERER])
  RandomNumLT(30,16)
  See(Protagonist)
  RandomNumLT(26,10)
  OR(4)
    See([GOODCUTOFF.0.0.RANGER_ALL])
    See([GOODCUTOFF.0.0.DRUID_ALL])
    See([GOODCUTOFF.0.0.PALADIN_ALL])
    See([GOODCUTOFF.0.0.CLERIC_ALL])
  RandomNumLT(27,10)
  OR(3)
    See([GOODCUTOFF.0.0.MONK])
    See([GOODCUTOFF.0.0.BARD_ALL])
    See([GOODCUTOFF.0.0.THIEF_ALL])
  RandomNumLT(24,9)
  OR(3)
    See(StrongestOf)
    See(BestAC)
    See(LeastDamagedOf)
  RandomNumLT(25,9)
  See(WeakestOf)
  RandomNumLT(23,8)
  See(WorstAC)
  RandomNumLT(21,8)
  See(MostDamagedOf)
  RandomNumLT(32,11)
  OR(2)
    See(TenthNearest([GOODCUTOFF.0.0.MAGE_ALL]))
    See(TenthNearest([GOODCUTOFF.0.0.SORCERER]))
  RandomNumLT(20,8)
  See(TenthNearestEnemyOf(Myself))
  False()
THEN
  RESPONSE #100
    Continue()
END

IF
  !Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  HPPercentLT(Myself,35)
THEN
  RESPONSE #100
    SpellNoDec(Myself,CLERIC_HEAL)
END

IF
  !Allegiance(LastSeenBy(Myself),GOODCUTOFF)
  HPPercentGT(Myself,50)
THEN
  RESPONSE #100
    MoveToObject(Player1)
END

IF
  GlobalTimerExpired("TailSlap","AR6005")
  Range(LastSeenBy(Myself),20)
  !StateCheck(LastSeenBy(Myself),STATE_SLEEPING)
THEN
  RESPONSE #100
    SetGlobalTimer("TailSlap","AR6005",30)
    SwingOnce()
    DisplayString(Myself,74022)
    ForceSpell(LastSeenBy(Myself),CLERIC_EARTHQUAKE)
END

IF
  GlobalTimerExpired("ElectrifiedFloorTimer","AR6005")
THEN
  RESPONSE #100
    SetGlobalTimer("ElectrifiedFloorTimer","AR6005",21)
    DisplayString(Myself,72905)
    PlaySound("am6005e")
    ForceSpell(Myself,ABAZIGAL_SHOCKWAVE)
END

IF
  !GlobalTimerNotExpired("WINGBUFFET","LOCALS")
  Range(LastSeenBy(Myself),10)
  RandomNumLT(6,4)
THEN
  RESPONSE #100
    SetGlobalTimer("WINGBUFFET","LOCALS",12)
    ForceSpell(LastSeenBy(Myself),DRAGON_WING_BUFFET)
    Wait(1)
END

IF
  !GlobalTimerNotExpired("WINGBUFFET","LOCALS")
  Range(SecondNearestEnemyOf(Myself),10)
  RandomNumLT(7,4)
THEN
  RESPONSE #100
    SetGlobalTimer("WINGBUFFET","LOCALS",12)
    ForceSpell(NearestEnemyOf(Myself),DRAGON_WING_BUFFET)
    Wait(1)
END

IF
  !Range(LastSeenBy(Myself),4)
  CheckStatLT(LastSeenBy(Myself),100,RESISTELECTRICITY)
  !GlobalTimerNotExpired("BLUEDRAGONBREATH","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("BLUEDRAGONBREATH","LOCALS",12)
    ForceSpell(LastSeenBy(Myself),BLUE_DRAGON_BREATH)
    ReallyForceSpellRES("ABAZ02",LastSeenBy(Myself))
END

IF
  CheckStatGT(LastSeenBy(Myself),4,NUMBEROFATTACKS)
  !StateCheck(LastSeenBy(Myself),STATE_STUNNED)
  !StateCheck(LastSeenBy(Myself),STATE_HELPLESS)
  Range(LastSeenBy(Myself),10)
  RandomNumLT(8,5)
  !GlobalTimerNotExpired("SlowDown","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("SlowDown","LOCALS",12)
    ForceSpell(LastSeenBy(Myself),WIZARD_MAZE)
  RESPONSE #100
    SetGlobalTimer("SlowDown","LOCALS",18)
    ForceSpell(LastSeenBy(Myself),WIZARD_BIGBYS_CLENCHED_FIST)
END

IF
  Gender(LastSeenBy(Myself),SUMMONED)
  CheckStatGT(LastSeenBy(Myself),90,RESISTSLASHING)
  CheckStatLT(LastSeenBy(Myself),90,MAGICDAMAGERESISTANCE)
  RandomNumLT(9,5)
  !GlobalTimerNotExpired("Mordenkainen","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("Mordenkainen","LOCALS",6)
    ForceSpell(LastSeenBy(Myself),WIZARD_MAGIC_MISSILE)
  RESPONSE #100
    SetGlobalTimer("Mordenkainen","LOCALS",6)
    ForceSpell(LastSeenBy(Myself),WIZARD_DISINTEGRATE)
END

IF
  ActionListEmpty()
  Gender(LastSeenBy(Myself),SUMMONED)
  !CheckStatGT(LastSeenBy(Myself),25,INT)
  !LevelGT(LastSeenBy(Myself),12)
  RandomNumLT(10,6)
  !GlobalTimerNotExpired("Death","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("Death","LOCALS",30)
    ForceSpell(LastSeenBy(Myself),WIZARD_DEATH_SPELL)
END

IF
  ActionListEmpty()
  OR(2)
    StateCheck(LastSeenBy(Myself),STATE_HASTED)
    CheckStatGT(LastSeenBy(Myself),4,NUMBEROFATTACKS)
  CheckStatGT(LastSeenBy(Myself),17,STR)
  HPPercentLT(Myself,100)
  !StateCheck(LastSeenBy(Myself),STATE_STUNNED)
  !StateCheck(LastSeenBy(Myself),STATE_HELPLESS)
  Range(LastSeenBy(Myself),10)
  Global("Imprison","LOCALS",0)
THEN
  RESPONSE #100
    SetGlobal("Imprison","LOCALS",1)
    ForceSpell(LastSeenBy(Myself),WIZARD_IMPRISONMENT)
END

IF
  ActionListEmpty()
  Exists(LastAttackerOf(Myself))
  OR(8)
    !See([GOODCUTOFF])
    !See(LastAttackerOf(Myself))
    StateCheck(NearestEnemyOf(Myself),STATE_MIRRORIMAGE)
    StateCheck(NearestEnemyOf(Myself),0x60400010)
    StateCheck(NearestEnemyOf(Myself),STATE_BLUR)
    Gender(NearestEnemyOf(Myself),ILLUSIONARY)
    Specifics(NearestEnemyOf(Myself),207)
    Specifics(NearestEnemyOf(Myself),205)
  RandomNumLT(11,6)
THEN
  RESPONSE #100
    SetGlobalTimer("TrueSight","LOCALS",60)
    ForceSpell(Myself,WIZARD_TRUE_SIGHT)
END

IF
  RandomNum(12,5)
  ActionListEmpty()
  !GlobalTimerNotExpired("dbMalison","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("dbMalison","LOCALS",1000)
    ForceSpell(LastSeenBy(Myself),WIZARD_GREATER_MALISON)
END

IF
  ActionListEmpty()
  OR(6)
    Class(LastSeenBy(Myself),PALADIN_ALL)
    Class(LastSeenBy(Myself),DRUID_ALL)
    Class(LastSeenBy(Myself),CLERIC_ALL)
    Class(LastSeenBy(Myself),BARD_ALL)
    Class(LastSeenBy(Myself),MAGE_ALL)
    Class(LastSeenBy(Myself),SORCERER)
  !StateCheck(LastSeenBy(Myself),STATE_SILENCED)
  RandomNumLT(13,5)
  !HasBounceEffects(LastSeenBy(Myself))
  !GlobalTimerNotExpired("dbSilence","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("dbSilence","LOCALS",15)
    ForceSpell(LastSeenBy(Myself),DRAGON_SILENCE)
END

IF
  ActionListEmpty()
  RandomNumLT(14,6)
  !GlobalTimerNotExpired("dbDispel","LOCALS")
  !StateCheck(LastSeenBy(Myself),STATE_SILENCED)
  !StateCheck(LastSeenBy(Myself),STATE_STUNNED)
  !StateCheck(LastSeenBy(Myself),STATE_SLEEPING)
  OR(11)
    HasImmunityEffects(LastSeenBy(Myself))
    HasBounceEffects(LastSeenBy(Myself))
    StateCheck(LastSeenBy(Myself),STATE_BLESS)
    StateCheck(LastSeenBy(Myself),STATE_HASTED)
    StateCheck(LastSeenBy(Myself),STATE_LUCK)
    StateCheck(LastSeenBy(Myself),STATE_CHANT)
    StateCheck(LastSeenBy(Myself),STATE_DRAWUPONHOLYMIGHT)
    StateCheck(LastSeenBy(Myself),STATE_BLUR)
    StateCheck(LastSeenBy(Myself),STATE_MIRRORIMAGE)
    CheckStatGT(LastSeenBy(Myself),0,STONESKINS)
    CheckStatGT(LastSeenBy(Myself),0,125)
THEN
  RESPONSE #100
    SetGlobalTimer("dbDispel","LOCALS",60)
    ForceSpell(LastSeenBy(Myself),2302)
END

IF
  ActionListEmpty()
  RandomNumLT(15,6)
  OR(13)
    CheckStatGT(LastSeenBy(Myself),0,178)
    CheckStatGT(LastSeenBy(Myself),50,RESISTMAGICFIRE)
    CheckStatGT(LastSeenBy(Myself),50,RESISTMAGICCOLD)
    CheckStatGT(LastSeenBy(Myself),50,RESISTACID)
    CheckStatGT(LastSeenBy(Myself),50,RESISTELECTRICITY)
    CheckStatGT(LastSeenBy(Myself),0,126)
    CheckStatGT(LastSeenBy(Myself),0,130)
    CheckStatGT(LastSeenBy(Myself),0,163)
    CheckStatGT(LastSeenBy(Myself),0,179)
    CheckStatGT(LastSeenBy(Myself),0,181)
    CheckStatGT(LastSeenBy(Myself),0,128)
    CheckStatGT(LastSeenBy(Myself),3,STONESKINS)
    CheckStatGT(LastSeenBy(Myself),0,SCRIPTINGSTATE5)
  !GlobalTimerNotExpired("dbSecret","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("dbSecret","LOCALS",30)
    ForceSpell(LastSeenBy(Myself),WIZARD_SECRET_WORD)
  RESPONSE #50
    SetGlobalTimer("dbSecret","LOCALS",30)
    ForceSpell(LastSeenBy(Myself),WIZARD_BREACH)
  RESPONSE #25
    SetGlobalTimer("dbSecret","LOCALS",30)
    ForceSpell(LastSeenBy(Myself),WIZARD_PIERCE_MAGIC)
END

IF
  CheckStatLT(LastSeenBy(Myself),51,RESISTMAGIC)
  !GlobalTimerNotExpired("Barrage","LOCALS")
  RandomNumLT(16,9)
THEN
  RESPONSE #100
    SetGlobalTimer("Barrage","LOCALS",6)
    ForceSpell(LastSeenBy(Myself),WIZARD_CHAOS)
  RESPONSE #100
    SetGlobalTimer("Barrage","LOCALS",12)
    ForceSpell(LastSeenBy(Myself),WIZARD_MAZE)
  RESPONSE #100
    SetGlobalTimer("Barrage","LOCALS",6)
    ForceSpell(LastSeenBy(Myself),WIZARD_FINGER_OF_DEATH)
  RESPONSE #100
    SetGlobalTimer("Barrage","LOCALS",6)
    ForceSpell(LastSeenBy(Myself),WIZARD_SLOW)
  RESPONSE #100
    SetGlobalTimer("Barrage","LOCALS",12)
    ForceSpell(LastSeenBy(Myself),WIZARD_POWER_WORD_BLIND)
  RESPONSE #100
    SetGlobalTimer("Barrage","LOCALS",12)
    ForceSpell(LastSeenBy(Myself),WIZARD_PRISMATIC_SPRAY)
END

IF
  ActionListEmpty()
  HPPercentLT(Myself,30)
  RandomNumLT(17,9)
THEN
  RESPONSE #100
    RunAwayFrom(LastSeenBy(Myself),45)
END

IF
  ActionListEmpty()
THEN
  RESPONSE #100
    AttackReevaluate(LastSeenBy(Myself),30)
END

