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