Changeset 298 for trunk/UPlayer.pas


Ignore:
Timestamp:
Jul 10, 2019, 5:35:15 PM (5 years ago)
Author:
chronos
Message:
  • Added: New item list type reference. Used in players for reference to selected nation.
  • Modified: New game players selection migrated to dynamic item/list forms.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UPlayer.pas

    r297 r298  
    122122    procedure GetValue(Index: Integer; out Value); override;
    123123    procedure SetValue(Index: Integer; var Value); override;
     124    function GetReferenceList(Index: Integer): TItemList; override;
    124125    class function GetClassSysName: string; override;
     126    class function GetClassName: string; override;
    125127    function IsAllowedMoveTarget(CellFrom, CellTo: TPlayerCell): Boolean;
    126128    procedure ReduceMovesPower;
     
    139141    procedure LoadFromNode(Node: TDOMNode); override;
    140142    procedure SaveToNode(Node: TDOMNode); override;
    141     constructor Create;
     143    constructor Create; override;
    142144    destructor Destroy; override;
    143145    procedure Assign(Source: TItem); override;
     
    153155  { TPlayers }
    154156
    155   TPlayers = class(TFPGObjectList<TPlayer>)
     157  TPlayers = class(TItemList)
    156158  public
    157159    Game: TObject; //TGame;
    158     NewId: Integer;
    159     function FindById(Id: Integer): TPlayer;
     160    class function GetItemClass: TItemClass; override;
    160161    procedure New(Name: string; Color: TColor; Mode: TPlayerMode);
    161     function GetNewId: Integer;
    162     procedure LoadFromNode(Node: TDOMNode);
    163     procedure SaveToNode(Node: TDOMNode);
    164     constructor Create(FreeObjects: Boolean = True);
     162    function CreateItem(Name: string = ''): TItem; override;
    165163    function GetFirstHuman: TPlayer;
    166     procedure Assign(Source: TPlayers);
    167164    procedure LoadConfig(Config: TXmlConfig; Path: string);
    168165    procedure SaveConfig(Config: TXmlConfig; Path: string);
     
    216213  SDefenderPowerPositive = 'Defender power have to be higher then or equal to 0.';
    217214  SUnfinishedBattle = 'Unfinished battle';
     215  SLow = 'Low';
     216  SMedium = 'Medium';
     217  SHigh = 'High';
     218  SHuman = 'Human';
     219  SComputer = 'Computer';
     220  SNation = 'Nation';
     221  SMode = 'Mode';
     222  SColor = 'Color';
     223  SAgressivity = 'Agressivity';
     224  SStartUnits = 'Start units';
     225  SDefensive = 'Defensive';
    218226
    219227function ComparePointer(const Item1, Item2: Integer): Integer;
     
    497505function TPlayers.GetAliveCount: Integer;
    498506var
    499   Player: TPlayer;
     507  I: Integer;
    500508begin
    501509  Result := 0;
    502   for Player in Self do
    503     if Player.IsAlive then Inc(Result);
     510  for I := 0 to Count - 1 do
     511    if TPlayer(Items[I]).IsAlive then Inc(Result);
    504512end;
    505513
    506514procedure TPlayers.GetAlivePlayers(Players: TPlayers);
    507515var
    508   Player: TPlayer;
     516  I: Integer;
    509517begin
    510518  Players.Clear;
    511   for Player in Self do
    512     if Player.IsAlive then Players.Add(Player);
    513 end;
    514 
    515 function TPlayers.FindById(Id: Integer): TPlayer;
     519  for I := 0 to Count - 1 do
     520    if TPlayer(Items[I]).IsAlive then Players.Add(TPlayer(Items[I]));
     521end;
     522
     523class function TPlayers.GetItemClass: TItemClass;
     524begin
     525  Result := TPlayer;
     526end;
     527
     528procedure TPlayers.New(Name: string; Color: TColor; Mode: TPlayerMode);
     529begin
     530  AddItem(Name);
     531  TPlayer(Last).Color := Color;
     532  TPlayer(Last).Mode := Mode;
     533  if Mode = pmComputer then
     534    TPlayer(Last).Agressivity := caMedium;
     535end;
     536
     537function TPlayers.CreateItem(Name: string): TItem;
     538begin
     539  Result := inherited;
     540  TPlayer(Result).Game := Game;
     541end;
     542
     543function TPlayers.GetFirstHuman: TPlayer;
    516544var
    517545  I: Integer;
    518546begin
    519547  I := 0;
    520   while (I < Count) and (Items[I].Id <> Id) do Inc(I);
    521   if I < Count then Result := Items[I]
     548  while (I < Count) and (TPlayer(Items[I]).Mode <> pmHuman) do Inc(I);
     549  if I < Count then Result := TPlayer(Items[I])
    522550    else Result := nil;
    523 end;
    524 
    525 procedure TPlayers.New(Name: string; Color: TColor; Mode: TPlayerMode);
    526 var
    527   NewPlayer: TPlayer;
    528 begin
    529   NewPlayer := TPlayer.Create;
    530   NewPlayer.Game := Game;
    531   NewPlayer.Name := Name;
    532   NewPlayer.Color := Color;
    533   NewPlayer.Mode := Mode;
    534   NewPlayer.Id := GetNewId;
    535   if Mode = pmComputer then
    536     NewPlayer.Agressivity := caMedium;
    537   Add(NewPlayer);
    538 end;
    539 
    540 function TPlayers.GetNewId: Integer;
    541 begin
    542   Result := NewId;
    543   Inc(NewId);
    544 end;
    545 
    546 procedure TPlayers.LoadFromNode(Node: TDOMNode);
    547 var
    548   Node2: TDOMNode;
    549   NewPlayer: TPlayer;
    550 begin
    551   Count := 0;
    552   Node2 := Node.FirstChild;
    553   while Assigned(Node2) and (Node2.NodeName = 'Player') do begin
    554     NewPlayer := TPlayer.Create;
    555     NewPlayer.Game := Game;
    556     NewPlayer.LoadFromNode(Node2);
    557     Add(NewPlayer);
    558     Node2 := Node2.NextSibling;
    559   end;
    560 end;
    561 
    562 procedure TPlayers.SaveToNode(Node: TDOMNode);
    563 var
    564   I: Integer;
    565   NewNode: TDOMNode;
    566 begin
    567   for I := 0 to Count - 1 do begin;
    568     NewNode := Node.OwnerDocument.CreateElement('Player');
    569     Node.AppendChild(NewNode);
    570     Items[I].SaveToNode(NewNode);
    571   end;
    572 end;
    573 
    574 constructor TPlayers.Create(FreeObjects: Boolean = True);
    575 begin
    576   inherited;
    577   NewId := 1;
    578 end;
    579 
    580 function TPlayers.GetFirstHuman: TPlayer;
    581 var
    582   I: Integer;
    583 begin
    584   I := 0;
    585   while (I < Count) and (Items[I].Mode <> pmHuman) do Inc(I);
    586   if I < Count then Result := Items[I]
    587     else Result := nil;
    588 end;
    589 
    590 procedure TPlayers.Assign(Source: TPlayers);
    591 var
    592   I: Integer;
    593 begin
    594   while Count > Source.Count do
    595     Delete(Count - 1);
    596   while Count < Source.Count do
    597     Add(TPlayer.Create);
    598   for I := 0 to Count - 1 do begin
    599     Items[I].Assign(Source[I]);
    600     Items[I].Game := Game;
    601   end;
    602   NewId := Source.NewId;
    603551end;
    604552
     
    615563      Count := NewCount;
    616564      for I := 0 to Count - 1 do begin
    617         Items[I] := TPlayer.Create;
    618         Items[I].Id := GetNewId;
    619         Items[I].Game := Game;
    620         Items[I].LoadConfig(Config, Path + '/Player' + IntToStr(I));
     565        Items[I] := CreateItem;
     566        TPlayer(Items[I]).LoadConfig(Config, Path + '/Player' + IntToStr(I));
    621567      end;
    622568    end;
     
    629575begin
    630576  for I := 0 to Count - 1 do
    631     Items[I].SaveConfig(Config, Path + '/Player' + IntToStr(I));
     577    TPlayer(Items[I]).SaveConfig(Config, Path + '/Player' + IntToStr(I));
    632578  with Config do begin
    633579    SetValue(DOMString(Path + '/Count'), Count);
     
    637583function TPlayers.GetAlivePlayers: TPlayerArray;
    638584var
    639   Player: TPlayer;
     585  I: Integer;
    640586begin
    641587  SetLength(Result, 0);
    642   for Player in Self do
    643     if Player.IsAlive then begin
     588  for I := 0 to Count - 1 do
     589    if TPlayer(Items[I]).IsAlive then begin
    644590      SetLength(Result, Length(Result) + 1);
    645       Result[Length(Result) - 1] := Player;
     591      Result[Length(Result) - 1] := TPlayer(Items[I]);
    646592    end;
    647593end;
     
    649595function TPlayers.GetAlivePlayersWithCities: TPlayerArray;
    650596var
    651   Player: TPlayer;
     597  I: Integer;
    652598begin
    653599  SetLength(Result, 0);
    654   for Player in Self do
    655     if Player.TotalCities > 0 then begin
     600  for I := 0 to Count - 1 do
     601    if TPlayer(Items[I]).TotalCities > 0 then begin
    656602      SetLength(Result, Length(Result) + 1);
    657       Result[Length(Result) - 1] := Player;
     603      Result[Length(Result) - 1] := TPlayer(Items[I]);
    658604    end;
    659605end;
     
    803749  NewNode: TDOMNode;
    804750begin
    805   Id := ReadInteger(Node, 'Id', 0);
    806   Name := ReadString(Node, 'Name', '');
    807   Color := ReadInteger(Node, 'Color', clSilver);
    808   Mode := TPlayerMode(ReadInteger(Node, 'Mode', Integer(pmHuman)));
     751  inherited;
    809752  StartCell := TGame(FGame).Map.Cells.FindById(ReadInteger(Node, 'StartCell', 0));
    810   StartUnits := ReadInteger(Node, 'StartUnits', 0);
    811   Agressivity := TComputerAgressivity(ReadInteger(Node, 'Agressivity', Integer(caMedium)));
    812   Defensive := ReadBoolean(Node, 'Defensive', False);
    813753
    814754  with Node do begin
     
    834774  NewNode: TDOMNode;
    835775begin
    836   WriteInteger(Node, 'Id', Id);
    837   WriteString(Node, 'Name', Name);
    838   WriteInteger(Node, 'Color', Color);
    839   WriteInteger(Node, 'Mode', Integer(Mode));
     776  inherited;
    840777  WriteInteger(Node, 'StartCell', StartCell.Id);
    841   WriteInteger(Node, 'StartUnits', StartUnits);
    842   WriteInteger(Node, 'Agressivity', Integer(Agressivity));
    843   WriteBoolean(Node, 'Defensive', Defensive);
    844778
    845779  with Node do begin
     
    862796constructor TPlayer.Create;
    863797begin
     798  inherited;
    864799  Units := TUnits.Create(False);
    865800  Moves := TUnitMoves.Create;
     
    879814  FreeAndNil(Moves);
    880815  FreeAndNil(Units);
    881   inherited Destroy;
     816  inherited;
    882817end;
    883818
    884819procedure TPlayer.Assign(Source: TItem);
    885820begin
    886   Id := Source.Id;
    887   Name := Source.Name;
    888   Color := TPlayer(Source).Color;
    889   Mode := TPlayer(Source).Mode;
    890   Game := TPlayer(Source).Game;
     821  inherited;
     822  //Game := TPlayer(Source).Game;
    891823  TotalCells := TPlayer(Source).TotalCells;
    892824  TotalUnits := TPlayer(Source).TotalUnits;
     
    894826  TotalDiscovered := TPlayer(Source).TotalDiscovered;
    895827  TotalWinObjectiveCells := TPlayer(Source).TotalWinObjectiveCells;
    896   StartUnits := TPlayer(Source).StartUnits;
    897828  StartCell := TPlayer(Source).StartCell;
    898   Agressivity := TPlayer(Source).Agressivity;
    899   Defensive := TPlayer(Source).Defensive;
    900   Nation := TPlayer(Source).Nation;
    901829end;
    902830
     
    12661194
    12671195class function TPlayer.GetFields: TItemFields;
     1196var
     1197  Field: TItemField;
    12681198begin
    12691199  Result := inherited;
    1270   Result.AddField(2, 'Color', 'Color', dtColor);
    1271   Result.AddField(3, 'Mode', 'Mode', dtInteger);
    1272   Result.AddField(4, 'StartUnits', 'StartUnits', dtInteger);
    1273   Result.AddField(5, 'Agressivity', 'Agressivity', dtInteger);
    1274   Result.AddField(6, 'Defensive', 'Defensive', dtBoolean);
     1200  Field := Result.AddField(2, 'Nation', SNation, dtReference);
     1201  Field := Result.AddField(3, 'Mode', SMode, dtEnumeration);
     1202  Field.EnumStates.Add(SHuman);
     1203  Field.EnumStates.Add(SComputer);
     1204  Result.AddField(4, 'Color', SColor, dtColor);
     1205  Field := Result.AddField(5, 'Agressivity', SAgressivity, dtEnumeration);
     1206  Field.EnumStates.Add(SLow);
     1207  Field.EnumStates.Add(SMedium);
     1208  Field.EnumStates.Add(SHigh);
     1209  Result.AddField(6, 'Defensive', SDefensive, dtBoolean);
     1210  Result.AddField(7, 'StartUnits', SStartUnits, dtInteger);
    12751211end;
    12761212
    12771213procedure TPlayer.GetValue(Index: Integer; out Value);
    12781214begin
    1279   inherited GetValue(Index, Value);
     1215  if Index = 1 then string(Value) := Name
     1216  else if Index = 2 then TNation(Value) := Nation
     1217  else if Index = 3 then TPlayerMode(Value) := Mode
     1218  else if Index = 4 then TColor(Value) := Color
     1219  else if Index = 5 then TComputerAgressivity(Value) := Agressivity
     1220  else if Index = 6 then Boolean(Value) := Defensive
     1221  else if Index = 7 then Integer(Value) := StartUnits
     1222  else inherited;
    12801223end;
    12811224
    12821225procedure TPlayer.SetValue(Index: Integer; var Value);
    12831226begin
    1284   inherited SetValue(Index, Value);
     1227  if Index = 1 then Name := string(Value)
     1228  else if Index = 2 then Nation := TNation(Value)
     1229  else if Index = 3 then Mode := TPlayerMode(Value)
     1230  else if Index = 4 then Color := TColor(Value)
     1231  else if Index = 5 then Agressivity := TComputerAgressivity(Value)
     1232  else if Index = 6 then Defensive := Boolean(Value)
     1233  else if Index = 7 then StartUnits := Integer(Value)
     1234  else inherited;
     1235end;
     1236
     1237function TPlayer.GetReferenceList(Index: Integer): TItemList;
     1238begin
     1239  if Index = 2 then Result := TGame(Game).GameSystem.Nations
     1240  else Result := nil;
    12851241end;
    12861242
     
    12881244begin
    12891245  Result := 'Player';
     1246end;
     1247
     1248class function TPlayer.GetClassName: string;
     1249begin
     1250  Result := SPlayer;
    12901251end;
    12911252
Note: See TracChangeset for help on using the changeset viewer.