]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
added TEXF_FORCELINEAR to many special effect textures
[divverent/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
6 {
7         float maxfrac, maxrealfrac;
8         int n;
9         entity_render_t *ent;
10         float tracemins[3], tracemaxs[3];
11         trace_t trace;
12         float tempnormal[3], starttransformed[3], endtransformed[3];
13
14         memset (&trace, 0 , sizeof(trace_t));
15         trace.fraction = 1;
16         trace.realfraction = 1;
17         VectorCopy (end, trace.endpos);
18
19         if (hitent)
20                 *hitent = 0;
21         if (cl.worldmodel && cl.worldmodel->TraceBox)
22                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, vec3_origin, vec3_origin, end, SUPERCONTENTS_SOLID);
23
24         if (normal)
25                 VectorCopy(trace.plane.normal, normal);
26         maxfrac = trace.fraction;
27         maxrealfrac = trace.realfraction;
28
29         tracemins[0] = min(start[0], end[0]);
30         tracemaxs[0] = max(start[0], end[0]);
31         tracemins[1] = min(start[1], end[1]);
32         tracemaxs[1] = max(start[1], end[1]);
33         tracemins[2] = min(start[2], end[2]);
34         tracemaxs[2] = max(start[2], end[2]);
35
36         // look for embedded bmodels
37         for (n = 0;n < cl.num_entities;n++)
38         {
39                 if (!cl.entities_active[n])
40                         continue;
41                 ent = &cl.entities[n].render;
42                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
43                         continue;
44                 if (!ent->model || !ent->model->TraceBox)
45                         continue;
46                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
47                         continue;
48                 // if transparent and not selectable, skip entity
49                 if (!(cl.entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
50                         continue;
51                 if (ent == ignoreent)
52                         continue;
53                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
54                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
55                 Collision_ClipTrace_Box(&trace, ent->model->normalmins, ent->model->normalmaxs, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID, SUPERCONTENTS_SOLID, 0, NULL);
56                 if (maxrealfrac < trace.realfraction)
57                         continue;
58
59                 //if (ent->model && ent->model->TraceBox)
60                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID);
61
62                 if (maxrealfrac > trace.realfraction)
63                 {
64                         if (hitent)
65                                 *hitent = n;
66                         maxfrac = trace.fraction;
67                         maxrealfrac = trace.realfraction;
68                         if (normal)
69                         {
70                                 VectorCopy(trace.plane.normal, tempnormal);
71                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
72                         }
73                 }
74         }
75         maxfrac = bound(0, maxfrac, 1);
76         maxrealfrac = bound(0, maxrealfrac, 1);
77         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
78         if (impact)
79                 VectorLerp(start, maxfrac, end, impact);
80         return maxfrac;
81 }
82
83 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
84 {
85         // FIXME: check multiple brush models
86         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
87                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
88 }
89
90 dp_model_t *CL_GetModelByIndex(int modelindex)
91 {
92         if(!modelindex)
93                 return NULL;
94         if (modelindex < 0)
95         {
96                 modelindex = -(modelindex+1);
97                 if (modelindex < MAX_MODELS)
98                         return cl.csqc_model_precache[modelindex];
99         }
100         else
101         {
102                 if(modelindex < MAX_MODELS)
103                         return cl.model_precache[modelindex];
104         }
105         return NULL;
106 }
107
108 dp_model_t *CL_GetModelFromEdict(prvm_edict_t *ed)
109 {
110         if (!ed || ed->priv.server->free)
111                 return NULL;
112         return CL_GetModelByIndex((int)ed->fields.client->modelindex);
113 }
114
115 void CL_LinkEdict(prvm_edict_t *ent)
116 {
117         vec3_t mins, maxs;
118
119         if (ent == prog->edicts)
120                 return;         // don't add the world
121
122         if (ent->priv.server->free)
123                 return;
124
125         // set the abs box
126
127         if (ent->fields.client->solid == SOLID_BSP)
128         {
129                 dp_model_t *model = CL_GetModelByIndex( (int)ent->fields.client->modelindex );
130                 if (model == NULL)
131                 {
132                         Con_Printf("edict %i: SOLID_BSP with invalid modelindex!\n", PRVM_NUM_FOR_EDICT(ent));
133
134                         model = CL_GetModelByIndex( 0 );
135                 }
136
137                 if( model != NULL )
138                 {
139                         if (!model->TraceBox && developer.integer >= 1)
140                                 Con_Printf("edict %i: SOLID_BSP with non-collidable model\n", PRVM_NUM_FOR_EDICT(ent));
141
142                         if (ent->fields.client->angles[0] || ent->fields.client->angles[2] || ent->fields.client->avelocity[0] || ent->fields.client->avelocity[2])
143                         {
144                                 VectorAdd(ent->fields.client->origin, model->rotatedmins, mins);
145                                 VectorAdd(ent->fields.client->origin, model->rotatedmaxs, maxs);
146                         }
147                         else if (ent->fields.client->angles[1] || ent->fields.client->avelocity[1])
148                         {
149                                 VectorAdd(ent->fields.client->origin, model->yawmins, mins);
150                                 VectorAdd(ent->fields.client->origin, model->yawmaxs, maxs);
151                         }
152                         else
153                         {
154                                 VectorAdd(ent->fields.client->origin, model->normalmins, mins);
155                                 VectorAdd(ent->fields.client->origin, model->normalmaxs, maxs);
156                         }
157                 }
158                 else
159                 {
160                         // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
161                         VectorAdd(ent->fields.client->origin, ent->fields.client->mins, mins);
162                         VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, maxs);
163                 }
164         }
165         else
166         {
167                 VectorAdd(ent->fields.client->origin, ent->fields.client->mins, mins);
168                 VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, maxs);
169         }
170
171         VectorCopy(mins, ent->fields.client->absmin);
172         VectorCopy(maxs, ent->fields.client->absmax);
173
174         World_LinkEdict(&cl.world, ent, ent->fields.client->absmin, ent->fields.client->absmax);
175 }
176
177 int CL_GenericHitSuperContentsMask(const prvm_edict_t *passedict)
178 {
179         prvm_eval_t *val;
180         if (passedict)
181         {
182                 val = PRVM_EDICTFIELDVALUE(passedict, prog->fieldoffsets.dphitcontentsmask);
183                 if (val && val->_float)
184                         return (int)val->_float;
185                 else if (passedict->fields.client->solid == SOLID_SLIDEBOX)
186                 {
187                         if ((int)passedict->fields.client->flags & FL_MONSTER)
188                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_MONSTERCLIP;
189                         else
190                                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_PLAYERCLIP;
191                 }
192                 else if (passedict->fields.client->solid == SOLID_CORPSE)
193                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
194                 else if (passedict->fields.client->solid == SOLID_TRIGGER)
195                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;
196                 else
197                         return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
198         }
199         else
200                 return SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_CORPSE;
201 }
202
203 /*
204 ==================
205 CL_Move
206 ==================
207 */
208 trace_t CL_Move(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, qboolean hitnetworkbrushmodels, qboolean hitnetworkplayers, int *hitnetworkentity, qboolean hitcsqcentities)
209 {
210         vec3_t hullmins, hullmaxs;
211         int i, bodysupercontents;
212         int passedictprog;
213         qboolean pointtrace;
214         prvm_edict_t *traceowner, *touch;
215         trace_t trace;
216         // bounding box of entire move area
217         vec3_t clipboxmins, clipboxmaxs;
218         // size of the moving object
219         vec3_t clipmins, clipmaxs;
220         // size when clipping against monsters
221         vec3_t clipmins2, clipmaxs2;
222         // start and end origin of move
223         vec3_t clipstart, clipend;
224         // trace results
225         trace_t cliptrace;
226         // matrices to transform into/out of other entity's space
227         matrix4x4_t matrix, imatrix;
228         // model of other entity
229         dp_model_t *model;
230         // list of entities to test for collisions
231         int numtouchedicts;
232         prvm_edict_t *touchedicts[MAX_EDICTS];
233
234         if (hitnetworkentity)
235                 *hitnetworkentity = 0;
236
237         VectorCopy(start, clipstart);
238         VectorCopy(end, clipend);
239         VectorCopy(mins, clipmins);
240         VectorCopy(maxs, clipmaxs);
241         VectorCopy(mins, clipmins2);
242         VectorCopy(maxs, clipmaxs2);
243 #if COLLISIONPARANOID >= 3
244         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
245 #endif
246
247         // clip to world
248         Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
249         cliptrace.bmodelstartsolid = cliptrace.startsolid;
250         if (cliptrace.startsolid || cliptrace.fraction < 1)
251                 cliptrace.ent = prog ? prog->edicts : NULL;
252         if (type == MOVE_WORLDONLY)
253                 return cliptrace;
254
255         if (type == MOVE_MISSILE)
256         {
257                 // LordHavoc: modified this, was = -15, now -= 15
258                 for (i = 0;i < 3;i++)
259                 {
260                         clipmins2[i] -= 15;
261                         clipmaxs2[i] += 15;
262                 }
263         }
264
265         // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
266         if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
267                 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
268         else
269         {
270                 VectorCopy(clipmins, hullmins);
271                 VectorCopy(clipmaxs, hullmaxs);
272         }
273
274         // create the bounding box of the entire move
275         for (i = 0;i < 3;i++)
276         {
277                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
278                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
279         }
280
281         // debug override to test against everything
282         if (sv_debugmove.integer)
283         {
284                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
285                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
286         }
287
288         // if the passedict is world, make it NULL (to avoid two checks each time)
289         // this checks prog because this function is often called without a CSQC
290         // VM context
291         if (prog == NULL || passedict == prog->edicts)
292                 passedict = NULL;
293         // precalculate prog value for passedict for comparisons
294         passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
295         // figure out whether this is a point trace for comparisons
296         pointtrace = VectorCompare(clipmins, clipmaxs);
297         // precalculate passedict's owner edict pointer for comparisons
298         traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
299
300         // collide against network entities
301         if (hitnetworkbrushmodels)
302         {
303                 for (i = 0;i < cl.num_brushmodel_entities;i++)
304                 {
305                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
306                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
307                                 continue;
308                         Collision_ClipToGenericEntity(&trace, ent->model, ent->frame2, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
309                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
310                                 *hitnetworkentity = cl.brushmodel_entities[i];
311                         Collision_CombineTraces(&cliptrace, &trace, NULL, true);
312                 }
313         }
314
315         // collide against player entities
316         if (hitnetworkplayers)
317         {
318                 vec3_t origin, entmins, entmaxs;
319                 matrix4x4_t entmatrix, entinversematrix;
320
321                 if(gamemode == GAME_NEXUIZ)
322                 {
323                         // don't hit network players, if we are a nonsolid player
324                         if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
325                                 goto skipnetworkplayers;
326                 }
327
328                 for (i = 1;i <= cl.maxclients;i++)
329                 {
330                         entity_render_t *ent = &cl.entities[i].render;
331
332                         // don't hit ourselves
333                         if (i == cl.playerentity)
334                                 continue;
335
336                         // don't hit players that don't exist
337                         if (!cl.scores[i-1].name[0])
338                                 continue;
339
340                         if(gamemode == GAME_NEXUIZ)
341                         {
342                                 // don't hit spectators or nonsolid players
343                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
344                                         continue;
345                         }
346
347                         Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
348                         VectorAdd(origin, cl.playerstandmins, entmins);
349                         VectorAdd(origin, cl.playerstandmaxs, entmaxs);
350                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
351                                 continue;
352                         Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
353                         Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
354                         Collision_ClipToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
355                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
356                                 *hitnetworkentity = i;
357                         Collision_CombineTraces(&cliptrace, &trace, NULL, false);
358                 }
359
360 skipnetworkplayers:
361                 ;
362         }
363
364         // clip to entities
365         // because this uses World_EntitiestoBox, we know all entity boxes overlap
366         // the clip region, so we can skip culling checks in the loop below
367         // note: if prog is NULL then there won't be any linked entities
368         numtouchedicts = 0;
369         if (hitcsqcentities && prog != NULL)
370         {
371                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
372                 if (numtouchedicts > MAX_EDICTS)
373                 {
374                         // this never happens
375                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
376                         numtouchedicts = MAX_EDICTS;
377                 }
378         }
379         for (i = 0;i < numtouchedicts;i++)
380         {
381                 touch = touchedicts[i];
382
383                 if (touch->fields.client->solid < SOLID_BBOX)
384                         continue;
385                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
386                         continue;
387
388                 if (passedict)
389                 {
390                         // don't clip against self
391                         if (passedict == touch)
392                                 continue;
393                         // don't clip owned entities against owner
394                         if (traceowner == touch)
395                                 continue;
396                         // don't clip owner against owned entities
397                         if (passedictprog == touch->fields.client->owner)
398                                 continue;
399                         // don't clip points against points (they can't collide)
400                         if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
401                                 continue;
402                 }
403
404                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
405
406                 // might interact, so do an exact clip
407                 model = NULL;
408                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
409                 {
410                         unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
411                         // if the modelindex is 0, it shouldn't be SOLID_BSP!
412                         if (modelindex > 0 && modelindex < MAX_MODELS)
413                                 model = cl.model_precache[(int)touch->fields.client->modelindex];
414                 }
415                 if (model)
416                         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);
417                 else
418                         Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
419                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
420                 if ((int)touch->fields.client->flags & FL_MONSTER)
421                         Collision_ClipToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask);
422                 else
423                         Collision_ClipToGenericEntity(&trace, model, (int) touch->fields.client->frame, touch->fields.client->mins, touch->fields.client->maxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
424
425                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
426                         *hitnetworkentity = 0;
427                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
428         }
429
430         return cliptrace;
431 }