Learn to use Cases!
In this basic tutorial I will be showing you the advantages of using cases, what they are, why we use them and I will show you how much better it makes your script look!
What are cases?
Cases are basically a method which is simply faster and neater, to call many things in one procedure, instead of having many procedures.
Difference between non cased and cased procedures.
For example of a non-cased procedure:
SCAR Code:
procedure WhatTreeWeCutting;
begin
if ((GetSkillLevel('woodcutting')) < 14) and
((GetSkillLevel('woodcutting')) >= 1) then
TreeToCut := Normal;
if ((GetSkillLevel('woodcutting')) < 29) and
((GetSkillLevel('woodcutting')) >= 15) then
TreeToCut := Oak;
if ((GetSkillLevel('woodcutting')) < 99) and
((GetSkillLevel('woodcutting')) >= 30) then
TreeToCut := Willow;
end;
And a cased procedure of the same thing:
SCAR Code:
procedure DoWhatToCut;
begin
Case (GetSkillLevel('woodcutting')) of
1..14: TreeToCut := Normal;
15..29: TreeToCut := Oak;
30..99: TreeToCut := Willow;
end;
end;
Note: notice how i use 1..15. Cases dont have to have this, this basicly means the numbers 1 to 15, instead of typing it as 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15: TreeToCut......
The method above is the Integer case way, you can also do a string way as exampled bellow:
SCAR Code:
procedure DoWhatToCut;
begin
Case LowerCase(TreeToCut) of
'normal': Writeln('weeee normal tree is what we want to cut');
'oak': Writeln('weeee oak tree is what we want to cut');
'willow': Writeln('weeee willow tree is what we want to cut');
end;
end;
The case above converts TreeToCut to all chars to be lowercase so then you wont have any problems identifying your case.
Failsafe cases
To failsafe a case you must use else at the end of it, so if none of the options were picked it does the else, for example if the case doesnt match the criteria it will do the else:
SCAR Code:
procedure DoWhatToCut;
begin
Case (GetSkillLevel('woodcutting')) of
1..14: TreeToCut := Normal;
15..29: TreeToCut := Oak;
30..99: TreeToCut := Willow;
else
Writeln('We couldnt detect what tree to cut!');
end;
Other types of Cases (Used for antiban or just random things)
This type of Case bellow is mainly used in antiban, where it has a chance of doing the case but in some cases it will just skip through, an example of this is:
SCAR Code:
procedure SomeCaseExample;
begin
case random(50) of
1: IdleTime(1000 + Random(1000), 500, 0.5);
10: HoverSkill('random', false);
25: PickUpMouse;
end;
end;
You could also do:
SCAR Code:
procedure SomeCaseExample;
begin
case random(50) of
1..5: IdleTime(1000 + Random(1000), 500, 0.5);
10..15: HoverSkill('random', false);
25..30: PickUpMouse;
35.. 50: wait(100);
end;
end;
Notice how I use Random(50);, there is also a procedure in SCAR which is called RandomRange(1,10); This would choose a random number in the range which is 1-10.
Advance Casing
Casing is particularly easy, but you could use them in more advance situations, for example. If you are trying to make a multiple tree finding procedure you would want to only make one procedure for it, instead of making a new procedure for each tree to cut. In the example bellow is how ill show you to do it, in 3 procedures/Functions.
SCAR Code:
procedure LoadTrees(WhatTree: string); // procedure that loads the trees given bellow in the next procedure.
begin
case LowerCase(WhatTree) of
'tree':
begin
Tcolour := 1327929;
Tolerance := 10;
Tree := 'Normal';
UpText := 'n Tree';
FireLV := '1';
end;
'oak':
begin
Tcolour := 7386279;
Tolerance := 10;
Tree := 'Oak';
UpText := 'n Oak';
FireLV := '15';
end;
'willow':
begin
Tcolour := 9422778;
Tolerance := 10;
Tree := 'Willow';
UpText := 'n Willow';
FireLV := '30';
end;
'all':
begin
Tcolour := 1327929;
Tolerance := 10;
Tree := 'All';
UpText := 'hop';
end;
end;
end;
procedure DecidePlayer; //This procedure decides on what tree the script will load depending on WC level.
begin
case (Players[CurrentPlayer].Integers[0]) of
0..14: LoadTrees('Tree');
15..29: LoadTrees('Oak');
30..99: LoadTrees('Willow');
end;
end;
function FindTree(var X, Y: Integer; Click, hover, Time: boolean): Boolean;// Finding the tree with TPA(Crappy TPA).
var
TPA: TPointArray;
ATPA: T2DPointArray;
I, H: Integer;
begin
If not LoggedIn then exit;
FindColorsSpiralTolerance(MSCX, MSCY, TPA, [U]Tcolour[/U], MSX1, MSY1, MSX2, MSY2, [U]Tolerance[/U]);
ATPA := TPAtoATPA(TPA, 55);
if Length(ATPA) = 0 then
Exit;
H := High(ATPA);
for I := 0 to H do
begin
MiddleTPAEx(ATPA[i], X, Y);
if Hover then
MMouse(X, Y, 3, 3);
Wait(50 + Random(50));
if IsUpText([U]UpText[/U]) then
begin
if (Click) then
begin
Mouse(x, y, 0, 0, true);
GetMousePos(X, Y);
Writeln('Found ' + Tree + ' Tree at X: ' + IntToStr(X) + ' Y: ' + IntToStr(Y));
end;
if FlagPresent then
if (FlagDistance > 5) then
begin
FFlag(0);
FindTree(X, y, True, True, True);
end;
Result := True;
Exit;
end;
wait(100+random(100));
end;
end;
Ahh i tried to underline the global varribles used to load the trees but it just turned out as [ u] thingo .
As you notice this is a an advance procedure above, but the casing is simple, Basicly im feeding the script my woodcut level, which then goes through the case to find the correct range and result for the case, then loads the specific tree which i can cut with my current woodcut level.
Now i have all the woodcutting variables loaded for my FindTree procedure, so now I do not need to make 3 different FindTree functions for each tree as i load the one i want to cut and use the loaded variables in the procedure as a substitute to having all of the tree variables.
This makes your script look much neater and easy to read for the person viewing your script!
Overall About Cases:
Cases really do make your script look much neater and make you have way less procedures in your script also. Cases are essential in order to obtain members unless you do not use any type of these methods in your script.
UNDER CONSTRUCTION - .