PDA

View Full Version : How to Use Cases!



Bionicle
12-29-2009, 08:35 PM
Introduction

Well I got really bored today, and so I wrote this beginner tutorial, simply because I couldn't find many on this subject. Case's are extremely useful when scripting, they can make your script look neater and more professional, and they open up a few opportunities for things such as AntiBan.

What is a Case?

A case is in this format:
begin
Case {Integer, string or procedure} of
{Integer or string}: {what you want it to do}
{Second integer or string}: {what you want it to do}
{Third integer or string}: {what you want it to do}
//etc.
end;
end;

Integer Cases

Integer Cases are case statements that use integers for the different cases, as opposed to strings. These are more common, and you will see them in nearly every script in the AntiBan section. Here is an example:
program CaseExample;

var
i: Integer;

Procedure TheCase;
begin
Case i of
0: WriteLn('Case 1');
1: WriteLn('Case 2');
2: WriteLn('Case 3');
3: WriteLn('Case 4');
end;
end;

Procedure WriteTheCase;
begin
for i := 0 to 3 do
begin
TheCase;
Wait(300);
end;
end;

begin
WriteTheCase;
end.

If you don't know how the WriteTheCase procedure works, refer to my tutorial on loops. (http://www.villavu.com/forum/showthread.php?t=50774)
Now, copy this and pase it into scar. If you hit play, it will come out with this in the debug box:

Successfully compiled (78 ms)
Case 1
Case 2
Case 3
Case 4
Successfully executed

String Cases

String Cases are case statements that use strings for the different cases, as opposed to integers. These aren't as common, but you will see them from time to time. Here is an example of a simple one:
program CaseExample2;

const
WhichOne = 'Hello'; //Hello, World

procedure TheCase;
begin
Case WhichOne of
'Hello': begin
MoveMouse(179, 285);
Writeln('Hello Mouse is at 179, 285');
end;
'World': begin
MoveMouse(791, 760);
Writeln('World Mouse is at 791, 760');
end;
end;
end;

begin
TheCase;
end.

Cases With Procedures

These are more widly used, they are used with procedures that return something. For example, GetSkillLevel:
program CaseExample3;
{.include SRL/srl.scar} //we need to put this in this time because we're using an SRL procedure; GetSkillLevel

procedure GetTheLevel;
begin
case (GetSkillLevel('Mining')) of
1..14: Writeln('You can mine Clay, Copper and Tin.');
15..19: Writeln('You can mine Clay, Copper, Tin and Iron.');
20..29: Writeln('You can mine Clay, Copper, Tin, Iron and Silver.');
30..39: Writeln('You can mine Clay, Copper, Tin, Iron, Silver and Coal.');
40..54: Writeln('You can mine Clay, Copper, Tin, Iron, Silver, Coal and Gold.');
55..69: Writeln('You can mine Clay, Copper, Tin, Iron, Silver, Coal, Gold and Mithril.');
70..84: Writeln('You can mine Clay, Copper, Tin, Iron, Silver, Coal, Gold, Mithril and Adamantite.');
85..99: Writeln('You can mine Clay, Copper, Tin, Iron, Silver, Coal, Gold, Mithril, Adamantite and Rune.');
end;
end;

begin
GetTheLevel;
end.

This will get your current mining level, and then return the number. The number of your level will then trigger the case of that number, and use the case.

Random Cases

Random cases are most often found in AntiBan, they randomly choose a number between 0 and the number specified, then does that case. For example:
program CaseExample4;

procedure RandomMoveMouse;
begin
Case (Random(10)) of
0: MoveMouse(0, 0);
1: MoveMouse(10, 10);
2: MoveMouse(20, 20);
3: MoveMouse(30, 30);
4: MoveMouse(40, 40);
5: MoveMouse(50, 50);
6: MoveMouse(60, 60);
7: MoveMouse(70, 70);
8: MoveMouse(80, 80);
9: MoveMouse(90, 90);
end;
end;

begin
RandomMoveMouse;
end.

This will randomly choose one of the coordinates specified in the case, and move the mouse to it. Copy it into SCAR, you can play around with it, and then try making your own random case.

Calling Specific Cases

These are procedures made so that you call the integer of the case you want along with the procedure. Here is an example with writing random things:
procedure WriteRandomStuff(WhichToWrite: Integer); //So when you call it, you call it with the case you want to use
begin
case WhichToWrite of
1: Writeln('Write this!'); //if you call it WhichToWrite(1); then you will get this.
2: Writeln('We"re writing!'); //if you call it WhichToWrite(2); then you will get this.
3: Writeln('Write stuff!'); //etc...
4: Writeln('Aaaand writing!');
5: Writeln('This is fun.');
end;
end;

Conclusion

In conclusion, cases are very widely used, and if used properly, can be very beneficial. Any questions, comments, or corrections, don't hesitate to post below.

Awkwardsaw
12-29-2009, 08:45 PM
nice :D

you forgot the

procedure areyouanidiot(number1through10 : integer);
begin
case number1through10 of
1..10 : writeln('your not an idiot!');
else
writeln('your an idiot!');
end;
end;

;)

Bionicle
12-29-2009, 08:59 PM
nice :D

you forgot the

procedure areyouanidiot(number1through10 : integer);
begin
case number1through10 of
1..10 : writeln('your not an idiot!');
else
writeln('your an idiot!');
end;
end;

;)

Took me a good five minutes to figure out if you were trying to diss me or not. But i get it now haha, ill add that in :p thanks :).

twobac
03-20-2010, 09:36 PM
Question for you.

Trying to find which fish i caught from the chat box.


program New;
{.include srl/srl.scar}
var n: integer;
FishCaught, Name, Chat: string;

procedure fish;
begin
if FindChatBoxText('ou catch some', 8, clBlack) then
begin
Name := GetChatBoxText(8, clBlack);
writeln(Name);
end;

case Name of
'shrimp':FishCaught := 'Gots some shrimps on the barby';
'anchovie':FishCaught := 'Salty anchovie';
end;

writeln(FishCaught + ' Cool');
end;
begin
fish
end.

Not working, as I can only get the case function to work where I enter the whole of the text string. I dont want to have in the case funtion 'you catch some Shrimps.', etc

Any Ideas?

HyperSecret
03-20-2010, 09:40 PM
Question for you.

Trying to find which fish i caught from the chat box.


program New;
{.include srl/srl.scar}
var n: integer;
FishCaught, Name, Chat: string;

procedure fish;
begin
if FindChatBoxText('ou catch some', 8, clBlack) then
begin
Name := GetChatBoxText(8, clBlack);
writeln(Name);
end;

case Name of
'shrimp':FishCaught := 'Gots some shrimps on the barby';
'anchovie':FishCaught := 'Salty anchovie';
end;

writeln(FishCaught + ' Cool');
end;
begin
fish
end.

Not working, as I can only get the case function to work where I enter the whole of the text string. I dont want to have in the case funtion 'you catch some Shrimps.', etc

Any Ideas?

You would have to do some string manipulation and shorten the string and get it down to just the fish name.

Nice tutorial.

Bionicle
03-20-2010, 09:41 PM
Question for you.

Trying to find which fish i caught from the chat box.


program New;
{.include srl/srl.scar}
var n: integer;
FishCaught, Name, Chat: string;

procedure fish;
begin
if FindChatBoxText('ou catch some', 8, clBlack) then
begin
Name := GetChatBoxText(8, clBlack);
writeln(Name);
end;

case Name of
'shrimp':FishCaught := 'Gots some shrimps on the barby';
'anchovie':FishCaught := 'Salty anchovie';
end;

writeln(FishCaught + ' Cool');
end;
begin
fish
end.

Not working, as I can only get the case function to work where I enter the whole of the text string. I dont want to have in the case funtion 'you catch some Shrimps.', etc

Any Ideas?

Well, i don't have an explanation for you, but your coding is perfectly fine..you just have to mess around with the strings a bit.
For example, in my one script i was making my own banking procedure, 'eposit-x' wasn't working, but 'x' did. Just try mixing it around a bit.

eg: 'imp', 'chovi'
'some s', 'some a'

twobac
03-20-2010, 09:57 PM
maybe i am going about htis the wrong way. different example

I want to find the skill I have just leveled

The chat box shows the text
"You've just advanced a Fishing Level! You have reached level 7."

I can find the text line containing this text by using a chat function and I can assign the whole line to a variable.
'ou''ve', 'ust', 'dvanced a', SKILL 'evel' (only using this 'text')
MyVariable = "You've just advanced a Fishing Level! You have reached level 7."
so WriteLn(MyVariable) prints the whole line in debug.
I want to use "case" to interogate the variable MyVariable with a list of SKILLS to find out which skill has been leveled.

Should I be doing something else or a different function like a IfTextContainsX

Sorry just started to script.

Thanks

HyperSecret
03-20-2010, 10:12 PM
cases check to see if certain strings match, not if certain strings are contained in a string. So you would have to cut and manipulate the string to get it to be what you want.

twobac
03-20-2010, 10:18 PM
ok great thanks,

where can I find info on text control functions, I guess I would need something like the excel Mid() function and others? I have looked at the functions in text.scar, but do not see anything there.

Naum
03-20-2010, 10:20 PM
Check my sig ;)
http://villavu.com/forum/showthread.php?t=38799

Should be Pos()..

twobac
03-20-2010, 10:35 PM
Thanks so much for all the help, will look at the other thread after I play with srl_Explode (pressed CTRL + Space and got a list of functions!).
:)
Ed: OMG all I needed was Between!!!!!!!


Thanks a load Naum :)

Wanted
03-20-2010, 10:52 PM
Nice, but you should explain the

'a', 'b', 'c':

1, 2, 4, 7:

IIRC it works with all primitives... useless for boolean but useful for variants? That's only if you wanted to go into more depth though.

BraK
06-08-2010, 05:47 PM
haha i so never really got the whole case thing or arrays but this broke it down a bit better for me. old dogs can learn new tricks.

Rewrote a procedure in my personal fletcher for random choosing with this tut.

OLD CODE



procedure Randomfletch;
var
moon, c:integer;
Begin
Begin
Moon:=random(3);
end;
if moon=1
then
Begin
c:=54
end;
if moon=2
then
begin
c:=65
end;
if moon=0
then
begin
c:=45
end;
typesend(IntToStr(c));
end;


NEW CODE



procedure Randomfletch;
var
c:integer;
Begin
case (random(3)) of
0:c:=54;
1:c:=65;
2:c:=45;
end;
typesend(IntToStr(c));
end;


PS: i know my code looks like crap but remember that this is my first time scripting in 2 years+. Hit up Iraq twice. Gotta love the cash paid for 2 cars now i'm on my third.

lordsaturn
06-08-2010, 07:08 PM
Two things..


i := Random(8);
case i of
0..2: //standards
begin
DoStuff;
end;
3: DoSomething;
else
begin
Writeln('i is not 0, 1, or 2');
end;
end;

Great tut :D

i luffs yeww
06-09-2010, 12:19 PM
haha i so never really got the whole case thing or arrays but this broke it down a bit better for me. old dogs can learn new tricks.

Rewrote a procedure in my personal fletcher for random choosing with this tut.

OLD CODE



procedure Randomfletch;
var
moon, c:integer;
Begin
Begin
Moon:=random(3);
end;
if moon=1
then
Begin
c:=54
end;
if moon=2
then
begin
c:=65
end;
if moon=0
then
begin
c:=45
end;
typesend(IntToStr(c));
end;


NEW CODE



procedure Randomfletch;
var
c:integer;
Begin
case (random(3)) of
0:c:=54;
1:c:=65;
2:c:=45;
end;
typesend(IntToStr(c));
end;


PS: i know my code looks like crap but remember that this is my first time scripting in 2 years+. Hit up Iraq twice. Gotta love the cash paid for 2 cars now i'm on my third.

You went to Iraq? :o

Also, that could be changed to..


procedure RandomFletch;
begin
TypeSend(IntToStr(27 + Random(72)));
end;


:)

BraK
06-09-2010, 04:24 PM
You went to Iraq? :o

Also, that could be changed to..


procedure RandomFletch;
begin
TypeSend(IntToStr(27 + Random(72)));
end;


:)

Yea just got back from my second time over there this past winter.

I put it using 45, 54, 65 because it's what I normally press without looking, when I do it manually. The goal is to make it do what i normally do manually for mine. I video recorded myself doing it about 50 times before I started making this script.

anonymity
06-09-2010, 08:12 PM
Nice Tut, it's short simple and to the point.


I video recorded myself doing it about 50 times before I started making this script.
^ FTW - a lot of times I do things without thinking and completely forget to add it to my scripts.

Bebe
06-09-2010, 08:30 PM
*switch statement

Wizzup?
06-09-2010, 08:58 PM
*switch statement

I don't think it is a switch statement, now is it? This is pascal, not C. A switch statement works fundamentally different.

Ogre
06-09-2010, 09:10 PM
I put it using 45, 54, 65 because it's what I normally press without looking, when I do it manually. The goal is to make it do what i normally do manually for mine. I video recorded myself doing it about 50 times before I started making this script.

Unless you only want those three numbers, you may like using this

It will do things like 34, 33, 343, 345, 45, 454, etc
procedure RandomFletch;
var
a, b, i: integer;
begin
a:= 3 + Random(5);
b:= a;
if (RBool) then
begin
for i:= 1 to 1 + Random(2) do
b:= b + a * strtoint(floattostr(pow(10, i)));
end else
begin
if (RBool) then
begin
for i:= 1 to Random(2) + 1 do
b:= b + (a + (-i + 2)) * strtoint(floattostr(pow(10, i)));
end else
begin
for i:= 1 to 2 do
b:= b + (a - i) * strtoint(floattostr(pow(10, i)));
end;
end;
TypeSend(IntToStr(b));
end;

BraK
06-10-2010, 03:32 PM
Unless you only want those three numbers, you may like using this

It will do things like 34, 33, 343, 345, 45, 454, etc
procedure RandomFletch;
var
a, b, i: integer;
begin
a:= 3 + Random(5);
b:= a;
if (RBool) then
begin
for i:= 1 to 1 + Random(2) do
b:= b + a * strtoint(floattostr(pow(10, i)));
end else
begin
if (RBool) then
begin
for i:= 1 to Random(2) + 1 do
b:= b + (a + (-i + 2)) * strtoint(floattostr(pow(10, i)));
end else
begin
for i:= 1 to 2 do
b:= b + (a - i) * strtoint(floattostr(pow(10, i)));
end;
end;
TypeSend(IntToStr(b));
end;

That's an interesting approach i haven't really used Floats before but i'll look into it. But that is a huge peice of script you got there. Thank's for the info. :)