//The States and Contents
//	State 0: Closed chest
//	State 1: Opened Chest
//	State 2: Looted Chest
//	selfmoney = armor level (0-3, 4 is random and 5 is random + super armor)
//	selflevel = armor type based on class (0 = random... see list below)
//	+1 content = locked
//	+2 content = map
//	+4 content = fire trapped
//	+8 content = secret bottom
//	+16 content = invisible
//	+32 content = contains armor (!invalidates map and secret bottom!)
//	+64 content = armor costs money (only works with +32 content!)
//	+128 content = rune warded chest

//------------------------------------------------------------------------------------
//ARMOR TYPES DEPEND ON SELFLEVEL
//	0 = randomize armor type
//	1 = Archaeologist.
//	2 = G'nome.
//	3 = Soldier.
//	4 = Elf.
//	5 = Adventurer.
//	6 = Wizard.
//	7 = Healer.
//	8 = Rogue.
//	9 = Paladin.
//	10= Tourist.
//	11= Valkyrie.
//	12= All above excluding 9, 10 and 11

//------------------------------------------------------------------------------------
// Do special setup for certain chests
IfSpawned

  //Rune chest
  tmpx = selfcontent & 128
  tmpy = 128
  IfXIsEqualToY
    tmpargument = 1
    ChangeArmor

  //Invisible chest
  tmpx = selfcontent & 16
  tmpy = 16
  IfXIsEqualToY
    tmpargument = 0
    SetAlpha
    SetLight

  //Armor chest (remember armor type)
  tmpx = selfcontent & 32
  tmpy = 32
  IfXIsEqualToY
    tmpx = selflevel
    tmpy = 0
    IfXIsEqualToY
      tmpx = rand % 11 + 1      //Randomize  (use in loot)
    tmpy = 12
    IfXIsEqualToY
      tmpx = rand % 8 + 1       //Randomize  (use in shops)
    tmpy = 1
    IfXIsEqualToY
      tmpargument = [ARCH]  //Archaelogist
      tmpturn = 24             //Which message to display when failed to loot
    tmpy = 2
    IfXIsEqualToY
      tmpargument = [GNOM]  //G'nome
      tmpturn = 25             //Which message to display when failed to loot
    tmpy = 3
    IfXIsEqualToY
      tmpargument = [SOLD]  //Soldier
      tmpturn = 26             //Which message to display when failed to loot
    tmpy = 4
    IfXIsEqualToY
      tmpargument = [ELFF]  //Elf
      tmpturn = 27             //Which message to display when failed to loot
    tmpy = 5
    IfXIsEqualToY
      tmpargument = [ADVE]  //Adventurer
      tmpturn = 28             //Which message to display when failed to loot
    tmpy = 6
    IfXIsEqualToY
      tmpargument = [WIZA]  //Wizard
      tmpturn = 29             //Which message to display when failed to loot
    tmpy = 7
    IfXIsEqualToY
      tmpargument = [HEAL]  //Healer
      tmpturn = 30             //Which message to display when failed to loot
    tmpy = 8
    IfXIsEqualToY
      tmpargument = [ROGU]  //Rogue
      tmpturn = 31             //Which message to display when failed to loot
    tmpy = 9
    IfXIsEqualToY
      tmpargument = [PALA]  //Paladin
      tmpturn = 32             //Which message to display when failed to loot
    tmpy = 10
    IfXIsEqualToY
      tmpargument = [TOUR]  //Tourist
      tmpturn = 33             //Which message to display when failed to loot
    tmpy = 11
    IfXIsEqualToY
      tmpargument = [VALK]  //Valkyrie
      tmpturn = 34             //Which message to display when failed to loot
    
    //do it
    tmpx = tmpargument
    tmpy = tmpturn
    tmpargument = 0
    SetXY
    
    //randomize armor level?
    tmpx = selfmoney
    tmpy = 4
    IfXIsEqualToY
      tmpargument = rand % 3
      SetMoney
    Else
      IfXIsMoreThanY
        tmpargument = rand % 4
        SetMoney

//------------------------------------------------------------------------------------
// Someone cast an unlock spell
IfOrdered
  tmpx = selforder
  tmpy = 491  // The unique code for an unlock order
  IfXIsEqualToY
    // Is it closed?
    tmpargument = 0
    IfStateIs
    
      //Is it rune warded?
      tmpargument = 1
      IfArmorIs
        tmpargument = 23
        SendMessageNear
      Else
        //Fire trapped?
        tmpx = selfcontent & 4
        tmpy = 4
        IfXIsEqualToY
          tmpargument = ACTIONKD
          DoAction
            // Tell the players...
            tmpargument = 11
            SendMessageNear

            // Spawn the trap particle
            tmpx = selfx
            tmpy = selfy
            tmpdistance = selfz
            tmpargument = 0
            SpawnExactParticle

            // Wake up sleepers
            tmpargument = [WAKE]
            tmpdistance = [WAKE]
            OrderSpecialID
        
        // Open it up
        Else 
          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
  Else
  
    // The unique code for a looted shop
    tmpx = selforder
    tmpy = 983  
    IfXIsEqualToY
      tmpx = selfcontent & 64
      tmpy = 64
      IfXIsEqualToY
        tmpargument = selfcontent - 64
        SetContent
    Else
    
      //Was it disenchanted?
      tmpargument = 1
      IfArmorIs
        tmpx = selforder
        tmpy = [UNDO]
        IfXIsEqualToY
          tmpargument = 0
          ChangeArmor
          tmpargument = 35
          SendMessageNear
          
//------------------------------------------------------------------------------------
// 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      
        
        //Rune warded?
        tmpargument = 1
        IfArmorIs
          tmpargument = [KEYR]      //Need a rune key
          CostTargetItemID
            tmpargument = 2
            ChangeArmor         //Deactivate the magical runes
            
            //Tell what happened
            tmpargument = 35
            SendMessageNear
            tmpargument = 4
            PlaySound
            
            //Give som xp
            tmpargument = 10
            tmpdistance = EXPSECRET
            GiveExperienceToTarget
          Else
            IfTimeOut
              tmpargument = 50
              SetTime
              tmpargument = 23    //Tell them it's warded
              SendMessageNear
        Else    
          //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            
            
            //Check for firetrap
            tmpx = selfcontent & 4
            tmpy = 4
            IfXIsEqualToY
              // Check for disarm skill
              tmpx = 0
              tmpy = 75  // 75% chance to fail
              tmpargument = [DISA]
              IfTargetHasSkillID
                tmpargument = targetdex > 7            //+2% chance of success per dexterity point
                tmpargument = tmpargument + targetlevel //+1% chance of success per level
                tmpx = rand % 100 + 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 = 10
                  SendMessageNear
                  
                  // Open up for real
                  tmpargument = 1
                  SetState
                  tmpargument = 60
                  SetBumpHeight
                  KeepAction
                  tmpargument = 0
                  PlaySound
                  
                  // Wait a while
                  tmpargument = 40
                  SetTime
              Else
                // 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 = 9
                  SendMessageNear
                  
                  // Spawn the trap particle
                  tmpx = selfx
                  tmpy = selfy
                  tmpdistance = selfz
                  tmpargument = 0
                  SpawnExactParticle
                  
                  // Wake up sleepers
                  tmpargument = [WAKE]
                  tmpdistance = [WAKE]
                  OrderSpecialID
            Else          
              // 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

          //Is it an armor chest?
          tmpx = selfcontent & 32
          tmpy = 32
          IfXIsEqualToY
            tmpargument = 0
            GetXY                   //tmpx is class IDSZ and tmpy is which message to play if failed
            tmpargument = tmpx
            IfTargetHasID
            
              //Do they need to pay for it?
              tmpx = selfcontent & 64
              tmpy = 64
              IfXIsEqualToY
                // Check money
                tmpargument = selfmoney     //armor level
                TargetPayForArmor
                  tmpdistance = tmpy  // The base cost of the new armor

                  // Swap armor
                  ChangeTargetArmor
                  
                  //ChangeTargetArmor sets tmpargument to the current armor type in the chest
                  //So not we use selfmoney to make the script remember the armor level
                  SetMoney
                  
                  tmpargument = 14 + tmpx
                  tmpx = tmpdistance
                  SendMessageNear
                Else
                  // Not enough money...
                  tmpargument = 18
                  SendMessageNear
              Else
                //Swap armor
                tmpy = selflevel            //which class
                tmpargument = selfmoney&3   //which level
                ChangeTargetArmor
                SetContent
                tmpargument = selfmoney&3 + 19
                SendMessageNear
            Else
              //Wrong armor type for class
              tmpargument = tmpy
              SendMessageNear

          //No armor, no traps, and no lock; just loot it already!
          Else
          
            // Say it's empty...
            IfStateIs2
              // Say the chest is empty...
              tmpargument = 0
              SendMessageNear
              
            //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
                  
                //Tell about the secret bottom!
                tmpargument = 12
                SendMessageNear
                
                //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

            // Drop the contents
            IfStateIs1
            
              //Contains map
              tmpx = selfcontent & 2
              tmpy = 2
              IfXIsEqualToY
                ShowMap
                  ShowYouAreHere
                  // Give Experience
                  tmpargument = 20
                  tmpdistance = EXPSECRET
                  GiveExperienceToTarget
                  
                  //Take away the map
                  tmpx = selfcontent & 8
                  tmpy = 8
                  IfXIsEqualToY
                    tmpargument = selfcontent - 2
                    SetContent              //Wait! There is still a secret bottom!
                  Else
                    tmpargument = 2         //Truly empty...
                    SetState
                  
                  // Tell everyone
                  tmpargument = 7
                  SendMessage
                  tmpargument = 2
                  PlayFullSound
                Else
                  // Don't need two maps...
                  tmpargument = 8
                  SendMessageNear
              Else
                tmpargument = 2
                SendMessageNear
              
                //Secret bottom?
                tmpx = selfcontent & 8
                tmpy = 8
                IfXIsEqualToY
                  tmpargument = rand & 1 + 3    //Set the secret bottom
                  SetState
                  IfStateIs3    //Drop items first
                    DropItems
                    DropKeys
                  IfStateIs4    //Drop money first
                    tmpargument = selfmoney
                    DropMoney
                Else
                  tmpargument = selfmoney
                  DropMoney
                  DropItems
                  DropKeys
              
                  //Truly empty now...
                  tmpargument = 2
                  SetState

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