summaryrefslogtreecommitdiff
path: root/source/cmodel.c
blob: 51611f9699a8321d9371feb905771c8a120ce1bd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
/*
Copyright (C) 1997-2001 Id Software, Inc.

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 -- model loading

#include "com_local.h"

#define CM_Malloc( size )	Z_TagMalloc( size, TAG_CMODEL )
#define CM_Mallocz( size )	Z_TagMallocz( size, TAG_CMODEL )
#define CM_Free( ptr )		Z_Free( ptr )

static mapsurface_t	nullsurface;
static cleaf_t		nullleaf;

static int			floodvalid;
static int			checkcount;

static cvar_t		*map_noareas;
static cvar_t		*map_load_entities;

void	CM_FloodAreaConnections( cm_t *cm );

/*
===============================================================================

					MAP LOADING

===============================================================================
*/

static byte	*cmod_base;
static cmcache_t	*cmod;

/*
=================
CMod_LoadSubmodels
=================
*/
static void CMod_LoadSubmodels (lump_t *l)
{
	dmodel_t	*in;
	cmodel_t	*out;
	int			i, j, count;
	uint32		headnode;

	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadSubmodels: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count < 1)
		Com_Error (ERR_DROP, "Map with no models");
	if (count > MAX_MAP_MODELS)
		Com_Error (ERR_DROP, "Map has too many models");

	cmod->cmodels = CM_Malloc( sizeof( *out ) * count );
	cmod->numcmodels = count;

	out = cmod->cmodels;
	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]);
		}
		headnode = LittleLong (in->headnode);
		if( headnode >= cmod->numnodes ) {
			// FIXME: headnode may be garbage for some models
			Com_DPrintf( "CMod_LoadSubmodels: bad headnode for model %d\n", i );
			out->headnode = NULL;
		} else {
			out->headnode = cmod->nodes + headnode;
		}
	}
}


/*
=================
CMod_LoadSurfaces
=================
*/
static void CMod_LoadSurfaces (lump_t *l)
{
	texinfo_t	*in;
	mapsurface_t	*out;
	int			i, count;

	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadSurfaces: funny lump size");
	count = l->filelen / sizeof(*in);
	if (count < 1)
		Com_Error (ERR_DROP, "Map with no surfaces");
	if (count > MAX_MAP_TEXINFO)
		Com_Error (ERR_DROP, "Map has too many surfaces");

	cmod->numtexinfo = count;
	cmod->surfaces = CM_Malloc( sizeof( *out ) * count );

	out = cmod->surfaces;
	for ( i=0 ; i<count ; i++, in++, out++)
	{
		Q_strncpyz (out->c.name, in->texture, sizeof(out->c.name));
		Q_strncpyz (out->rname, in->texture, sizeof(out->rname));
		out->c.flags = LittleLong (in->flags);
		out->c.value = LittleLong (in->value);
	}
}


/*
=================
CMod_LoadNodes

=================
*/
static void CMod_LoadNodes (lump_t *l)
{
	dnode_t		*in;
	uint32		child;
	cnode_t		*out;
	int			i, j, count;
	uint32		planeNum;
	
	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadNodes: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count < 1)
		Com_Error (ERR_DROP, "Map has no nodes");
	if (count > MAX_MAP_NODES)
		Com_Error (ERR_DROP, "Map has too many nodes");

	cmod->numnodes = count;
	cmod->nodes = CM_Malloc( sizeof( *out ) * count );

	out = cmod->nodes;
	for (i=0 ; i<count ; i++, out++, in++)
	{
		planeNum = LittleLong(in->planenum);
		if( planeNum >= cmod->numplanes ) {
			Com_Error (ERR_DROP, "CMod_LoadNodes: bad planenum");
		}
		out->plane = cmod->planes + planeNum;

		for( j = 0; j < 2; j++ ) {
			child = LittleLong( in->children[j] );
			if( child & 0x80000000 ) {
				child = ~child;
				if( child >= cmod->numleafs ) {
					Com_Error( ERR_DROP, "CMod_LoadNodes: bad leafnum" );
				}
				out->children[j] = ( cnode_t * )( cmod->leafs + child );
			} else {
				if( child >= count ) {
					Com_Error ( ERR_DROP, "CMod_LoadNodes: bad nodenum" );
				}
				out->children[j] = cmod->nodes + child;
			}
		}
	}

}

/*
=================
CMod_LoadBrushes

=================
*/
static void CMod_LoadBrushes (lump_t *l)
{
	dbrush_t	*in;
	cbrush_t	*out;
	int			i, count;
	uint32		firstSide, numSides;
	
	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadBrushes: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count > MAX_MAP_BRUSHES)
		Com_Error (ERR_DROP, "Map has too many brushes");

	cmod->numbrushes = count;
	cmod->brushes = CM_Malloc( sizeof( *out ) * count );

	out = cmod->brushes;
	for (i=0 ; i<count ; i++, out++, in++)
	{
		firstSide = LittleLong(in->firstside);
		numSides = LittleLong(in->numsides);
		if( firstSide + numSides > cmod->numbrushsides ) {
			Com_Error (ERR_DROP, "CMod_LoadBrushes: bad brushsides");
		}
		out->firstbrushside = cmod->brushsides + firstSide;
		out->numsides = numSides;
		out->contents = LittleLong(in->contents);
        out->checkcount = 0;
	}

}

/*
=================
CMod_LoadLeafs
=================
*/
static void CMod_LoadLeafs (lump_t *l)
{
	int			i;
	cleaf_t		*out;
	dleaf_t 	*in;
	int			count;
	uint16		areaNum, firstBrush, numBrushes;
	
	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadLeafs: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count < 1)
		Com_Error (ERR_DROP, "Map with no leafs");
	// need to save space for box planes
	if (count > MAX_MAP_LEAFS)
		Com_Error (ERR_DROP, "Map has too many leafs");
	
	cmod->numleafs = count;
	cmod->numclusters = 0;
	cmod->leafs = CM_Malloc( sizeof( *out ) * count );

	out = cmod->leafs;
	for ( i=0 ; i<count ; i++, in++, out++)
	{
        out->plane = NULL;
		out->contents = LittleLong (in->contents);
		out->cluster = ( signed short )LittleShort (in->cluster);
		areaNum = LittleShort (in->area);
		if( areaNum >= cmod->numareas ) {
			Com_Error (ERR_DROP, "CMod_LoadLeafs: bad areanum");
		}
		out->area = areaNum;

		firstBrush = LittleShort (in->firstleafbrush);
		numBrushes = LittleShort (in->numleafbrushes);
		if( firstBrush + numBrushes > cmod->numleafbrushes ) {
			Com_Error (ERR_DROP, "CMod_LoadLeafs: bad brushnum");
		}

		out->firstleafbrush = cmod->leafbrushes + firstBrush;
		out->numleafbrushes = numBrushes;

		if (out->cluster >= cmod->numclusters)
			cmod->numclusters = out->cluster + 1;
	}

	if (cmod->leafs[0].contents != CONTENTS_SOLID)
		Com_Error (ERR_DROP, "Map leaf 0 is not CONTENTS_SOLID");
}

/*
=================
CMod_LoadPlanes
=================
*/
static void CMod_LoadPlanes (lump_t *l)
{
	int			i;
	cplane_t	*out;
	dplane_t 	*in;
	int			count;
	
	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadPlanes: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count < 1)
		Com_Error (ERR_DROP, "Map with no planes");
	if (count > MAX_MAP_PLANES)
		Com_Error (ERR_DROP, "Map has too many planes");
		
	cmod->numplanes = count;
	cmod->planes = CM_Malloc( sizeof( *out ) * count );

	out = cmod->planes;
	for ( i=0 ; i<count ; i++, in++, out++)
	{
		out->normal[0] = LittleFloat( in->normal[0] );
		out->normal[1] = LittleFloat( in->normal[1] );
		out->normal[2] = LittleFloat( in->normal[2] );
		out->dist = LittleFloat (in->dist);
		//out->type = LittleLong (in->type);

		SetPlaneType( out );
		SetPlaneSignbits( out );
	}
}

/*
=================
CMod_LoadLeafBrushes
=================
*/
static void CMod_LoadLeafBrushes (lump_t *l)
{
	int			i;
	cbrush_t	**out;
	uint16	 	*in;
	int			count;
	uint16		brushNum;
	
	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadLeafBrushes: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count < 1)
		Com_Error (ERR_DROP, "Map with no leafbrushes");
	if (count > MAX_MAP_LEAFBRUSHES)
		Com_Error (ERR_DROP, "Map has too many leafbrushes");
	
	cmod->numleafbrushes = count;
	cmod->leafbrushes = CM_Malloc( sizeof( *out ) * count );

	out = cmod->leafbrushes;
	for ( i=0 ; i<count ; i++, in++, out++) {
		brushNum = LittleShort (*in);
		if( brushNum >= cmod->numbrushes ) {
			Com_Error (ERR_DROP, "CMod_LoadLeafBrushes: bad brushnum");
		}
		*out = cmod->brushes + brushNum;
	}
}

/*
=================
CMod_LoadBrushSides
=================
*/
static void CMod_LoadBrushSides (lump_t *l)
{
	int			i;
	cbrushside_t	*out;
	dbrushside_t 	*in;
	int			count;
	uint32			planeNum;
	uint16			texinfoNum;

	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadBrushSides: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count > MAX_MAP_BRUSHSIDES)
		Com_Error (ERR_DROP, "Map has too many brush sides");

	cmod->numbrushsides = count;
	cmod->brushsides = CM_Malloc( sizeof( *out ) * count );

	out = cmod->brushsides;
	for ( i=0 ; i<count ; i++, in++, out++)
	{
		planeNum = LittleShort (in->planenum);
		if( planeNum >= cmod->numplanes ) {
			Com_Error (ERR_DROP, "CMod_LoadBrushSides: bad planenum");
		}
		out->plane = cmod->planes + planeNum;
		texinfoNum = LittleShort (in->texinfo);
		if( texinfoNum == (uint16)-1 ) {
			out->surface = &nullsurface;
		} else {
			if (texinfoNum >= cmod->numtexinfo)
				Com_Error (ERR_DROP, "CMod_LoadBrushSides: bad texinfo");
			out->surface = cmod->surfaces + texinfoNum;
		}
	}
}

/*
=================
CMod_LoadAreas
=================
*/
static void CMod_LoadAreas (lump_t *l)
{
	int			i;
	carea_t		*out;
	darea_t 	*in;
	int			count;
	uint32		numPortals, firstPortal;

	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadAreas: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count > MAX_MAP_AREAS)
		Com_Error (ERR_DROP, "Map has too many areas");

	cmod->numareas = count;
	cmod->areas = CM_Malloc( sizeof( *out ) * count );

	out = cmod->areas;
	for ( i=0 ; i<count ; i++, in++, out++)
	{
		numPortals = LittleLong (in->numareaportals);
		firstPortal = LittleLong (in->firstareaportal);
		if( firstPortal + numPortals > cmod->numareaportals ) {
			Com_Error (ERR_DROP, "CMod_LoadAreas: bad areaportals");
		}
		out->numareaportals = numPortals;
		out->firstareaportal = firstPortal;
		out->floodvalid = 0;
	}
}

/*
=================
CMod_LoadAreaPortals
=================
*/
static void CMod_LoadAreaPortals (lump_t *l)
{
	int			i;
	careaportal_t		*out;
	dareaportal_t 	*in;
	int			count;
	uint32		portalNum, otherArea;

	in = (void *)(cmod_base + l->fileofs);
	if (l->filelen % sizeof(*in))
		Com_Error (ERR_DROP, "CMod_LoadAreaPortals: funny lump size");
	count = l->filelen / sizeof(*in);

	if (count > MAX_MAP_AREAS)
		Com_Error (ERR_DROP, "Map has too many areas");

	cmod->numareaportals = count;
	cmod->areaportals = CM_Malloc( sizeof( *out ) * count );

	out = cmod->areaportals;
	for ( i=0 ; i<count ; i++, in++, out++)
	{
		portalNum = LittleLong (in->portalnum);
		if( portalNum >= MAX_MAP_AREAPORTALS ) {
			Com_Error (ERR_DROP, "CMod_LoadAreaPortals: bad portalnum");
		}
		out->portalnum = portalNum;
		otherArea = LittleLong (in->otherarea);
		if( otherArea >= MAX_MAP_AREAS ) {
			Com_Error (ERR_DROP, "CMod_LoadAreaPortals: bad otherarea");
		}
		out->otherarea = otherArea;
	}
}

/*
=================
CMod_LoadVisibility
=================
*/
static void CMod_LoadVisibility( lump_t *l ) {
	int		i;
	uint32 numClusters;

	if( !l->filelen ) {
		return;
	}

	if( l->filelen > MAX_MAP_VISIBILITY )
		Com_Error (ERR_DROP, "Map has too large visibility lump");

	cmod->numvisibility = l->filelen;
	cmod->vis = CM_Malloc( l->filelen );
	memcpy( cmod->vis, cmod_base + l->fileofs, l->filelen );

	numClusters = LittleLong( cmod->vis->numclusters );
	cmod->vis->numclusters = numClusters;
	for( i = 0; i < numClusters; i++ ) {
		cmod->vis->bitofs[i][0] = LittleLong( cmod->vis->bitofs[i][0] );
		cmod->vis->bitofs[i][1] = LittleLong( cmod->vis->bitofs[i][1] );
	}

	cmod->visrowsize = ( numClusters + 7 ) >> 3;
}


/*
=================
CMod_LoadEntityString
=================
*/
static void CMod_LoadEntityString( lump_t *l ) {
	if( l->filelen > MAX_MAP_ENTSTRING ) {
		Com_Error( ERR_DROP, "Map has too large entity lump" );
	}

	cmod->numEntityChars = l->filelen;
	cmod->entitystring = CM_Malloc( l->filelen + 1 );
	memcpy( cmod->entitystring, cmod_base + l->fileofs, l->filelen );
	cmod->entitystring[l->filelen] = 0;
}

#if 0
/*
==================
CM_ConcatToEntityString
==================
*/
static void CM_ConcatToEntityString( const char *text ) {
	int len;

	len = strlen( text );
	if( numEntityChars + len > sizeof( cmod->entitystring ) - 1 ) {
		Com_Error( ERR_DROP, "CM_ConcatToEntityString: oversize entity lump" );
	}

	strcpy( cmod->entitystring + numEntityChars, text );
	numEntityChars += len;
}

/*
==================
CM_ParseMapFile

Parses complete *.map file
==================
*/
static qboolean CM_ParseMapFile( const char *data ) {
	char *token;
	int numInlineModels;
	qboolean inlineModel;
	char buffer[MAX_STRING_CHARS];
	char key[MAX_TOKEN_CHARS];

	numInlineModels = 0;

	while( 1 ) {
		token = COM_Parse( ( const char ** )&data );
		if( !data ) {
			break;
		}

		if( *token != '{' ) {
			Com_WPrintf( "CM_ParseMapFile: expected '{', found '%s'\n", token );
			return qfalse;
		}

		CM_ConcatToEntityString( "{ " );

		inlineModel = qfalse;

		// Parse entity
		while( 1 ) {
			token = COM_Parse( ( const char ** )&data );
			if( !data ) {
				Com_WPrintf( "CM_ParseMapFile: expected key, found EOF\n" );
				return qfalse;
			}

			if( *token == '}' ) {
				// FIXME HACK: restore inline model number
				// This may not work properly if the entity order is different!!!
				if( inlineModel && numInlineModels ) {
					Com_sprintf( buffer, sizeof( buffer ), "\"model\" \"*%i\" } ", numInlineModels );
					CM_ConcatToEntityString( buffer );
				} else {
					CM_ConcatToEntityString( "} " );
				}

				if( inlineModel ) {
					numInlineModels++;
				}

				break;
			}

			if( *token == '{' ) {
				inlineModel = qtrue;

				// Parse brush
				while( 1 ) {
					token = COM_Parse( ( const char ** )&data );
					if( !data ) {
						Com_WPrintf( "CM_ParseMapFile: expected brush data, found EOF\n" );
						return qfalse;
					}

					if( *token == '}' ) {
						break;
					}

					if( *token == '{' ) {
						Com_WPrintf( "CM_ParseMapFile: expected brush data, found '{'\n" );
						return qfalse;
					}

				}

				continue;
			}

			Q_strncpyz( key, token, sizeof( key ) );

			token = COM_Parse( ( const char ** )&data );
			if( !data ) {
				Com_WPrintf( "CM_ParseMapFile: expected value, found EOF\n" );
				return qfalse;
			}

			if( *token == '}' || *token == '{' ) {
				Com_WPrintf( "CM_ParseMapFile: expected value, found '%s'\n", token );
				return qfalse;
			}

			Com_sprintf( buffer, sizeof( buffer ), "\"%s\" \"%s\" ", key, token );

			CM_ConcatToEntityString( buffer );

		}
	}

	cmod->entitystring[numEntityChars] = 0;

	if( numInlineModels != numcmodels + 1 ) {
		Com_WPrintf( "CM_ParseMapFile: mismatched number of inline models\n" );
		return qfalse;
	}

	return qtrue;
}
#endif

#define CM_CACHESIZE		16

static cmcache_t	cm_cache[CM_CACHESIZE];

void CM_FreeMap( cm_t *cm ) {
	cmcache_t *cache;

	if( cm->floodnums ) {
		Z_Free( cm->floodnums );
	}

	cache = cm->cache;
	if( cache ) {
		if( cache->refcount <= 0 ) {
			Com_Error( ERR_FATAL, "CM_FreeMap: negative refcount" );
		}
		if( --cache->refcount == 0 ) {

#define CM_FREE( f ) \
	do { \
		if( cache->f ) { \
			CM_Free( cache->f ); \
		} \
	} while( 0 )

			CM_FREE( vis );
			CM_FREE( surfaces );
			CM_FREE( planes );
			CM_FREE( brushsides );
			CM_FREE( brushes );
			CM_FREE( leafbrushes );
			CM_FREE( areaportals );
			CM_FREE( areas );
			CM_FREE( leafs );
			CM_FREE( nodes );
			CM_FREE( cmodels );
			CM_FREE( entitystring );

			memset( cache, 0, sizeof( *cache ) );
		}
	}

	memset( cm, 0, sizeof( *cm ) );
}


/*
==================
CM_LoadMap

Loads in the map and all submodels
==================
*/
qboolean CM_LoadMap( cm_t *cm, const char *name, uint32 flags, uint32 *checksum ) {
	cmcache_t		*cache;
	byte			*buf;
	int				i;
	dheader_t		header;
	int				length;
//	char *entstring;
//	char buffer[MAX_QPATH];

	if( !name ) {
		Com_Error( ERR_FATAL, "CM_LoadMap: NULL name" );
	}
	if( !name[0] ) {
		Com_Error( ERR_DROP, "CM_LoadMap: empty name" );
	}

	for( i = 0, cache = cm_cache; i < CM_CACHESIZE; i++, cache++ ) {
		if( !cache->name[0] ) {
			continue;
		}
		if( strcmp( cache->name, name ) ) {
			continue;
		}
		//if( !( cache->flags & CM_LOAD_VISONLY ) || ( flags & CM_LOAD_VISONLY ) )
		{
			cm->cache = cache;
			if( checksum ) {
				*checksum = cache->checksum;
			}
			cm->cache = cache;
			if( flags & CM_LOAD_CLIENT ) {
				cm->floodnums = NULL;
				cm->portalopen = NULL;
			} else {
				cm->floodnums = CM_Mallocz( sizeof( int ) * cache->numareas +
					sizeof( qboolean ) * cache->numareaportals );
				cm->portalopen = ( qboolean * )( cm->floodnums + cache->numareas );
				CM_FloodAreaConnections( cm );
			}
			cache->refcount++;
			return qtrue; // still have the right version
		}
		break;
	}

	// find a free slot
	for( i = 0, cache = cm_cache; i < CM_CACHESIZE; i++, cache++ ) {
		if( !cache->name[0] ) {
			break;
		}
	}
	if( i == CM_CACHESIZE ) {
		Com_Error( ERR_DROP, "Out of cache slots to load %s", name );
	}

	//
	// load the file
	//
	length = FS_LoadFileEx( name, (void **)&buf, FS_FLAG_CACHE );
	if( !buf )
		Com_Error( ERR_DROP, "Couldn't load %s", name );

	cache->checksum = LittleLong( Com_BlockChecksum( buf, length ) );
	if( checksum ) {
		*checksum = cache->checksum;
	}

	header = *( dheader_t * )buf;
	for( i = 0; i < sizeof( dheader_t )/4; i++ )
		(( uint32 * )&header)[i] = LittleLong( (( uint32 * )&header)[i] );

	if( header.version != BSPVERSION ) {
		Com_Error( ERR_DROP, "CM_LoadMap: %s has wrong version number (%i should be %i)",
			name, header.version, BSPVERSION );
	}

	cmod_base = buf;
	cmod = cache;

#define CM_LOAD( Func, Lump ) \
	do { \
		CMod_Load##Func( &header.lumps[LUMP_##Lump] ); \
	} while( 0 )

	// load into heap
	CM_LOAD( Visibility, VISIBILITY );
	CM_LOAD( Surfaces, TEXINFO );
	CM_LOAD( Planes, PLANES );
	CM_LOAD( BrushSides, BRUSHSIDES );
	CM_LOAD( Brushes, BRUSHES );
	CM_LOAD( LeafBrushes, LEAFBRUSHES );
	CM_LOAD( AreaPortals, AREAPORTALS );
	CM_LOAD( Areas, AREAS );
	CM_LOAD( Leafs, LEAFS );
	CM_LOAD( Nodes, NODES );
	CM_LOAD( Submodels, MODELS );

#if 0
	// Load the entity string from file, if specified
	entstring = NULL;
	if( cmod->load_entities->integer && !clientload ) {
		COM_StripExtension( name, buffer, sizeof( buffer ) );
		Q_strcat( buffer, sizeof( buffer ), ".map" );
		FS_LoadFile( buffer, ( void ** )&entstring );
	}

	if( entstring ) {
		Com_Printf( "Loading %s...\n", buffer );
		if( !CM_ParseMapFile( entstring ) ) {
			CMod_LoadEntityString( &header.lumps[LUMP_ENTITIES] );
		}
		FS_FreeFile( entstring );
	} else {
		CMod_LoadEntityString( &header.lumps[LUMP_ENTITIES] );
	}
#else
	CMod_LoadEntityString( &header.lumps[LUMP_ENTITIES] );
#endif

	FS_FreeFile( buf );

	cache->refcount = 1;
	cm->cache = cache;

	if( flags & CM_LOAD_CLIENT ) {
		cm->floodnums = NULL;
		cm->portalopen = NULL;
	} else {
		cm->floodnums = CM_Mallocz( sizeof( int ) * cache->numareas +
			sizeof( qboolean ) * cache->numareaportals );
		cm->portalopen = ( qboolean * )( cm->floodnums + cache->numareas );
		CM_FloodAreaConnections( cm );
	}

	Q_strncpyz( cache->name, name, sizeof( cache->name ) );

	//map_load_entities->modified = qfalse;

	return qtrue;
}

/*
==================
CM_InlineModel
==================
*/
cmodel_t *CM_InlineModel( cm_t *cm, const char *name ) {
	int		num;
	cmodel_t *cmodel;

	if( !name || name[0] != '*' ) {
		Com_Error( ERR_DROP, "CM_InlineModel: bad name: %s", name );
	}
	if( !cm->cache ) {
		Com_Error( ERR_DROP, "CM_InlineModel: NULL cache" );
	}
	num = atoi( name + 1 );
	if( num < 1 || num >= cm->cache->numcmodels ) {
		Com_Error ( ERR_DROP, "CM_InlineModel: bad number: %d", num );
	}

	cmodel = &cm->cache->cmodels[num];

	return cmodel;
}

int CM_NumClusters( cm_t *cm ) {
	if( !cm->cache ) {
		return 0;
	}
	return cm->cache->numclusters;
}

int CM_NumInlineModels( cm_t *cm ) {
	if( !cm->cache ) {
		return 0;
	}
	return cm->cache->numcmodels;
}

char *CM_EntityString( cm_t *cm ) {
	if( !cm->cache ) {
		return "";
	}
	if( !cm->cache->entitystring ) {
		return "";
	}
	return cm->cache->entitystring;
}

cnode_t *CM_NodeNum( cm_t *cm, int number ) {
	if( !cm->cache ) {
		Com_Error( ERR_DROP, "CM_NodeNum: NULL cache" );
	}
	if( number == -1 ) {
		return ( cnode_t * )cm->cache->leafs; // special case for solid leaf
	}
	if( number < 0 || number >= cm->cache->numnodes ) {
		Com_Error( ERR_DROP, "CM_NodeNum: bad number: %d", number );
	}
	return cm->cache->nodes + number;
}

cleaf_t *CM_LeafNum( cm_t *cm, int number ) {
	if( !cm->cache ) {
		Com_Error( ERR_DROP, "CM_LeafNum: NULL cache" );
	}
	if( number < 0 || number >= cm->cache->numleafs ) {
		Com_Error( ERR_DROP, "CM_LeafNum: bad number: %d", number );
	}
	return cm->cache->leafs + number;
}

//=======================================================================

static cplane_t	box_planes[12];
static cnode_t	box_nodes[6];
static cnode_t	*box_headnode;
static cbrush_t	box_brush;
static cbrush_t *box_leafbrush;
static cbrushside_t box_brushsides[6];
static cleaf_t	box_leaf;
static cleaf_t	box_emptyleaf;

/*
===================
CM_InitBoxHull

Set up the planes and nodes so that the six floats of a bounding box
can just be stored out and get a proper clipping hull structure.
===================
*/
static void CM_InitBoxHull( void ) {
	int			i;
	int			side;
	cnode_t		*c;
	cplane_t	*p;
	cbrushside_t	*s;

	box_headnode = &box_nodes[0];

	box_brush.numsides = 6;
	box_brush.firstbrushside = &box_brushsides[0];
	box_brush.contents = CONTENTS_MONSTER;

	box_leaf.contents = CONTENTS_MONSTER;
	box_leaf.firstleafbrush = &box_leafbrush;
	box_leaf.numleafbrushes = 1;

	box_leafbrush = &box_brush;

	for( i = 0; i < 6; i++ ) {
		side = i & 1;

		// brush sides
		s = &box_brushsides[i];
		s->plane = &box_planes[i*2+side];
		s->surface = &nullsurface;

		// nodes
		c = &box_nodes[i];
		c->plane = &box_planes[i*2];
		c->children[side] = ( cnode_t * )&box_emptyleaf;
		if( i != 5 )
			c->children[side^1] = &box_nodes[i + 1];
		else
			c->children[side^1] = ( cnode_t * )&box_leaf;

		// planes
		p = &box_planes[i*2];
		p->type = i >> 1;
		p->signbits = 0;
		VectorClear( p->normal );
		p->normal[i>>1] = 1;

		p = &box_planes[i*2+1];
		p->type = 3 + ( i >> 1 );
		p->signbits = 0;
		VectorClear( p->normal );
		p->normal[i>>1] = -1;
	}	
}


/*
===================
CM_HeadnodeForBox

To keep everything totally uniform, bounding boxes are turned into small
BSP trees instead of being compared directly.
===================
*/
cnode_t *CM_HeadnodeForBox( vec3_t mins, vec3_t maxs ) {
	box_planes[0].dist = maxs[0];
	box_planes[1].dist = -maxs[0];
	box_planes[2].dist = mins[0];
	box_planes[3].dist = -mins[0];
	box_planes[4].dist = maxs[1];
	box_planes[5].dist = -maxs[1];
	box_planes[6].dist = mins[1];
	box_planes[7].dist = -mins[1];
	box_planes[8].dist = maxs[2];
	box_planes[9].dist = -maxs[2];
	box_planes[10].dist = mins[2];
	box_planes[11].dist = -mins[2];

	return box_headnode;
}


/*
==================
CM_PointLeaf_r

==================
*/
static cleaf_t *CM_PointLeaf_r( vec3_t p, cnode_t *node ) {
	float		d;

	while( node->plane ) {
		d = PlaneDiffFast( p, node->plane );
		if( d < 0 )
			node = node->children[1];
		else
			node = node->children[0];
	}

	return ( cleaf_t * )node;
}

cleaf_t *CM_PointLeaf( cm_t *cm, vec3_t p ) {
	if( !cm->cache ) {
		return &nullleaf;		// server may call this without map loaded
	}
	return CM_PointLeaf_r( p, cm->cache->nodes );
}


/*
=============
CM_BoxLeafnums

Fills in a list of all the leafs touched
=============
*/
static int		leaf_count, leaf_maxcount;
static cleaf_t	**leaf_list;
static float	*leaf_mins, *leaf_maxs;
static cnode_t	*leaf_topnode;

static void CM_BoxLeafs_r( cnode_t *node ) {
	cplane_t	*plane;
	int		s;

	while( 1 ) {
		plane = node->plane;
		if( !plane ) {
			if( leaf_count >= leaf_maxcount ) {
				return;
			}
			leaf_list[leaf_count++] = ( cleaf_t * )node;
			return;
		}
	
		s = BoxOnPlaneSideFast( leaf_mins, leaf_maxs, plane );
		if( s == 1 ) {
			node = node->children[0];
		} else if( s == 2 ) {
			node = node->children[1];
		} else {
			// go down both
			if( !leaf_topnode ) {
				leaf_topnode = node;
			}
			CM_BoxLeafs_r( node->children[0] );
			node = node->children[1];
		}

	}
}

static int CM_BoxLeafs_headnode( vec3_t mins, vec3_t maxs, cleaf_t **list, int listsize,
									cnode_t *headnode, cnode_t **topnode )
{
	leaf_list = list;
	leaf_count = 0;
	leaf_maxcount = listsize;
	leaf_mins = mins;
	leaf_maxs = maxs;

	leaf_topnode = NULL;

	CM_BoxLeafs_r( headnode );

	if( topnode )
		*topnode = leaf_topnode;

	return leaf_count;
}

int	CM_BoxLeafs( cm_t *cm, vec3_t mins, vec3_t maxs, cleaf_t **list, int listsize, cnode_t **topnode ) {
	if( !cm->cache )	// map not loaded
		return 0;
	return CM_BoxLeafs_headnode( mins, maxs, list,
		listsize, cm->cache->nodes, topnode );
}



/*
==================
CM_PointContents

==================
*/
int CM_PointContents( vec3_t p, cnode_t *headnode ) {
	cleaf_t		*leaf;

	if( !headnode ) {
		return 0;
	}

	leaf = CM_PointLeaf_r( p, headnode );

	return leaf->contents;
}

/*
==================
CM_TransformedPointContents

Handles offseting and rotation of the end points for moving and
rotating entities
==================
*/
int	CM_TransformedPointContents( vec3_t p, cnode_t *headnode, vec3_t origin, vec3_t angles ) {
	vec3_t		p_l;
	vec3_t		temp;
	vec3_t		forward, right, up;
	cleaf_t		*leaf;

	if( !headnode ) {
		return 0;
	}

	// subtract origin offset
	VectorSubtract (p, origin, p_l);

	// rotate start and end into the models frame of reference
	if (headnode != box_headnode && 
	(angles[0] || angles[1] || angles[2]) )
	{
		AngleVectors (angles, forward, right, up);

		VectorCopy (p_l, temp);
		p_l[0] = DotProduct (temp, forward);
		p_l[1] = -DotProduct (temp, right);
		p_l[2] = DotProduct (temp, up);
	}

	leaf = CM_PointLeaf_r( p_l, headnode );

	return leaf->contents;
}


/*
===============================================================================

BOX TRACING

===============================================================================
*/

// 1/32 epsilon to keep floating point happy
#define	DIST_EPSILON	(0.03125)

static vec3_t	trace_start, trace_end;
static vec3_t	trace_mins, trace_maxs;
static vec3_t	trace_extents;

static trace_t	*trace_trace;
static int		trace_contents;
static qboolean	trace_ispoint;		// optimized case

/*
================
CM_ClipBoxToBrush
================
*/
static void CM_ClipBoxToBrush (vec3_t mins, vec3_t maxs, vec3_t p1, vec3_t p2,
					  trace_t *trace, cbrush_t *brush)
{
	int			i, j;
	cplane_t	*plane, *clipplane;
	float		dist;
	float		enterfrac, leavefrac;
	vec3_t		ofs;
	float		d1, d2;
	qboolean	getout, startout;
	float		f;
	cbrushside_t	*side, *leadside;

	enterfrac = -1;
	leavefrac = 1;
	clipplane = NULL;

	if (!brush->numsides)
		return;

	getout = qfalse;
	startout = qfalse;
	leadside = NULL;

	side = brush->firstbrushside;
	for (i=0 ; i<brush->numsides ; i++, side++)
	{
		plane = side->plane;

		// FIXME: special case for axial

		if (!trace_ispoint)
		{	// general box case

			// push the plane out apropriately for mins/maxs

			// FIXME: use signbits into 8 way lookup for each mins/maxs
			for (j=0 ; j<3 ; j++)
			{
				if (plane->normal[j] < 0)
					ofs[j] = maxs[j];
				else
					ofs[j] = mins[j];
			}
			dist = DotProduct (ofs, plane->normal);
			dist = plane->dist - dist;
		}
		else
		{	// special point case
			dist = plane->dist;
		}

		d1 = DotProduct (p1, plane->normal) - dist;
		d2 = DotProduct (p2, plane->normal) - dist;

		if (d2 > 0)
			getout = qtrue;	// endpoint is not in solid
		if (d1 > 0)
			startout = qtrue;

		// if completely in front of face, no intersection
		if (d1 > 0 && d2 >= d1)
			return;

		if (d1 <= 0 && d2 <= 0)
			continue;

		// crosses face
		if (d1 > d2)
		{	// enter
			f = (d1-DIST_EPSILON) / (d1-d2);
			if (f > enterfrac)
			{
				enterfrac = f;
				clipplane = plane;
				leadside = side;
			}
		}
		else
		{	// leave
			f = (d1+DIST_EPSILON) / (d1-d2);
			if (f < leavefrac)
				leavefrac = f;
		}
	}

	if (!startout)
	{	// original point was inside brush
        trace->fraction = 0;
		trace->startsolid = qtrue;
		if (!getout)
			trace->allsolid = qtrue;
		return;
	}
	if (enterfrac < leavefrac)
	{
		if (enterfrac > -1 && enterfrac < trace->fraction)
		{
			if (enterfrac < 0)
				enterfrac = 0;
			trace->fraction = enterfrac;
			trace->plane = *clipplane;
			trace->surface = &(leadside->surface->c);
			trace->contents = brush->contents;
		}
	}
}

/*
================
CM_TestBoxInBrush
================
*/
static void CM_TestBoxInBrush (vec3_t mins, vec3_t maxs, vec3_t p1,
					  trace_t *trace, cbrush_t *brush)
{
	int			i, j;
	cplane_t	*plane;
	float		dist;
	vec3_t		ofs;
	float		d1;
	cbrushside_t	*side;

	if (!brush->numsides)
		return;

	side = brush->firstbrushside;
	for (i=0 ; i<brush->numsides ; i++, side++)
	{
		plane = side->plane;

		// FIXME: special case for axial

		// general box case

		// push the plane out apropriately for mins/maxs

		// FIXME: use signbits into 8 way lookup for each mins/maxs
		for (j=0 ; j<3 ; j++)
		{
			if (plane->normal[j] < 0)
				ofs[j] = maxs[j];
			else
				ofs[j] = mins[j];
		}
		dist = DotProduct (ofs, plane->normal);
		dist = plane->dist - dist;

		d1 = DotProduct (p1, plane->normal) - dist;

		// if completely in front of face, no intersection
		if (d1 > 0)
			return;

	}

	// inside this brush
	trace->startsolid = trace->allsolid = qtrue;
	trace->fraction = 0;
	trace->contents = brush->contents;
}


/*
================
CM_TraceToLeaf
================
*/
static void CM_TraceToLeaf ( cleaf_t *leaf )
{
	int			k;
	cbrush_t	*b, **leafbrush;

	if( !( leaf->contents & trace_contents ) )
		return;
	// trace line against all brushes in the leaf
	leafbrush = leaf->firstleafbrush;
	for (k=0 ; k<leaf->numleafbrushes ; k++, leafbrush++)
	{
		b = *leafbrush;
		if (b->checkcount == checkcount)
			continue;	// already checked this brush in another leaf
		b->checkcount = checkcount;

		if ( !(b->contents & trace_contents))
			continue;
		CM_ClipBoxToBrush (trace_mins, trace_maxs, trace_start, trace_end, trace_trace, b);
		if (!trace_trace->fraction)
			return;
	}

}


/*
================
CM_TestInLeaf
================
*/
static void CM_TestInLeaf ( cleaf_t *leaf )
{
	int			k;
	cbrush_t	*b, **leafbrush;

	if( !( leaf->contents & trace_contents ) )
		return;
	// trace line against all brushes in the leaf
	leafbrush = leaf->firstleafbrush;
	for (k=0 ; k<leaf->numleafbrushes ; k++, leafbrush++)
	{
		b = *leafbrush;
		if (b->checkcount == checkcount)
			continue;	// already checked this brush in another leaf
		b->checkcount = checkcount;

		if ( !(b->contents & trace_contents))
			continue;
		CM_TestBoxInBrush (trace_mins, trace_maxs, trace_start, trace_trace, b);
		if (!trace_trace->fraction)
			return;
	}

}


/*
==================
CM_RecursiveHullCheck

==================
*/
static void CM_RecursiveHullCheck ( cnode_t *node, float p1f, float p2f, vec3_t p1, vec3_t p2)
{
	cplane_t	*plane;
	float		t1, t2, offset;
	float		frac, frac2;
	float		idist;
	int			i;
	vec3_t		mid;
	int			side;
	float		midf;

	if (trace_trace->fraction <= p1f)
		return;		// already hit something nearer

	// if plane is NULL, we are in a leaf node
	plane = node->plane;
	if (!plane)
	{
		CM_TraceToLeaf ( ( cleaf_t * )node );
		return;
	}

	//
	// find the point distances to the seperating plane
	// and the offset for the size of the box
	//
	if (plane->type < 3)
	{
		t1 = p1[plane->type] - plane->dist;
		t2 = p2[plane->type] - plane->dist;
		offset = trace_extents[plane->type];
	}
	else
	{
		t1 = DotProduct (plane->normal, p1) - plane->dist;
		t2 = DotProduct (plane->normal, p2) - plane->dist;
		if (trace_ispoint)
			offset = 0;
		else
			offset = fabs(trace_extents[0]*plane->normal[0]) +
				fabs(trace_extents[1]*plane->normal[1]) +
				fabs(trace_extents[2]*plane->normal[2]);
	}

	// see which sides we need to consider
	if (t1 >= offset && t2 >= offset)
	{
		CM_RecursiveHullCheck (node->children[0], p1f, p2f, p1, p2);
		return;
	}
	if (t1 < -offset && t2 < -offset)
	{
		CM_RecursiveHullCheck (node->children[1], p1f, p2f, p1, p2);
		return;
	}

	// put the crosspoint DIST_EPSILON pixels on the near side
	if (t1 < t2)
	{
		idist = 1.0/(t1-t2);
		side = 1;
		frac2 = (t1 + offset + DIST_EPSILON)*idist;
		frac = (t1 - offset + DIST_EPSILON)*idist;
	}
	else if (t1 > t2)
	{
		idist = 1.0/(t1-t2);
		side = 0;
		frac2 = (t1 - offset - DIST_EPSILON)*idist;
		frac = (t1 + offset + DIST_EPSILON)*idist;
	}
	else
	{
		side = 0;
		frac = 1;
		frac2 = 0;
	}

	// move up to the node
	if (frac < 0)
		frac = 0;
	if (frac > 1)
		frac = 1;
		
	midf = p1f + (p2f - p1f)*frac;
	for (i=0 ; i<3 ; i++)
		mid[i] = p1[i] + frac*(p2[i] - p1[i]);

	CM_RecursiveHullCheck (node->children[side], p1f, midf, p1, mid);


	// go past the node
	if (frac2 < 0)
		frac2 = 0;
	if (frac2 > 1)
		frac2 = 1;
		
	midf = p1f + (p2f - p1f)*frac2;
	for (i=0 ; i<3 ; i++)
		mid[i] = p1[i] + frac2*(p2[i] - p1[i]);

	CM_RecursiveHullCheck (node->children[side^1], midf, p2f, mid, p2);
}



//======================================================================

/*
==================
CM_BoxTrace
==================
*/
void CM_BoxTrace( trace_t *trace, vec3_t start, vec3_t end,
					vec3_t mins, vec3_t maxs,
					cnode_t *headnode, int brushmask )
{
	int		i;

	checkcount++;		// for multi-check avoidance

	// fill in a default trace
	trace_trace = trace;
	memset (trace_trace, 0, sizeof( *trace_trace ));
	trace_trace->fraction = 1;
	trace_trace->surface = &(nullsurface.c);

	if( !headnode ) {
		return;
	}

	trace_contents = brushmask;
	VectorCopy (start, trace_start);
	VectorCopy (end, trace_end);
	VectorCopy (mins, trace_mins);
	VectorCopy (maxs, trace_maxs);

	//
	// check for position test special case
	//
	if (start[0] == end[0] && start[1] == end[1] && start[2] == end[2])
	{
		cleaf_t		*leafs[1024];
		int		i, numleafs;
		vec3_t	c1, c2;

		VectorAdd (start, mins, c1);
		VectorAdd (start, maxs, c2);
		for (i=0 ; i<3 ; i++)
		{
			c1[i] -= 1;
			c2[i] += 1;
		}

		numleafs = CM_BoxLeafs_headnode (c1, c2, leafs, 1024, headnode, NULL);
		for (i=0 ; i<numleafs ; i++)
		{
			CM_TestInLeaf (leafs[i]);
			if (trace_trace->allsolid)
				break;
		}
		VectorCopy (start, trace_trace->endpos);
		return;
	}

	//
	// check for point special case
	//
	if (mins[0] == 0 && mins[1] == 0 && mins[2] == 0
		&& maxs[0] == 0 && maxs[1] == 0 && maxs[2] == 0)
	{
		trace_ispoint = qtrue;
		VectorClear (trace_extents);
	}
	else
	{
		trace_ispoint = qfalse;
		trace_extents[0] = -mins[0] > maxs[0] ? -mins[0] : maxs[0];
		trace_extents[1] = -mins[1] > maxs[1] ? -mins[1] : maxs[1];
		trace_extents[2] = -mins[2] > maxs[2] ? -mins[2] : maxs[2];
	}

	//
	// general sweeping through world
	//
	CM_RecursiveHullCheck (headnode, 0, 1, start, end);

	if (trace_trace->fraction == 1)
	{
		VectorCopy (end, trace_trace->endpos);
	}
	else
	{
		for (i=0 ; i<3 ; i++)
			trace_trace->endpos[i] = start[i] + trace_trace->fraction * (end[i] - start[i]);
	}
	
}


/*
==================
CM_TransformedBoxTrace

Handles offseting and rotation of the end points for moving and
rotating entities
==================
*/
void CM_TransformedBoxTrace ( trace_t *trace, vec3_t start, vec3_t end,
						  vec3_t mins, vec3_t maxs,
						  cnode_t *headnode, int brushmask,
						  vec3_t origin, vec3_t angles)
{
	vec3_t		start_l, end_l;
	vec3_t		a;
	vec3_t		forward, right, up;
	vec3_t		temp;
	qboolean	rotated;

	// subtract origin offset
	VectorSubtract (start, origin, start_l);
	VectorSubtract (end, origin, end_l);

	// rotate start and end into the models frame of reference
	if (headnode != box_headnode && 
	(angles[0] || angles[1] || angles[2]) )
		rotated = qtrue;
	else
		rotated = qfalse;

	if (rotated)
	{
		AngleVectors (angles, forward, right, up);

		VectorCopy (start_l, temp);
		start_l[0] = DotProduct (temp, forward);
		start_l[1] = -DotProduct (temp, right);
		start_l[2] = DotProduct (temp, up);

		VectorCopy (end_l, temp);
		end_l[0] = DotProduct (temp, forward);
		end_l[1] = -DotProduct (temp, right);
		end_l[2] = DotProduct (temp, up);
	}

	// sweep the box through the model
	CM_BoxTrace ( trace, start_l, end_l, mins, maxs, headnode, brushmask);

	if (rotated && trace->fraction != 1.0)
	{
		// FIXME: figure out how to do this with existing angles
		VectorNegate (angles, a);
		AngleVectors (a, forward, right, up);

		VectorCopy (trace->plane.normal, temp);
		trace->plane.normal[0] = DotProduct (temp, forward);
		trace->plane.normal[1] = -DotProduct (temp, right);
		trace->plane.normal[2] = DotProduct (temp, up);
	}

	trace->endpos[0] = start[0] + trace->fraction * (end[0] - start[0]);
	trace->endpos[1] = start[1] + trace->fraction * (end[1] - start[1]);
	trace->endpos[2] = start[2] + trace->fraction * (end[2] - start[2]);
}

/*
===============================================================================

PVS / PHS

===============================================================================
*/

static byte		pvsrow[MAX_MAP_LEAFS/8];
static byte		phsrow[MAX_MAP_LEAFS/8];

/*
===================
CM_DecompressVis
===================
*/
static void CM_DecompressVis( byte *in, byte *out, int rowsize ) {
	int		c;
	byte	*out_p;

	out_p = out;
	do {
		if( *in ) {
			*out_p++ = *in++;
			continue;
		}
	
		c = in[1];
		in += 2;
		if( ( out_p - out ) + c > rowsize ) {
			c = rowsize - ( out_p - out );
			Com_WPrintf( "CM_DecompressVis: overrun\n" );
		}
		while( c ) {
			*out_p++ = 0;
			c--;
		}
	} while( out_p - out < rowsize );
}

byte *CM_ClusterPVS( cm_t *cm, int cluster ) {
	cmcache_t *cache = cm->cache;

	if( !cache || !cache->vis ) {
		memset( pvsrow, 0xff, sizeof( pvsrow ) );
	} else if( cluster == -1 ) {
		memset( pvsrow, 0, cache->visrowsize );
	} else {
        if( cluster < 0 || cluster >= cache->vis->numclusters ) {
            Com_Error( ERR_DROP, "CM_ClusterPVS: bad cluster" );
        }
		CM_DecompressVis( ( byte * )cache->vis + cache->vis->bitofs[cluster][DVIS_PVS],
			pvsrow, cache->visrowsize );
	}
	return pvsrow;
}

byte *CM_ClusterPHS( cm_t *cm, int cluster ) {
	cmcache_t *cache = cm->cache;

	if( !cache || !cache->vis ) {
		memset( phsrow, 0xff, sizeof( phsrow ) );
	} else if( cluster == -1 ) {
		memset( phsrow, 0, cache->visrowsize );
	} else {
        if( cluster < 0 || cluster >= cache->vis->numclusters ) {
            Com_Error( ERR_DROP, "CM_ClusterPVS: bad cluster" );
        }
		CM_DecompressVis( ( byte * )cache->vis + cache->vis->bitofs[cluster][DVIS_PHS],
			phsrow, cache->visrowsize );
	}
	return phsrow;
}

/*
===============================================================================

AREAPORTALS

===============================================================================
*/

static void FloodArea_r( cm_t *cm, int number, int floodnum ) {
	int		i;
	careaportal_t	*p;
	carea_t *area;

	area = &cm->cache->areas[number];
	if( area->floodvalid == floodvalid ) {
		if( cm->floodnums[number] == floodnum )
			return;
		Com_Error( ERR_DROP, "FloodArea_r: reflooded" );
	}

	cm->floodnums[number] = floodnum;
	area->floodvalid = floodvalid;
	p = &cm->cache->areaportals[area->firstareaportal];
	for( i = 0; i < area->numareaportals; i++, p++ ) {
		if( cm->portalopen[p->portalnum] )
			FloodArea_r( cm, p->otherarea, floodnum );
	}
}

/*
====================
CM_FloodAreaConnections
====================
*/
void CM_FloodAreaConnections( cm_t *cm ) {
	int		i;
	carea_t	*area;
	int		floodnum;

	// all current floods are now invalid
	floodvalid++;
	floodnum = 0;

	// area 0 is not used
	for( i = 1; i < cm->cache->numareas; i++ ) {
		area = &cm->cache->areas[i];
		if( area->floodvalid == floodvalid )
			continue;		// already flooded into
		floodnum++;
		FloodArea_r( cm, i, floodnum );
	}

}

void CM_SetAreaPortalState( cm_t *cm, int portalnum, qboolean open ) {
	if( portalnum > cm->cache->numareaportals )
		Com_Error( ERR_DROP, "CM_SetAreaPortalState: areaportal > numareaportals" );

	cm->portalopen[portalnum] = open;
	CM_FloodAreaConnections( cm );
}

qboolean CM_AreasConnected( cm_t *cm, int area1, int area2 ) {
	cmcache_t *cache = cm->cache;

    if( !cache ) {
        return qfalse;
    }
	if( map_noareas->integer )
		return qtrue;

	if( area1 > cache->numareas || area2 > cache->numareas )
		Com_Error( ERR_DROP, "CM_AreasConnected: area > numareas" );

	if( cm->floodnums[area1] == cm->floodnums[area2] )
		return qtrue;

	return qfalse;
}


/*
=================
CM_WriteAreaBits

Writes a length byte followed by a bit vector of all the areas
that area in the same flood as the area parameter

This is used by the client refreshes to cull visibility
=================
*/
int CM_WriteAreaBits( cm_t *cm, byte *buffer, int area ) {
	cmcache_t *cache = cm->cache;
	int		i;
	int		floodnum;
	int		bytes;

    if( !cache ) {
        return 0;
    }

	bytes = ( cache->numareas + 7 ) >> 3;

	if( map_noareas->integer || !area ) {
		// for debugging, send everything
		memset( buffer, 255, bytes );
	} else {
		memset( buffer, 0, bytes );

		floodnum = cm->floodnums[area];
		for ( i = 0; i < cache->numareas; i++) {
			if( cm->floodnums[i] == floodnum ) {
				Q_SetBit( buffer, i );
			}
		}
	}

	return bytes;
}

int CM_WritePortalBits( cm_t *cm, byte *buffer ) {
	int		i, bytes, numportals;

    if( !cm->cache ) {
        return 0;
    }

	numportals = cm->cache->numareaportals;
	if( numportals > MAX_MAP_AREAS ) {
		/* HACK: use the same array size as areabytes!
		 * It is nonsense for a map to have > 256 areaportals anyway. */
		Com_WPrintf( "CM_WritePortalBits: too many areaportals\n" );
		numportals = MAX_MAP_AREAS;
	}

	bytes = ( numportals + 7 ) >> 3;
	memset( buffer, 0, bytes );
	for( i = 0; i < numportals; i++ ) {
		if( cm->portalopen[i] ) {
			Q_SetBit( buffer, i );
		}
	}

	return bytes;
}

void CM_SetPortalStates( cm_t *cm, byte *buffer, int bytes ) {
	int		i, numportals;

	if( !bytes ) {
        for( i = 0; i < cm->cache->numareaportals; i++ ) {
            cm->portalopen[i] = qtrue;
        }
	} else {
        numportals = bytes << 3;
        if( numportals > cm->cache->numareaportals ) {
            numportals = cm->cache->numareaportals;
        }
        for( i = 0; i < numportals; i++ ) {
            cm->portalopen[i] = Q_IsBitSet( buffer, i ) ? qtrue : qfalse;
        }
    }

	CM_FloodAreaConnections( cm );
}


/*
===================
CM_WritePortalState

Writes the portal state to a savegame file
===================
*/
void CM_WritePortalState( cm_t *cm, fileHandle_t f ) {
	// FIXME: incompatible savegames
	FS_Write( cm->portalopen, sizeof( qboolean ) * cm->cache->numareaportals, f );
}

/*
===================
CM_ReadPortalState

Reads the portal state from a savegame file
and recalculates the area connections
===================
*/
void CM_ReadPortalState( cm_t *cm, fileHandle_t f ) {
	FS_Read( cm->portalopen, sizeof( qboolean ) * cm->cache->numareaportals, f );
	CM_FloodAreaConnections( cm );
}

/*
=============
CM_HeadnodeVisible

Returns qtrue if any leaf under headnode has a cluster that
is potentially visible
=============
*/
qboolean CM_HeadnodeVisible( cnode_t *node, byte *visbits ) {
	cleaf_t *leaf;
	int		cluster;

	if( !node->plane ) {
		leaf = ( cleaf_t * )node;
		cluster = leaf->cluster;
		if( cluster == -1 )
			return qfalse;
		if( Q_IsBitSet( visbits, cluster ) )
			return qtrue;
		return qfalse;
	}

	if( CM_HeadnodeVisible( node->children[0], visbits ) )
		return qtrue;
	return CM_HeadnodeVisible( node->children[1], visbits );
}


/*
============
CM_FatPVS

The client will interpolate the view position,
so we can't use a single PVS point
===========
*/
byte *CM_FatPVS( cm_t *cm, const vec3_t org ) {
	static byte	fatpvs[MAX_MAP_LEAFS/8];
	cleaf_t	*leafs[64];
	int		clusters[64];
	int		i, j, count;
	int		longs;
	uint32	*src, *dst;
	vec3_t	mins, maxs;
		
	if( !cm->cache ) {	// map not loaded
		memset( fatpvs, 0, sizeof( fatpvs ) );
		return fatpvs;
	}

	for( i = 0; i < 3; i++ ) {
		mins[i] = org[i] - 8;
		maxs[i] = org[i] + 8;
	}

	count = CM_BoxLeafs( cm, mins, maxs, leafs, 64, NULL );
	if( count < 1 )
		Com_Error( ERR_DROP, "CM_FatPVS: leaf count < 1" );
	longs = ( cm->cache->numclusters + 31 ) >> 5;

	// convert leafs to clusters
	for( i = 0 ; i < count; i++ ) {
		clusters[i] = leafs[i]->cluster;
	}

	src = ( uint32 * )CM_ClusterPVS( cm, clusters[0] );
	dst = ( uint32 * )fatpvs;
	for( j = 0; j < longs; j++ ) {
		*dst++ = *src++;
	}

	// or in all the other leaf bits
	for( i = 1; i < count; i++ ) {
		for( j = 0; j < i; j++ ) {
			if( clusters[i] == clusters[j] ) {
				goto nextleaf; // already have the cluster we want
			}
		}
		src = ( uint32 * )CM_ClusterPVS( cm, clusters[i] );
		dst = ( uint32 * )fatpvs;
		for( j = 0; j < longs; j++ ) {
			*dst++ |= *src++;
		}

nextleaf:;
	}

	return fatpvs;
}

/*
=============
CM_Init
=============
*/
void CM_Init( void ) {
	CM_InitBoxHull();

	map_noareas = Cvar_Get( "map_noareas", "0", 0 );
	map_load_entities = Cvar_Get( "map_load_entities", "0", 0 );
}