-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
r_world.cpp
625 lines (522 loc) · 19.9 KB
/
r_world.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
/** @file r_world.cpp World Setup/Refresh.
*
* @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 <cstdlib>
#include <cstring>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_play.h"
#include "de_render.h"
#include "de_graphics.h"
#include "de_audio.h"
#include "de_misc.h"
#include "map/lineowner.h"
#include "Plane"
#ifdef __CLIENT__
# include "MaterialSnapshot"
# include "MaterialVariantSpec"
#endif
#include <de/Observers>
using namespace de;
boolean ddMapSetup;
/// Notified when the current map changes.
MapChangeAudience audienceForMapChange;
#undef R_SetupFog
DENG_EXTERN_C void R_SetupFog(float start, float end, float density, float *rgb)
{
Con_Execute(CMDS_DDAY, "fog on", true, false);
Con_Executef(CMDS_DDAY, true, "fog start %f", start);
Con_Executef(CMDS_DDAY, true, "fog end %f", end);
Con_Executef(CMDS_DDAY, true, "fog density %f", density);
Con_Executef(CMDS_DDAY, true, "fog color %.0f %.0f %.0f",
rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
}
#undef R_SetupFogDefaults
DENG_EXTERN_C void R_SetupFogDefaults()
{
// Go with the defaults.
Con_Execute(CMDS_DDAY,"fog off", true, false);
}
void R_SideSectionCoords(Line::Side const &side, int section,
coord_t *retBottom, coord_t *retTop, Vector2f *retMaterialOrigin)
{
Sector const *frontSec = side.sectorPtr();
Sector const *backSec = side.back().sectorPtr();
Line const &line = side.line();
bool const unpegBottom = (line.flags() & DDLF_DONTPEGBOTTOM) != 0;
bool const unpegTop = (line.flags() & DDLF_DONTPEGTOP) != 0;
coord_t bottom = 0, top = 0; // Shutup compiler.
// Single sided?
if(!frontSec || !backSec || !side.back().hasSections()/*front side of a "window"*/)
{
bottom = frontSec->floor().visHeight();
top = frontSec->ceiling().visHeight();
if(retMaterialOrigin)
{
*retMaterialOrigin = side.middle().visMaterialOrigin();
if(unpegBottom)
{
retMaterialOrigin->y -= top - bottom;
}
}
}
else
{
bool const stretchMiddle = side.isFlagged(SDF_MIDDLE_STRETCH);
Surface const *surface = &side.surface(section);
Plane const *ffloor = &frontSec->floor();
Plane const *fceil = &frontSec->ceiling();
Plane const *bfloor = &backSec->floor();
Plane const *bceil = &backSec->ceiling();
switch(section)
{
case Line::Side::Top:
// Can't go over front ceiling (would induce geometry flaws).
if(bceil->visHeight() < ffloor->visHeight())
bottom = ffloor->visHeight();
else
bottom = bceil->visHeight();
top = fceil->visHeight();
if(retMaterialOrigin)
{
*retMaterialOrigin = surface->visMaterialOrigin();
if(!unpegTop)
{
// Align with normal middle texture.
retMaterialOrigin->y -= fceil->visHeight() - bceil->visHeight();
}
}
break;
case Line::Side::Bottom: {
bool const raiseToBackFloor = (fceil->surface().hasSkyMaskedMaterial() && bceil->surface().hasSkyMaskedMaterial() &&
fceil->visHeight() < bceil->visHeight() &&
bfloor->visHeight() > fceil->visHeight());
coord_t t = bfloor->visHeight();
bottom = ffloor->visHeight();
// Can't go over the back ceiling, would induce polygon flaws.
if(bfloor->visHeight() > bceil->visHeight())
t = bceil->visHeight();
// Can't go over front ceiling, would induce polygon flaws.
// In the special case of a sky masked upper we must extend the bottom
// section up to the height of the back floor.
if(t > fceil->visHeight() && !raiseToBackFloor)
t = fceil->visHeight();
top = t;
if(retMaterialOrigin)
{
*retMaterialOrigin = surface->visMaterialOrigin();
if(bfloor->visHeight() > fceil->visHeight())
{
retMaterialOrigin->y -= (raiseToBackFloor? t : fceil->visHeight()) - bfloor->visHeight();
}
if(unpegBottom)
{
// Align with normal middle texture.
retMaterialOrigin->y += (raiseToBackFloor? t : fceil->visHeight()) - bfloor->visHeight();
}
}
break; }
case Line::Side::Middle:
if(!side.line().isSelfReferencing())
{
bottom = de::max(bfloor->visHeight(), ffloor->visHeight());
top = de::min(bceil->visHeight(), fceil->visHeight());
}
else
{
bottom = ffloor->visHeight();
top = bceil->visHeight();
}
if(retMaterialOrigin)
{
retMaterialOrigin->x = surface->visMaterialOrigin().x;
retMaterialOrigin->y = 0;
}
if(surface->hasMaterial() && !stretchMiddle)
{
#ifdef __CLIENT__
bool const clipBottom = !(!(devRendSkyMode || P_IsInVoid(viewPlayer)) && ffloor->surface().hasSkyMaskedMaterial() && bfloor->surface().hasSkyMaskedMaterial());
bool const clipTop = !(!(devRendSkyMode || P_IsInVoid(viewPlayer)) && fceil->surface().hasSkyMaskedMaterial() && bceil->surface().hasSkyMaskedMaterial());
#else
bool const clipBottom = !(ffloor->surface().hasSkyMaskedMaterial() && bfloor->surface().hasSkyMaskedMaterial());
bool const clipTop = !(fceil->surface().hasSkyMaskedMaterial() && bceil->surface().hasSkyMaskedMaterial());
#endif
HEdge &hedge = *side.leftHEdge();
coord_t openBottom, openTop;
if(!side.line().isSelfReferencing())
{
openBottom = bottom;
openTop = top;
}
else
{
openBottom = hedge.bspLeaf().sector().floor().visHeight();
openTop = hedge.bspLeaf().sector().ceiling().visHeight();
}
int const matHeight = surface->material().height();
coord_t const matYOffset = surface->visMaterialOrigin().y;
if(openTop > openBottom)
{
if(unpegBottom)
{
bottom += matYOffset;
top = bottom + matHeight;
}
else
{
top += matYOffset;
bottom = top - matHeight;
}
if(retMaterialOrigin && top > openTop)
{
retMaterialOrigin->y = top - openTop;
}
// Clip it?
if(clipTop || clipBottom)
{
if(clipBottom && bottom < openBottom)
bottom = openBottom;
if(clipTop && top > openTop)
top = openTop;
}
if(retMaterialOrigin && !clipTop)
{
retMaterialOrigin->y = 0;
}
}
}
break;
}
}
if(retBottom) *retBottom = bottom;
if(retTop) *retTop = top;
}
coord_t R_OpenRange(Line::Side const &side, Sector const *frontSec,
Sector const *backSec, coord_t *retBottom, coord_t *retTop)
{
DENG_UNUSED(side); // Don't remove (present for API symmetry) -ds
DENG_ASSERT(frontSec != 0);
coord_t top;
if(backSec && backSec->ceiling().height() < frontSec->ceiling().height())
{
top = backSec->ceiling().height();
}
else
{
top = frontSec->ceiling().height();
}
coord_t bottom;
if(backSec && backSec->floor().height() > frontSec->floor().height())
{
bottom = backSec->floor().height();
}
else
{
bottom = frontSec->floor().height();
}
if(retBottom) *retBottom = bottom;
if(retTop) *retTop = top;
return top - bottom;
}
coord_t R_VisOpenRange(Line::Side const &side, Sector const *frontSec,
Sector const *backSec, coord_t *retBottom, coord_t *retTop)
{
DENG_UNUSED(side); // Don't remove (present for API symmetry) -ds
DENG_ASSERT(frontSec != 0);
coord_t top;
if(backSec && backSec->ceiling().visHeight() < frontSec->ceiling().visHeight())
{
top = backSec->ceiling().visHeight();
}
else
{
top = frontSec->ceiling().visHeight();
}
coord_t bottom;
if(backSec && backSec->floor().visHeight() > frontSec->floor().visHeight())
{
bottom = backSec->floor().visHeight();
}
else
{
bottom = frontSec->floor().visHeight();
}
if(retBottom) *retBottom = bottom;
if(retTop) *retTop = top;
return top - bottom;
}
#ifdef __CLIENT__
bool R_SideBackClosed(Line::Side const &side, bool ignoreOpacity)
{
if(!side.hasSections()) return false;
if(!side.back().hasSections()) return true;
if(side.line().isSelfReferencing()) return false; // Never.
if(side.hasSector() && side.back().hasSector())
{
Sector const &frontSec = side.sector();
Sector const &backSec = side.back().sector();
if(backSec.floor().visHeight() >= backSec.ceiling().visHeight()) return true;
if(backSec.ceiling().visHeight() <= frontSec.floor().visHeight()) return true;
if(backSec.floor().visHeight() >= frontSec.ceiling().visHeight()) return true;
}
// Perhaps a middle material completely covers the opening?
if(side.hasSector() && side.middle().hasMaterial())
{
// Ensure we have up to date info about the material.
MaterialSnapshot const &ms = side.middle().material().prepare(Rend_MapSurfaceMaterialSpec());
if(ignoreOpacity || (ms.isOpaque() && !side.middle().blendMode() && side.middle().opacity() >= 1))
{
// Stretched middles always cover the opening.
if(side.isFlagged(SDF_MIDDLE_STRETCH))
return true;
coord_t openRange, openBottom, openTop;
openRange = R_VisOpenRange(side, &openBottom, &openTop);
if(ms.height() >= openRange)
{
// Possibly; check the placement.
coord_t bottom, top;
R_SideSectionCoords(side, Line::Side::Middle, &bottom, &top);
return (top > bottom && top >= openTop && bottom <= openBottom);
}
}
}
return false;
}
Line *R_FindLineNeighbor(Sector const *sector, Line const *line,
LineOwner const *own, bool antiClockwise, binangle_t *diff)
{
LineOwner const *cown = antiClockwise? &own->prev() : &own->next();
Line *other = &cown->line();
if(other == line)
return 0;
if(diff) *diff += (antiClockwise? cown->angle() : own->angle());
if(!other->hasBackSector() || !other->isSelfReferencing())
{
if(sector) // Must one of the sectors match?
{
if(other->frontSectorPtr() == sector ||
(other->hasBackSector() && other->backSectorPtr() == sector))
return other;
}
else
{
return other;
}
}
// Not suitable, try the next.
return R_FindLineNeighbor(sector, line, cown, antiClockwise, diff);
}
/**
* @param side Line side for which to determine covered opening status.
* @param frontSec Sector in front of the wall.
* @param backSec Sector behind the wall. Can be @c 0.
*
* @return @c true iff Line::Side @a front has a "middle" Material which completely
* covers the open range defined by sectors @a frontSec and @a backSec.
*
* @note Anything calling this is likely working at the wrong level (should work with
* half-edges instead).
*/
static bool middleMaterialCoversOpening(Line::Side const &side)
{
if(!side.hasSector()) return false; // Never.
if(!side.hasSections()) return false;
if(!side.middle().hasMaterial()) return false;
// Stretched middles always cover the opening.
if(side.isFlagged(SDF_MIDDLE_STRETCH))
return true;
// Ensure we have up to date info about the material.
MaterialSnapshot const &ms = side.middle().material().prepare(Rend_MapSurfaceMaterialSpec());
if(ms.isOpaque() && !side.middle().blendMode() && side.middle().opacity() >= 1)
{
coord_t openRange, openBottom, openTop;
openRange = R_VisOpenRange(side, &openBottom, &openTop);
if(ms.height() >= openRange)
{
// Possibly; check the placement.
coord_t bottom, top;
R_SideSectionCoords(side, Line::Side::Middle, &bottom, &top);
return (top > bottom && top >= openTop && bottom <= openBottom);
}
}
return false;
}
Line *R_FindSolidLineNeighbor(Sector const *sector, Line const *line,
LineOwner const *own, bool antiClockwise, binangle_t *diff)
{
LineOwner const *cown = antiClockwise? &own->prev() : &own->next();
Line *other = &cown->line();
if(other == line) return 0;
if(diff) *diff += (antiClockwise? cown->angle() : own->angle());
if(!((other->isBspWindow()) && other->frontSectorPtr() != sector) &&
!other->isSelfReferencing())
{
if(!other->hasFrontSector()) return other;
if(!other->hasBackSector()) return other;
if(other->frontSector().floor().visHeight() >= sector->ceiling().visHeight() ||
other->frontSector().ceiling().visHeight() <= sector->floor().visHeight() ||
other->backSector().floor().visHeight() >= sector->ceiling().visHeight() ||
other->backSector().ceiling().visHeight() <= sector->floor().visHeight() ||
other->backSector().ceiling().visHeight() <= other->backSector().floor().visHeight())
return other;
// Both front and back MUST be open by this point.
// Perhaps a middle material completely covers the opening?
// We should not give away the location of false walls (secrets).
Line::Side &otherSide = other->side(other->frontSectorPtr() == sector? Line::Front : Line::Back);
if(middleMaterialCoversOpening(otherSide))
return other;
}
// Not suitable, try the next.
return R_FindSolidLineNeighbor(sector, line, cown, antiClockwise, diff);
}
#endif // __CLIENT__
static void resetAllMapPlaneVisHeights(GameMap &map)
{
foreach(Sector *sector, map.sectors())
foreach(Plane *plane, sector->planes())
{
plane->resetVisHeight();
}
}
static void updateAllMapSectors(GameMap &map)
{
foreach(Sector *sector, map.sectors())
{
sector->updateSoundEmitterOrigin();
#ifdef __CLIENT__
map.updateMissingMaterialsForLinesOfSector(*sector);
S_MarkSectorReverbDirty(sector);
#endif
}
}
#undef R_SetupMap
DENG_EXTERN_C void R_SetupMap(int mode, int flags)
{
DENG_UNUSED(flags);
switch(mode)
{
case DDSMM_INITIALIZE:
// A new map is about to be setup.
ddMapSetup = true;
#ifdef __CLIENT__
App_Materials().purgeCacheQueue();
#endif
return;
case DDSMM_AFTER_LOADING:
DENG_ASSERT(theMap);
// Update everything again. Its possible that after loading we
// now have more HOMs to fix, etc..
theMap->initSkyFix();
updateAllMapSectors(*theMap);
resetAllMapPlaneVisHeights(*theMap);
theMap->initPolyobjs();
DD_ResetTimer();
return;
case DDSMM_FINALIZE: {
DENG_ASSERT(theMap);
if(gameTime > 20000000 / TICSPERSEC)
{
// In very long-running games, gameTime will become so large that it cannot be
// accurately converted to 35 Hz integer tics. Thus it needs to be reset back
// to zero.
gameTime = 0;
}
// We are now finished with the map entity db.
EntityDatabase_Delete(theMap->entityDatabase);
#ifdef __SERVER__
// Init server data.
Sv_InitPools();
#endif
#ifdef __CLIENT__
// Recalculate the light range mod matrix.
Rend_CalcLightModRange();
#endif
theMap->initPolyobjs();
P_MapSpawnPlaneParticleGens();
updateAllMapSectors(*theMap);
resetAllMapPlaneVisHeights(*theMap);
#ifdef __CLIENT__
theMap->buildSurfaceLists();
float startTime = Timer_Seconds();
Rend_CacheForMap();
App_Materials().processCacheQueue();
VERBOSE( Con_Message("Precaching took %.2f seconds.", Timer_Seconds() - startTime) )
#endif
S_SetupForChangedMap();
// Map setup has been completed.
// Run any commands specified in Map Info.
de::Uri mapUri = theMap->uri();
ded_mapinfo_t *mapInfo = Def_GetMapInfo(reinterpret_cast<uri_s *>(&mapUri));
if(mapInfo && mapInfo->execute)
{
Con_Execute(CMDS_SCRIPT, mapInfo->execute, true, false);
}
// Run the special map setup command, which the user may alias to do something useful.
String cmd = "init-" + mapUri.resolved();
if(Con_IsValidCommand(cmd.toUtf8().constData()))
{
Con_Executef(CMDS_SCRIPT, false, "%s", cmd.toUtf8().constData());
}
#ifdef __CLIENT__
// Clear any input events that might have accumulated during the
// setup period.
DD_ClearEvents();
#endif
// Now that the setup is done, let's reset the tictimer so it'll
// appear that no time has passed during the setup.
DD_ResetTimer();
// Kill all local commands and determine the invoid status of players.
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
player_t *plr = &ddPlayers[i];
ddplayer_t *ddpl = &plr->shared;
//clients[i].numTics = 0;
// Determine if the player is in the void.
ddpl->inVoid = true;
if(ddpl->mo)
{
BspLeaf *bspLeaf = theMap->bspLeafAtPoint(ddpl->mo->origin);
if(bspLeaf)
{
/// @todo $nplanes
if(ddpl->mo->origin[VZ] >= bspLeaf->sector().floor().visHeight() &&
ddpl->mo->origin[VZ] < bspLeaf->sector().ceiling().visHeight() - 4)
{
ddpl->inVoid = false;
}
}
}
}
// Reset the map tick timer.
ddMapTime = 0;
// We've finished setting up the map.
ddMapSetup = false;
// Inform the timing system to suspend the starting of the clock.
firstFrameAfterLoad = true;
DENG2_FOR_AUDIENCE(MapChange, i) i->currentMapChanged();
Z_PrintStatus();
return; }
default:
Con_Error("R_SetupMap: Unknown setup mode %i", mode);
exit(1); // Unreachable.
}
}