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