anchor.diff:          else
             Citizens.Add(U);
         end;
  -    end;
     end;
   
     if aClosest then
  
positive.code:   end;
  
    //Scan one tile further than the maximum radius due to rounding
    lx := max(round(aLoc.X-(MaxRad+1)),1); //1.42 gets rounded to 1
    ly := max(round(aLoc.Y-(MaxRad+1)),1); //1.42 gets rounded to 1
    hx := min(round(aLoc.X+(MaxRad+1)),fMapX); //1.42 gets rounded to 1
    hy := min(round(aLoc.Y+(MaxRad+1)),fMapY); //1.42 gets rounded to 1
  
    for i:=ly to hy do for k:=lx to hx do
    if (Land[i,k].IsUnit <> nil) then
    begin
      //Check archer sector. If it's not within the 90 degree sector for this direction, then don't use this tile (continue)
      dX := k-aLoc.X;
      dY := i-aLoc.Y;
      case Dir of
        dir_N : if not((abs(dX)<=-dY)and(dY<0)) then continue;
        dir_NE: if not((dX>0)        and(dY<0)) then continue;
        dir_E:  if not((dX>0)and (abs(dY)<=dX)) then continue;
        dir_SE: if not((dX>0)        and(dY>0)) then continue;
        dir_S : if not((abs(dX)<=dY) and(dY>0)) then continue;
        dir_SW: if not((dX<0)        and(dY>0)) then continue;
        dir_W:  if not((dX<0)and(abs(dY)<=-dX)) then continue;
        dir_NW: if not((dX<0)        and(dY<0)) then continue;
      end;
  
      U := Land[i,k].IsUnit;
  
      //Alliance is the check that will invalidate most candidates, so do it early on
      if (U = nil)
  
negative.code: { Should scan withing given radius and return closest unit with given Alliance status
    Should be optimized versus usual UnitsHitTest
    Prefer Warriors over Citizens}
  function TTerrain.UnitsHitTestWithinRad(aLoc: TKMPoint; MinRad, MaxRad: Single; aPlayer: TPlayerIndex; aAlliance: TAllianceType; Dir: TKMDirection; const aClosest: Boolean): Pointer;
  var
    I,K: Integer; //Counters
    LowX,LowY,HighX,HighY: Integer; //Ranges
    dX,dY: Integer;
    RequiredMaxRad: Single;
    U,C,W: TKMUnit; //CurrentUnit, BestWarrior, BestCitizen
    Warriors, Citizens: TList;
    P: TKMPoint;
  