]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
Added dp_waterscroll <scale> <speed> shader keyword as simple simulation of water...
[divverent/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         prvm_eval_t *val;
201         if (passedict)
202         {
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)
207                 {
208                         if ((int)passedict->fields.client->flags & FL_MONSTER)
209                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
210                         else
211                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
212                 }
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;
217                 else
218                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
219         }
220         else
221                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
222 }
223
224 /*
225 ==================
226 CL_Move
227 ==================
228 */
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)
230 {
231         int i, bodysupercontents;
232         int passedictprog;
233         prvm_edict_t *traceowner, *touch;
234         trace_t trace;
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
240         vec3_t clipstart;
241         // trace results
242         trace_t cliptrace;
243         // matrices to transform into/out of other entity's space
244         matrix4x4_t matrix, imatrix;
245         // model of other entity
246         dp_model_t *model;
247         // list of entities to test for collisions
248         int numtouchedicts;
249         static prvm_edict_t *touchedicts[MAX_EDICTS];
250
251         if (hitnetworkentity)
252                 *hitnetworkentity = 0;
253
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]);
259 #endif
260
261         // clip to world
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)
267                 goto finished;
268
269         if (type == MOVE_MISSILE)
270         {
271                 // LordHavoc: modified this, was = -15, now -= 15
272                 for (i = 0;i < 3;i++)
273                 {
274                         clipmins2[i] -= 15;
275                         clipmaxs2[i] += 15;
276                 }
277         }
278
279         // create the bounding box of the entire move
280         for (i = 0;i < 3;i++)
281         {
282                 clipboxmins[i] = clipstart[i] - 1;
283                 clipboxmaxs[i] = clipstart[i] + 1;
284         }
285
286         // debug override to test against everything
287         if (sv_debugmove.integer)
288         {
289                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
290                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
291         }
292
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
295         // VM context
296         if (prog == NULL || passedict == prog->edicts)
297                 passedict = NULL;
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;
302
303         // collide against network entities
304         if (hitnetworkbrushmodels)
305         {
306                 for (i = 0;i < cl.num_brushmodel_entities;i++)
307                 {
308                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
309                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
310                                 continue;
311                         Collision_ClipPointToGenericEntity(&trace, ent->model, ent->frameblend, ent->skeleton, 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);
315                 }
316         }
317
318         // collide against player entities
319         if (hitnetworkplayers)
320         {
321                 vec3_t origin, entmins, entmaxs;
322                 matrix4x4_t entmatrix, entinversematrix;
323
324                 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
325                 {
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;
329                 }
330
331                 for (i = 1;i <= cl.maxclients;i++)
332                 {
333                         entity_render_t *ent = &cl.entities[i].render;
334
335                         // don't hit ourselves
336                         if (i == cl.playerentity)
337                                 continue;
338
339                         // don't hit players that don't exist
340                         if (!cl.scores[i-1].name[0])
341                                 continue;
342
343                         if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
344                         {
345                                 // don't hit spectators or nonsolid players
346                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
347                                         continue;
348                         }
349
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))
354                                 continue;
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, NULL, NULL, 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);
361                 }
362
363 skipnetworkplayers:
364                 ;
365         }
366
367         // clip to entities
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
371         numtouchedicts = 0;
372         if (hitcsqcentities && prog != NULL)
373         {
374                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
375                 if (numtouchedicts > MAX_EDICTS)
376                 {
377                         // this never happens
378                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
379                         numtouchedicts = MAX_EDICTS;
380                 }
381         }
382         for (i = 0;i < numtouchedicts;i++)
383         {
384                 touch = touchedicts[i];
385
386                 if (touch->fields.client->solid < SOLID_BBOX)
387                         continue;
388                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
389                         continue;
390
391                 if (passedict)
392                 {
393                         // don't clip against self
394                         if (passedict == touch)
395                                 continue;
396                         // don't clip owned entities against owner
397                         if (traceowner == touch)
398                                 continue;
399                         // don't clip owner against owned entities
400                         if (passedictprog == touch->fields.client->owner)
401                                 continue;
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)))
404                                 continue;
405                 }
406
407                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
408
409                 // might interact, so do an exact clip
410                 model = NULL;
411                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
412                         model = CL_GetModelFromEdict(touch);
413                 if (model)
414                         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                 else
416                         Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
417                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
418                 if ((int)touch->fields.client->flags & FL_MONSTER)
419                         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                 else
421                         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
423                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
424                         *hitnetworkentity = 0;
425                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
426         }
427
428 finished:
429         return cliptrace;
430 }
431
432 /*
433 ==================
434 CL_TraceLine
435 ==================
436 */
437 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
438 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)
439 #else
440 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)
441 #endif
442 {
443         int i, bodysupercontents;
444         int passedictprog;
445         prvm_edict_t *traceowner, *touch;
446         trace_t trace;
447         // bounding box of entire move area
448         vec3_t clipboxmins, clipboxmaxs;
449         // size when clipping against monsters
450         vec3_t clipmins2, clipmaxs2;
451         // start and end origin of move
452         vec3_t clipstart, clipend;
453         // trace results
454         trace_t cliptrace;
455         // matrices to transform into/out of other entity's space
456         matrix4x4_t matrix, imatrix;
457         // model of other entity
458         dp_model_t *model;
459         // list of entities to test for collisions
460         int numtouchedicts;
461         static prvm_edict_t *touchedicts[MAX_EDICTS];
462 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
463         vec3_t end;
464         vec_t len = 0;
465
466         if (VectorCompare(start, pEnd))
467                 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
468
469         if(collision_endposnudge.value > 0)
470         {
471                 // TRICK: make the trace 1 qu longer!
472                 VectorSubtract(pEnd, start, end);
473                 len = VectorNormalizeLength(end);
474                 VectorMA(pEnd, collision_endposnudge.value, end, end);
475         }
476         else
477                 VectorCopy(pEnd, end);
478 #else
479         if (VectorCompare(start, end))
480                 return CL_TracePoint(start, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
481 #endif
482
483         if (hitnetworkentity)
484                 *hitnetworkentity = 0;
485
486         VectorCopy(start, clipstart);
487         VectorCopy(end, clipend);
488         VectorClear(clipmins2);
489         VectorClear(clipmaxs2);
490 #if COLLISIONPARANOID >= 3
491         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
492 #endif
493
494         // clip to world
495         Collision_ClipLineToWorld(&cliptrace, cl.worldmodel, clipstart, clipend, hitsupercontentsmask);
496         cliptrace.bmodelstartsolid = cliptrace.startsolid;
497         if (cliptrace.startsolid || cliptrace.fraction < 1)
498                 cliptrace.ent = prog ? prog->edicts : NULL;
499         if (type == MOVE_WORLDONLY)
500                 goto finished;
501
502         if (type == MOVE_MISSILE)
503         {
504                 // LordHavoc: modified this, was = -15, now -= 15
505                 for (i = 0;i < 3;i++)
506                 {
507                         clipmins2[i] -= 15;
508                         clipmaxs2[i] += 15;
509                 }
510         }
511
512         // create the bounding box of the entire move
513         for (i = 0;i < 3;i++)
514         {
515                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + clipmins2[i] - 1;
516                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + clipmaxs2[i] + 1;
517         }
518
519         // debug override to test against everything
520         if (sv_debugmove.integer)
521         {
522                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
523                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
524         }
525
526         // if the passedict is world, make it NULL (to avoid two checks each time)
527         // this checks prog because this function is often called without a CSQC
528         // VM context
529         if (prog == NULL || passedict == prog->edicts)
530                 passedict = NULL;
531         // precalculate prog value for passedict for comparisons
532         passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
533         // precalculate passedict's owner edict pointer for comparisons
534         traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
535
536         // collide against network entities
537         if (hitnetworkbrushmodels)
538         {
539                 for (i = 0;i < cl.num_brushmodel_entities;i++)
540                 {
541                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
542                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
543                                 continue;
544                         Collision_ClipLineToGenericEntity(&trace, ent->model, ent->frameblend, ent->skeleton, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, end, hitsupercontentsmask);
545                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
546                                 *hitnetworkentity = cl.brushmodel_entities[i];
547                         Collision_CombineTraces(&cliptrace, &trace, NULL, true);
548                 }
549         }
550
551         // collide against player entities
552         if (hitnetworkplayers)
553         {
554                 vec3_t origin, entmins, entmaxs;
555                 matrix4x4_t entmatrix, entinversematrix;
556
557                 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
558                 {
559                         // don't hit network players, if we are a nonsolid player
560                         if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
561                                 goto skipnetworkplayers;
562                 }
563
564                 for (i = 1;i <= cl.maxclients;i++)
565                 {
566                         entity_render_t *ent = &cl.entities[i].render;
567
568                         // don't hit ourselves
569                         if (i == cl.playerentity)
570                                 continue;
571
572                         // don't hit players that don't exist
573                         if (!cl.scores[i-1].name[0])
574                                 continue;
575
576                         if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
577                         {
578                                 // don't hit spectators or nonsolid players
579                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
580                                         continue;
581                         }
582
583                         Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
584                         VectorAdd(origin, cl.playerstandmins, entmins);
585                         VectorAdd(origin, cl.playerstandmaxs, entmaxs);
586                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
587                                 continue;
588                         Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
589                         Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
590                         Collision_ClipLineToGenericEntity(&trace, NULL, NULL, NULL, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, end, hitsupercontentsmask);
591                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
592                                 *hitnetworkentity = i;
593                         Collision_CombineTraces(&cliptrace, &trace, NULL, false);
594                 }
595
596 skipnetworkplayers:
597                 ;
598         }
599
600         // clip to entities
601         // because this uses World_EntitiestoBox, we know all entity boxes overlap
602         // the clip region, so we can skip culling checks in the loop below
603         // note: if prog is NULL then there won't be any linked entities
604         numtouchedicts = 0;
605         if (hitcsqcentities && prog != NULL)
606         {
607                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
608                 if (numtouchedicts > MAX_EDICTS)
609                 {
610                         // this never happens
611                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
612                         numtouchedicts = MAX_EDICTS;
613                 }
614         }
615         for (i = 0;i < numtouchedicts;i++)
616         {
617                 touch = touchedicts[i];
618
619                 if (touch->fields.client->solid < SOLID_BBOX)
620                         continue;
621                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
622                         continue;
623
624                 if (passedict)
625                 {
626                         // don't clip against self
627                         if (passedict == touch)
628                                 continue;
629                         // don't clip owned entities against owner
630                         if (traceowner == touch)
631                                 continue;
632                         // don't clip owner against owned entities
633                         if (passedictprog == touch->fields.client->owner)
634                                 continue;
635                         // don't clip points against points (they can't collide)
636                         if (VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
637                                 continue;
638                 }
639
640                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
641
642                 // might interact, so do an exact clip
643                 model = NULL;
644                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
645                         model = CL_GetModelFromEdict(touch);
646                 if (model)
647                         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                 else
649                         Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
650                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
651                 if (type == MOVE_MISSILE && (int)touch->fields.client->flags & FL_MONSTER)
652                         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                 else
654                         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);
655
656                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
657                         *hitnetworkentity = 0;
658                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
659         }
660
661 finished:
662 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
663         if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
664                 Collision_ShortenTrace(&cliptrace, len / (len + collision_endposnudge.value), pEnd);
665 #endif
666         return cliptrace;
667 }
668
669 /*
670 ==================
671 CL_Move
672 ==================
673 */
674 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
675 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 #else
677 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)
678 #endif
679 {
680         vec3_t hullmins, hullmaxs;
681         int i, bodysupercontents;
682         int passedictprog;
683         qboolean pointtrace;
684         prvm_edict_t *traceowner, *touch;
685         trace_t trace;
686         // bounding box of entire move area
687         vec3_t clipboxmins, clipboxmaxs;
688         // size of the moving object
689         vec3_t clipmins, clipmaxs;
690         // size when clipping against monsters
691         vec3_t clipmins2, clipmaxs2;
692         // start and end origin of move
693         vec3_t clipstart, clipend;
694         // trace results
695         trace_t cliptrace;
696         // matrices to transform into/out of other entity's space
697         matrix4x4_t matrix, imatrix;
698         // model of other entity
699         dp_model_t *model;
700         // list of entities to test for collisions
701         int numtouchedicts;
702         static prvm_edict_t *touchedicts[MAX_EDICTS];
703 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
704         vec3_t end;
705         vec_t len = 0;
706
707         if (VectorCompare(mins, maxs))
708         {
709                 vec3_t shiftstart, shiftend;
710                 VectorAdd(start, mins, shiftstart);
711                 VectorAdd(pEnd, mins, shiftend);
712                 if (VectorCompare(start, pEnd))
713                         trace = CL_TracePoint(shiftstart, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
714                 else
715                         trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
716                 VectorSubtract(trace.endpos, mins, trace.endpos);
717                 return trace;
718         }
719
720         if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
721         {
722                 // TRICK: make the trace 1 qu longer!
723                 VectorSubtract(pEnd, start, end);
724                 len = VectorNormalizeLength(end);
725                 VectorMA(pEnd, collision_endposnudge.value, end, end);
726         }
727         else
728                 VectorCopy(pEnd, end);
729 #else
730         if (VectorCompare(mins, maxs))
731         {
732                 vec3_t shiftstart, shiftend;
733                 VectorAdd(start, mins, shiftstart);
734                 VectorAdd(end, mins, shiftend);
735                 if (VectorCompare(start, end))
736                         trace = CL_TracePoint(shiftstart, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
737                 else
738                         trace = CL_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, hitnetworkbrushmodels, hitnetworkplayers, hitnetworkentity, hitcsqcentities);
739                 VectorSubtract(trace.endpos, mins, trace.endpos);
740                 return trace;
741         }
742 #endif
743
744         if (hitnetworkentity)
745                 *hitnetworkentity = 0;
746
747         VectorCopy(start, clipstart);
748         VectorCopy(end, clipend);
749         VectorCopy(mins, clipmins);
750         VectorCopy(maxs, clipmaxs);
751         VectorCopy(mins, clipmins2);
752         VectorCopy(maxs, clipmaxs2);
753 #if COLLISIONPARANOID >= 3
754         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
755 #endif
756
757         // clip to world
758         Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
759         cliptrace.bmodelstartsolid = cliptrace.startsolid;
760         if (cliptrace.startsolid || cliptrace.fraction < 1)
761                 cliptrace.ent = prog ? prog->edicts : NULL;
762         if (type == MOVE_WORLDONLY)
763                 goto finished;
764
765         if (type == MOVE_MISSILE)
766         {
767                 // LordHavoc: modified this, was = -15, now -= 15
768                 for (i = 0;i < 3;i++)
769                 {
770                         clipmins2[i] -= 15;
771                         clipmaxs2[i] += 15;
772                 }
773         }
774
775         // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
776         if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
777                 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
778         else
779         {
780                 VectorCopy(clipmins, hullmins);
781                 VectorCopy(clipmaxs, hullmaxs);
782         }
783
784         // create the bounding box of the entire move
785         for (i = 0;i < 3;i++)
786         {
787                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
788                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
789         }
790
791         // debug override to test against everything
792         if (sv_debugmove.integer)
793         {
794                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
795                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
796         }
797
798         // if the passedict is world, make it NULL (to avoid two checks each time)
799         // this checks prog because this function is often called without a CSQC
800         // VM context
801         if (prog == NULL || passedict == prog->edicts)
802                 passedict = NULL;
803         // precalculate prog value for passedict for comparisons
804         passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
805         // figure out whether this is a point trace for comparisons
806         pointtrace = VectorCompare(clipmins, clipmaxs);
807         // precalculate passedict's owner edict pointer for comparisons
808         traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
809
810         // collide against network entities
811         if (hitnetworkbrushmodels)
812         {
813                 for (i = 0;i < cl.num_brushmodel_entities;i++)
814                 {
815                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
816                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
817                                 continue;
818                         Collision_ClipToGenericEntity(&trace, ent->model, ent->frameblend, ent->skeleton, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
819                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
820                                 *hitnetworkentity = cl.brushmodel_entities[i];
821                         Collision_CombineTraces(&cliptrace, &trace, NULL, true);
822                 }
823         }
824
825         // collide against player entities
826         if (hitnetworkplayers)
827         {
828                 vec3_t origin, entmins, entmaxs;
829                 matrix4x4_t entmatrix, entinversematrix;
830
831                 if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
832                 {
833                         // don't hit network players, if we are a nonsolid player
834                         if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
835                                 goto skipnetworkplayers;
836                 }
837
838                 for (i = 1;i <= cl.maxclients;i++)
839                 {
840                         entity_render_t *ent = &cl.entities[i].render;
841
842                         // don't hit ourselves
843                         if (i == cl.playerentity)
844                                 continue;
845
846                         // don't hit players that don't exist
847                         if (!cl.scores[i-1].name[0])
848                                 continue;
849
850                         if(gamemode == GAME_NEXUIZ || gamemode == GAME_XONOTIC)
851                         {
852                                 // don't hit spectators or nonsolid players
853                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
854                                         continue;
855                         }
856
857                         Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
858                         VectorAdd(origin, cl.playerstandmins, entmins);
859                         VectorAdd(origin, cl.playerstandmaxs, entmaxs);
860                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
861                                 continue;
862                         Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
863                         Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
864                         Collision_ClipToGenericEntity(&trace, NULL, NULL, NULL, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
865                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
866                                 *hitnetworkentity = i;
867                         Collision_CombineTraces(&cliptrace, &trace, NULL, false);
868                 }
869
870 skipnetworkplayers:
871                 ;
872         }
873
874         // clip to entities
875         // because this uses World_EntitiestoBox, we know all entity boxes overlap
876         // the clip region, so we can skip culling checks in the loop below
877         // note: if prog is NULL then there won't be any linked entities
878         numtouchedicts = 0;
879         if (hitcsqcentities && prog != NULL)
880         {
881                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
882                 if (numtouchedicts > MAX_EDICTS)
883                 {
884                         // this never happens
885                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
886                         numtouchedicts = MAX_EDICTS;
887                 }
888         }
889         for (i = 0;i < numtouchedicts;i++)
890         {
891                 touch = touchedicts[i];
892
893                 if (touch->fields.client->solid < SOLID_BBOX)
894                         continue;
895                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
896                         continue;
897
898                 if (passedict)
899                 {
900                         // don't clip against self
901                         if (passedict == touch)
902                                 continue;
903                         // don't clip owned entities against owner
904                         if (traceowner == touch)
905                                 continue;
906                         // don't clip owner against owned entities
907                         if (passedictprog == touch->fields.client->owner)
908                                 continue;
909                         // don't clip points against points (they can't collide)
910                         if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
911                                 continue;
912                 }
913
914                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
915
916                 // might interact, so do an exact clip
917                 model = NULL;
918                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
919                         model = CL_GetModelFromEdict(touch);
920                 if (model)
921                         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                 else
923                         Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
924                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
925                 if ((int)touch->fields.client->flags & FL_MONSTER)
926                         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                 else
928                         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
930                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
931                         *hitnetworkentity = 0;
932                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
933         }
934
935 finished:
936 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
937         if(!VectorCompare(start, pEnd) && collision_endposnudge.value > 0)
938                 Collision_ShortenTrace(&cliptrace, len / (len + collision_endposnudge.value), pEnd);
939 #endif
940         return cliptrace;
941 }