1 /*
2 	Relay -- a tool to record and play Quake2 demos
3 	Copyright (C) 2000 Conor Davis
4 
5 	This program is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU General Public License
7 	as published by the Free Software Foundation; either version 2
8 	of the License, or (at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 	Conor Davis
20 	cedavis@planetquake.com
21 */
22 
23 #ifndef __BSP_H
24 #define __BSP_H
25 
26 #include "q2defines.h"
27 #include "block.h"
28 
29 #define MAX_MAP_MODELS		1024
30 #define MAX_MAP_BRUSHES 	8192
31 #define MAX_MAP_ENTITIES	2048
32 #define MAX_MAP_ENTSTRING	0x40000
33 #define MAX_MAP_TEXINFO 	8192
34 
35 #define MAX_MAP_AREAS		256
36 #define MAX_MAP_AREAPORTALS 1024
37 #define MAX_MAP_PLANES		65536
38 #define MAX_MAP_NODES		65536
39 #define MAX_MAP_BRUSHSIDES	65536
40 #define MAX_MAP_LEAFS		65536
41 #define MAX_MAP_VERTS		65536
42 #define MAX_MAP_FACES		65536
43 #define MAX_MAP_LEAFFACES	65536
44 #define MAX_MAP_LEAFBRUSHES 65536
45 #define MAX_MAP_PORTALS 	65536
46 #define MAX_MAP_EDGES		128000
47 #define MAX_MAP_SURFEDGES	256000
48 #define MAX_MAP_LIGHTING	0x200000
49 #define MAX_MAP_VISIBILITY	0x100000
50 
51 typedef struct
52 {
53 	int 	fileofs, filelen;
54 } lump_t;
55 
56 #define LUMP_ENTITIES		0
57 #define LUMP_PLANES 		1
58 #define LUMP_VERTEXES		2
59 #define LUMP_VISIBILITY 	3
60 #define LUMP_NODES			4
61 #define LUMP_TEXINFO		5
62 #define LUMP_FACES			6
63 #define LUMP_LIGHTING		7
64 #define LUMP_LEAFS			8
65 #define LUMP_LEAFFACES		9
66 #define LUMP_LEAFBRUSHES	10
67 #define LUMP_EDGES			11
68 #define LUMP_SURFEDGES		12
69 #define LUMP_MODELS 		13
70 #define LUMP_BRUSHES		14
71 #define LUMP_BRUSHSIDES 	15
72 #define LUMP_POP			16
73 #define LUMP_AREAS			17
74 #define LUMP_AREAPORTALS	18
75 #define HEADER_LUMPS		19
76 
77 typedef struct
78 {
79 	float		mins[3], maxs[3];
80 	float		origin[3];		// for sounds or lights
81 	int 		headnode;
82 	int 		firstface, numfaces;	// submodels just draw faces
83 										// without walking the bsp tree
84 } model_t;
85 
86 typedef struct
87 {
88 	float	point[3];
89 } vertex_t;
90 
91 // 0-2 are axial planes
92 #define PLANE_X 		0
93 #define PLANE_Y 		1
94 #define PLANE_Z 		2
95 
96 // 3-5 are non-axial planes snapped to the nearest
97 #define PLANE_ANYX		3
98 #define PLANE_ANYY		4
99 #define PLANE_ANYZ		5
100 
101 // planes (x&~1) and (x&~1)+1 are allways opposites
102 
103 typedef struct
104 {
105 	float	normal[3];
106 	float	dist;
107 	int 	type;		// PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate
108 } plane_t;
109 
110 typedef struct
111 {
112 	int 		planenum;
113 	int 		children[2];	// negative numbers are -(leafs+1), not nodes
114 	short		mins[3];		// for frustom culling
115 	short		maxs[3];
116 	unsigned short	firstface;
117 	unsigned short	numfaces;	// counting both sides
118 } node_t;
119 
120 typedef struct texinfo_s
121 {
122 	float		vecs[2][4]; 	// [s/t][xyz offset]
123 	int 		flags;			// miptex flags + overrides
124 	int 		value;			// light emission, etc
125 	char		texture[32];	// texture name (textures/*.wal)
126 	int 		nexttexinfo;	// for animations, -1 = end of chain
127 } texinfo_t;
128 
129 // note that edge 0 is never used, because negative edge nums are used for
130 // counterclockwise use of the edge in a face
131 typedef struct
132 {
133 	unsigned short	v[2];		// vertex numbers
134 } edge_t;
135 
136 #define MAXLIGHTMAPS	4
137 typedef struct
138 {
139 	unsigned short	planenum;
140 	short		side;
141 
142 	int 		firstedge;		// we must support > 64k edges
143 	short		numedges;
144 	short		texinfo;
145 
146 // lighting info
147 	byte		styles[MAXLIGHTMAPS];
148 	int 		lightofs;		// start of [numstyles*surfsize] samples
149 } face_t;
150 
151 typedef struct
152 {
153 	int 			contents;			// OR of all brushes (not needed?)
154 
155 	short			cluster;
156 	short			area;
157 
158 	short			mins[3];			// for frustum culling
159 	short			maxs[3];
160 
161 	unsigned short	firstleafface;
162 	unsigned short	numleaffaces;
163 
164 	unsigned short	firstleafbrush;
165 	unsigned short	numleafbrushes;
166 } leaf_t;
167 
168 typedef struct
169 {
170 	unsigned short	planenum;		// facing out of the leaf
171 	short	texinfo;
172 } brushside_t;
173 
174 typedef struct
175 {
176 	int 		firstside;
177 	int 		numsides;
178 	int 		contents;
179 } brush_t;
180 
181 // the visibility lump consists of a header with a count, then
182 // byte offsets for the PVS and PHS of each cluster, then the raw
183 // compressed bit vectors
184 #define DVIS_PVS	0
185 #define DVIS_PHS	1
186 typedef struct
187 {
188 	int 		numclusters;
189 	int 		bitofs[8][2];	// bitofs[numclusters][2]
190 } vis_t;
191 
192 // each area has a list of portals that lead into other areas
193 // when portals are closed, other areas may not be visible or
194 // hearable even if the vis info says that it should be
195 typedef struct
196 {
197 	int 	portalnum;
198 	int 	otherarea;
199 } areaportal_t;
200 
201 typedef struct
202 {
203 	int 	numareaportals;
204 	int 	firstareaportal;
205 } area_t;
206 
207 typedef struct
208 {
209 	int 			version;
210 	int 			num[HEADER_LUMPS];
211 	model_t 		models[MAX_MAP_MODELS];
212 	byte			visdata[MAX_MAP_VISIBILITY];
213 	byte			lightdata[MAX_MAP_LIGHTING];
214 	char			entdata[MAX_MAP_ENTSTRING];
215 	leaf_t			leafs[MAX_MAP_LEAFS];
216 	plane_t 		planes[MAX_MAP_PLANES];
217 	vertex_t		vertexes[MAX_MAP_VERTS];
218 	node_t			nodes[MAX_MAP_NODES];
219 	texinfo_t		texinfo[MAX_MAP_TEXINFO];
220 	face_t			faces[MAX_MAP_FACES];
221 	edge_t			edges[MAX_MAP_EDGES];
222 	unsigned short	leaffaces[MAX_MAP_LEAFFACES];
223 	unsigned short	leafbrushes[MAX_MAP_LEAFBRUSHES];
224 	int 			surfedges[MAX_MAP_SURFEDGES];
225 	brush_t 		brushes[MAX_MAP_BRUSHES];
226 	brushside_t 	brushsides[MAX_MAP_BRUSHSIDES];
227 	area_t			areas[MAX_MAP_AREAS];
228 	areaportal_t	areaportals[MAX_MAP_AREAPORTALS];
229 } bsp_t;
230 
231 extern int ReadBSP(bsp_t *bsp, block_t *block);
232 extern int PointInLeafnum(bsp_t *bsp, vec3_t point);
233 extern int PointInCluster(bsp_t *bsp, vec3_t point);
234 
235 extern qboolean ClusterVisible(bsp_t *bsp, int cluster1, int cluster2, int viewset);
236 extern qboolean inPVS(bsp_t *bsp, vec3_t p1, vec3_t p2);
237 extern qboolean inPHS(bsp_t *bsp, vec3_t p1, vec3_t p2);
238 
239 #endif	// __BSP_H
240