Changeset 344


Ignore:
Timestamp:
Dec 23, 2024, 8:16:05 AM (6 hours ago)
Author:
chronos
Message:
  • Modified: Improved implementation of TItemList class to be generic class to avoid explicit typecasting.
Location:
trunk
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Building.pas

    r317 r344  
    2727  { TBuildingKinds }
    2828
    29   TBuildingKinds = class(TItemList)
    30     class function GetItemClass: TItemClass; override;
    31     function FindBySpecialType(SpecialType: TBuildingSpecialType): TItem;
     29  TBuildingKinds = class(TItemList<TBuildingKind>)
     30    function FindBySpecialType(SpecialType: TBuildingSpecialType): TBuildingKind;
    3231  end;
    3332
     
    4645  { TBuildings }
    4746
    48   TBuildings = class(TItemList)
     47  TBuildings = class(TItemList<TBuilding>)
    4948    Game: TObject; // TGame;
    50     class function GetItemClass: TItemClass; override;
    5149  end;
    5250
     
    6765uses
    6866  Map;
    69 
    70 { TBuildings }
    71 
    72 class function TBuildings.GetItemClass: TItemClass;
    73 begin
    74   Result := TBuilding;
    75 end;
    7667
    7768{ TBuilding }
     
    148139{ TBuildingKinds }
    149140
    150 class function TBuildingKinds.GetItemClass: TItemClass;
    151 begin
    152   Result := TBuildingKind;
    153 end;
    154 
    155141function TBuildingKinds.FindBySpecialType(SpecialType: TBuildingSpecialType
    156   ): TItem;
     142  ): TBuildingKind;
    157143var
    158144  I: Integer;
    159145begin
    160146  I := 0;
    161   while (I < Count) and (TBuildingKind(Items[I]).SpecialType <> SpecialType) do Inc(I);
     147  while (I < Count) and (Items[I].SpecialType <> SpecialType) do Inc(I);
    162148  if I < Count then Result := Items[I]
    163149    else Result := nil;
  • trunk/Core.pas

    r339 r344  
    606606  FormMain.FormClient.Client := nil;
    607607  for I := 0 to Game.Players.Count - 1 do begin
    608     Player := TPlayer(Game.Players[I]);
     608    Player := Game.Players[I];
    609609    with Player do
    610610    if Mode = pmHuman then begin
     
    739739  FormMain.FormClient.Client := nil;
    740740  for I := 0 to Game.Players.Count - 1 do begin
    741     Player := TPlayer(Game.Players[I]);
     741    Player := Game.Players[I];
    742742    with Player do
    743743    if Mode = pmHuman then begin
  • trunk/Forms/FormCharts.pas

    r318 r344  
    6262  Chart1.Series.Clear;
    6363  for I := 0 to Game.Players.Count - 1 do
    64   with TPlayer(Game.Players[I]) do begin
     64  with Game.Players[I] do begin
    6565    NewSeries := TLineSeries.Create(nil);
    6666    NewSeries.LinePen.Color := Color;
  • trunk/Forms/FormGameSystem.lfm

    r330 r344  
    11object FormGameSystem: TFormGameSystem
    22  Left = 954
    3   Height = 482
     3  Height = 402
    44  Top = 479
    5   Width = 651
     5  Width = 542
    66  Caption = 'Game system'
    7   ClientHeight = 482
    8   ClientWidth = 651
    9   DesignTimePPI = 144
     7  ClientHeight = 402
     8  ClientWidth = 542
     9  DesignTimePPI = 120
    1010  OnCreate = FormCreate
    1111  OnDestroy = FormDestroy
    12   LCLVersion = '3.4.0.0'
     12  LCLVersion = '3.6.0.0'
    1313  object ButtonSave: TButton
    14     Left = 160
    15     Height = 38
    16     Top = 432
    17     Width = 113
     14    Left = 133
     15    Height = 32
     16    Top = 360
     17    Width = 94
    1818    Anchors = [akLeft, akBottom]
    1919    Caption = 'Save'
     
    2323  end
    2424  object ButtonLoad: TButton
    25     Left = 24
    26     Height = 38
    27     Top = 432
    28     Width = 113
     25    Left = 20
     26    Height = 32
     27    Top = 360
     28    Width = 94
    2929    Anchors = [akLeft, akBottom]
    3030    Caption = 'Load'
     
    3434  end
    3535  object ButtonCancel: TButton
    36     Left = 374
    37     Height = 38
    38     Top = 432
    39     Width = 113
     36    Left = 311
     37    Height = 32
     38    Top = 360
     39    Width = 94
    4040    Anchors = [akRight, akBottom]
    4141    Caption = 'Cancel'
     
    4545  end
    4646  object ButtonOk: TButton
    47     Left = 516
    48     Height = 38
    49     Top = 432
    50     Width = 113
     47    Left = 430
     48    Height = 32
     49    Top = 360
     50    Width = 94
    5151    Anchors = [akRight, akBottom]
    5252    Caption = 'OK'
     
    5656  end
    5757  object PageControl1: TPageControl
    58     Left = 16
    59     Height = 400
    60     Top = 16
    61     Width = 613
     58    Left = 13
     59    Height = 334
     60    Top = 13
     61    Width = 511
    6262    ActivePage = TabSheetGeneral
    6363    Anchors = [akTop, akLeft, akRight, akBottom]
     
    6767    object TabSheetGeneral: TTabSheet
    6868      Caption = 'General'
    69       ClientHeight = 362
    70       ClientWidth = 605
     69      ClientHeight = 301
     70      ClientWidth = 503
    7171      ParentFont = False
    7272      object CheckBoxEmptyCellsNeutral: TCheckBox
    73         Left = 16
    74         Height = 29
    75         Top = 16
    76         Width = 330
     73        Left = 13
     74        Height = 24
     75        Top = 13
     76        Width = 277
    7777        Caption = 'Set cells without player units as neutral'
    7878        ParentFont = False
     
    8080      end
    8181      object CheckBoxUnitsSplitMerge: TCheckBox
    82         Left = 16
    83         Height = 29
    84         Top = 55
    85         Width = 213
     82        Left = 13
     83        Height = 24
     84        Top = 46
     85        Width = 178
    8686        Caption = 'Units can split or merge'
    8787        ParentFont = False
     
    8989      end
    9090      object CheckBoxUnitsMoveImmediately: TCheckBox
    91         Left = 16
    92         Height = 29
    93         Top = 95
    94         Width = 217
     91        Left = 13
     92        Height = 24
     93        Top = 79
     94        Width = 182
    9595        Caption = 'Units move immediately'
    9696        ParentFont = False
     
    9898      end
    9999      object Label5: TLabel
    100         Left = 16
    101         Height = 25
    102         Top = 136
    103         Width = 153
     100        Left = 13
     101        Height = 20
     102        Top = 113
     103        Width = 128
    104104        Caption = 'Preferred grid type:'
    105105        ParentColor = False
     
    107107      end
    108108      object ComboBoxPreferredGridType: TComboBox
    109         Left = 272
    110         Height = 33
    111         Top = 136
    112         Width = 304
    113         ItemHeight = 25
     109        Left = 227
     110        Height = 28
     111        Top = 113
     112        Width = 253
     113        ItemHeight = 20
    114114        Items.Strings = (
    115115          'None'
     
    140140  end
    141141  object OpenDialog1: TOpenDialog
    142     Left = 440
    143     Top = 160
     142    Left = 367
     143    Top = 133
    144144  end
    145145  object SaveDialog1: TSaveDialog
    146     Left = 232
    147     Top = 160
     146    Left = 193
     147    Top = 133
    148148  end
    149149end
  • trunk/Forms/FormGameSystem.pas

    r330 r344  
    5151
    5252uses
    53   MapType;
     53  MapType, ItemList;
    5454
    5555resourcestring
  • trunk/Forms/FormItem.pas

    r321 r344  
    8080  J: Integer;
    8181  Control: TControl;
    82   ReferenceList: TItemList;
     82  ReferenceList: TItemList<TItem>;
    8383  ReferenceItem: TItem;
    8484begin
     
    106106      try
    107107        TComboBox(Control).Items.Clear;
    108         ReferenceList := Item.GetReferenceList(Index);
     108        ReferenceList := TItemList<TItem>(Item.GetReferenceList(Index));
    109109        if Assigned(ReferenceList) then
    110110          for J := 0 to ReferenceList.Count - 1 do
  • trunk/Forms/FormList.pas

    r317 r344  
    4646      Selected: Boolean);
    4747  private
    48     FList: TItemList;
    49     procedure SetList(AValue: TItemList);
     48    FList: IList<TItem>;
     49    procedure SetList(AValue: IList<TItem>);
    5050    procedure UpdateListViewColumns;
    5151  public
     
    5454    procedure UpdateInterface;
    5555    procedure UpdateList;
    56     property List: TItemList read FList write SetList;
     56    property List: IList<TItem> read FList write SetList;
    5757  end;
    5858
     
    9898  if Assigned(ListView1.Selected) then
    9999  with TItem(ListView1.Selected.Data) do begin
    100     TempEntry := List.CreateItem;
     100    TempEntry := List.CreateBaseItem;
    101101    TempEntry.Assign(TItem(ListView1.Selected.Data));
    102102    FormItem := TFormItem.Create(Self);
     
    122122  if (MaxItemCount <> -1) and (List.Count >= MaxItemCount) then Exit;
    123123
    124   TempEntry := List.CreateItem;
     124  TempEntry := List.CreateBaseItem;
    125125  FormItem := TFormItem.Create(Self);
    126126  try
    127     TempEntry.Name := List.GetNextAvailableName(SNew + ' ' + LowerCase(List.GetItemClass.GetClassName));
     127    TempEntry.Name := List.GetNextAvailableName(SNew + ' ' + LowerCase(List.GetName));
    128128    FormItem.Item := TempEntry;
    129129    if FormItem.ShowModal = mrOk then begin
     
    146146  if (MaxItemCount <> -1) and (List.Count >= MaxItemCount) then Exit;
    147147
    148   TempEntry := List.CreateItem;
     148  TempEntry := List.CreateBaseItem;
    149149  TempEntry.Assign(TItem(ListView1.Selected.Data));
    150150  FormItem := TFormItem.Create(Self);
     
    219219    //Item.Caption := GetName;
    220220    Item.Data := List[Item.Index];
    221     Fields := List.GetItemClass.GetFields;
     221    Fields := List.GetItemFields;
    222222    try
    223223      for I := 0 to Fields.Count - 1 do begin
     
    250250end;
    251251
    252 procedure TFormList.SetList(AValue: TItemList);
     252procedure TFormList.SetList(AValue: IList<TItem>);
    253253begin
    254254  if FList = AValue then Exit;
     
    261261  Fields: TItemFields;
    262262begin
    263   Fields := List.GetItemClass.GetFields;
     263  Fields := List.GetItemFields;
    264264  ListView1.Columns.BeginUpdate;
    265265  try
  • trunk/Forms/FormNew.pas

    r330 r344  
    388388  GamePreview.GeneratePlayers := False;
    389389  GamePreview.New;
    390   GamePreview.CurrentPlayer := TPlayer(GamePreview.Players.First);
     390  GamePreview.CurrentPlayer := GamePreview.Players.First;
    391391  Bitmap := Image1.Picture.Bitmap;
    392392  Bitmap.SetSize(Image1.Width, Image1.Height);
  • trunk/Game.pas

    r342 r344  
    295295var
    296296  Cell: TCell;
    297   NewPower: Integer;
    298297begin
    299298  // Randomize map terrain
     
    317316    NewPower := Random(MaxNeutralUnits + 1);
    318317    if (NewPower > 0) and not Assigned(OneUnit) then begin
    319       OneUnit := Units.AddNew(TUnitKind(GameSystem.UnitKinds[0]), NewPower);
     318      OneUnit := Units.AddNew(GameSystem.UnitKinds[0], NewPower);
    320319    end;
    321320    Player := nil;
     
    330329  with Cell do begin
    331330    if (Terrain = ttNormal) and CityEnabled and (Random < CityPercentage / 100) then begin
    332       Building := TBuilding(Buildings.AddItem('City'));
    333       Building.Kind := TBuildingKind(GameSystem.BuildingKinds.FindBySpecialType(stCity));
     331      Building := Buildings.AddItem('City');
     332      Building.Kind := GameSystem.BuildingKinds.FindBySpecialType(stCity);
    334333    end;
    335334  end;
     
    344343
    345344  for I := 0 to Players.Count - 1 do begin
    346     TPlayer(Players[I]).Reset;
    347     TPlayer(Players[I]).StartCell := nil;
     345    Players[I].Reset;
     346    Players[I].StartCell := nil;
    348347  end;
    349348  for I := 0 to Players.Count - 1 do
    350   with TPlayer(Players[I]) do begin
    351     Player := TPlayer(Players[I]);
     349  with Players[I] do begin
     350    Player := Players[I];
    352351    PlayerMap.Update;
    353352    if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin
     
    355354      if Assigned(Player.StartCell) then begin
    356355        if SymetricMap and (I = 1) then
    357           StartCell := Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)];
     356          StartCell := Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(Players[0].StartCell)];
    358357
    359358        if CityEnabled then begin
    360           StartCell.Building := TBuilding(Buildings.AddItem('City'));
    361           StartCell.Building.Kind := TBuildingKind(GameSystem.BuildingKinds.FindBySpecialType(stCity));
     359          StartCell.Building := Buildings.AddItem('City');
     360          StartCell.Building.Kind := GameSystem.BuildingKinds.FindBySpecialType(stCity);
    362361        end;
    363362        StartCell.Player := Player;
    364363        if GameSystem.UnitKinds.Count > 0 then begin
    365364          if not Assigned(StartCell.OneUnit) then
    366             StartCell.OneUnit := Self.Units.AddNew(TUnitKind(GameSystem.UnitKinds[0]), Player.StartUnits);
     365            StartCell.OneUnit := Self.Units.AddNew(GameSystem.UnitKinds[0], Player.StartUnits);
    367366          StartCell.OneUnit.Power := Player.StartUnits;
    368           StartCell.OneUnit.Kind := TUnitKind(GameSystem.UnitKinds[0]);
     367          StartCell.OneUnit.Kind := GameSystem.UnitKinds[0];
    369368          StartCell.OneUnit.Player := Player;
    370369        end;
     
    428427begin
    429428  for I := 0 to Players.Count - 1 do begin
    430     Player := TPlayer(Players[I]);
     429    Player := Players[I];
    431430    if Assigned(Player.StartCell) then begin
    432431      Player.StartCell.Weight := 1;
     
    633632      if Assigned(NewNode) then
    634633        Players.LoadFromNode(NewNode);
    635       if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
     634      if Players.Count > 0 then CurrentPlayer := Players[0]
    636635        else CurrentPlayer := nil;
    637636
     
    644643
    645644      for I := 0 to Players.Count - 1 do begin
    646         TPlayer(Players[I]).PlayerMap.Update;
    647         TPlayer(Players[I]).PlayerMap.CheckVisibility;
     645        Players[I].PlayerMap.Update;
     646        Players[I].PlayerMap.CheckVisibility;
    648647      end;
    649648      ComputePlayerStats;
     
    742741begin
    743742  for I := 0 to Players.Count - 1 do
    744   with TPlayer(Players[I]) do begin
     743  with Players[I] do begin
    745744    TotalUnits := 0;
    746745    TotalCells := 0;
     
    763762begin
    764763  for I := 0 to Players.Count - 1 do
    765   with TPlayer(Players[I]) do begin
     764  with Players[I] do begin
    766765    NewStat := TGameTurnStat.Create;
    767766    NewStat.DiscoveredCells := TotalDiscovered;
     
    804803      NewPlayerIndex := NewPlayerIndex mod AlivePlayers.Count;
    805804    end;
    806     CurrentPlayer := TPlayer(AlivePlayers[NewPlayerIndex]);
     805    CurrentPlayer := AlivePlayers[NewPlayerIndex];
    807806  finally
    808807    AlivePlayers.Free;
     
    931930  I: Integer;
    932931begin
    933   for I := 0 to Players.Count - 1 do TPlayer(Players[I]).Clear;
     932  for I := 0 to Players.Count - 1 do Players[I].Clear;
    934933  Map.Clear;
    935934  Units.Clear;
     
    960959
    961960  InitPlayers;
    962   if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
     961  if Players.Count > 0 then CurrentPlayer := Players[0]
    963962    else CurrentPlayer := nil;
    964963
  • trunk/GameSystem.pas

    r342 r344  
    44
    55uses
    6   Classes, SysUtils, Generics.Collections, Units, DOM, XMLRead, XMLWrite, XML,
     6  Classes, SysUtils, Generics.Collections, DOM, XMLRead, XMLWrite, XML,
    77  XMLConf, FileUtil, LazFileUtils, MapType, Nation, Building, ItemList, UnitKind;
    88
  • trunk/ItemList.pas

    r342 r344  
    77
    88type
    9   TItemList = class;
     9  //TItemList<T> = class;
    1010  TUndefinedEnum = (eeNone);
    1111
     
    3434    function AddField(Index: Integer; SysName, Name: string; DataType: TDataType): TItemField;
    3535    function SearchByIndex(Index: Integer): TItemField;
     36  end;
     37
     38  { IList }
     39
     40  IList<T> = interface
     41    function GetName: string;
     42    function GetCount: SizeInt;
     43    function GetItem(Index: SizeInt): T;
     44    function Remove(constref AValue: T): SizeInt;
     45    function Add(constref AValue: T): SizeInt;
     46    function CreateBaseItem(Name: string = ''): T;
     47    function GetNextAvailableName(Name: string): string;
     48    function GetItemFields: TItemFields;
     49    procedure SetItem(Index: SizeInt; AValue: T);
     50    property Count: SizeInt read GetCount;
     51    property Items[Index: SizeInt]: T read GetItem write SetItem; default;
    3652  end;
    3753
     
    7187    class function GetClassSysName: string; virtual;
    7288    class function GetClassName: string; virtual;
    73     function GetReferenceList(Index: Integer): TItemList; virtual;
     89    function GetReferenceList(Index: Integer): IList<TItem>; virtual;
    7490    constructor Create; virtual;
    7591  end;
     
    7793  TItemClass = class of TItem;
    7894
     95  IItemList = interface(IList<TItem>)
     96  end;
     97
    7998  { TItemList }
    8099
    81   TItemList = class(TObjectList<TItem>)
     100  TItemList<T: TItem> = class(TObjectList<T>, IList<TItem>)
    82101  private
    83102    procedure RecalculateNewId(Reset: Boolean);
    84103    procedure RecalculateItemsId;
     104    function GetItem(Index: SizeInt): TItem;
     105    procedure SetItem(Index: SizeInt; AValue: TItem);
     106  protected
     107    function GetCount: SizeInt; override;
    85108  public
     109  type
     110  var
    86111    NewId: Integer;
    87     class function GetItemClass: TItemClass; virtual;
     112    function GetName: string;
     113    function GetItemFields: TItemFields;
     114    function Remove(constref AValue: TItem): SizeInt;
     115    function Add(constref AValue: TItem): SizeInt;
     116    function CreateItem(Name: string = ''): T; virtual;
     117    function CreateBaseItem(Name: string = ''): TItem;
    88118    function IncrementName(Name: string): string;
    89119    function GetNextAvailableName(Name: string): string;
    90     function FindById(Id: Integer): TItem;
    91     function FindByName(Name: string): TItem;
     120    function FindById(Id: Integer): T;
     121    function FindByName(Name: string): T;
    92122    function GetNewId: Integer;
    93123    function ToString: string; override;
    94     procedure Assign(Source: TItemList); virtual;
    95     function Compare(ItemList: TItemList): Boolean; virtual;
    96     function AddItem(Name: string = ''): TItem; virtual;
    97     function CreateItem(Name: string = ''): TItem; virtual;
     124    procedure Assign(Source: TItemList<T>); virtual;
     125    function Compare(ItemList: TItemList<T>): Boolean; virtual;
     126    function AddItem(Name: string = ''): T; virtual;
    98127    procedure LoadFromNode(Node: TDOMNode); virtual;
    99128    procedure SaveToNode(Node: TDOMNode); virtual;
    100129    constructor Create(FreeObjects: Boolean = True);
     130    function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
     131    function _AddRef: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
     132    function _Release: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
    101133  end;
    102134
     
    145177{ TItemList }
    146178
    147 procedure TItemList.Assign(Source: TItemList);
     179procedure TItemList<T>.Assign(Source: TItemList<T>);
    148180var
    149181  I: Integer;
     
    155187end;
    156188
    157 function TItemList.Compare(ItemList: TItemList): Boolean;
     189function TItemList<T>.Compare(ItemList: TItemList<T>): Boolean;
    158190var
    159191  I: Integer;
     
    167199end;
    168200
    169 function TItemList.AddItem(Name: string): TItem;
    170 begin
    171   Result := CreateItem(Name);
     201function TItemList<T>.AddItem(Name: string): T;
     202begin
     203  Result := T.Create;
     204  Result.Name := Name;
    172205  Result.Id := GetNewId;
    173206  Add(Result);
    174207end;
    175208
    176 function TItemList.CreateItem(Name: string): TItem;
    177 begin
    178   Result := GetItemClass.Create;
     209function TItemList<T>.CreateItem(Name: string): T;
     210begin
     211  Result := T.Create;
    179212  Result.Name := Name;
    180213end;
    181214
    182 procedure TItemList.LoadFromNode(Node: TDOMNode);
     215function TItemList<T>.CreateBaseItem(Name: string): TItem;
     216begin
     217  Result := TItem(CreateItem(Name));
     218end;
     219
     220procedure TItemList<T>.LoadFromNode(Node: TDOMNode);
    183221var
    184222  Node2: TDOMNode;
    185   NewItem: TItem;
     223  NewItem: T;
    186224begin
    187225  Count := 0;
    188226  Node2 := Node.FirstChild;
    189   while Assigned(Node2) and (Node2.NodeName = UnicodeString(GetItemClass.GetClassSysName)) do begin
     227  while Assigned(Node2) and (Node2.NodeName = UnicodeString(T.GetClassSysName)) do begin
    190228    NewItem := CreateItem;
    191229    NewItem.LoadFromNode(Node2);
     
    195233end;
    196234
    197 procedure TItemList.SaveToNode(Node: TDOMNode);
     235procedure TItemList<T>.SaveToNode(Node: TDOMNode);
    198236var
    199237  I: Integer;
     
    203241  for I := 0 to Count - 1 do
    204242  with TItem(Items[I]) do begin
    205     NewNode2 := Node.OwnerDocument.CreateElement(UnicodeString(GetItemClass.GetClassSysName));
     243    NewNode2 := Node.OwnerDocument.CreateElement(UnicodeString(T.GetClassSysName));
    206244    Node.AppendChild(NewNode2);
    207245    SaveToNode(NewNode2);
     
    209247end;
    210248
    211 constructor TItemList.Create(FreeObjects: Boolean);
     249constructor TItemList<T>.Create(FreeObjects: Boolean);
    212250begin
    213251  inherited;
     
    215253end;
    216254
    217 procedure TItemList.RecalculateNewId(Reset: Boolean);
     255function TItemList<T>.QueryInterface(constref iid: tguid; out obj): longint;
     256  stdcall;
     257begin
     258end;
     259
     260function TItemList<T>._AddRef: Longint; stdcall;
     261begin
     262end;
     263
     264function TItemList<T>._Release: Longint; stdcall;
     265begin
     266end;
     267
     268function TItemList<T>.GetCount: SizeInt;
     269begin
     270  Result := Count;
     271end;
     272
     273function TItemList<T>.GetName: string;
     274begin
     275  Result := T.GetClassName;
     276end;
     277
     278function TItemList<T>.GetItemFields: TItemFields;
     279begin
     280  Result := T.GetFields;
     281end;
     282
     283function TItemList<T>.Remove(constref AValue: TItem): SizeInt;
     284begin
     285  Result := inherited Remove(T(AValue));
     286end;
     287
     288function TItemList<T>.Add(constref AValue: TItem): SizeInt;
     289begin
     290  Result := inherited Add(T(AValue));
     291end;
     292
     293procedure TItemList<T>.RecalculateNewId(Reset: Boolean);
    218294var
    219295  I: Integer;
     
    226302end;
    227303
    228 procedure TItemList.RecalculateItemsId;
     304procedure TItemList<T>.RecalculateItemsId;
    229305var
    230306  I: Integer;
     
    235311end;
    236312
    237 class function TItemList.GetItemClass: TItemClass;
    238 begin
    239   Result := TItem;
    240 end;
    241 
    242 function TItemList.IncrementName(Name: string): string;
     313function TItemList<T>.GetItem(Index: SizeInt): TItem;
     314begin
     315  Result := inherited GetItem(Index);
     316end;
     317
     318procedure TItemList<T>.SetItem(Index: SizeInt; AValue: TItem);
     319begin
     320  inherited SetItem(Index, T(AValue));
     321end;
     322
     323function TItemList<T>.IncrementName(Name: string): string;
    243324var
    244325  I: Integer;
     
    253334end;
    254335
    255 function TItemList.GetNextAvailableName(Name: string): string;
     336function TItemList<T>.GetNextAvailableName(Name: string): string;
    256337begin
    257338  Result := Name;
     
    260341end;
    261342
    262 function TItemList.FindById(Id: Integer): TItem;
     343function TItemList<T>.FindById(Id: Integer): T;
    263344var
    264345  I: Integer;
     
    270351end;
    271352
    272 function TItemList.FindByName(Name: string): TItem;
     353function TItemList<T>.FindByName(Name: string): T;
    273354var
    274355  I: Integer;
     
    280361end;
    281362
    282 function TItemList.GetNewId: Integer;
     363function TItemList<T>.GetNewId: Integer;
    283364begin
    284365  Result := NewId;
     
    286367end;
    287368
    288 function TItemList.ToString: string;
     369function TItemList<T>.ToString: string;
    289370var
    290371  I: Integer;
     
    370451var
    371452  ReadId: Integer;
    372   ReferenceList: TItemList;
     453  ReferenceList: IList<TItem>;
    373454begin
    374455  if Field.DataType = dtString then begin
     
    389470  if Field.DataType = dtReference then begin
    390471    ReadId := ReadInteger(Node, Field.SysName, 0);
    391     ReferenceList := GetReferenceList(Field.Index);
     472    ReferenceList := IList<TItem>(GetReferenceList(Field.Index));
    392473    if (ReadId > 0) and Assigned(ReferenceList) then
    393474      SetValueReference(Field.Index, TItem(ReferenceList[ReadId]));
     
    632713end;
    633714
    634 function TItem.GetReferenceList(Index: Integer): TItemList;
     715function TItem.GetReferenceList(Index: Integer): IList<TItem>;
    635716begin
    636717  Result := nil;
  • trunk/Nation.pas

    r317 r344  
    2121  { TNations }
    2222
    23   TNations = class(TItemList)
    24     class function GetItemClass: TItemClass; override;
     23  TNations = class(TItemList<TNation>)
    2524  end;
    2625
     
    6766end;
    6867
    69 { TNations }
    70 
    71 class function TNations.GetItemClass: TItemClass;
    72 begin
    73   Result := TNation;
    74 end;
    75 
    7668end.
    7769
  • trunk/Player.pas

    r342 r344  
    100100    procedure GetValue(Index: Integer; out Value); override;
    101101    procedure SetValue(Index: Integer; var Value); override;
    102     function GetReferenceList(Index: Integer): TItemList; override;
     102    function GetReferenceList(Index: Integer): IList<TItem>; override;
    103103    class function GetClassSysName: string; override;
    104104    class function GetClassName: string; override;
     
    134134  { TPlayers }
    135135
    136   TPlayers = class(TItemList)
     136  TPlayers = class(TItemList<TPlayer>)
    137137  public
    138138    Game: TObject; //TGame;
    139     class function GetItemClass: TItemClass; override;
    140139    procedure New(Name: string; Color: TColor; Mode: TPlayerMode);
    141     function CreateItem(Name: string = ''): TItem; override;
     140    function CreateItem(Name: string = ''): TPlayer; override;
    142141    function GetFirstHuman: TPlayer;
    143142    procedure LoadConfig(Config: TXmlConfig; Path: string);
     
    488487  Result := 0;
    489488  for I := 0 to Count - 1 do
    490     if TPlayer(Items[I]).IsAlive then Inc(Result);
     489    if Items[I].IsAlive then Inc(Result);
    491490end;
    492491
     
    497496  Players.Clear;
    498497  for I := 0 to Count - 1 do
    499     if TPlayer(Items[I]).IsAlive then Players.Add(TPlayer(Items[I]));
    500 end;
    501 
    502 class function TPlayers.GetItemClass: TItemClass;
    503 begin
    504   Result := TPlayer;
     498    if Items[I].IsAlive then Players.Add(Items[I]);
    505499end;
    506500
     
    508502begin
    509503  AddItem(Name);
    510   TPlayer(Last).Color := Color;
    511   TPlayer(Last).Mode := Mode;
     504  Last.Color := Color;
     505  Last.Mode := Mode;
    512506  if Mode = pmComputer then
    513     TPlayer(Last).Agressivity := caMedium;
    514 end;
    515 
    516 function TPlayers.CreateItem(Name: string): TItem;
     507    Last.Agressivity := caMedium;
     508end;
     509
     510function TPlayers.CreateItem(Name: string): TPlayer;
    517511begin
    518512  Result := inherited;
    519   TPlayer(Result).Game := Game;
     513  Result.Game := Game;
    520514end;
    521515
     
    525519begin
    526520  I := 0;
    527   while (I < Count) and (TPlayer(Items[I]).Mode <> pmHuman) do Inc(I);
    528   if I < Count then Result := TPlayer(Items[I])
     521  while (I < Count) and (Items[I].Mode <> pmHuman) do Inc(I);
     522  if I < Count then Result := Items[I]
    529523    else Result := nil;
    530524end;
     
    544538        Items[I] := CreateItem;
    545539        Items[I].Id := GetNewId;
    546         TPlayer(Items[I]).LoadConfig(Config, Path + '/Player' + IntToStr(I));
     540        Items[I].LoadConfig(Config, Path + '/Player' + IntToStr(I));
    547541      end;
    548542    end;
     
    555549begin
    556550  for I := 0 to Count - 1 do
    557     TPlayer(Items[I]).SaveConfig(Config, Path + '/Player' + IntToStr(I));
     551    Items[I].SaveConfig(Config, Path + '/Player' + IntToStr(I));
    558552  with Config do begin
    559553    SetValue(DOMString(Path + '/Count'), Count);
     
    568562  SetLength(Result, 0);
    569563  for I := 0 to Count - 1 do
    570     if TPlayer(Items[I]).IsAlive then begin
     564    if Items[I].IsAlive then begin
    571565      SetLength(Result, Length(Result) + 1);
    572       Result[Length(Result) - 1] := TPlayer(Items[I]);
     566      Result[Length(Result) - 1] := Items[I];
    573567    end;
    574568end;
     
    581575  SetLength(Result, 0);
    582576  for I := 0 to Count - 1 do
    583     if TPlayer(Items[I]).TotalCities > 0 then begin
     577    if Items[I].TotalCities > 0 then begin
    584578      SetLength(Result, Length(Result) + 1);
    585       Result[Length(Result) - 1] := TPlayer(Items[I]);
     579      Result[Length(Result) - 1] := Items[I];
    586580    end;
    587581end;
     
    828822    Defensive := GetValue(DOMString(Path + '/Defensive'), False);
    829823    Agressivity := TComputerAgressivity(GetValue(DOMString(Path + '/Agressivity'), 0));
    830     Nation := TNation(TGame(Game).GameSystem.Nations.FindById(GetValue(DOMString(Path + '/Nation'), 0)));
     824    Nation := TGame(Game).GameSystem.Nations.FindById(GetValue(DOMString(Path + '/Nation'), 0));
    831825  end;
    832826end;
     
    12281222end;
    12291223
    1230 function TPlayer.GetReferenceList(Index: Integer): TItemList;
     1224function TPlayer.GetReferenceList(Index: Integer): IList<TItem>;
    12311225begin
    12321226  if Index = 2 then Result := TGame(Game).GameSystem.Nations
     
    12721266    ((TGame(Game).GrowCells = gcPlayerCities) and (Assigned(Building) and (Building.Kind.SpecialType = stCity)))) then begin
    12731267      if not Assigned(OneUnit) then begin
    1274         NewUnit := TGame(Game).Units.AddNew(TUnitKind(TGame(Game).GameSystem.UnitKinds.First), 0);
     1268        NewUnit := TGame(Game).Units.AddNew(TGame(Game).GameSystem.UnitKinds.First, 0);
    12751269        NewUnit.Player := Self;
    12761270        NewUnit.MapCell := Cells[I];
  • trunk/UnitKind.pas

    r339 r344  
    2525  { TUnitKinds }
    2626
    27   TUnitKinds = class(TItemList)
     27  TUnitKinds = class(TItemList<TUnitKind>)
    2828    constructor Create(FreeObjects: Boolean = True);
    29     class function GetItemClass: TItemClass; override;
    3029  end;
    3130
     
    9695end;
    9796
    98 class function TUnitKinds.GetItemClass: TItemClass;
    99 begin
    100   Result := TUnitKind;
    101 end;
    102 
    10397end.
    10498
  • trunk/Units.pas

    r342 r344  
    177177begin
    178178  Player := TGame(Game).Players.FindById(PlayerId);
    179   Kind := TUnitKind(TGame(Game).GameSystem.UnitKinds.FindById(KindId));
     179  Kind := TGame(Game).GameSystem.UnitKinds.FindById(KindId);
    180180end;
    181181
Note: See TracChangeset for help on using the changeset viewer.