/
r_world.c
1751 lines (1450 loc) · 48.9 KB
/
r_world.c
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.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*
* 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
*/
/**
* World Setup and Refresh.
*/
// HEADER FILES ------------------------------------------------------------
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_play.h"
#include "de_refresh.h"
#include "de_render.h"
#include "de_graphics.h"
#include "de_audio.h"
#include "de_misc.h"
#include "materialvariant.h"
// MACROS ------------------------------------------------------------------
// $smoothplane: Maximum speed for a smoothed plane.
#define MAX_SMOOTH_PLANE_MOVE (64)
// $smoothmatoffset: Maximum speed for a smoothed material offset.
#define MAX_SMOOTH_MATERIAL_MOVE (8)
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
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;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static surfacelistnode_t* unusedSurfaceListNodes = 0;
// CODE --------------------------------------------------------------------
/**
* Allocate a new surface list node.
*/
static surfacelistnode_t* allocListNode(void)
{
surfacelistnode_t* node = Z_Calloc(sizeof(*node), PU_APPSTATIC, 0);
return node;
}
#if 0
/**
* Free all memory acquired for the given surface list node.
*
* @todo This function is never called anywhere?
*/
static void freeListNode(surfacelistnode_t* node)
{
if(node)
Z_Free(node);
}
#endif
surfacelistnode_t* R_SurfaceListNodeCreate(void)
{
surfacelistnode_t* node;
// Is there a free node in the unused list?
if(unusedSurfaceListNodes)
{
node = unusedSurfaceListNodes;
unusedSurfaceListNodes = node->next;
}
else
{
node = allocListNode();
}
node->data = NULL;
node->next = NULL;
return node;
}
void R_SurfaceListNodeDestroy(surfacelistnode_t* node)
{
// Move it to the list of unused nodes.
node->data = NULL;
node->next = unusedSurfaceListNodes;
unusedSurfaceListNodes = node;
}
void R_SurfaceListAdd(surfacelist_t* sl, Surface* suf)
{
surfacelistnode_t* node;
if(!sl || !suf)
return;
// Check whether this surface is already in the list.
node = sl->head;
while(node)
{
if((Surface*) node->data == suf)
return; // Yep.
node = node->next;
}
// Not found, add it to the list.
node = R_SurfaceListNodeCreate();
node->data = suf;
node->next = sl->head;
sl->head = node;
sl->num++;
}
boolean R_SurfaceListRemove(surfacelist_t* sl, const Surface* suf)
{
surfacelistnode_t* last, *n;
if(!sl || !suf)
return false;
last = sl->head;
if(last)
{
n = last->next;
while(n)
{
if((Surface*) n->data == suf)
{
last->next = n->next;
R_SurfaceListNodeDestroy(n);
sl->num--;
return true;
}
last = n;
n = n->next;
}
}
return false;
}
void R_SurfaceListClear(surfacelist_t* sl)
{
if(sl)
{
surfacelistnode_t* node, *next;
node = sl->head;
while(node)
{
next = node->next;
R_SurfaceListRemove(sl, (Surface*)node->data);
node = next;
}
}
}
boolean R_SurfaceListIterate(surfacelist_t* sl, boolean (*callback) (Surface* suf, void*),
void* context)
{
boolean result = true;
surfacelistnode_t* n, *np;
if(sl)
{
n = sl->head;
while(n)
{
np = n->next;
if((result = callback((Surface*) n->data, context)) == 0)
break;
n = np;
}
}
return result;
}
boolean updateSurfaceScroll(Surface* suf, void* context)
{
// X Offset
suf->oldOffset[0][0] = suf->oldOffset[0][1];
suf->oldOffset[0][1] = suf->offset[0];
if(suf->oldOffset[0][0] != suf->oldOffset[0][1])
if(fabs(suf->oldOffset[0][0] - suf->oldOffset[0][1]) >=
MAX_SMOOTH_MATERIAL_MOVE)
{
// Too fast: make an instantaneous jump.
suf->oldOffset[0][0] = suf->oldOffset[0][1];
}
// Y Offset
suf->oldOffset[1][0] = suf->oldOffset[1][1];
suf->oldOffset[1][1] = suf->offset[1];
if(suf->oldOffset[1][0] != suf->oldOffset[1][1])
if(fabs(suf->oldOffset[1][0] - suf->oldOffset[1][1]) >=
MAX_SMOOTH_MATERIAL_MOVE)
{
// Too fast: make an instantaneous jump.
suf->oldOffset[1][0] = suf->oldOffset[1][1];
}
return true;
}
/**
* $smoothmatoffset: Roll the surface material offset tracker buffers.
*/
void R_UpdateSurfaceScroll(void)
{
surfacelist_t* slist;
if(!theMap) return;
slist = GameMap_ScrollingSurfaces(theMap);
if(!slist) return;
R_SurfaceListIterate(slist, updateSurfaceScroll, NULL);
}
boolean resetSurfaceScroll(Surface* suf, void* context)
{
// 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];
Surface_Update(suf);
/// @fixme Do not assume surface is from the CURRENT map.
R_SurfaceListRemove(GameMap_ScrollingSurfaces(theMap), suf);
return true;
}
boolean interpSurfaceScroll(Surface* suf, void* context)
{
// 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];
Surface_Update(suf);
// Has this material reached its destination?
if(suf->visOffset[0] == suf->offset[0] && suf->visOffset[1] == suf->offset[1])
{
/// @fixme Do not assume surface is from the CURRENT map.
R_SurfaceListRemove(GameMap_ScrollingSurfaces(theMap), suf);
}
return true;
}
/**
* $smoothmatoffset: interpolate the visual offset.
*/
void R_InterpolateSurfaceScroll(boolean resetNextViewer)
{
surfacelist_t* slist;
if(!theMap) return;
slist = GameMap_ScrollingSurfaces(theMap);
if(!slist) return;
if(resetNextViewer)
{
// Reset the material offset trackers.
R_SurfaceListIterate(slist, resetSurfaceScroll, NULL);
}
// While the game is paused there is no need to calculate any
// visual material offsets.
else //if(!clientPaused)
{
// Set the visible material offsets.
R_SurfaceListIterate(slist, interpSurfaceScroll, NULL);
}
}
void R_AddTrackedPlane(planelist_t* plist, Plane *pln)
{
uint i;
if(!plist || !pln) return;
// Check whether we are already tracking this plane.
for(i = 0; i < plist->num; ++i)
{
if(plist->array[i] == pln)
return; // Yes we are.
}
plist->num++;
// Only allocate memory when it's needed.
if(plist->num > plist->maxNum)
{
plist->maxNum *= 2;
// The first time, allocate 8 watched plane nodes.
if(!plist->maxNum)
plist->maxNum = 8;
plist->array = Z_Realloc(plist->array, sizeof(Plane*) * (plist->maxNum + 1), PU_MAP);
}
// Add the plane to the list.
plist->array[plist->num-1] = pln;
plist->array[plist->num] = NULL; // Terminate.
}
boolean R_RemoveTrackedPlane(planelist_t *plist, const Plane *pln)
{
uint i;
if(!plist || !pln)
return false;
for(i = 0; i < plist->num; ++i)
{
if(plist->array[i] == pln)
{
if(i == plist->num - 1)
plist->array[i] = NULL;
else
memmove(&plist->array[i], &plist->array[i+1],
sizeof(Plane*) * (plist->num - 1 - i));
plist->num--;
return true;
}
}
return false;
}
/**
* $smoothplane: Roll the height tracker buffers.
*/
void R_UpdateTrackedPlanes(void)
{
planelist_t* plist;
uint i;
if(!theMap) return;
plist = GameMap_TrackedPlanes(theMap);
if(!plist) return;
for(i = 0; i < plist->num; ++i)
{
Plane* pln = plist->array[i];
pln->oldHeight[0] = pln->oldHeight[1];
pln->oldHeight[1] = pln->height;
if(pln->oldHeight[0] != pln->oldHeight[1])
if(fabs(pln->oldHeight[0] - pln->oldHeight[1]) >=
MAX_SMOOTH_PLANE_MOVE)
{
// Too fast: make an instantaneous jump.
pln->oldHeight[0] = pln->oldHeight[1];
}
}
}
/**
* $smoothplane: interpolate the visual offset.
*/
void R_InterpolateTrackedPlanes(boolean resetNextViewer)
{
planelist_t* plist;
Plane* pln;
uint i;
if(!theMap) return;
plist = GameMap_TrackedPlanes(theMap);
if(!plist) return;
if(resetNextViewer)
{
// $smoothplane: Reset the plane height trackers.
for(i = 0; i < plist->num; ++i)
{
pln = plist->array[i];
pln->visHeightDelta = 0;
pln->visHeight = pln->oldHeight[0] = pln->oldHeight[1] = pln->height;
if(pln->type == PLN_FLOOR || pln->type == PLN_CEILING)
{
R_MarkDependantSurfacesForDecorationUpdate(pln);
}
if(R_RemoveTrackedPlane(plist, pln))
i = (i > 0? i-1 : 0);
}
}
// While the game is paused there is no need to calculate any
// visual plane offsets $smoothplane.
else //if(!clientPaused)
{
// $smoothplane: Set the visible offsets.
for(i = 0; i < plist->num; ++i)
{
pln = plist->array[i];
pln->visHeightDelta = pln->oldHeight[0] * (1 - frameTimePos) +
pln->height * frameTimePos -
pln->height;
// Visible plane height.
pln->visHeight = pln->height + pln->visHeightDelta;
if(pln->type == PLN_FLOOR || pln->type == PLN_CEILING)
{
R_MarkDependantSurfacesForDecorationUpdate(pln);
}
// Has this plane reached its destination?
if(pln->visHeight == pln->height) /// @todo Can this fail? (float equality)
{
if(R_RemoveTrackedPlane(plist, pln))
i = (i > 0? i-1 : 0);
}
}
}
}
/**
* Called when a floor or ceiling height changes to update the plotted
* decoration origins for surfaces whose material offset is dependant upon
* the given plane.
*/
void R_MarkDependantSurfacesForDecorationUpdate(Plane* pln)
{
LineDef** linep;
if(!pln || !pln->sector->lineDefs)
return;
// Mark the decor lights on the sides of this plane as requiring
// an update.
linep = pln->sector->lineDefs;
while(*linep)
{
LineDef* li = *linep;
if(!li->L_backside)
{
if(pln->type != PLN_MID)
Surface_Update(&li->L_frontside->SW_surface(SS_MIDDLE));
}
else if(li->L_backsector != li->L_frontsector)
{
byte side =
(li->L_frontsector == pln->sector? FRONT : BACK);
Surface_Update(&li->L_side(side)->SW_surface(SS_BOTTOM));
Surface_Update(&li->L_side(side)->SW_surface(SS_TOP));
if(pln->type == PLN_FLOOR)
Surface_Update(&li->L_side(side^1)->SW_surface(SS_BOTTOM));
else
Surface_Update(&li->L_side(side^1)->SW_surface(SS_TOP));
}
linep++;
}
}
static boolean markSurfaceForDecorationUpdate(Surface* surface, void* paramaters)
{
material_t* material = (material_t*) paramaters;
if(material == surface->material)
{
Surface_Update(surface);
}
return 1; // Continue iteration.
}
void R_UpdateMapSurfacesOnMaterialChange(material_t* material)
{
surfacelist_t* slist;
if(!material || !theMap || ddMapSetup) return;
slist = GameMap_DecoratedSurfaces(theMap);
if(!slist) return;
// Light decorations will need a refresh.
R_SurfaceListIterate(slist, markSurfaceForDecorationUpdate, material);
}
/**
* 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)
{
Surface* suf;
Plane* plane;
if(!sec)
return NULL; // Do wha?
// Allocate the new plane.
plane = Z_Malloc(sizeof(Plane), PU_MAP, 0);
// Resize this sector's plane list.
sec->planes = Z_Realloc(sec->planes, sizeof(Plane*) * (++sec->planeCount + 1), PU_MAP);
// Add the new plane to the end of the list.
sec->planes[sec->planeCount-1] = plane;
sec->planes[sec->planeCount] = NULL; // Terminate.
// Setup header for DMU.
plane->header.type = DMU_PLANE;
// Initalize the plane.
plane->sector = sec;
plane->height = plane->oldHeight[0] = plane->oldHeight[1] = 0;
plane->visHeight = plane->visHeightDelta = 0;
V3d_Copy(plane->PS_base.origin, sec->base.origin);
memset(&plane->PS_base.thinker, 0, sizeof(plane->PS_base.thinker));
plane->speed = 0;
plane->target = 0;
plane->type = PLN_MID;
plane->planeID = sec->planeCount-1;
// Initialize the surface.
memset(&plane->surface, 0, sizeof(plane->surface));
suf = &plane->surface;
suf->header.type = DMU_SURFACE; // Setup header for DMU.
suf->normal[VZ] = 1;
V3f_BuildTangents(suf->tangent, suf->bitangent, suf->normal);
suf->owner = (void*) plane;
/// @todo The initial material should be the "unknown" material.
Surface_SetMaterial(suf, NULL);
Surface_SetMaterialOrigin(suf, 0, 0);
Surface_SetColorAndAlpha(suf, 1, 1, 1, 1);
Surface_SetBlendMode(suf, BM_NORMAL);
/**
* 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;
biassurface_t** newList;
uint n = 0;
newList = Z_Calloc(sec->planeCount * sizeof(biassurface_t*), PU_MAP, NULL);
// Copy the existing list?
if(bspLeaf->bsuf)
{
for(; n < sec->planeCount - 1; ++n)
{
newList[n] = bspLeaf->bsuf[n];
}
Z_Free(bspLeaf->bsuf);
}
if(!ddMapSetup)
{
biassurface_t* bsuf = SB_CreateSurface();
bsuf->size = BspLeaf_NumFanVertices(bspLeaf);
bsuf->illum = Z_Calloc(sizeof(vertexillum_t) * bsuf->size, PU_MAP, 0);
{ uint i;
for(i = 0; i < bsuf->size; ++i)
SB_InitVertexIllum(&bsuf->illum[i]);
}
newList[n] = bsuf;
}
bspLeaf->bsuf = newList;
ssecIter++;
} while(*ssecIter);
}
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)
{
Plane* plane, **newList = NULL;
BspLeaf** ssecIter;
surfacelist_t* slist;
planelist_t* plist;
uint i;
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 = sec->planes[id];
// Create a new plane list?
if(sec->planeCount > 1)
{
uint n;
newList = Z_Malloc(sizeof(Plane**) * sec->planeCount, PU_MAP, 0);
// Copy ptrs to the planes.
n = 0;
for(i = 0; i < sec->planeCount; ++i)
{
if(i == id) continue;
newList[n++] = sec->planes[i];
}
newList[n] = NULL; // Terminate.
}
// If this plane is currently being watched, remove it.
plist = GameMap_TrackedPlanes(theMap);
if(plist) R_RemoveTrackedPlane(plist, plane);
// If this plane's surface is in the moving list, remove it.
slist = GameMap_ScrollingSurfaces(theMap);
if(slist) R_SurfaceListRemove(slist, &plane->surface);
// If this plane's surface is in the deocrated list, remove it.
slist = GameMap_DecoratedSurfaces(theMap);
if(slist) R_SurfaceListRemove(slist, &plane->surface);
// If this plane's surface is in the glowing list, remove it.
slist = GameMap_GlowingSurfaces(theMap);
if(slist) R_SurfaceListRemove(slist, &plane->surface);
// Destroy the biassurfaces for this plane.
ssecIter = sec->bspLeafs;
while(*ssecIter)
{
BspLeaf* bspLeaf = *ssecIter;
SB_DestroySurface(bspLeaf->bsuf[id]);
if(id < sec->planeCount)
memmove(bspLeaf->bsuf + id, bspLeaf->bsuf + id + 1, sizeof(biassurface_t*));
ssecIter++;
}
// Destroy the specified plane.
Z_Free(plane);
sec->planeCount--;
// Link the new list to the sector.
Z_Free(sec->planes);
sec->planes = newList;
}
surfacedecor_t* R_CreateSurfaceDecoration(Surface* suf)
{
surfacedecor_t* d, *s, *decorations;
uint i;
if(!suf) return NULL;
decorations = Z_Malloc(sizeof(*decorations) * (++suf->numDecorations), PU_MAP, 0);
if(suf->numDecorations > 1)
{
// Copy the existing decorations.
for(i = 0; i < suf->numDecorations - 1; ++i)
{
d = &decorations[i];
s = &suf->decorations[i];
memcpy(d, s, sizeof(*d));
}
Z_Free(suf->decorations);
}
// Add the new decoration.
d = &decorations[suf->numDecorations - 1];
suf->decorations = decorations;
return d;
}
void R_ClearSurfaceDecorations(Surface* suf)
{
if(!suf) return;
if(suf->decorations)
Z_Free(suf->decorations);
suf->decorations = NULL;
suf->numDecorations = 0;
}
void GameMap_UpdateSkyFixForSector(GameMap* map, Sector* sec)
{
boolean skyFloor, skyCeil;
assert(map);
if(!sec || 0 == sec->lineDefCount) return;
skyFloor = Surface_IsSkyMasked(&sec->SP_floorsurface);
skyCeil = Surface_IsSkyMasked(&sec->SP_ceilsurface);
if(!skyFloor && !skyCeil) return;
if(skyCeil)
{
mobj_t* mo;
// Adjust for the plane height.
if(sec->SP_ceilvisheight > map->skyFix[PLN_CEILING].height)
{
// Must raise the skyfix ceiling.
map->skyFix[PLN_CEILING].height = sec->SP_ceilvisheight;
}
// Check that all the mobjs in the sector fit in.
for(mo = sec->mobjList; mo; mo = mo->sNext)
{
float extent = mo->origin[VZ] + mo->height;
if(extent > map->skyFix[PLN_CEILING].height)
{
// Must raise the skyfix ceiling.
map->skyFix[PLN_CEILING].height = extent;
}
}
}
if(skyFloor)
{
// Adjust for the plane height.
if(sec->SP_floorvisheight < map->skyFix[PLN_FLOOR].height)
{
// Must lower the skyfix floor.
map->skyFix[PLN_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_frontside && li->L_backside)
{
SideDef* si = li->L_frontsector == sec? li->L_frontside : li->L_backside;
if(si->SW_middlematerial)
{
if(skyCeil)
{
float top = sec->SP_ceilvisheight + si->SW_middlevisoffset[VY];
if(top > map->skyFix[PLN_CEILING].height)
{
// Must raise the skyfix ceiling.
map->skyFix[PLN_CEILING].height = top;
}
}
if(skyFloor)
{
float bottom = sec->SP_floorvisheight +
si->SW_middlevisoffset[VY] - Material_Height(si->SW_middlematerial);
if(bottom < map->skyFix[PLN_FLOOR].height)
{
// Must lower the skyfix floor.
map->skyFix[PLN_FLOOR].height = bottom;
}
}
}
}
linePtr++;
} while(*linePtr);
}
}
void GameMap_InitSkyFix(GameMap* map)
{
uint i;
assert(map);
map->skyFix[PLN_FLOOR].height = DDMAXFLOAT;
map->skyFix[PLN_CEILING].height = DDMINFLOAT;
// Update for sector plane heights and mobjs which intersect the ceiling.
for(i = 0; i < map->numSectors; ++i)
{
GameMap_UpdateSkyFixForSector(map, map->sectors + i);
}
}
/**
* @return Ptr to the lineowner for this line for this vertex
* else @c NULL.
*/
lineowner_t* R_GetVtxLineOwner(const Vertex *v, const LineDef *line)
{
if(v == line->L_v1)
return line->L_vo1;
if(v == line->L_v2)
return line->L_vo2;
return NULL;
}
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);
}
void R_SetupFogDefaults(void)
{
// Go with the defaults.
Con_Execute(CMDS_DDAY,"fog off", true, false);
}
/**
* Returns pointers to the line's vertices in such a fashion that verts[0]
* is the leftmost vertex and verts[1] is the rightmost vertex, when the
* line lies at the edge of `sector.'
*/
void R_OrderVertices(const LineDef *line, const Sector *sector, Vertex *verts[2])
{
byte edge;
edge = (sector == line->L_frontsector? 0:1);
verts[0] = line->L_v(edge);
verts[1] = line->L_v(edge^1);
}
/**
* A neighbour is a line that shares a vertex with 'line', and faces the
* specified sector.
*/
LineDef *R_FindLineNeighbor(const Sector *sector, const LineDef *line,
const lineowner_t *own, boolean antiClockwise,
binangle_t *diff)
{
lineowner_t *cown = own->link[!antiClockwise];
LineDef *other = cown->lineDef;
if(other == line)
return NULL;
if(diff) *diff += (antiClockwise? cown->angle : own->angle);
if(!other->L_backside || other->L_frontsector != other->L_backsector)
{
if(sector) // Must one of the sectors match?
{
if(other->L_frontsector == sector ||
(other->L_backside && 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(const Sector* sector, const LineDef* line,
const lineowner_t* own, boolean antiClockwise, binangle_t* diff)
{
lineowner_t* cown = own->link[!antiClockwise];
LineDef* other = cown->lineDef;
int side;
if(other == line) return NULL;
if(diff) *diff += (antiClockwise? cown->angle : own->angle);
if(!(other->buildData.windowEffect && other->L_frontsector != sector))
{
if(!other->L_frontside || !other->L_backside)
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->sideDefs[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) ||