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