Changeset 154 for trunk/UGame.pas
- Timestamp:
- Nov 16, 2017, 12:16:36 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r153 r154 204 204 procedure ComputePlayerStats; virtual; 205 205 procedure Generate; virtual; 206 procedure Clear; 206 207 constructor Create; virtual; 207 208 destructor Destroy; override; … … 284 285 procedure ClearMovesFromCell(Cell: TCell); 285 286 procedure MoveAll; 287 procedure ReduceMovesPower; 286 288 procedure CheckCounterMove(Move: TUnitMove); 287 289 procedure SetMode(AValue: TPlayerMode); 288 290 function SetMove(CellFrom, CellTo: TCell; Power: Integer; Confirmation: Boolean = True): TUnitMove; 289 291 procedure UpdateRepeatMoves; 292 procedure RemoveEmptyUnitMoves; 290 293 public 291 294 Id: Integer; … … 441 444 procedure CalculatePlayersDistance; 442 445 procedure PropagatePlayerDistance(List: TCells); 446 procedure InitDefaultPlayers; 443 447 public 444 448 Players: TPlayers; … … 474 478 constructor Create; 475 479 destructor Destroy; override; 480 procedure Clear; 476 481 procedure New; 477 482 procedure EndGame(Winner: TPlayer = nil); … … 963 968 Move: TUnitMove; 964 969 CellText: string; 970 CellLink: TCellLink; 965 971 begin 966 972 with Canvas, View do … … 971 977 Pen.Style := psSolid; 972 978 Pen.Width := 3; 973 for C := 0 to Player.Game.Map.CellLinks.Count - 1do974 with TCellLink(Player.Game.Map.CellLinks[C])do begin979 for CellLink in Player.Game.Map.CellLinks do 980 with CellLink do begin 975 981 if Length(Points) >= 2 then begin 976 982 MoveTo(View.CellToCanvasPos(Points[0])); … … 981 987 982 988 // Draw cells 983 for C := 0 to Cells.Count - 1 do begin 984 Cell := TPlayerCell(Cells[C]); 989 for Cell in Cells do begin 985 990 if (Cell.MapCell.Terrain <> ttVoid) and Cell.MapCell.IsVisible(View) then begin 986 991 if Cell.MapCell.Player = Player then … … 1014 1019 // Draw arrows 1015 1020 Pen.Color := clCream; 1016 for I := 0 to Player.Moves.Count - 1 do begin 1017 Move := TUnitMove(Player.Moves[I]); 1021 for Move in Player.Moves do begin 1018 1022 PosFrom := Player.Game.Map.CellToPos(Move.CellFrom); 1019 1023 PosTo := Player.Game.Map.CellToPos(Move.CellTo); … … 1236 1240 with Config do begin 1237 1241 NewCount := GetValue(DOMString(Path + '/Count'), -1); 1242 NewPlayerId := 1; 1238 1243 if NewCount >= 2 then begin 1239 1244 Self.Clear; … … 1241 1246 for I := 0 to Count - 1 do begin 1242 1247 Items[I] := TPlayer.Create; 1248 TPlayer(Items[I]).Id := GetNewPlayerId; 1243 1249 TPlayer(Items[I]).Game := Game; 1244 1250 TPlayer(Items[I]).LoadConfig(Config, Path + '/Player' + IntToStr(I)); … … 1604 1610 end; 1605 1611 1612 procedure TMap.Clear; 1613 begin 1614 CellLinks.Clear; 1615 Cells.Clear; 1616 end; 1617 1606 1618 constructor TMap.Create; 1607 1619 begin … … 1760 1772 raise Exception.Create(SNegativeCellPowerNotAllowed); 1761 1773 FPower := AValue; 1774 //Check; 1762 1775 end; 1763 1776 … … 2023 2036 var 2024 2037 NewNode: TDOMNode; 2038 Move: TUnitMove; 2025 2039 begin 2026 2040 Id := ReadInteger(Node, 'Id', 0); … … 2311 2325 procedure TComputer.IncreaseMoves; 2312 2326 var 2313 I: Integer;2327 Move: TUnitMove; 2314 2328 AvailPower: Integer; 2315 2329 begin 2316 2330 // If available power remains then use all for existed unit moves 2317 for I := 0 to Player.Moves.Count - 1do2318 with TUnitMove(Player.Moves[I])do begin2331 for Move in Player.Moves do 2332 with Move do begin 2319 2333 if CellFrom.GetAvialPower > 0 then begin 2320 2334 AvailPower := CellFrom.GetAvialPower; … … 2470 2484 destructor TPlayer.Destroy; 2471 2485 begin 2486 Client := nil; 2472 2487 FreeAndNil(Computer); 2473 2488 FreeAndNil(TurnStats); … … 2645 2660 if CountOnce > CellFrom.Power then 2646 2661 UnitCount := CellFrom.Power; 2662 CountOnce := 0; 2647 2663 if CellTo.Player = Self then begin 2648 2664 // Inner move … … 2665 2681 end; 2666 2682 CellFrom.Power := CellFrom.Power - UnitCount; 2667 CountOnce := 0; 2668 end; 2669 end; 2670 end; 2671 2672 // Remove empty moves 2673 for I := Moves.Count - 1 downto 0 do 2674 if (TUnitMove(Moves[I]).CellFrom.Player = Self) and 2675 (TUnitMove(Moves[I]).CountOnce = 0) and (TUnitMove(Moves[I]).CountRepeat = 0) then 2676 Moves.Delete(I); 2683 end; 2684 end; 2685 end; 2686 2687 RemoveEmptyUnitMoves; 2688 end; 2689 2690 procedure TPlayer.ReduceMovesPower; 2691 var 2692 UnitMove: TUnitMove; 2693 Power: Integer; 2694 begin 2695 // Power of cell can be reduced by unsucessful enemy attack 2696 for UnitMove in Moves do begin 2697 Power := UnitMove.CellFrom.GetAvialPower; 2698 if Power < 0 then begin 2699 if Abs(Power) <= UnitMove.CountOnce then 2700 UnitMove.CountOnce := UnitMove.CountOnce - Abs(Power) 2701 end; 2702 end; 2677 2703 end; 2678 2704 … … 2740 2766 // Add new move 2741 2767 if (CountOnce > 0) or (CountRepeat > 0) then begin 2742 NewMove := TUnitMove (Moves[Moves.Add(TUnitMove.Create)]);2768 NewMove := TUnitMove.Create; 2743 2769 NewMove.List := Moves; 2744 2770 NewMove.CellFrom := CellFrom; … … 2746 2772 NewMove.CountOnce := CountOnce; 2747 2773 NewMove.CountRepeat := CountRepeat; 2774 Moves.Add(NewMove); 2748 2775 Result := NewMove; 2749 2776 CheckCounterMove(NewMove); … … 2783 2810 else CountOnce := CellFrom.GetAvialPower; 2784 2811 end; 2812 RemoveEmptyUnitMoves; 2813 end; 2814 2815 procedure TPlayer.RemoveEmptyUnitMoves; 2816 var 2817 I: Integer; 2818 begin 2819 // Remove empty moves 2820 for I := Moves.Count - 1 downto 0 do 2821 if (TUnitMove(Moves[I]).CellFrom.Player = Self) and 2822 (TUnitMove(Moves[I]).CountOnce = 0) and (TUnitMove(Moves[I]).CountRepeat = 0) then 2823 Moves.Delete(I); 2785 2824 end; 2786 2825 … … 2844 2883 procedure TGame.BuildTerrain; 2845 2884 var 2846 C : Integer;2885 Cell: TCell; 2847 2886 begin 2848 2887 if (Map.Shape = msImage) and FileExists(MapImageFileName) and … … 2853 2892 2854 2893 // Randomize map terrain 2855 for C := 0 to Map.Cells.Count - 1do2856 with TCell(Map.Cells[C])do begin2894 for Cell in Map.Cells do 2895 with Cell do begin 2857 2896 if (VoidEnabled and (Random < VoidPercentage / 100)) or 2858 2897 (Map.IsOutsideShape(PosPx)) then Terrain := ttVoid … … 2954 2993 procedure TGame.InitClients; 2955 2994 var 2956 I: Integer; 2995 Client: TClient; 2996 Player: TPlayer; 2957 2997 begin 2958 2998 Clients.Clear; 2959 2999 Clients.New('Spectator'); 2960 for I := 0 to Players.Count - 1 do 2961 with TPlayer(Players[I]) do 3000 3001 for Player in Players do 3002 with Player do 2962 3003 if Mode = pmHuman then begin 2963 Clients.New( TPlayer(Players[I]).Name);2964 TPlayer(Players[I]).Client := TClient(Clients.Last);2965 end; 2966 2967 for I := 0 to Clients.Count - 1do2968 with TClient(Clients[I])do begin3004 Clients.New(Player.Name); 3005 Player.Client := TClient(Clients.Last); 3006 end; 3007 3008 for Client in Clients do 3009 with Client do begin 2969 3010 View.Clear; 2970 3011 View.Zoom := 1; … … 3041 3082 PropagatePlayerDistance(NeighborList); 3042 3083 FreeAndNil(NeighborList); 3084 end; 3085 3086 procedure TGame.InitDefaultPlayers; 3087 begin 3088 Players.Clear; 3089 Players.New(SPlayer + ' 1', clBlue, pmHuman); 3090 Players.New(SPlayer + ' 2', clRed, pmComputer); 3043 3091 end; 3044 3092 … … 3103 3151 RootNode: TDOMNode; 3104 3152 I: Integer; 3153 Move: TUnitMove; 3105 3154 begin 3106 3155 Self.FileName := FileName; 3156 Clear; 3107 3157 ReadXMLFile(Doc, FileName); 3108 3158 with Doc do try … … 3136 3186 3137 3187 InitClients; 3138 3139 3188 Map.Cells.FixRefId; 3140 3189 … … 3277 3326 CheckWinObjective; 3278 3327 CurrentPlayer.PlayerMap.CheckVisibility; 3328 CurrentPlayer.ReduceMovesPower; 3279 3329 // For computers take view from previous human 3280 3330 //if CurrentPlayer.Mode = pmComputer then CurrentPlayer.View.Assign(PrevPlayer.View); … … 3323 3373 Randomize; 3324 3374 3325 Players.New(SPlayer + ' 1', clBlue, pmHuman);3326 Players.New(SPlayer + ' 2', clRed, pmComputer);3327 3328 3375 VoidEnabled := True; 3329 3376 VoidPercentage := 20; … … 3342 3389 end; 3343 3390 3391 procedure TGame.Clear; 3392 begin 3393 Clients.Clear; 3394 Players.Clear; 3395 Map.Clear; 3396 end; 3397 3344 3398 procedure TGame.New; 3345 3399 var … … 3349 3403 Player: TPlayer; 3350 3404 begin 3405 Clear; 3351 3406 FileName := SNewGameFile; 3352 3407 TurnCounter := 1; 3353 3408 3409 Map.Generate; 3354 3410 BuildTerrain; 3355 3411 3356 3412 // Build bridges 3357 Map.CellLinks.Clear;3358 3413 if BridgeEnabled then begin 3359 3414 BuildMapAreas; … … 3375 3430 end; 3376 3431 3432 InitDefaultPlayers; 3377 3433 for Player in Players do Player.StartCell := nil; 3378 3434 I := 0; 3379 3435 for Player in Players do 3380 3436 with Player do begin 3381 Clear;3382 3437 PlayerMap.Update; 3383 3438 if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin … … 3414 3469 ArrowCenter: TPoint; 3415 3470 Move: TUnitMove; 3471 CellLink: TCellLink; 3416 3472 begin 3417 3473 with Canvas, View do … … 3423 3479 Pen.Style := psSolid; 3424 3480 Pen.Width := 3; 3425 for C := 0 to CellLinks.Count - 1do3426 with TCellLink(CellLinks[C])do begin3481 for CellLink in CellLinks do 3482 with CellLink do begin 3427 3483 if Length(Points) >= 2 then begin 3428 3484 MoveTo(View.CellToCanvasPos(Points[0])); … … 3433 3489 3434 3490 // Draw cells 3435 for C := 0 to Cells.Count - 1 do begin 3436 Cell := TCell(Cells[C]); 3491 for Cell in Cells do begin 3437 3492 if (Cell.Terrain <> ttVoid) and Cell.IsVisible(View) then begin 3438 3493 if Assigned(SelectedCell) and (SelectedCell = Cell) then … … 3449 3504 // Draw arrows 3450 3505 Pen.Color := clCream; 3451 for I := 0 to Game.CurrentPlayer.Moves.Count - 1 do begin 3452 Move := TUnitMove(Game.CurrentPlayer.Moves[I]); 3506 for Move in Game.CurrentPlayer.Moves do begin 3453 3507 PosFrom := CellToPos(Move.CellFrom); 3454 3508 PosTo := CellToPos(Move.CellTo);
Note:
See TracChangeset
for help on using the changeset viewer.