SRL Archive - FAQ

SortATPAFrom(SomeATPA, Point(MSCX, MSCY))


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.

//============================================================================//
// 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;

riwu

2013-01-29 07:20:56

SortATPAFrom() only sorts the TPoints in the TPAs rather than the ATPA.

Press Play

2013-01-29 07:30:46

From: riwu
SortATPAFrom() only sorts the TPoints in the TPAs rather than the ATPA.


So all these years… Either I misinterpreted the tutorials, or they're wrong! Thanks so much! :)

I guess this could get moved to SRL Snippets or something then.

Sjoe

2013-01-29 08:16:41

So is it better to use SortATPAFromMidPoint?

riwu

2013-01-29 09:11:18

From: Sjoekeloe
So is it better to use SortATPAFromMidPoint?

They are for different purposes. In most cases for RS scripting SortATPAFromMidPoint() will be what you are looking for.

Sjoe

2013-01-29 09:23:18

From: riwu
They are for different purposes. In most cases for RS scripting SortATPAFromMidPoint() will be what you are looking for.


But that will also sort the TPAs rather than ATPA's?

Press Play

2013-01-29 15:06:00

Yes, that's right - I just tested it :) So basically I made a function that already exists ahaha

riwu

2013-01-29 20:28:00

From: Sjoekeloe
But that will also sort the TPAs rather than ATPA's?

Like for an ATPA: [TPA1, TPA2, TPA3], SortATPAFromMidPoint() will sort the positions of the TPA1, TPA2, and TPA3, but SortATPAFrom() only sorts the Tpoints of the TPA1, TPA2, TPA3. Position of the TPAs within the ATPA remains unaltered (or is altered in an arbitrary manner?)

mormonman

2013-01-29 21:18:53

Hey I made that :)