]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
added newline at end of file to hush a gcc warning
[divverent/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 /*
6 // not yet used
7 typedef struct physentity_s
8 {
9         // this may be a entity_t, or a edict_t, or whatever
10         void *realentity;
11
12         // can be NULL if it is a bbox object
13         model_t *bmodel;
14
15         // node this entity crosses
16         // for avoiding unnecessary collisions
17         physnode_t *node;
18
19         // matrix for converting from model to world coordinates
20         double modeltoworldmatrix[3][4];
21
22         // matrix for converting from world to model coordinates
23         double worldtomodelmatrix[3][4];
24
25         // if this is a bmodel, this is used for culling it quickly
26         // if this is not a bmodel, this is used for actual collisions
27         double mins[3], maxs[3];
28 }
29 physentity_t;
30 */
31
32 trace_t CL_TraceBox(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitbmodels, int *hitent, int hitsupercontentsmask, qboolean hitplayers)
33 {
34         int n;
35         entity_render_t *ent;
36         vec3_t tracemins, tracemaxs;
37         trace_t cliptrace, trace;
38         vec3_t starttransformed, endtransformed, starttransformedmins, endtransformedmins, starttransformedmaxs, endtransformedmaxs;
39         vec3_t startmins, startmaxs, endmins, endmaxs, entmins, entmaxs;
40         vec_t *playermins, *playermaxs;
41
42         VectorAdd(start, mins, startmins);
43         VectorAdd(start, maxs, startmaxs);
44         VectorAdd(end, mins, endmins);
45         VectorAdd(end, maxs, endmaxs);
46
47         memset (&cliptrace, 0 , sizeof(trace_t));
48         cliptrace.fraction = 1;
49         cliptrace.realfraction = 1;
50
51         Mod_CheckLoaded(cl.worldmodel);
52         if (cl.worldmodel && cl.worldmodel->TraceBox)
53                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &cliptrace, startmins, startmaxs, endmins, endmaxs, hitsupercontentsmask);
54
55         if (hitent)
56                 *hitent = 0;
57
58         if (hitbmodels && cl_num_brushmodel_entities)
59         {
60                 tracemins[0] = min(start[0], end[0]) + mins[0];
61                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
62                 tracemins[1] = min(start[1], end[1]) + mins[1];
63                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
64                 tracemins[2] = min(start[2], end[2]) + mins[2];
65                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
66
67                 // look for embedded bmodels
68                 for (n = 0;n < cl_num_brushmodel_entities;n++)
69                 {
70                         ent = &cl_entities[cl_brushmodel_entities[n]].render;
71                         if (!BoxesOverlap(tracemins, tracemaxs, ent->mins, ent->maxs))
72                                 continue;
73
74                         Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
75                         Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
76                         VectorAdd(starttransformed, mins, starttransformedmins);
77                         VectorAdd(starttransformed, maxs, starttransformedmaxs);
78                         VectorAdd(endtransformed, mins, endtransformedmins);
79                         VectorAdd(endtransformed, maxs, endtransformedmaxs);
80
81                         memset (&trace, 0 , sizeof(trace_t));
82                         trace.fraction = 1;
83                         trace.realfraction = 1;
84
85                         if (ent->model && ent->model->TraceBox)
86                                 ent->model->TraceBox(ent->model, 0, &trace, starttransformedmins, starttransformedmaxs, endtransformedmins, endtransformedmaxs, hitsupercontentsmask);
87
88                         // LordHavoc: take the 'best' answers from the new trace and combine with existing data
89                         if (trace.allsolid)
90                                 cliptrace.allsolid = true;
91                         if (trace.startsolid)
92                         {
93                                 cliptrace.startsolid = true;
94                                 if (cliptrace.realfraction == 1)
95                                         if (hitent)
96                                                 *hitent = cl_brushmodel_entities[n];
97                         }
98                         // don't set this except on the world, because it can easily confuse
99                         // monsters underwater if there's a bmodel involved in the trace
100                         // (inopen && inwater is how they check water visibility)
101                         //if (trace.inopen)
102                         //      cliptrace.inopen = true;
103                         if (trace.inwater)
104                                 cliptrace.inwater = true;
105                         if (trace.realfraction < cliptrace.realfraction)
106                         {
107                                 cliptrace.fraction = trace.fraction;
108                                 cliptrace.realfraction = trace.realfraction;
109                                 cliptrace.plane = trace.plane;
110                                 if (hitent)
111                                         *hitent = cl_brushmodel_entities[n];
112                                 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
113                         }
114                         cliptrace.startsupercontents |= trace.startsupercontents;
115                 }
116         }
117         if (hitplayers)
118         {
119                 tracemins[0] = min(start[0], end[0]) + mins[0];
120                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
121                 tracemins[1] = min(start[1], end[1]) + mins[1];
122                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
123                 tracemins[2] = min(start[2], end[2]) + mins[2];
124                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
125
126                 for (n = 1;n < cl.maxclients+1;n++)
127                 {
128                         if (n != cl.playerentity)
129                         {
130                                 ent = &cl_entities[n].render;
131                                 // FIXME: crouch
132                                 playermins = cl_playerstandmins;
133                                 playermaxs = cl_playerstandmaxs;
134                                 VectorAdd(ent->origin, playermins, entmins);
135                                 VectorAdd(ent->origin, playermaxs, entmaxs);
136                                 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
137                                         continue;
138
139                                 memset (&trace, 0 , sizeof(trace_t));
140                                 trace.fraction = 1;
141                                 trace.realfraction = 1;
142
143                                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
144                                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
145                                 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
146
147                                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
148                                 if (trace.allsolid)
149                                         cliptrace.allsolid = true;
150                                 if (trace.startsolid)
151                                 {
152                                         cliptrace.startsolid = true;
153                                         if (cliptrace.realfraction == 1)
154                                                 if (hitent)
155                                                         *hitent = n;
156                                 }
157                                 // don't set this except on the world, because it can easily confuse
158                                 // monsters underwater if there's a bmodel involved in the trace
159                                 // (inopen && inwater is how they check water visibility)
160                                 //if (trace.inopen)
161                                 //      cliptrace.inopen = true;
162                                 if (trace.inwater)
163                                         cliptrace.inwater = true;
164                                 if (trace.realfraction < cliptrace.realfraction)
165                                 {
166                                         cliptrace.fraction = trace.fraction;
167                                         cliptrace.realfraction = trace.realfraction;
168                                         cliptrace.plane = trace.plane;
169                                         if (hitent)
170                                                 *hitent = n;
171                                         Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
172                                 }
173                                 cliptrace.startsupercontents |= trace.startsupercontents;
174                         }
175                 }
176         }
177         cliptrace.fraction = bound(0, cliptrace.fraction, 1);
178         cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
179         VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
180         return cliptrace;
181 }
182
183 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
184 {
185         float maxfrac, maxrealfrac;
186         int n;
187         entity_render_t *ent;
188         float tracemins[3], tracemaxs[3];
189         trace_t trace;
190         float tempnormal[3], starttransformed[3], endtransformed[3];
191
192         memset (&trace, 0 , sizeof(trace_t));
193         trace.fraction = 1;
194         trace.realfraction = 1;
195         VectorCopy (end, trace.endpos);
196
197         if (hitent)
198                 *hitent = 0;
199         Mod_CheckLoaded(cl.worldmodel);
200         if (cl.worldmodel && cl.worldmodel->TraceBox)
201                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
202
203         if (normal)
204                 VectorCopy(trace.plane.normal, normal);
205         //cl_traceline_startsupercontents = trace.startsupercontents;
206         maxfrac = trace.fraction;
207         maxrealfrac = trace.realfraction;
208
209         tracemins[0] = min(start[0], end[0]);
210         tracemaxs[0] = max(start[0], end[0]);
211         tracemins[1] = min(start[1], end[1]);
212         tracemaxs[1] = max(start[1], end[1]);
213         tracemins[2] = min(start[2], end[2]);
214         tracemaxs[2] = max(start[2], end[2]);
215
216         // look for embedded bmodels
217         for (n = 0;n < cl_num_entities;n++)
218         {
219                 if (!cl_entities_active[n])
220                         continue;
221                 ent = &cl_entities[n].render;
222                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
223                         continue;
224                 if (!ent->model || !ent->model->TraceBox)
225                         continue;
226                 // if transparent and not selectable, skip entity
227                 if (!(cl_entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
228                         continue;
229                 if (ent == ignoreent)
230                         continue;
231                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
232                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
233
234                 if (ent->model && ent->model->TraceBox)
235                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
236
237                 //cl_traceline_startsupercontents |= trace.startsupercontents;
238                 if (maxrealfrac > trace.realfraction)
239                 {
240                         if (hitent)
241                                 *hitent = n;
242                         maxfrac = trace.fraction;
243                         maxrealfrac = trace.realfraction;
244                         if (normal)
245                         {
246                                 VectorCopy(trace.plane.normal, tempnormal);
247                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
248                         }
249                 }
250         }
251         maxfrac = bound(0, maxfrac, 1);
252         maxrealfrac = bound(0, maxrealfrac, 1);
253         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
254         if (impact)
255                 VectorLerp(start, maxfrac, end, impact);
256         return maxfrac;
257 }
258
259 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
260 {
261         // FIXME: check multiple brush models
262         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
263                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
264 }
265
266 int CL_PointQ1Contents(const vec3_t p)
267 {
268         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents);
269         /*
270         // FIXME: check multiple brush models
271         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
272                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
273         return 0;
274         */
275 }
276
277 int CL_PointSuperContents(const vec3_t p)
278 {
279         return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
280         /*
281         // FIXME: check multiple brush models
282         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
283                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
284         return 0;
285         */
286 }
287