// Create the character
IfSpawned
  KeepAction


// Remove the charge
IfTakenOut
  tmpargument = 0
  SetContent
  SetTargetToWhoeverIsHolding
    IfTargetIsAPlayer
      tmpargument = 2
      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 = tmpargument & 1
          tmpy = 0
          IfXIsEqualToY
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 0
            SpawnExactParticle
    // Put particles around the target
    tmpargument = [LOCK]
    tmpdistance = 14 // Items, Friends, and Enemies
    SetTargetToNearestBlahID
      GetContent
      tmpx = tmpargument & 1
      tmpy = 0
      IfXIsEqualToY
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 0
        SpawnExactParticle


// Allow it to be cast or fizzle
Else
  GetContent
  tmpx = tmpargument
  tmpy = 255
  IfXIsMoreThanY
    // Make the holder cast it
    SetTargetToWhoeverIsHolding
      SetOwnerToTarget
      tmpargument = ACTIONZA
      CorrectActionForHand
      TargetDoAction
    // Find a target
    tmpargument = [LOCK]
    tmpdistance = 14 // Items, Friends, and Enemies
    SetTargetToNearestBlahID
      // Play a sound
      tmpargument = 0
      tmpdistance = 11025
      PlaySound
      tmpargument = 20
      SetReloadTime

      // Tell it to open
      tmpargument = 491 // The special unlock order
      OrderTarget
      MakeNameKnown
      MakeUsageKnown
    Else
      // Couldn't find a target
      tmpargument = 1
      SendMessageNear
  Else
    tmpy = 0
    IfXIsMoreThanY
      // Didn't pump it enough
      tmpargument = 1
      SendMessageNear
  // Reset the charge counter
  tmpargument = 0
  SetContent


// Return to spellbook, Do last!
IfDropped
  tmpargument = 0
  SetContent
  BecomeSpellbook
  DisaffirmCharacter
  tmpargument = ACTIONJB
  DoAction
  KeepAction


End
