//!!NOTE: MODULE MODIFIED SCRIPT!!
//------------------------------------------------------------------------------------
//Invisible chest
IfSpawned
  tmpx = selfcontent & 16
  tmpy = 16
  IfXIsEqualToY
    tmpargument = 0
    SetLight

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

        //Unlock?
        tmpx = selfcontent & 1
        tmpy = 1
        IfXIsEqualToY
          tmpargument = selfcontent - 1
          SetContent

//------------------------------------------------------------------------------------
// Someone is trying to open it
IfBumped
  // Can they open the chest?
  SetTargetToWhoeverBumped
  IfTargetCanOpenStuff
    IfFacingTarget
      // Is it closed?
      tmpargument = 0
      IfStateIs
        //Animate the target
        tmpargument = ACTIONMG
        TargetDoAction      
        
        //Locked?
        tmpx = selfcontent & 1
        tmpy = 1
        IfXIsEqualToY
          tmpargument = [DISA]
          IfTargetHasSkillID          //They picked the lock
          
            //Unlock it
            tmpargument = selfcontent - 1
            SetContent
            tmpargument = 1
            PlaySound
            tmpargument = 6
            SendMessageNear
            
            //Award some xp
            tmpargument = 25
            tmpdistance = EXPROLEPLAY
            GiveExperienceToTarget
            
          Else          
            tmpargument = [KEYC]
            CostTargetItemID          //They used a key
            
              //Unlock it
              tmpargument = selfcontent - 1
              SetContent
              tmpargument = 1
              PlaySound
              tmpargument = 5
              SendMessageNear
              
              //Award some xp
              tmpargument = 10
              tmpdistance = EXPSECRET
              GiveExperienceToTarget
      
            Else
              IfTimeOut           //Tell it's locked
                tmpargument = 1
                SendMessageNear
                tmpargument = 40
                SetTime
                              
        //Unlocked!
        Else 
          // Give some experience
          tmpargument = 5
          tmpdistance = EXPDARE
          GiveExperienceToTarget
          
          //Animate
          tmpargument = ACTIONMG
          TargetDoAction

          //Invisible chests!
          tmpx = selfcontent & 16
          tmpy = 16
          IfXIsEqualToY
            tmpargument = 13
            SendMessageNear
            tmpargument = 30
            tmpdistance = EXPSECRET
            GiveExperienceToTarget
            tmpargument = selfcontent - 16
            SetContent            
          
          // 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
      Else                
        // Wait before allowing loot
        IfTimeOut
          // Reset the timer
          tmpargument = 40
          SetTime
            
          // Make target crouch
          IfFacingTarget
            tmpargument = ACTIONMG
            TargetDoAction

            //Secret bottom
            tmpx = selfstate
            tmpy = 2
            IfXIsMoreThanY
              tmpargument = [DISA]
              IfTargetHasSkillID
                tmpx = 65535              // Character easily finds the false bottom
              Else
                tmpx = targetwis
              tmpy = rand & 4095 + 3584   // Need between 14 and 30 wisdom
              IfXIsMoreThanY
              
                //Drop opposite of last
                IfStateIs3
                  tmpargument = selfmoney
                  DropMoney
                Else
                  DropItems
                  DropKeys
                
                //Truly empty now
                tmpargument = 2
                SetState
                
                //award some xp
                tmpargument = 10
                tmpdistance = EXPSECRET
                GiveExperienceToTarget
              Else
                tmpargument = 2       //They did not find it... tell its empty
                SendMessageNear
              
            // Say it's empty...
            IfStateIs2
              // Say the chest is empty...
              tmpargument = 0
              SendMessageNear

            // Drop the contents
            IfStateIs1
            
              //Contains map
              tmpx = selfcontent & 2
              tmpy = 2
              IfXIsEqualToY
                ShowMap
                  ShowYouAreHere
                  // Give Experience
                  tmpargument = 20
                  tmpdistance = EXPSECRET
                  GiveExperienceToTarget

                  // Tell everyone
                  tmpargument = 7
                  SendMessage
                  tmpargument = 2
                  PlayFullSound
                Else
                  // Don't need two maps...
                  tmpargument = 8
                  SendMessageNear
              Else
              
                //Secret bottom?
                tmpx = selfcontent & 8
                tmpy = 8
                IfXIsEqualToY
                  tmpargument = 2
                  SendMessageNear
                  tmpargument = rand & 1 + 3    //Set the secret bottom
                  SetState
                  IfStateIs3    //Drop items first
                    DropItems
                    DropKeys
                  IfStateIs4    //Drop money first
                    tmpargument = selfmoney
                    DropMoney
                Else
                  tmpargument = 2
                  SendMessageNear
                  
                  //SPAWN THE ANCIENT SCROLL
                  tmpargument = 108
                  tmpdistance = selfz + 75
                  tmpy = rand & 128 + selfy - 64
                  tmpx = rand & 128 + selfx - 64
                  SpawnExactCharacterXYZ
              
              //Empty now...
              IfStateIs1          //no secret bottom
                tmpargument = 2
                SetState

//------------------------------------------------------------------------------------
// All done
End
//------------------------------------------------------------------------------------
