/
sectorcluster.cpp
823 lines (689 loc) · 24.8 KB
/
sectorcluster.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/** @file sectorcluster.cpp World map sector cluster.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @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 <QRect>
#include <QMap>
#include <QSet>
#include <QtAlgorithms>
#include "Face"
#include "BspLeaf"
#include "Line"
#include "Plane"
#include "Surface"
#include "world/map.h"
#include "world/p_object.h"
#include "world/p_players.h"
#ifdef __CLIENT__
# include "render/rend_main.h" // useBias
#endif
#include "world/sector.h"
using namespace de;
namespace internal
{
/// Classification flags:
enum ClusterFlag
{
NeverMapped = 0x01,
AllMissingBottom = 0x02,
AllMissingTop = 0x04,
AllSelfRef = 0x08,
PartSelfRef = 0x10
};
Q_DECLARE_FLAGS(ClusterFlags, ClusterFlag)
Q_DECLARE_OPERATORS_FOR_FLAGS(ClusterFlags)
}
using namespace internal;
static QRectF qrectFromAABox(AABoxd const &aaBox)
{
return QRectF(QPointF(aaBox.minX, aaBox.maxY), QPointF(aaBox.maxX, aaBox.minY));
}
DENG2_PIMPL(Sector::Cluster),
DENG2_OBSERVES(Sector::Cluster, Deletion),
DENG2_OBSERVES(Plane, Deletion),
DENG2_OBSERVES(Plane, HeightChange)
#ifdef __CLIENT__
, DENG2_OBSERVES(Plane, HeightSmoothedChange)
#endif
{
bool needClassify; ///< @c true= (Re)classification is necessary.
ClusterFlags flags;
BspLeafs bspLeafs;
QScopedPointer<AABoxd> aaBox;
Cluster *mappedVisFloor;
Cluster *mappedVisCeiling;
struct BoundaryInfo
{
/// Lists of unique exterior clusters which share a boundary edge with
/// "this" cluster (i.e., one edge per cluster).
QList<HEdge *> uniqueInnerEdges; /// not owned.
QList<HEdge *> uniqueOuterEdges; /// not owned.
};
QScopedPointer<BoundaryInfo> boundaryInfo;
#ifdef __CLIENT__
/// BSP leafs in the neighborhood effecting environmental audio characteristics.
typedef QSet<BspLeaf *> ReverbBspLeafs;
ReverbBspLeafs reverbBspLeafs;
/// Final environmental audio characteristics.
AudioEnvironmentFactors reverb;
bool needReverbUpdate; ///< @c true= marked for update.
#endif
Instance(Public *i)
: Base(i),
needClassify(true),
flags(0),
mappedVisFloor(0),
mappedVisCeiling(0)
#ifdef __CLIENT__
,needReverbUpdate(true)
#endif
{
#ifdef __CLIENT__
zap(reverb);
#endif
}
~Instance()
{
clearMapping(Sector::Floor);
clearMapping(Sector::Ceiling);
DENG2_FOR_PUBLIC_AUDIENCE(Deletion, i) i->sectorClusterBeingDeleted(self);
}
inline bool floorIsMapped()
{
return mappedVisFloor != 0 && mappedVisFloor != thisPublic;
}
inline bool ceilingIsMapped()
{
return mappedVisCeiling != 0 && mappedVisCeiling != thisPublic;
}
inline bool needRemapVisPlanes()
{
return mappedVisFloor == 0 || mappedVisCeiling == 0;
}
Cluster **mappedClusterAdr(int planeIdx)
{
if(planeIdx == Sector::Floor) return &mappedVisFloor;
if(planeIdx == Sector::Ceiling) return &mappedVisCeiling;
return 0;
}
inline Plane *mappedPlane(int planeIdx)
{
Cluster **clusterAdr = mappedClusterAdr(planeIdx);
if(clusterAdr && *clusterAdr)
{
return &(*clusterAdr)->plane(planeIdx);
}
return 0;
}
void observeCluster(Cluster *cluster, bool yes = true)
{
if(!cluster || cluster == thisPublic)
return;
if(yes) cluster->audienceForDeletion += this;
else cluster->audienceForDeletion -= this;
}
void observePlane(Plane *plane, bool yes = true, bool observeHeight = true)
{
if(!plane) return;
if(yes)
{
plane->audienceForDeletion += this;
if(observeHeight)
{
plane->audienceForHeightChange += this;
#ifdef __CLIENT__
plane->audienceForHeightSmoothedChange += this;
#endif
}
}
else
{
plane->audienceForDeletion -= this;
plane->audienceForHeightChange -= this;
#ifdef __CLIENT__
plane->audienceForHeightSmoothedChange -= this;
#endif
}
}
void map(int planeIdx, Cluster *newCluster, bool permanent = false)
{
Cluster **clusterAdr = mappedClusterAdr(planeIdx);
if(!clusterAdr || *clusterAdr == newCluster)
return;
observeCluster(*clusterAdr, false);
observePlane(mappedPlane(planeIdx), false);
*clusterAdr = newCluster;
observeCluster(*clusterAdr);
observePlane(mappedPlane(planeIdx), true, !permanent);
}
void clearMapping(int planeIdx)
{
map(planeIdx , 0);
}
/**
* To be called when a plane moves to possibly invalidate mapped planes so
* that they will be re-evaluated later.
*/
void maybeInvalidateMapping(int planeIdx)
{
if(classification() & NeverMapped)
return;
Cluster **clusterAdr = mappedClusterAdr(planeIdx);
if(!clusterAdr || *clusterAdr == thisPublic)
return;
clearMapping(planeIdx);
if(classification() & (AllMissingBottom|AllMissingTop))
{
// Reclassify incase material visibility has changed.
needClassify = true;
}
}
/**
* Returns a copy of the classification flags for the cluster, performing
* classification of the cluster if necessary.
*/
ClusterFlags classification()
{
if(needClassify)
{
needClassify = false;
flags &= ~(NeverMapped|PartSelfRef);
flags |= AllSelfRef|AllMissingBottom|AllMissingTop;
foreach(BspLeaf *leaf, bspLeafs)
{
HEdge const *base = leaf->poly().hedge();
HEdge const *hedge = base;
do
{
if(!hedge->mapElement())
continue;
// This edge defines a section of a map line.
if(hedge->twin().hasFace())
{
BspLeaf const &backLeaf = hedge->twin().face().mapElement()->as<BspLeaf>();
Cluster const *backCluster = backLeaf.hasCluster()? &backLeaf.cluster() : 0;
if(backCluster != thisPublic)
{
LineSideSegment const &seg = hedge->mapElement()->as<LineSideSegment>();
if(!seg.line().isSelfReferencing())
{
flags &= ~AllSelfRef;
LineSide const &lineSide = seg.lineSide();
if(lineSide.bottom().hasMaterial() &&
!lineSide.bottom().hasFixMaterial())
{
flags &= ~AllMissingBottom;
}
if(lineSide.top().hasMaterial() &&
!lineSide.top().hasFixMaterial())
{
flags &= ~AllMissingTop;
}
}
else
{
flags |= PartSelfRef;
}
}
}
else
{
// If a back geometry is missing then never map planes.
flags |= NeverMapped;
flags &= ~(PartSelfRef|AllSelfRef|AllMissingBottom|AllMissingTop);
// We're done.
return flags;
}
} while((hedge = &hedge->next()) != base);
}
}
return flags;
}
void initBoundaryInfo()
{
QMap<Cluster *, HEdge *> extClusterMap;
foreach(BspLeaf *leaf, bspLeafs)
{
HEdge *base = leaf->poly().hedge();
HEdge *hedge = base;
do
{
if(!hedge->mapElement())
continue;
DENG2_ASSERT(hedge->twin().hasFace()); // Sanity check.
BspLeaf &backLeaf = hedge->twin().face().mapElement()->as<BspLeaf>();
if(!backLeaf.hasCluster())
continue;
if(&backLeaf.cluster() == thisPublic)
continue;
extClusterMap.insert(&backLeaf.cluster(), hedge);
} while((hedge = &hedge->next()) != base);
}
boundaryInfo.reset(new BoundaryInfo);
QRectF boundingRect = qrectFromAABox(self.aaBox());
foreach(HEdge *hedge, extClusterMap)
{
Cluster &extCluster = hedge->twin().face().mapElement()->as<BspLeaf>().cluster();
if(boundingRect.contains(qrectFromAABox(extCluster.aaBox())))
{
boundaryInfo->uniqueInnerEdges.append(hedge);
}
else
{
boundaryInfo->uniqueOuterEdges.append(hedge);
}
}
}
void remapVisPlanes()
{
Sector §or = self.sector();
// By default both planes are mapped to the parent sector.
if(!floorIsMapped()) map(Sector::Floor, thisPublic);
if(!ceilingIsMapped()) map(Sector::Ceiling, thisPublic);
if(classification() & NeverMapped)
return;
if(classification() & (AllSelfRef|PartSelfRef))
{
// Should we permanently map planes to another cluster?
// Is it time to initialize the boundary info?
if(boundaryInfo.isNull())
{
initBoundaryInfo();
}
foreach(HEdge *hedge, boundaryInfo->uniqueOuterEdges)
{
Cluster &extCluster = hedge->twin().face().mapElement()->as<BspLeaf>().cluster();
if(!hedge->mapElement()->as<LineSideSegment>().line().isSelfReferencing())
continue;
if(!(classification() & AllSelfRef) &&
(extCluster.d->classification() & AllSelfRef))
continue;
if(extCluster.d->mappedVisFloor == thisPublic)
continue;
// Setup the mapping and we're done.
map(Sector::Floor, &extCluster, true /*permanently*/);
map(Sector::Ceiling, &extCluster, true /*permanently*/);
break;
}
if(floorIsMapped())
{
// Remove the mapping from all inner clusters to this, forcing
// their re-evaluation (however next time a different cluster
// will be selected from the boundary).
foreach(HEdge *hedge, boundaryInfo->uniqueInnerEdges)
{
Cluster &extCluster = hedge->twin().face().mapElement()->as<BspLeaf>().cluster();
if(!hedge->mapElement()->as<LineSideSegment>().line().isSelfReferencing())
continue;
if(!(classification() & AllSelfRef) &&
(extCluster.d->classification() & AllSelfRef))
continue;
if(extCluster.d->mappedVisFloor == thisPublic)
{
extCluster.d->clearMapping(Sector::Floor);
}
if(extCluster.d->mappedVisCeiling == thisPublic)
{
extCluster.d->clearMapping(Sector::Ceiling);
}
}
// Permanent mappings won't be remapped.
boundaryInfo.reset();
return;
}
}
if(classification() & AllSelfRef)
return;
// Dynamic mapping may be needed for one or more planes.
bool doFloor = !floorIsMapped() && classification().testFlag(AllMissingBottom);
bool doCeiling = !ceilingIsMapped() && classification().testFlag(AllMissingTop);
// The plane must not use a sky-masked material.
if(sector.floor().surface().hasSkyMaskedMaterial())
doFloor = false;
if(sector.ceiling().surface().hasSkyMaskedMaterial())
doCeiling = false;
if(!doFloor && !doCeiling)
return;
// The sector must have open space.
if(sector.ceiling().height() <= sector.floor().height())
return;
// Is it time to initialize the boundary info?
if(boundaryInfo.isNull())
{
initBoundaryInfo();
}
// Map "this" cluster to the first outer cluster found.
foreach(HEdge *hedge, boundaryInfo->uniqueOuterEdges)
{
Cluster &extCluster = hedge->twin().face().mapElement()->as<BspLeaf>().cluster();
if(doFloor && !floorIsMapped() &&
extCluster.visFloor().height() > sector.floor().height())
{
map(Sector::Floor, &extCluster);
if(!doCeiling) break;
}
if(doCeiling && !ceilingIsMapped() &&
extCluster.visCeiling().height() < sector.ceiling().height())
{
map(Sector::Ceiling, &extCluster);
if(!doFloor) break;
}
}
// Clear mappings for all inner clusters to force re-evaluation (which
// may in turn lead to their inner clusters being re-evaluated, producing
// a "ripple effect" that will remap any deeply nested dependents).
foreach(HEdge *hedge, boundaryInfo->uniqueInnerEdges)
{
Cluster &extCluster = hedge->twin().face().mapElement()->as<BspLeaf>().cluster();
if(extCluster.d->classification() & NeverMapped)
continue;
if(doFloor && extCluster.visFloor().height() >= sector.floor().height())
{
extCluster.d->clearMapping(Sector::Floor);
}
if(doCeiling && extCluster.visCeiling().height() <= sector.ceiling().height())
{
extCluster.d->clearMapping(Sector::Ceiling);
}
}
}
#ifdef __CLIENT__
/**
* To be called when the height changes to update the plotted decoration
* origins for surfaces whose material offset is dependant upon this.
*/
void markDependantSurfacesForDecorationUpdate()
{
if(ddMapSetup) return;
foreach(LineSide *side, self.sector().sides())
{
if(side->hasSections())
{
side->middle().markAsNeedingDecorationUpdate();
side->bottom().markAsNeedingDecorationUpdate();
side ->top().markAsNeedingDecorationUpdate();
}
if(side->back().hasSections())
{
LineSide &back = side->back();
back.middle().markAsNeedingDecorationUpdate();
back.bottom().markAsNeedingDecorationUpdate();
back .top().markAsNeedingDecorationUpdate();
}
}
}
#endif // __CLIENT__
/// Observes Sector::Cluster Deletion.
void sectorClusterBeingDeleted(Cluster const &cluster)
{
if( mappedVisFloor == &cluster) clearMapping(Sector::Floor);
if(mappedVisCeiling == &cluster) clearMapping(Sector::Ceiling);
}
/// Observes Plane Deletion.
void planeBeingDeleted(Plane const &plane)
{
clearMapping(plane.indexInSector());
}
/// Observes Plane HeightChange.
void planeHeightChanged(Plane &plane, coord_t oldHeight)
{
DENG2_UNUSED(oldHeight);
// Check if there are any camera players in this sector. If their height
// is now above the ceiling/below the floor they are now in the void.
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
player_t *plr = &ddPlayers[i];
ddplayer_t *ddpl = &plr->shared;
if(!ddpl->inGame || !ddpl->mo)
continue;
if(!ddpl->mo->bspLeaf || ddpl->mo->bspLeaf->isDegenerate())
continue;
if(&ddpl->mo->bspLeaf->cluster() != thisPublic)
continue;
if((ddpl->flags & DDPF_CAMERA) &&
(ddpl->mo->origin[VZ] > self.visCeiling().height() - 4 ||
ddpl->mo->origin[VZ] < self.visFloor().height()))
{
ddpl->inVoid = true;
}
}
#ifdef __CLIENT__
// A plane move means we must re-apply missing material fixes.
/// @todo optimize: Defer until actually necessary.
foreach(LineSide *side, self.sector().sides())
{
side->fixMissingMaterials();
}
// We'll need to recalculate environmental audio characteristics.
needReverbUpdate = true;
if(!ddMapSetup && useBias)
{
// Inform bias surfaces of changed geometry.
foreach(BspLeaf *bspLeaf, bspLeafs)
{
bspLeaf->updateBiasAfterGeometryMove(plane.indexInSector());
}
}
markDependantSurfacesForDecorationUpdate();
#endif // __CLIENT__
// We may need to update one or both mapped planes.
maybeInvalidateMapping(plane.indexInSector());
}
#ifdef __CLIENT__
/// Observes Plane HeightSmoothedChange.
void planeHeightSmoothedChanged(Plane &plane, coord_t oldHeight)
{
DENG2_UNUSED(oldHeight);
markDependantSurfacesForDecorationUpdate();
// We may need to update one or both mapped planes.
maybeInvalidateMapping(plane.indexInSector());
}
void addReverbBspLeaf(BspLeaf *bspLeaf)
{
// Degenerate leafs never contribute.
if(!bspLeaf || !bspLeaf->isDegenerate())
return;
reverbBspLeafs.insert(bspLeaf);
}
static int addReverbBspLeafWorker(BspLeaf *bspLeaf, void *context)
{
static_cast<Instance *>(context)->addReverbBspLeaf(bspLeaf);
return false; // Continue iteration.
}
/**
* Perform environmental audio (reverb) initialization.
*
* Determines the BSP leafs which contribute to the environmental audio
* characteristics. Given that BSP leafs do not change shape (on the XY plane,
* that is), they do not move and are not created/destroyed once the map has
* been loaded; this step can be pre-processed.
*
* @pre The Map's BSP leaf blockmap must be ready for use.
*/
void findReverbBspLeafs()
{
AABoxd affectionBounds = self.aaBox();
affectionBounds.minX -= 128;
affectionBounds.minY -= 128;
affectionBounds.maxX += 128;
affectionBounds.maxY += 128;
// Link all non-degenerate BspLeafs whose axis-aligned bounding box intersects
// with the affection bounds to the reverb set.
self.sector().map().bspLeafsBoxIterator(affectionBounds, 0, addReverbBspLeafWorker, this);
}
/**
* Recalculate environmental audio (reverb) for the sector.
*/
void updateReverb()
{
// Need to initialize?
if(reverbBspLeafs.isEmpty())
{
findReverbBspLeafs();
}
needReverbUpdate = false;
uint spaceVolume = int((self.visCeiling().height() - self.visFloor().height())
* self.roughArea());
reverb[SRD_SPACE] = reverb[SRD_VOLUME] =
reverb[SRD_DECAY] = reverb[SRD_DAMPING] = 0;
foreach(BspLeaf *bspLeaf, reverbBspLeafs)
{
if(bspLeaf->updateReverb())
{
BspLeaf::AudioEnvironmentFactors const &leafReverb = bspLeaf->reverb();
reverb[SRD_SPACE] += leafReverb[SRD_SPACE];
reverb[SRD_VOLUME] += leafReverb[SRD_VOLUME] / 255.0f * leafReverb[SRD_SPACE];
reverb[SRD_DECAY] += leafReverb[SRD_DECAY] / 255.0f * leafReverb[SRD_SPACE];
reverb[SRD_DAMPING] += leafReverb[SRD_DAMPING] / 255.0f * leafReverb[SRD_SPACE];
}
}
float spaceScatter;
if(reverb[SRD_SPACE])
{
spaceScatter = spaceVolume / reverb[SRD_SPACE];
// These three are weighted by the space.
reverb[SRD_VOLUME] /= reverb[SRD_SPACE];
reverb[SRD_DECAY] /= reverb[SRD_SPACE];
reverb[SRD_DAMPING] /= reverb[SRD_SPACE];
}
else
{
spaceScatter = 0;
reverb[SRD_VOLUME] = .2f;
reverb[SRD_DECAY] = .4f;
reverb[SRD_DAMPING] = 1;
}
// If the space is scattered, the reverb effect lessens.
reverb[SRD_SPACE] /= (spaceScatter > .8 ? 10 : spaceScatter > .6 ? 4 : 1);
// Normalize the reverb space [0..1]
// 0= very small
// .99= very large
// 1.0= only for open areas (special case).
reverb[SRD_SPACE] /= 120e6;
if(reverb[SRD_SPACE] > .99)
reverb[SRD_SPACE] = .99f;
if(self.visCeiling().surface().hasSkyMaskedMaterial() ||
self.visFloor().surface().hasSkyMaskedMaterial())
{
// An "open" sector.
// It can still be small, in which case; reverb is diminished a bit.
if(reverb[SRD_SPACE] > .5)
reverb[SRD_VOLUME] = 1; // Full volume.
else
reverb[SRD_VOLUME] = .5f; // Small, but still open.
reverb[SRD_SPACE] = 1;
}
else
{
// A "closed" sector.
// Large spaces have automatically a bit more audible reverb.
reverb[SRD_VOLUME] += reverb[SRD_SPACE] / 4;
}
if(reverb[SRD_VOLUME] > 1)
reverb[SRD_VOLUME] = 1;
}
#endif // __CLIENT__
};
Sector::Cluster::Cluster(BspLeafs const &bspLeafs) : d(new Instance(this))
{
d->bspLeafs.append(bspLeafs);
foreach(BspLeaf *bspLeaf, bspLeafs)
{
// Attribute the BSP leaf to the cluster.
bspLeaf->setCluster(this);
}
}
Sector &Sector::Cluster::sector() const
{
return d->bspLeafs.first()->parent().as<Sector>();
}
Plane &Sector::Cluster::plane(int planeIndex) const
{
// Physical planes are never mapped.
return sector().plane(planeIndex);
}
Plane &Sector::Cluster::visPlane(int planeIndex) const
{
if(planeIndex >= Floor && planeIndex <= Ceiling)
{
// Time to remap the planes?
if(d->needRemapVisPlanes())
{
d->remapVisPlanes();
}
/// @todo Cache this result.
Cluster *mappedCluster = (planeIndex == Ceiling? d->mappedVisCeiling : d->mappedVisFloor);
if(mappedCluster && mappedCluster != this)
{
return mappedCluster->visPlane(planeIndex);
}
}
// Not mapped.
return sector().plane(planeIndex);
}
AABoxd const &Sector::Cluster::aaBox() const
{
// If the cluster is comprised of a single BSP leaf we can use the bounding
// box of the leaf's geometry directly.
if(d->bspLeafs.count() == 1)
{
return d->bspLeafs.first()->poly().aaBox();
}
// Time to determine bounds?
if(d->aaBox.isNull())
{
// Unite the geometry bounding boxes of all BSP leafs in the cluster.
foreach(BspLeaf *leaf, d->bspLeafs)
{
AABoxd const &leafAABox = leaf->poly().aaBox();
if(!d->aaBox.isNull())
{
V2d_UniteBox((*d->aaBox).arvec2, leafAABox.arvec2);
}
else
{
d->aaBox.reset(new AABoxd(leafAABox));
}
}
}
return *d->aaBox;
}
Sector::Cluster::BspLeafs const &Sector::Cluster::bspLeafs() const
{
return d->bspLeafs;
}
#ifdef __CLIENT__
coord_t Sector::Cluster::roughArea() const
{
AABoxd const &bounds = aaBox();
return (bounds.maxX - bounds.minX) * (bounds.maxY - bounds.minY);
}
void Sector::Cluster::markReverbDirty(bool yes)
{
d->needReverbUpdate = yes;
}
AudioEnvironmentFactors const &Sector::Cluster::reverb() const
{
// Perform any scheduled update now.
if(d->needReverbUpdate)
{
d->updateReverb();
}
return d->reverb;
}
#endif // __CLIENT__