SDL/Win32: queue a vid_restart if resized; this should fix vid_resizable issue on...
[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))
22         {
23                 // TRICK: make the trace 1 qu longer!
24                 VectorSubtract(pEnd, start, end);
25                 len = VectorNormalizeLength(end);
26                 VectorAdd(pEnd, 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->TraceBox)
40                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, vec3_origin, vec3_origin, 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->TraceBox)
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))
76                         Collision_ShortenTrace(&trace, len / (len + 1), pEnd);
77 #endif
78                 if (maxrealfrac < trace.realfraction)
79                         continue;
80
81                 ent->model->TraceBox(ent->model, ent->frameblend[0].subframe, &trace, starttransformed, vec3_origin, vec3_origin, 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 && developer.integer >= 1)
161                                 Con_Printf("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 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
230 trace_t CL_Move(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)
231 #else
232 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)
233 #endif
234 {
235         vec3_t hullmins, hullmaxs;
236         int i, bodysupercontents;
237         int passedictprog;
238         qboolean pointtrace;
239         prvm_edict_t *traceowner, *touch;
240         trace_t trace;
241         // bounding box of entire move area
242         vec3_t clipboxmins, clipboxmaxs;
243         // size of the moving object
244         vec3_t clipmins, clipmaxs;
245         // size when clipping against monsters
246         vec3_t clipmins2, clipmaxs2;
247         // start and end origin of move
248         vec3_t clipstart, clipend;
249         // trace results
250         trace_t cliptrace;
251         // matrices to transform into/out of other entity's space
252         matrix4x4_t matrix, imatrix;
253         // model of other entity
254         dp_model_t *model;
255         // list of entities to test for collisions
256         int numtouchedicts;
257         prvm_edict_t *touchedicts[MAX_EDICTS];
258 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
259         vec3_t end;
260         vec_t len = 0;
261
262         if(!VectorCompare(start, pEnd))
263         {
264                 // TRICK: make the trace 1 qu longer!
265                 VectorSubtract(pEnd, start, end);
266                 len = VectorNormalizeLength(end);
267                 VectorAdd(pEnd, end, end);
268         }
269         else
270                 VectorCopy(pEnd, end);
271 #endif
272
273         if (hitnetworkentity)
274                 *hitnetworkentity = 0;
275
276         VectorCopy(start, clipstart);
277         VectorCopy(end, clipend);
278         VectorCopy(mins, clipmins);
279         VectorCopy(maxs, clipmaxs);
280         VectorCopy(mins, clipmins2);
281         VectorCopy(maxs, clipmaxs2);
282 #if COLLISIONPARANOID >= 3
283         Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
284 #endif
285
286         // clip to world
287         Collision_ClipToWorld(&cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask);
288         cliptrace.bmodelstartsolid = cliptrace.startsolid;
289         if (cliptrace.startsolid || cliptrace.fraction < 1)
290                 cliptrace.ent = prog ? prog->edicts : NULL;
291         if (type == MOVE_WORLDONLY)
292                 goto finished;
293
294         if (type == MOVE_MISSILE)
295         {
296                 // LordHavoc: modified this, was = -15, now -= 15
297                 for (i = 0;i < 3;i++)
298                 {
299                         clipmins2[i] -= 15;
300                         clipmaxs2[i] += 15;
301                 }
302         }
303
304         // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
305         if (cl.worldmodel && cl.worldmodel->brush.RoundUpToHullSize)
306                 cl.worldmodel->brush.RoundUpToHullSize(cl.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
307         else
308         {
309                 VectorCopy(clipmins, hullmins);
310                 VectorCopy(clipmaxs, hullmaxs);
311         }
312
313         // create the bounding box of the entire move
314         for (i = 0;i < 3;i++)
315         {
316                 clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
317                 clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
318         }
319
320         // debug override to test against everything
321         if (sv_debugmove.integer)
322         {
323                 clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = -999999999;
324                 clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] =  999999999;
325         }
326
327         // if the passedict is world, make it NULL (to avoid two checks each time)
328         // this checks prog because this function is often called without a CSQC
329         // VM context
330         if (prog == NULL || passedict == prog->edicts)
331                 passedict = NULL;
332         // precalculate prog value for passedict for comparisons
333         passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0;
334         // figure out whether this is a point trace for comparisons
335         pointtrace = VectorCompare(clipmins, clipmaxs);
336         // precalculate passedict's owner edict pointer for comparisons
337         traceowner = passedict ? PRVM_PROG_TO_EDICT(passedict->fields.client->owner) : NULL;
338
339         // collide against network entities
340         if (hitnetworkbrushmodels)
341         {
342                 for (i = 0;i < cl.num_brushmodel_entities;i++)
343                 {
344                         entity_render_t *ent = &cl.entities[cl.brushmodel_entities[i]].render;
345                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, ent->mins, ent->maxs))
346                                 continue;
347                         Collision_ClipToGenericEntity(&trace, ent->model, ent->frameblend[0].subframe, vec3_origin, vec3_origin, 0, &ent->matrix, &ent->inversematrix, start, mins, maxs, end, hitsupercontentsmask);
348                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
349                                 *hitnetworkentity = cl.brushmodel_entities[i];
350                         Collision_CombineTraces(&cliptrace, &trace, NULL, true);
351                 }
352         }
353
354         // collide against player entities
355         if (hitnetworkplayers)
356         {
357                 vec3_t origin, entmins, entmaxs;
358                 matrix4x4_t entmatrix, entinversematrix;
359
360                 if(gamemode == GAME_NEXUIZ)
361                 {
362                         // don't hit network players, if we are a nonsolid player
363                         if(cl.scores[cl.playerentity-1].frags == -666 || cl.scores[cl.playerentity-1].frags == -616)
364                                 goto skipnetworkplayers;
365                 }
366
367                 for (i = 1;i <= cl.maxclients;i++)
368                 {
369                         entity_render_t *ent = &cl.entities[i].render;
370
371                         // don't hit ourselves
372                         if (i == cl.playerentity)
373                                 continue;
374
375                         // don't hit players that don't exist
376                         if (!cl.scores[i-1].name[0])
377                                 continue;
378
379                         if(gamemode == GAME_NEXUIZ)
380                         {
381                                 // don't hit spectators or nonsolid players
382                                 if(cl.scores[i-1].frags == -666 || cl.scores[i-1].frags == -616)
383                                         continue;
384                         }
385
386                         Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
387                         VectorAdd(origin, cl.playerstandmins, entmins);
388                         VectorAdd(origin, cl.playerstandmaxs, entmaxs);
389                         if (!BoxesOverlap(clipboxmins, clipboxmaxs, entmins, entmaxs))
390                                 continue;
391                         Matrix4x4_CreateTranslate(&entmatrix, origin[0], origin[1], origin[2]);
392                         Matrix4x4_CreateTranslate(&entinversematrix, -origin[0], -origin[1], -origin[2]);
393                         Collision_ClipToGenericEntity(&trace, NULL, 0, cl.playerstandmins, cl.playerstandmaxs, SUPERCONTENTS_BODY, &entmatrix, &entinversematrix, start, mins, maxs, end, hitsupercontentsmask);
394                         if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
395                                 *hitnetworkentity = i;
396                         Collision_CombineTraces(&cliptrace, &trace, NULL, false);
397                 }
398
399 skipnetworkplayers:
400                 ;
401         }
402
403         // clip to entities
404         // because this uses World_EntitiestoBox, we know all entity boxes overlap
405         // the clip region, so we can skip culling checks in the loop below
406         // note: if prog is NULL then there won't be any linked entities
407         numtouchedicts = 0;
408         if (hitcsqcentities && prog != NULL)
409         {
410                 numtouchedicts = World_EntitiesInBox(&cl.world, clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
411                 if (numtouchedicts > MAX_EDICTS)
412                 {
413                         // this never happens
414                         Con_Printf("CL_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
415                         numtouchedicts = MAX_EDICTS;
416                 }
417         }
418         for (i = 0;i < numtouchedicts;i++)
419         {
420                 touch = touchedicts[i];
421
422                 if (touch->fields.client->solid < SOLID_BBOX)
423                         continue;
424                 if (type == MOVE_NOMONSTERS && touch->fields.client->solid != SOLID_BSP)
425                         continue;
426
427                 if (passedict)
428                 {
429                         // don't clip against self
430                         if (passedict == touch)
431                                 continue;
432                         // don't clip owned entities against owner
433                         if (traceowner == touch)
434                                 continue;
435                         // don't clip owner against owned entities
436                         if (passedictprog == touch->fields.client->owner)
437                                 continue;
438                         // don't clip points against points (they can't collide)
439                         if (pointtrace && VectorCompare(touch->fields.client->mins, touch->fields.client->maxs) && (type != MOVE_MISSILE || !((int)touch->fields.client->flags & FL_MONSTER)))
440                                 continue;
441                 }
442
443                 bodysupercontents = touch->fields.client->solid == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
444
445                 // might interact, so do an exact clip
446                 model = NULL;
447                 if ((int) touch->fields.client->solid == SOLID_BSP || type == MOVE_HITMODEL)
448                 {
449                         unsigned int modelindex = (unsigned int)touch->fields.client->modelindex;
450                         // if the modelindex is 0, it shouldn't be SOLID_BSP!
451                         if (modelindex > 0 && modelindex < MAX_MODELS)
452                                 model = cl.model_precache[(int)touch->fields.client->modelindex];
453                 }
454                 if (model)
455                         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);
456                 else
457                         Matrix4x4_CreateTranslate(&matrix, touch->fields.client->origin[0], touch->fields.client->origin[1], touch->fields.client->origin[2]);
458                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
459                 if ((int)touch->fields.client->flags & FL_MONSTER)
460                         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);
461                 else
462                         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);
463
464                 if (cliptrace.realfraction > trace.realfraction && hitnetworkentity)
465                         *hitnetworkentity = 0;
466                 Collision_CombineTraces(&cliptrace, &trace, (void *)touch, touch->fields.client->solid == SOLID_BSP);
467         }
468
469 finished:
470 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
471         if(!VectorCompare(start, pEnd))
472                 Collision_ShortenTrace(&cliptrace, len / (len + 1), pEnd);
473 #endif
474         return cliptrace;
475 }