3 #include "cl_collision.h"
5 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
7 float maxfrac, maxrealfrac;
10 float tracemins[3], tracemaxs[3];
12 float tempnormal[3], starttransformed[3], endtransformed[3];
14 memset (&trace, 0 , sizeof(trace_t));
16 trace.realfraction = 1;
17 VectorCopy (end, trace.endpos);
21 if (cl.worldmodel && cl.worldmodel->TraceBox)
22 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, vec3_origin, vec3_origin, end, SUPERCONTENTS_SOLID);
25 VectorCopy(trace.plane.normal, normal);
26 maxfrac = trace.fraction;
27 maxrealfrac = trace.realfraction;
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]);
36 // look for embedded bmodels
37 for (n = 0;n < cl.num_entities;n++)
39 if (!cl.entities_active[n])
41 ent = &cl.entities[n].render;
42 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
44 if (!ent->model || !ent->model->TraceBox)
46 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
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))))
53 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
54 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
55 Collision_ClipTrace_Box(&trace, ent->model->normalmins, ent->model->normalmaxs, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID, SUPERCONTENTS_SOLID, 0, NULL);
56 if (maxrealfrac < trace.realfraction)
59 //if (ent->model && ent->model->TraceBox)
60 ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID);
62 if (maxrealfrac > trace.realfraction)
66 maxfrac = trace.fraction;
67 maxrealfrac = trace.realfraction;
70 VectorCopy(trace.plane.normal, tempnormal);
71 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
75 maxfrac = bound(0, maxfrac, 1);
76 maxrealfrac = bound(0, maxrealfrac, 1);
77 //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
79 VectorLerp(start, maxfrac, end, impact);
83 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
85 // FIXME: check multiple brush models
86 if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
87 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
90 model_t *CL_GetModelByIndex(int modelindex)
96 modelindex = -(modelindex+1);
97 if (modelindex < MAX_MODELS)
98 return cl.csqc_model_precache[modelindex];
102 if(modelindex < MAX_MODELS)
103 return cl.model_precache[modelindex];
108 model_t *CL_GetModelFromEdict(prvm_edict_t *ed)
110 if (!ed || ed->priv.server->free)
112 return CL_GetModelByIndex((int)ed->fields.client->modelindex);
115 void CL_LinkEdict(prvm_edict_t *ent)
117 if (ent == prog->edicts)
118 return; // don't add the world
120 if (ent->priv.server->free)
123 VectorAdd(ent->fields.client->origin, ent->fields.client->mins, ent->fields.client->absmin);
124 VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, ent->fields.client->absmax);
126 World_LinkEdict(&cl.world, ent, ent->fields.client->absmin, ent->fields.client->absmax);
129 int CL_GenericHitSuperContentsMask(const prvm_edict_t *passedict)
134 val = PRVM_EDICTFIELDVALUE(passedict, prog->fieldoffsets.dphitcontentsmask);
135 if (val && val->_float)
136 return (int)val->_float;
137 else if (passedict->fields.client->solid == SOLID_SLIDEBOX)
139 if ((int)passedict->fields.client->flags & FL_MONSTER)
140 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
142 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
144 else if (passedict->fields.client->solid == SOLID_CORPSE)
145 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
146 else if (passedict->fields.client->solid == SOLID_TRIGGER)
147 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
149 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
152 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
160 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)
162 vec3_t hullmins, hullmaxs;
163 int i, bodysupercontents;
166 prvm_edict_t *traceowner, *touch;
168 // bounding box of entire move area
169 vec3_t clipboxmins, clipboxmaxs;
170 // size of the moving object
171 vec3_t clipmins, clipmaxs;
172 // size when clipping against monsters
173 vec3_t clipmins2, clipmaxs2;
174 // start and end origin of move
175 vec3_t clipstart, clipend;
178 // matrices to transform into/out of other entity's space
179 matrix4x4_t matrix, imatrix;
180 // model of other entity
182 // list of entities to test for collisions
184 prvm_edict_t *touchedicts[MAX_EDICTS];
186 if (hitnetworkentity)
187 *hitnetworkentity = 0;
189 VectorCopy(start, clipstart);
190 VectorCopy(end, clipend);
191 VectorCopy(mins, clipmins);
192 VectorCopy(maxs, clipmaxs);
193 VectorCopy(mins, clipmins2);
194 VectorCopy(maxs, clipmaxs2);
195 #if COLLISIONPARANOID >= 3
196 Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
200 Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
201 cliptrace.bmodelstartsolid = cliptrace.startsolid;
202 if (cliptrace.startsolid || cliptrace.fraction < 1)
203 cliptrace.ent = prog ? prog->edicts : NULL;
204 if (type == MOVE_WORLDONLY)
207 if (type == MOVE_MISSILE)
209 // LordHavoc: modified this, was = -15, now -= 15
210 for (i = 0;i < 3;i++)
217 // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
218 if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
219 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
222 VectorCopy(clipmins, hullmins);
223 VectorCopy(clipmaxs, hullmaxs);
226 // create the bounding box of the entire move
227 for (i = 0;i < 3;i++)
229 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
230 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
233 // debug override to test against everything
234 if (sv_debugmove.integer)
236 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
237 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
240 // if the passedict is world, make it NULL (to avoid two checks each time)
241 // this checks prog because this function is often called without a CSQC
243 if (prog == NULL || passedict == prog->edicts)
245 // precalculate prog value for passedict for comparisons
246 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
247 // figure out whether this is a point trace for comparisons
248 pointtrace = VectorCompare(clipmins, clipmaxs);
249 // precalculate passedict's owner edict pointer for comparisons
250 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : 0;
252 // collide against network entities
253 if (hitnetworkbrushmodels)
255 for (i = 0;i < cl.num_brushmodel_entities;i++)
257 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
258 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
260 Collision_ClipToGenericEntity(&trace, ent->model, ent->frame2, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
261 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
262 *hitnetworkentity = cl.brushmodel_entities[i];
263 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
267 // collide against player entities
268 if (hitnetworkplayers)
270 vec3_t origin, entmins, entmaxs;
271 matrix4x4_t entmatrix, entinversematrix;
272 for (i = 1;i < cl.maxclients+1;i++)
274 entity_render_t *ent = &cl.entities[i].render;
275 // don't hit ourselves
276 if (i == cl.playerentity)
278 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
279 VectorAdd(origin, cl.playerstandmins, entmins);
280 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
281 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
283 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
284 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
285 Collision_ClipToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
286 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
287 *hitnetworkentity = i;
288 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
293 // because this uses World_EntitiestoBox, we know all entity boxes overlap
294 // the clip region, so we can skip culling checks in the loop below
295 // note: if prog is NULL then there won't be any linked entities
297 if (hitcsqcentities && prog != NULL)
299 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
300 if (numtouchedicts > MAX_EDICTS)
302 // this never happens
303 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
304 numtouchedicts = MAX_EDICTS;
307 for (i = 0;i < numtouchedicts;i++)
309 touch = touchedicts[i];
311 if (touch->fields.client->solid < SOLID_BBOX)
313 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
318 // don't clip against self
319 if (passedict == touch)
321 // don't clip owned entities against owner
322 if (traceowner == touch)
324 // don't clip owner against owned entities
325 if (passedictprog == touch->fields.client->owner)
327 // don't clip points against points (they can't collide)
328 if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
332 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
334 // might interact, so do an exact clip
336 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
338 unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
339 // if the modelindex is 0, it shouldn't be SOLID_BSP!
340 if (modelindex > 0 && modelindex < MAX_MODELS)
341 model = cl.model_precache[(int)touch->fields.client->modelindex];
344 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);
346 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
347 Matrix4x4_Invert_Simple(&imatrix, &matrix);
348 if ((int)touch->fields.client->flags & FL_MONSTER)
349 Collision_ClipToGenericEntity(&trace, model, touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
351 Collision_ClipToGenericEntity(&trace, model, touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
353 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
354 *hitnetworkentity = 0;
355 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);