Changeset 590 for trunk


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.
Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Database.pas

    r558 r590  
    77
    88uses
    9   SysUtils, Protocol, CmdList;
     9  SysUtils, Protocol, CmdList, Map;
    1010
    1111const
     
    120120function MapGeneratorAvailable: Boolean;
    121121procedure CreateElevation;
    122 procedure CreateMap(preview: Boolean);
     122procedure CreateMap(Preview: Boolean);
    123123procedure InitMapEditor;
    124124procedure ReleaseMapEditor;
     
    970970end;
    971971
    972 procedure CreateMap(preview: Boolean);
     972procedure CreateMap(Preview: Boolean);
    973973const
    974974  ShHiHills = 6; { of land }
     
    10891089
    10901090var
    1091   X, Y, N, Dir, plus, Count, Loc0, Loc1, bLand, bHills, bMountains, V8: Integer;
     1091  X, Y, N, Dir, Plus, Count, Loc0, Loc1, bLand, bHills, bMountains, V8: Integer;
    10921092  CopyFrom: array [0 .. lxmax * lymax - 1] of Integer;
    10931093  Adjacent: TVicinity8Loc;
     
    10951095begin
    10961096  FillChar(RealMap, MapSize * SizeOf(Cardinal), 0);
    1097   plus := 0;
     1097  Plus := 0;
    10981098  bMountains := 256;
    1099   while plus < MapSize * LandMass * ShMountains div 10000 do
     1099  while Plus < MapSize * LandMass * ShMountains div 10000 do
    11001100  begin
    11011101    Dec(bMountains);
    1102     Inc(plus, ElCount[bMountains]);
    1103   end;
    1104   Count := plus;
    1105   plus := 0;
     1102    Inc(Plus, ElCount[bMountains]);
     1103  end;
     1104  Count := Plus;
     1105  Plus := 0;
    11061106  bHills := bMountains;
    1107   while plus < MapSize * LandMass * ShHiHills div 10000 do
     1107  while Plus < MapSize * LandMass * ShHiHills div 10000 do
    11081108  begin
    11091109    Dec(bHills);
    1110     Inc(plus, ElCount[bHills]);
    1111   end;
    1112   Inc(Count, plus);
     1110    Inc(Plus, ElCount[bHills]);
     1111  end;
     1112  Inc(Count, Plus);
    11131113  bLand := bHills;
    11141114  while Count < MapSize * LandMass div 100 do
     
    11441144    end;
    11451145
    1146   if not preview then
    1147   begin
    1148     plus := 36 * 56 * 20 * ShTestRiver div (LandMass * 100);
    1149     if plus > MapSize then
    1150       plus := MapSize;
     1146  if not Preview then
     1147  begin
     1148    Plus := 36 * 56 * 20 * ShTestRiver div (LandMass * 100);
     1149    if Plus > MapSize then
     1150      Plus := MapSize;
    11511151    Loc0 := DelphiRandom(MapSize);
    1152     for N := 0 to plus - 1 do
     1152    for N := 0 to Plus - 1 do
    11531153    begin
    11541154      if (RealMap[Loc0] and fTerrain >= fGrass) and (Loc0 >= lx) and
     
    12671267      ($F shl 27);
    12681268
    1269   if not preview then
     1269  if not Preview then
    12701270  begin
    12711271    FindContinents;
     
    19411941// cix=-2 - don't search city, don't calculate city benefits, just government of player p
    19421942var
    1943   p0, Tile, special: Integer;
     1943  p0, Tile, Special: Integer;
    19441944begin
    19451945  with Info do
     
    19711971    end; // no city found here
    19721972
    1973     special := Tile and fSpecial and ResourceMask[P] shr 5;
     1973    Special := Tile and fSpecial and ResourceMask[P] shr 5;
    19741974    with Terrain[Tile and fTerrain] do
    19751975    begin
    1976       Food := FoodRes[special];
    1977       Prod := ProdRes[special];
    1978       Trade := TradeRes[special];
    1979       if (special > 0) and (Tile and fTerrain <> fGrass) and
     1976      Food := FoodRes[Special];
     1977      Prod := ProdRes[Special];
     1978      Trade := TradeRes[Special];
     1979      if (Special > 0) and (Tile and fTerrain <> fGrass) and
    19801980        (RW[P].NatBuilt[imSpacePort] > 0) then
    19811981      begin // GeoSat effect
  • 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^);
  • trunk/Integrated.lpi

    r539 r590  
    108108      </Item4>
    109109    </RequiredPackages>
    110     <Units Count="47">
     110    <Units Count="48">
    111111      <Unit0>
    112112        <Filename Value="Integrated.lpr"/>
     
    369369        <IsPartOfProject Value="True"/>
    370370      </Unit46>
     371      <Unit47>
     372        <Filename Value="Map.pas"/>
     373        <IsPartOfProject Value="True"/>
     374      </Unit47>
    371375    </Units>
    372376  </ProjectOptions>
  • trunk/MiniMap.pas

    r545 r590  
    55
    66uses
    7   Classes, SysUtils, Protocol, ClientTools,
     7  Classes, SysUtils, Protocol, ClientTools, Map,
    88  {$IFDEF DPI}Dpi.Graphics, Dpi.Common{$ELSE}Graphics{$ENDIF};
    99
     
    111111procedure TMiniMap.LoadFromMapFile(FileName: string; var nMapLandTiles, nMapStartPositions: Integer);
    112112var
    113   x, y, lxFile, lyFile: integer;
    114   MapFile: file;
    115   s: string[255];
     113  x, y: integer;
    116114  MapRow: array [0 .. lxmax - 1] of Cardinal;
    117115  ImageFileName: string;
     116  Map: TMap;
     117  Tile: Cardinal;
    118118begin
    119119  ImageFileName := Copy(FileName, 1, Length(FileName) - Length(CevoMapExt)) + CevoMapPictureExt;
     
    125125    if Bitmap.Height > MaxHeightMapLogo then
    126126      Bitmap.Height := MaxHeightMapLogo;
    127     Size.X := Bitmap.Width div 2;
    128     Size.Y := Bitmap.Height;
     127    Size := Point(Bitmap.Width div 2, Bitmap.Height);
    129128  end else begin
    130129    Mode := mmNone;
    131     Size.X := MaxWidthMapLogo;
    132     Size.Y := MaxHeightMapLogo;
    133   end;
    134 
    135   AssignFile(MapFile, FileName);
     130    Size := Point(MaxWidthMapLogo, MaxHeightMapLogo);
     131  end;
     132
     133  Map := TMap.Create;
    136134  try
    137     Reset(MapFile, 4);
    138     BlockRead(MapFile, s[1], 2); { file id }
    139     BlockRead(MapFile, x, 1); { format id }
    140     BlockRead(MapFile, x, 1); // MaxTurn
    141     BlockRead(MapFile, lxFile, 1);
    142     BlockRead(MapFile, lyFile, 1);
     135    Map.LoadFromFile(FileName);
    143136    nMapLandTiles := 0;
    144137    nMapStartPositions := 0;
    145     for y := 0 to lyFile - 1 do begin
    146       BlockRead(MapFile, MapRow, lxFile);
    147       for x := 0 to lxFile - 1 do
    148       begin
    149         if (MapRow[x] and fTerrain) in [fGrass, fPrairie, fTundra, fSwamp,
     138    for y := 0 to Map.Size.Y - 1 do begin
     139      for X := 0 to Map.Size.X - 1 do begin
     140        Tile := Map.Tiles[Y * Map.Size.X + X];
     141        if (Tile and fTerrain) in [fGrass, fPrairie, fTundra, fSwamp,
    150142          fForest, fHills] then
    151143          Inc(nMapLandTiles);
    152         if MapRow[x] and (fPrefStartPos or fStartPos) <> 0 then
     144        if Tile and (fPrefStartPos or fStartPos) <> 0 then
    153145          Inc(nMapStartPositions);
    154       end
     146      end;
    155147    end;
    156148    if nMapStartPositions > nPl then
    157149      nMapStartPositions := nPl;
    158     CloseFile(MapFile);
    159   except
    160     CloseFile(MapFile);
     150  finally
     151    FreeAndNil(Map);
    161152  end;
    162153end;
Note: See TracChangeset for help on using the changeset viewer.