//This spell removes every enchant on the target and
//unkurses anything that it has. Intelligence increases
//the chance for success.

// Create the character
IfSpawned
  KeepAction

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

// Allow it to be used
IfUsed
  SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      tmpx = selfcontent
      tmpy = 768
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpargument = 7
          CostTargetMana
            tmpargument = selfcontent + tmpargument
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 0
            SpawnExactParticle
      
      // Put particles around the target (If we find one)
      tmpx = 0
      SetTargetToNearbyEnemy        //Prefer enemies before others...
        tmpx = 1
      Else
        SetTargetToNearestLifeform
          tmpx = targetdistance
          tmpy = 128*3
          IfXIsLessThanY
            tmpx = 1
          Else
            tmpx = 0
          
      tmpy = 1
      IfXIsEqualToY
        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 a target
    SetTargetToNearestLifeform
    tmpx = targetdistance
    tmpy = 128*3
    IfXIsLessThanY
      // Can it be enchanted?
      tmpargument = [XENC]
      IfTargetHasAnyID
        // Can't be enchanted...
        tmpargument = 4
        SendMessageNear
      Else
        SetOldTarget
        SetTargetToWhoeverIsHolding
        tmpx = targetint > 8
        SetTargetToOldTarget
        tmpy = targetint > 8
        
        //Check if the target resists
        tmpx = rand % 10 + tmpx
        tmpy = rand % 10 + tmpy
        IfXIsMoreThanY
          //Do the magic
          UnkurseTarget
          tmpargument = [DISP]
          DispelTargetEnchantID
          tmpargument = [UNDO]
          OrderTarget
          
          //Tell them what was disenchanted
          tmpargument = 2
          SendMessageNear   
        Else
          tmpargument = 4
          SendMessageNear
    Else
      // Couldn't find a target, target the other hand (or the caster if that also fails)
      SetTargetToWhoeverIsHolding
      IfHeldInLeftHand
        SetTargetToTargetRightHand
      Else
        SetTargetToTargetLeftHand
    
      //Do the magic
      UnkurseTarget
      tmpargument = [DISP]
      DispelTargetEnchantID
      tmpargument = [UNDO]
      OrderTarget
      
      //Tell them what was disenchanted
      tmpargument = 2
      SendMessageNear   
  
    // Play a sound
    tmpargument = 0
    PlaySound
    tmpargument = 20
    SetReloadTime
  
  Else
    tmpx = selfcontent
    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
