-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmodel.c
1695 lines (1401 loc) · 41 KB
/
cmodel.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
/*
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// cmodel.c - collision model.
#ifdef SERVERONLY
#include "qwsvdef.h"
#else
#include "common.h"
#include "cvar.h"
#endif
#include <limits.h>
typedef struct cnode_s {
// common with leaf
int contents; // 0, to differentiate from leafs
struct cnode_s *parent;
// node specific
mplane_t *plane;
struct cnode_s *children[2];
} cnode_t;
typedef struct cleaf_s {
// common with node
int contents; // a negative contents number
struct cnode_s *parent;
// leaf specific
byte ambient_sound_level[NUM_AMBIENTS];
} cleaf_t;
static char loadname[32]; // for hunk tags
static char map_name[MAX_QPATH];
static unsigned int map_checksum, map_checksum2;
static int numcmodels;
static cmodel_t map_cmodels[MAX_MAP_MODELS];
static mplane_t *map_planes;
static int numplanes;
static cnode_t *map_nodes;
static int numnodes;
static mclipnode_t *map_clipnodes;
static int numclipnodes;
static cleaf_t *map_leafs;
static int numleafs;
static int visleafs;
static byte map_novis[MAX_MAP_LEAFS/8];
static byte *map_pvs; // fully expanded and decompressed
static byte *map_phs; // only valid if we are the server
static int map_vis_rowbytes; // for both pvs and phs
static int map_vis_rowlongs; // map_vis_rowbytes / 4
static char *map_entitystring;
static qbool map_halflife;
static mphysicsnormal_t* map_physicsnormals; // must be same number as clipnodes to save reallocations in worst case scenario
static byte *cmod_base; // for CM_Load* functions
// lumps immediately follow:
typedef struct {
char lumpname[24];
int fileofs;
int filelen;
} bspx_lump_t;
void* Mod_BSPX_FindLump(bspx_header_t* bspx_header, char* lumpname, int* plumpsize, byte* mod_base);
bspx_header_t* Mod_LoadBSPX(int filesize, byte* mod_base);
/*
===============================================================================
HULL BOXES
===============================================================================
*/
static hull_t box_hull;
static mclipnode_t box_clipnodes[6];
static mplane_t box_planes[6];
/*
** CM_InitBoxHull
**
** Set up the planes and clipnodes so that the six floats of a bounding box
** can just be stored out and get a proper hull_t structure.
*/
static void CM_InitBoxHull(void)
{
int side, i;
box_hull.clipnodes = box_clipnodes;
box_hull.planes = box_planes;
box_hull.firstclipnode = 0;
box_hull.lastclipnode = 5;
for (i = 0; i < 6; i++) {
box_clipnodes[i].planenum = i;
side = i & 1;
box_clipnodes[i].children[side] = CONTENTS_EMPTY;
box_clipnodes[i].children[side ^ 1] = (i != 5) ? (i + 1) : CONTENTS_SOLID;
box_planes[i].type = i >> 1;
box_planes[i].normal[i >> 1] = 1;
}
}
/*
** CM_HullForBox
**
** To keep everything totally uniform, bounding boxes are turned into small
** BSP trees instead of being compared directly.
*/
hull_t *CM_HullForBox (vec3_t mins, vec3_t maxs)
{
box_planes[0].dist = maxs[0];
box_planes[1].dist = mins[0];
box_planes[2].dist = maxs[1];
box_planes[3].dist = mins[1];
box_planes[4].dist = maxs[2];
box_planes[5].dist = mins[2];
return &box_hull;
}
int CM_CachedHullPointContents(hull_t* hull, int num, vec3_t p, float* min_dist)
{
mclipnode_t* node;
mplane_t* plane;
float d;
*min_dist = 999;
while (num >= 0) {
if (num < hull->firstclipnode || num > hull->lastclipnode) {
if (map_halflife && num == hull->lastclipnode + 1) {
return CONTENTS_EMPTY;
}
Sys_Error("CM_HullPointContents: bad node number");
}
node = hull->clipnodes + num;
plane = hull->planes + node->planenum;
d = PlaneDiff(p, plane);
if (d < 0) {
*min_dist = min(*min_dist, -d);
num = node->children[1];
}
else {
*min_dist = min(*min_dist, d);
num = node->children[0];
}
}
return num;
}
int CM_HullPointContents(hull_t *hull, int num, vec3_t p)
{
mclipnode_t *node;
mplane_t *plane;
float d;
while (num >= 0) {
if (num < hull->firstclipnode || num > hull->lastclipnode) {
if (map_halflife && num == hull->lastclipnode + 1) {
return CONTENTS_EMPTY;
}
Sys_Error("CM_HullPointContents: bad node number");
}
node = hull->clipnodes + num;
plane = hull->planes + node->planenum;
d = PlaneDiff(p, plane);
num = (d < 0) ? node->children[1] : node->children[0];
}
return num;
}
/*
===============================================================================
LINE TESTING IN HULLS
===============================================================================
*/
// 1/32 epsilon to keep floating point happy
#define DIST_EPSILON 0.03125
enum { TR_EMPTY, TR_SOLID, TR_BLOCKED };
typedef struct {
hull_t *hull;
trace_t trace;
int leafcount;
} hulltrace_local_t;
//====================
int RecursiveHullTrace (hulltrace_local_t *htl, int num, float p1f, float p2f, const vec3_t p1, const vec3_t p2)
{
mplane_t *plane;
float t1, t2;
mclipnode_t *node;
int i;
int nearside;
float frac, midf;
vec3_t mid;
int check, oldcheck;
hull_t *hull = htl->hull;
trace_t *trace = &htl->trace;
start:
if (num < 0) {
// this is a leaf node
htl->leafcount++;
if (num == CONTENTS_SOLID) {
if (htl->leafcount == 1)
trace->startsolid = true;
return TR_SOLID;
}
else {
if (num == CONTENTS_EMPTY)
trace->inopen = true;
else
trace->inwater = true;
return TR_EMPTY;
}
}
// FIXME, check at load time
if (num < hull->firstclipnode || num > hull->lastclipnode)
{
if (map_halflife && num == hull->lastclipnode + 1)
return TR_EMPTY;
Sys_Error ("RecursiveHullTrace: bad node number");
}
node = hull->clipnodes + num;
//
// find the point distances
//
plane = hull->planes + node->planenum;
if (plane->type < 3) {
t1 = p1[plane->type] - plane->dist;
t2 = p2[plane->type] - plane->dist;
}
else {
t1 = DotProduct (plane->normal, p1) - plane->dist;
t2 = DotProduct (plane->normal, p2) - plane->dist;
}
// see which sides we need to consider
if (t1 >= 0 && t2 >= 0) {
num = node->children[0]; // go down the front side
goto start;
}
if (t1 < 0 && t2 < 0) {
num = node->children[1]; // go down the back side
goto start;
}
// find the intersection point
frac = t1 / (t1 - t2);
frac = bound (0, frac, 1);
midf = p1f + (p2f - p1f)*frac;
for (i = 0; i < 3; i++)
mid[i] = p1[i] + frac*(p2[i] - p1[i]);
// move up to the node
nearside = (t1 < t2) ? 1 : 0;
check = RecursiveHullTrace (htl, node->children[nearside], p1f, midf, p1, mid);
if (check == TR_BLOCKED)
return check;
// if we started in solid, allow us to move out to an empty area
if (check == TR_SOLID && (trace->inopen || trace->inwater))
return check;
oldcheck = check;
// go past the node
check = RecursiveHullTrace (htl, node->children[1 - nearside], midf, p2f, mid, p2);
if (check == TR_EMPTY || check == TR_BLOCKED)
return check;
if (oldcheck != TR_EMPTY)
return check; // still in solid
// near side is empty, far side is solid
// this is the impact point
if (!nearside) {
VectorCopy (plane->normal, trace->plane.normal);
trace->plane.dist = plane->dist;
}
else {
VectorNegate (plane->normal, trace->plane.normal);
trace->plane.dist = -plane->dist;
}
// put the final point DIST_EPSILON pixels on the near side
if (t1 < t2)
frac = (t1 + DIST_EPSILON) / (t1 - t2);
else
frac = (t1 - DIST_EPSILON) / (t1 - t2);
frac = bound (0, frac, 1);
midf = p1f + (p2f - p1f)*frac;
for (i = 0; i < 3; i++)
mid[i] = p1[i] + frac*(p2[i] - p1[i]);
trace->fraction = midf;
VectorCopy (mid, trace->endpos);
trace->physicsnormal = (!nearside ? num + 1 : -(num + 1));
return TR_BLOCKED;
}
trace_t CM_HullTrace (hull_t *hull, vec3_t start, vec3_t end)
{
int check;
// this structure is passed as a pointer to RecursiveHullTrace
// so as not to use much stack but still be thread safe
hulltrace_local_t htl;
htl.hull = hull;
htl.leafcount = 0;
// fill in a default trace
memset (&htl.trace, 0, sizeof(htl.trace));
htl.trace.fraction = 1;
htl.trace.startsolid = false;
VectorCopy (end, htl.trace.endpos);
check = RecursiveHullTrace (&htl, hull->firstclipnode, 0, 1, start, end);
if (check == TR_SOLID) {
htl.trace.startsolid = htl.trace.allsolid = true;
// it would be logical to set fraction to 0, but original id code
// would leave it at 1. We emulate that just in case.
// (FIXME: is it just QW, or NQ as well?)
//htl.trace.fraction = 0;
VectorCopy (start, htl.trace.endpos);
}
return htl.trace;
}
//===========================================================================
int CM_NumInlineModels (void)
{
return numcmodels;
}
char *CM_EntityString (void)
{
return map_entitystring;
}
int CM_Leafnum (const cleaf_t *leaf)
{
assert (leaf);
return leaf - map_leafs;
}
int CM_LeafAmbientLevel (const cleaf_t *leaf, int ambient_channel)
{
assert ((unsigned)ambient_channel <= NUM_AMBIENTS);
assert (leaf);
return leaf->ambient_sound_level[ambient_channel];
}
// always returns a valid cleaf_t pointer
cleaf_t *CM_PointInLeaf (const vec3_t p)
{
float d;
cnode_t *node;
mplane_t *plane;
if (!numnodes)
Host_Error ("CM_PointInLeaf: numnodes == 0");
node = map_nodes;
while (1) {
if (node->contents < 0) {
return (cleaf_t *)node;
}
plane = node->plane;
d = DotProduct(p, plane->normal) - plane->dist;
node = (d > 0) ? node->children[0] : node->children[1];
}
return NULL; // never reached
}
byte *CM_LeafPVS (const cleaf_t *leaf)
{
if (leaf == map_leafs)
return map_novis;
return map_pvs + (leaf - 1 - map_leafs) * map_vis_rowbytes;
}
/*
** only the server may call this
*/
byte *CM_LeafPHS (const cleaf_t *leaf)
{
if (leaf == map_leafs)
return map_novis;
if (!map_phs) {
return NULL;
}
return map_phs + (leaf - 1 - map_leafs) * map_vis_rowbytes;
}
/*
=============================================================================
The PVS must include a small area around the client to allow head bobbing
or other small motion on the client side. Otherwise, a bob might cause an
entity that should be visible to not show up, especially when the bob
crosses a waterline.
=============================================================================
*/
static int fatbytes;
static byte fatpvs[MAX_MAP_LEAFS/8];
static vec3_t fatpvs_org;
static void AddToFatPVS_r (cnode_t *node)
{
int i;
float d;
byte *pvs;
mplane_t *plane;
while (1)
{ // if this is a leaf, accumulate the pvs bits
if (node->contents < 0)
{
if (node->contents != CONTENTS_SOLID)
{
pvs = CM_LeafPVS ( (cleaf_t *)node);
for (i=0 ; i<fatbytes ; i++)
fatpvs[i] |= pvs[i];
}
return;
}
plane = node->plane;
d = DotProduct (fatpvs_org, plane->normal) - plane->dist;
if (d > 8)
node = node->children[0];
else if (d < -8)
node = node->children[1];
else
{ // go down both
AddToFatPVS_r (node->children[0]);
node = node->children[1];
}
}
}
/*
=============
CM_FatPVS
Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
given point.
=============
*/
byte *CM_FatPVS (vec3_t org)
{
VectorCopy (org, fatpvs_org);
fatbytes = (visleafs+31)>>3;
memset (fatpvs, 0, fatbytes);
AddToFatPVS_r (map_nodes);
return fatpvs;
}
/*
** Recursively build a list of leafs touched by a rectangular volume
*/
static int leafs_count;
static int leafs_maxcount;
static qbool leafs_overflow;
static int *leafs_list;
static int leafs_topnode;
static vec3_t leafs_mins, leafs_maxs;
static void FindTouchedLeafs_r(const cnode_t *node)
{
mplane_t *splitplane;
cleaf_t *leaf;
int sides;
while (1) {
if (node->contents == CONTENTS_SOLID) {
return;
}
// the node is a leaf
if (node->contents < 0) {
if (leafs_count == leafs_maxcount) {
leafs_overflow = true;
return;
}
leaf = (cleaf_t *)node;
leafs_list[leafs_count++] = leaf - map_leafs;
return;
}
// NODE_MIXED
splitplane = node->plane;
sides = BOX_ON_PLANE_SIDE(leafs_mins, leafs_maxs, splitplane);
// recurse down the contacted sides
if (sides == 1) {
node = node->children[0];
}
else if (sides == 2) {
node = node->children[1];
}
else {
if (leafs_topnode == -1) {
leafs_topnode = node - map_nodes;
}
FindTouchedLeafs_r(node->children[0]);
node = node->children[1];
}
}
}
/*
** Returns an array filled with leaf nums
*/
int CM_FindTouchedLeafs (const vec3_t mins, const vec3_t maxs, int leafs[], int maxleafs, int headnode, int *topnode)
{
leafs_count = 0;
leafs_maxcount = maxleafs;
leafs_list = leafs;
leafs_topnode = -1;
leafs_overflow = false;
VectorCopy (mins, leafs_mins);
VectorCopy (maxs, leafs_maxs);
FindTouchedLeafs_r (&map_nodes[headnode]);
if (leafs_overflow) {
leafs_count = -1;
}
if (topnode)
*topnode = leafs_topnode;
return leafs_count;
}
/*
===============================================================================
BRUSHMODEL LOADING
===============================================================================
*/
static void CM_LoadEntities (lump_t *l)
{
if (l->filelen <= 0 || l->filelen >= INT_MAX) {
map_entitystring = NULL;
return;
}
map_entitystring = (char *) Hunk_AllocName(l->filelen + 1, loadname);
memcpy(map_entitystring, cmod_base + l->fileofs, l->filelen);
}
/*
=================
CM_LoadSubmodels
=================
*/
static void CM_LoadSubmodels (lump_t *l)
{
dmodel_t *in;
cmodel_t *out;
int i, j, count;
in = (dmodel_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in))
Host_Error ("CM_LoadMap: funny lump size");
count = l->filelen / sizeof(*in);
if (count < 1)
Host_Error ("Map with no models");
if (count > MAX_MAP_MODELS)
Host_Error ("Map has too many models (%d vs %d)", count, MAX_MAP_MODELS);
out = map_cmodels;
numcmodels = count;
visleafs = LittleLong (in[0].visleafs);
for (i = 0; i < count; i++, in++, out++)
{
for (j = 0; j < 3; j++) {
// spread the mins / maxs by a pixel
out->mins[j] = LittleFloat (in->mins[j]) - 1;
out->maxs[j] = LittleFloat (in->maxs[j]) + 1;
out->origin[j] = LittleFloat (in->origin[j]);
}
for (j = 0; j < MAX_MAP_HULLS; j++) {
out->hulls[j].planes = map_planes;
out->hulls[j].clipnodes = map_clipnodes;
out->hulls[j].firstclipnode = LittleLong (in->headnode[j]);
out->hulls[j].lastclipnode = numclipnodes - 1;
}
VectorClear (out->hulls[0].clip_mins);
VectorClear (out->hulls[0].clip_maxs);
if (map_halflife)
{
VectorSet (out->hulls[1].clip_mins, -16, -16, -36);
VectorSet (out->hulls[1].clip_maxs, 16, 16, 36);
VectorSet (out->hulls[2].clip_mins, -32, -32, -36);
VectorSet (out->hulls[2].clip_maxs, 32, 32, 36);
// not really used
VectorSet (out->hulls[3].clip_mins, -16, -16, -18);
VectorSet (out->hulls[3].clip_maxs, 16, 16, 18);
}
else
{
VectorSet (out->hulls[1].clip_mins, -16, -16, -24);
VectorSet (out->hulls[1].clip_maxs, 16, 16, 32);
VectorSet (out->hulls[2].clip_mins, -32, -32, -24);
VectorSet (out->hulls[2].clip_maxs, 32, 32, 64);
}
}
}
/*
=================
CM_SetParent
=================
*/
static void CM_SetParent(cnode_t *node, cnode_t *parent)
{
if (node->parent && node->parent == parent) {
Host_Error("Infinite loop detected in node list (node %p, base %p)\n", node, map_nodes);
return;
}
node->parent = parent;
if (node->contents < 0) {
return;
}
CM_SetParent (node->children[0], node);
CM_SetParent (node->children[1], node);
}
/*
=================
CM_LoadNodes
=================
*/
static void CM_LoadNodes (lump_t *l)
{
int i, j, count, p;
dnode_t *in;
cnode_t *out;
const int max_nodes = (INT_MAX / sizeof(*out));
in = (dnode_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in))
Host_Error ("CM_LoadMap: funny lump size");
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_nodes) {
Host_Error("CM_LoadMap: invalid node count (%d vs 0-%d)", count, max_nodes);
}
out = (cnode_t *) Hunk_AllocName ( count*sizeof(*out), loadname);
map_nodes = out;
numnodes = count;
for (i = 0; i < count; i++, in++, out++) {
p = LittleLong(in->planenum);
if (p < 0 || p >= numplanes) {
Host_Error("Node %d has invalid plane# %d (0 to %d)\n", i, p, numplanes);
return;
}
out->plane = map_planes + p;
for (j = 0; j < 2; j++) {
p = LittleShort(in->children[j]);
if (p < -numleafs || p >= numnodes) {
Host_Error("Node %d child[%d] #%d (%d to %d)", i, j, p, -numleafs, numnodes);
return;
}
out->children[j] = (p >= 0) ? (map_nodes + p) : ((cnode_t*)(map_leafs + (-1 - p)));
}
}
CM_SetParent(map_nodes, NULL); // sets nodes and leafs
}
static void CM_LoadNodes29a(lump_t *l)
{
int i, j, count, p;
dnode29a_t *in;
cnode_t *out;
const int max_nodes = (INT_MAX / sizeof(*out));
in = (dnode29a_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in))
Host_Error("CM_LoadMap: funny lump size");
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_nodes) {
Host_Error("CM_LoadMap: invalid node count (%d vs 0-%d)", count, max_nodes);
}
out = (cnode_t *)Hunk_AllocName(count * sizeof(*out), loadname);
map_nodes = out;
numnodes = count;
for (i = 0; i < count; i++, in++, out++) {
p = LittleLong(in->planenum);
if (p < 0 || p >= numplanes) {
Host_Error("Node %d has invalid plane# %d (0 to %d)\n", i, p, numplanes);
return;
}
out->plane = map_planes + p;
for (j = 0; j < 2; j++) {
p = LittleLong(in->children[j]);
if (p < -numleafs || p >= numnodes) {
Host_Error("Node %d child[%d] #%d (%d to %d)", i, j, p, -numleafs, numnodes);
return;
}
out->children[j] = (p >= 0) ? (map_nodes + p) : ((cnode_t *)(map_leafs + (-1 - p)));
}
}
CM_SetParent(map_nodes, NULL); // sets nodes and leafs
}
static void CM_LoadNodesBSP2(lump_t *l)
{
int i, j, count, p;
dnode_bsp2_t *in;
cnode_t *out;
const int max_nodes = (INT_MAX / sizeof(*out));
in = (dnode_bsp2_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in))
Host_Error("CM_LoadMap: funny lump size");
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_nodes) {
Host_Error("CM_LoadMap: invalid node count (%d vs 0-%d)", count, max_nodes);
}
out = (cnode_t *)Hunk_AllocName(count * sizeof(*out), loadname);
map_nodes = out;
numnodes = count;
for (i = 0; i < count; i++, in++, out++) {
p = LittleLong(in->planenum);
if (p < 0) {
Host_Error("Node %d has negative plane# %d\n", i, in->planenum);
return;
}
out->plane = map_planes + p;
for (j = 0; j < 2; j++) {
p = LittleLong(in->children[j]);
if (p < -numleafs || p >= numnodes) {
Host_Error("Node %d child[%d] #%d (%d to %d)", i, j, p, -numleafs, numnodes);
return;
}
out->children[j] = (p >= 0) ? (map_nodes + p) : ((cnode_t *)(map_leafs + (-1 - p)));
}
}
CM_SetParent(map_nodes, NULL); // sets nodes and leafs
}
/*
** CM_LoadLeafs
*/
static void CM_LoadLeafs (lump_t *l)
{
dleaf_t *in;
cleaf_t *out;
int i, j, count, p;
int max_leafs = (INT_MAX / sizeof(*out));
in = (dleaf_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in))
Host_Error ("CM_LoadMap: funny lump size");
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_leafs) {
Host_Error("CM_LoadMap: invalid leaf count (%d vs 0-%d)", count, max_leafs);
}
out = (cleaf_t *) Hunk_AllocName ( count*sizeof(*out), loadname);
map_leafs = out;
numleafs = count;
for (i = 0; i < count; i++, in++, out++) {
p = LittleLong(in->contents);
out->contents = p;
for (j = 0; j < 4; j++)
out->ambient_sound_level[j] = in->ambient_level[j];
}
}
static void CM_LoadLeafs29a (lump_t *l)
{
dleaf29a_t *in;
cleaf_t *out;
int i, j, count, p;
int max_leafs = (INT_MAX / sizeof(*out));
in = (dleaf29a_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in)) {
Host_Error("CM_LoadMap: funny lump size");
}
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_leafs) {
Host_Error("CM_LoadMap: invalid leaf count (%d vs 0-%d)", count, max_leafs);
}
out = Hunk_AllocName ( count*sizeof(*out), loadname);
map_leafs = out;
numleafs = count;
for (i = 0; i < count; i++, in++, out++) {
p = LittleLong(in->contents);
out->contents = p;
for (j = 0; j < 4; j++) {
out->ambient_sound_level[j] = in->ambient_level[j];
}
}
}
static void CM_LoadLeafsBSP2 (lump_t *l)
{
dleaf_bsp2_t *in;
cleaf_t *out;
int i, j, count, p;
int max_leafs = (INT_MAX / sizeof(*out));
in = (dleaf_bsp2_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in)) {
Host_Error("CM_LoadMap: funny lump size");
}
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_leafs) {
Host_Error("CM_LoadMap: invalid leaf count (%d vs 0-%d)", count, max_leafs);
}
out = Hunk_AllocName ( count*sizeof(*out), loadname);
map_leafs = out;
numleafs = count;
for (i = 0; i < count; i++, in++, out++) {
p = LittleLong(in->contents);
out->contents = p;
for (j = 0; j < 4; j++)
out->ambient_sound_level[j] = in->ambient_level[j];
}
}
/*
=================
CM_LoadClipnodes
=================
*/
static void CM_LoadClipnodes(lump_t *l)
{
dclipnode_t *in;
mclipnode_t *out;
int i, count;
const int max_clipnodes = (INT_MAX / sizeof(*out));
in = (dclipnode_t *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in))
Host_Error("CM_LoadMap: funny lump size");
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_clipnodes) {
Host_Error("CM_LoadMap: invalid clipnode count (%d vs 0-%d)", count, max_clipnodes);
}
out = (mclipnode_t *)Hunk_AllocName(count * sizeof(*out), loadname);
map_clipnodes = out;
numclipnodes = count;
for (i = 0; i < count; i++, out++, in++) {
out->planenum = LittleLong(in->planenum);
if (out->planenum < 0 || out->planenum >= numplanes) {
Host_Error("CM_LoadClipNodes: node %d references plane %d (numplanes %d)", i, out->planenum, numplanes);
return;
}
out->children[0] = LittleShort(in->children[0]);
out->children[1] = LittleShort(in->children[1]);
}
}
static void CM_LoadClipnodesBSP2(lump_t *l)
{
dclipnode29a_t *in;
mclipnode_t *out;
int i, count;
const int max_clipnodes = (INT_MAX / sizeof(*out));
in = (void *)(cmod_base + l->fileofs);
if (l->filelen % sizeof(*in)) {
Host_Error("CM_LoadMap: funny lump size");
}
count = l->filelen / sizeof(*in);
if (count < 0 || count > max_clipnodes) {
Host_Error("CM_LoadMap: invalid clipnode count (%d vs 0-%d)", count, max_clipnodes);
}
out = Hunk_AllocName(count * sizeof(*out), loadname);
map_clipnodes = out;
numclipnodes = count;
for (i = 0; i < count; i++, out++, in++) {
out->planenum = LittleLong(in->planenum);
if (out->planenum < 0 || out->planenum >= numplanes) {
Host_Error("CM_LoadClipNodes: node %d references plane %d (numplanes %d)", i, out->planenum, numplanes);
return;
}
out->children[0] = LittleLong(in->children[0]);
out->children[1] = LittleLong(in->children[1]);
}
}
static qbool CM_LoadPhysicsNormalsData(byte* data, int datalength)
{
mphysicsnormal_t* in = (mphysicsnormal_t*)(data + 8);
int i;
if (datalength != 8 + sizeof(map_physicsnormals[0]) * numclipnodes) {
return false;
}
#ifndef CLIENTONLY
{