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