anchor.diff:          end;
       end;
   
  -  until( (OCount+8>=length(OList))or //List can't hold any more cells,
  +  until( //List can't hold any more cells? removed
            IsDestinationReached or // Destination point is reached
            (MinCost.Cost=65535)); // There's no more open cells available
   
  
positive.code: end;
  
  
  procedure TPathFinding.ReturnRoute(out NodeList:TKMPointList);
  var i,k:integer; NodesCount:integer;
  begin
    NodeList.Clearup;
  
    if not fRouteSuccessfullyBuilt then
    begin
      NodeList.Clearup; //Something went wrong
      exit;
    end;
  
    //Calculate NodeCount
    k:=MinCost.ID; NodesCount:=0;
    repeat
      inc(NodesCount);
      k:=OList[k].Parent;
    until(k=0);
  
    {if NodeCount > length(Nodes) then begin
      NodeCount:=0; //Something went wrong
      Nodes[0]:=LocA;
      exit;
    end;}
  
    //Assemble the route reversing the list, since path is LocB>LocA in fact
    k:=MinCost.ID;
    for i:=1 to NodesCount do begin
      NodeList.AddEntry(OList[k].Pos);
      //Nodes[NodeCount-i]:=OList[k].Pos;
      k:=OList[k].Parent;
    end;
  
    NodeList.Inverse;
      //Nodes[0]:=LocA;
  
  
    //Should ajoin straight pieces to reduce mem usage
    //Important rule:
    // - First node is LocA,
    // - next to last node is neighbour of LocB (important for delivery to workers),
    // - last node is LocB
  
    {if NodeCount>3 then begin
    k:=2;
    for i:=3 to NodeCount-1 do begin //simplify within LocA+1 .. LocB-2 range
    // i/k are always -1 since array is [0..Count-1] range
      if (sign(Nodes[k-1].X-Nodes[k-2].X) = sign(Nodes[i-1].X-Nodes[i-2].X))and //Direction matches
         (sign(Nodes[k-1].Y-Nodes[k-2].Y) = sign(Nodes[i-1].Y-Nodes[i-2].Y)) then begin
        Nodes[k-1]:=Nodes[i-1];
      end else begin
        inc(k);
        Nodes[k-1]:=Nodes[i-1];
      end;
    end;
    inc(k);
    Nodes[k-1]:=Nodes[NodeCount-1];
    NodeCount:=k;
    end;}
  end;
  
  
  
  function TPathFinding.CheckRouteCanExist():boolean;
  begin
    //Don't try to make a route if it's obviously impossible
  
negative.code: end;
  
  
  procedure TPathFinding.ReturnRoute(out NodeList:TKMPointList);
  var i,k:integer; NodesCount:integer;
  begin
    NodeList.Clearup;
  
    if not fRouteSuccessfullyBuilt then
    begin
      NodeList.Clearup; //Something went wrong
      exit;
    end;
  
    //Calculate NodeCount
    k:=MinCost.ID; NodesCount:=0;
    repeat
      inc(NodesCount);
      k:=OList[k].Parent;
    until(k=0);
  
    {if NodeCount > length(Nodes) then begin
      NodeCount:=0; //Something went wrong
      Nodes[0]:=LocA;
      exit;
    end;}
  
    //Assemble the route reversing the list, since path is LocB>LocA in fact
    k:=MinCost.ID;
    for i:=1 to NodesCount do begin
      NodeList.AddEntry(OList[k].Pos);
      //Nodes[NodeCount-i]:=OList[k].Pos;
      k:=OList[k].Parent;
    end;
  
    NodeList.Inverse;
      //Nodes[0]:=LocA;
  
  
    //Should ajoin straight pieces to reduce mem usage
    //Important rule:
    // - First node is LocA,
    // - next to last node is neighbour of LocB (important for delivery to workers),
    // - last node is LocB
  
    {if NodeCount>3 then begin
    k:=2;
    for i:=3 to NodeCount-1 do begin //simplify within LocA+1 .. LocB-2 range
    // i/k are always -1 since array is [0..Count-1] range
      if (sign(Nodes[k-1].X-Nodes[k-2].X) = sign(Nodes[i-1].X-Nodes[i-2].X))and //Direction matches
         (sign(Nodes[k-1].Y-Nodes[k-2].Y) = sign(Nodes[i-1].Y-Nodes[i-2].Y)) then begin
        Nodes[k-1]:=Nodes[i-1];
      end else begin
        inc(k);
        Nodes[k-1]:=Nodes[i-1];
      end;
    end;
    inc(k);
    Nodes[k-1]:=Nodes[NodeCount-1];
    NodeCount:=k;
    end;}
  end;
  
  
  end.
  
  