csqc patch from [515], seems to work with [515]'s dpcsqc test mod, needs a lot of...
[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 prvm_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 origin;
39         vec3_t starttransformed, endtransformed, starttransformedmins, endtransformedmins, starttransformedmaxs, endtransformedmaxs;
40         vec3_t startmins, startmaxs, endmins, endmaxs, entmins, entmaxs;
41         vec_t *playermins, *playermaxs;
42
43         VectorAdd(start, mins, startmins);
44         VectorAdd(start, maxs, startmaxs);
45         VectorAdd(end, mins, endmins);
46         VectorAdd(end, maxs, endmaxs);
47
48         memset (&cliptrace, 0 , sizeof(trace_t));
49         cliptrace.fraction = 1;
50         cliptrace.realfraction = 1;
51
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                                 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
135                                 VectorAdd(origin, playermins, entmins);
136                                 VectorAdd(origin, playermaxs, entmaxs);
137                                 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
138                                         continue;
139
140                                 memset (&trace, 0 , sizeof(trace_t));
141                                 trace.fraction = 1;
142                                 trace.realfraction = 1;
143
144                                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
145                                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
146                                 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
147
148                                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
149                                 if (trace.allsolid)
150                                         cliptrace.allsolid = true;
151                                 if (trace.startsolid)
152                                 {
153                                         cliptrace.startsolid = true;
154                                         if (cliptrace.realfraction == 1)
155                                                 if (hitent)
156                                                         *hitent = n;
157                                 }
158                                 // don't set this except on the world, because it can easily confuse
159                                 // monsters underwater if there's a bmodel involved in the trace
160                                 // (inopen && inwater is how they check water visibility)
161                                 //if (trace.inopen)
162                                 //      cliptrace.inopen = true;
163                                 if (trace.inwater)
164                                         cliptrace.inwater = true;
165                                 if (trace.realfraction < cliptrace.realfraction)
166                                 {
167                                         cliptrace.fraction = trace.fraction;
168                                         cliptrace.realfraction = trace.realfraction;
169                                         cliptrace.plane = trace.plane;
170                                         if (hitent)
171                                                 *hitent = n;
172                                         Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
173                                 }
174                                 cliptrace.startsupercontents |= trace.startsupercontents;
175                         }
176                 }
177         }
178         cliptrace.fraction = bound(0, cliptrace.fraction, 1);
179         cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
180         VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
181         return cliptrace;
182 }
183
184 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent, qboolean csqcents)
185 {
186         float maxfrac, maxrealfrac;
187         int n, entsnum;
188         entity_t *entlist;
189         unsigned char *entactivelist;
190         entity_render_t *ent;
191         float tracemins[3], tracemaxs[3];
192         trace_t trace;
193         float tempnormal[3], starttransformed[3], endtransformed[3];
194
195         memset (&trace, 0 , sizeof(trace_t));
196         trace.fraction = 1;
197         trace.realfraction = 1;
198         VectorCopy (end, trace.endpos);
199
200         if (hitent)
201                 *hitent = 0;
202         if (cl.worldmodel && cl.worldmodel->TraceBox)
203                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
204
205         if (normal)
206                 VectorCopy(trace.plane.normal, normal);
207         //cl_traceline_startsupercontents = trace.startsupercontents;
208         maxfrac = trace.fraction;
209         maxrealfrac = trace.realfraction;
210
211         tracemins[0] = min(start[0], end[0]);
212         tracemaxs[0] = max(start[0], end[0]);
213         tracemins[1] = min(start[1], end[1]);
214         tracemaxs[1] = max(start[1], end[1]);
215         tracemins[2] = min(start[2], end[2]);
216         tracemaxs[2] = max(start[2], end[2]);
217
218         if(csqcents)
219         {
220                 entlist = cl_csqcentities;
221                 entactivelist = cl_csqcentities_active;
222                 entsnum = cl_num_csqcentities;
223         }
224         else
225         {
226                 entlist = cl_entities;
227                 entactivelist = cl_entities_active;
228                 entsnum = cl_num_entities;
229         }
230
231         // look for embedded bmodels
232         for (n = 0;n < entsnum;n++)
233         {
234                 if (!entactivelist[n])
235                         continue;
236                 ent = &entlist[n].render;
237                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
238                         continue;
239                 if (!ent->model || !ent->model->TraceBox)
240                         continue;
241                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
242                         continue;
243                 // if transparent and not selectable, skip entity
244                 if (!(entlist[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
245                         continue;
246                 if (ent == ignoreent)
247                         continue;
248                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
249                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
250
251                 if (ent->model && ent->model->TraceBox)
252                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
253
254                 //cl_traceline_startsupercontents |= trace.startsupercontents;
255                 if (maxrealfrac > trace.realfraction)
256                 {
257                         if (hitent)
258                                 *hitent = n;
259                         maxfrac = trace.fraction;
260                         maxrealfrac = trace.realfraction;
261                         if (normal)
262                         {
263                                 VectorCopy(trace.plane.normal, tempnormal);
264                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
265                         }
266                 }
267         }
268         maxfrac = bound(0, maxfrac, 1);
269         maxrealfrac = bound(0, maxrealfrac, 1);
270         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
271         if (impact)
272                 VectorLerp(start, maxfrac, end, impact);
273         return maxfrac;
274 }
275
276 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
277 {
278         // FIXME: check multiple brush models
279         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
280                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
281 }
282
283 int CL_PointQ1Contents(const vec3_t p)
284 {
285         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents);
286         /*
287         // FIXME: check multiple brush models
288         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
289                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
290         return 0;
291         */
292 }
293
294 int CL_PointSuperContents(const vec3_t p)
295 {
296         return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
297         /*
298         // FIXME: check multiple brush models
299         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
300                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
301         return 0;
302         */
303 }
304