//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)
      SetTargetToNearbyEnemy        //Prefer enemies before others...
        DoNothing
      Else
        tmpdistance = BLAHENEMIES + BLAHFRIENDS + BLAHITEMS   //Target anything but the dead
        tmpargument = [NONE]
        SetTargetToWideBlahID
          tmpx = targetdistance
          tmpy = 128*3
          IfXIsMoreThanY
            SetTargetToWhoeverIsHolding     //It's too far away, target caster          
      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
    SetTargetToNearbyEnemy        //Prefer enemies before others...
      tmpx = targetdistance
    Else
      tmpdistance = BLAHENEMIES + BLAHFRIENDS + BLAHITEMS   //Target anything but the dead
      tmpargument = [NONE]
      SetTargetToWideBlahID
        tmpx = targetdistance
      Else
        tmpx = 9999         //None found
    tmpy = 128*3
    IfXIsLessThanY
      // Can it be enchanted?
      tmpargument = [XENC]
      IfTargetHasAnyID
        // Can't be enchanted...
        tmpargument = 4
        SendMessageNear
      Else
        SetOldTarget
        tmpargument = 0
        SetContent
        IfTargetIsOnHatedTeam
          SetTargetToWhoeverIsHolding
          tmpx = targetint > 8
          SetTargetToOldTarget
          tmpy = targetint > 8
          
          //Check if the target resists
          tmpx = rand % 10 + tmpx
          tmpy = rand % 10 + tmpy
          IfXIsLessThanY
            tmpargument = 1
            SetContent
            
        tmpargument = 0
        IfContentIs                 //Content == 1 means it was resisted
          //Do the magic
          UnkurseTarget
          tmpargument = [DISP]
          DispelTargetEnchantID
          tmpargument = [UNDO]
          OrderTarget
          
          //Tell them what was disenchanted
          tmpargument = 2
          SendMessageNear   
          tmpargument = 0     //Remove that charge!
          SetContent
        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
