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