//Sustained by magic!
IfSpawned
  SetTargetToSelf
  SetOwnerToTarget
  EnchantTarget

  //It doesnt open when dead
  tmpargument = passage
  ClosePassage

  //Create the anti-teleport blocker
  tmpargument = 0
  IfArmorIs
    tmpargument = passage + 1
    ClosePassage

  //Setup so that [GOOD] guys can sacrifice their soul to open
  tmpargument = 0
  tmpx = 0
  tmpy = 2
  SetXY

//Become yellow if attacked by holy!
IfAttacked
  GetDamageType
  tmpx = tmpargument
  tmpy = DAMAGEHOLY
  IfXIsEqualToY

    //Twice in a row kills the door (always check this first)
    tmpargument = 3
    IfArmorIs
      SetTargetToSelf
      KillTarget
      SetTargetToWhoeverAttacked
      tmpargument = 30
      tmpdistance = EXPREVENGE
      GiveExperienceToTarget

    //Someone is trying to kill the door
    tmpargument = 0
    IfArmorIs
      tmpargument = 3
      ChangeArmor
      SetTargetToWhoeverAttacked
      tmpargument = 6
      SendMessageNear
      PlaySound
      tmpargument = 300
      SetTime

    //Someone killing an open door?
    tmpargument = 1
    IfArmorIs
      SetTargetToSelf
      KillTarget

//Holy attacks need to be rapid, or they have no effect
tmpargument = 3
IfArmorIs
  IfTimeOut
    tmpargument = 0
    ChangeArmor        //Back in buisness!

//Someone disenchanted us! How rude! (or used holy magic)
IfKilled
  tmpargument = 30
  tmpdistance = EXPKILL
  GiveExperienceToGoodTeam
  SetTargetToSelf
  tmpargument = 2
  ChangeArmor
  RespawnTarget         //Award xp and look dead, but respawn so that scripts still work
  tmpargument = 0
  PlaySound
  SendMessageNear

//It's still a riddeling locked door
tmpargument = 0
IfArmorIs
  IfTimeOut
    tmpargument = 80
    SetTime
    tmpargument = passage + 2
    SetTargetToWhoeverIsInPassage
      tmpturn = 0
      tmpdistance = 0       //0 = Riddle not solved    1 = Riddle solved
      tmpx = selfcontent    //Which riddle

      //Riddle of the dead (opens up to dead stuff only)
      tmpy = 7
      IfXIsEqualToY
        IfTargetIsAlive
          tmpargument = [UNDE]
          IfTargetHasID
            tmpdistance = 1     //Undead work fine
        Else
          tmpdistance = 1       //It's a body, works fine for me...

      //Riddle of the darkness (opens up to light sources only)
      tmpy = 8
      IfXIsEqualToY
        tmpargument = [TORC]
        IfTargetHoldingItemID
          tmpdistance = 1     //Torches work fine
        tmpargument = [LAMP]
        IfTargetHoldingItemID
          tmpdistance = 1     //Lamps work fine
        tmpargument = [MAGI]
        IfTargetHoldingItemID
          tmpargument = [BALL]
          IfTargetHoldingItemID
            tmpdistance = 1     //Assume it's a book of light and open up!

      //Riddle of the greed (opens up to valuable items only)
      tmpy = 9
      IfXIsEqualToY
        IfTargetIsOnHatedTeam       //Don't check enemies
          DoNothing
        Else
          tmpargument = 0
          GetTargetArmorPrice
          tmpy = 249
          IfXIsMoreThanY
            tmpargument = 0	      //Spawn a spell effect
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle

            PoofTarget
              tmpargument = 1
              ChangeArmor
              tmpargument = 2
              SendMessage
              PlayFullSound
              tmpargument = 0           //Do not say "yes I am opening" next time
              SetContent
          Else
            tmpargument = 1	      //Spawn a spell effect
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle
          tmpdistance = 0

      //Tell the riddle to non special IDSZ [GOOD] players
      IfTargetIsAPlayer
        tmpargument = [GOOD]
        IfTargetHasSpecialID          //Paladins and healers break the door
          tmpargument = 0
          GetXY
          IfXIsEqualToY
            tmpargument = targetlevel*targetlevel*50
            tmpargument = 0 - tmpargument   //Lose xp (higher level = more)
            tmpdistance = EXPROLEPLAY
            GiveExperienceToTarget
            tmpdistance = 0 - tmpargument   //Remember for message later
            tmpargument = 90*targetlevel   //Take 1 damage per 3 levels
            DamageTarget
            tmpargument = 1
            ChangeArmor
            tmpargument = 0           //Do not say "yes I am opening" next time
            SetContent
            tmpargument = 8       //Target traded it's soul!
          Else
            tmpy = 0
            IfXIsMoreThanY
              tmpargument = 7     //Offer to trade soul for opening
            Else
              tmpargument = 4     //Will not open to the likes of you

          SendMessage
          PlayFullSound
          tmpargument = 250
          SetTime

          tmpargument = 0
          GetXY
          tmpx = tmpx+1
          tmpy = 2
          SetXY
        Else
          IfDistanceIsMoreThanTurn      //Proceeds if tmpdisdance is not 0
            tmpargument = 1
            ChangeArmor
            tmpargument = 2
            SendMessage
            PlayFullSound
            tmpargument = 0           //Do not say "yes I am opening" next time
            SetContent

            //Award xp for solving puzzle
            tmpargument = 40
            tmpdistance = EXPSECRET
            GiveExperienceToGoodTeam

          Else                    //Still riddeling...
            tmpargument = selfcontent
            SendMessageNear
            PlaySound
            tmpargument = 250
            SetTime

    //Reset the soul leech counter
    Else
      tmpargument = 0
      GetXY
      IfXIsLessThanY
        tmpx = 0
        tmpy = 2
        SetXY

//Open the door
tmpargument = 1
IfArmorIs
  tmpargument = passage + 2
  SetTargetToWhoeverIsInPassage
    IfTargetCanOpenStuff
      tmpargument = 0
      GetXY
      IfXIsMoreThanY                  //Open up for soul traders
        tmpargument = passage
        OpenPassage
      Else
        tmpargument = [GOOD]
        IfTargetHasSpecialID            //Still refuses to open to really good guys
          tmpargument = passage
          ClosePassage
          IfTimeOut
            tmpargument = 4
            SendMessage
            PlaySound
            tmpargument = 250
            SetTime
        Else
          tmpargument = passage
          OpenPassage
  Else
    tmpargument = passage
    ClosePassage


// Someone cast an unlock spell
IfOrdered
  tmpx = selforder
  tmpy = 491  // The unique code for an unlock order
  IfXIsEqualToY
    tmpargument = 2
    IfArmorIs                   //dead, so open up!
      tmpargument = passage
      OpenPassage
      tmpargument = 3
      SendMessageNear
      tmpargument = 50
      tmpdistance = EXPSECRET
      GiveExperienceToGoodTeam
    tmpargument = 0             //still alive, resist magic
    IfArmorIs
      tmpargument = 5
      PlaySound
      SendMessageNear


// Handle the animation and sound
KeepAction
tmpargument = passage	// Is the passage clear?
IfPassageOpen			//
  tmpargument = 0		  //
  IfStateIs			  // State 0 ( Closed )
    tmpargument = ACTIONMG	    //
    DoAction			    //
      tmpargument = 1		      // Open it
      SetState			      //
      tmpargument = 3		      //
      PlaySound			      //

      //Open up the anti-teleport blocker
      tmpargument = passage + 1
      OpenPassage

      //Randomly say "I am opening..."
      tmpargument = 0
      IfContentIs
        tmpargument = 1
        SetContent
      Else             //Only if not solved this update
        tmpargument = 1
        IfArmorIs               //Only if alive
          tmpx = rand & 255
          tmpy = 102
          IfXIsLessThanY         //40% chance
            tmpargument = 1
            SendMessageNear
            PlaySound
Else
  tmpargument = 1		  //
  IfStateIs			  // State 1 ( Open )
    tmpargument = ACTIONMJ	    //
    DoAction			    //
      tmpargument = 0		      // Close it
      SetState			      //
      tmpargument = 3		      //
      PlaySound
      

End				// Finished with this character
