1
//------------------------------------------------------------------------
2
// LEVEL : Level structure read/write functions.
3
//------------------------------------------------------------------------
5
// GL-Friendly Node Builder (C) 2000-2005 Andrew Apted
7
// Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
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.
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.
19
//------------------------------------------------------------------------
21
// ZDBSP format support based on code (C) 2002,2003 Randy Heit
23
//------------------------------------------------------------------------
50
#define ALLOC_BLKNUM 1024
53
// per-level variables
55
boolean_g lev_doing_normal;
56
boolean_g lev_doing_hexen;
58
static boolean_g lev_force_v3;
59
static boolean_g lev_force_v5;
62
#define LEVELARRAY(TYPE, BASEVAR, NUMVAR) \
63
TYPE ** BASEVAR = NULL; \
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)
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)
80
int num_normal_vert = 0;
82
int num_complete_seg = 0;
85
/* ----- allocation routines ---------------------------- */
87
#define ALLIGATOR(TYPE, BASEVAR, NUMVAR) \
89
if ((NUMVAR % ALLOC_BLKNUM) == 0) \
91
BASEVAR = UtilRealloc(BASEVAR, (NUMVAR + ALLOC_BLKNUM) * \
94
BASEVAR[NUMVAR] = (TYPE *) UtilCalloc(sizeof(TYPE)); \
96
return BASEVAR[NUMVAR - 1]; \
100
vertex_t *NewVertex(void)
101
ALLIGATOR(vertex_t, lev_vertices, num_vertices)
103
linedef_t *NewLinedef(void)
104
ALLIGATOR(linedef_t, lev_linedefs, num_linedefs)
106
sidedef_t *NewSidedef(void)
107
ALLIGATOR(sidedef_t, lev_sidedefs, num_sidedefs)
109
sector_t *NewSector(void)
110
ALLIGATOR(sector_t, lev_sectors, num_sectors)
112
thing_t *NewThing(void)
113
ALLIGATOR(thing_t, lev_things, num_things)
116
ALLIGATOR(seg_t, segs, num_segs)
118
subsec_t *NewSubsec(void)
119
ALLIGATOR(subsec_t, subsecs, num_subsecs)
121
node_t *NewNode(void)
122
ALLIGATOR(node_t, nodes, num_nodes)
124
node_t *NewStaleNode(void)
125
ALLIGATOR(node_t, stale_nodes, num_stale_nodes)
127
wall_tip_t *NewWallTip(void)
128
ALLIGATOR(wall_tip_t, wall_tips, num_wall_tips)
131
/* ----- free routines ---------------------------- */
133
#define FREEMASON(TYPE, BASEVAR, NUMVAR) \
136
for (i=0; i < NUMVAR; i++) \
137
UtilFree(BASEVAR[i]); \
140
BASEVAR = NULL; NUMVAR = 0; \
144
void FreeVertices(void)
145
FREEMASON(vertex_t, lev_vertices, num_vertices)
147
void FreeLinedefs(void)
148
FREEMASON(linedef_t, lev_linedefs, num_linedefs)
150
void FreeSidedefs(void)
151
FREEMASON(sidedef_t, lev_sidedefs, num_sidedefs)
153
void FreeSectors(void)
154
FREEMASON(sector_t, lev_sectors, num_sectors)
156
void FreeThings(void)
157
FREEMASON(thing_t, lev_things, num_things)
160
FREEMASON(seg_t, segs, num_segs)
162
void FreeSubsecs(void)
163
FREEMASON(subsec_t, subsecs, num_subsecs)
166
FREEMASON(node_t, nodes, num_nodes)
168
void FreeStaleNodes(void)
169
FREEMASON(node_t, stale_nodes, num_stale_nodes)
171
void FreeWallTips(void)
172
FREEMASON(wall_tip_t, wall_tips, num_wall_tips)
175
/* ----- lookup routines ------------------------------ */
177
#define LOOKERUPPER(BASEVAR, NUMVAR, NAMESTR) \
179
if (index < 0 || index >= NUMVAR) \
180
FatalError("No such %s number #%d", NAMESTR, index); \
182
return BASEVAR[index]; \
185
vertex_t *LookupVertex(int index)
186
LOOKERUPPER(lev_vertices, num_vertices, "vertex")
188
linedef_t *LookupLinedef(int index)
189
LOOKERUPPER(lev_linedefs, num_linedefs, "linedef")
191
sidedef_t *LookupSidedef(int index)
192
LOOKERUPPER(lev_sidedefs, num_sidedefs, "sidedef")
194
sector_t *LookupSector(int index)
195
LOOKERUPPER(lev_sectors, num_sectors, "sector")
197
thing_t *LookupThing(int index)
198
LOOKERUPPER(lev_things, num_things, "thing")
200
seg_t *LookupSeg(int index)
201
LOOKERUPPER(segs, num_segs, "seg")
203
subsec_t *LookupSubsec(int index)
204
LOOKERUPPER(subsecs, num_subsecs, "subsector")
206
node_t *LookupNode(int index)
207
LOOKERUPPER(nodes, num_nodes, "node")
209
node_t *LookupStaleNode(int index)
210
LOOKERUPPER(stale_nodes, num_stale_nodes, "stale_node")
213
/* ----- reading routines ------------------------------ */
217
// CheckForNormalNodes
219
int CheckForNormalNodes(void)
223
/* Note: an empty NODES lump can be valid */
224
if (FindLevelLump("NODES") == NULL)
227
lump = FindLevelLump("SEGS");
229
if (! lump || lump->length == 0 || CheckLevelLumpZero(lump))
232
lump = FindLevelLump("SSECTORS");
234
if (! lump || lump->length == 0 || CheckLevelLumpZero(lump))
243
void GetVertices(void)
247
lump_t *lump = FindLevelLump("VERTEXES");
250
count = lump->length / sizeof(raw_vertex_t);
255
PrintDebug("GetVertices: num = %d\n", count);
258
if (!lump || count == 0)
259
FatalError("Couldn't find any Vertices");
261
raw = (raw_vertex_t *) lump->data;
263
for (i=0; i < count; i++, raw++)
265
vertex_t *vert = NewVertex();
267
vert->x = (float_g) SINT16(raw->x);
268
vert->y = (float_g) SINT16(raw->y);
273
num_normal_vert = num_vertices;
275
num_complete_seg = 0;
281
void GetSectors(void)
285
lump_t *lump = FindLevelLump("SECTORS");
288
count = lump->length / sizeof(raw_sector_t);
290
if (!lump || count == 0)
291
FatalError("Couldn't find any Sectors");
296
PrintDebug("GetSectors: num = %d\n", count);
299
raw = (raw_sector_t *) lump->data;
301
for (i=0; i < count; i++, raw++)
303
sector_t *sector = NewSector();
305
sector->floor_h = SINT16(raw->floor_h);
306
sector->ceil_h = SINT16(raw->ceil_h);
308
memcpy(sector->floor_tex, raw->floor_tex, sizeof(sector->floor_tex));
309
memcpy(sector->ceil_tex, raw->ceil_tex, sizeof(sector->ceil_tex));
311
sector->light = UINT16(raw->light);
312
sector->special = UINT16(raw->special);
313
sector->tag = SINT16(raw->tag);
315
sector->coalesce = (sector->tag >= 900 && sector->tag < 1000) ?
318
/* sector indices never change */
321
sector->warned_facing = -1;
323
/* Note: rej_* fields are handled completely in reject.c */
334
lump_t *lump = FindLevelLump("THINGS");
337
count = lump->length / sizeof(raw_thing_t);
339
if (!lump || count == 0)
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");
350
PrintDebug("GetThings: num = %d\n", count);
353
raw = (raw_thing_t *) lump->data;
355
for (i=0; i < count; i++, raw++)
357
thing_t *thing = NewThing();
359
thing->x = SINT16(raw->x);
360
thing->y = SINT16(raw->y);
362
thing->type = UINT16(raw->type);
363
thing->options = UINT16(raw->options);
372
void GetThingsHexen(void)
375
raw_hexen_thing_t *raw;
376
lump_t *lump = FindLevelLump("THINGS");
379
count = lump->length / sizeof(raw_hexen_thing_t);
381
if (!lump || count == 0)
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");
392
PrintDebug("GetThingsHexen: num = %d\n", count);
395
raw = (raw_hexen_thing_t *) lump->data;
397
for (i=0; i < count; i++, raw++)
399
thing_t *thing = NewThing();
401
thing->x = SINT16(raw->x);
402
thing->y = SINT16(raw->y);
404
thing->type = UINT16(raw->type);
405
thing->options = UINT16(raw->options);
414
void GetSidedefs(void)
418
lump_t *lump = FindLevelLump("SIDEDEFS");
421
count = lump->length / sizeof(raw_sidedef_t);
423
if (!lump || count == 0)
424
FatalError("Couldn't find any Sidedefs");
429
PrintDebug("GetSidedefs: num = %d\n", count);
432
raw = (raw_sidedef_t *) lump->data;
434
for (i=0; i < count; i++, raw++)
436
sidedef_t *side = NewSidedef();
438
side->sector = (SINT16(raw->sector) == -1) ? NULL :
439
LookupSector(UINT16(raw->sector));
442
side->sector->ref_count++;
444
side->x_offset = SINT16(raw->x_offset);
445
side->y_offset = SINT16(raw->y_offset);
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));
451
/* sidedef indices never change */
456
static INLINE_G sidedef_t *SafeLookupSidedef(uint16_g num)
461
if ((int)num >= num_sidedefs && (sint16_g)(num) < 0)
464
return LookupSidedef(num);
470
void GetLinedefs(void)
474
lump_t *lump = FindLevelLump("LINEDEFS");
477
count = lump->length / sizeof(raw_linedef_t);
479
if (!lump || count == 0)
480
FatalError("Couldn't find any Linedefs");
485
PrintDebug("GetLinedefs: num = %d\n", count);
488
raw = (raw_linedef_t *) lump->data;
490
for (i=0; i < count; i++, raw++)
494
vertex_t *start = LookupVertex(UINT16(raw->start));
495
vertex_t *end = LookupVertex(UINT16(raw->end));
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);
509
line->flags = UINT16(raw->flags);
510
line->type = UINT16(raw->type);
511
line->tag = SINT16(raw->tag);
513
line->two_sided = (line->flags & LINEFLAG_TWO_SIDED) ? TRUE : FALSE;
514
line->is_precious = (line->tag >= 900 && line->tag < 1000) ?
517
line->right = SafeLookupSidedef(UINT16(raw->sidedef1));
518
line->left = SafeLookupSidedef(UINT16(raw->sidedef2));
522
line->right->ref_count++;
523
line->right->on_special |= (line->type > 0) ? 1 : 0;
528
line->left->ref_count++;
529
line->left->on_special |= (line->type > 0) ? 1 : 0;
532
line->self_ref = (line->left && line->right &&
533
(line->left->sector == line->right->sector));
542
void GetLinedefsHexen(void)
545
raw_hexen_linedef_t *raw;
546
lump_t *lump = FindLevelLump("LINEDEFS");
549
count = lump->length / sizeof(raw_hexen_linedef_t);
551
if (!lump || count == 0)
552
FatalError("Couldn't find any Linedefs");
557
PrintDebug("GetLinedefsHexen: num = %d\n", count);
560
raw = (raw_hexen_linedef_t *) lump->data;
562
for (i=0; i < count; i++, raw++)
566
vertex_t *start = LookupVertex(UINT16(raw->start));
567
vertex_t *end = LookupVertex(UINT16(raw->end));
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);
581
line->flags = UINT16(raw->flags);
582
line->type = UINT8(raw->type);
586
for (j=0; j < 5; j++)
587
line->specials[j] = UINT8(raw->specials[j]);
589
// -JL- Added missing twosided flag handling that caused a broken reject
590
line->two_sided = (line->flags & LINEFLAG_TWO_SIDED) ? TRUE : FALSE;
592
line->right = SafeLookupSidedef(UINT16(raw->sidedef1));
593
line->left = SafeLookupSidedef(UINT16(raw->sidedef2));
595
// -JL- Added missing sidedef handling that caused all sidedefs to be pruned
598
line->right->ref_count++;
599
line->right->on_special |= (line->type > 0) ? 1 : 0;
604
line->left->ref_count++;
605
line->left->on_special |= (line->type > 0) ? 1 : 0;
615
void GetStaleNodes(void)
619
lump_t *lump = FindLevelLump("NODES");
622
count = lump->length / sizeof(raw_node_t);
624
if (!lump || count < 5)
630
PrintDebug("GetStaleNodes: num = %d\n", count);
633
raw = (raw_node_t *) lump->data;
635
/* must allocate all the nodes beforehand, since they contain
636
* internal references to each other.
638
for (i=0; i < count; i++)
643
for (i=0; i < count; i++, raw++)
645
node_t *nd = LookupStaleNode(i);
647
nd->x = SINT16(raw->x);
648
nd->y = SINT16(raw->y);
649
nd->dx = SINT16(raw->dx);
650
nd->dy = SINT16(raw->dy);
654
/* Note: we ignore the subsector references */
656
if ((UINT16(raw->right) & 0x8000U) == 0)
658
nd->r.node = LookupStaleNode(UINT16(raw->right));
661
if ((UINT16(raw->left) & 0x8000U) == 0)
663
nd->l.node = LookupStaleNode(UINT16(raw->left));
666
/* we also ignore the bounding boxes -- not needed */
671
static INLINE_G int TransformSegDist(const seg_t *seg)
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;
676
return (int) ceil(UtilComputeDist(seg->start->x - sx, seg->start->y - sy));
679
static INLINE_G int TransformAngle(angle_g angle)
683
result = (int)(angle * 65536.0 / 360.0);
688
return (result & 0xFFFF);
691
static int SegCompare(const void *p1, const void *p2)
693
const seg_t *A = ((const seg_t **) p1)[0];
694
const seg_t *B = ((const seg_t **) p2)[0];
697
InternalError("Seg %p never reached a subsector !", A);
700
InternalError("Seg %p never reached a subsector !", B);
702
return (A->index - B->index);
706
/* ----- writing routines ------------------------------ */
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";
712
void PutVertices(char *name, int do_gl)
720
lump = CreateGLLump(name);
722
lump = CreateLevelLump(name);
724
for (i=0, count=0; i < num_vertices; i++)
727
vertex_t *vert = lev_vertices[i];
729
if ((do_gl ? 1 : 0) != ((vert->index & IS_GL_VERTEX) ? 1 : 0))
734
raw.x = SINT16(I_ROUND(vert->x));
735
raw.y = SINT16(I_ROUND(vert->y));
737
AppendLevelLump(lump, &raw, sizeof(raw));
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);
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);
752
void PutV2Vertices(int do_v5)
759
lump = CreateGLLump("GL_VERT");
762
AppendLevelLump(lump, lev_v5_magic, 4);
764
AppendLevelLump(lump, lev_v2_magic, 4);
766
for (i=0, count=0; i < num_vertices; i++)
769
vertex_t *vert = lev_vertices[i];
771
if (! (vert->index & IS_GL_VERTEX))
774
raw.x = SINT32((int)(vert->x * 65536.0));
775
raw.y = SINT32((int)(vert->y * 65536.0));
777
AppendLevelLump(lump, &raw, sizeof(raw));
782
if (count != num_gl_vert)
783
InternalError("PutV2Vertices miscounted (%d != %d)", count,
787
MarkSoftFailure(LIMIT_GL_VERT);
790
void PutSectors(void)
793
lump_t *lump = CreateLevelLump("SECTORS");
797
for (i=0; i < num_sectors; i++)
800
sector_t *sector = lev_sectors[i];
802
raw.floor_h = SINT16(sector->floor_h);
803
raw.ceil_h = SINT16(sector->ceil_h);
805
memcpy(raw.floor_tex, sector->floor_tex, sizeof(raw.floor_tex));
806
memcpy(raw.ceil_tex, sector->ceil_tex, sizeof(raw.ceil_tex));
808
raw.light = UINT16(sector->light);
809
raw.special = UINT16(sector->special);
810
raw.tag = SINT16(sector->tag);
812
AppendLevelLump(lump, &raw, sizeof(raw));
815
if (num_sectors > 65534)
816
MarkHardFailure(LIMIT_SECTORS);
817
else if (num_sectors > 32767)
818
MarkSoftFailure(LIMIT_SECTORS);
821
void PutSidedefs(void)
824
lump_t *lump = CreateLevelLump("SIDEDEFS");
828
for (i=0; i < num_sidedefs; i++)
831
sidedef_t *side = lev_sidedefs[i];
833
raw.sector = (side->sector == NULL) ? SINT16(-1) :
834
UINT16(side->sector->index);
836
raw.x_offset = SINT16(side->x_offset);
837
raw.y_offset = SINT16(side->y_offset);
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));
843
AppendLevelLump(lump, &raw, sizeof(raw));
846
if (num_sidedefs > 65534)
847
MarkHardFailure(LIMIT_SIDEDEFS);
848
else if (num_sidedefs > 32767)
849
MarkSoftFailure(LIMIT_SIDEDEFS);
852
void PutLinedefs(void)
855
lump_t *lump = CreateLevelLump("LINEDEFS");
859
for (i=0; i < num_linedefs; i++)
862
linedef_t *line = lev_linedefs[i];
864
raw.start = UINT16(line->start->index);
865
raw.end = UINT16(line->end->index);
867
raw.flags = UINT16(line->flags);
868
raw.type = UINT16(line->type);
869
raw.tag = SINT16(line->tag);
871
raw.sidedef1 = line->right ? UINT16(line->right->index) : 0xFFFF;
872
raw.sidedef2 = line->left ? UINT16(line->left->index) : 0xFFFF;
874
AppendLevelLump(lump, &raw, sizeof(raw));
877
if (num_linedefs > 65534)
878
MarkHardFailure(LIMIT_LINEDEFS);
879
else if (num_linedefs > 32767)
880
MarkSoftFailure(LIMIT_LINEDEFS);
883
void PutLinedefsHexen(void)
886
lump_t *lump = CreateLevelLump("LINEDEFS");
890
for (i=0; i < num_linedefs; i++)
892
raw_hexen_linedef_t raw;
893
linedef_t *line = lev_linedefs[i];
895
raw.start = UINT16(line->start->index);
896
raw.end = UINT16(line->end->index);
898
raw.flags = UINT16(line->flags);
899
raw.type = UINT8(line->type);
902
for (j=0; j < 5; j++)
903
raw.specials[j] = UINT8(line->specials[j]);
905
raw.sidedef1 = line->right ? UINT16(line->right->index) : 0xFFFF;
906
raw.sidedef2 = line->left ? UINT16(line->left->index) : 0xFFFF;
908
AppendLevelLump(lump, &raw, sizeof(raw));
911
if (num_linedefs > 65534)
912
MarkHardFailure(LIMIT_LINEDEFS);
913
else if (num_linedefs > 32767)
914
MarkSoftFailure(LIMIT_LINEDEFS);
917
static INLINE_G uint16_g VertexIndex16Bit(const vertex_t *v)
919
if (v->index & IS_GL_VERTEX)
920
return (uint16_g) ((v->index & ~IS_GL_VERTEX) | 0x8000U);
922
return (uint16_g) v->index;
925
static INLINE_G uint32_g VertexIndex32BitV5(const vertex_t *v)
927
if (v->index & IS_GL_VERTEX)
928
return (uint32_g) ((v->index & ~IS_GL_VERTEX) | 0x80000000U);
930
return (uint32_g) v->index;
936
lump_t *lump = CreateLevelLump("SEGS");
940
// sort segs into ascending index
941
qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
943
for (i=0, count=0; i < num_segs; i++)
946
seg_t *seg = segs[i];
948
// ignore minisegs and degenerate segs
949
if (! seg->linedef || seg->degenerate)
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));
959
AppendLevelLump(lump, &raw, sizeof(raw));
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);
972
if (count != num_complete_seg)
973
InternalError("PutSegs miscounted (%d != %d)", count,
977
MarkHardFailure(LIMIT_SEGS);
978
else if (count > 32767)
979
MarkSoftFailure(LIMIT_SEGS);
985
lump_t *lump = CreateGLLump("GL_SEGS");
989
// sort segs into ascending index
990
qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
992
for (i=0, count=0; i < num_segs; i++)
995
seg_t *seg = segs[i];
997
// ignore degenerate segs
1001
raw.start = UINT16(VertexIndex16Bit(seg->start));
1002
raw.end = UINT16(VertexIndex16Bit(seg->end));
1003
raw.side = UINT16(seg->side);
1006
raw.linedef = UINT16(seg->linedef->index);
1008
raw.linedef = UINT16(0xFFFF);
1011
raw.partner = UINT16(seg->partner->index);
1013
raw.partner = UINT16(0xFFFF);
1015
AppendLevelLump(lump, &raw, sizeof(raw));
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);
1027
if (count != num_complete_seg)
1028
InternalError("PutGLSegs miscounted (%d != %d)", count,
1032
InternalError("PutGLSegs with %d (> 65534) segs", count);
1033
else if (count > 32767)
1034
MarkSoftFailure(LIMIT_GL_SEGS);
1037
void PutV3Segs(int do_v5)
1040
lump_t *lump = CreateGLLump("GL_SEGS");
1043
AppendLevelLump(lump, lev_v3_magic, 4);
1047
// sort segs into ascending index
1048
qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
1050
for (i=0, count=0; i < num_segs; i++)
1053
seg_t *seg = segs[i];
1055
// ignore degenerate segs
1056
if (seg->degenerate)
1061
raw.start = UINT32(VertexIndex32BitV5(seg->start));
1062
raw.end = UINT32(VertexIndex32BitV5(seg->end));
1066
raw.start = UINT32(seg->start->index);
1067
raw.end = UINT32(seg->end->index);
1070
raw.side = UINT16(seg->side);
1073
raw.linedef = UINT16(seg->linedef->index);
1075
raw.linedef = UINT16(0xFFFF);
1078
raw.partner = UINT32(seg->partner->index);
1080
raw.partner = UINT32(0xFFFFFFFF);
1082
AppendLevelLump(lump, &raw, sizeof(raw));
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);
1094
if (count != num_complete_seg)
1095
InternalError("PutGLSegs miscounted (%d != %d)", count,
1099
void PutSubsecs(char *name, int do_gl)
1107
lump = CreateGLLump(name);
1109
lump = CreateLevelLump(name);
1111
for (i=0; i < num_subsecs; i++)
1114
subsec_t *sub = subsecs[i];
1116
raw.first = UINT16(sub->seg_list->index);
1117
raw.num = UINT16(sub->seg_count);
1119
AppendLevelLump(lump, &raw, sizeof(raw));
1122
PrintDebug("PUT SUBSEC %04X First %04X Num %04X\n",
1123
sub->index, UINT16(raw.first), UINT16(raw.num));
1127
if (num_subsecs > 32767)
1128
MarkHardFailure(do_gl ? LIMIT_GL_SSECT : LIMIT_SSECTORS);
1131
void PutV3Subsecs(int do_v5)
1138
lump = CreateGLLump("GL_SSECT");
1141
AppendLevelLump(lump, lev_v3_magic, 4);
1143
for (i=0; i < num_subsecs; i++)
1145
raw_v3_subsec_t raw;
1146
subsec_t *sub = subsecs[i];
1148
raw.first = UINT32(sub->seg_list->index);
1149
raw.num = UINT32(sub->seg_count);
1151
AppendLevelLump(lump, &raw, sizeof(raw));
1154
PrintDebug("PUT V3 SUBSEC %06X First %06X Num %06X\n",
1155
sub->index, UINT32(raw.first), UINT32(raw.num));
1159
if (!do_v5 && num_subsecs > 32767)
1160
MarkHardFailure(LIMIT_GL_SSECT);
1163
static int node_cur_index;
1165
static void PutOneNode(node_t *node, lump_t *lump)
1170
PutOneNode(node->r.node, lump);
1173
PutOneNode(node->l.node, lump);
1175
node->index = node_cur_index++;
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));
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);
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);
1193
raw.right = UINT16(node->r.node->index);
1194
else if (node->r.subsec)
1195
raw.right = UINT16(node->r.subsec->index | 0x8000);
1197
InternalError("Bad right child in node %d", node->index);
1200
raw.left = UINT16(node->l.node->index);
1201
else if (node->l.subsec)
1202
raw.left = UINT16(node->l.subsec->index | 0x8000);
1204
InternalError("Bad left child in node %d", node->index);
1206
AppendLevelLump(lump, &raw, sizeof(raw));
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);
1216
static void PutOneV5Node(node_t *node, lump_t *lump)
1221
PutOneV5Node(node->r.node, lump);
1224
PutOneV5Node(node->l.node, lump);
1226
node->index = node_cur_index++;
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));
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);
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);
1244
raw.right = UINT32(node->r.node->index);
1245
else if (node->r.subsec)
1246
raw.right = UINT32(node->r.subsec->index | 0x80000000U);
1248
InternalError("Bad right child in V5 node %d", node->index);
1251
raw.left = UINT32(node->l.node->index);
1252
else if (node->l.subsec)
1253
raw.left = UINT32(node->l.subsec->index | 0x80000000U);
1255
InternalError("Bad left child in V5 node %d", node->index);
1257
AppendLevelLump(lump, &raw, sizeof(raw));
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);
1267
void PutNodes(char *name, int do_gl, int do_v5, node_t *root)
1274
lump = CreateGLLump(name);
1276
lump = CreateLevelLump(name);
1283
PutOneV5Node(root, lump);
1285
PutOneNode(root, lump);
1288
if (node_cur_index != num_nodes)
1289
InternalError("PutNodes miscounted (%d != %d)",
1290
node_cur_index, num_nodes);
1292
if (!do_v5 && node_cur_index > 32767)
1293
MarkHardFailure(LIMIT_NODES);
1297
/* ----- ZDBSP format writing --------------------------- */
1299
static const uint8_g *lev_ZD_magic = (uint8_g *) "ZNOD";
1301
void PutZVertices(void)
1305
uint32_g orgverts = UINT32(num_normal_vert);
1306
uint32_g newverts = UINT32(num_gl_vert);
1308
ZLibAppendLump(&orgverts, 4);
1309
ZLibAppendLump(&newverts, 4);
1313
for (i=0, count=0; i < num_vertices; i++)
1315
raw_v2_vertex_t raw;
1316
vertex_t *vert = lev_vertices[i];
1318
if (! (vert->index & IS_GL_VERTEX))
1321
raw.x = SINT32((int)(vert->x * 65536.0));
1322
raw.y = SINT32((int)(vert->y * 65536.0));
1324
ZLibAppendLump(&raw, sizeof(raw));
1329
if (count != num_gl_vert)
1330
InternalError("PutZVertices miscounted (%d != %d)",
1331
count, num_gl_vert);
1334
void PutZSubsecs(void)
1338
uint32_g raw_num = UINT32(num_subsecs);
1340
int cur_seg_index = 0;
1342
ZLibAppendLump(&raw_num, 4);
1345
for (i=0; i < num_subsecs; i++)
1347
subsec_t *sub = subsecs[i];
1350
raw_num = UINT32(sub->seg_count);
1352
ZLibAppendLump(&raw_num, 4);
1354
// sanity check the seg index values
1356
for (seg = sub->seg_list; seg; seg = seg->next, cur_seg_index++)
1358
// ignore minisegs and degenerate segs
1359
if (! seg->linedef || seg->degenerate)
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);
1369
if (count != sub->seg_count)
1370
InternalError("PutZSubsecs: miscounted segs in sub %d (%d != %d)\n",
1371
i, count, sub->seg_count);
1374
if (cur_seg_index != num_complete_seg)
1375
InternalError("PutZSubsecs miscounted segs (%d != %d)",
1376
cur_seg_index, num_complete_seg);
1382
uint32_g raw_num = UINT32(num_complete_seg);
1384
ZLibAppendLump(&raw_num, 4);
1387
for (i=0, count=0; i < num_segs; i++)
1389
seg_t *seg = segs[i];
1391
// ignore minisegs and degenerate segs
1392
if (! seg->linedef || seg->degenerate)
1395
if (count != seg->index)
1396
InternalError("PutZSegs: seg index mismatch (%d != %d)\n",
1400
uint32_g v1 = UINT32(VertexIndex32BitV5(seg->start));
1401
uint32_g v2 = UINT32(VertexIndex32BitV5(seg->end));
1403
uint16_g line = UINT16(seg->linedef->index);
1404
uint8_g side = seg->side;
1406
ZLibAppendLump(&v1, 4);
1407
ZLibAppendLump(&v2, 4);
1408
ZLibAppendLump(&line, 2);
1409
ZLibAppendLump(&side, 1);
1415
if (count != num_complete_seg)
1416
InternalError("PutZSegs miscounted (%d != %d)",
1417
count, num_complete_seg);
1420
static void PutOneZNode(node_t *node)
1425
PutOneZNode(node->r.node);
1428
PutOneZNode(node->l.node);
1430
node->index = node_cur_index++;
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));
1437
ZLibAppendLump(&raw.x, 2);
1438
ZLibAppendLump(&raw.y, 2);
1439
ZLibAppendLump(&raw.dx, 2);
1440
ZLibAppendLump(&raw.dy, 2);
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);
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);
1452
ZLibAppendLump(&raw.b1, sizeof(raw.b1));
1453
ZLibAppendLump(&raw.b2, sizeof(raw.b2));
1456
raw.right = UINT32(node->r.node->index);
1457
else if (node->r.subsec)
1458
raw.right = UINT32(node->r.subsec->index | 0x80000000U);
1460
InternalError("Bad right child in V5 node %d", node->index);
1463
raw.left = UINT32(node->l.node->index);
1464
else if (node->l.subsec)
1465
raw.left = UINT32(node->l.subsec->index | 0x80000000U);
1467
InternalError("Bad left child in V5 node %d", node->index);
1469
ZLibAppendLump(&raw.right, 4);
1470
ZLibAppendLump(&raw.left, 4);
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);
1480
void PutZNodes(node_t *root)
1482
uint32_g raw_num = UINT32(num_nodes);
1484
ZLibAppendLump(&raw_num, 4);
1492
if (node_cur_index != num_nodes)
1493
InternalError("PutZNodes miscounted (%d != %d)",
1494
node_cur_index, num_nodes);
1497
void SaveZDFormat(node_t *root_node)
1501
// leave SEGS and SSECTORS empty
1502
CreateLevelLump("SEGS");
1503
CreateLevelLump("SSECTORS");
1505
lump = CreateLevelLump("NODES");
1507
AppendLevelLump(lump, lev_ZD_magic, 4);
1509
ZLibBeginLump(lump);
1514
PutZNodes(root_node);
1520
/* ----- whole-level routines --------------------------- */
1525
void LoadLevel(void)
1529
const char *level_name = GetLevelName();
1531
boolean_g normal_exists = CheckForNormalNodes();
1533
lev_doing_normal = !cur_info->gwa_mode && (cur_info->force_normal ||
1534
(!cur_info->no_normal && !normal_exists));
1536
// -JL- Identify Hexen mode by presence of BEHAVIOR lump
1537
lev_doing_hexen = (FindLevelLump("BEHAVIOR") != NULL);
1539
if (lev_doing_normal)
1540
sprintf(message, "Building normal and GL nodes on %s", level_name);
1542
sprintf(message, "Building GL nodes on %s", level_name);
1544
if (lev_doing_hexen)
1545
strcat(message, " (Hexen)");
1547
lev_doing_hexen |= cur_info->force_hexen;
1549
DisplaySetBarText(1, message);
1551
PrintVerbose("\n\n");
1552
PrintMsg("%s\n", message);
1559
if (lev_doing_hexen)
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);
1573
if (cur_info->fast && !lev_doing_normal &&
1574
normal_exists && num_sectors > 5 && num_linedefs > 100)
1576
PrintVerbose("Using original nodes to speed things up\n");
1580
if (lev_doing_normal)
1582
// NOTE: order here is critical
1584
if (cur_info->pack_sides)
1585
DetectDuplicateSidedefs();
1587
if (cur_info->merge_vert)
1588
DetectDuplicateVertices();
1590
if (!cur_info->no_prune)
1593
// always prune vertices (ignore -noprune), otherwise all the
1594
// unused vertices from seg splits would keep accumulating.
1597
if (!cur_info->no_prune)
1600
if (cur_info->prune_sect)
1604
CalculateWallTips();
1606
if (lev_doing_hexen)
1608
// -JL- Find sectors containing polyobjs
1609
DetectPolyobjSectors();
1612
DetectOverlappingLines();
1613
DetectWindowEffects();
1619
void FreeLevel(void)
1636
void PutGLChecksum(void)
1642
Adler32_Begin(&crc);
1644
lump = FindLevelLump("VERTEXES");
1646
if (lump && lump->length > 0)
1647
Adler32_AddBlock(&crc, lump->data, lump->length);
1649
lump = FindLevelLump("LINEDEFS");
1651
if (lump && lump->length > 0)
1652
Adler32_AddBlock(&crc, lump->data, lump->length);
1654
Adler32_Finish(&crc);
1656
sprintf(num_buf, "0x%08x", crc);
1658
AddGLTextLine("CHECKSUM", num_buf);
1664
void SaveLevel(node_t *root_node)
1666
lev_force_v3 = (cur_info->spec_version == 3) ? TRUE : FALSE;
1667
lev_force_v5 = (cur_info->spec_version == 5) ? TRUE : FALSE;
1669
// Note: RoundOffBspTree will convert the GL vertices in segs to
1670
// their normal counterparts (pointer change: use normal_dup).
1672
if (cur_info->spec_version == 1)
1673
RoundOffBspTree(root_node);
1677
if (num_normal_vert > 32767 || num_gl_vert > 32767)
1679
if (cur_info->spec_version < 3)
1681
lev_force_v5 = TRUE;
1682
MarkV5Switch(LIMIT_VERTEXES | LIMIT_GL_SEGS);
1686
if (num_segs > 65534)
1688
if (cur_info->spec_version < 3)
1690
lev_force_v5 = TRUE;
1691
MarkV5Switch(LIMIT_GL_SSECT | LIMIT_GL_SEGS);
1695
if (num_nodes > 32767)
1697
if (cur_info->spec_version < 5)
1699
lev_force_v5 = TRUE;
1700
MarkV5Switch(LIMIT_GL_NODES);
1704
if (cur_info->spec_version == 1)
1705
PutVertices("GL_VERT", TRUE);
1707
PutV2Vertices(lev_force_v5);
1709
if (lev_force_v3 || lev_force_v5)
1710
PutV3Segs(lev_force_v5);
1714
if (lev_force_v3 || lev_force_v5)
1715
PutV3Subsecs(lev_force_v5);
1717
PutSubsecs("GL_SSECT", TRUE);
1719
PutNodes("GL_NODES", TRUE, lev_force_v5, root_node);
1721
// -JL- Add empty PVS lump
1722
CreateGLLump("GL_PVS");
1725
if (lev_doing_normal)
1727
if (cur_info->spec_version != 1)
1728
RoundOffBspTree(root_node);
1730
NormaliseBspTree(root_node);
1732
PutVertices("VERTEXES", FALSE);
1736
if (lev_doing_hexen)
1743
// don't report a problem when -v5 was explicitly given
1744
if (cur_info->spec_version < 5)
1747
SaveZDFormat(root_node);
1752
PutSubsecs("SSECTORS", FALSE);
1753
PutNodes("NODES", FALSE, FALSE, root_node);
1756
// -JL- Don't touch blockmap and reject if not doing normal nodes
1759
if (!cur_info->no_reject || !FindLevelLump("REJECT"))
1763
// keyword support (v5.0 of the specs)
1764
AddGLTextLine("BUILDER", "glBSP " GLBSP_VER);
1766
const char *time_str = UtilTimeString();
1768
AddGLTextLine("TIME", time_str);
1771
// this must be done _after_ the normal nodes have been built,
1772
// so that we use the new VERTEXES lump in the checksum.