📄 globallists.pas
字号:
unit GlobalLists;
(*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*
GlobalLists
2004/04 ChrstphrR - chrstphrr@tubbybears.net
--
Overview:
--
Lists used globally throughout the source, such as the Summon???Lists will
be based on classes defined here
--
Revisions:
--
0.2v initial write
TSummonMobList class created on base TRandomList class.
*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*)
interface
uses
Classes,
List32;
(*=============================================================================*
TRandList
2004/04/07 ChrstphrR
--
Overview:
--
Base class from which the Summon???Lists will be derived.
Holds a list that allows for random selection of Integers[] in the list.
Since this is a read-only list from the /database folder, no Save* routines
have been made.
Provides:
TotalWeighting for Weighted Probability
RandChoice selection routine
Distribution toggle between Weighted and Uniform
Features:
= Toggle between Weighted/Uniform Probability.
Load data files as class method
= Add items into the list.
= Remove items into the list.
= Safe Clear of items AND objects;
= Modify Items / Weights in the list.
(The ID's loaded... are part of Integers[] and for derived classes, will
be renamed for clarity and relevance to -that- list.)
--
Revisions:
--
v0.2 [ChrstphrR] - Initial writing of this class.
*=============================================================================*)
type
TDistType = ( distWeighted, distUniform );
Type TRandList = class(TIntList32)
private
fDistribution : TDistType;
fMobDB : TIntList32; // internal pointer to MobDB list.
fTotalWeight : Cardinal; // counter for Weighted Probability.
protected
Procedure SetDistribution(
const
Value : TDistType
);
Procedure SetMobDB(
const
Value : TIntList32
);
Function GetWeight(
Index : Integer
) : Cardinal;
Procedure SetWeight(
Index : Integer;
Value : Cardinal
);
Function GetTotalWeight : Cardinal;
public
Constructor Create(
FileName : String;
DistType : TDistType = distWeighted
); OverLoad;
Destructor Destroy; OverRide;
//CRW Compiler warning stating that this overrides the old method
// Yes, it does - we're storing Cardinals instead of TObjects :)
{$WARNINGS OFF}
Function AddObject(
const
Item : Cardinal;
Weight : Cardinal
) : Integer; OverLoad;
{$WARNINGS ON}
procedure Clear;
procedure Delete(Index: Integer);
procedure LoadFromFile(
const
FileName : String
); OverRide;
procedure LoadFromStream(
Stream : TStream
); OverRide;
function RandomChoice : Cardinal;
property Distribution : TDistType
read fDistribution
write SetDistribution
default distWeighted; // Assuming Weighted Probability is wanted
property MobDB : TIntList32
read fMobDB
write SetMobDB
default NIL; //Safe default form
property Weights[Index : Integer] : Cardinal
read GetWeight
write SetWeight;
property TotalWeight : Cardinal
read GetTotalWeight;
End;(* <TClassName> ==========================================================*)
(*=============================================================================*
TSummonMobList
2004/04/09 ChrstphrR
--
Overview:
--
Implemented for SummonMobList (thus the name!)
Reads a different format in the Create() routine than the ancestor TRandList.
Optional, overloaded constructor that lets you set up all the Essentials in
one call.
FileName is the Path + Filename that contains a List of the format(s)
MobJNAME,DBweight,MVPweight,MobID
MobJNAME,DBweight,,MobID
,DBweight,,MobID
( Current MOBJNAME,DBWeight will not work, but won't corrupt the list)
* At Colus' request, I'm keeping my internal format, but reading the
a modified BUT compatible data file. This manner promotes internal efficiency
with memory, and algorithm, AND maintains readability of the data files, which
Colus desired for ease of use for the DB devs, and users adventurous enough to
alter the data for their setup.
--
Revisions:
--
v0.2 [ChrstphrR] - Initial writing of this class.
N.B. - When this List is created, and USED in Fusion, the
SummonMobListMVP load code must be changed.
*=============================================================================*)
Type TSummonMobList = class(TRandList)
public
Constructor Create(
FileName : String;
DistType : TDistType = distWeighted
); OverLoad;
Destructor Destroy; OverRide;
procedure LoadFromFile(
const
FileName : String
); OverRide;
procedure LoadFromStream(
Stream : TStream
); OverRide;
End;(* <TClassName> ==========================================================*)
implementation
uses
SysUtils;
(*-----------------------------------------------------------------------------*
TRandList.Create()
start: 2004/04/07 ChrstphrR
Optional, overloaded constructor that lets you set up all the Essentials in
one call.
FileName is the Path + Filename that contains a List of the format
MobID,DBWeight
*-----------------------------------------------------------------------------*)
Constructor TRandList.Create(
FileName : String;
DistType : TDistType
);
Begin
inherited Create;
// Always call ancestor's routines first in Create
Distribution := DistType;
LoadFromFile( FileName );
End;(* TRandList.Create()
*-----------------------------------------------------------------------------*)
(*-----------------------------------------------------------------------------*
TRandList.Destroy
Clean up any messes we made inside Destroy - this is our last chance! :)
*-----------------------------------------------------------------------------*)
Destructor TRandList.Destroy;
Begin
// Always call ancestor's routines after you clean up
// objects you created as part of this class
Clear;
//CRW Clear is safe for objexts in this class -- this is NOT always so
// such as in TStrings / TStringList derived classes.
inherited;
End;(* T.Destroy
*-----------------------------------------------------------------------------*)
(*-----------------------------------------------------------------------------*
Property Method that safely writes Distribution value.
Input value is NEVER modified.
*-----------------------------------------------------------------------------*)
Procedure TRandList.SetDistribution(
const
Value : TDistType
);
Begin
if (Value <> fDistribution) then
fDistribution := Value;
End;(* Proc TRandList.SetDistribution()
*-----------------------------------------------------------------------------*)
(*-----------------------------------------------------------------------------*
*-----------------------------------------------------------------------------*)
Procedure TRandList.SetMobDB(
const
Value : TIntList32
);
Begin
if (Value <> fMobDB) then
fMobDB := Value;
End;(* Proc TRandList.SetMobDB()
*-----------------------------------------------------------------------------*)
(*-----------------------------------------------------------------------------*
Returns appropriate total weight, based on fDistribution type.
*-----------------------------------------------------------------------------*)
Function TRandList.GetTotalWeight : Cardinal;
Begin
case fDistribution of
distWeighted :
Result := fTotalWeight;
distUniform :
Result := Count;
else
Assert(
fDistribution in [distWeighted, distUniform],
'TRandList.GetTotalWeight -- undefined Distribution Type'
);
Result := 0;
end;//case
End;(* Func TRandList.GetTotalWeight
*-----------------------------------------------------------------------------*)
(*-----------------------------------------------------------------------------*
*-----------------------------------------------------------------------------*)
Function TRandList.GetWeight(
Index : Integer
) : Cardinal;
Begin
Result := Cardinal( Objects[Index] );
//Compiler warns this as an unsafe Cast ... and...
// it IS, if not controlled -- but this is one of the routines that controls
// this cast to/from Cardinal/TObject to ensure safety.
End;(* Func TRandList.GetWeight()
*-----------------------------------------------------------------------------*)
(*-----------------------------------------------------------------------------*
Adjust Weighting at Weight[Index], and adjusts internal totalweight for the
Weighted distribuution.
*-----------------------------------------------------------------------------*)
Procedure TRandList.SetWeight(
Index : Integer;
Value : Cardinal
);
Begin
if (Index >= 0) AND (Index < Count) AND (Weights[Index] <> Value) then
begin
// Weight will increase or decrease depending on the end difference
fTotalWeight := fTotalWeight + ( Value - Weights[Index] );
Objects[Index] := TObject( Value );
//Compiler warns this as an unsafe Cast ... and...
// it IS, if not controlled -- but this is one of the routines that controls
// this cast to/from Cardinal/TObject to ensure safety.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -