// Create the character
IfSpawned
  IfStateIs0
    // It's an imported spell in hand
    KeepAction
  Else
    // It's a creature
    MakeNameUnknown
    NotAnItem
    tmpargument = 1
    SetState
    SpawnPoof
    tmpargument = 100
    SetTime
IfChanged
  // It's a spell in hand
  tmpargument = 0
  SetState
  KeepAction


// Monster AI
IfStateIs0
  GetState
Else
  // Character death and explosion for states 1, 2, and 3
  IfKilled
    // Explode
    JoinNullTeam
    tmpx = selfx
    tmpy = selfy
    tmpdistance = selfz
    tmpargument = 0
    SpawnExactParticle
    // Tell everyone it died
    tmpargument = 1
    tmpdistance = rand & 2047 + 10000
    PlaySound
    tmpargument = MESSAGEDEATH
    SendMessage
IfStateIs3
  // Wait for something to get too close
  IfBumped
    // Self destruct
    SetTargetToSelf
    KillTarget
  IfTimeOut
    tmpdistance = 16000 // Square of distance
    SetTargetToDistantEnemy
      // Self destruct
      SetTargetToSelf
      KillTarget
    tmpargument = 25
    SetTime
IfStateIs2
  // Delay so it doesn't explode right away...
  IfTimeOut
    tmpargument = 3
    SetState
IfStateIs1
  // Delay so it doesn't explode right away...
  tmpargument = 2
  SetState




// Spell AI
IfStateIs0
  // Remove the charge
  IfTakenOut
    tmpargument = 0
    SetContent
    SetTargetToWhoeverIsHolding
      IfTargetIsAPlayer
        tmpargument = 3
        IfNameIsKnown
          tmpargument = 4
        SendMessageNear
  // Allow it to be used
  IfUsed
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      GetContent
      tmpx = tmpargument
      tmpy = 256
      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 = [HUMA]
      tmpdistance = 7  // Dead Friends and Enemies...
      SetTargetToNearestBlahID
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 4
        SpawnExactParticle
  Else
    GetContent
    tmpx = tmpargument
    tmpy = 255
    IfXIsMoreThanY
      // Make the holder cast it
      SetTargetToWhoeverIsHolding
        tmpargument = ACTIONZA
        CorrectActionForHand
        TargetDoAction
      // Find a dead humanoid
      tmpargument = [HUMA]
      tmpdistance = 7  // Dead friends or enemies...
      SetTargetToNearestBlahID
        // Poof that target ( players can't be )
        MakeNameKnown
        MakeUsageKnown
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturn
        tmpdistance = targetz
        PoofTarget
          // Replace the target with a Wight
          SetTargetToWhoeverIsHolding
          SetOwnerToTarget
          SpawnCharacterXYZ
            EnchantChild


            // Raise it from the dead
            GetContent
            tmpargument = 1
            SetChildState
            tmpargument = ACTIONMD
            ChildDoActionOverride
          tmpargument = 0
          tmpdistance = 22050
          PlaySound
          tmpargument = 20
          SetReloadTime
        Else
          // Couldn't poof it
          tmpargument = 1
          SendMessageNear
      Else
        // Couldn't find a body
        tmpargument = 2
        SendMessageNear
    Else
      tmpy = 0
      IfXIsMoreThanY
        // Didn't pump it enough
        tmpargument = 2
        SendMessageNear
    // Reset the charge counter
    tmpargument = 0
    SetContent
  // Return to spellbook, Do last!
  IfDropped
    tmpargument = 0
    SetContent
    BecomeSpellbook
    DisaffirmCharacter
    tmpargument = ACTIONJB
    DoAction
    KeepAction
End
