]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
fix skinfile memory leaks on ZYM, DPM, and PSK model loaders, and move the skinfile...
[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, start, 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                         }
105                         cliptrace.startsupercontents |= trace.startsupercontents;
106                 }
107         }
108         if (hitplayers)
109         {
110                 tracemins[0] = min(start[0], end[0]) + mins[0];
111                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
112                 tracemins[1] = min(start[1], end[1]) + mins[1];
113                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
114                 tracemins[2] = min(start[2], end[2]) + mins[2];
115                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
116
117                 for (n = 1;n < cl.maxclients+1;n++)
118                 {
119                         if (n != cl.playerentity)
120                         {
121                                 ent = &cl_entities[n].render;
122                                 // FIXME: crouch
123                                 playermins = cl_playerstandmins;
124                                 playermaxs = cl_playerstandmaxs;
125                                 Matrix4x4_OriginFromMatrix(&ent->matrix, origin);
126                                 VectorAdd(origin, playermins, entmins);
127                                 VectorAdd(origin, playermaxs, entmaxs);
128                                 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
129                                         continue;
130
131                                 memset (&trace, 0 , sizeof(trace_t));
132                                 trace.fraction = 1;
133                                 trace.realfraction = 1;
134
135                                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
136                                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
137                                 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
138
139                                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
140                                 if (trace.allsolid)
141                                         cliptrace.allsolid = true;
142                                 if (trace.startsolid)
143                                 {
144                                         cliptrace.startsolid = true;
145                                         if (cliptrace.realfraction == 1)
146                                                 if (hitent)
147                                                         *hitent = n;
148                                 }
149                                 // don't set this except on the world, because it can easily confuse
150                                 // monsters underwater if there's a bmodel involved in the trace
151                                 // (inopen && inwater is how they check water visibility)
152                                 //if (trace.inopen)
153                                 //      cliptrace.inopen = true;
154                                 if (trace.inwater)
155                                         cliptrace.inwater = true;
156                                 if (trace.realfraction < cliptrace.realfraction)
157                                 {
158                                         cliptrace.fraction = trace.fraction;
159                                         cliptrace.realfraction = trace.realfraction;
160                                         cliptrace.plane = trace.plane;
161                                         if (hitent)
162                                                 *hitent = n;
163                                         Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
164                                 }
165                                 cliptrace.startsupercontents |= trace.startsupercontents;
166                         }
167                 }
168         }
169         cliptrace.fraction = bound(0, cliptrace.fraction, 1);
170         cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
171         VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
172         return cliptrace;
173 }
174
175 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)
176 {
177         float maxfrac, maxrealfrac;
178         int n, entsnum;
179         entity_t *entlist;
180         unsigned char *entactivelist;
181         entity_render_t *ent;
182         float tracemins[3], tracemaxs[3];
183         trace_t trace;
184         float tempnormal[3], starttransformed[3], endtransformed[3];
185
186         memset (&trace, 0 , sizeof(trace_t));
187         trace.fraction = 1;
188         trace.realfraction = 1;
189         VectorCopy (end, trace.endpos);
190
191         if (hitent)
192                 *hitent = 0;
193         if (cl.worldmodel && cl.worldmodel->TraceBox)
194                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
195
196         if (normal)
197                 VectorCopy(trace.plane.normal, normal);
198         //cl_traceline_startsupercontents = trace.startsupercontents;
199         maxfrac = trace.fraction;
200         maxrealfrac = trace.realfraction;
201
202         tracemins[0] = min(start[0], end[0]);
203         tracemaxs[0] = max(start[0], end[0]);
204         tracemins[1] = min(start[1], end[1]);
205         tracemaxs[1] = max(start[1], end[1]);
206         tracemins[2] = min(start[2], end[2]);
207         tracemaxs[2] = max(start[2], end[2]);
208
209         if(csqcents)
210         {
211                 entlist = cl_csqcentities;
212                 entactivelist = cl_csqcentities_active;
213                 entsnum = cl_num_csqcentities;
214         }
215         else
216         {
217                 entlist = cl_entities;
218                 entactivelist = cl_entities_active;
219                 entsnum = cl_num_entities;
220         }
221
222         // look for embedded bmodels
223         for (n = 0;n < entsnum;n++)
224         {
225                 if (!entactivelist[n])
226                         continue;
227                 ent = &entlist[n].render;
228                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
229                         continue;
230                 if (!ent->model || !ent->model->TraceBox)
231                         continue;
232                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
233                         continue;
234                 // if transparent and not selectable, skip entity
235                 if (!(entlist[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
236                         continue;
237                 if (ent == ignoreent)
238                         continue;
239                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
240                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
241
242                 if (ent->model && ent->model->TraceBox)
243                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
244
245                 //cl_traceline_startsupercontents |= trace.startsupercontents;
246                 if (maxrealfrac > trace.realfraction)
247                 {
248                         if (hitent)
249                                 *hitent = n;
250                         maxfrac = trace.fraction;
251                         maxrealfrac = trace.realfraction;
252                         if (normal)
253                         {
254                                 VectorCopy(trace.plane.normal, tempnormal);
255                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
256                         }
257                 }
258         }
259         maxfrac = bound(0, maxfrac, 1);
260         maxrealfrac = bound(0, maxrealfrac, 1);
261         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
262         if (impact)
263                 VectorLerp(start, maxfrac, end, impact);
264         return maxfrac;
265 }
266
267 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
268 {
269         // FIXME: check multiple brush models
270         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
271                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
272 }
273
274 int CL_PointQ1Contents(const vec3_t p)
275 {
276         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents);
277         /*
278         // FIXME: check multiple brush models
279         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
280                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
281         return 0;
282         */
283 }
284
285 int CL_PointSuperContents(const vec3_t p)
286 {
287         return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
288         /*
289         // FIXME: check multiple brush models
290         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
291                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
292         return 0;
293         */
294 }
295