//Set type of chest at spawn
IfSpawned
  tmpargument = 2		//Randomize the chest type
  tmpx = rand % 100		//55% chance for artifact
  tmpy = 55
  IfXIsMoreThanY		      //35% for armor chest
    tmpargument = 1
    tmpy = 90
    IfXIsMoreThanY		//10% for mere gold & treasure
      tmpargument = 0
  SetContent


//Spawn sparklies
IfTimeOut
  IfStateIs0
    tmpargument = 0
    IfArmorIs
      tmpx = rand % 53 + selfx - 26
      tmpy = rand % 53 + selfy - 26
      tmpdistance = rand & 30 + 15
      SpawnExactParticle
      tmpargument = 7
      SetTime

//Open yourself
IfBumped
  SetTargetToWhoeverBumped
  IfTargetCanOpenStuff
    IfFacingTarget			//Open up
      tmpargument = ACTIONMG
      TargetDoAction			//Make target crouch

      IfStateIs0			//Unopened
        tmpargument = ACTIONMG
        DoAction
        tmpargument = 60
        SetBumpHeight
        KeepAction
        tmpargument = 1
        SetState

        //Wait some time
        tmpargument = 40
        SetTime

        // Tell the players...
        tmpargument = 2
        SendMessageNear
        tmpargument = 0
        IfArmorIs
          tmpargument = 0		//Magic chest sound
        Else
          tmpargument = 3		//Normal chest sound
        PlaySound

    IfTimeOut

      //Unlooted Open Chest
      IfStateIs1

        tmpargument = 0              //Treasure Chest
        IfContentIs
          tmpargument = selfmoney
          DropMoney
          tmpargument = rand % 3 + 1
          tmpdistance = SPAWNORIGIN
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = rand % 3 + 1
          SpawnParticle
          tmpargument = 2
          SetState

	      //Random artifact chest
        tmpargument = 1
        IfContentIs
          tmpx = rand % 7
          tmpy = 0
          IfXIsEqualToY
            tmpargument = 86		//Mana Elixir
          tmpy = 1
          IfXIsEqualToY
            tmpargument = 85		//Vorporal Blade!
          tmpy = 2
          IfXIsEqualToY
            tmpargument = 87     //Metamorph orb
          tmpy = 3
          IfXIsEqualToY
            tmpargument = 88		//The Pike
          tmpy = 4
          IfXIsEqualToY
            tmpargument = 89		//Sojurn
          tmpy = 5
          IfXIsEqualToY
            tmpargument = 90		//Gemstonebow
          tmpy = 6
          IfXIsEqualToY
            tmpargument = 91		//Iceball Wand
          tmpdistance = selfz + 30
          tmpx = selfx - 37
          tmpy = selfy
          SpawnExactCharacterXYZ
          tmpargument = 2
          SetState
          tmpargument = 0
          SendMessageNear
          tmpargument = 50
          SetTime

	      //Armor chest (level 4)
        tmpx = selfcontent
        tmpy = 1
        IfXIsMoreThanY

          //randomize armor type
          tmpargument = 2
          IfContentIs
            tmpargument = rand % 13 + 4
            SetContent

          //It's already set
          tmpx = selfcontent
          tmpy = 4
          IfXIsEqualToY
            tmpargument = [ARCH]
          tmpy = 5
          IfXIsEqualToY
            tmpargument = [WIZA]
          tmpy = 6
          IfXIsEqualToY
            tmpargument = [ROGU]
          tmpy = 7
          IfXIsEqualToY
            tmpargument = [SOLD]
          tmpy = 8
          IfXIsEqualToY
            tmpargument = [ADVE]
          tmpy = 9
          IfXIsEqualToY
            tmpargument = [GNOM]
          tmpy = 10
          IfXIsEqualToY
            tmpargument = [HEAL]
          tmpy = 11
          IfXIsEqualToY
            tmpargument = [PALA]
          tmpy = 12
          IfXIsEqualToY
            tmpargument = [ELFF]
          tmpy = 12
          IfXIsMoreThanY
            tmpargument = [XXXX]    //Fits all classes

          //Try to give it
          IfTargetHasID
            tmpargument = 3
            ChangeTargetArmor
            tmpargument = 5
            SendMessageNear
            tmpargument = 2
            SetState
            tmpargument = 50
            SetTime
          Else
            tmpturn = tmpx
            tmpx = tmpargument
            tmpy = [XXXX]         //Do it anyways, fits all classes
            IfXIsEqualToY
              tmpargument = 3
              ChangeTargetArmor
              tmpargument = 5
              SendMessageNear
              tmpargument = 2
              SetState
              tmpargument = 50
              SetTime
            Else
              tmpargument = tmpturn		//Wrong armor type
              SendMessageNear
              tmpargument = 80
              SetTime

      //Empty chest
      IfTimeOut
        IfStateIs2
          tmpargument = 1
          SendMessageNear
          tmpargument = 50
          SetTime

// All done
End
