]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_collision.c
implemented r_transparent_alphatocoverage 2 which promotes alphablend if
[xonotic/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
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)
7 #else
8 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
9 #endif
10 {
11         float maxfrac, maxrealfrac;
12         int n;
13         entity_render_t *ent;
14         float tracemins[3], tracemaxs[3];
15         trace_t trace;
16         float tempnormal[3], starttransformed[3], endtransformed[3];
17 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
18         vec3_t end;
19         vec_t len = 0;
20
21         if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
22         {
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);
27         }
28         else
29                 VectorCopy(pEnd, end);
30 #endif
31
32         memset (&trace, 0 , sizeof(trace_t));
33         trace.fraction = 1;
34         trace.realfraction = 1;
35         VectorCopy (end, trace.endpos);
36
37         if (hitent)
38                 *hitent = 0;
39         if (cl.worldmodel && cl.worldmodel->TraceLine)
40                 cl.worldmodel->TraceLine(cl.worldmodel, NULL, NULL, &trace, start, end, SUPERCONTENTS_SOLID);
41
42         if (normal)
43                 VectorCopy(trace.plane.normal, normal);
44         maxfrac = trace.fraction;
45         maxrealfrac = trace.realfraction;
46
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]);
53
54         // look for embedded bmodels
55         for (n = 0;n < cl.num_entities;n++)
56         {
57                 if (!cl.entities_active[n])
58                         continue;
59                 ent = &cl.entities[n].render;
60                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
61                         continue;
62                 if (!ent->model || !ent->model->TraceLine)
63                         continue;
64                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
65                         continue;
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))))
68                         continue;
69                 if (ent == ignoreent)
70                         continue;
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);
77 #endif
78                 if (maxrealfrac < trace.realfraction)
79                         continue;
80
81                 ent->model->TraceLine(ent->model, ent->frameblend, ent->skeleton, &trace, starttransformed, endtransformed, SUPERCONTENTS_SOLID);
82
83                 if (maxrealfrac > trace.realfraction)
84                 {
85                         if (hitent)
86                                 *hitent = n;
87                         maxfrac = trace.fraction;
88                         maxrealfrac = trace.realfraction;
89                         if (normal)
90                         {
91                                 VectorCopy(trace.plane.normal, tempnormal);
92                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
93                         }
94                 }
95         }
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__);
99         if (impact)
100                 VectorLerp(start, maxfrac, end, impact);
101         return maxfrac;
102 }
103
104 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
105 {
106         // FIXME: check multiple brush models
107         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
108                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
109 }
110
111 dp_model_t *CL_GetModelByIndex(int modelindex)
112 {
113         if(!modelindex)
114                 return NULL;
115         if (modelindex < 0)
116         {
117                 modelindex = -(modelindex+1);
118                 if (modelindex < MAX_MODELS)
119                         return cl.csqc_model_precache[modelindex];
120         }
121         else
122         {
123                 if(modelindex < MAX_MODELS)
124                         return cl.model_precache[modelindex];
125         }
126         return NULL;
127 }
128
129 dp_model_t *CL_GetModelFromEdict(prvm_edict_t *ed)
130 {
131         if (!ed || ed->priv.server->free)
132                 return NULL;
133         return CL_GetModelByIndex((int)ed->fields.client->modelindex);
134 }
135
136 void CL_LinkEdict(prvm_edict_t *ent)
137 {
138         vec3_t mins, maxs;
139
140         if (ent == prog->edicts)
141                 return;         // don't add the world
142
143         if (ent->priv.server->free)
144                 return;
145
146         // set the abs box
147
148         if (ent->fields.client->solid == SOLID_BSP)
149         {
150                 dp_model_t *model = CL_GetModelByIndex( (int)ent->fields.client->modelindex );
151                 if (model == NULL)
152                 {
153                         Con_Printf("edict %i: SOLID_BSP with invalid modelindex!\n", PRVM_NUM_FOR_EDICT(ent));
154
155                         model = CL_GetModelByIndex( 0 );
156                 }
157
158                 if( model != NULL )
159                 {
160                         if (!model->TraceBox)
161                                 Con_DPrintf("edict %i: SOLID_BSP with non-collidable model\n", PRVM_NUM_FOR_EDICT(ent));
162
163                         if (ent->fields.client->angles[0] || ent->fields.client->angles[2] || ent->fields.client->avelocity[0] || ent->fields.client->avelocity[2])
164                         {
165                                 VectorAdd(ent->fields.client->origin, model->rotatedmins, mins);
166                                 VectorAdd(ent->fields.client->origin, model->rotatedmaxs, maxs);
167                         }
168                         else if (ent->fields.client->angles[1] || ent->fields.client->avelocity[1])
169                         {
170                                 VectorAdd(ent->fields.client->origin, model->yawmins, mins);
171                                 VectorAdd(ent->fields.client->origin, model->yawmaxs, maxs);
172                         }
173                         else
174                         {
175                                 VectorAdd(ent->fields.client->origin, model->normalmins, mins);
176                                 VectorAdd(ent->fields.client->origin, model->normalmaxs, maxs);
177                         }
178                 }
179                 else
180                 {
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);
184                 }
185         }
186         else
187         {
188                 VectorAdd(ent->fields.client->origin, ent->fields.client->mins, mins);
189                 VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, maxs);
190         }
191
192         VectorCopy(mins, ent->fields.client->absmin);
193         VectorCopy(maxs, ent->fields.client->absmax);
194
195         World_LinkEdict(&cl.world, ent, ent->fields.client->absmin, ent->fields.client->absmax);
196 }
197
198 int CL_GenericHitSuperContentsMask(const prvm_edict_t *passedict)
199 {
200         if (passedict)
201         {
202                 int dphitcontentsmask = (int)PRVM_clientedictfloat(passedict, dphitcontentsmask);
203                 if (dphitcontentsmask)
204                         return dphitcontentsmask;
205                 else if (passedict->fields.client->solid == SOLID_SLIDEBOX)
206                 {
207                         if ((int)passedict->fields.client->flags & FL_MONSTER)
208                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
209                         else
210                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
211                 }
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;
216                 else
217                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
218         }
219         else
220                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
221 }
222
223 /*
224 ==================
225 CL_Move
226 ==================
227 */
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)
229 {
230         int i, bodysupercontents;
231         int passedictprog;
232         prvm_edict_t *traceowner, *touch;
233         trace_t trace;
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
239         vec3_t clipstart;
240         // trace results
241         trace_t cliptrace;
242         // matrices to transform into/out of other entity's space
243         matrix4x4_t matrix, imatrix;
244         // model of other entity
245         dp_model_t *model;
246         // list of entities to test for collisions
247         int numtouchedicts;
248         static prvm_edict_t *touchedicts[MAX_EDICTS];
249
250         if (hitnetworkentity)
251                 *hitnetworkentity = 0;
252
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]);
258 #endif
259
260         // clip to world
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)
266                 goto finished;
267
268         if (type == MOVE_MISSILE)
269         {
270                 // LordHavoc: modified this, was = -15, now -= 15
271                 for (i = 0;i < 3;i++)
272                 {
273                         clipmins2[i] -= 15;
274                         clipmaxs2[i] += 15;
275                 }
276         }
277
278         // create the bounding box of the entire move
279         for (i = 0;i < 3;i++)
280         {
281                 clipboxmins[i] = clipstart[i] - 1;
282                 clipboxmaxs[i] = clipstart[i] + 1;
283         }
284
285         // debug override to test against everything
286         if (sv_debugmove.integer)
287         {
288                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
289                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
290         }
291
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
294         // VM context
295         if (prog == NULL || passedict == prog->edicts)
296                 passedict = NULL;
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;
301
302         // collide against network entities
303         if (hitnetworkbrushmodels)
304         {
305                 for (i = 0;i < cl.num_brushmodel_entities;i++)
306                 {
307                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
308                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
309                                 continue;
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);
314                 }
315         }
316
317         // collide against player entities
318         if (hitnetworkplayers)
319         {
320                 vec3_t origin, entmins, entmaxs;
321                 matrix4x4_t entmatrix, entinversematrix;
322
323                 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
324                 {
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;
328                 }
329
330                 for (i = 1;i <= cl.maxclients;i++)
331                 {
332                         entity_render_t *ent = &cl.entities[i].render;
333
334                         // don't hit ourselves
335                         if (i == cl.playerentity)
336                                 continue;
337
338                         // don't hit players that don't exist
339                         if (!cl.scores[i-1].name[0])
340                                 continue;
341
342                         if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
343                         {
344                                 // don't hit spectators or nonsolid players
345                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
346                                         continue;
347                         }
348
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))
353                                 continue;
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);
360                 }
361
362 skipnetworkplayers:
363                 ;
364         }
365
366         // clip to entities
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
370         numtouchedicts = 0;
371         if (hitcsqcentities && prog != NULL)
372         {
373                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
374                 if (numtouchedicts > MAX_EDICTS)
375                 {
376                         // this never happens
377                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
378                         numtouchedicts = MAX_EDICTS;
379                 }
380         }
381         for (i = 0;i < numtouchedicts;i++)
382         {
383                 touch = touchedicts[i];
384
385                 if (touch->fields.client->solid < SOLID_BBOX)
386                         continue;
387                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
388                         continue;
389
390                 if (passedict)
391                 {
392                         // don't clip against self
393                         if (passedict == touch)
394                                 continue;
395                         // don't clip owned entities against owner
396                         if (traceowner == touch)
397                                 continue;
398                         // don't clip owner against owned entities
399                         if (passedictprog == touch->fields.client->owner)
400                                 continue;
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)))
403                                 continue;
404                 }
405
406                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
407
408                 // might interact, so do an exact clip
409                 model = NULL;
410                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
411                         model = CL_GetModelFromEdict(touch);
412                 if (model)
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);
414                 else
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);
419                 else
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);
421
422                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
423                         *hitnetworkentity = 0;
424                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
425         }
426
427 finished:
428         return cliptrace;
429 }
430
431 /*
432 ==================
433 CL_TraceLine
434 ==================
435 */
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)
438 #else
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)
440 #endif
441 {
442         int i, bodysupercontents;
443         int passedictprog;
444         prvm_edict_t *traceowner, *touch;
445         trace_t trace;
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;
452         // trace results
453         trace_t cliptrace;
454         // matrices to transform into/out of other entity's space
455         matrix4x4_t matrix, imatrix;
456         // model of other entity
457         dp_model_t *model;
458         // list of entities to test for collisions
459         int numtouchedicts;
460         static prvm_edict_t *touchedicts[MAX_EDICTS];
461 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
462         vec3_t end;
463         vec_t len = 0;
464
465         if (VectorCompare(start, pEnd))
466                 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
467
468         if(collision_endposnudge.value > 0)
469         {
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);
474         }
475         else
476                 VectorCopy(pEnd, end);
477 #else
478         if (VectorCompare(start, end))
479                 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
480 #endif
481
482         if (hitnetworkentity)
483                 *hitnetworkentity = 0;
484
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]);
491 #endif
492
493         // clip to world
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)
499                 goto finished;
500
501         if (type == MOVE_MISSILE)
502         {
503                 // LordHavoc: modified this, was = -15, now -= 15
504                 for (i = 0;i < 3;i++)
505                 {
506                         clipmins2[i] -= 15;
507                         clipmaxs2[i] += 15;
508                 }
509         }
510
511         // create the bounding box of the entire move
512         for (i = 0;i < 3;i++)
513         {
514                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + clipmins2[i] - 1;
515                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + clipmaxs2[i] + 1;
516         }
517
518         // debug override to test against everything
519         if (sv_debugmove.integer)
520         {
521                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
522                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
523         }
524
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
527         // VM context
528         if (prog == NULL || passedict == prog->edicts)
529                 passedict = NULL;
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;
534
535         // collide against network entities
536         if (hitnetworkbrushmodels)
537         {
538                 for (i = 0;i < cl.num_brushmodel_entities;i++)
539                 {
540                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
541                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
542                                 continue;
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);
547                 }
548         }
549
550         // collide against player entities
551         if (hitnetworkplayers)
552         {
553                 vec3_t origin, entmins, entmaxs;
554                 matrix4x4_t entmatrix, entinversematrix;
555
556                 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
557                 {
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;
561                 }
562
563                 for (i = 1;i <= cl.maxclients;i++)
564                 {
565                         entity_render_t *ent = &cl.entities[i].render;
566
567                         // don't hit ourselves
568                         if (i == cl.playerentity)
569                                 continue;
570
571                         // don't hit players that don't exist
572                         if (!cl.scores[i-1].name[0])
573                                 continue;
574
575                         if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
576                         {
577                                 // don't hit spectators or nonsolid players
578                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
579                                         continue;
580                         }
581
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))
586                                 continue;
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);
593                 }
594
595 skipnetworkplayers:
596                 ;
597         }
598
599         // clip to entities
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
603         numtouchedicts = 0;
604         if (hitcsqcentities && prog != NULL)
605         {
606                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
607                 if (numtouchedicts > MAX_EDICTS)
608                 {
609                         // this never happens
610                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
611                         numtouchedicts = MAX_EDICTS;
612                 }
613         }
614         for (i = 0;i < numtouchedicts;i++)
615         {
616                 touch = touchedicts[i];
617
618                 if (touch->fields.client->solid < SOLID_BBOX)
619                         continue;
620                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
621                         continue;
622
623                 if (passedict)
624                 {
625                         // don't clip against self
626                         if (passedict == touch)
627                                 continue;
628                         // don't clip owned entities against owner
629                         if (traceowner == touch)
630                                 continue;
631                         // don't clip owner against owned entities
632                         if (passedictprog == touch->fields.client->owner)
633                                 continue;
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)))
636                                 continue;
637                 }
638
639                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
640
641                 // might interact, so do an exact clip
642                 model = NULL;
643                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
644                         model = CL_GetModelFromEdict(touch);
645                 if (model)
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);
647                 else
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);
652                 else
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);
654
655                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
656                         *hitnetworkentity = 0;
657                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
658         }
659
660 finished:
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);
664 #endif
665         return cliptrace;
666 }
667
668 /*
669 ==================
670 CL_Move
671 ==================
672 */
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)
675 #else
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)
677 #endif
678 {
679         vec3_t hullmins, hullmaxs;
680         int i, bodysupercontents;
681         int passedictprog;
682         qboolean pointtrace;
683         prvm_edict_t *traceowner, *touch;
684         trace_t trace;
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;
693         // trace results
694         trace_t cliptrace;
695         // matrices to transform into/out of other entity's space
696         matrix4x4_t matrix, imatrix;
697         // model of other entity
698         dp_model_t *model;
699         // list of entities to test for collisions
700         int numtouchedicts;
701         static prvm_edict_t *touchedicts[MAX_EDICTS];
702 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
703         vec3_t end;
704         vec_t len = 0;
705
706         if (VectorCompare(mins, maxs))
707         {
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);
713                 else
714                         trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities, false);
715                 VectorSubtract(trace.endpos, mins, trace.endpos);
716                 return trace;
717         }
718
719         if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
720         {
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);
725         }
726         else
727                 VectorCopy(pEnd, end);
728 #else
729         if (VectorCompare(mins, maxs))
730         {
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);
736                 else
737                         trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities, false);
738                 VectorSubtract(trace.endpos, mins, trace.endpos);
739                 return trace;
740         }
741 #endif
742
743         if (hitnetworkentity)
744                 *hitnetworkentity = 0;
745
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]);
754 #endif
755
756         // clip to world
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)
762                 goto finished;
763
764         if (type == MOVE_MISSILE)
765         {
766                 // LordHavoc: modified this, was = -15, now -= 15
767                 for (i = 0;i < 3;i++)
768                 {
769                         clipmins2[i] -= 15;
770                         clipmaxs2[i] += 15;
771                 }
772         }
773
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);
777         else
778         {
779                 VectorCopy(clipmins, hullmins);
780                 VectorCopy(clipmaxs, hullmaxs);
781         }
782
783         // create the bounding box of the entire move
784         for (i = 0;i < 3;i++)
785         {
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;
788         }
789
790         // debug override to test against everything
791         if (sv_debugmove.integer)
792         {
793                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
794                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
795         }
796
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
799         // VM context
800         if (prog == NULL || passedict == prog->edicts)
801                 passedict = NULL;
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;
808
809         // collide against network entities
810         if (hitnetworkbrushmodels)
811         {
812                 for (i = 0;i < cl.num_brushmodel_entities;i++)
813                 {
814                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
815                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
816                                 continue;
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);
821                 }
822         }
823
824         // collide against player entities
825         if (hitnetworkplayers)
826         {
827                 vec3_t origin, entmins, entmaxs;
828                 matrix4x4_t entmatrix, entinversematrix;
829
830                 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
831                 {
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;
835                 }
836
837                 for (i = 1;i <= cl.maxclients;i++)
838                 {
839                         entity_render_t *ent = &cl.entities[i].render;
840
841                         // don't hit ourselves
842                         if (i == cl.playerentity)
843                                 continue;
844
845                         // don't hit players that don't exist
846                         if (!cl.scores[i-1].name[0])
847                                 continue;
848
849                         if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
850                         {
851                                 // don't hit spectators or nonsolid players
852                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
853                                         continue;
854                         }
855
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))
860                                 continue;
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);
867                 }
868
869 skipnetworkplayers:
870                 ;
871         }
872
873         // clip to entities
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
877         numtouchedicts = 0;
878         if (hitcsqcentities && prog != NULL)
879         {
880                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
881                 if (numtouchedicts > MAX_EDICTS)
882                 {
883                         // this never happens
884                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
885                         numtouchedicts = MAX_EDICTS;
886                 }
887         }
888         for (i = 0;i < numtouchedicts;i++)
889         {
890                 touch = touchedicts[i];
891
892                 if (touch->fields.client->solid < SOLID_BBOX)
893                         continue;
894                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
895                         continue;
896
897                 if (passedict)
898                 {
899                         // don't clip against self
900                         if (passedict == touch)
901                                 continue;
902                         // don't clip owned entities against owner
903                         if (traceowner == touch)
904                                 continue;
905                         // don't clip owner against owned entities
906                         if (passedictprog == touch->fields.client->owner)
907                                 continue;
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)))
910                                 continue;
911                 }
912
913                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
914
915                 // might interact, so do an exact clip
916                 model = NULL;
917                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
918                         model = CL_GetModelFromEdict(touch);
919                 if (model)
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);
921                 else
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);
926                 else
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);
928
929                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
930                         *hitnetworkentity = 0;
931                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
932         }
933
934 finished:
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);
938 #endif
939         return cliptrace;
940 }