Changeset 158 for trunk/UGame.pas
- Timestamp:
- Nov 19, 2017, 1:02:02 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r157 r158 98 98 99 99 TCellLink = class 100 Allocated: Boolean;101 100 Points: array of TPoint; 102 101 Cells: TCells; … … 184 183 procedure SetSize(AValue: TPoint); virtual; 185 184 protected 185 FPixelRect: TRect; 186 186 FNewCellId: Integer; 187 187 function GetNewCellId: Integer; virtual; … … 216 216 destructor Destroy; override; 217 217 procedure Paint(Canvas: TCanvas; View: TView); virtual; 218 function GetPixelRect: TRect; virtual;218 function CalculatePixelRect: TRect; virtual; 219 219 procedure ForEachCells(Method: TMethod); virtual; 220 220 property Size: TPoint read GetSize write SetSize; 221 property PixelRect: TRect read FPixelRect; 221 222 end; 222 223 … … 315 316 Moves: TUnitMoves; 316 317 Computer: TComputer; 318 function IsAlive: Boolean; 317 319 procedure Clear; 318 320 procedure LoadFromNode(Node: TDOMNode); … … 350 352 351 353 TPlayers = class(TFPGObjectList<TPlayer>) 354 public 352 355 Game: TGame; 353 356 NewPlayerId: Integer; 357 function GetAliveCount: Integer; 354 358 function FindById(Id: Integer): TPlayer; 355 359 procedure New(Name: string; Color: TColor; Mode: TPlayerMode); … … 362 366 procedure LoadConfig(Config: TXmlConfig; Path: string); 363 367 procedure SaveConfig(Config: TXmlConfig; Path: string); 368 function GetAlivePlayers: TPlayerArray; 369 function GetAlivePlayersWithCities: TPlayerArray; 364 370 end; 365 371 … … 478 484 procedure SaveToFile(FileName: string); 479 485 procedure ComputePlayerStats; 480 function GetAlivePlayers: TPlayerArray;481 function GetAlivePlayersWithCities: TPlayerArray;482 486 procedure NextTurn; 483 487 procedure CheckWinObjective; … … 732 736 constructor TCellLink.Create; 733 737 begin 734 Allocated := True;735 738 Cells := TCells.Create; 736 739 Cells.FreeObjects := False; … … 739 742 destructor TCellLink.Destroy; 740 743 var 741 Links: TCellLinks;742 FirstLink: TCellLink;743 744 I: Integer; 744 745 begin … … 746 747 if TCell(Cells[I]).Neighbors.Remove(TCell(Cells[1 - I])) = -1 then 747 748 raise Exception.Create(SCellRemoveNeighborError); 748 Links := TCell(Cells[I]).Links;749 if Links.Count > 0 then750 FirstLink := TCellLink(Links[0]);751 749 if TCell(Cells[I]).Links.Remove(Self) = -1 then 752 750 raise Exception.Create(SCellRemoveNeighborError); 753 751 end; 754 752 FreeAndNil(Cells); 755 Allocated := False;756 753 inherited Destroy; 757 754 end; … … 1159 1156 { TPlayers } 1160 1157 1158 function TPlayers.GetAliveCount: Integer; 1159 var 1160 Player: TPlayer; 1161 begin 1162 Result := 0; 1163 for Player in Self do 1164 if Player.IsAlive then Inc(Result); 1165 end; 1166 1161 1167 function TPlayers.FindById(Id: Integer): TPlayer; 1162 1168 var … … 1354 1360 msRectangle: Result := False; 1355 1361 msImage: begin 1356 Rect := GetPixelRect;1362 Rect := PixelRect; 1357 1363 with Image.Picture.Bitmap do begin 1358 1364 Pos := Point(Trunc(Coord.X / (Rect.Right - Rect.Left) * Width), … … 1363 1369 end; 1364 1370 msRounded: begin 1365 Rect := GetPixelRect;1371 Rect := PixelRect; 1366 1372 Center := Point(Rect.Left + (Rect.Right - Rect.Left) div 2, 1367 1373 Rect.Top + (Rect.Bottom - Rect.Top) div 2); … … 1594 1600 procedure TMap.ComputePlayerStats; 1595 1601 var 1596 I: Integer;1597 begin 1598 for I := 0 to Cells.Count - 1do1599 with TCell(Cells[I])do begin1602 Cell: TCell; 1603 begin 1604 for Cell in Cells do 1605 with Cell do begin 1600 1606 if Assigned(Player) then begin 1601 1607 Player.TotalCells := Player.TotalCells + 1; … … 1621 1627 NewCell := TCell.Create; 1622 1628 NewCell.Map := Self; 1623 NewCell.PosPx := Point(X ,Y);1629 NewCell.PosPx := Point(X * DefaultCellSize.X, Y * DefaultCellSize.Y); 1624 1630 NewCell.Id := GetNewCellId; 1631 SetLength(NewCell.Polygon, 1); 1632 NewCell.Polygon[0] := NewCell.PosPx; 1625 1633 Cells[Y * FSize.X + X] := NewCell; 1626 1634 end; 1635 FPixelRect := CalculatePixelRect; 1627 1636 end; 1628 1637 … … 1729 1738 end; 1730 1739 1731 function TMap. GetPixelRect: TRect;1740 function TMap.CalculatePixelRect: TRect; 1732 1741 var 1733 1742 I: Integer; … … 2539 2548 MapRect: TRect; 2540 2549 begin 2541 MapRect := Game.Map. GetPixelRect;2550 MapRect := Game.Map.PixelRect; 2542 2551 SourceRect := Bounds(MapRect.Left + (MapRect.Right - MapRect.Left) div 2 - (SourceRect.Right - SourceRect.Left) div 2, 2543 2552 MapRect.Top + (MapRect.Bottom - MapRect.Top) div 2 - (SourceRect.Bottom - SourceRect.Top) div 2, … … 2912 2921 end; 2913 2922 2923 function TPlayer.IsAlive: Boolean; 2924 begin 2925 Result := (TotalCells > 0) and Assigned(StartCell); 2926 end; 2927 2914 2928 procedure TPlayer.CheckCounterMove(Move: TUnitMove); 2915 2929 var … … 3014 3028 with TMapArea(Areas[I]) do begin 3015 3029 GetBorderCells(BorderList); 3016 for J := 0 to 1do begin3030 for J := 0 to 4 do begin 3017 3031 3018 3032 Cell := TCell(BorderList[Random(BorderList.Count)]); … … 3086 3100 View.Clear; 3087 3101 View.Zoom := 1; 3088 if Assigned(ControlPlayer) then View.CenterPlayerCity(ControlPlayer) 3102 if Assigned(ControlPlayer) and Assigned(ControlPlayer.StartCell) then 3103 View.CenterPlayerCity(ControlPlayer) 3089 3104 else View.CenterMap; 3090 3105 end; … … 3096 3111 Cell: TCell; 3097 3112 List: TCells; 3113 I: Integer; 3098 3114 begin 3099 3115 with Player do begin … … 3112 3128 List.FreeObjects := False; 3113 3129 Map.Cells.GetCellsWithWeight(List, Round(LongestDistance * 0.6), Round(LongestDistance * 0.8)); 3114 StartCell := List[Random(List.Count)]; 3130 3131 // Remove cells already allocated to different player 3132 for I := List.Count - 1 downto 0 do 3133 if Assigned(TCell(List[I]).Player) then 3134 List.Delete(I); 3135 3136 if List.Count > 0 then 3137 StartCell := List[Random(List.Count)]; 3115 3138 finally 3116 3139 FreeAndNil(List); … … 3353 3376 end; 3354 3377 3355 function T Game.GetAlivePlayers: TPlayerArray;3356 var 3357 I: Integer;3378 function TPlayers.GetAlivePlayers: TPlayerArray; 3379 var 3380 Player: TPlayer; 3358 3381 begin 3359 3382 SetLength(Result, 0); 3360 for I := 0 to Players.Count - 1do3361 if TPlayer(Players[I]).TotalCells > 0then begin3383 for Player in Self do 3384 if Player.IsAlive then begin 3362 3385 SetLength(Result, Length(Result) + 1); 3363 Result[Length(Result) - 1] := TPlayer(Players[I]);3364 end; 3365 end; 3366 3367 function T Game.GetAlivePlayersWithCities: TPlayerArray;3368 var 3369 I: Integer;3386 Result[Length(Result) - 1] := Player; 3387 end; 3388 end; 3389 3390 function TPlayers.GetAlivePlayersWithCities: TPlayerArray; 3391 var 3392 Player: TPlayer; 3370 3393 begin 3371 3394 SetLength(Result, 0); 3372 for I := 0 to Players.Count - 1do3373 if TPlayer(Players[I]).TotalCities > 0 then begin3395 for Player in Self do 3396 if Player.TotalCities > 0 then begin 3374 3397 SetLength(Result, Length(Result) + 1); 3375 Result[Length(Result) - 1] := TPlayer(Players[I]);3398 Result[Length(Result) - 1] := Player; 3376 3399 end; 3377 3400 end; … … 3392 3415 if Assigned(FOnPlayerChange) then 3393 3416 FOnPlayerChange(Self); 3394 until CurrentPlayer. TotalCells > 0;3417 until CurrentPlayer.IsAlive; 3395 3418 if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then begin 3396 3419 Inc(TurnCounter); … … 3413 3436 Winner := nil; 3414 3437 if WinObjective = woDefeatAllOponents then begin 3415 AlivePlayers := GetAlivePlayers;3438 AlivePlayers := Players.GetAlivePlayers; 3416 3439 if (Length(AlivePlayers) <= 1) then begin 3417 3440 if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]); … … 3420 3443 end else 3421 3444 if WinObjective = woDefeatAllOponentsCities then begin 3422 AlivePlayers := GetAlivePlayersWithCities;3445 AlivePlayers := Players.GetAlivePlayersWithCities; 3423 3446 if (Length(AlivePlayers) <= 1) then begin 3424 3447 if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]); … … 3503 3526 if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin 3504 3527 SelectPlayerStartCell(Player); 3505 if SymetricMap and (I = 1) then 3506 StartCell := TCell(Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)]); 3507 3508 StartCell.Terrain := ttCity; 3509 StartCell.Player := Player; 3510 StartCell.Power := Player.StartUnits; 3528 if Assigned(Player.StartCell) then begin 3529 if SymetricMap and (I = 1) then 3530 StartCell := TCell(Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)]); 3531 3532 StartCell.Terrain := ttCity; 3533 StartCell.Player := Player; 3534 StartCell.Power := Player.StartUnits; 3535 end; 3511 3536 end; 3512 3537 PlayerMap.CheckVisibility; … … 3517 3542 3518 3543 InitClients; 3544 ComputePlayerStats; 3519 3545 end; 3520 3546
Note:
See TracChangeset
for help on using the changeset viewer.