added in_pitch_min and in_pitch_max cvars to limit pitch (default: -90 to +90)
[divverent/darkplaces.git] / chase.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // chase.c -- chase camera code
21
22 #include "quakedef.h"
23
24 cvar_t chase_back = {CVAR_SAVE, "chase_back", "48"};
25 cvar_t chase_up = {CVAR_SAVE, "chase_up", "24"};
26 cvar_t chase_active = {CVAR_SAVE, "chase_active", "0"};
27
28 void Chase_Init (void)
29 {
30         Cvar_RegisterVariable (&chase_back);
31         Cvar_RegisterVariable (&chase_up);
32         Cvar_RegisterVariable (&chase_active);
33 }
34
35 void Chase_Reset (void)
36 {
37         // for respawning and teleporting
38 //      start position 12 units behind head
39 }
40
41 int traceline_endcontents;
42
43 static entity_render_t *traceline_entity[MAX_EDICTS];
44 static int traceline_entities;
45
46 // builds list of entities for TraceLine to check later
47 void TraceLine_ScanForBModels(void)
48 {
49         int i;
50         entity_render_t *ent;
51         model_t *model;
52         traceline_entities = 0;
53         for (i = 1;i < MAX_EDICTS;i++)
54         {
55                 ent = &cl_entities[i].render;
56                 model = ent->model;
57                 // look for embedded brush models only
58                 if (model && model->name[0] == '*')
59                 {
60                         // this does nothing for * models currently...
61                         //Mod_CheckLoaded(model);
62                         if (model->type == mod_brush)
63                         {
64                                 traceline_entity[traceline_entities++] = ent;
65                                 if (ent->angles[0] || ent->angles[2])
66                                 {
67                                         // pitch or roll
68                                         VectorAdd(ent->origin, model->rotatedmins, ent->mins);
69                                         VectorAdd(ent->origin, model->rotatedmaxs, ent->maxs);
70                                 }
71                                 else if (ent->angles[1])
72                                 {
73                                         // yaw
74                                         VectorAdd(ent->origin, model->yawmins, ent->mins);
75                                         VectorAdd(ent->origin, model->yawmaxs, ent->maxs);
76                                 }
77                                 else
78                                 {
79                                         VectorAdd(ent->origin, model->normalmins, ent->mins);
80                                         VectorAdd(ent->origin, model->normalmaxs, ent->maxs);
81                                 }
82                         }
83                 }
84         }
85 }
86
87 float TraceLine (vec3_t start, vec3_t end, vec3_t impact, vec3_t normal, int contents, int hitbmodels)
88 {
89         double maxfrac, startd[3], endd[3];
90         trace_t trace;
91
92 // FIXME: broken, fix it
93 //      if (impact == NULL && normal == NULL && contents == 0)
94 //              return SV_TestLine (cl.worldmodel->hulls, 0, start, end);
95
96         VectorCopy(start, startd);
97         VectorCopy(end, endd);
98
99         Mod_CheckLoaded(cl.worldmodel);
100         memset (&trace, 0, sizeof(trace));
101         VectorCopy (endd, trace.endpos);
102         trace.fraction = 1;
103         trace.startcontents = contents;
104         VectorCopy(startd, RecursiveHullCheckInfo.start);
105         VectorSubtract(endd, startd, RecursiveHullCheckInfo.dist);
106         RecursiveHullCheckInfo.hull = cl.worldmodel->hulls;
107         RecursiveHullCheckInfo.trace = &trace;
108         SV_RecursiveHullCheck (0, 0, 1, startd, endd);
109         if (impact)
110                 VectorCopy (trace.endpos, impact);
111         if (normal)
112                 VectorCopy (trace.plane.normal, normal);
113         traceline_endcontents = trace.endcontents;
114         maxfrac = trace.fraction;
115
116         if (hitbmodels && traceline_entities)
117         {
118                 int n;
119                 entity_render_t *ent;
120                 double start2[3], end2[3], tracemins[3], tracemaxs[3];
121                 tracemins[0] = min(start[0], end[0]);
122                 tracemaxs[0] = max(start[0], end[0]);
123                 tracemins[1] = min(start[1], end[1]);
124                 tracemaxs[1] = max(start[1], end[1]);
125                 tracemins[2] = min(start[2], end[2]);
126                 tracemaxs[2] = max(start[2], end[2]);
127
128                 // look for embedded bmodels
129                 for (n = 0;n < traceline_entities;n++)
130                 {
131                         ent = traceline_entity[n];
132                         if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
133                          || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
134                          || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
135                                 continue;
136
137                         softwaretransformforentity(ent);
138                         softwareuntransform(start, start2);
139                         softwareuntransform(end, end2);
140
141                         memset (&trace, 0, sizeof(trace));
142                         VectorCopy (end2, trace.endpos);
143                         trace.fraction = 1;
144                         trace.startcontents = contents;
145                         VectorCopy(start2, RecursiveHullCheckInfo.start);
146                         VectorSubtract(end2, start2, RecursiveHullCheckInfo.dist);
147                         RecursiveHullCheckInfo.hull = ent->model->hulls;
148                         RecursiveHullCheckInfo.trace = &trace;
149                         SV_RecursiveHullCheck (ent->model->hulls->firstclipnode, 0, 1, start2, end2);
150
151                         if (trace.allsolid || trace.startsolid || trace.fraction < maxfrac)
152                         {
153                                 maxfrac = trace.fraction;
154                                 if (impact)
155                                 {
156                                         softwaretransform(trace.endpos, impact);
157                                 }
158                                 if (normal)
159                                 {
160                                         softwaretransformdirection(trace.plane.normal, normal);
161                                 }
162                                 traceline_endcontents = trace.endcontents;
163                         }
164                 }
165         }
166         return maxfrac;
167 }
168
169 void Chase_Update (void)
170 {
171         vec3_t  forward, stop, chase_dest, normal;
172         float   dist;
173
174         chase_back.value = bound(0, chase_back.value, 128);
175         chase_up.value = bound(-48, chase_up.value, 96);
176
177         AngleVectors (cl.viewangles, forward, NULL, NULL);
178
179         dist = -chase_back.value - 8;
180         chase_dest[0] = r_refdef.vieworg[0] + forward[0] * dist;
181         chase_dest[1] = r_refdef.vieworg[1] + forward[1] * dist;
182         chase_dest[2] = r_refdef.vieworg[2] + forward[2] * dist + chase_up.value;
183
184         TraceLine (r_refdef.vieworg, chase_dest, stop, normal, 0, true);
185         chase_dest[0] = stop[0] + forward[0] * 8 + normal[0] * 4;
186         chase_dest[1] = stop[1] + forward[1] * 8 + normal[1] * 4;
187         chase_dest[2] = stop[2] + forward[2] * 8 + normal[2] * 4;
188
189         VectorCopy (chase_dest, r_refdef.vieworg);
190 }
191