title: What is the fastest way to calculate fog of war?
body: <p>I have the following function in my FogOfWar class, it calculates if its team can see the position that is passed on the arguments:</p>
  
  <pre><code>package mwoke {
      public class FogOfWar {
          public var game:Game;
          public var team:int;
          private var cache:Vector.&lt;CachePos&gt; = new Vector.&lt;CachePos&gt;();
          private var chars:Vector.&lt;Character&gt; = new Vector.&lt;Character&gt;();
  
          public function FogOfWar($game:Game, $team:int) {
              game = $game;
              team = $team;
          }
  
          public function think():void {
              cache.length = 0;
              chars.length = 0;
  
              var ent:Character;
              for(var i:int = 0; i &lt; game.entities.length;++i) {
                  if(game.entities[i] == null) continue;
                  if(!(game.entities[i] is Character)) continue;
                  ent = Character(game.entities[i]);
                  if(ent.team != team) continue;
                  if(ent.vision &lt;= 0.0) continue;
                  chars.push(ent);
              }
          }
  
          public function canSee(x:Number, y:Number):Boolean {
              if(x &lt; game.minX) return false;
              if(y &lt; game.minY) return false;
              if(x &gt;= game.maxX) return false;
              if(y &gt;= game.maxY) return false;
  
              for each(var v:CachePos in cache) {
                  if(v.x == x &amp;&amp; v.y == y) {
                      return v.result;
                  }
              }
              var res:Boolean = false;
  
              for each(var ent:Character in chars) {
                  if(Game.distance(x, y, ent.x, ent.y) &lt; ent.vision) {
                      res = true;
                      break;
                  }
              }
  
              var tmp:CachePos = new CachePos();
              tmp.x = x;
              tmp.y = y;
              tmp.result = res;
              cache.push(tmp);
              return res;
          }
      }
  }
  internal class CachePos {
      public var x:Number;
      public var y:Number;
      public var result:Boolean;
  }
  </code></pre>
  
  <p>My problem is that, with only 3 characters in the game, it can only run 4000 times per second, which is not too much.</p>
  
  <p>I can implement a cache, so it caches the most commons positions, but, what else can I do to optimize it?</p>