SRL Archive - SRL Archives

Some Code Shortening


Rich

2009-10-10 06:03:28

{*******************************************************************************
function IsUpText(UpText: String): Boolean;
By: Freddy1990, edited by R1ch
Description: Returns up text that might be to the right
*******************************************************************************}

function IsUpText(UpText: string): Boolean;
begin
Result:= IsUpTextMulti([UpText]);
end;

{*******************************************************************************
function IsUpTextMulti(Text: TStringArray): Boolean;
By: Starblaster100 / Nielsie95 / Freddy1990, edited by R1ch
Description: Checks for the occurance of any of the strings in the array.
Use: IsUpTextMulti(['Att', 'ttac', 'ack', 'monst', 'ster'])
*******************************************************************************}

function IsUpTextMulti(Text: TStringArray): Boolean;
var
I : Integer;

begin
for I:= 0 to High(Text) do
begin
Result:= (Pos(Text[I], GetUpText) > 0);
if Result then
Exit;
end;
end;

{*******************************************************************************
function WaitUpTextMulti(S: TStringArray; Time: integer): Boolean;
By: Marpis & N1ke!, edited by R1ch
Description: Waits for a TStringArray of UpText, returns true if found
*******************************************************************************}
function WaitUpTextMulti(S: TStringArray; Time: Integer): Boolean;
var
T: Integer;
begin
Result := false;
T := GetSystemTime + Time;
while (GetSystemTime < T) do
begin
if (IsUpTextMulti(S)) then
begin
Result := True;
Exit;
end;
Wait(20 + Random(20));
end;
end;

{*******************************************************************************
function WaitFunc(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean;
By: Rasta Magician, small edit by EvilChicken!, even smaller by R1ch
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 '@'.
*******************************************************************************}
function WaitFunc(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean;
var
T: Integer;
begin
T := GetSystemTime + MaxTime;
while (GetSystemTime < T) do
begin
Result:= (Func());
if Result then
Exit;
Wait(WaitPerLoop);
end;
end;

{*******************************************************************************
Function DidClick(Red: Boolean; Time: integer);
By: Rasta Magician, edited by R1ch
Description: Checks for the X after a click, Times Out after Time.
*******************************************************************************}
Function DidClick(Red: Boolean; Time: integer): boolean;
var
TimeOut, X, Y, Trash: integer;
C : TIntegerArray;
begin
GetMousePos(x, y);
SetLength(C, 2);
C:= [255, 65535];
TimeOut := GetSystemTime + Time;
while (GetSystemTime < TimeOut) do
for I:= 0 to 1 do
if FindColor(Trash, Trash, C[I], X - 15, Y - 15, X + 15, Y + 15) then
begin
Result:= ((Red) xor (I = 0)) then
Exit;
end;
end;

{*******************************************************************************
procedure ClickNorth(AngleHigh : boolean);
By: R1ch
Description: Clicks the compass to make it perfect North. Will set angle at
highest or lowest depending on AngleHigh
*******************************************************************************}
procedure ClickNorth(AngleHigh: boolean);
begin
if Random(4) = 1 then
begin
MouseCircle(542, 24, 15, 2);
WaitOptionMulti(['ace', 'orth'], 1200);
end else
MouseCircle(542, 24, 15, 1);
SetAngle(AngleHigh);
end;

There was no custom options in IsUptextMultiCustom, so just changed the name to IsUptextMulti. Also shortened it.

Changed IsUptext and WaitUpTextMulti to use IsUptextMulti.

Shortened WaitFunc a little bit.

Shortened DidClick by a lot.

Changed ClickNorth to use MouseCircle.


Comments?

Quickmarch

2009-10-10 06:15:26

I've already shortened DidClick: http://www.villavu.com/forum/showthread.php?t=51355 , but as I found out, it only search's for one colour, and if it didn't find it, it would still be in the loop.

Rich

2009-10-10 06:31:25

From: Quickmarch
I've already shortened DidClick: http://www.villavu.com/forum/showthread.php?t=51355 , but as I found out, it only search's for one colour, and if it didn't find it, it would still be in the loop.


Ok then. I've added another modded DidClick, which checks for both colours.

Sir R. M8gic1an

2009-10-10 07:21:49

Please, stop with the shortnings, they serve no useful purpose and most of the time they are 'longer' for a reason.

Here is why yours won't be committed:
IsUpTextMulti: Yours is less efficient because it has to run GetUpText for each string in the array, while the current one only needs to get the text once.

IsUpText: The way you've done it it'll waste an awful lot of time looping through the array (which only have one string) and use a million variables you wouldn't need if you just did it the way it is done right now. In terms of wasted time, the current one is shorter than yours.

WaitUpTextMulti: the only change you did was make it use WaitUpTextMulti instead of WaitUpTextMultiCustom…

WaitFunc: no real changes, to be sure I don't really know which one is more efficient, but I'd say they're the same. No point changing, and yours is less readable(imo).

DidClick: Yours only checks one color…

ClickNorth: Mouse -> MouseCircle… bleh. Could be done; Not really a shortning tho.

So can we please stop with these shortnings? As you can see, there's a reason for us to make it longer!

~RM

Rich

2009-10-10 08:21:01

From: Rasta Magician
Please, stop with the shortnings, they serve no useful purpose and most of the time they are 'longer' for a reason.

Here is why yours won't be committed:
IsUpTextMulti: Yours is less efficient because it has to run GetUpText for each string in the array, while the current one only needs to get the text once.

IsUpText: The way you've done it it'll waste an awful lot of time looping through the array (which only have one string) and use a million variables you wouldn't need if you just did it the way it is done right now. In terms of wasted time, the current one is shorter than yours.


Fair enough.

From: Rasta Magician
WaitUpTextMulti: the only change you did was make it use WaitUpTextMulti instead of WaitUpTextMultiCustom…


I know…

From: Rasta Magician
WaitFunc: no real changes, to be sure I don't really know which one is more efficient, but I'd say they're the same. No point changing, and yours is less readable(imo).


Mine is more efficient. Less readable? :confused:

From: Rasta Magician
DidClick: Yours only checks one color…


No it doesn't.

From: Rasta Magician
ClickNorth: Mouse -> MouseCircle… bleh. Could be done; Not really a shortning tho.


Not a shortening, but this is what MouseCircle was made for…

From: Rasta Magician
So can we please stop with these shortnings? As you can see, there's a reason for us to make it longer!

~RM


How comes when Naum posts shortenings everyone loves it, but when I do, I get told to forget about it?

Dan Cardin

2009-10-10 08:51:20

Either certain devs dont like to be told that their work isnt as good as a lower member's…or i dont know. Personally i would take shortness and ease of expanding (like in here where i thought using an array to make it easier to add new options) over ease of reading. It may be less obvious, but it is still readable.

Sir R. M8gic1an

2009-10-10 08:53:39


function Empty:boolean;
begin
end;

function WaitFunc(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean;
var
T: Integer;
begin
{
T := GetSystemTime + MaxTime;
while (GetSystemTime < T) do
begin
}
if (Func()) then
begin
Result := True;
Exit;
end;
{
Wait(WaitPerLoop);
end;
}
end;

function WaitFuncRich(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean;
var
T: Integer;
begin
{
T := GetSystemTime + MaxTime;
while (GetSystemTime < T) do
begin
}
Result:= (Func());
if Result then
Exit;
{
Wait(WaitPerLoop);
end;
}
end;

var i, t: integer;

begin
t := GetSystemTime;
for i := 0 to 99999 do
WaitFunc(@Empty, 20, 300);

writeln('WaitFunc took: '+IntToStr(GetSystemTime-t));

t := GetSystemTime;
for i := 0 to 99999 do
WaitFuncRich(@Empty, 20, 300);

writeln('WaitFuncR1ch took: '+IntToStr(GetSystemTime-t));
end.


Successfully compiled (125 ms)
Line 1: [Hint] (1:10): Variable 'Result' never used in script
Line 7: [Hint] (7:1): Variable 'T' never used in script
Line 27: [Hint] (27:1): Variable 'T' never used in script
WaitFunc took: 1919
WaitFuncR1ch took: 2028

Successfully executed


As for DidClick, indeed it does check the two colors, but i guess you can figure out for yourself that calling FindColor twice is faster than calling it twice, inside of a loop and with an array?

And why are Nauman's shortenings loved? I have no clue. Maybe i should tell him off as well;

The only reason i say this is so people don't waste time doing something that is already done in a better way :s I'm sure you spent some time on these, only to find out they are less efficient :(

@Dan: Efficiency my friends, efficiency. Or at least in this case.

~RM

Rich

2009-10-10 11:21:09

Then why is everyone changing things to the way I changed WaitFunc, if it's less efficient?

Sir R. M8gic1an

2009-10-10 12:41:54

From: R1ch
Then why is everyone changing things to the way I changed WaitFunc, if it's less efficient?


Just because the mass does it, it does not mean it should be done… Chceck for yourself, would you rather have something less efficient? Go ahead.

~RM