zippoxer Posted October 22, 2011 Share Posted October 22, 2011 (edited) 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 December 4, 2011 by zippoxer replaced scar tag with code tag Quote Link to comment Share on other sites More sharing options...
Wanted Posted October 23, 2011 Share Posted October 23, 2011 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] Quote Link to comment Share on other sites More sharing options...
zippoxer Posted October 23, 2011 Author Share Posted October 23, 2011 Nice to know CallProc is also new for me. Let's delete this thread then. Quote Link to comment Share on other sites More sharing options...
FHannes Posted October 23, 2011 Share Posted October 23, 2011 Nice to know CallProc is also new for me. Let's delete this thread then. Heh, doesn't mean the thread isn't useful Nice to see people contribute Quote Link to comment Share on other sites More sharing options...