IF
  Global("Prep","LOCALS",0)
  See(NearestEnemyOf(Myself))
THEN
  RESPONSE #100
    SetGlobal("Prep","LOCALS",1)
    SetGlobal("ChainContingency","LOCALS",0)
    SetGlobal("Contingency","LOCALS",0)
    SetGlobal("Sequencer","LOCALS",0)
    SetGlobalTimer("Cast","LOCALS",0)
    SetGlobalTimer("TimeStop","LOCALS",0)
    SetGlobalTimer("YELLOWDRAGONBREATH","GLOBAL",0)
    DisplayString(Myself,39968)  // ~Spell Trigger - Fired~
    ReallyForceSpell(Myself,WIZARD_PROTECTION_FROM_MAGIC_WEAPONS)
    ReallyForceSpell(Myself,WIZARD_PROTECTION_FROM_MAGIC_ENERGY)
    ReallyForceSpell(Myself,WIZARD_IMPROVED_HASTE)
    SetGlobalTimer("RecastProMagWeap","LOCALS",24)
    GiveItemCreate("potn55",Myself,5,0,0)  // ~Potion of Superior Healing~
    SetGlobal("Time","LOCALS",3)
    SetGlobal("Wilt","LOCALS",4)
    SetGlobal("Mordy","LOCALS",5)
    SetGlobal("ProMagWeap","LOCALS",5)
    SetGlobal("Chaos","LOCALS",4)
    SetGlobal("Abjure","LOCALS",0)
    SetGlobal("SpellShield","LOCALS",1)
    SetGlobal("Stoneskin","LOCALS",4)
    SetGlobal("Malison","LOCALS",1)
    SetGlobal("Remove","LOCALS",0)
    SetGlobal("Slow","LOCALS",5)
    SetGlobal("Melf","LOCALS",5)
    SetGlobal("Missile","LOCALS",5)
    Continue()
END

IF
  See(NearestEnemyOf(Myself))
  Global("ChainContingency","LOCALS",0)
THEN
  RESPONSE #100
    DisplayString(Myself,26328)  // ~Chain Contingency~
    SetGlobal("ChainContingency","LOCALS",1)
    ReallyForceSpell(Myself,WIZARD_SPELL_TRAP)
    ReallyForceSpell(Myself,WIZARD_SPELL_IMMUNITY_ABJURATION)
    ReallyForceSpell(Myself,WIZARD_TIME_STOP)
    SetGlobalTimer("TimeStop","LOCALS",18)
END

IF
  OR(2)
    ActionListEmpty()
    GlobalTimerExpired("Cast","LOCALS")
  Range(NearestEnemyOf(Myself),20)
  !GlobalTimerNotExpired("Buffet","LOCALS")
THEN
  RESPONSE #100
    SetGlobalTimer("Buffet","LOCALS",18)
    ForceSpell(Myself,DRAGON_WING_BUFFET)
END

IF
  !Detect(NearestEnemyOf(Myself))
  !Detect(Player1)
  !Range(Player1,20)
THEN
  RESPONSE #100
    ForceSpell(Player1,WIZARD_DIMENSION_DOOR)
END

IF
  TookDamage()
  Global("Contingency","LOCALS",0)
THEN
  RESPONSE #100
    DisplayString(Myself,40252)  // ~Contingency - Protection from Magical Weapons~
    ApplySpell(Myself,WIZARD_PROTECTION_FROM_MAGIC_WEAPONS)
    SetGlobalTimer("RecastProMagWeap","LOCALS",24)
    SetGlobal("Contingency","LOCALS",1)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalTimerExpired("RecastProMagWeap","LOCALS")
  GlobalGT("ProMagWeap","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  Exists(NearestEnemyOf(Myself))
  HPPercentLT(Myself,100)
  OR(2)
    See(LastAttackerOf(Myself))
    Range(NearestEnemyOf(Myself),10)
THEN
  RESPONSE #100
    IncrementGlobal("ProMagWeap","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SetGlobalTimer("RecastProMagWeap","LOCALS",24)
    SpellNoDec(Myself,WIZARD_PROTECTION_FROM_MAGIC_WEAPONS)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Stoneskin","LOCALS",0)
  CheckStatLT(Myself,1,STONESKINS)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    IncrementGlobal("Stoneskin","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(Myself,WIZARD_STONE_SKIN)
END

IF
  OR(2)
    ActionListEmpty()
    GlobalTimerExpired("Cast","LOCALS")
  OR(5)
    See([GOODCUTOFF.0.0.BARD_ALL])
    See([GOODCUTOFF.0.0.CLERIC_ALL])
    See([GOODCUTOFF.0.0.DRUID_ALL])
    See([GOODCUTOFF.0.0.LONG_BOW])
    See([GOODCUTOFF.0.0.SORCERER])
  GlobalTimerExpired("YELLOWDRAGONBREATH","GLOBAL")
THEN
  RESPONSE #100
    ForceSpellRES("ritdrag",LastSeenBy(Myself))  // ritdrag
    SetGlobalTimer("YELLOWDRAGONBREATH","GLOBAL",18)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalTimerExpired("TimeStop","LOCALS")
  GlobalGT("Time","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  OR(2)
    See([PC])
    !CombatCounter(0)
THEN
  RESPONSE #100
    IncrementGlobal("Time","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(Myself,WIZARD_TIME_STOP)
    SetGlobalTimer("TimeStop","LOCALS",18)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Abjure","LOCALS",0)
  See([PC])
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    IncrementGlobal("Abjure","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(Myself,WIZARD_SPELL_IMMUNITY_ABJURATION)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("SpellShield","LOCALS",0)
  See([PC])
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    IncrementGlobal("SpellShield","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(Myself,WIZARD_SPELL_SHIELD)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  Global("Sequencer","LOCALS",0)
  See([PC])
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    SetGlobal("Sequencer","LOCALS",1)
    DisplayString(Myself,25951)  // ~Spell Sequencer~
    ReallyForceSpell(Myself,WIZARD_IMPROVED_INVISIBILITY)
    ReallyForceSpell(LastSeenBy(Myself),WIZARD_TRUE_DISPEL_MAGIC)
    ReallyForceSpell(LastSeenBy(Myself),WIZARD_SLOW)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Malison","LOCALS",0)
  See([PC])
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    IncrementGlobal("Malison","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(LastSeenBy(Myself),WIZARD_GREATER_MALISON)
    ReallyForceSpell(LastSeenBy(Myself),DRAGON_FEAR)
    ReallyForceSpell(LastSeenBy(Myself),DRAGON_LOWER_FIRE)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  HasItem("potn55",Myself)  // ~Potion of Superior Healing~
  HPPercentLT(Myself,50)
THEN
  RESPONSE #100
    SetGlobalTimer("Cast","LOCALS",6)
    UseItem("potn55",Myself)  // ~Potion of Superior Healing~
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Chaos","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  See([PC])
THEN
  RESPONSE #100
    IncrementGlobal("Chaos","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(LastSeenBy(Myself),WIZARD_CHAOS)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Wilt","LOCALS",0)
  See([PC])
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    IncrementGlobal("Wilt","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(LastSeenBy(Myself),WIZARD_ABI_DALZIMS_HORRID_WILTING)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Mordy","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  OR(2)
    !CombatCounter(0)
    See(NearestEnemyOf(Myself))
THEN
  RESPONSE #100
    IncrementGlobal("Mordy","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(Myself,WIZARD_MORDENKAINENS_SWORD)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Slow","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  See([PC])
THEN
  RESPONSE #100
    IncrementGlobal("Slow","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(LastSeenBy(Myself),WIZARD_SLOW)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("MelfAcid","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  See([PC])
THEN
  RESPONSE #100
    IncrementGlobal("MelfAcid","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(LastSeenBy(Myself),WIZARD_MELF_ACID_ARROW)
END

IF
  GlobalTimerExpired("Cast","LOCALS")
  GlobalGT("Missile","LOCALS",0)
  CheckStatLT(Myself,25,SPELLFAILUREMAGE)
  See([PC])
THEN
  RESPONSE #100
    IncrementGlobal("Missile","LOCALS",-1)
    SetGlobalTimer("Cast","LOCALS",6)
    SpellNoDec(LastSeenBy(Myself),WIZARD_MAGIC_MISSILE)
END

IF
  !GlobalTimerExpired("Cast","LOCALS")
  See([PC])
THEN
  RESPONSE #100
    AttackOneRound(LastSeenBy(Myself))
END

IF
  !Detect(NearestEnemyOf(Myself))
  CombatCounter(0)
  ActionListEmpty()
  Global("Prep","LOCALS",1)
THEN
  RESPONSE #100
    DisplayString(Myself,26703)  // ~Damn, where are they?~
    ForceSpell(Myself,CLERIC_HEAL)
    SetGlobal("Prep","LOCALS",0)
END

