Changeset 342
- Timestamp:
- Dec 22, 2024, 10:52:26 AM (10 hours ago)
- Location:
- trunk
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Game.pas
r339 r342 6 6 Classes, SysUtils, ExtCtrls, Graphics, XMLConf, XMLRead, XMLWrite, Forms, 7 7 DOM, Math, LazFileUtils, XML, Dialogs, LCLType, LCLIntf, Building, Geometry, 8 Player, Map, MapType, &Unit, GameSystem;8 Player, Map, MapType, Units, GameSystem; 9 9 10 10 const … … 49 49 procedure SetRunning(AValue: Boolean); 50 50 procedure BuildTerrain; 51 procedure PlaceUnits; 51 52 procedure PlaceCities; 52 53 procedure InitPlayers; … … 91 92 procedure LoadFromFile(FileName: string); 92 93 procedure SaveToFile(FileName: string); 94 function ToString: string; override; 93 95 procedure ComputePlayerStats; 94 96 procedure NextPlayer; … … 301 303 (Map.IsOutsideShape(PosPx)) then Terrain := ttVoid 302 304 else Terrain := ttNormal; 305 end; 306 end; 307 308 procedure TGame.PlaceUnits; 309 var 310 Cell: TCell; 311 NewPower: Integer; 312 begin 313 if GameSystem.UnitKinds.Count = 0 then Exit; 314 315 for Cell in Map.Cells do 316 with Cell do begin 303 317 NewPower := Random(MaxNeutralUnits + 1); 304 318 if (NewPower > 0) and not Assigned(OneUnit) then begin … … 348 362 end; 349 363 StartCell.Player := Player; 350 if not Assigned(StartCell.OneUnit) then 351 StartCell.OneUnit := Self.Units.AddNew(TUnitKind(GameSystem.UnitKinds[0]), Player.StartUnits); 352 StartCell.OneUnit.Power := Player.StartUnits; 353 StartCell.OneUnit.Kind := TUnitKind(GameSystem.UnitKinds[0]); 354 StartCell.OneUnit.Player := Player; 364 if GameSystem.UnitKinds.Count > 0 then begin 365 if not Assigned(StartCell.OneUnit) then 366 StartCell.OneUnit := Self.Units.AddNew(TUnitKind(GameSystem.UnitKinds[0]), Player.StartUnits); 367 StartCell.OneUnit.Power := Player.StartUnits; 368 StartCell.OneUnit.Kind := TUnitKind(GameSystem.UnitKinds[0]); 369 StartCell.OneUnit.Player := Player; 370 end; 355 371 end; 356 372 end; … … 694 710 end; 695 711 712 function TGame.ToString: string; 713 begin 714 Result := 'StoredRandSeed: ' + IntToStr(StoredRandSeed) + LineEnding; 715 Result := Result + 'MapType: ' + IntToStr(Integer(MapType)) + LineEnding; 716 Result := Result + 'SymetricMap: ' + BoolToStr(SymetricMap) + LineEnding; 717 Result := Result + 'CyclicMap: ' + BoolToStr(CyclicMap) + LineEnding; 718 Result := Result + 'FogOfWar: ' + BoolToStr(FogOfWar) + LineEnding; 719 Result := Result + 'VoidEnabled: ' + BoolToStr(VoidEnabled) + LineEnding; 720 Result := Result + 'VoidPercentage: ' + IntToStr(VoidPercentage) + LineEnding; 721 Result := Result + 'MaxNeutralUnits: ' + IntToStr(MaxNeutralUnits) + LineEnding; 722 Result := Result + 'MaxPower: ' + IntToStr(MaxPower) + LineEnding; 723 Result := Result + 'GrowCells: ' + IntToStr(Integer(GrowCells)) + LineEnding; 724 Result := Result + 'GrowAmount: ' + IntToStr(Integer(GrowAmount)) + LineEnding; 725 Result := Result + 'CityEnabled: ' + BoolToStr(CityEnabled) + LineEnding; 726 Result := Result + 'CityPercentage: ' + IntToStr(CityPercentage) + LineEnding; 727 Result := Result + 'BridgeEnabled: ' + BoolToStr(BridgeEnabled) + LineEnding; 728 Result := Result + 'TurnCounter: ' + IntToStr(TurnCounter) + LineEnding; 729 Result := Result + 'WinObjective: ' + IntToStr(Integer(WinObjective)) + LineEnding; 730 Result := Result + 'StayAliveForDefinedTurns: ' + IntToStr(StayAliveForDefinedTurns) + LineEnding; 731 Result := Result + 'Running: ' + BoolToStr(Running) + LineEnding; 732 Result := Result + 'GameSystem: ' + LineEnding + GameSystem.ToString + LineEnding; 733 Result := Result + 'Map: ' + LineEnding + Map.ToString + LineEnding; 734 Result := Result + 'Players: ' + LineEnding + Players.ToString + LineEnding; 735 Result := Result + 'Units: ' + LineEnding + Units.ToString + LineEnding; 736 end; 737 696 738 procedure TGame.ComputePlayerStats; 697 739 var … … 905 947 Map.MaxPower := MaxPower; 906 948 BuildTerrain; 949 PlaceUnits; 907 950 PlaceCities; 908 951 WinObjectiveMapPrepare; … … 931 974 function TGame.Compare(Game: TGame): Boolean; 932 975 begin 933 Result := BridgeEnabled = Game.BridgeEnabled; 976 Result := (StoredRandSeed = Game.StoredRandSeed) and 977 (DevelMode = Game.DevelMode) and 978 Players.Compare(Game.Players) and 979 (MapType = Game.MapType) and 980 Map.Compare(Game.Map) and 981 (MapImageFileName = Game.MapImageFileName) and 982 (VoidEnabled = Game.VoidEnabled) and 983 (VoidPercentage = Game.VoidPercentage) and 984 (SymetricMap = Game.SymetricMap) and 985 (CyclicMap = Game.CyclicMap) and 986 (GrowCells = Game.GrowCells) and 987 (GrowAmount = Game.GrowAmount) and 988 (CityEnabled = Game.CityEnabled) and 989 (CityPercentage = Game.CityPercentage) and 990 (TurnCounter = Game.TurnCounter) and 991 (WinObjective = Game.WinObjective) and 992 (SpecialCaptureCellCount = Game.SpecialCaptureCellCount) and 993 (StayAliveForDefinedTurns = Game.StayAliveForDefinedTurns) and 994 (MaxNeutralUnits = Game.MaxNeutralUnits) and 995 (FileName = Game.FileName) and 996 (FogOfWar = Game.FogOfWar) and 997 (BridgeEnabled = Game.BridgeEnabled) and 998 (MaxPower = Game.MaxPower) and 999 GameSystem.Compare(Game.GameSystem); 934 1000 end; 935 1001 -
trunk/GameSystem.pas
r339 r342 4 4 5 5 uses 6 Classes, SysUtils, Generics.Collections, &Unit, DOM, XMLRead, XMLWrite, XML,6 Classes, SysUtils, Generics.Collections, Units, DOM, XMLRead, XMLWrite, XML, 7 7 XMLConf, FileUtil, LazFileUtils, MapType, Nation, Building, ItemList, UnitKind; 8 8 … … 27 27 function GetName: string; 28 28 procedure Assign(Source: TGameSystem); 29 function Compare(GameSystem: TGameSystem): Boolean; 29 30 procedure LoadFromNode(Node: TDOMNode); 30 31 procedure SaveToNode(Node: TDOMNode); … … 133 134 BuildingKinds.Assign(Source.BuildingKinds); 134 135 Nations.Assign(Source.Nations); 136 end; 137 138 function TGameSystem.Compare(GameSystem: TGameSystem): Boolean; 139 begin 140 Result := (FileName = GameSystem.FileName) and 141 (UnitsMoveImmediately = GameSystem.UnitsMoveImmediately) and 142 (MaxPlayerCount = GameSystem.MaxPlayerCount) and 143 (UnitsSplitMerge = GameSystem.UnitsSplitMerge) and 144 (EmptyCellsNeutral = GameSystem.EmptyCellsNeutral) and 145 (PreferedMapType = GameSystem.PreferedMapType); // and 146 //(UnitKinds.Assign(GameSystem.UnitKinds) and 147 //(BuildingKinds.Assign(GameSystem.BuildingKinds) and 148 //(Nations.Assign(GameSystem.Nations); 135 149 end; 136 150 -
trunk/ItemList.pas
r336 r342 41 41 private 42 42 procedure AssignValue(Source: TItem; Field: TItemField); 43 function CompareValue(Item: TItem; Field: TItemField): Boolean; 43 44 procedure LoadValueFromNode(Node: TDOMNode; Field: TItemField); virtual; 44 45 procedure SaveValueToNode(Node: TDOMNode; Field: TItemField); virtual; … … 64 65 procedure SetValueReference(Index: Integer; Value: TItem); 65 66 procedure Assign(Source: TItem); virtual; 67 function Compare(Item: TItem): Boolean; virtual; 68 function ToString: string; override; 66 69 procedure LoadFromNode(Node: TDOMNode); virtual; 67 70 procedure SaveToNode(Node: TDOMNode); virtual; … … 88 91 function FindByName(Name: string): TItem; 89 92 function GetNewId: Integer; 93 function ToString: string; override; 90 94 procedure Assign(Source: TItemList); virtual; 95 function Compare(ItemList: TItemList): Boolean; virtual; 91 96 function AddItem(Name: string = ''): TItem; virtual; 92 97 function CreateItem(Name: string = ''): TItem; virtual; … … 150 155 end; 151 156 157 function TItemList.Compare(ItemList: TItemList): Boolean; 158 var 159 I: Integer; 160 begin 161 Result := Count = ItemList.Count; 162 if not Result then Exit; 163 for I := 0 to Count - 1 do begin 164 Result := Result and TItem(Items[I]).Compare(ItemList.Items[I]); 165 if not Result then Break; 166 end; 167 end; 168 152 169 function TItemList.AddItem(Name: string): TItem; 153 170 begin … … 269 286 end; 270 287 288 function TItemList.ToString: string; 289 var 290 I: Integer; 291 begin 292 Result := ''; 293 for I := 0 to Count - 1 do 294 with TItem(Items[I]) do begin 295 Result := Result + ToString + LineEnding; 296 end; 297 end; 298 271 299 { TItemFields } 272 300 … … 312 340 if Field.DataType = dtReference then begin 313 341 SetValueReference(Field.Index, Source.GetValueReference(Field.Index)); 342 end else 343 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); 344 end; 345 346 function TItem.CompareValue(Item: TItem; Field: TItemField): Boolean; 347 begin 348 if Field.DataType = dtString then begin 349 Result := GetValueString(Field.Index) = Item.GetValueString(Field.Index); 350 end else 351 if Field.DataType = dtColor then begin 352 Result := GetValueColor(Field.Index) = Item.GetValueColor(Field.Index); 353 end else 354 if Field.DataType = dtInteger then begin 355 Result := GetValueInteger(Field.Index) = Item.GetValueInteger(Field.Index); 356 end else 357 if Field.DataType = dtBoolean then begin 358 Result := GetValueBoolean(Field.Index) = Item.GetValueBoolean(Field.Index); 359 end else 360 if Field.DataType = dtEnumeration then begin 361 Result := GetValueEnumeration(Field.Index) = Item.GetValueEnumeration(Field.Index); 362 end else 363 if Field.DataType = dtReference then begin 364 Result := GetValueReference(Field.Index) = Item.GetValueReference(Field.Index); 314 365 end else 315 366 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); … … 503 554 end; 504 555 556 function TItem.Compare(Item: TItem): Boolean; 557 var 558 I: Integer; 559 Fields: TItemFields; 560 begin 561 Result := True; 562 Result := Result and (Id = Item.Id); 563 if Item is ClassType then begin 564 Fields := GetFields; 565 try 566 for I := 0 to Fields.Count - 1 do begin 567 Result := Result and CompareValue(Item, Fields[I]); 568 if not Result then Break; 569 end; 570 finally 571 Fields.Free; 572 end; 573 end; 574 end; 575 576 function TItem.ToString: string; 577 var 578 Fields: TItemFields; 579 I: Integer; 580 begin 581 Result := 'Id: ' + IntToStr(Id) + LineEnding; 582 Fields := GetFields; 583 try 584 for I := 0 to Fields.Count - 1 do begin 585 Result := Result + Fields[I].SysName + ': ' + GetValueAsText(Fields[I].Index) + LineEnding; 586 end; 587 finally 588 Fields.Free; 589 end; 590 end; 591 505 592 procedure TItem.LoadFromNode(Node: TDOMNode); 506 593 var -
trunk/Map.pas
r338 r342 5 5 uses 6 6 Classes, SysUtils, Graphics, ExtCtrls, Geometry, DOM, Generics.Collections, 7 Generics.Defaults, Building, XML, &Unit;7 Generics.Defaults, Building, XML, Units; 8 8 9 9 const … … 162 162 function IsValidIndex(Index: TPoint): Boolean; virtual; 163 163 procedure Assign(Source: TMap); virtual; 164 function Compare(Map: TMap): Boolean; 164 165 procedure LoadFromFile(FileName: string); virtual; 165 166 procedure SaveToFile(FileName: string); virtual; … … 539 540 end; 540 541 542 function TMap.Compare(Map: TMap): Boolean; 543 begin 544 Result := True; 545 end; 546 541 547 procedure TMap.LoadFromFile(FileName: string); 542 548 begin -
trunk/Player.pas
r339 r342 5 5 uses 6 6 Classes, SysUtils, Graphics, Map, DOM, Generics.Collections, Generics.Defaults, 7 XMLConf, XML, Math, Geometry, &Unit, Nation, ItemList, TurnStats;7 XMLConf, XML, Math, Geometry, Units, Nation, ItemList, TurnStats; 8 8 9 9 type -
trunk/Tests.pas
r336 r342 17 17 end; 18 18 19 { TTestCaseMap } 20 21 TTestCaseMap = class(TTestCase) 22 Game: TGame; 23 procedure Run; override; 24 constructor Create; override; 25 destructor Destroy; override; 26 end; 27 19 28 function GetTestCases: TTestCases; 20 29 21 30 22 31 implementation 32 33 uses 34 Geometry, MapType, Map; 23 35 24 36 function GetTestCases: TTestCases; … … 27 39 with Result do begin 28 40 with TTestCaseGame(AddNew('Load and save', TTestCaseGame)) do begin 29 41 end; 42 with TTestCaseMap(AddNew('Map cells connection', TTestCaseMap)) do begin 30 43 end; 31 44 end; … … 44 57 Game2.LoadFromFile(FileName); 45 58 Evaluate(Game2.Compare(Game)); 59 Log := 'Game1: ' + LineEnding + Game.ToString + LineEnding + 60 'Game2: ' + LineEnding + Game2.ToString + LineEnding; 46 61 Game2.Free; 47 62 end; … … 59 74 end; 60 75 76 { TTestCaseMap } 77 78 procedure TTestCaseMap.Run; 79 var 80 X, Y: Integer; 81 Connected: Boolean; 82 Cell: TCell; 83 Expected: Integer; 84 const 85 Size = 10; 86 begin 87 Connected := True; 88 Game.Map.Size := TPoint.Create(Size, Size); 89 Game.MapType := mtSquare; 90 Game.New; 91 for Y := 0 to Size - 1 do 92 for X := 0 to Size - 1 do begin 93 Cell := Game.Map.Cells[X + Y * Size]; 94 Expected := 4; 95 if (X = 0) or (X = Size - 1) then Dec(Expected); 96 if (Y = 0) or (Y = Size - 1) then Dec(Expected); 97 if Cell.Neighbors.Count <> Expected then begin 98 Log := Log + 'Cell ' + IntToStr(X + Y * Size) + ' expected neighbors count ' + 99 IntToStr(Expected) + ' but is ' + IntToStr(Cell.Neighbors.Count) + LineEnding; 100 if not Connected then Break; 101 end; 102 end; 103 Evaluate(Connected); 104 end; 105 106 constructor TTestCaseMap.Create; 107 begin 108 inherited; 109 Game := TGame.Create; 110 end; 111 112 destructor TTestCaseMap.Destroy; 113 begin 114 FreeAndNil(Game); 115 inherited; 116 end; 117 61 118 end. 62 119 -
trunk/Units.pas
r341 r342 1 unit &Unit;1 unit Units; 2 2 3 3 interface -
trunk/xtactics.lpi
r340 r342 260 260 </Unit26> 261 261 <Unit27> 262 <Filename Value="Unit.pas"/> 263 <IsPartOfProject Value="True"/> 264 <UnitName Value="&Unit"/> 262 <Filename Value="Units.pas"/> 263 <IsPartOfProject Value="True"/> 265 264 </Unit27> 266 265 <Unit28>
Note:
See TracChangeset
for help on using the changeset viewer.