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