0dae92c90f36e77784127beeb29347f1e72b8500
[xonotic/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
6 {
7         float maxfrac, maxrealfrac;
8         int n;
9         entity_render_t *ent;
10         float tracemins[3], tracemaxs[3];
11         trace_t trace;
12         float tempnormal[3], starttransformed[3], endtransformed[3];
13
14         memset (&trace, 0 , sizeof(trace_t));
15         trace.fraction = 1;
16         trace.realfraction = 1;
17         VectorCopy (end, trace.endpos);
18
19         if (hitent)
20                 *hitent = 0;
21         if (cl.worldmodel && cl.worldmodel->TraceBox)
22                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, vec3_origin, vec3_origin, end, SUPERCONTENTS_SOLID);
23
24         if (normal)
25                 VectorCopy(trace.plane.normal, normal);
26         maxfrac = trace.fraction;
27         maxrealfrac = trace.realfraction;
28
29         tracemins[0] = min(start[0], end[0]);
30         tracemaxs[0] = max(start[0], end[0]);
31         tracemins[1] = min(start[1], end[1]);
32         tracemaxs[1] = max(start[1], end[1]);
33         tracemins[2] = min(start[2], end[2]);
34         tracemaxs[2] = max(start[2], end[2]);
35
36         // look for embedded bmodels
37         for (n = 0;n < cl.num_entities;n++)
38         {
39                 if (!cl.entities_active[n])
40                         continue;
41                 ent = &cl.entities[n].render;
42                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
43                         continue;
44                 if (!ent->model || !ent->model->TraceBox)
45                         continue;
46                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
47                         continue;
48                 // if transparent and not selectable, skip entity
49                 if (!(cl.entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
50                         continue;
51                 if (ent == ignoreent)
52                         continue;
53                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
54                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
55
56                 //if (ent->model && ent->model->TraceBox)
57                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID);
58
59                 if (maxrealfrac > trace.realfraction)
60                 {
61                         if (hitent)
62                                 *hitent = n;
63                         maxfrac = trace.fraction;
64                         maxrealfrac = trace.realfraction;
65                         if (normal)
66                         {
67                                 VectorCopy(trace.plane.normal, tempnormal);
68                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
69                         }
70                 }
71         }
72         maxfrac = bound(0, maxfrac, 1);
73         maxrealfrac = bound(0, maxrealfrac, 1);
74         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
75         if (impact)
76                 VectorLerp(start, maxfrac, end, impact);
77         return maxfrac;
78 }
79
80 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
81 {
82         // FIXME: check multiple brush models
83         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
84                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
85 }
86
87 model_t *CL_GetModelByIndex(int modelindex)
88 {
89         if(!modelindex)
90                 return NULL;
91         if (modelindex < 0)
92         {
93                 modelindex = -(modelindex+1);
94                 if (modelindex < MAX_MODELS)
95                         return cl.csqc_model_precache[modelindex];
96         }
97         else
98         {
99                 if(modelindex < MAX_MODELS)
100                         return cl.model_precache[modelindex];
101         }
102         return NULL;
103 }
104
105 model_t *CL_GetModelFromEdict(prvm_edict_t *ed)
106 {
107         if (!ed || ed->priv.server->free)
108                 return NULL;
109         return CL_GetModelByIndex((int)ed->fields.client->modelindex);
110 }
111
112 void CL_LinkEdict(prvm_edict_t *ent)
113 {
114         if (ent == prog->edicts)
115                 return;         // don't add the world
116
117         if (ent->priv.server->free)
118                 return;
119
120         VectorAdd(ent->fields.client->origin, ent->fields.client->mins, ent->fields.client->absmin);
121         VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, ent->fields.client->absmax);
122
123         World_LinkEdict(&cl.world, ent, ent->fields.client->absmin, ent->fields.client->absmax);
124 }
125
126 int CL_GenericHitSuperContentsMask(const prvm_edict_t *passedict)
127 {
128         prvm_eval_t *val;
129         if (passedict)
130         {
131                 val = PRVM_EDICTFIELDVALUE(passedict, prog->fieldoffsets.dphitcontentsmask);
132                 if (val && val->_float)
133                         return (int)val->_float;
134                 else if (passedict->fields.client->solid == SOLID_SLIDEBOX)
135                 {
136                         if ((int)passedict->fields.client->flags & FL_MONSTER)
137                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
138                         else
139                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
140                 }
141                 else if (passedict->fields.client->solid == SOLID_CORPSE)
142                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
143                 else
144                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
145         }
146         else
147                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
148 }
149
150 /*
151 ==================
152 CL_Move
153 ==================
154 */
155 extern cvar_t sv_debugmove;
156 trace_t CL_Move(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities)
157 {
158         vec3_t hullmins, hullmaxs;
159         int i, bodysupercontents;
160         int passedictprog;
161         qboolean pointtrace;
162         prvm_edict_t *traceowner, *touch;
163         trace_t trace;
164         // bounding box of entire move area
165         vec3_t clipboxmins, clipboxmaxs;
166         // size of the moving object
167         vec3_t clipmins, clipmaxs;
168         // size when clipping against monsters
169         vec3_t clipmins2, clipmaxs2;
170         // start and end origin of move
171         vec3_t clipstart, clipend;
172         // trace results
173         trace_t cliptrace;
174         // matrices to transform into/out of other entity's space
175         matrix4x4_t matrix, imatrix;
176         // model of other entity
177         model_t *model;
178         // list of entities to test for collisions
179         int numtouchedicts;
180         prvm_edict_t *touchedicts[MAX_EDICTS];
181
182         if (hitnetworkentity)
183                 *hitnetworkentity = 0;
184
185         VectorCopy(start, clipstart);
186         VectorCopy(end, clipend);
187         VectorCopy(mins, clipmins);
188         VectorCopy(maxs, clipmaxs);
189         VectorCopy(mins, clipmins2);
190         VectorCopy(maxs, clipmaxs2);
191 #if COLLISIONPARANOID >= 3
192         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
193 #endif
194
195         // clip to world
196         Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
197         cliptrace.bmodelstartsolid = cliptrace.startsolid;
198         if (cliptrace.startsolid || cliptrace.fraction < 1)
199                 cliptrace.ent = prog ? prog->edicts : NULL;
200         if (type == MOVE_WORLDONLY)
201                 return cliptrace;
202
203         if (type == MOVE_MISSILE)
204         {
205                 // LordHavoc: modified this, was = -15, now -= 15
206                 for (i = 0;i < 3;i++)
207                 {
208                         clipmins2[i] -= 15;
209                         clipmaxs2[i] += 15;
210                 }
211         }
212
213         // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
214         if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
215                 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
216         else
217         {
218                 VectorCopy(clipmins, hullmins);
219                 VectorCopy(clipmaxs, hullmaxs);
220         }
221
222         // create the bounding box of the entire move
223         for (i = 0;i < 3;i++)
224         {
225                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
226                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
227         }
228
229         // debug override to test against everything
230         if (sv_debugmove.integer)
231         {
232                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
233                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
234         }
235
236         // if the passedict is world, make it NULL (to avoid two checks each time)
237         // this checks prog because this function is often called without a CSQC
238         // VM context
239         if (prog == NULL || passedict == prog->edicts)
240                 passedict = NULL;
241         // precalculate prog value for passedict for comparisons
242         passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
243         // figure out whether this is a point trace for comparisons
244         pointtrace = VectorCompare(clipmins, clipmaxs);
245         // precalculate passedict's owner edict pointer for comparisons
246         traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : 0;
247
248         // collide against network entities
249         if (hitnetworkbrushmodels)
250         {
251                 for (i = 0;i < cl.num_brushmodel_entities;i++)
252                 {
253                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
254                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
255                                 continue;
256                         Collision_ClipToGenericEntity(&trace, ent->model, ent->frame, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
257                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
258                                 *hitnetworkentity = cl.brushmodel_entities[i];
259                         Collision_CombineTraces(&cliptrace, &trace, NULL, true);
260                 }
261         }
262
263         // collide against player entities
264         if (hitnetworkplayers)
265         {
266                 vec3_t origin, entmins, entmaxs;
267                 matrix4x4_t entmatrix, entinversematrix;
268                 for (i = 1;i < cl.maxclients+1;i++)
269                 {
270                         entity_render_t *ent = &cl.entities[i].render;
271                         // don't hit ourselves
272                         if (i == cl.playerentity)
273                                 continue;
274                         Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
275                         VectorAdd(origin, cl.playerstandmins, entmins);
276                         VectorAdd(origin, cl.playerstandmaxs, entmaxs);
277                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
278                                 continue;
279                         Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
280                         Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
281                         Collision_ClipToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
282                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
283                                 *hitnetworkentity = i;
284                         Collision_CombineTraces(&cliptrace, &trace, NULL, false);
285                 }
286         }
287
288         // clip to entities
289         // because this uses World_EntitiestoBox, we know all entity boxes overlap
290         // the clip region, so we can skip culling checks in the loop below
291         // note: if prog is NULL then there won't be any linked entities
292         numtouchedicts = 0;
293         if (hitcsqcentities && prog != NULL)
294         {
295                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
296                 if (numtouchedicts > MAX_EDICTS)
297                 {
298                         // this never happens
299                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
300                         numtouchedicts = MAX_EDICTS;
301                 }
302         }
303         for (i = 0;i < numtouchedicts;i++)
304         {
305                 touch = touchedicts[i];
306
307                 if (touch->fields.client->solid < SOLID_BBOX)
308                         continue;
309                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
310                         continue;
311
312                 if (passedict)
313                 {
314                         // don't clip against self
315                         if (passedict == touch)
316                                 continue;
317                         // don't clip owned entities against owner
318                         if (traceowner == touch)
319                                 continue;
320                         // don't clip owner against owned entities
321                         if (passedictprog == touch->fields.client->owner)
322                                 continue;
323                         // don't clip points against points (they can't collide)
324                         if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
325                                 continue;
326                 }
327
328                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
329
330                 // might interact, so do an exact clip
331                 model = NULL;
332                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
333                 {
334                         unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
335                         // if the modelindex is 0, it shouldn't be SOLID_BSP!
336                         if (modelindex > 0 && modelindex < MAX_MODELS)
337                                 model = sv.models[(int)touch->fields.client->modelindex];
338                 }
339                 if (model)
340                         Matrix4x4_CreateFromQuakeEntity(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2], touch->fields.client->angles[0], touch->fields.client->angles[1], touch->fields.client->angles[2], 1);
341                 else
342                         Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
343                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
344                 if ((int)touch->fields.client->flags & FL_MONSTER)
345                         Collision_ClipToGenericEntity(&trace, model, touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
346                 else
347                         Collision_ClipToGenericEntity(&trace, model, touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
348
349                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
350                         *hitnetworkentity = 0;
351                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
352         }
353
354         return cliptrace;
355 }