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


// Spell AI
IfUsed
  GetContent
  tmpx = tmpargument
  tmpy = 1024
  IfXIsLessThanY
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID			//Arcane casters only
      tmpy = targetmanaflow
      IfXIsLessThanY			//Enough manaflow?
        tmpx = targetint
        GetContent
        tmpy = tmpargument * 7		//Enough intelligence to reach next spell power level?
        IfXIsMoreThanY
          tmpargument = 13
          CostTargetMana		//Mana cost
            GetContent
            tmpargument = tmpargument + 7
            SetContent			//Casting Time
            tmpargument = tmpargument > 8
            SetState			//Set spell power
Else
  GetContent
  tmpx = tmpargument
  tmpy = 256
  IfXIsMoreThanY
    // Make the target shoot
    SetTargetToWhoeverIsHolding
      tmpargument = ACTIONZA
      CorrectActionForHand
      TargetDoAction

    // Figured out what it was
    MakeNameKnown
    MakeUsageKnown
    // Shoot off some missiles
    tmpdistance = selfz
    tmpx = selfx
    tmpy = selfy
    IfStateIs1		//Spell power level 1 (4 orbs)
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
    IfStateIs2		//Spell power level 2 (8 orbs)
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
    IfStateIs3	//Spell power level 3 (12 orbs)
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
    IfStateIs4		//Spell power level 4 (16 orbs! :P)
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 3
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 4
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 5
      tmpturn = rand & 1250
      SpawnExactParticle
      tmpargument = 6
      tmpturn = rand & 1250
      SpawnExactParticle
    tmpargument = 0
    tmpdistance = rand & 2047 + 10000
    PlaySound
  // Reset the charge counter
  tmpargument = 0
  SetContent


// Put the little mana ball on the
// character's hand
GetContent
tmpx = tmpargument
tmpy = 0
IfXIsMoreThanY
  tmpturn = tmpargument < 4 + 5000
  tmpdistance = SPAWNORIGIN
  tmpargument = 0
  SpawnAttachedSizedParticle


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