//Set type of chest at spawn
IfSpawned
  tmpargument = 0
  IfStateIsNot
    GetState
    SetContent
    tmpargument = 0
    SetState

//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 % 11
          tmpy = 0
          IfXIsEqualToY
            tmpx = rand & 1		//Only 50% if vengance is rolled...
            tmpy = 1
            IfXIsEqualToY
              tmpargument = 95		//Vengance!
            Else
              tmpx = rand % 10 + 1	//Nope, make it one of the below
          tmpy = 1
          IfXIsEqualToY
            tmpargument = 77		//Flying Carpet
          tmpy = 2
          IfXIsEqualToY
            tmpargument = 83		//Mana Elixir
          tmpy = 3
          IfXIsEqualToY
            tmpargument = 84		//Vorporal Blade!
          tmpy = 4
          IfXIsEqualToY
            tmpargument = 88		//Myolnir
          tmpy = 5
          IfXIsEqualToY
            tmpargument = 87		//Life Elixir
          tmpy = 6
          IfXIsEqualToY
            tmpargument = 89		//Arcane Nova Orb
          tmpy = 7
          IfXIsEqualToY
            tmpargument = 92		//Dark Ritual
          tmpy = 8
          IfXIsEqualToY
            tmpargument = 85		//Amulet of Yore
          tmpy = 9
          IfXIsEqualToY
            tmpargument = 97		//Dragon tongue
          tmpy = 10
          IfXIsEqualToY
            tmpargument = 114		//Gungnr
          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
