Hosted by
 |
with Ada.Exceptions; use Ada.Exceptions;
package body Choices is
function Create
(Regular_Size : in Natural := Default_Regular_Size;
Special_Size : in Natural := Default_Special_Size)
return Choice_Access
is
Result : Choice_Access := new Choice(Regular_Size, Special_Size);
begin
Reset(Result.Gen);
return Result;
end Create;
procedure Add_Regular
(This : access Choice;
Add : in Glyph_Access) is
begin
This.Regular_Count := This.Regular_Count + 1;
This.Regular(This.Regular_Count) := Add;
end Add_Regular;
function Get_Regular
(This : access Choice;
Index : in Positive)
return Glyph_Access is
begin
return This.Regular(Index);
end Get_Regular;
procedure Add_Special
(This : access Choice;
Add : in Glyph_Access) is
begin
This.Special_Count := This.Special_Count + 1;
This.Special(This.Special_Count) := Add;
end Add_Special;
function Get_Special
(This : access Choice;
Index : in Positive)
return Glyph_Access is
begin
return This.Special(Index);
end Get_Special;
function Choose
(From : in Glyph_Array;
Count : in Positive;
Gen : in Generator)
return Glyph_Access is
begin
return From(1 + Natural(Real'Floor(Real(Random(Gen)) * Real(Count))));
end Choose;
function Choose_Regular
(This : access Choice)
return Glyph_Access is
begin
return Choose(This.Regular, This.Regular_Count, This.Gen);
end Choose_Regular;
function Choose_Special
(This : access Choice)
return Glyph_Access is
begin
return Choose(This.Special, This.Special_Count, This.Gen);
end Choose_Special;
function Choose
(This : access Choice;
Special : in Percent)
return Glyph_Access is
begin
if This.Regular_Count > 0 then
if This.Special_Count > 0 then
if Real(Random(This.Gen)) < Real(Special) / 100.0
then return Choose_Special(This);
else return Choose_Regular(This);
end if;
else
return Choose_Regular(This);
end if;
elsif This.Special_Count > 0 then
return Choose_Special(This);
else
Raise_Exception(No_Glyphs_To_Choose_From'Identity);
end if;
end Choose;
end Choices;
|