//This function makes an item fall to the floor when spawned
IfSpawned
  tmpargument = ACTIONJB
  DoAction
  KeepAction
  MakeCrushValid
IfDropped				// Make it lie on floor
  KeepAction
IfGrabbed				// Tell them what they've won, Johnny!
  SetTargetToWhoeverIsHolding
  IfTargetIsAPlayer
    tmpargument = 0
    SendMessageNear
IfHitGround
  tmpargument = 0
  PlaySound
  tmpargument = ACTIONDB
  DoAction
IfHeld			//Hide in hand
  tmpargument = 1
  SetState
Else
  tmpargument = 0
  SetState

//Spell stuff
IfUsed
  UndoEnchant
    UnsparkleIcon
    tmpargument = 25
    SetReloadTime
  Else
    SetTargetToWhoeverIsHolding
    SetOwnerToTarget
    tmpargument = [HMAG]
    IfTargetHasSkillID				//Need divine spell skill
      tmpx = selfcontent
      tmpy = 768					//Max charge
      IfXIsLessThanY
        tmpx = selfcontent * 2
        tmpy = targetmanaflow			//Enough spell power?
        IfYIsMoreThanX
          tmpargument = 4				//Casting Time
          CostTargetMana
            tmpargument = tmpargument + selfcontent	    //Increase the charge
            SetContent
            
      //Make the target glow
      SetTargetToNearestFriend
        tmpx = targetdistance
        tmpy = 128*4
        IfXIsMoreThanY
          SetTargetToWhoeverIsHolding
      Else
        SetTargetToWhoeverIsHolding
      tmpx = targetx
      tmpy = targety
      tmpdistance = targetz
      tmpargument = 0
      SpawnExactParticle
Else
  tmpx = selfcontent
  tmpy = 255
  IfXIsMoreThanY		//Minimum charge needed
    tmpargument = 50		//Set cooldown time
    SetReloadTime
    
    tmpargument = [XENC]
    IfTargetHasAnyID
      tmpargument = 5
      SendMessageNear		//Tell what happened
    Else
      //Award xp and stuff for daring to do it
      IfNameIsKnown
        DoNothing
      Else
        tmpargument = 20
        tmpdistance = EXPDARE
        GiveExperienceToTarget
        MakeNameKnown
        MakeUsageKnown
      
      //Tell what happened
      tmpargument = 3
      SendMessageNear		
      tmpargument = 1
      PlaySound
      EnchantTarget
      
        //Sparklies!
        tmpargument = GREEN
        SparkleIcon
        
        //Mana cost and power depends on level
        tmpargument = -selfcontent > 2   //Cost depends on charge
        tmpdistance = 0      // Give owner 0 life per second
        tmpx = 0               // Give target 0 mana per second
        tmpy = selfcontent > 2    // Give target half the mana cost life per second
        SetEnchantBoostValues
  Else				
    tmpx = selfcontent
    tmpy = 0
    IfXIsMoreThanY		//Only if there was a charge
      tmpargument = 1
      SendMessageNear		//Tell it didn't work
      tmpargument = 2
      PlaySound
  tmpargument = 0
  SetContent			//Reset counter
  

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

End					// All done
