Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / viewloc.qc
1 #include "util.qh"
2
3 #if defined(CSQC)
4         #include "../dpdefs/csprogsdefs.qh"
5     #include "../client/defs.qh"
6     #include "constants.qh"
7 #elif defined(MENUQC)
8 #elif defined(SVQC)
9         #include "../server/defs.qh"
10 #endif
11
12 // client movement
13 void viewloc_PlayerPhysics()
14 {
15         if(self.viewloc)
16         {
17                 vector oldmovement = self.movement;
18                 self.movement_x = oldmovement_y;
19                 self.movement_y = 0;
20
21                 if(self.movement_x < 0)
22                         self.movement_x = -self.movement_x;
23
24                 vector level_start, level_end;
25                 level_start = self.viewloc.enemy.origin;
26                 level_end = self.viewloc.goalentity.origin;
27                 vector forward, backward;
28                 forward = vectoangles(normalize(level_end - level_start));
29                 backward = vectoangles(normalize(level_start - level_end));
30
31                 if(self.movement_x < 0) // left
32                         self.angles_y = backward_y;
33                 if(self.movement_x > 0) // right
34                         self.angles_y = forward_y;
35
36                 if(oldmovement_x > 0)
37 #ifdef CSQC
38                         input_angles_x =
39 #endif
40                         self.v_angle_x = self.angles_x = -50;
41                 else if(oldmovement_x < 0)
42 #ifdef CSQC
43                         input_angles_x =
44 #endif
45                         self.v_angle_x = self.angles_x = 50;
46
47                 //if(!PHYS_INPUT_BUTTON_CROUCH(self) && !IS_DUCKED(self))
48 #ifdef SVQC
49                         //self.BUTTON_CROUCH = (oldmovement_x < 0);
50                         if(oldmovement_x < 0)
51                                 self.BUTTON_CROUCH = true;
52 #elif defined(CSQC)
53                         if(oldmovement_x < 0) { input_buttons |= 16; self.flags |= FL_DUCKED; } //else { input_buttons &= ~16; self.flags &= ~FL_DUCKED; }
54 #endif
55         }
56 }
57
58 #ifdef CSQC
59
60 void viewloc_SetTags()
61 {
62         if(self.viewloc && wasfreed(self.viewloc))
63                 self.viewloc = world;
64
65         if(self.viewloc.entnum != self.tag_networkviewloc)
66         if(self.tag_networkviewloc == 0)
67                 self.viewloc = world;
68         else
69                 self.viewloc = findfloat(world, entnum, self.tag_networkviewloc);
70 }
71
72 vector old_camera_angle = '0 0 0';
73 void viewloc_SetViewLocation()
74 {
75         entity view = CSQCModel_server2csqc(player_localentnum);
76         if(!view) { return; }
77         //NOTE: the "cam_" cvars sould probably be changed out with a spawnflag or an entity key. I have it like this for my testing -- Player_2
78         if(view.viewloc && !wasfreed(view.viewloc) && view.viewloc.enemy && view.viewloc.goalentity)
79         {
80                 vector position_a, position_b, camera_position, camera_angle, forward, backward;
81                 //vector scratch;
82
83                 position_a = view.viewloc.enemy.origin;
84                 position_b = view.viewloc.goalentity.origin;
85
86 #if 0
87                 /*TODO: have the camera only move when a player moves too much from the center of the camera
88                  * basically the player can move around in a "box" in the center of th screen with out changing the camera position or angles
89                 */
90                 if (cvar("cam_box")) {
91                         camera_position = vec_bounds_in(view.origin, position_a, position_b);
92                 }
93                 else
94 #endif
95                         camera_position = vec_bounds_in(view.origin, position_a, position_b);
96
97
98                 camera_angle = '0 0 0';
99
100                 // a tracking camera follows the player when it leaves the world box
101                 if (cvar("cam_track")) {
102                         camera_angle = aim_vec (camera_position, view.origin);
103                 }
104
105                 // hard snap changes the angle as soon as it crosses over the nearest 90 degree mark
106                 if (cvar("cam_snap_hard")){
107                         camera_angle = angle_snap_vec(aim_vec(camera_position, view.origin), 90);
108                 }
109
110                 // tries to avoid snapping unless it *really* needs to
111                 if (cvar("cam_snap_close")){
112
113                         // like hard snap, but don't snap angles yet.
114                         camera_angle = aim_vec(camera_position, view.origin);
115
116                         /* if the difference between the old and new angle is 60 degrees or more, switch angles.
117                          * NOTE: bug/feature: this will use non-snaped angles for one frame.
118                          * doing this resualts in less code, faster code, and a smoother transisition between angles.
119                          */
120                         float camera_angle_diff = max(camera_angle_y, old_camera_angle_y) - min(camera_angle_y, old_camera_angle_y);
121
122                         if ( camera_angle_diff >= 60)
123                                 old_camera_angle_y = angle_snap_f(camera_angle_y, 90);
124                         else
125                                 camera_angle_y = old_camera_angle_y;
126                 }
127
128                 //unlocking this allows the camera to look up and down. this also allows a top-down view.
129                 if (!cvar("cam_snap_unlock")) {
130                         camera_angle_x = 0;
131                         camera_angle_z = 0;
132                 }
133
134 #if 0
135                 LOG_TRACE(vtos(camera_position), "\n");
136                 LOG_TRACE(vtos(old_camera_angle), "\n");
137                 LOG_TRACE(vtos(camera_angle), "\n");
138 #endif
139
140                 freeze_org = getpropertyvec(VF_ORIGIN);
141                 freeze_ang = getpropertyvec(VF_ANGLES);
142                 setproperty(VF_ORIGIN, camera_position);
143                 setproperty(VF_ANGLES, camera_angle);
144
145                 forward = vectoangles(normalize(vec_to_min(position_b, position_a) - vec_to_max(position_b, position_a)));
146                 backward = vectoangles(normalize(vec_to_max(position_b, position_a) - vec_to_min(position_b, position_a)));
147
148                 if(input_movevalues_y < 0) // left
149                         view.angles_y = backward_y;
150                 if(input_movevalues_y > 0) // favour right
151                         view.angles_y = forward_y;
152
153                 setproperty(VF_CL_VIEWANGLES, view.angles);
154         }
155 }
156
157 #endif