/
hedge.cpp
381 lines (333 loc) · 12.2 KB
/
hedge.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
/**
* @file hedge.cpp
* Map half-edge implementation. @ingroup map
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2012 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 "de_base.h"
#include "de_console.h"
#include "de_play.h"
#include "de_refresh.h"
coord_t WallDivNode_Height(walldivnode_t* node)
{
assert(node);
return node->height;
}
walldivnode_t* WallDivNode_Next(walldivnode_t* node)
{
assert(node);
uint idx = node - node->divs->nodes;
if(idx+1 >= node->divs->num) return 0;
return &node->divs->nodes[idx+1];
}
walldivnode_t* WallDivNode_Prev(walldivnode_t* node)
{
assert(node);
uint idx = node - node->divs->nodes;
if(idx == 0) return 0;
return &node->divs->nodes[idx-1];
}
uint WallDivs_Size(const walldivs_t* wd)
{
assert(wd);
return wd->num;
}
walldivnode_t* WallDivs_First(walldivs_t* wd)
{
assert(wd);
return &wd->nodes[0];
}
walldivnode_t* WallDivs_Last(walldivs_t* wd)
{
assert(wd);
return &wd->nodes[wd->num-1];
}
static int C_DECL sortWallDivNode(const void* e1, const void* e2)
{
const coord_t h1 = ((walldivnode_t*)e1)->height;
const coord_t h2 = ((walldivnode_t*)e2)->height;
if(h1 > h2) return 1;
if(h2 > h1) return -1;
return 0;
}
static void addWallDivNode(walldivs_t* wd, coord_t height)
{
assert(wd);
struct walldivnode_s* node = &wd->nodes[wd->num++];
node->divs = wd;
node->height = height;
}
static walldivnode_t* findWallDivNodeByZOrigin(walldivs_t* wallDivs, coord_t height)
{
Q_ASSERT(wallDivs);
for(uint i = 0; i < wallDivs->num; ++i)
{
walldivnode_t* node = &wallDivs->nodes[i];
if(node->height == height)
return node;
}
return NULL;
}
static void addWallDivNodesForPlaneIntercepts(HEdge* hedge, walldivs_t* wallDivs,
SideDefSection section, coord_t bottomZ, coord_t topZ, boolean doRight)
{
const boolean isTwoSided = (hedge->lineDef && hedge->lineDef->L_frontside && hedge->lineDef->L_backside)? true:false;
const boolean clockwise = !doRight;
const LineDef* line = hedge->lineDef;
SideDef* frontSide = HEDGE_SIDEDEF(hedge);
Sector* frontSec = frontSide->sector;
// Check for neighborhood division?
if(section == SS_MIDDLE && isTwoSided) return;
if(hedge->lineDef->inFlags & LF_POLYOBJ) return;
if(!hedge->lineDef) return;
// Polyobj edges are never split.
if(hedge->lineDef && (hedge->lineDef->inFlags & LF_POLYOBJ)) return;
// Only edges at sidedef ends can/should be split.
if(!((hedge == frontSide->hedgeLeft && !doRight) ||
(hedge == frontSide->hedgeRight && doRight)))
return;
if(bottomZ >= topZ) return; // Obviously no division.
// Retrieve the start owner node.
lineowner_t* base = R_GetVtxLineOwner(line->L_v(hedge->side^doRight), line);
lineowner_t* own = base;
boolean stopScan = false;
do
{
own = own->link[clockwise];
if(own == base)
{
stopScan = true;
}
else
{
LineDef* iter = own->lineDef;
if(LINE_SELFREF(iter))
continue;
uint i = 0;
do
{ // First front, then back.
Sector* scanSec = NULL;
if(!i && iter->L_frontside && iter->L_frontsector != frontSec)
scanSec = iter->L_frontsector;
else if(i && iter->L_backside && iter->L_backsector != frontSec)
scanSec = iter->L_backsector;
if(scanSec)
{
if(scanSec->SP_ceilvisheight - scanSec->SP_floorvisheight > 0)
{
for(uint j = 0; j < scanSec->planeCount && !stopScan; ++j)
{
Plane* pln = scanSec->SP_plane(j);
if(pln->visHeight > bottomZ && pln->visHeight < topZ)
{
if(!findWallDivNodeByZOrigin(wallDivs, pln->visHeight))
{
addWallDivNode(wallDivs, pln->visHeight);
// Have we reached the div limit?
if(wallDivs->num == WALLDIVS_MAX_NODES)
stopScan = true;
}
}
if(!stopScan)
{
// Clip a range bound to this height?
if(pln->type == PLN_FLOOR && pln->visHeight > bottomZ)
bottomZ = pln->visHeight;
else if(pln->type == PLN_CEILING && pln->visHeight < topZ)
topZ = pln->visHeight;
// All clipped away?
if(bottomZ >= topZ)
stopScan = true;
}
}
}
else
{
/**
* A zero height sector is a special case. In this
* instance, the potential division is at the height
* of the back ceiling. This is because elsewhere
* we automatically fix the case of a floor above a
* ceiling by lowering the floor.
*/
coord_t z = scanSec->SP_ceilvisheight;
if(z > bottomZ && z < topZ)
{
if(!findWallDivNodeByZOrigin(wallDivs, z))
{
addWallDivNode(wallDivs, z);
// Have we reached the div limit?
if(wallDivs->num == WALLDIVS_MAX_NODES)
stopScan = true;
}
}
}
}
} while(!stopScan && ++i < 2);
// Stop the scan when a single sided line is reached.
if(!iter->L_frontside || !iter->L_backside)
stopScan = true;
}
} while(!stopScan);
}
static void buildWallDiv(walldivs_t* wallDivs, HEdge* hedge,
SideDefSection section, coord_t bottomZ, coord_t topZ, boolean doRight)
{
wallDivs->num = 0;
// Nodes are arranged according to their Z axis height in ascending order.
// The first node is the bottom.
addWallDivNode(wallDivs, bottomZ);
// Add nodes for intercepts.
addWallDivNodesForPlaneIntercepts(hedge, wallDivs, section, bottomZ, topZ, doRight);
// The last node is the top.
addWallDivNode(wallDivs, topZ);
if(!(wallDivs->num > 2)) return;
// Sorting is required. This shouldn't take too long...
// There seldom are more than two or three nodes.
qsort(wallDivs->nodes, wallDivs->num, sizeof(*wallDivs->nodes), sortWallDivNode);
#ifdef RANGECHECK
for(uint i = 1; i < wallDivs->num - 1; ++i)
{
const walldivnode_t* node = &wallDivs->nodes[i];
if(node->height > topZ || node->height < bottomZ)
{
Con_Error("WallDiv node #%i pos (%f) <> hi (%f), low (%f), num=%i\n",
i, node->height, topZ, bottomZ, wallDivs->num);
}
}
#endif
}
boolean HEdge_PrepareWallDivs(HEdge* hedge, SideDefSection section,
Sector* frontSec, Sector* backSec,
walldivs_t* leftWallDivs, walldivs_t* rightWallDivs, float matOffset[2])
{
Q_ASSERT(hedge);
SideDef* frontSide = HEDGE_SIDEDEF(hedge);
boolean visible = false;
coord_t low, hi;
// Single sided?
if(!frontSec || !backSec ||
(hedge->twin && !HEDGE_SIDEDEF(hedge->twin))/*front side of a "window"*/)
{
low = frontSec->SP_floorvisheight;
hi = frontSec->SP_ceilvisheight;
if(matOffset)
{
Surface* suf = &frontSide->SW_middlesurface;
matOffset[0] = suf->visOffset[0] + (float)(hedge->offset);
matOffset[1] = suf->visOffset[1];
if(hedge->lineDef->flags & DDLF_DONTPEGBOTTOM)
{
matOffset[1] += -(hi - low);
}
}
visible = hi > low;
}
else
{
LineDef* line = hedge->lineDef;
Plane* ffloor = frontSec->SP_plane(PLN_FLOOR);
Plane* fceil = frontSec->SP_plane(PLN_CEILING);
Plane* bfloor = backSec->SP_plane(PLN_FLOOR);
Plane* bceil = backSec->SP_plane(PLN_CEILING);
Surface* suf = &frontSide->SW_surface(section);
visible = R_FindBottomTop(hedge->lineDef, hedge->side, section,
hedge->offset + suf->visOffset[VX], suf->visOffset[VY],
ffloor, fceil, bfloor, bceil,
(line->flags & DDLF_DONTPEGBOTTOM)? true : false,
(line->flags & DDLF_DONTPEGTOP)? true : false,
(frontSide->flags & SDF_MIDDLE_STRETCH)? true : false,
LINE_SELFREF(line)? true : false,
&low, &hi, matOffset);
}
if(!visible) return false;
buildWallDiv(leftWallDivs, hedge, section, low, hi, false/*is-left-edge*/);
buildWallDiv(rightWallDivs, hedge, section, low, hi, true/*is-right-edge*/);
return true;
}
HEdge* HEdge_New(void)
{
HEdge* hedge = static_cast<HEdge*>(Z_Calloc(sizeof *hedge, PU_MAPSTATIC, 0));
hedge->header.type = DMU_HEDGE;
return hedge;
}
HEdge* HEdge_NewCopy(const HEdge* other)
{
assert(other);
HEdge* hedge = static_cast<HEdge*>(Z_Malloc(sizeof *hedge, PU_MAPSTATIC, 0));
memcpy(hedge, other, sizeof *hedge);
return hedge;
}
void HEdge_Delete(HEdge* hedge)
{
assert(hedge);
for(uint i = 0; i < 3; ++i)
{
if(hedge->bsuf[i])
{
SB_DestroySurface(hedge->bsuf[i]);
}
}
Z_Free(hedge);
}
int HEdge_SetProperty(HEdge* hedge, const setargs_t* args)
{
DENG_UNUSED(hedge);
Con_Error("HEdge_SetProperty: Property %s is not writable.\n", DMU_Str(args->prop));
return false; // Continue iteration.
}
int HEdge_GetProperty(const HEdge* hedge, setargs_t* args)
{
switch(args->prop)
{
case DMU_VERTEX0:
DMU_GetValue(DMT_HEDGE_V, &hedge->HE_v1, args, 0);
break;
case DMU_VERTEX1:
DMU_GetValue(DMT_HEDGE_V, &hedge->HE_v2, args, 0);
break;
case DMU_LENGTH:
DMU_GetValue(DMT_HEDGE_LENGTH, &hedge->length, args, 0);
break;
case DMU_OFFSET:
DMU_GetValue(DMT_HEDGE_OFFSET, &hedge->offset, args, 0);
break;
case DMU_SIDEDEF:
DMU_GetValue(DMT_HEDGE_SIDEDEF, &HEDGE_SIDEDEF(hedge), args, 0);
break;
case DMU_LINEDEF:
DMU_GetValue(DMT_HEDGE_LINEDEF, &hedge->lineDef, args, 0);
break;
case DMU_FRONT_SECTOR: {
Sector* sec = hedge->sector? hedge->sector : NULL;
DMU_GetValue(DMT_HEDGE_SECTOR, &sec, args, 0);
break;
}
case DMU_BACK_SECTOR: {
Sector* sec = HEDGE_BACK_SECTOR(hedge);
DMU_GetValue(DMT_HEDGE_SECTOR, &sec, args, 0);
break;
}
case DMU_ANGLE:
DMU_GetValue(DMT_HEDGE_ANGLE, &hedge->angle, args, 0);
break;
default:
Con_Error("HEdge_GetProperty: No property %s.\n", DMU_Str(args->prop));
}
return false; // Continue iteration.
}