anchor.diff:  end;
   
   
  -procedure TNavMeshPathFinding.ReturnRoute(out aRouteArray: TKMWordArray; out aRoutePointArray: TKMPointArray);
  +procedure TNavMeshPathFinding.ReturnRoute(out aDistance: Word; out aRoutePointArray: TKMPointArray);
   const
     USED_PENALIZATION = 40;
   var
     Count: Word;
  -  N: TNavMeshNode;
  -  Output: TKMWordArray;
  -begin
  +  N, FinalN: TNavMeshNode;
  +begin
  +  aDistance := 0;
  +  if (fMinN = nil) then
  +    Exit;
  +  FinalN := fMinN;
  +  N := fMinN.Parent;
  +  Count := 1;
  +  while (N <> nil) do
  +  begin
  +    aDistance := aDistance + KMDistanceAbs(N.Point, FinalN.Point); // DistanceAbs is very rough but when is result different from actual distance there are probably obstacles in path and it is better to count with higher distance
  +    FinalN := N;
  +    N := N.Parent;
  +    Inc(Count,1);
  +  end;
  +
  +  SetLength(aRoutePointArray, Count+1);
     N := fMinN;
     Count := 0;
     while (N <> nil) do
     begin
  +    aRoutePointArray[Count+1] := N.Point;
       N := N.Parent;
       Inc(Count,1);
     end;
  -
  -  SetLength(aRoutePointArray, Count+1);
  +  aRoutePointArray[Count] := gAIFields.NavMesh.Polygons[ FinalN.Idx ].CenterPoint;
     aRoutePointArray[0] := gAIFields.NavMesh.Polygons[fMinN.Idx].CenterPoint;
  -
  -  SetLength(Output, Count);
  -  N := fMinN;
  -  Count := 0;
  -  while (N <> nil) do
  -  begin
  -    aRoutePointArray[Count+1] := N.Point;
  -    Output[Count] := N.Idx;
  -    N := N.Parent;
  -    Inc(Count,1);
  -  end;
  -  aRoutePointArray[Count] := gAIFields.NavMesh.Polygons[ Output[Count-1] ].CenterPoint;
  -end;
  -
  -
  -function TNavMeshPathFinding.Route_Make(aStart, aEnd: TKMPoint; out aRouteArray: TKMWordArray; out aRoutePointArray: TKMPointArray): Boolean;
  -var Output: Boolean;
  +end;
  +
  +
  +function TNavMeshPathFinding.InitRoute(aStart, aEnd: TKMPoint; out aDistance: Word; out aRoutePointArray: TKMPointArray): Boolean;
  +var
  +  Output: Boolean;
   begin
     Result := False;
  -  fStart := gAIFields.NavMesh.FindClosestPolygon(aStart);
  -  fEnd := gAIFields.NavMesh.FindClosestPolygon(aEnd);
  +  fStart := gAIFields.NavMesh.Point2Polygon[ aStart.Y, aStart.X ];
  +  fEnd := gAIFields.NavMesh.Point2Polygon[ aEnd.Y, aEnd.X ];
     if (fStart = High(Word)) OR (fEnd = High(Word)) then  // Non-Existing polygon
       Exit;
  +  fMode := pm_ShortestWay;
     Output := MakeRoute();
     if Output then
     begin
  -    ReturnRoute(aRouteArray, aRoutePointArray);
  +    ReturnRoute(aDistance, aRoutePointArray);
       aRoutePointArray[0] := aEnd;
     end;
     Result := Output;
   end;
   
   
  +function TNavMeshPathFinding.ShortestRoute(aStart, aEnd: TKMPoint; out aDistance: Word; out aRoutePointArray: TKMPointArray): Boolean;
  +begin
  +  Result := InitRoute(aStart, aEnd, aDistance, aRoutePointArray);
  +end;
  +
  +
  +function TNavMeshPathFinding.AvoidTrafficRoute(aOwner: TKMHandIndex; aStart, aEnd: TKMPoint; out aDistance: Word; out aRoutePointArray: TKMPointArray): Boolean;
  +begin
  +  fOwner := aOwner;
  +  fMode := pm_AvoidTraffic;
  +  Result := InitRoute(aStart, aEnd, aDistance, aRoutePointArray);
  +end;
  +
  +
  +function TNavMeshPathFinding.AvoidEnemyRoute(aOwner: TKMHandIndex; aGroup: TGroupType; aStart, aEnd: TKMPoint; out aDistance: Word; out aRoutePointArray: TKMPointArray): Boolean;
  +begin
  +  fOwner := aOwner;
  +  fMode := pm_AvoidSpecEnemy;
  +  fGroupType := aGroup;
  +  Result := InitRoute(aStart, aEnd, aDistance, aRoutePointArray);
  +end;
  +
   end.
  
positive.code:   DX := Abs(SPoint.X - EPoint.X);
    DY := Abs(SPoint.Y - EPoint.Y);
    if (DX > DY) then
      Output := (DX shl 3) + (DY shl 2)
    else
      Output := (DY shl 3) + (DX shl 2);
    Result := Output;
  end;
  
  
negative.code: interface
  uses
    Math, KM_CommonTypes, KM_Points,
    BinaryHeap, KM_Defaults;
  
  
  type
    TPathfindingMode = ( pm_ShortestWay,   // Find shortest way in NavMesh
                         pm_AvoidTraffic,  // Try avoid traffic
                         pm_AvoidSpecEnemy // Try avoid anti type units (cav will keep distance form spears etc)
                       );
  
    TNavMeshNode = class
      Idx,Estim: Word; // Index of navMeshNode in navmesh, estimated cost from navMeshNode to end
      CostTo: Longword; // Cost to Idx navMeshNode
      Point: TKMPoint; // Point in navMeshNode of navmesh (each triangle [created by points in navMeshNode] have its point -> multiple poits will create path)
      Parent: TNavMeshNode; // Parent navMeshNode
    end;
  