PDA

View Full Version : rotateCamera(); and randomChance();



Clarity
10-14-2014, 12:00 AM
randomChance();
This is an easier way of having a certain chance of doing a task, saving a few lines every time you want to do something like this. randomChance(2) has a 50% chance of returning true, randomChance(3) has a 33% chance of returning, randomChance(4) is 25%, et cetera.

Edit: We both misunderstood how random(2) worked, so basically this function combines if "random(2) = 0 or 1 then" to "if randomChance(2) then"

function randomChance(randomNumber: integer): boolean;
begin
if randomNumber and (random(1, randomNumber) = 1) then
result := true;
end;


Example Usage:

if randomChance(10) then writeln('You won the lottery!');


rotateCamera();
This is a function that randomly moves the camera on both axes. It is more useful in OGL scripts when trying to search for objects that may have evaded view. Utilizes randomChance(); as described above. Also useful in color scripts when having north or aerial orientation isn't required.

procedure rotateCamera;
const
KEYS_HORIZONTAL: TIntegerArray = [0, 37, 39];
KEYS_VERTICAL: TIntegerArray = [0, 38, 40];
var
pressKeyHorizontal, pressKeyVertical: integer;
begin
pressKeyHorizontal := KEYS_HORIZONTAL[random(0, 2)];
pressKeyVertical := KEYS_VERTICAL[random(0, 2)];
if randomChance(2) then
begin
if (pressKeyHorizontal > 0) then
keyDown(pressKeyHorizontal);
if (pressKeyVertical > 0) then
begin
if (pressKeyHorizontal > 0) then
wait(random(350));
keyDown(pressKeyVertical);
end;
end
else
begin
if (pressKeyVertical > 0) then
keyDown(pressKeyVertical);
if (pressKeyHorizontal > 0) then
begin
if (pressKeyVertical > 0) then
wait(random(350));
keyDown(pressKeyHorizontal);
end;
end;
if (pressKeyHorizontal > 0) or (pressKeyVertical > 0) then
wait(random(750));
if randomChance(2) then
begin
if (pressKeyHorizontal > 0) then
keyUp(pressKeyHorizontal);
if (pressKeyVertical > 0) then
begin
if (pressKeyHorizontal > 0) then
wait(random(350));
keyUp(pressKeyVertical);
end;
end
else
begin
if (pressKeyVertical > 0) then
keyUp(pressKeyVertical);
if (pressKeyHorizontal > 0) then
begin
if (pressKeyVertical > 0) then
wait(random(350));
keyUp(pressKeyHorizontal);
end;
end;
end;


Example Usage (on repeated loop):
http://i.imgur.com/f5ATE5S.gif
A single usage is one of those camera nudges.

Credits to my buddy Obscurity.

Incurable
10-14-2014, 01:13 AM
Interesting, thanks for sharing.

I don't understand how randomChance works, could you explain it for me?

Olly
10-14-2014, 01:28 AM
RandomChance is the same as RBool from SRL-5 !

The Mayor
10-14-2014, 01:29 AM
Couldn't you just go:


function randomChance(randomNumber: integer): boolean;
begin
result := random(randomNumber) = 0;
end

slacky
10-14-2014, 01:38 AM
Interesting, thanks for sharing.

I don't understand how randomChance works, could you explain it for me?
It simply generates a random number in the range of [1..randomNumber] where randomNumber is the given number then it checks the generated number is 1.
Well first if checks if the given number (read: RandomNumber) is 0, if that's the case then it just skips and returns False.

Basically:
So: Result := Random(somenumber) = 0;

It can all be pressed in to one line:

function RandomChance(randNum: integer): boolean;
begin
Result := randNum and (Random(1, randNum) = 1);
end;

(could be shortened even more if you start at 0 and not at 1)

Obscurity
10-14-2014, 01:41 AM
I wanted it to be a random chance from 1 to x, The Mayor. Using yours, randomChance(2) would be a 33% chance, not a 50%. :P.

I like yours, Slacky. Thanks.

EDIT: Clarity, the version I sent you didn't have chance. Chance is not the chance that nothing will happen - it's the chance that it'll press a vertical key before a horizontal key - if it decides to press any at all. The 0 in the arrays are the 1 in 3 chance that it won't press anything. So, technically, it has a ~10% chance to press nothing at all.

Clarity
10-14-2014, 01:42 AM
Interesting, thanks for sharing.

I don't understand how randomChance works, could you explain it for me?

randomChance(2) means a 1 in 2 (50%) chance. If you have a procedure:

myProcedure;


And you want a 50% chance of the procedure happening in your script, you would write:

if randomChance(2) then myProcedure;


And so on:

if randomChance(2) then myProcedure; //1 in 2 (50%) chance
if randomChance(3) then myProcedure; //1 in 3 (33.33%) chance
if randomChance(4) then myProcedure; //1 in 4 (25%) chance
if randomChance(5) then myProcedure; //1 in 5 (20%) chance
if randomChance(6) then myProcedure; //1 in 6 (16.67%) chance
if randomChance(7) then myProcedure; //1 in 7 (14.3%) chance



RandomChance is the same as RBool from SRL-5 !

Honestly didn't know that was there, I've never looked at SRL-5, no copying intended! If that's the case, can I add it to SRL-6? :)

E: Ninja'd by Obscurity.


EDIT: Clarity, the version I sent you didn't have chance. Chance is not the chance that nothing will happen - it's the chance that it'll press a vertical key before a horizontal key - if it decides to press any at all.

Whoops. I goofed :p

slacky
10-14-2014, 01:47 AM
Honestly didn't know that was there, I've never looked at SRL-5, no copying intended! If that's the case, can I add it to SRL-6? :)
Is there really a need for it.. We usually just do if Random(x) = 0 then... or for your example if Random(1,x) = 1 then. So I really doubt that it would be a needed addition.

The Mayor
10-14-2014, 01:52 AM
The Mayor. Using yours, randomChance(2) would be a 33% chance, not a 50%. :P

random(2) will return either 0 or 1, meaning it has a 50% chance of returning 0 and resulting true :)

Obscurity
10-14-2014, 01:57 AM
Oh? I thought random(2) could return 0, 1, or 2, being 3 possibilities. Thanks for pointing that out.

Clarity
10-14-2014, 01:57 AM
Is there really a need for it.. We usually just do if Random(x) = 0 then... or for your example if Random(1,x) = 1 then. So I really doubt that it would be a needed addition.
If people are very used to that way, then sure, it's redundant. I personally like the perceived convenience of this version, but if nobody cares then never mind.
E: Looking at the above discussion it seems like it's not necessary anyway. Seems like I also misunderstood how random(2) works. I'll get back to my corner and work on includes, haha.

Incurable
10-14-2014, 02:17 AM
It simply generates a random number in the range of [1..randomNumber] where randomNumber is the given number then it checks the generated number is 1.)

Ahhh, I was unaware that random(int, int) could be used as a replacement for randomRange. That explains it.

Thanks guys!