PDA

View Full Version : Stuff every SRL scripter should know! pt. 1



munk
10-03-2007, 08:24 PM
Stuff every SRL scripter should know! pt. 1

1. Introduction
2. Using Arrays and For Statements
3. Fun with TVariants(pt.1 TPoints and Tboxes)
4. Optimizing Randoms


1. INTRODUCTION

To me, SCAR/SRL has always been about integrity. I want my scripts to be able to catch anything it might get itself into. If it misclicks an object, i want it to try again to click the object. If it walks past a covered symbol, it should find something to tell it to stop and go back. That sort of thing. So here I have gathered up a things that I use all the time. Alot of dealing with integrity, and some stuff I just think is cool :) I hope this can help someone.

2. USING ARRAYS AND FOR STATEMENTS

You should know what Arrays are by now, but I'm gonna do a quick explanation. Arrays are just variables that store a series of variables characterized by a number between 2 brackets after the Array name. (i.e. Array[0]) In SCAR, you must always set the array length before assigning any variables to it. It can be done when you are declaring the array, or in the script.


var
arrVar:Array[0..2] of Integer; // "0..2" declares ArrVar[0], ArrVar[1], and ArrVar[2]

or

var
arrVar:Array of Integer;
begin
SetArrayLength(arrVar,3); // this also declares ArrVar[0], ArrVar[1], and ArrVar[2]
end.


You may have noticed I used "Array of Integer", declaring all those variables as integers. You can declare them as any variable type, including array. (i.e. Array[0..2] of Array of string) Next is 'For to/downto do' statements(ill just be calling them "For Statements"), which you should also already know, but i'll still quickly go over it. A for Statement is basically a loop with an automatic counter. Counters are used and needed constantly and For Statements do all the work for you, it really is great. Quick example:

var
i:integer;
begin
for i:=0 to 5 do writeln(inttostr(i));
end.

First, declare the variable that is used for the counter(I almost always use "i" for my basic loops). This for statement will write a debug line of the var i, 6 times. Of course, each time its been increased by one. It should have counted from 0 to 6. I use for statements for everything, incase the first try at something messes up, it will loop and try again. The for statement makes it easy to assign how many tries it should do.

Now that you know both Arrays and For Statements you can really get into some cool stuff. First, this is the basic looping through an array using a for statement. If you can't do this then you'll have problems with the rest. Example:

var
i,len:integer;
arrVar:array of string;
begin
len:=getArrayLength(arrVar)-1; //minus 1 since the array starts on 0, not 1
For i:=0 to len do
begin
writeln(arrVar[i]);
end;
end.

That will loop through every variable stored in the array. Now if you understand that, lets get cookin. If I get one thing accross in this whole tutorial, Think logically outside the box. heres a great example of what I mean use an array with a for statement to create a walking procedure...

var
i:integer;
sr,er:array[0..3] of integer;
begin
sr[0]:=10;
er[0]:=180;
sr[1]:=50;
er[1]:=150;
sr[2]:=80;
er[2]:=240;
sr[3]:=100;
er[3]:=270;
for i:=0 to 3 do
begin
radialwalk(findroadcolor,sr[i],er[i],60,0,0);
end;
end.

All that is doing is changing the StartRadial and EndRadial so that itll walk a different way depending on the counter 'i'. That was just an example I wrote real quick, you can take it so much further. Use it for things like changing tolerance, search areas, and anything! Ill end this section with a good example from the chicken killer I wrote.

var
i:integer;
colors:array[0..3] of integer;
begin
if (not(loggedin)) then exit;
colors[0]:=5227996;
colors[1]:=923765;
colors[2]:=4962766;
colors[3]:=5269622;
for i:=0 to 3 do
begin
if findobj(x,y,'icken',colors[i],5) then
begin
mouse(x,y,0,0,false);
wait(50+random(50));
if chooseoption('tack') then
begin
findnormalrandoms;
flag;
ftwait(6);
exit;
end;
end;
end;
wait(50);
end;

This is a broken down version but all you need to see is that i loop through the colors of the chicken so that it performs a FindObj with each color.


3. FUN WITH TVARIANTS

So you may have seen some variables that have a 'T' before them and been like "whaa?". I dont know what the 'T' stands for so dont ask me, but I do know there just kinda special variables that hold special types of data. The one I really wanted to focus on is TBox's, of course, I must introduce the Tpoint first. Tpoints are really handy. A Tpoint is a variable that stores an X coordinate, and a Y coordinate. Example:

var
i:integer;
coords:TPoint;
begin
for i:=1 to 28 do
begin
coords:=itemcoords(i);
mouse(coords.x,coords.y,3,3,true);
end;
end.

coords is the TPoint variable, it assigned its X and Y coordinates by ItemCoords(an SRL procedure). This would click every inventory spot, i dont know why youd ever use it, it's just an example. You can assign the X and Y coordinates manually too:

var
coords:TPoint;
begin
coords.x:=300;
coords.y:=275;
end.


Now if you have an understanding of that, you can move on to TBoxes. TBoxes are essentially the same as TPoints, only they store 4 points instead of 2. TBoxes contain an X1, Y1, X2, and Y2 coordinate, creating a box. I mostly use these for the inventory with the handy SRL function InvBox. Heres an example from my fishing script.


for i:=1 to 28 do
begin
if existsitem(i) then // if theres an item in the slot
begin
slot:=invbox(i); //create Tbox of the inventory slot
if not(findDTM(flyrod,x,y,slot.x1,slot.y1,slot.x2,slo t.y2)) then //if its not a flyrod
if not(findDTM(feathers,x,y,slot.x1,slot.y1,slot.x2,s lot.y2)) then //and not feathers
begin
slotcoords:=itemcoords(i); //create TPoint of slot coordinates
mouse(slotcoords.x,slotcoords.y,3,3,false); //left click item in slot
wait(100+random(100));
chooseoption('All'); //deposit all
wait(200+random(300));
end;
end;
end;

This is during the bankscreen, and it's just going through all 28 inventory slots, and if its not a flyrod or feathers, it deposits it in the bank. well that's TPoints and TBoxes. I will go over other TVariables in later parts of this tutorial.


4. OPTIMIZING RANDOMS

So you got this script. it runs perfect when you watch it. but when you leave it, it misses a random and gets nailed with a baguette or whatever, and your in the middle of nowhere, and the script doesnt know how to handle it, and you wake up to your guy just walkin around in the middle of nowhere with and inventory full of mysterious boxes.....? just me? alright well, heres my point. There are little tricks to putting random checks in scripts. Along with failsafes, these can really improve what was a script that got caught up before. First, always note points in procedures. What I mean is, lets say your cooking. A random will usually appear when you use the raw food on the stove. Most randoms appear after a click on the target object. I always make sure to include a thourough random check after the main target click. most of mine look something like:

mouse(x,y,0,0,true);
findnormalrandoms;
flag;
ftwait(2);

That right there will catch almost all talking randoms. you can put excessive randomchecks in places that you wont ever get any randoms, but why? all you need is that up there^

Of course, I always try to include a findNormalRandoms and a findFastRandoms in my main loop. Its kinda a failsafe if a script gets caught up doing something that doesnt include a random check, it'll atleast run a random check in the main loop. Atleast you'll wake up to an inventory full of sapphires, security books, beer, and triangle sandwhiches, instead of mysterious boxes.If anyone else has any suggestions or ideas that help out, please post or pm them to me and ill ad them.




Alright, that's it for now. It was gonna be longer but I'm gonna save some of it for the next part. The next part will have more advanced stuff that I enjoy. I hope this helps someone, and please if you find any errors in this let me know. The code was mostly written really fast and I dont want a mistake teachin someone the wrong thing. haha. thanks for readin.

RudeBoiAlex
10-03-2007, 08:27 PM
Ever seen my scripting :p

not every srl members have to know that

munk
10-03-2007, 08:33 PM
Ever seen my scripting :p

not every srl members have to know that

hey, hey! i didnt say members. i said scripters ;) and its actually just stuff i think is cool and everyone should learn about.

RudeBoiAlex
10-03-2007, 08:38 PM
opps soory i just saw every SRL ;)

The Claw
10-08-2007, 08:17 AM
Nice :)

Negaal
12-12-2007, 03:58 PM
Nice...one thing i dont understand is
var
i:integer;
sr,er:array[0..3] of integer;
begin
sr[0]:=10;
er[0]:=180;
sr[1]:=50;
er[1]:=150;
sr[2]:=80;
er[2]:=240;
sr[3]:=100;
er[3]:=270;
for i:=0 to 3 do
begin
radialwalk(findroadcolor,sr[i],er[i],60,0,0);
end;
end.

this to show l33tness?
It could be only 4 lines when you don't use array...

Psychor
12-12-2007, 05:02 PM
nice tut, help to clear a few things for me.

I like to put a random check after i close my bankscreen also, i get them alot there.

King of Knives
12-13-2007, 07:49 PM
Nice:) T3h spam!! RAM's gonna get you :p Lol, just kidding :D

Good tut. I knew it all, though, but you should when you're SRL Member :p
Some SCAR-Newcomers could use the knowledge of arrays and simpler loops. I had been looking for arrays before I knew they existed, so.. :S And I used repeat for everything - Where I more efficiently could've used For Statements. Again, Good tutorial.

I wouldn't call the section about TxxxArrays "TVariants" since there is a special array-sort called "TVariantArray" that can store most types of variables. But that's just me.

-Knives

munk
12-13-2007, 09:45 PM
wow. i forgot i wrote this. Its a little outdated, i should fix it up.

@negaal - yes, it was to show l33tness:D no, actually it was just an example of how to think outside the box and manipulate things in a loop. Yes i realized how innefficient/redundant it is.

Thanks everyone. Im glad poeple still read this:D