1 /**
2  * @file
3  * @brief Tracing functions
4  */
5 
6 /*
7 All original material Copyright (C) 2002-2013 UFO: Alien Invasion.
8 
9 Copyright (C) 1997-2001 Id Software, Inc.
10 
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 See the GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 
26 */
27 
28 #pragma once
29 
30 #include "../shared/typedefs.h"
31 
32 /*
33  * ufo2map and ufo have a different view on the data in the bsp tree,
34  * so they use different structs and classes.
35  */
36 #if defined(COMPILE_MAP)
37   #define TR_TILE_TYPE			dMapTile_t
38   #define TR_PLANE_TYPE			dBspPlane_t
39 #elif defined(COMPILE_UFO)
40   #define TR_TILE_TYPE			MapTile
41   #define TR_PLANE_TYPE			cBspPlane_t
42 #else
43   #error Either COMPILE_MAP or COMPILE_UFO must be defined in order for tracing.c to work.
44 #endif
45 /** @note all the above types are declared in typedefs.h */
46 
47 /**
48  * mask to trace against all the different visible levels (1-8) (resp. (1<<[0-7])
49  */
50 #define TRACING_ALL_VISIBLE_LEVELS 0x1FF
51 
52 /** a trace is returned when a box is swept through the world */
53 typedef struct trace_s {
54 	bool allsolid;				/**< if true, plane is not valid */
55 	bool startsolid;			/**< if true, the initial point was in a solid area */
56 	float fraction;				/**< distance traveled, 1.0 = didn't hit anything, 0.0 Inside of a brush */
57 	vec3_t endpos;				/**< final position along line */
58 	TR_PLANE_TYPE plane;		/**< surface plane at impact - normal is in there */
59 	cBspSurface_t* surface;	    /**< surface hit */
60 	int planenum;				/**< index of the plane hit, used for map debugging */
61 	uint32_t contentFlags;		/**< contents on other side of surface hit */
62 	int32_t leafnum;
63 	int mapTile;				/**< the map tile we hit something */
64 	struct le_s* le;			/**< not set by CM_*() functions */
65 	int entNum;					/**< not set by CM_*() functions */
66 
trace_strace_s67 	inline trace_s () {
68 		init();
69 	}
inittrace_s70 	inline void init() {
71 		OBJZERO(*this);
72 	}
73 } trace_t;
74 
75 typedef struct mapTiles_s {
76 	/** @note loaded map tiles with this assembly.  ufo2map has exactly 1. */
77 	TR_TILE_TYPE mapTiles[MAX_MAPTILES];
78 
79 	/** @note number of loaded map tiles (map assembly) */
80 	int numTiles;
81 } mapTiles_t;
82 
83 /*==============================================================
84 BOX AND LINE TRACING
85 ==============================================================*/
86 
87 int TR_BoxOnPlaneSide(const vec3_t mins, const vec3_t maxs, const TR_PLANE_TYPE *plane);
88 
89 void TR_BuildTracingNode_r(TR_TILE_TYPE *tile, tnode_t** tnode, int32_t nodenum, int level);
90 
91 #ifdef COMPILE_MAP
92 trace_t TR_SingleTileBoxTrace(mapTiles_t* mapTiles, const Line &traceLine, const AABB* traceBox, const int levelmask, const int brushmask, const int brushreject);
93 #endif
94 int TR_TestLine_r(TR_TILE_TYPE *tile, int32_t nodenum, const vec3_t start, const vec3_t end);
95 trace_t TR_BoxTrace(TR_TILE_TYPE *tile, const vec3_t start, const vec3_t end, const AABB &traceBox, const int headnode, const int brushmask, const int brushreject, const float fraction);
96 
97 bool TR_TestLine(mapTiles_t* mapTiles, const vec3_t start, const vec3_t end, const int levelmask);
98 bool TR_TestLineDM(mapTiles_t* mapTiles, const vec3_t start, const vec3_t end, vec3_t hit, const int levelmask);
99 trace_t TR_TileBoxTrace(TR_TILE_TYPE *myTile, const vec3_t start, const vec3_t end, const AABB &aabb, const int levelmask, const int brushmask, const int brushreject);
100