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

// Tell them what they've won...
IfGrabbed
  SetTargetToWhoeverIsHolding
  IfTargetIsAPlayer
    tmpargument = 0
    SendMessageNear

// What happens if it is used by a arcane spellcaster?
IfUsed
  SetTargetToWhoeverIsHolding
  tmpargument = [WMAG]
  IfTargetHasSkillID		//Can cast spell?

    //Aiming Cursor
    SetTargetToOwner
    GetContent
    tmpdistance = tmpargument > 8 + 4 < 6
    tmpturn = targetturn
    tmpx = targetx
    tmpy = targety
    Compass
    tmpdistance = targetz
    tmpargument = 4
    SpawnExactParticle

    //Charge it up!
    SetTargetToWhoeverIsHolding
    GetContent
    tmpx = tmpargument
    tmpy = 768
    IfXIsLessThanY
      tmpy = targetmanaflow / 2 + 512		//Need at least 4 mana flow
      IfXIsLessThanY		//Enough power?
        tmpy = 50
        tmpx = targetmana
        IfXIsMoreThanY
          tmpx = 0
          tmpargument = [WIZA]      //Level 5 wizards have 33% reduced mana cost
          IfTargetHasID
            tmpx = 1
          tmpargument = [ARCH]      //Archmages get this bonus as well
          IfTargetHasID
            tmpx = 1
            
          tmpdistance = 50
          tmpy = 1
          IfXIsEqualToY
            tmpargument = 50
            tmpx = targetlevel
            tmpy = 3
            IfXIsMoreThanY
              tmpargument = 33
              tmpdistance = 75
          Else
            tmpargument = 50
          CostTargetMana
          GetContent
          tmpargument = tmpargument + tmpdistance
          SetContent

Else			//Player released the charge
  GetContent
  tmpx = tmpargument
  tmpy = 770
  IfXIsMoreThanY	//Finished Charging
    // Make the target shoot
    SetTargetToWhoeverIsHolding
      tmpargument = ACTIONZA
      CorrectActionForHand
      TargetDoAction

    //Spawn the magic effects
    SetTargetToWhoeverIsHolding
    tmpdistance = 0
    tmpargument = [WIZA]
    IfTargetHasID           //Level 5 Wizards have a 100% success
      tmpx = targetlevel
      tmpy = 3
      IfXIsMoreThanY
        tmpdistance = 1
    tmpargument = [ARCH]    //Archmages have always 100% success
    IfTargetHasID
      tmpdistance = 1


    //If tmpdistance = 1 then it's at least a level 5 wizard
    tmpturn = 0
    IfDistanceIsMoreThanTurn
      tmpx = 0
      tmpy = 1
    Else
      tmpturn = targetint > 9	//Intelligence reduces malfunction chance
      tmpx = 50 - tmpturn   	//The chance to fail (50% -1% per 2 int)
      tmpy = rand % 100
    IfXIsLessThanY
      tmpdistance = selfcontent > 8 + 4 < 6
      tmpturn = targetturn
      tmpx = targetx
      tmpy = targety
      Compass
      tmpdistance = targetz
      TeleportTarget
        tmpargument = 0		//Spell effect
        SpawnExactParticle
        SpawnExactParticle
        SpawnExactParticle
        tmpargument = 0		//Play the spell effect sound
        PlaySound
      Else			//Teleported inside wall
        tmpargument = 3
        SendMessageNear
    Else			//Lost control!
      tmpargument = 4
      SendMessageNear
      tmpdistance = selfcontent > 8 + 4 < 6
      tmpturn = rand
      tmpx = rand % 750 + selfx - 375
      tmpy = rand % 750 + selfy - 375
      Compass
      tmpdistance = rand % 1000 + selfz	//Randomize height
      TeleportTarget
        tmpargument = rand & 511 + 512
        tmpdistance = DAMAGEZAP
        DamageTarget			//Deal some feedback damage
        tmpargument = 1
        SpawnExactParticle		//Spell effects
        SpawnExactParticle
        SpawnExactParticle
        tmpargument = 1		//Play the spell effect sound
        PlaySound

    //Figured out what it was
    IfNameIsKnown
      DoNothing
    Else
      MakeNameKnown
      MakeUsageKnown
      SetTargetToWhoeverIsHolding
      tmpargument = 30
      tmpdistance = EXPSECRET
      GiveExperienceToTarget

  Else			//Not enough charged
    tmpy = 0
    GetContent
    tmpx = tmpargument
    IfXIsMoreThanY	//Only display message if charged
      tmpargument = 1
      SendMessageNear
  tmpargument = 0
  SetContent

// Put the little mana ball on the
// character's hand
GetContent
tmpx = tmpargument
tmpy = 0
IfXIsMoreThanY
  tmpturn = tmpargument * 20
  tmpdistance = SPAWNORIGIN
  tmpargument = 3
  SpawnAttachedSizedParticle

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

// Finish up
End
