Merge branch 'terencehill/spectate_player' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / physics.qc
1 #include "physics.qh"
2
3 #include <client/autocvars.qh>
4 #include <client/defs.qh>
5 #include <client/miscfunctions.qh>
6 #include <client/main.qh>
7 #include <common/mapinfo.qh>
8 #include <lib/csqcmodel/cl_player.qh>
9
10 // Physics (#15)
11
12 vector acc_prevspeed;
13 float acc_prevtime, acc_avg, top_speed, top_speed_time;
14 float physics_update_time, discrete_speed, discrete_acceleration;
15 void HUD_Physics()
16 {
17         if(!autocvar__hud_configure)
18         {
19                 if(!autocvar_hud_panel_physics) return;
20                 if(spectatee_status == -1 && (autocvar_hud_panel_physics == 1 || autocvar_hud_panel_physics == 3)) return;
21                 if(autocvar_hud_panel_physics == 3 && !(ISGAMETYPE(RACE) || ISGAMETYPE(CTS))) return;
22         }
23
24         HUD_Panel_LoadCvars();
25
26         draw_beginBoldFont();
27
28         if (autocvar_hud_panel_physics_dynamichud)
29                 HUD_Scale_Enable();
30         else
31                 HUD_Scale_Disable();
32         HUD_Panel_DrawBg();
33         if(panel_bg_padding)
34         {
35                 panel_pos += '1 1 0' * panel_bg_padding;
36                 panel_size -= '2 2 0' * panel_bg_padding;
37         }
38
39         float acceleration_progressbar_scale = 0;
40         if(autocvar_hud_panel_physics_progressbar && autocvar_hud_panel_physics_acceleration_progressbar_scale > 1)
41                 acceleration_progressbar_scale = autocvar_hud_panel_physics_acceleration_progressbar_scale;
42
43         float text_scale;
44         if (autocvar_hud_panel_physics_text_scale <= 0)
45                 text_scale = 1;
46         else
47                 text_scale = min(autocvar_hud_panel_physics_text_scale, 1);
48
49         //compute speed
50         float speed, conversion_factor = GetSpeedUnitFactor(autocvar_hud_panel_physics_speed_unit);
51         string unit = GetSpeedUnit(autocvar_hud_panel_physics_speed_unit);
52         vector vel = (csqcplayer ? csqcplayer.velocity : pmove_vel);
53
54         float max_speed = floor( autocvar_hud_panel_physics_speed_max * conversion_factor + 0.5 );
55         if (autocvar__hud_configure)
56                 speed = floor( max_speed * 0.65 + 0.5 );
57         else if(autocvar_hud_panel_physics_speed_vertical)
58                 speed = floor( vlen(vel) * conversion_factor + 0.5 );
59         else
60                 speed = floor( vlen(vel - vel.z * '0 0 1') * conversion_factor + 0.5 );
61
62         //compute acceleration
63         float acceleration, f;
64         if (autocvar__hud_configure)
65                 acceleration = autocvar_hud_panel_physics_acceleration_max * 0.3;
66         else
67         {
68                 // 1 m/s = 0.0254 qu/s; 1 g = 9.80665 m/s^2
69                 f = time - acc_prevtime;
70                 if(autocvar_hud_panel_physics_acceleration_vertical)
71                         acceleration = (vlen(vel) - vlen(acc_prevspeed));
72                 else
73                         acceleration = (vlen(vel - '0 0 1' * vel.z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed.z));
74
75                 acceleration = acceleration * (1 / max(0.0001, f)) * (0.0254 / 9.80665);
76
77                 acc_prevspeed = vel;
78                 acc_prevtime = time;
79
80                 if(autocvar_hud_panel_physics_acceleration_movingaverage)
81                 {
82                         f = bound(0, f * 10, 1);
83                         acc_avg = acc_avg * (1 - f) + acceleration * f;
84                         acceleration = acc_avg;
85                 }
86         }
87
88         const int acc_decimals = 2;
89         if(time > physics_update_time)
90         {
91                 discrete_acceleration = acceleration;
92                 // workaround for ftos_decimals returning a negative 0
93                 if(discrete_acceleration > -1 / (10 ** acc_decimals) && discrete_acceleration < 0)
94                         discrete_acceleration = 0;
95                 discrete_speed = speed;
96                 physics_update_time += autocvar_hud_panel_physics_update_interval;
97                 if(physics_update_time < time)
98                         physics_update_time = time + autocvar_hud_panel_physics_update_interval;
99         }
100
101         //compute layout
102         float panel_ar = panel_size.x/panel_size.y;
103         vector speed_offset = '0 0 0', acceleration_offset = '0 0 0';
104         if (panel_ar >= 5 && !acceleration_progressbar_scale)
105         {
106                 panel_size.x *= 0.5;
107                 if (autocvar_hud_panel_physics_flip)
108                         speed_offset.x = panel_size.x;
109                 else
110                         acceleration_offset.x = panel_size.x;
111         }
112         else
113         {
114                 panel_size.y *= 0.5;
115                 if (autocvar_hud_panel_physics_flip)
116                         speed_offset.y = panel_size.y;
117                 else
118                         acceleration_offset.y = panel_size.y;
119         }
120         int speed_baralign, acceleration_baralign;
121         if (autocvar_hud_panel_physics_baralign == 1)
122                 acceleration_baralign = speed_baralign = 1;
123     else if(autocvar_hud_panel_physics_baralign == 4)
124                 acceleration_baralign = speed_baralign = 2;
125         else if (autocvar_hud_panel_physics_flip)
126         {
127                 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 2);
128                 speed_baralign = (autocvar_hud_panel_physics_baralign == 3);
129         }
130         else
131         {
132                 speed_baralign = (autocvar_hud_panel_physics_baralign == 2);
133                 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 3);
134         }
135         if (autocvar_hud_panel_physics_acceleration_progressbar_mode == 0)
136                 acceleration_baralign = 3; //override hud_panel_physics_baralign value for acceleration
137
138         //draw speed
139         if(speed)
140         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
141                 HUD_Panel_DrawProgressBar(panel_pos + speed_offset, panel_size, "progressbar", speed/max_speed, 0, speed_baralign, autocvar_hud_progressbar_speed_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
142         vector tmp_offset = '0 0 0', tmp_size = '0 0 0';
143         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
144         {
145                 tmp_size.x = panel_size.x * 0.75;
146                 tmp_size.y = panel_size.y * text_scale;
147                 if (speed_baralign)
148                         tmp_offset.x = panel_size.x - tmp_size.x;
149                 //else
150                         //tmp_offset_x = 0;
151                 tmp_offset.y = (panel_size.y - tmp_size.y) / 2;
152                 drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(discrete_speed), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
153
154                 //draw speed unit
155                 if (speed_baralign)
156                         tmp_offset.x = 0;
157                 else
158                         tmp_offset.x = tmp_size.x;
159                 if (autocvar_hud_panel_physics_speed_unit_show)
160                 {
161                         //tmp_offset_y = 0;
162                         tmp_size.x = panel_size.x * (1 - 0.75);
163                         tmp_size.y = panel_size.y * 0.4 * text_scale;
164                         tmp_offset.y = (panel_size.y * 0.4 - tmp_size.y) / 2;
165                         drawstring_aspect(panel_pos + speed_offset + tmp_offset, unit, tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
166                 }
167         }
168
169         //compute and draw top speed
170         if (autocvar_hud_panel_physics_topspeed)
171         if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
172         {
173                 if (autocvar__hud_configure)
174                 {
175                         top_speed = floor( max_speed * 0.75 + 0.5 );
176                         f = 1;
177                 }
178                 else
179                 {
180                         if (speed >= top_speed)
181                         {
182                                 top_speed = speed;
183                                 top_speed_time = time;
184                         }
185                         if (top_speed != 0)
186                         {
187                                 f = max(1, autocvar_hud_panel_physics_topspeed_time);
188                                 // divide by f to make it start from 1
189                                 f = cos( ((time - top_speed_time) / f) * PI/2 );
190                         }
191             else //hide top speed 0, it would be stupid
192                                 f = 0;
193                 }
194                 if (f > 0)
195                 {
196                         //top speed progressbar peak
197                         if(speed < top_speed)
198                         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
199                         {
200                                 float peak_offsetX;
201                                 vector peak_size = '0 0 0';
202                                 if (speed_baralign == 0)
203                                         peak_offsetX = min(top_speed, max_speed)/max_speed * panel_size.x;
204                 else if (speed_baralign == 1)
205                                         peak_offsetX = (1 - min(top_speed, max_speed)/max_speed) * panel_size.x;
206                 else // if (speed_baralign == 2)
207                     peak_offsetX = min(top_speed, max_speed)/max_speed * panel_size.x * 0.5;
208                                 peak_size.x = floor(panel_size.x * 0.01 + 1.5);
209                 peak_size.y = panel_size.y;
210                 if (speed_baralign == 2) // draw two peaks, on both sides
211                 {
212                     drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size.x + peak_offsetX - peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
213                     drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size.x - peak_offsetX + peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
214                 }
215                 else
216                     drawfill(panel_pos + speed_offset + eX * (peak_offsetX - peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
217                         }
218
219                         //top speed
220                         tmp_offset.y = panel_size.y * 0.4;
221                         tmp_size.x = panel_size.x * (1 - 0.75);
222                         tmp_size.y = (panel_size.y - tmp_offset.y) * text_scale;
223                         tmp_offset.y += (panel_size.y - tmp_offset.y - tmp_size.y) / 2;
224                         drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(top_speed), tmp_size, '1 0 0', f * panel_fg_alpha, DRAWFLAG_NORMAL);
225                 }
226                 else
227                         top_speed = 0;
228         }
229
230         //draw acceleration
231         if(acceleration)
232         if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 3)
233         {
234                 vector progressbar_color;
235                 if(acceleration < 0)
236                         progressbar_color = autocvar_hud_progressbar_acceleration_neg_color;
237                 else
238                         progressbar_color = autocvar_hud_progressbar_acceleration_color;
239
240                 f = acceleration/autocvar_hud_panel_physics_acceleration_max;
241                 if (autocvar_hud_panel_physics_acceleration_progressbar_nonlinear)
242                         f = (f >= 0 ? sqrt(f) : -sqrt(-f));
243
244                 if (acceleration_progressbar_scale) // allow progressbar to go out of panel bounds
245                 {
246                         tmp_size = acceleration_progressbar_scale * panel_size.x * eX + panel_size.y * eY;
247
248                         if (acceleration_baralign == 1)
249                                 tmp_offset.x = panel_size.x - tmp_size.x;
250                         else if (acceleration_baralign == 2 || acceleration_baralign == 3)
251                                 tmp_offset.x = (panel_size.x - tmp_size.x) / 2;
252                         else
253                                 tmp_offset.x = 0;
254                         tmp_offset.y = 0;
255                 }
256                 else
257                 {
258                         tmp_size = panel_size;
259                         tmp_offset = '0 0 0';
260                 }
261
262                 HUD_Panel_DrawProgressBar(panel_pos + acceleration_offset + tmp_offset, tmp_size, "accelbar", f, 0, acceleration_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
263         }
264
265         if(autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 3)
266         {
267                 tmp_size.x = panel_size.x;
268                 tmp_size.y = panel_size.y * text_scale;
269                 tmp_offset.x = 0;
270                 tmp_offset.y = (panel_size.y - tmp_size.y) / 2;
271
272                 drawstring_aspect(panel_pos + acceleration_offset + tmp_offset, strcat(ftos_decimals(discrete_acceleration, acc_decimals), "g"), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
273         }
274
275         draw_endBoldFont();
276 }