SRL Archive - OSR Advanced Scripting Tutorials

Using Almighty TPA's for object finding


Timothegreat

2008-04-04 12:41:38

I know all of you have heard of the ever powerful TPA (TPointArray) and T2DPointArray. Many of you who have heard of it think it is hard/confusing. Well i am here to iron out those confusing spots. I will help you better understand TPA's and T2DPointArrays, and be able to use them in object finding.

In reading this tutorial i assume you have already learned some of the basics of scar and srl.

TPoint

var
Blah: TPoint;

A point on a screen stored into a variable. Like a tpoint's value could have an
x value of 100 and a y value of 105. That would be a point on your screen.

You can also store coords to a TPoint.

TPoint:= inttopoint(100,105);

TPA (TPointArray)

var
Blah: TPointArray;

- Short for TPointArray.
- In essence it is an array of TPoints. (see above)
- used in many ways including object finding.

T2DPointArray

var
Blah: T2DPointArray;

- used to be called Array of TPointArray but thats changed.
- In essence it is an array of TPointArrays.
- used in certain functions to make life easier.

Functions for using TPA's

function FindColorsTolerance(var Points: TPointArray; Color, xs, ys, xe, ye, Tolerance: integer):boolean;

The function above ^^ is used in storing TPoints of a color to a TPointArray. This will aid you in object finding.

function MiddleTPA(tpa:TPointArray):TPoint

The function above ^^ takes the middle of a TPA and stores it to a TPoint. This helps when getting the middle of an object.

Now that you have learned the above functions, we will get to using TPA's in object finding.

Using TPA's for object finding

function FindRange:boolean;
var
i: integer;
Range: TPointArray;
R: TPoint;
begin
if not LoggedIn then exit;
FindColorsTolerance(Range,1319583,MSX1,MSY1,MSX2,MSY2,25);
if length(Range) = 0 then
begin
FindColorsTolerance(Range,593993,10,10,508,325,5);
end;
for i:= 0 to High(Range)do
begin
if FindNormalRandoms then
begin
ClickFood;
FindRange;
end;
if CookScreen then break;
R:= Range[i];
if FindColorTolerance(x,y,8160902,R.X-20,R.Y-20,R.X+20,R.Y+20,30)then
begin
MMouse(R.X,R.Y,4,4);
wait(75+random(25));
if (Pos('ange',rs_GetUpText) <> 0) then
begin
GetMousePos(x,y);
result:= True;
Mouse(x,y,0,0,true);
FFlag(0);
if FindNormalRandoms then
begin
ClickFood;
FindRange;
end;
wait(750+random(500));
Break;
exit;
end;
end;
end;
if not result then
begin
if ModFindObjCustom(x,y,['ange'],[1319583,8094852],15)then
begin
if (Pos('ange',rs_GetUpText) <> 0) then
begin
GetMousePos(x,y);
result:= True;
Mouse(x,y,0,0,true);
FFlag(0);
if FindNormalRandoms then
begin
ClickFood;
FindRange;
end;
wait(750+random(500));
exit;
end;
end;
end;
end;

I took this function out of a cooker i am making. The above function won't compile because you dont have my other functions needed for it to compile.

When you use FindColorsTolerance it stores all the points on the screen that have the color to a TPointArray, in this case being a TPA named Range.

Then you could use that TPA to get a single TPoint from it. Even though i don't use MiddleTPA in the function above, you could use it to get a TPoint for moving your mouse to. I chose to just make the mouse go to each point in the array - another approach.

Functions for advance object finding

function SplitTPA(arr:TPointArray; Dist:LongInt):T2DPointArray

The function above will split up a TPointArray into a T2DPointArray (an array of TPA's) and ea TPointArray inside will contain groups of points within a certain distance.

function TPAtoATPA(TPA:TPointArray; Dist:LongInt):T2DPointArray

note:

SplitTPA splits points into groups with a max distance between all the points in the array.
TPAtoATPA splits points into groups with a max distance based on the first point.

Like this:


function TPAtoATPAEx(TPA:TPointArray; w:LongInt; h:LongInt):T2DPointArray

The function above splits TPA's into a T2DPointArray with arrays of points groups of certain height and width inside it. So inside the T2DPointArray there will be groups of points with same height and width. Just like TPAtoATPA but height and width not distance.

Using TPA's and T2DPointArrays for object finding

function FindOres:boolean;
var
tx,ty,i: integer;
CoalPoints,MithPoints: TPointArray;
Mith2D,Coal2D: T2DPointArray;
MithrilPoint,CoalPoint: TPoint;
begin
if not LoggedIn then exit;
x:= MSCX;
y:= MSCY;
if (Players[CurrentPlayer].Strings[1] = 'both') then
begin
FindColorsSpiralTolerance(x,y,MithPoints,8871004,MSX1,MSY1,MSX2,MSY2,10);
Mith2D:= TPAtoATPAEx(MithPoints,30,30); // splits tpa into points with groups of certain height and width
for i:= 0 to length(Mith2D)-1 do
begin
if not LoggedIn then
begin
break;
end;
MithrilPoint:= MiddleTPA(Mith2D[i]); // takes the middle point of TPA
tx:= MithrilPoint.X;
ty:= MithrilPoint.Y;
MMouse(tx,ty,2,2);
wait(90+random(50));
if isuptext('ine')then
begin
GetMousePos(x,y);
OreColor:= GetColor(tx,ty);
MineMith:= true;
result:= true;
break;
exit;
end;
end;
end;
if Result then exit;
if (Players[CurrentPlayer].Strings[1] = 'coal')or(Players[CurrentPlayer].Strings[1] = 'both') then
begin
x:= MSCX;
y:= MSCY;
FindColorsSpiralTolerance(x,y,CoalPoints,2966082,MSX1,MSY1,MSX2,MSY2,10);
Coal2D:= TPAtoATPAEx(CoalPoints,30,30);
for i:= 0 to length(Coal2D)-1 do
begin
if not LoggedIn then
begin
break;
end;
CoalPoint:= MiddleTPA(Coal2D[i]); // takes the middle point of TPA
tx:= CoalPoint.X;
ty:= CoalPoint.Y;
MMouse(tx,ty,2,2);
wait(90+random(50));
if isuptext('ine')then
begin
GetMousePos(x,y);
OreColor:= GetColor(tx,ty);
MineCoal:= true;
result:= true;
break;
exit;
end;
end;
end;
end;

I took this function from my guild miner. It uses advanced techniques with TPA's to find rocks in the guild. This function works flawlessly. You can use all the functions in this tutorial to create a flawless object finder like this. Turning on code hints when using TPA's will help you better understand them too.

Conclusion

Thanks for reading my tutorial about TPA's and object finding. You are welcome to use my functions as long as you properly credit.

If i have forgotten anything please tell me.

I hope you have enjoyed my kinda lengthy tutorial. :)

the scar noob

2008-04-04 14:59:06

Nice tutorial!:) Though, I prefer to explain it a bit more, like that a T2DPointArray is 2-deminsional and so that there also excists T3DPointArray, have a look at SRL's Playerform made by Sumilion, I think it's being used over there;)

I'm only suggesting;)

Oh, maybe you could expand your tutorial by explaining other functions from inside the WizzyPlugin aswell:)

-Tsn.

Sir R. M8gic1an

2008-04-04 17:35:29

*****5 stars :) cleared a lot of things, especially the SplitTPA/TPAtoATPA image

~RM

EvilChicken!

2008-04-04 18:25:24

Tell more about theese functions:

{*******************************************************************************
procedure RAaSTPAEx(var a: TPointArray; const w, h: Integer);
Description: Leaves one point per box with side lengths W and H to the TPA.
*******************************************************************************}

{*******************************************************************************
procedure RAaSTPA(var a: TPointArray; const Dist: Integer);
Description: Leaves one point per box with the side length Dist.
*******************************************************************************}

{*******************************************************************************
procedure SortTPAFrom(var a: TPointArray; const From: TPoint);
Description: Sorts the TPointArray a from the point From.
Closest one to the point is [0], second closest is [1] etc.
*******************************************************************************}

{*******************************************************************************
procedure SortATPAFrom(var a: T2DPointArray; const From: TPoint);
Description: Sorts the T2DPointArray a from the point From.
*******************************************************************************}


{*******************************************************************************
procedure SortATPAFromFirstPoint(var a: T2DPointArray; const From: TPoint);
Description: Sorts the T2DPointArray a from the point From.
*******************************************************************************}


{*******************************************************************************
procedure InvertTPA(var a: TPointArray);
Description: Inverts / Reverts the TPointArray a.
*******************************************************************************}
{*******************************************************************************
procedure InvertATPA(var a: T2DPointArray);
Description: Inverts / Reverts the T2DPointArray a.
*******************************************************************************}
{*******************************************************************************
procedure SortATPASize(var a: T2DPointArray; const BigFirst: Boolean);
Description: Sorts the T2DPointArray a from either largest or smallest, by the
amount of points in the TPAs.
*******************************************************************************}


{*******************************************************************************
function CombineTPA(Ar1, Ar2: TPointArray): TPointArray;
Description: Combines the TPointArrays Ar1 and Ar2, and results the combination.
*******************************************************************************}
{*******************************************************************************
function SplitTPAEx(arr: TPointArray; w, h: Integer): T2DPointArray;
Description: Splits the points with max X and Y distances W and H to their
own TPointArrays.
*******************************************************************************}
{*******************************************************************************
procedure FilterPointsPie(var Points: TPointArray; const SD, ED, MinR, MaxR: Extended; Mx, My: Integer);
Description: Removes the points in the TPointArray Points that are not within
the degrees SD (StartDegree) and ED (EndDegree) and the distances
MinR (MinRadius) and MaxR (MaxRadius) from the origin (Mx, My).
*******************************************************************************}


{*******************************************************************************
function RemoveDistTPointArray(x, y, dist: Integer; ThePoints: TPointArray; RemoveHigher: Boolean): TPointArray;
Description: Removes the points that are inside or outside the distance Dist
from the point (x, y) from the TPointArray ThePoints.
*******************************************************************************}


{*******************************************************************************
function GetTPABounds(TPA: TPointArray): TBox;
Description: Returns the boundaries of the TPA as a TBox.
*******************************************************************************}


{*******************************************************************************
function FindTPAinTPA(SearchTPA, TotalTPA: TPointArray; var Matches: TPointArray): Boolean;
Description: Looks for the TPA SearchTPA in the TPA TotalTPA and returns
the matched points to the TPA Matches. Returns true if there were atleast one
match(es).
*******************************************************************************}


{*******************************************************************************
function FindTextTPAinTPA(Height: Integer; SearchTPA, TotalTPA: TPointArray; var Matches: TPointArray): Boolean;
Description: Read the description of FindTPAinTPA. Additional Height parameter.
*******************************************************************************}


{*******************************************************************************
function FindGapsTPA(TPA: TPointArray; MinPixels: Integer): T2DPointArray;
Description: Finds the possible gaps in the TPointArray TPA and results the
gaps as a T2DPointArray. Considers as a gap if the gap length is >= MinPixels.
Only horizontal, sorry folks.
*******************************************************************************}
{*******************************************************************************
Function MergeATPA(ATPA: T2DPointArray): TPointArray;
Description: Merges the TPointArrays of the T2DPointArray ATPA in to one TPA.
*******************************************************************************}

Note: Underline the differences between the normal and the "Ex" procedures.. (TPAToATPA vs. TPAToATPAEx)

Pleeeease?

the scar noob

2008-04-04 20:12:09

From: EvilChicken!

Note: Underline the differences between the normal and the "Ex" procedures.. (TPAToATPA vs. TPAToATPAEx)

Pleeeease?

Meh! It's 02:06 AM atm over here, I'll only tell you about TPAToATPA and TPAToATPAEx;)

function TPAtoATPA(TPA: TPointArray; Dist: Integer): T2DPointArray;
Input: TPointArray and distance.
What does it do? It devides the TPointArray you've inputted into seperated groups. It does this by the "distance".
It makes groups that are close to eachother, by a given max distance.

function TPAtoATPAEx(TPA: TPointArray; w, h: Integer): T2DPointArray;

Input: TPointArray and a "widh" and "heigth".
What does it do? It devides the TPointArray in sperated groups, just as TPAToATPA does, but! instead of checking the distance, it checks the widh and height, like the widh and height of an object, not the distance.

They both output a 2-dimensional array, but makesthe groups in a different way:)

I hope that cleared it out better..

-Tsn.

HellBoyz

2008-04-04 20:43:26

Nice tut mate bookmark lol :p

Timothegreat

2008-04-05 17:55:58

im glad you like it guys :)

skilld u

2008-04-07 13:25:12

very nice! I never understood TPA's very well. Added rep. :D

Timothegreat

2008-05-04 11:40:30

no problem… il probably make a form tutorial soon also :)

this tutorial needs to be seen more ;)

tetazoo

2008-05-04 12:44:35

From: Timothegreat

…
SplitTPA splits points into groups with a max distance between all the points in the array.
TPAtoATPA splits points into groups with a max distance based on the first point.
…
The function above splits TPA's into a T2DPointArray with arrays of points groups of certain height and width inside it. So inside the T2DPointArray there will be groups of points with same height and width. Just like TPAtoATPA but height and width not distance.
…


Regarding TPAtoATPAEx, how can points have a height and width? They are just points. This part confuses me. Do you mean within a certain box around a point? For example, if we start with the TPoint 10,10, do width 3 and height 4 mean all the points within +/- 3 pixels in the x-direction and 4 pixels in the y-direction (i.e. anything within the box 7,6 to 13,14)?

Assuming that's the case, then I have a follow-up question about how it groups the TPoint arrays (applies to function that does this by distance as well). Does it do it somehat holistically by looking at all of the points at once or does it just start with the first point in the input array and add in every other point within a certain distance/box to that array to form the first group and then takes the next unused point in the array as the basis for the next group of TPoints, finding all of the remaining unused points within a certain distance/box, and so on?

n3ss3s

2008-05-04 12:56:37

Nice tutorial, but the cold truth is that if doing object finding, there is no need for a huge chunk of code since FindObjTPA does it all…

Camaro'

2008-05-04 14:07:02

wow, gr8 tut man.. Rep +

and btw n3ss3s, the big chunk of code is needed because when using findobjtpa it moves the mouse around the screen if it doesnt find it. If u use the "big chunk of code" then you can save it as a boolean and be
repeat
if not AttackMonster then wait(1000+Random(1000));
until(imadmin)or (immod) or (banned)// lol

Timothegreat

2008-05-17 21:35:03

From: tetazoo
Regarding TPAtoATPAEx, how can points have a height and width? They are just points. This part confuses me. Do you mean within a certain box around a point?


the array of the points has a certain width and height not the points themselves.

Dracody

2008-06-01 14:40:15

Thanks Timo!! this helped me a lot, those little pics you put are really helpfull :D I like em, I would like the other TPA guides to add pics also , would be really really helpfull ::p

Timothegreat

2008-06-04 16:36:57

np glad i can be of help to you. ;)

and yes… pics do help.

13om13e12

2008-06-06 11:32:17

How is the Distance Argument Measured in TPA's? Is it pixels? and why is it declared as a longint?

Timothegreat

2008-06-06 15:45:33

distance is measured in pixels… and idk im sure it can be a longint or an integer

Macho Man67

2008-06-07 13:20:18

Thank you! Great tutorial, helped me a lot. The pics were nice too. Rep +

Will definitely be using this in my upcoming script!

Timothegreat

2008-06-07 19:57:22

From: Macho Man67
Thank you! Great tutorial, helped me a lot. The pics were nice too. Rep +

Will definitely be using this in my upcoming script!


np dude, thanks :)

Claymore

2008-07-09 11:11:46

I still dont get how to do TPA, i read yours and Naurman's Guide… :confused:
I know the functions, i just dont know how to put them all together to make it work.


program UsingTPALast; {TimoTheGreat, thanks for the guide!}
{.include SRL/SRL.scar}

function FindWillows: Boolean;
var
WX, WY, x, y, i, W, WillowColor : Integer;
WillowPoints : TPointArray;
Willow2D : T2DPointArray;
WillowPoint : TPoint;

begin
x := MSCX
y := MSCY
W := High(Willow2D);
FindColorsSpiralTolerance(x, y, WillowPoints, 2119757, MSX1, MSY1, MSX2, MSY2, 10);
Willow2D := TPAtoATPAEx(WillowPoints, 3, 3);
for I := 0 to W do
begin
WillowPoint := MiddleTPA(Willow2D[i]);
WX := WillowPoint.X;
WY := WillowPoint.Y;
MMouse(WX, WY, 1, 1);
Wait(530 + Random(890));
if IsUpText('hop') then
begin
GetMousePos(x, y)
WillowColor := GetColor(WX, WY);
Mouse(x, y, 0, 0, true);
Writeln('True');
Result := True;
Break;
Exit;
end else
Writeln('False');
Result := False;
Exit;
end;
end;

procedure PleaseWork;
begin
if FindWillows then
Writeln('');
end;

begin
ActivateClient;
Wait(2000);
PleaseWork;
end.

Also what does High({Insert T2DPointArray}) do?

What could be wrong with this

Dervish

2008-11-04 12:44:46

Rep++, 5 star rate.

Thanks alot, i've read many TPA tuts but this one is the best (its the one which made me understand TPA's).

Ty ty ty ! :D

Laimonas171

2008-11-04 13:02:48

great!