//The States and Contents
//State 0: Closed chest
//State 1: Opened Chest
//State 2: Empty Chest
//Content 0: Locked chest
//Content 1: Unlocked Chest

//Using a key to unlock the chest
IfBumped
  IfTargetCanOpenStuff
    tmpargument = 0
    IfContentIs
      tmpargument = [KEYC]
      IfTargetHasItemID
        CostTargetItemID
          tmpargument = 1  //Unlocked
          SetContent
          PlaySound
        

// 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 = 1
        SetContent
        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
      tmpargument = 0
      IfContentIs  						//LOCKED
        // Are they in front?
        IfFacingTarget
          IfTimeOut
            // Make target crouch and tell the players...
            tmpargument = ACTIONMG
            TargetDoAction  
            tmpargument = 1
            SendMessageNear 
            tmpargument = 40
            SetTime

      Else //UNLOCKED
        // Are they in front?
        IfFacingTarget

            
          // Make target crouch
          tmpargument = ACTIONMG
          TargetDoAction

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

            
          // Open it up
          tmpargument = ACTIONMG
          DoAction
            tmpargument = 1
            SetState
            tmpargument = 60
            SetBumpHeight
            KeepAction
            tmpargument = 0
            PlaySound
            // Wait a while
            tmpargument = 40
            SetTime
            // Tell the players...
            tmpargument = 4
            SendMessageNear
                    
        
      // Wait before allowing
      IfTimeOut
        IfFacingTarget
          // Reset the timer
          tmpargument = 40
          SetTime
            
          // Make target crouch
          tmpargument = ACTIONMG
          TargetDoAction

          // Say it's empty...
          IfStateIs2
            tmpargument = 0
            SendMessageNear

    // Drop the contents
    IfStateIs1
      DropItems
      DropMoney
      DropKeys
      tmpargument = 2
      SendMessageNear
      tmpargument = 2 //Empty now...
      SetState

// All done
End
