SRL Archive - Reflection Development

SortTTAFrom


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:
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

nielsie95

2011-07-01 18:11:00

Looking good :)

Yago

2011-07-01 21:15:50

Nice stuff…

noidea

2011-07-01 21:18:23

hehehe dat quicksort. Nice man. Also its great to see you on the irc!

Nava2

2011-07-02 02:20:17

Plugin-ize it and I wish we had TTile mirrored with TPA so we could use them interchangably. I know TTile = TPoint, but still.. :(

Wizzyplugin would be awesome!

Coh3n

2011-07-02 02:54:23

From: Nava2
Plugin-ize it and I wish we had TTile mirrored with TPA so we could use them interchangably. I know TTile = TPoint, but still.. :(

Wizzyplugin would be awesome!
Or a TTile could just be declared as a TPoint. Problem solved. I know people will argue that TTile <> TPoint, but according to the syntax, they are the same. Having this is pointless in my opinion.

type
TTile = TPoint;

Goobs

2011-07-02 03:58:18

I completely agree with you Coh3n - it takes so many extra variables and such when dealing with tiles and conversions to MS and such.

Nice functions! I might just borrow one and credit you ;)

blackfire553

2011-07-02 14:24:03

You can use TTAToTPA to use the wizzy plugin sort
{*******************************************************************************
function TTAtoTPA(TTA: TTileArray): TPointArray;
By: Echo_
Description: Converts a TTileArray to a TPointArray.
*******************************************************************************}
function TTAtoTPA(TTA: TTileArray): TPointArray;
var
i: Integer;
TPA: TPointArray;
begin
SetArrayLength(TPA, GetArrayLength(TTA));
for i := 0 to High(TTA) do TPA[i] := TileToPoint(TTA[i]);
Result := TPA;
end;

I still like being able to pull the tiles out of a TGroundItemArray, now my PickUpGroundItems can sort the items by distance, then check to see if it is an item i want to pick up, much more efficient than when i was doing TileToMS on every point every time my chracter moved.

Wizzup?

2011-07-02 16:13:02

From: Coh3n
Or a TTile could just be declared as a TPoint. Problem solved. I know people will argue that TTile <> TPoint, but according to the syntax, they are the same. Having this is pointless in my opinion.

type
TTile = TPoint;


Yeah, this sounds like a neat solution. TBH the TTA code reminds me of wizzyplugin/simba a bit. (a big fat _reminds_ ;) )

Drakan

2011-07-04 13:34:44

Idk about ref1, but in reflection 2 it's been declared as a TPoint and TPointArray for quite some time. Just use the built in TPA functions to sort.