3 #include "strafehud.qh"
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>
11 #include <client/view.qh> // for v_flipped state
14 #include <common/animdecide.qh> // anim_implicit_state
15 #include <common/ent_cs.qh> // CSQCModel_server2csqc()
18 #include <client/hud/panel/racetimer.qh> // checkpoint information (race_*)
21 #include <lib/csqcmodel/common.qh> // for IS_PLAYER() macro
22 #include <common/resources/cl_resources.qh> // IS_DEAD() macro
26 void HUD_StrafeHUD_Export(int fh)
28 // allow saving cvars that aesthetically change the panel into hud skin files
31 float GeomLerp(float a, float _lerp, float b); // declare GeomLerp here since there's no header file for it
35 static float hud_lasttime = 0;
39 // generic hud routines
40 if(!autocvar__hud_configure)
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; }
47 HUD_Panel_LoadCvars();
49 if(autocvar_hud_panel_strafehud_dynamichud)
62 panel_pos += '1 1 0' * panel_bg_padding;
63 panel_size -= '2 2 0' * panel_bg_padding;
66 // find out whether the local csqcmodel entity is valid
67 if(spectatee_status > 0 || isdemo())
70 strafeplayer = CSQCModel_server2csqc(player_localentnum - 1);
75 strafeplayer = csqcplayer;
79 if(csqcplayer && strafeplayer)
81 float strafe_waterlevel;
83 // check the player waterlevel without affecting the player entity, this way we can fetch waterlevel even if client prediction is disabled
86 void old_contentstransition(int, int) = strafeplayer.contentstransition;
87 float old_watertype = strafeplayer.watertype;
88 float old_waterlevel = strafeplayer.waterlevel;
90 strafeplayer.contentstransition = func_null; // unset the contentstransition function if present
91 _Movetype_CheckWater(strafeplayer);
92 strafe_waterlevel = strafeplayer.waterlevel; // store the player waterlevel
95 strafeplayer.contentstransition = old_contentstransition;
96 strafeplayer.watertype = old_watertype;
97 strafeplayer.waterlevel = old_waterlevel;
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;
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;
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;
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;
132 vector movement = PHYS_INPUT_MOVEVALUES(strafeplayer);
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);
145 float shift_offset = 0;
146 bool straight_overturn = false;
147 bool immobile = speed <= 0;
150 float neutral_offset;
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
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;
184 if(PHYS_FRICTION(strafeplayer) == 0) {
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;
192 real_onslick = onslick;
194 onground_lasttime = time;
195 onslick_last = onslick;
197 else if(jumpheld || swimming) onground_lasttime = 0;
199 if(onground_lasttime == 0)
200 onground_expired = true;
202 onground_expired = (time - onground_lasttime) >= autocvar_hud_panel_strafehud_timeout_ground; // timeout for slick ramps
204 if(!onground && !onground_expired) // if ground timeout hasn't expired yet use ground physics
207 onslick = onslick_last;
209 if(!autocvar__hud_configure)
211 maxspeed = PHYS_MAXSPEED(strafeplayer) * maxspeed_mod;
212 maxaccel = PHYS_ACCELERATE(strafeplayer);
216 movespeed = vlen(vec2(movement));
217 if(movespeed == 0) movespeed = maxspeed;
218 else movespeed = min(movespeed, maxspeed);
220 if(!autocvar_hud_panel_strafehud_uncapped)
221 arrow_size = max(arrow_size, 1);
223 // determine frametime
224 if((csqcplayer_status == CSQCPLAYERSTATUS_PREDICTED) && (input_timelength > 0))
226 float dt_client = input_timelength;
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
231 // calculate average frametime
232 dt_sum += dt_client*dt_client;
233 dt_time += dt_client;
235 if(((time - dt_update) > autocvar_hud_panel_strafehud_fps_update) || (dt_update == 0))
237 dt = dt_sum / dt_time;
239 dt_time = dt_sum = 0;
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
245 dt_update = dt_time = dt_sum = 0;
248 // determine whether the player is pressing forwards or backwards keys
249 if(islocal) // if entity is local player
253 keys_fwd = STRAFEHUD_KEYS_FORWARD;
255 else if(movement.x < 0)
257 keys_fwd = STRAFEHUD_KEYS_BACKWARD;
261 keys_fwd = STRAFEHUD_KEYS_NONE;
264 else // alternatively determine direction by querying pressed keys
266 if((keys & KEY_FORWARD) && !(keys & KEY_BACKWARD))
268 keys_fwd = STRAFEHUD_KEYS_FORWARD;
270 else if(!(keys & KEY_FORWARD) && (keys & KEY_BACKWARD))
272 keys_fwd = STRAFEHUD_KEYS_BACKWARD;
276 keys_fwd = STRAFEHUD_KEYS_NONE;
280 // determine player wishdir
281 if(islocal) // if entity is local player
289 else if(movement.y > 0)
306 wishangle = RAD2DEG * atan2(movement.y, movement.x);
307 // wrap the wish angle if it exceeds ±90°
308 if(fabs(wishangle) > 90)
310 if(wishangle < 0) wishangle += 180;
311 else wishangle -= 180;
312 wishangle = -wishangle;
317 else // alternatively calculate wishdir by querying pressed keys
319 if(keys & KEY_FORWARD || keys & KEY_BACKWARD)
331 else if(!(keys & KEY_RIGHT))
333 wishangle = 0; // wraps at 180°
337 strafekeys = fabs(wishangle) > 45;
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
343 range_minangle = 45 - fabs(wishangle) % 45;
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
348 if(autocvar_hud_panel_strafehud_range == 0)
350 if(autocvar__hud_configure)
356 hudangle = range_minangle; // use minimum angle required if dynamically setting hud angle
361 hudangle = bound(0, fabs(autocvar_hud_panel_strafehud_range), 360); // limit HUD range to 360 degrees, higher values don't make sense
364 // detect air strafe turning
365 if((!strafekeys && vlen(vec2(movement)) > 0) || onground || autocvar__hud_configure)
369 else // air strafe only
371 bool turn_expired = (time - turn_lasttime) >= autocvar_hud_panel_strafehud_timeout_turn; // timeout for jumping with strafe keys only
375 else if(turn_expired)
378 if(turn) // CPMA turning
382 turn_lasttime = time;
383 turnangle = wishangle;
385 else // retain last state until strafe turning times out
387 wishangle = turnangle;
390 // calculate the maximum air strafe speed and acceleration
391 float strafity = 1 - (90 - fabs(wishangle)) / 45;
393 if(PHYS_MAXAIRSTRAFESPEED(strafeplayer) != 0)
395 maxspeed = min(maxspeed, GeomLerp(PHYS_MAXAIRSPEED(strafeplayer), strafity, PHYS_MAXAIRSTRAFESPEED(strafeplayer)));
397 movespeed = min(movespeed, maxspeed);
399 if(PHYS_AIRSTRAFEACCELERATE(strafeplayer) != 0)
401 maxaccel = GeomLerp(PHYS_AIRACCELERATE(strafeplayer), strafity, PHYS_AIRSTRAFEACCELERATE(strafeplayer));
406 maxaccel *= dt * movespeed;
407 bestspeed = max(movespeed - maxaccel, 0); // target speed to gain maximum acceleration
409 float frictionspeed; // speed lost from friction
410 float strafespeed; // speed minus friction
412 if((speed > 0) && onground)
414 float strafefriction = onslick ? PHYS_FRICTION_SLICK(strafeplayer) : PHYS_FRICTION(strafeplayer);
416 frictionspeed = speed * dt * strafefriction * max(PHYS_STOPSPEED(strafeplayer) / speed, 1);
417 strafespeed = max(speed - frictionspeed, 0);
425 minspeed = autocvar_hud_panel_strafehud_switch_minspeed;
426 if(minspeed < 0) minspeed = bestspeed + frictionspeed;
428 // get current strafing angle ranging from -180° to +180°
429 if(!autocvar__hud_configure)
433 // calculate view angle relative to the players current velocity direction
434 angle = vel_angle - view_angle;
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;
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)
444 if(keys_fwd == STRAFEHUD_KEYS_FORWARD)
448 else if(keys_fwd == STRAFEHUD_KEYS_BACKWARD)
454 fwd = fabs(angle) <= 90;
457 // otherwise determine by examining the strafe angle
460 if(wishangle < 0) // detect direction using wishangle since the direction is not yet set
462 fwd = angle <= -wishangle;
466 fwd = angle >= -wishangle;
470 // shift the strafe angle by 180° when strafing backwards
473 if(angle < 0) angle += 180;
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))
480 straight_overturn = true;
489 else // simulate turning for HUD setup
491 const float demo_maxangle = 55; // maximum angle before changing direction
492 const float demo_turnspeed = 40; // turning speed in degrees per second
494 static float demo_position = -37 / demo_maxangle; // current positioning value between -1 and +1
496 if(autocvar__hud_panel_strafehud_demo)
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;
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);
509 angle = demo_position;
510 angle *= demo_maxangle;
514 if(angle < 0) wishangle *= -1;
517 // invert the wish angle when strafing backwards
520 wishangle = -wishangle;
523 // flip angles if v_flipped is enabled
524 if(autocvar_v_flipped)
527 wishangle = -wishangle;
530 // determine whether the player is strafing left or right
533 direction = STRAFEHUD_DIRECTION_RIGHT;
535 else if(wishangle < 0)
537 direction = STRAFEHUD_DIRECTION_LEFT;
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;
546 direction = STRAFEHUD_DIRECTION_NONE;
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
558 odd_bestangle = -bestangle - wishangle;
559 bestangle -= wishangle;
560 prebestangle -= wishangle;
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;
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)
571 currentangle_size.x = min(currentangle_size.x, 10);
572 currentangle_size.y = min(currentangle_size.y, 10);
574 currentangle_size.x *= panel_size.x;
575 currentangle_size.y *= panel_size.y;
576 if(!autocvar_hud_panel_strafehud_uncapped)
578 currentangle_size.x = max(currentangle_size.x, 1);
579 currentangle_size.y = max(currentangle_size.y, 1);
583 currentangle_size.y = max(currentangle_size.y, 0);
585 if(mode == STRAFEHUD_MODE_VIEW_CENTERED)
587 currentangle_offset = angle/hudangle * panel_size.x;
591 currentangle_offset = bound(-hudangle/2, angle, hudangle/2)/hudangle * panel_size.x + panel_size.x/2;
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);
600 if((angle > -wishangle && direction == STRAFEHUD_DIRECTION_LEFT) || (angle < -wishangle && direction == STRAFEHUD_DIRECTION_RIGHT))
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;
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;
619 // the neutral zone fills the whole strafe bar
623 if(panel_size.x > 0 && panel_size.y > 0 && autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha > 0)
625 switch(autocvar_hud_panel_strafehud_style)
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);
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);
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;
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;
649 float current_offset = 0;
650 preaccelzone_right_offset = current_offset;
651 current_offset += preaccelzone_width;
653 accelzone_right_offset = current_offset;
654 current_offset += accelzone_width;
656 overturn_offset = current_offset;
657 current_offset += overturn_width;
659 accelzone_left_offset = current_offset;
660 current_offset += accelzone_width;
662 preaccelzone_left_offset = current_offset;
663 current_offset += preaccelzone_width;
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
668 // shift hud if operating in view angle centered mode
669 if(mode == STRAFEHUD_MODE_VIEW_CENTERED)
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;
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;
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);
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);
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);
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);
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
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;
710 // remove switch indicator width from offset
711 if(direction == STRAFEHUD_DIRECTION_LEFT)
714 offset -= bestangle_width;
716 switch_offset -= bestangle_width;
721 switch_offset -= bestangle_width;
723 offset -= bestangle_width;
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);
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)
738 float slicksteps = max(autocvar_hud_panel_strafehud_slickdetector_granularity, 0);
739 bool slickdetected = false;
741 if(!autocvar_hud_panel_strafehud_uncapped)
742 slicksteps = min(slicksteps, 4);
743 slicksteps = 90 / 2 ** slicksteps;
745 slickdetected = real_onslick; // don't need to traceline if already touching slick
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)
754 slickoffset.z = -cos(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
755 slickrotate = sin(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
757 for(float j = 0; j < 360 && !slickdetected; j += slicksteps)
759 slickoffset.x = sin(j * DEG2RAD) * slickrotate;
760 slickoffset.y = cos(j * DEG2RAD) * slickrotate;
762 traceline(traceorigin, traceorigin + slickoffset, MOVE_NOMONSTERS, strafeplayer);
763 if((PHYS_FRICTION(strafeplayer) == 0 && trace_fraction < 1) || trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK) slickdetected = true;
768 // if a traceline hit a slick surface
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);
778 text_offset_top = text_offset_bottom = slickdetector_height;
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)
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)
789 indicator_direction = !indicator_direction;
791 // draw the direction indicator caps at the sides of the hud
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);
800 // draw the actual strafe angle
802 float moveangle = fabs(angle + wishangle);
803 float strafe_ratio = 0;
805 // player is overturning
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;
813 // player gains speed by strafing
814 else if(moveangle >= real_bestangle)
816 currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
817 strafe_ratio = (90 - moveangle) / (90 - real_bestangle);
819 else if(moveangle >= real_prebestangle)
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);
826 if(autocvar_hud_panel_strafehud_style == STRAFEHUD_STYLE_GRADIENT)
828 currentangle_color = StrafeHUD_mixColors(autocvar_hud_panel_strafehud_angle_neutral_color, currentangle_color, fabs(strafe_ratio));
832 if(mode == STRAFEHUD_MODE_VIEW_CENTERED || straight_overturn)
834 currentangle_offset = panel_size.x/2;
837 float angleheight_offset = currentangle_size.y;
838 float ghost_offset = 0;
839 if(autocvar_hud_panel_strafehud_bestangle && direction != STRAFEHUD_DIRECTION_NONE)
841 ghost_offset = bound(0, (odd_angles ? odd_bestangle_offset : bestangle_offset), panel_size.x);
844 switch(autocvar_hud_panel_strafehud_angle_style)
846 case STRAFEHUD_INDICATOR_SOLID:
847 if(currentangle_size.x > 0 && currentangle_size.y > 0)
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);
853 case STRAFEHUD_INDICATOR_DASHED:
854 if(currentangle_size.x > 0 && currentangle_size.y > 0)
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)
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);
866 case STRAFEHUD_INDICATOR_NONE:
868 // don't offset text and arrows if the angle indicator line isn't drawn
869 angleheight_offset = panel_size.y;
872 float angle_offset_top = 0, angle_offset_bottom = 0;
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
878 if(autocvar_hud_panel_strafehud_angle_arrow > 0)
882 if(autocvar_hud_panel_strafehud_angle_arrow == 1 || autocvar_hud_panel_strafehud_angle_arrow >= 3)
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);
887 angle_offset_top += arrow_size; // further offset the top text offset if the top arrow is drawn
889 if(autocvar_hud_panel_strafehud_angle_arrow >= 2)
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);
894 angle_offset_bottom += arrow_size; // further offset the bottom text offset if the bottom arrow is drawn
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);
903 draw_beginBoldFont();
905 // show speed when crossing the start trigger
907 static float startspeed = 0, starttime = 0; // displayed value and timestamp for fade out
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)
911 if((race_checkpointtime > 0) && (starttime != race_checkpointtime))
913 starttime = race_checkpointtime;
918 if(autocvar_hud_panel_strafehud_startspeed)
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));
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;
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)
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
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))
940 height_min = height_max = strafeplayer.origin.z;
942 else if(strafeplayer.origin.z > height_max)
944 height_max = strafeplayer.origin.z;
945 float jumpheight_new = height_max - height_min;
947 if((jumpheight_new * length_conversion_factor) > max(autocvar_hud_panel_strafehud_jumpheight_min, 0))
949 jumpheight = jumpheight_new;
954 if(autocvar_hud_panel_strafehud_jumpheight)
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));
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;
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)
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
980 if(type == STRAFEHUD_STYLE_GRADIENT && gradientType == STRAFEHUD_GRADIENT_NONE) type = STRAFEHUD_STYLE_DRAWFILL;
982 if(alpha <= 0 && type != STRAFEHUD_STYLE_GRADIENT || width <= 0) return;
986 mirror_width = min(fabs(offset), width);
987 mirror_offset = panel_size.x + hidden_width - fabs(offset);
993 mirror_width = min(offset + width - panel_size.x - hidden_width, width);
994 mirror_offset = max(offset - panel_size.x - hidden_width, 0);
997 width = max(width, 0);
998 if((offset + width) > panel_size.x)
1000 overflow_width = (offset + width) - panel_size.x;
1001 width = panel_size.x - offset;
1005 if(mirror_offset < 0)
1007 mirror_width += mirror_offset;
1011 mirror_width = max(mirror_width, 0);
1012 if((mirror_offset + mirror_width) > panel_size.x)
1014 overflow_mirror_width = (mirror_offset + mirror_width) - panel_size.x;
1015 mirror_width = panel_size.x - mirror_offset;
1017 mirror_size.x = mirror_width;
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);
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);
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
1035 float gradient_offset, gradient_mirror_offset;
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;
1042 switch(gradient_start)
1045 case 0: // no offset required
1046 gradient_offset = gradient_mirror_offset = 0;
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);
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;
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);
1062 vector StrafeHUD_mixColors(vector color1, vector color2, float ratio)
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;
1073 void StrafeHUD_drawGradient(vector color1, vector color2, vector size, float original_width, float offset, float alpha, float gradientOffset, int gradientType)
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)
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);
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)
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);
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)
1112 if((height <= 0) || (lasttime <= 0) || (fadetime <= 0) || ((time - lasttime) >= fadetime))
1115 float alpha = cos(((time - lasttime) / fadetime) * 90 * DEG2RAD); // fade non-linear like the physics panel does
1116 vector size = panel_size;
1120 case STRAFEHUD_TEXT_TOP:
1124 case STRAFEHUD_TEXT_BOTTOM:
1125 offset += panel_size.y;
1129 drawstring_aspect(panel_pos + eY * offset, text, size, color, alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
1133 // length unit conversion (km and miles are only included to match the GetSpeedUnit* functions)
1134 float GetLengthUnitFactor(int length_unit)
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;
1147 string GetLengthUnit(int length_unit)
1151 // translator-friendly strings without the initial space
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"));