anchor.diff:    GTS: TKMGroupTypeSet;
     G: TKMUnitGroup;
   begin
  -  try
  -    Result := -1;
  -    if InRange(aPlayer, 0, gHands.Count - 1) and (gHands[aPlayer].Enabled)
  -      and gTerrain.TileInMapCoords(X, Y)
  -      and ((aGroupType = gtAny) or (aGroupType in GROUP_TYPES_VALID)) then
  -    begin
  -      if aGroupType = gtAny then
  -        GTS := GROUP_TYPES_VALID
  -      else
  -        GTS := [aGroupType];
  -
  -      G := gHands[aPlayer].UnitGroups.GetClosestGroup(KMPoint(X,Y), GTS);
  -      if (G <> nil) and not G.IsDead then
  -      begin
  -        Result := G.UID;
  -        fIDCache.CacheGroup(G, G.UID);
  -      end;
  -    end
  -    else
  -      LogIntParamWarn(aMethodName, [aPlayer, X, Y, Ord(aGroupType)]);
  -  except
  -    gScriptEvents.ExceptionOutsideScript := True; //Don't blame script for this exception
  -    raise;
  -  end;
  +  Result := -1;
  +  if InRange(aPlayer, 0, gHands.Count - 1) and (gHands[aPlayer].Enabled)
  +    and gTerrain.TileInMapCoords(X, Y)
  +    and ((aGroupType = gtAny) or (aGroupType in GROUP_TYPES_VALID)) then
  +  begin
  +    if aGroupType = gtAny then
  +      GTS := GROUP_TYPES_VALID
  +    else
  +      GTS := [aGroupType];
  +
  +    G := gHands[aPlayer].UnitGroups.GetClosestGroup(KMPoint(X,Y), GTS);
  +    if (G <> nil) and not G.IsDead then
  +    begin
  +      Result := G.UID;
  +      fIDCache.CacheGroup(G, G.UID);
  +    end;
  +  end
  +  else
  +    LogIntParamWarn(aMethodName, [aPlayer, X, Y, Ord(aGroupType)]);
   end;
   
   
  
positive.code: end;
  
  
  procedure TKMScriptStates._AIGroupsFormationGet(aPlayer: Integer; aGroupType: TKMGroupType; out aCount, aColumns: Integer; aMethodName: string);
  begin
    try
      if InRange(aPlayer, 0, gHands.Count - 1) and (gHands[aPlayer].Enabled)
        and (aGroupType in GROUP_TYPES_VALID) then
      begin
        if gHands[aPlayer].AI.Setup.NewAI then
        begin
          aCount := gHands[aPlayer].AI.ArmyManagement.Defence.TroopFormations[aGroupType].NumUnits;
          aColumns := gHands[aPlayer].AI.ArmyManagement.Defence.TroopFormations[aGroupType].UnitsPerRow;
        end
        else
        begin
          aCount := gHands[aPlayer].AI.General.DefencePositions.TroopFormations[aGroupType].NumUnits;
          aColumns := gHands[aPlayer].AI.General.DefencePositions.TroopFormations[aGroupType].UnitsPerRow;
        end
      end
      else
        LogIntParamWarn(aMethodName, [aPlayer, Ord(aGroupType)]);
    except
      gScriptEvents.ExceptionOutsideScript := True; //Don't blame script for this exception
      raise;
    end;
  end;
  
  
negative.code: //* Gets the formation the AI uses for defence positions for specified player and group type
  //* group count and columns are returned in aCount and aColumns variables
  procedure TKMScriptStates.AIGroupsFormationGetEx(aPlayer: Integer; aGroupType: TKMGroupType; out aCount, aColumns: Integer);
  var
    succeed: Boolean;
  begin
    try
      _AIGroupsFormationGet(aPlayer, aGroupType, aCount, aColumns, succeed);
      if not succeed then
        LogParamWarn('States.AIGroupsFormationGetEx', [aPlayer, GetEnumName(TypeInfo(TKMGroupType), Integer(aGroupType))]);
    except
      gScriptEvents.ExceptionOutsideScript := True; //Don't blame script for this exception
      raise;
    end;
  end;
  
  
  