title: Javascript scripting error
body: <p>After reading up on Field of View algorithms, I decided to create one myself for a game I'm working on. After a couple of hours, I came up with the following script:</p>
  
  <pre><code>function CalculateFOV() {
      ClearFOV();
  
      map[player.x][player.y].light = 100;
  
      var open = new Array();
      var closed = new Array();
  
      sourceNeighbors = map[player.x][player.y].neighbors;
  
      for(var i = 0;i&lt;sourceNeighbors.length;i++) {
          open.push(sourceNeighbors[i]);
      }
  
      while(open.length &gt; 0) {
          var curTile = open[0];
  
          var highestLightValue = 0;
  
          for(j in curTile.neighbors) {
              if(curTile.neighbors[j].light &gt; highestLightValue) {
                  highestLightValue = neighbors[j];               
              }
          }
  
          curTile.light = highestLightValue-10;
  
          if(curTile.light &gt; 10) {
              for(var j = 0;j&lt;curTile.neighbors.length;j++) {
                  var addCell = true;
  
                  if(FindValue(closed, open[0].neighbors[j])) addCell = false;
  
                  if(addCell) {
                      open.push(neighbors[j]);
                  }
              }
          }
  
          closed.push(curTile);
          open.shift();
      }
  }
  
  function ClearFOV() {
      for(var x = 0;x&lt;mapSizeX;x++) {
          for(var y = 0;y&lt;mapSizeY;y++) {
              map[x][y].lightValue = 0;
          }
      }
  }
  
  function FindValue(list, value) {
      for(var i = 0;i&lt;list.length;i++) {
          if(list[i] == value) {
              return true;
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>It's supposed to spread a light-value outward from a source, decreasing as it goes. It uses a Closed list of tiles which have already been given a light value in order to avoid hitting a single cell multiple times. This should <em>theoretically</em> increase the efficiency a lot.</p>
  
  <p>Unfortunately, there seems to be an error, perhaps more than one, with it. My tired brain is failing to locate it (or them), so I would really appreciate some help with this. Does it even make sense?</p>
  
  <p>Also, in case you might need it, here's the Tile class:</p>
  
  <pre><code>function Tile(x,y,character, thisMap, blocked, blockSight) {
      this.x = x;
      this.y = y;
      this.character = character;
      this.blocked = blocked;
      this.blockSight = blockSight;
      this.lightValue = 25;
  
      this.neighbors = new Array();
  }
  
  Tile.prototype = {
      create: function(blocked, blockSight, character) {
          this.blocked = blocked;
          this.blockSight = blockSight;
          this.character = character;
  
          var rockTile = RandomRange(1,4);
          var rockTileStr = "rock"+rockTile;
      },
  
      draw: function() {
          var id = '#t' + this.x + '_' + this.y;
  
          var lightColor = this.lightValue/100;
  
          $(id).css("opacity", lightColor); 
      },
  
      assign: function() {
          var north = this.x-1;
          var south = this.x+1;
          var east = this.y+1;
          var west = this.y-1;
  
          if(north &gt;= 0 &amp;&amp; south &lt;= mapSizeX &amp;&amp; west &gt;= 0 &amp;&amp; east &lt;= mapSizeY) {
  
              //1st: we add the northwest neighbor.
              // *|/
              // -#-
              // /|\
              this.neighbors[0] = map[north][west];
  
              //2nd: we add the north neighbor.
              // \*/
              // -#-
              // /|\
              this.neighbors[1] = map[north][this.y];
  
              //3rd: we add the northeast neighbor.
              // \|*
              // -#-
              // /|\
              this.neighbors[2] = map[north][east];
  
              //4th: we add the east neighbor.
              // \|/
              // -#*
              // /|\
              this.neighbors[3] = map[this.x][east];
  
              //5th: we add the southeast neighbor.
              // \|/
              // -#-
              // /|*
              this.neighbors[4] = map[south][east];
  
              //6th: we add the south neighbor.
              // \|/
              // -#-
              // /*\
              this.neighbors[5] = map[south][this.y];
  
              //7th: we add the southwest neighbor.
              // \|/
              // -#-
              // *|\
              this.neighbors[6] = map[south][west];
  
              //8th: we add the west neighbor.
              // \|/
              // *#-
              // /|\
              this.neighbors[7] = map[this.x][west];
          }
      },
  }
  </code></pre>
  
  <p>Thanks, and apologies for posting such a long question; I just don't know where else to turn at the moment.</p>