Press Play
2013-01-29 06:56:15
So, I was writing a private script for myself (writing the custom object finder), and during debugging, I noticed that SortATPAFrom(SomeATPA, Point(MSCX, MSCY)) was kind of buggy. The first TPA in the array (SomeATPA[0]) is closest to Point(MSCX, MSCY), but when moving on to other elements (eg. SomeATPA[1]), it jumps to the TPA in the top left corner.
I wasn't sure what was happening, so I decided to write my own sorting function. It's not really that 'proper', but it fixes the problem. Then, I decided I should post this here to let someone know :)
EDIT: And feel free to use my temporary fix.
I wasn't sure what was happening, so I decided to write my own sorting function. It's not really that 'proper', but it fixes the problem. Then, I decided I should post this here to let someone know :)
EDIT: And feel free to use my temporary fix.
//============================================================================//
// My own fuction for sorting an ATPA from Runescape's centre of main screen
// -Uses bubble sort.
//============================================================================//
type
TDistanceIndex = record
TPA: TPointArray;
Point: TPoint;
Distance: LongInt;
end;
procedure SortATPAFromMSCentre(var ATPA: T2DPointArray);
var
Temp: Array of TDistanceIndex;
h, i: Integer;
f: Boolean;
begin
SetLength(Temp, Length(ATPA));
h := High(ATPA);
// Set array
for i := 0 to h do
begin
Temp[i].TPA := ATPA[i];
Temp[i].Point := ATPA[i][0];
Temp[i].Distance := Distance(Temp[i].Point.x, Temp[i].Point.y, MSCX, MSCY);
end;
// Arrange Array
repeat
f:= True;
for i := 1 to h-1 do
begin
if Temp[i-1].Distance > Temp[i].Distance then
begin
Swap(Temp[i-1], Temp[i]);
f:= False;
end;
end;
until(f)
// Reset array
for i := 0 to h do
ATPA[i] := Temp[i].TPA;
end;