Changeset 82 for trunk/UGame.pas


Ignore:
Timestamp:
Nov 5, 2014, 9:51:30 AM (10 years ago)
Author:
chronos
Message:
  • Added: New game option "Fog of war" which cause map to be covered to black and only explored cells will be visible.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r81 r82  
    218218  TPlayerMap = class
    219219    Cells: TPlayerCells;
     220    Player: TPlayer;
     221    procedure Update;
    220222    constructor Create;
    221223    destructor Destroy; override;
     224    procedure CheckVisibility;
     225    procedure Paint(Canvas: TCanvas; View: TView);
    222226  end;
    223227
     
    278282    procedure SaveToNode(Node: TDOMNode);
    279283    constructor Create;
     284    function GetFirstHuman: TPlayer;
    280285  end;
    281286
     
    354359    MaxNeutralUnits: Integer;
    355360    FileName: string;
     361    FogOfWar: Boolean;
    356362    function AttackProbability(AttackCount, DefendCount, Depth: Integer): Double;
    357363    procedure LoadConfig(Config: TXmlConfig; Path: string);
     
    465471end;
    466472
     473function HalfColor(Color: TColor): TColor;
     474begin
     475  Result :=
     476    ((((Color shr 0) and $ff) shr 1) shl 0) or
     477    ((((Color shr 8) and $ff) shr 1) shl 8) or
     478    ((((Color shr 16) and $ff) shr 1) shl 16) or
     479    ((((Color shr 24) and $ff) shr 0) shl 24);
     480end;
     481
    467482{ TPlayerMap }
     483
     484procedure TPlayerMap.Update;
     485var
     486  I: Integer;
     487  OldCount: Integer;
     488begin
     489  // Update players cells count to map cells count to be 1:1
     490  OldCount := Cells.Count;
     491  Cells.Count := Player.Game.Map.Cells.Count;
     492  for I := OldCount to Cells.Count - 1 do
     493    Cells[I] := TPlayerCell.Create;
     494
     495  for I := 0 to Cells.Count - 1 do
     496  with TPlayerCell(Cells[I]) do begin
     497    Explored := False;
     498    InVisibleRange := False;
     499    MapCell := TCell(Player.Game.Map.Cells[I]);
     500  end;
     501end;
    468502
    469503constructor TPlayerMap.Create;
     
    476510  Cells.Free;
    477511  inherited Destroy;
     512end;
     513
     514procedure TPlayerMap.CheckVisibility;
     515var
     516  I: Integer;
     517  C: Integer;
     518  NeighCount: Integer;
     519begin
     520  for I := 0 to Cells.Count - 1 do
     521  with TPlayerCell(Cells[I]) do begin
     522    NeighCount := 0;
     523    for C := 0 to MapCell.Neighbors.Count - 1 do
     524      if TCell(MapCell.Neighbors[C]).Player = Player then
     525        Inc(NeighCount);
     526
     527    InVisibleRange := (NeighCount > 0) or (TCell(MapCell).Player = Player);
     528    if InVisibleRange and not Explored then Explored := True;
     529  end;
     530end;
     531
     532procedure TPlayerMap.Paint(Canvas: TCanvas; View: TView);
     533var
     534  I: Integer;
     535  C: Integer;
     536  Cell: TPlayerCell;
     537  PosFrom, PosTo: TPoint;
     538  Angle: Double;
     539  ArrowCenter: TPoint;
     540  Move: TUnitMove;
     541  CellText: string;
     542begin
     543  with Canvas, View do
     544  try
     545    Lock;
     546    for C := 0 to Cells.Count - 1 do begin
     547      Cell := TPlayerCell(Cells[C]);
     548      if (Cell.MapCell.Terrain <> ttVoid) and Cell.MapCell.IsVisible(View) then begin
     549        CellText := IntToStr(Cell.MapCell.GetAvialPower);
     550        if Assigned(SelectedCell) and (SelectedCell = Cell.MapCell) then
     551            Brush.Color := clGreen
     552          else if Assigned(SelectedCell) and Player.Game.Map.IsCellsNeighbor(SelectedCell, Cell.MapCell) then
     553            Brush.Color := clPurple
     554          else if Player.Game.FogOfWar then begin
     555            if Cell.InVisibleRange then begin
     556              if not Cell.Explored then begin
     557                Brush.Color := HalfColor(Cell.MapCell.GetColor);
     558                CellText := '';
     559              end else Brush.Color := Cell.MapCell.GetColor
     560            end else begin
     561              Brush.Color := clBlack;
     562              CellText := '';
     563            end;
     564          end else Brush.Color := Cell.MapCell.GetColor;
     565        Player.Game.Map.PaintCell(Canvas, Cell.MapCell.PosPx, CellText, View, Cell.MapCell);
     566      end else
     567      if Game.FogOfWar and (Cell.MapCell.Terrain = ttVoid) and (not Cell.Explored) then begin
     568        Brush.Color := clBlack;
     569        Player.Game.Map.PaintCell(Canvas, Cell.MapCell.PosPx, '', View, Cell.MapCell);
     570      end;
     571    end;
     572
     573    // Draw arrows
     574    Pen.Color := clCream;
     575    for I := 0 to Game.Moves.Count - 1 do begin
     576      Move := TUnitMove(Game.Moves[I]);
     577      PosFrom := Player.Game.Map.CellToPos(Move.CellFrom);
     578      PosTo := Player.Game.Map.CellToPos(Move.CellTo);
     579      if Move.CountRepeat > 0 then Pen.Width := 2
     580        else Pen.Width := 1;
     581      Angle := ArcTan((PosTo.Y - PosFrom.Y) / (PosTo.X - PosFrom.X));
     582      if Sign(PosTo.X - PosFrom.X) = -1 then Angle := Angle + Pi;
     583      ArrowCenter := View.CellToCanvasPos(Point(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
     584        Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)));
     585      Player.Game.Map.DrawArrow(Canvas, View, ArrowCenter,
     586        Angle, IntToStr(Move.CountOnce));
     587    end;
     588  finally
     589    Unlock;
     590  end;
    478591end;
    479592
     
    607720  inherited Create;
    608721  NewPlayerId := 1;
     722end;
     723
     724function TPlayers.GetFirstHuman: TPlayer;
     725var
     726  I: Integer;
     727begin
     728  I := 0;
     729  while (I < Count) and (TPlayer(Items[I]).Mode <> pmHuman) do Inc(I);
     730  if I < Count then Result := TPlayer(Items[I])
     731    else Result := nil;
    609732end;
    610733
     
    17201843procedure TPlayer.Paint(Canvas: TCanvas);
    17211844begin
    1722   Game.Map.Paint(Canvas, View);
     1845  PlayerMap.Paint(Canvas, View);
    17231846end;
    17241847
     
    17291852  StartCell := nil;
    17301853  PlayerMap := TPlayerMap.Create;
     1854  PlayerMap.Player := Self;
    17311855end;
    17321856
     
    20232147    SetValue(Path + '/MapImage', MapImageFileName);
    20242148    SetValue(Path + '/SymetricMap', SymetricMap);
     2149    SetValue(Path + '/FogOfWar', FogOfWar);
    20252150    SetValue(Path + '/VoidEnabled', VoidEnabled);
    20262151    SetValue(Path + '/VoidPercentage', VoidPercentage);
     
    20442169    MapImageFileName := GetValue(Path + '/MapImage', MapImageFileName);
    20452170    SymetricMap := GetValue(Path + '/SymetricMap', False);
     2171    FogOfWar := GetValue(Path + '/FogOfWar', False);
    20462172    VoidEnabled := GetValue(Path + '/VoidEnabled', True);
    20472173    VoidPercentage := GetValue(Path + '/VoidPercentage', 20);
     
    20732199    with RootNode do begin
    20742200      SymetricMap := ReadBoolean(RootNode, 'SymetricMap', False);
     2201      FogOfWar := ReadBoolean(RootNode, 'FogOfWar', False);
    20752202      VoidEnabled := ReadBoolean(RootNode, 'VoidEnabled', False);
    20762203      VoidPercentage := ReadInteger(RootNode, 'VoidPercentage', 0);
     
    21202247    with RootNode do begin
    21212248      WriteBoolean(RootNode, 'SymetricMap', SymetricMap);
     2249      WriteBoolean(RootNode, 'FogOfWar', FogOfWar);
    21222250      WriteBoolean(RootNode, 'VoidEnabled', VoidEnabled);
    21232251      WriteInteger(RootNode, 'VoidPercentage', VoidPercentage);
     
    21942322  Map.Grow(CurrentPlayer);
    21952323  UpdateRepeatMoves(CurrentPlayer);
     2324  CurrentPlayer.PlayerMap.CheckVisibility;
    21962325  ComputePlayerStats;
    21972326  PrevPlayer := CurrentPlayer;
     
    22072336  CheckWinObjective;
    22082337  // For computers take view from previous human
    2209   if CurrentPlayer.Mode = pmComputer then CurrentPlayer.View.Assign(PrevPlayer.View);
     2338  //if CurrentPlayer.Mode = pmComputer then CurrentPlayer.View.Assign(PrevPlayer.View);
    22102339end;
    22112340
     
    22742403var
    22752404  I: Integer;
    2276   StartCell: TCell;
    22772405  Counter: Integer;
    22782406  AllCells: TCellArray;
     
    23012429
    23022430  if SymetricMap then begin
    2303     for C := 0 to Map.Cells.Count div 2 - 1 do begin
     2431    for C := 0 to (Map.Cells.Count div 2) - 1 do begin
    23042432      TCell(Map.Cells[C]).Terrain :=
    23052433        TCell(Map.Cells[Map.Cells.Count - 1 - C]).Terrain;
     
    23112439  for I := 0 to Players.Count - 1 do
    23122440  with TPlayer(Players[I]) do begin
     2441    PlayerMap.Update;
    23132442    View.Clear;
    23142443    if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin
     
    23322461    View.Zoom := 1;
    23332462    View.CenterMap;
     2463    PlayerMap.CheckVisibility;
    23342464  end;
    23352465  if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
Note: See TracChangeset for help on using the changeset viewer.