1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 -----------------------------------
21 MAPREADER.PAS ÂÅÐÑÈß ÎÒ 13.11.07
23 Ïîääåðæêà êàðò âåðñèè 1
24 -----------------------------------
33 TDataBlock
= packed record
38 TDataBlocksArray
= packed array of TDataBlock
;
40 TMapReader
= class(TObject
)
44 FDataBlocks
: TDataBlocksArray
;
45 function GetBlocks(BlocksType
: Byte): TDataBlocksArray
;
48 destructor Destroy(); override;
49 function LoadMap(Data
: Pointer): Boolean;
51 function HandledVersion(): Byte; virtual;
53 property GetError
: Byte read FError
;
54 property GetVersion
: Byte read FVersion
;
57 TMapReader_1
= class(TMapReader
)
60 function GetMapHeader(): TMapHeaderRec_1
;
61 function GetTextures(): TTexturesRec1Array
;
62 function GetPanels(): TPanelsRec1Array
;
63 function GetItems(): TItemsRec1Array
;
64 function GetAreas(): TAreasRec1Array
;
65 function GetMonsters(): TMonsterRec1Array
;
66 function GetTriggers(): TTriggersRec1Array
;
67 function HandledVersion(): Byte; override;
72 MAP_ERROR_SIGNATURE
= $01;
73 MAP_ERROR_VERSION
= $02;
80 function g_Texture_NumNameFindStart(name
: String): Boolean;
81 function g_Texture_NumNameFindNext(var newName
: String): Byte;
89 NNF_PureName
: String; // Èìÿ òåêñòóðû áåç öèôð â êîíöå
90 NNF_FirstNum
: Integer; // ×èñëî ó íà÷àëüíîé òåêñòóðû
91 NNF_CurrentNum
: Integer; // Ñëåäóþùåå ÷èñëî ó òåêñòóðû
93 function g_Texture_NumNameFindStart(name
: String): Boolean;
101 NNF_CurrentNum
:= -1;
103 for i
:= Length(name
) downto 1 do
104 if (name
[i
] = '_') then // "_" - ñèìâîë íà÷àëà íîìåðíîãî ïîñòôèêñà
106 if i
= Length(name
) then
107 begin // Íåò öèôð â êîíöå ñòðîêè
112 NNF_PureName
:= Copy(name
, 1, i
);
118 // Íå ïåðåâåñòè â ÷èñëî:
119 if not TryStrToInt(name
, NNF_FirstNum
) then
127 function g_Texture_NumNameFindNext(var newName
: String): Byte;
129 if (NNF_PureName
= '') or (NNF_CurrentNum
< 0) then
132 Result
:= NNF_NO_NAME
;
136 newName
:= NNF_PureName
+ IntToStr(NNF_CurrentNum
);
138 if NNF_CurrentNum
< NNF_FirstNum
then
139 Result
:= NNF_NAME_BEFORE
141 if NNF_CurrentNum
> NNF_FirstNum
then
142 Result
:= NNF_NAME_AFTER
144 Result
:= NNF_NAME_EQUALS
;
149 { T M a p R e a d e r _ 1 : }
151 function TMapReader_1
.GetAreas(): TAreasRec1Array
;
153 TempDataBlocks
: TDataBlocksArray
;
159 TempDataBlocks
:= GetBlocks(BLOCK_AREAS
);
161 if TempDataBlocks
= nil then Exit
;
163 size
:= SizeOf(TAreaRec_1
);
165 for a
:= 0 to High(TempDataBlocks
) do
166 for b
:= 0 to (TempDataBlocks
[a
].Block
.BlockSize
div size
)-1 do
168 SetLength(Result
, Length(Result
)+1);
169 CopyMemory(@Result
[High(Result
)], Pointer(LongWord(TempDataBlocks
[a
].Data
)+b
*size
), size
);
172 TempDataBlocks
:= nil;
175 function TMapReader_1
.GetItems(): TItemsRec1Array
;
177 TempDataBlocks
: TDataBlocksArray
;
183 TempDataBlocks
:= GetBlocks(BLOCK_ITEMS
);
185 if TempDataBlocks
= nil then Exit
;
187 size
:= SizeOf(TItemRec_1
);
189 for a
:= 0 to High(TempDataBlocks
) do
190 for b
:= 0 to (TempDataBlocks
[a
].Block
.BlockSize
div size
)-1 do
192 SetLength(Result
, Length(Result
)+1);
193 CopyMemory(@Result
[High(Result
)], Pointer(LongWord(TempDataBlocks
[a
].Data
)+b
*size
), size
);
196 TempDataBlocks
:= nil;
199 function TMapReader_1
.GetMapHeader(): TMapHeaderRec_1
;
201 TempDataBlocks
: TDataBlocksArray
;
203 ZeroMemory(@Result
, SizeOf(TMapHeaderRec_1
));
205 TempDataBlocks
:= GetBlocks(BLOCK_HEADER
);
207 if TempDataBlocks
= nil then Exit
;
209 CopyMemory(@Result
, TempDataBlocks
[0].Data
, SizeOf(TMapHeaderRec_1
));
211 TempDataBlocks
:= nil;
214 function TMapReader_1
.GetMonsters(): TMonsterRec1Array
;
216 TempDataBlocks
: TDataBlocksArray
;
222 TempDataBlocks
:= GetBlocks(BLOCK_MONSTERS
);
224 if TempDataBlocks
= nil then Exit
;
226 size
:= SizeOf(TMonsterRec_1
);
228 for a
:= 0 to High(TempDataBlocks
) do
229 for b
:= 0 to (TempDataBlocks
[a
].Block
.BlockSize
div size
)-1 do
231 SetLength(Result
, Length(Result
)+1);
232 CopyMemory(@Result
[High(Result
)], Pointer(LongWord(TempDataBlocks
[a
].Data
)+b
*size
), size
);
235 TempDataBlocks
:= nil;
238 function TMapReader_1
.GetPanels(): TPanelsRec1Array
;
240 TempDataBlocks
: TDataBlocksArray
;
246 TempDataBlocks
:= GetBlocks(BLOCK_PANELS
);
248 if TempDataBlocks
= nil then Exit
;
250 size
:= SizeOf(TPanelRec_1
);
252 for a
:= 0 to High(TempDataBlocks
) do
253 for b
:= 0 to (TempDataBlocks
[a
].Block
.BlockSize
div size
)-1 do
255 SetLength(Result
, Length(Result
)+1);
256 CopyMemory(@Result
[High(Result
)], Pointer(LongWord(TempDataBlocks
[a
].Data
)+b
*size
), size
);
259 TempDataBlocks
:= nil;
262 function TMapReader_1
.GetTextures(): TTexturesRec1Array
;
264 TempDataBlocks
: TDataBlocksArray
;
270 TempDataBlocks
:= GetBlocks(BLOCK_TEXTURES
);
272 if TempDataBlocks
= nil then Exit
;
274 size
:= SizeOf(TTextureRec_1
);
276 for a
:= 0 to High(TempDataBlocks
) do
277 for b
:= 0 to (TempDataBlocks
[a
].Block
.BlockSize
div size
)-1 do
279 SetLength(Result
, Length(Result
)+1);
280 CopyMemory(@Result
[High(Result
)], Pointer(LongWord(TempDataBlocks
[a
].Data
)+b
*size
), size
);
283 TempDataBlocks
:= nil;
286 function TMapReader_1
.GetTriggers(): TTriggersRec1Array
;
288 TempDataBlocks
: TDataBlocksArray
;
294 TempDataBlocks
:= GetBlocks(BLOCK_TRIGGERS
);
296 if TempDataBlocks
= nil then Exit
;
298 size
:= SizeOf(TTriggerRec_1
);
300 for a
:= 0 to High(TempDataBlocks
) do
301 for b
:= 0 to (TempDataBlocks
[a
].Block
.BlockSize
div size
)-1 do
303 SetLength(Result
, Length(Result
)+1);
304 CopyMemory(@Result
[High(Result
)], Pointer(LongWord(TempDataBlocks
[a
].Data
)+b
*size
), size
);
307 TempDataBlocks
:= nil;
310 function TMapReader_1
.HandledVersion
: Byte;
315 { T M a p R e a d e r : }
317 constructor TMapReader
.Create();
320 FError
:= MAP_ERROR_NONE
;
324 destructor TMapReader
.Destroy();
331 procedure TMapReader
.FreeMap();
335 if FDataBlocks
<> nil then
336 for a
:= 0 to High(FDataBlocks
) do
337 if FDataBlocks
[a
].Data
<> nil then FreeMem(FDataBlocks
[a
].Data
);
341 FError
:= MAP_ERROR_NONE
;
344 function TMapReader
.GetBlocks(BlocksType
: Byte): TDataBlocksArray
;
350 if FDataBlocks
= nil then Exit
;
352 for a
:= 0 to High(FDataBlocks
) do
353 if FDataBlocks
[a
].Block
.BlockType
= BlocksType
then
355 SetLength(Result
, Length(Result
)+1);
356 Result
[High(Result
)] := FDataBlocks
[a
];
360 function TMapReader
.HandledVersion(): Byte;
365 function TMapReader
.LoadMap(Data
: Pointer): Boolean;
369 Sign
: array[0..2] of Char;
374 CopyMemory(@Sign
[0], Data
, 3);
375 if Sign
<> MAP_SIGNATURE
then
377 FError
:= MAP_ERROR_SIGNATURE
;
382 CopyMemory(@Ver
, Pointer(LongWord(Data
)+adr
), 1);
384 if Ver
> HandledVersion() then
386 FError
:= MAP_ERROR_VERSION
;
392 SetLength(FDataBlocks
, Length(FDataBlocks
)+1);
393 _id
:= High(FDataBlocks
);
395 CopyMemory(@FDataBlocks
[_id
].Block
, Pointer(LongWord(Data
)+adr
), SizeOf(TBlock
));
396 adr
:= adr
+SizeOf(TBlock
);
398 FDataBlocks
[_id
].Data
:= GetMemory(FDataBlocks
[_id
].Block
.BlockSize
);
400 CopyMemory(FDataBlocks
[_id
].Data
, Pointer(LongWord(Data
)+adr
), FDataBlocks
[_id
].Block
.BlockSize
);
402 adr
:= adr
+FDataBlocks
[_id
].Block
.BlockSize
;
403 until FDataBlocks
[_id
].Block
.BlockType
= BLOCK_NONE
;