]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_collision.c
got rid of Mod_CheckLoaded, changed how model system restart works to make this work...
[xonotic/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 starttransformed, endtransformed, starttransformedmins, endtransformedmins, starttransformedmaxs, endtransformedmaxs;
39         vec3_t startmins, startmaxs, endmins, endmaxs, entmins, entmaxs;
40         vec_t *playermins, *playermaxs;
41
42         VectorAdd(start, mins, startmins);
43         VectorAdd(start, maxs, startmaxs);
44         VectorAdd(end, mins, endmins);
45         VectorAdd(end, maxs, endmaxs);
46
47         memset (&cliptrace, 0 , sizeof(trace_t));
48         cliptrace.fraction = 1;
49         cliptrace.realfraction = 1;
50
51         if (cl.worldmodel && cl.worldmodel->TraceBox)
52                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &cliptrace, startmins, startmaxs, endmins, endmaxs, hitsupercontentsmask);
53
54         if (hitent)
55                 *hitent = 0;
56
57         if (hitbmodels && cl_num_brushmodel_entities)
58         {
59                 tracemins[0] = min(start[0], end[0]) + mins[0];
60                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
61                 tracemins[1] = min(start[1], end[1]) + mins[1];
62                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
63                 tracemins[2] = min(start[2], end[2]) + mins[2];
64                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
65
66                 // look for embedded bmodels
67                 for (n = 0;n < cl_num_brushmodel_entities;n++)
68                 {
69                         ent = &cl_entities[cl_brushmodel_entities[n]].render;
70                         if (!BoxesOverlap(tracemins, tracemaxs, ent->mins, ent->maxs))
71                                 continue;
72
73                         Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
74                         Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
75                         VectorAdd(starttransformed, mins, starttransformedmins);
76                         VectorAdd(starttransformed, maxs, starttransformedmaxs);
77                         VectorAdd(endtransformed, mins, endtransformedmins);
78                         VectorAdd(endtransformed, maxs, endtransformedmaxs);
79
80                         memset (&trace, 0 , sizeof(trace_t));
81                         trace.fraction = 1;
82                         trace.realfraction = 1;
83
84                         if (ent->model && ent->model->TraceBox)
85                                 ent->model->TraceBox(ent->model, 0, &trace, starttransformedmins, starttransformedmaxs, endtransformedmins, endtransformedmaxs, hitsupercontentsmask);
86
87                         // LordHavoc: take the 'best' answers from the new trace and combine with existing data
88                         if (trace.allsolid)
89                                 cliptrace.allsolid = true;
90                         if (trace.startsolid)
91                         {
92                                 cliptrace.startsolid = true;
93                                 if (cliptrace.realfraction == 1)
94                                         if (hitent)
95                                                 *hitent = cl_brushmodel_entities[n];
96                         }
97                         // don't set this except on the world, because it can easily confuse
98                         // monsters underwater if there's a bmodel involved in the trace
99                         // (inopen && inwater is how they check water visibility)
100                         //if (trace.inopen)
101                         //      cliptrace.inopen = true;
102                         if (trace.inwater)
103                                 cliptrace.inwater = true;
104                         if (trace.realfraction < cliptrace.realfraction)
105                         {
106                                 cliptrace.fraction = trace.fraction;
107                                 cliptrace.realfraction = trace.realfraction;
108                                 cliptrace.plane = trace.plane;
109                                 if (hitent)
110                                         *hitent = cl_brushmodel_entities[n];
111                                 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
112                         }
113                         cliptrace.startsupercontents |= trace.startsupercontents;
114                 }
115         }
116         if (hitplayers)
117         {
118                 tracemins[0] = min(start[0], end[0]) + mins[0];
119                 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
120                 tracemins[1] = min(start[1], end[1]) + mins[1];
121                 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
122                 tracemins[2] = min(start[2], end[2]) + mins[2];
123                 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
124
125                 for (n = 1;n < cl.maxclients+1;n++)
126                 {
127                         if (n != cl.playerentity)
128                         {
129                                 ent = &cl_entities[n].render;
130                                 // FIXME: crouch
131                                 playermins = cl_playerstandmins;
132                                 playermaxs = cl_playerstandmaxs;
133                                 VectorAdd(ent->origin, playermins, entmins);
134                                 VectorAdd(ent->origin, playermaxs, entmaxs);
135                                 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
136                                         continue;
137
138                                 memset (&trace, 0 , sizeof(trace_t));
139                                 trace.fraction = 1;
140                                 trace.realfraction = 1;
141
142                                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
143                                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
144                                 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
145
146                                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
147                                 if (trace.allsolid)
148                                         cliptrace.allsolid = true;
149                                 if (trace.startsolid)
150                                 {
151                                         cliptrace.startsolid = true;
152                                         if (cliptrace.realfraction == 1)
153                                                 if (hitent)
154                                                         *hitent = n;
155                                 }
156                                 // don't set this except on the world, because it can easily confuse
157                                 // monsters underwater if there's a bmodel involved in the trace
158                                 // (inopen && inwater is how they check water visibility)
159                                 //if (trace.inopen)
160                                 //      cliptrace.inopen = true;
161                                 if (trace.inwater)
162                                         cliptrace.inwater = true;
163                                 if (trace.realfraction < cliptrace.realfraction)
164                                 {
165                                         cliptrace.fraction = trace.fraction;
166                                         cliptrace.realfraction = trace.realfraction;
167                                         cliptrace.plane = trace.plane;
168                                         if (hitent)
169                                                 *hitent = n;
170                                         Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
171                                 }
172                                 cliptrace.startsupercontents |= trace.startsupercontents;
173                         }
174                 }
175         }
176         cliptrace.fraction = bound(0, cliptrace.fraction, 1);
177         cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
178         VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
179         return cliptrace;
180 }
181
182 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
183 {
184         float maxfrac, maxrealfrac;
185         int n;
186         entity_render_t *ent;
187         float tracemins[3], tracemaxs[3];
188         trace_t trace;
189         float tempnormal[3], starttransformed[3], endtransformed[3];
190
191         memset (&trace, 0 , sizeof(trace_t));
192         trace.fraction = 1;
193         trace.realfraction = 1;
194         VectorCopy (end, trace.endpos);
195
196         if (hitent)
197                 *hitent = 0;
198         if (cl.worldmodel && cl.worldmodel->TraceBox)
199                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
200
201         if (normal)
202                 VectorCopy(trace.plane.normal, normal);
203         //cl_traceline_startsupercontents = trace.startsupercontents;
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         // look for embedded bmodels
215         for (n = 0;n < cl_num_entities;n++)
216         {
217                 if (!cl_entities_active[n])
218                         continue;
219                 ent = &cl_entities[n].render;
220                 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
221                         continue;
222                 if (!ent->model || !ent->model->TraceBox)
223                         continue;
224                 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
225                         continue;
226                 // if transparent and not selectable, skip entity
227                 if (!(cl_entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
228                         continue;
229                 if (ent == ignoreent)
230                         continue;
231                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
232                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
233
234                 if (ent->model && ent->model->TraceBox)
235                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
236
237                 //cl_traceline_startsupercontents |= trace.startsupercontents;
238                 if (maxrealfrac > trace.realfraction)
239                 {
240                         if (hitent)
241                                 *hitent = n;
242                         maxfrac = trace.fraction;
243                         maxrealfrac = trace.realfraction;
244                         if (normal)
245                         {
246                                 VectorCopy(trace.plane.normal, tempnormal);
247                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
248                         }
249                 }
250         }
251         maxfrac = bound(0, maxfrac, 1);
252         maxrealfrac = bound(0, maxrealfrac, 1);
253         //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
254         if (impact)
255                 VectorLerp(start, maxfrac, end, impact);
256         return maxfrac;
257 }
258
259 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
260 {
261         // FIXME: check multiple brush models
262         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
263                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
264 }
265
266 int CL_PointQ1Contents(const vec3_t p)
267 {
268         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents);
269         /*
270         // FIXME: check multiple brush models
271         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
272                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
273         return 0;
274         */
275 }
276
277 int CL_PointSuperContents(const vec3_t p)
278 {
279         return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
280         /*
281         // FIXME: check multiple brush models
282         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
283                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
284         return 0;
285         */
286 }
287