1 /*
2 ===========================================================================
3 
4 Return to Castle Wolfenstein multiplayer GPL Source Code
5 Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (“RTCW MP Source Code”).
8 
9 RTCW MP Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 RTCW MP Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with RTCW MP Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the RTCW MP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW MP Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 
30 #include "q_shared.h"
31 #include "qcommon.h"
32 #include "cm_polylib.h"
33 
34 
35 //	(SA) DM needs more than 256 since this includes func_static and func_explosives
36 //#define	MAX_SUBMODELS		256
37 //#define	BOX_MODEL_HANDLE	255
38 
39 #define MAX_SUBMODELS           512
40 #define BOX_MODEL_HANDLE        511
41 #define CAPSULE_MODEL_HANDLE    510
42 
43 
44 typedef struct {
45 	cplane_t    *plane;
46 	int children[2];                // negative numbers are leafs
47 } cNode_t;
48 
49 typedef struct {
50 	int cluster;
51 	int area;
52 
53 	int firstLeafBrush;
54 	int numLeafBrushes;
55 
56 	int firstLeafSurface;
57 	int numLeafSurfaces;
58 } cLeaf_t;
59 
60 typedef struct cmodel_s {
61 	vec3_t mins, maxs;
62 	cLeaf_t leaf;               // submodels don't reference the main tree
63 } cmodel_t;
64 
65 typedef struct {
66 	cplane_t    *plane;
67 	int surfaceFlags;
68 	int shaderNum;
69 } cbrushside_t;
70 
71 typedef struct {
72 	int shaderNum;              // the shader that determined the contents
73 	int contents;
74 	vec3_t bounds[2];
75 	int numsides;
76 	cbrushside_t    *sides;
77 	int checkcount;             // to avoid repeated testings
78 } cbrush_t;
79 
80 
81 typedef struct {
82 	int checkcount;                     // to avoid repeated testings
83 	int surfaceFlags;
84 	int contents;
85 	struct patchCollide_s   *pc;
86 } cPatch_t;
87 
88 
89 typedef struct {
90 	int floodnum;
91 	int floodvalid;
92 } cArea_t;
93 
94 typedef struct {
95 	char name[MAX_QPATH];
96 
97 	int numShaders;
98 	dshader_t   *shaders;
99 
100 	int numBrushSides;
101 	cbrushside_t *brushsides;
102 
103 	int numPlanes;
104 	cplane_t    *planes;
105 
106 	int numNodes;
107 	cNode_t     *nodes;
108 
109 	int numLeafs;
110 	cLeaf_t     *leafs;
111 
112 	int numLeafBrushes;
113 	int         *leafbrushes;
114 
115 	int numLeafSurfaces;
116 	int         *leafsurfaces;
117 
118 	int numSubModels;
119 	cmodel_t    *cmodels;
120 
121 	int numBrushes;
122 	cbrush_t    *brushes;
123 
124 	int numClusters;
125 	int clusterBytes;
126 	byte        *visibility;
127 	qboolean vised;             // if false, visibility is just a single cluster of ffs
128 
129 	int numEntityChars;
130 	char        *entityString;
131 
132 	int numAreas;
133 	cArea_t     *areas;
134 	int         *areaPortals;   // [ numAreas*numAreas ] reference counts
135 
136 	int numSurfaces;
137 	cPatch_t    **surfaces;         // non-patches will be NULL
138 
139 	int floodvalid;
140 	int checkcount;                         // incremented on each trace
141 } clipMap_t;
142 
143 
144 // keep 1/8 unit away to keep the position valid before network snapping
145 // and to avoid various numeric issues
146 #define SURFACE_CLIP_EPSILON    ( 0.125 )
147 
148 extern clipMap_t cm;
149 extern int c_pointcontents;
150 extern int c_traces, c_brush_traces, c_patch_traces;
151 extern cvar_t      *cm_noAreas;
152 extern cvar_t      *cm_noCurves;
153 extern cvar_t      *cm_playerCurveClip;
154 
155 // cm_test.c
156 
157 // Used for oriented capsule collision detection
158 typedef struct
159 {
160 	qboolean use;
161 	float radius;
162 	float halfheight;
163 	vec3_t offset;
164 } sphere_t;
165 
166 typedef struct {
167 	vec3_t start;
168 	vec3_t end;
169 	vec3_t size[2];         // size of the box being swept through the model
170 	vec3_t offsets[8];      // [signbits][x] = either size[0][x] or size[1][x]
171 	float maxOffset;        // longest corner length from origin
172 	vec3_t extents;         // greatest of abs(size[0]) and abs(size[1])
173 	vec3_t bounds[2];       // enclosing box of start and end surrounding by size
174 	vec3_t modelOrigin;     // origin of the model tracing through
175 	int contents;           // ored contents of the model tracing through
176 	qboolean isPoint;       // optimized case
177 	trace_t trace;          // returned from trace call
178 	sphere_t sphere;        // sphere for oriendted capsule collision
179 } traceWork_t;
180 
181 typedef struct leafList_s {
182 	int count;
183 	int maxcount;
184 	qboolean overflowed;
185 	int     *list;
186 	vec3_t bounds[2];
187 	int lastLeaf;           // for overflows where each leaf can't be stored individually
188 	void ( *storeLeafs )( struct leafList_s *ll, int nodenum );
189 } leafList_t;
190 
191 
192 int CM_BoxBrushes( const vec3_t mins, const vec3_t maxs, cbrush_t **list, int listsize );
193 
194 void CM_StoreLeafs( leafList_t *ll, int nodenum );
195 void CM_StoreBrushes( leafList_t *ll, int nodenum );
196 
197 void CM_BoxLeafnums_r( leafList_t *ll, int nodenum );
198 
199 cmodel_t    *CM_ClipHandleToModel( clipHandle_t handle );
200 qboolean CM_BoundsIntersect( const vec3_t mins, const vec3_t maxs, const vec3_t mins2, const vec3_t maxs2 );
201 qboolean CM_BoundsIntersectPoint( const vec3_t mins, const vec3_t maxs, const vec3_t point );
202 
203 // cm_patch.c
204 
205 struct patchCollide_s   *CM_GeneratePatchCollide( int width, int height, vec3_t *points );
206 void CM_TraceThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc );
207 qboolean CM_PositionTestInPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc );
208 void CM_ClearLevelPatches( void );
209