// When the party first goes into any location call this to clear what's around them

function InitFow(%position)
{
   %tile = fow.PickTile(getWord(%position,0), getWord(%position, 1));

   %x = getWord(%tile, 0);
   %y = getWord(%tile, 1);
   
   ClearTile(%x SPC %y);                
   ClearTile(%x+1 SPC %y);                                 
   ClearTile(%x+2 SPC %y);               
   ClearTile(%x+3 SPC %y);               
   ClearTile(%x+4 SPC %y);               
   ClearTile(%x-1 SPC %y);               
   ClearTile(%x-2 SPC %y);               
   ClearTile(%x-3 SPC %y);               
   ClearTile(%x-4 SPC %y);               
   ClearTile(%x SPC %y+1);               
   ClearTile(%x+1 SPC %y+1);                                 
   ClearTile(%x+2 SPC %y+1);               
   ClearTile(%x+3 SPC %y+1);               
   ClearTile(%x+4 SPC %y+1);               
   ClearTile(%x-1 SPC %y+1);               
   ClearTile(%x-2 SPC %y+1);               
   ClearTile(%x-3 SPC %y+1);               
   ClearTile(%x-4 SPC %y+1);               
   ClearTile(%x SPC %y+2);               
   ClearTile(%x+1 SPC %y+2);                                 
   ClearTile(%x+2 SPC %y+2);               
   ClearTile(%x+3 SPC %y+2);               
   ClearTile(%x+4 SPC %y+2);               
   ClearTile(%x-1 SPC %y+2);               
   ClearTile(%x-2 SPC %y+2);               
   ClearTile(%x-3 SPC %y+2);               
   ClearTile(%x-4 SPC %y+2);               
   ClearTile(%x SPC %y+3);               
   ClearTile(%x+1 SPC %y+3);                                 
   ClearTile(%x+2 SPC %y+3);               
   ClearTile(%x+3 SPC %y+3);               
   ClearTile(%x+4 SPC %y+3);               
   ClearTile(%x-1 SPC %y+3);               
   ClearTile(%x-2 SPC %y+3);               
   ClearTile(%x-3 SPC %y+3);               
   ClearTile(%x-4 SPC %y+3);               
   ClearTile(%x SPC %y-1);               
   ClearTile(%x+1 SPC %y-1);                                 
   ClearTile(%x+2 SPC %y-1);               
   ClearTile(%x+3 SPC %y-1);               
   ClearTile(%x+4 SPC %y-1);               
   ClearTile(%x-1 SPC %y-1);               
   ClearTile(%x-2 SPC %y-1);               
   ClearTile(%x-3 SPC %y-1);               
   ClearTile(%x-4 SPC %y-1);               
   ClearTile(%x SPC %y-2);               
   ClearTile(%x+1 SPC %y-2);                                 
   ClearTile(%x+2 SPC %y-2);               
   ClearTile(%x+3 SPC %y-2);               
   ClearTile(%x+4 SPC %y-2);               
   ClearTile(%x-1 SPC %y-2);               
   ClearTile(%x-2 SPC %y-2);               
   ClearTile(%x-3 SPC %y-2);               
   ClearTile(%x-4 SPC %y-2);               
   ClearTile(%x SPC %y-3);               
   ClearTile(%x+1 SPC %y-3);                                 
   ClearTile(%x+2 SPC %y-3);               
   ClearTile(%x+3 SPC %y-3);               
   ClearTile(%x+4 SPC %y-3);               
   ClearTile(%x-1 SPC %y-3);               
   ClearTile(%x-2 SPC %y-3);               
   ClearTile(%x-3 SPC %y-3);               
   ClearTile(%x-4 SPC %y-3);               
   ClearTile(%x SPC %y-4);               
   ClearTile(%x+1 SPC %y-4);                                 
   ClearTile(%x+2 SPC %y-4);               
   ClearTile(%x+3 SPC %y-4);               
   ClearTile(%x+4 SPC %y-4);               
   ClearTile(%x-1 SPC %y-4);               
   ClearTile(%x-2 SPC %y-4);               
   ClearTile(%x-3 SPC %y-4);               
   ClearTile(%x-4 SPC %y-4);               
   ClearTile(%x SPC %y+4);               
   ClearTile(%x+1 SPC %y+4);                                 
   ClearTile(%x+2 SPC %y+4);               
   ClearTile(%x+3 SPC %y+4);               
   ClearTile(%x+4 SPC %y+4);               
   ClearTile(%x-1 SPC %y+4);               
   ClearTile(%x-2 SPC %y+4);               
   ClearTile(%x-3 SPC %y+4);               
   ClearTile(%x-4 SPC %y+4);       
}

function ReplaceRightEdge()
{
   %tile = fow.PickTile(getWord(player.GetPosition(),0), getWord(player.GetPosition(), 1));

   %x = getWord(%tile, 0);
   %y = getWord(%tile, 1);
      
   if(getword(fow.GetTileType(%x+5 SPC %y), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y-1), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y-1, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y-2), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y-2, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y-3), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y-3, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y-4), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y-4, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y-5, terrain_sheet2ImageMap, 51);               

   if(getword(fow.GetTileType(%x+5 SPC %y+1), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y+1, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y+2), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y+2, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y+3), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y+3, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y+4), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y+4, terrain_sheet2ImageMap, 45);               

   if(getword(fow.GetTileType(%x+5 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x+5 SPC %y+5, terrain_sheet2ImageMap, 53);               
}

function ReplaceLeftEdge()
{
   %tile = fow.PickTile(getWord(player.GetPosition(),0), getWord(player.GetPosition(), 1));

   %x = getWord(%tile, 0);
   %y = getWord(%tile, 1);
      
   if(getword(fow.GetTileType(%x-5 SPC %y), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y-1), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y-1, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y-2), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y-2, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y-3), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y-3, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y-4), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y-4, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y-5, terrain_sheet2ImageMap, 50);               

   if(getword(fow.GetTileType(%x-5 SPC %y+1), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y+1, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y+2), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y+2, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y+3), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y+3, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y+4), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y+4, terrain_sheet2ImageMap, 46);               

   if(getword(fow.GetTileType(%x-5 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x-5 SPC %y+5, terrain_sheet2ImageMap, 52);               
}

function ReplaceBottomEdge()
{
   %tile = fow.PickTile(getWord(player.GetPosition(),0), getWord(player.GetPosition(), 1));

   %x = getWord(%tile, 0);
   %y = getWord(%tile, 1);
      
   if(getword(fow.GetTileType(%x SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x+1 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x-1 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x-2 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x-2 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x-3 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x-3 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x-4 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x-4 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x+1 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x+1 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x+2 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x+2 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x+3 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x+3 SPC %y+5, terrain_sheet2ImageMap, 43);               

   if(getword(fow.GetTileType(%x+4 SPC %y+5), 2) !$= "")   
      fow.SetStaticTile(%x+4 SPC %y+5, terrain_sheet2ImageMap, 43);               
}

function ReplaceTopEdge()
{
   %tile = fow.PickTile(getWord(player.GetPosition(),0), getWord(player.GetPosition(), 1));

   %x = getWord(%tile, 0);
   %y = getWord(%tile, 1);
      
   // top
   
   if(getword(fow.GetTileType(%x SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x SPC %y-5, terrain_sheet2ImageMap, 48);               
      
   if(getword(fow.GetTileType(%x-1 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x-1 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x-2 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x-2 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x-3 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x-3 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x-4 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x-4 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x+1 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x+1 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x+2 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x+2 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x+3 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x+3 SPC %y-5, terrain_sheet2ImageMap, 48);               

   if(getword(fow.GetTileType(%x+4 SPC %y-5), 2) !$= "")   
      fow.SetStaticTile(%x+4 SPC %y-5, terrain_sheet2ImageMap, 48);               
}

function ClearTile(%tile)
{
   // if the tile has already been cleared don't add it
      
   if($tilesrecord[%tile] $= true)
      return;
   
   fow.ClearTile(%tile);   
      
   $tiles[$gcounter] = %tile;
   $tilesrecord[%tile] = true;   // so we can keep track of what tiles have been cleared   
   
   $gcounter++;  
}

// this is called after a vehicle or people combat, the tiles get reset after a fresh load so let's 
// load what's in memory

function ClearFow()
{
   for(%i=0;%i<=$gcounter;%i++)
   {
      fow.clearTile($tiles[%i]);                                    
   }
}