//Start the music
IfSpawned
  tmpargument = 10
  tmpdistance = 0
  PlayMusic

// Setup the gate frames ( tile animation )
IfSpawned
  tmpx = 128
  tmpy = 130
  tmpargument = 0
  SetXY
  tmpx = 132
  tmpy = 134
  tmpargument = 1
  SetXY
  tmpx = 144
  tmpy = 146
  tmpargument = 2
  SetXY
  tmpx = 148
  tmpy = 150
  tmpargument = 3
  SetXY
  tmpx = 160
  tmpy = 162
  tmpargument = 4
  SetXY
  tmpx = 164
  tmpy = 166
  tmpargument = 5
  SetXY
  tmpx = 176
  tmpy = 178
  tmpargument = 6
  SetXY
  tmpx = 180
  tmpy = 182
  tmpargument = 7
  SetXY
  tmpargument = 50
  SetTime
  
IfTimeOut
  IfStateIs0
    // Wait until the closest player moves
    tmpargument = 4
    SetTime
    tmpargument = [HUMA]
    tmpdistance = 14  // Items, Friends, Enemies
    SetTargetToNearestBlahID
      tmpx = targetx - 4608
      tmpy = targety - 7104
      tmpx = xydistance
      tmpy = 80
      IfXIsMoreThanY
        // Play close sound
        tmpargument = 0
        PlaySound
        // Close the door
        tmpargument = ACTIONMJ
        DoActionOverride
        tmpargument = 4
        SetFrame
        tmpargument = 1
        SetState
  IfStateIs1
    tmpargument = 2
    SetTime
    GetContent
    tmpargument = tmpargument + 1
    SetContent
    tmpx = tmpargument
    tmpy = 8
    IfXIsLessThanY
      // Update the map image
      tmpargument = tmpx
      GetXY
      tmpargument = tmpx
      tmpdistance = tmpy
      tmpx = 4480
      tmpy = 7296
      SetTileXY
      tmpargument = tmpdistance
      tmpx = 4608
      SetTileXY
    Else
      tmpx = tmpx - 8 * 42
      tmpargument = 254 - tmpx
      SetLight
End
