Changeset 122 for trunk


Ignore:
Timestamp:
Oct 26, 2022, 5:25:25 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Partial implementation for load/save of current game.
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r121 r122  
    119119procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    120120begin
     121  //Engine.SaveToFile('Game.bmg');
    121122  Core.PersistentForm1.Save(Self);
    122123end;
  • trunk/Languages/BigMetro.cs.po

    r118 r122  
    251251msgstr "týden"
    252252
     253#: uengine.swrongfileformat
     254msgid "Wrong file format"
     255msgstr "Špatný formát souboru"
     256
    253257#: uformmain.sfridayshort
    254258msgid "FRI"
  • trunk/Languages/BigMetro.de.po

    r118 r122  
    253253msgstr ""
    254254
     255#: uengine.swrongfileformat
     256msgid "Wrong file format"
     257msgstr ""
     258
    255259#: uformmain.sfridayshort
    256260msgid "FRI"
  • trunk/Languages/BigMetro.fr.po

    r118 r122  
    253253msgstr ""
    254254
     255#: uengine.swrongfileformat
     256msgid "Wrong file format"
     257msgstr ""
     258
    255259#: uformmain.sfridayshort
    256260msgid "FRI"
  • trunk/Languages/BigMetro.pot

    r118 r122  
    241241msgstr ""
    242242
     243#: uengine.swrongfileformat
     244msgid "Wrong file format"
     245msgstr ""
     246
    243247#: uformmain.sfridayshort
    244248msgid "FRI"
  • trunk/UEngine.pas

    r121 r122  
    1010  URegistry, UMetaCanvas, Generics.Collections, Generics.Defaults, UMenu,
    1111  UControls, UMetroPassenger, UColors, UView, URiver, UTrack, UCity, UGeometric,
    12   UPersistentForm, UTranslator;
     12  UTranslator, DOM, XMLRead, XMLWrite, UXMLUtils;
    1313
    1414type
     
    3737    function GetMaxPassengers: Integer;
    3838    function IsBestStationForShape(Shape: TStationShape; Check, Current: TLineStation): Boolean;
     39    procedure LoadFromXmlNode(Node: TDOMNode);
     40    procedure SaveToXmlNode(Node: TDOMNode);
    3941    constructor Create;
    4042    destructor Destroy; override;
     
    4749    function GetRect: TRect;
    4850    function AddNew: TMapStation;
     51    procedure LoadFromXmlNode(Node: TDOMNode);
     52    procedure SaveToXmlNode(Node: TDOMNode);
    4953  end;
    5054
     
    7175    procedure UpdateEndingLines;
    7276  public
     77    Id: Integer;
    7378    Index: Integer;
    7479    Engine: TEngine;
     
    8287    destructor Destroy; override;
    8388    function IsCircular: Boolean;
     89    procedure LoadFromXmlNode(Node: TDOMNode);
     90    procedure SaveToXmlNode(Node: TDOMNode);
    8491  end;
    8592
     
    8996    Engine: TEngine;
    9097    function AddNew(Color: TColor): TMetroLine;
     98    function FindById(Id: Integer): TMetroLine;
    9199    function SearchByColor(Color: TColor): TMetroLine;
     100    procedure LoadFromXmlNode(Node: TDOMNode);
     101    procedure SaveToXmlNode(Node: TDOMNode);
    92102  end;
    93103
     
    121131    procedure SetLine(AValue: TMetroLine);
    122132  public
     133    Engine: TEngine;
    123134    Passengers: TMetroPassengers;
    124135    TrackPosition: TTrackPosition;
     
    127138    TargetStation: TLineStation;
    128139    Carriages: TMetroCarriages;
     140    procedure LoadFromXmlNode(Node: TDOMNode);
     141    procedure SaveToXmlNode(Node: TDOMNode);
    129142    procedure FindTargetStation;
    130143    function GetTargetStationDistance: Integer;
     
    140153    function GetUnusedCount: Integer;
    141154    function AddNew: TMetroTrain;
     155    procedure LoadFromXmlNode(Node: TDOMNode);
     156    procedure SaveToXmlNode(Node: TDOMNode);
    142157  end;
    143158
     
    309324    procedure LoadFromRegistry;
    310325    procedure SaveToRegistry;
     326    procedure LoadFromFile(FileName: string);
     327    procedure SaveToFile(FileName: string);
    311328    constructor Create(AOwner: TComponent); override;
    312329    destructor Destroy; override;
     
    397414  SSuccessReason = 'Your metro transported enough passengers and you have reached your goal for this city.';
    398415  SUnlockedCity = 'City %s is now unlocked.';
     416  SWrongFileFormat = 'Wrong file format';
    399417
    400418  // Cities
     
    513531  Result := TMetroTrain.Create;
    514532  Add(Result);
     533end;
     534
     535procedure TMetroTrains.LoadFromXmlNode(Node: TDOMNode);
     536var
     537  Node2: TDOMNode;
     538  NewItem: TMetroTrain;
     539begin
     540  Node2 := Node.FirstChild;
     541  while Assigned(Node2) and (Node2.NodeName = 'Train') do begin
     542    NewItem := TMetroTrain.Create;
     543    NewItem.LoadFromXmlNode(Node2);
     544    Add(NewItem);
     545    Node2 := Node2.NextSibling;
     546  end;
     547end;
     548
     549procedure TMetroTrains.SaveToXmlNode(Node: TDOMNode);
     550var
     551  I: Integer;
     552  NewNode: TDOMNode;
     553begin
     554  for I := 0 to Count - 1 do begin;
     555    NewNode := Node.OwnerDocument.CreateElement('Train');
     556    Node.AppendChild(NewNode);
     557    Items[I].SaveToXmlNode(NewNode);
     558  end;
    515559end;
    516560
     
    568612end;
    569613
     614procedure TMapStations.LoadFromXmlNode(Node: TDOMNode);
     615var
     616  Node2: TDOMNode;
     617  NewItem: TMapStation;
     618begin
     619  Node2 := Node.FirstChild;
     620  while Assigned(Node2) and (Node2.NodeName = 'Line') do begin
     621    NewItem := TMapStation.Create;
     622    NewItem.LoadFromXmlNode(Node2);
     623    Add(NewItem);
     624    Node2 := Node2.NextSibling;
     625  end;
     626end;
     627
     628procedure TMapStations.SaveToXmlNode(Node: TDOMNode);
     629var
     630  I: Integer;
     631  NewNode: TDOMNode;
     632begin
     633  for I := 0 to Count - 1 do begin;
     634    NewNode := Node.OwnerDocument.CreateElement('Station');
     635    Node.AppendChild(NewNode);
     636    Items[I].SaveToXmlNode(NewNode);
     637  end;
     638end;
     639
    570640{ TMetroLines }
    571641
     
    579649end;
    580650
     651function TMetroLines.FindById(Id: Integer): TMetroLine;
     652var
     653  I: Integer;
     654begin
     655  I := 0;
     656  while (I < Count) and (Items[I].Id <> Id) do Inc(I);
     657  if I < Count then Result := Items[I]
     658    else Result := nil;
     659end;
     660
    581661function TMetroLines.SearchByColor(Color: TColor): TMetroLine;
    582662var
     
    587667  if I < Count then Result := Items[I]
    588668    else Result := nil;
     669end;
     670
     671procedure TMetroLines.LoadFromXmlNode(Node: TDOMNode);
     672var
     673  Node2: TDOMNode;
     674  NewItem: TMetroLine;
     675begin
     676  Node2 := Node.FirstChild;
     677  while Assigned(Node2) and (Node2.NodeName = 'Line') do begin
     678    NewItem := TMetroLine.Create;
     679    NewItem.LoadFromXmlNode(Node2);
     680    Add(NewItem);
     681    Node2 := Node2.NextSibling;
     682  end;
     683end;
     684
     685procedure TMetroLines.SaveToXmlNode(Node: TDOMNode);
     686var
     687  I: Integer;
     688  NewNode: TDOMNode;
     689begin
     690  for I := 0 to Count - 1 do begin;
     691    NewNode := Node.OwnerDocument.CreateElement('Line');
     692    Node.AppendChild(NewNode);
     693    Items[I].SaveToXmlNode(NewNode);
     694  end;
    589695end;
    590696
     
    786892end;
    787893
     894procedure TMetroLine.LoadFromXmlNode(Node: TDOMNode);
     895var
     896  Node2: TDOMNode;
     897begin
     898  Id := ReadInteger(Node, 'Id', Id);
     899  Color := TColor(ReadInteger(Node, 'Color', Color));
     900
     901  Node2 := Node.FindNode('Track');
     902  if Assigned(Node2) then
     903    Track.LoadFromXmlNode(Node2);
     904end;
     905
     906procedure TMetroLine.SaveToXmlNode(Node: TDOMNode);
     907var
     908  Node2: TDOMNode;
     909begin
     910  WriteInteger(Node, 'Id', Id);
     911  WriteInteger(Node, 'Color', Color);
     912
     913  Node2 := Node.OwnerDocument.CreateElement('Track');
     914  Node.AppendChild(Node2);
     915  Track.SaveToXmlNode(Node2);
     916end;
     917
    788918{ TMetroTrain }
    789919
     
    797927    TargetStation := nil;
    798928  end;
     929end;
     930
     931procedure TMetroTrain.LoadFromXmlNode(Node: TDOMNode);
     932begin
     933  Line := Engine.Lines.FindById(ReadInteger(Node, 'Line', 0));
     934end;
     935
     936procedure TMetroTrain.SaveToXmlNode(Node: TDOMNode);
     937begin
     938  if Assigned(Line) then WriteInteger(Node, 'Line', Line.Id)
     939    else WriteInteger(Node, 'Line', 0);
    799940end;
    800941
     
    10311172  Result := (Check.MapStation.ShapeDistance[Shape] <> -1) and
    10321173    (Check.MapStation.ShapeDistance[Shape] <= Distance);
     1174end;
     1175
     1176procedure TMapStation.LoadFromXmlNode(Node: TDOMNode);
     1177begin
     1178  Position.X := ReadInteger(Node, 'PositionX', Position.X);
     1179  Position.Y := ReadInteger(Node, 'PositionY', Position.Y);
     1180  Shape := TStationShape(ReadInteger(Node, 'Shape', Integer(Shape)));
     1181  IsTerminal := ReadBoolean(Node, 'IsTerminal', IsTerminal);
     1182end;
     1183
     1184procedure TMapStation.SaveToXmlNode(Node: TDOMNode);
     1185begin
     1186  WriteInteger(Node, 'PositionX', Position.X);
     1187  WriteInteger(Node, 'PositionY', Position.Y);
     1188  WriteInteger(Node, 'Shape', Integer(Shape));
     1189  WriteBoolean(Node, 'IsTerminal', IsTerminal);
    10331190end;
    10341191
     
    33693526  finally
    33703527    Free;
     3528  end;
     3529end;
     3530
     3531procedure TEngine.LoadFromFile(FileName: string);
     3532var
     3533  NewNode: TDOMNode;
     3534  Doc: TXMLDocument;
     3535  RootNode: TDOMNode;
     3536begin
     3537  ReadXMLFile(Doc, FileName);
     3538  Clear;
     3539  with Doc do try
     3540    if Doc.DocumentElement.NodeName <> 'BigMetroGame' then
     3541      raise Exception.Create(SWrongFileFormat);
     3542    RootNode := Doc.DocumentElement;
     3543    with RootNode do begin
     3544      ServedPassengerCount := ReadInteger(RootNode, 'ServedPassengerCount', ServedPassengerCount);
     3545
     3546      NewNode := FindNode('Stations');
     3547      Stations.LoadFromXmlNode(NewNode);
     3548
     3549      NewNode := FindNode('Lines');
     3550      Lines.LoadFromXmlNode(NewNode);
     3551
     3552      NewNode := FindNode('Trains');
     3553      Trains.LoadFromXmlNode(NewNode);
     3554    end;
     3555  finally
     3556    FreeAndNil(Doc);
     3557  end;
     3558end;
     3559
     3560procedure TEngine.SaveToFile(FileName: string);
     3561var
     3562  NewNode: TDOMNode;
     3563  Doc: TXMLDocument;
     3564  RootNode: TDOMNode;
     3565  I: Integer;
     3566begin
     3567  Doc := TXMLDocument.Create;
     3568  with Doc do try
     3569    RootNode := CreateElement('BigMetroGame');
     3570    AppendChild(RootNode);
     3571    with RootNode do begin
     3572      WriteInteger(RootNode, 'ServedPassengerCount', ServedPassengerCount);
     3573
     3574      for I := 0 to Lines.Count - 1 do
     3575        Lines[I].Id := I + 1;
     3576
     3577      NewNode := OwnerDocument.CreateElement('Stations');
     3578      AppendChild(NewNode);
     3579      Stations.SaveToXmlNode(NewNode);
     3580
     3581      NewNode := OwnerDocument.CreateElement('Lines');
     3582      AppendChild(NewNode);
     3583      Lines.SaveToXmlNode(NewNode);
     3584
     3585      NewNode := OwnerDocument.CreateElement('Trains');
     3586      AppendChild(NewNode);
     3587      Trains.SaveToXmlNode(NewNode);
     3588    end;
     3589    if ExtractFileDir(FileName) <> '' then
     3590      ForceDirectories(ExtractFileDir(FileName));
     3591    WriteXMLFile(Doc, FileName);
     3592  finally
     3593    FreeAndNil(Doc);
    33713594  end;
    33723595end;
  • trunk/UTrack.pas

    r107 r122  
    44
    55uses
    6   Classes, SysUtils, Math, Generics.Collections, UGeometric;
     6  Classes, SysUtils, Math, Generics.Collections, UGeometric, DOM, XMLRead,
     7  XMLWrite, UXMLUtils;
    78
    89type
     
    4849    // Move to TTrackLink later
    4950    function GetDistance: Integer;
     51
     52    procedure LoadFromXmlNode(Node: TDOMNode);
     53    procedure SaveToXmlNode(Node: TDOMNode);
    5054    constructor Create;
    5155    destructor Destroy; override;
     
    5761    Track: TTrack;
    5862    function AddNew: TTrackPoint;
     63    procedure LoadFromXmlNode(Node: TDOMNode);
     64    procedure SaveToXmlNode(Node: TDOMNode);
    5965  end;
    6066
     
    6470    Points: TTrackPoints;
    6571    Shift: TPoint;
     72    procedure LoadFromXmlNode(Node: TDOMNode);
     73    procedure SaveToXmlNode(Node: TDOMNode);
    6674    constructor Create;
    6775    destructor Destroy; override;
     
    7381    function SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
    7482    function AddNew: TTrackLink;
     83    procedure LoadFromXmlNode(Node: TDOMNode);
     84    procedure SaveToXmlNode(Node: TDOMNode);
    7585  end;
    7686
     
    8696    constructor Create;
    8797    destructor Destroy; override;
     98    procedure LoadFromXmlNode(Node: TDOMNode);
     99    procedure SaveToXmlNode(Node: TDOMNode);
    88100  end;
    89101
     
    200212end;
    201213
     214procedure TTrackLinks.LoadFromXmlNode(Node: TDOMNode);
     215var
     216  Node2: TDOMNode;
     217  NewItem: TTrackLink;
     218begin
     219  Node2 := Node.FirstChild;
     220  while Assigned(Node2) and (Node2.NodeName = 'Link') do begin
     221    NewItem := TTrackLink.Create;
     222    NewItem.LoadFromXmlNode(Node2);
     223    Add(NewItem);
     224    Node2 := Node2.NextSibling;
     225  end;
     226end;
     227
     228procedure TTrackLinks.SaveToXmlNode(Node: TDOMNode);
     229var
     230  I: Integer;
     231  Node2: TDOMNode;
     232begin
     233  for I := 0 to Count - 1 do begin;
     234    Node2 := Node.OwnerDocument.CreateElement('Link');
     235    Node.AppendChild(Node2);
     236    Items[I].SaveToXmlNode(Node2);
     237  end;
     238end;
     239
    202240{ TTrackPoints }
    203241
     
    206244  Result := TTrackPoint.Create;
    207245  Result.Track := Track;
     246end;
     247
     248procedure TTrackPoints.LoadFromXmlNode(Node: TDOMNode);
     249var
     250  Node2: TDOMNode;
     251  NewItem: TTrackPoint;
     252begin
     253  Node2 := Node.FirstChild;
     254  while Assigned(Node2) and (Node2.NodeName = 'Point') do begin
     255    NewItem := TTrackPoint.Create;
     256    NewItem.LoadFromXmlNode(Node2);
     257    Add(NewItem);
     258    Node2 := Node2.NextSibling;
     259  end;
     260end;
     261
     262procedure TTrackPoints.SaveToXmlNode(Node: TDOMNode);
     263var
     264  I: Integer;
     265  Node2: TDOMNode;
     266begin
     267  for I := 0 to Count - 1 do begin;
     268    Node2 := Node.OwnerDocument.CreateElement('Point');
     269    Node.AppendChild(Node2);
     270    Items[I].SaveToXmlNode(Node2);
     271  end;
    208272end;
    209273
     
    222286  FreeAndNil(Links);
    223287  inherited;
     288end;
     289
     290procedure TTrack.LoadFromXmlNode(Node: TDOMNode);
     291begin
     292  Points.LoadFromXmlNode(Node);
     293  Links.LoadFromXmlNode(Node);
     294end;
     295
     296procedure TTrack.SaveToXmlNode(Node: TDOMNode);
     297begin
     298  Points.SaveToXmlNode(Node);
     299  Links.SaveToXmlNode(Node);
    224300end;
    225301
     
    281357{ TTrackLink }
    282358
     359procedure TTrackLink.LoadFromXmlNode(Node: TDOMNode);
     360begin
     361
     362end;
     363
     364procedure TTrackLink.SaveToXmlNode(Node: TDOMNode);
     365begin
     366
     367end;
     368
    283369constructor TTrackLink.Create;
    284370begin
     
    444530end;
    445531
     532procedure TTrackPoint.LoadFromXmlNode(Node: TDOMNode);
     533begin
     534  Position.X := ReadInteger(Node, 'PositionX', Position.X);
     535  Position.Y := ReadInteger(Node, 'PositionY', Position.Y);
     536end;
     537
     538procedure TTrackPoint.SaveToXmlNode(Node: TDOMNode);
     539begin
     540  WriteInteger(Node, 'PositionX', Position.X);
     541  WriteInteger(Node, 'PositionY', Position.Y);
     542end;
     543
    446544constructor TTrackPoint.Create;
    447545begin
Note: See TracChangeset for help on using the changeset viewer.