anchor.diff:  end;
   
   
  -function TKMArmyDefence.FindPlaceForGroup(aGroup: TKMUnitGroup; aTakeClosest: Boolean): Boolean;
  -var
  -  I, Matched: Integer;
  -  Distance, Best: Single;
  +function TKMArmyDefence.FindPlaceForGroup(aGroup: TKMUnitGroup): Boolean;
  +var
  +  I, BestIdx: Integer;
  +  BestWeight: Word;
   begin
     Result := False;
  -  Matched := -1;
  -  Best := MaxSingle;
   
     //Try to link to existing group
  +  BestWeight := 0;
  +  BestIdx := -1;
     for I := 0 to Count - 1 do
  -  if Positions[I].CanAccept(aGroup, TroopFormations[aGroup.GroupType].NumUnits) then
  -  begin
  -    //Take closest position that is empty or requries restocking
  -    Distance := KMLengthSqr(aGroup.Position, Positions[I].Position.Loc);
  -    if (Distance < Best) then
  -    begin
  -      Matched := I;
  -      Best := Distance;
  -      if not aTakeClosest then
  -        Break;
  -    end;
  -  end;
  -
  -  if (Matched <> -1) then
  +    if Positions[I].CanAccept(aGroup, TroopFormations[aGroup.GroupType].NumUnits)
  +      AND (BestWeight < Positions[I].Weight) then
  +    begin
  +      BestIdx := I;
  +      BestWeight := Positions[I].Weight;
  +    end;
  +
  +  if (BestIdx <> -1) then
     begin
       Result := True;
  -    if (Positions[Matched].CurrentGroup = nil) then
  +    if (Positions[BestIdx].Group = nil) then
       begin
         //New position
  -      Positions[Matched].CurrentGroup := aGroup;
  +      Positions[BestIdx].Group := aGroup;
         if (aGroup.UnitsPerRow < TroopFormations[aGroup.GroupType].UnitsPerRow) then
           aGroup.UnitsPerRow := TroopFormations[aGroup.GroupType].UnitsPerRow;
  -      aGroup.OrderWalk(Positions[Matched].Position.Loc, True);
  +      aGroup.OrderWalk( Positions[BestIdx].Position.Loc, True );
       end
       else
         //Append to existing position
  -      RestockPositionWith(Positions[Matched].CurrentGroup, aGroup);
  +      RestockPositionWith( Positions[BestIdx].Group, aGroup );
     end;
   end;
   
  
positive.code:     fOwner: TKMHandIndex;
      fFirstLineCnt: Word;
      fPositions: TKMList;
  
      function GetCount: Integer; inline;
      function GetPosition(aIndex: Integer): TKMDefencePosition; inline;
      procedure RestockPositionWith(aDefenceGroup, aGroup: TKMUnitGroup);
    public
      TroopFormations: array [TGroupType] of TKMFormation; //Defines how defending troops will be formatted. 0 means leave unchanged.
  
      constructor Create(aPlayer: TKMHandIndex);
      destructor Destroy; override;
      procedure Save(SaveStream: TKMemoryStream);
      procedure Load(LoadStream: TKMemoryStream);
  
negative.code:     property Positions[aIndex: Integer]: TKMDefencePosition read GetPosition; default;
  
      procedure OwnerUpdate(aPlayer: TKMHandIndex);
      function DefenceStatus(): TKMDefenceStatus;
      function FindPositionOf(aGroup: TKMUnitGroup): TKMDefencePosition;
      function FindPlaceForGroup(aGroup: TKMUnitGroup): Boolean;
      procedure ReleaseGroup(aGroup: TKMUnitGroup); overload;
      procedure ReleaseGroup(aDefPosIdx: Integer); overload;
      procedure FindEnemyInDefLine(aEnemyGroups: TKMUnitGroupArray);
  
      procedure UpdateState(aTick: Cardinal);
      procedure Paint();
  