/
sector.cpp
456 lines (390 loc) · 12.2 KB
/
sector.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
/** @file sector.h World Map Sector.
*
* @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 <QListIterator>
#include <QtAlgorithms>
#include <de/Log>
#include "map/bspleaf.h"
#include "map/linedef.h"
#include "map/gamemap.h"
#ifdef __CLIENT__
# include "render/rend_bias.h"
#endif
#include "map/sector.h"
using namespace de;
float const Sector::DEFAULT_LIGHT_LEVEL = 1.f;
Vector3f const Sector::DEFAULT_LIGHT_COLOR = Vector3f(1.f, 1.f, 1.f);
DENG2_PIMPL(Sector)
{
/// Bounding box for the sector.
AABoxd aaBox;
/// Rough approximation of sector area (map units squared).
coord_t roughArea;
/// Primary sound emitter. Others are linked to this, forming a chain.
ddmobj_base_t soundEmitter;
/// List of lines which reference the sector (not owned).
Lines lines;
/// List of BSP leafs which reference the sector (not owned).
BspLeafs bspLeafs;
Instance(Public *i)
: Base(i),
roughArea(0)
{
std::memset(&soundEmitter, 0, sizeof(soundEmitter));
}
~Instance()
{
#ifdef __CLIENT__
// Destroy the biassurfaces for each plane.
foreach(BspLeaf *bspLeaf, bspLeafs)
for(uint i = 0; i < self.planeCount(); ++i)
{
SB_DestroySurface(bspLeaf->_bsuf[i]);
}
#endif
qDeleteAll(self._planes);
}
void notifyLightLevelChanged(float oldLightLevel)
{
DENG2_FOR_PUBLIC_AUDIENCE(LightLevelChange, i)
{
i->lightLevelChanged(self, oldLightLevel);
}
}
void notifyLightColorChanged(Vector3f const &oldLightColor,
int changedComponents)
{
DENG2_FOR_PUBLIC_AUDIENCE(LightColorChange, i)
{
i->lightColorChanged(self, oldLightColor, changedComponents);
}
}
void notifyLightColorChanged(Vector3f const &oldLightColor)
{
// Predetermine which components have changed.
int changedComponents = 0;
for(int i = 0; i < 3; ++i)
{
if(!de::fequal(self._lightColor[i], oldLightColor[i]))
changedComponents |= (1 << i);
}
notifyLightColorChanged(oldLightColor, changedComponents);
}
};
Sector::Sector(float lightLevel, Vector3f const &lightColor)
: MapElement(DMU_SECTOR),
_lightLevel(lightLevel), _oldLightLevel(_lightLevel),
_lightColor(lightColor), _oldLightColor(_lightColor),
d(new Instance(this))
{
_frameFlags = 0;
_validCount = 0;
_mobjList = 0;
std::memset(&_lightGridData, 0, sizeof(_lightGridData));
std::memset(_reverb, 0, sizeof(_reverb));
_origIndex = 0;
}
float Sector::lightLevel() const
{
return _lightLevel;
}
void Sector::setLightLevel(float newLightLevel_)
{
float newLightLevel = de::clamp(0.f, newLightLevel_, 1.f);
if(!de::fequal(_lightLevel, newLightLevel))
{
float oldLightLevel = _lightLevel;
_lightLevel = newLightLevel;
// Notify interested parties of the change.
d->notifyLightLevelChanged(oldLightLevel);
}
}
Vector3f const &Sector::lightColor() const
{
return _lightColor;
}
void Sector::setLightColor(Vector3f const &newLightColor_)
{
Vector3f newLightColor = Vector3f(de::clamp(0.f, newLightColor_.x, 1.f),
de::clamp(0.f, newLightColor_.y, 1.f),
de::clamp(0.f, newLightColor_.z, 1.f));
if(_lightColor != newLightColor)
{
Vector3f oldLightColor = _lightColor;
_lightColor = newLightColor;
// Notify interested parties of the change.
d->notifyLightColorChanged(oldLightColor);
}
}
void Sector::setLightColorComponent(int component, float newStrength)
{
DENG_ASSERT(component >= 0 && component < 3);
newStrength = de::clamp(0.f, newStrength, 1.f);
if(!de::fequal(_lightColor[component], newStrength))
{
Vector3f oldLightColor = _lightColor;
_lightColor[component] = newStrength;
// Notify interested parties of the change.
d->notifyLightColorChanged(oldLightColor, (1 << component));
}
}
struct mobj_s *Sector::firstMobj() const
{
return _mobjList;
}
ddmobj_base_t &Sector::soundEmitter()
{
return d->soundEmitter;
}
ddmobj_base_t const &Sector::soundEmitter() const
{
return const_cast<ddmobj_base_t const &>(const_cast<Sector &>(*this).soundEmitter());
}
AudioEnvironmentFactors const &Sector::audioEnvironmentFactors() const
{
return _reverb;
}
uint Sector::origIndex() const
{
return _origIndex;
}
int Sector::frameFlags() const
{
return _frameFlags;
}
int Sector::validCount() const
{
return _validCount;
}
Plane &Sector::plane(int planeIndex)
{
if(planeIndex >= 0 && planeIndex < _planes.count())
{
return *_planes[planeIndex];
}
/// @throw MissingPlaneError The referenced plane does not exist.
throw MissingPlaneError("Sector::plane", QString("Missing plane %1").arg(planeIndex));
}
Plane const &Sector::plane(int planeIndex) const
{
return const_cast<Plane const &>(const_cast<Sector &>(*this).plane(planeIndex));
}
Sector::Lines const &Sector::lines() const
{
return d->lines;
}
void Sector::buildLines(GameMap const &map)
{
d->lines.clear();
#ifdef DENG2_QT_4_7_OR_NEWER
uint count = 0;
foreach(LineDef *line, map.lines())
{
if(line->frontSectorPtr() == this ||
line->backSectorPtr() == this)
++count;
}
if(0 == count) return;
d->lines.reserve(count);
#endif
foreach(LineDef *line, map.lines())
{
if(line->frontSectorPtr() == this ||
line->backSectorPtr() == this)
{
// Ownership of the line is not given to the sector.
d->lines.append(line);
}
}
}
Sector::Planes const &Sector::planes() const
{
return _planes;
}
Sector::BspLeafs const &Sector::bspLeafs() const
{
return d->bspLeafs;
}
void Sector::buildBspLeafs(GameMap const &map)
{
d->bspLeafs.clear();
#ifdef DENG2_QT_4_7_OR_NEWER
uint count = 0;
foreach(BspLeaf *bspLeaf, map.bspLeafs())
{
if(bspLeaf->sectorPtr() == this)
++count;
}
if(0 == count) return;
d->bspLeafs.reserve(count);
#endif
foreach(BspLeaf *bspLeaf, map.bspLeafs())
{
if(bspLeaf->sectorPtr() == this)
{
// Ownership of the BSP leaf is not given to the sector.
d->bspLeafs.append(bspLeaf);
}
}
}
Sector::BspLeafs const &Sector::reverbBspLeafs() const
{
return _reverbBspLeafs;
}
AABoxd const &Sector::aaBox() const
{
return d->aaBox;
}
coord_t Sector::roughArea() const
{
return d->roughArea;
}
void Sector::updateAABox()
{
V2d_Set(d->aaBox.min, DDMAXFLOAT, DDMAXFLOAT);
V2d_Set(d->aaBox.max, DDMINFLOAT, DDMINFLOAT);
if(!d->bspLeafs.count()) return;
QListIterator<BspLeaf *> leafIt(d->bspLeafs);
BspLeaf *leaf = leafIt.next();
V2d_CopyBox(d->aaBox.arvec2, leaf->aaBox().arvec2);
while(leafIt.hasNext())
{
leaf = leafIt.next();
V2d_UniteBox(d->aaBox.arvec2, leaf->aaBox().arvec2);
}
}
void Sector::updateRoughArea()
{
d->roughArea = 0;
if(!d->bspLeafs.count()) return;
QListIterator<BspLeaf *> leafIt(d->bspLeafs);
BspLeaf *leaf = leafIt.next();
d->roughArea += (leaf->aaBox().maxX - leaf->aaBox().minX) *
(leaf->aaBox().maxY - leaf->aaBox().minY);
while(leafIt.hasNext())
{
leaf = leafIt.next();
d->roughArea += (leaf->aaBox().maxX - leaf->aaBox().minX) *
(leaf->aaBox().maxY - leaf->aaBox().minY);
}
}
void Sector::linkSoundEmitter(ddmobj_base_t &newEmitter)
{
// The sector's base is always head of the chain, so link the other after it.
newEmitter.thinker.prev = &d->soundEmitter.thinker;
newEmitter.thinker.next = d->soundEmitter.thinker.next;
if(newEmitter.thinker.next)
newEmitter.thinker.next->prev = &newEmitter.thinker;
d->soundEmitter.thinker.next = &newEmitter.thinker;
}
void Sector::updateSoundEmitterOrigin()
{
d->soundEmitter.origin[VX] = (d->aaBox.minX + d->aaBox.maxX) / 2;
d->soundEmitter.origin[VY] = (d->aaBox.minY + d->aaBox.maxY) / 2;
d->soundEmitter.origin[VZ] = (floor().height() + ceiling().height()) / 2;
}
int Sector::property(setargs_t &args) const
{
switch(args.prop)
{
case DMU_LIGHT_LEVEL:
DMU_GetValue(DMT_SECTOR_LIGHTLEVEL, &_lightLevel, &args, 0);
break;
case DMU_COLOR:
DMU_GetValue(DMT_SECTOR_RGB, &_lightColor.x, &args, 0);
DMU_GetValue(DMT_SECTOR_RGB, &_lightColor.y, &args, 1);
DMU_GetValue(DMT_SECTOR_RGB, &_lightColor.z, &args, 2);
break;
case DMU_COLOR_RED:
DMU_GetValue(DMT_SECTOR_RGB, &_lightColor.x, &args, 0);
break;
case DMU_COLOR_GREEN:
DMU_GetValue(DMT_SECTOR_RGB, &_lightColor.y, &args, 0);
break;
case DMU_COLOR_BLUE:
DMU_GetValue(DMT_SECTOR_RGB, &_lightColor.z, &args, 0);
break;
case DMU_BASE: {
ddmobj_base_t const *soundEmitterAdr = &d->soundEmitter;
DMU_GetValue(DMT_SECTOR_BASE, &soundEmitterAdr, &args, 0);
break; }
case DMU_LINEDEF_COUNT: {
int val = lineCount();
DMU_GetValue(DDVT_INT, &val, &args, 0);
break; }
case DMT_MOBJS:
DMU_GetValue(DMT_SECTOR_MOBJLIST, &_mobjList, &args, 0);
break;
case DMU_VALID_COUNT:
DMU_GetValue(DMT_SECTOR_VALIDCOUNT, &_validCount, &args, 0);
break;
case DMU_FLOOR_PLANE: {
Plane *pln = _planes[Plane::Floor];
DMU_GetValue(DMT_SECTOR_FLOORPLANE, &pln, &args, 0);
break; }
case DMU_CEILING_PLANE: {
Plane* pln = _planes[Plane::Ceiling];
DMU_GetValue(DMT_SECTOR_CEILINGPLANE, &pln, &args, 0);
break; }
default:
/// @throw UnknownPropertyError The requested property does not exist.
throw UnknownPropertyError("Sector::property", QString("Property '%1' is unknown").arg(DMU_Str(args.prop)));
}
return false; // Continue iteration.
}
int Sector::setProperty(setargs_t const &args)
{
switch(args.prop)
{
case DMU_COLOR: {
Vector3f newLightColor;
DMU_SetValue(DMT_SECTOR_RGB, &newLightColor.x, &args, 0);
DMU_SetValue(DMT_SECTOR_RGB, &newLightColor.y, &args, 1);
DMU_SetValue(DMT_SECTOR_RGB, &newLightColor.z, &args, 2);
setLightColor(newLightColor);
break; }
case DMU_COLOR_RED: {
float newStrength;
DMU_SetValue(DMT_SECTOR_RGB, &newStrength, &args, 0);
setLightRed(newStrength);
break; }
case DMU_COLOR_GREEN: {
float newStrength;
DMU_SetValue(DMT_SECTOR_RGB, &newStrength, &args, 0);
setLightGreen(newStrength);
break; }
case DMU_COLOR_BLUE: {
float newStrength;
DMU_SetValue(DMT_SECTOR_RGB, &newStrength, &args, 0);
setLightBlue(newStrength);
break; }
case DMU_LIGHT_LEVEL: {
float newLightLevel;
DMU_SetValue(DMT_SECTOR_LIGHTLEVEL, &newLightLevel, &args, 0);
setLightLevel(newLightLevel);
break; }
case DMU_VALID_COUNT:
DMU_SetValue(DMT_SECTOR_VALIDCOUNT, &_validCount, &args, 0);
break;
default:
/// @throw WritePropertyError The requested property is not writable.
throw WritePropertyError("Sector::setProperty", QString("Property '%1' is not writable").arg(DMU_Str(args.prop)));
}
return false; // Continue iteration.
}