PDA

View Full Version : Need help with repeat function



rapid_spitfire
05-11-2014, 06:34 PM
Hey all, i've made an Al-Kharid tanner script, using mostly modified code from mayors all in one tutorial. It works alright but sometimes it fails to find "Ellis"(tanner) because he's behind a wall or something while its searching for his color, then it runs back to the bank without tanning the cowhides. Anyways just wondering how i could make it keep searching for him until it finds him and right clicks him. Here's the code i use. I'm 3 days in so its very novice.

procedure Rtan();
var
myBox: TBox;
x, y,i,d: integer;
TPA: TPointArray;
ATPA: T2DPointArray;

begin

wait(randomRange(500,1500));
findColorsSpiralTolerance(x, y, TPA, 15333370, mainScreen.getBounds(), 1, colorSetting(2, 0.99, 10.96));

if length(TPA) < 1 then
exit;

smartImage.debugTPA(TPA, false);
ATPA := TPA.toATPA(30,30);
ATPA.filterBetween(0, 3);
ATPA.sortFromMidPoint(mainscreen.playerPoint);
smartImage.debugATPA(ATPA);
for i := 0 to high(ATPA) do
begin

MiddleTPAEx((ATPA[i]), x, y);
mouse(x, y, 10, 30, MOUSE_MOVE);
if isMouseOverText(['llis'], 5000) then
begin
// GetMousePos(x,y);

fastClick(MOUSE_RIGHT);
if chooseOption.select(['ides'])then
wait(randomRange(1200,3400));
myBox := intToBox(287, 329, 508, 350);
mouseBox(myBox, MOUSE_LEFT);

smartImage.clear;
wait(randomRange(100,1000));
end;



end;
end;

The Killer
05-11-2014, 06:41 PM
Firstly, please keep standards up (see http://villavu.com/forum/showthread.php?t=60288) as its hard to read your code without proper standards.
I don't know how much knowledge you have but you should change the procedure into a function and do something along the lines of:

MarkTime(T);
repeat
//finding stuff here
if WaitOption then
begin
Wait(randomRange(1200,3400));
Result := true;
end;
until(result) or (TimeFromMark(T) > 10000);


when looping always remember to have a way to break the loop such as the timeout I have shown above

rapid_spitfire
05-11-2014, 07:08 PM
Thanks for the quick response, i will try to follow the standards from now on, its just a lot to take in at once. I'm still a little confused how to use the function instead of the procedure, I'll read into it more. (I've only used simba for 3 days so it'll improve)

masterBB
05-11-2014, 08:30 PM
Thanks for the quick response, i will try to follow the standards from now on, its just a lot to take in at once. I'm still a little confused how to use the function instead of the procedure, I'll read into it more. (I've only used simba for 3 days so it'll improve)

Fixed spacing. This is better readable for people who might be able to help you.

procedure Rtan();
var
myBox: TBox;
x, y, i, d: integer;
TPA: TPointArray;
ATPA: T2DPointArray;
begin
wait(randomRange(500, 1500));
findColorsSpiralTolerance(x, y, TPA, 15333370, mainScreen.getBounds(), 1, colorSetting(2, 0.99, 10.96));

if length(TPA) < 1 then
exit;

smartImage.debugTPA(TPA, false);
ATPA := TPA.toATPA(30, 30);
ATPA.filterBetween(0, 3);
ATPA.sortFromMidPoint(mainscreen.playerPoint);
smartImage.debugATPA(ATPA);

for i := 0 to high(ATPA) do
begin
MiddleTPAEx((ATPA[i]), x, y);
mouse(x, y, 10, 30, MOUSE_MOVE);

if isMouseOverText(['llis'], 5000) then
begin
fastClick(MOUSE_RIGHT);

if chooseOption.select(['ides']) then //note that this if only determines if the wait is executed, not the code below that.
wait(randomRange(1200, 3400));

myBox := intToBox(287, 329, 508, 350);
mouseBox(myBox, MOUSE_LEFT);
smartImage.clear;
wait(randomRange(100, 1000));
end;
end;
end;

rj
05-11-2014, 09:28 PM
Btw converting the ATPA can be done in 1 line if you wanted to, it might appear sloppy though

ATPA := TPA.toATPA(30, 30).filterBetween(0, 3).sortFromMidPoint(mainscreen.playerPoint);

rapid_spitfire
05-12-2014, 01:59 AM
Thanks guys, you're really helpful

The problem i have is that sometimes "Ellis" is behind a wall or something so his ATPA isn't found when it searches the first time, then it goes to the next procedure (runToBank();) without tanning. How would you guys make it keep searching for him until the if chooseOption.select(['ides']) then is found? Sometimes it finds him and misses him with the click if he moves also. Please explain using code above for easy understanding for myself

One more thing, can you teach me how to find a dtm in the bank screen? It doesnt seem to work when i tell it to get mainscreen bounds then look for it. I've been using myBox := intToBox(287, 329, 508, 350);
mouseBox(myBox, MOUSE_LEFT); but i know thats bad because the location can change of the item

rapid_spitfire
05-14-2014, 04:17 AM
Hey guys, I really need help setting up a repeat loop, i implemented j and was wondering if this would work.

procedure Rtan();
var
myBox: TBox;
x, y, i, d, j: integer;
TPA: TPointArray;
ATPA: T2DPointArray;
begin
repeat//repeat
j:=0;
wait(randomRange(500, 1500));
findColorsSpiralTolerance(x, y, TPA, 15333370, mainScreen.getBounds(), 1, colorSetting(2, 0.99, 10.96));

if length(TPA) < 1 then
exit;

smartImage.debugTPA(TPA, false);
ATPA := TPA.toATPA(30, 30);
ATPA.filterBetween(0, 3);
ATPA.sortFromMidPoint(mainscreen.playerPoint);
smartImage.debugATPA(ATPA);

for i := 0 to high(ATPA) do
begin
MiddleTPAEx((ATPA[i]), x, y);
mouse(x, y, 10, 30, MOUSE_MOVE);

if isMouseOverText(['llis'], 5000) then
begin
fastClick(MOUSE_RIGHT);

if chooseOption.select(['ides']) then //I want it to repeat until this option is found
begin
inc(j);//here
wait(randomRange(1200, 3400));

myBox := intToBox(287, 329, 508, 350);
mouseBox(myBox, MOUSE_LEFT);
smartImage.clear;
wait(randomRange(100, 1000));
end;
end;
end; until(j>0);//until here
end;

sdf
05-14-2014, 04:57 AM
That would work (the j:=0 at the start is redundant, since it's a local procedure) but for neatness, you can do a while loop:

function Rtan(): Boolean;
var
myBox: TBox;
x, y, i, d: integer;
TPA: TPointArray;
ATPA: T2DPointArray;
begin
while not result do
begin
wait(randomRange(500, 1500));
findColorsSpiralTolerance(x, y, TPA, 15333370, mainScreen.getBounds(), 1, colorSetting(2, 0.99, 10.96));
if length(TPA) < 1 then
exit(false);
smartImage.debugTPA(TPA, false);

ATPA := TPA.toATPA(30, 30);
ATPA.filterBetween(0, 3);
ATPA.sortFromMidPoint(mainscreen.playerPoint);
smartImage.debugATPA(ATPA);

for i := 0 to high(ATPA) do
begin
MiddleTPAEx((ATPA[i]), x, y);
mouse(x, y, 10, 30, MOUSE_MOVE);
if isMouseOverText(['llis'], 5000) then
begin
fastClick(MOUSE_RIGHT);
if chooseOption.select(['ides']) then
begin
result := true;
wait(randomRange(1200, 3400));
myBox := intToBox(287, 329, 508, 350);
mouseBox(myBox, MOUSE_LEFT);
smartImage.clear;
wait(randomRange(100, 1000));
end;
end;
end;
end;
end;

Does the same thing.

The Mayor
05-14-2014, 05:04 AM
Hey guys, I really need help setting up a repeat loop


if chooseOption.select(['ides']) then //I want it to repeat until this option is found


You do realise that chooseOptions is the list of options that popup when you right click? You wouldn't want to repeat this if you haven't found Ellis yet :p

It sounds to me like you want to repeat searching for Ellis until you successfully right click him and select 'ides' option. A new window pops up asking you what hides you want to tan, so you could repeat looking for Ellis until that window pops up.

An easy way to do this would be to repeat the Rtan procedure above, until that window is open. To know if the window is open you could create a simple boolean function.


function isTanMenuOpen(): boolean;
begin
result := (getColor(250, 46) = 379903);
end;


This checks if a single TPoint on the screen (in this case the point(250, 46)) matches the colour 379903. For example, you could pick the yellow colour from part of the 'C' in Crafting (which is what I did above). Just drag the cross-hair onto smart and use the colour picker.

http://puu.sh/8LBzN.jpg

Then you could do something like:


procedure tanHides();
begin

repeat
Rtan(); // run your RTan procedure to look for Ellis

if isTanMenuOpen then // if this isTanMenuOpen results true then break out of the loop
break;

until (false);

//when you break out of the repeat..until loop you end up here
// here you would click Tan button with mouseBox
end;


You could easily make it a bit more advanced by making it rotate the compass each time it loops to give it a better chance of finding Ellis:


procedure tanHides();
begin

repeat
Rtan();

if isTanMenuOpen then
break;

randomCompass(10, 200, false); //if Rtan failed and Menu isnt open then it will rotate compass and search again

until(false)

// tan button
end;


However, this could potentially loop forever, so it's best to add in a timer.


procedure tanHides();
var
timer: TTimeMarker; //declare 'timer' as a TTimeMarker
begin

timer.start(); //start the timer

repeat
Rtan();

if isTanMenuOpen then
break;

randomCompass(10, 200, false);

until timer.getTime() > 60000; //repeat for a max of 60 seconds

//Tan button

end;


If you wanted to make it even better you could make it walk to Ellis again if it hasn't found him within 20 seconds


procedure tanHides();
var
timer: TTimeMarker;
begin

timer.start();

repeat
Rtan();

if isTanMenuOpen then
break;

randomCompass(10, 200, false);

if timer.getTime() > 20000 then
walkToEllis(); //of whatever your walk procedure is

until timer.getTime() > 60000;

//Tan button

end;

rapid_spitfire
05-14-2014, 05:42 AM
Man, i love you guys, thanks for putting the time into helping me. And Mr.Mayor I'm using SPS walking, wouldnt it mess up if i rotated the compass? My next step in my script is to make it less bot like so I'll be using that rotate compass function you just taught me. Could i blindwalk or use some other method that won't matter if the compass is north?

Also in the code below can i use Rtan(); like that in another procedure or do i need to copy paste the code into this new procedure?

procedure tanHides();
begin

repeat
Rtan(); //Exception in Script: Unknown declaration "Rtan" at line 26, column 5

if isTanMenuOpen then
break;

randomCompass(10, 200, false);

until(false)

// tan button
end;

The Mayor
05-14-2014, 07:27 AM
Man, i love you guys, thanks for putting the time into helping me. And Mr.Mayor I'm using SPS walking, wouldnt it mess up if i rotated the compass? My next step in my script is to make it less bot like so I'll be using that rotate compass function you just taught me. Could i blindwalk or use some other method that won't matter if the compass is north?

Also in the code below can i use Rtan(); like that in another procedure or do i need to copy paste the code into this new procedure?

procedure tanHides();
begin

repeat
Rtan(); //Exception in Script: Unknown declaration "Rtan" at line 26, column 5

if isTanMenuOpen then
break;

randomCompass(10, 200, false);

until(false)

// tan button
end;

If you call a procedure inside another procedure, it must be above it in the script. So Rtan must be above tanHides (simba reads top to bottom).

You dont have to have north, you can put


spsAnyAngle := true;


in your mainloop after you setupSRL(); This might be tiny bit less accurate and a tiny bit slower but you shouldn't notice a difference. If you want it to be north, you could just add

minimap.setAngle(MM_DIRECTION_NORTH);

before you call sps.walkPath(path)


EDIT: Forgot to say you should remove the mousebox stuff from Rtan as you would put it at the bottom of tanHides


procedure Rtan();
var
x, y, i: integer;
TPA: TPointArray;
ATPA: T2DPointArray;
begin
findColorsSpiralTolerance(x, y, TPA, 15333370, mainScreen.getBounds(), 1, colorSetting(2, 0.99, 10.96));

if length(TPA) < 1 then
exit;

smartImage.debugTPA(TPA, false);
ATPA := TPA.toATPA(30, 30);
ATPA.filterBetween(0, 3);
ATPA.sortFromMidPoint(mainscreen.playerPoint);
smartImage.debugATPA(ATPA);

for i := 0 to high(ATPA) do
begin
MiddleTPAEx((ATPA[i]), x, y);
mouse(x, y, 10, 30, MOUSE_MOVE);

if isMouseOverText(['llis'], 5000) then
begin
fastClick(MOUSE_RIGHT);
chooseOption.select(['ides']) then
smartImage.clear;
wait(randomRange(1200, 3400));
break;
end;
end;
end;

rapid_spitfire
05-14-2014, 09:34 PM
Hey, when i use spsAnyAngle := true; It keeps failing to find my location, i put it in my main loop under setupSRL(); SPS only seems to work for me if the compass is true north. I'm learning so much from you, thanks dude.

Olly
05-14-2014, 09:59 PM
SPS finds it pretty hard to get position in the desert, I would suggest trying another method.

rapid_spitfire
05-14-2014, 11:30 PM
SPS finds it pretty hard to get position in the desert, I would suggest trying another method.

What would you recommend? I havnt learnt any other method yet