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