SRL Archive - OSR Help

Efficiency with if-else


purerange86

2014-01-20 16:34:59

Hey, I am coding a nmz script that I'll be releasing to the public. I currently have this procedure:


procedure drinkpray;
begin
if finddtm(pray1,x,y,mix1,miy1,mix2,miy2) then begin
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true);
end else if finddtm(pray2,x,y,mix1,miy1,mix2,miy2) then begin
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true);
end else if finddtm(pray3,x,y,mix1,miy1,mix2,miy2) then begin
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true);
end else if finddtm(pray4,x,y,mix1,miy1,mix2,miy2) then begin
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true);
end else if findcolortolerance(10933076,x,y,744,488,904,738,3) then begin
mmouse(x,y,1,1);
wait(133+random(222));
if isuptext('rink') then clickmouse2(mouse_left);
end else writeln('no more pray');
end;

It's functional but there's gotta be a better way to do this. Anyone able to show me how?

The Mayor

2014-01-20 16:44:04

From: purerange86
It's functional but there's gotta be a better way to do this. Anyone able to show me how?


Ok so firstly you don't need a begin and end if only one statement follows the 'then'



if isUpText('hello') then
writeLn('hi');

if isUpText('hello') then
begin
writeLn('hi');
writeLn('hey');
end;


procedure drinkpray;
begin

if finddtm(pray1,x,y,mix1,miy1,mix2,miy2) then
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true)
else
if finddtm(pray2,x,y,mix1,miy1,mix2,miy2) then
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true)
else
if finddtm(pray3,x,y,mix1,miy1,mix2,miy2) then
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true)
else
if finddtm(pray4,x,y,mix1,miy1,mix2,miy2) then
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true)
else
if findcolortolerance(10933076,x,y,744,488,904,738,3) then
begin
mmouse(x,y,1,1);
wait(133+random(222));
if isuptext('rink') then
clickmouse2(mouse_left);
end else
writeln('no more pray');
end;


E:

I guess if you didn't want a particular order you could just use or:


procedure drinkpray;
begin

if finddtm(pray1,x,y,mix1,miy1,mix2,miy2) or
finddtm(pray2,x,y,mix1,miy1,mix2,miy2) or
finddtm(pray3,x,y,mix1,miy1,mix2,miy2) or
finddtm(pray4,x,y,mix1,miy1,mix2,miy2) then
mouse(x+randomrange(-4,4),y+randomrange(-8,3),1,1,true)
else
if findcolortolerance(10933076,x,y,744,488,904,738,3) then
begin
mmouse(x,y,1,1);
wait(133+random(222));
if isuptext('rink') then
clickmouse2(mouse_left);
end else
writeln('no more pray');
end;


Brandon

2014-01-20 17:01:31


procedure drinkprayer;
var
I, X, Y: Integer;
begin
for I := 0 To High(Dtms) do
if (Not FindDTM(Dtms[i] , X, Y, MIX1, MIY1, MIX2, MIY2)) then
begin
if (FindColorTolerance(10933076, X, Y, 744, 488, 904, 738, 3)) then
begin
MMouse(x,y,1,1);
Wait(133 + Random(222));
if IsUptext('rink') then
ClickMouse2(MOUSE_LEFT);
Exit;
end;
end else
break;

Mouse(X + RandomRange(-4,4), Y + RandomRange(-8,3), 1, 1, True);
end;

purerange86

2014-01-21 21:32:01

From: Brandon

procedure drinkprayer;
var
I, X, Y: Integer;
begin
for I := 0 To High(Dtms) do
if (Not FindDTM(Dtms[i] , X, Y, MIX1, MIY1, MIX2, MIY2)) then
begin
if (FindColorTolerance(10933076, X, Y, 744, 488, 904, 738, 3)) then
begin
MMouse(x,y,1,1);
Wait(133 + Random(222));
if IsUptext('rink') then
ClickMouse2(MOUSE_LEFT);
Exit;
end;
end else
break;

Mouse(X + RandomRange(-4,4), Y + RandomRange(-8,3), 1, 1, True);
end;


Many thanks to both of you for all the help!

Brandon, this looks awesome, and is really helpful–thanks for putting the time into this. I didn't realize that you could use a if-not thing to define your variables, which I found really cool.
I'm unclear about a few things though. what does high(dtms) return? Do I need to define an array of dtms? Lastly what does "break" do?
Sorry, I'm a noob I know. I'm looking at a loops tut but I'm still not completely sure whats going on

The Mayor

2014-01-21 22:10:58

From: purerange86
Many thanks to both of you for all the help!

Brandon, this looks awesome, and is really helpful–thanks for putting the time into this. I didn't realize that you could use a if-not thing to define your variables, which I found really cool.
I'm unclear about a few things though. what does high(dtms) return? Do I need to define an array of dtms? Lastly what does "break" do?
Sorry, I'm a noob I know. I'm looking at a loops tut but I'm still not completely sure whats going on


for … to … do loops are a little bit more complicated that if .. then .. else statements :)

high() just returns the highest number in an array, in this case the highest number in the DTM array. e.g. [dtm1, dtm2, dtm3, dtm4] would return 3, as arrays start at 0 (0, 1, 2, 3).


for I := 0 To High(Dtms) do

is basically


for I := 0 To 3 do

The break means it will break out of the loop, and continue with the rest of the procedure (which in this case is just the mouse() statement). If it does find any of the DTMs in the array it will break out of the loop and move the mouse to it and click. To use this you would obviously have to put all your DTMs into an array when you start up the script.

Brandon

2014-01-21 22:11:15

From: purerange86
Many thanks to both of you for all the help!

Brandon, this looks awesome, and is really helpful–thanks for putting the time into this. I didn't realize that you could use a if-not thing to define your variables, which I found really cool.
I'm unclear about a few things though. what does high(dtms) return? Do I need to define an array of dtms? Lastly what does "break" do?
Sorry, I'm a noob I know. I'm looking at a loops tut but I'm still not completely sure whats going on


DTMs is an array of DTM's and High returns the (length - 1).

Break exits the loop but not the function. "Breaks" out of the loop. The reason I found it more efficient is because it is easier to just create an array of DTM's and have the loop find DTMs.. Rather than "Find DTM1, FindDTM2, FindDTM3, etc..".


Up to you though.