//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 = 2
          SetState

        tmpargument = 1              //Regular Chest
        IfContentIs
          tmpargument = selfmoney
          DropItems
          DropMoney
          DropKeys
          tmpargument = 2
          SetState
          tmpargument = 0
          SendMessageNear
          tmpargument = 50
          SetTime

        tmpargument = 2              //Armor Chest
        IfContentIs
          tmpargument = [ROGU]
          IfTargetHasID
            tmpargument = 3
            ChangeTargetArmor
            tmpargument = 5
            SendMessageNear
            tmpargument = 2
            SetState
            tmpargument = 50
            SetTime
          Else
            tmpargument = 4		//Wrong armor type
            SendMessageNear
            tmpargument = 50
            SetTime

        tmpargument = 3              //Special Chest
        IfContentIs
          tmpx = rand % 4
          tmpy = 0
          IfXIsEqualToY
            tmpargument = 67		//Chiuaua X2
          tmpy = 1
          IfXIsEqualToY
            tmpargument = 69		//Magic Pick
          tmpy = 2
          IfXIsEqualToY
            tmpargument = 67		//Chiuaua
          tmpy = 3
          IfXIsEqualToY
            tmpargument = 68		//Everburn torch
          tmpdistance = selfz + 30
          tmpx = selfx - 37
          tmpy = selfy
          SpawnExactCharacterXYZ
          tmpargument = 2
          SetState
          tmpargument = 0
          SendMessageNear
          tmpargument = 50
          SetTime

          //And drop treasure!
          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

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

// All done
End
