File: UMiniMap.pas

package info (click to toggle)
c-evo-dh 3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,540 kB
  • sloc: pascal: 57,645; xml: 243; makefile: 114; sh: 4
file content (331 lines) | stat: -rw-r--r-- 10,541 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{$INCLUDE Switches.inc}
unit UMiniMap;

interface

uses
  Classes, SysUtils, Graphics, Protocol, ClientTools;

type
  TMiniMode = (mmNone, mmPicture, mmMultiPlayer);
  TMapArray = array[0 .. lxmax * lymax - 1] of Byte;

  { TMiniMap }

  TMiniMap = class
  const
    MaxWidthMapLogo = 96;
    MaxHeightMapLogo = 96;
  var
    Bitmap: TBitmap; { game world sample preview }
    Size: TPoint;
    Colors: array [0 .. 11, 0 .. 1] of TColor;
    Mode: TMiniMode;
    MapOptions: TMapOptions;
    procedure LoadFromLogFile(FileName: string; var LastTurn: Integer; DefaultSize: TPoint);
    procedure LoadFromMapFile(FileName: string; var nMapLandTiles, nMapStartPositions: Integer);
    procedure PaintRandom(Brightness, StartLandMass: Integer; WorldSize: TPoint);
    procedure PaintFile(SaveMap: TMapArray);
    procedure Paint(MyMap: PTileList; MapWidth: Integer; ClientMode: Integer;
      xxt, xwMini: Integer);
    constructor Create;
    destructor Destroy; override;
  end;


implementation

uses
  ScreenTools, UPixelPointer, Global, GameServer, IsoEngine, Tribes;

const
  // save map tile flags
  smOwned = $20;
  smUnit = $40;
  smCity = $80;

{ TMiniMap }

constructor TMiniMap.Create;
var
  X, Y: Integer;
begin
  Bitmap := TBitmap.Create;

  for X := 0 to 11 do
    for Y := 0 to 1 do
      Colors[x, y] := HGrSystem.Data.Canvas.Pixels[66 + x, 67 + y];
end;

destructor TMiniMap.Destroy;
begin
  FreeAndNil(Bitmap);
  inherited;
end;

procedure TMiniMap.LoadFromLogFile(FileName: string; var LastTurn: Integer; DefaultSize: TPoint);
var
  SaveMap: TMapArray;
  y: Integer;
  Dummy: Integer;
  FileLandMass: integer;
  LogFile: file;
  s: string[255];
  MapRow: array [0 .. lxmax - 1] of Cardinal;
begin
  AssignFile(LogFile, FileName);
  try
    Reset(LogFile, 4);
    BlockRead(LogFile, s[1], 2); { file id }
    BlockRead(LogFile, Dummy, 1); { format id }
    if Dummy >= $000E01 then
      BlockRead(LogFile, Dummy, 1); { item stored since 0.14.1 }
    BlockRead(LogFile, Size.X, 1);
    BlockRead(LogFile, Size.Y, 1);
    BlockRead(LogFile, FileLandMass, 1);
    if FileLandMass = 0 then
      for y := 0 to Size.Y - 1 do
        BlockRead(LogFile, MapRow, Size.X);
    BlockRead(LogFile, Dummy, 1);
    BlockRead(LogFile, Dummy, 1);
    BlockRead(LogFile, LastTurn, 1);
    BlockRead(LogFile, SaveMap, 1);
    if SaveMap[0] = $80 then
      Mode := mmMultiPlayer
    else
      Mode := mmPicture;
    if Mode = mmPicture then
      BlockRead(LogFile, SaveMap[4], (Size.X * Size.Y - 1) div 4);
    CloseFile(LogFile);
  except
    CloseFile(LogFile);
    LastTurn := 0;
    Size := DefaultSize;
    Mode := mmNone;
  end;
  PaintFile(SaveMap);
end;

procedure TMiniMap.LoadFromMapFile(FileName: string; var nMapLandTiles, nMapStartPositions: Integer);
var
  x, y, lxFile, lyFile: integer;
  MapFile: file;
  s: string[255];
  MapRow: array [0 .. lxmax - 1] of Cardinal;
  ImageFileName: string;
begin
  ImageFileName := Copy(FileName, 1, Length(FileName) - Length(CevoMapExt)) + CevoMapPictureExt;
  Mode := mmPicture;
  if LoadGraphicFile(Bitmap, ImageFileName, [gfNoError]) then
  begin
    if Bitmap.width div 2 > MaxWidthMapLogo then
      Bitmap.width := MaxWidthMapLogo * 2;
    if Bitmap.height > MaxHeightMapLogo then
      Bitmap.height := MaxHeightMapLogo;
    Size.X := Bitmap.width div 2;
    Size.Y := Bitmap.height;
  end else begin
    Mode := mmNone;
    Size.X := MaxWidthMapLogo;
    Size.Y := MaxHeightMapLogo;
  end;

  AssignFile(MapFile, FileName);
  try
    Reset(MapFile, 4);
    BlockRead(MapFile, s[1], 2); { file id }
    BlockRead(MapFile, x, 1); { format id }
    BlockRead(MapFile, x, 1); // MaxTurn
    BlockRead(MapFile, lxFile, 1);
    BlockRead(MapFile, lyFile, 1);
    nMapLandTiles := 0;
    nMapStartPositions := 0;
    for y := 0 to lyFile - 1 do begin
      BlockRead(MapFile, MapRow, lxFile);
      for x := 0 to lxFile - 1 do
      begin
        if (MapRow[x] and fTerrain) in [fGrass, fPrairie, fTundra, fSwamp,
          fForest, fHills] then
          inc(nMapLandTiles);
        if MapRow[x] and (fPrefStartPos or fStartPos) <> 0 then
          inc(nMapStartPositions);
      end
    end;
    if nMapStartPositions > nPl then
      nMapStartPositions := nPl;
    CloseFile(MapFile);
  except
    CloseFile(MapFile);
  end;
end;

procedure TMiniMap.PaintRandom(Brightness, StartLandMass: Integer; WorldSize: TPoint);
var
  i, x, y, xm, cm: Integer;
  MiniPixel: TPixelPointer;
  Map: ^TTileList;
begin
  Map := PreviewMap(StartLandMass);
  Size := WorldSize;

  Bitmap.PixelFormat := pf24bit;
  Bitmap.SetSize(Size.X * 2, Size.Y);
  Bitmap.BeginUpdate;
  MiniPixel := PixelPointer(Bitmap);
  for y := 0 to ScaleToNative(Size.Y) - 1 do begin
    for x := 0 to ScaleToNative(Size.X) - 1 do begin
      for i := 0 to 1 do begin
        xm := (x * 2 + i + y and 1) mod (ScaleToNative(Size.X) * 2);
        MiniPixel.SetX(xm);
        cm := Colors[Map[ScaleFromNative(x) * lxmax div Size.X + lxmax *
          ((ScaleFromNative(y) * (lymax - 1) + Size.Y div 2) div (Size.Y - 1))] and
          fTerrain, i];
        MiniPixel.Pixel^.B := ((cm shr 16) and $FF) * Brightness div 3;
        MiniPixel.Pixel^.G := ((cm shr 8) and $FF) * Brightness div 3;
        MiniPixel.Pixel^.R := ((cm shr 0) and $FF) * Brightness div 3;
      end;
    end;
    MiniPixel.NextLine;
  end;
  Bitmap.EndUpdate;
end;

procedure TMiniMap.PaintFile(SaveMap: TMapArray);
var
  i, x, y, xm, cm, Tile, OwnColor, EnemyColor: integer;
  MiniPixel: TPixelPointer;
  PrevMiniPixel: TPixelPointer;
begin
  OwnColor := HGrSystem.Data.Canvas.Pixels[95, 67];
  EnemyColor := HGrSystem.Data.Canvas.Pixels[96, 67];
  Bitmap.PixelFormat := pf24bit;
  Bitmap.SetSize(Size.X * 2, Size.Y);
  if Mode = mmPicture then begin
    Bitmap.BeginUpdate;
    MiniPixel := PixelPointer(Bitmap);
    PrevMiniPixel := PixelPointer(Bitmap, 0, -1);
    for y := 0 to ScaleToNative(Size.Y) - 1 do begin
      for x := 0 to ScaleToNative(Size.X) - 1 do begin
        for i := 0 to 1 do begin
          xm := (x * 2 + i + y and 1) mod (ScaleToNative(Size.X) * 2);
          MiniPixel.SetX(xm);
          Tile := SaveMap[ScaleFromNative(x) + Size.X * ScaleFromNative(y)];
          if Tile and fTerrain = fUNKNOWN then
            cm := $000000
          else if Tile and smCity <> 0 then begin
            if Tile and smOwned <> 0 then cm := OwnColor
              else cm := EnemyColor;
            if y > 0 then begin
              // 2x2 city dot covers two lines
              PrevMiniPixel.SetX(xm);
              PrevMiniPixel.Pixel^.B := cm shr 16;
              PrevMiniPixel.Pixel^.G:= cm shr 8 and $FF;
              PrevMiniPixel.Pixel^.R := cm and $FF;
            end;
          end
          else if (i = 0) and (Tile and smUnit <> 0) then begin
            if Tile and smOwned <> 0 then cm := OwnColor
              else cm := EnemyColor;
          end else
            cm := Colors[Tile and fTerrain, i];
          MiniPixel.Pixel^.B := (cm shr 16) and $ff;
          MiniPixel.Pixel^.G := (cm shr 8) and $ff;
          MiniPixel.Pixel^.R := (cm shr 0) and $ff;
        end;
      end;
      MiniPixel.NextLine;
      PrevMiniPixel.NextLine;
    end;
    Bitmap.EndUpdate;
  end;
end;

procedure TMiniMap.Paint(MyMap: PTileList; MapWidth: Integer; ClientMode: Integer;
  xxt, xwMini: Integer);
var
  x, y, Loc, i, hw, xm, cm, cmPolOcean, cmPolNone: integer;
  PrevMiniPixel: TPixelPointer;
  MiniPixel: TPixelPointer;
  TerrainTile: Cardinal;
  MyCity: PCity;
  EnemyCity: PCityInfo;
  MyUnit: PUn;
  EnemyUnit: PUnitInfo;
begin
  if not Assigned(MyMap) then Exit;
  cmPolOcean := HGrSystem.Data.Canvas.Pixels[101, 67];
  cmPolNone := HGrSystem.Data.Canvas.Pixels[102, 67];
  hw := MapWidth div (xxt * 2);
  with Bitmap.Canvas do begin
    Brush.Color := $000000;
    FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  end;
  Bitmap.PixelFormat := pf24bit;
  Bitmap.SetSize(Size.X * 2, Size.Y);
  Bitmap.BeginUpdate;
  MiniPixel := PixelPointer(Bitmap);
  PrevMiniPixel := PixelPointer(Bitmap, 0, -1);
  for y := 0 to ScaleToNative(Size.Y) - 1 do begin
    for x := 0 to ScaleToNative(Size.X) - 1 do begin
      Loc := ScaleFromNative(x) + Size.X * ScaleFromNative(y);
      if (MyMap[Loc] and fTerrain) <> fUNKNOWN then begin
        for i := 0 to 1 do begin
          xm := ((x - ScaleToNative(xwMini)) * 2 + i + y and 1 - ScaleToNative(hw) +
            ScaleToNative(Size.X) * 5) mod (ScaleToNative(Size.X) * 2);
          MiniPixel.SetX(xm);
          TerrainTile := MyMap[Loc] and fTerrain;
          if TerrainTile > 11 then TerrainTile := 0;
          cm := Colors[TerrainTile, i];
          if ClientMode = cEditMap then begin
            if MyMap[Loc] and (fPrefStartPos or fStartPos) <> 0 then
              cm := $FFFFFF;
          end
          else if MyMap[Loc] and fCity <> 0 then begin
            // City
            MyCity := GetMyCityByLoc(Loc);
            if Assigned(MyCity) then cm := Tribe[me].Color
            else begin
              EnemyCity := GetEnemyCityByLoc(Loc);
              if Assigned(EnemyCity) then
                cm := Tribe[EnemyCity^.Owner].Color;
            end;
            cm := $808080 or cm shr 1; { increase brightness }
            if y > 0 then begin
              // 2x2 city dot covers two lines
              PrevMiniPixel.SetX(xm);
              PrevMiniPixel.Pixel^.B := (cm shr 16) and $ff;
              PrevMiniPixel.Pixel^.G := (cm shr 8) and $ff;
              PrevMiniPixel.Pixel^.R := (cm shr 0) and $ff;
            end;
          end
          else if (i = 0) and (MyMap[Loc] and fUnit <> 0) then begin
            // Unit
            MyUnit := GetMyUnitByLoc(Loc);
            if Assigned(MyUnit) then cm := Tribe[me].Color
            else begin
              EnemyUnit := GetEnemyUnitByLoc(Loc);
              if Assigned(EnemyUnit) then
                cm := Tribe[EnemyUnit.Owner].Color;
            end;
            cm := $808080 or cm shr 1; { increase brightness }
          end
          else if moPolitical in MapOptions then begin
            // Political
            if MyMap[Loc] and fTerrain < fGrass then cm := cmPolOcean
            else if MyRO.Territory[Loc] < 0 then cm := cmPolNone
            else cm := Tribe[MyRO.Territory[Loc]].Color;
          end;
          MiniPixel.Pixel^.B := (cm shr 16) and $ff;
          MiniPixel.Pixel^.G := (cm shr 8) and $ff;
          MiniPixel.Pixel^.R := (cm shr 0) and $ff;
        end;
      end;
    end;
    MiniPixel.NextLine;
    PrevMiniPixel.NextLine;
  end;
  Bitmap.EndUpdate;
end;

end.