]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
changed color control menu correction value to 0.5 (medium grey on linear color monitors)
[divverent/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 /*
6 // not yet used
7 typedef struct physentity_s
8 {
9         // this may be a entity_t, or a prvm_edict_t, or whatever
10         void *realentity;
11
12         // can be NULL if it is a bbox object
13         model_t *bmodel;
14
15         // node this entity crosses
16         // for avoiding unnecessary collisions
17         physnode_t *node;
18
19         // matrix for converting from model to world coordinates
20         double modeltoworldmatrix[3][4];
21
22         // matrix for converting from world to model coordinates
23         double worldtomodelmatrix[3][4];
24
25         // if this is a bmodel, this is used for culling it quickly
26         // if this is not a bmodel, this is used for actual collisions
27         double mins[3], maxs[3];
28 }
29 physentity_t;
30 */
31
32 trace_t CL_TraceBox(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitbmodels, int *hitent, int hitsupercontentsmask, qboolean hitplayers)
33 {
34         int n;
35         entity_render_t *ent;
36         vec3_t tracemins, tracemaxs;
37         trace_t cliptrace, trace;
38         vec3_t origin;
39         vec3_t starttransformed, endtransformed;
40         vec3_t entmins, entmaxs;
41         vec_t *playermins, *playermaxs;
42
43         memset (&cliptrace, 0 , sizeof(trace_t));
44         cliptrace.fraction = 1;
45         cliptrace.realfraction = 1;
46
47         if (cl.worldmodel && cl.worldmodel->TraceBox)
48                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &cliptrace, start, mins, maxs, end, hitsupercontentsmask);
49
50         if (hitent)
51                 *hitent = 0;
52
53         if (hitbmodels && cl.num_brushmodel_entities)
54         {
55                 tracemins[0] = min(start[0], end[0]) + mins[0];
56                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
57                 tracemins[1] = min(start[1], end[1]) + mins[1];
58                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
59                 tracemins[2] = min(start[2], end[2]) + mins[2];
60                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
61
62                 // look for embedded bmodels
63                 for (n = 0;n < cl.num_brushmodel_entities;n++)
64                 {
65                         ent = &cl.entities[cl.brushmodel_entities[n]].render;
66                         if (!BoxesOverlap(tracemins, tracemaxs, ent->mins, ent->maxs))
67                                 continue;
68
69                         Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
70                         Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
71
72                         memset (&trace, 0 , sizeof(trace_t));
73                         trace.fraction = 1;
74                         trace.realfraction = 1;
75
76                         if (ent->model && ent->model->TraceBox)
77                                 ent->model->TraceBox(ent->model, 0, &trace, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask);
78
79                         // LordHavoc: take the 'best' answers from the new trace and combine with existing data
80                         if (trace.allsolid)
81                                 cliptrace.allsolid = true;
82                         if (trace.startsolid)
83                         {
84                                 cliptrace.startsolid = true;
85                                 if (cliptrace.realfraction == 1)
86                                         if (hitent)
87                                                 *hitent = cl.brushmodel_entities[n];
88                         }
89                         // don't set this except on the world, because it can easily confuse
90                         // monsters underwater if there's a bmodel involved in the trace
91                         // (inopen && inwater is how they check water visibility)
92                         //if (trace.inopen)
93                         //      cliptrace.inopen = true;
94                         if (trace.inwater)
95                                 cliptrace.inwater = true;
96                         if (trace.realfraction < cliptrace.realfraction)
97                         {
98                                 cliptrace.fraction = trace.fraction;
99                                 cliptrace.realfraction = trace.realfraction;
100                                 cliptrace.plane = trace.plane;
101                                 if (hitent)
102                                         *hitent = cl.brushmodel_entities[n];
103                                 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
104                                 cliptrace.hitsupercontents = trace.hitsupercontents;
105                                 cliptrace.hitq3surfaceflags = trace.hitq3surfaceflags;
106                                 cliptrace.hittexture = trace.hittexture;
107                         }
108                         cliptrace.startsupercontents |= trace.startsupercontents;
109                 }
110         }
111         if (hitplayers)
112         {
113                 tracemins[0] = min(start[0], end[0]) + mins[0];
114                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
115                 tracemins[1] = min(start[1], end[1]) + mins[1];
116                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
117                 tracemins[2] = min(start[2], end[2]) + mins[2];
118                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
119
120                 for (n = 1;n < cl.maxclients+1;n++)
121                 {
122                         if (n != cl.playerentity)
123                         {
124                                 ent = &cl.entities[n].render;
125                                 // FIXME: crouch
126                                 playermins = cl.playerstandmins;
127                                 playermaxs = cl.playerstandmaxs;
128                                 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
129                                 VectorAdd(origin, playermins, entmins);
130                                 VectorAdd(origin, playermaxs, entmaxs);
131                                 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
132                                         continue;
133
134                                 memset (&trace, 0 , sizeof(trace_t));
135                                 trace.fraction = 1;
136                                 trace.realfraction = 1;
137
138                                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
139                                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
140                                 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_BODY, 0, NULL);
141
142                                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
143                                 if (trace.allsolid)
144                                         cliptrace.allsolid = true;
145                                 if (trace.startsolid)
146                                 {
147                                         cliptrace.startsolid = true;
148                                         if (cliptrace.realfraction == 1)
149                                                 if (hitent)
150                                                         *hitent = n;
151                                 }
152                                 // don't set this except on the world, because it can easily confuse
153                                 // monsters underwater if there's a bmodel involved in the trace
154                                 // (inopen && inwater is how they check water visibility)
155                                 //if (trace.inopen)
156                                 //      cliptrace.inopen = true;
157                                 if (trace.inwater)
158                                         cliptrace.inwater = true;
159                                 if (trace.realfraction < cliptrace.realfraction)
160                                 {
161                                         cliptrace.fraction = trace.fraction;
162                                         cliptrace.realfraction = trace.realfraction;
163                                         cliptrace.plane = trace.plane;
164                                         if (hitent)
165                                                 *hitent = n;
166                                         Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
167                                         cliptrace.hitsupercontents = trace.hitsupercontents;
168                                         cliptrace.hitq3surfaceflags = trace.hitq3surfaceflags;
169                                         cliptrace.hittexture = trace.hittexture;
170                                 }
171                                 cliptrace.startsupercontents |= trace.startsupercontents;
172                         }
173                 }
174         }
175         cliptrace.fraction = bound(0, cliptrace.fraction, 1);
176         cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
177         VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
178         return cliptrace;
179 }
180
181 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent, qboolean csqcents)
182 {
183         float maxfrac, maxrealfrac;
184         int n, entsnum;
185         entity_t *entlist;
186         unsigned char *entactivelist;
187         entity_render_t *ent;
188         float tracemins[3], tracemaxs[3];
189         trace_t trace;
190         float tempnormal[3], starttransformed[3], endtransformed[3];
191
192         memset (&trace, 0 , sizeof(trace_t));
193         trace.fraction = 1;
194         trace.realfraction = 1;
195         VectorCopy (end, trace.endpos);
196
197         if (hitent)
198                 *hitent = 0;
199         if (cl.worldmodel && cl.worldmodel->TraceBox)
200                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, vec3_origin, vec3_origin, end, SUPERCONTENTS_SOLID);
201
202         if (normal)
203                 VectorCopy(trace.plane.normal, normal);
204         maxfrac = trace.fraction;
205         maxrealfrac = trace.realfraction;
206
207         tracemins[0] = min(start[0], end[0]);
208         tracemaxs[0] = max(start[0], end[0]);
209         tracemins[1] = min(start[1], end[1]);
210         tracemaxs[1] = max(start[1], end[1]);
211         tracemins[2] = min(start[2], end[2]);
212         tracemaxs[2] = max(start[2], end[2]);
213
214         if(csqcents)
215         {
216                 entlist = cl.csqcentities;
217                 entactivelist = cl.csqcentities_active;
218                 entsnum = cl.num_csqcentities;
219         }
220         else
221         {
222                 entlist = cl.entities;
223                 entactivelist = cl.entities_active;
224                 entsnum = cl.num_entities;
225         }
226
227         // look for embedded bmodels
228         for (n = 0;n < entsnum;n++)
229         {
230                 if (!entactivelist[n])
231                         continue;
232                 ent = &entlist[n].render;
233                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
234                         continue;
235                 if (!ent->model || !ent->model->TraceBox)
236                         continue;
237                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
238                         continue;
239                 // if transparent and not selectable, skip entity
240                 if (!(entlist[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
241                         continue;
242                 if (ent == ignoreent)
243                         continue;
244                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
245                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
246
247                 //if (ent->model && ent->model->TraceBox)
248                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, vec3_origin, vec3_origin, endtransformed, SUPERCONTENTS_SOLID);
249
250                 if (maxrealfrac > trace.realfraction)
251                 {
252                         if (hitent)
253                                 *hitent = n;
254                         maxfrac = trace.fraction;
255                         maxrealfrac = trace.realfraction;
256                         if (normal)
257                         {
258                                 VectorCopy(trace.plane.normal, tempnormal);
259                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
260                         }
261                 }
262         }
263         maxfrac = bound(0, maxfrac, 1);
264         maxrealfrac = bound(0, maxrealfrac, 1);
265         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
266         if (impact)
267                 VectorLerp(start, maxfrac, end, impact);
268         return maxfrac;
269 }
270
271 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
272 {
273         // FIXME: check multiple brush models
274         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
275                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
276 }
277
278 int CL_PointSuperContents(const vec3_t p)
279 {
280         return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
281 }
282