PDA

View Full Version : Timing



TomTuff
12-27-2010, 11:49 PM
There are many functions available in the SRL include that can makes script more effective in many ways. They allow the avoidance of static waits (i.e. Wait(Amount + Random(Int)); ). They allow the script to continue on as soon as possible. They also allow the script to be able to handle lag better.

Let's take an example of clicking a tree. Once you mouse to the tree, you would naturally check for the uptext next, right? Most people would do something like Wait(800 + Random(80)); after they have MMouse'd. However, in cases of lag and slow computers, the uptext might not show up in this time. On faster computers, the uptext might have come up in about 100 ms. You could instead use WaitUptext('illow', 1200);. Let's compare how this would work to how a human plays: A human KNOWs it's going to have its mouse pointer on that tree. They wouldn't look at the uptext. The only reason our scripts do is to make sure we did things right. So, using WaitOption, we continue on as soon as we can, thus seeming more human like.

Here are some of the best functions in timing.scar:


{************************************************* ******************************
function WaitOption(S: String; Time: Integer): Boolean;
By: N1ke!
Description: Waits for an Option and selects it
************************************************** *****************************}

{************************************************* ******************************
function WaitOptionMulti(S: TStringArray; Time: Integer): Boolean;
By: Marpis, N1ke! & Rasta Magician
Description: Waits for a TStringArray of options and selects one of them
************************************************** *****************************}

{************************************************* ******************************
function WaitUptext(S: String; Time: Integer): Boolean;
By: Marpis edited by N1ke!
Description: Waits for an UpText, returns true if found
************************************************** *****************************}

{************************************************* ******************************
function WaitUpTextMulti(S: TStringArray; Time: integer): Boolean;
By: Marpis & N1ke!
Description: Waits for a TStringArray of UpText, returns true if found
************************************************** *****************************}

{************************************************* ******************************
function WaitFunc(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean;
By: Rasta Magician, small edit by EvilChicken!
Description: Waits for function Func to be true. WaitPerLoop is how often you
want to call "Func" function.
Example: "WaitFunc(@BankScreen, 10 + Random(15), 750);" will check if BankScreen
is open every 10-25th millisecond, for a maximum of 750 milliseconds.
Notice the '@'.
************************************************** *****************************}
//^Personal favorite


{************************************************* ******************************
function WaitFindColors(var x, y: integer; Color, Tol, x1, y1, x2, y2, MaxTime: integer): Boolean;
By: IceFire908 based completely off WaitFindColor
Description: Waits for a color at (x, y) with tolerance Tol, returns true if found
************************************************** *****************************}

{************************************************* ******************************
function WaitInvCount(Count, MaxTime: Integer; CountType: (MaxCT, MinCT, ExactCT)): boolean;
By: Rasta Magician
Description: Waits for a maximum inv count. Returns true if InvCount <= Count
************************************************** *****************************}

//all found in timing.scar of SRL



It's small, but it can make a huge difference in the human-likeness and efficiency of your scripts.

Here are some examples of when using these functions could be used.

Ex 1: Result.
Using some of these functions could be used to return a boolean in a function.

function OpenTheBank: Boolean;
begin
//banking stuff
Result := WaitFunc(@BankScreen, 10 + Random(5), 4000);
end;


you could also use WaitInvCount.

function WithdrawSomeItem: Boolean;
begin
//stuff here
Result := WaitInvCount(1, 4000, MinCT);
end;


Ex 2: Uptext, Options
you could replace this

function ClickRock: Boolean;
var
Rock: TPoint;
begin
Rock := FindRock;
MMouse(Rock.X, Rock.Y, 4, 4);
Wait(200 + Random(20));
if IsUptext('oal') then
begin
ClickMouse2(False);
Result := ChooseOption('ine');
end;
end;

with


function ClickRock: Boolean;
var
Rock: TPoint;
begin
Rock := FindRock;
MMouse(Rock.X, Rock.Y, 4, 4);
if WaitUptext('oal', 1200) then //see here
begin
ClickMouse2(False);
Result := WaitOption('ine', 1200); //and here
end;
end;


Protip: When searching for options or uptext, i generally make the max time be 400 * number of chars you're searching for.

3Garrett3
12-28-2010, 02:32 AM
Repped :)

A couple functions I didn't know about. WaitFunc seems really useful, can you use pointers to direct it at any simple function?

NCDS
12-28-2010, 03:09 AM
Repped :)

A couple functions I didn't know about. WaitFunc seems really useful, can you use pointers to direct it at any simple function?

Really? Quite useful.

You would use it like:

WaitFunc(@BankScreen, 2500); // Would wait 2 and a half seconds or until BankScreen is found.

3Garrett3
12-28-2010, 03:19 AM
Really? Quite useful.

You would use it like:

WaitFunc(@BankScreen, 2500); // Would wait 2 and a half seconds or until BankScreen is found.


Can you use it like:


WaitFunc(@FindRock, 10000);


And it would wait 10 seconds for the script to find the rock, and return false if it doesn't after 10 seconds? So it will search over and over for 10 seconds and then quit?

NCDS
12-28-2010, 03:22 AM
Can you use it like:


WaitFunc(@FindRock, 10000);


And it would wait 10 seconds for the script to find the rock, and return false if it doesn't after 10 seconds? So it will search over and over for 10 seconds and then quit?

If you used it in a if..then, or to set the Result, then yes. :)

TomTuff
12-28-2010, 06:56 AM
Really? Quite useful.

You would use it like:

WaitFunc(@BankScreen, 2500); // Would wait 2 and a half seconds or until BankScreen is found.


Wrong number of params, but yeah, that's how it works :P
Correct params:

WaitFunc(@BankScreen, 10 + Random(15), 2500);



Can you use it like:


WaitFunc(@FindRock, 10000);


And it would wait 10 seconds for the script to find the rock, and return false if it doesn't after 10 seconds? So it will search over and over for 10 seconds and then quit?

yes, but you don't really need to do that if you use the other timing functions :P


off note:
the function can't have any parameters.

Wanted
12-28-2010, 07:17 AM
Cool tutorial. Could use some examples though ;)

TomTuff
12-28-2010, 07:48 AM
Cool tutorial. Could use some examples though ;)

added a couple, not very good examples though :O

Dgby714
12-28-2010, 08:09 AM
I was bored...

{************************************************* ******************************
function WaitFuncEx(Func: string; var Args: TVariantArray; WaitPerLoop, MaxWait: integer): boolean;
By: Dgby714
Description: Calls Func with arguments Args every WaitPerLoop milliseconds
for a max of MaxWait milliseconds.
Func can return any basic types (boolean, string, integer).
boolean: Returns it.
string: Returns true if string equals 'true'.
integer: Returns true unless it equals 0.
************************************************** *****************************}
function WaitFuncEx(Func: string; var Args: TVariantArray; WaitPerLoop, MaxWait: integer): boolean;
var
T: integer;
Temp: variant;
begin
T := GetSystemTime + MaxWait;
while (GetSystemTime < T) do
begin
Temp := CallProc(Func, Args);
if (not (VarType(Temp) = varString)) then
Temp := ToStr(Temp);
Result := StrToBoolDef(Temp, False);
if (Result) then
Exit;
Wait(WaitPerLoop);
end;
end;

Example: Using FindColor, x and y are in V[0] and V[1]
program new;

function FindColorEx(var x, y: integer; color, xs, ys, xe, ye: integer): boolean;
begin
Result := FindColor(x, y, color, xs, ys, xe, ye);
end;

{************************************************* ******************************
function WaitFuncEx(Func: string; var Args: TVariantArray; WaitPerLoop, MaxWait: integer): boolean;
By: Dgby714
Description: Calls Func with arguments Args every WaitPerLoop milliseconds
for a max of MaxWait milliseconds.
Func can return any basic types (boolean, string, integer).
boolean: Returns it.
string: Returns true if string equals 'true'.
integer: Returns true unless it equals 0.
************************************************** *****************************}
function WaitFuncEx(Func: string; var Args: TVariantArray; WaitPerLoop, MaxWait: integer): boolean;
var
T: integer;
Temp: variant;
begin
T := GetSystemTime + MaxWait;
while (GetSystemTime < T) do
begin
Temp := CallProc(Func, Args);
if (not (VarType(Temp) = varString)) then
Temp := ToStr(Temp);
Result := StrToBoolDef(Temp, False);
if (Result) then
Exit;
Wait(WaitPerLoop);
end;
end;

var
V: TVariantArray;

begin
V := [0, 0, 16711680, 4, 4, 500, 500];
WriteLn(WaitFuncEx('FindColorEx', V, 10 + Random(15), 1000));
WriteLn(V);
end.

TomTuff
12-28-2010, 08:35 AM
Whoa Dg, that should be added to SRL promptly!

kingarabian
12-28-2010, 09:59 PM
Whoa Dg, that should be added to SRL promptly!

Tom what If im doing this:
I am clicking the spell and moving the mouse to the coordinates of where I want it to be casted. I want it to keep the mouse there until the object/item appears.

Function GetReadyToCast(var x,y:integer):boolean;
var
TempPoint : TPoint;
begin
if MSCheckWine > 0 then
exit;
result := SearchForColorOnTile(x, y, Tile(2931,3515), 80, 40, GROUND_COLOR_WINE_GRAY, 8, 10); //credits nearly all to Feroc1ty.
cast('telekinetic grab', false);
TempPoint := TileToMs(Point(2931,3515),random(4));
MMouse(TempPoint.x,TempPoint.y,2,2)
Writeln('Ready to cast');
end;

^That is roughly how I want to do it, however if I add it to the main loop , it keeps clicking the spell and moving the mouse until the object/item shows. I dont want it to do that, I want it to cast the spell and move the mouse to the position of the object/item and then keep the mouse there until the object/item appears so it can click it.

So I was looking over timing.scar and I tried to add some functions from there but cant really get it to work out. Anyhelp would be appreciated.

TomTuff
12-28-2010, 10:10 PM
function IsSpellActivated(SpellBox: TBox): Boolean; //spellbox is the box to look in
var
T: Integer;
begin
if not(LoggedIn) then
Exit;
T := GetSystemTime + 6000; //change the + 6000 for the max time
while (GetSystemTime <= T) do
begin
Result := (CountColorTolerance(clWhite, SpellBox.X1, SpellBox.Y1, SpellBox.X2, SpellBox.X2, 8) > Amount);//changing clWhite to the white outline of the active spell may be necessary. Amount should be the minimum number of white pixels to find
if Result then
Exit;
Wait(10 + Random(15));
end;
end;

There might be a function in like that in magic.scar, but if there's not, that should work great!

edit: wait nevermind, i thought you wanted to wait for the spell to be active...
just change the "Result" line to whatever is necessary!

Coh3n
01-16-2012, 09:24 PM
Dgby, that function is added for the next version. :)

bud related
01-17-2012, 05:15 AM
hey i keep geting errors when i go to run simba here is the errorError] C:\Simba\Includes\MSI/MSI/Core/Antiban.simba(340:31): Invalid number of parameters at line 339
Compiling failed.
[Error] (46:5): Unknown identifier 'mouse' at line 45
Compiling failed.
plz help