3 #include "cl_collision.h"
5 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
6 float CL_SelectTraceLine(const vec3_t start, const vec3_t pEnd, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
8 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
11 float maxfrac, maxrealfrac;
14 float tracemins[3], tracemaxs[3];
16 float tempnormal[3], starttransformed[3], endtransformed[3];
17 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
21 if(!VectorCompare(start, pEnd))
23 // TRICK: make the trace 1 qu longer!
24 VectorSubtract(pEnd, start, end);
25 len = VectorNormalizeLength(end);
26 VectorAdd(pEnd, end, end);
29 VectorCopy(pEnd, end);
32 memset (&trace, 0 , sizeof(trace_t));
34 trace.realfraction = 1;
35 VectorCopy (end, trace.endpos);
39 if (cl.worldmodel && cl.worldmodel->TraceBox)
40 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, vec3_origin, vec3_origin, end, SUPERCONTENTS_SOLID);
43 VectorCopy(trace.plane.normal, normal);
44 maxfrac = trace.fraction;
45 maxrealfrac = trace.realfraction;
47 tracemins[0] = min(start[0], end[0]);
48 tracemaxs[0] = max(start[0], end[0]);
49 tracemins[1] = min(start[1], end[1]);
50 tracemaxs[1] = max(start[1], end[1]);
51 tracemins[2] = min(start[2], end[2]);
52 tracemaxs[2] = max(start[2], end[2]);
54 // look for embedded bmodels
55 for (n = 0;n < cl.num_entities;n++)
57 if (!cl.entities_active[n])
59 ent = &cl.entities[n].render;
60 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
62 if (!ent->model || !ent->model->TraceBox)
64 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
66 // if transparent and not selectable, skip entity
67 if (!(cl.entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
71 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
72 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
73 Collision_ClipTrace_Box(&trace, ent->model->normalmins, ent->model->normalmaxs, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID, SUPERCONTENTS_SOLID, 0, NULL);
74 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
75 if(!VectorCompare(start, pEnd))
76 Collision_ShortenTrace(&trace, len / (len + 1), pEnd);
78 if (maxrealfrac < trace.realfraction)
81 ent->model->TraceBox(ent->model, ent->frameblend[0].subframe, &trace, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID);
83 if (maxrealfrac > trace.realfraction)
87 maxfrac = trace.fraction;
88 maxrealfrac = trace.realfraction;
91 VectorCopy(trace.plane.normal, tempnormal);
92 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
96 maxfrac = bound(0, maxfrac, 1);
97 maxrealfrac = bound(0, maxrealfrac, 1);
98 //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
100 VectorLerp(start, maxfrac, end, impact);
104 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
106 // FIXME: check multiple brush models
107 if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
108 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
111 dp_model_t *CL_GetModelByIndex(int modelindex)
117 modelindex = -(modelindex+1);
118 if (modelindex < MAX_MODELS)
119 return cl.csqc_model_precache[modelindex];
123 if(modelindex < MAX_MODELS)
124 return cl.model_precache[modelindex];
129 dp_model_t *CL_GetModelFromEdict(prvm_edict_t *ed)
131 if (!ed || ed->priv.server->free)
133 return CL_GetModelByIndex((int)ed->fields.client->modelindex);
136 void CL_LinkEdict(prvm_edict_t *ent)
140 if (ent == prog->edicts)
141 return; // don't add the world
143 if (ent->priv.server->free)
148 if (ent->fields.client->solid == SOLID_BSP)
150 dp_model_t *model = CL_GetModelByIndex( (int)ent->fields.client->modelindex );
153 Con_Printf("edict %i: SOLID_BSP with invalid modelindex!\n", PRVM_NUM_FOR_EDICT(ent));
155 model = CL_GetModelByIndex( 0 );
160 if (!model->TraceBox && developer.integer >= 1)
161 Con_Printf("edict %i: SOLID_BSP with non-collidable model\n", PRVM_NUM_FOR_EDICT(ent));
163 if (ent->fields.client->angles[0] || ent->fields.client->angles[2] || ent->fields.client->avelocity[0] || ent->fields.client->avelocity[2])
165 VectorAdd(ent->fields.client->origin, model->rotatedmins, mins);
166 VectorAdd(ent->fields.client->origin, model->rotatedmaxs, maxs);
168 else if (ent->fields.client->angles[1] || ent->fields.client->avelocity[1])
170 VectorAdd(ent->fields.client->origin, model->yawmins, mins);
171 VectorAdd(ent->fields.client->origin, model->yawmaxs, maxs);
175 VectorAdd(ent->fields.client->origin, model->normalmins, mins);
176 VectorAdd(ent->fields.client->origin, model->normalmaxs, maxs);
181 // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
182 VectorAdd(ent->fields.client->origin, ent->fields.client->mins, mins);
183 VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, maxs);
188 VectorAdd(ent->fields.client->origin, ent->fields.client->mins, mins);
189 VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, maxs);
192 VectorCopy(mins, ent->fields.client->absmin);
193 VectorCopy(maxs, ent->fields.client->absmax);
195 World_LinkEdict(&cl.world, ent, ent->fields.client->absmin, ent->fields.client->absmax);
198 int CL_GenericHitSuperContentsMask(const prvm_edict_t *passedict)
203 val = PRVM_EDICTFIELDVALUE(passedict, prog->fieldoffsets.dphitcontentsmask);
204 if (val && val->_float)
205 return (int)val->_float;
206 else if (passedict->fields.client->solid == SOLID_SLIDEBOX)
208 if ((int)passedict->fields.client->flags & FL_MONSTER)
209 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
211 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
213 else if (passedict->fields.client->solid == SOLID_CORPSE)
214 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
215 else if (passedict->fields.client->solid == SOLID_TRIGGER)
216 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
218 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
221 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
229 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
230 trace_t CL_Move(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t pEnd, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities)
232 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)
235 vec3_t hullmins, hullmaxs;
236 int i, bodysupercontents;
239 prvm_edict_t *traceowner, *touch;
241 // bounding box of entire move area
242 vec3_t clipboxmins, clipboxmaxs;
243 // size of the moving object
244 vec3_t clipmins, clipmaxs;
245 // size when clipping against monsters
246 vec3_t clipmins2, clipmaxs2;
247 // start and end origin of move
248 vec3_t clipstart, clipend;
251 // matrices to transform into/out of other entity's space
252 matrix4x4_t matrix, imatrix;
253 // model of other entity
255 // list of entities to test for collisions
257 prvm_edict_t *touchedicts[MAX_EDICTS];
258 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
262 if(!VectorCompare(start, pEnd))
264 // TRICK: make the trace 1 qu longer!
265 VectorSubtract(pEnd, start, end);
266 len = VectorNormalizeLength(end);
267 VectorAdd(pEnd, end, end);
270 VectorCopy(pEnd, end);
273 if (hitnetworkentity)
274 *hitnetworkentity = 0;
276 VectorCopy(start, clipstart);
277 VectorCopy(end, clipend);
278 VectorCopy(mins, clipmins);
279 VectorCopy(maxs, clipmaxs);
280 VectorCopy(mins, clipmins2);
281 VectorCopy(maxs, clipmaxs2);
282 #if COLLISIONPARANOID >= 3
283 Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
287 Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
288 cliptrace.bmodelstartsolid = cliptrace.startsolid;
289 if (cliptrace.startsolid || cliptrace.fraction < 1)
290 cliptrace.ent = prog ? prog->edicts : NULL;
291 if (type == MOVE_WORLDONLY)
294 if (type == MOVE_MISSILE)
296 // LordHavoc: modified this, was = -15, now -= 15
297 for (i = 0;i < 3;i++)
304 // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
305 if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
306 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
309 VectorCopy(clipmins, hullmins);
310 VectorCopy(clipmaxs, hullmaxs);
313 // create the bounding box of the entire move
314 for (i = 0;i < 3;i++)
316 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
317 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
320 // debug override to test against everything
321 if (sv_debugmove.integer)
323 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
324 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
327 // if the passedict is world, make it NULL (to avoid two checks each time)
328 // this checks prog because this function is often called without a CSQC
330 if (prog == NULL || passedict == prog->edicts)
332 // precalculate prog value for passedict for comparisons
333 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
334 // figure out whether this is a point trace for comparisons
335 pointtrace = VectorCompare(clipmins, clipmaxs);
336 // precalculate passedict's owner edict pointer for comparisons
337 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
339 // collide against network entities
340 if (hitnetworkbrushmodels)
342 for (i = 0;i < cl.num_brushmodel_entities;i++)
344 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
345 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
347 Collision_ClipToGenericEntity(&trace, ent->model, ent->frameblend[0].subframe, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
348 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
349 *hitnetworkentity = cl.brushmodel_entities[i];
350 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
354 // collide against player entities
355 if (hitnetworkplayers)
357 vec3_t origin, entmins, entmaxs;
358 matrix4x4_t entmatrix, entinversematrix;
360 if(gamemode == GAME_NEXUIZ)
362 // don't hit network players, if we are a nonsolid player
363 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
364 goto skipnetworkplayers;
367 for (i = 1;i <= cl.maxclients;i++)
369 entity_render_t *ent = &cl.entities[i].render;
371 // don't hit ourselves
372 if (i == cl.playerentity)
375 // don't hit players that don't exist
376 if (!cl.scores[i-1].name[0])
379 if(gamemode == GAME_NEXUIZ)
381 // don't hit spectators or nonsolid players
382 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
386 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
387 VectorAdd(origin, cl.playerstandmins, entmins);
388 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
389 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
391 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
392 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
393 Collision_ClipToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
394 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
395 *hitnetworkentity = i;
396 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
404 // because this uses World_EntitiestoBox, we know all entity boxes overlap
405 // the clip region, so we can skip culling checks in the loop below
406 // note: if prog is NULL then there won't be any linked entities
408 if (hitcsqcentities && prog != NULL)
410 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
411 if (numtouchedicts > MAX_EDICTS)
413 // this never happens
414 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
415 numtouchedicts = MAX_EDICTS;
418 for (i = 0;i < numtouchedicts;i++)
420 touch = touchedicts[i];
422 if (touch->fields.client->solid < SOLID_BBOX)
424 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
429 // don't clip against self
430 if (passedict == touch)
432 // don't clip owned entities against owner
433 if (traceowner == touch)
435 // don't clip owner against owned entities
436 if (passedictprog == touch->fields.client->owner)
438 // don't clip points against points (they can't collide)
439 if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
443 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
445 // might interact, so do an exact clip
447 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
449 unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
450 // if the modelindex is 0, it shouldn't be SOLID_BSP!
451 if (modelindex > 0 && modelindex < MAX_MODELS)
452 model = cl.model_precache[(int)touch->fields.client->modelindex];
455 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);
457 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
458 Matrix4x4_Invert_Simple(&imatrix, &matrix);
459 if ((int)touch->fields.client->flags & FL_MONSTER)
460 Collision_ClipToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
462 Collision_ClipToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
464 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
465 *hitnetworkentity = 0;
466 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
470 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
471 if(!VectorCompare(start, pEnd))
472 Collision_ShortenTrace(&cliptrace, len / (len + 1), pEnd);