// Create the character
IfSpawned
  KeepAction

// Remove the charge
IfTakenOut
  tmpargument = 0
  SetContent
  SetTargetToWhoeverIsHolding
    IfTargetIsAPlayer
      tmpargument = 3
      SendMessageNear

// Allow it to be used
IfUsed
  UndoEnchant
    tmpargument = 60
    SetReloadTime
  Else
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      tmpx = selfcontent
      tmpy = 768
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpx = 4
          tmpargument = tmpx
          CostTargetMana
            tmpargument = selfcontent + tmpx
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 0
            SpawnExactParticle
      // Put particles around the target
      SetTargetToNearestEnemy
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 0
        SpawnExactParticle

// Allow it to be cast or fizzle
Else
  tmpx = selfcontent
  tmpy = 767
  IfXIsMoreThanY
    // Make the holder cast it
    SetTargetToWhoeverIsHolding
      SetOwnerToTarget
      tmpargument = ACTIONZA
      CorrectActionForHand
      TargetDoAction
    // Find an enemy
    SetTargetToNearestEnemy
      // Can it be enchanted?
      tmpargument = [XENC]
      IfTargetHasAnyID
        // Can't be enchanted...
        tmpargument = 4
        SendMessageNear
      Else
        SetOldTarget			//Remember the target
        tmpdistance =  targetwis > 8
        tmpx = rand & 31 + tmpdistance	//Check for wisdom
        SetTargetToWhoeverIsHolding	//Check caster, not target
        tmpdistance = targetint > 8
        tmpy = rand & 31 + tmpdistance	//Check for intelligence
        SetTargetToOldTarget		//Now revert back to the target again
        IfXIsLessThanY			//Check if it resisted or not
          // Enchant that target
          EnchantTarget
            tmpargument = RED
            SparkleIcon
            MakeNameKnown
            MakeUsageKnown
            tmpargument = 2
            SendMessageNear
          Else
            tmpargument = 0
            SendMessageNear
        Else
          tmpargument = 5		//The target resisted
          SendMessageNear

      // Play a sound
      tmpargument = 0
      PlaySound
      tmpargument = 20
      SetReloadTime
    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
