PDA

View Full Version : TReflectionGroundItemArray.SortStacks() (+ compareTile / T2DReflectionGroundItemArra)



Floor66
02-16-2015, 08:01 PM
I had a need for this in one of my scripts. It will take an array of ground items (such as the one attained with TReflectGroundItemArray.GetAll()) and sort it in a 2D array, by tile.
So, the result of this function is an array with each item containing all ground items that share the same position in-game.


type
T2DReflectGroundItemArray = Array of TReflectGroundItemArray

function compareTile(t1, t2: TTile): Boolean;
begin
Result := (t1.X = t2.X) and (t1.Y = t2.Y);
end;

function TReflectGroundItemArray.SortStacks(): T2DReflectGroundItemArray;
var
tmp: T2DReflectGroundItemArray;
i, j, k, l: Integer;
inserted: Boolean;
begin
if Length(Self) = 0 then
Exit;

SetLength(tmp, 1);
SetLength(tmp[0], 1);
tmp[0][0] := Self[0];

if Length(Self) > 1 then
begin
j := High(Self);
for i := 1 to j do
begin
inserted := False;

l := High(tmp);
for k := 0 to l do
begin
if compareTile(Self[i].Tile, tmp[k][0].Tile) then
begin
SetLength(tmp[k], Length(tmp[k]) + 1);
tmp[k][High(tmp[k])] := Self[i];
inserted := True;
Break;
end;
end;

if not inserted then
begin
SetLength(tmp, Length(tmp) + 1);
SetLength(tmp[High(tmp)], 1);
tmp[High(tmp)][0] := Self[i];
end;
end;
end;

Result := tmp;
end;


Let me know if you see any pesky access violations/obvious mistakes/performance improvements here :D