//Chest that is locked with a chest key [KEYC]
//State 0 is closed and state 1 is open
//Content 0: Only drop items (with secret bottom)
//Content 1: Show Map (with secret bottom)
//Content 2: Secret bottom (Drops the rest)
//Content 3: Totally empty
//Content 4: Only drop money (with secret bottom)

// Someone cast an unlock spell
IfOrdered
  tmpx = selforder
  tmpy = 491  // The unique code for an unlock order
  IfXIsEqualToY
    // Is it closed?
    tmpargument = 0
    IfStateIs
      // Open it up
      tmpargument = ACTIONMG
      DoAction
        tmpargument = 1
        SetState
        tmpargument = 60
        SetBumpHeight
        KeepAction
        tmpargument = 0
        tmpdistance = 11025
        PlaySound
        // Wait a while
        tmpargument = 40
        SetTime
        // Tell the players...
        tmpargument = 3
        SendMessageNear

// Someone is trying to open it
IfBumped
  // Can they open the chest?
  SetTargetToWhoeverBumped
  IfTargetCanOpenStuff

    // Is it closed?
    tmpargument = 0
    IfStateIs

      // Are they in front?
      IfFacingTarget

        // Can they pick the lock?
        tmpargument = [DISA]
        IfTargetHasSkillID

          // Make target crouch
          tmpargument = ACTIONMG
          TargetDoAction
          tmpargument = 1
          PlaySound

          // Give some experience
          tmpargument = 25
          tmpdistance = EXPROLEPLAY
          GiveExperienceToTarget

          // Open it up
          tmpargument = ACTIONMG
          DoAction
            tmpargument = 1
            SetState
            tmpargument = 60
            SetBumpHeight
            KeepAction
            tmpargument = 0
            tmpdistance = 11025
            PlaySound
            // Wait a while
            tmpargument = 40
            SetTime
            // Tell the players...
            tmpargument = 4
            SendMessageNear
        Else

          // Do they have a chest key?
          tmpargument = [KEYC]
          CostTargetItemID

            // Make target crouch
            tmpargument = ACTIONMG
            TargetDoAction
            tmpargument = 1
            PlaySound

            // Give some experience
            tmpargument = 5
            tmpdistance = EXPDARE
            GiveExperienceToTarget

            // Open it up
            tmpargument = ACTIONMG
            DoAction
              tmpargument = 1
              SetState
              tmpargument = 60
              SetBumpHeight
              KeepAction
              tmpargument = 0
              tmpdistance = 11025
              PlaySound
              // Wait a while
              tmpargument = 40
              SetTime
              // Tell the players...
              tmpargument = 5
              SendMessageNear
          IfTimeOut
            // Say it's locked...
            tmpargument = 2
            SendMessageNear
            // Wait a while
            tmpargument = 40
            SetTime
    Else
      // Wait before allowing to loot
      IfTimeOut
        // Reset the timer
        tmpargument = 40
        SetTime

        // Make target crouch
        tmpargument = ACTIONMG
        TargetDoAction

        tmpargument = 0			//Content 0: Chest that drops just items and has secret bottom
        IfContentIs
          DropItems
          DropKeys
          tmpargument = 2
          SetContent
          tmpargument = 0
          SendMessageNear

        tmpargument = 1		//Content 1: chest contains map and secret bottom
        IfContentIs
          ShowMap
            tmpargument = 25
            tmpdistance = EXPSECRET
            GiveExperienceToTarget
            ShowYouAreHere
            tmpargument = 2
            SetContent
            tmpargument = 2
            PlaySound
          Else
            tmpargument = 8
            SendMessageNear

        tmpargument = 2		//Content 2: Secret bottom
        IfContentIs
          tmpx = rand % 4352 + targetwis
          tmpargument = [DISA]
          IfTargetHasSkillID		//Rouges always make it
            tmpx = 9000
          tmpy = 8704			//50% chance at 17 wis
          IfXIsMoreThanY
            DropItems			//Drop loot
            DropMoney
            DropKeys
            tmpargument = 7		//Secret bottom!
            SendMessageNear
            tmpargument = 15
            tmpdistance = EXPSECRET
            GiveExperienceToTarget	//Award some xp
            tmpargument = 3
            SetContent
          Else
            tmpargument = 1
            SendMessageNear

        tmpargument = 3		//Content 3: Totally looted
        IfContentIs
          tmpargument = 1
          SendMessageNear

        tmpargument = 4		//Content 4: Only drop money and still has secret bottom
        IfContentIs
          DropMoney
          tmpargument = 2
          SetContent
          tmpargument = 0
          SendMessageNear

// All done
End
