Changeset 75 for trunk/UGame.pas


Ignore:
Timestamp:
Oct 4, 2014, 11:33:16 PM (10 years ago)
Author:
chronos
Message:
  • Added: New game option "Map shape" which can use image in file as base for map generation. Black color is used as void.
  • Modified: New game dialog restructured to separate player, map and rules settings.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r74 r75  
    77uses
    88  Classes, SysUtils, ExtCtrls, Graphics, Contnrs, XMLConf, XMLRead, XMLWrite,
    9   DOM, Math, FileUtil, UXMLUtils;
     9  DOM, Math, FileUtil, UXMLUtils, Dialogs;
    1010
    1111const
     
    9191  end;
    9292
     93  TMapShape = (msRectangle, msImage);
     94
    9395  { TMap }
    9496
     
    107109    DefaultCellSize: TPoint;
    108110    Cells: TObjectList; // TList<TCell>
     111    Shape: TMapShape;
     112    Image: TImage;
     113    function IsOutsideShape(Coord: TPoint): Boolean; virtual;
    109114    function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; virtual;
    110115    function IsValidIndex(Index: TPoint): Boolean; virtual;
     
    266271    FOnWin: TWinEvent;
    267272    FRunning: Boolean;
     273    LoadedImageFileName: string;
    268274    procedure Attack(var AttackPower, DefendPower: Integer);
    269275    procedure MoveAll(Player: TPlayer);
     
    277283    Players: TPlayers;
    278284    Map: TMap;
     285    MapImageFileName: string;
    279286    VoidEnabled: Boolean;
    280287    VoidPercentage: Integer;
     
    596603end;
    597604
     605function TMap.IsOutsideShape(Coord: TPoint): Boolean;
     606var
     607  Rect: TRect;
     608  Color: TColor;
     609  Pos: TPoint;
     610begin
     611  case Shape of
     612    msRectangle: Result := False;
     613    msImage: begin
     614      Rect := GetPixelRect;
     615      with Image.Picture.Bitmap do begin
     616        Pos := Point(Trunc(Coord.X / (Rect.Right - Rect.Left) * Width),
     617          Trunc(Coord.Y / (Rect.Bottom - Rect.Top) * Height));
     618        Color := Canvas.Pixels[Pos.X, Pos.Y];
     619      end;
     620      Result := Color <> clWhite;
     621    end;
     622    else Result := False;
     623  end;
     624end;
     625
    598626procedure TMap.DrawArrow(Canvas: TCanvas; View: TView; Pos: TPoint;
    599627  Angle: Double; Text: string);
     
    655683  Size := Source.Size;
    656684  DefaultCellSize := Source.DefaultCellSize;
     685  Shape := Source.Shape;
    657686  //FSize := Source.Size;
    658687
     
    683712  DefaultCellSize.Y := ReadInteger(Node, 'DefaultCellSizeY', 1);
    684713  MaxPower := ReadInteger(Node, 'MaxPower', 99);
     714  Shape := TMapShape(ReadInteger(Node, 'Shape', Integer(msRectangle)));
    685715end;
    686716
     
    694724  WriteInteger(Node, 'DefaultCellSizeY', DefaultCellSize.Y);
    695725  WriteInteger(Node, 'MaxPower', MaxPower);
     726  WriteInteger(Node, 'Shape', Integer(Shape));
    696727  NewNode := Node.OwnerDocument.CreateElement('Cells');
    697728  Node.AppendChild(NewNode);
     
    828859  Cells := TObjectList.create;
    829860  Size := Point(0, 0);
     861  Image := TImage.Create(nil);
    830862end;
    831863
    832864destructor TMap.Destroy;
    833865begin
     866  Image.Free;
    834867  Size := Point(0, 0);
    835868  FreeAndNil(Cells);
     
    16911724  with Config do begin
    16921725    SetValue(Path + '/GridType', Integer(MapType));
     1726    SetValue(Path + '/MapImage', MapImageFileName);
    16931727    SetValue(Path + '/SymetricMap', SymetricMap);
    16941728    SetValue(Path + '/VoidEnabled', VoidEnabled);
     
    16961730    SetValue(Path + '/MapSizeX', Map.Size.X);
    16971731    SetValue(Path + '/MapSizeY', Map.Size.Y);
     1732    SetValue(Path + '/MapShape', Integer(Map.Shape));
    16981733    SetValue(Path + '/CityEnabled', CityEnabled);
    16991734    SetValue(Path + '/CityPercentage', CityPercentage);
     
    17101745  with Config do begin
    17111746    MapType := TMapType(GetValue(Path + '/GridType', Integer(mtHexagon)));
     1747    MapImageFileName := GetValue(Path + '/MapImage', MapImageFileName);
    17121748    SymetricMap := GetValue(Path + '/SymetricMap', False);
    17131749    VoidEnabled := GetValue(Path + '/VoidEnabled', True);
     
    17151751    Map.Size := Point(GetValue(Path + '/MapSizeX', 10),
    17161752      GetValue(Path + '/MapSizeY', 10));
     1753    Map.Shape := TMapShape(GetValue(Path + '/MapShape', 0));
    17171754    CityEnabled := GetValue(Path + '/CityEnabled', False);
    17181755    CityPercentage := GetValue(Path + '/CityPercentage', 10);
     
    19111948  Players := TPlayers.Create;
    19121949
     1950  MapImageFileName := 'Images/Maps/WorldMap.png';
     1951
    19131952  Randomize;
    19141953
     
    19521991  TurnCounter := 1;
    19531992  Moves.Clear;
     1993  if (Map.Shape = msImage) and FileExists(MapImageFileName) and
     1994  (LoadedImageFileName <> MapImageFileName) then begin
     1995    LoadedImageFileName := MapImageFileName;
     1996    Map.Image.Picture.LoadFromFile(MapImageFileName);
     1997  end;
    19541998  AllCells := Map.GetAllCells;
    19551999  for C := 0 to Length(AllCells) - 1 do
    19562000  with AllCells[C] do begin
    1957     if VoidEnabled and (Random < VoidPercentage / 100) then Terrain := ttVoid
     2001    if (VoidEnabled and (Random < VoidPercentage / 100)) or
     2002    (Map.IsOutsideShape(PosPx)) then Terrain := ttVoid
    19582003      else begin
    19592004        if CityEnabled and (Random < CityPercentage / 100) then Terrain := ttCity
     
    19802025      StartCell := nil;
    19812026      Counter := 0;
    1982       while not Assigned(StartCell) or Assigned(StartCell.Player) do begin
     2027      while not Assigned(StartCell) or Assigned(StartCell.Player) or
     2028     (StartCell.Terrain = ttVoid) do begin
    19832029        StartCell := AllCells[Random(Length(AllCells))];
    19842030        Inc(Counter);
Note: See TracChangeset for help on using the changeset viewer.