// Create the character
IfSpawned
  KeepAction

// Remove the charge
IfTakenOut
  tmpargument = 0
  SetContent
  SetTargetToWhoeverIsHolding
    IfTargetIsAPlayer
      tmpargument = 3
      SendMessageNear
      
// Allow it to be charged up
IfUsed
  // Does it have one going?
  UndoEnchant
    tmpargument = 60
    SetReloadTime
  // Nope, so charge
  Else
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      tmpx = selfcontent
      tmpy = 256
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpargument = 8
          CostTargetMana
            tmpargument = selfcontent + 8             //Cast speed
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 2
            SpawnExactParticle
            
      // Spawn particles around target
      SetTargetToNearestFriend
      tmpx = targetdistance
      tmpy = 250
      IfXIsMoreThanY
        SetTargetToWhoeverIsHolding
      tmpx = targetx
      tmpy = targety
      tmpdistance = targetz
      tmpargument = 2
      SpawnExactParticle

// Let it be cast
Else
  tmpx = selfcontent
  tmpy = 0
  IfXIsMoreThanY
    tmpy = 255
    IfXIsMoreThanY
      // Cast the spell...
      UndoEnchant  // There can be only one
      SetTargetToWhoeverIsHolding
      SetOwnerToTarget

      // Find the target
      SetTargetToNearestFriend
      tmpx = targetdistance
      tmpy = 250
      IfXIsMoreThanY
        SetTargetToWhoeverIsHolding

      // Do the spell
      EnchantTarget
        MakeUsageKnown
        MakeNameKnown
        tmpargument = RED
        SparkleIcon
        tmpargument = 2
        SendMessageNear
      Else
        tmpargument = 1
        SendMessageNear

      // Do flashy things
      tmpargument = 0
      PlaySound
      tmpargument = 60
      SetReloadTime

    // Nothing happens...
    Else
      tmpargument = 0
      SendMessageNear

    // Reset the charge counter
    tmpargument = 0
    SetContent

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

End
