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) && collision_endposnudge.value > 0)
23 // TRICK: make the trace 1 qu longer!
24 VectorSubtract(pEnd, start, end);
25 len = VectorNormalizeLength(end);
26 VectorMA(pEnd, collision_endposnudge.value, 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->TraceLine)
40 cl.worldmodel->TraceLine(cl.worldmodel, NULL, NULL, &trace, start, 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->TraceLine)
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) && collision_endposnudge.value > 0)
76 Collision_ShortenTrace(&trace, len / (len + collision_endposnudge.value), pEnd);
78 if (maxrealfrac < trace.realfraction)
81 ent->model->TraceLine(ent->model, ent->frameblend, ent->skeleton, &trace, starttransformed, 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)
161 Con_DPrintf("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)
202 int dphitcontentsmask = (int)PRVM_EDICTFIELDFLOAT(passedict, prog->fieldoffsets.dphitcontentsmask);
203 if (dphitcontentsmask)
204 return dphitcontentsmask;
205 else if (passedict->fields.client->solid == SOLID_SLIDEBOX)
207 if ((int)passedict->fields.client->flags & FL_MONSTER)
208 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
210 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
212 else if (passedict->fields.client->solid == SOLID_CORPSE)
213 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
214 else if (passedict->fields.client->solid == SOLID_TRIGGER)
215 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
217 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
220 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
228 trace_t CL_TracePoint(const vec3_t start, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities)
230 int i, bodysupercontents;
232 prvm_edict_t *traceowner, *touch;
234 // bounding box of entire move area
235 vec3_t clipboxmins, clipboxmaxs;
236 // size when clipping against monsters
237 vec3_t clipmins2, clipmaxs2;
238 // start and end origin of move
242 // matrices to transform into/out of other entity's space
243 matrix4x4_t matrix, imatrix;
244 // model of other entity
246 // list of entities to test for collisions
248 static prvm_edict_t *touchedicts[MAX_EDICTS];
250 if (hitnetworkentity)
251 *hitnetworkentity = 0;
253 VectorCopy(start, clipstart);
254 VectorClear(clipmins2);
255 VectorClear(clipmaxs2);
256 #if COLLISIONPARANOID >= 3
257 Con_Printf("move(%f %f %f)", clipstart[0], clipstart[1], clipstart[2]);
261 Collision_ClipPointToWorld(&cliptrace, cl.worldmodel, clipstart, hitsupercontentsmask);
262 cliptrace.bmodelstartsolid = cliptrace.startsolid;
263 if (cliptrace.startsolid || cliptrace.fraction < 1)
264 cliptrace.ent = prog ? prog->edicts : NULL;
265 if (type == MOVE_WORLDONLY)
268 if (type == MOVE_MISSILE)
270 // LordHavoc: modified this, was = -15, now -= 15
271 for (i = 0;i < 3;i++)
278 // create the bounding box of the entire move
279 for (i = 0;i < 3;i++)
281 clipboxmins[i] = clipstart[i] - 1;
282 clipboxmaxs[i] = clipstart[i] + 1;
285 // debug override to test against everything
286 if (sv_debugmove.integer)
288 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
289 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
292 // if the passedict is world, make it NULL (to avoid two checks each time)
293 // this checks prog because this function is often called without a CSQC
295 if (prog == NULL || passedict == prog->edicts)
297 // precalculate prog value for passedict for comparisons
298 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
299 // precalculate passedict's owner edict pointer for comparisons
300 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
302 // collide against network entities
303 if (hitnetworkbrushmodels)
305 for (i = 0;i < cl.num_brushmodel_entities;i++)
307 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
308 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
310 Collision_ClipPointToGenericEntity(&trace, ent->model, ent->frameblend, ent->skeleton, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, hitsupercontentsmask);
311 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
312 *hitnetworkentity = cl.brushmodel_entities[i];
313 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
317 // collide against player entities
318 if (hitnetworkplayers)
320 vec3_t origin, entmins, entmaxs;
321 matrix4x4_t entmatrix, entinversematrix;
323 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
325 // don't hit network players, if we are a nonsolid player
326 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
327 goto skipnetworkplayers;
330 for (i = 1;i <= cl.maxclients;i++)
332 entity_render_t *ent = &cl.entities[i].render;
334 // don't hit ourselves
335 if (i == cl.playerentity)
338 // don't hit players that don't exist
339 if (!cl.scores[i-1].name[0])
342 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
344 // don't hit spectators or nonsolid players
345 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
349 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
350 VectorAdd(origin, cl.playerstandmins, entmins);
351 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
352 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
354 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
355 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
356 Collision_ClipPointToGenericEntity(&trace, NULL, NULL, NULL, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, hitsupercontentsmask);
357 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
358 *hitnetworkentity = i;
359 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
367 // because this uses World_EntitiestoBox, we know all entity boxes overlap
368 // the clip region, so we can skip culling checks in the loop below
369 // note: if prog is NULL then there won't be any linked entities
371 if (hitcsqcentities && prog != NULL)
373 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
374 if (numtouchedicts > MAX_EDICTS)
376 // this never happens
377 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
378 numtouchedicts = MAX_EDICTS;
381 for (i = 0;i < numtouchedicts;i++)
383 touch = touchedicts[i];
385 if (touch->fields.client->solid < SOLID_BBOX)
387 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
392 // don't clip against self
393 if (passedict == touch)
395 // don't clip owned entities against owner
396 if (traceowner == touch)
398 // don't clip owner against owned entities
399 if (passedictprog == touch->fields.client->owner)
401 // don't clip points against points (they can't collide)
402 if (VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
406 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
408 // might interact, so do an exact clip
410 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
411 model = CL_GetModelFromEdict(touch);
413 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);
415 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
416 Matrix4x4_Invert_Simple(&imatrix, &matrix);
417 if ((int)touch->fields.client->flags & FL_MONSTER)
418 Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipstart, hitsupercontentsmask);
420 Collision_ClipPointToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, hitsupercontentsmask);
422 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
423 *hitnetworkentity = 0;
424 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
436 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
437 trace_t CL_TraceLine(const vec3_t start, const vec3_t pEnd, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities, qboolean hitsurfaces)
439 trace_t CL_TraceLine(const vec3_t start, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities, qboolean hitsurfaces)
442 int i, bodysupercontents;
444 prvm_edict_t *traceowner, *touch;
446 // bounding box of entire move area
447 vec3_t clipboxmins, clipboxmaxs;
448 // size when clipping against monsters
449 vec3_t clipmins2, clipmaxs2;
450 // start and end origin of move
451 vec3_t clipstart, clipend;
454 // matrices to transform into/out of other entity's space
455 matrix4x4_t matrix, imatrix;
456 // model of other entity
458 // list of entities to test for collisions
460 static prvm_edict_t *touchedicts[MAX_EDICTS];
461 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
465 if (VectorCompare(start, pEnd))
466 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
468 if(collision_endposnudge.value > 0)
470 // TRICK: make the trace 1 qu longer!
471 VectorSubtract(pEnd, start, end);
472 len = VectorNormalizeLength(end);
473 VectorMA(pEnd, collision_endposnudge.value, end, end);
476 VectorCopy(pEnd, end);
478 if (VectorCompare(start, end))
479 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
482 if (hitnetworkentity)
483 *hitnetworkentity = 0;
485 VectorCopy(start, clipstart);
486 VectorCopy(end, clipend);
487 VectorClear(clipmins2);
488 VectorClear(clipmaxs2);
489 #if COLLISIONPARANOID >= 3
490 Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
494 Collision_ClipLineToWorld(&cliptrace, cl.worldmodel, clipstart, clipend, hitsupercontentsmask, hitsurfaces);
495 cliptrace.bmodelstartsolid = cliptrace.startsolid;
496 if (cliptrace.startsolid || cliptrace.fraction < 1)
497 cliptrace.ent = prog ? prog->edicts : NULL;
498 if (type == MOVE_WORLDONLY)
501 if (type == MOVE_MISSILE)
503 // LordHavoc: modified this, was = -15, now -= 15
504 for (i = 0;i < 3;i++)
511 // create the bounding box of the entire move
512 for (i = 0;i < 3;i++)
514 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + clipmins2[i] - 1;
515 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + clipmaxs2[i] + 1;
518 // debug override to test against everything
519 if (sv_debugmove.integer)
521 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
522 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
525 // if the passedict is world, make it NULL (to avoid two checks each time)
526 // this checks prog because this function is often called without a CSQC
528 if (prog == NULL || passedict == prog->edicts)
530 // precalculate prog value for passedict for comparisons
531 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
532 // precalculate passedict's owner edict pointer for comparisons
533 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
535 // collide against network entities
536 if (hitnetworkbrushmodels)
538 for (i = 0;i < cl.num_brushmodel_entities;i++)
540 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
541 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
543 Collision_ClipLineToGenericEntity(&trace, ent->model, ent->frameblend, ent->skeleton, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, end, hitsupercontentsmask, hitsurfaces);
544 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
545 *hitnetworkentity = cl.brushmodel_entities[i];
546 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
550 // collide against player entities
551 if (hitnetworkplayers)
553 vec3_t origin, entmins, entmaxs;
554 matrix4x4_t entmatrix, entinversematrix;
556 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
558 // don't hit network players, if we are a nonsolid player
559 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
560 goto skipnetworkplayers;
563 for (i = 1;i <= cl.maxclients;i++)
565 entity_render_t *ent = &cl.entities[i].render;
567 // don't hit ourselves
568 if (i == cl.playerentity)
571 // don't hit players that don't exist
572 if (!cl.scores[i-1].name[0])
575 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
577 // don't hit spectators or nonsolid players
578 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
582 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
583 VectorAdd(origin, cl.playerstandmins, entmins);
584 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
585 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
587 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
588 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
589 Collision_ClipLineToGenericEntity(&trace, NULL, NULL, NULL, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, end, hitsupercontentsmask, hitsurfaces);
590 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
591 *hitnetworkentity = i;
592 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
600 // because this uses World_EntitiestoBox, we know all entity boxes overlap
601 // the clip region, so we can skip culling checks in the loop below
602 // note: if prog is NULL then there won't be any linked entities
604 if (hitcsqcentities && prog != NULL)
606 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
607 if (numtouchedicts > MAX_EDICTS)
609 // this never happens
610 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
611 numtouchedicts = MAX_EDICTS;
614 for (i = 0;i < numtouchedicts;i++)
616 touch = touchedicts[i];
618 if (touch->fields.client->solid < SOLID_BBOX)
620 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
625 // don't clip against self
626 if (passedict == touch)
628 // don't clip owned entities against owner
629 if (traceowner == touch)
631 // don't clip owner against owned entities
632 if (passedictprog == touch->fields.client->owner)
634 // don't clip points against points (they can't collide)
635 if (VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
639 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
641 // might interact, so do an exact clip
643 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
644 model = CL_GetModelFromEdict(touch);
646 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);
648 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
649 Matrix4x4_Invert_Simple(&imatrix, &matrix);
650 if (type == MOVE_MISSILE && (int)touch->fields.client->flags & FL_MONSTER)
651 Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
653 Collision_ClipLineToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipend, hitsupercontentsmask, hitsurfaces);
655 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
656 *hitnetworkentity = 0;
657 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
661 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
662 if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
663 Collision_ShortenTrace(&cliptrace, len / (len + collision_endposnudge.value), pEnd);
673 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
674 trace_t CL_TraceBox(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)
676 trace_t CL_TraceBox(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)
679 vec3_t hullmins, hullmaxs;
680 int i, bodysupercontents;
683 prvm_edict_t *traceowner, *touch;
685 // bounding box of entire move area
686 vec3_t clipboxmins, clipboxmaxs;
687 // size of the moving object
688 vec3_t clipmins, clipmaxs;
689 // size when clipping against monsters
690 vec3_t clipmins2, clipmaxs2;
691 // start and end origin of move
692 vec3_t clipstart, clipend;
695 // matrices to transform into/out of other entity's space
696 matrix4x4_t matrix, imatrix;
697 // model of other entity
699 // list of entities to test for collisions
701 static prvm_edict_t *touchedicts[MAX_EDICTS];
702 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
706 if (VectorCompare(mins, maxs))
708 vec3_t shiftstart, shiftend;
709 VectorAdd(start, mins, shiftstart);
710 VectorAdd(pEnd, mins, shiftend);
711 if (VectorCompare(start, pEnd))
712 trace = CL_TracePoint(shiftstart, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
714 trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities, false);
715 VectorSubtract(trace.endpos, mins, trace.endpos);
719 if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
721 // TRICK: make the trace 1 qu longer!
722 VectorSubtract(pEnd, start, end);
723 len = VectorNormalizeLength(end);
724 VectorMA(pEnd, collision_endposnudge.value, end, end);
727 VectorCopy(pEnd, end);
729 if (VectorCompare(mins, maxs))
731 vec3_t shiftstart, shiftend;
732 VectorAdd(start, mins, shiftstart);
733 VectorAdd(end, mins, shiftend);
734 if (VectorCompare(start, end))
735 trace = CL_TracePoint(shiftstart, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
737 trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities, false);
738 VectorSubtract(trace.endpos, mins, trace.endpos);
743 if (hitnetworkentity)
744 *hitnetworkentity = 0;
746 VectorCopy(start, clipstart);
747 VectorCopy(end, clipend);
748 VectorCopy(mins, clipmins);
749 VectorCopy(maxs, clipmaxs);
750 VectorCopy(mins, clipmins2);
751 VectorCopy(maxs, clipmaxs2);
752 #if COLLISIONPARANOID >= 3
753 Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
757 Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
758 cliptrace.bmodelstartsolid = cliptrace.startsolid;
759 if (cliptrace.startsolid || cliptrace.fraction < 1)
760 cliptrace.ent = prog ? prog->edicts : NULL;
761 if (type == MOVE_WORLDONLY)
764 if (type == MOVE_MISSILE)
766 // LordHavoc: modified this, was = -15, now -= 15
767 for (i = 0;i < 3;i++)
774 // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
775 if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
776 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
779 VectorCopy(clipmins, hullmins);
780 VectorCopy(clipmaxs, hullmaxs);
783 // create the bounding box of the entire move
784 for (i = 0;i < 3;i++)
786 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
787 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
790 // debug override to test against everything
791 if (sv_debugmove.integer)
793 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
794 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
797 // if the passedict is world, make it NULL (to avoid two checks each time)
798 // this checks prog because this function is often called without a CSQC
800 if (prog == NULL || passedict == prog->edicts)
802 // precalculate prog value for passedict for comparisons
803 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
804 // figure out whether this is a point trace for comparisons
805 pointtrace = VectorCompare(clipmins, clipmaxs);
806 // precalculate passedict's owner edict pointer for comparisons
807 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
809 // collide against network entities
810 if (hitnetworkbrushmodels)
812 for (i = 0;i < cl.num_brushmodel_entities;i++)
814 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
815 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
817 Collision_ClipToGenericEntity(&trace, ent->model, ent->frameblend, ent->skeleton, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
818 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
819 *hitnetworkentity = cl.brushmodel_entities[i];
820 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
824 // collide against player entities
825 if (hitnetworkplayers)
827 vec3_t origin, entmins, entmaxs;
828 matrix4x4_t entmatrix, entinversematrix;
830 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
832 // don't hit network players, if we are a nonsolid player
833 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
834 goto skipnetworkplayers;
837 for (i = 1;i <= cl.maxclients;i++)
839 entity_render_t *ent = &cl.entities[i].render;
841 // don't hit ourselves
842 if (i == cl.playerentity)
845 // don't hit players that don't exist
846 if (!cl.scores[i-1].name[0])
849 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
851 // don't hit spectators or nonsolid players
852 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
856 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
857 VectorAdd(origin, cl.playerstandmins, entmins);
858 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
859 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
861 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
862 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
863 Collision_ClipToGenericEntity(&trace, NULL, NULL, NULL, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
864 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
865 *hitnetworkentity = i;
866 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
874 // because this uses World_EntitiestoBox, we know all entity boxes overlap
875 // the clip region, so we can skip culling checks in the loop below
876 // note: if prog is NULL then there won't be any linked entities
878 if (hitcsqcentities && prog != NULL)
880 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
881 if (numtouchedicts > MAX_EDICTS)
883 // this never happens
884 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
885 numtouchedicts = MAX_EDICTS;
888 for (i = 0;i < numtouchedicts;i++)
890 touch = touchedicts[i];
892 if (touch->fields.client->solid < SOLID_BBOX)
894 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
899 // don't clip against self
900 if (passedict == touch)
902 // don't clip owned entities against owner
903 if (traceowner == touch)
905 // don't clip owner against owned entities
906 if (passedictprog == touch->fields.client->owner)
908 // don't clip points against points (they can't collide)
909 if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
913 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
915 // might interact, so do an exact clip
917 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
918 model = CL_GetModelFromEdict(touch);
920 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);
922 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
923 Matrix4x4_Invert_Simple(&imatrix, &matrix);
924 if ((int)touch->fields.client->flags & FL_MONSTER)
925 Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
927 Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
929 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
930 *hitnetworkentity = 0;
931 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
935 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
936 if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
937 Collision_ShortenTrace(&cliptrace, len / (len + collision_endposnudge.value), pEnd);