forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dos.Structures.pas
397 lines (347 loc) · 11.9 KB
/
Dos.Structures.pas
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
unit Dos.Structures;
{$include lem_directives.inc}
interface
uses
Classes, SysUtils, Types,
GR32,
Dos.Consts,
Base.Utils, Base.Bitmaps,
Prog.Data;
// LVL raw level file structure consts as used by dos en winlemmings and probably many other platforms.
const
LVL_MAXOBJECTCOUNT = 32;
LVL_MAXTERRAINCOUNT = 400;
LVL_MAXSTEELCOUNT = 32;
type
TLVLObject = packed record
case Byte of
0: ( AsInt64: Int64 );
1: ( D0, D1: DWord);
2: ( W0, W1, W2, W3: Word);
3: ( B0, B1, B2, B3, B4, B5, B6, B7: Byte);
4: (
XPos : Word; // swap lo and hi
YPos : Word; // swap lo and hi
ObjectID : Word;
Modifier : Byte;
DisplayMode : Byte; // $8F = invert; $0F = normal
);
end;
{
bits 0..3 = modifier
bits 4..7 shl 8 for xpos
bits 8..15 add to xpos
bits 16..24 9 bits number YPos
}
TLVLTerrain = packed record
case Byte of
0: ( D0: DWord );
1: ( W0, W1: Word );
2: ( B0, B1, B2, B3: Byte );
3: (
XPos : Word;
YPos : Byte; // 9 bits
TerrainID : Byte;
);
end;
TLVLSteel = packed record
case Byte of
0: ( D0: DWord );
1: ( W0, W1: Word);
2: ( B0, B1, B2, B3: Byte);
3: (
XPos : Byte; // 9 bits
YPos : Byte; // 7 bits bits 1..7
Area : Byte; // bits 0..3 is height in 4 pixel units (then add 4)
// bit 4..7 is width in 4 pixel units (then add 4)
b : Byte; // always zero
);
end;
TLVLTitle = array[0..31] of AnsiChar;
TLVLObjects = array[0..LVL_MAXOBJECTCOUNT - 1] of TLVLObject;
TLVLTerrains = array[0..LVL_MAXTERRAINCOUNT - 1] of TLVLTerrain;
TLVLSteels = array[0..LVL_MAXSTEELCOUNT - 1] of TLVLSteel;
// the dos level record 2048 bytes
PLVLRec = ^TLVLRec;
TLVLRec = packed record
public
{0000} ReleaseRate : Word; // big endian, swap!
{ } LemmingsCount : Word; // big endian, swap!
{ } RescueCount : Word; // big endian, swap!
{ } TimeLimit : Word; // big endian, swap!
{0008} ClimberCount : Word; // big endian, swap!
{ } FloaterCount : Word; // big endian, swap!
{ } BomberCount : Word; // big endian, swap!
{ } BlockerCount : Word; // big endian, swap!
{ } BuilderCount : Word; // big endian, swap!
{ } BasherCount : Word; // big endian, swap!
{ } MinerCount : Word; // big endian, swap!
{ } DiggerCount : Word; // big endian, swap!
{0018} ScreenPosition : Word; // big endian, swap!
{001A} GraphicSet : Word; // big endian, swap!
{ } GraphicSetEx : Word; // big endian, swap!
{001E} Reserved : Word; // big endian, swap! $FFFF if SuperLemming else $0000
{0020} Objects : TLVLObjects;
{0120} Terrain : TLVLTerrains;
{0760} Steel : TLVLSteels;
{07E0} LevelName : TLVLTitle; // 32 bytes
public
function SwapProp(aProp: Word): Word; inline;
function GetObjectCount(out entranceCount, exitCount: Integer): Integer;
function GetTerrainCount: Integer;
function GetSteelCount: Integer;
function IsSuperLemming: Boolean; inline;
end;
const
LVL_SIZE = SizeOf(TLVLRec);
LVL_TITLE_OFFSET = $750;
{-------------------------------------------------------------------------------
GROUNDXX.DAT files (1056 bytes) (16 objects, 64 terrain, color palette)
• Somewhere there is documentation for details. todo: retrieve that.
• Little Endian words, so we can just load them from disk halleluyah!
-------------------------------------------------------------------------------}
type
PDosMetaObject = ^TDosMetaObject;
TDosMetaObject = packed record
oAnimation_flags : Word; // 2
oStart_animation_frame_index : Byte; // 3
oAnimation_frame_count : Byte; // 4
oWidth : Byte; // 5
oHeight : Byte; // 6
oAnimation_frame_data_size : Word; // 8 size in bytes of each animation frame
oMask_offset_from_image : Word; // 10
oUnknown1 : Word; // 12
oUnknown2 : Word; // 14
oTrigger_left : Word; // 16
oTrigger_top : Word; // 18
oTrigger_width : Byte; // 19
oTrigger_height : Byte; // 20
oTrigger_effect_id : Byte; // 21
oAnimation_frames_base_loc : Word; // 23
oPreview_image_location : Word; // 25
oUnknown3 : Word; // 27
oSound_effect_id : Byte; // 28
end;
TDosMetaObjectArray = packed array[0..15] of TDOSMetaObject;
PDosMetaTerrain = ^TDosMetaTerrain;
TDosMetaTerrain = packed record
tWidth : Byte;
tHeight : Byte;
tImage_loc : Word;
tMask_loc : Word;
tUnknown1 : Word
end;
TDosMetaTerrainArray = packed array[0..63] of TDosMetaTerrain;
TDosEGAPalette8 = packed array[0..7] of Byte;
PDosVgaColorRec = ^TDosVgaColorRec;
TDosVgaColorRec = packed record
R, G, B: Byte;
end;
TDosVGAPalette8 = packed array[0..7] of TDosVGAColorRec;
// this is the total structure of a dos ground?.dat
TDosGroundRec = packed record
ObjectInfoArray : TDosMetaObjectArray;
TerrainInfoArray : TDosMetaTerrainArray;
EGA_PaletteCustom : TDosEGAPalette8;
EGA_PaletteStandard : TDOSEGAPalette8;
EGA_PalettePreview : TDOSEGAPalette8;
VGA_PaletteCustom : TDOSVGAPalette8;
VGA_PaletteStandard : TDOSVGAPalette8;
VGA_PalettePreview : TDOSVGAPalette8;
end;
TDosVGAPalette16 = packed array[0..15] of TDosVGAColorRec;
TDosVgaSpecPaletteHeader = packed record
VgaPal: TDosVGAPalette8; // 24
EgaPal: TDosEGAPalette8; // 8
UnknownPal: array[0..7] of Byte; // maybe even less colors
end;
// big endian words, swap!
TDosOddTableRec = packed record
ReleaseRate : Word;
LemmingsCount : Word;
RescueCount : Word;
TimeLimit : Word;
ClimberCount : Word;
FloaterCount : Word;
BomberCount : Word;
BlockerCount : Word;
BuilderCount : Word;
BasherCount : Word;
MinerCount : Word;
DiggerCount : Word;
LevelName : array[0..31] of AnsiChar;
end;
type
// So this is a very non-oop record to easily load the oddtable in records
TDosOddTable = record
public
Recs: array of TDosOddTableRec;
procedure Clear;
procedure LoadFromFile(const aStylename, aFilename: string);
procedure LoadFromStream(S: TStream);
end;
const
// These values do *need* to be converted (about shl 2)
DosInLevelPalettes: array[Boolean] of TDosVGAPalette8 = (
// the default palette (christmas = false)
(
(R: 000; G: 000; B: 000), // black
(R: 016; G: 016; B: 056), // blue
(R: 000; G: 044; B: 000), // green
(R: 060; G: 052; B: 052), // white
(R: 044; G: 044; B: 000), // yellow
(R: 060; G: 008; B: 008), // red
(R: 032; G: 032; B: 032), // gray
(R: 000; G: 000; B: 000) // not used: probably this color is replaced with the standard palette entry in ground??.dat
),
// the christmas palette (christmas = true)
(
(R: 000; G: 000; B: 000), // black
(R: 060; G: 008; B: 008), // red
(R: 000; G: 044; B: 000), // green
(R: 060; G: 052; B: 052), // white
(R: 044; G: 044; B: 000), // yellow
(R: 016; G: 016; B: 056), // blue
(R: 032; G: 032; B: 032), // gray
(R: 000; G: 000; B: 000) // not used: probably this color is replaced with the standard palette entry in ground??.dat
)
);
const
{ These values do not need to be converted }
DosMainMenuPalette: TDosVGAPalette16 = (
(R: 000; G: 000; B: 000), // black
(R: 128; G: 064; B: 032), // browns
(R: 096; G: 048; B: 032),
(R: 048; G: 000; B: 016),
(R: 032; G: 008; B: 124), // purples
(R: 064; G: 044; B: 144),
(R: 104; G: 088; B: 164),
(R: 152; G: 140; B: 188),
(R: 000; G: 080; B: 000), // greens
(R: 000; G: 096; B: 016),
(R: 000; G: 112; B: 032),
(R: 000; G: 128; B: 064),
(R: 208; G: 208; B: 208), // white
(R: 176; G: 176; B: 000), // yellow
(R: 064; G: 080; B: 176), // blue
(R: 224; G: 128; B: 144) // pink
);
function DosVgaColorToColor32(const ColorRec: TDosVgaColorRec): TColor32;
function GetDosMainMenuPaletteColors32: TArrayOfColor32;
procedure DosVgaPalette8ToLemmixPalette(const DosPal: TDosVGAPalette8; var LemmixPal: TArrayOfColor32);
implementation
function GetDosMainMenuPaletteColors32: TArrayOfColor32;
var
i: Integer;
P: PColor32;
begin
SetLength(Result, 16);
P := @Result[0];
for i := 0 to 15 do begin
P^.A := 0;
P^.R := DosMainMenuPalette[i].R;
P^.G := DosMainMenuPalette[i].G;
P^.B := DosMainMenuPalette[i].B;
Inc(P); // typed ptr increment
end;
end;
function DosVgaColorToColor32(const ColorRec: TDosVgaColorRec): TColor32;
begin
Result.R := ColorRec.R * 4;
Result.G := ColorRec.G * 4;
Result.B := ColorRec.B * 4;
end;
{ TLVLRec }
function TLVLRec.SwapProp(aProp: Word): Word;
begin
Result := System.Swap(aProp);
end;
function TLVLRec.GetObjectCount(out entranceCount, exitCount: Integer): Integer;
var
O: TLVLObject;
i: Integer;
begin
Result := 0;
entranceCount := 0;
exitCount := 0;
for i := 0 to LVL_MAXOBJECTCOUNT - 1 do begin
O := Objects[i];
if O.AsInt64 <> 0 then begin
Inc(Result);
case Integer(O.B5 and 15) of
DOS_OBJECT_ID_EXIT: Inc(exitCount);
DOS_OBJECT_ID_ENTRANCE: Inc(entranceCount);
end;
end;
end;
end;
function TLVLRec.GetTerrainCount: Integer;
var
T: TLVLTerrain;
i: Integer;
begin
Result := 0;
for i := 0 to LVL_MAXTERRAINCOUNT - 1 do begin
T := Terrain[i];
if T.D0 <> $FFFFFFFF then
Inc(Result);
end;
end;
function TLVLRec.GetSteelCount: Integer;
var
S: TLVLSteel;
i: Integer;
begin
Result := 0;
for i := 0 to LVL_MAXSTEELCOUNT - 1 do begin
S := Steel[i];
if S.D0 <> 0 then
Inc(Result);
end;
end;
function TLVLRec.IsSuperLemming: Boolean;
begin
Result := Reserved = $FFFF;
end;
{TDosOddTable}
procedure TDosOddTable.Clear;
begin
SetLength(Recs, 0);
end;
procedure TDosOddTable.LoadFromFile(const aStylename, aFilename: string);
var
F: TStream;
begin
F := TData.CreateDataStream(aStylename, aFileName, TDataType.LemmingData);
try
LoadFromStream(F);
finally
F.Free;
end;
end;
procedure TDosOddTable.LoadFromStream(S: TStream);
var
i: Integer;
begin
SetLength(Recs, S.Size div SizeOf(TDosOddTableRec));
for i := 0 to Length(Recs) - 1 do
S.ReadBuffer(Recs[i], Sizeof(TDosOddTableRec));
end;
procedure DosVgaPalette8ToLemmixPalette(const DosPal: TDosVGAPalette8; var LemmixPal: TArrayOfColor32);
// 6 --> 8 bit color conversions
var
i: Integer;
E: PColor32Entry;
D: PDosVgaColorRec;
begin
SetLength(LemmixPal, 8);
for i := 0 to 7 do begin
E := @LemmixPal[i];
D := @DosPal[i];
E^.A := 0;
E^.R := (Integer(D^.R) * 255) div 63;
E^.G := (Integer(D^.G) * 255) div 63;
E^.B := (Integer(D^.B) * 255) div 63;
end;
end;
end.