//NOTE: The dimlock caster is both a clerical and wizard caster.
//Armor 0 = wizard and Armor 1 = cleric
//------------------------------------------------------------------------------
// 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 - 1 is one shot, 0 is unlimited uses
// 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 - /

//------------------------------------------------------------------------------
// Jump on enemies with Size spell and over obstacles
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfTargetKilled
      tmpargument = LATCHJUMP
      PressLatchButton
  Else
    IfTargetIsAPlatform
      tmpargument = LATCHJUMP
      PressLatchButton
    SetTargetToOldTarget
      
//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  tmpargument = 1
  PlaySound

  // Send messages
  tmpargument = 0
  IfArmorIs
    IfTargetIsOnHatedTeam       //Wizard death
      tmpargument = 0
    Else
      tmpargument = 1
  Else
    IfTargetIsOnHatedTeam       //Priest death
      tmpargument = 4
    Else
      tmpargument = 3
  SendMessageNear

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropItems
  DropKeys

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
// ZZ> Oops
IfHealed
  tmpargument = 2
  SendMessageNear

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

  SetTargetToWhoeverAttacked
    IfTargetIsOnSameTeam
      SetTargetToOldTarget

  // Keep distance
  tmpx = targetx
  tmpy = targety
  tmpdistance = -400
  tmpturn = targetturnto
  Compass
  ClearWaypoints
  AddWaypoint


//------------------------------------------------------------------------------
// 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
IfStateIs11
  // Right hand
  IfTimeOut
    // Release the spell?
    tmpx = targetdistance - 128
    GetContent
    tmpy = tmpargument & 1792 > 1
    IfXIsLessThanY
      // Distance is good...
      tmpx = selfmana
      tmpy = 256
      IfXIsMoreThanY
        // Is it one use?
        tmpy = 16384
        tmpx = tmpargument & tmpy
        IfXIsEqualToY
          tmpargument = tmpargument + 32768
          SetContent
        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
  Else
    // Keep charging
    tmpargument = LATCHRIGHT
    PressLatchButton
    tmpx = targetx - selfx > 1 + selfx
    tmpy = targety - selfy > 1 + selfy
    ClearWaypoints
    AddWaypoint

//------------------------------------------------------------------------------
// ZZ> Find something to do...
IfTimeOut
  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
      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 0
        tmpargument = 0
        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 = tmpargument & 24 < 4
          SetTime
          tmpx = tmpargument
          tmpy = 0
          IfXIsEqualToY
            // Use states 12 and 13 for uncharged spells...
            tmpturn = tmpturn + 2
          tmpargument = tmpturn
          SetState


  IfStateIs0
    tmpargument = 20
    SetTime
    
    // Go after enemies
    SetTargetToWideEnemy
      SetTurnModeToWatchTarget
      tmpargument = rand & 7 + 4
      SetTime
      tmpargument = 3
      SetState
      tmpx = rand & 511 - 256 + targetx
      tmpy = rand & 511 - 256 + targety
      tmpturn = targetturnto
      GetContent
      tmpdistance = tmpargument & 7 < 7
      Compass
    Else
      SetTurnModeToVelocity
      // Wander around
      tmpargument = rand & 15 + 25
      SetTime
      tmpx = rand & 255 - 256 + selfspawnx
      tmpy = rand & 511 - 256 + selfspawny
    ClearWaypoints
    AddWaypoint

  //Clerical AI
  tmpargument = 1
  IfArmorIs
    tmpx = selfmana
    tmpy = 255
    IfXIsMoreThanY
      SetOldTarget
      SetTargetToNearestFriend
        IfTargetIsHurt
          SetTurnModeToVelocity
          tmpx = targetx
          tmpy = targety
          tmpdistance = 200
          tmpturn = targetturnto
          Compass
          ClearWaypoints
          AddWaypoint
          
          //Heal them?
          tmpx = targetdistance
          tmpy = 200
          IfXIsLessThanY
            IfFacingTarget
              tmpargument = ACTIONZA
              DoAction
              tmpargument = rand % 2091 + 4192    //8-16 life points (dimlock heal 1/8 due magic resist)
              HealTarget
              
              tmpargument = rand & 15 + 35
              SetTime
              
              //Spell effect
              tmpargument = 6
              tmpx = targetx
              tmpy = targety
              tmpdistance = targetz
              SpawnExactParticle
              tmpargument = 4
              PlaySound
              
              //Cost some mana
              SetTargetToSelf
              tmpargument = 256
              CostTargetMana
              
      SetTargetToOldTarget

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