Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 894 lines (755 sloc) 24.508 kb
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Foobar; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 #include "l_cmd.h"
23 #include "l_math.h"
24 #include "l_mem.h"
25 #include "l_log.h"
e9a8f99 @bnoordhuis Fix include paths.
authored
26 #include "botlib/l_script.h"
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
27 #include "l_bsp_hl.h"
28 #include "l_bsp_ent.h"
29
30 //=============================================================================
31
32 int hl_nummodels;
33 hl_dmodel_t *hl_dmodels;//[HL_MAX_MAP_MODELS];
34 int hl_dmodels_checksum;
35
36 int hl_visdatasize;
37 byte *hl_dvisdata;//[HL_MAX_MAP_VISIBILITY];
38 int hl_dvisdata_checksum;
39
40 int hl_lightdatasize;
41 byte *hl_dlightdata;//[HL_MAX_MAP_LIGHTING];
42 int hl_dlightdata_checksum;
43
44 int hl_texdatasize;
45 byte *hl_dtexdata;//[HL_MAX_MAP_MIPTEX]; // (dmiptexlump_t)
46 int hl_dtexdata_checksum;
47
48 int hl_entdatasize;
49 char *hl_dentdata;//[HL_MAX_MAP_ENTSTRING];
50 int hl_dentdata_checksum;
51
52 int hl_numleafs;
53 hl_dleaf_t *hl_dleafs;//[HL_MAX_MAP_LEAFS];
54 int hl_dleafs_checksum;
55
56 int hl_numplanes;
57 hl_dplane_t *hl_dplanes;//[HL_MAX_MAP_PLANES];
58 int hl_dplanes_checksum;
59
60 int hl_numvertexes;
61 hl_dvertex_t *hl_dvertexes;//[HL_MAX_MAP_VERTS];
62 int hl_dvertexes_checksum;
63
64 int hl_numnodes;
65 hl_dnode_t *hl_dnodes;//[HL_MAX_MAP_NODES];
66 int hl_dnodes_checksum;
67
68 int hl_numtexinfo;
69 hl_texinfo_t *hl_texinfo;//[HL_MAX_MAP_TEXINFO];
70 int hl_texinfo_checksum;
71
72 int hl_numfaces;
73 hl_dface_t *hl_dfaces;//[HL_MAX_MAP_FACES];
74 int hl_dfaces_checksum;
75
76 int hl_numclipnodes;
77 hl_dclipnode_t *hl_dclipnodes;//[HL_MAX_MAP_CLIPNODES];
78 int hl_dclipnodes_checksum;
79
80 int hl_numedges;
81 hl_dedge_t *hl_dedges;//[HL_MAX_MAP_EDGES];
82 int hl_dedges_checksum;
83
84 int hl_nummarksurfaces;
85 unsigned short *hl_dmarksurfaces;//[HL_MAX_MAP_MARKSURFACES];
86 int hl_dmarksurfaces_checksum;
87
88 int hl_numsurfedges;
89 int *hl_dsurfedges;//[HL_MAX_MAP_SURFEDGES];
90 int hl_dsurfedges_checksum;
91
92 //int num_entities;
93 //entity_t entities[HL_MAX_MAP_ENTITIES];
94
95
96 //#ifdef //ME
97
98 int hl_bspallocated = false;
99 int hl_allocatedbspmem = 0;
100
101 void HL_AllocMaxBSP(void)
102 {
103 //models
104 hl_nummodels = 0;
105 hl_dmodels = (hl_dmodel_t *) GetMemory(HL_MAX_MAP_MODELS * sizeof(hl_dmodel_t));
106 hl_allocatedbspmem = HL_MAX_MAP_MODELS * sizeof(hl_dmodel_t);
107 //visibility
108 hl_visdatasize = 0;
109 hl_dvisdata = (byte *) GetMemory(HL_MAX_MAP_VISIBILITY * sizeof(byte));
110 hl_allocatedbspmem += HL_MAX_MAP_VISIBILITY * sizeof(byte);
111 //light data
112 hl_lightdatasize = 0;
113 hl_dlightdata = (byte *) GetMemory(HL_MAX_MAP_LIGHTING * sizeof(byte));
114 hl_allocatedbspmem += HL_MAX_MAP_LIGHTING * sizeof(byte);
115 //texture data
116 hl_texdatasize = 0;
117 hl_dtexdata = (byte *) GetMemory(HL_MAX_MAP_MIPTEX * sizeof(byte)); // (dmiptexlump_t)
118 hl_allocatedbspmem += HL_MAX_MAP_MIPTEX * sizeof(byte);
119 //entities
120 hl_entdatasize = 0;
121 hl_dentdata = (char *) GetMemory(HL_MAX_MAP_ENTSTRING * sizeof(char));
122 hl_allocatedbspmem += HL_MAX_MAP_ENTSTRING * sizeof(char);
123 //leaves
124 hl_numleafs = 0;
125 hl_dleafs = (hl_dleaf_t *) GetMemory(HL_MAX_MAP_LEAFS * sizeof(hl_dleaf_t));
126 hl_allocatedbspmem += HL_MAX_MAP_LEAFS * sizeof(hl_dleaf_t);
127 //planes
128 hl_numplanes = 0;
129 hl_dplanes = (hl_dplane_t *) GetMemory(HL_MAX_MAP_PLANES * sizeof(hl_dplane_t));
130 hl_allocatedbspmem += HL_MAX_MAP_PLANES * sizeof(hl_dplane_t);
131 //vertexes
132 hl_numvertexes = 0;
133 hl_dvertexes = (hl_dvertex_t *) GetMemory(HL_MAX_MAP_VERTS * sizeof(hl_dvertex_t));
134 hl_allocatedbspmem += HL_MAX_MAP_VERTS * sizeof(hl_dvertex_t);
135 //nodes
136 hl_numnodes = 0;
137 hl_dnodes = (hl_dnode_t *) GetMemory(HL_MAX_MAP_NODES * sizeof(hl_dnode_t));
138 hl_allocatedbspmem += HL_MAX_MAP_NODES * sizeof(hl_dnode_t);
139 //texture info
140 hl_numtexinfo = 0;
141 hl_texinfo = (hl_texinfo_t *) GetMemory(HL_MAX_MAP_TEXINFO * sizeof(hl_texinfo_t));
142 hl_allocatedbspmem += HL_MAX_MAP_TEXINFO * sizeof(hl_texinfo_t);
143 //faces
144 hl_numfaces = 0;
145 hl_dfaces = (hl_dface_t *) GetMemory(HL_MAX_MAP_FACES * sizeof(hl_dface_t));
146 hl_allocatedbspmem += HL_MAX_MAP_FACES * sizeof(hl_dface_t);
147 //clip nodes
148 hl_numclipnodes = 0;
149 hl_dclipnodes = (hl_dclipnode_t *) GetMemory(HL_MAX_MAP_CLIPNODES * sizeof(hl_dclipnode_t));
150 hl_allocatedbspmem += HL_MAX_MAP_CLIPNODES * sizeof(hl_dclipnode_t);
151 //edges
152 hl_numedges = 0;
153 hl_dedges = (hl_dedge_t *) GetMemory(HL_MAX_MAP_EDGES * sizeof(hl_dedge_t));
154 hl_allocatedbspmem += HL_MAX_MAP_EDGES, sizeof(hl_dedge_t);
155 //mark surfaces
156 hl_nummarksurfaces = 0;
157 hl_dmarksurfaces = (unsigned short *) GetMemory(HL_MAX_MAP_MARKSURFACES * sizeof(unsigned short));
158 hl_allocatedbspmem += HL_MAX_MAP_MARKSURFACES * sizeof(unsigned short);
159 //surface edges
160 hl_numsurfedges = 0;
161 hl_dsurfedges = (int *) GetMemory(HL_MAX_MAP_SURFEDGES * sizeof(int));
162 hl_allocatedbspmem += HL_MAX_MAP_SURFEDGES * sizeof(int);
163 //print allocated memory
164 Log_Print("allocated ");
165 PrintMemorySize(hl_allocatedbspmem);
166 Log_Print(" of BSP memory\n");
167 } //end of the function HL_AllocMaxBSP
168
169 void HL_FreeMaxBSP(void)
170 {
171 //models
172 hl_nummodels = 0;
173 FreeMemory(hl_dmodels);
174 hl_dmodels = NULL;
175 //visibility
176 hl_visdatasize = 0;
177 FreeMemory(hl_dvisdata);
178 hl_dvisdata = NULL;
179 //light data
180 hl_lightdatasize = 0;
181 FreeMemory(hl_dlightdata);
182 hl_dlightdata = NULL;
183 //texture data
184 hl_texdatasize = 0;
185 FreeMemory(hl_dtexdata);
186 hl_dtexdata = NULL;
187 //entities
188 hl_entdatasize = 0;
189 FreeMemory(hl_dentdata);
190 hl_dentdata = NULL;
191 //leaves
192 hl_numleafs = 0;
193 FreeMemory(hl_dleafs);
194 hl_dleafs = NULL;
195 //planes
196 hl_numplanes = 0;
197 FreeMemory(hl_dplanes);
198 hl_dplanes = NULL;
199 //vertexes
200 hl_numvertexes = 0;
201 FreeMemory(hl_dvertexes);
202 hl_dvertexes = NULL;
203 //nodes
204 hl_numnodes = 0;
205 FreeMemory(hl_dnodes);
206 hl_dnodes = NULL;
207 //texture info
208 hl_numtexinfo = 0;
209 FreeMemory(hl_texinfo);
210 hl_texinfo = NULL;
211 //faces
212 hl_numfaces = 0;
213 FreeMemory(hl_dfaces);
214 hl_dfaces = NULL;
215 //clip nodes
216 hl_numclipnodes = 0;
217 FreeMemory(hl_dclipnodes);
218 hl_dclipnodes = NULL;
219 //edges
220 hl_numedges = 0;
221 FreeMemory(hl_dedges);
222 hl_dedges = NULL;
223 //mark surfaces
224 hl_nummarksurfaces = 0;
225 FreeMemory(hl_dmarksurfaces);
226 hl_dmarksurfaces = NULL;
227 //surface edges
228 hl_numsurfedges = 0;
229 FreeMemory(hl_dsurfedges);
230 hl_dsurfedges = NULL;
231 //
232 Log_Print("freed ");
233 PrintMemorySize(hl_allocatedbspmem);
234 Log_Print(" of BSP memory\n");
235 hl_allocatedbspmem = 0;
236 } //end of the function HL_FreeMaxBSP
237 //#endif //ME
238
239 /*
240 ===============
241 FastChecksum
242 ===============
243 */
244
245 int FastChecksum(void *buffer, int bytes)
246 {
247 int checksum = 0;
248
e49a071 @bnoordhuis Gut FastChecksum(). It's an assignment with undefined behaviour, GCC doe...
authored
249 // the result of FastChecksum isn't actually used anywhere
250 // while( bytes-- )
251 // checksum = (checksum << 4) ^ *((char *)buffer)++;
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
252
253 return checksum;
254 }
255
256 /*
257 ===============
258 HL_CompressVis
259 ===============
260 */
261 int HL_CompressVis(byte *vis, byte *dest)
262 {
263 int j;
264 int rep;
265 int visrow;
266 byte *dest_p;
267
268 dest_p = dest;
269 visrow = (hl_numleafs + 7)>>3;
270
271 for (j=0 ; j<visrow ; j++)
272 {
273 *dest_p++ = vis[j];
274 if (vis[j])
275 continue;
276
277 rep = 1;
278 for ( j++; j<visrow ; j++)
279 if (vis[j] || rep == 255)
280 break;
281 else
282 rep++;
283 *dest_p++ = rep;
284 j--;
285 }
286
287 return dest_p - dest;
288 }
289
290
291 /*
292 ===================
293 HL_DecompressVis
294 ===================
295 */
296 void HL_DecompressVis (byte *in, byte *decompressed)
297 {
298 int c;
299 byte *out;
300 int row;
301
302 row = (hl_numleafs+7)>>3;
303 out = decompressed;
304
305 do
306 {
307 if (*in)
308 {
309 *out++ = *in++;
310 continue;
311 }
312
313 c = in[1];
314 in += 2;
315 while (c)
316 {
317 *out++ = 0;
318 c--;
319 }
320 } while (out - decompressed < row);
321 }
322
323 //=============================================================================
324
325 /*
326 =============
327 HL_SwapBSPFile
328
329 Byte swaps all data in a bsp file.
330 =============
331 */
332 void HL_SwapBSPFile (qboolean todisk)
333 {
601bf2d @bnoordhuis Fix warning: array subscript is above array bounds.
authored
334 int i, j, k, c;
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
335 hl_dmodel_t *d;
336 hl_dmiptexlump_t *mtl;
337
338
339 // models
340 for (i = 0; i < hl_nummodels; i++)
341 {
342 d = &hl_dmodels[i];
343
344 for (j = 0; j < HL_MAX_MAP_HULLS; j++)
345 d->headnode[j] = LittleLong(d->headnode[j]);
346
347 d->visleafs = LittleLong(d->visleafs);
348 d->firstface = LittleLong(d->firstface);
349 d->numfaces = LittleLong(d->numfaces);
350
351 for (j = 0; j < 3; j++)
352 {
353 d->mins[j] = LittleFloat(d->mins[j]);
354 d->maxs[j] = LittleFloat(d->maxs[j]);
355 d->origin[j] = LittleFloat(d->origin[j]);
356 }
357 }
358
359 //
360 // vertexes
361 //
362 for (i = 0; i < hl_numvertexes; i++)
363 {
364 for (j = 0; j < 3; j++)
365 hl_dvertexes[i].point[j] = LittleFloat (hl_dvertexes[i].point[j]);
366 }
367
368 //
369 // planes
370 //
371 for (i=0 ; i<hl_numplanes ; i++)
372 {
373 for (j=0 ; j<3 ; j++)
374 hl_dplanes[i].normal[j] = LittleFloat (hl_dplanes[i].normal[j]);
375 hl_dplanes[i].dist = LittleFloat (hl_dplanes[i].dist);
376 hl_dplanes[i].type = LittleLong (hl_dplanes[i].type);
377 }
378
379 //
380 // texinfos
381 //
382 for (i=0 ; i<hl_numtexinfo ; i++)
383 {
601bf2d @bnoordhuis Fix warning: array subscript is above array bounds.
authored
384 for (j=0 ; j<2 ; j++)
385 {
386 for (k=0; k<4; k++)
387 {
388 hl_texinfo[i].vecs[j][k] = LittleFloat (hl_texinfo[i].vecs[j][k]);
389 }
390 }
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
391 hl_texinfo[i].miptex = LittleLong (hl_texinfo[i].miptex);
392 hl_texinfo[i].flags = LittleLong (hl_texinfo[i].flags);
393 }
394
395 //
396 // faces
397 //
398 for (i=0 ; i<hl_numfaces ; i++)
399 {
400 hl_dfaces[i].texinfo = LittleShort (hl_dfaces[i].texinfo);
401 hl_dfaces[i].planenum = LittleShort (hl_dfaces[i].planenum);
402 hl_dfaces[i].side = LittleShort (hl_dfaces[i].side);
403 hl_dfaces[i].lightofs = LittleLong (hl_dfaces[i].lightofs);
404 hl_dfaces[i].firstedge = LittleLong (hl_dfaces[i].firstedge);
405 hl_dfaces[i].numedges = LittleShort (hl_dfaces[i].numedges);
406 }
407
408 //
409 // nodes
410 //
411 for (i=0 ; i<hl_numnodes ; i++)
412 {
413 hl_dnodes[i].planenum = LittleLong (hl_dnodes[i].planenum);
414 for (j=0 ; j<3 ; j++)
415 {
416 hl_dnodes[i].mins[j] = LittleShort (hl_dnodes[i].mins[j]);
417 hl_dnodes[i].maxs[j] = LittleShort (hl_dnodes[i].maxs[j]);
418 }
419 hl_dnodes[i].children[0] = LittleShort (hl_dnodes[i].children[0]);
420 hl_dnodes[i].children[1] = LittleShort (hl_dnodes[i].children[1]);
421 hl_dnodes[i].firstface = LittleShort (hl_dnodes[i].firstface);
422 hl_dnodes[i].numfaces = LittleShort (hl_dnodes[i].numfaces);
423 }
424
425 //
426 // leafs
427 //
428 for (i=0 ; i<hl_numleafs ; i++)
429 {
430 hl_dleafs[i].contents = LittleLong (hl_dleafs[i].contents);
431 for (j=0 ; j<3 ; j++)
432 {
433 hl_dleafs[i].mins[j] = LittleShort (hl_dleafs[i].mins[j]);
434 hl_dleafs[i].maxs[j] = LittleShort (hl_dleafs[i].maxs[j]);
435 }
436
437 hl_dleafs[i].firstmarksurface = LittleShort (hl_dleafs[i].firstmarksurface);
438 hl_dleafs[i].nummarksurfaces = LittleShort (hl_dleafs[i].nummarksurfaces);
439 hl_dleafs[i].visofs = LittleLong (hl_dleafs[i].visofs);
440 }
441
442 //
443 // clipnodes
444 //
445 for (i=0 ; i<hl_numclipnodes ; i++)
446 {
447 hl_dclipnodes[i].planenum = LittleLong (hl_dclipnodes[i].planenum);
448 hl_dclipnodes[i].children[0] = LittleShort (hl_dclipnodes[i].children[0]);
449 hl_dclipnodes[i].children[1] = LittleShort (hl_dclipnodes[i].children[1]);
450 }
451
452 //
453 // miptex
454 //
455 if (hl_texdatasize)
456 {
457 mtl = (hl_dmiptexlump_t *)hl_dtexdata;
458 if (todisk)
459 c = mtl->nummiptex;
460 else
461 c = LittleLong(mtl->nummiptex);
462 mtl->nummiptex = LittleLong (mtl->nummiptex);
463 for (i=0 ; i<c ; i++)
464 mtl->dataofs[i] = LittleLong(mtl->dataofs[i]);
465 }
466
467 //
468 // marksurfaces
469 //
470 for (i=0 ; i<hl_nummarksurfaces ; i++)
471 hl_dmarksurfaces[i] = LittleShort (hl_dmarksurfaces[i]);
472
473 //
474 // surfedges
475 //
476 for (i=0 ; i<hl_numsurfedges ; i++)
477 hl_dsurfedges[i] = LittleLong (hl_dsurfedges[i]);
478
479 //
480 // edges
481 //
482 for (i=0 ; i<hl_numedges ; i++)
483 {
484 hl_dedges[i].v[0] = LittleShort (hl_dedges[i].v[0]);
485 hl_dedges[i].v[1] = LittleShort (hl_dedges[i].v[1]);
486 }
487 } //end of the function HL_SwapBSPFile
488
489
490 hl_dheader_t *hl_header;
491 int hl_fileLength;
492
493 int HL_CopyLump (int lump, void *dest, int size, int maxsize)
494 {
495 int length, ofs;
496
497 length = hl_header->lumps[lump].filelen;
498 ofs = hl_header->lumps[lump].fileofs;
499
500 if (length % size) {
501 Error ("LoadBSPFile: odd lump size");
502 }
503 // somehow things got out of range
504 if ((length/size) > maxsize) {
505 printf("WARNING: exceeded max size for lump %d size %d > maxsize %d\n", lump, (length/size), maxsize);
506 length = maxsize * size;
507 }
508 if ( ofs + length > hl_fileLength ) {
509 printf("WARNING: exceeded file length for lump %d\n", lump);
510 length = hl_fileLength - ofs;
511 if ( length <= 0 ) {
512 return 0;
513 }
514 }
515
516 memcpy (dest, (byte *)hl_header + ofs, length);
517
518 return length / size;
519 }
520
521 /*
522 =============
523 HL_LoadBSPFile
524 =============
525 */
526 void HL_LoadBSPFile (char *filename, int offset, int length)
527 {
528 int i;
529
530 //
531 // load the file header
532 //
533 hl_fileLength = LoadFile (filename, (void **)&hl_header, offset, length);
534
535 // swap the header
536 for (i=0 ; i< sizeof(hl_dheader_t)/4 ; i++)
537 ((int *)hl_header)[i] = LittleLong ( ((int *)hl_header)[i]);
538
539 if (hl_header->version != HL_BSPVERSION)
540 Error ("%s is version %i, not %i", filename, hl_header->version, HL_BSPVERSION);
541
542 hl_nummodels = HL_CopyLump (HL_LUMP_MODELS, hl_dmodels, sizeof(hl_dmodel_t), HL_MAX_MAP_MODELS );
543 hl_numvertexes = HL_CopyLump (HL_LUMP_VERTEXES, hl_dvertexes, sizeof(hl_dvertex_t), HL_MAX_MAP_VERTS );
544 hl_numplanes = HL_CopyLump (HL_LUMP_PLANES, hl_dplanes, sizeof(hl_dplane_t), HL_MAX_MAP_PLANES );
545 hl_numleafs = HL_CopyLump (HL_LUMP_LEAFS, hl_dleafs, sizeof(hl_dleaf_t), HL_MAX_MAP_LEAFS );
546 hl_numnodes = HL_CopyLump (HL_LUMP_NODES, hl_dnodes, sizeof(hl_dnode_t), HL_MAX_MAP_NODES );
547 hl_numtexinfo = HL_CopyLump (HL_LUMP_TEXINFO, hl_texinfo, sizeof(hl_texinfo_t), HL_MAX_MAP_TEXINFO );
548 hl_numclipnodes = HL_CopyLump (HL_LUMP_CLIPNODES, hl_dclipnodes, sizeof(hl_dclipnode_t), HL_MAX_MAP_CLIPNODES );
549 hl_numfaces = HL_CopyLump (HL_LUMP_FACES, hl_dfaces, sizeof(hl_dface_t), HL_MAX_MAP_FACES );
550 hl_nummarksurfaces = HL_CopyLump (HL_LUMP_MARKSURFACES, hl_dmarksurfaces, sizeof(hl_dmarksurfaces[0]), HL_MAX_MAP_MARKSURFACES );
551 hl_numsurfedges = HL_CopyLump (HL_LUMP_SURFEDGES, hl_dsurfedges, sizeof(hl_dsurfedges[0]), HL_MAX_MAP_SURFEDGES );
552 hl_numedges = HL_CopyLump (HL_LUMP_EDGES, hl_dedges, sizeof(hl_dedge_t), HL_MAX_MAP_EDGES );
553
554 hl_texdatasize = HL_CopyLump (HL_LUMP_TEXTURES, hl_dtexdata, 1, HL_MAX_MAP_MIPTEX );
555 hl_visdatasize = HL_CopyLump (HL_LUMP_VISIBILITY, hl_dvisdata, 1, HL_MAX_MAP_VISIBILITY );
556 hl_lightdatasize = HL_CopyLump (HL_LUMP_LIGHTING, hl_dlightdata, 1, HL_MAX_MAP_LIGHTING );
557 hl_entdatasize = HL_CopyLump (HL_LUMP_ENTITIES, hl_dentdata, 1, HL_MAX_MAP_ENTSTRING );
558
559 FreeMemory(hl_header); // everything has been copied out
560
561 //
562 // swap everything
563 //
564 HL_SwapBSPFile (false);
565
566 hl_dmodels_checksum = FastChecksum( hl_dmodels, hl_nummodels*sizeof(hl_dmodels[0]) );
567 hl_dvertexes_checksum = FastChecksum( hl_dvertexes, hl_numvertexes*sizeof(hl_dvertexes[0]) );
568 hl_dplanes_checksum = FastChecksum( hl_dplanes, hl_numplanes*sizeof(hl_dplanes[0]) );
569 hl_dleafs_checksum = FastChecksum( hl_dleafs, hl_numleafs*sizeof(hl_dleafs[0]) );
570 hl_dnodes_checksum = FastChecksum( hl_dnodes, hl_numnodes*sizeof(hl_dnodes[0]) );
571 hl_texinfo_checksum = FastChecksum( hl_texinfo, hl_numtexinfo*sizeof(hl_texinfo[0]) );
572 hl_dclipnodes_checksum = FastChecksum( hl_dclipnodes, hl_numclipnodes*sizeof(hl_dclipnodes[0]) );
573 hl_dfaces_checksum = FastChecksum( hl_dfaces, hl_numfaces*sizeof(hl_dfaces[0]) );
574 hl_dmarksurfaces_checksum = FastChecksum( hl_dmarksurfaces, hl_nummarksurfaces*sizeof(hl_dmarksurfaces[0]) );
575 hl_dsurfedges_checksum = FastChecksum( hl_dsurfedges, hl_numsurfedges*sizeof(hl_dsurfedges[0]) );
576 hl_dedges_checksum = FastChecksum( hl_dedges, hl_numedges*sizeof(hl_dedges[0]) );
577 hl_dtexdata_checksum = FastChecksum( hl_dtexdata, hl_numedges*sizeof(hl_dtexdata[0]) );
578 hl_dvisdata_checksum = FastChecksum( hl_dvisdata, hl_visdatasize*sizeof(hl_dvisdata[0]) );
579 hl_dlightdata_checksum = FastChecksum( hl_dlightdata, hl_lightdatasize*sizeof(hl_dlightdata[0]) );
580 hl_dentdata_checksum = FastChecksum( hl_dentdata, hl_entdatasize*sizeof(hl_dentdata[0]) );
581
582 }
583
584 //============================================================================
585
586 FILE *wadfile;
587 hl_dheader_t outheader;
588
589 void HL_AddLump (int lumpnum, void *data, int len)
590 {
591 hl_lump_t *lump;
592
593 lump = &hl_header->lumps[lumpnum];
594
595 lump->fileofs = LittleLong( ftell(wadfile) );
596 lump->filelen = LittleLong(len);
597 SafeWrite (wadfile, data, (len+3)&~3);
598 }
599
600 /*
601 =============
602 HL_WriteBSPFile
603
604 Swaps the bsp file in place, so it should not be referenced again
605 =============
606 */
607 void HL_WriteBSPFile (char *filename)
608 {
609 hl_header = &outheader;
610 memset (hl_header, 0, sizeof(hl_dheader_t));
611
612 HL_SwapBSPFile (true);
613
614 hl_header->version = LittleLong (HL_BSPVERSION);
615
616 wadfile = SafeOpenWrite (filename);
617 SafeWrite (wadfile, hl_header, sizeof(hl_dheader_t)); // overwritten later
618
619 HL_AddLump (HL_LUMP_PLANES, hl_dplanes, hl_numplanes*sizeof(hl_dplane_t));
620 HL_AddLump (HL_LUMP_LEAFS, hl_dleafs, hl_numleafs*sizeof(hl_dleaf_t));
621 HL_AddLump (HL_LUMP_VERTEXES, hl_dvertexes, hl_numvertexes*sizeof(hl_dvertex_t));
622 HL_AddLump (HL_LUMP_NODES, hl_dnodes, hl_numnodes*sizeof(hl_dnode_t));
623 HL_AddLump (HL_LUMP_TEXINFO, hl_texinfo, hl_numtexinfo*sizeof(hl_texinfo_t));
624 HL_AddLump (HL_LUMP_FACES, hl_dfaces, hl_numfaces*sizeof(hl_dface_t));
625 HL_AddLump (HL_LUMP_CLIPNODES, hl_dclipnodes, hl_numclipnodes*sizeof(hl_dclipnode_t));
626 HL_AddLump (HL_LUMP_MARKSURFACES, hl_dmarksurfaces, hl_nummarksurfaces*sizeof(hl_dmarksurfaces[0]));
627 HL_AddLump (HL_LUMP_SURFEDGES, hl_dsurfedges, hl_numsurfedges*sizeof(hl_dsurfedges[0]));
628 HL_AddLump (HL_LUMP_EDGES, hl_dedges, hl_numedges*sizeof(hl_dedge_t));
629 HL_AddLump (HL_LUMP_MODELS, hl_dmodels, hl_nummodels*sizeof(hl_dmodel_t));
630
631 HL_AddLump (HL_LUMP_LIGHTING, hl_dlightdata, hl_lightdatasize);
632 HL_AddLump (HL_LUMP_VISIBILITY, hl_dvisdata, hl_visdatasize);
633 HL_AddLump (HL_LUMP_ENTITIES, hl_dentdata, hl_entdatasize);
634 HL_AddLump (HL_LUMP_TEXTURES, hl_dtexdata, hl_texdatasize);
635
636 fseek (wadfile, 0, SEEK_SET);
637 SafeWrite (wadfile, hl_header, sizeof(hl_dheader_t));
638 fclose (wadfile);
639 }
640
641 //============================================================================
642
643 #define ENTRIES(a) (sizeof(a)/sizeof(*(a)))
644 #define ENTRYSIZE(a) (sizeof(*(a)))
645
3ced3bf @bnoordhuis Fix warnings: return type defaults to ‘int’.
authored
646 unsigned ArrayUsage( char *szItem, int items, int maxitems, int itemsize )
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
647 {
648 float percentage = maxitems ? items * 100.0 / maxitems : 0.0;
649
650 qprintf("%-12s %7i/%-7i %7i/%-7i (%4.1f%%)",
651 szItem, items, maxitems, items * itemsize, maxitems * itemsize, percentage );
652 if ( percentage > 80.0 )
653 qprintf( "VERY FULL!\n" );
654 else if ( percentage > 95.0 )
655 qprintf( "SIZE DANGER!\n" );
656 else if ( percentage > 99.9 )
657 qprintf( "SIZE OVERFLOW!!!\n" );
658 else
659 qprintf( "\n" );
660 return items * itemsize;
661 }
662
3ced3bf @bnoordhuis Fix warnings: return type defaults to ‘int’.
authored
663 unsigned GlobUsage( char *szItem, int itemstorage, int maxstorage )
796b185 @bnoordhuis Convert Windows line endings to UNIX line endings.
authored
664 {
665 float percentage = maxstorage ? itemstorage * 100.0 / maxstorage : 0.0;
666
667 qprintf("%-12s [variable] %7i/%-7i (%4.1f%%)",
668 szItem, itemstorage, maxstorage, percentage );
669 if ( percentage > 80.0 )
670 qprintf( "VERY FULL!\n" );
671 else if ( percentage > 95.0 )
672 qprintf( "SIZE DANGER!\n" );
673 else if ( percentage > 99.9 )
674 qprintf( "SIZE OVERFLOW!!!\n" );
675 else
676 qprintf( "\n" );
677 return itemstorage;
678 }
679
680 /*
681 =============
682 HL_PrintBSPFileSizes
683
684 Dumps info about current file
685 =============
686 */
687 void HL_PrintBSPFileSizes(void)
688 {
689 int totalmemory = 0;
690
691 qprintf("\n");
692 qprintf("Object names Objects/Maxobjs Memory / Maxmem Fullness\n" );
693 qprintf("------------ --------------- --------------- --------\n" );
694
695 totalmemory += ArrayUsage( "models", hl_nummodels, ENTRIES(hl_dmodels), ENTRYSIZE(hl_dmodels) );
696 totalmemory += ArrayUsage( "planes", hl_numplanes, ENTRIES(hl_dplanes), ENTRYSIZE(hl_dplanes) );
697 totalmemory += ArrayUsage( "vertexes", hl_numvertexes, ENTRIES(hl_dvertexes), ENTRYSIZE(hl_dvertexes) );
698 totalmemory += ArrayUsage( "nodes", hl_numnodes, ENTRIES(hl_dnodes), ENTRYSIZE(hl_dnodes) );
699 totalmemory += ArrayUsage( "texinfos", hl_numtexinfo, ENTRIES(hl_texinfo), ENTRYSIZE(hl_texinfo) );
700 totalmemory += ArrayUsage( "faces", hl_numfaces, ENTRIES(hl_dfaces), ENTRYSIZE(hl_dfaces) );
701 totalmemory += ArrayUsage( "clipnodes", hl_numclipnodes, ENTRIES(hl_dclipnodes), ENTRYSIZE(hl_dclipnodes) );
702 totalmemory += ArrayUsage( "leaves", hl_numleafs, ENTRIES(hl_dleafs), ENTRYSIZE(hl_dleafs) );
703 totalmemory += ArrayUsage( "marksurfaces",hl_nummarksurfaces,ENTRIES(hl_dmarksurfaces),ENTRYSIZE(hl_dmarksurfaces) );
704 totalmemory += ArrayUsage( "surfedges", hl_numsurfedges, ENTRIES(hl_dsurfedges), ENTRYSIZE(hl_dsurfedges) );
705 totalmemory += ArrayUsage( "edges", hl_numedges, ENTRIES(hl_dedges), ENTRYSIZE(hl_dedges) );
706
707 totalmemory += GlobUsage( "texdata", hl_texdatasize, sizeof(hl_dtexdata) );
708 totalmemory += GlobUsage( "lightdata", hl_lightdatasize, sizeof(hl_dlightdata) );
709 totalmemory += GlobUsage( "visdata", hl_visdatasize, sizeof(hl_dvisdata) );
710 totalmemory += GlobUsage( "entdata", hl_entdatasize, sizeof(hl_dentdata) );
711
712 qprintf( "=== Total BSP file data space used: %d bytes ===\n\n", totalmemory );
713 }
714
715
716
717 /*
718 =================
719 ParseEpair
720 =================
721 * /
722 epair_t *ParseEpair (void)
723 {
724 epair_t *e;
725
726 e = malloc (sizeof(epair_t));
727 memset (e, 0, sizeof(epair_t));
728
729 if (strlen(token) >= MAX_KEY-1)
730 Error ("ParseEpar: token too long");
731 e->key = copystring(token);
732 GetToken (false);
733 if (strlen(token) >= MAX_VALUE-1)
734 Error ("ParseEpar: token too long");
735 e->value = copystring(token);
736
737 return e;
738 } //*/
739
740
741 /*
742 ================
743 ParseEntity
744 ================
745 * /
746 qboolean ParseEntity (void)
747 {
748 epair_t *e;
749 entity_t *mapent;
750
751 if (!GetToken (true))
752 return false;
753
754 if (strcmp (token, "{") )
755 Error ("ParseEntity: { not found");
756
757 if (num_entities == HL_MAX_MAP_ENTITIES)
758 Error ("num_entities == HL_MAX_MAP_ENTITIES");
759
760 mapent = &entities[num_entities];
761 num_entities++;
762
763 do
764 {
765 if (!GetToken (true))
766 Error ("ParseEntity: EOF without closing brace");
767 if (!strcmp (token, "}") )
768 break;
769 e = ParseEpair ();
770 e->next = mapent->epairs;
771 mapent->epairs = e;
772 } while (1);
773
774 return true;
775 } //*/
776
777 /*
778 ================
779 ParseEntities
780
781 Parses the dentdata string into entities
782 ================
783 */
784 void HL_ParseEntities (void)
785 {
786 script_t *script;
787
788 num_entities = 0;
789 script = LoadScriptMemory(hl_dentdata, hl_entdatasize, "*Half-Life bsp file");
790 SetScriptFlags(script, SCFL_NOSTRINGWHITESPACES |
791 SCFL_NOSTRINGESCAPECHARS);
792
793 while(ParseEntity(script))
794 {
795 } //end while
796
797 FreeScript(script);
798 } //end of the function HL_ParseEntities
799
800
801 /*
802 ================
803 UnparseEntities
804
805 Generates the dentdata string from all the entities
806 ================
807 */
808 void HL_UnparseEntities (void)
809 {
810 char *buf, *end;
811 epair_t *ep;
812 char line[2048];
813 int i;
814
815 buf = hl_dentdata;
816 end = buf;
817 *end = 0;
818
819 for (i=0 ; i<num_entities ; i++)
820 {
821 ep = entities[i].epairs;
822 if (!ep)
823 continue; // ent got removed
824
825 strcat (end,"{\n");
826 end += 2;
827
828 for (ep = entities[i].epairs ; ep ; ep=ep->next)
829 {
830 sprintf (line, "\"%s\" \"%s\"\n", ep->key, ep->value);
831 strcat (end, line);
832 end += strlen(line);
833 }
834 strcat (end,"}\n");
835 end += 2;
836
837 if (end > buf + HL_MAX_MAP_ENTSTRING)
838 Error ("Entity text too long");
839 }
840 hl_entdatasize = end - buf + 1;
841 } //end of the function HL_UnparseEntities
842
843
844 /*
845 void SetKeyValue (entity_t *ent, char *key, char *value)
846 {
847 epair_t *ep;
848
849 for (ep=ent->epairs ; ep ; ep=ep->next)
850 if (!strcmp (ep->key, key) )
851 {
852 free (ep->value);
853 ep->value = copystring(value);
854 return;
855 }
856 ep = malloc (sizeof(*ep));
857 ep->next = ent->epairs;
858 ent->epairs = ep;
859 ep->key = copystring(key);
860 ep->value = copystring(value);
861 }
862
863 char *ValueForKey (entity_t *ent, char *key)
864 {
865 epair_t *ep;
866
867 for (ep=ent->epairs ; ep ; ep=ep->next)
868 if (!strcmp (ep->key, key) )
869 return ep->value;
870 return "";
871 }
872
873 vec_t FloatForKey (entity_t *ent, char *key)
874 {
875 char *k;
876
877 k = ValueForKey (ent, key);
878 return atof(k);
879 }
880
881 void GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
882 {
883 char *k;
884 double v1, v2, v3;
885
886 k = ValueForKey (ent, key);
887 // scanf into doubles, then assign, so it is vec_t size independent
888 v1 = v2 = v3 = 0;
889 sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
890 vec[0] = v1;
891 vec[1] = v2;
892 vec[2] = v3;
893 } //*/
Something went wrong with that request. Please try again.