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->TraceLine)
40 cl.worldmodel->TraceLine(cl.worldmodel, 0, &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))
76 Collision_ShortenTrace(&trace, len / (len + 1), pEnd);
78 if (maxrealfrac < trace.realfraction)
81 ent->model->TraceLine(ent->model, ent->frameblend[0].subframe, &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 && 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 trace_t CL_TracePoint(const vec3_t start, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities)
231 int i, bodysupercontents;
233 prvm_edict_t *traceowner, *touch;
235 // bounding box of entire move area
236 vec3_t clipboxmins, clipboxmaxs;
237 // size when clipping against monsters
238 vec3_t clipmins2, clipmaxs2;
239 // start and end origin of move
243 // matrices to transform into/out of other entity's space
244 matrix4x4_t matrix, imatrix;
245 // model of other entity
247 // list of entities to test for collisions
249 prvm_edict_t *touchedicts[MAX_EDICTS];
251 if (hitnetworkentity)
252 *hitnetworkentity = 0;
254 VectorCopy(start, clipstart);
255 VectorClear(clipmins2);
256 VectorClear(clipmaxs2);
257 #if COLLISIONPARANOID >= 3
258 Con_Printf("move(%f %f %f)", clipstart[0], clipstart[1], clipstart[2]);
262 Collision_ClipPointToWorld(&cliptrace, cl.worldmodel, clipstart, hitsupercontentsmask);
263 cliptrace.bmodelstartsolid = cliptrace.startsolid;
264 if (cliptrace.startsolid || cliptrace.fraction < 1)
265 cliptrace.ent = prog ? prog->edicts : NULL;
266 if (type == MOVE_WORLDONLY)
269 if (type == MOVE_MISSILE)
271 // LordHavoc: modified this, was = -15, now -= 15
272 for (i = 0;i < 3;i++)
279 // create the bounding box of the entire move
280 for (i = 0;i < 3;i++)
282 clipboxmins[i] = clipstart[i] - 1;
283 clipboxmaxs[i] = clipstart[i] + 1;
286 // debug override to test against everything
287 if (sv_debugmove.integer)
289 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
290 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
293 // if the passedict is world, make it NULL (to avoid two checks each time)
294 // this checks prog because this function is often called without a CSQC
296 if (prog == NULL || passedict == prog->edicts)
298 // precalculate prog value for passedict for comparisons
299 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
300 // precalculate passedict's owner edict pointer for comparisons
301 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
303 // collide against network entities
304 if (hitnetworkbrushmodels)
306 for (i = 0;i < cl.num_brushmodel_entities;i++)
308 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
309 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
311 Collision_ClipPointToGenericEntity(&trace, ent->model, ent->frameblend[0].subframe, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, hitsupercontentsmask);
312 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
313 *hitnetworkentity = cl.brushmodel_entities[i];
314 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
318 // collide against player entities
319 if (hitnetworkplayers)
321 vec3_t origin, entmins, entmaxs;
322 matrix4x4_t entmatrix, entinversematrix;
324 if(gamemode == GAME_NEXUIZ)
326 // don't hit network players, if we are a nonsolid player
327 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
328 goto skipnetworkplayers;
331 for (i = 1;i <= cl.maxclients;i++)
333 entity_render_t *ent = &cl.entities[i].render;
335 // don't hit ourselves
336 if (i == cl.playerentity)
339 // don't hit players that don't exist
340 if (!cl.scores[i-1].name[0])
343 if(gamemode == GAME_NEXUIZ)
345 // don't hit spectators or nonsolid players
346 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
350 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
351 VectorAdd(origin, cl.playerstandmins, entmins);
352 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
353 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
355 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
356 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
357 Collision_ClipPointToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, hitsupercontentsmask);
358 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
359 *hitnetworkentity = i;
360 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
368 // because this uses World_EntitiestoBox, we know all entity boxes overlap
369 // the clip region, so we can skip culling checks in the loop below
370 // note: if prog is NULL then there won't be any linked entities
372 if (hitcsqcentities && prog != NULL)
374 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
375 if (numtouchedicts > MAX_EDICTS)
377 // this never happens
378 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
379 numtouchedicts = MAX_EDICTS;
382 for (i = 0;i < numtouchedicts;i++)
384 touch = touchedicts[i];
386 if (touch->fields.client->solid < SOLID_BBOX)
388 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
393 // don't clip against self
394 if (passedict == touch)
396 // don't clip owned entities against owner
397 if (traceowner == touch)
399 // don't clip owner against owned entities
400 if (passedictprog == touch->fields.client->owner)
402 // don't clip points against points (they can't collide)
403 if (VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
407 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
409 // might interact, so do an exact clip
411 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
413 unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
414 // if the modelindex is 0, it shouldn't be SOLID_BSP!
415 if (modelindex > 0 && modelindex < MAX_MODELS)
416 model = cl.model_precache[(int)touch->fields.client->modelindex];
419 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);
421 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
422 Matrix4x4_Invert_Simple(&imatrix, &matrix);
423 if ((int)touch->fields.client->flags & FL_MONSTER)
424 Collision_ClipToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipstart, hitsupercontentsmask);
426 Collision_ClipPointToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, hitsupercontentsmask);
428 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
429 *hitnetworkentity = 0;
430 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
442 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
443 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)
445 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)
448 int i, bodysupercontents;
450 prvm_edict_t *traceowner, *touch;
452 // bounding box of entire move area
453 vec3_t clipboxmins, clipboxmaxs;
454 // size when clipping against monsters
455 vec3_t clipmins2, clipmaxs2;
456 // start and end origin of move
457 vec3_t clipstart, clipend;
460 // matrices to transform into/out of other entity's space
461 matrix4x4_t matrix, imatrix;
462 // model of other entity
464 // list of entities to test for collisions
466 prvm_edict_t *touchedicts[MAX_EDICTS];
467 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
471 if(!VectorCompare(start, pEnd))
473 // TRICK: make the trace 1 qu longer!
474 VectorSubtract(pEnd, start, end);
475 len = VectorNormalizeLength(end);
476 VectorAdd(pEnd, end, end);
479 VectorCopy(pEnd, end);
482 if (VectorCompare(start, end))
483 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
485 if (hitnetworkentity)
486 *hitnetworkentity = 0;
488 VectorCopy(start, clipstart);
489 VectorCopy(end, clipend);
490 VectorClear(clipmins2);
491 VectorClear(clipmaxs2);
492 #if COLLISIONPARANOID >= 3
493 Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
497 Collision_ClipLineToWorld(&cliptrace, cl.worldmodel, clipstart, clipend, hitsupercontentsmask);
498 cliptrace.bmodelstartsolid = cliptrace.startsolid;
499 if (cliptrace.startsolid || cliptrace.fraction < 1)
500 cliptrace.ent = prog ? prog->edicts : NULL;
501 if (type == MOVE_WORLDONLY)
504 if (type == MOVE_MISSILE)
506 // LordHavoc: modified this, was = -15, now -= 15
507 for (i = 0;i < 3;i++)
514 // create the bounding box of the entire move
515 for (i = 0;i < 3;i++)
517 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + clipmins2[i] - 1;
518 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + clipmaxs2[i] + 1;
521 // debug override to test against everything
522 if (sv_debugmove.integer)
524 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
525 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
528 // if the passedict is world, make it NULL (to avoid two checks each time)
529 // this checks prog because this function is often called without a CSQC
531 if (prog == NULL || passedict == prog->edicts)
533 // precalculate prog value for passedict for comparisons
534 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
535 // precalculate passedict's owner edict pointer for comparisons
536 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
538 // collide against network entities
539 if (hitnetworkbrushmodels)
541 for (i = 0;i < cl.num_brushmodel_entities;i++)
543 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
544 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
546 Collision_ClipLineToGenericEntity(&trace, ent->model, ent->frameblend[0].subframe, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, end, hitsupercontentsmask);
547 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
548 *hitnetworkentity = cl.brushmodel_entities[i];
549 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
553 // collide against player entities
554 if (hitnetworkplayers)
556 vec3_t origin, entmins, entmaxs;
557 matrix4x4_t entmatrix, entinversematrix;
559 if(gamemode == GAME_NEXUIZ)
561 // don't hit network players, if we are a nonsolid player
562 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
563 goto skipnetworkplayers;
566 for (i = 1;i <= cl.maxclients;i++)
568 entity_render_t *ent = &cl.entities[i].render;
570 // don't hit ourselves
571 if (i == cl.playerentity)
574 // don't hit players that don't exist
575 if (!cl.scores[i-1].name[0])
578 if(gamemode == GAME_NEXUIZ)
580 // don't hit spectators or nonsolid players
581 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
585 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
586 VectorAdd(origin, cl.playerstandmins, entmins);
587 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
588 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
590 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
591 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
592 Collision_ClipLineToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, end, hitsupercontentsmask);
593 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
594 *hitnetworkentity = i;
595 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
603 // because this uses World_EntitiestoBox, we know all entity boxes overlap
604 // the clip region, so we can skip culling checks in the loop below
605 // note: if prog is NULL then there won't be any linked entities
607 if (hitcsqcentities && prog != NULL)
609 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
610 if (numtouchedicts > MAX_EDICTS)
612 // this never happens
613 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
614 numtouchedicts = MAX_EDICTS;
617 for (i = 0;i < numtouchedicts;i++)
619 touch = touchedicts[i];
621 if (touch->fields.client->solid < SOLID_BBOX)
623 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
628 // don't clip against self
629 if (passedict == touch)
631 // don't clip owned entities against owner
632 if (traceowner == touch)
634 // don't clip owner against owned entities
635 if (passedictprog == touch->fields.client->owner)
637 // don't clip points against points (they can't collide)
638 if (VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
642 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
644 // might interact, so do an exact clip
646 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
648 unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
649 // if the modelindex is 0, it shouldn't be SOLID_BSP!
650 if (modelindex > 0 && modelindex < MAX_MODELS)
651 model = cl.model_precache[(int)touch->fields.client->modelindex];
654 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);
656 Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
657 Matrix4x4_Invert_Simple(&imatrix, &matrix);
658 if (type == MOVE_MISSILE && (int)touch->fields.client->flags & FL_MONSTER)
659 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);
661 Collision_ClipLineToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipend, hitsupercontentsmask);
663 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
664 *hitnetworkentity = 0;
665 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
669 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
670 if(!VectorCompare(start, pEnd))
671 Collision_ShortenTrace(&cliptrace, len / (len + 1), pEnd);
681 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
682 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)
684 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)
687 vec3_t hullmins, hullmaxs;
688 int i, bodysupercontents;
691 prvm_edict_t *traceowner, *touch;
693 // bounding box of entire move area
694 vec3_t clipboxmins, clipboxmaxs;
695 // size of the moving object
696 vec3_t clipmins, clipmaxs;
697 // size when clipping against monsters
698 vec3_t clipmins2, clipmaxs2;
699 // start and end origin of move
700 vec3_t clipstart, clipend;
703 // matrices to transform into/out of other entity's space
704 matrix4x4_t matrix, imatrix;
705 // model of other entity
707 // list of entities to test for collisions
709 prvm_edict_t *touchedicts[MAX_EDICTS];
710 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
714 if(!VectorCompare(start, pEnd))
716 // TRICK: make the trace 1 qu longer!
717 VectorSubtract(pEnd, start, end);
718 len = VectorNormalizeLength(end);
719 VectorAdd(pEnd, end, end);
722 VectorCopy(pEnd, end);
725 if (VectorCompare(mins, maxs))
727 vec3_t shiftstart, shiftend;
728 VectorAdd(start, mins, shiftstart);
729 VectorAdd(end, mins, shiftend);
730 if (VectorCompare(start, end))
731 trace = CL_TracePoint(shiftstart, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
733 trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
734 VectorSubtract(trace.endpos, mins, trace.endpos);
738 if (hitnetworkentity)
739 *hitnetworkentity = 0;
741 VectorCopy(start, clipstart);
742 VectorCopy(end, clipend);
743 VectorCopy(mins, clipmins);
744 VectorCopy(maxs, clipmaxs);
745 VectorCopy(mins, clipmins2);
746 VectorCopy(maxs, clipmaxs2);
747 #if COLLISIONPARANOID >= 3
748 Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
752 Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
753 cliptrace.bmodelstartsolid = cliptrace.startsolid;
754 if (cliptrace.startsolid || cliptrace.fraction < 1)
755 cliptrace.ent = prog ? prog->edicts : NULL;
756 if (type == MOVE_WORLDONLY)
759 if (type == MOVE_MISSILE)
761 // LordHavoc: modified this, was = -15, now -= 15
762 for (i = 0;i < 3;i++)
769 // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
770 if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
771 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
774 VectorCopy(clipmins, hullmins);
775 VectorCopy(clipmaxs, hullmaxs);
778 // create the bounding box of the entire move
779 for (i = 0;i < 3;i++)
781 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
782 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
785 // debug override to test against everything
786 if (sv_debugmove.integer)
788 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
789 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = 999999999;
792 // if the passedict is world, make it NULL (to avoid two checks each time)
793 // this checks prog because this function is often called without a CSQC
795 if (prog == NULL || passedict == prog->edicts)
797 // precalculate prog value for passedict for comparisons
798 passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
799 // figure out whether this is a point trace for comparisons
800 pointtrace = VectorCompare(clipmins, clipmaxs);
801 // precalculate passedict's owner edict pointer for comparisons
802 traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
804 // collide against network entities
805 if (hitnetworkbrushmodels)
807 for (i = 0;i < cl.num_brushmodel_entities;i++)
809 entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
810 if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
812 Collision_ClipToGenericEntity(&trace, ent->model, ent->frameblend[0].subframe, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
813 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
814 *hitnetworkentity = cl.brushmodel_entities[i];
815 Collision_CombineTraces(&cliptrace, &trace, NULL, true);
819 // collide against player entities
820 if (hitnetworkplayers)
822 vec3_t origin, entmins, entmaxs;
823 matrix4x4_t entmatrix, entinversematrix;
825 if(gamemode == GAME_NEXUIZ)
827 // don't hit network players, if we are a nonsolid player
828 if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
829 goto skipnetworkplayers;
832 for (i = 1;i <= cl.maxclients;i++)
834 entity_render_t *ent = &cl.entities[i].render;
836 // don't hit ourselves
837 if (i == cl.playerentity)
840 // don't hit players that don't exist
841 if (!cl.scores[i-1].name[0])
844 if(gamemode == GAME_NEXUIZ)
846 // don't hit spectators or nonsolid players
847 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
851 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
852 VectorAdd(origin, cl.playerstandmins, entmins);
853 VectorAdd(origin, cl.playerstandmaxs, entmaxs);
854 if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
856 Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
857 Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
858 Collision_ClipToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
859 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
860 *hitnetworkentity = i;
861 Collision_CombineTraces(&cliptrace, &trace, NULL, false);
869 // because this uses World_EntitiestoBox, we know all entity boxes overlap
870 // the clip region, so we can skip culling checks in the loop below
871 // note: if prog is NULL then there won't be any linked entities
873 if (hitcsqcentities && prog != NULL)
875 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
876 if (numtouchedicts > MAX_EDICTS)
878 // this never happens
879 Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
880 numtouchedicts = MAX_EDICTS;
883 for (i = 0;i < numtouchedicts;i++)
885 touch = touchedicts[i];
887 if (touch->fields.client->solid < SOLID_BBOX)
889 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
894 // don't clip against self
895 if (passedict == touch)
897 // don't clip owned entities against owner
898 if (traceowner == touch)
900 // don't clip owner against owned entities
901 if (passedictprog == touch->fields.client->owner)
903 // don't clip points against points (they can't collide)
904 if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
908 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
910 // might interact, so do an exact clip
912 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
914 unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
915 // if the modelindex is 0, it shouldn't be SOLID_BSP!
916 if (modelindex > 0 && modelindex < MAX_MODELS)
917 model = cl.model_precache[(int)touch->fields.client->modelindex];
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, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
927 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);
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))
937 Collision_ShortenTrace(&cliptrace, len / (len + 1), pEnd);