// Game logic for level 1 of The Streets of Noir
// Only commands available are north, east, south, and west
// Each location has a patrolman, who will send you back to start if he catches you.
// There's a running clock, with each step taking 1 minute
// Each patrolman takes a nap at regualar intervals, once every 3, 4, or 5 minutes.
// You have to reach the goal, only visiting locations at the right times, so that
//   you never encounter a patrolman who's awake.

// The town of Noir is laid out in a simple grid.
function townloc(x, y) {
  ordinal = ["0th", "1st", "2nd", "3rd", "4th", "5th", "6th"]
  return "The corner of " + ordinal[x] + " avenue and " + ordinal[y] + " street"
}

// Each intersection may or may not have a night watchman
patrol = [0, 0, 3, 0, 0, 5, 4, 5, 4, 5, 4, 0, 0, 3, 5, 3, 0, 5]

// Whether the patrolman is awake depends on the time
function pawake() {
  return this.patrol && gamestate.turn % this.patrol
}

// Sometimes there's a bell tower in the background
function chimestring(when) {
  if (when % 15) return ""
  r = "\n\nIn the distance, a bell chimes "
  switch ((when / 15) % 4) {
    case 0: return r + "the hour."
    case 1: return r + "once. It's 15 minutes past the hour."
    case 2: return r + "twice. It's 30 minutes past the hour."
    case 3: return r + "thrice. It's 45 minutes past the hour."
  }
}

// Some descriptional and flavor text
startinfo = "You're standing in front of the inn where you're living, in the town of Noir. " +
  "It's the middle of the night."
finishinfo = "As you step out onto the dock facing east, you see the sun rise over the water. " +
  "In the distance, a rooster crows. It's a new day.\n\nCongratulations! You have beaten level 1."
clues = ["The men in blue sleep one in three....",
  "The men in gray sleep one in four....",
  "The men in white sleep one in five....",
  "When the big hand reaches 12, they all nod off!",
  "I love to watch the sun rise at the northeast dock.",
  "Out after curfew? You just need to be in the right place at the right time.",
  "Don't give up! The guards have a short memory.",
  "Seems like the night never ends around here, doesn't it?",
  "Whoever waits for the sun will always find himself in darkness.",
  "When I was younger I could travel a block every minute."]

function randomclue() {
  return clues[Math.floor(Math.random() * clues.length)]
}

function towndes() {
  d = ""
  if (this.patrol) {
    ucolor = {3: "blue", 4: "gray", 5: "white"}[this.patrol]
    d += "There is a patrolman in a " + ucolor + " uniform leaning against a wall."
    if (this.pawake()) {
      d += " He sees you and shouts, 'You there! You're breaking curfew! I'm taking you home!'"
    } else {
      d += " He seems to be soundly asleep."
    }
  } else if (Math.random() < 0.5) {
    d += "The streets are deserted, except for an old man wandering past, muttering" +
      " to himself: " + randomclue()
  } else {
    d += "The streets are totally deserted."
  }
  
  if (!this.pawake()) d += chimestring(gamestate.turn)
    
  return d
}

function towntick() {
  gamestate.turn += 1
}

// What happens when you enter a room
function townhap() {
  // Send you back to start if you cross a patrolman
  if (this.pawake()) {
    pausecom(function() { gamestate.at = 0 ; showroom() })
  }
  return false
}

function builddungeon() {
  // The streets in the 3x6 grid follow a simple pattern
  for (y = 1, t = 0; y <= 6; ++y) {
    for (x = 1; x <= 3; ++x, ++t) {
      e = [y < 6, x < 3, y > 1, x > 1]
      w = [t + 3, t + 1, t - 3, t - 1]
      rooms[t] = new room(townloc(x,y), towndes, e, w, towntick, townhap)
      rooms[t].patrol = patrol[t]
      rooms[t].pawake = pawake
    }
  }
  // Starting room description
  rooms[0].des = function () { return startinfo + chimestring(gamestate.turn) }
  // Add the goal east of the northeast corner
  rooms[t] = new room(townloc(4, 6), finishinfo, [0, 0, 0, 0], [0, 0, 0, 0], false, false)
  rooms[t-1].exits[1] = true
  rooms[t-1].whither[1] = t
}

function initializestate() {
  gamestate.level = 1
  gamestate.at = 0
  gamestate.turn = 15
}


