mormonman
2010-12-16 15:28:28
They didn't find the object if it was on your tile, plus I like the idea of using a circle for MaxDist.
{*******************************************************************************
function SortedCircleTPA(Center: TPoint; Radius: Integer): TPointArray;
By: mormonman
Description: Returns an array of all TPoints inside the circle. Sorted from
closest to the Center.
*******************************************************************************}
function SortedCircleTPA(Center: TPoint; Radius: Integer): TPointArray;
var
X, Y: Integer;
begin
for X := Center.x - Radius to Center.x + Radius do
for Y := Center.y - Radius to Center.y + Radius do
if Distance(X, Y, Center.x, Center.y) < Radius then
Result := CombineTPA(Result, [Point(X, Y)]);
SortTPAFrom(Result, Center);
end;
{*******************************************************************************
function GetObjectsByIDEx(IDs: TIntegerArray; objType, MaxDist: Integer): TRSObjectArray;
By: mormonman
Description: Returns an array of all objects within the MaxDist with the IDs.
Sorted from closest to the your player.
MaxDist: This is the maximum distance from your player you wish to
search. The smaller the number, the faster the search is
completed. The maximum is 52.
*******************************************************************************}
function GetObjectsByIDEx(IDs: TIntegerArray; objType, MaxDist: Integer): TRSObjectArray;
var
f, resCount: Integer;
TP: TTile;
TileObject: TRSObject;
TPA: TPointArray;
begin
SetLength(Result, 104*104);
resCount := 0;
TP := GetMyPos;
TPA := SortedCircleTPA(TileToPoint(TP), MaxDist);
if length(TPA) < 1 then Exit;
for f := 0 to High(TPA) do
begin
TileObject := GetObjectAt(Tile(TPA[f].x, TPA[f].y), objType);
if (not (TileObject = NULL_RSOBJECT) and InIntArray(IDs, TileObject.ID)) then
begin
Result[resCount] := TileObject;
Inc(resCount);
end;
end;
SetLength(Result, resCount);
end;
{*******************************************************************************
function GetObjectsByID(IDs: TIntegerArray; objType, MaxDist: Integer): TRSObjectArray;
By: mormonman
Description: Returns an array of all objects within the MaxDist with the ID.
Sorted from closest to the your player.
MaxDist: This is the maximum distance from your player you wish to
search. The smaller the number, the faster the search is
completed. The maximum is 52.
*******************************************************************************}
function GetObjectsByID(ID: Integer; objType, MaxDist: Integer): TRSObjectArray;
begin
Result := GetObjectsByIDEx([ID], objType, MaxDist);
end;
{*******************************************************************************
function GetObjectByIDEx(IDs: TIntegerArray; objType, MaxDist: Integer): TRSObject;
By: mormonman
Description: Returns the closest object within the MaxDist with the IDs.
MaxDist: This is the maximum distance from your player you wish to
search. The smaller the number, the faster the search is
completed. The maximum is 52.
*******************************************************************************}
function GetObjectByIDEx(IDs: TIntegerArray; objType, MaxDist: Integer): TRSObject;
var
f: Integer;
TP: TTile;
TileObject: TRSObject;
TPA: TPointArray;
begin
TP := GetMyPos;
TPA := SortedCircleTPA(TileToPoint(TP), MaxDist);
if length(TPA) < 1 then Exit;
for f := 0 to High(TPA) do
begin
TileObject := GetObjectAt(Tile(TPA[f].x, TPA[f].y), objType);
if (not (TileObject = NULL_RSOBJECT) and InIntArray(IDs, TileObject.ID)) then
begin
Result := TileObject;
Exit;
end;
end;
end;
{*******************************************************************************
function GetObjectByID(IDs: TIntegerArray; objType, MaxDist: Integer): TRSObject;
By: mormonman
Description: Returns the closest object within the MaxDist with the ID.
MaxDist: This is the maximum distance from your player you wish to
search. The smaller the number, the faster the search is
completed. The maximum is 52.
*******************************************************************************}
function GetObjectByID(ID, objType, MaxDist: Integer): TRSObject;
begin
Result := GetObjectByIDEx([ID], objType, MaxDist);
end;