1 //------------------------------------------------------------------------
2 // LEVEL : Level structure read/write functions.
3 //------------------------------------------------------------------------
4 //
5 // GL-Friendly Node Builder (C) 2000-2005 Andrew Apted
6 //
7 // Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 //------------------------------------------------------------------------
20 //
21 // ZDBSP format support based on code (C) 2002,2003 Randy Heit
22 //
23 //------------------------------------------------------------------------
24
25 #include "system.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <ctype.h>
32 #include <math.h>
33 #include <limits.h>
34 #include <assert.h>
35
36 #include "analyze.h"
37 #include "blockmap.h"
38 #include "level.h"
39 #include "node.h"
40 #include "reject.h"
41 #include "seg.h"
42 #include "structs.h"
43 #include "util.h"
44 #include "wad.h"
45
46
47 #define DEBUG_LOAD 0
48 #define DEBUG_BSP 0
49
50 #define ALLOC_BLKNUM 1024
51
52
53 // per-level variables
54
55 boolean_g lev_doing_normal;
56 boolean_g lev_doing_hexen;
57
58 static boolean_g lev_force_v3;
59 static boolean_g lev_force_v5;
60
61
62 #define LEVELARRAY(TYPE, BASEVAR, NUMVAR) \
63 TYPE ** BASEVAR = NULL; \
64 int NUMVAR = 0;
65
66
67 LEVELARRAY(vertex_t, lev_vertices, num_vertices)
68 LEVELARRAY(linedef_t, lev_linedefs, num_linedefs)
69 LEVELARRAY(sidedef_t, lev_sidedefs, num_sidedefs)
70 LEVELARRAY(sector_t, lev_sectors, num_sectors)
71 LEVELARRAY(thing_t, lev_things, num_things)
72
73 static LEVELARRAY(seg_t, segs, num_segs)
74 static LEVELARRAY(subsec_t, subsecs, num_subsecs)
75 static LEVELARRAY(node_t, nodes, num_nodes)
76 static LEVELARRAY(node_t, stale_nodes,num_stale_nodes)
77 static LEVELARRAY(wall_tip_t,wall_tips, num_wall_tips)
78
79
80 int num_normal_vert = 0;
81 int num_gl_vert = 0;
82 int num_complete_seg = 0;
83
84
85 /* ----- allocation routines ---------------------------- */
86
87 #define ALLIGATOR(TYPE, BASEVAR, NUMVAR) \
88 { \
89 if ((NUMVAR % ALLOC_BLKNUM) == 0) \
90 { \
91 BASEVAR = UtilRealloc(BASEVAR, (NUMVAR + ALLOC_BLKNUM) * \
92 sizeof(TYPE *)); \
93 } \
94 BASEVAR[NUMVAR] = (TYPE *) UtilCalloc(sizeof(TYPE)); \
95 NUMVAR += 1; \
96 return BASEVAR[NUMVAR - 1]; \
97 }
98
99
NewVertex(void)100 vertex_t *NewVertex(void)
101 ALLIGATOR(vertex_t, lev_vertices, num_vertices)
102
103 linedef_t *NewLinedef(void)
104 ALLIGATOR(linedef_t, lev_linedefs, num_linedefs)
105
106 sidedef_t *NewSidedef(void)
107 ALLIGATOR(sidedef_t, lev_sidedefs, num_sidedefs)
108
109 sector_t *NewSector(void)
110 ALLIGATOR(sector_t, lev_sectors, num_sectors)
111
112 thing_t *NewThing(void)
113 ALLIGATOR(thing_t, lev_things, num_things)
114
115 seg_t *NewSeg(void)
116 ALLIGATOR(seg_t, segs, num_segs)
117
118 subsec_t *NewSubsec(void)
119 ALLIGATOR(subsec_t, subsecs, num_subsecs)
120
121 node_t *NewNode(void)
122 ALLIGATOR(node_t, nodes, num_nodes)
123
124 node_t *NewStaleNode(void)
125 ALLIGATOR(node_t, stale_nodes, num_stale_nodes)
126
127 wall_tip_t *NewWallTip(void)
128 ALLIGATOR(wall_tip_t, wall_tips, num_wall_tips)
129
130
131 /* ----- free routines ---------------------------- */
132
133 #define FREEMASON(TYPE, BASEVAR, NUMVAR) \
134 { \
135 int i; \
136 for (i=0; i < NUMVAR; i++) \
137 UtilFree(BASEVAR[i]); \
138 if (BASEVAR) \
139 UtilFree(BASEVAR); \
140 BASEVAR = NULL; NUMVAR = 0; \
141 }
142
143
144 void FreeVertices(void)
145 FREEMASON(vertex_t, lev_vertices, num_vertices)
146
147 void FreeLinedefs(void)
148 FREEMASON(linedef_t, lev_linedefs, num_linedefs)
149
150 void FreeSidedefs(void)
151 FREEMASON(sidedef_t, lev_sidedefs, num_sidedefs)
152
153 void FreeSectors(void)
154 FREEMASON(sector_t, lev_sectors, num_sectors)
155
156 void FreeThings(void)
157 FREEMASON(thing_t, lev_things, num_things)
158
159 void FreeSegs(void)
160 FREEMASON(seg_t, segs, num_segs)
161
162 void FreeSubsecs(void)
163 FREEMASON(subsec_t, subsecs, num_subsecs)
164
165 void FreeNodes(void)
166 FREEMASON(node_t, nodes, num_nodes)
167
168 void FreeStaleNodes(void)
169 FREEMASON(node_t, stale_nodes, num_stale_nodes)
170
171 void FreeWallTips(void)
172 FREEMASON(wall_tip_t, wall_tips, num_wall_tips)
173
174
175 /* ----- lookup routines ------------------------------ */
176
177 #define LOOKERUPPER(BASEVAR, NUMVAR, NAMESTR) \
178 { \
179 if (index < 0 || index >= NUMVAR) \
180 FatalError("No such %s number #%d", NAMESTR, index); \
181 \
182 return BASEVAR[index]; \
183 }
184
185 vertex_t *LookupVertex(int index)
186 LOOKERUPPER(lev_vertices, num_vertices, "vertex")
187
188 linedef_t *LookupLinedef(int index)
189 LOOKERUPPER(lev_linedefs, num_linedefs, "linedef")
190
191 sidedef_t *LookupSidedef(int index)
192 LOOKERUPPER(lev_sidedefs, num_sidedefs, "sidedef")
193
194 sector_t *LookupSector(int index)
195 LOOKERUPPER(lev_sectors, num_sectors, "sector")
196
197 thing_t *LookupThing(int index)
198 LOOKERUPPER(lev_things, num_things, "thing")
199
200 seg_t *LookupSeg(int index)
201 LOOKERUPPER(segs, num_segs, "seg")
202
203 subsec_t *LookupSubsec(int index)
204 LOOKERUPPER(subsecs, num_subsecs, "subsector")
205
206 node_t *LookupNode(int index)
207 LOOKERUPPER(nodes, num_nodes, "node")
208
209 node_t *LookupStaleNode(int index)
210 LOOKERUPPER(stale_nodes, num_stale_nodes, "stale_node")
211
212
213 /* ----- reading routines ------------------------------ */
214
215
216 //
217 // CheckForNormalNodes
218 //
219 int CheckForNormalNodes(void)
220 {
221 lump_t *lump;
222
223 /* Note: an empty NODES lump can be valid */
224 if (FindLevelLump("NODES") == NULL)
225 return FALSE;
226
227 lump = FindLevelLump("SEGS");
228
229 if (! lump || lump->length == 0 || CheckLevelLumpZero(lump))
230 return FALSE;
231
232 lump = FindLevelLump("SSECTORS");
233
234 if (! lump || lump->length == 0 || CheckLevelLumpZero(lump))
235 return FALSE;
236
237 return TRUE;
238 }
239
240 //
241 // GetVertices
242 //
GetVertices(void)243 void GetVertices(void)
244 {
245 int i, count=-1;
246 raw_vertex_t *raw;
247 lump_t *lump = FindLevelLump("VERTEXES");
248
249 if (lump)
250 count = lump->length / sizeof(raw_vertex_t);
251
252 DisplayTicker();
253
254 # if DEBUG_LOAD
255 PrintDebug("GetVertices: num = %d\n", count);
256 # endif
257
258 if (!lump || count == 0)
259 FatalError("Couldn't find any Vertices");
260
261 raw = (raw_vertex_t *) lump->data;
262
263 for (i=0; i < count; i++, raw++)
264 {
265 vertex_t *vert = NewVertex();
266
267 vert->x = (float_g) SINT16(raw->x);
268 vert->y = (float_g) SINT16(raw->y);
269
270 vert->index = i;
271 }
272
273 num_normal_vert = num_vertices;
274 num_gl_vert = 0;
275 num_complete_seg = 0;
276 }
277
278 //
279 // GetSectors
280 //
GetSectors(void)281 void GetSectors(void)
282 {
283 int i, count=-1;
284 raw_sector_t *raw;
285 lump_t *lump = FindLevelLump("SECTORS");
286
287 if (lump)
288 count = lump->length / sizeof(raw_sector_t);
289
290 if (!lump || count == 0)
291 FatalError("Couldn't find any Sectors");
292
293 DisplayTicker();
294
295 # if DEBUG_LOAD
296 PrintDebug("GetSectors: num = %d\n", count);
297 # endif
298
299 raw = (raw_sector_t *) lump->data;
300
301 for (i=0; i < count; i++, raw++)
302 {
303 sector_t *sector = NewSector();
304
305 sector->floor_h = SINT16(raw->floor_h);
306 sector->ceil_h = SINT16(raw->ceil_h);
307
308 memcpy(sector->floor_tex, raw->floor_tex, sizeof(sector->floor_tex));
309 memcpy(sector->ceil_tex, raw->ceil_tex, sizeof(sector->ceil_tex));
310
311 sector->light = UINT16(raw->light);
312 sector->special = UINT16(raw->special);
313 sector->tag = SINT16(raw->tag);
314
315 sector->coalesce = (sector->tag >= 900 && sector->tag < 1000) ?
316 TRUE : FALSE;
317
318 /* sector indices never change */
319 sector->index = i;
320
321 sector->warned_facing = -1;
322
323 /* Note: rej_* fields are handled completely in reject.c */
324 }
325 }
326
327 //
328 // GetThings
329 //
GetThings(void)330 void GetThings(void)
331 {
332 int i, count=-1;
333 raw_thing_t *raw;
334 lump_t *lump = FindLevelLump("THINGS");
335
336 if (lump)
337 count = lump->length / sizeof(raw_thing_t);
338
339 if (!lump || count == 0)
340 {
341 // Note: no error if no things exist, even though technically a map
342 // will be unplayable without the player starts.
343 PrintWarn("Couldn't find any Things");
344 return;
345 }
346
347 DisplayTicker();
348
349 # if DEBUG_LOAD
350 PrintDebug("GetThings: num = %d\n", count);
351 # endif
352
353 raw = (raw_thing_t *) lump->data;
354
355 for (i=0; i < count; i++, raw++)
356 {
357 thing_t *thing = NewThing();
358
359 thing->x = SINT16(raw->x);
360 thing->y = SINT16(raw->y);
361
362 thing->type = UINT16(raw->type);
363 thing->options = UINT16(raw->options);
364
365 thing->index = i;
366 }
367 }
368
369 //
370 // GetThingsHexen
371 //
GetThingsHexen(void)372 void GetThingsHexen(void)
373 {
374 int i, count=-1;
375 raw_hexen_thing_t *raw;
376 lump_t *lump = FindLevelLump("THINGS");
377
378 if (lump)
379 count = lump->length / sizeof(raw_hexen_thing_t);
380
381 if (!lump || count == 0)
382 {
383 // Note: no error if no things exist, even though technically a map
384 // will be unplayable without the player starts.
385 PrintWarn("Couldn't find any Things");
386 return;
387 }
388
389 DisplayTicker();
390
391 # if DEBUG_LOAD
392 PrintDebug("GetThingsHexen: num = %d\n", count);
393 # endif
394
395 raw = (raw_hexen_thing_t *) lump->data;
396
397 for (i=0; i < count; i++, raw++)
398 {
399 thing_t *thing = NewThing();
400
401 thing->x = SINT16(raw->x);
402 thing->y = SINT16(raw->y);
403
404 thing->type = UINT16(raw->type);
405 thing->options = UINT16(raw->options);
406
407 thing->index = i;
408 }
409 }
410
411 //
412 // GetSidedefs
413 //
GetSidedefs(void)414 void GetSidedefs(void)
415 {
416 int i, count=-1;
417 raw_sidedef_t *raw;
418 lump_t *lump = FindLevelLump("SIDEDEFS");
419
420 if (lump)
421 count = lump->length / sizeof(raw_sidedef_t);
422
423 if (!lump || count == 0)
424 FatalError("Couldn't find any Sidedefs");
425
426 DisplayTicker();
427
428 # if DEBUG_LOAD
429 PrintDebug("GetSidedefs: num = %d\n", count);
430 # endif
431
432 raw = (raw_sidedef_t *) lump->data;
433
434 for (i=0; i < count; i++, raw++)
435 {
436 sidedef_t *side = NewSidedef();
437
438 side->sector = (SINT16(raw->sector) == -1) ? NULL :
439 LookupSector(UINT16(raw->sector));
440
441 if (side->sector)
442 side->sector->ref_count++;
443
444 side->x_offset = SINT16(raw->x_offset);
445 side->y_offset = SINT16(raw->y_offset);
446
447 memcpy(side->upper_tex, raw->upper_tex, sizeof(side->upper_tex));
448 memcpy(side->lower_tex, raw->lower_tex, sizeof(side->lower_tex));
449 memcpy(side->mid_tex, raw->mid_tex, sizeof(side->mid_tex));
450
451 /* sidedef indices never change */
452 side->index = i;
453 }
454 }
455
SafeLookupSidedef(uint16_g num)456 static INLINE_G sidedef_t *SafeLookupSidedef(uint16_g num)
457 {
458 if (num == 0xFFFF)
459 return NULL;
460
461 if ((int)num >= num_sidedefs && (sint16_g)(num) < 0)
462 return NULL;
463
464 return LookupSidedef(num);
465 }
466
467 //
468 // GetLinedefs
469 //
GetLinedefs(void)470 void GetLinedefs(void)
471 {
472 int i, count=-1;
473 raw_linedef_t *raw;
474 lump_t *lump = FindLevelLump("LINEDEFS");
475
476 if (lump)
477 count = lump->length / sizeof(raw_linedef_t);
478
479 if (!lump || count == 0)
480 FatalError("Couldn't find any Linedefs");
481
482 DisplayTicker();
483
484 # if DEBUG_LOAD
485 PrintDebug("GetLinedefs: num = %d\n", count);
486 # endif
487
488 raw = (raw_linedef_t *) lump->data;
489
490 for (i=0; i < count; i++, raw++)
491 {
492 linedef_t *line;
493
494 vertex_t *start = LookupVertex(UINT16(raw->start));
495 vertex_t *end = LookupVertex(UINT16(raw->end));
496
497 start->ref_count++;
498 end->ref_count++;
499
500 line = NewLinedef();
501
502 line->start = start;
503 line->end = end;
504
505 /* check for zero-length line */
506 line->zero_len = (fabs(start->x - end->x) < DIST_EPSILON) &&
507 (fabs(start->y - end->y) < DIST_EPSILON);
508
509 line->flags = UINT16(raw->flags);
510 line->type = UINT16(raw->type);
511 line->tag = SINT16(raw->tag);
512
513 line->two_sided = (line->flags & LINEFLAG_TWO_SIDED) ? TRUE : FALSE;
514 line->is_precious = (line->tag >= 900 && line->tag < 1000) ?
515 TRUE : FALSE;
516
517 line->right = SafeLookupSidedef(UINT16(raw->sidedef1));
518 line->left = SafeLookupSidedef(UINT16(raw->sidedef2));
519
520 if (line->right)
521 {
522 line->right->ref_count++;
523 line->right->on_special |= (line->type > 0) ? 1 : 0;
524 }
525
526 if (line->left)
527 {
528 line->left->ref_count++;
529 line->left->on_special |= (line->type > 0) ? 1 : 0;
530 }
531
532 line->self_ref = (line->left && line->right &&
533 (line->left->sector == line->right->sector));
534
535 line->index = i;
536 }
537 }
538
539 //
540 // GetLinedefsHexen
541 //
GetLinedefsHexen(void)542 void GetLinedefsHexen(void)
543 {
544 int i, j, count=-1;
545 raw_hexen_linedef_t *raw;
546 lump_t *lump = FindLevelLump("LINEDEFS");
547
548 if (lump)
549 count = lump->length / sizeof(raw_hexen_linedef_t);
550
551 if (!lump || count == 0)
552 FatalError("Couldn't find any Linedefs");
553
554 DisplayTicker();
555
556 # if DEBUG_LOAD
557 PrintDebug("GetLinedefsHexen: num = %d\n", count);
558 # endif
559
560 raw = (raw_hexen_linedef_t *) lump->data;
561
562 for (i=0; i < count; i++, raw++)
563 {
564 linedef_t *line;
565
566 vertex_t *start = LookupVertex(UINT16(raw->start));
567 vertex_t *end = LookupVertex(UINT16(raw->end));
568
569 start->ref_count++;
570 end->ref_count++;
571
572 line = NewLinedef();
573
574 line->start = start;
575 line->end = end;
576
577 // check for zero-length line
578 line->zero_len = (fabs(start->x - end->x) < DIST_EPSILON) &&
579 (fabs(start->y - end->y) < DIST_EPSILON);
580
581 line->flags = UINT16(raw->flags);
582 line->type = UINT8(raw->type);
583 line->tag = 0;
584
585 /* read specials */
586 for (j=0; j < 5; j++)
587 line->specials[j] = UINT8(raw->specials[j]);
588
589 // -JL- Added missing twosided flag handling that caused a broken reject
590 line->two_sided = (line->flags & LINEFLAG_TWO_SIDED) ? TRUE : FALSE;
591
592 line->right = SafeLookupSidedef(UINT16(raw->sidedef1));
593 line->left = SafeLookupSidedef(UINT16(raw->sidedef2));
594
595 // -JL- Added missing sidedef handling that caused all sidedefs to be pruned
596 if (line->right)
597 {
598 line->right->ref_count++;
599 line->right->on_special |= (line->type > 0) ? 1 : 0;
600 }
601
602 if (line->left)
603 {
604 line->left->ref_count++;
605 line->left->on_special |= (line->type > 0) ? 1 : 0;
606 }
607
608 line->index = i;
609 }
610 }
611
612 //
613 // GetStaleNodes
614 //
GetStaleNodes(void)615 void GetStaleNodes(void)
616 {
617 int i, count=-1;
618 raw_node_t *raw;
619 lump_t *lump = FindLevelLump("NODES");
620
621 if (lump)
622 count = lump->length / sizeof(raw_node_t);
623
624 if (!lump || count < 5)
625 return;
626
627 DisplayTicker();
628
629 # if DEBUG_LOAD
630 PrintDebug("GetStaleNodes: num = %d\n", count);
631 # endif
632
633 raw = (raw_node_t *) lump->data;
634
635 /* must allocate all the nodes beforehand, since they contain
636 * internal references to each other.
637 */
638 for (i=0; i < count; i++)
639 {
640 NewStaleNode();
641 }
642
643 for (i=0; i < count; i++, raw++)
644 {
645 node_t *nd = LookupStaleNode(i);
646
647 nd->x = SINT16(raw->x);
648 nd->y = SINT16(raw->y);
649 nd->dx = SINT16(raw->dx);
650 nd->dy = SINT16(raw->dy);
651
652 nd->index = i;
653
654 /* Note: we ignore the subsector references */
655
656 if ((UINT16(raw->right) & 0x8000U) == 0)
657 {
658 nd->r.node = LookupStaleNode(UINT16(raw->right));
659 }
660
661 if ((UINT16(raw->left) & 0x8000U) == 0)
662 {
663 nd->l.node = LookupStaleNode(UINT16(raw->left));
664 }
665
666 /* we also ignore the bounding boxes -- not needed */
667 }
668 }
669
670
TransformSegDist(const seg_t * seg)671 static INLINE_G int TransformSegDist(const seg_t *seg)
672 {
673 float_g sx = seg->side ? seg->linedef->end->x : seg->linedef->start->x;
674 float_g sy = seg->side ? seg->linedef->end->y : seg->linedef->start->y;
675
676 return (int) ceil(UtilComputeDist(seg->start->x - sx, seg->start->y - sy));
677 }
678
TransformAngle(angle_g angle)679 static INLINE_G int TransformAngle(angle_g angle)
680 {
681 int result;
682
683 result = (int)(angle * 65536.0 / 360.0);
684
685 if (result < 0)
686 result += 65536;
687
688 return (result & 0xFFFF);
689 }
690
SegCompare(const void * p1,const void * p2)691 static int SegCompare(const void *p1, const void *p2)
692 {
693 const seg_t *A = ((const seg_t **) p1)[0];
694 const seg_t *B = ((const seg_t **) p2)[0];
695
696 if (A->index < 0)
697 InternalError("Seg %p never reached a subsector !", A);
698
699 if (B->index < 0)
700 InternalError("Seg %p never reached a subsector !", B);
701
702 return (A->index - B->index);
703 }
704
705
706 /* ----- writing routines ------------------------------ */
707
708 static const uint8_g *lev_v2_magic = (uint8_g *) "gNd2";
709 static const uint8_g *lev_v3_magic = (uint8_g *) "gNd3";
710 static const uint8_g *lev_v5_magic = (uint8_g *) "gNd5";
711
PutVertices(char * name,int do_gl)712 void PutVertices(char *name, int do_gl)
713 {
714 int count, i;
715 lump_t *lump;
716
717 DisplayTicker();
718
719 if (do_gl)
720 lump = CreateGLLump(name);
721 else
722 lump = CreateLevelLump(name);
723
724 for (i=0, count=0; i < num_vertices; i++)
725 {
726 raw_vertex_t raw;
727 vertex_t *vert = lev_vertices[i];
728
729 if ((do_gl ? 1 : 0) != ((vert->index & IS_GL_VERTEX) ? 1 : 0))
730 {
731 continue;
732 }
733
734 raw.x = SINT16(I_ROUND(vert->x));
735 raw.y = SINT16(I_ROUND(vert->y));
736
737 AppendLevelLump(lump, &raw, sizeof(raw));
738
739 count++;
740 }
741
742 if (count != (do_gl ? num_gl_vert : num_normal_vert))
743 InternalError("PutVertices miscounted (%d != %d)", count,
744 do_gl ? num_gl_vert : num_normal_vert);
745
746 if (lev_doing_normal && ! do_gl && count > 65534)
747 MarkHardFailure(LIMIT_VERTEXES);
748 else if (count > 32767)
749 MarkSoftFailure(do_gl ? LIMIT_GL_VERT : LIMIT_VERTEXES);
750 }
751
PutV2Vertices(int do_v5)752 void PutV2Vertices(int do_v5)
753 {
754 int count, i;
755 lump_t *lump;
756
757 DisplayTicker();
758
759 lump = CreateGLLump("GL_VERT");
760
761 if (do_v5)
762 AppendLevelLump(lump, lev_v5_magic, 4);
763 else
764 AppendLevelLump(lump, lev_v2_magic, 4);
765
766 for (i=0, count=0; i < num_vertices; i++)
767 {
768 raw_v2_vertex_t raw;
769 vertex_t *vert = lev_vertices[i];
770
771 if (! (vert->index & IS_GL_VERTEX))
772 continue;
773
774 raw.x = SINT32((int)(vert->x * 65536.0));
775 raw.y = SINT32((int)(vert->y * 65536.0));
776
777 AppendLevelLump(lump, &raw, sizeof(raw));
778
779 count++;
780 }
781
782 if (count != num_gl_vert)
783 InternalError("PutV2Vertices miscounted (%d != %d)", count,
784 num_gl_vert);
785
786 if (count > 32767)
787 MarkSoftFailure(LIMIT_GL_VERT);
788 }
789
PutSectors(void)790 void PutSectors(void)
791 {
792 int i;
793 lump_t *lump = CreateLevelLump("SECTORS");
794
795 DisplayTicker();
796
797 for (i=0; i < num_sectors; i++)
798 {
799 raw_sector_t raw;
800 sector_t *sector = lev_sectors[i];
801
802 raw.floor_h = SINT16(sector->floor_h);
803 raw.ceil_h = SINT16(sector->ceil_h);
804
805 memcpy(raw.floor_tex, sector->floor_tex, sizeof(raw.floor_tex));
806 memcpy(raw.ceil_tex, sector->ceil_tex, sizeof(raw.ceil_tex));
807
808 raw.light = UINT16(sector->light);
809 raw.special = UINT16(sector->special);
810 raw.tag = SINT16(sector->tag);
811
812 AppendLevelLump(lump, &raw, sizeof(raw));
813 }
814
815 if (num_sectors > 65534)
816 MarkHardFailure(LIMIT_SECTORS);
817 else if (num_sectors > 32767)
818 MarkSoftFailure(LIMIT_SECTORS);
819 }
820
PutSidedefs(void)821 void PutSidedefs(void)
822 {
823 int i;
824 lump_t *lump = CreateLevelLump("SIDEDEFS");
825
826 DisplayTicker();
827
828 for (i=0; i < num_sidedefs; i++)
829 {
830 raw_sidedef_t raw;
831 sidedef_t *side = lev_sidedefs[i];
832
833 raw.sector = (side->sector == NULL) ? SINT16(-1) :
834 UINT16(side->sector->index);
835
836 raw.x_offset = SINT16(side->x_offset);
837 raw.y_offset = SINT16(side->y_offset);
838
839 memcpy(raw.upper_tex, side->upper_tex, sizeof(raw.upper_tex));
840 memcpy(raw.lower_tex, side->lower_tex, sizeof(raw.lower_tex));
841 memcpy(raw.mid_tex, side->mid_tex, sizeof(raw.mid_tex));
842
843 AppendLevelLump(lump, &raw, sizeof(raw));
844 }
845
846 if (num_sidedefs > 65534)
847 MarkHardFailure(LIMIT_SIDEDEFS);
848 else if (num_sidedefs > 32767)
849 MarkSoftFailure(LIMIT_SIDEDEFS);
850 }
851
PutLinedefs(void)852 void PutLinedefs(void)
853 {
854 int i;
855 lump_t *lump = CreateLevelLump("LINEDEFS");
856
857 DisplayTicker();
858
859 for (i=0; i < num_linedefs; i++)
860 {
861 raw_linedef_t raw;
862 linedef_t *line = lev_linedefs[i];
863
864 raw.start = UINT16(line->start->index);
865 raw.end = UINT16(line->end->index);
866
867 raw.flags = UINT16(line->flags);
868 raw.type = UINT16(line->type);
869 raw.tag = SINT16(line->tag);
870
871 raw.sidedef1 = line->right ? UINT16(line->right->index) : 0xFFFF;
872 raw.sidedef2 = line->left ? UINT16(line->left->index) : 0xFFFF;
873
874 AppendLevelLump(lump, &raw, sizeof(raw));
875 }
876
877 if (num_linedefs > 65534)
878 MarkHardFailure(LIMIT_LINEDEFS);
879 else if (num_linedefs > 32767)
880 MarkSoftFailure(LIMIT_LINEDEFS);
881 }
882
PutLinedefsHexen(void)883 void PutLinedefsHexen(void)
884 {
885 int i, j;
886 lump_t *lump = CreateLevelLump("LINEDEFS");
887
888 DisplayTicker();
889
890 for (i=0; i < num_linedefs; i++)
891 {
892 raw_hexen_linedef_t raw;
893 linedef_t *line = lev_linedefs[i];
894
895 raw.start = UINT16(line->start->index);
896 raw.end = UINT16(line->end->index);
897
898 raw.flags = UINT16(line->flags);
899 raw.type = UINT8(line->type);
900
901 // write specials
902 for (j=0; j < 5; j++)
903 raw.specials[j] = UINT8(line->specials[j]);
904
905 raw.sidedef1 = line->right ? UINT16(line->right->index) : 0xFFFF;
906 raw.sidedef2 = line->left ? UINT16(line->left->index) : 0xFFFF;
907
908 AppendLevelLump(lump, &raw, sizeof(raw));
909 }
910
911 if (num_linedefs > 65534)
912 MarkHardFailure(LIMIT_LINEDEFS);
913 else if (num_linedefs > 32767)
914 MarkSoftFailure(LIMIT_LINEDEFS);
915 }
916
VertexIndex16Bit(const vertex_t * v)917 static INLINE_G uint16_g VertexIndex16Bit(const vertex_t *v)
918 {
919 if (v->index & IS_GL_VERTEX)
920 return (uint16_g) ((v->index & ~IS_GL_VERTEX) | 0x8000U);
921
922 return (uint16_g) v->index;
923 }
924
VertexIndex32BitV5(const vertex_t * v)925 static INLINE_G uint32_g VertexIndex32BitV5(const vertex_t *v)
926 {
927 if (v->index & IS_GL_VERTEX)
928 return (uint32_g) ((v->index & ~IS_GL_VERTEX) | 0x80000000U);
929
930 return (uint32_g) v->index;
931 }
932
PutSegs(void)933 void PutSegs(void)
934 {
935 int i, count;
936 lump_t *lump = CreateLevelLump("SEGS");
937
938 DisplayTicker();
939
940 // sort segs into ascending index
941 qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
942
943 for (i=0, count=0; i < num_segs; i++)
944 {
945 raw_seg_t raw;
946 seg_t *seg = segs[i];
947
948 // ignore minisegs and degenerate segs
949 if (! seg->linedef || seg->degenerate)
950 continue;
951
952 raw.start = UINT16(VertexIndex16Bit(seg->start));
953 raw.end = UINT16(VertexIndex16Bit(seg->end));
954 raw.angle = UINT16(TransformAngle(seg->p_angle));
955 raw.linedef = UINT16(seg->linedef->index);
956 raw.flip = UINT16(seg->side);
957 raw.dist = UINT16(TransformSegDist(seg));
958
959 AppendLevelLump(lump, &raw, sizeof(raw));
960
961 count++;
962
963 # if DEBUG_BSP
964 PrintDebug("PUT SEG: %04X Vert %04X->%04X Line %04X %s "
965 "Angle %04X (%1.1f,%1.1f) -> (%1.1f,%1.1f)\n", seg->index,
966 UINT16(raw.start), UINT16(raw.end), UINT16(raw.linedef),
967 seg->side ? "L" : "R", UINT16(raw.angle),
968 seg->start->x, seg->start->y, seg->end->x, seg->end->y);
969 # endif
970 }
971
972 if (count != num_complete_seg)
973 InternalError("PutSegs miscounted (%d != %d)", count,
974 num_complete_seg);
975
976 if (count > 65534)
977 MarkHardFailure(LIMIT_SEGS);
978 else if (count > 32767)
979 MarkSoftFailure(LIMIT_SEGS);
980 }
981
PutGLSegs(void)982 void PutGLSegs(void)
983 {
984 int i, count;
985 lump_t *lump = CreateGLLump("GL_SEGS");
986
987 DisplayTicker();
988
989 // sort segs into ascending index
990 qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
991
992 for (i=0, count=0; i < num_segs; i++)
993 {
994 raw_gl_seg_t raw;
995 seg_t *seg = segs[i];
996
997 // ignore degenerate segs
998 if (seg->degenerate)
999 continue;
1000
1001 raw.start = UINT16(VertexIndex16Bit(seg->start));
1002 raw.end = UINT16(VertexIndex16Bit(seg->end));
1003 raw.side = UINT16(seg->side);
1004
1005 if (seg->linedef)
1006 raw.linedef = UINT16(seg->linedef->index);
1007 else
1008 raw.linedef = UINT16(0xFFFF);
1009
1010 if (seg->partner)
1011 raw.partner = UINT16(seg->partner->index);
1012 else
1013 raw.partner = UINT16(0xFFFF);
1014
1015 AppendLevelLump(lump, &raw, sizeof(raw));
1016
1017 count++;
1018
1019 # if DEBUG_BSP
1020 PrintDebug("PUT GL SEG: %04X Line %04X %s Partner %04X "
1021 "(%1.1f,%1.1f) -> (%1.1f,%1.1f)\n", seg->index, UINT16(raw.linedef),
1022 seg->side ? "L" : "R", UINT16(raw.partner),
1023 seg->start->x, seg->start->y, seg->end->x, seg->end->y);
1024 # endif
1025 }
1026
1027 if (count != num_complete_seg)
1028 InternalError("PutGLSegs miscounted (%d != %d)", count,
1029 num_complete_seg);
1030
1031 if (count > 65534)
1032 InternalError("PutGLSegs with %d (> 65534) segs", count);
1033 else if (count > 32767)
1034 MarkSoftFailure(LIMIT_GL_SEGS);
1035 }
1036
PutV3Segs(int do_v5)1037 void PutV3Segs(int do_v5)
1038 {
1039 int i, count;
1040 lump_t *lump = CreateGLLump("GL_SEGS");
1041
1042 if (! do_v5)
1043 AppendLevelLump(lump, lev_v3_magic, 4);
1044
1045 DisplayTicker();
1046
1047 // sort segs into ascending index
1048 qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
1049
1050 for (i=0, count=0; i < num_segs; i++)
1051 {
1052 raw_v3_seg_t raw;
1053 seg_t *seg = segs[i];
1054
1055 // ignore degenerate segs
1056 if (seg->degenerate)
1057 continue;
1058
1059 if (do_v5)
1060 {
1061 raw.start = UINT32(VertexIndex32BitV5(seg->start));
1062 raw.end = UINT32(VertexIndex32BitV5(seg->end));
1063 }
1064 else
1065 {
1066 raw.start = UINT32(seg->start->index);
1067 raw.end = UINT32(seg->end->index);
1068 }
1069
1070 raw.side = UINT16(seg->side);
1071
1072 if (seg->linedef)
1073 raw.linedef = UINT16(seg->linedef->index);
1074 else
1075 raw.linedef = UINT16(0xFFFF);
1076
1077 if (seg->partner)
1078 raw.partner = UINT32(seg->partner->index);
1079 else
1080 raw.partner = UINT32(0xFFFFFFFF);
1081
1082 AppendLevelLump(lump, &raw, sizeof(raw));
1083
1084 count++;
1085
1086 # if DEBUG_BSP
1087 PrintDebug("PUT V3 SEG: %06X Line %04X %s Partner %06X "
1088 "(%1.1f,%1.1f) -> (%1.1f,%1.1f)\n", seg->index, UINT16(raw.linedef),
1089 seg->side ? "L" : "R", UINT32(raw.partner),
1090 seg->start->x, seg->start->y, seg->end->x, seg->end->y);
1091 # endif
1092 }
1093
1094 if (count != num_complete_seg)
1095 InternalError("PutGLSegs miscounted (%d != %d)", count,
1096 num_complete_seg);
1097 }
1098
PutSubsecs(char * name,int do_gl)1099 void PutSubsecs(char *name, int do_gl)
1100 {
1101 int i;
1102 lump_t *lump;
1103
1104 DisplayTicker();
1105
1106 if (do_gl)
1107 lump = CreateGLLump(name);
1108 else
1109 lump = CreateLevelLump(name);
1110
1111 for (i=0; i < num_subsecs; i++)
1112 {
1113 raw_subsec_t raw;
1114 subsec_t *sub = subsecs[i];
1115
1116 raw.first = UINT16(sub->seg_list->index);
1117 raw.num = UINT16(sub->seg_count);
1118
1119 AppendLevelLump(lump, &raw, sizeof(raw));
1120
1121 # if DEBUG_BSP
1122 PrintDebug("PUT SUBSEC %04X First %04X Num %04X\n",
1123 sub->index, UINT16(raw.first), UINT16(raw.num));
1124 # endif
1125 }
1126
1127 if (num_subsecs > 32767)
1128 MarkHardFailure(do_gl ? LIMIT_GL_SSECT : LIMIT_SSECTORS);
1129 }
1130
PutV3Subsecs(int do_v5)1131 void PutV3Subsecs(int do_v5)
1132 {
1133 int i;
1134 lump_t *lump;
1135
1136 DisplayTicker();
1137
1138 lump = CreateGLLump("GL_SSECT");
1139
1140 if (! do_v5)
1141 AppendLevelLump(lump, lev_v3_magic, 4);
1142
1143 for (i=0; i < num_subsecs; i++)
1144 {
1145 raw_v3_subsec_t raw;
1146 subsec_t *sub = subsecs[i];
1147
1148 raw.first = UINT32(sub->seg_list->index);
1149 raw.num = UINT32(sub->seg_count);
1150
1151 AppendLevelLump(lump, &raw, sizeof(raw));
1152
1153 # if DEBUG_BSP
1154 PrintDebug("PUT V3 SUBSEC %06X First %06X Num %06X\n",
1155 sub->index, UINT32(raw.first), UINT32(raw.num));
1156 # endif
1157 }
1158
1159 if (!do_v5 && num_subsecs > 32767)
1160 MarkHardFailure(LIMIT_GL_SSECT);
1161 }
1162
1163 static int node_cur_index;
1164
PutOneNode(node_t * node,lump_t * lump)1165 static void PutOneNode(node_t *node, lump_t *lump)
1166 {
1167 raw_node_t raw;
1168
1169 if (node->r.node)
1170 PutOneNode(node->r.node, lump);
1171
1172 if (node->l.node)
1173 PutOneNode(node->l.node, lump);
1174
1175 node->index = node_cur_index++;
1176
1177 raw.x = SINT16(node->x);
1178 raw.y = SINT16(node->y);
1179 raw.dx = SINT16(node->dx / (node->too_long ? 2 : 1));
1180 raw.dy = SINT16(node->dy / (node->too_long ? 2 : 1));
1181
1182 raw.b1.minx = SINT16(node->r.bounds.minx);
1183 raw.b1.miny = SINT16(node->r.bounds.miny);
1184 raw.b1.maxx = SINT16(node->r.bounds.maxx);
1185 raw.b1.maxy = SINT16(node->r.bounds.maxy);
1186
1187 raw.b2.minx = SINT16(node->l.bounds.minx);
1188 raw.b2.miny = SINT16(node->l.bounds.miny);
1189 raw.b2.maxx = SINT16(node->l.bounds.maxx);
1190 raw.b2.maxy = SINT16(node->l.bounds.maxy);
1191
1192 if (node->r.node)
1193 raw.right = UINT16(node->r.node->index);
1194 else if (node->r.subsec)
1195 raw.right = UINT16(node->r.subsec->index | 0x8000);
1196 else
1197 InternalError("Bad right child in node %d", node->index);
1198
1199 if (node->l.node)
1200 raw.left = UINT16(node->l.node->index);
1201 else if (node->l.subsec)
1202 raw.left = UINT16(node->l.subsec->index | 0x8000);
1203 else
1204 InternalError("Bad left child in node %d", node->index);
1205
1206 AppendLevelLump(lump, &raw, sizeof(raw));
1207
1208 # if DEBUG_BSP
1209 PrintDebug("PUT NODE %04X Left %04X Right %04X "
1210 "(%d,%d) -> (%d,%d)\n", node->index, UINT16(raw.left),
1211 UINT16(raw.right), node->x, node->y,
1212 node->x + node->dx, node->y + node->dy);
1213 # endif
1214 }
1215
PutOneV5Node(node_t * node,lump_t * lump)1216 static void PutOneV5Node(node_t *node, lump_t *lump)
1217 {
1218 raw_v5_node_t raw;
1219
1220 if (node->r.node)
1221 PutOneV5Node(node->r.node, lump);
1222
1223 if (node->l.node)
1224 PutOneV5Node(node->l.node, lump);
1225
1226 node->index = node_cur_index++;
1227
1228 raw.x = SINT16(node->x);
1229 raw.y = SINT16(node->y);
1230 raw.dx = SINT16(node->dx / (node->too_long ? 2 : 1));
1231 raw.dy = SINT16(node->dy / (node->too_long ? 2 : 1));
1232
1233 raw.b1.minx = SINT16(node->r.bounds.minx);
1234 raw.b1.miny = SINT16(node->r.bounds.miny);
1235 raw.b1.maxx = SINT16(node->r.bounds.maxx);
1236 raw.b1.maxy = SINT16(node->r.bounds.maxy);
1237
1238 raw.b2.minx = SINT16(node->l.bounds.minx);
1239 raw.b2.miny = SINT16(node->l.bounds.miny);
1240 raw.b2.maxx = SINT16(node->l.bounds.maxx);
1241 raw.b2.maxy = SINT16(node->l.bounds.maxy);
1242
1243 if (node->r.node)
1244 raw.right = UINT32(node->r.node->index);
1245 else if (node->r.subsec)
1246 raw.right = UINT32(node->r.subsec->index | 0x80000000U);
1247 else
1248 InternalError("Bad right child in V5 node %d", node->index);
1249
1250 if (node->l.node)
1251 raw.left = UINT32(node->l.node->index);
1252 else if (node->l.subsec)
1253 raw.left = UINT32(node->l.subsec->index | 0x80000000U);
1254 else
1255 InternalError("Bad left child in V5 node %d", node->index);
1256
1257 AppendLevelLump(lump, &raw, sizeof(raw));
1258
1259 # if DEBUG_BSP
1260 PrintDebug("PUT V5 NODE %08X Left %08X Right %08X "
1261 "(%d,%d) -> (%d,%d)\n", node->index, UINT32(raw.left),
1262 UINT32(raw.right), node->x, node->y,
1263 node->x + node->dx, node->y + node->dy);
1264 # endif
1265 }
1266
PutNodes(char * name,int do_gl,int do_v5,node_t * root)1267 void PutNodes(char *name, int do_gl, int do_v5, node_t *root)
1268 {
1269 lump_t *lump;
1270
1271 DisplayTicker();
1272
1273 if (do_gl)
1274 lump = CreateGLLump(name);
1275 else
1276 lump = CreateLevelLump(name);
1277
1278 node_cur_index = 0;
1279
1280 if (root)
1281 {
1282 if (do_v5)
1283 PutOneV5Node(root, lump);
1284 else
1285 PutOneNode(root, lump);
1286 }
1287
1288 if (node_cur_index != num_nodes)
1289 InternalError("PutNodes miscounted (%d != %d)",
1290 node_cur_index, num_nodes);
1291
1292 if (!do_v5 && node_cur_index > 32767)
1293 MarkHardFailure(LIMIT_NODES);
1294 }
1295
1296
1297 /* ----- ZDBSP format writing --------------------------- */
1298
1299 static const uint8_g *lev_ZD_magic = (uint8_g *) "ZNOD";
1300
PutZVertices(void)1301 void PutZVertices(void)
1302 {
1303 int count, i;
1304
1305 uint32_g orgverts = UINT32(num_normal_vert);
1306 uint32_g newverts = UINT32(num_gl_vert);
1307
1308 ZLibAppendLump(&orgverts, 4);
1309 ZLibAppendLump(&newverts, 4);
1310
1311 DisplayTicker();
1312
1313 for (i=0, count=0; i < num_vertices; i++)
1314 {
1315 raw_v2_vertex_t raw;
1316 vertex_t *vert = lev_vertices[i];
1317
1318 if (! (vert->index & IS_GL_VERTEX))
1319 continue;
1320
1321 raw.x = SINT32((int)(vert->x * 65536.0));
1322 raw.y = SINT32((int)(vert->y * 65536.0));
1323
1324 ZLibAppendLump(&raw, sizeof(raw));
1325
1326 count++;
1327 }
1328
1329 if (count != num_gl_vert)
1330 InternalError("PutZVertices miscounted (%d != %d)",
1331 count, num_gl_vert);
1332 }
1333
PutZSubsecs(void)1334 void PutZSubsecs(void)
1335 {
1336 int i;
1337 int count;
1338 uint32_g raw_num = UINT32(num_subsecs);
1339
1340 int cur_seg_index = 0;
1341
1342 ZLibAppendLump(&raw_num, 4);
1343 DisplayTicker();
1344
1345 for (i=0; i < num_subsecs; i++)
1346 {
1347 subsec_t *sub = subsecs[i];
1348 seg_t *seg;
1349
1350 raw_num = UINT32(sub->seg_count);
1351
1352 ZLibAppendLump(&raw_num, 4);
1353
1354 // sanity check the seg index values
1355 count = 0;
1356 for (seg = sub->seg_list; seg; seg = seg->next, cur_seg_index++)
1357 {
1358 // ignore minisegs and degenerate segs
1359 if (! seg->linedef || seg->degenerate)
1360 continue;
1361
1362 if (cur_seg_index != seg->index)
1363 InternalError("PutZSubsecs: seg index mismatch in sub %d (%d != %d)\n",
1364 i, cur_seg_index, seg->index);
1365
1366 count++;
1367 }
1368
1369 if (count != sub->seg_count)
1370 InternalError("PutZSubsecs: miscounted segs in sub %d (%d != %d)\n",
1371 i, count, sub->seg_count);
1372 }
1373
1374 if (cur_seg_index != num_complete_seg)
1375 InternalError("PutZSubsecs miscounted segs (%d != %d)",
1376 cur_seg_index, num_complete_seg);
1377 }
1378
PutZSegs(void)1379 void PutZSegs(void)
1380 {
1381 int i, count;
1382 uint32_g raw_num = UINT32(num_complete_seg);
1383
1384 ZLibAppendLump(&raw_num, 4);
1385 DisplayTicker();
1386
1387 for (i=0, count=0; i < num_segs; i++)
1388 {
1389 seg_t *seg = segs[i];
1390
1391 // ignore minisegs and degenerate segs
1392 if (! seg->linedef || seg->degenerate)
1393 continue;
1394
1395 if (count != seg->index)
1396 InternalError("PutZSegs: seg index mismatch (%d != %d)\n",
1397 count, seg->index);
1398
1399 {
1400 uint32_g v1 = UINT32(VertexIndex32BitV5(seg->start));
1401 uint32_g v2 = UINT32(VertexIndex32BitV5(seg->end));
1402
1403 uint16_g line = UINT16(seg->linedef->index);
1404 uint8_g side = seg->side;
1405
1406 ZLibAppendLump(&v1, 4);
1407 ZLibAppendLump(&v2, 4);
1408 ZLibAppendLump(&line, 2);
1409 ZLibAppendLump(&side, 1);
1410 }
1411
1412 count++;
1413 }
1414
1415 if (count != num_complete_seg)
1416 InternalError("PutZSegs miscounted (%d != %d)",
1417 count, num_complete_seg);
1418 }
1419
PutOneZNode(node_t * node)1420 static void PutOneZNode(node_t *node)
1421 {
1422 raw_v5_node_t raw;
1423
1424 if (node->r.node)
1425 PutOneZNode(node->r.node);
1426
1427 if (node->l.node)
1428 PutOneZNode(node->l.node);
1429
1430 node->index = node_cur_index++;
1431
1432 raw.x = SINT16(node->x);
1433 raw.y = SINT16(node->y);
1434 raw.dx = SINT16(node->dx / (node->too_long ? 2 : 1));
1435 raw.dy = SINT16(node->dy / (node->too_long ? 2 : 1));
1436
1437 ZLibAppendLump(&raw.x, 2);
1438 ZLibAppendLump(&raw.y, 2);
1439 ZLibAppendLump(&raw.dx, 2);
1440 ZLibAppendLump(&raw.dy, 2);
1441
1442 raw.b1.minx = SINT16(node->r.bounds.minx);
1443 raw.b1.miny = SINT16(node->r.bounds.miny);
1444 raw.b1.maxx = SINT16(node->r.bounds.maxx);
1445 raw.b1.maxy = SINT16(node->r.bounds.maxy);
1446
1447 raw.b2.minx = SINT16(node->l.bounds.minx);
1448 raw.b2.miny = SINT16(node->l.bounds.miny);
1449 raw.b2.maxx = SINT16(node->l.bounds.maxx);
1450 raw.b2.maxy = SINT16(node->l.bounds.maxy);
1451
1452 ZLibAppendLump(&raw.b1, sizeof(raw.b1));
1453 ZLibAppendLump(&raw.b2, sizeof(raw.b2));
1454
1455 if (node->r.node)
1456 raw.right = UINT32(node->r.node->index);
1457 else if (node->r.subsec)
1458 raw.right = UINT32(node->r.subsec->index | 0x80000000U);
1459 else
1460 InternalError("Bad right child in V5 node %d", node->index);
1461
1462 if (node->l.node)
1463 raw.left = UINT32(node->l.node->index);
1464 else if (node->l.subsec)
1465 raw.left = UINT32(node->l.subsec->index | 0x80000000U);
1466 else
1467 InternalError("Bad left child in V5 node %d", node->index);
1468
1469 ZLibAppendLump(&raw.right, 4);
1470 ZLibAppendLump(&raw.left, 4);
1471
1472 # if DEBUG_BSP
1473 PrintDebug("PUT Z NODE %08X Left %08X Right %08X "
1474 "(%d,%d) -> (%d,%d)\n", node->index, UINT32(raw.left),
1475 UINT32(raw.right), node->x, node->y,
1476 node->x + node->dx, node->y + node->dy);
1477 # endif
1478 }
1479
PutZNodes(node_t * root)1480 void PutZNodes(node_t *root)
1481 {
1482 uint32_g raw_num = UINT32(num_nodes);
1483
1484 ZLibAppendLump(&raw_num, 4);
1485 DisplayTicker();
1486
1487 node_cur_index = 0;
1488
1489 if (root)
1490 PutOneZNode(root);
1491
1492 if (node_cur_index != num_nodes)
1493 InternalError("PutZNodes miscounted (%d != %d)",
1494 node_cur_index, num_nodes);
1495 }
1496
SaveZDFormat(node_t * root_node)1497 void SaveZDFormat(node_t *root_node)
1498 {
1499 lump_t *lump;
1500
1501 // leave SEGS and SSECTORS empty
1502 CreateLevelLump("SEGS");
1503 CreateLevelLump("SSECTORS");
1504
1505 lump = CreateLevelLump("NODES");
1506
1507 AppendLevelLump(lump, lev_ZD_magic, 4);
1508
1509 ZLibBeginLump(lump);
1510
1511 PutZVertices();
1512 PutZSubsecs();
1513 PutZSegs();
1514 PutZNodes(root_node);
1515
1516 ZLibFinishLump();
1517 }
1518
1519
1520 /* ----- whole-level routines --------------------------- */
1521
1522 //
1523 // LoadLevel
1524 //
LoadLevel(void)1525 void LoadLevel(void)
1526 {
1527 char message[256];
1528
1529 const char *level_name = GetLevelName();
1530
1531 boolean_g normal_exists = CheckForNormalNodes();
1532
1533 lev_doing_normal = !cur_info->gwa_mode && (cur_info->force_normal ||
1534 (!cur_info->no_normal && !normal_exists));
1535
1536 // -JL- Identify Hexen mode by presence of BEHAVIOR lump
1537 lev_doing_hexen = (FindLevelLump("BEHAVIOR") != NULL);
1538
1539 if (lev_doing_normal)
1540 sprintf(message, "Building normal and GL nodes on %s", level_name);
1541 else
1542 sprintf(message, "Building GL nodes on %s", level_name);
1543
1544 if (lev_doing_hexen)
1545 strcat(message, " (Hexen)");
1546
1547 lev_doing_hexen |= cur_info->force_hexen;
1548
1549 DisplaySetBarText(1, message);
1550
1551 PrintVerbose("\n\n");
1552 PrintMsg("%s\n", message);
1553 PrintVerbose("\n");
1554
1555 GetVertices();
1556 GetSectors();
1557 GetSidedefs();
1558
1559 if (lev_doing_hexen)
1560 {
1561 GetLinedefsHexen();
1562 GetThingsHexen();
1563 }
1564 else
1565 {
1566 GetLinedefs();
1567 GetThings();
1568 }
1569
1570 PrintVerbose("Loaded %d vertices, %d sectors, %d sides, %d lines, %d things\n",
1571 num_vertices, num_sectors, num_sidedefs, num_linedefs, num_things);
1572
1573 if (cur_info->fast && !lev_doing_normal &&
1574 normal_exists && num_sectors > 5 && num_linedefs > 100)
1575 {
1576 PrintVerbose("Using original nodes to speed things up\n");
1577 GetStaleNodes();
1578 }
1579
1580 if (lev_doing_normal)
1581 {
1582 // NOTE: order here is critical
1583
1584 if (cur_info->pack_sides)
1585 DetectDuplicateSidedefs();
1586
1587 if (cur_info->merge_vert)
1588 DetectDuplicateVertices();
1589
1590 if (!cur_info->no_prune)
1591 PruneLinedefs();
1592
1593 // always prune vertices (ignore -noprune), otherwise all the
1594 // unused vertices from seg splits would keep accumulating.
1595 PruneVertices();
1596
1597 if (!cur_info->no_prune)
1598 PruneSidedefs();
1599
1600 if (cur_info->prune_sect)
1601 PruneSectors();
1602 }
1603
1604 CalculateWallTips();
1605
1606 if (lev_doing_hexen)
1607 {
1608 // -JL- Find sectors containing polyobjs
1609 DetectPolyobjSectors();
1610 }
1611
1612 DetectOverlappingLines();
1613 DetectWindowEffects();
1614 }
1615
1616 //
1617 // FreeLevel
1618 //
FreeLevel(void)1619 void FreeLevel(void)
1620 {
1621 FreeVertices();
1622 FreeSidedefs();
1623 FreeLinedefs();
1624 FreeSectors();
1625 FreeThings();
1626 FreeSegs();
1627 FreeSubsecs();
1628 FreeNodes();
1629 FreeStaleNodes();
1630 FreeWallTips();
1631 }
1632
1633 //
1634 // PutGLChecksum
1635 //
PutGLChecksum(void)1636 void PutGLChecksum(void)
1637 {
1638 uint32_g crc;
1639 lump_t *lump;
1640 char num_buf[32];
1641
1642 Adler32_Begin(&crc);
1643
1644 lump = FindLevelLump("VERTEXES");
1645
1646 if (lump && lump->length > 0)
1647 Adler32_AddBlock(&crc, lump->data, lump->length);
1648
1649 lump = FindLevelLump("LINEDEFS");
1650
1651 if (lump && lump->length > 0)
1652 Adler32_AddBlock(&crc, lump->data, lump->length);
1653
1654 Adler32_Finish(&crc);
1655
1656 sprintf(num_buf, "0x%08x", crc);
1657
1658 AddGLTextLine("CHECKSUM", num_buf);
1659 }
1660
1661 //
1662 // SaveLevel
1663 //
SaveLevel(node_t * root_node)1664 void SaveLevel(node_t *root_node)
1665 {
1666 lev_force_v3 = (cur_info->spec_version == 3) ? TRUE : FALSE;
1667 lev_force_v5 = (cur_info->spec_version == 5) ? TRUE : FALSE;
1668
1669 // Note: RoundOffBspTree will convert the GL vertices in segs to
1670 // their normal counterparts (pointer change: use normal_dup).
1671
1672 if (cur_info->spec_version == 1)
1673 RoundOffBspTree(root_node);
1674
1675 // GL Nodes
1676 {
1677 if (num_normal_vert > 32767 || num_gl_vert > 32767)
1678 {
1679 if (cur_info->spec_version < 3)
1680 {
1681 lev_force_v5 = TRUE;
1682 MarkV5Switch(LIMIT_VERTEXES | LIMIT_GL_SEGS);
1683 }
1684 }
1685
1686 if (num_segs > 65534)
1687 {
1688 if (cur_info->spec_version < 3)
1689 {
1690 lev_force_v5 = TRUE;
1691 MarkV5Switch(LIMIT_GL_SSECT | LIMIT_GL_SEGS);
1692 }
1693 }
1694
1695 if (num_nodes > 32767)
1696 {
1697 if (cur_info->spec_version < 5)
1698 {
1699 lev_force_v5 = TRUE;
1700 MarkV5Switch(LIMIT_GL_NODES);
1701 }
1702 }
1703
1704 if (cur_info->spec_version == 1)
1705 PutVertices("GL_VERT", TRUE);
1706 else
1707 PutV2Vertices(lev_force_v5);
1708
1709 if (lev_force_v3 || lev_force_v5)
1710 PutV3Segs(lev_force_v5);
1711 else
1712 PutGLSegs();
1713
1714 if (lev_force_v3 || lev_force_v5)
1715 PutV3Subsecs(lev_force_v5);
1716 else
1717 PutSubsecs("GL_SSECT", TRUE);
1718
1719 PutNodes("GL_NODES", TRUE, lev_force_v5, root_node);
1720
1721 // -JL- Add empty PVS lump
1722 CreateGLLump("GL_PVS");
1723 }
1724
1725 if (lev_doing_normal)
1726 {
1727 if (cur_info->spec_version != 1)
1728 RoundOffBspTree(root_node);
1729
1730 NormaliseBspTree(root_node);
1731
1732 PutVertices("VERTEXES", FALSE);
1733 PutSectors();
1734 PutSidedefs();
1735
1736 if (lev_doing_hexen)
1737 PutLinedefsHexen();
1738 else
1739 PutLinedefs();
1740
1741 if (lev_force_v5)
1742 {
1743 // don't report a problem when -v5 was explicitly given
1744 if (cur_info->spec_version < 5)
1745 MarkZDSwitch();
1746
1747 SaveZDFormat(root_node);
1748 }
1749 else
1750 {
1751 PutSegs();
1752 PutSubsecs("SSECTORS", FALSE);
1753 PutNodes("NODES", FALSE, FALSE, root_node);
1754 }
1755
1756 // -JL- Don't touch blockmap and reject if not doing normal nodes
1757 PutBlockmap();
1758
1759 if (!cur_info->no_reject || !FindLevelLump("REJECT"))
1760 PutReject();
1761 }
1762
1763 // keyword support (v5.0 of the specs)
1764 AddGLTextLine("BUILDER", "glBSP " GLBSP_VER);
1765 {
1766 const char *time_str = UtilTimeString();
1767 if (time_str)
1768 AddGLTextLine("TIME", time_str);
1769 }
1770
1771 // this must be done _after_ the normal nodes have been built,
1772 // so that we use the new VERTEXES lump in the checksum.
1773 PutGLChecksum();
1774 }
1775
1776