-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
bspbuilder.cpp
654 lines (534 loc) · 20.4 KB
/
bspbuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/**
* @file bspbuilder.cpp
* BSP Builder. @ingroup map
*
* Based on glBSP 2.24 (in turn, based on BSP 2.3), which is hosted on
* SourceForge: http://sourceforge.net/projects/glbsp/
*
* @authors Copyright © 2007-2012 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006-2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
* @authors Copyright © 2000-2007 Andrew Apted <ajapted@gmail.com>
* @authors Copyright © 1998-2000 Colin Reed <cph@moria.org.uk>
* @authors Copyright © 1998-2000 Lee Killough <killough@rsn.hp.com>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <math.h>
#include "de_base.h"
#include "de_bsp.h"
#include "de_play.h"
#include "de_misc.h"
#include "de_console.h"
#include "edit_map.h"
#include "p_mapdata.h"
#include <de/Log>
#include "bspbuilder/intersection.hh"
#include "bspbuilder/bspbuilder.hh"
#include "bspbuilder/superblockmap.hh"
using namespace de;
BspBuilder::BspBuilder(GameMap* _map) :
splitCostFactor(BSPBUILDER_PARTITION_COST_HEDGESPLIT),
map(_map), lineDefInfos(0), rootNode(0), builtOK(false)
{}
static int C_DECL clearBspHEdgeInfo(BinaryTree* tree, void* /*parameters*/)
{
if(BinaryTree_IsLeaf(tree))
{
BspLeaf* leaf = static_cast<BspLeaf*>(BinaryTree_UserData(tree));
HEdge* hedge = leaf->hedge;
do
{
Z_Free(HEdge_DetachBspBuildInfo(hedge));
} while((hedge = hedge->next) != leaf->hedge);
}
return 0; // Continue iteration.
}
BspBuilder::~BspBuilder()
{
// We are finished with the BSP build data.
if(rootNode)
{
// We're done with the build info.
BinaryTree_PreOrder(rootNode, clearBspHEdgeInfo, NULL/*no parameters*/);
BinaryTree_Delete(rootNode);
}
rootNode = NULL;
}
static void initAABoxFromEditableLineDefVertexes(AABoxf* aaBox, const LineDef* line)
{
const double* from = line->L_v1->buildData.pos;
const double* to = line->L_v2->buildData.pos;
aaBox->minX = MIN_OF(from[VX], to[VX]);
aaBox->minY = MIN_OF(from[VY], to[VY]);
aaBox->maxX = MAX_OF(from[VX], to[VX]);
aaBox->maxY = MAX_OF(from[VY], to[VY]);
}
void BspBuilder::findMapBounds(AABoxf* aaBox) const
{
Q_ASSERT(aaBox);
AABoxf bounds;
boolean initialized = false;
for(uint i = 0; i < GameMap_LineDefCount(map); ++i)
{
LineDef* line = GameMap_LineDef(map, i);
// Do not consider zero-length LineDefs.
if(lineDefInfo(*line).flags.testFlag(LineDefInfo::ZEROLENGTH)) continue;
AABoxf lineAABox;
initAABoxFromEditableLineDefVertexes(&lineAABox, line);
if(initialized)
{
V2f_AddToBox(bounds.arvec2, lineAABox.min);
}
else
{
V2f_InitBox(bounds.arvec2, lineAABox.min);
initialized = true;
}
V2f_AddToBox(bounds.arvec2, lineAABox.max);
}
if(initialized)
{
V2f_CopyBox(aaBox->arvec2, bounds.arvec2);
return;
}
// Clear.
V2f_Set(aaBox->min, DDMAXFLOAT, DDMAXFLOAT);
V2f_Set(aaBox->max, DDMINFLOAT, DDMINFLOAT);
}
void BspBuilder::createInitialHEdges(SuperBlock& hedgeList)
{
Q_ASSERT(map);
for(uint i = 0; i < map->numLineDefs; ++i)
{
LineDef* line = GameMap_LineDef(map, i);
HEdge* front = NULL;
HEdge* back = NULL;
// Polyobj lines are completely ignored.
if(line->inFlags & LF_POLYOBJ) continue;
// Ignore zero-length and polyobj lines.
if(!lineDefInfo(*line).flags.testFlag(LineDefInfo::ZEROLENGTH)
/*&& !lineDefInfo(*line).overlap*/)
{
// Check for Humungously long lines.
if(ABS(line->v[0]->buildData.pos[VX] - line->v[1]->buildData.pos[VX]) >= 10000 ||
ABS(line->v[0]->buildData.pos[VY] - line->v[1]->buildData.pos[VY]) >= 10000)
{
if(3000 >=
M_Length(line->v[0]->buildData.pos[VX] - line->v[1]->buildData.pos[VX],
line->v[0]->buildData.pos[VY] - line->v[1]->buildData.pos[VY]))
{
LOG_WARNING("LineDef #%d is very long, it may cause problems.") << line->buildData.index;
}
}
if(line->sideDefs[FRONT])
{
SideDef* side = line->sideDefs[FRONT];
if(!side->sector)
LOG_INFO("Bad SideDef on LineDef #%d.") << line->buildData.index;
front = newHEdge(line, line, line->v[0], line->v[1], side->sector, false);
hedgeList.hedgePush(front);
}
else
{
LOG_INFO("LineDef #%d has no front SideDef!") << line->buildData.index;
}
if(line->sideDefs[BACK])
{
SideDef* side = line->sideDefs[BACK];
if(!side->sector)
LOG_INFO("Bad SideDef on LineDef #%d.") << line->buildData.index;
back = newHEdge(line, line, line->v[1], line->v[0], side->sector, true);
hedgeList.hedgePush(back);
if(front)
{
// Half-edges always maintain a one-to-one relationship with their
// twins, so if one gets split, the other must be split also.
back->twin = front;
front->twin = back;
}
}
else
{
if(lineDefInfo(*line).flags.testFlag(LineDefInfo::TWOSIDED))
{
LOG_INFO("LineDef #%d is two-sided but has no back SideDef.") << line->buildData.index;
lineDefInfo(*line).flags &= ~LineDefInfo::TWOSIDED;
}
// Handle the 'One-Sided Window' trick.
if(line->buildData.windowEffect && front)
{
HEdge* other = newHEdge(front->bspBuildInfo->lineDef, line,
line->v[1], line->v[0], line->buildData.windowEffect, true);
hedgeList.hedgePush(other);
// Setup the twin-ing (it's very strange to have a mini
// and a normal partnered together).
other->twin = front;
front->twin = other;
}
}
}
// @todo edge tips should be created when half-edges are created.
double x1 = line->v[0]->buildData.pos[VX];
double y1 = line->v[0]->buildData.pos[VY];
double x2 = line->v[1]->buildData.pos[VX];
double y2 = line->v[1]->buildData.pos[VY];
addEdgeTip(line->v[0], x2 - x1, y2 - y1, back, front);
addEdgeTip(line->v[1], x1 - x2, y1 - y2, front, back);
}
}
void BspBuilder::initForMap()
{
uint numLineDefs = GameMap_LineDefCount(map);
lineDefInfos.resize(numLineDefs);
for(uint i = 0; i < numLineDefs; ++i)
{
LineDef* l = GameMap_LineDef(map, i);
LineDefInfo& info = lineDefInfo(*l);
const Vertex* start = l->v[0];
const Vertex* end = l->v[1];
// Check for zero-length line.
if((fabs(start->buildData.pos[VX] - end->buildData.pos[VX]) < DIST_EPSILON) &&
(fabs(start->buildData.pos[VY] - end->buildData.pos[VY]) < DIST_EPSILON))
info.flags |= LineDefInfo::ZEROLENGTH;
if(l->sideDefs[BACK] && l->sideDefs[FRONT])
{
info.flags |= LineDefInfo::TWOSIDED;
if(l->sideDefs[BACK]->sector == l->sideDefs[FRONT]->sector)
info.flags |= LineDefInfo::SELFREF;
}
}
}
static int C_DECL linkTreeNode(BinaryTree* tree, void* /*parameters*/)
{
// We are only interested in BspNodes at this level.
if(BinaryTree_IsLeaf(tree)) return false; // Continue iteration.
BspNode* node = static_cast<BspNode*>(BinaryTree_UserData(tree));
if(BinaryTree* right = BinaryTree_Right(tree))
{
runtime_mapdata_header_t* child = reinterpret_cast<runtime_mapdata_header_t*>(BinaryTree_UserData(right));
BspNode_SetRight(node, child);
}
if(BinaryTree* left = BinaryTree_Left(tree))
{
runtime_mapdata_header_t* child = reinterpret_cast<runtime_mapdata_header_t*>(BinaryTree_UserData(left));
BspNode_SetLeft(node, child);
}
return false; // Continue iteration.
}
void BspBuilder::initHEdgesAndBuildBsp(SuperBlockmap& blockmap, HPlane& hplane)
{
Q_ASSERT(map);
createInitialHEdges(blockmap.root());
// Build the tree.
rootNode = NULL;
builtOK = buildNodes(blockmap.root(), &rootNode, hplane);
if(builtOK)
{
// Wind leafs.
windLeafs(rootNode);
// Link up the BSP object tree.
BinaryTree_PostOrder(rootNode, linkTreeNode, NULL/*no parameters*/);
}
}
boolean BspBuilder::build()
{
if(!map) return false;
initForMap();
// Find maximal vertexes.
AABoxf mapBounds;
findMapBounds(&mapBounds);
LOG_VERBOSE("Map bounds:")
<< " min[x:" << mapBounds.minX << ", y:" << mapBounds.minY << "]"
<< " max[x:" << mapBounds.maxX << ", y:" << mapBounds.maxY << "]";
AABox mapBoundsi;
mapBoundsi.minX = (int) floor(mapBounds.minX);
mapBoundsi.minY = (int) floor(mapBounds.minY);
mapBoundsi.maxX = (int) ceil(mapBounds.maxX);
mapBoundsi.maxY = (int) ceil(mapBounds.maxY);
AABox blockBounds;
blockBounds.minX = mapBoundsi.minX - (mapBoundsi.minX & 0x7);
blockBounds.minY = mapBoundsi.minY - (mapBoundsi.minY & 0x7);
int bw = ((mapBoundsi.maxX - blockBounds.minX) / 128) + 1;
int bh = ((mapBoundsi.maxY - blockBounds.minY) / 128) + 1;
blockBounds.maxX = blockBounds.minX + 128 * M_CeilPow2(bw);
blockBounds.maxY = blockBounds.minY + 128 * M_CeilPow2(bh);
SuperBlockmap* blockmap = new SuperBlockmap(blockBounds);
HPlane* hplane = new HPlane(this);
initHEdgesAndBuildBsp(*blockmap, *hplane);
delete hplane;
delete blockmap;
return builtOK;
}
BinaryTree* BspBuilder::root() const
{
return rootNode;
}
const HPlaneIntercept* BspBuilder::hplaneInterceptByVertex(HPlane& hplane, Vertex* vertex)
{
if(!vertex) return NULL; // Hmm...
for(HPlane::Intercepts::const_iterator it = hplane.begin(); it != hplane.end(); ++it)
{
const HPlaneIntercept* inter = &*it;
if(((HEdgeIntercept*)inter->userData)->vertex == vertex) return inter;
}
return NULL;
}
HEdgeIntercept* BspBuilder::hedgeInterceptByVertex(HPlane& hplane, Vertex* vertex)
{
const HPlaneIntercept* hpi = hplaneInterceptByVertex(hplane, vertex);
if(!hpi) return NULL; // Not found.
return (HEdgeIntercept*) hpi->userData;
}
void BspBuilder::addHEdgesBetweenIntercepts(HPlane& hplane,
HEdgeIntercept* start, HEdgeIntercept* end, HEdge** right, HEdge** left)
{
Q_ASSERT(start && end);
// Create the half-edge pair.
// Leave 'linedef' field as NULL as these are not linedef-linked.
// Leave 'side' as zero too.
const BspHEdgeInfo& info = hplane.partitionHEdgeInfo();
(*right) = newHEdge(NULL, info.lineDef, start->vertex, end->vertex, start->after, false);
( *left) = newHEdge(NULL, info.lineDef, end->vertex, start->vertex, start->after, false);
// Twin the half-edges together.
(*right)->twin = *left;
( *left)->twin = *right;
/*
DEBUG_Message(("buildHEdgesBetweenIntersections: Capped intersection:\n"));
DEBUG_Message((" %p RIGHT sector %d (%1.1f,%1.1f) -> (%1.1f,%1.1f)\n",
(*right), ((*right)->sector? (*right)->sector->index : -1),
(*right)->v[0]->V_pos[VX], (*right)->v[0]->V_pos[VY],
(*right)->v[1]->V_pos[VX], (*right)->v[1]->V_pos[VY]));
DEBUG_Message((" %p LEFT sector %d (%1.1f,%1.1f) -> (%1.1f,%1.1f)\n",
(*left), ((*left)->sector? (*left)->sector->index : -1),
(*left)->v[0]->V_pos[VX], (*left)->v[0]->V_pos[VY],
(*left)->v[1]->V_pos[VX], (*left)->v[1]->V_pos[VY]));
*/
}
void Bsp_MergeHEdgeIntercepts(HEdgeIntercept* final, const HEdgeIntercept* other)
{
Q_ASSERT(final && other);
/*
LOG_TRACE("Bsp_MergeHEdgeIntercepts: Merging intersections:");
HEdgeIntercept::DebugPrint(*final);
HEdgeIntercept::DebugPrint(*other);
*/
if(final->selfRef && !other->selfRef)
{
if(final->before && other->before)
final->before = other->before;
if(final->after && other->after)
final->after = other->after;
final->selfRef = false;
}
if(!final->before && other->before)
final->before = other->before;
if(!final->after && other->after)
final->after = other->after;
/*
LOG_TRACE("Bsp_MergeHEdgeIntercepts: Result:");
HEdgeIntercept::DebugPrint(*final);
*/
}
void BspBuilder::mergeIntersections(HPlane& hplane)
{
HPlane::Intercepts::iterator node = hplane.begin();
while(node != hplane.end())
{
HPlane::Intercepts::iterator np = node; np++;
if(np == hplane.end()) break;
double len = *np - *node;
if(len < -0.1)
{
Con_Error("BspBuilder_MergeIntersections: Invalid intercept order - %1.3f > %1.3f\n",
node->distance, np->distance);
}
else if(len > 0.2)
{
node++;
continue;
}
HEdgeIntercept* cur = (HEdgeIntercept*)node->userData;
HEdgeIntercept* next = (HEdgeIntercept*)np->userData;
/*if(len > DIST_EPSILON)
{
LOG_DEBUG("Skipping very short half-edge (len: %1.3f) near [%1.1f, %1.1f]")
<< len << cur->vertex->V_pos[VX] << cur->vertex->V_pos[VY];
}*/
// Merge info for the two intersections into one.
Bsp_MergeHEdgeIntercepts(cur, next);
// Destroy the orphaned info.
deleteHEdgeIntercept(next);
// Unlink this intercept.
hplane.deleteIntercept(np);
}
}
void BspBuilder::buildHEdgesAtIntersectionGaps(HPlane& hplane, SuperBlock& rightList,
SuperBlock& leftList)
{
HPlane::Intercepts::const_iterator node = hplane.begin();
while(node != hplane.end())
{
HPlane::Intercepts::const_iterator np = node; np++;
if(np == hplane.end()) break;
HEdgeIntercept* cur = (HEdgeIntercept*)((*node).userData);
HEdgeIntercept* next = (HEdgeIntercept*)((*np).userData);
if(!(!cur->after && !next->before))
{
// Check for some nasty open/closed or close/open cases.
if(cur->after && !next->before)
{
if(!cur->selfRef)
{
double pos[2];
pos[VX] = cur->vertex->buildData.pos[VX] + next->vertex->buildData.pos[VX];
pos[VY] = cur->vertex->buildData.pos[VY] + next->vertex->buildData.pos[VY];
pos[VX] /= 2;
pos[VY] /= 2;
MPE_RegisterUnclosedSectorNear(cur->after, pos[VX], pos[VY]);
}
}
else if(!cur->after && next->before)
{
if(!next->selfRef)
{
double pos[2];
pos[VX] = cur->vertex->buildData.pos[VX] + next->vertex->buildData.pos[VX];
pos[VY] = cur->vertex->buildData.pos[VY] + next->vertex->buildData.pos[VY];
pos[VX] /= 2;
pos[VY] /= 2;
MPE_RegisterUnclosedSectorNear(next->before, pos[VX], pos[VY]);
}
}
else
{
// This is definitetly open space.
HEdge* right, *left;
// Do a sanity check on the sectors (just for good measure).
if(cur->after != next->before)
{
if(!cur->selfRef && !next->selfRef)
{
LOG_DEBUG("Sector mismatch: #%d (%1.1f,%1.1f) != #%d (%1.1f,%1.1f)")
<< cur->after->buildData.index << cur->vertex->buildData.pos[VX]
<< cur->vertex->buildData.pos[VY] << next->before->buildData.index
<< next->vertex->buildData.pos[VX] << next->vertex->buildData.pos[VY];
}
// Choose the non-self-referencing sector when we can.
if(cur->selfRef && !next->selfRef)
{
cur->after = next->before;
}
}
addHEdgesBetweenIntercepts(hplane, cur, next, &right, &left);
// Add the new half-edges to the appropriate lists.
rightList.hedgePush(right);
leftList.hedgePush(left);
}
}
node++;
}
}
void BspBuilder::addMiniHEdges(HPlane& hplane, SuperBlock& rightList, SuperBlock& leftList)
{
/*#if _DEBUG
HPlane_Print(hplane);
#endif*/
// Fix any issues with the current intersections.
mergeIntersections(hplane);
const BspHEdgeInfo& info = hplane.partitionHEdgeInfo();
LOG_TRACE("Building HEdges along partition [%1.1f, %1.1f] > [%1.1f, %1.1f]")
<< info.pSX << info.pSY << info.pDX << info.pDY;
// Find connections in the intersections.
buildHEdgesAtIntersectionGaps(hplane, rightList, leftList);
}
HEdgeIntercept* BspBuilder::newHEdgeIntercept(Vertex* vert, const BspHEdgeInfo* part,
boolean selfRef)
{
HEdgeIntercept* inter = new HEdgeIntercept();
inter->vertex = vert;
inter->selfRef = selfRef;
inter->before = openSectorAtPoint(vert, -part->pDX, -part->pDY);
inter->after = openSectorAtPoint(vert, part->pDX, part->pDY);
return inter;
}
void BspBuilder::deleteHEdgeIntercept(HEdgeIntercept* inter)
{
Q_ASSERT(inter);
delete inter;
}
void BspBuilder::addEdgeTip(Vertex* vert, double dx, double dy, HEdge* back,
HEdge* front)
{
edgetip_t* tip = MPE_NewEdgeTip();
edgetip_t* after;
tip->angle = M_SlopeToAngle(dx, dy);
tip->ET_edge[BACK] = back;
tip->ET_edge[FRONT] = front;
// Find the correct place (order is increasing angle).
for(after = vert->buildData.tipSet; after && after->ET_next;
after = after->ET_next) {}
while(after && tip->angle + ANG_EPSILON < after->angle)
after = after->ET_prev;
// Link it in.
if(after)
tip->ET_next = after->ET_next;
else
tip->ET_next = vert->buildData.tipSet;
tip->ET_prev = after;
if(after)
{
if(after->ET_next)
after->ET_next->ET_prev = tip;
after->ET_next = tip;
}
else
{
if(vert->buildData.tipSet)
vert->buildData.tipSet->ET_prev = tip;
vert->buildData.tipSet = tip;
}
}
Sector* BspBuilder::openSectorAtPoint(Vertex* vert, double dX, double dY)
{
edgetip_t* tip;
double angle = M_SlopeToAngle(dX, dY);
// First check whether there's a wall_tip that lies in the exact direction of
// the given direction (which is relative to the vertex).
for(tip = vert->buildData.tipSet; tip; tip = tip->ET_next)
{
double diff = fabs(tip->angle - angle);
if(diff < ANG_EPSILON || diff > (360.0 - ANG_EPSILON))
{ // Yes, found one.
return NULL;
}
}
// OK, now just find the first wall_tip whose angle is greater than the angle
// we're interested in. Therefore we'll be on the FRONT side of that tip edge.
for(tip = vert->buildData.tipSet; tip; tip = tip->ET_next)
{
if(angle + ANG_EPSILON < tip->angle)
{
// Found it.
return (tip->ET_edge[FRONT]? tip->ET_edge[FRONT]->sector : NULL);
}
if(!tip->ET_next)
{
// No more tips, therefore this is the BACK of the tip with the largest angle.
return (tip->ET_edge[BACK]? tip->ET_edge[BACK]->sector : NULL);
}
}
Con_Error("Vertex %d has no tips !", vert->buildData.index);
exit(1); // Unreachable.
}