Changeset 590 for trunk/GameServer.pas


Ignore:
Timestamp:
Jul 24, 2024, 10:25:56 PM (4 months ago)
Author:
chronos
Message:
  • Modified: Map load and save moved to separate class TMap.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GameServer.pas

    r589 r590  
    88uses
    99  Protocol, Database, DynLibs, Platform, DateUtils, LazFileUtils, Brain, Global,
     10  Map,
    1011  {$IFDEF DPI}Graphics{$ELSE}Graphics{$ENDIF};
    1112
     
    7374function LoadGame(const FileName: string; Turn: Integer;
    7475  MovieMode: Boolean): Boolean;
    75 procedure EditMap(const Map: string; Newlx, Newly, NewLandMass: Integer);
     76procedure EditMap(const AMapFileName: string; Newlx, Newly, NewLandMass: Integer);
    7677procedure DirectHelp(Command: Integer);
    7778function ToAutoSaveFileName(FileName: string): string;
     
    8081procedure ChangeClient;
    8182procedure NextPlayer;
    82 function PreviewMap(lm: Integer): Pointer;
     83function PreviewMap(ALandMass: Integer): Pointer;
    8384
    8485
     
    283284end;
    284285
    285 function PreviewMap(lm: Integer): Pointer;
     286function PreviewMap(ALandMass: Integer): Pointer;
    286287begin
    287288  lx := lxmax;
    288289  ly := lymax;
    289290  MapSize := lx * ly;
    290   LandMass := lm;
     291  LandMass := ALandMass;
    291292  DelphiRandSeed := PreviewRND;
    292293  if not PreviewElevation then
     
    563564end;
    564565
    565 procedure SaveMap(FileName: string);
    566 var
    567   I: Integer;
    568   MapFile: TFileStream;
    569   S: string[255];
    570 begin
    571   MapFile := TFileStream.Create(FileName, fmCreate or fmShareExclusive);
    572   try
    573     MapFile.Position := 0;
    574     S := 'cEvoMap'#0;
    575     MapFile.Write(S[1], 8); { file id }
    576     I := 0;
    577     MapFile.Write(I, 4); { format id }
    578     MapFile.Write(MaxTurn, 4);
    579     MapFile.Write(lx, 4);
    580     MapFile.Write(ly, 4);
    581     MapFile.Write(RealMap, MapSize * 4);
    582   finally
    583     FreeAndNil(MapFile);
    584   end;
    585 end;
    586 
    587 function LoadMap(FileName: string): Boolean;
    588 var
    589   I, Loc1: Integer;
    590   MapFile: TFileStream;
    591   S: string[255];
    592 begin
    593   Result := False;
    594   if not FileExists(FileName) then Exit;
    595   MapFile := TFileStream.Create(FileName, fmOpenRead or fmShareExclusive);
    596   try
    597     MapFile.Position := 0;
    598     MapFile.Read(S[1], 8); { file id }
    599     MapFile.Read(I, 4); { format id }
    600     if I = 0 then
    601     begin
    602       MapFile.Read(I, 4); // MaxTurn
    603       MapFile.Read(lx, 4);
    604       MapFile.Read(ly, 4);
    605       ly := ly and not 1;
    606       if lx > lxmax then
    607         lx := lxmax;
    608       if ly > lymax then
    609         ly := lymax;
    610       MapSize := lx * ly;
    611       MapFile.Read(RealMap, MapSize * 4);
    612       for Loc1 := 0 to MapSize - 1 do
    613       begin
    614         RealMap[Loc1] := RealMap[Loc1] and
    615           ($7F01FFFF or fPrefStartPos or fStartPos) or ($F shl 27);
    616         if RealMap[Loc1] and (fTerrain or fSpecial) = fSwamp or fSpecial2 then
    617           RealMap[Loc1] := RealMap[Loc1] and not (fTerrain or fSpecial) or
    618             (fSwamp or fSpecial1);
    619         if (RealMap[Loc1] and fDeadLands <> 0) and
    620           (RealMap[Loc1] and fTerrain <> fArctic) then
    621           RealMap[Loc1] := RealMap[Loc1] and not (fTerrain or fSpecial)
    622             or fDesert;
    623       end;
    624       Result := True;
    625     end;
    626   finally
    627     FreeAndNil(MapFile);
    628   end;
    629 end;
    630 
    631566procedure SaveGame(FileName: string; Auto: Boolean);
    632567var
     
    717652  BrainUsed: Set of 0 .. 254; { used brains }
    718653  AIBrains: TBrains;
     654  Map: TMap;
    719655begin
    720656  for p1 := 0 to nPl - 1 do begin
     
    865801  else
    866802  begin // predefined map
    867     if Mode = moPlaying then
    868       LoadMap(MapFileName); // new game -- load map from file
     803    if Mode = moPlaying then begin
     804      Map := TMap.Create;
     805      Map.LoadFromFile(MapFileName); // new game -- load map from file
     806      MapSize := Map.MapSize;
     807      lx := Map.Size.X;
     808      ly := Map.Size.Y;
     809      Move(Map.Tiles[0], RealMap, MapSize * 4);
     810      FreeAndNil(Map);
     811    end;
    869812    GetMem(MapField, MapSize * 4);
    870813    Move(RealMap, MapField^, MapSize * 4);
     
    14191362end;
    14201363
    1421 procedure EditMap(const Map: string; Newlx, Newly, NewLandMass: Integer);
     1364procedure EditMap(const AMapFileName: string; Newlx, Newly, NewLandMass: Integer);
    14221365var
    14231366  p1, Loc1: Integer;
    14241367  Game: TNewGameData;
     1368  Map: TMap;
    14251369begin
    14261370  Notify(ntStartDone);
    14271371  Notify(ntInitLocalHuman);
    1428   MapFileName := Map;
     1372  MapFileName := AMapFileName;
    14291373  lx := Newlx;
    14301374  ly := Newly;
     
    14381382  GAlive := 0;
    14391383  GWatching := 1;
    1440   if not LoadMap(MapFileName) then
    1441     for Loc1 := 0 to MapSize - 1 do
    1442       RealMap[Loc1] := fOcean or ($F shl 27);
     1384  for Loc1 := 0 to MapSize - 1 do
     1385    RealMap[Loc1] := fOcean or ($F shl 27);
     1386  if FileExists(MapFileName) then begin
     1387    Map := TMap.Create;
     1388    Map.LoadFromFile(MapFileName);
     1389    MapSize := Map.MapSize;
     1390    lx := Map.Size.X;
     1391    ly := Map.Size.Y;
     1392    Move(Map.Tiles[0], RealMap, MapSize * 4);
     1393    FreeAndNil(Map);
     1394  end;
    14431395  CL := nil;
    14441396  InitMapEditor;
     
    27202672  ShowNegoData: TShowNegoData;
    27212673  Logged, Ok, HasShipChanged, AllHumansDead, OfferFullySupported: Boolean;
     2674  Map: TMap;
    27222675begin
    27232676  if Command = sTurn then
     
    32953248      if Player = 0 then
    32963249      begin
    3297         if Command = sSaveMap then
    3298           SaveMap(MapFileName);
     3250        if Command = sSaveMap then begin
     3251          Map := TMap.Create;
     3252          Map.Size := Point(lx, ly);
     3253          Map.MaxTurn := MaxTurn;
     3254          Move(RealMap, Map.Tiles[0], MapSize * 4);
     3255          Map.SaveToFile(MapFileName);
     3256          FreeAndNil(Map);
     3257        end;
    32993258        Notify(ntBackOn);
    33003259        BrainTerm.Client(cBreakGame, -1, nil^);
Note: See TracChangeset for help on using the changeset viewer.