IF
  Die()
  Global("agtenemy","GLOBAL",1)
THEN
  RESPONSE #100
    SetGlobal("agtenemy","GLOBAL",0)
END

IF
  OR(2)
    Dead("agtomtha")
    Exists("agtomtha")
THEN
  RESPONSE #100
    DestroySelf()
END

IF
  Global("agtmorph","LOCALS",0)
  Global("ag_tslface","LOCALS",1)
THEN
  RESPONSE #100
    Face(5)
    SetGlobal("ag_tslface","LOCALS",0)    
END

IF
  !StateCheck(Myself,STATE_CHARMED)
  Global("agaenemy","GLOBAL",1)
  !Global("agtenemy","GLOBAL",1)
THEN
  RESPONSE #100
    SetGlobal("agtenemy","GLOBAL",1)
    Enemy()
END

IF
  !Allegiance(Myself,ENEMY)
  !StateCheck(Myself,STATE_CHARMED)
  Global("agtenemy","GLOBAL",1)
THEN
  RESPONSE #100
    Enemy()
END

IF
  OnCreation()
  OR(2)
    TimeLT(6)
    TimeGT(21)
THEN
  RESPONSE #100
    SetGlobal("agtslinv","LOCALS",1)
    Deactivate(Myself)
END


//Tomthal appears during daytime if not active
IF
  !TimeLT(6)
  !TimeGT(21)
  Global("agtslinv","LOCALS",1)
THEN
  RESPONSE #100
    Activate(Myself)
    SetGlobal("agtslinv","LOCALS",0)
END

//Tomthal disappears at night
IF
  OR(2)
    TimeLT(6)
    TimeGT(21)
  Global("agtslinv","LOCALS",0)
THEN
  RESPONSE #100
    SetGlobal("agtslinv","LOCALS",1)
    Deactivate(Myself)
END

IF
  HPPercentLT(Myself,15)
  Global("agtmorph","LOCALS",1)
  !HasItem("potn52",Myself)
THEN
  RESPONSE #100
    EscapeArea()
END

IF
  HPPercentLT(Myself,25)
  Global("agtmorph","LOCALS",1)
  !HasItem("potn52",Myself)
  Detect(NearestEnemyOf())
  !StateCheck(Myself,STATE_INVISIBLE)
  !StateCheck(Myself,STATE_IMPROVEDINVISIBILITY)
THEN
  RESPONSE #100
    RunAwayFromNoInterrupt(NearestEnemyOf(),45)
    Hide()
END

IF
  HPPercentLT(Myself,50)
  !GlobalTimerNotExpired("agt","LOCALS")
  HasItem("potn52",Myself)
THEN
  RESPONSE #100
    UseItem("potn52",Myself)
    SetGlobalTimer("agt","LOCALS",6)
END

IF
  StateCheck(Myself,STATE_POISONED)
  !GlobalTimerNotExpired("agt","LOCALS")
  HasItem("potn20",Myself)
THEN
  RESPONSE #100
    UseItem("potn20",Myself)
    SetGlobalTimer("agt","LOCALS",6)
END

IF
  AttackedBy([GOODCUTOFF],DEFAULT)
  !Allegiance(Myself,ENEMY)
THEN
  RESPONSE #100
    SetGlobal("agtenemy","GLOBAL",1)
    Enemy()
END

IF
  OR(4)
    !Allegiance(Myself,ENEMY)
    !IsActive(Myself)
    StateCheck(Myself,STATE_INVISIBLE)
    StateCheck(Myself,STATE_IMPROVEDINVISIBILITY)
THEN
  RESPONSE #100
    NoAction()
END

IF
  Global("agtmorph","LOCALS",0)
THEN
  RESPONSE #100
    Polymorph(SVIRFNEBLIN_DARK_AXE)
    SetGlobal("agtmorph","LOCALS",1)
END

IF
  HasItem("potn11",Myself)
  !GlobalTimerNotExpired("agt","LOCALS")
THEN
  RESPONSE #100
    UseItem("potn11",Myself)
    SetGlobalTimer("agt","LOCALS",6)
END

IF
  HaveSpell(WIZARD_MIRROR_IMAGE)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_MIRRORIMAGE)
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(Myself,WIZARD_MIRROR_IMAGE)
END

IF
  HaveSpell(WIZARD_BLUR)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_BLUR)
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(Myself,WIZARD_BLUR)
END

IF
  !See([GOODCUTOFF.0.0.MAGE_ALL])
  !See([GOODCUTOFF.0.0.CLERIC_ALL])
  !See([GOODCUTOFF.0.0.DRUID_ALL])
  !See([GOODCUTOFF.0.0.BARD_ALL])
  !See([GOODCUTOFF])
  False()
THEN
  RESPONSE #100
    Continue()
END

IF
  HaveSpell(WIZARD_CONE_OF_COLD)
  !GlobalTimerNotExpired("agt","LOCALS")
  !Range(LastSeenBy(),10)
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
  CheckStatLT(LastSeenBy(),50,RESISTCOLD)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_CONE_OF_COLD)
END

IF
  HaveSpell(WIZARD_DOMINATION)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(LastSeenBy(),STATE_SLEEPING)
  !StateCheck(LastSeenBy(),STATE_HELPLESS)
  !StateCheck(LastSeenBy(),STATE_CONFUSED)
  !StateCheck(LastSeenBy(),STATE_PANIC)
  !HasBounceEffects(LastSeenBy())
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_DOMINATION)
END

IF
  HaveSpell(WIZARD_FIREBALL)
  !GlobalTimerNotExpired("agt","LOCALS")
  !Range(LastSeenBy(),10)
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
  CheckStatLT(LastSeenBy(),50,RESISTMAGICFIRE)
  CheckStatLT(LastSeenBy(),50,RESISTMAGIC)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_FIREBALL)
END

IF
  HaveSpell(WIZARD_SPIDER_SPAWN)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_SPIDER_SPAWN)
END

IF
  HaveSpell(WIZARD_STONE_SKIN)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(Myself,WIZARD_STONE_SKIN)
END

IF
  HaveSpell(WIZARD_CONFUSION)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(LastSeenBy(),STATE_SLEEPING)
  !StateCheck(LastSeenBy(),STATE_HELPLESS)
  !StateCheck(LastSeenBy(),STATE_CONFUSED)
  !StateCheck(LastSeenBy(),STATE_PANIC)
  !HasBounceEffects(LastSeenBy())
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_CONFUSION)
END

IF
  HaveSpell(WIZARD_MONSTER_SUMMONING_1)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_MONSTER_SUMMONING_1)
END

IF
  HaveSpell(WIZARD_MELF_METEOR)
  !GlobalTimerNotExpired("agt","LOCALS")
  !HasItem("melfmet",Myself)
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(Myself(),WIZARD_MELF_METEOR)
END

IF
  HaveSpell(WIZARD_MELF_ACID_ARROW)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
  CheckStatLT(LastSeenBy(),50,RESISTACID)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_MELF_ACID_ARROW)
END

IF
  HaveSpell(WIZARD_AGANNAZAR_SCORCHER)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
  CheckStatLT(LastSeenBy(),50,RESISTFIRE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_AGANNAZAR_SCORCHER)
END

IF
  HaveSpell(WIZARD_CHARM_PERSON)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(LastSeenBy(),STATE_SLEEPING)
  !StateCheck(LastSeenBy(),STATE_HELPLESS)
  !HasBounceEffects(LastSeenBy())
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_CHARM_PERSON)
END

IF
  HaveSpell(WIZARD_SLEEP)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(LastSeenBy(),STATE_SLEEPING)
  !StateCheck(LastSeenBy(),STATE_HELPLESS)
  !HasBounceEffects(LastSeenBy())
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_SLEEP)
END

IF
  HaveSpell(WIZARD_CHROMATIC_ORB)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
  CheckStatLT(LastSeenBy(),50,RESISTMAGIC)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_CHROMATIC_ORB)
END

IF
  HaveSpell(WIZARD_BLINDNESS)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
  CheckStatLT(LastSeenBy(),50,RESISTMAGIC)
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_BLINDNESS)
END

IF
  HaveSpell(WIZARD_MAGIC_MISSILE)
  !GlobalTimerNotExpired("agt","LOCALS")
  !StateCheck(Myself,STATE_SILENCED)
  !CheckStatGT(Myself,75,SPELLFAILUREMAGE)
  !HasBounceEffects(LastSeenBy())
THEN
  RESPONSE #100
    SetGlobalTimer("agt","LOCALS",6)
    Spell(LastSeenBy(),WIZARD_MAGIC_MISSILE)
END

IF
  GlobalTimerNotExpired("agt","LOCALS")
THEN
  RESPONSE #100
    AttackOneRound(LastSeenBy())
END

IF
  !GlobalTimerNotExpired("agt","LOCALS")
THEN
  RESPONSE #100
    Attack(LastSeenBy())
END
