Changeset 407
- Timestamp:
- Jan 8, 2025, 10:18:12 AM (37 hours ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Building.pas
r344 r407 39 39 public 40 40 Kind: TBuildingKind; 41 Game: TObject; // TGame 42 class function GetFields: TItemFields; override; 43 procedure GetValue(Index: Integer; out Value); override; 44 procedure SetValue(Index: Integer; var Value); override; 45 function GetReferenceList(Index: Integer): TBaseItemList; override; 41 46 procedure Assign(Source: TItem); override; 42 47 property MapCell: TObject read FMapCell write SetMapCell; // TMapCell; … … 47 52 TBuildings = class(TItemList<TBuilding>) 48 53 Game: TObject; // TGame; 54 function CreateItem(Name: string = ''): TBuilding; override; 55 procedure Assign(Source: TItemList<TBuilding>); override; 49 56 end; 50 57 … … 64 71 65 72 uses 66 Map ;73 Map, Game; 67 74 68 75 { TBuilding } … … 80 87 end; 81 88 89 class function TBuilding.GetFields: TItemFields; 90 var 91 Field: TItemField; 92 begin 93 Result := inherited; 94 Field := Result.AddField(2, 'Kind', SBuildingKind, dtReference); 95 end; 96 97 procedure TBuilding.GetValue(Index: Integer; out Value); 98 begin 99 if Index = 1 then string(Value) := Name 100 else if Index = 2 then TBuildingKind(Value) := Kind 101 else inherited; 102 end; 103 104 procedure TBuilding.SetValue(Index: Integer; var Value); 105 begin 106 if Index = 1 then Name := string(Value) 107 else if Index = 2 then Kind := TBuildingKind(Value) 108 else inherited; 109 end; 110 111 function TBuilding.GetReferenceList(Index: Integer): TBaseItemList; 112 begin 113 if Index = 2 then Result := TGame(Game).GameSystem.BuildingKinds.BaseItemList 114 else Result := nil; 115 end; 116 82 117 procedure TBuilding.Assign(Source: TItem); 83 118 begin 84 119 inherited; 85 120 Kind := TBuilding(Source).Kind; 121 end; 122 123 { TBuildings } 124 125 function TBuildings.CreateItem(Name: string): TBuilding; 126 begin 127 Result := inherited; 128 Result.Game := Game; 129 end; 130 131 procedure TBuildings.Assign(Source: TItemList<TBuilding>); 132 var 133 I: Integer; 134 begin 135 inherited; 136 for I := 0 to Count - 1 do 137 Items[I].Game := Game; 86 138 end; 87 139 -
trunk/Core.pas
r406 r407 284 284 if not Assigned(Game.CurrentPlayer) then 285 285 Game.CurrentPlayer := Game.Players.GetFirstHuman; 286 if Assigned(Game.CurrentPlayer) then begin286 if Assigned(Game.CurrentPlayer) and (Game.CurrentPlayer.Mode = pmHuman) then begin 287 287 FormMain.FormClient.Client := TClientGUI(LocalClients.SearchPlayer(Game.CurrentPlayer)); 288 288 end else begin -
trunk/Game.pas
r399 r407 58 58 procedure InitDefaultPlayers; 59 59 procedure WinObjectiveMapPrepare; 60 procedure BuildCity(Cell: TCell); 60 61 public 61 62 GameSystem: TGameSystem; … … 342 343 with Cell do begin 343 344 if (Terrain = ttNormal) and CityEnabled and (Random < CityPercentage / 100) then begin 344 Building := Buildings.AddItem('City'); 345 Building.Kind := GameSystem.BuildingKinds.FindBySpecialType(stCity); 345 BuildCity(Cell); 346 346 end; 347 347 end; … … 369 369 StartCell := Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(Players[0].StartCell)]; 370 370 371 if CityEnabled then begin 372 StartCell.Building := Buildings.AddItem('City'); 373 StartCell.Building.Kind := GameSystem.BuildingKinds.FindBySpecialType(stCity); 374 end; 371 if CityEnabled then BuildCity(StartCell); 375 372 StartCell.Player := Player; 376 373 if GameSystem.UnitKinds.Count > 0 then begin … … 497 494 end; 498 495 496 procedure TGame.BuildCity(Cell: TCell); 497 var 498 CityBuildingKind: TBuildingKind; 499 begin 500 CityBuildingKind := GameSystem.BuildingKinds.FindBySpecialType(stCity); 501 if not Assigned(CityBuildingKind) then begin 502 CityBuildingKind := GameSystem.BuildingKinds.AddItem('City'); 503 CityBuildingKind.SpecialType := stCity; 504 end; 505 if not Assigned(Cell.Building) then begin 506 Cell.Building := Buildings.AddItem('City'); 507 Cell.Building.Kind := CityBuildingKind; 508 Cell.Building.Game := Self; 509 end; 510 end; 511 499 512 procedure TGame.Assign(Source: TGame); 500 513 begin … … 502 515 DevelMode := Source.DevelMode; 503 516 Players.Assign(Source.Players); 517 Buildings.Assign(Source.Buildings); 504 518 MapType := Source.MapType; 505 519 Map.Assign(Source.Map); … … 647 661 Units.LoadFromNode(NewNode); 648 662 663 NewNode := FindNode('Buildings'); 664 if Assigned(NewNode) then 665 Buildings.LoadFromNode(NewNode); 666 649 667 Map.Cells.FixRefId; 650 668 Units.FixRefId; … … 697 715 Units.RecalculateItemsId; 698 716 Players.RecalculateItemsId; 717 Buildings.RecalculateItemsId; 699 718 700 719 NewNode := OwnerDocument.CreateElement('GameSystem'); … … 713 732 AppendChild(NewNode); 714 733 Units.SaveToNode(NewNode); 734 735 NewNode := OwnerDocument.CreateElement('Buildings'); 736 AppendChild(NewNode); 737 Buildings.SaveToNode(NewNode); 715 738 end; 716 739 if ExtractFileDir(FileName) <> '' then … … 955 978 Map.Clear; 956 979 Units.Clear; 980 Buildings.Clear; 957 981 end; 958 982 -
trunk/Map.pas
r398 r407 48 48 Extra: TExtraType; 49 49 OneUnitId: Integer; // Temporary value 50 BuildingId: Integer; // Temporary value 50 51 function Compare(Cell: TCell): Boolean; 51 52 procedure ConnectTo(Cell: TCell); … … 1064 1065 end else Player := nil; 1065 1066 1067 if BuildingId <> 0 then begin 1068 Building := TGame(Map.Game).Buildings.FindById(BuildingId); 1069 if not Assigned(Building) then 1070 raise Exception.Create('Referenced building id ' + IntToStr(BuildingId) + ' not found.'); 1071 end else Building := nil; 1072 1066 1073 if OneUnitId <> 0 then begin 1067 1074 OneUnit := TGame(Map.Game).Units.FindById(OneUnitId); … … 1095 1102 PosPx.Y := ReadInteger(Node, 'PosY', 0); 1096 1103 PlayerId := ReadInteger(Node, 'Player', 0); 1104 BuildingId := ReadInteger(Node, 'Building', 0); 1097 1105 1098 1106 Node3 := Node.FindNode('Neighbours'); … … 1135 1143 WriteInteger(Node, 'Player', TPlayer(Player).Id) 1136 1144 else WriteInteger(Node, 'Player', 0); 1145 if Assigned(Building) then 1146 WriteInteger(Node, 'Building', Building.Id) 1147 else WriteInteger(Node, 'Building', 0); 1137 1148 NewNode := Node.OwnerDocument.CreateElement('Neighbours'); 1138 1149 Node.AppendChild(NewNode); -
trunk/Packages/Common/ItemList.pas
r399 r407 83 83 public 84 84 type 85 TAddEvent = function 85 TAddEvent = function(constref AValue: TItem): SizeInt of object; 86 86 TGetCountEvent = function: SizeInt of object; 87 87 TSetItemEvent = procedure(Index: SizeInt; AValue: TItem) of object; 88 TGetNameEvent = procedure 88 TGetNameEvent = procedure(out Name: string) of object; 89 89 TGetItemEvent = function(Index: SizeInt): TItem of object; 90 90 TGetItemFieldsEvent = function: TItemFields of object; 91 91 TRemoveEvent = function(constref AValue: TItem): SizeInt of object; 92 TGetNextAvailableNameEvent = procedure 92 TGetNextAvailableNameEvent = procedure(Name: string; out NewName: string) of object; 93 93 TCreateItemEvent = function(Name: string = ''): TItem of object; 94 TFindByIdEvent = function(Id: Integer): TItem of object; 94 95 private 95 96 FOnAdd: TAddEvent; 96 97 FOnCreateItem: TCreateItemEvent; 98 FOnFindById: TFindByIdEvent; 97 99 FOnGetCount: TGetCountEvent; 98 100 FOnGetItem: TGetItemEvent; … … 112 114 function GetNextAvailableName(Name: string): string; 113 115 function GetItemFields: TItemFields; 116 function FindById(Id: Integer): TItem; 114 117 property Count: SizeInt read GetCount; 115 118 property Items[Index: SizeInt]: TItem read GetItem write SetItem; default; … … 125 128 property OnCreateItem: TCreateItemEvent read FOnCreateItem 126 129 write FOnCreateItem; 130 property OnFindById: TFindByIdEvent read FOnFindById 131 write FOnFindById; 127 132 end; 128 133 … … 141 146 function BaseGetItemFields: TItemFields; 142 147 function BaseCreateItem(Name: string = ''): TItem; 148 function BaseFindById(Id: Integer): TItem; 143 149 procedure BaseGetNextAvailableName(Name: string; out NewName: string); 144 150 public … … 245 251 end; 246 252 253 function TItemList<T>.BaseFindById(Id: Integer): TItem; 254 begin 255 Result := FindById(Id); 256 end; 257 247 258 procedure TItemList<T>.LoadFromNode(Node: TDOMNode); 248 259 var … … 286 297 FBaseItemList.OnGetNextAvailableName := BaseGetNextAvailableName; 287 298 FBaseItemList.OnGetName := BaseGetName; 299 FBaseItemList.OnFindById := BaseFindById; 288 300 NewId := 1; 289 301 end; … … 483 495 ReadId: Integer; 484 496 ReferenceList: TBaseItemList; 497 RefItem: TItem; 485 498 begin 486 499 if Field.DataType = dtString then begin … … 503 516 ReferenceList := GetReferenceList(Field.Index); 504 517 if (ReadId > 0) and Assigned(ReferenceList) then 505 SetValueReference(Field.Index, TItem(ReferenceList[ReadId])); 518 RefItem := ReferenceList.FindById(ReadId); 519 if Assigned(RefItem) then 520 SetValueReference(Field.Index, RefItem) 521 else raise Exception.Create('Reference id ' + IntToStr(ReadId) + ' not found.'); 506 522 end else 507 523 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); … … 817 833 end; 818 834 835 function TBaseItemList.FindById(Id: Integer): TItem; 836 begin 837 if Assigned(FOnFindById) then Result := FOnFindById(Id) 838 else raise Exception.Create('Undefined FindById handler'); 839 end; 840 819 841 end. 820 842
Note:
See TracChangeset
for help on using the changeset viewer.