//Begin as unactivated
IfSpawned
  tmpargument = [STAR]
  SetState

//Do new game updates
tmpargument = [STAR]
IfStateIsNot
  IfTimeOut
    tmpargument = 75
    SetTime

    tmpx = selfstate
    tmpy = selfcontent
    IfXIsMoreThanY
      tmpargument = selfcontent
      tmpx = rand % 4 + 1
      tmpy = 0
      SetXY
      
      tmpdistance = [CHIM]
      tmpargument = tmpx
      OrderSpecialID
      
      tmpargument = selfcontent + 1
      SetContent
      
      //We are done for this game
      tmpx = selfstate
      tmpy = selfcontent
      IfXIsEqualToY
        tmpargument = 0
        SetState
        
    //This allows truesight characters to see the next solution
    Else    
      tmpdistance = 128*8
      SetTargetToDistantEnemy
        IfTargetCanSeeInvisible
          IfTargetCanSeeKurses
            tmpdistance = [CHIM]
            tmpargument = selfstate
            GetXY
            tmpargument = tmpx+10
            OrderSpecialID

  IfOrdered
    tmpargument = selfstate
    GetXY
    tmpy = selforder
    IfXIsEqualToY
      tmpargument = selfstate + 1
      SetState
      
      //0 means we reached the last one
      GetXY
      tmpy = 0         
      IfXIsEqualToY
      
        //Did we reach the final game?
        tmpargument = 6 + difficulty          //Modify this to set the puzzle level limit! (limit=state+1)
        IfStateIs
          SetTargetToOwner
          tmpargument = 120
          tmpdistance = EXPSECRET
          GiveExperienceToTarget
          
          //Open the next challenge
          tmpargument = passage
          OpenPassage
          
          //Tell what happened (yay we did it)
          tmpargument = 3
          PlayFullSound
          tmpargument = 1
          SendMessage
          
          //No longer needed
          GoPoof
          
        Else
          //Nope, but success! The player won this level, go to next one
          tmpargument = 1
          PlayFullSound
          
          //New game
          tmpargument = 0
          SetContent
          
          //One more chime next time
          tmpargument = selfstate + 1
          SetState
          
          //Let the player take a breath before going again
          tmpargument = 150
          SetTime  

    //They did the wrong one!
    Else
      
      //Notify that they did wrong
      SetTargetToOwner
      tmpargument = 0
      PlayFullSound
      SendMessage
      
      //Reset game
      tmpargument = 0
      SetContent
      
      //Reset puzzle order
      tmpargument = 0
      tmpx = 0
      tmpy = 0
      SetXY
      tmpargument = 1
      SetXY
      tmpargument = 2
      SetXY
      tmpargument = 3
      SetXY
      tmpargument = 4
      SetXY
      tmpargument = 5
      SetXY
      tmpargument = 6
      SetXY
      tmpargument = 7
      SetXY
      tmpargument = 8
      SetXY
      tmpargument = 9
      SetXY
      tmpargument = 10
      SetXY
      
      //Reset puzzle progression (just reducing it seems to cause bugs?!)
      tmpargument = 1
      SetState
        
      //Let the player take a breath before going again
      tmpargument = 150
      SetTime
      
      //Spawn some punishment
      tmpargument = 0
      tmpx = targetx
      tmpy = targety
      tmpdistance = selfz
      SpawnExactParticle
      
Else
  //Puzzle starts at level 1
  IfTimeOut
    tmpargument = 25
    SetTime
    tmpargument = 3
    SetTargetToWhoeverIsInPassage
      IfTargetIsAPlayer
        tmpargument = 1
        SetState
        SetOwnerToTarget
  
End					// All done
