Jump to content
zippoxer

Waiting for a color/bitmap to appear with a timeout

Recommended Posts

Using WaitTrue and WaitFalse functions you can wait for any function to return True or False with a timeout.

 

For example, I have a function called Attack that simply clicks the monster and another called HasTarget that checks if any creature is targeted and returns True or False. I want to have a safe bot that understands possible mistakes and fixes them. Let's implement it:

Attack(creatureX, creatureY);
if WaitTrue(@HasTarget, 2000) then
begin
 WriteLn('Creature successfully targeted. Now waiting for it to die.');
 if WaitFalse(@HasTarget, 30000) then
 begin
   WriteLn('Success. Target disappeared (died), looting it.');
   // TODO: Looter.
 else
 begin
   WriteLn('Timeout. 30 seconds past and the target is still alive. Re-attacking it.');
   // TODO: Re-attack.
 end;
else
begin
 WriteLn('Timeout. 2 seconds past and nothing is targeted yet, trying another creature');
 // TODO: Try to attack another creature...
end;[/scar]

If anyone is interested, this is the needed block of code to have WaitTrue and WaitFalse:
[scar]type
 Void = function: Boolean;

function WaitFunc(func: Void; positive: Boolean; timeout: Integer): Boolean;
var start: Integer;
begin
 start := GetSystemTime;
 Result := True;
 while func() <> positive do
 begin
   if GetSystemTime - start >= timeout then
   begin
     Result := False;
     break;
   end;
 end;
end;

function WaitTrue(func: Void; timeout: Integer): Boolean;
begin
 Result := WaitFunc(func, True, timeout);
end;

function WaitFalse(func: Void; timeout: Integer): Boolean;
begin
 Result := WaitFunc(func, False, timeout);
end;

Edited by zippoxer
replaced scar tag with code tag
Link to comment
Share on other sites

OSI already has these

 

[sCAR]{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function WaitFunc(Func: function: Boolean; DesiredResult: Boolean; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Boolean;

Contributors: Wanted

Description: Repeats a function until DesiredResult.

Date Created: August 10th, 2011. By Wanted

Last Modified: September 26th, 2011. By Wanted

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

 

function WaitFunc(Func: function: Boolean; DesiredResult: Boolean; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Boolean;

var

T: LongInt;

begin

Result := True;

T := GetSystemTime + RR(MinTotalWait, MaxTotalWait);

while (GetSystemTime < T) do

if (Func() = DesiredResult) then

Exit

else

WaitRR(MinWait, MaxWait);

Result := False;

end;

 

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function WaitFuncEx(Func: string; Parameters: TVariantArray; DesiredResult: Boolean; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Boolean;

Contributors: Wanted

Description: Repeats a function until DesiredResult.

Date Created: September 26th, 2011. By Wanted

Last Modified: September 26th, 2011. By Wanted

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

 

function WaitFuncEx(Func: string; Parameters: TVariantArray; DesiredResult: Boolean; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Boolean;

var

T: LongInt;

begin

Result := True;

T := GetSystemTime + RR(MinTotalWait, MaxTotalWait);

while (GetSystemTime < T) do

if (CallProc(Func, Parameters) = DesiredResult) then

Exit

else

WaitRR(MinWait, MaxWait);

Result := False;

end;

 

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function WaitFindColorTolerance(var X, Y: Integer; Color, X1, Y1, X2, Y2, Tol: Integer; DesiredResult: Boolean; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Boolean;

Contributors: Wanted

Description: Repeats FindColorTolerance until DesiredResult.

Date Created: September 26th, 2011. By Wanted

Last Modified: September 26th, 2011. By Wanted

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

 

function WaitFindColorTolerance(var X, Y: Integer; Color, X1, Y1, X2, Y2, Tol: Integer; DesiredResult: Boolean; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Boolean;

var

T: LongInt;

begin

Result := True;

T := GetSystemTime + RR(MinTotalWait, MaxTotalWait);

while (GetSystemTime < T) do

if (FindColorTolerance(X, Y, Color, X1, Y1, X2, Y2, Tol) = DesiredResult) then

Exit

else

WaitRR(MinWait, MaxWait);

Result := False;

end;

 

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

procedure WaitNone;

Contributors: Wanted

Description: For use with while (this) do DoNothing

Date Created: August 10th, 2011. By Wanted

Last Modified: August 10th, 2011. By Wanted

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

 

procedure WaitNone;

begin

end;[/sCAR]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...