// Someone cast an unlock spell
IfOrdered
  tmpx = selforder
  tmpy = 491  // The unique code for an unlock order
  IfXIsEqualToY
    // Is it closed?
    tmpargument = 0
    IfStateIs
      // Play the trap action
      tmpargument = ACTIONKD
      DoAction
        tmpargument = 0
        SetBumpHeight
        // Wait a while
        tmpargument = 500
        SetTime
        // Tell the players...
        tmpargument = 3
        SendMessageNear
        // Spawn the trap particle
        tmpx = selfx
        tmpy = selfy
        tmpdistance = selfz
        tmpargument = 0
        SpawnExactParticle
        // Wake up sleepers
        tmpargument = [WAKE]
        tmpdistance = [WAKE]
        OrderSpecialID



// 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


        // Make target crouch
        tmpargument = ACTIONMG
        TargetDoAction


        // Check for disarm skill
        tmpx = 0
        tmpy = 8192  // 50% chance at 16 Dex
        tmpargument = [DISA]
        IfTargetHasSkillID
          tmpx = rand & 8191 + targetdex



        // Check if it was disarmed
        IfXIsMoreThanY


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


          // Open it up
          tmpargument = ACTIONMG
          DoAction
            // The trap has been disarmed
            tmpargument = 4
            SendMessageNear
            // Open up for real
            tmpargument = 1
            SetState
            tmpargument = 60
            SetBumpHeight
            KeepAction
            tmpargument = 0
            tmpdistance = 11025
            PlaySound
            // Wait a while
            tmpargument = 40
            SetTime
        Else
          // Give some experience
          tmpargument = 5
          tmpdistance = EXPDARE
          GiveExperienceToTarget


          // Play the trap action
          tmpargument = ACTIONKD
          DoAction
            // The trap has been sprung
            tmpargument = 0
            SetBumpHeight
            // Wait a while
            tmpargument = 500
            SetTime
            // Tell the players...
            tmpargument = 5
            SendMessageNear
            // Spawn the trap particle
            tmpx = selfx
            tmpy = selfy
            tmpdistance = selfz
            tmpargument = 0
            SpawnExactParticle
            // Wake up sleepers
            tmpargument = [WAKE]
            tmpdistance = [WAKE]
            OrderSpecialID
    Else
      // Wait before allowing a swap
      IfTimeOut
        // Reset the timer
        tmpargument = 40
        SetTime


        // Make target crouch
        tmpargument = ACTIONMG
        TargetDoAction




        // Say it's empty...  Or check for false bottom
        IfStateIs2
          // Say the chest is empty...
          tmpargument = 0
          SendMessageNear
          // Content... 0 is normal chest, 2 is false bottom
          GetContent
          tmpargument = tmpargument & 2
          SetState
          IfStateIs2
            // It has a false bottom
            tmpargument = [DISA]
            IfTargetHasSkillID
              // Character easily finds the false bottom
              tmpargument = 1
              SendMessageNear


              // Figure out what to drop...  Opposite of first drop
              GetContent
              tmpargument = tmpargument & 1
              SetState
              IfStateIs0
                DropItems
              Else
                tmpargument = selfmoney
                DropMoney



              // No more false bottom
              tmpargument = 0
              SetContent
              tmpargument = 2
              SetState
            Else
              // Check wisdom until it is found
              tmpx = targetwis
              tmpy = rand & 4095 + 3584  // Need between 14 and 30 wisdom
              IfXIsMoreThanY
                // Character finds the false bottom
                tmpargument = 1
                SendMessageNear


                // Figure out what to drop...  Opposite of first drop
                GetContent
                tmpargument = tmpargument & 1
                SetState
                IfStateIs0
                  DropItems
                Else
                  tmpargument = selfmoney
                  DropMoney



                // No more false bottom
                tmpargument = 0
                SetContent
                tmpargument = 2
                SetState
          IfStateIs0
            // No false bottom
            tmpargument = 2
            SetState




        // Drop the contents
        IfStateIs1
          // Content...  0 is money, 1 is items
          GetContent
          tmpargument = tmpargument & 1
          SetState
          IfStateIs0
            tmpx = selfmoney
            tmpy = 0
            IfXIsMoreThanY
              // Drop the money
              tmpargument = selfmoney
              DropMoney
              tmpargument = 2
              SendMessageNear
            Else
              // No money in a money chest...
              tmpargument = 0
              SendMessageNear
          Else
            // Found some items
            DropItems
            tmpargument = 2
            SendMessageNear
          tmpargument = 2
          SetState




// All done
End
