PDA

View Full Version : [TUT]Fun with TPoints, TBoxes, and More![/TUT]



Infantry001
03-25-2007, 09:16 PM
Fun with TPoints, TBoxes, and More!

So, im guessing you came here knowing nothing about tpoints? Well, today im here to teach you about TPoints, TPointArrays, TBoxes, and TBoxArrays. Make sure you understand the basics of SCAR before attempting this tutorial.

First off, open up scar.

Set these global variables:

var
i : integer;
MyTpoint : tpoint;
MyTpointArray : TPointArray;
MyTBox : tbox;
MyTBoxArray : TBoxArray;

And include SRL (you need this for TBox).

Alright, now let me describe each variable type.

TPoint - Used to store an x and y coordinate within one variable. Lets say you are using MyTpoint as your tpoint. The "x" coordinate would be MyTpoint.x, and the "y" coordinate would be MyTpoint.y.

TPointArray - Store multiple TPoints within an array. Using the first TPoint would be, say, MyTPointArray[0].x and MyTPointArray[0].y. Don't forget to set the array length!

TBox - Pretty much the same as TPoint, except uses x1,y1,x2, and y2 as arguments. x1,y1 would be the top left corner of the box made, and x2,y2 would be bottom right corner.

TBoxArray - Stores multiple TBoxes in an array.

To use these, lets start off with a midpoint function:


TPoint

//Fx,fy - first coords
//sx,sy - second coords
function Midpoint(fx,fy,sx,sy : integer) : tpoint;
begin
result.x := Round((fx + sx) / 2);
result.y := Round((fy + sy) / 2);
end;

Now, try this in your main loop:

begin
SetupSRL;
MyTPoint := MidPoint(0,0,500,500);
MMouse(MyTPoint.x,MyTPoint.y,0,0);
end.

As you can see, midpoint will result in a TPoint, and MyTPoint is set as the MidPoint of 0,0 and 500,500. Run the script and watch the mouse move to 250,250.

Next, lets look at a TPoint Array!


TPointArray

Now, you should know a bit about arrays, but if not, let me take a small detour...

Arrays are a way of making one variable have many different values. Each value of the array is shown by the
number. The format is this: Array[0]. Say you want array to hold 3 different colors. First, you would need to
set the array length (setarraylength(Array,3)). This would result in an array of 0-2, not 1-3. Then, do this:
Array[0] := 0;
Array[1] := 65336;
Array[2] := 1667438;
Then, you could find the color Array[0] by FindColor(x,y,Array[0],MSX1,MSY1,MSX2,MSY2). This has been a quick
detour on arrays.

Within the same script, add this procedure:

procedure SetMidpointArray;
var i : integer;
begin
setarraylength(MyTPointArray,4);
for i := 0 to 3 do
MyTPointArray := MidPoint(0,0,i * 100,i * 100);
end;

This will set each TPoint to a midpoint using a for..to..do statement. If you do not understand, check out some
other tutorials!

Now, add to your main loop:
SetMidpointArray;
for i := 0 to 3 do
MMouse(MyTPointArray[i].x,MyTPointArray[i].y,0,0);

This will set the midpoint array, and then move the mouse to each midpoint!


TBox

Time to move onto TBoxes!

So, TBoxes are pretty much a set of 2 TPoints, with the parameters being x1,y1,x2, and y2. They are useful for many things, but TBoxArrays are mor commonly used.

Lets say you want to look for a color around a certain point. First, you would use FindColor, right? From there, what do you do? That's right! Make a TBox.

Put this into scar:

function FindColorInBox(x,y,color : integer) : boolean;
begin
MyTBox.x1 := x - 30;
MyTBox.y1 := y - 30;
MyTBox.x2 := x + 30;
MyTBox.y2 := y + 30;
if FindColor(x,y,color,MyTBox.x1,MyTBox.y1,MyTBox.x2, MyTBox.y2) then
result := true;
end;

First off, this creates a box around the x,y coordinate that is 30 pixels by 30 pixels. Then, if it finds "color" inside that box, it will result true. Used in conjunction with FindColorSkipBox, you can do some cool things.

function FindDifferentColor : boolean;
begin
if FindColor(x,y,9724949,MSX1,MSY1,MSX2,MSY2) then
if FindColorInBox(x,y,65366) then
if FindColorSkipBox(x,y,65366,MSX1,MSY1,MSX2,MSY2,MyT Box) then
result := true;
end;

Combining this procedure with the last one, check out what it does. If it finds the color 9724949, then it looks in a box around that for the color 65366. If it is found, then FindColorSkipBox will look for the color 65366
again, but without looking into MyTBox. Using this concept, you could determine whether an object is what you are looking for, or just something with a similar color.

For example, maybe a script wants to mine a rock but someone has an ores. It could use FindDIfferentColor, except it substitutes the first FindColor with the rock color, the second with the outer
parts of the ore, and the last one with the rock color. If it finds the rock color (which could either be the actual rock, or an ore) it looks in a TBox around the color to see if it finds the color that makes it an
ore. If it does, it will look again for the rock color, but skipping the box it found the dropped ore in! Useful, huh?

And... this leads me into TBoxArrays!


TBoxArrays

If you haven't read the last section, READ IT! Only then will you understand this part.

Lets say there is more than one ore dropped, and it is in many different locations. Now what? We can make the script skip every location the dropped ores are in.

We are basically using the same function as before, except looped and modified a bit.

function FindAllBoxes : boolean;
var
a : integer;
begin
SetArrayLength(MyTBoxArray,0)
repeat
if FindColorSkipBoxArray(x,y,RockColor,MSX1,MSY1,MSX2 ,MSY2,MyTBoxArray) then
begin
if FindColorInBox(x,y,OreColor) then
begin
SetArrayLength(MyTBoxArray,a + 1)
MyTBoxArray[a].x1 := MyTBox.x1;
MyTBoxArray[a].y1 := MyTBox.y1;
MyTBoxArray[a].x2 := MyTBox.x2;
MyTBoxArray[a].y2 := MyTBox.y2;
a := a + 1;
end else
break;
end;
until(false)
if FindColorSkipBoxArray(x,y,RockColor,MSX1,MSY1,MSX2 ,MSY2,MyTBoxArray) then
begin
MMouse(x,y,3,3);
Writeln('This should be our rock!');
end;
end;

So...let's go over this step by step.

First of all, we must Set our arraylength so we dont get a runtime error.
Second, We have FindColorSkipBoxArray to look for a color everywhere in the mainscreen except the TBoxes we told it to skip. We need TBoxArray to make it look for other instances of the color outside the ore. If we just put
FindColorSkipBox, We would constantly find the same 2 boxes.
Third, the FindColor is there to see if the orecolor if found. If it isnt, then we know our boxes are all made, therefore, we break from the loop.
Fourth, we find the middle of the function. You must add 1 to the arraylength if we are going to add a box to skip. Next, we declare the points of the tboxarray as the points we found our color inside of. Then, we add 1 so
the array increases next time we find the ore color. (and normally, that repeat loop would have a failsafe, but for demonstration purposes, i wont add one.
Finally, We look at the whole screen, skipping the boxes where the orecolor was found. We move our mouse there, and TADA! Our rock should be right under!

This is just one method of applying a TBoxArray. You can use them for looking for items in the inventory, in the bank, and on the floor. There are tons of uses for TBoxArrays!




So in conclusion, thank you for reading this tutorial. I appreciate the time you have spent. And make me happy by leaving a comment on this tut! Feel free to correct it or offer suggestions.

:D[i]Infantry001

lordsaturn
04-03-2007, 06:24 PM
Very nice, thx for the tut.

Infantry001
04-04-2007, 02:37 PM
Thanks for the first reply... lol

Btw, im only doing TBoxes if people read this tut. If not, im not going to waste my time

n3ss3s
04-05-2007, 07:23 PM
I read it.
Nice indeed.
Is it possible to just put
mouse(mytpoint,0,0,true);
??

Smartzkid
04-05-2007, 07:52 PM
awww....Read the title....got all excited that I was finally gonna get to read a tut on TBoxArrays.......then found out that that part's not written yet, and may never be written

But a nice tut, even though I really didn't learn anything, it solidified my knowledge of TPoints

Infantry001
04-05-2007, 11:10 PM
@n3ss3s, no you have to declare the x and y variable. Mouse doesnt accept a TPoint in its parameters

@SMartzKid, ill write the last part just for you :D

WhiteShadow
04-05-2007, 11:29 PM
Not to sound rude or anything but a TBox and TBoxArray tutorial can be scrapped up in around 5 - 10 minutes or less..

aha.. ;P

Nice what you've done so far though.

Infantry001
04-06-2007, 12:22 AM
Yea i just did it (It wouldve taken 5-10 minutes, but i had to go somwhere lol)

Smartzkid
04-06-2007, 12:59 AM
@SMartzKid, ill write the last part just for you :D

:D Thanks a ton man!

*ravenously reads the new piece of writing*

EDIT:

<3 it!

ronny.m.p
04-11-2007, 09:07 PM
Nice tut sometimes it's hard to teach people about things like TPoints but you did a great job!

TheGodfather
04-17-2007, 10:55 PM
This forum is perfect :p (thanks, I learned alot more than for what i came for :) )

TheGodfather
04-18-2007, 12:24 AM
super tut man...

lord19931014
07-22-2007, 07:00 PM
nice tut man keep it up :)

itSchRis917
07-24-2007, 01:17 AM
Maybe you aren't cut out to be a bussinessman Infintry001, but you might be cut out to be a teacher. ;) Really nice tut, helped me a lot with minimap finding and such.

gerauchert
07-26-2007, 06:28 PM
i actually understand this fully now THANKS!

EDIT: W00t 100th post

Infantry001
07-26-2007, 07:05 PM
Haha, thanks all. Feel free to ask any questions!

sherlockmeister
07-27-2007, 08:38 PM
did you people know that you can declare a TPoint/TBox array like this

MyTBoxArray: array of TBox;

i did that by mistake

Infantry001
08-01-2007, 05:10 AM
Yep, you can entirely. But, its easier to type TBoxArray ;) lol

Santa_Clause
08-01-2007, 06:29 AM
I love TBoxArray and TPointArray...

Can I do something like having the script search for a colour in 4 different boxes by making a TBoxArray and setting each parameter as one of them?

The Claw
08-09-2007, 09:08 AM
Thanks heaps for this tutorial, it has really helped me a lot :)

Sir R. M8gic1an
08-12-2007, 09:38 AM
amazing tutorial :D

Infantry001
08-21-2007, 02:24 AM
Santy, you mean like this?

procedure FindCols;
var
i,iX,iY : integer;
TBArr : array[0..3] of TBox;
begin
TBArr[0].x1 := 1;
TBArr[0].y1 := 2;
TBArr[0].x2 := 3;
TBArr[0].y2 := 4;
TBArr[1].x1 := 5;
TBArr[1].y1 := 6;
TBArr[1].x2 := 7;
TBArr[1].y2 := 8;
TBArr[2].x1 := 9;
TBArr[2].y1 := 10;
TBArr[2].x2 := 11;
TBArr[2].y2 := 12;
TBArr[3].x1 := 13;
TBArr[3].y1 := 14;
TBArr[3].x2 := 15;
TBArr[3].y2 := 16;
for i := 0 to 3 do
if FindColor(iX,iY,Color,TBArr[i].x1,TBArr[i].y1,TBArr[i].x2,TBArr[i].y2) then
break;
end;

Thanks, Claw and Rasta!

Distort
08-25-2007, 04:15 PM
I have a problem with something you said early on in this tutorial.

"First off, this creates a box around the x,y coordinate that is 30 pixels by 30 pixels. Then, if it finds "color" inside that box, it will result true. Used in conjunction with FindColorSkipBox, you can do some cool things."

You are looking in a 60*60 box arond the x and y co-ordinates. since you declared something like this:
x=100
y=100

MyTBox.x1 := x - 30 = 70;
MyTBox.y1 := y - 30 = 70;
MyTBox.x2 := x + 30 = 130;
MyTBox.y2 := y + 30 = 130;

Therefore, a 60*60 box. Unless, -30 means "keep x as it is"

Infantry001
08-25-2007, 07:41 PM
Haha, yeah, you're right. I should probably change that :p

Distort
08-25-2007, 08:37 PM
Apart from that minor detail, good tutorial ;)