<commit_before>unit KM_TerrainFinder;
{$I KaM_Remake.inc}
interface
uses
  Math, SysUtils,
  KM_Defaults, KM_CommonClasses, KM_Points;

type
  //General implementation of algorithm that finds something on terrain without building a route to it
  TKMTerrainFinderCommon = class
  private
    fMapX: Word;
    fMapY: Word;
    fStart: TKMPointArray;
    fRadius: Byte;
    fMaxCount: Word;
    BestDist: Byte;
    BestLoc: TKMPoint;
    fVisited: array of array of Byte;

    //Temp for list that we need to fill
    fLocs: TKMPointTagList;

    procedure InitVisited;
    procedure SaveTile(const X,Y: Word; aWalkDistance: Byte);
    procedure UseFinder;
  protected
    fPassability: TPassabilitySet;
    function CanWalkHere(const X,Y: Word): Boolean; virtual; abstract;
    function CanUse(const X,Y: Word): Boolean; virtual; abstract;
  public
    constructor Create;
    destructor Destroy; override;
    function FindNearest(aStart: TKMPoint; aRadius: Byte; aPassability: TPassabilitySet; out aEnd: TKMPoint): Boolean; overload;
    procedure FindNearest(aStart: TKMPointArray; aRadius: Byte; aPassability: TPassabilitySet; aMaxCount: Word; aLocs: TKMPointTagList); overload;
    procedure Save(SaveStream: TKMemoryStream); virtual;
    procedure Load(LoadStream: TKMemoryStream); virtual;
  end;


  //Simple algorithm implementation required to override abstract methods
  TKMTerrainFinder = class(TKMTerrainFinderCommon)
  protected
    function CanWalkHere(const X,Y: Word): Boolean; override;
    function CanUse(const X,Y: Word): Boolean; override;
  public
    procedure GetTilesWithinDistance(aStart: TKMPoint; aRadius: Byte; aPass: TPassability; aList: TKMPointList);
  end;


implementation
uses
  KM_ResMapElements, KM_Terrain;


{ TKMTerrainFinder }
constructor TKMTerrainFinderCommon.Create;
begin
  inherited;

  fMapX := gTerrain.MapX;
  fMapY := gTerrain.MapY;
  SetLength(fVisited, fMapY+1, fMapX+1);
end;


destructor TKMTerrainFinderCommon.Destroy;
begin

  inherited;
end;


function TKMTerrainFinderCommon.FindNearest(aStart: TKMPoint; aRadius: Byte; aPassability: TPassabilitySet; out aEnd: TKMPoint): Boolean;
begin
  SetLength(fStart, 1);
  fStart[0] := aStart;
  fRadius := aRadius;
  fPassability := aPassability;
  fMaxCount := 1;

  UseFinder;

  aEnd := BestLoc;<commit_msg>Cleanup<commit_after>unit KM_TerrainFinder;
{$I KaM_Remake.inc}
interface
uses
  Math, SysUtils,
  KM_Defaults, KM_CommonClasses, KM_Points;

type
  //General implementation of algorithm that finds something on terrain without building a route to it
  TKMTerrainFinderCommon = class
  private
    fMapX: Word;
    fMapY: Word;
    fStart: TKMPointArray;
    fRadius: Byte;
    fMaxCount: Word;
    BestDist: Byte;
    BestLoc: TKMPoint;
    fVisited: array of array of Byte;

    //Temp for list that we need to fill
    fLocs: TKMPointTagList;

    procedure InitVisited;
    procedure SaveTile(const X,Y: Word; aWalkDistance: Byte);
    procedure UseFinder;
  protected
    fPassability: TPassabilitySet;
    function CanWalkHere(const X,Y: Word): Boolean; virtual; abstract;
    function CanUse(const X,Y: Word): Boolean; virtual; abstract;
  public
    constructor Create;
    function FindNearest(aStart: TKMPoint; aRadius: Byte; aPassability: TPassabilitySet; out aEnd: TKMPoint): Boolean; overload;
    procedure FindNearest(aStart: TKMPointArray; aRadius: Byte; aPassability: TPassabilitySet; aMaxCount: Word; aLocs: TKMPointTagList); overload;
    procedure Save(SaveStream: TKMemoryStream); virtual;
    procedure Load(LoadStream: TKMemoryStream); virtual;
  end;


  //Simple algorithm implementation required to override abstract methods
  TKMTerrainFinder = class(TKMTerrainFinderCommon)
  protected
    function CanWalkHere(const X,Y: Word): Boolean; override;
    function CanUse(const X,Y: Word): Boolean; override;
  public
    procedure GetTilesWithinDistance(aStart: TKMPoint; aRadius: Byte; aPass: TPassability; aList: TKMPointList);
  end;


implementation
uses
  KM_ResMapElements, KM_Terrain;


{ TKMTerrainFinder }
constructor TKMTerrainFinderCommon.Create;
begin
  inherited;

  fMapX := gTerrain.MapX;
  fMapY := gTerrain.MapY;
  SetLength(fVisited, fMapY+1, fMapX+1);
end;


function TKMTerrainFinderCommon.FindNearest(aStart: TKMPoint; aRadius: Byte; aPassability: TPassabilitySet; out aEnd: TKMPoint): Boolean;
begin
  SetLength(fStart, 1);
  fStart[0] := aStart;
  fRadius := aRadius;
  fPassability := aPassability;
  fMaxCount := 1;

  UseFinder;

  aEnd := BestLoc;
{'fraction_non_alphanumeric': 0.08433200177541056, 'fraction_numerical': 0.0022192632046160675, 'mean_word_length': 4.785622593068036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5867198', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1461, 'n_words': 405}