//------------------------------------------------------------------------------
//Use wand!
IfTimeOut
  IfFacingTarget
    IfTargetIsAlive
      IfTargetIsOnHatedTeam
        tmpargument = LATCHRIGHT
        PressLatchButton

// ZZ> Handle death by sending a message and other stuff
IfKilled
  tmpargument = 2
  tmpdistance = rand & 1023 + 10000
  PlaySound


  // Send messages
  IfTargetIsOnHatedTeam
    tmpargument = 0
  Else
    tmpargument = 1
  SendMessageNear


  // Drop goodies
  tmpargument = 65535
  DropMoney


  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Award quest experience
  tmpargument = 100
  tmpdistance = EXPQUEST
  GiveExperienceToTargetTeam

  // Finish it up
  BeatModule
  EnableExport
  // Mark this module as beaten, hidden modules may open up
  tmpargument = [BEAT]
  AddIDSZ
  // Fix the end text
  ClearEndMessage
  tmpargument = 3
  AddEndMessage

  // Order team to die
  tmpdistance = [EVIL]
  tmpargument = [UNDO]
  OrderSpecialID


  // Put out the eyes
  DisaffirmCharacter
  SpawnPoof
  SpawnPoof
  SpawnPoof
  SpawnPoof


//------------------------------------------------------------------------------
// ZZ> Refill hp over time
IfSpawned
  SetTargetToSelf
  EnchantTarget
  


//------------------------------------------------------------------------------
// ZZ> Keep 'em guessin'
IfAttacked
  tmpargument = rand & 1
  tmpdistance = rand & 1023 + 10000
  PlaySound


  SetTargetToWhoeverAttacked
    IfTargetIsOnHatedTeam
      GetState
    Else
      SetTargetToOldTarget


  IfStateIs1
    GetState
  Else
    IfStateIs2
      GetState
    Else
      // Teleport away...
      tmpargument = 1
      SetState
      tmpargument = 20
      SetTime



//------------------------------------------------------------------------------
IfBumped
  IfStateIs1
    GetState
  Else
    IfStateIs2
      GetState
    Else
      // Teleport away...
      tmpargument = 1
      SetState
      tmpargument = 0
      SetTime
  IfTargetIsOnHatedTeam
    IfFacingTarget
      tmpargument = LATCHRIGHT
      PressLatchButton 


//------------------------------------------------------------------------------
// ZZ> For charging a spell...
IfStateIs10
  // Left hand
  IfTimeOut
    // Release the spell?
    tmpx = targetdistance - 128
    GetContent
    tmpy = tmpargument & 7 < 7
    IfXIsLessThanY
      // Distance is good...
      tmpx = selfmana
      tmpy = 256
      IfXIsMoreThanY
        // Is it one use?
        tmpy = 64
        tmpx = tmpargument & tmpy
        IfXIsEqualToY
          tmpargument = tmpargument + 128
          SetContent
        tmpargument = 0
        SetState
      Else
        // Get more mana first
        tmpargument = LATCHLEFT
        PressLatchButton
    Else
      // Get closer first
      tmpargument = LATCHLEFT
      PressLatchButton
    tmpx = targetx - selfx > 1 + selfx
    tmpy = targety - selfy > 1 + selfy
    ClearWaypoints
    AddWaypoint
    tmpargument = 10
    SetTime
  Else
    // Keep charging
    tmpargument = LATCHLEFT
    PressLatchButton
    tmpx = targetx - selfx > 1 + selfx
    tmpy = targety - selfy > 1 + selfy
    ClearWaypoints
    AddWaypoint

//------------------------------------------------------------------------------
// ZZ> Find something to do...
IfTimeOut
  CleanUp


  IfStateIs15
    // Fall through to normal state
    tmpargument = 0
    SetState



  IfStateIs13
    // Un-charged spell in right hand...
    IfTargetIsOnHatedTeam
      tmpx = targetdistance - 128
      GetContent
      tmpy = tmpargument & 1792 > 1
      IfXIsLessThanY
        // Distance is good...
        IfFacingTarget
          tmpargument = LATCHRIGHT
          PressLatchButton
      Else
        IfFacingTarget
          tmpargument = LATCHRIGHT
          PressLatchButton
      tmpargument = 15
      SetState
      tmpargument = rand & 15 + 20
      SetTime
    Else
      tmpargument = 0
      SetState



  IfStateIs12
    IfTargetIsOnHatedTeam
      // Un-charged spell in left hand...
      tmpx = targetdistance - 128
      GetContent
      tmpy = tmpargument & 7 < 7
      IfXIsLessThanY
        // Distance is good...
        IfFacingTarget
          tmpargument = LATCHLEFT
          PressLatchButton
      tmpargument = 15
      SetState
      tmpargument = rand & 15 + 20
      SetTime
    Else
      tmpargument = 0
      SetState



  IfStateIs3
    // Pick a spell to use on the enemy...  Left or right
    IfTargetIsOnHatedTeam
      GetContent
      tmpx = tmpargument & 32896
      tmpy = 0
      IfXIsEqualToY
        // Neither hand is useable...  Fall through to state 1
        tmpargument = 1
        SetState
      Else
        tmpturn = 10  // Left hand state


      // Just set the state and time...
      tmpargument = tmpargument & 24 < 4
      SetTime
      tmpx = tmpargument
      tmpy = 0
      IfXIsEqualToY
        // Use states 12 and 13 for uncharged spells...
        tmpturn = tmpturn + 2
      tmpargument = tmpturn
      SetState






  IfStateIs2
    // Halfway through a teleport
    tmpargument = rand & 15 + 35
    SetTime
    tmpx = rand & 511 - 256 + selfx
    tmpy = rand & 511 - 256 + selfy
    Teleport
    tmpx = rand & 511 - 256 + selfx
    tmpy = rand & 511 - 256 + selfy
    Teleport
    tmpargument = 15
    SetState
    // Face the target
    tmpx = targetx
    tmpy = targety
    ClearWaypoints
    AddWaypoint



  IfStateIs1
    // Want to start a teleport
    tmpargument = ACTIONMJ
    DoAction
      tmpargument = 2
      SetState
      tmpargument = 40  // Don't let 'em get hit out of it...
      SetDamageTime
      tmpargument = 16
      SetTime
    Else
      // Try again in a bit...
      tmpargument = 5
      SetTime







  IfStateIs0
    tmpargument = 20
    SetTime
    tmpx = rand & 255
    tmpy = 200
    IfXIsMoreThanY
      // Teleport...
      tmpargument = 1
      SetState
      SetTime
    Else
      // Go after enemies
      SetTargetToNearestEnemy
        tmpargument = rand & 7 + 4
        SetTime
        tmpargument = 3
        SetState
    //    tmpx = targetx
     //   tmpy = targety
        tmpturn = targetturnto
      Else
        // Wander around
        tmpargument = rand & 15 + 25
        SetTime
        tmpx = rand & 511 - 256 + selfspawnx
        tmpy = rand & 511 - 256 + selfspawny
      ClearWaypoints
      AddWaypoint


//------------------------------------------------------------------------------
// ZZ> Put on some eyes
IfSpawned
  tmpargument = 6
  tmpdistance = 9
  SpawnAttachedParticle
  tmpdistance = 10
  SpawnAttachedParticle



//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
