/
r_world.cpp
1612 lines (1361 loc) · 48.3 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @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/plane.h"
#ifdef __CLIENT__
# include "MaterialSnapshot"
# include "MaterialVariantSpec"
#endif
#include <de/Observers>
// $smoothmatoffset: Maximum speed for a smoothed material offset.
#define MAX_SMOOTH_MATERIAL_MOVE (8)
using namespace de;
float rendLightWallAngle = 1.2f; // Intensity of angle-based wall lighting.
byte rendLightWallAngleSmooth = true;
float rendSkyLight = .2f; // Intensity factor.
byte rendSkyLightAuto = true;
boolean firstFrameAfterLoad;
boolean ddMapSetup;
/// Notified when the current map changes.
MapChangeAudience audienceForMapChange;
/**
* $smoothmatoffset: Roll the surface material offset tracker buffers.
*/
void R_UpdateSurfaceScroll()
{
if(!theMap) return;
foreach(Surface *surface, theMap->scrollingSurfaces())
{
// X Offset
surface->oldOffset[0][0] = surface->oldOffset[0][1];
surface->oldOffset[0][1] = surface->offset[0];
if(surface->oldOffset[0][0] != surface->oldOffset[0][1])
if(de::abs(surface->oldOffset[0][0] - surface->oldOffset[0][1]) >=
MAX_SMOOTH_MATERIAL_MOVE)
{
// Too fast: make an instantaneous jump.
surface->oldOffset[0][0] = surface->oldOffset[0][1];
}
// Y Offset
surface->oldOffset[1][0] = surface->oldOffset[1][1];
surface->oldOffset[1][1] = surface->offset[1];
if(surface->oldOffset[1][0] != surface->oldOffset[1][1])
if(de::abs(surface->oldOffset[1][0] - surface->oldOffset[1][1]) >=
MAX_SMOOTH_MATERIAL_MOVE)
{
// Too fast: make an instantaneous jump.
surface->oldOffset[1][0] = surface->oldOffset[1][1];
}
}
}
/**
* $smoothmatoffset: interpolate the visual offset.
*/
void R_InterpolateSurfaceScroll(boolean resetNextViewer)
{
if(!theMap) return;
SurfaceSet &surfaces = theMap->scrollingSurfaces();
SurfaceSet::iterator it = surfaces.begin();
while(it != surfaces.end())
{
Surface &suf = **it;
if(resetNextViewer)
{
// Reset the material offset trackers.
// X Offset.
suf.visOffsetDelta[0] = 0;
suf.oldOffset[0][0] = suf.oldOffset[0][1] = suf.offset[0];
// Y Offset.
suf.visOffsetDelta[1] = 0;
suf.oldOffset[1][0] = suf.oldOffset[1][1] = suf.offset[1];
suf.update();
it++;
}
// While the game is paused there is no need to calculate any
// visual material offsets.
else //if(!clientPaused)
{
// Set the visible material offsets.
// X Offset.
suf.visOffsetDelta[0] =
suf.oldOffset[0][0] * (1 - frameTimePos) +
suf.offset[0] * frameTimePos - suf.offset[0];
// Y Offset.
suf.visOffsetDelta[1] =
suf.oldOffset[1][0] * (1 - frameTimePos) +
suf.offset[1] * frameTimePos - suf.offset[1];
// Visible material offset.
suf.visOffset[0] = suf.offset[0] + suf.visOffsetDelta[0];
suf.visOffset[1] = suf.offset[1] + suf.visOffsetDelta[1];
suf.update();
// Has this material reached its destination?
if(suf.visOffset[0] == suf.offset[0] && suf.visOffset[1] == suf.offset[1])
{
it = surfaces.erase(it);
}
else
{
it++;
}
}
}
}
void R_AddTrackedPlane(PlaneSet *plist, Plane *pln)
{
if(!plist || !pln) return;
plist->insert(pln);
}
boolean R_RemoveTrackedPlane(PlaneSet *plist, Plane *pln)
{
if(!plist || !pln) return false;
return plist->remove(pln);
}
/**
* $smoothplane: Roll the height tracker buffers.
*/
void R_UpdateTrackedPlanes()
{
if(!theMap) return;
PlaneSet* plist = GameMap_TrackedPlanes(theMap);
if(!plist) return;
foreach(Plane *plane, *plist)
{
plane->updateHeightTracking();
}
}
/**
* $smoothplane: interpolate the visual offset.
*/
void R_InterpolateTrackedPlanes(boolean resetNextViewer)
{
if(!theMap) return;
PlaneSet* plist = GameMap_TrackedPlanes(theMap);
if(!plist) return;
if(resetNextViewer)
{
// Reset the plane height trackers.
foreach(Plane *plane, *plist)
{
plane->resetVisHeight();
}
// Tracked movement is now all done.
plist->clear();
}
// While the game is paused there is no need to calculate any
// visual plane offsets $smoothplane.
else //if(!clientPaused)
{
// Set the visible offsets.
QMutableSetIterator<Plane *> iter(*plist);
while(iter.hasNext())
{
Plane *plane = iter.next();
plane->lerpVisHeight();
// Has this plane reached its destination?
if(plane->visHeight() == plane->height()) /// @todo Can this fail? (float equality)
{
iter.remove();
}
}
}
}
void R_UpdateMapSurfacesOnMaterialChange(Material *material)
{
if(!material || ddMapSetup) return;
if(!theMap) return;
#ifdef __CLIENT__
foreach(Surface *surface, theMap->decoratedSurfaces())
{
if(material == surface->material)
{
surface->update();
}
}
#endif
}
/**
* Create a new plane for the given sector. The plane will be initialized
* with default values.
*
* Post: The sector's plane list will be replaced, the new plane will be
* linked to the end of the list.
*
* @param sec Sector for which a new plane will be created.
*
* @return Ptr to the newly created plane.
*/
Plane *R_NewPlaneForSector(Sector *sec)
{
if(!sec) return NULL; // Do wha?
// Allocate the new plane.
Plane *plane = new Plane(*sec, de::Vector3f(0, 0, 1));
// Resize this sector's plane list.
sec->planes.append(plane);
plane->_type = Plane::Middle;
plane->_inSectorIndex = sec->planes.size() - 1;
// Initialize the surface.
/// @todo The initial material should be the "unknown" material.
plane->surface().updateBaseOrigin();
#ifdef __CLIENT__
/**
* Resize the biassurface lists for the BSP leaf planes.
* If we are in map setup mode, don't create the biassurfaces now,
* as planes are created before the bias system is available.
*/
if(sec->bspLeafs && *sec->bspLeafs)
{
BspLeaf **ssecIter = sec->bspLeafs;
do
{
BspLeaf *bspLeaf = *ssecIter;
uint n = 0;
biassurface_t **newList = (biassurface_t **) Z_Calloc(sec->planeCount() * sizeof(biassurface_t *), PU_MAP, 0);
// Copy the existing list?
if(bspLeaf->bsuf)
{
for(; n < sec->planeCount() - 1 /* exclude newly added */; ++n)
{
newList[n] = bspLeaf->bsuf[n];
}
Z_Free(bspLeaf->bsuf);
bspLeaf->bsuf = 0;
}
if(!ddMapSetup)
{
biassurface_t *bsuf = SB_CreateSurface();
bsuf->size = Rend_NumFanVerticesForBspLeaf(bspLeaf);
bsuf->illum = (vertexillum_t *) Z_Calloc(sizeof(vertexillum_t) * bsuf->size, PU_MAP, 0);
for(uint i = 0; i < bsuf->size; ++i)
{
SB_InitVertexIllum(&bsuf->illum[i]);
}
newList[n] = bsuf;
}
bspLeaf->bsuf = newList;
ssecIter++;
} while(*ssecIter);
}
#endif
return plane;
}
/**
* Permanently destroys the specified plane of the given sector.
* The sector's plane list is updated accordingly.
*
* @param id The sector, plane id to be destroyed.
* @param sec Ptr to sector for which a plane will be destroyed.
*/
void R_DestroyPlaneOfSector(uint id, Sector *sec)
{
if(!sec) return; // Do wha?
if(id > sec->planeCount())
Con_Error("P_DestroyPlaneOfSector: Plane id #%i is not valid for "
"sector #%u", id, (uint) GET_SECTOR_IDX(sec));
Plane *plane = sec->planes[id];
// If this plane is currently being watched, remove it.
R_RemoveTrackedPlane(GameMap_TrackedPlanes(theMap), plane);
// If this plane's surface is in the moving list, remove it.
theMap->scrollingSurfaces().remove(&plane->surface());
#ifdef __CLIENT__
// If this plane's surface is in the glowing list, remove it.
theMap->glowingSurfaces().remove(&plane->surface());
// If this plane's surface is in the decorated list, remove it.
theMap->decoratedSurfaces().remove(&plane->surface());
// Destroy the biassurfaces for this plane.
for(BspLeaf **bspLeafIter = sec->bspLeafs; *bspLeafIter; bspLeafIter++)
{
BspLeaf *bspLeaf = *bspLeafIter;
DENG2_ASSERT(bspLeaf->bsuf != 0);
SB_DestroySurface(bspLeaf->bsuf[id]);
if(id < sec->planeCount())
{
std::memmove(bspLeaf->bsuf + id, bspLeaf->bsuf + id + 1, sizeof(biassurface_t *));
}
}
#endif // __CLIENT__
// Destroy the specified plane.
sec->planes.removeOne(plane);
delete plane;
}
void GameMap_UpdateSkyFixForSector(GameMap *map, Sector *sec)
{
DENG_ASSERT(map);
if(!sec || 0 == sec->lineDefCount) return;
bool skyFloor = sec->SP_floorsurface.isSkyMasked();
bool skyCeil = sec->SP_ceilsurface.isSkyMasked();
if(!skyFloor && !skyCeil) return;
if(skyCeil)
{
// Adjust for the plane height.
if(sec->SP_ceilvisheight > map->skyFix[Plane::Ceiling].height)
{
// Must raise the skyfix ceiling.
map->skyFix[Plane::Ceiling].height = sec->SP_ceilvisheight;
}
// Check that all the mobjs in the sector fit in.
for(mobj_t *mo = sec->mobjList; mo; mo = mo->sNext)
{
float extent = mo->origin[VZ] + mo->height;
if(extent > map->skyFix[Plane::Ceiling].height)
{
// Must raise the skyfix ceiling.
map->skyFix[Plane::Ceiling].height = extent;
}
}
}
if(skyFloor)
{
// Adjust for the plane height.
if(sec->SP_floorvisheight < map->skyFix[Plane::Floor].height)
{
// Must lower the skyfix floor.
map->skyFix[Plane::Floor].height = sec->SP_floorvisheight;
}
}
// Update for middle textures on two sided linedefs which intersect the
// floor and/or ceiling of their front and/or back sectors.
if(sec->lineDefs && *sec->lineDefs)
{
LineDef **linePtr = sec->lineDefs;
do
{
LineDef *li = *linePtr;
// Must be twosided.
if(li->L_frontsidedef && li->L_backsidedef)
{
SideDef *si = li->L_frontsector == sec? li->L_frontsidedef : li->L_backsidedef;
if(si->SW_middlematerial)
{
if(skyCeil)
{
float top = sec->SP_ceilvisheight + si->SW_middlevisoffset[VY];
if(top > map->skyFix[Plane::Ceiling].height)
{
// Must raise the skyfix ceiling.
map->skyFix[Plane::Ceiling].height = top;
}
}
if(skyFloor)
{
float bottom = sec->SP_floorvisheight +
si->SW_middlevisoffset[VY] - si->SW_middlematerial->height();
if(bottom < map->skyFix[Plane::Floor].height)
{
// Must lower the skyfix floor.
map->skyFix[Plane::Floor].height = bottom;
}
}
}
}
linePtr++;
} while(*linePtr);
}
}
void GameMap_InitSkyFix(GameMap *map)
{
DENG_ASSERT(map);
map->skyFix[Plane::Floor].height = DDMAXFLOAT;
map->skyFix[Plane::Ceiling].height = DDMINFLOAT;
// Update for sector plane heights and mobjs which intersect the ceiling.
for(uint i = 0; i < map->sectorCount(); ++i)
{
GameMap_UpdateSkyFixForSector(map, &map->sectors[i]);
}
}
/**
* @return Lineowner for this line for this vertex; otherwise @c 0.
*/
LineOwner *R_GetVtxLineOwner(Vertex const *v, LineDef const *line)
{
if(v == line->L_v1)
return line->L_vo1;
if(v == line->L_v2)
return line->L_vo2;
return 0;
}
#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_OrderVertices(LineDef const *line, Sector const *sector, Vertex *verts[2])
{
byte edge = (sector == line->L_frontsector? 0:1);
verts[0] = line->L_v(edge);
verts[1] = line->L_v(edge^1);
}
boolean R_FindBottomTop2(SideDefSection section, int lineFlags,
Sector *frontSec, Sector *backSec, SideDef *frontDef, SideDef *backDef,
coord_t *low, coord_t *hi, float matOffset[2])
{
bool const unpegBottom = !!(lineFlags & DDLF_DONTPEGBOTTOM);
bool const unpegTop = !!(lineFlags & DDLF_DONTPEGTOP);
// Single sided?
if(!frontSec || !backSec || !backDef/*front side of a "window"*/)
{
*low = frontSec->SP_floorvisheight;
*hi = frontSec->SP_ceilvisheight;
if(matOffset)
{
Surface *suf = &frontDef->SW_middlesurface;
matOffset[0] = suf->visOffset[0];
matOffset[1] = suf->visOffset[1];
if(unpegBottom)
{
matOffset[1] -= *hi - *low;
}
}
}
else
{
boolean const stretchMiddle = !!(frontDef->flags & SDF_MIDDLE_STRETCH);
Plane *ffloor = &frontSec->floor();
Plane *fceil = &frontSec->ceiling();
Plane *bfloor = &backSec->floor();
Plane *bceil = &backSec->ceiling();
Surface *suf = &frontDef->SW_surface(section);
switch(section)
{
case SS_TOP:
// Can't go over front ceiling (would induce geometry flaws).
if(bceil->visHeight() < ffloor->visHeight())
*low = ffloor->visHeight();
else
*low = bceil->visHeight();
*hi = fceil->visHeight();
if(matOffset)
{
matOffset[0] = suf->visOffset[0];
matOffset[1] = suf->visOffset[1];
if(!unpegTop)
{
// Align with normal middle texture.
matOffset[1] -= fceil->visHeight() - bceil->visHeight();
}
}
break;
case SS_BOTTOM: {
bool const raiseToBackFloor = (fceil->surface().isSkyMasked() && bceil->surface().isSkyMasked() &&
fceil->visHeight() < bceil->visHeight() &&
bfloor->visHeight() > fceil->visHeight());
coord_t t = bfloor->visHeight();
*low = 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();
*hi = t;
if(matOffset)
{
matOffset[0] = suf->visOffset[0];
matOffset[1] = suf->visOffset[1];
if(bfloor->visHeight() > fceil->visHeight())
{
matOffset[1] -= (raiseToBackFloor? t : fceil->visHeight()) - bfloor->visHeight();
}
if(unpegBottom)
{
// Align with normal middle texture.
matOffset[1] += (raiseToBackFloor? t : fceil->visHeight()) - bfloor->visHeight();
}
}
break; }
case SS_MIDDLE:
*low = MAX_OF(bfloor->visHeight(), ffloor->visHeight());
*hi = MIN_OF(bceil->visHeight(), fceil->visHeight());
if(matOffset)
{
matOffset[0] = suf->visOffset[0];
matOffset[1] = 0;
}
if(suf->material && !stretchMiddle)
{
bool const clipBottom = !(!(devRendSkyMode || P_IsInVoid(viewPlayer)) && ffloor->surface().isSkyMasked() && bfloor->surface().isSkyMasked());
bool const clipTop = !(!(devRendSkyMode || P_IsInVoid(viewPlayer)) && fceil->surface().isSkyMasked() && bceil->surface().isSkyMasked());
coord_t const openBottom = *low;
coord_t const openTop = *hi;
int const matHeight = suf->material->height();
coord_t const matYOffset = suf->visOffset[VY];
if(openTop > openBottom)
{
if(unpegBottom)
{
*low += matYOffset;
*hi = *low + matHeight;
}
else
{
*hi += matYOffset;
*low = *hi - matHeight;
}
if(matOffset && *hi > openTop)
{
matOffset[1] = *hi - openTop;
}
// Clip it?
if(clipTop || clipBottom)
{
if(clipBottom && *low < openBottom)
*low = openBottom;
if(clipTop && *hi > openTop)
*hi = openTop;
}
if(matOffset && !clipTop)
{
matOffset[1] = 0;
}
}
}
break;
}
}
return /*is_visible=*/ *hi > *low;
}
boolean R_FindBottomTop(SideDefSection section, int lineFlags,
Sector *frontSec, Sector *backSec, SideDef *frontDef, SideDef *backDef,
coord_t *low, coord_t *hi)
{
return R_FindBottomTop2(section, lineFlags, frontSec, backSec, frontDef, backDef,
low, hi, 0/*offset not needed*/);
}
coord_t R_OpenRange(Sector const *frontSec, Sector const *backSec, coord_t *retBottom, coord_t *retTop)
{
DENG_ASSERT(frontSec);
coord_t top;
if(backSec && backSec->SP_ceilheight < frontSec->SP_ceilheight)
{
top = backSec->SP_ceilheight;
}
else
{
top = frontSec->SP_ceilheight;
}
coord_t bottom;
if(backSec && backSec->SP_floorheight > frontSec->SP_floorheight)
{
bottom = backSec->SP_floorheight;
}
else
{
bottom = frontSec->SP_floorheight;
}
if(retBottom) *retBottom = bottom;
if(retTop) *retTop = top;
return top - bottom;
}
coord_t R_VisOpenRange(Sector const *frontSec, Sector const *backSec, coord_t *retBottom, coord_t *retTop)
{
DENG_ASSERT(frontSec);
coord_t top;
if(backSec && backSec->SP_ceilvisheight < frontSec->SP_ceilvisheight)
{
top = backSec->SP_ceilvisheight;
}
else
{
top = frontSec->SP_ceilvisheight;
}
coord_t bottom;
if(backSec && backSec->SP_floorvisheight > frontSec->SP_floorvisheight)
{
bottom = backSec->SP_floorvisheight;
}
else
{
bottom = frontSec->SP_floorvisheight;
}
if(retBottom) *retBottom = bottom;
if(retTop) *retTop = top;
return top - bottom;
}
#ifdef __CLIENT__
boolean R_MiddleMaterialCoversOpening(int lineFlags, Sector *frontSec, Sector *backSec,
SideDef *frontDef, SideDef *backDef, boolean ignoreOpacity)
{
if(!frontSec || !frontDef) return false; // Never.
Material *material = frontDef->SW_middlematerial;
if(!material) return false;
// Ensure we have up to date info about the material.
MaterialSnapshot const &ms = material->prepare(Rend_MapSurfaceMaterialSpec());
if(ignoreOpacity || (ms.isOpaque() && !frontDef->SW_middleblendmode && frontDef->SW_middlergba[3] >= 1))
{
coord_t openRange, openBottom, openTop;
// Stretched middles always cover the opening.
if(frontDef->flags & SDF_MIDDLE_STRETCH) return true;
// Might the material cover the opening?
openRange = R_VisOpenRange(frontSec, backSec, &openBottom, &openTop);
if(ms.height() >= openRange)
{
// Possibly; check the placement.
coord_t bottom, top;
if(R_FindBottomTop(SS_MIDDLE, lineFlags, frontSec, backSec, frontDef, backDef,
&bottom, &top))
{
return (top >= openTop && bottom <= openBottom);
}
}
}
return false;
}
boolean R_MiddleMaterialCoversLineOpening(LineDef *line, int side, boolean ignoreOpacity)
{
DENG_ASSERT(line);
Sector *frontSec = line->L_sector(side);
Sector *backSec = line->L_sector(side ^ 1);
SideDef *frontDef = line->L_sidedef(side);
SideDef *backDef = line->L_sidedef(side ^ 1);
return R_MiddleMaterialCoversOpening(line->flags, frontSec, backSec, frontDef, backDef, ignoreOpacity);
}
LineDef *R_FindLineNeighbor(Sector const *sector, LineDef const *line,
LineOwner const *own, boolean antiClockwise, binangle_t *diff)
{
LineOwner const *cown = antiClockwise? &own->prev() : &own->next();
LineDef *other = &cown->lineDef();
if(other == line)
return NULL;
if(diff) *diff += (antiClockwise? cown->angle() : own->angle());
if(!other->L_backsidedef || other->L_frontsector != other->L_backsector)
{
if(sector) // Must one of the sectors match?
{
if(other->L_frontsector == sector ||
(other->L_backsidedef && other->L_backsector == sector))
return other;
}
else
{
return other;
}
}
// Not suitable, try the next.
return R_FindLineNeighbor(sector, line, cown, antiClockwise, diff);
}
LineDef *R_FindSolidLineNeighbor(Sector const *sector, LineDef const *line,
LineOwner const *own, boolean antiClockwise, binangle_t *diff)
{
LineOwner const *cown = antiClockwise? &own->prev() : &own->next();
LineDef *other = &cown->lineDef();
int side;
if(other == line) return NULL;
if(diff) *diff += (antiClockwise? cown->angle() : own->angle());
if(!((other->inFlags & LF_BSPWINDOW) && other->L_frontsector != sector))
{
if(!other->L_frontsidedef || !other->L_backsidedef)
return other;
if(!LINE_SELFREF(other) &&
(other->L_frontsector->SP_floorvisheight >= sector->SP_ceilvisheight ||
other->L_frontsector->SP_ceilvisheight <= sector->SP_floorvisheight ||
other->L_backsector->SP_floorvisheight >= sector->SP_ceilvisheight ||
other->L_backsector->SP_ceilvisheight <= sector->SP_floorvisheight ||
other->L_backsector->SP_ceilvisheight <= other->L_backsector->SP_floorvisheight))
return other;
// Both front and back MUST be open by this point.
// Check for mid texture which fills the gap between floor and ceiling.
// We should not give away the location of false walls (secrets).
side = (other->L_frontsector == sector? 0 : 1);
if(other->L_sidedef(side)->SW_middlematerial)
{
float oFCeil = other->L_frontsector->SP_ceilvisheight;
float oFFloor = other->L_frontsector->SP_floorvisheight;
float oBCeil = other->L_backsector->SP_ceilvisheight;
float oBFloor = other->L_backsector->SP_floorvisheight;
if((side == 0 &&
((oBCeil > sector->SP_floorvisheight &&
oBFloor <= sector->SP_floorvisheight) ||
(oBFloor < sector->SP_ceilvisheight &&
oBCeil >= sector->SP_ceilvisheight) ||
(oBFloor < sector->SP_ceilvisheight &&
oBCeil > sector->SP_floorvisheight))) ||
( /* side must be 1 */
((oFCeil > sector->SP_floorvisheight &&
oFFloor <= sector->SP_floorvisheight) ||
(oFFloor < sector->SP_ceilvisheight &&
oFCeil >= sector->SP_ceilvisheight) ||
(oFFloor < sector->SP_ceilvisheight &&
oFCeil > sector->SP_floorvisheight))) )
{
if(!R_MiddleMaterialCoversLineOpening(other, side, false))
return 0;
}
}
}
// Not suitable, try the next.
return R_FindSolidLineNeighbor(sector, line, cown, antiClockwise, diff);
}
LineDef *R_FindLineBackNeighbor(Sector const *sector, LineDef const *line,
LineOwner const *own, boolean antiClockwise, binangle_t *diff)
{
LineOwner const *cown = antiClockwise? &own->prev() : &own->next();
LineDef *other = &cown->lineDef();
if(other == line) return 0;
if(diff) *diff += (antiClockwise? cown->angle() : own->angle());
if(!other->L_backsidedef || other->L_frontsector != other->L_backsector ||
(other->inFlags & LF_BSPWINDOW))
{
if(!(other->L_frontsector == sector ||
(other->L_backsidedef && other->L_backsector == sector)))
return other;
}
// Not suitable, try the next.
return R_FindLineBackNeighbor(sector, line, cown, antiClockwise, diff);
}
LineDef *R_FindLineAlignNeighbor(Sector const *sec, LineDef const *line,
LineOwner const *own, boolean antiClockwise, int alignment)
{
int const SEP = 10;
LineOwner const *cown = antiClockwise? &own->prev() : &own->next();
LineDef *other = &cown->lineDef();
binangle_t diff;
if(other == line)
return NULL;
if(!LINE_SELFREF(other))
{
diff = line->angle - other->angle;
if(alignment < 0)
diff -= BANG_180;
if(other->L_frontsector != sec)
diff -= BANG_180;
if(diff < SEP || diff > BANG_360 - SEP)
return other;
}
// Can't step over non-twosided lines.
if((!other->L_backsidedef || !other->L_frontsidedef))
return NULL;
// Not suitable, try the next.
return R_FindLineAlignNeighbor(sec, line, cown, antiClockwise, alignment);
}
#endif // __CLIENT__
static inline void initSurfaceMaterialOffset(Surface *suf)
{
DENG_ASSERT(suf);
suf->visOffset[VX] = suf->oldOffset[0][VX] = suf->oldOffset[1][VX] = suf->offset[VX];
suf->visOffset[VY] = suf->oldOffset[0][VY] = suf->oldOffset[1][VY] = suf->offset[VY];
}
/**
* Set intial values of various tracked and interpolated properties
* (lighting, smoothed planes etc).
*/
void R_MapInitSurfaces(boolean forceUpdate)
{
if(novideo) return;
for(uint i = 0; i < NUM_SECTORS; ++i)
{
Sector *sec = SECTOR_PTR(i);
R_UpdateSector(sec, forceUpdate);
for(uint j = 0; j < sec->planeCount(); ++j)
{
Plane *pln = sec->SP_plane(j);
pln->_visHeight = pln->_oldHeight[0] = pln->_oldHeight[1] = pln->_height;
initSurfaceMaterialOffset(&pln->surface());
}
}
for(uint i = 0; i < NUM_SIDEDEFS; ++i)
{
SideDef *si = SIDE_PTR(i);
initSurfaceMaterialOffset(&si->SW_topsurface);
initSurfaceMaterialOffset(&si->SW_middlesurface);
initSurfaceMaterialOffset(&si->SW_bottomsurface);
}
}
#ifdef __CLIENT__
static void addToSurfaceSets(Surface *suf, Material *material)
{
if(!suf || !material) return;
if(material->hasGlow())
{
theMap->glowingSurfaces().insert(suf);
}
if(material->isDecorated())
{
theMap->decoratedSurfaces().insert(suf);
}
}
#endif // __CLIENT__
void R_MapInitSurfaceLists()
{
#ifdef __CLIENT__
theMap->decoratedSurfaces().clear();
theMap->glowingSurfaces().clear();
for(uint i = 0; i < NUM_SIDEDEFS; ++i)
{
SideDef *side = SIDE_PTR(i);
addToSurfaceSets(&side->SW_middlesurface, side->SW_middlematerial);
addToSurfaceSets(&side->SW_topsurface, side->SW_topmaterial);
addToSurfaceSets(&side->SW_bottomsurface, side->SW_bottommaterial);
}
for(uint i = 0; i < NUM_SECTORS; ++i)
{
Sector *sec = SECTOR_PTR(i);
if(!sec->lineDefCount) continue;
for(uint j = 0; j < sec->planeCount(); ++j)
{