Changeset 265 for trunk/UGame.pas
- Timestamp:
- Jan 15, 2019, 1:03:40 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r263 r265 8 8 Classes, SysUtils, ExtCtrls, Graphics, XMLConf, XMLRead, XMLWrite, Forms, 9 9 DOM, Math, LazFileUtils, UXMLUtils, Dialogs, LCLType, LCLIntf, fgl, 10 UGeometry, UPlayer, UMap, UMapType ;10 UGeometry, UPlayer, UMap, UMapType, UUnit, UGameSystem; 11 11 12 12 const 13 13 DefaultPlayerStartUnits = 5; 14 MinPlayerCount = 1; 14 15 MaxPlayerCount = 8; 15 16 … … 28 29 TGrowCells = (gcNone, gcPlayerCities, gcPlayerAll); 29 30 TMapType = (mtNone, mtHexagon, mtSquare, mtTriangle, mtRandom, mtIsometric); 30 TWinObjective = (wo DefeatAllOponents, woDefeatAllOponentsCities,31 woSpecialCaptureCell, woStayAliveForDefinedTurns );31 TWinObjective = (woNone, woDefeatAllOponents, woDefeatAllOponentsCities, 32 woSpecialCaptureCell, woStayAliveForDefinedTurns, woCaptureEntireMap); 32 33 33 34 { TGame } … … 35 36 TGame = class 36 37 private 38 FGameSystem: TGameSystem; 37 39 FMapType: TMapType; 38 40 FOnChange: TNotifyEvent; … … 46 48 ProbabilityMatrix: array of array of Single; 47 49 procedure RecordTurnStats; 50 procedure SetGameSystem(AValue: TGameSystem); 48 51 procedure SetMapType(AValue: TMapType); 49 52 procedure SetRunning(AValue: Boolean); … … 57 60 DevelMode: Boolean; 58 61 Players: TPlayers; 62 Units: TUnits; 59 63 Map: TMap; 60 64 MapImageFileName: string; … … 77 81 BridgeEnabled: Boolean; 78 82 MaxPower: Integer; 79 EmptyCellsNeutral: Boolean;80 83 procedure PostConfig; 81 84 procedure Assign(Source: TGame); … … 95 98 property Running: Boolean read FRunning write SetRunning; 96 99 property MapType: TMapType read FMapType write SetMapType; 100 property GameSystem: TGameSystem read FGameSystem write SetGameSystem; 97 101 published 98 102 property OnMoveUpdated: TMoveUpdatedEvent read FOnMoveUpdated write FOnMoveUpdated; … … 123 127 124 128 resourcestring 125 SMinimumPlayers = 'You need at least two players';129 SMinimumPlayers = 'You need at least one player'; 126 130 SHuman = 'Human'; 127 131 SComputer = 'Computer'; … … 263 267 if FRunning = AValue then Exit; 264 268 if AValue then begin 265 if Players.Count < 2then raise Exception.Create(SMinimumPlayers);269 if Players.Count < 1 then raise Exception.Create(SMinimumPlayers); 266 270 FRunning := AValue; 267 271 end else begin … … 284 288 else Terrain := ttNormal; 285 289 end; 286 Power := Random(MaxNeutralUnits + 1); 290 if not Assigned(OneUnit) then 291 OneUnit := Units.AddNew(GameSystem.UnitKinds[0], Random(MaxNeutralUnits + 1)); 287 292 Player := nil; 288 293 end; … … 428 433 BridgeEnabled := Source.BridgeEnabled; 429 434 MaxPower := Source.MaxPower; 430 EmptyCellsNeutral := Source.EmptyCellsNeutral;435 GameSystem := Source.GameSystem; 431 436 end; 432 437 … … 452 457 SetValue(DOMString(Path + '/StayAliveForDefinedTurns'), StayAliveForDefinedTurns); 453 458 SetValue(DOMString(Path + '/SpecialCaptureCellCount'), SpecialCaptureCellCount); 454 SetValue(DOMString(Path + '/EmptyCellsNeutral'), EmptyCellsNeutral);455 459 Players.SaveConfig(Config, Path + '/Players'); 456 460 end; … … 488 492 StayAliveForDefinedTurns := GetValue(DOMString(Path + '/StayAliveForDefinedTurns'), 20); 489 493 SpecialCaptureCellCount := GetValue(DOMString(Path + '/SpecialCaptureCellCount'), 1); 490 EmptyCellsNeutral := GetValue(DOMString(Path + '/EmptyCellsNeutral'), False);491 494 Players.LoadConfig(Config, Path + '/Players'); 492 495 end; … … 524 527 WinObjective := TWinObjective(ReadInteger(RootNode, 'WinObjective', Integer(woDefeatAllOponents))); 525 528 StayAliveForDefinedTurns := ReadInteger(RootNode, 'StayAliveForDefinedTurns', 10); 526 EmptyCellsNeutral := ReadBoolean(RootNode, 'EmptyCellsNeutral', False); 529 530 NewNode := FindNode('GameSystem'); 531 if Assigned(NewNode) then 532 GameSystem.LoadFromNode(NewNode); 527 533 528 534 NewNode := FindNode('Map'); … … 536 542 else CurrentPlayer := nil; 537 543 544 NewNode := FindNode('Units'); 545 if Assigned(NewNode) then 546 Units.LoadFromNode(NewNode); 547 538 548 Map.Cells.FixRefId; 549 Units.FixRefId; 539 550 540 551 for I := 0 to Players.Count - 1 do begin … … 578 589 WriteInteger(RootNode, 'WinObjective', Integer(WinObjective)); 579 590 WriteInteger(RootNode, 'StayAliveForDefinedTurns', StayAliveForDefinedTurns); 580 WriteBoolean(RootNode, 'EmptyCellsNeutral', EmptyCellsNeutral);581 591 WriteBoolean(RootNode, 'Running', Running); 592 593 NewNode := OwnerDocument.CreateElement('GameSystem'); 594 AppendChild(NewNode); 595 GameSystem.SaveToNode(NewNode); 582 596 583 597 NewNode := OwnerDocument.CreateElement('Map'); … … 588 602 AppendChild(NewNode); 589 603 Players.SaveToNode(NewNode); 604 605 NewNode := OwnerDocument.CreateElement('Units'); 606 AppendChild(NewNode); 607 Units.SaveToNode(NewNode); 590 608 end; 591 609 if ExtractFileDir(FileName) <> '' then … … 633 651 TurnStats.Add(NewStat); 634 652 end; 653 end; 654 655 procedure TGame.SetGameSystem(AValue: TGameSystem); 656 begin 657 if FGameSystem = AValue then Exit; 658 FGameSystem := AValue; 635 659 end; 636 660 … … 687 711 R: Boolean; 688 712 I: Integer; 713 PlayersCount: Integer; 689 714 begin 690 715 Winner := nil; … … 724 749 if TurnCounter > StayAliveForDefinedTurns then 725 750 EndGame(nil); 751 end else 752 if WinObjective = woCaptureEntireMap then begin 753 Player := nil; 754 for I := 0 to Map.Cells.Count - 1 do 755 if TCell(Map.Cells[I]).Terrain <> ttVoid then begin 756 if (TCell(Map.Cells[I]).Player <> nil) then begin 757 if Player = nil then begin 758 // First player found 759 Player := TPlayer(TCell(Map.Cells[I]).Player); 760 end else 761 if Player <> TCell(Map.Cells[I]).Player then begin 762 // Multiple players still alive 763 Player := nil; 764 Break; 765 end; 766 end else begin 767 // Neutral cell 768 Player := nil; 769 Break; 770 end; 771 end; 772 if Player <> nil then 773 EndGame(Player); 726 774 end; 727 775 end; … … 729 777 constructor TGame.Create; 730 778 begin 779 Units := TUnits.Create; 780 Units.Game := Self; 731 781 Map := TMap.Create; 782 Map.Game := Self; 732 783 Players := TPlayers.Create; 733 784 Players.Game := Self; … … 742 793 MaxNeutralUnits := Min(4, MaxPower); 743 794 744 Map.Game := Self;745 795 Map.Size := TPoint.Create(3, 3); 746 796 end; … … 750 800 FreeAndNil(Players); 751 801 FreeAndNil(Map); 802 FreeAndNil(Units); 752 803 inherited Destroy; 753 804 end; … … 784 835 end; 785 836 786 for Player in Players do Player.StartCell := nil; 837 for Player in Players do begin 838 Player.Reset; 839 Player.StartCell := nil; 840 end; 787 841 I := 0; 788 842 for Player in Players do … … 797 851 StartCell.Terrain := ttCity; 798 852 StartCell.Player := Player; 799 StartCell.Power := Player.StartUnits; 853 if not Assigned(StartCell.OneUnit) then 854 StartCell.OneUnit := Units.AddNew(GameSystem.UnitKinds[0], Player.StartUnits); 855 StartCell.OneUnit.Power := Player.StartUnits; 856 StartCell.OneUnit.Kind := GameSystem.UnitKinds[0]; 800 857 end; 801 858 end;
Note:
See TracChangeset
for help on using the changeset viewer.