// Create the character
IfSpawned
  IfStateIs0
    // It's an imported spell in hand
    KeepAction
  Else
    // Mimic speed based on state
    MakeNameUnknown
    SetTargetToSelf
    NotAnItem
    tmpx = selfstate
    SpawnPoof
    IfStateIs1
      tmpargument = 0 - 1500
      GiveLifeToTarget
      tmpargument = 50
    IfStateIs2
      tmpargument = 0 - 700
      GiveLifeToTarget
      tmpargument = 75
    IfStateIs3
      tmpargument = 100
    SetSpeedPercent
    tmpargument = 50
    SetTime
IfChanged
  // It's a spell in hand
  tmpargument = 0
  SetState
  KeepAction
  
//------------------------------------------------------------------------------
// ZZ> Someone cast an unlock spell
IfOrdered
  tmpx = selforder
  tmpy = 491  // The unique code for an unlock order
  IfXIsEqualToY
    // Die
    SetTargetToSelf
    KillTarget
    tmpargument = 3
    SendMessageNear

// Monster AI
IfStateIs0
  DoNothing
Else
  // Character screams
  IfAttacked
    tmpargument = 0
    PlaySound

  // Character death
  IfKilled
    // Tell everyone it died
    tmpargument = 0
    PlaySound
    tmpargument = 0
    IfTargetIsOnSameTeam
      tmpargument = 1
      IfTargetIsSelf
        tmpargument = 2
    SendMessageNear

  // Do different AI's
  IfStateIs1
    // Stupid AI
    IfTimeOut
      // Movement
      SetTargetToNearbyEnemy
        tmpargument = 60
        SetTime             // Reset the timer
      Else
        SetTargetToOwner
        tmpargument = 125   // Reset the timer
        SetTime
      tmpx = rand & 511 + targetx - 256
      tmpy = rand & 511 + targety - 256
      ClearWaypoints
      AddWaypoint
      tmpargument = rand & 1 + 1
      PlaySound
    IfBumped
      // Attack anyone
      tmpargument = ACTIONUA
      DoAction  // Couldn't use latches...
  IfStateIs2
    // Middle level AI
    IfTimeOut
      // Movement
      SetTargetToWideEnemy
        // Actively look for enemies
        tmpx = targetdistance
        tmpy = 600
        IfXIsLessThanY
          // Attack the enemy?
          tmpy = 100
          IfXIsLessThanY
            tmpargument = ACTIONUA
            DoAction  // Couldn't use latches...
            tmpargument = rand & 1 + 1
            PlaySound
          // Chase the enemy
          tmpx = targetx
          tmpy = targety
        Else
          tmpx = rand & 511 + ownerx - 256
          tmpy = rand & 511 + ownery - 256
      Else
        tmpx = rand & 511 + ownerx - 256
        tmpy = rand & 511 + ownery - 256
      ClearWaypoints
      AddWaypoint
      // Reset the timer
      tmpargument = 75
      SetTime
  IfStateIs3
    // Bad boy AI
    IfTimeOut
      // Mana drips
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 3
      SpawnExactParticle
      // Movement
      tmpx = ownerx
      tmpy = ownery
      SetTargetToNearestFriend
        tmpturn = targetturnto + 32768
        tmpdistance = 100
        Compass
      // Save the values for X and Y...
      tmpargument = 0
      SetXY
      tmpx = ownerdistance
      tmpy = 800
      IfXIsLessThanY
        SetTargetToNearestEnemy
          // Attack the enemy?
          SetTurnModeToWatchTarget
          tmpx = targetdistance
          tmpy = 140
          IfXIsLessThanY
            IfFacingTarget
              tmpargument = ACTIONUA
              DoAction  // Couldn't use latches...
              tmpargument = rand & 1 + 1
              PlaySound
          
          // Chase the enemy
          tmpargument = 0
          GetXY  // Restore the saved values...
          tmpturn = targetturnto
          tmpdistance = 600
          Compass
        Else
          SetTurnModeToVelocity
          tmpargument = 0
          GetXY    
      Else
        SetTurnModeToVelocity
        tmpargument = 0
        GetXY    
      ClearWaypoints
      AddWaypoint
      // Reset the timer
      tmpargument = 50
      SetTime

// Spell AI
IfStateIs0
  // Remove the charge
  IfTakenOut
    tmpargument = 0
    SetContent
    SetTargetToWhoeverIsHolding
      IfTargetIsAPlayer
        tmpargument = 5
        IfNameIsKnown
          tmpargument = 6
        SendMessageNear
  // Allow it to be used
  IfUsed
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      GetContent
      tmpx = tmpargument
      tmpy = 768
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpx = 4
          tmpargument = tmpx
          CostTargetMana
            GetContent
            tmpargument = tmpargument + tmpx
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 4
            SpawnExactParticle
      // Put particles around the target
      tmpargument = [CHES]
      tmpdistance = 14  // Friends, items and Enemies...
      SetTargetToWideBlahID
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 4
        SpawnExactParticle
  Else
    GetContent
    tmpx = tmpargument
    tmpy = 255
    IfXIsMoreThanY
      // Make the holder cast it
      SetTargetToWhoeverIsHolding
        tmpargument = ACTIONMC // MC because ZA/ZC spawns an attack... ZA03AL
        TargetDoAction
      // Find a dead humanoid
      tmpargument = [CHES]
      tmpdistance = 14
      SetTargetToWideBlahID
        // Poof that target ( players can't be )
        
        //Some modules get broke if not
        DropTargetKeys          

        //Identify it        
        SetOldTarget
        SetTargetToWhoeverIsHolding
        IfTargetIsAPlayer
          IfNameIsKnown
            DoNothing
          Else
            MakeNameKnown
            MakeUsageKnown
            tmpargument = 20
            tmpdistance = EXPSECRET
            GiveExperienceToTarget
        
        //Characters with more than 18 INT can extract money as well
        tmpx = targetint
        SetTargetToOldTarget
        tmpy = 4608
        IfXIsMoreThanY
          tmpargument = targetmoney
          DropTargetMoney
 
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturn
        tmpdistance = targetz
        
        // Replace the target with a mimic
        PoofTarget
          SetTargetToWhoeverIsHolding
          SetOwnerToTarget
          SpawnCharacterXYZ
            EnchantChild
            // Set the drain
            GetContent
            tmpargument = 0 - tmpargument > 2
            tmpdistance = 0
            tmpx = 0
            tmpy = 0
            SetEnchantBoostValues

            // Set the AI type
            GetContent
            tmpargument = tmpargument > 8 // State of new mimic, 0 to 3
            SetChildState
            tmpargument = ACTIONMD
            ChildDoActionOverride
          tmpargument = 3
          PlaySound
          tmpargument = 20
          SetReloadTime
        Else
          // Couldn't poof it
          tmpargument = 3
          SendMessageNear
      Else
        // Couldn't find a target
        tmpargument = 4
        SendMessageNear
    Else
      tmpy = 0
      IfXIsMoreThanY
        // Didn't pump it enough
        tmpargument = 4
        SendMessageNear
    // Reset the charge counter
    tmpargument = 0
    SetContent
  // Return to spellbook, Do last!
  IfDropped
    tmpargument = 0
    SetContent
    BecomeSpellbook
    DisaffirmCharacter
    tmpargument = ACTIONJB
    DoAction
    KeepAction
End
