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