function FindInfantryPath(%bot, %destination)
{
   $openlist = new SimGroup();
   $closedlist = new SimGroup();
   
   // get the grid name
   
   %grid = $currentgrid;
   
   // get the origin tile
   
   %OriginTile = %grid.PickTile(%bot.GetPosition());   
   
   %DestTile = %grid.PickTile(%destination);   
   %x = getWord(%DestTile, 0);
   %y = getWord(%DestTile, 1);
   
   // if they click on a block square then return
   
   if(CheckInfantryBlock(%grid, %x, %y) $= true)
   {
      echo("Clicked on blocked square in astar");
      return -1;
   }
   
   // add the parent node to the open list
   
   %parentnode = new ScriptObject();
   %parentnode.tile = %OriginTile;
   %parentnode.h = 0;
   %parentnode.g = 0;
   %parentnode.f = 0;   
         
   %tilex = getWord(%OriginTile, 0);
   %tiley = getWord(%OriginTile, 1);

   $closedlist.Add(%parentnode);
   
   %closed[%tilex, %tiley] = true;
      
   for(%j=1;%j<100;%j++)
   {
      echo("j is " @ %j);
      
      if(CheckInfantryBlock(%grid, %tilex+1, %tiley) $= false)
      {
         if(%closed[%tilex+1, %tiley] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex+1 SPC %tiley;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex+1 SPC %tiley, %DestTile);
            %node.g = %parentnode.g+10;
            %node.f = %node.h+%node.g;
            %closed[%tilex+1, %tiley] = true;
         }
      }

      if(CheckInfantryBlock(%grid, %tilex-1, %tiley) $= false)
      {
         if(%closed[%tilex-1, %tiley] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex-1 SPC %tiley;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex-1 SPC %tiley, %DestTile);
            %node.g = %parentnode.g+10;
            %node.f = %node.h+%node.g;
            %closed[%tilex-1, %tiley] = true;
         }
      }
      
      if(CheckInfantryBlock(%grid, %tilex, %tiley+1) $= false)
      {
         if(%closed[%tilex, %tiley+1] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex SPC %tiley+1;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex SPC %tiley+1, %DestTile);
            %node.g = %parentnode.g+10;
            %node.f = %node.h+%node.g;
            %closed[%tilex, %tiley+1] = true;
         }
      }

      if(CheckInfantryBlock(%grid, %tilex, %tiley-1) $= false)
      {
         if(%closed[%tilex, %tiley-1] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex SPC %tiley-1;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex SPC %tiley-1, %DestTile);
            %node.g = %parentnode.g+10;
            %node.f = %node.h+%node.g;
            %closed[%tilex, %tiley-1] = true;
         }
      }

      if(CheckInfantryBlock(%grid, %tilex+1, %tiley-1) $= false)
      {
         if(%closed[%tilex+1, %tiley-1] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex+1 SPC %tiley-1;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex+1 SPC %tiley-1, %DestTile);
            %node.g = %parentnode.g+14;
            %node.f = %node.h+%node.g;
            %closed[%tilex+1, %tiley-1] = true;
         }
      }
      
      if(CheckInfantryBlock(%grid, %tilex-1, %tiley-1) $= false)
      {
         if(%closed[%tilex-1, %tiley-1] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex-1 SPC %tiley-1;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex-1 SPC %tiley-1, %DestTile);
            %node.g = %parentnode.g+14;
            %node.f = %node.h+%node.g;
            %closed[%tilex-1, %tiley-1] = true;
         }
      }

      if(CheckInfantryBlock(%grid, %tilex-1, %tiley+1) $= false)
      {
         if(%closed[%tilex-1, %tiley+1] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex-1 SPC %tiley+1;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex-1 SPC %tiley+1, %DestTile);
            %node.g = %parentnode.g+14;
            %node.f = %node.h+%node.g;
            %closed[%tilex-1, %tiley+1] = true;
         }
      }

      if(CheckInfantryBlock(%grid, %tilex+1, %tiley+1) $= false)
      {
         if(%closed[%tilex+1, %tiley+1] !$= true) 
         {
            %node = new ScriptObject();
            
            %node.tile = %tilex+1 SPC %tiley+1;
            %node.parentnode = %parentnode;      
            $openlist.add(%node);
            
            %node.h = GenerateHeuristic(%tilex+1 SPC %tiley+1, %DestTile);
            %node.g = %parentnode.g+14;
            %node.f = %node.h+%node.g;
            %closed[%tilex+1, %tiley+1] = true;
         }
      }

      // find the node with the lowest score
      
      %lowestscore = 10000;

      %count = $openlist.GetCount();

      for(%i=0;%i<%count;%i++)
      {
         %node = $openlist.GetObject(%i);
         
         if(%node.f < %lowestscore)
         {
            %index = %i;
            %lowestscore = %node.f; 
         }
      }
      
      // make this the new parent square
            
      %parentnode = $openlist.GetObject(%index);
      
      // drop the parent square into the closed list

      $openlist.Remove(%parentnode);        
      $closedlist.Add(%parentnode);
      
      // update tilex and tiley
      
      %tilex = getWord(%parentnode.tile, 0);
      %tiley = getWord(%parentnode.tile, 1);
            
      %tile = %tilex SPC %tiley;
      
      if(%tile $= %DestTile)
      {
         $currentgrid = %grid;
         
         %path = GenerateInfantryPath(%bot, %parentnode);
            
         return %path;
      }
   }
   
   // no path found
   
   echo("PATH NOT FOUND");
   return -1;
}

// for some reason on some interiors when clicking outside the room it finds a path
// if a parentnode is a -1 return a failure even though it's trying to generate a path

function GenerateInfantryPath(%bot, %endnode)
{
   echo("NEW INFANTRY PATH GENERATED");
   
   %i = 1;
   
   %x = getWord(%endnode.tile, 0);
   %y = getWord(%endnode.tile, 1);
   %dest = getTileCenter($currentgrid, %x, %y);  

   %path = %dest;
   
   // give %parentnode a value to start the while loop
   
   %parentnode = %endnode;
      
   while(%parentnode !$= "")
   {
      %i++;
      
      %parentnode = %parentnode.parentnode;   
      
      if(%parentnode $= -1)
      {
         echo("Final path not found");
         return -1;
      }
      
      if(%parentnode !$= "")
      {
         %x = getWord(%parentnode.tile, 0);
         %y = getWord(%parentnode.tile, 1);
         %dest = getTileCenter($currentgrid, %x, %y);  

         %path = %dest SPC %path;
      }
      
      if(%i > 100)
         return -1;
   }   
   
   // the first 2 coordinates need to be removed since they are the origin
   
   %path = removeWord(%path, 0);
   %path = removeWord(%path, 0);
   
   echo("INFANTRY PATN RETURNED");
      
   return %path;
}

// when moving, either combat or non-combat, this will check the square to see if it's occupied

function CheckInfantryBlock(%grid, %tilex, %tiley)
{
   %block = %grid.GetTileCustomData(%tilex, %tiley);
      
   if(%block $= "block")
      return true;     
   else 
      return false;
      
}