Changeset 193 for trunk/UGame.pas


Ignore:
Timestamp:
May 14, 2018, 5:02:00 PM (6 years ago)
Author:
chronos
Message:
  • Added: Isometric map type.
  • Modified: Removed unneeded explicit typecast for list items.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r191 r193  
    1212const
    1313  DefaultPlayerStartUnits = 5;
    14   HexCellMulX = 1.12;
    15   HexCellMulY = 1.292;
    1614  SquareCellMulX = 1.05;
    1715  SquareCellMulY = 1.05;
     
    414412  TGrowAmount = (gaByOne, gaBySquareRoot);
    415413  TGrowCells = (gcNone, gcPlayerCities, gcPlayerAll);
    416   TMapType = (mtNone, mtHexagon, mtSquare, mtTriangle, mtVoronoi);
     414  TMapType = (mtNone, mtHexagon, mtSquare, mtTriangle, mtVoronoi, mtIsometric);
    417415  TWinObjective = (woDefeatAllOponents, woDefeatAllOponentsCities,
    418416    woSpecialCaptureCell, woStayAliveForDefinedTurns);
     
    636634begin
    637635  for I := 0 to Cells.Count - 1 do begin
    638     if TCell(Cells[I]).Neighbors.Remove(TCell(Cells[1 - I])) = -1 then
     636    if Cells[I].Neighbors.Remove(Cells[1 - I]) = -1 then
    639637      raise Exception.Create(SCellRemoveNeighborError);
    640     if TCell(Cells[I]).Links.Remove(Self) = -1 then
     638    if Cells[I].Links.Remove(Self) = -1 then
    641639      raise Exception.Create(SCellRemoveNeighborError);
    642640  end;
     
    756754begin
    757755  I := 0;
    758   while (I < Count) and (TPlayerCell(Items[I]).MapCell <> Cell) do Inc(I);
    759   if I < Count then Result := TPlayerCell(Items[I])
     756  while (I < Count) and (Items[I].MapCell <> Cell) do Inc(I);
     757  if I < Count then Result := Items[I]
    760758    else Result := nil;
    761759end;
     
    785783    NewNode := Node.OwnerDocument.CreateElement('Cell');
    786784    Node.AppendChild(NewNode);
    787     TPlayerCell(Items[I]).SaveToNode(NewNode);
     785    Items[I].SaveToNode(NewNode);
    788786  end;
    789787end;
     
    825823
    826824  for I := 0 to Cells.Count - 1 do
    827   with TPlayerCell(Cells[I]) do begin
     825  with Cells[I] do begin
    828826    List := Cells;
    829827    Explored := False;
    830828    InVisibleRange := False;
    831     MapCell := TCell(Player.Game.Map.Cells[I]);
     829    MapCell := Player.Game.Map.Cells[I];
    832830  end;
    833831end;
     
    852850begin
    853851  for I := 0 to Cells.Count - 1 do
    854   with TPlayerCell(Cells[I]) do begin
     852  with Cells[I] do begin
    855853    NeighCount := 0;
    856854    for C := 0 to MapCell.Neighbors.Count - 1 do
    857       if TCell(MapCell.Neighbors[C]).Player = Player then
     855      if MapCell.Neighbors[C].Player = Player then
    858856        Inc(NeighCount);
    859857
    860     InVisibleRange := (NeighCount > 0) or (TCell(MapCell).Player = Player);
     858    InVisibleRange := (NeighCount > 0) or (MapCell.Player = Player);
    861859    if InVisibleRange and not Explored then Explored := True;
    862860  end;
     
    999997begin
    1000998  for I := 0 to Count - 1 do
    1001   TCell(Items[I]).FixRefId;
     999    Items[I].FixRefId;
    10021000end;
    10031001
     
    10071005begin
    10081006  I := 0;
    1009   while (I < Count) and (TCell(Items[I]).Id <> Id) do Inc(I);
    1010   if I < Count then Result := TCell(Items[I])
     1007  while (I < Count) and (Items[I].Id <> Id) do Inc(I);
     1008  if I < Count then Result := Items[I]
    10111009    else Result := nil;
    10121010end;
     
    10441042begin
    10451043  for I := 0 to Count - 1 do
    1046   with TCell(Items[I]) do begin
     1044  with Items[I] do begin
    10471045    NewNode2 := Node.OwnerDocument.CreateElement('Cell');
    10481046    Node.AppendChild(NewNode2);
     
    10901088begin
    10911089  I := 0;
    1092   while (I < Count) and (TPlayer(Items[I]).Id <> Id) do Inc(I);
    1093   if I < Count then Result := TPlayer(Items[I])
     1090  while (I < Count) and (Items[I].Id <> Id) do Inc(I);
     1091  if I < Count then Result := Items[I]
    10941092    else Result := nil;
    10951093end;
     
    11401138    NewNode := Node.OwnerDocument.CreateElement('Player');
    11411139    Node.AppendChild(NewNode);
    1142     TPlayer(Items[I]).SaveToNode(NewNode);
     1140    Items[I].SaveToNode(NewNode);
    11431141  end;
    11441142end;
     
    11551153begin
    11561154  I := 0;
    1157   while (I < Count) and (TPlayer(Items[I]).Mode <> pmHuman) do Inc(I);
    1158   if I < Count then Result := TPlayer(Items[I])
     1155  while (I < Count) and (Items[I].Mode <> pmHuman) do Inc(I);
     1156  if I < Count then Result := Items[I]
    11591157    else Result := nil;
    11601158end;
     
    11691167    Add(TPlayer.Create);
    11701168  for I := 0 to Count - 1 do begin
    1171     TPlayer(Items[I]).Assign(TPlayer(Source[I]));
    1172     TPlayer(Items[I]).Game := Game;
     1169    Items[I].Assign(Source[I]);
     1170    Items[I].Game := Game;
    11731171  end;
    11741172  NewPlayerId := Source.NewPlayerId;
     
    11881186      for I := 0 to Count - 1 do begin
    11891187        Items[I] := TPlayer.Create;
    1190         TPlayer(Items[I]).Id := GetNewPlayerId;
    1191         TPlayer(Items[I]).Game := Game;
    1192         TPlayer(Items[I]).LoadConfig(Config, Path + '/Player' + IntToStr(I));
     1188        Items[I].Id := GetNewPlayerId;
     1189        Items[I].Game := Game;
     1190        Items[I].LoadConfig(Config, Path + '/Player' + IntToStr(I));
    11931191      end;
    11941192    end;
     
    12011199begin
    12021200  for I := 0 to Count - 1 do
    1203     TPlayer(Items[I]).SaveConfig(Config, Path + '/Player' + IntToStr(I));
     1201    Items[I].SaveConfig(Config, Path + '/Player' + IntToStr(I));
    12041202  with Config do begin
    12051203    SetValue(DOMString(Path + '/Count'), Count);
     
    13851383  for I := 0 to Cells.Count - 1 do begin
    13861384    Cells[I] := TCell.Create;
    1387     TCell(Cells[I]).Map := Self;
    1388     TCell(Cells[I]).Assign(TCell(Source.Cells[I]));
     1385    Cells[I].Map := Self;
     1386    Cells[I].Assign(Source.Cells[I]);
    13891387  end;
    13901388  }
     
    14421440  Result := nil;
    14431441  for I := 0 to Cells.Count - 1 do
    1444   if TCell(Cells[I]).Terrain <> ttVoid then begin
    1445     if TCell(Cells[I]).Polygon.IsPointInside(Pos) then begin
    1446       Result := TCell(Cells[I]);
     1442  if Cells[I].Terrain <> ttVoid then begin
     1443    if Cells[I].Polygon.IsPointInside(Pos) then begin
     1444      Result := Cells[I];
    14471445      Exit;
    14481446    end;
     
    14621460begin
    14631461  for I := 0 to Cells.Count - 1 do
    1464   with TCell(Cells[I]) do begin
     1462  with Cells[I] do begin
    14651463    if (Player = APlayer) and ((Game.GrowCells = gcPlayerAll) or
    14661464    ((Game.GrowCells = gcPlayerCities) and (Terrain = ttCity))) then begin
     
    15881586  // Generic way to create two sides symetric map independent to shape
    15891587  for C := 0 to (Cells.Count div 2) - 1 do begin
    1590     TCell(Cells[C]).Terrain := TCell(Cells[Cells.Count - 1 - C]).Terrain;
    1591     TCell(Cells[C]).Power := TCell(Cells[Cells.Count - 1 - C]).Power;
    1592 
    1593     for I := TCell(Cells[C]).Links.Count - 1 downto 0 do begin
    1594       CellLink := TCell(Cells[C]).Links[I];
     1588    Cells[C].Terrain := Cells[Cells.Count - 1 - C].Terrain;
     1589    Cells[C].Power := Cells[Cells.Count - 1 - C].Power;
     1590
     1591    for I := Cells[C].Links.Count - 1 downto 0 do begin
     1592      CellLink := Cells[C].Links[I];
    15951593
    15961594      // Remove cells on first half of the map
     
    16241622  for C := 0 to (Cells.Count div 2) - 1 do begin
    16251623    // Make copy of links from second half
    1626     OppositeCell := TCell(Cells[Cells.Count - 1 - C]);
     1624    OppositeCell := Cells[Cells.Count - 1 - C];
    16271625    for CellLink in OppositeCell.Links do
    16281626      if (Cells.IndexOf(CellLink.Cells[0]) > (Cells.Count div 2)) and
     
    16871685  // This is generic algorithm to determine pixel size of entire map
    16881686  for I := 0 to Cells.Count - 1 do begin
    1689     CellRect := TCell(Cells[I]).Polygon.GetRect;
     1687    CellRect := Cells[I].Polygon.GetRect;
    16901688    if I = 0 then Result := CellRect
    16911689      else begin
     
    19351933    NewNode2 := NewNode.OwnerDocument.CreateElement('Neighbour');
    19361934    NewNode.AppendChild(NewNode2);
    1937     WriteInteger(NewNode2, 'Id', TCell(Neighbors[I]).Id);
     1935    WriteInteger(NewNode2, 'Id', Neighbors[I].Id);
    19381936  end;
    19391937  NewNode := Node.OwnerDocument.CreateElement('Polygon');
     
    20282026  FreeAndNil(Links);
    20292027  for I := Neighbors.Count - 1 downto 0 do
    2030     if TCell(Neighbors[I]).Neighbors.Remove(Self) = -1 then
     2028    if Neighbors[I].Neighbors.Remove(Self) = -1 then
    20312029      raise Exception.Create(SCellRemoveNeighborError);
    20322030  FreeAndNil(Neighbors);
     
    24352433  // Get list of all enemy border cells
    24362434  for C := 0 to AllCells.Count - 1 do
    2437   with TCell(AllCells[C]) do begin
     2435  with AllCells[C] do begin
    24382436    if (Player <> Game.CurrentPlayer) and (Player <> nil) and (Terrain <> ttVoid) then begin
    24392437      CanAttack := 0;
    24402438      for I := 0 to Neighbors.Count - 1 do
    2441       if ((TCell(Neighbors[I]).Player = Game.CurrentPlayer) or
    2442       (TCell(Neighbors[I]).Player = nil)) and (TCell(Neighbors[I]).Terrain <> ttVoid) then begin
     2439      if ((Neighbors[I].Player = Game.CurrentPlayer) or
     2440      (Neighbors[I].Player = nil)) and (Neighbors[I].Terrain <> ttVoid) then begin
    24432441        Inc(CanAttack);
    24442442      end;
     
    24582456    // Set mark for selected border cells
    24592457    for C := 0 to TargetCells.Count - 1 do
    2460       TCell(TargetCells[C]).Mark := True;
     2458      TargetCells[C].Mark := True;
    24612459
    24622460    // Move all power from unmarked cells and mark them
    24632461    NewTargetCells.Count := 0;
    24642462    for C := 0 to TargetCells.Count - 1 do
    2465     with TCell(TargetCells[C]) do begin
     2463    with TargetCells[C] do begin
    24662464      for I := 0 to Neighbors.Count - 1 do begin
    2467         if (TCell(Neighbors[I]).Terrain <> ttVoid) and (not TCell(Neighbors[I]).Mark) then begin
    2468           if (TCell(TargetCells[C]).Player = Game.CurrentPlayer) and
    2469           (TCell(Neighbors[I]).Player = Game.CurrentPlayer) then begin
     2465        if (Neighbors[I].Terrain <> ttVoid) and (not Neighbors[I].Mark) then begin
     2466          if (TargetCells[C].Player = Game.CurrentPlayer) and
     2467          (Neighbors[I].Player = Game.CurrentPlayer) then begin
    24702468            // Do not take units from front line
    2471             Cells2 := TCell(Neighbors[I]).Neighbors;
     2469            Cells2 := Neighbors[I].Neighbors;
    24722470            CanAttack := 0;
    24732471            for J := 0 to Cells2.Count - 1 do
    2474             if ((TCell(Cells2[J]).Player <> Game.CurrentPlayer) or (TCell(Cells2[J]).Player = nil))
    2475             and (TCell(Cells2[J]).Terrain <> ttVoid) then begin
     2472            if ((Cells2[J].Player <> Game.CurrentPlayer) or (Cells2[J].Player = nil))
     2473            and (Cells2[J].Terrain <> ttVoid) then begin
    24762474              Inc(CanAttack);
    24772475            end;
    24782476            if CanAttack = 0 then begin
    2479               MovedPower := TCell(Neighbors[I]).GetAvialPower;
    2480               if (TCell(TargetCells[C]).GetAvialPower + TCell(TargetCells[C]).GetAttackPower + MovedPower) > Game.Map.MaxPower then
    2481                 MovedPower := Game.Map.MaxPower - TCell(TargetCells[C]).GetAvialPower - TCell(TargetCells[C]).GetAttackPower;
    2482               Player.SetMove(TCell(Neighbors[I]), TCell(TargetCells[C]), MovedPower, False);
     2477              MovedPower := Neighbors[I].GetAvialPower;
     2478              if (TargetCells[C].GetAvialPower + TargetCells[C].GetAttackPower + MovedPower) > Game.Map.MaxPower then
     2479                MovedPower := Game.Map.MaxPower - TargetCells[C].GetAvialPower - TargetCells[C].GetAttackPower;
     2480              Player.SetMove(Neighbors[I], TargetCells[C], MovedPower, False);
    24832481            end;
    24842482          end;
    2485           TCell(Neighbors[I]).Mark := True;
    2486           NewTargetCells.Add(TCell(Neighbors[I]));
     2483          Neighbors[I].Mark := True;
     2484          NewTargetCells.Add(Neighbors[I]);
    24872485        end;
    24882486      end;
     
    25392537  // Get list of border cells
    25402538  for C := 0 to AllCells.Count - 1 do
    2541   with TCell(AllCells[C]) do begin
     2539  with AllCells[C] do begin
    25422540    if (Terrain <> ttVoid) and (Player = Game.CurrentPlayer)  then begin
    2543       if AttackersCount(TCell(AllCells[C])) > 0 then
     2541      if AttackersCount(AllCells[C]) > 0 then
    25442542        BorderCells.Add(AllCells[C]);
    25452543    end;
     
    25482546  // Move all units back to inner area from weak border cells
    25492547  for C := 0 to BorderCells.Count - 1 do
    2550   with TCell(BorderCells[C]) do begin
     2548  with BorderCells[C] do begin
    25512549    // Calculate enemy power
    25522550    // TODO: Do not sum different enemy power to one value
    25532551    EnemyPower := 0;
    25542552    for I := 0 to Neighbors.Count - 1 do
    2555     if (TCell(Neighbors[I]).Player <> Game.CurrentPlayer) and (TCell(Neighbors[I]).Player <> nil) then begin
    2556       Inc(EnemyPower, TCell(Neighbors[I]).Power);
     2553    if (Neighbors[I].Player <> Game.CurrentPlayer) and (Neighbors[I].Player <> nil) then begin
     2554      Inc(EnemyPower, Neighbors[I].Power);
    25572555    end;
    25582556    if EnemyPower > (GetAvialPower + GetAttackPower) then begin
     
    25612559        Player.Moves.Remove(MovesTo[I]);
    25622560      for I := 0 to Neighbors.Count - 1 do
    2563       if (TCell(Neighbors[I]).Player = Player) and (AttackersCount(TCell(Neighbors[I])) = 0) then begin
    2564         Player.SetMove(TCell(BorderCells[C]), TCell(Neighbors[I]), GetAvialPower, False);
     2561      if (Neighbors[I].Player = Player) and (AttackersCount(Neighbors[I]) = 0) then begin
     2562        Player.SetMove(BorderCells[C], Neighbors[I], GetAvialPower, False);
    25652563        Break;
    25662564      end;
     
    25772575  Result := 0;
    25782576  for I := 0 to Cell.Neighbors.Count - 1 do
    2579   if (TCell(Cell.Neighbors[I]).Player <> Game.CurrentPlayer) and
    2580     (TCell(Cell.Neighbors[I]).Player <> nil) then begin
     2577  if (Cell.Neighbors[I].Player <> Game.CurrentPlayer) and
     2578    (Cell.Neighbors[I].Player <> nil) then begin
    25812579      Inc(Result);
    25822580  end;
     
    28282826
    28292827  for C := 0 to List.Count - 1 do
    2830   with TCell(List[C]) do begin
     2828  with List[C] do begin
    28312829    for I := 0 to Neighbors.Count - 1 do
    2832     with TCell(Neighbors[I]) do
     2830    with Neighbors[I] do
    28332831    if (not Mark) and (Terrain <> ttVoid) and (Area <> SourceArea) and ((DestArea = nil) or (DestArea = Area)) then begin
    2834       NewList.Add(TCell(TCell(List[C]).Neighbors[I]));
     2832      NewList.Add(List[C].Neighbors[I]);
    28352833      Mark := True;
    28362834    end else
    28372835    if (not Mark) and (Terrain = ttVoid) then begin
    2838       NewListVoid.Add(TCell(TCell(List[C]).Neighbors[I]));
     2836      NewListVoid.Add(List[C].Neighbors[I]);
    28392837      Mark := True;
    28402838    end;
     
    28432841  if NewList.Count > 0 then begin
    28442842    // We found cell with different area
    2845     Result := TCell(NewList[Random(NewList.Count)]);
     2843    Result := NewList[Random(NewList.Count)];
    28462844  end else
    28472845  if NewListVoid.Count > 0 then begin
     
    28782876    for J := 0 to 4 do begin
    28792877
    2880     Cell := TCell(BorderList[Random(BorderList.Count)]);
     2878    Cell := BorderList[Random(BorderList.Count)];
    28812879    List.Clear;
    28822880    List.Add(Cell);
     
    29132911begin
    29142912  for C := 0 to Cells.Count - 1 do
    2915   with TCell(Cells[C]) do
     2913  with Cells[C] do
    29162914    Area := nil;
    29172915  Areas.Clear;
    29182916  for C := 0 to Cells.Count - 1 do
    2919   with TCell(Cells[C]) do
     2917  with Cells[C] do
    29202918  if (Terrain <> ttVoid) and (not Assigned(Area)) then begin
    29212919    NewArea := TMapArea.Create;
     
    30603058    mtTriangle: Map := TTriangleMap.Create;
    30613059    mtVoronoi: Map := TVoronoiMap.Create;
     3060    mtIsometric: Map := TIsometricMap.Create;
    30623061    else Map := TMap.Create;
    30633062  end;
     
    31283127      // Remove cells already allocated to different player
    31293128      for I := List.Count - 1 downto 0 do
    3130         if Assigned(TCell(List[I]).Player) then
     3129        if Assigned(List[I].Player) then
    31313130          List.Delete(I);
    31323131
     
    32773276      if Assigned(NewNode) then
    32783277        Players.LoadFromNode(NewNode);
    3279       if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
     3278      if Players.Count > 0 then CurrentPlayer := Players[0]
    32803279        else CurrentPlayer := nil;
    32813280
     
    32833282
    32843283      for I := 0 to Players.Count - 1 do begin
    3285         TPlayer(Players[I]).PlayerMap.Update;
    3286         TPlayer(Players[I]).PlayerMap.CheckVisibility;
     3284        Players[I].PlayerMap.Update;
     3285        Players[I].PlayerMap.CheckVisibility;
    32873286      end;
    32883287      Running := ReadBoolean(RootNode, 'Running', True);
     
    33413340begin
    33423341  for I := 0 to Players.Count - 1 do
    3343   with TPlayer(Players[I]) do begin
     3342  with Players[I] do begin
    33443343    TotalUnits := 0;
    33453344    TotalCells := 0;
     
    33483347    TotalDiscovered := 0;
    33493348    for J := 0 to PlayerMap.Cells.Count - 1 do
    3350     with TPlayerCell(PlayerMap.Cells[J]) do begin
     3349    with PlayerMap.Cells[J] do begin
    33513350      if Explored then Inc(TotalDiscovered);
    33523351    end;
     
    33623361begin
    33633362  for I := 0 to Players.Count - 1 do
    3364   with TPlayer(Players[I]) do begin
     3363  with Players[I] do begin
    33653364    NewStat := TGameTurnStat.Create;
    33663365    NewStat.DiscoveredCells := TotalDiscovered;
     
    33843383  // Skip dead players
    33853384  repeat
    3386     CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
     3385    CurrentPlayer := Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count];
    33873386    if Assigned(FOnPlayerChange) then
    33883387      FOnPlayerChange(Self);
     
    34123411    AlivePlayers := Players.GetAlivePlayers;
    34133412    if (Length(AlivePlayers) <= 1) then begin
    3414       if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]);
     3413      if Length(AlivePlayers) > 0 then Winner := AlivePlayers[0];
    34153414      EndGame(Winner);
    34163415    end;
     
    34193418    AlivePlayers := Players.GetAlivePlayersWithCities;
    34203419    if (Length(AlivePlayers) <= 1) then begin
    3421       if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]);
     3420      if Length(AlivePlayers) > 0 then Winner := AlivePlayers[0];
    34223421      EndGame(Winner);
    34233422    end;
     
    34983497      if Assigned(Player.StartCell) then begin
    34993498        if SymetricMap and (I = 1) then
    3500           StartCell := TCell(Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)]);
     3499          StartCell := Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(Players[0].StartCell)];
    35013500
    35023501        StartCell.Terrain := ttCity;
     
    35083507    Inc(I);
    35093508  end;
    3510   if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
     3509  if Players.Count > 0 then CurrentPlayer := Players[0]
    35113510    else CurrentPlayer := nil;
    35123511
Note: See TracChangeset for help on using the changeset viewer.