some minor cleanup to CL_TraceLine, added CL_SelectTraceLine which is to be used...
[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 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 int cl_traceline_startsupercontents;
33
34 float CL_TraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int hitbmodels, entity_render_t **hitent, int hitsupercontentsmask)
35 {
36         float maxfrac, maxrealfrac;
37         int n;
38         entity_render_t *ent;
39         float tracemins[3], tracemaxs[3];
40         trace_t trace;
41         float tempnormal[3], starttransformed[3], endtransformed[3];
42
43         memset (&trace, 0 , sizeof(trace_t));
44         trace.fraction = 1;
45         trace.realfraction = 1;
46         VectorCopy (end, trace.endpos);
47
48         if (hitent)
49                 *hitent = &cl_entities[0].render;
50         Mod_CheckLoaded(cl.worldmodel);
51         if (cl.worldmodel && cl.worldmodel->TraceBox)
52                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, hitsupercontentsmask);
53
54         if (normal)
55                 VectorCopy(trace.plane.normal, normal);
56         cl_traceline_startsupercontents = trace.startsupercontents;
57         maxfrac = trace.fraction;
58         maxrealfrac = trace.realfraction;
59
60         if (hitbmodels && cl_num_brushmodel_entities)
61         {
62                 tracemins[0] = min(start[0], end[0]);
63                 tracemaxs[0] = max(start[0], end[0]);
64                 tracemins[1] = min(start[1], end[1]);
65                 tracemaxs[1] = max(start[1], end[1]);
66                 tracemins[2] = min(start[2], end[2]);
67                 tracemaxs[2] = max(start[2], end[2]);
68
69                 // look for embedded bmodels
70                 for (n = 0;n < cl_num_brushmodel_entities;n++)
71                 {
72                         ent = cl_brushmodel_entities[n];
73                         if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
74                          || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
75                          || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
76                                 continue;
77
78                         Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
79                         Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
80
81                         if (ent->model && ent->model->TraceBox)
82                                 ent->model->TraceBox(ent->model, 0, &trace, starttransformed, starttransformed, endtransformed, endtransformed, hitsupercontentsmask);
83
84                         cl_traceline_startsupercontents |= trace.startsupercontents;
85                         if (maxrealfrac > trace.realfraction)
86                         {
87                                 if (hitent)
88                                         *hitent = ent;
89                                 maxfrac = trace.fraction;
90                                 maxrealfrac = trace.realfraction;
91                                 if (normal)
92                                 {
93                                         VectorCopy(trace.plane.normal, tempnormal);
94                                         Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
95                                 }
96                         }
97                 }
98         }
99         if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
100         if (impact)
101                 VectorLerp(start, maxfrac, end, impact);
102         return maxfrac;
103 }
104
105 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent)
106 {
107         float maxfrac, maxrealfrac;
108         int n;
109         entity_render_t *ent;
110         float tracemins[3], tracemaxs[3];
111         trace_t trace;
112         float tempnormal[3], starttransformed[3], endtransformed[3];
113
114         memset (&trace, 0 , sizeof(trace_t));
115         trace.fraction = 1;
116         trace.realfraction = 1;
117         VectorCopy (end, trace.endpos);
118
119         if (hitent)
120                 *hitent = 0;
121         Mod_CheckLoaded(cl.worldmodel);
122         if (cl.worldmodel && cl.worldmodel->TraceBox)
123                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
124
125         if (normal)
126                 VectorCopy(trace.plane.normal, normal);
127         cl_traceline_startsupercontents = trace.startsupercontents;
128         maxfrac = trace.fraction;
129         maxrealfrac = trace.realfraction;
130
131         tracemins[0] = min(start[0], end[0]);
132         tracemaxs[0] = max(start[0], end[0]);
133         tracemins[1] = min(start[1], end[1]);
134         tracemaxs[1] = max(start[1], end[1]);
135         tracemins[2] = min(start[2], end[2]);
136         tracemaxs[2] = max(start[2], end[2]);
137
138         // look for embedded bmodels
139         for (n = 0;n < MAX_EDICTS;n++)
140         {
141                 if (!cl_entities[n].state_current.active)
142                         continue;
143                 ent = &cl_entities[n].render;
144                 if (!ent->model || !ent->model->TraceBox)
145                         continue;
146                 //if (((ent->model->type != mod_brushq1 && ent->model->type != mod_brushq2 && ent->model->type != mod_brushq3) || ent->alpha < 1) && !(cl_entities[n].state_current.effects & EF_SELECTABLE))
147                 //      continue;
148                 if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
149                  || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
150                  || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
151                         continue;
152
153                 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
154                 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
155
156                 if (ent->model && ent->model->TraceBox)
157                         ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
158
159                 cl_traceline_startsupercontents |= trace.startsupercontents;
160                 if (maxrealfrac > trace.realfraction)
161                 {
162                         if (hitent)
163                                 *hitent = n;
164                         maxfrac = trace.fraction;
165                         maxrealfrac = trace.realfraction;
166                         if (normal)
167                         {
168                                 VectorCopy(trace.plane.normal, tempnormal);
169                                 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
170                         }
171                 }
172         }
173         if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
174         if (impact)
175                 VectorLerp(start, maxfrac, end, impact);
176         return maxfrac;
177 }
178
179 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
180 {
181         // FIXME: check multiple brush models
182         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
183                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
184 }
185
186 int CL_PointQ1Contents(const vec3_t p)
187 {
188         CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
189         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, cl_traceline_startsupercontents);
190         /*
191         // FIXME: check multiple brush models
192         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
193                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
194         return 0;
195         */
196 }
197
198 int CL_PointSuperContents(const vec3_t p)
199 {
200         CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
201         return cl_traceline_startsupercontents;
202         /*
203         // FIXME: check multiple brush models
204         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
205                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
206         return 0;
207         */
208 }
209