//------------------------------------------------------------------------------
// Each hand has a spell usage list, which is stored in content.  8 bits
// for each hand, bits 0-7 for left, 8-15 for right.
// Bit 7 - 1 is useable, 0 is unuseable
// Bit 6 - UNUSED
// Bit 5 - 1 is need a [HUMA] corpse, 0 is use anytime
// Bit 4 - \Charge time
// Bit 3 - /
// Bit 2 - \
// Bit 1 -  Range
// Bit 0 - /

//Haha! Player was killed
IfTargetIsAPlayer
  tmpargument = 0
  GetXY
  IfXIsEqualToY
    tmpx = tmpx + 1
    SetXY
    tmpargument = 5
    PlayFullSound
    tmpargument = 4
    SendMessage
  IfTargetKilled
    tmpargument = 4
    PlayFullSound
    tmpargument = 3
    SendMessage
    SetTargetToSelf
    tmpargument = 0
    SetState
    
    //Return to the start point
    tmpx = selfspawnx
    tmpy = selfspawny
    tmpdistance = selfz
    Teleport
    
IfTargetIsAMount
  tmpargument = 1
  GetXY
  IfXIsEqualToY
    tmpx = tmpx + 1
    SetXY
    tmpargument = 6
    PlayFullSound
    tmpargument = 5
    SendMessage

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

// Handle death by sending a message and other stuff
IfKilled
  tmpargument = 3
  PlayFullSound

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

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropItems
  DropKeys

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

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

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

  // Put out the eyes
  DisaffirmCharacter
  SpawnPoof
  SpawnPoof
  SpawnPoof
  SpawnPoof

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
// ZZ> Keep 'em guessin'
IfAttacked
  tmpargument = rand & 2
  PlaySound

  SetTargetToWhoeverAttacked
  tmpargument = [BRAZ]
  IfTargetHasID                 //Braziers =(
    // Teleport away...
    tmpargument = 1
    SetState
    tmpargument = 20
    SetTime
  Else
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      SetTargetToOldTarget

  tmpargument = 1
  IfStateIsNot
    tmpargument = 2
    IfStateIsNot
      // Teleport away...
      tmpargument = 1
      SetState
      tmpargument = 20
      SetTime 

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

  //Make ghouls ressurect
  CleanUp     
  
  // Charging a left hand spell?
  IfStateIs10
    // Release the spell?
    tmpx = targetdistance - 128
    GetContent
    tmpy = 600
    IfXIsLessThanY
      // Distance is good...
      tmpx = selfmana
      tmpy = 256
      IfXIsMoreThanY
        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

  // Charging a right hand spell?
  IfStateIs11  
    // Release the spell?
    tmpx = targetdistance - 128
    GetContent
    tmpy = tmpargument & 1792 > 1
    IfXIsLessThanY
      // Distance is good...
      tmpx = selfmana
      tmpy = 256
      IfXIsMoreThanY
        tmpargument = 0
        SetState
      Else
        // Get more mana first
        tmpargument = LATCHRIGHT
        PressLatchButton
    Else
      // Get closer first
      tmpargument = LATCHRIGHT
      PressLatchButton
    tmpx = targetx - selfx > 1 + selfx
    tmpy = targety - selfy > 1 + selfy
    ClearWaypoints
    AddWaypoint
    tmpargument = 10
    SetTime  
    
  // Fall through to normal state
  IfStateIs15
    tmpargument = 0
    SetState

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

  // Un-charged spell in left hand...
  IfStateIs12
    IfTargetIsOnHatedTeam
      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

  // Pick a spell to use on the enemy...  Left or right
  IfStateIs3  
    IfTargetIsOnHatedTeam
      GetContent
      tmpx = tmpargument & 32896
      tmpy = 0
      IfXIsEqualToY
        // Neither hand is useable...  Fall through to state 1
        tmpargument = 1
        SetState
      Else
        tmpy = 32896
        IfXIsEqualToY
          // Both hands are useable, so pick one...  0 or 8
          tmpx = rand & 8
        Else
          // Figure out if it's left or right...  0 or 8
          tmpx = tmpx > 12
        tmpy = 0
        tmpturn = 11  // Right hand state
        IfXIsEqualToY
          tmpturn = 10  // Left hand state
        tmpargument = tmpargument > tmpx & 127
        tmpy = 32
        tmpx = tmpargument & tmpy
        IfXIsEqualToY
          // We need a [HUMA] corpse around...
          tmpx = tmpargument  // Save it...
          tmpargument = [HUMA]
          tmpdistance = 7 // Dead friends and enemies...
          SetTargetToNearestBlahID
            tmpargument = tmpx & 24 < 4
            SetTime
            tmpargument = tmpturn
            SetState
          Else
            // Drop back to normal state
            tmpargument = 0
            SetState
        Else
          // Just set the state and time...
          tmpargument = 150
          SetTime
          tmpx = tmpargument
          tmpy = 0
          IfXIsEqualToY
            // Use states 12 and 13 for uncharged spells...
            tmpturn = tmpturn + 2
          tmpargument = tmpturn
          SetState

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

  // Want to start a teleport
  IfStateIs1
    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
      SetTargetToWideEnemy
        tmpargument = rand & 7 + 4
        SetTime
        tmpargument = 3
        SetState
        tmpx = targetx
        tmpy = targety
      Else
        // Wander around
        tmpargument = rand & 15 + 25
        SetTime
        tmpx = rand & 511 - 256 + selfspawnx
        tmpy = rand & 511 - 256 + selfspawny
      ClearWaypoints
      AddWaypoint
      
Else

  // Continue timed action actions

  // Keep charging right hand spell
  IfStateIs11
    tmpargument = LATCHRIGHT
    PressLatchButton
    tmpx = targetx - selfx > 1 + selfx
    tmpy = targety - selfy > 1 + selfy
    ClearWaypoints
    AddWaypoint
    
  // Keep charging left hand spell
  IfStateIs10
    tmpargument = LATCHLEFT
    PressLatchButton
    tmpx = targetx - selfx > 1 + selfx
    tmpy = targety - selfy > 1 + selfy
    ClearWaypoints
    AddWaypoint 
    
//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
