Changeset 36 for trunk/UGame.pas


Ignore:
Timestamp:
Mar 8, 2014, 12:01:08 AM (11 years ago)
Author:
chronos
Message:
  • Added: Save and load game setting to persistent XML config file.
  • Fixed: Show error on player start cell initialization if there is not free place for other start cells.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        55xtactics.dbg
        66heaptrclog.trc
         7Config.xml
  • trunk/UGame.pas

    r35 r36  
    66
    77uses
    8   Classes, SysUtils, ExtCtrls, Graphics, Contnrs;
     8  Classes, SysUtils, ExtCtrls, Graphics, Contnrs, XMLConf;
    99
    1010const
     
    2929
    3030  TCell = class
     31  private
     32    FPower: Integer;
     33    procedure SetPower(AValue: Integer);
     34  public
    3135    Pos: TPoint;
    3236    Terrain: TTerrainType;
    33     Power: Integer;
    3437    Player: TPlayer;
    3538    MovesFrom: TObjectList;
     
    3942    constructor Create;
    4043    destructor Destroy; override;
     44    property Power: Integer read FPower write SetPower;
    4145  end;
    4246
     
    176180    Moves: TObjectList; // TList<TMove>
    177181    TurnCounter: Integer;
    178 
     182    procedure SaveConfig(Config: TXmlConfig; Path: string);
     183    procedure LoadConfig(Config: TXmlConfig; Path: string);
    179184    procedure ComputePlayerStats;
    180185    function GetAlivePlayers: TPlayerArray;
     
    362367
    363368{ TCell }
     369
     370procedure TCell.SetPower(AValue: Integer);
     371begin
     372  if FPower = AValue then Exit;
     373  if AValue < 0 then
     374    raise Exception.Create('Not allowed to substract power under zero do negative value');
     375  FPower := AValue;
     376end;
    364377
    365378function TCell.GetColor: TColor;
     
    568581  DefenderRoll: Integer;
    569582begin
     583  if AttackPower < 1 then
     584    raise Exception.Create('Attacker power have to be higher then 0.');
     585  if DefendPower < 0 then
     586    raise Exception.Create('Defender power have to be higher then or equal to 0.');
    570587  while (AttackPower > 0) and (DefendPower > 0) do begin
    571588    // Earch side do dice roll and compare result. Defender wins tie
     
    590607      if CellTo.Player = Player then begin
    591608        // Inner move
    592         CellTo.Power := CellTo.Power + CountOnce;
    593609      end else begin
    594610        AttackerPower := CountOnce;
     
    705721end;
    706722
     723procedure TGame.SaveConfig(Config: TXmlConfig; Path: string);
     724begin
     725  with Config do begin
     726    SetValue(Path + '/VoidEnabled', VoidEnabled);
     727    SetValue(Path + '/VoidPercentage', VoidPercentage);
     728    SetValue(Path + '/MapSizeX', Map.Size.X);
     729    SetValue(Path + '/MapSizeY', Map.Size.Y);
     730  end;
     731end;
     732
     733procedure TGame.LoadConfig(Config: TXmlConfig; Path: string);
     734begin
     735  with Config do begin
     736    VoidEnabled := GetValue(Path + '/VoidEnabled', True);
     737    VoidPercentage := GetValue(Path + '/VoidPercentage', 20);
     738    Map.Size := Point(GetValue(Path + '/MapSizeX', 15),
     739      GetValue(Path + '/MapSizeY', 15));
     740  end;
     741end;
     742
    707743procedure TGame.ComputePlayerStats;
    708744var
     
    796832  I: Integer;
    797833  StartCell: TCell;
     834  Counter: Integer;
    798835begin
    799836  TurnCounter := 1;
     
    811848    View.Clear;
    812849    if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin
    813       StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)];
     850      // Try to obtain start cell for each player
     851      StartCell := nil;
     852      Counter := 0;
     853      while not Assigned(StartCell) or Assigned(StartCell.Player) do begin
     854        StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)];
     855        Inc(Counter);
     856        if Counter > 100 then
     857          raise Exception.Create('Cannot choose start cell for player');
     858      end;
    814859      StartCell.Terrain := ttNormal;
    815860      StartCell.Player := TPlayer(Players[I]);
Note: See TracChangeset for help on using the changeset viewer.