1
//------------------------------------------------------------------------
2
// LEVEL : Level structure read/write functions.
3
//------------------------------------------------------------------------
5
// GL-Friendly Node Builder (C) 2000-2007 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!\n");
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!\n");
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;
608
line->self_ref = (line->left && line->right &&
609
(line->left->sector == line->right->sector));
618
void GetStaleNodes(void)
622
lump_t *lump = FindLevelLump("NODES");
625
count = lump->length / sizeof(raw_node_t);
627
if (!lump || count < 5)
633
PrintDebug("GetStaleNodes: num = %d\n", count);
636
raw = (raw_node_t *) lump->data;
638
/* must allocate all the nodes beforehand, since they contain
639
* internal references to each other.
641
for (i=0; i < count; i++)
646
for (i=0; i < count; i++, raw++)
648
node_t *nd = LookupStaleNode(i);
650
nd->x = SINT16(raw->x);
651
nd->y = SINT16(raw->y);
652
nd->dx = SINT16(raw->dx);
653
nd->dy = SINT16(raw->dy);
657
/* Note: we ignore the subsector references */
659
if ((UINT16(raw->right) & 0x8000U) == 0)
661
nd->r.node = LookupStaleNode(UINT16(raw->right));
664
if ((UINT16(raw->left) & 0x8000U) == 0)
666
nd->l.node = LookupStaleNode(UINT16(raw->left));
669
/* we also ignore the bounding boxes -- not needed */
674
static INLINE_G int TransformSegDist(const seg_t *seg)
676
float_g sx = seg->side ? seg->linedef->end->x : seg->linedef->start->x;
677
float_g sy = seg->side ? seg->linedef->end->y : seg->linedef->start->y;
679
return (int) ceil(UtilComputeDist(seg->start->x - sx, seg->start->y - sy));
682
static INLINE_G int TransformAngle(angle_g angle)
686
result = (int)(angle * 65536.0 / 360.0);
691
return (result & 0xFFFF);
694
static int SegCompare(const void *p1, const void *p2)
696
const seg_t *A = ((const seg_t **) p1)[0];
697
const seg_t *B = ((const seg_t **) p2)[0];
700
InternalError("Seg %p never reached a subsector !", A);
703
InternalError("Seg %p never reached a subsector !", B);
705
return (A->index - B->index);
709
/* ----- writing routines ------------------------------ */
711
static const uint8_g *lev_v2_magic = (uint8_g *) "gNd2";
712
static const uint8_g *lev_v3_magic = (uint8_g *) "gNd3";
713
static const uint8_g *lev_v5_magic = (uint8_g *) "gNd5";
715
void PutVertices(char *name, int do_gl)
723
lump = CreateGLLump(name);
725
lump = CreateLevelLump(name);
727
for (i=0, count=0; i < num_vertices; i++)
730
vertex_t *vert = lev_vertices[i];
732
if ((do_gl ? 1 : 0) != ((vert->index & IS_GL_VERTEX) ? 1 : 0))
737
raw.x = SINT16(I_ROUND(vert->x));
738
raw.y = SINT16(I_ROUND(vert->y));
740
AppendLevelLump(lump, &raw, sizeof(raw));
745
if (count != (do_gl ? num_gl_vert : num_normal_vert))
746
InternalError("PutVertices miscounted (%d != %d)", count,
747
do_gl ? num_gl_vert : num_normal_vert);
749
if (lev_doing_normal && ! do_gl && count > 65534)
750
MarkHardFailure(LIMIT_VERTEXES);
751
else if (count > 32767)
752
MarkSoftFailure(do_gl ? LIMIT_GL_VERT : LIMIT_VERTEXES);
755
void PutV2Vertices(int do_v5)
762
lump = CreateGLLump("GL_VERT");
765
AppendLevelLump(lump, lev_v5_magic, 4);
767
AppendLevelLump(lump, lev_v2_magic, 4);
769
for (i=0, count=0; i < num_vertices; i++)
772
vertex_t *vert = lev_vertices[i];
774
if (! (vert->index & IS_GL_VERTEX))
777
raw.x = SINT32((int)(vert->x * 65536.0));
778
raw.y = SINT32((int)(vert->y * 65536.0));
780
AppendLevelLump(lump, &raw, sizeof(raw));
785
if (count != num_gl_vert)
786
InternalError("PutV2Vertices miscounted (%d != %d)", count,
790
MarkSoftFailure(LIMIT_GL_VERT);
793
void PutSectors(void)
796
lump_t *lump = CreateLevelLump("SECTORS");
800
for (i=0; i < num_sectors; i++)
803
sector_t *sector = lev_sectors[i];
805
raw.floor_h = SINT16(sector->floor_h);
806
raw.ceil_h = SINT16(sector->ceil_h);
808
memcpy(raw.floor_tex, sector->floor_tex, sizeof(raw.floor_tex));
809
memcpy(raw.ceil_tex, sector->ceil_tex, sizeof(raw.ceil_tex));
811
raw.light = UINT16(sector->light);
812
raw.special = UINT16(sector->special);
813
raw.tag = SINT16(sector->tag);
815
AppendLevelLump(lump, &raw, sizeof(raw));
818
if (num_sectors > 65534)
819
MarkHardFailure(LIMIT_SECTORS);
820
else if (num_sectors > 32767)
821
MarkSoftFailure(LIMIT_SECTORS);
824
void PutSidedefs(void)
827
lump_t *lump = CreateLevelLump("SIDEDEFS");
831
for (i=0; i < num_sidedefs; i++)
834
sidedef_t *side = lev_sidedefs[i];
836
raw.sector = (side->sector == NULL) ? SINT16(-1) :
837
UINT16(side->sector->index);
839
raw.x_offset = SINT16(side->x_offset);
840
raw.y_offset = SINT16(side->y_offset);
842
memcpy(raw.upper_tex, side->upper_tex, sizeof(raw.upper_tex));
843
memcpy(raw.lower_tex, side->lower_tex, sizeof(raw.lower_tex));
844
memcpy(raw.mid_tex, side->mid_tex, sizeof(raw.mid_tex));
846
AppendLevelLump(lump, &raw, sizeof(raw));
849
if (num_sidedefs > 65534)
850
MarkHardFailure(LIMIT_SIDEDEFS);
851
else if (num_sidedefs > 32767)
852
MarkSoftFailure(LIMIT_SIDEDEFS);
855
void PutLinedefs(void)
858
lump_t *lump = CreateLevelLump("LINEDEFS");
862
for (i=0; i < num_linedefs; i++)
865
linedef_t *line = lev_linedefs[i];
867
raw.start = UINT16(line->start->index);
868
raw.end = UINT16(line->end->index);
870
raw.flags = UINT16(line->flags);
871
raw.type = UINT16(line->type);
872
raw.tag = SINT16(line->tag);
874
raw.sidedef1 = line->right ? UINT16(line->right->index) : 0xFFFF;
875
raw.sidedef2 = line->left ? UINT16(line->left->index) : 0xFFFF;
877
AppendLevelLump(lump, &raw, sizeof(raw));
880
if (num_linedefs > 65534)
881
MarkHardFailure(LIMIT_LINEDEFS);
882
else if (num_linedefs > 32767)
883
MarkSoftFailure(LIMIT_LINEDEFS);
886
void PutLinedefsHexen(void)
889
lump_t *lump = CreateLevelLump("LINEDEFS");
893
for (i=0; i < num_linedefs; i++)
895
raw_hexen_linedef_t raw;
896
linedef_t *line = lev_linedefs[i];
898
raw.start = UINT16(line->start->index);
899
raw.end = UINT16(line->end->index);
901
raw.flags = UINT16(line->flags);
902
raw.type = UINT8(line->type);
905
for (j=0; j < 5; j++)
906
raw.specials[j] = UINT8(line->specials[j]);
908
raw.sidedef1 = line->right ? UINT16(line->right->index) : 0xFFFF;
909
raw.sidedef2 = line->left ? UINT16(line->left->index) : 0xFFFF;
911
AppendLevelLump(lump, &raw, sizeof(raw));
914
if (num_linedefs > 65534)
915
MarkHardFailure(LIMIT_LINEDEFS);
916
else if (num_linedefs > 32767)
917
MarkSoftFailure(LIMIT_LINEDEFS);
920
static INLINE_G uint16_g VertexIndex16Bit(const vertex_t *v)
922
if (v->index & IS_GL_VERTEX)
923
return (uint16_g) ((v->index & ~IS_GL_VERTEX) | 0x8000U);
925
return (uint16_g) v->index;
928
static INLINE_G uint32_g VertexIndex32BitV5(const vertex_t *v)
930
if (v->index & IS_GL_VERTEX)
931
return (uint32_g) ((v->index & ~IS_GL_VERTEX) | 0x80000000U);
933
return (uint32_g) v->index;
939
lump_t *lump = CreateLevelLump("SEGS");
943
// sort segs into ascending index
944
qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
946
for (i=0, count=0; i < num_segs; i++)
949
seg_t *seg = segs[i];
951
// ignore minisegs and degenerate segs
952
if (! seg->linedef || seg->degenerate)
955
raw.start = UINT16(VertexIndex16Bit(seg->start));
956
raw.end = UINT16(VertexIndex16Bit(seg->end));
957
raw.angle = UINT16(TransformAngle(seg->p_angle));
958
raw.linedef = UINT16(seg->linedef->index);
959
raw.flip = UINT16(seg->side);
960
raw.dist = UINT16(TransformSegDist(seg));
962
AppendLevelLump(lump, &raw, sizeof(raw));
967
PrintDebug("PUT SEG: %04X Vert %04X->%04X Line %04X %s "
968
"Angle %04X (%1.1f,%1.1f) -> (%1.1f,%1.1f)\n", seg->index,
969
UINT16(raw.start), UINT16(raw.end), UINT16(raw.linedef),
970
seg->side ? "L" : "R", UINT16(raw.angle),
971
seg->start->x, seg->start->y, seg->end->x, seg->end->y);
975
if (count != num_complete_seg)
976
InternalError("PutSegs miscounted (%d != %d)", count,
980
MarkHardFailure(LIMIT_SEGS);
981
else if (count > 32767)
982
MarkSoftFailure(LIMIT_SEGS);
988
lump_t *lump = CreateGLLump("GL_SEGS");
992
// sort segs into ascending index
993
qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
995
for (i=0, count=0; i < num_segs; i++)
998
seg_t *seg = segs[i];
1000
// ignore degenerate segs
1001
if (seg->degenerate)
1004
raw.start = UINT16(VertexIndex16Bit(seg->start));
1005
raw.end = UINT16(VertexIndex16Bit(seg->end));
1006
raw.side = UINT16(seg->side);
1009
raw.linedef = UINT16(seg->linedef->index);
1011
raw.linedef = UINT16(0xFFFF);
1014
raw.partner = UINT16(seg->partner->index);
1016
raw.partner = UINT16(0xFFFF);
1018
AppendLevelLump(lump, &raw, sizeof(raw));
1023
PrintDebug("PUT GL SEG: %04X Line %04X %s Partner %04X "
1024
"(%1.1f,%1.1f) -> (%1.1f,%1.1f)\n", seg->index, UINT16(raw.linedef),
1025
seg->side ? "L" : "R", UINT16(raw.partner),
1026
seg->start->x, seg->start->y, seg->end->x, seg->end->y);
1030
if (count != num_complete_seg)
1031
InternalError("PutGLSegs miscounted (%d != %d)", count,
1035
InternalError("PutGLSegs with %d (> 65534) segs", count);
1036
else if (count > 32767)
1037
MarkSoftFailure(LIMIT_GL_SEGS);
1040
void PutV3Segs(int do_v5)
1043
lump_t *lump = CreateGLLump("GL_SEGS");
1046
AppendLevelLump(lump, lev_v3_magic, 4);
1050
// sort segs into ascending index
1051
qsort(segs, num_segs, sizeof(seg_t *), SegCompare);
1053
for (i=0, count=0; i < num_segs; i++)
1056
seg_t *seg = segs[i];
1058
// ignore degenerate segs
1059
if (seg->degenerate)
1064
raw.start = UINT32(VertexIndex32BitV5(seg->start));
1065
raw.end = UINT32(VertexIndex32BitV5(seg->end));
1069
raw.start = UINT32(seg->start->index);
1070
raw.end = UINT32(seg->end->index);
1073
raw.side = UINT16(seg->side);
1076
raw.linedef = UINT16(seg->linedef->index);
1078
raw.linedef = UINT16(0xFFFF);
1081
raw.partner = UINT32(seg->partner->index);
1083
raw.partner = UINT32(0xFFFFFFFF);
1085
AppendLevelLump(lump, &raw, sizeof(raw));
1090
PrintDebug("PUT V3 SEG: %06X Line %04X %s Partner %06X "
1091
"(%1.1f,%1.1f) -> (%1.1f,%1.1f)\n", seg->index, UINT16(raw.linedef),
1092
seg->side ? "L" : "R", UINT32(raw.partner),
1093
seg->start->x, seg->start->y, seg->end->x, seg->end->y);
1097
if (count != num_complete_seg)
1098
InternalError("PutGLSegs miscounted (%d != %d)", count,
1102
void PutSubsecs(char *name, int do_gl)
1110
lump = CreateGLLump(name);
1112
lump = CreateLevelLump(name);
1114
for (i=0; i < num_subsecs; i++)
1117
subsec_t *sub = subsecs[i];
1119
raw.first = UINT16(sub->seg_list->index);
1120
raw.num = UINT16(sub->seg_count);
1122
AppendLevelLump(lump, &raw, sizeof(raw));
1125
PrintDebug("PUT SUBSEC %04X First %04X Num %04X\n",
1126
sub->index, UINT16(raw.first), UINT16(raw.num));
1130
if (num_subsecs > 32767)
1131
MarkHardFailure(do_gl ? LIMIT_GL_SSECT : LIMIT_SSECTORS);
1134
void PutV3Subsecs(int do_v5)
1141
lump = CreateGLLump("GL_SSECT");
1144
AppendLevelLump(lump, lev_v3_magic, 4);
1146
for (i=0; i < num_subsecs; i++)
1148
raw_v3_subsec_t raw;
1149
subsec_t *sub = subsecs[i];
1151
raw.first = UINT32(sub->seg_list->index);
1152
raw.num = UINT32(sub->seg_count);
1154
AppendLevelLump(lump, &raw, sizeof(raw));
1157
PrintDebug("PUT V3 SUBSEC %06X First %06X Num %06X\n",
1158
sub->index, UINT32(raw.first), UINT32(raw.num));
1162
if (!do_v5 && num_subsecs > 32767)
1163
MarkHardFailure(LIMIT_GL_SSECT);
1166
static int node_cur_index;
1168
static void PutOneNode(node_t *node, lump_t *lump)
1173
PutOneNode(node->r.node, lump);
1176
PutOneNode(node->l.node, lump);
1178
node->index = node_cur_index++;
1180
raw.x = SINT16(node->x);
1181
raw.y = SINT16(node->y);
1182
raw.dx = SINT16(node->dx / (node->too_long ? 2 : 1));
1183
raw.dy = SINT16(node->dy / (node->too_long ? 2 : 1));
1185
raw.b1.minx = SINT16(node->r.bounds.minx);
1186
raw.b1.miny = SINT16(node->r.bounds.miny);
1187
raw.b1.maxx = SINT16(node->r.bounds.maxx);
1188
raw.b1.maxy = SINT16(node->r.bounds.maxy);
1190
raw.b2.minx = SINT16(node->l.bounds.minx);
1191
raw.b2.miny = SINT16(node->l.bounds.miny);
1192
raw.b2.maxx = SINT16(node->l.bounds.maxx);
1193
raw.b2.maxy = SINT16(node->l.bounds.maxy);
1196
raw.right = UINT16(node->r.node->index);
1197
else if (node->r.subsec)
1198
raw.right = UINT16(node->r.subsec->index | 0x8000);
1200
InternalError("Bad right child in node %d", node->index);
1203
raw.left = UINT16(node->l.node->index);
1204
else if (node->l.subsec)
1205
raw.left = UINT16(node->l.subsec->index | 0x8000);
1207
InternalError("Bad left child in node %d", node->index);
1209
AppendLevelLump(lump, &raw, sizeof(raw));
1212
PrintDebug("PUT NODE %04X Left %04X Right %04X "
1213
"(%d,%d) -> (%d,%d)\n", node->index, UINT16(raw.left),
1214
UINT16(raw.right), node->x, node->y,
1215
node->x + node->dx, node->y + node->dy);
1219
static void PutOneV5Node(node_t *node, lump_t *lump)
1224
PutOneV5Node(node->r.node, lump);
1227
PutOneV5Node(node->l.node, lump);
1229
node->index = node_cur_index++;
1231
raw.x = SINT16(node->x);
1232
raw.y = SINT16(node->y);
1233
raw.dx = SINT16(node->dx / (node->too_long ? 2 : 1));
1234
raw.dy = SINT16(node->dy / (node->too_long ? 2 : 1));
1236
raw.b1.minx = SINT16(node->r.bounds.minx);
1237
raw.b1.miny = SINT16(node->r.bounds.miny);
1238
raw.b1.maxx = SINT16(node->r.bounds.maxx);
1239
raw.b1.maxy = SINT16(node->r.bounds.maxy);
1241
raw.b2.minx = SINT16(node->l.bounds.minx);
1242
raw.b2.miny = SINT16(node->l.bounds.miny);
1243
raw.b2.maxx = SINT16(node->l.bounds.maxx);
1244
raw.b2.maxy = SINT16(node->l.bounds.maxy);
1247
raw.right = UINT32(node->r.node->index);
1248
else if (node->r.subsec)
1249
raw.right = UINT32(node->r.subsec->index | 0x80000000U);
1251
InternalError("Bad right child in V5 node %d", node->index);
1254
raw.left = UINT32(node->l.node->index);
1255
else if (node->l.subsec)
1256
raw.left = UINT32(node->l.subsec->index | 0x80000000U);
1258
InternalError("Bad left child in V5 node %d", node->index);
1260
AppendLevelLump(lump, &raw, sizeof(raw));
1263
PrintDebug("PUT V5 NODE %08X Left %08X Right %08X "
1264
"(%d,%d) -> (%d,%d)\n", node->index, UINT32(raw.left),
1265
UINT32(raw.right), node->x, node->y,
1266
node->x + node->dx, node->y + node->dy);
1270
void PutNodes(char *name, int do_gl, int do_v5, node_t *root)
1277
lump = CreateGLLump(name);
1279
lump = CreateLevelLump(name);
1286
PutOneV5Node(root, lump);
1288
PutOneNode(root, lump);
1291
if (node_cur_index != num_nodes)
1292
InternalError("PutNodes miscounted (%d != %d)",
1293
node_cur_index, num_nodes);
1295
if (!do_v5 && node_cur_index > 32767)
1296
MarkHardFailure(LIMIT_NODES);
1300
/* ----- ZDBSP format writing --------------------------- */
1302
static const uint8_g *lev_ZD_magic = (uint8_g *) "ZNOD";
1304
void PutZVertices(void)
1308
uint32_g orgverts = UINT32(num_normal_vert);
1309
uint32_g newverts = UINT32(num_gl_vert);
1311
ZLibAppendLump(&orgverts, 4);
1312
ZLibAppendLump(&newverts, 4);
1316
for (i=0, count=0; i < num_vertices; i++)
1318
raw_v2_vertex_t raw;
1319
vertex_t *vert = lev_vertices[i];
1321
if (! (vert->index & IS_GL_VERTEX))
1324
raw.x = SINT32((int)(vert->x * 65536.0));
1325
raw.y = SINT32((int)(vert->y * 65536.0));
1327
ZLibAppendLump(&raw, sizeof(raw));
1332
if (count != num_gl_vert)
1333
InternalError("PutZVertices miscounted (%d != %d)",
1334
count, num_gl_vert);
1337
void PutZSubsecs(void)
1341
uint32_g raw_num = UINT32(num_subsecs);
1343
int cur_seg_index = 0;
1345
ZLibAppendLump(&raw_num, 4);
1348
for (i=0; i < num_subsecs; i++)
1350
subsec_t *sub = subsecs[i];
1353
raw_num = UINT32(sub->seg_count);
1355
ZLibAppendLump(&raw_num, 4);
1357
// sanity check the seg index values
1359
for (seg = sub->seg_list; seg; seg = seg->next, cur_seg_index++)
1361
// ignore minisegs and degenerate segs
1362
if (! seg->linedef || seg->degenerate)
1365
if (cur_seg_index != seg->index)
1366
InternalError("PutZSubsecs: seg index mismatch in sub %d (%d != %d)\n",
1367
i, cur_seg_index, seg->index);
1372
if (count != sub->seg_count)
1373
InternalError("PutZSubsecs: miscounted segs in sub %d (%d != %d)\n",
1374
i, count, sub->seg_count);
1377
if (cur_seg_index != num_complete_seg)
1378
InternalError("PutZSubsecs miscounted segs (%d != %d)",
1379
cur_seg_index, num_complete_seg);
1385
uint32_g raw_num = UINT32(num_complete_seg);
1387
ZLibAppendLump(&raw_num, 4);
1390
for (i=0, count=0; i < num_segs; i++)
1392
seg_t *seg = segs[i];
1394
// ignore minisegs and degenerate segs
1395
if (! seg->linedef || seg->degenerate)
1398
if (count != seg->index)
1399
InternalError("PutZSegs: seg index mismatch (%d != %d)\n",
1403
uint32_g v1 = UINT32(VertexIndex32BitV5(seg->start));
1404
uint32_g v2 = UINT32(VertexIndex32BitV5(seg->end));
1406
uint16_g line = UINT16(seg->linedef->index);
1407
uint8_g side = seg->side;
1409
ZLibAppendLump(&v1, 4);
1410
ZLibAppendLump(&v2, 4);
1411
ZLibAppendLump(&line, 2);
1412
ZLibAppendLump(&side, 1);
1418
if (count != num_complete_seg)
1419
InternalError("PutZSegs miscounted (%d != %d)",
1420
count, num_complete_seg);
1423
static void PutOneZNode(node_t *node)
1428
PutOneZNode(node->r.node);
1431
PutOneZNode(node->l.node);
1433
node->index = node_cur_index++;
1435
raw.x = SINT16(node->x);
1436
raw.y = SINT16(node->y);
1437
raw.dx = SINT16(node->dx / (node->too_long ? 2 : 1));
1438
raw.dy = SINT16(node->dy / (node->too_long ? 2 : 1));
1440
ZLibAppendLump(&raw.x, 2);
1441
ZLibAppendLump(&raw.y, 2);
1442
ZLibAppendLump(&raw.dx, 2);
1443
ZLibAppendLump(&raw.dy, 2);
1445
raw.b1.minx = SINT16(node->r.bounds.minx);
1446
raw.b1.miny = SINT16(node->r.bounds.miny);
1447
raw.b1.maxx = SINT16(node->r.bounds.maxx);
1448
raw.b1.maxy = SINT16(node->r.bounds.maxy);
1450
raw.b2.minx = SINT16(node->l.bounds.minx);
1451
raw.b2.miny = SINT16(node->l.bounds.miny);
1452
raw.b2.maxx = SINT16(node->l.bounds.maxx);
1453
raw.b2.maxy = SINT16(node->l.bounds.maxy);
1455
ZLibAppendLump(&raw.b1, sizeof(raw.b1));
1456
ZLibAppendLump(&raw.b2, sizeof(raw.b2));
1459
raw.right = UINT32(node->r.node->index);
1460
else if (node->r.subsec)
1461
raw.right = UINT32(node->r.subsec->index | 0x80000000U);
1463
InternalError("Bad right child in V5 node %d", node->index);
1466
raw.left = UINT32(node->l.node->index);
1467
else if (node->l.subsec)
1468
raw.left = UINT32(node->l.subsec->index | 0x80000000U);
1470
InternalError("Bad left child in V5 node %d", node->index);
1472
ZLibAppendLump(&raw.right, 4);
1473
ZLibAppendLump(&raw.left, 4);
1476
PrintDebug("PUT Z NODE %08X Left %08X Right %08X "
1477
"(%d,%d) -> (%d,%d)\n", node->index, UINT32(raw.left),
1478
UINT32(raw.right), node->x, node->y,
1479
node->x + node->dx, node->y + node->dy);
1483
void PutZNodes(node_t *root)
1485
uint32_g raw_num = UINT32(num_nodes);
1487
ZLibAppendLump(&raw_num, 4);
1495
if (node_cur_index != num_nodes)
1496
InternalError("PutZNodes miscounted (%d != %d)",
1497
node_cur_index, num_nodes);
1500
void SaveZDFormat(node_t *root_node)
1504
// leave SEGS and SSECTORS empty
1505
CreateLevelLump("SEGS");
1506
CreateLevelLump("SSECTORS");
1508
lump = CreateLevelLump("NODES");
1510
AppendLevelLump(lump, lev_ZD_magic, 4);
1512
ZLibBeginLump(lump);
1517
PutZNodes(root_node);
1523
/* ----- whole-level routines --------------------------- */
1528
void LoadLevel(void)
1532
const char *level_name = GetLevelName();
1534
boolean_g normal_exists = CheckForNormalNodes();
1536
lev_doing_normal = !cur_info->gwa_mode && (cur_info->force_normal ||
1537
(!cur_info->no_normal && !normal_exists));
1539
// -JL- Identify Hexen mode by presence of BEHAVIOR lump
1540
lev_doing_hexen = (FindLevelLump("BEHAVIOR") != NULL);
1542
if (lev_doing_normal)
1543
message = UtilFormat("Building normal and GL nodes on %s%s",
1544
level_name, lev_doing_hexen ? " (Hexen)" : "");
1546
message = UtilFormat("Building GL nodes on %s%s",
1547
level_name, lev_doing_hexen ? " (Hexen)" : "");
1549
lev_doing_hexen |= cur_info->force_hexen;
1551
DisplaySetBarText(1, message);
1553
PrintVerbose("\n\n");
1554
PrintMsg("%s\n", message);
1563
if (lev_doing_hexen)
1574
PrintVerbose("Loaded %d vertices, %d sectors, %d sides, %d lines, %d things\n",
1575
num_vertices, num_sectors, num_sidedefs, num_linedefs, num_things);
1577
if (cur_info->fast && !lev_doing_normal &&
1578
normal_exists && num_sectors > 5 && num_linedefs > 100)
1580
PrintVerbose("Using original nodes to speed things up\n");
1584
if (lev_doing_normal)
1586
// NOTE: order here is critical
1588
if (cur_info->pack_sides)
1589
DetectDuplicateSidedefs();
1591
if (cur_info->merge_vert)
1592
DetectDuplicateVertices();
1594
if (!cur_info->no_prune)
1597
// always prune vertices (ignore -noprune), otherwise all the
1598
// unused vertices from seg splits would keep accumulating.
1601
if (!cur_info->no_prune)
1604
if (cur_info->prune_sect)
1608
CalculateWallTips();
1610
if (lev_doing_hexen)
1612
// -JL- Find sectors containing polyobjs
1613
DetectPolyobjSectors();
1616
DetectOverlappingLines();
1618
if (cur_info->window_fx)
1619
DetectWindowEffects();
1625
void FreeLevel(void)
1642
void PutGLOptions(void)
1644
char option_buf[128];
1646
sprintf(option_buf, "-v%d -factor %d", cur_info->spec_version, cur_info->factor);
1648
if (cur_info->fast ) strcat(option_buf, " -f");
1649
if (cur_info->force_normal ) strcat(option_buf, " -n");
1650
if (cur_info->merge_vert ) strcat(option_buf, " -m");
1651
if (cur_info->pack_sides ) strcat(option_buf, " -p");
1652
if (cur_info->prune_sect ) strcat(option_buf, " -u");
1653
if (cur_info->skip_self_ref) strcat(option_buf, " -s");
1654
if (cur_info->window_fx ) strcat(option_buf, " -y");
1656
if (cur_info->no_normal) strcat(option_buf, " -xn");
1657
if (cur_info->no_reject) strcat(option_buf, " -xr");
1658
if (cur_info->no_prune ) strcat(option_buf, " -xu");
1660
AddGLTextLine("OPTIONS", option_buf);
1666
void PutGLChecksum(void)
1672
Adler32_Begin(&crc);
1674
lump = FindLevelLump("VERTEXES");
1676
if (lump && lump->length > 0)
1677
Adler32_AddBlock(&crc, lump->data, lump->length);
1679
lump = FindLevelLump("LINEDEFS");
1681
if (lump && lump->length > 0)
1682
Adler32_AddBlock(&crc, lump->data, lump->length);
1684
Adler32_Finish(&crc);
1686
sprintf(num_buf, "0x%08x", crc);
1688
AddGLTextLine("CHECKSUM", num_buf);
1694
void SaveLevel(node_t *root_node)
1696
lev_force_v3 = (cur_info->spec_version == 3) ? TRUE : FALSE;
1697
lev_force_v5 = (cur_info->spec_version == 5) ? TRUE : FALSE;
1699
// Note: RoundOffBspTree will convert the GL vertices in segs to
1700
// their normal counterparts (pointer change: use normal_dup).
1702
if (cur_info->spec_version == 1)
1703
RoundOffBspTree(root_node);
1707
if (num_normal_vert > 32767 || num_gl_vert > 32767)
1709
if (cur_info->spec_version < 3)
1711
lev_force_v5 = TRUE;
1712
MarkV5Switch(LIMIT_VERTEXES | LIMIT_GL_SEGS);
1716
if (num_segs > 65534)
1718
if (cur_info->spec_version < 3)
1720
lev_force_v5 = TRUE;
1721
MarkV5Switch(LIMIT_GL_SSECT | LIMIT_GL_SEGS);
1725
if (num_nodes > 32767)
1727
if (cur_info->spec_version < 5)
1729
lev_force_v5 = TRUE;
1730
MarkV5Switch(LIMIT_GL_NODES);
1734
if (cur_info->spec_version == 1)
1735
PutVertices("GL_VERT", TRUE);
1737
PutV2Vertices(lev_force_v5);
1739
if (lev_force_v3 || lev_force_v5)
1740
PutV3Segs(lev_force_v5);
1744
if (lev_force_v3 || lev_force_v5)
1745
PutV3Subsecs(lev_force_v5);
1747
PutSubsecs("GL_SSECT", TRUE);
1749
PutNodes("GL_NODES", TRUE, lev_force_v5, root_node);
1751
// -JL- Add empty PVS lump
1752
CreateGLLump("GL_PVS");
1755
if (lev_doing_normal)
1757
if (cur_info->spec_version != 1)
1758
RoundOffBspTree(root_node);
1760
NormaliseBspTree(root_node);
1762
PutVertices("VERTEXES", FALSE);
1766
if (lev_doing_hexen)
1773
// don't report a problem when -v5 was explicitly given
1774
if (cur_info->spec_version < 5)
1777
SaveZDFormat(root_node);
1782
PutSubsecs("SSECTORS", FALSE);
1783
PutNodes("NODES", FALSE, FALSE, root_node);
1786
// -JL- Don't touch blockmap and reject if not doing normal nodes
1789
if (!cur_info->no_reject || !FindLevelLump("REJECT"))
1793
// keyword support (v5.0 of the specs)
1794
AddGLTextLine("BUILDER", "glBSP " GLBSP_VER);
1797
char *time_str = UtilTimeString();
1801
AddGLTextLine("TIME", time_str);
1806
// this must be done _after_ the normal nodes have been built,
1807
// so that we use the new VERTEXES lump in the checksum.