File: CevoMapFile.pas

package info (click to toggle)
cevomapgen 39-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: pascal: 2,765; xml: 169; makefile: 50
file content (342 lines) | stat: -rw-r--r-- 8,605 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
332
333
334
335
336
337
338
339
340
341
342
unit CevoMapFile;

{***********************************************************

Project:    C-evo External Map Generator
Copyright:  1999-2024 P Blackman
License:    GPLv3+

Class for the Cevo map file format
Routines to read and write the file

***********************************************************}

interface uses MapTiles, CevoMap, INIFiles;

const
    MapHeader   = 'cEvoMap' + chr(0);

type
    TmapFile = class(TObject)

    // THeader, tTile, TmapBody used by MapCheck
    type
        // Header section of the cevo map file, 24 bytes
        THeader =
        packed record
            FormatID: packed array [1..8] of Char;
            Version,
            MaxTurn,
            LX,
            LY: DWord;
        end;

        TmapBody = array [1..lxmax*lymax] of tTile;

    private const

    private
        fFile:     file of Byte;
        fArea:     DWord;
        fINIFile:  TINIFile;
        fLoadPath: String;
        fSavePath: String;
        fSaveFileName: String;

    protected
        procedure SetWidth(W: DWord);
        procedure SetHeight(H: DWord);
        procedure InitBody;
        procedure SaveTile(W, H: Integer; T: tTerrain; River: Boolean; Res: tResource; Special: tSpecial; Start: Boolean);

    public
        fHeader:   tHeader;
        fBody:     TMapBody;

        constructor Create;
        destructor Destroy; override;
        function MapIndex(W, H: Integer): Integer;

        function OpenMFile(FileName: String): Boolean;
        function LoadFromFile: Boolean;
        procedure LoadMapData(MyMap: TMap);

        procedure SaveMFile(FileName: String);
        procedure SaveTileData(MyMap: TMap);

        procedure ReadINIFile;
        procedure WriteINIFile;

        property Width: DWord read fHeader.LX write SetWidth;
        property Height: DWord read fHeader.LY write SetHeight;
        property Area: DWord read fArea write fArea;
        property LoadPath: String read fLoadPath;
        property SavePath: String read fSavePath;
        property SaveFileName: String read FSaveFileName;
    end;


implementation uses Classes, SysUtils, Message;

const
    NullSpecial = $78; // Value found in existing maps
    HeaderSize  = 24;
    RiverMask   = $80;
    BookHeader  = 'cEvoBook';



constructor Tmapfile.Create;
begin
    inherited Create;

    ReadINIFile;

    with fHeader do
    begin
        FormatID := MapHeader;
        Version  := 0;
        MaxTurn  := 0;
        LX       := 0;
        LY       := 0;
    end;
end;

destructor TmapFile.Destroy;
begin
    WriteINIFile;
    fINIFile.Free;
    inherited Destroy;
end;

function TMapFile.MapIndex(W, H: Integer): Integer;
begin
    Result := W + (H - 1) * Width; // Index starts from 1 (Map editor starts from 0)
end;


// =========================  Map File Load routines ============================

procedure TmapFile.InitBody;
var S: Integer;
begin
    for S := 1 to Area do
        with fBody[S] do
        begin
            Terrain  := 0;
            Improve  := 0;
            StartPos := 0;
            Special  := 0;
        end;
end;

procedure TmapFile.SetWidth(W: DWord);
begin
    with fHeader do
    begin
        LX    := W;
        fArea := LX * LY;
    end;
    InitBody;
end;

procedure TmapFile.SetHeight(H: DWord);
begin
    with fHeader do
    begin
        LY    := H;
        fArea := LX * LY;
    end;
    InitBody;
end;


function TmapFile.OpenMFile(FileName: String): Boolean;
begin
    try
        FileMode := 0;
        AssignFile(fFile, FileName);
        Reset(fFile);
        Result    := True;
        fLoadPath := ExtractFilePath(FileName);
    except
        ShowMessage ('Failed to open ' + FileName);
        Result := False;
        fLoadPath := '';
    end;
end;

function TmapFile.LoadFromFile: Boolean;
var NumRead: Integer;
begin
    Result  := False;
    NumRead := 0;
    BlockRead(fFile, fHeader, HeaderSize, NumRead);

    if NumRead <> HeaderSize then
        ShowMessage('Invalid File (truncated!)')
    else
    if (fHeader.FormatID = MapHeader) or (fHeader.FormatID = BookHeader) then
    begin
        with fHeader do
            fArea := LX * LY;

        BlockRead(fFile, fBody, Area * sizeof(tTile), NumRead);

        if NumRead <> Area * sizeof(tTile) then
            ShowMessage('Truncated Map File Body')
        else
            Result := True;
    end
    else
        ShowMessage('Not a valid C-evo map or book file');

    CloseFile(fFile);
end;


// Load the bits of the data structure needed for display/analysis
procedure TMapFile.LoadMapData(MyMap: TMap);
var W, H: Integer;
    Tb: Byte;
    T: tTerrain;
    B: tResource;
    S: tSpecial;
    R: Boolean;

begin
    for H := 1 to Height do
        for W := 1 to Width do
        begin
            Tb := fBody[W + (H - 1) * Width].Terrain;

            // mask out non terain resouirce info beyond $F Hex
            T := tTerrain(Tb AND $F);

            R := (Tb AND Rivermask) <> 0;
            B := tResource((Tb AND $60) DIV $20); // $40 & £20 translate to 1 & 2

            Tb := fBody[MapIndex(W, H)].Special;
            if (Tb = 0) Or (Tb = NullSpecial) then
                S := NoSpecial
            else
                S := tSpecial((Tb+1) DIV 2);

            MyMap.SetTerrain(W, H, T, R);
            MyMap.SetBonus(W, H, B);
            MyMap.SetSpecial(W, H, S);

            if fBody[W + (H - 1) * Width].StartPos in [$20, $40] then
                MyMap.SetStartPosition(W, H);
        end;
    MyMap.GetCitySites;
end;


// =========================  Map File Save routines ============================

procedure TmapFile.SaveMFile(FileName: String);
var NumWrite: Integer;
begin
    FileMode := 2;
    AssignFile(fFile, FileName);
    ReWrite(fFile);

    NumWrite := 0;
    BlockWrite(fFile, fHeader, HeaderSize, NumWrite);
    Assert(NumWrite = HeaderSize, 'Wrong size file header');

    BlockWrite(fFile, fBody, Area * sizeof(tTile), NumWrite);
    Assert(NumWrite = Area * sizeof(tTile), 'Wrong size file body');

    CloseFile(fFile);
    fSaveFileName := ExtractFileName(FileName);
    fSavePath     := ExtractFilePath(FileName);
end;


procedure TmapFile.SaveTile(W, H: Integer; T: tTerrain; River: Boolean; Res: tResource; Special: tSpecial; Start: Boolean);
var temp: Byte;
begin
    if River AND (T <> Ocean) AND (T <> Coast) then
        Temp := Ord(T) OR RiverMask
    else
        temp := Ord(T);

    Assert(NOT ((t = Ocean) AND (Res > Nothing)), 'Resource in Ocean square ' + IntToStr(W) + IntToStr(H));

    if Res <> Nothing then
    begin
        // Bonus resources, bits are $20 & $40 }
        fBody[MapIndex(W, H)].Terrain := Temp OR (Ord(Res) SHL 5);
        fBody[MapIndex(W, H)].Special := NullSpecial;
    end
    else
    if Special <> NoSpecial then
    begin
        // DeadLands & Specials
        fBody[MapIndex(W, H)].Terrain := Temp;
        fBody[MapIndex(W, H)].Special := 2*Ord(Special) -1;
    end
    else
    begin
        fBody[MapIndex(W, H)].Terrain := Temp;
        fBody[MapIndex(W, H)].Special := NullSpecial;
    end;

    with fBody[MapIndex(W, H)] do
        // Check Terrain value valid
        Assert((Terrain AND $0F >= 0) AND (Terrain AND $0F <= $0B));

    with fBody[MapIndex(W, H)] do
        // Check Resource value valid
        Assert((Terrain AND $E0) IN [$00, $20, $40, $80, $A0, $C0]);

    if Start then
        fBody[MapIndex(W, H)].StartPos := $40;
end;


procedure TMapFile.SaveTileData(MyMap: TMap);
var W, H: Integer;

begin
    //MyMap.PlaceStartPositions;

    for H := 1 to Height do
        for W := 1 to Width do
            SaveTile(W, H, MyMap.GetTerrain(W, H),
                MyMap.Tiles[W, H].River,
                MyMap.GetBonus(W, H),
                MyMap.GetSpecial(W, H),
                MyMap.StartPosition(W, H));
end;


// =========================  INI File routines ===========================
procedure TmapFile.ReadINIFile;
var MyPath,
    inifilename: String;
begin
    MyPath      := GetAppConfigDir(False);
    inifilename := MyPath + 'CivMapFile.INI';
    fIniFile    := TINIFile.Create(inifilename);
    fLoadPath   := fIniFile.ReadString('MAPPATHS', 'LOAD', MyPath);
    fSavePath   := fIniFile.ReadString('MAPPATHS', 'SAVE', MyPath);

    // No default for file name
    fSaveFileName := fIniFile.ReadString('MAPPATHS', 'NAME', '');
end;

procedure TmapFile.WriteINIFile;
begin
    fIniFile.WriteString('MAPPATHS', 'LOAD', fLoadPath);
    fIniFile.WriteString('MAPPATHS', 'SAVE', fSavePath);
    fIniFile.WriteString('MAPPATHS', 'NAME', fSaveFileName);
end;


initialization

    Assert (Sizeof (tMapFile.tHeader) = headerSize);
end.