/
linedef.cpp
460 lines (394 loc) · 12.6 KB
/
linedef.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
/** @file linedef.h World Map Line.
*
* @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 <cmath>
#include <de/mathutil.h>
#include <de/Vector>
#include "de_base.h"
#include "m_misc.h"
#include "Materials"
#include "map/sector.h"
#include "map/sidedef.h"
#include "map/vertex.h"
#include "map/linedef.h"
using namespace de;
LineDef::Side::Side(Sector *sector)
: _sector(sector),
_sideDef(0),
_leftHEdge(0),
_rightHEdge(0),
_shadowVisCount(0)
{}
bool LineDef::Side::hasSector() const
{
return _sector != 0;
}
Sector &LineDef::Side::sector() const
{
if(_sector)
{
return *_sector;
}
/// @throw LineDef::MissingSectorError Attempted with no sector attributed.
throw LineDef::MissingSectorError("LineDef::Side::sector", "No sector is attributed");
}
bool LineDef::Side::hasSideDef() const
{
return _sideDef != 0;
}
SideDef &LineDef::Side::sideDef() const
{
if(_sideDef)
{
return *_sideDef;
}
/// @throw LineDef::MissingSideDefError Attempted with no sidedef configured.
throw LineDef::MissingSideDefError("LineDef::Side::sideDef", "No sidedef is configured");
}
HEdge &LineDef::Side::leftHEdge() const
{
DENG_ASSERT(_leftHEdge != 0);
return *_leftHEdge;
}
HEdge &LineDef::Side::rightHEdge() const
{
DENG_ASSERT(_rightHEdge != 0);
return *_rightHEdge;
}
int LineDef::Side::shadowVisCount() const
{
return _shadowVisCount;
}
void LineDef::Side::updateSoundEmitterOrigins()
{
if(!_sideDef) return;
_sideDef->middle().updateSoundEmitterOrigin();
_sideDef->bottom().updateSoundEmitterOrigin();
_sideDef->top().updateSoundEmitterOrigin();
}
void LineDef::Side::updateSurfaceNormals()
{
if(!_sideDef) return;
LineDef const &line = _sideDef->line();
byte sid = line.frontSideDefPtr() == _sideDef? FRONT : BACK;
Surface &middleSurface = _sideDef->middle();
Surface &bottomSurface = _sideDef->bottom();
Surface &topSurface = _sideDef->top();
Vector3f normal((line.vertexOrigin(sid^1)[VY] - line.vertexOrigin(sid )[VY]) / line.length(),
(line.vertexOrigin(sid )[VX] - line.vertexOrigin(sid^1)[VX]) / line.length(),
0);
// All line side surfaces have the same normals.
middleSurface.setNormal(normal); // will normalize
bottomSurface.setNormal(normal);
topSurface.setNormal(normal);
}
DENG2_PIMPL(LineDef)
{
/// Vertexes:
Vertex *from;
Vertex *to;
/// Direction vector from -> to.
Vector2d direction;
/// Logical line slope (i.e., world angle) classification.
slopetype_t slopeType;
/// Accurate length.
coord_t length;
/// Bounding box encompassing the map space coordinates of both vertexes.
AABoxd aaBox;
/// Logical sides:
Side front;
Side back;
/// Original index in the archived map.
uint origIndex;
/// Whether the line has been mapped by each player yet.
bool mapped[DDMAXPLAYERS];
Instance(Public *i, Vertex &from, Vertex &to,
Sector *frontSector, Sector *backSector)
: Base(i),
from(&from),
to(&to),
direction(Vector2d(to.origin()) - Vector2d(from.origin())),
slopeType(M_SlopeTypeXY(direction.x, direction.y)),
length(direction.length()),
front(frontSector),
back(backSector),
origIndex(0)
{
std::memset(mapped, 0, sizeof(mapped));
}
};
LineDef::LineDef(Vertex &from, Vertex &to, Sector *frontSector, Sector *backSector)
: MapElement(DMU_LINEDEF),
_vo1(0),
_vo2(0),
_flags(0),
_inFlags(0),
_angle(0),
_validCount(0),
d(new Instance(this, from, to, frontSector, backSector))
{
_angle = bamsAtan2(int( d->direction.y ), int( d->direction.x ));
updateAABox();
}
int LineDef::flags() const
{
return _flags;
}
uint LineDef::origIndex() const
{
return d->origIndex;
}
void LineDef::setOrigIndex(uint newIndex)
{
d->origIndex = newIndex;
}
bool LineDef::isBspWindow() const
{
return (_inFlags & LF_BSPWINDOW) != 0;
}
bool LineDef::isFromPolyobj() const
{
return (_inFlags & LF_POLYOBJ) != 0;
}
LineDef::Side &LineDef::side(int back)
{
return back? d->back : d->front;
}
LineDef::Side const &LineDef::side(int back) const
{
return back? d->back : d->front;
}
Vertex &LineDef::vertex(int to)
{
DENG_ASSERT((to? d->to : d->from) != 0);
return to? *d->to : *d->from;
}
Vertex const &LineDef::vertex(int to) const
{
DENG_ASSERT((to? d->to : d->from) != 0);
return to? *d->to : *d->from;
}
LineOwner *LineDef::vertexOwner(int to) const
{
DENG_ASSERT((to? _vo2 : _vo1) != 0);
return to? _vo2 : _vo1;
}
AABoxd const &LineDef::aaBox() const
{
return d->aaBox;
}
void LineDef::updateAABox()
{
V2d_InitBox(d->aaBox.arvec2, d->from->origin());
V2d_AddToBox(d->aaBox.arvec2, d->to->origin());
}
coord_t LineDef::length() const
{
return d->length;
}
Vector2d const &LineDef::direction() const
{
return d->direction;
}
slopetype_t LineDef::slopeType() const
{
return d->slopeType;
}
void LineDef::updateSlopeType()
{
d->direction = d->to->origin() - d->from->origin();
d->slopeType = M_SlopeTypeXY(d->direction.x, d->direction.y);
}
binangle_t LineDef::angle() const
{
return _angle;
}
int LineDef::boxOnSide(AABoxd const &box) const
{
coord_t v1Direction[2] = { direction().x, direction().y };
return M_BoxOnLineSide(&box, d->from->origin(), v1Direction);
}
int LineDef::boxOnSide_FixedPrecision(AABoxd const &box) const
{
/*
* Apply an offset to both the box and the line to bring everything into
* the 16.16 fixed-point range. We'll use the midpoint of the line as the
* origin, as typically this test is called when a bounding box is
* somewhere in the vicinity of the line. The offset is floored to integers
* so we won't change the discretization of the fractional part into 16-bit
* precision.
*/
coord_t offset[2] = { de::floor(d->from->origin()[VX] + d->direction.x/2),
de::floor(d->from->origin()[VY] + d->direction.y/2) };
fixed_t boxx[4];
boxx[BOXLEFT] = FLT2FIX(box.minX - offset[VX]);
boxx[BOXRIGHT] = FLT2FIX(box.maxX - offset[VX]);
boxx[BOXBOTTOM] = FLT2FIX(box.minY - offset[VY]);
boxx[BOXTOP] = FLT2FIX(box.maxY - offset[VY]);
fixed_t pos[2] = { FLT2FIX(d->from->origin()[VX] - offset[VX]),
FLT2FIX(d->from->origin()[VY] - offset[VY]) };
fixed_t delta[2] = { FLT2FIX(d->direction.x),
FLT2FIX(d->direction.y) };
return M_BoxOnLineSide_FixedPrecision(boxx, pos, delta);
}
bool LineDef::isMappedByPlayer(int playerNum) const
{
DENG2_ASSERT(playerNum >= 0 && playerNum < DDMAXPLAYERS);
return d->mapped[playerNum];
}
void LineDef::markMappedByPlayer(int playerNum, bool yes)
{
DENG2_ASSERT(playerNum >= 0 && playerNum < DDMAXPLAYERS);
d->mapped[playerNum] = yes;
}
int LineDef::validCount() const
{
return _validCount;
}
int LineDef::property(setargs_t &args) const
{
switch(args.prop)
{
case DMU_VERTEX0:
DMU_GetValue(DMT_LINEDEF_V, &d->from, &args, 0);
break;
case DMU_VERTEX1:
DMU_GetValue(DMT_LINEDEF_V, &d->to, &args, 0);
break;
case DMU_DX:
DMU_GetValue(DMT_LINEDEF_DX, &d->direction.x, &args, 0);
break;
case DMU_DY:
DMU_GetValue(DMT_LINEDEF_DY, &d->direction.y, &args, 0);
break;
case DMU_DXY:
DMU_GetValue(DMT_LINEDEF_DX, &d->direction.x, &args, 0);
DMU_GetValue(DMT_LINEDEF_DY, &d->direction.y, &args, 1);
break;
case DMU_LENGTH:
DMU_GetValue(DMT_LINEDEF_LENGTH, &d->length, &args, 0);
break;
case DMU_ANGLE: {
angle_t lineAngle = BANG_TO_ANGLE(_angle);
DMU_GetValue(DDVT_ANGLE, &lineAngle, &args, 0);
break; }
case DMU_SLOPETYPE:
DMU_GetValue(DMT_LINEDEF_SLOPETYPE, &d->slopeType, &args, 0);
break;
case DMU_FRONT_SECTOR: {
Sector const *frontSector = frontSectorPtr();
DMU_GetValue(DMT_LINEDEF_SECTOR, &frontSector, &args, 0);
break; }
case DMU_BACK_SECTOR: {
Sector const *backSector = backSectorPtr();
DMU_GetValue(DMT_LINEDEF_SECTOR, &backSector, &args, 0);
break; }
case DMU_FLAGS:
DMU_GetValue(DMT_LINEDEF_FLAGS, &_flags, &args, 0);
break;
case DMU_SIDEDEF0: {
SideDef const *frontSideDef = frontSideDefPtr();
DMU_GetValue(DDVT_PTR, &frontSideDef, &args, 0);
break; }
case DMU_SIDEDEF1: {
SideDef const *backSideDef = backSideDefPtr();
DMU_GetValue(DDVT_PTR, &backSideDef, &args, 0);
break; }
case DMU_BOUNDING_BOX:
if(args.valueType == DDVT_PTR)
{
AABoxd const *aaBoxAdr = &d->aaBox;
DMU_GetValue(DDVT_PTR, &aaBoxAdr, &args, 0);
}
else
{
DMU_GetValue(DMT_LINEDEF_AABOX, &d->aaBox.minX, &args, 0);
DMU_GetValue(DMT_LINEDEF_AABOX, &d->aaBox.maxX, &args, 1);
DMU_GetValue(DMT_LINEDEF_AABOX, &d->aaBox.minY, &args, 2);
DMU_GetValue(DMT_LINEDEF_AABOX, &d->aaBox.maxY, &args, 3);
}
break;
case DMU_VALID_COUNT:
DMU_GetValue(DMT_LINEDEF_VALIDCOUNT, &_validCount, &args, 0);
break;
default:
/// @throw UnknownPropertyError The requested property does not exist.
throw UnknownPropertyError("LineDef::property", QString("Property '%1' is unknown").arg(DMU_Str(args.prop)));
}
return false; // Continue iteration.
}
int LineDef::setProperty(setargs_t const &args)
{
/// @todo fixme: Changing the sector and/or sidedef references via the DMU
/// API should be disabled - it has no concept of what is actually needed
/// to effect such changes at run time.
///
/// At best this will result in strange glitches but more than likely a
/// fatal error (or worse) would happen should anyone try to use this...
switch(args.prop)
{
case DMU_FRONT_SECTOR: {
Sector *newFrontSector = frontSectorPtr();
DMU_SetValue(DMT_LINEDEF_SECTOR, &newFrontSector, &args, 0);
d->front._sector = newFrontSector;
break; }
case DMU_BACK_SECTOR: {
Sector *newBackSector = backSectorPtr();
DMU_SetValue(DMT_LINEDEF_SECTOR, &newBackSector, &args, 0);
d->back._sector = newBackSector;
break; }
case DMU_SIDEDEF0: {
SideDef *newFrontSideDef = frontSideDefPtr();
DMU_SetValue(DMT_LINEDEF_SIDEDEF, &newFrontSideDef, &args, 0);
d->front._sideDef = newFrontSideDef;
break; }
case DMU_SIDEDEF1: {
SideDef *newBackSideDef = backSideDefPtr();
DMU_SetValue(DMT_LINEDEF_SIDEDEF, &newBackSideDef, &args, 0);
d->back._sideDef = newBackSideDef;
break; }
case DMU_VALID_COUNT:
DMU_SetValue(DMT_LINEDEF_VALIDCOUNT, &_validCount, &args, 0);
break;
case DMU_FLAGS: {
#ifdef __CLIENT__
int oldFlags = _flags;
#endif
DMU_SetValue(DMT_LINEDEF_FLAGS, &_flags, &args, 0);
#ifdef __CLIENT__
/// @todo Surface should observe.
if(hasFrontSideDef())
{
if((_flags & DDLF_DONTPEGTOP) != (oldFlags & DDLF_DONTPEGTOP))
{
frontSideDef().top().markAsNeedingDecorationUpdate();
}
if((_flags & DDLF_DONTPEGBOTTOM) != (oldFlags & DDLF_DONTPEGBOTTOM))
{
frontSideDef().bottom().markAsNeedingDecorationUpdate();
}
}
#endif // __CLIENT__
break; }
default:
/// @throw WritePropertyError The requested property is not writable.
throw WritePropertyError("LineDef::setProperty", QString("Property '%1' is not writable").arg(DMU_Str(args.prop)));
}
return false; // Continue iteration.
}