]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/strafehud.qc
Transifex autosync
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / strafehud.qc
1 // Author: Juhu
2
3 #include "strafehud.qh"
4
5 #include <client/draw.qh>
6 #include <lib/csqcmodel/cl_player.qh>
7 #include <common/physics/player.qh>
8 #include <common/physics/movetypes/movetypes.qh>
9
10 // non-essential
11 #include <client/view.qh> // for v_flipped state
12
13 // non-local players
14 #include <common/animdecide.qh> // anim_implicit_state
15 #include <common/ent_cs.qh> // CSQCModel_server2csqc()
16
17 // start speed
18 #include <client/hud/panel/racetimer.qh> // checkpoint information (race_*)
19
20 // jump height
21 #include <lib/csqcmodel/common.qh> // for IS_PLAYER() macro
22 #include <common/resources/cl_resources.qh> // IS_DEAD() macro
23
24 // StrafeHUD (#25)
25
26 void HUD_StrafeHUD_Export(int fh)
27 {
28         // allow saving cvars that aesthetically change the panel into hud skin files
29 }
30
31 float GeomLerp(float a, float _lerp, float b); // declare GeomLerp here since there's no header file for it
32
33 void HUD_StrafeHUD()
34 {
35         static float hud_lasttime = 0;
36         entity strafeplayer;
37         bool islocal;
38
39         // generic hud routines
40         if(!autocvar__hud_configure)
41         {
42                 if(!autocvar_hud_panel_strafehud ||
43                    (spectatee_status == -1 && (autocvar_hud_panel_strafehud == 1 || autocvar_hud_panel_strafehud == 3)) ||
44                    (autocvar_hud_panel_strafehud == 3 && !MUTATOR_CALLHOOK(HUD_StrafeHUD_showoptional))) { hud_lasttime = time; return; }
45         }
46
47         HUD_Panel_LoadCvars();
48
49         if(autocvar_hud_panel_strafehud_dynamichud)
50                 HUD_Scale_Enable();
51         else
52                 HUD_Scale_Disable();
53
54         HUD_Panel_DrawBg();
55
56         if(panel_bg_padding)
57         {
58                 panel_pos  += '1 1 0' * panel_bg_padding;
59                 panel_size -= '2 2 0' * panel_bg_padding;
60         }
61
62         // find out whether the local csqcmodel entity is valid
63         if(spectatee_status > 0 || isdemo())
64         {
65                 islocal = false;
66                 strafeplayer = CSQCModel_server2csqc(player_localentnum - 1);
67         }
68         else
69         {
70                 islocal = true;
71                 strafeplayer = csqcplayer;
72         }
73
74         // draw strafehud
75         if(csqcplayer && strafeplayer)
76         {
77                 float strafe_waterlevel;
78
79                 // check the player waterlevel without affecting the player entity, this way we can fetch waterlevel even if client prediction is disabled
80                 {
81                         // store old values
82                         void old_contentstransition(int, int) = strafeplayer.contentstransition;
83                         float old_watertype = strafeplayer.watertype;
84                         float old_waterlevel = strafeplayer.waterlevel;
85
86                         strafeplayer.contentstransition = func_null; // unset the contentstransition function if present
87                         _Movetype_CheckWater(strafeplayer);
88                         strafe_waterlevel = strafeplayer.waterlevel; // store the player waterlevel
89
90                         // restore old values
91                         strafeplayer.contentstransition = old_contentstransition;
92                         strafeplayer.watertype = old_watertype;
93                         strafeplayer.waterlevel = old_waterlevel;
94                 }
95
96                 int keys = STAT(PRESSED_KEYS);
97                 // try to ignore if track_canjump is enabled, doesn't work in spectator mode if spectated player uses +jetpack or cl_movement_track_canjump
98                 bool jumpheld = false;
99                 if(islocal)
100                 {
101                         if((PHYS_INPUT_BUTTON_JUMP(strafeplayer) || PHYS_INPUT_BUTTON_JETPACK(strafeplayer)) && !PHYS_CL_TRACK_CANJUMP(strafeplayer))
102                                 jumpheld = true;
103                 }
104                 else
105                 {
106                         if((keys & KEY_JUMP) && !PHYS_TRACK_CANJUMP(strafeplayer))
107                                 jumpheld = true;
108                 }
109
110                 // persistent
111                 static float onground_lasttime       = 0;
112                 static bool  onslick_last            = false;
113                 static float turn_lasttime           = 0;
114                 static bool  turn                    = false;
115                 static float turnangle;
116                 static float dt_update               = 0;
117                 static int   dt_time                 = 0;
118                 static float dt_sum                  = 0;
119                 static float dt                      = 0;
120
121                 // physics
122                 // doesn't get changed by ground timeout and isn't affected by jump input
123                 bool   real_onground                 = islocal ? IS_ONGROUND(strafeplayer) : !(strafeplayer.anim_implicit_state & ANIMIMPLICITSTATE_INAIR);
124                 // doesn't get changed by ground timeout
125                 bool   real_onslick                  = false;
126                 // if jump is held assume we are in air, avoids flickering of the hud when hitting the ground
127                 bool   onground                      = real_onground && !jumpheld;
128                 bool   onslick                       = real_onslick;
129                 bool   onground_expired;
130                 bool   strafekeys;
131                 // the hud will not work well while swimming
132                 bool   swimming                      = strafe_waterlevel >= WATERLEVEL_SWIMMING;
133                 // use local csqcmodel entity for this even when spectating, flickers too much otherwise
134                 float  speed                         = !autocvar__hud_configure ? vlen(vec2(csqcplayer.velocity)) : 1337;
135                 // only the local csqcplayer entity contains this information even when spectating
136                 float  maxspeed_mod                  = IS_DUCKED(csqcplayer) ? .5 : 1;
137                 float  maxspeed_phys                 = onground ? PHYS_MAXSPEED(strafeplayer) : PHYS_MAXAIRSPEED(strafeplayer);
138                 float  maxspeed                      = !autocvar__hud_configure ? maxspeed_phys * maxspeed_mod : 320;
139                 float  movespeed;
140                 float  bestspeed;
141                 float  maxaccel_phys                 = onground ? PHYS_ACCELERATE(strafeplayer) : PHYS_AIRACCELERATE(strafeplayer);
142                 float  maxaccel                      = !autocvar__hud_configure ? maxaccel_phys : 1;
143                 // change the range from 0° - 360° to -180° - 180° to match how view_angle represents angles
144                 float  vel_angle                     = vectoangles(strafeplayer.velocity).y - (vectoangles(strafeplayer.velocity).y > 180 ? 360 : 0);
145                 float  view_angle                    = PHYS_INPUT_ANGLES(strafeplayer).y;
146                 float  angle;
147                 vector movement                      = PHYS_INPUT_MOVEVALUES(strafeplayer);
148                 bool   fwd;
149                 int    keys_fwd;
150                 float  wishangle;
151                 int    direction;
152
153                 // HUD
154                 int    mode;
155                 float  speed_conversion_factor       = GetSpeedUnitFactor(autocvar_hud_speed_unit);
156                 float  length_conversion_factor      = GetLengthUnitFactor(autocvar_hud_speed_unit);
157                 // use more decimals when displaying km or miles
158                 int    length_decimals               = autocvar_hud_speed_unit >= 3 && autocvar_hud_speed_unit <= 5 ? 6 : 2;
159                 float  antiflicker_angle             = bound(0, autocvar_hud_panel_strafehud_antiflicker_angle, 180);
160                 float  minspeed;
161                 float  shift_offset                  = 0;
162                 bool   straight_overturn             = false;
163                 bool   immobile                      = speed <= 0;
164                 float  hudangle;
165                 float  hidden_width;
166                 float  neutral_offset;
167                 float  neutral_width;
168                 vector currentangle_color            = autocvar_hud_panel_strafehud_angle_neutral_color;
169                 float  currentangle_offset;
170                 vector currentangle_size;
171                 float  bestangle;
172                 float  prebestangle;
173                 float  odd_bestangle;
174                 float  bestangle_offset;
175                 float  switch_bestangle_offset;
176                 bool   odd_angles                    = false;
177                 float  odd_bestangle_offset          = 0;
178                 float  switch_odd_bestangle_offset   = 0;
179                 float  bestangle_width;
180                 float  accelzone_left_offset;
181                 float  accelzone_right_offset;
182                 float  accelzone_width;
183                 float  preaccelzone_left_offset;
184                 float  preaccelzone_right_offset;
185                 float  preaccelzone_width;
186                 float  overturn_offset;
187                 float  overturn_width;
188                 float  slickdetector_height;
189                 vector direction_size_vertical;
190                 vector direction_size_horizontal;
191                 float  range_minangle;
192                 float  text_offset_top               = 0;
193                 float  text_offset_bottom            = 0;
194
195                 // real_* variables which are always positive with no wishangle offset
196                 float real_bestangle;
197                 float real_prebestangle;
198
199                 if(autocvar_hud_panel_strafehud_mode >= 0 && autocvar_hud_panel_strafehud_mode <= 1)
200                         mode = autocvar_hud_panel_strafehud_mode;
201                 else
202                         mode = STRAFEHUD_MODE_VIEW_CENTERED;
203
204                 // there's only one size cvar for the arrows, they will always have a 45° angle to ensure proper rendering without antialiasing
205                 float arrow_size = max(panel_size.y * min(autocvar_hud_panel_strafehud_angle_arrow_size, 10), 0);
206
207                 if(onground)
208                 {
209                         if(PHYS_FRICTION(strafeplayer) == 0)
210                         {
211                                 onslick = true;
212                         }
213                         else // don't use IS_ONSLICK(), it only works for the local player and only if client prediction is enabled
214                         {
215                                 trace_dphitq3surfaceflags = 0;
216                                 tracebox(strafeplayer.origin, strafeplayer.mins, strafeplayer.maxs, strafeplayer.origin - '0 0 1', MOVE_NOMONSTERS, strafeplayer);
217                                 onslick = trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK;
218                         }
219                         real_onslick = onslick;
220
221                         onground_lasttime = time;
222                         onslick_last = onslick;
223                 }
224                 else if(jumpheld || swimming)
225                 {
226                         onground_lasttime = 0;
227                 }
228
229                 if(onground_lasttime == 0)
230                         onground_expired = true;
231                 else
232                         onground_expired = (time - onground_lasttime) >= autocvar_hud_panel_strafehud_timeout_ground; // timeout for slick ramps
233
234                 if(!onground && !onground_expired) // if ground timeout hasn't expired yet use ground physics
235                 {
236                         onground = true;
237                         onslick = onslick_last;
238
239                         if(!autocvar__hud_configure)
240                         {
241                                 maxspeed = PHYS_MAXSPEED(strafeplayer) * maxspeed_mod;
242                                 maxaccel = PHYS_ACCELERATE(strafeplayer);
243                         }
244                 }
245
246                 movespeed = vlen(vec2(movement));
247                 if(movespeed == 0)
248                         movespeed = maxspeed;
249                 else
250                         movespeed = min(movespeed, maxspeed);
251
252                 if(!autocvar_hud_panel_strafehud_uncapped)
253                         arrow_size = max(arrow_size, 1);
254
255                 // determine frametime
256                 if((csqcplayer_status == CSQCPLAYERSTATUS_PREDICTED) && (input_timelength > 0))
257                 {
258                         float dt_client = input_timelength;
259
260                         if(dt_client > .05) // server splits frames longer than 50 ms into two moves
261                                 dt_client /= 2; // doesn't ensure frames are smaller than 50 ms, just splits large frames in half, matches server behaviour
262
263                         // calculate average frametime
264                         dt_sum += dt_client * dt_client;
265                         dt_time += dt_client;
266
267                         if(((time - dt_update) > autocvar_hud_panel_strafehud_fps_update) || (dt_update == 0))
268                         {
269                                 dt = dt_sum / dt_time;
270                                 dt_update = time;
271                                 dt_time = dt_sum = 0;
272                         }
273                 }
274                 else // when spectating other players server ticrate will be used, this may not be accurate but there is no way to find other player's frametime
275                 {
276                         dt = ticrate;
277                         dt_update = dt_time = dt_sum = 0;
278                 }
279
280                 // determine whether the player is pressing forwards or backwards keys
281                 if(islocal) // if entity is local player
282                 {
283                         if(movement.x > 0)
284                                 keys_fwd = STRAFEHUD_KEYS_FORWARD;
285                         else if(movement.x < 0)
286                                 keys_fwd = STRAFEHUD_KEYS_BACKWARD;
287                         else
288                                 keys_fwd = STRAFEHUD_KEYS_NONE;
289                 }
290                 else // alternatively determine direction by querying pressed keys
291                 {
292                         if((keys & KEY_FORWARD) && !(keys & KEY_BACKWARD))
293                                 keys_fwd = STRAFEHUD_KEYS_FORWARD;
294                         else if(!(keys & KEY_FORWARD) && (keys & KEY_BACKWARD))
295                                 keys_fwd = STRAFEHUD_KEYS_BACKWARD;
296                         else
297                                 keys_fwd = STRAFEHUD_KEYS_NONE;
298                 }
299
300                 // determine player wishdir
301                 if(islocal) // if entity is local player
302                 {
303                         if(movement.x == 0)
304                         {
305                                 if(movement.y < 0)
306                                         wishangle = -90;
307                                 else if(movement.y > 0)
308                                         wishangle = 90;
309                                 else
310                                         wishangle = 0;
311                         }
312                         else
313                         {
314                                 if(movement.y == 0)
315                                 {
316                                         wishangle = 0;
317                                 }
318                                 else
319                                 {
320                                         wishangle = RAD2DEG * atan2(movement.y, movement.x);
321                                         // wrap the wish angle if it exceeds ±90°
322                                         if(fabs(wishangle) > 90)
323                                         {
324                                                 if(wishangle < 0)
325                                                         wishangle += 180;
326                                                 else
327                                                         wishangle -= 180;
328
329                                                 wishangle *= -1;
330                                         }
331                                 }
332                         }
333                 }
334                 else // alternatively calculate wishdir by querying pressed keys
335                 {
336                         if((keys & KEY_FORWARD) || (keys & KEY_BACKWARD))
337                                 wishangle = 45;
338                         else
339                                 wishangle = 90;
340                         if(keys & KEY_LEFT)
341                                 wishangle *= -1;
342                         else if(!(keys & KEY_RIGHT))
343                                 wishangle = 0; // wraps at 180°
344                 }
345
346                 strafekeys = fabs(wishangle) > 45;
347
348                 // determine minimum required angle to display full strafe range
349                 range_minangle = fabs(wishangle) % 90; // maximum range is 90 degree
350                 if(range_minangle > 45) range_minangle = 45 - fabs(wishangle) % 45; // minimum angle range is 45
351                 range_minangle = 90 - range_minangle; // calculate value which is never >90 or <45
352                 range_minangle *= 2; // multiply to accommodate for both sides of the hud
353
354                 if(autocvar_hud_panel_strafehud_range == 0)
355                 {
356                         if(autocvar__hud_configure)
357                                 hudangle = 90;
358                         else
359                                 hudangle = range_minangle; // use minimum angle required if dynamically setting hud angle
360                 }
361                 else
362                 {
363                         hudangle = bound(0, fabs(autocvar_hud_panel_strafehud_range), 360); // limit HUD range to 360 degrees, higher values don't make sense
364                 }
365
366                 // detect air strafe turning
367                 if((!strafekeys && vlen(vec2(movement)) > 0) || onground || autocvar__hud_configure)
368                 {
369                         turn = false;
370                 }
371                 else // air strafe only
372                 {
373                         bool turn_expired = (time - turn_lasttime) >= autocvar_hud_panel_strafehud_timeout_turn; // timeout for jumping with strafe keys only
374
375                         if(strafekeys)
376                                 turn = true;
377                         else if(turn_expired)
378                                 turn = false;
379
380                         if(turn) // CPMA turning
381                         {
382                                 if(strafekeys)
383                                 {
384                                         turn_lasttime = time;
385                                         turnangle = wishangle;
386                                 }
387                                 else // retain last state until strafe turning times out
388                                 {
389                                         wishangle = turnangle;
390                                 }
391
392                                 // calculate the maximum air strafe speed and acceleration
393                                 float strafity = 1 - (90 - fabs(wishangle)) / 45;
394
395                                 if(PHYS_MAXAIRSTRAFESPEED(strafeplayer) != 0)
396                                         maxspeed = min(maxspeed, GeomLerp(PHYS_MAXAIRSPEED(strafeplayer), strafity, PHYS_MAXAIRSTRAFESPEED(strafeplayer)));
397
398                                 movespeed = min(movespeed, maxspeed);
399
400                                 if(PHYS_AIRSTRAFEACCELERATE(strafeplayer) != 0)
401                                         maxaccel = GeomLerp(PHYS_AIRACCELERATE(strafeplayer), strafity, PHYS_AIRSTRAFEACCELERATE(strafeplayer));
402                         }
403                 }
404
405                 maxaccel *= dt * movespeed;
406                 bestspeed = max(movespeed - maxaccel, 0); // target speed to gain maximum acceleration
407
408                 float frictionspeed; // speed lost from friction
409                 float strafespeed; // speed minus friction
410
411                 if((speed > 0) && onground)
412                 {
413                         float strafefriction = onslick ? PHYS_FRICTION_SLICK(strafeplayer) : PHYS_FRICTION(strafeplayer);
414
415                         frictionspeed = speed * dt * strafefriction * max(PHYS_STOPSPEED(strafeplayer) / speed, 1);
416                         strafespeed = max(speed - frictionspeed, 0);
417                 }
418                 else
419                 {
420                         frictionspeed = 0;
421                         strafespeed = speed;
422                 }
423
424                 minspeed = autocvar_hud_panel_strafehud_switch_minspeed;
425                 if(minspeed < 0)
426                         minspeed = bestspeed + frictionspeed;
427
428                 // get current strafing angle ranging from -180° to +180°
429                 if(!autocvar__hud_configure)
430                 {
431                         if(speed > 0)
432                         {
433                                 // calculate view angle relative to the players current velocity direction
434                                 angle = vel_angle - view_angle;
435
436                                 // if the angle goes above 180° or below -180° wrap it to the opposite side since we want the interior angle
437                                 if(angle > 180)
438                                         angle -= 360;
439                                 else if(angle < -180)
440                                         angle += 360;
441
442                                 // determine whether the player is strafing forwards or backwards
443                                 // if the player isn't strafe turning use forwards/backwards keys to determine direction
444                                 if(fabs(wishangle) != 90)
445                                 {
446                                         if(keys_fwd == STRAFEHUD_KEYS_FORWARD)
447                                                 fwd = true;
448                                         else if(keys_fwd == STRAFEHUD_KEYS_BACKWARD)
449                                                 fwd = false;
450                                         else
451                                                 fwd = fabs(angle) <= 90;
452                                 }
453                                 // otherwise determine by examining the strafe angle
454                                 else
455                                 {
456                                         if(wishangle < 0) // detect direction using wishangle since the direction is not yet set
457                                                 fwd = angle <= -wishangle;
458                                         else
459                                                 fwd = angle >= -wishangle;
460                                 }
461
462                                 // shift the strafe angle by 180° when strafing backwards
463                                 if(!fwd)
464                                 {
465                                         if(angle < 0)
466                                                 angle += 180;
467                                         else
468                                                 angle -= 180;
469                                 }
470
471                                 // don't make the angle indicator switch side too much at ±180° if anti flicker is turned on
472                                 if(angle > (180 - antiflicker_angle) || angle < (-180 + antiflicker_angle))
473                                         straight_overturn = true;
474                         }
475                         else
476                         {
477                                 angle = 0;
478                                 fwd = true;
479                         }
480                 }
481                 else // simulate turning for HUD setup
482                 {
483                         const float demo_maxangle = 55; // maximum angle before changing direction
484                         const float demo_turnspeed = 40; // turning speed in degrees per second
485
486                         static float demo_position = -37 / demo_maxangle; // current positioning value between -1 and +1
487
488                         if(autocvar__hud_panel_strafehud_demo)
489                         {
490                                 float demo_dt = time - hud_lasttime;
491                                 float demo_step = (demo_turnspeed / demo_maxangle) * demo_dt;
492                                 demo_position = ((demo_position + demo_step) % 4 + 4) % 4;
493                         }
494
495                         // triangle wave function
496                         if(demo_position > 3)
497                                 angle = -1 + (demo_position - 3);
498                         else if(demo_position > 1)
499                                 angle = +1 - (demo_position - 1);
500                         else
501                                 angle = demo_position;
502                         angle *= demo_maxangle;
503
504                         fwd = true;
505                         wishangle = 45;
506                         if(angle < 0)
507                                 wishangle *= -1;
508                 }
509
510                 // invert the wish angle when strafing backwards
511                 if(!fwd)
512                         wishangle *= -1;
513
514                 // flip angles if v_flipped is enabled
515                 if(autocvar_v_flipped)
516                 {
517                         angle *= -1;
518                         wishangle *= -1;
519                 }
520
521                 // determine whether the player is strafing left or right
522                 if(wishangle > 0)
523                 {
524                         direction = STRAFEHUD_DIRECTION_RIGHT;
525                 }
526                 else if(wishangle < 0)
527                 {
528                         direction = STRAFEHUD_DIRECTION_LEFT;
529                 }
530                 else
531                 {
532                         if(angle > antiflicker_angle && angle < (180 - antiflicker_angle))
533                                 direction = STRAFEHUD_DIRECTION_RIGHT;
534                         else if(angle < -antiflicker_angle && angle > (-180 + antiflicker_angle))
535                                 direction = STRAFEHUD_DIRECTION_LEFT;
536                         else
537                                 direction = STRAFEHUD_DIRECTION_NONE;
538                 }
539
540                 // best angle to strafe at
541                 // in case of ground friction we may decelerate if the acceleration is smaller than the speed loss from friction
542                 real_bestangle = bestangle = (strafespeed > bestspeed ? acos(bestspeed / strafespeed) * RAD2DEG : 0);
543                 real_prebestangle = prebestangle = (strafespeed > movespeed ? acos(movespeed / strafespeed) * RAD2DEG : 0);
544                 if(direction == STRAFEHUD_DIRECTION_LEFT) // the angle becomes negative in case we strafe left
545                 {
546                         bestangle *= -1;
547                         prebestangle *= -1;
548                 }
549                 odd_bestangle = -bestangle - wishangle;
550                 bestangle -= wishangle;
551                 prebestangle -= wishangle;
552
553                 // various offsets and size calculations of hud indicator elements
554                 // how much is hidden by the current hud angle
555                 hidden_width = (360 - hudangle) / hudangle * panel_size.x;
556
557                 // current angle
558                 currentangle_size.x = autocvar_hud_panel_strafehud_angle_width;
559                 currentangle_size.y = autocvar_hud_panel_strafehud_angle_height;
560                 currentangle_size.z = 0;
561                 if(!autocvar_hud_panel_strafehud_uncapped)
562                 {
563                         currentangle_size.x = min(currentangle_size.x, 10);
564                         currentangle_size.y = min(currentangle_size.y, 10);
565                 }
566                 currentangle_size.x *= panel_size.x;
567                 currentangle_size.y *= panel_size.y;
568                 if(!autocvar_hud_panel_strafehud_uncapped)
569                 {
570                         currentangle_size.x = max(currentangle_size.x, 1);
571                         currentangle_size.y = max(currentangle_size.y, 1);
572                 }
573                 else
574                 {
575                         currentangle_size.y = max(currentangle_size.y, 0);
576                 }
577                 if(mode == STRAFEHUD_MODE_VIEW_CENTERED)
578                         currentangle_offset = angle / hudangle * panel_size.x;
579                 else
580
581                         currentangle_offset = bound(-hudangle / 2, angle, hudangle / 2) / hudangle * panel_size.x + panel_size.x / 2;
582
583                 // best strafe acceleration angle
584                 bestangle_offset        =  bestangle / hudangle * panel_size.x + panel_size.x / 2;
585                 switch_bestangle_offset = -bestangle / hudangle * panel_size.x + panel_size.x / 2;
586                 bestangle_width = panel_size.x * autocvar_hud_panel_strafehud_switch_width;
587                 if(!autocvar_hud_panel_strafehud_uncapped)
588                         bestangle_width = max(bestangle_width, 1);
589
590                 if((angle > -wishangle && direction == STRAFEHUD_DIRECTION_LEFT) || (angle < -wishangle && direction == STRAFEHUD_DIRECTION_RIGHT))
591                 {
592                         odd_angles = true;
593                         odd_bestangle_offset = odd_bestangle / hudangle * panel_size.x + panel_size.x / 2;
594                         switch_odd_bestangle_offset = (odd_bestangle + bestangle * 2) / hudangle * panel_size.x + panel_size.x / 2;
595                 }
596                 // direction indicator
597                 direction_size_vertical.x = autocvar_hud_panel_strafehud_direction_width;
598                 if(!autocvar_hud_panel_strafehud_uncapped)
599                         direction_size_vertical.x = min(direction_size_vertical.x, 1);
600                 direction_size_vertical.x *= panel_size.y;
601                 if(!autocvar_hud_panel_strafehud_uncapped)
602                         direction_size_vertical.x = max(direction_size_vertical.x, 1);
603                 direction_size_vertical.y = panel_size.y + direction_size_vertical.x * 2;
604                 direction_size_vertical.z = 0;
605                 direction_size_horizontal.x = panel_size.x * min(autocvar_hud_panel_strafehud_direction_length, .5);
606                 direction_size_horizontal.y = direction_size_vertical.x;
607                 direction_size_horizontal.z = 0;
608
609                 // the neutral zone fills the whole strafe bar
610                 if(immobile)
611                 {
612                         // draw neutral zone
613                         if(panel_size.x > 0 && panel_size.y > 0 && autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha > 0)
614                         {
615                                 switch(autocvar_hud_panel_strafehud_style)
616                                 {
617                                         default:
618                                         case STRAFEHUD_STYLE_DRAWFILL:
619                                                 drawfill(
620                                                         panel_pos, panel_size,
621                                                         autocvar_hud_panel_strafehud_bar_neutral_color,
622                                                         autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha,
623                                                         DRAWFLAG_NORMAL);
624                                                 break;
625
626                                         case STRAFEHUD_STYLE_PROGRESSBAR:
627                                                 HUD_Panel_DrawProgressBar(
628                                                         panel_pos, panel_size, "progressbar", 1, 0, 0,
629                                                         autocvar_hud_panel_strafehud_bar_neutral_color,
630                                                         autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha,
631                                                         DRAWFLAG_NORMAL);
632                                 }
633                         }
634                 }
635                 else
636                 {
637                         // calculate various zones of the strafe-o-meter
638                         if(autocvar_hud_panel_strafehud_bar_preaccel)
639                                 preaccelzone_width = (fabs(bestangle - prebestangle)) / hudangle * panel_size.x;
640                         else
641                                 preaccelzone_width = 0;
642                         accelzone_width = (90 - fabs(bestangle + wishangle)) / hudangle * panel_size.x;
643                         overturn_width = 180 / hudangle * panel_size.x;
644                         neutral_width = 360 / hudangle * panel_size.x - accelzone_width * 2 - preaccelzone_width * 2 - overturn_width;
645
646                         {
647                                 float current_offset = 0;
648                                 preaccelzone_right_offset = current_offset;
649                                 current_offset += preaccelzone_width;
650
651                                 accelzone_right_offset = current_offset;
652                                 current_offset += accelzone_width;
653
654                                 overturn_offset = current_offset;
655                                 current_offset += overturn_width;
656
657                                 accelzone_left_offset = current_offset;
658                                 current_offset += accelzone_width;
659
660                                 preaccelzone_left_offset = current_offset;
661                                 current_offset += preaccelzone_width;
662
663                                 // the wrapping code may struggle if we always append it on the right side
664                                 neutral_offset = direction == STRAFEHUD_DIRECTION_LEFT ? current_offset : -neutral_width;
665                         }
666
667                         // shift hud if operating in view angle centered mode
668                         if(mode == STRAFEHUD_MODE_VIEW_CENTERED)
669                         {
670                                 shift_offset = -currentangle_offset;
671                                 bestangle_offset += shift_offset;
672                                 switch_bestangle_offset += shift_offset;
673                                 odd_bestangle_offset += shift_offset;
674                                 switch_odd_bestangle_offset += shift_offset;
675                         }
676                         if(direction == STRAFEHUD_DIRECTION_LEFT)
677                                 shift_offset += -360 / hudangle * panel_size.x;
678
679                         // calculate how far off-center the strafe zones currently are
680                         shift_offset += (panel_size.x + neutral_width) / 2 - wishangle / hudangle * panel_size.x;
681
682                         // shift strafe zones into correct place
683                         neutral_offset += shift_offset;
684                         accelzone_left_offset += shift_offset;
685                         accelzone_right_offset += shift_offset;
686                         preaccelzone_left_offset += shift_offset;
687                         preaccelzone_right_offset += shift_offset;
688                         overturn_offset += shift_offset;
689
690                         // draw left acceleration zone
691                         HUD_Panel_DrawStrafeHUD(
692                                 accelzone_left_offset, accelzone_width, hidden_width,
693                                 autocvar_hud_panel_strafehud_bar_accel_color,
694                                 autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha,
695                                 autocvar_hud_panel_strafehud_style, STRAFEHUD_GRADIENT_LEFT);
696
697                         if(autocvar_hud_panel_strafehud_bar_preaccel)
698                                 HUD_Panel_DrawStrafeHUD(
699                                         preaccelzone_left_offset, preaccelzone_width, hidden_width,
700                                         autocvar_hud_panel_strafehud_bar_accel_color,
701                                         autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha,
702                                         autocvar_hud_panel_strafehud_style, STRAFEHUD_GRADIENT_RIGHT);
703
704                         // draw right acceleration zone
705                         HUD_Panel_DrawStrafeHUD(
706                                 accelzone_right_offset, accelzone_width, hidden_width,
707                                 autocvar_hud_panel_strafehud_bar_accel_color,
708                                 autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha,
709                                 autocvar_hud_panel_strafehud_style, STRAFEHUD_GRADIENT_RIGHT);
710
711                         if(autocvar_hud_panel_strafehud_bar_preaccel)
712                                 HUD_Panel_DrawStrafeHUD(
713                                         preaccelzone_right_offset, preaccelzone_width, hidden_width,
714                                         autocvar_hud_panel_strafehud_bar_accel_color,
715                                         autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha,
716                                         autocvar_hud_panel_strafehud_style, STRAFEHUD_GRADIENT_LEFT);
717
718                         // draw overturn zone
719                         //   this is technically incorrect
720                         //   acceleration decreases at 90 degrees but speed loss happens a little bit after 90 degrees,
721                         //   however due to sv_airstopaccelerate that's hard to calculate
722                         HUD_Panel_DrawStrafeHUD(
723                                 overturn_offset, overturn_width, hidden_width,
724                                 autocvar_hud_panel_strafehud_bar_overturn_color,
725                                 autocvar_hud_panel_strafehud_bar_overturn_alpha * panel_fg_alpha,
726                                 autocvar_hud_panel_strafehud_style, STRAFEHUD_GRADIENT_BOTH);
727
728                         // draw neutral zone
729                         HUD_Panel_DrawStrafeHUD(
730                                 neutral_offset, neutral_width, hidden_width,
731                                 autocvar_hud_panel_strafehud_bar_neutral_color,
732                                 autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha,
733                                 autocvar_hud_panel_strafehud_style, STRAFEHUD_GRADIENT_NONE);
734
735                         // only draw indicators if minspeed is reached
736                         if(autocvar_hud_panel_strafehud_switch && speed >= minspeed && bestangle_width > 0 && autocvar_hud_panel_strafehud_switch_alpha > 0)
737                         {
738                                 // draw the switch indicator(s)
739                                 float offset = !odd_angles ? bestangle_offset : odd_bestangle_offset;
740                                 float switch_offset = !odd_angles ? switch_bestangle_offset : switch_odd_bestangle_offset;
741
742                                 // remove switch indicator width from offset
743                                 if(direction == STRAFEHUD_DIRECTION_LEFT)
744                                 {
745                                         if(!odd_angles)
746                                                 offset -= bestangle_width;
747                                         else
748                                                 switch_offset -= bestangle_width;
749                                 }
750                                 else
751                                 {
752                                         if(!odd_angles)
753                                                 switch_offset -= bestangle_width;
754                                         else
755                                                 offset -= bestangle_width;
756                                 }
757
758                                 HUD_Panel_DrawStrafeHUD(
759                                         switch_offset, bestangle_width, hidden_width,
760                                         autocvar_hud_panel_strafehud_switch_color,
761                                         autocvar_hud_panel_strafehud_switch_alpha * panel_fg_alpha,
762                                         STRAFEHUD_STYLE_DRAWFILL, STRAFEHUD_GRADIENT_NONE);
763
764                                 if(direction == STRAFEHUD_DIRECTION_NONE)
765                                         HUD_Panel_DrawStrafeHUD(
766                                                 offset, bestangle_width, hidden_width,
767                                                 autocvar_hud_panel_strafehud_switch_color,
768                                                 autocvar_hud_panel_strafehud_switch_alpha * panel_fg_alpha,
769                                                 STRAFEHUD_STYLE_DRAWFILL, STRAFEHUD_GRADIENT_NONE);
770                         }
771                 }
772
773                 // slick detector
774                 slickdetector_height = max(autocvar_hud_panel_strafehud_slickdetector_height, 0);
775                 if(!autocvar_hud_panel_strafehud_uncapped)
776                         slickdetector_height = min(slickdetector_height, 1);
777                 slickdetector_height *= panel_size.y;
778                 if(autocvar_hud_panel_strafehud_slickdetector &&
779                    autocvar_hud_panel_strafehud_slickdetector_range > 0 &&
780                    autocvar_hud_panel_strafehud_slickdetector_alpha > 0 &&
781                    slickdetector_height > 0 &&
782                    panel_size.x > 0)
783                 {
784                         float slicksteps = max(autocvar_hud_panel_strafehud_slickdetector_granularity, 0);
785                         bool slickdetected = false;
786
787                         if(!autocvar_hud_panel_strafehud_uncapped)
788                                 slicksteps = min(slicksteps, 4);
789                         slicksteps = 90 / 2 ** slicksteps;
790
791                         slickdetected = real_onslick; // don't need to traceline if already touching slick
792
793                         // traceline into every direction
794                         trace_dphitq3surfaceflags = 0;
795                         vector traceorigin = strafeplayer.origin + eZ * strafeplayer.mins.z;
796                         for(float i = 0; i < 90 && !slickdetected; i += slicksteps)
797                         {
798                                 vector slickoffset;
799                                 float slickrotate;
800                                 slickoffset.z = -cos(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
801                                 slickrotate = sin(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
802
803                                 for(float j = 0; j < 360 && !slickdetected; j += slicksteps)
804                                 {
805                                         slickoffset.x = sin(j * DEG2RAD) * slickrotate;
806                                         slickoffset.y = cos(j * DEG2RAD) * slickrotate;
807
808                                         traceline(traceorigin, traceorigin + slickoffset, MOVE_NOMONSTERS, strafeplayer);
809                                         if((PHYS_FRICTION(strafeplayer) == 0 && trace_fraction < 1) 
810                                         || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK))
811                                                 slickdetected = true;
812                                         if(i == 0)
813                                                 break;
814                                 }
815                         }
816
817                         // if a traceline hit a slick surface
818                         if(slickdetected)
819                         {
820                                 vector slickdetector_size = panel_size;
821                                 slickdetector_size.y = slickdetector_height;
822
823                                 // top horizontal line
824                                 drawfill(
825                                         panel_pos - eY * slickdetector_size.y, slickdetector_size,
826                                         autocvar_hud_panel_strafehud_slickdetector_color,
827                                         autocvar_hud_panel_strafehud_slickdetector_alpha * panel_fg_alpha,
828                                         DRAWFLAG_NORMAL);
829
830                                 // bottom horizontal line
831                                 drawfill(
832                                         panel_pos + eY * panel_size.y,
833                                         slickdetector_size, autocvar_hud_panel_strafehud_slickdetector_color,
834                                         autocvar_hud_panel_strafehud_slickdetector_alpha * panel_fg_alpha,
835                                         DRAWFLAG_NORMAL);
836                         }
837
838                         text_offset_top = text_offset_bottom = slickdetector_height;
839                 }
840
841                 if(autocvar_hud_panel_strafehud_direction &&
842                    direction != STRAFEHUD_DIRECTION_NONE &&
843                    direction_size_vertical.x > 0 &&
844                    autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha > 0)
845                 {
846                         bool indicator_direction = direction == STRAFEHUD_DIRECTION_LEFT;
847                         // invert left/right when strafing backwards or when strafing towards the opposite side indicated by the direction variable
848                         // if both conditions are true then it's inverted twice hence not inverted at all
849                         if(!fwd != odd_angles)
850                                 indicator_direction = !indicator_direction;
851
852                         // draw the direction indicator caps at the sides of the hud
853                         // vertical line
854                         if(direction_size_vertical.y > 0)
855                                 drawfill(
856                                         panel_pos - eY * direction_size_horizontal.y + eX * (indicator_direction ? -direction_size_vertical.x : panel_size.x),
857                                         direction_size_vertical, autocvar_hud_panel_strafehud_direction_color,
858                                         autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha,
859                                         DRAWFLAG_NORMAL);
860
861                         // top horizontal line
862                         drawfill(
863                                 panel_pos + eX * (indicator_direction ? 0 : panel_size.x - direction_size_horizontal.x) - eY * direction_size_horizontal.y,
864                                 direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color,
865                                 autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha,
866                                 DRAWFLAG_NORMAL);
867
868                         // bottom horizontal line
869                         drawfill(
870                                 panel_pos + eX * (indicator_direction ? 0 : panel_size.x - direction_size_horizontal.x) + eY * panel_size.y,
871                                 direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color,
872                                 autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha,
873                                 DRAWFLAG_NORMAL);
874                 }
875
876                 // draw the actual strafe angle
877                 if(!immobile)
878                 {
879                         float moveangle = fabs(angle + wishangle);
880                         float strafe_ratio = 0;
881
882                         // player is overturning
883                         if(moveangle >= 90)
884                         {
885                                 currentangle_color = autocvar_hud_panel_strafehud_angle_overturn_color;
886                                 strafe_ratio = (moveangle - 90) / 90;
887                                 if(strafe_ratio > 1) strafe_ratio = 2 - strafe_ratio;
888                                 strafe_ratio *= -1;
889                         }
890                         // player gains speed by strafing
891                         else if(moveangle >= real_bestangle)
892                         {
893                                 currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
894                                 strafe_ratio = (90 - moveangle) / (90 - real_bestangle);
895                         }
896                         else if(moveangle >= real_prebestangle)
897                         {
898                                 if(autocvar_hud_panel_strafehud_bar_preaccel)
899                                         currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
900                                 strafe_ratio = (moveangle - real_prebestangle) / (real_bestangle - real_prebestangle);
901                         }
902
903                         if(autocvar_hud_panel_strafehud_style == STRAFEHUD_STYLE_GRADIENT)
904                                 currentangle_color = StrafeHUD_mixColors(autocvar_hud_panel_strafehud_angle_neutral_color, currentangle_color, fabs(strafe_ratio));
905                 }
906
907                 if(mode == STRAFEHUD_MODE_VIEW_CENTERED || straight_overturn)
908                         currentangle_offset = panel_size.x / 2;
909
910                 float angleheight_offset = currentangle_size.y;
911                 float ghost_offset = 0;
912                 if(autocvar_hud_panel_strafehud_bestangle && direction != STRAFEHUD_DIRECTION_NONE)
913                         ghost_offset = bound(0, (odd_angles ? odd_bestangle_offset : bestangle_offset), panel_size.x);
914
915                 switch(autocvar_hud_panel_strafehud_angle_style)
916                 {
917                         case STRAFEHUD_INDICATOR_SOLID:
918                                 if(currentangle_size.x > 0 && currentangle_size.y > 0)
919                                 {
920                                         if(autocvar_hud_panel_strafehud_bestangle && direction != STRAFEHUD_DIRECTION_NONE)
921                                                 drawfill(
922                                                         panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (ghost_offset - currentangle_size.x / 2),
923                                                         currentangle_size, autocvar_hud_panel_strafehud_bestangle_color,
924                                                         autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha,
925                                                         DRAWFLAG_NORMAL);
926                                         drawfill(
927                                                 panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (currentangle_offset - currentangle_size.x / 2),
928                                                 currentangle_size, currentangle_color,
929                                                 autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha,
930                                                 DRAWFLAG_NORMAL);
931                                 }
932                                 break;
933                         case STRAFEHUD_INDICATOR_DASHED:
934                                 if(currentangle_size.x > 0 && currentangle_size.y > 0)
935                                 {
936                                         vector line_size = currentangle_size;
937                                         line_size.y = currentangle_size.y / (bound(2, autocvar_hud_panel_strafehud_angle_dashes, currentangle_size.y) * 2 - 1);
938                                         for(float i = 0; i < currentangle_size.y; i += line_size.y * 2)
939                                         {
940                                                 if(i + line_size.y * 2 >= currentangle_size.y)
941                                                         line_size.y = currentangle_size.y - i;
942                                                 if(autocvar_hud_panel_strafehud_bestangle && direction != STRAFEHUD_DIRECTION_NONE)
943                                                         drawfill(
944                                                                 panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2 - i) + eX * (ghost_offset - line_size.x / 2),
945                                                                 line_size, autocvar_hud_panel_strafehud_bestangle_color,
946                                                                 autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
947                                                 drawfill(
948                                                         panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2 - i) + eX * (currentangle_offset - line_size.x / 2),
949                                                         line_size, currentangle_color,
950                                                         autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
951                                         }
952                                 }
953                                 break;
954                         case STRAFEHUD_INDICATOR_NONE:
955                         default:
956                                 // don't offset text and arrows if the angle indicator line isn't drawn
957                                 angleheight_offset = panel_size.y;
958                                 currentangle_size = '0 0 0';
959                 }
960
961                 float angle_offset_top = 0, angle_offset_bottom = 0;
962
963                 // offset text if any angle indicator is drawn
964                 if((autocvar_hud_panel_strafehud_angle_alpha > 0) ||
965                    (autocvar_hud_panel_strafehud_bestangle && autocvar_hud_panel_strafehud_bestangle_alpha > 0))
966                 {
967                         // offset text by amount the angle indicator extrudes from the strafehud bar
968                         angle_offset_top = angle_offset_bottom = (angleheight_offset - panel_size.y) / 2;
969                 }
970
971                 if(autocvar_hud_panel_strafehud_angle_arrow > 0)
972                 {
973                         if(arrow_size > 0)
974                         {
975                                 if(autocvar_hud_panel_strafehud_angle_arrow == 1 || autocvar_hud_panel_strafehud_angle_arrow >= 3)
976                                 {
977                                         if(autocvar_hud_panel_strafehud_bestangle && direction != STRAFEHUD_DIRECTION_NONE)
978                                                 StrafeHUD_drawStrafeArrow(
979                                                         panel_pos + eY * ((panel_size.y - angleheight_offset) / 2) + eX * ghost_offset,
980                                                         arrow_size, autocvar_hud_panel_strafehud_bestangle_color,
981                                                         autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, true, currentangle_size.x);
982                                         StrafeHUD_drawStrafeArrow(
983                                                 panel_pos + eY * ((panel_size.y - angleheight_offset) / 2) + eX * currentangle_offset,
984                                                 arrow_size, currentangle_color,
985                                                 autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, true, currentangle_size.x);
986
987                                         angle_offset_top += arrow_size; // further offset the top text offset if the top arrow is drawn
988                                 }
989                                 if(autocvar_hud_panel_strafehud_angle_arrow >= 2)
990                                 {
991                                         if(autocvar_hud_panel_strafehud_bestangle && direction != STRAFEHUD_DIRECTION_NONE)
992                                                 StrafeHUD_drawStrafeArrow(
993                                                         panel_pos + eY * ((panel_size.y - angleheight_offset) / 2 + angleheight_offset) + eX * ghost_offset,
994                                                         arrow_size, autocvar_hud_panel_strafehud_bestangle_color,
995                                                         autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, false, currentangle_size.x);
996                                         StrafeHUD_drawStrafeArrow(
997                                                 panel_pos + eY * ((panel_size.y - angleheight_offset) / 2 + angleheight_offset) + eX * currentangle_offset,
998                                                 arrow_size, currentangle_color,
999                                                 autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, false, currentangle_size.x);
1000
1001                                         angle_offset_bottom += arrow_size; // further offset the bottom text offset if the bottom arrow is drawn
1002                                 }
1003                         }
1004                 }
1005
1006                 // make sure text doesn't draw inside the strafehud bar
1007                 text_offset_top = max(angle_offset_top, text_offset_top);
1008                 text_offset_bottom = max(angle_offset_bottom, text_offset_bottom);
1009
1010                 draw_beginBoldFont();
1011
1012                 // show speed when crossing the start trigger
1013                 {
1014                         static float startspeed = 0, starttime = 0; // displayed value and timestamp for fade out
1015
1016                         // check if the start trigger was hit (will also trigger if the finish trigger was hit if those have the same ID)
1017                         if((race_nextcheckpoint == 1) || (race_checkpoint == 254 && race_nextcheckpoint == 255))
1018                         {
1019                                 if((race_checkpointtime > 0) && (starttime != race_checkpointtime))
1020                                 {
1021                                         starttime = race_checkpointtime;
1022                                         startspeed = speed;
1023                                 }
1024                         }
1025
1026                         if(autocvar_hud_panel_strafehud_startspeed)
1027                         {
1028                                 float startspeed_height = autocvar_hud_panel_strafehud_startspeed_size * panel_size.y;
1029                                 string startspeed_text = ftos_decimals(startspeed * speed_conversion_factor, 2);
1030                                 if(autocvar_hud_panel_strafehud_unit_show)
1031                                         startspeed_text = strcat(startspeed_text, GetSpeedUnit(autocvar_hud_speed_unit));
1032
1033                                 bool was_drawn = StrafeHUD_drawTextIndicator(
1034                                         startspeed_text, startspeed_height,
1035                                         autocvar_hud_panel_strafehud_startspeed_color,
1036                                         autocvar_hud_panel_strafehud_startspeed_fade,
1037                                         starttime, text_offset_bottom, STRAFEHUD_TEXT_BOTTOM);
1038
1039                                 if(was_drawn)
1040                                         text_offset_bottom += startspeed_height;
1041                         }
1042                 }
1043
1044                 // show height achieved by a single jump
1045                 // FIXME: checking z position differences is unreliable (warpzones, teleporter, kill, etc) but using velocity to calculate jump height would be
1046                 //        inaccurate in hud code (possibly different tick rate than physics, doesn't run when hud isn't drawn, rounding errors)
1047                 {
1048                         static float height_min = 0, height_max = 0; // ground and peak of jump z coordinates
1049                         static float jumpheight = 0, jumptime = 0;   // displayed value and timestamp for fade out
1050
1051                         // tries to catch kill and spectate but those are not reliable
1052                         if((strafeplayer.velocity.z <= 0) || real_onground || swimming || IS_DEAD(strafeplayer) || !IS_PLAYER(strafeplayer))
1053                         {
1054                                 height_min = height_max = strafeplayer.origin.z;
1055                         }
1056                         else if(strafeplayer.origin.z > height_max)
1057                         {
1058                                 height_max = strafeplayer.origin.z;
1059                                 float jumpheight_new = height_max - height_min;
1060
1061                                 if((jumpheight_new * length_conversion_factor) > max(autocvar_hud_panel_strafehud_jumpheight_min, 0))
1062                                 {
1063                                         jumpheight = jumpheight_new;
1064                                         jumptime = time;
1065                                 }
1066                         }
1067
1068                         if(autocvar_hud_panel_strafehud_jumpheight)
1069                         {
1070                                 float jumpheight_height = autocvar_hud_panel_strafehud_jumpheight_size * panel_size.y;
1071                                 string jumpheight_text = ftos_decimals(jumpheight * length_conversion_factor, length_decimals);
1072                                 if(autocvar_hud_panel_strafehud_unit_show)
1073                                         jumpheight_text = strcat(jumpheight_text, GetLengthUnit(autocvar_hud_speed_unit));
1074
1075                                 bool was_drawn = StrafeHUD_drawTextIndicator(
1076                                         jumpheight_text, jumpheight_height,
1077                                         autocvar_hud_panel_strafehud_jumpheight_color,
1078                                         autocvar_hud_panel_strafehud_jumpheight_fade,
1079                                         jumptime, text_offset_top, STRAFEHUD_TEXT_TOP);
1080
1081                                 if(was_drawn)
1082                                         text_offset_top += jumpheight_height;
1083                         }
1084                 }
1085
1086                 draw_endBoldFont();
1087         }
1088         hud_lasttime = time;
1089 }
1090
1091 // functions to make hud elements align perfectly in the hud area
1092 void HUD_Panel_DrawStrafeHUD(float offset, float width, float hidden_width, vector color, float alpha, int type, int gradientType)
1093 {
1094         float mirror_offset, mirror_width;
1095         vector size = panel_size;
1096         vector mirror_size = panel_size;
1097         float overflow_width = 0, overflow_mirror_width = 0;
1098         float original_width = width; // required for gradient
1099
1100         if(type == STRAFEHUD_STYLE_GRADIENT && gradientType == STRAFEHUD_GRADIENT_NONE)
1101                 type = STRAFEHUD_STYLE_DRAWFILL;
1102
1103         if(alpha <= 0 && type != STRAFEHUD_STYLE_GRADIENT || width <= 0)
1104                 return;
1105
1106         if(offset < 0)
1107         {
1108                 mirror_width = min(fabs(offset), width);
1109                 mirror_offset = panel_size.x + hidden_width - fabs(offset);
1110                 width += offset;
1111                 offset = 0;
1112         }
1113         else
1114         {
1115                 mirror_width = min(offset + width - panel_size.x - hidden_width, width);
1116                 mirror_offset = max(offset - panel_size.x - hidden_width, 0);
1117         }
1118
1119         width = max(width, 0);
1120         if((offset + width) > panel_size.x)
1121         {
1122                 overflow_width = (offset + width) - panel_size.x;
1123                 width = panel_size.x - offset;
1124         }
1125         size.x = width;
1126
1127         if(mirror_offset < 0)
1128         {
1129                 mirror_width += mirror_offset;
1130                 mirror_offset = 0;
1131         }
1132
1133         mirror_width = max(mirror_width, 0);
1134         if((mirror_offset + mirror_width) > panel_size.x)
1135         {
1136                 overflow_mirror_width = (mirror_offset + mirror_width) - panel_size.x;
1137                 mirror_width = panel_size.x - mirror_offset;
1138         }
1139         mirror_size.x = mirror_width;
1140
1141         switch(type)
1142         {
1143                 default:
1144                 case STRAFEHUD_STYLE_DRAWFILL: // no styling (drawfill)
1145                         if(mirror_size.x > 0 && mirror_size.y > 0)
1146                                 drawfill(panel_pos + eX * mirror_offset, mirror_size, color, alpha, DRAWFLAG_NORMAL);
1147                         if(size.x > 0 && size.y > 0)
1148                                 drawfill(panel_pos + eX * offset, size, color, alpha, DRAWFLAG_NORMAL);
1149                         break;
1150
1151                 case STRAFEHUD_STYLE_PROGRESSBAR: // progress bar style
1152                         if(mirror_size.x > 0 && mirror_size.y > 0)
1153                                 HUD_Panel_DrawProgressBar(
1154                                         panel_pos + eX * mirror_offset,
1155                                         mirror_size, "progressbar",
1156                                         1, 0, 0, color, alpha, DRAWFLAG_NORMAL);
1157                         if(size.x > 0 && size.y > 0)
1158                                 HUD_Panel_DrawProgressBar(
1159                                         panel_pos + eX * offset,
1160                                         size, "progressbar",
1161                                         1, 0, 0, color, alpha, DRAWFLAG_NORMAL);
1162                         break;
1163
1164                 case STRAFEHUD_STYLE_GRADIENT: // gradient style (types: 1 = left, 2 = right, 3 = both)
1165                         // determine whether the gradient starts in the mirrored or the non-mirrored area
1166                         int gradient_start;
1167                         float gradient_offset, gradient_mirror_offset;
1168
1169                         if(offset == 0 && mirror_offset == 0)
1170                                 gradient_start = width > mirror_width ? 2 : 1;
1171                         else if(offset == 0)
1172                                 gradient_start = 2;
1173                         else if(mirror_offset == 0)
1174                                 gradient_start = 1;
1175                         else
1176                                 gradient_start = 0;
1177
1178                         switch(gradient_start)
1179                         {
1180                                 default:
1181                                 case 0: // no offset required
1182                                         gradient_offset = gradient_mirror_offset = 0;
1183                                         break;
1184                                 case 1: // offset starts in non-mirrored area, mirrored area requires offset
1185                                         gradient_offset = 0;
1186                                         gradient_mirror_offset = original_width - (mirror_width + overflow_mirror_width);
1187                                         break;
1188                                 case 2: // offset starts in mirrored area, non-mirrored area requires offset
1189                                         gradient_offset = original_width - (width + overflow_width);
1190                                         gradient_mirror_offset = 0;
1191                         }
1192
1193                         StrafeHUD_drawGradient(
1194                                 color, autocvar_hud_panel_strafehud_bar_neutral_color,
1195                                 mirror_size, original_width, mirror_offset,
1196                                 alpha, gradient_mirror_offset, gradientType);
1197
1198                         StrafeHUD_drawGradient(
1199                                 color, autocvar_hud_panel_strafehud_bar_neutral_color,
1200                                 size, original_width, offset,
1201                                 alpha, gradient_offset, gradientType);
1202         }
1203 }
1204
1205 vector StrafeHUD_mixColors(vector color1, vector color2, float ratio)
1206 {
1207         vector mixedColor;
1208         if(ratio <= 0) return color1;
1209         if(ratio >= 1) return color2;
1210         mixedColor.x = color1.x + (color2.x - color1.x) * ratio;
1211         mixedColor.y = color1.y + (color2.y - color1.y) * ratio;
1212         mixedColor.z = color1.z + (color2.z - color1.z) * ratio;
1213         return mixedColor;
1214 }
1215
1216 void StrafeHUD_drawGradient(vector color1, vector color2, vector size, float original_width, float offset, float alpha, float gradientOffset, int gradientType)
1217 {
1218         float color_ratio, alpha1, alpha2;
1219         vector segment_size = size;
1220         alpha1 = bound(0, alpha, 1);
1221         alpha2 = bound(0, autocvar_hud_panel_strafehud_bar_neutral_alpha, 1);
1222         if((alpha1 + alpha2) == 0) return;
1223         color_ratio = alpha1 / (alpha1 + alpha2);
1224         for(int i = 0; i < size.x; ++i)
1225         {
1226                 float ratio, alpha_ratio, combine_ratio1, combine_ratio2;
1227                 segment_size.x = min(size.x - i, 1); // each gradient segment is 1 unit wide except if there is less than 1 unit of gradient remaining
1228                 ratio = (i + segment_size.x / 2 + gradientOffset) / original_width * (gradientType == STRAFEHUD_GRADIENT_BOTH ? 2 : 1);
1229                 if(ratio > 1) ratio = 2 - ratio;
1230                 if(gradientType != STRAFEHUD_GRADIENT_RIGHT) ratio = 1 - ratio;
1231                 alpha_ratio = alpha1 - (alpha1 - alpha2) * ratio;
1232                 combine_ratio1 = ratio * (1 - color_ratio);
1233                 combine_ratio2 = (1 - ratio) * color_ratio;
1234                 ratio = (combine_ratio1 + combine_ratio2) == 0 ? 1 : combine_ratio1 / (combine_ratio1 + combine_ratio2);
1235
1236                 if(alpha_ratio > 0)
1237                         drawfill(
1238                                 panel_pos + eX * (offset + i),
1239                                 segment_size,
1240                                 StrafeHUD_mixColors(color1, color2, ratio),
1241                                 alpha_ratio,
1242                                 DRAWFLAG_NORMAL);
1243         }
1244 }
1245
1246 // draw the strafe arrows (inspired by drawspritearrow() in common/mutators/mutator/waypoints/waypointsprites.qc)
1247 void StrafeHUD_drawStrafeArrow(vector origin, float size, vector color, float alpha, bool flipped, float connection_width)
1248 {
1249         origin = HUD_Shift(origin);
1250         float width = HUD_ScaleX(size * 2 + connection_width);
1251         float height = HUD_ScaleY(size);
1252         if(flipped) origin -= size * eY;
1253         R_BeginPolygon("", DRAWFLAG_NORMAL, true);
1254         if(connection_width > 0)
1255         {
1256                 R_PolygonVertex(origin + (connection_width / 2 * eX) + (flipped ? height * eY : '0 0 0'), '0 0 0', color, alpha);
1257                 R_PolygonVertex(origin - (connection_width / 2 * eX) + (flipped ? height * eY : '0 0 0'), '0 0 0', color, alpha);
1258         }
1259         else
1260         {
1261                 R_PolygonVertex(origin + (flipped ? height * eY : '0 0 0'), '0 0 0', color, alpha);
1262         }
1263         R_PolygonVertex(origin + (flipped ? '0 0 0' : height * eY) - (width / 2) * eX, '0 0 0', color, alpha);
1264         R_PolygonVertex(origin + (flipped ? '0 0 0' : height * eY) + (width / 2) * eX, '0 0 0', color, alpha);
1265         R_EndPolygon();
1266 }
1267
1268 // draw a fading text indicator above or below the strafe meter, return true if something was displayed
1269 bool StrafeHUD_drawTextIndicator(string text, float height, vector color, float fadetime, float lasttime, float offset, int position)
1270 {
1271         if((height <= 0) || (lasttime <= 0) || (fadetime <= 0) || ((time - lasttime) >= fadetime))
1272                 return false;
1273
1274         float alpha = cos(((time - lasttime) / fadetime) * 90 * DEG2RAD); // fade non-linear like the physics panel does
1275         vector size = panel_size;
1276         size.y = height;
1277
1278         switch(position)
1279         {
1280                 case STRAFEHUD_TEXT_TOP:
1281                         offset += size.y;
1282                         offset *= -1;
1283                         break;
1284                 case STRAFEHUD_TEXT_BOTTOM:
1285                         offset += panel_size.y;
1286                         break;
1287         }
1288
1289         drawstring_aspect(panel_pos + eY * offset, text, size, color, alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1290         return true;
1291 }
1292
1293 // length unit conversion (km and miles are only included to match the GetSpeedUnit* functions)
1294 float GetLengthUnitFactor(int length_unit)
1295 {
1296         switch(length_unit)
1297         {
1298                 default:
1299                 case 1: return 1.0;
1300                 case 2: return 0.0254;
1301                 case 3: return 0.0254 * 0.001;
1302                 case 4: return 0.0254 * 0.001 * 0.6213711922;
1303                 case 5: return 0.0254 * 0.001 * 0.5399568035;
1304         }
1305 }
1306
1307 string GetLengthUnit(int length_unit)
1308 {
1309         switch(length_unit)
1310         {
1311                 // translator-friendly strings without the initial space
1312                 default:
1313                 case 1: return strcat(" ", _("qu"));
1314                 case 2: return strcat(" ", _("m"));
1315                 case 3: return strcat(" ", _("km"));
1316                 case 4: return strcat(" ", _("mi"));
1317                 case 5: return strcat(" ", _("nmi"));
1318         }
1319 }