// 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 = 10
        tmpx = targetmana
        IfXIsMoreThanY
          tmpargument = 50
          CostTargetMana   
          GetContent
          tmpargument = tmpargument + 50
          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
    tmpturn = targetint > 9	//Intelligence reduces malfunction chance
    tmpx = 50 - tmpturn   	//The chance to fail (50% -1% per 2 int)
    tmpy = rand % 100		
    IfXIsLessThanY
      GetContent
      tmpdistance = tmpargument > 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 = tmpargument > 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
