SRL Archive - SRL Archives

A few things I made


Rich

2009-09-21 00:38:35

{*******************************************************************************
function PercentProc(Proc : procedure; Percent : Integer) : Boolean;
By: R1ch
Description: Has a Percent/100 chance of calling a procedure (Proc)
Example: PercentProc(@Write, 60);
*******************************************************************************}
function PercentProc(Proc : procedure; Percent : Integer) : Boolean;
begin
if not InRange(Percent, 1, 99) then
begin
srl_Warn('PercentProc', 'Percent should be within 1 and 99 ', warn_AllVersions);
Exit;
end;
Result:= Random(100) <= Percent;
if Result then
Proc();
end;

{*******************************************************************************
function PercentBool(Func : function : Boolean; Percent : Integer) : Boolean;
By: R1ch
Description: Has a Percent/100 chance of calling a Boolean Function (Func)
*******************************************************************************}
function PercentBool(Func : function : Boolean; Percent : Integer) : Boolean;
begin
if not InRange(Percent, 1, 99) then
begin
srl_Warn('PercentBool', 'Percent should be within 1 and 99 ', warn_AllVersions);
Exit;
end;
Result:= Random(100) <= Percent;
if Result then
Func();
end;

{*******************************************************************************
function PercentStr(Func : function : Boolean; Percent : Integer) : Boolean;
By: R1ch
Description: Has a Percent/100 chance of calling a String Function (Func)
*******************************************************************************}
function PercentStr(Func : function : Boolean; Percent : Integer) : Boolean;
begin
if not InRange(Percent, 1, 99) then
begin
srl_Warn('PercentStr', 'Percent should be within 1 and 99 ', warn_AllVersions);
Exit;
end;
Result:= Random(100) <= Percent;
if Result then
Func();
end;

{*******************************************************************************
function PercentInt(Func : function : Boolean; Percent : Integer) : Boolean;
By: R1ch
Description: Has a Percent/100 chance of calling an Integer Function (Func)
*******************************************************************************}
function PercentInt(Func : function : Boolean; Percent : Integer) : Boolean;
begin
if not InRange(Percent, 1, 99) then
begin
srl_Warn('PercentInt', 'Percent should be within 1 and 99 ', warn_AllVersions);
Exit;
end;
Result:= Random(100) <= Percent;
if Result then
Func();
end;

{*******************************************************************************
function PercentStrEx(Func : function : string; Percent : Integer) : string;
By: R1ch
Description: Has a Percent/100 chance of calling a String Function (Func), and
Result is Result of Func.
*******************************************************************************}
function PercentStrEx(Func : function : string; Percent : Integer) : string;
begin
if not InRange(Percent, 1, 99) then
begin
srl_Warn('PercentStr', 'Percent should be within 1 and 99 ', warn_AllVersions);
Exit;
end;
if Random(100) <= Percent then
Result:= Func();
end;

{*******************************************************************************
function PercentIntEx(Func : function : string; Percent : Integer) : Integer;
By: R1ch
Description: Has a Percent/100 chance of calling a String Function (Func), and
Result is Result of Func.
*******************************************************************************}
function PercentIntEx(Func : function : string; Percent : Integer) : Integer;
begin
if not InRange(Percent, 1, 99) then
begin
srl_Warn('PercentStr', 'Percent should be within 1 and 99 ', warn_AllVersions);
Exit;
end;
if Random(100) <= Percent then
Result:= Func();
end;

{*******************************************************************************
procedure function PercentChance(Percent : Integer) : Boolean;
By: R1ch
Description: Has a Percent/100 chance of returning True
*******************************************************************************}
function PercentChance(Percent : Integer) : Boolean;
begin
Result:= Random(100) <= Percent;
end;

{*******************************************************************************
procedure MakeNorth;
By: R1ch
Description: Makes the compass North, with a 70% chance of ClickNorth, else MakeCompass
*******************************************************************************}
procedure MakeNorth;
begin
if not PercentProc(@ClickNorth, 70) then
MakeCompass('N');
end;

Made them whilst I was bored. May be used, may be not, doesn't bother me :p

Comments?
Richard.

ShowerThoughts

2009-09-21 00:41:59

Some of those must be added, they are fine some not :p.

Rich

2009-09-21 00:48:55

From: Hermen
Some of those must be added, they are fine some not :p.


Thanks :) Which ones aren't fine?

Anyway, an example of how PercentProc could be used in a script:

program New;
{.include SRL/SRL.scar}

procedure Write;
begin
WriteLn('Hello!');
end;

procedure Write2;
begin
WriteLn('Bye!');
end;

begin
SetupSRL;
if not PercentProc(@Write, 50) then
Write2;
end.

A feeble example, I know, but it gets the job done.

Richard.

Main

2009-09-21 02:17:06

Bored eh?
Do you mind a make function/procedure that tracks which direction your character has moved? (E.G Moved without scar, like being attacked from a distance) then walk towards the original direction :)?

marpis

2009-09-21 09:19:35

From: R1ch
Thanks :) Which ones aren't fine?

Anyway, an example of how PercentProc could be used in a script:

program New;
{.include SRL/SRL.scar}

procedure Write;
begin
WriteLn('Hello!');
end;

procedure Write2;
begin
WriteLn('Bye!');
end;

begin
SetupSRL;
if not PercentProc(@Write, 50) then
Write2;
end.

A feeble example, I know, but it gets the job done.

Richard.


case Random(x) of ?
I don't know where these could be used, can you give us an example that would really be useful?

Rich

2009-09-21 10:30:26

From: Main_Ftw
Bored eh?
Do you mind a make function/procedure that tracks which direction your character has moved? (E.G Moved without scar, like being attacked from a distance) then walk towards the original direction :)?


I'll give that a shot later. I've already got an idea in my head how I'll do it.

From: marpis
case Random(x) of ?
I don't know where these could be used, can you give us an example that would really be useful?


I don't know where they could be used either :p But I'm sure someone around here could find a use for them.

Richard.

mixster

2009-09-21 21:11:58

Result := Random(100) < percent;
if Result then
Proc();

Makes more sense than your most peculiar case that made me cringe.

With your function related ones, they all have to be boolean returning functions despite having string, integer etc in the name. Also, if it is a function, then I would say it's likely people want the result and so it has less use in that way.

For the procedurey stuff, it has a use as an antiban, but it is not much and doesn't always work with peoples stuffs.

Floor66

2009-09-21 21:18:28

Something like 'PercentBool' would be handy… Without the function stuff.

Rich

2009-09-21 21:19:44

From: mixster
Result := Random(100) < percent;
if Result then
Proc();

Makes more sense than your most peculiar case that made me cringe.

With your function related ones, they all have to be boolean returning functions despite having string, integer etc in the name. Also, if it is a function, then I would say it's likely people want the result and so it has less use in that way.

For the procedurey stuff, it has a use as an antiban, but it is not much and doesn't always work with peoples stuffs.


Cheers, I'll change the case thingy now.

I've got a way to make the functions returns the matching results of the function called (tell me if you don't understand) but I'm going to do a bit of testing first.

EDIT:

From: Floor66
Something like 'PercentBool' would be handy… Without the function stuff.


What do you mean Without the function stuff?

EDIT2: Found a way. Will edit first post in a sec.

EDIT3: New ones added.

Richard.

Floor66

2009-09-21 21:23:22

Umm… Well… You put PercentBool(Chance : Integer): Boolean;
And give a percentage chance that it sets the boolean true.

Rich

2009-09-21 21:34:10

From: Floor66
Umm… Well… You put PercentBool(Chance : Integer): Boolean;
And give a percentage chance that it sets the boolean true.


Ah, I get you now. I'll make it in a sec.

EDIT:

{*******************************************************************************
procedure function PercentChance(Percent : Integer) : Boolean;
By: R1ch
Description: Has a Percent/100 chance of returning True
*******************************************************************************}
function PercentChance(Percent : Integer) : Boolean;
begin
Result:= Random(100) <= Percent;
end;