forked from bnoordhuis/bspc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_hl.c
1114 lines (1065 loc) · 30.8 KB
/
map_hl.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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "qbsp.h"
#include "l_bsp_hl.h"
#include "aas_map.h" //AAS_CreateMapBrushes
int hl_numbrushes;
int hl_numclipbrushes;
//#define HL_PRINT
#define HLCONTENTS
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int HL_TextureContents(char *name)
{
if (!Q_strncasecmp (name, "sky",3))
return CONTENTS_SOLID;
if (!Q_strncasecmp(name+1,"!lava",5))
return CONTENTS_LAVA;
if (!Q_strncasecmp(name+1,"!slime",6))
return CONTENTS_SLIME;
/*
if (!Q_strncasecmp (name, "!cur_90",7))
return CONTENTS_CURRENT_90;
if (!Q_strncasecmp (name, "!cur_0",6))
return CONTENTS_CURRENT_0;
if (!Q_strncasecmp (name, "!cur_270",8))
return CONTENTS_CURRENT_270;
if (!Q_strncasecmp (name, "!cur_180",8))
return CONTENTS_CURRENT_180;
if (!Q_strncasecmp (name, "!cur_up",7))
return CONTENTS_CURRENT_UP;
if (!Q_strncasecmp (name, "!cur_dwn",8))
return CONTENTS_CURRENT_DOWN;
//*/
if (name[0] == '!')
return CONTENTS_WATER;
/*
if (!Q_strncasecmp (name, "origin",6))
return CONTENTS_ORIGIN;
if (!Q_strncasecmp (name, "clip",4))
return CONTENTS_CLIP;
if( !Q_strncasecmp( name, "translucent", 11 ) )
return CONTENTS_TRANSLUCENT;
if( name[0] == '@' )
return CONTENTS_TRANSLUCENT;
//*/
return CONTENTS_SOLID;
} //end of the function HL_TextureContents
//===========================================================================
// Generates two new brushes, leaving the original
// unchanged
//
// modified for Half-Life because there are quite a lot of tiny node leaves
// in the Half-Life bsps
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void HL_SplitBrush(bspbrush_t *brush, int planenum, int nodenum,
bspbrush_t **front, bspbrush_t **back)
{
bspbrush_t *b[2];
int i, j;
winding_t *w, *cw[2], *midwinding;
plane_t *plane, *plane2;
side_t *s, *cs;
float d, d_front, d_back;
*front = *back = NULL;
plane = &mapplanes[planenum];
// check all points
d_front = d_back = 0;
for (i=0 ; i<brush->numsides ; i++)
{
w = brush->sides[i].winding;
if (!w)
continue;
for (j=0 ; j<w->numpoints ; j++)
{
d = DotProduct (w->p[j], plane->normal) - plane->dist;
if (d > 0 && d > d_front)
d_front = d;
if (d < 0 && d < d_back)
d_back = d;
} //end for
} //end for
if (d_front < 0.1) // PLANESIDE_EPSILON)
{ // only on back
*back = CopyBrush (brush);
Log_Print("HL_SplitBrush: only on back\n");
return;
} //end if
if (d_back > -0.1) // PLANESIDE_EPSILON)
{ // only on front
*front = CopyBrush (brush);
Log_Print("HL_SplitBrush: only on front\n");
return;
} //end if
// create a new winding from the split plane
w = BaseWindingForPlane (plane->normal, plane->dist);
for (i = 0; i < brush->numsides && w; i++)
{
plane2 = &mapplanes[brush->sides[i].planenum ^ 1];
ChopWindingInPlace(&w, plane2->normal, plane2->dist, 0); // PLANESIDE_EPSILON);
} //end for
if (!w || WindingIsTiny(w))
{ // the brush isn't really split
int side;
Log_Print("HL_SplitBrush: no split winding\n");
side = BrushMostlyOnSide (brush, plane);
if (side == PSIDE_FRONT)
*front = CopyBrush (brush);
if (side == PSIDE_BACK)
*back = CopyBrush (brush);
return;
}
if (WindingIsHuge(w))
{
Log_Print("HL_SplitBrush: WARNING huge split winding\n");
} //end of
midwinding = w;
// split it for real
for (i = 0; i < 2; i++)
{
b[i] = AllocBrush (brush->numsides+1);
b[i]->original = brush->original;
} //end for
// split all the current windings
for (i=0 ; i<brush->numsides ; i++)
{
s = &brush->sides[i];
w = s->winding;
if (!w)
continue;
ClipWindingEpsilon (w, plane->normal, plane->dist,
0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1]);
for (j=0 ; j<2 ; j++)
{
if (!cw[j])
continue;
#if 0
if (WindingIsTiny (cw[j]))
{
FreeWinding (cw[j]);
continue;
}
#endif
cs = &b[j]->sides[b[j]->numsides];
b[j]->numsides++;
*cs = *s;
// cs->planenum = s->planenum;
// cs->texinfo = s->texinfo;
// cs->visible = s->visible;
// cs->original = s->original;
cs->winding = cw[j];
cs->flags &= ~SFL_TESTED;
} //end for
} //end for
// see if we have valid polygons on both sides
for (i=0 ; i<2 ; i++)
{
BoundBrush (b[i]);
for (j=0 ; j<3 ; j++)
{
if (b[i]->mins[j] < -4096 || b[i]->maxs[j] > 4096)
{
Log_Print("HL_SplitBrush: bogus brush after clip\n");
break;
} //end if
} //end for
if (b[i]->numsides < 3 || j < 3)
{
FreeBrush (b[i]);
b[i] = NULL;
Log_Print("HL_SplitBrush: numsides < 3\n");
} //end if
} //end for
if ( !(b[0] && b[1]) )
{
if (!b[0] && !b[1])
Log_Print("HL_SplitBrush: split removed brush\n");
else
Log_Print("HL_SplitBrush: split not on both sides\n");
if (b[0])
{
FreeBrush (b[0]);
*front = CopyBrush (brush);
} //end if
if (b[1])
{
FreeBrush (b[1]);
*back = CopyBrush (brush);
} //end if
return;
} //end if
// add the midwinding to both sides
for (i = 0; i < 2; i++)
{
cs = &b[i]->sides[b[i]->numsides];
b[i]->numsides++;
cs->planenum = planenum^i^1;
cs->texinfo = 0;
//store the node number in the surf to find the texinfo later on
cs->surf = nodenum;
//
cs->flags &= ~SFL_VISIBLE;
cs->flags &= ~SFL_TESTED;
if (i==0)
cs->winding = CopyWinding (midwinding);
else
cs->winding = midwinding;
} //end for
{
vec_t v1;
int i;
for (i=0 ; i<2 ; i++)
{
v1 = BrushVolume (b[i]);
if (v1 < 1)
{
FreeBrush (b[i]);
b[i] = NULL;
Log_Print("HL_SplitBrush: tiny volume after clip\n");
} //end if
} //end for
} //*/
*front = b[0];
*back = b[1];
} //end of the function HL_SplitBrush
//===========================================================================
// returns true if the tree starting at nodenum has only solid leaves
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int HL_SolidTree_r(int nodenum)
{
if (nodenum < 0)
{
switch(hl_dleafs[(-nodenum) - 1].contents)
{
case HL_CONTENTS_EMPTY:
{
return false;
} //end case
case HL_CONTENTS_SOLID:
#ifdef HLCONTENTS
case HL_CONTENTS_CLIP:
#endif //HLCONTENTS
case HL_CONTENTS_SKY:
#ifdef HLCONTENTS
case HL_CONTENTS_TRANSLUCENT:
#endif //HLCONTENTS
{
return true;
} //end case
case HL_CONTENTS_WATER:
case HL_CONTENTS_SLIME:
case HL_CONTENTS_LAVA:
#ifdef HLCONTENTS
//these contents should not be found in the BSP
case HL_CONTENTS_ORIGIN:
case HL_CONTENTS_CURRENT_0:
case HL_CONTENTS_CURRENT_90:
case HL_CONTENTS_CURRENT_180:
case HL_CONTENTS_CURRENT_270:
case HL_CONTENTS_CURRENT_UP:
case HL_CONTENTS_CURRENT_DOWN:
#endif //HLCONTENTS
default:
{
return false;
} //end default
} //end switch
return false;
} //end if
if (!HL_SolidTree_r(hl_dnodes[nodenum].children[0])) return false;
if (!HL_SolidTree_r(hl_dnodes[nodenum].children[1])) return false;
return true;
} //end of the function HL_SolidTree_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *HL_CreateBrushes_r(bspbrush_t *brush, int nodenum)
{
int planenum;
bspbrush_t *front, *back;
hl_dleaf_t *leaf;
//if it is a leaf
if (nodenum < 0)
{
leaf = &hl_dleafs[(-nodenum) - 1];
if (leaf->contents != HL_CONTENTS_EMPTY)
{
#ifdef HL_PRINT
qprintf("\r%5i", ++hl_numbrushes);
#endif //HL_PRINT
} //end if
switch(leaf->contents)
{
case HL_CONTENTS_EMPTY:
{
FreeBrush(brush);
return NULL;
} //end case
case HL_CONTENTS_SOLID:
#ifdef HLCONTENTS
case HL_CONTENTS_CLIP:
#endif //HLCONTENTS
case HL_CONTENTS_SKY:
#ifdef HLCONTENTS
case HL_CONTENTS_TRANSLUCENT:
#endif //HLCONTENTS
{
brush->side = CONTENTS_SOLID;
return brush;
} //end case
case HL_CONTENTS_WATER:
{
brush->side = CONTENTS_WATER;
return brush;
} //end case
case HL_CONTENTS_SLIME:
{
brush->side = CONTENTS_SLIME;
return brush;
} //end case
case HL_CONTENTS_LAVA:
{
brush->side = CONTENTS_LAVA;
return brush;
} //end case
#ifdef HLCONTENTS
//these contents should not be found in the BSP
case HL_CONTENTS_ORIGIN:
case HL_CONTENTS_CURRENT_0:
case HL_CONTENTS_CURRENT_90:
case HL_CONTENTS_CURRENT_180:
case HL_CONTENTS_CURRENT_270:
case HL_CONTENTS_CURRENT_UP:
case HL_CONTENTS_CURRENT_DOWN:
{
Error("HL_CreateBrushes_r: found contents %d in Half-Life BSP", leaf->contents);
return NULL;
} //end case
#endif //HLCONTENTS
default:
{
Error("HL_CreateBrushes_r: unknown contents %d in Half-Life BSP", leaf->contents);
return NULL;
} //end default
} //end switch
return NULL;
} //end if
//if the rest of the tree is solid
/*if (HL_SolidTree_r(nodenum))
{
brush->side = CONTENTS_SOLID;
return brush;
} //end if*/
//
planenum = hl_dnodes[nodenum].planenum;
planenum = FindFloatPlane(hl_dplanes[planenum].normal, hl_dplanes[planenum].dist);
//split the brush with the node plane
HL_SplitBrush(brush, planenum, nodenum, &front, &back);
//free the original brush
FreeBrush(brush);
//every node must split the brush in two
if (!front || !back)
{
Log_Print("HL_CreateBrushes_r: WARNING node not splitting brush\n");
//return NULL;
} //end if
//create brushes recursively
if (front) front = HL_CreateBrushes_r(front, hl_dnodes[nodenum].children[0]);
if (back) back = HL_CreateBrushes_r(back, hl_dnodes[nodenum].children[1]);
//link the brushes if possible and return them
if (front)
{
for (brush = front; brush->next; brush = brush->next);
brush->next = back;
return front;
} //end if
else
{
return back;
} //end else
} //end of the function HL_CreateBrushes_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *HL_CreateBrushesFromBSP(int modelnum)
{
bspbrush_t *brushlist;
bspbrush_t *brush;
hl_dnode_t *headnode;
vec3_t mins, maxs;
int i;
//
headnode = &hl_dnodes[hl_dmodels[modelnum].headnode[0]];
//get the mins and maxs of the world
VectorCopy(headnode->mins, mins);
VectorCopy(headnode->maxs, maxs);
//enlarge these mins and maxs
for (i = 0; i < 3; i++)
{
mins[i] -= 8;
maxs[i] += 8;
} //end for
//NOTE: have to add the BSP tree mins and maxs to the MAP mins and maxs
AddPointToBounds(mins, map_mins, map_maxs);
AddPointToBounds(maxs, map_mins, map_maxs);
//
if (!modelnum)
{
Log_Print("brush size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n",
map_mins[0], map_mins[1], map_mins[2],
map_maxs[0], map_maxs[1], map_maxs[2]);
} //end if
//create one huge brush containing the whole world
brush = BrushFromBounds(mins, maxs);
VectorCopy(mins, brush->mins);
VectorCopy(maxs, brush->maxs);
//
#ifdef HL_PRINT
qprintf("creating Half-Life brushes\n");
qprintf("%5d brushes", hl_numbrushes = 0);
#endif //HL_PRINT
//create the brushes
brushlist = HL_CreateBrushes_r(brush, hl_dmodels[modelnum].headnode[0]);
//
#ifdef HL_PRINT
qprintf("\n");
#endif //HL_PRINT
//now we've got a list with brushes!
return brushlist;
} //end of the function HL_CreateBrushesFromBSP
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *HL_MergeBrushes(bspbrush_t *brushlist, int modelnum)
{
int nummerges, merged;
bspbrush_t *b1, *b2, *tail, *newbrush, *newbrushlist;
bspbrush_t *lastb2;
if (!brushlist) return NULL;
if (!modelnum) qprintf("%5d brushes merged", nummerges = 0);
do
{
for (tail = brushlist; tail; tail = tail->next)
{
if (!tail->next) break;
} //end for
merged = 0;
newbrushlist = NULL;
for (b1 = brushlist; b1; b1 = brushlist)
{
lastb2 = b1;
for (b2 = b1->next; b2; b2 = b2->next)
{
//can't merge brushes with different contents
if (b1->side != b2->side) newbrush = NULL;
else newbrush = TryMergeBrushes(b1, b2);
//if a merged brush is created
if (newbrush)
{
//copy the brush contents
newbrush->side = b1->side;
//add the new brush to the end of the list
tail->next = newbrush;
//remove the second brush from the list
lastb2->next = b2->next;
//remove the first brush from the list
brushlist = brushlist->next;
//free the merged brushes
FreeBrush(b1);
FreeBrush(b2);
//get a new tail brush
for (tail = brushlist; tail; tail = tail->next)
{
if (!tail->next) break;
} //end for
merged++;
if (!modelnum) qprintf("\r%5d", nummerges++);
break;
} //end if
lastb2 = b2;
} //end for
//if b1 can't be merged with any of the other brushes
if (!b2)
{
brushlist = brushlist->next;
//keep b1
b1->next = newbrushlist;
newbrushlist = b1;
} //end else
} //end for
brushlist = newbrushlist;
} while(merged);
if (!modelnum) qprintf("\n");
return newbrushlist;
} //end of the function HL_MergeBrushes
//===========================================================================
// returns the amount the face and the winding have overlap
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
float HL_FaceOnWinding(hl_dface_t *face, winding_t *winding)
{
int i, edgenum, side;
float dist, area;
hl_dplane_t plane;
vec_t *v1, *v2;
vec3_t normal, edgevec;
winding_t *w;
//
w = CopyWinding(winding);
memcpy(&plane, &hl_dplanes[face->planenum], sizeof(hl_dplane_t));
//check on which side of the plane the face is
if (face->side)
{
VectorNegate(plane.normal, plane.normal);
plane.dist = -plane.dist;
} //end if
for (i = 0; i < face->numedges && w; i++)
{
//get the first and second vertex of the edge
edgenum = hl_dsurfedges[face->firstedge + i];
side = edgenum > 0;
//if the face plane is flipped
v1 = hl_dvertexes[hl_dedges[abs(edgenum)].v[side]].point;
v2 = hl_dvertexes[hl_dedges[abs(edgenum)].v[!side]].point;
//create a plane through the edge vector, orthogonal to the face plane
//and with the normal vector pointing out of the face
VectorSubtract(v1, v2, edgevec);
CrossProduct(edgevec, plane.normal, normal);
VectorNormalize(normal);
dist = DotProduct(normal, v1);
//
ChopWindingInPlace(&w, normal, dist, 0.9); //CLIP_EPSILON
} //end for
if (w)
{
area = WindingArea(w);
FreeWinding(w);
return area;
} //end if
return 0;
} //end of the function HL_FaceOnWinding
//===========================================================================
// returns a list with brushes created by splitting the given brush with
// planes that go through the face edges and are orthogonal to the face plane
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *HL_SplitBrushWithFace(bspbrush_t *brush, hl_dface_t *face)
{
int i, edgenum, side, planenum, splits;
float dist;
hl_dplane_t plane;
vec_t *v1, *v2;
vec3_t normal, edgevec;
bspbrush_t *front, *back, *brushlist;
memcpy(&plane, &hl_dplanes[face->planenum], sizeof(hl_dplane_t));
//check on which side of the plane the face is
if (face->side)
{
VectorNegate(plane.normal, plane.normal);
plane.dist = -plane.dist;
} //end if
splits = 0;
brushlist = NULL;
for (i = 0; i < face->numedges; i++)
{
//get the first and second vertex of the edge
edgenum = hl_dsurfedges[face->firstedge + i];
side = edgenum > 0;
//if the face plane is flipped
v1 = hl_dvertexes[hl_dedges[abs(edgenum)].v[side]].point;
v2 = hl_dvertexes[hl_dedges[abs(edgenum)].v[!side]].point;
//create a plane through the edge vector, orthogonal to the face plane
//and with the normal vector pointing out of the face
VectorSubtract(v1, v2, edgevec);
CrossProduct(edgevec, plane.normal, normal);
VectorNormalize(normal);
dist = DotProduct(normal, v1);
//
planenum = FindFloatPlane(normal, dist);
//split the current brush
SplitBrush(brush, planenum, &front, &back);
//if there is a back brush just put it in the list
if (back)
{
//copy the brush contents
back->side = brush->side;
//
back->next = brushlist;
brushlist = back;
splits++;
} //end if
if (!front)
{
Log_Print("HL_SplitBrushWithFace: no new brush\n");
FreeBrushList(brushlist);
return NULL;
} //end if
//copy the brush contents
front->side = brush->side;
//continue splitting the front brush
brush = front;
} //end for
if (!splits)
{
FreeBrush(front);
return NULL;
} //end if
front->next = brushlist;
brushlist = front;
return brushlist;
} //end of the function HL_SplitBrushWithFace
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *HL_TextureBrushes(bspbrush_t *brushlist, int modelnum)
{
float area, largestarea;
int i, n, texinfonum, sn, numbrushes, ofs;
int bestfacenum, sidenodenum;
side_t *side;
hl_dmiptexlump_t *miptexlump;
hl_miptex_t *miptex;
bspbrush_t *brush, *nextbrush, *prevbrush, *newbrushes, *brushlistend;
vec_t defaultvec[4] = {1, 0, 0, 0};
if (!modelnum) qprintf("texturing brushes\n");
if (!modelnum) qprintf("%5d brushes", numbrushes = 0);
//get a pointer to the last brush in the list
for (brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next)
{
if (!brushlistend->next) break;
} //end for
//there's no previous brush when at the start of the list
prevbrush = NULL;
//go over the brush list
for (brush = brushlist; brush; brush = nextbrush)
{
nextbrush = brush->next;
//find a texinfo for every brush side
for (sn = 0; sn < brush->numsides; sn++)
{
side = &brush->sides[sn];
//
if (side->flags & SFL_TEXTURED) continue;
//number of the node that created this brush side
sidenodenum = side->surf; //see midwinding in HL_SplitBrush
//no face found yet
bestfacenum = -1;
//minimum face size
largestarea = 1;
//if optimizing the texture placement and not going for the
//least number of brushes
if (!lessbrushes)
{
for (i = 0; i < hl_numfaces; i++)
{
//the face must be in the same plane as the node plane that created
//this brush side
if (hl_dfaces[i].planenum == hl_dnodes[sidenodenum].planenum)
{
//get the area the face and the brush side overlap
area = HL_FaceOnWinding(&hl_dfaces[i], side->winding);
//if this face overlaps the brush side winding more than previous faces
if (area > largestarea)
{
//if there already was a face for texturing this brush side with
//a different texture
if (bestfacenum >= 0 &&
(hl_dfaces[bestfacenum].texinfo != hl_dfaces[i].texinfo))
{
//split the brush to fit the texture
newbrushes = HL_SplitBrushWithFace(brush, &hl_dfaces[i]);
//if new brushes where created
if (newbrushes)
{
//remove the current brush from the list
if (prevbrush) prevbrush->next = brush->next;
else brushlist = brush->next;
if (brushlistend == brush)
{
brushlistend = prevbrush;
nextbrush = newbrushes;
} //end if
//add the new brushes to the end of the list
if (brushlistend) brushlistend->next = newbrushes;
else brushlist = newbrushes;
//free the current brush
FreeBrush(brush);
//don't forget about the prevbrush pointer at the bottom of
//the outer loop
brush = prevbrush;
//find the end of the list
for (brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next)
{
if (!brushlistend->next) break;
} //end for
break;
} //end if
else
{
Log_Write("brush %d: no real texture split", numbrushes);
} //end else
} //end if
else
{
//best face for texturing this brush side
bestfacenum = i;
} //end else
} //end if
} //end if
} //end for
//if the brush was split the original brush is removed
//and we just continue with the next one in the list
if (i < hl_numfaces) break;
} //end if
else
{
//find the face with the largest overlap with this brush side
//for texturing the brush side
for (i = 0; i < hl_numfaces; i++)
{
//the face must be in the same plane as the node plane that created
//this brush side
if (hl_dfaces[i].planenum == hl_dnodes[sidenodenum].planenum)
{
//get the area the face and the brush side overlap
area = HL_FaceOnWinding(&hl_dfaces[i], side->winding);
//if this face overlaps the brush side winding more than previous faces
if (area > largestarea)
{
largestarea = area;
bestfacenum = i;
} //end if
} //end if
} //end for
} //end else
//if a face was found for texturing this brush side
if (bestfacenum >= 0)
{
//set the MAP texinfo values
texinfonum = hl_dfaces[bestfacenum].texinfo;
for (n = 0; n < 4; n++)
{
map_texinfo[texinfonum].vecs[0][n] = hl_texinfo[texinfonum].vecs[0][n];
map_texinfo[texinfonum].vecs[1][n] = hl_texinfo[texinfonum].vecs[1][n];
} //end for
//make sure the two vectors aren't of zero length otherwise use the default
//vector to prevent a divide by zero in the map writing
if (VectorLength(map_texinfo[texinfonum].vecs[0]) < 0.01)
memcpy(map_texinfo[texinfonum].vecs[0], defaultvec, sizeof(defaultvec));
if (VectorLength(map_texinfo[texinfonum].vecs[1]) < 0.01)
memcpy(map_texinfo[texinfonum].vecs[1], defaultvec, sizeof(defaultvec));
//
map_texinfo[texinfonum].flags = hl_texinfo[texinfonum].flags;
map_texinfo[texinfonum].value = 0; //HL_ and HL texinfos don't have a value
//the mip texture
miptexlump = (hl_dmiptexlump_t *) hl_dtexdata;
ofs = miptexlump->dataofs[hl_texinfo[texinfonum].miptex];
if ( ofs > hl_texdatasize ) {
ofs = miptexlump->dataofs[0];
}
miptex = (hl_miptex_t *)((byte *)miptexlump + ofs );
//get the mip texture name
strcpy(map_texinfo[texinfonum].texture, miptex->name);
//no animations in Quake1 and Half-Life mip textures
map_texinfo[texinfonum].nexttexinfo = -1;
//store the texinfo number
side->texinfo = texinfonum;
//
if (texinfonum > map_numtexinfo) map_numtexinfo = texinfonum;
//this side is textured
side->flags |= SFL_TEXTURED;
} //end if
else
{
//no texture for this side
side->texinfo = TEXINFO_NODE;
//this side is textured
side->flags |= SFL_TEXTURED;
} //end if
} //end for
//
if (!modelnum && prevbrush != brush) qprintf("\r%5d", ++numbrushes);
//previous brush in the list
prevbrush = brush;
} //end for
if (!modelnum) qprintf("\n");
//return the new list with brushes
return brushlist;
} //end of the function HL_TextureBrushes
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void HL_FixContentsTextures(bspbrush_t *brushlist)
{
int i, texinfonum;
bspbrush_t *brush;
for (brush = brushlist; brush; brush = brush->next)
{
//only fix the textures of water, slime and lava brushes
if (brush->side != CONTENTS_WATER &&
brush->side != CONTENTS_SLIME &&
brush->side != CONTENTS_LAVA) continue;
//
for (i = 0; i < brush->numsides; i++)
{
texinfonum = brush->sides[i].texinfo;
if (HL_TextureContents(map_texinfo[texinfonum].texture) == brush->side) break;
} //end for
//if no specific contents texture was found
if (i >= brush->numsides)
{
texinfonum = -1;
for (i = 0; i < map_numtexinfo; i++)
{
if (HL_TextureContents(map_texinfo[i].texture) == brush->side)
{
texinfonum = i;
break;
} //end if
} //end for
} //end if
//
if (texinfonum >= 0)
{
//give all the brush sides this contents texture
for (i = 0; i < brush->numsides; i++)
{
brush->sides[i].texinfo = texinfonum;
} //end for
} //end if
else Log_Print("brush contents %d with wrong textures\n", brush->side);
//
} //end for
/*
for (brush = brushlist; brush; brush = brush->next)
{
//give all the brush sides this contents texture
for (i = 0; i < brush->numsides; i++)
{
if (HL_TextureContents(map_texinfo[texinfonum].texture) != brush->side)
{
Error("brush contents %d with wrong contents textures %s\n", brush->side,
HL_TextureContents(map_texinfo[texinfonum].texture));
} //end if
} //end for
} //end for*/
} //end of the function HL_FixContentsTextures
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void HL_BSPBrushToMapBrush(bspbrush_t *bspbrush, entity_t *mapent)
{
mapbrush_t *mapbrush;
side_t *side;
int i, besttexinfo;
if (nummapbrushes >= MAX_MAPFILE_BRUSHES)
Error ("nummapbrushes == MAX_MAPFILE_BRUSHES");
mapbrush = &mapbrushes[nummapbrushes];
mapbrush->original_sides = &brushsides[nummapbrushsides];
mapbrush->entitynum = mapent - entities;
mapbrush->brushnum = nummapbrushes - mapent->firstbrush;
mapbrush->leafnum = -1;
mapbrush->numsides = 0;
besttexinfo = TEXINFO_NODE;
for (i = 0; i < bspbrush->numsides; i++)
{
if (!bspbrush->sides[i].winding) continue;
//
if (nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES)
Error ("MAX_MAPFILE_BRUSHSIDES");
side = &brushsides[nummapbrushsides];
//the contents of the bsp brush is stored in the side variable
side->contents = bspbrush->side;
side->surf = 0;
side->planenum = bspbrush->sides[i].planenum;
side->texinfo = bspbrush->sides[i].texinfo;
if (side->texinfo != TEXINFO_NODE)
{
//this brush side is textured
side->flags |= SFL_TEXTURED;
besttexinfo = side->texinfo;
} //end if
//
nummapbrushsides++;
mapbrush->numsides++;
} //end for
//
if (besttexinfo == TEXINFO_NODE)
{
mapbrush->numsides = 0;
hl_numclipbrushes++;
return;
} //end if
//set the texinfo for all the brush sides without texture
for (i = 0; i < mapbrush->numsides; i++)
{
if (mapbrush->original_sides[i].texinfo == TEXINFO_NODE)
{
mapbrush->original_sides[i].texinfo = besttexinfo;
} //end if
} //end for
//contents of the brush
mapbrush->contents = bspbrush->side;