blackfire553
2011-07-01 17:58:46
I needed a function to sort TTileArray from the point i was standing at for my PickUpGroundItems function
So here it is:
Attached is the script i used to test it
So here it is:
procedure SortTTAFrom(var a : TTileArray;const From : TTile);
var
i, l : Integer;
DistArr : TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(DistArr, l + 1);
for i := 0 to l do
DistArr[i] := Round(Sqr(From.x - a[i].x) + Sqr(From.y - a[i].y));
QuickTTASort(DistArr, a, 0, l, True);
end;procedure QuickTTASort(var A: TIntegerArray; var B: TTileArray; iLo, iHi: Integer; SortUp: Boolean);
var
Lo, Hi, Mid, T: Integer;
TT: TTile;
begin
if (Length(A) <> Length(B)) then Exit;
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) shr 1];
repeat
if SortUp then
begin
while (A[Lo] < Mid) do Inc(Lo);
while (A[Hi] > Mid) do Dec(Hi);
end else
begin
while (A[Lo] > Mid) do Inc(Lo);
while (A[Hi] < Mid) do Dec(Hi);
end;
if (Lo <= Hi) then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
TT := B[Lo];
B[Lo] := B[Hi];
B[Hi] := TT;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if (Hi > iLo) then QuickTTASort(A, B, iLo, Hi, SortUp);
if (Lo < iHi) then QuickTTASort(A, B, Lo, iHi, SortUp);
end; To convert TGroundItemArray to TTileArray:function TGIAToTTA(TGIA : TGroundItemArray) : TTileArray;
var
i: Integer;
TTA: TTileArray;
begin
SetArrayLength(TTA,GetArrayLength(TGIA));
for i := 0 to high(TGIA) do
TTA[i] := GroundItemToTile(TGIA[i]);
Result := TTA;
end;function GroundItemToTile(T : TGroundItem) : TTile;
begin
Result := Tile(T.Tile.x,T.Tile.y);
end; Attached is the script i used to test it