//This function makes an item fall to the floor when spawned
IfSpawned
  tmpargument = ACTIONJB
  DoAction
  KeepAction
  MakeCrushValid
  
//Crushed in a passage
IfCrushed
  tmpargument = 3
  tmpdistance = selfz
  tmpx = selfx
  tmpy = selfy
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  GoPoof
  tmpargument = 6
  SendMessageNear
  
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

  //Undo previous berserk spell
  UndoEnchant
    SetTargetToOwner
    tmpargument = selfcontent
    TargetJoinTeam
    UnsparkleIcon
    tmpargument = 50
    SetReloadTime
    
  Else  
    SetTargetToWhoeverIsHolding
    tmpargument = [HMAG]
    IfTargetHasSkillID				//Need divine spell skill
      tmpx = selfcontent
      tmpy = 512					//Max charge
      IfXIsLessThanY
        tmpx = selfcontent
        tmpy = targetmanaflow			//Enough spell power?
        IfYIsMoreThanX
          tmpargument = 4				//Casting Time
          CostTargetMana
            tmpargument = selfcontent + tmpargument	    //Increase the charge
            SetContent
            
      //Make the target glow
      SetTargetToNearbyEnemy
        DoNothing
      Else
        SetTargetToWhoeverIsHolding
      tmpx = rand & 1
      tmpy = 0
      IfXIsEqualToY
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 0
        SpawnExactParticle
  
Else
  tmpx = selfcontent
  tmpy = 511
  IfXIsMoreThanY		//Minimum charge needed
    tmpargument = 50		//Set cooldown time
    SetReloadTime
    
    //Only kurse enemies
    SetTargetToNearbyEnemy  
      
      //See if they are immune
      tmpargument = [XENC]
      IfTargetHasAnyID
        tmpargument = 5
        SendMessageNear		//Tell what happened
        tmpargument = 2
        PlaySound
      Else
        //Award xp and stuff for daring to do it
        SetOldTarget
        SetTargetToWhoeverIsHolding
        IfNameIsKnown
          DoNothing
        Else
          tmpargument = 20
          tmpdistance = EXPDARE
          GiveExperienceToTarget
          MakeNameKnown
          MakeUsageKnown
        tmpargument = 50
        SetReloadTime
        SetTargetToOldTarget
        
        //Check if they resist it
        tmpx = targetint > 8
        tmpx = rand % 10 + tmpx       //1-10 + INT
        SetTargetToWhoeverIsHolding
        tmpy = targetwis > 8
        tmpy = rand % 10 + tmpy      //1-10 + WIS
        SetTargetToOldTarget
        IfXIsLessThanY
        
          //Remember what base team they had
          tmpargument = targetteam
          SetContent
          SetOwnerToTarget            //Remember who we berserked

          EnchantTarget
            //Sparklies!
            tmpargument = RED
            SparkleIcon

            //Make them join TEAMX
            tmpargument = TEAMX
            TargetJoinTeam          
            
            //Sound effect
            tmpargument = 3
            SendMessageNear		
            tmpargument = 1
            PlaySound
            
            //Berserk duration = 0.5 second per wisdom
            SetTargetToWhoeverIsHolding
            tmpargument = targetwis > 8
            tmpargument = tmpargument * 25
            SetTime
            
          //Enchanting failed
          Else
            tmpargument = 1
            SendMessageNear		//Tell it didn't work
            tmpargument = 2
            PlaySound
          
        //Resisted!  
        Else
          tmpargument = 5
          SendMessageNear		
          tmpargument = 2
          PlaySound
          
    //Not valid enemy
    Else
      tmpargument = 1
      SendMessageNear		//Tell it didn't work
      tmpargument = 2
      PlaySound

  //Not charged enough
  Else
    tmpx = selfcontent
    tmpy = 0
    IfXIsMoreThanY		//Only if there was a charge
      tmpargument = 1
      SendMessageNear		//Tell it didn't work
      tmpargument = 2
      PlaySound

  //Reset counter
  tmpargument = 0
  SetContent			

IfTimeOut
  //Undo previous berserk spell
  UndoEnchant
    SetTargetToOwner
    tmpargument = selfcontent
    TargetJoinTeam
    UnsparkleIcon
    
IfPutAway
  //Undo previous berserk spell
  UndoEnchant
    SetTargetToOwner
    tmpargument = selfcontent
    TargetJoinTeam
    UnsparkleIcon
  

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

End					// All done
