-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
plane.cpp
378 lines (308 loc) · 9.08 KB
/
plane.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
/** @file plane.h World map plane.
*
* @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 <QList>
#include <de/Log>
#include "de_base.h"
#include "audio/s_environ.h"
#include "Line"
#include "world/map.h" /// @todo remove me.
#include "world/r_world.h" // ddMapSetup
#include "world/world.h"
#include "render/r_main.h" // frameTimePos
#include "world/plane.h"
using namespace de;
DENG2_PIMPL(Plane)
{
/// Sector that owns the plane.
Sector *sector;
/// Sound emitter.
ddmobj_base_t soundEmitter;
/// Index of the plane in the owning sector.
int inSectorIndex;
/// Current @em sharp height relative to @c 0 on the map up axis (positive is up).
coord_t height;
/// @em sharp height change tracking buffer (for smoothing).
coord_t oldHeight[2];
/// Target @em sharp height.
coord_t targetHeight;
/// Visual plane height (smoothed).
coord_t visHeight;
/// Delta between the current @em sharp height and the visual height.
coord_t visHeightDelta;
/// Movement speed (map space units per tic).
coord_t speed;
/// Plane surface.
Surface surface;
Instance(Public *i, Sector §or, coord_t height)
: Base(i),
sector(§or),
inSectorIndex(-1),
height(height),
targetHeight(height),
visHeight(height),
visHeightDelta(0),
speed(0),
surface(dynamic_cast<MapElement &>(*i))
{
oldHeight[0] = oldHeight[1] = height;
zap(soundEmitter);
}
~Instance()
{
#ifdef __CLIENT__
Map &map = App_World().map();
// If this plane is currently being watched, remove it.
/// @todo Map should observe Deletion.
map.trackedPlanes().remove(&self);
// If this plane's surface is in the moving list, remove it.
/// @todo Map should observe Deletion.
map.scrollingSurfaces().remove(&surface);
// If this plane's surface is in the glowing list, remove it.
/// @todo Map should observe Deletion.
map.glowingSurfaces().remove(&surface);
// If this plane's surface is in the decorated list, remove it.
/// @todo Map should observe Deletion.
map.decoratedSurfaces().remove(&surface);
#endif // __CLIENT__
DENG2_FOR_PUBLIC_AUDIENCE(Deletion, i) i->planeBeingDeleted(self);
}
void notifyHeightChanged(coord_t oldHeight)
{
DENG2_FOR_PUBLIC_AUDIENCE(HeightChange, i)
{
i->planeHeightChanged(self, oldHeight);
}
}
void applySharpHeightChange(coord_t newHeight)
{
// No change?
if(de::fequal(newHeight, height))
return;
coord_t oldHeight = height;
height = newHeight;
if(!ddMapSetup)
{
// Update the sound emitter origin for the plane.
self.updateSoundEmitterOrigin();
#ifdef __CLIENT__
// We need the decorations updated.
surface.markAsNeedingDecorationUpdate();
#endif
}
// Notify interested parties of the change.
notifyHeightChanged(oldHeight);
#ifdef __CLIENT__
/// @todo Map should observe.
if(!ddMapSetup)
{
if(App_World().hasMap())
{
App_World().map().trackedPlanes().insert(&self);
}
markDependantSurfacesForDecorationUpdate();
}
#endif
}
#ifdef __CLIENT__
/**
* To be called when the height changes to update the plotted decoration
* origins for surfaces whose material offset is dependant upon this.
*
* @todo Sector should observe instead.
*/
void markDependantSurfacesForDecorationUpdate()
{
if(ddMapSetup) return;
// "Middle" planes have no dependent surfaces.
if(inSectorIndex > Sector::Ceiling) return;
// Mark the decor lights on the sides of this plane as requiring
// an update.
foreach(Line::Side *side, sector->sides())
{
if(side->hasSections())
{
side->middle().markAsNeedingDecorationUpdate();
side->bottom().markAsNeedingDecorationUpdate();
side->top().markAsNeedingDecorationUpdate();
}
if(side->back().hasSections())
{
Line::Side &back = side->back();
back.middle().markAsNeedingDecorationUpdate();
back.bottom().markAsNeedingDecorationUpdate();
back.top().markAsNeedingDecorationUpdate();
}
}
}
#endif // __CLIENT__
};
Plane::Plane(Sector §or, Vector3f const &normal, coord_t height)
: MapElement(DMU_PLANE), d(new Instance(this, sector, height))
{
setNormal(normal);
}
Sector &Plane::sector()
{
return *d->sector;
}
Sector const &Plane::sector() const
{
return *d->sector;
}
int Plane::inSectorIndex() const
{
return d->inSectorIndex;
}
void Plane::setInSectorIndex(int newIndex)
{
d->inSectorIndex = newIndex;
}
bool Plane::isSectorFloor() const
{
return this == §or().floor();
}
bool Plane::isSectorCeiling() const
{
return this == §or().ceiling();
}
Surface &Plane::surface()
{
return d->surface;
}
Surface const &Plane::surface() const
{
return d->surface;
}
ddmobj_base_t &Plane::soundEmitter()
{
return d->soundEmitter;
}
ddmobj_base_t const &Plane::soundEmitter() const
{
return const_cast<ddmobj_base_t const &>(const_cast<Plane &>(*this).soundEmitter());
}
void Plane::updateSoundEmitterOrigin()
{
LOG_AS("Plane::updateSoundEmitterOrigin");
d->soundEmitter.origin[VX] = d->sector->soundEmitter().origin[VX];
d->soundEmitter.origin[VY] = d->sector->soundEmitter().origin[VY];
d->soundEmitter.origin[VZ] = d->height;
}
coord_t Plane::height() const
{
return d->height;
}
coord_t Plane::targetHeight() const
{
return d->targetHeight;
}
coord_t Plane::speed() const
{
return d->speed;
}
#ifdef __CLIENT__
coord_t Plane::visHeight() const
{
// $smoothplane
return d->visHeight;
}
coord_t Plane::visHeightDelta() const
{
// $smoothplane
return d->visHeightDelta;
}
void Plane::lerpVisHeight()
{
// $smoothplane
d->visHeightDelta = d->oldHeight[0] * (1 - frameTimePos) + d->height * frameTimePos - d->height;
// Visible plane height.
d->visHeight = d->height + d->visHeightDelta;
d->markDependantSurfacesForDecorationUpdate();
}
void Plane::resetVisHeight()
{
// $smoothplane
d->visHeightDelta = 0;
d->visHeight = d->oldHeight[0] = d->oldHeight[1] = d->height;
d->markDependantSurfacesForDecorationUpdate();
}
void Plane::updateHeightTracking()
{
// $smoothplane
d->oldHeight[0] = d->oldHeight[1];
d->oldHeight[1] = d->height;
if(d->oldHeight[0] != d->oldHeight[1])
{
if(de::abs(d->oldHeight[0] - d->oldHeight[1]) >= MAX_SMOOTH_MOVE)
{
// Too fast: make an instantaneous jump.
d->oldHeight[0] = d->oldHeight[1];
}
}
}
#endif // __CLIENT__
void Plane::setNormal(Vector3f const &newNormal)
{
d->surface.setNormal(newNormal); // will normalize
}
int Plane::property(DmuArgs &args) const
{
switch(args.prop)
{
case DMU_EMITTER:
args.setValue(DMT_PLANE_EMITTER, &d->soundEmitter, 0);
break;
case DMU_SECTOR:
args.setValue(DMT_PLANE_SECTOR, &d->sector, 0);
break;
case DMU_HEIGHT:
args.setValue(DMT_PLANE_HEIGHT, &d->height, 0);
break;
case DMU_TARGET_HEIGHT:
args.setValue(DMT_PLANE_TARGET, &d->targetHeight, 0);
break;
case DMU_SPEED:
args.setValue(DMT_PLANE_SPEED, &d->speed, 0);
break;
default:
return MapElement::property(args);
}
return false; // Continue iteration.
}
int Plane::setProperty(DmuArgs const &args)
{
switch(args.prop)
{
case DMU_HEIGHT: {
coord_t newHeight = d->height;
args.value(DMT_PLANE_HEIGHT, &newHeight, 0);
d->applySharpHeightChange(newHeight);
break; }
case DMU_TARGET_HEIGHT:
args.value(DMT_PLANE_TARGET, &d->targetHeight, 0);
break;
case DMU_SPEED:
args.value(DMT_PLANE_SPEED, &d->speed, 0);
break;
default:
return MapElement::setProperty(args);
}
return false; // Continue iteration.
}