This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapcomp.c
387 lines (297 loc) · 7.37 KB
/
mapcomp.c
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
#define VERSION "1.0"
/*
=============================================================================
MAPCOMP
by John Carmack
=============================================================================
*/
#include "cmdlib.h"
#include "doomdata.h"
#define MAXPATH 1025
//================
//
// wad file types
//
//================
typedef struct
{
long filepos;
long size;
char name[8];
} lumpinfo_t;
typedef struct
{
char identification[4];
long numlumps;
long infotableofs;
} wadinfo_t;
//==========================================================================
wadinfo_t newwad; // the file being written out
lumpinfo_t *newlumps;
int newhandle;
lumpinfo_t *lump_p; // wadlumps[lumpon]
int lumpon; // current lump
char loadwadpath[MAXPATH];
char lumppath[MAXPATH];
int datahandle; // can be an individual file or a WAD
wadinfo_t loadwad; // a WAD file to look for lumps in
lumpinfo_t *wadlumps;
lumpinfo_t *wadlump_p; // info on lump to copy
//============================================================================
int numtextures = 0;
char texnames[2048][8];
/*
==================
=
= AddTextureFile
=
==================
*/
void AddTextureFile (char *name)
{
maptexture_t *mtexture;
int i;
int *maptex;
int offset, maxoff;
int numtextures1;
int *directory;
//
// load the map texture definitions from
//
maxoff = LoadFile (name, (void **)&maptex);
numtextures1 = LittleLong(*maptex);
directory = maptex+1;
for (i=0 ; i<numtextures1 ; i++, directory++)
{
offset = LittleLong(*directory);
if (offset > maxoff)
Error ("InitTextures: bad texture directory");
mtexture = (maptexture_t *) ( (byte *)maptex + offset);
memcpy (texnames[numtextures], mtexture->name,8);
numtextures++;
}
free (maptex);
}
/*
================
=
= TextureNumForName
=
================
*/
int TextureNumForName (char *name)
{
int i;
char namet[9];
if (name[0] == '-') // no texture marker
return 0;
for (i=0 ; i<numtextures ; i++)
if (!strncasecmp (texnames[i], name, 8) )
return i;
namet[8] = 0;
memcpy (namet, name,8);
Error ("TextureNumForName: %s not found",namet);
return -1;
}
//============================================================================
/*
=================
=
= CopyLump
=
=================
*/
void CopyLump (void)
{
long size;
byte *buffer;
lseek (datahandle, LittleLong (wadlump_p->filepos) ,SEEK_SET);
size = LittleLong (wadlump_p->size);
strncpy (lump_p->name , wadlump_p->name , 8);
lump_p->filepos = LittleLong (tell(newhandle));
lump_p->size = LittleLong (size);
buffer = malloc (size);
SafeRead (datahandle, buffer, size);
SafeWrite (newhandle, buffer, size);
free (buffer);
lumpon++;
lump_p++;
wadlump_p++;
}
/*
=================
=
= OpenWad
=
=================
*/
void OpenWad (char *wadname)
{
//
// open it and read in header / lump directory
//
datahandle = SafeOpenRead (wadname);
SafeRead (datahandle,&loadwad,sizeof(loadwad));
loadwad.numlumps = LittleLong (loadwad.numlumps);
loadwad.infotableofs = LittleLong (loadwad.infotableofs);
if (strncmp(loadwad.identification,"IWAD",4))
Error ("Wad file %s doesn't have IWAD id\n",lumppath);
lseek (datahandle , loadwad.infotableofs , SEEK_SET);
wadlumps = wadlump_p = malloc ( loadwad.numlumps*sizeof(lumpinfo_t) );
SafeRead (datahandle, wadlumps , loadwad.numlumps*sizeof(lumpinfo_t));
}
/*
=====================
=
= OpenDest
=
=====================
*/
void OpenDest (char *name)
{
//
// get the output filename
//
printf ("Output wad file: %s\n", name);
newhandle = SafeOpenWrite (name);
newwad.numlumps = 0;
strncpy (newwad.identification, "IWAD", 4);
//
// allocate space for the lump directory
//
newlumps = malloc (0xfff0);
lump_p = newlumps;
lumpon = 0;
//
// position the file pointer to begin writing data
//
// leave space in the data file for the header
lseek (newhandle,sizeof(newwad),SEEK_SET);
}
/*
=================
=
= CrunchSidedefs
=
=================
*/
void CrunchSidedefs (void)
{
long size, newsize;
byte *buffer, *buf2;
int i, numsides;
mapsidedef_t *ms;
compmapsidedef_t *cms;
lseek (datahandle, LittleLong (wadlump_p->filepos) ,SEEK_SET);
size = LittleLong (wadlump_p->size);
buffer = malloc (size);
SafeRead (datahandle, buffer, size);
ms = (mapsidedef_t *)buffer;
numsides = size/sizeof(mapsidedef_t);
newsize = numsides*sizeof(compmapsidedef_t);
buf2 = malloc (newsize);
cms = (compmapsidedef_t *)buf2;
for (i=0 ; i<numsides ; i++)
{
cms->textureoffset = ms->textureoffset;
cms->rowoffset = ms->rowoffset;
cms->sector = ms->sector;
cms->toptexture = LittleShort (TextureNumForName (ms->toptexture));
cms->bottomtexture =LittleShort(TextureNumForName (ms->bottomtexture));
cms->midtexture = LittleShort (TextureNumForName (ms->midtexture));
ms++;
cms++;
}
strncpy (lump_p->name , wadlump_p->name , 8);
lump_p->filepos = LittleLong (tell(newhandle));
lump_p->size = LittleLong (newsize);
SafeWrite (newhandle, buf2, newsize);
free (buffer);
free (buf2);
printf ("%i sides, saved %i bytes\n", numsides, size - newsize);
lumpon++;
lump_p++;
wadlump_p++;
}
/*
===================
=
= CrunchWad
=
===================
*/
void CrunchWad (char *name)
{
char dest[MAXPATH];
OpenWad (name);
if (loadwad.numlumps != 10 || wadlumps[0].name[0] != 'E'
|| wadlumps[0].name[2] != 'M' || wadlumps[0].name[4] != 0 )
Error ("wad file does not seem to be an uncompressed doom map");
strcpy (dest, name);
StripExtension (dest);
strcat (dest,"c.wad");
OpenDest (dest);
wadlump_p->name[4] = 'C';
wadlump_p->name[5] = 0;
CopyLump ();
// copy label with a C appended
CopyLump ();
// copy things
CopyLump ();
// copy linedefs
CrunchSidedefs (); // compress sidedefs
CopyLump ();
// copy vertexes
CopyLump ();
// copy segs
CopyLump ();
// copy ssectors
CopyLump ();
// copy nodes
CopyLump ();
// copy sectors
CopyLump ();
// copy blockmap
close (datahandle);
//
// write lumpinfo table
//
newwad.numlumps = LittleLong (lumpon);
newwad.infotableofs = LittleLong (tell(newhandle));
write (newhandle,newlumps, lumpon*sizeof(lumpinfo_t));
//
// write wadinfo
//
lseek (newhandle,0,SEEK_SET);
write (newhandle,&newwad,sizeof(newwad));
close (newhandle);
}
/*
===================
=
= main
=
===================
*/
int main (int argc, char **argv)
{
int i, s;
printf ("\nMAPCOMP "VERSION" by John Carmack, copyright (c) 1993 Id Software\n");
if (argc == 1)
{
printf (
"Usage: mapcomp texturefile [texturefiles] -wads e1m1.wad ...\n"
"generates e1m1c.wad ...\n"
);
exit (1);
}
s = CheckParm ("wads");
if (!s)
Error ("no -wads parm");
for (i=1 ; i<s ; i++)
AddTextureFile (argv[i]);
printf ("numtextures : %i\n",numtextures);
for (i=s+1 ; i<argc ; i++)
CrunchWad (argv[i]);
return 0;
}