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.