3 #include "strafehud.qh"
5 #include <client/draw.qh>
6 #include <client/hud/panel/racetimer.qh>
7 #include <client/view.qh>
8 #include <common/animdecide.qh>
9 #include <common/ent_cs.qh>
10 #include <common/mapinfo.qh>
11 #include <common/physics/movetypes/movetypes.qh>
12 #include <common/physics/player.qh>
13 #include <common/resources/cl_resources.qh>
14 #include <lib/csqcmodel/cl_player.qh>
18 void HUD_StrafeHUD_Export(int fh)
20 // allow saving cvars that aesthetically change the panel into hud skin files
26 float GeomLerp(float a, float _lerp, float b); // declare GeomLerp here since there's no header file for it
33 // generic hud routines
34 if(!autocvar__hud_configure)
36 if(!autocvar_hud_panel_strafehud ||
37 (spectatee_status == -1 && (autocvar_hud_panel_strafehud == 1 || autocvar_hud_panel_strafehud == 3)) ||
38 (autocvar_hud_panel_strafehud == 3 && !MUTATOR_CALLHOOK(HUD_StrafeHUD_showoptional))) return;
41 HUD_Panel_LoadCvars();
43 if(autocvar_hud_panel_strafehud_dynamichud)
56 panel_pos += '1 1 0' * panel_bg_padding;
57 panel_size -= '2 2 0' * panel_bg_padding;
60 // find out whether the local csqcmodel entity is valid
61 if(spectatee_status > 0 || isdemo())
64 strafeplayer = CSQCModel_server2csqc(player_localentnum - 1);
69 strafeplayer = csqcplayer;
73 if(csqcplayer && strafeplayer)
75 float strafe_waterlevel;
77 // get the player waterlevel without affecting the player entity, this way we can fetch waterlevel even if client prediction is disabled
80 void old_contentstransition(int, int) = strafeplayer.contentstransition;
81 float old_watertype = strafeplayer.watertype;
82 float old_waterlevel = strafeplayer.waterlevel;
84 strafeplayer.contentstransition = func_null; // unset the contentstransition function if present
85 _Movetype_CheckWater(strafeplayer);
86 strafe_waterlevel = strafeplayer.waterlevel; // store the player waterlevel
89 strafeplayer.contentstransition = old_contentstransition;
90 strafeplayer.watertype = old_watertype;
91 strafeplayer.waterlevel = old_waterlevel;
95 static float demo_angle = -37;
96 static float demo_direction = 1;
97 static float demo_time = 0;
98 static float onground_lasttime = 0;
99 static float turn_lasttime = 0;
100 static bool turn = false;
101 static float turnangle;
102 static float turnspeed;
103 static float turnaccel;
104 static bool fwd = true;
105 static float dt_update = 0;
106 static int dt_time = 0;
107 static float dt_sum = 0;
111 int keys = STAT(PRESSED_KEYS);
112 bool jumpheld = islocal ? (PHYS_INPUT_BUTTON_JUMP(strafeplayer) || PHYS_INPUT_BUTTON_JETPACK(strafeplayer)) : (keys & KEY_JUMP); // doesn't work in spectator mode if spectated player uses +jetpack
113 bool onground = (islocal ? IS_ONGROUND(strafeplayer) : !(strafeplayer.anim_implicit_state & ANIMIMPLICITSTATE_INAIR)) && !jumpheld; // if jump is held assume we are in air
114 bool onground_expired;
116 bool swimming = strafe_waterlevel >= WATERLEVEL_SWIMMING; // the hud will not work well while swimming
117 bool spectating = entcs_GetSpecState(strafeplayer.sv_entnum) == ENTCS_SPEC_PURE;
118 float speed = !autocvar__hud_configure ? vlen(vec2(csqcplayer.velocity)) : 1337; // use local csqcmodel entity for this even when spectating, flickers too much otherwise
119 float maxspeed_mod = IS_DUCKED(csqcplayer) ? .5 : 1;
120 float maxspeed_phys = onground ? PHYS_MAXSPEED(strafeplayer) : PHYS_MAXAIRSPEED(strafeplayer);
121 float maxspeed = !autocvar__hud_configure ? maxspeed_phys * maxspeed_mod : 320;
124 float maxaccel_phys = onground ? PHYS_ACCELERATE(strafeplayer) : PHYS_AIRACCELERATE(strafeplayer);
125 float maxaccel = !autocvar__hud_configure ? maxaccel_phys : 1;
126 float vel_angle = vectoangles(strafeplayer.velocity).y - (vectoangles(strafeplayer.velocity).y > 180 ? 360 : 0); // change the range from 0° - 360° to -180° - 180° to match how view_angle represents angles
127 float view_angle = PHYS_INPUT_ANGLES(strafeplayer).y;
129 vector movement = PHYS_INPUT_MOVEVALUES(strafeplayer);
134 int mode = autocvar_hud_panel_strafehud_mode >= 0 && autocvar_hud_panel_strafehud_mode <= 1 ? autocvar_hud_panel_strafehud_mode : 0;
135 float speed_conversion_factor = GetSpeedUnitFactor(autocvar_hud_panel_strafehud_unit);
136 float length_conversion_factor = GetLengthUnitFactor(autocvar_hud_panel_strafehud_unit);
137 int length_decimals = autocvar_hud_panel_strafehud_unit >= 3 && autocvar_hud_panel_strafehud_unit <= 5 ? 6 : 2; // use more decimals when displaying km or miles
138 float antiflicker_angle = bound(0, autocvar_hud_panel_strafehud_antiflicker_angle, 180);
140 float shift_offset = 0;
141 bool straight_overturn = false;
142 bool immobile = speed <= 0;
144 float neutral_offset;
146 vector currentangle_color = autocvar_hud_panel_strafehud_angle_neutral_color;
147 float currentangle_offset;
148 vector currentangle_size = '0 0 0';
152 bool bestangle_anywhere = false;
153 float bestangle_offset;
154 float switch_bestangle_offset;
155 bool odd_angles = false;
156 float odd_bestangle_offset = 0;
157 float switch_odd_bestangle_offset = 0;
158 float bestangle_width;
159 float accelzone_left_offset;
160 float accelzone_right_offset;
161 float accelzone_width;
162 float preaccelzone_left_offset;
163 float preaccelzone_right_offset;
164 float preaccelzone_width;
165 float overturn_offset;
166 float overturn_width;
167 float slickdetector_height;
168 vector direction_size_vertical = '0 0 0';
169 vector direction_size_horizontal = '0 0 0';
170 float range_minangle;
171 float arrow_size = max(panel_size.y * min(autocvar_hud_panel_strafehud_angle_arrow_size, 10), 0); // there's only one size cvar for the arrows, they will always have a 45° angle to ensure proper rendering without antialiasing
173 if(onground) onground_lasttime = time;
174 else if(jumpheld) onground_lasttime = 0;
176 if(onground_lasttime == 0)
177 onground_expired = true;
179 onground_expired = (time - onground_lasttime) >= autocvar_hud_panel_strafehud_timeout_ground;
181 if(!onground && !onground_expired) // if ground timeout hasn't expired yet use ground physics
184 if(!autocvar__hud_configure)
185 maxaccel = PHYS_ACCELERATE(strafeplayer);
188 movespeed = vlen(vec2(movement));
189 if(movespeed == 0) movespeed = maxspeed;
190 else movespeed = min(movespeed, maxspeed);
192 if(!autocvar_hud_panel_strafehud_uncapped)
193 arrow_size = max(arrow_size, 1);
195 // determine frametime
196 if((csqcplayer_status == CSQCPLAYERSTATUS_PREDICTED) && (input_timelength > 0))
198 float dt_client = input_timelength;
200 if(dt_client > .05) // server splits frames longer than 50 ms into two moves
201 dt_client /= 2; // doesn't ensure frames are smaller than 50 ms, just splits large frames in half, matches server behaviour
203 // calculate average frametime
204 dt_sum += dt_client*dt_client;
205 dt_time += dt_client;
207 if(((time - dt_update) > autocvar_hud_panel_strafehud_fps_update) || (dt_update == 0))
209 dt = dt_sum / dt_time;
211 dt_time = dt_sum = 0;
214 else // when spectating other players server ticrate will be used, this may not be accurate but there is no way to find other player's frametime
217 dt_update = dt_time = dt_sum = 0;
220 // determine whether the player is pressing forwards or backwards keys
221 if(islocal) // if entity is local player
227 else if(movement.x < 0)
236 else // alternatively determine direction by querying pressed keys
238 if((keys & KEY_FORWARD) && !(keys & KEY_BACKWARD))
242 else if(!(keys & KEY_FORWARD) && (keys & KEY_BACKWARD))
252 // determine player wishdir
253 if(islocal) // if entity is local player
261 else if(movement.y > 0)
278 wishangle = RAD2DEG * atan2(movement.y, movement.x);
279 // wrap the wish angle if it exceeds ±90°
280 if(fabs(wishangle) > 90)
282 if(wishangle < 0) wishangle += 180;
283 else wishangle -= 180;
284 wishangle = -wishangle;
289 else // alternatively calculate wishdir by querying pressed keys
291 if(keys & KEY_FORWARD || keys & KEY_BACKWARD)
303 else if(!(keys & KEY_RIGHT))
305 wishangle = 0; // wraps at 180°
309 strafekeys = fabs(wishangle) > 45;
311 // determine minimum required angle to display full strafe range
312 range_minangle = fabs(wishangle) % 90; // maximum range is 90 degree
313 if(range_minangle > 45) // minimum angle range is 45
315 range_minangle = 45 - fabs(wishangle) % 45;
317 range_minangle = 90 - range_minangle; // calculate value which is never >90 or <45
318 range_minangle *= 2; // multiply to accommodate for both sides of the hud
320 if(autocvar_hud_panel_strafehud_range == 0)
322 if(autocvar__hud_configure)
328 hudangle = range_minangle; // use minimum angle required if dynamically setting hud angle
333 hudangle = bound(0, fabs(autocvar_hud_panel_strafehud_range), 360); // limit HUD range to 360 degrees, higher values don't make sense
336 // detect air strafe turning
337 if((!strafekeys && vlen(vec2(movement)) > 0) || onground || autocvar__hud_configure)
341 else // air strafe only
343 bool turn_expired = (time - turn_lasttime) >= autocvar_hud_panel_strafehud_timeout_turn;
347 if(onground_expired) // timeout for slick ramps
349 turn = true; // CPMA turning
350 turn_lasttime = time;
351 turnangle = wishangle;
353 // calculate the maximum air strafe speed and acceleration
354 float strafity = 1 - (90 - fabs(wishangle)) / 45;
356 if(PHYS_MAXAIRSTRAFESPEED(strafeplayer) != 0)
358 maxspeed = min(maxspeed, GeomLerp(PHYS_MAXAIRSPEED(strafeplayer), strafity, PHYS_MAXAIRSTRAFESPEED(strafeplayer)));
360 turnspeed = movespeed = min(movespeed, maxspeed);
362 if(PHYS_AIRSTRAFEACCELERATE(strafeplayer) != 0)
364 maxaccel = GeomLerp(PHYS_AIRACCELERATE(strafeplayer), strafity, PHYS_AIRSTRAFEACCELERATE(strafeplayer));
366 turnaccel = maxaccel;
371 if(!turn_expired) // retain last state until strafe turning times out
373 wishangle = turnangle;
374 movespeed = turnspeed;
375 maxaccel = turnaccel;
377 else // timeout for jumping with strafe keys only
384 maxaccel *= dt * movespeed;
385 bestspeed = max(movespeed - maxaccel, 0); // target speed to gain maximum acceleration
387 float frictionspeed; // speed lost from friction
388 float strafespeed; // speed minus friction
390 if((speed > 0) && onground)
392 float strafefriction = IS_ONSLICK(strafeplayer) ? PHYS_FRICTION_SLICK(strafeplayer) : PHYS_FRICTION(strafeplayer);
394 frictionspeed = speed * dt * strafefriction * max(PHYS_STOPSPEED(strafeplayer) / speed, 1);
395 strafespeed = max(speed - frictionspeed, 0);
403 minspeed = autocvar_hud_panel_strafehud_switch_minspeed < 0 ? bestspeed + frictionspeed : autocvar_hud_panel_strafehud_switch_minspeed;
405 // get current strafing angle ranging from -180° to +180°
406 if(!autocvar__hud_configure)
410 // calculate view angle relative to the players current velocity direction
411 angle = vel_angle - view_angle;
413 // if the angle goes above 180° or below -180° wrap it to the opposite side since we want the interior angle
414 if (angle > 180) angle -= 360;
415 else if(angle < -180) angle += 360;
417 // determine whether the player is strafing forwards or backwards
418 // if the player isn't strafe turning use forwards/backwards keys to determine direction
419 if(fabs(wishangle) != 90)
425 else if(keys_fwd < 0)
431 fwd = fabs(angle) <= 90;
434 // otherwise determine by examining the strafe angle
437 if(wishangle < 0) // detect direction using wishangle since the direction is not yet set
439 fwd = angle <= -wishangle;
443 fwd = angle >= -wishangle;
447 // shift the strafe angle by 180° when strafing backwards
450 if(angle < 0) angle += 180;
454 // don't make the angle indicator switch side too much at ±180° if anti flicker is turned on
455 if(angle > (180 - antiflicker_angle) || angle < (-180 + antiflicker_angle))
457 straight_overturn = true;
465 else // simulate turning for HUD setup
468 if(autocvar__hud_panel_strafehud_demo && ((time - demo_time) >= .025))
471 demo_angle += demo_direction;
472 if(fabs(demo_angle) >= 55)
474 demo_direction = -demo_direction;
478 wishangle = 45 * (demo_angle > 0 ? 1 : -1);
481 // invert the wish angle when strafing backwards
484 wishangle = -wishangle;
487 // flip angles if v_flipped is enabled
488 if(autocvar_v_flipped)
491 wishangle = -wishangle;
494 // determine whether the player is strafing left or right
497 direction = wishangle > 0 ? 1 : -1;
501 direction = (angle > antiflicker_angle && angle < (180 - antiflicker_angle)) ? 1 : (angle < -antiflicker_angle && angle > (-180 + antiflicker_angle)) ? -1 : 0;
504 // best angle to strafe at
505 bestangle = (strafespeed > bestspeed ? acos(bestspeed / strafespeed) * RAD2DEG * (direction < 0 ? -1 : 1) : 0);
506 prebestangle = (strafespeed > movespeed ? acos(movespeed / strafespeed) * RAD2DEG * (direction < 0 ? -1 : 1) : 0); // in case of ground friction we may decelerate if the acceleration is smaller than the speed loss from friction
507 odd_bestangle = -bestangle - wishangle;
508 bestangle -= wishangle;
509 prebestangle -= wishangle;
511 // various offsets and size calculations of hud indicator elements
512 // how much is hidden by the current hud angle
513 hidden_width = (360 - hudangle) / hudangle * panel_size.x;
515 currentangle_size.x = autocvar_hud_panel_strafehud_angle_width;
516 currentangle_size.y = autocvar_hud_panel_strafehud_angle_height;
517 if(!autocvar_hud_panel_strafehud_uncapped)
519 currentangle_size.x = min(currentangle_size.x, 10);
520 currentangle_size.y = min(currentangle_size.y, 10);
522 currentangle_size.x *= panel_size.x;
523 currentangle_size.y *= panel_size.y;
524 if(!autocvar_hud_panel_strafehud_uncapped)
526 currentangle_size.x = max(currentangle_size.x, 1);
527 currentangle_size.y = max(currentangle_size.y, 1);
531 currentangle_size.y = max(currentangle_size.y, 0);
535 currentangle_offset = angle/hudangle * panel_size.x;
539 currentangle_offset = bound(-hudangle/2, angle, hudangle/2)/hudangle * panel_size.x + panel_size.x/2;
541 // best strafe acceleration angle
542 bestangle_offset = bestangle/hudangle * panel_size.x + panel_size.x/2;
543 switch_bestangle_offset = -bestangle/hudangle * panel_size.x + panel_size.x/2;
544 bestangle_width = panel_size.x * autocvar_hud_panel_strafehud_switch_width;
545 if(!autocvar_hud_panel_strafehud_uncapped)
546 bestangle_width = max(bestangle_width, 1);
548 if(((angle > -wishangle && direction < 0) || (angle < -wishangle && direction > 0)) && (direction != 0))
551 odd_bestangle_offset = odd_bestangle/hudangle * panel_size.x + panel_size.x/2;
552 switch_odd_bestangle_offset = (odd_bestangle+bestangle*2)/hudangle * panel_size.x + panel_size.x/2;
554 // direction indicator
555 direction_size_vertical.x = autocvar_hud_panel_strafehud_direction_width;
556 if(!autocvar_hud_panel_strafehud_uncapped)
557 direction_size_vertical.x = min(direction_size_vertical.x, 1);
558 direction_size_vertical.x *= panel_size.y;
559 if(!autocvar_hud_panel_strafehud_uncapped)
560 direction_size_vertical.x = max(direction_size_vertical.x, 1);
561 direction_size_vertical.y = panel_size.y + direction_size_vertical.x*2;
562 direction_size_horizontal.x = panel_size.x * min(autocvar_hud_panel_strafehud_direction_length, .5);
563 direction_size_horizontal.y = direction_size_vertical.x;
565 overturn_width = 180/hudangle * panel_size.x;
567 // the neutral zone fills the whole strafe bar
571 if(panel_size.x > 0 && panel_size.y > 0 && autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha > 0)
573 switch(autocvar_hud_panel_strafehud_style)
577 drawfill(panel_pos, panel_size, autocvar_hud_panel_strafehud_bar_neutral_color, autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
581 HUD_Panel_DrawProgressBar(panel_pos, panel_size, "progressbar", 1, 0, 0, autocvar_hud_panel_strafehud_bar_neutral_color, autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
587 // calculate various zones of the strafe-o-meter
588 accelzone_width = (90 - fabs(bestangle + wishangle))/hudangle * panel_size.x;
589 preaccelzone_width = (fabs(bestangle - prebestangle))/hudangle * panel_size.x;
590 overturn_offset = accelzone_width + preaccelzone_width;
591 accelzone_right_offset = preaccelzone_width;
592 accelzone_left_offset = overturn_offset + overturn_width;
593 preaccelzone_right_offset = 0;
594 preaccelzone_left_offset = accelzone_left_offset + accelzone_width;
595 neutral_width = 360/hudangle * panel_size.x - accelzone_width*2 - preaccelzone_width*2 - overturn_width;
596 neutral_offset = direction < 0 ? preaccelzone_left_offset + preaccelzone_width : -neutral_width;
598 // shift hud if operating in view angle centered mode
601 shift_offset = -currentangle_offset;
602 bestangle_offset += shift_offset;
603 switch_bestangle_offset += shift_offset;
604 odd_bestangle_offset += shift_offset;
605 switch_odd_bestangle_offset += shift_offset;
607 if(direction < 0) shift_offset += -360/hudangle * panel_size.x;
608 // calculate how far off-center the strafe zones currently are
609 shift_offset += (panel_size.x + neutral_width)/2 - wishangle/hudangle * panel_size.x;
610 // shift strafe zones into correct place
611 neutral_offset += shift_offset;
612 accelzone_left_offset += shift_offset;
613 accelzone_right_offset += shift_offset;
614 preaccelzone_left_offset += shift_offset;
615 preaccelzone_right_offset += shift_offset;
616 overturn_offset += shift_offset;
618 // draw left acceleration zone
619 HUD_Panel_DrawStrafeHUD(accelzone_left_offset, accelzone_width, autocvar_hud_panel_strafehud_bar_accel_color, autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 1);
620 HUD_Panel_DrawStrafeHUD(preaccelzone_left_offset, preaccelzone_width, autocvar_hud_panel_strafehud_bar_preaccel_color, autocvar_hud_panel_strafehud_bar_preaccel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 0);
622 // draw right acceleration zone
623 HUD_Panel_DrawStrafeHUD(accelzone_right_offset, accelzone_width, autocvar_hud_panel_strafehud_bar_accel_color, autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 2);
624 HUD_Panel_DrawStrafeHUD(preaccelzone_right_offset, preaccelzone_width, autocvar_hud_panel_strafehud_bar_preaccel_color, autocvar_hud_panel_strafehud_bar_preaccel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 0);
626 // draw overturn zone (technically incorrect, acceleration decreases after 90 degrees but speed loss happens a little bit after 90 degrees, however due to sv_airstopaccelerate that's hard to calculate)
627 HUD_Panel_DrawStrafeHUD(overturn_offset, overturn_width, autocvar_hud_panel_strafehud_bar_overturn_color, autocvar_hud_panel_strafehud_bar_overturn_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 3);
630 HUD_Panel_DrawStrafeHUD(neutral_offset, neutral_width, autocvar_hud_panel_strafehud_bar_neutral_color, autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 0);
632 if(speed >= minspeed && bestangle_width > 0) // only draw indicators if minspeed is reached
634 // draw the switch indicator(s)
635 float offset = !odd_angles ? bestangle_offset : odd_bestangle_offset;
636 float switch_offset = !odd_angles ? switch_bestangle_offset : switch_odd_bestangle_offset;
638 // remove switch indicator width from offset
642 offset -= bestangle_width;
644 switch_offset -= bestangle_width;
649 switch_offset -= bestangle_width;
651 offset -= bestangle_width;
654 HUD_Panel_DrawStrafeHUD(switch_offset, bestangle_width, autocvar_hud_panel_strafehud_switch_color, autocvar_hud_panel_strafehud_switch_alpha * panel_fg_alpha, 0, 0);
655 if(direction == 0) HUD_Panel_DrawStrafeHUD(offset, bestangle_width, autocvar_hud_panel_strafehud_switch_color, autocvar_hud_panel_strafehud_switch_alpha * panel_fg_alpha, 0, 0);
660 slickdetector_height = max(autocvar_hud_panel_strafehud_slickdetector_height, 0);
661 if(!autocvar_hud_panel_strafehud_uncapped)
662 slickdetector_height = min(slickdetector_height, 1);
663 slickdetector_height *= panel_size.y;
664 if(autocvar_hud_panel_strafehud_slickdetector_range > 0 && autocvar_hud_panel_strafehud_slickdetector_alpha > 0 && slickdetector_height > 0 && panel_size.x > 0)
666 float slicksteps = max(autocvar_hud_panel_strafehud_slickdetector_granularity, 0);
667 bool slickdetected = false;
669 if(!autocvar_hud_panel_strafehud_uncapped)
670 slicksteps = min(slicksteps, 4);
671 slicksteps = 90 / 2 ** slicksteps;
673 if(islocal) slickdetected = (IS_ONSLICK(strafeplayer) || (IS_ONGROUND(strafeplayer) && (PHYS_FRICTION(strafeplayer) == 0))); // don't need to traceline if already touching slick
675 // traceline into every direction
676 trace_dphitq3surfaceflags = 0;
677 vector traceorigin = strafeplayer.origin + '0 0 1' * strafeplayer.mins.z;
678 for(float i = 0; i < 360 && !slickdetected; i += slicksteps)
682 slickoffset.z = -cos(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
683 slickrotate = sin(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
684 if(i != 0 && i != 180)
686 for(float j = 0; j < 180 && !slickdetected; j += slicksteps)
688 slickoffset.x = sin(j * DEG2RAD) * slickrotate;
689 slickoffset.y = cos(j * DEG2RAD) * slickrotate;
691 traceline(traceorigin, traceorigin + slickoffset, MOVE_NOMONSTERS, strafeplayer);
692 if((PHYS_FRICTION(strafeplayer) == 0 && trace_fraction < 1) || trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK) slickdetected = true;
697 slickoffset.x = slickoffset.y = 0;
698 traceline(traceorigin, traceorigin + slickoffset, MOVE_NOMONSTERS, strafeplayer);
699 if((PHYS_FRICTION(strafeplayer) == 0 && trace_fraction < 1) || trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK) slickdetected = true;
703 // if a traceline hit a slick surface
706 vector slickdetector_size = panel_size;
707 slickdetector_size.y = slickdetector_height;
708 // top horizontal line
709 drawfill(panel_pos - eY * slickdetector_size.y, slickdetector_size, autocvar_hud_panel_strafehud_slickdetector_color, autocvar_hud_panel_strafehud_slickdetector_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
710 // bottom horizontal line
711 drawfill(panel_pos + eY * panel_size.y, slickdetector_size, autocvar_hud_panel_strafehud_slickdetector_color, autocvar_hud_panel_strafehud_slickdetector_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
715 if(direction != 0 && direction_size_vertical.x > 0 && autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha > 0)
717 bool indicator_direction = direction < 0;
718 // invert left/right when strafing backwards or when strafing towards the opposite side indicated by the direction variable
719 // if both conditions are true then it's inverted twice hence not inverted at all
720 if(!fwd != odd_angles)
722 indicator_direction = !indicator_direction;
724 // draw the direction indicator caps at the sides of the hud
726 if(direction_size_vertical.y > 0) drawfill(panel_pos - eY * direction_size_horizontal.y + eX * (indicator_direction ? -direction_size_vertical.x : panel_size.x), direction_size_vertical, autocvar_hud_panel_strafehud_direction_color, autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
727 // top horizontal line
728 drawfill(panel_pos + eX * (indicator_direction ? 0 : panel_size.x - direction_size_horizontal.x) - eY * direction_size_horizontal.y, direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color, autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
729 // bottom horizontal line
730 drawfill(panel_pos + eX * (indicator_direction ? 0 : panel_size.x - direction_size_horizontal.x) + eY * panel_size.y, direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color, autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
733 if(strafespeed <= bestspeed && !immobile)
735 bestangle_anywhere = true; // moving forward should suffice to gain speed
738 // draw the actual strafe angle
739 if(!bestangle_anywhere && !immobile) // player gains speed with strafing
741 if((direction > 0 && (angle >= bestangle || angle <= -(bestangle + wishangle*2))) ||
742 (direction < 0 && (angle <= bestangle || angle >= -(bestangle + wishangle*2))))
743 currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
746 if(fabs(angle + wishangle) > 90) // player is overturning
748 currentangle_color = autocvar_hud_panel_strafehud_angle_overturn_color;
750 else if(bestangle_anywhere) // player gains speed without strafing
752 currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
755 if(mode == 0 || straight_overturn)
757 currentangle_offset = panel_size.x/2;
760 if(autocvar_hud_panel_strafehud_style == 2 && !immobile)
762 float moveangle = angle + wishangle;
763 float strafeangle = (bestangle + wishangle) * (direction < 0 ? -1 : 1);
764 float strafe_ratio = 0;
765 if(fabs(moveangle) > 90)
767 strafe_ratio = -((fabs(moveangle) - 90) / 90);
768 if(strafe_ratio < -1) strafe_ratio = -2 - strafe_ratio;
772 if(moveangle >= strafeangle)
774 strafe_ratio = 1 - (moveangle - strafeangle) / (90 - strafeangle);
776 else if(moveangle <= -strafeangle)
778 strafe_ratio = 1 - (moveangle + strafeangle) / (-90 + strafeangle);
783 currentangle_color = StrafeHUD_mixColors(autocvar_hud_panel_strafehud_angle_neutral_color, autocvar_hud_panel_strafehud_angle_overturn_color, -strafe_ratio);
787 currentangle_color = StrafeHUD_mixColors(autocvar_hud_panel_strafehud_angle_neutral_color, autocvar_hud_panel_strafehud_angle_accel_color, strafe_ratio);
791 float angleheight_offset = currentangle_size.y;
792 float ghost_offset = 0;
793 if(autocvar_hud_panel_strafehud_bestangle && direction != 0)
795 ghost_offset = !odd_angles ? bestangle_offset : odd_bestangle_offset;
796 if(ghost_offset < 0) ghost_offset = 0;
797 if(ghost_offset > panel_size.x) ghost_offset = panel_size.x;
800 switch(autocvar_hud_panel_strafehud_angle_style)
803 if(currentangle_size.x > 0 && currentangle_size.y > 0)
805 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (ghost_offset - currentangle_size.x/2), currentangle_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
806 drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (currentangle_offset - currentangle_size.x/2), currentangle_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
810 if(currentangle_size.x > 0 && currentangle_size.y > 0)
812 vector line_size = currentangle_size;
813 line_size.y = currentangle_size.y / (bound(2, autocvar_hud_panel_strafehud_angle_dashes, currentangle_size.y)*2-1);
814 for(float i = 0; i < currentangle_size.y; i += line_size.y*2)
816 if(i + line_size.y*2 >= currentangle_size.y) line_size.y = currentangle_size.y - i;
817 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2 - i) + eX * (ghost_offset - line_size.x/2), line_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
818 drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2 - i) + eX * (currentangle_offset - line_size.x/2), line_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
824 // don't offset text and arrows if the angle indicator line isn't drawn
825 angleheight_offset = panel_size.y;
828 if(autocvar_hud_panel_strafehud_angle_arrow > 0)
832 if(autocvar_hud_panel_strafehud_angle_arrow == 1 || autocvar_hud_panel_strafehud_angle_arrow >= 3)
834 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2) + eX * ghost_offset, arrow_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, true);
835 StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2) + eX * currentangle_offset, arrow_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, true);
837 if(autocvar_hud_panel_strafehud_angle_arrow >= 2)
839 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2 + angleheight_offset) + eX * ghost_offset, arrow_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, false);
840 StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2 + angleheight_offset) + eX * currentangle_offset, arrow_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, false);
845 draw_beginBoldFont();
846 // show speed when crossing the start trigger
847 if(autocvar_hud_panel_strafehud_startspeed_fade > 0)
849 static float startspeed = 0, starttime = 0; // displayed value and timestamp for fade out
851 if((race_nextcheckpoint == 1) || (race_checkpoint == 254 && race_nextcheckpoint == 255)) // check if the start trigger was hit (will also trigger if the finish trigger was hit if those have the same ID)
853 if(starttime != race_checkpointtime)
855 starttime = race_checkpointtime;
860 if((starttime > 0) && ((time - starttime) <= autocvar_hud_panel_strafehud_startspeed_fade) && autocvar_hud_panel_strafehud_startspeed_size > 0)
862 float text_alpha = cos(((time - starttime) / autocvar_hud_panel_strafehud_startspeed_fade) * 90 * DEG2RAD); // fade non-linear like the physics panel does
863 vector startspeed_size = panel_size;
864 startspeed_size.y = autocvar_hud_panel_strafehud_startspeed_size;
865 if(!autocvar_hud_panel_strafehud_uncapped)
866 startspeed_size.y = min(startspeed_size.y, 10);
867 startspeed_size.y *= panel_size.y;
868 if(!autocvar_hud_panel_strafehud_uncapped)
869 startspeed_size.y = max(startspeed_size.y, 1);
871 float text_offset = 0;
872 if((autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha > 0) || (autocvar_hud_panel_strafehud_bestangle && autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha > 0))
874 text_offset = (angleheight_offset - panel_size.y) / 2;
875 if(arrow_size > 0 && autocvar_hud_panel_strafehud_angle_arrow >= 2)
876 text_offset += arrow_size;
877 // make sure text doesn't draw inside the strafehud bar
878 text_offset = max(text_offset, 0);
881 string speed_unit = GetSpeedUnit(autocvar_hud_panel_strafehud_unit);
882 drawstring_aspect(panel_pos + eY * (panel_size.y + text_offset), strcat(ftos_decimals(startspeed * speed_conversion_factor, 2), autocvar_hud_panel_strafehud_unit_show ? speed_unit : ""), startspeed_size, autocvar_hud_panel_strafehud_startspeed_color, text_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
886 // show height achieved by a single jump
887 // FIXME: checking z position differences is unreliable (warpzones, teleporter, kill, etc) but using velocity to calculate jump height would be
888 // inaccurate in hud code (possibly different tick rate, doesn't run when hud isn't drawn, rounding errors)
889 if(autocvar_hud_panel_strafehud_jumpheight_fade > 0 && autocvar_hud_panel_strafehud_jumpheight_size > 0)
891 static float height_min = 0, height_max = 0; // ground and peak of jump z coordinates
892 static float jumpheight = 0, jumptime = 0; // displayed value and timestamp for fade out
894 // tries to catch kill and spectate but those are not reliable
895 if((strafeplayer.velocity.z <= 0) || IS_ONGROUND(strafeplayer) || swimming || IS_DEAD(strafeplayer) || spectating)
897 height_min = height_max = strafeplayer.origin.z;
899 else if(strafeplayer.origin.z > height_max)
901 height_max = strafeplayer.origin.z;
902 float jumpheight_new = (height_max - height_min) * length_conversion_factor;
904 if(jumpheight_new > max(autocvar_hud_panel_strafehud_jumpheight_min, 0))
906 jumpheight = jumpheight_new;
911 if((jumptime > 0) && (time - jumptime) <= autocvar_hud_panel_strafehud_jumpheight_fade)
913 float text_alpha = cos(((time - jumptime) / autocvar_hud_panel_strafehud_jumpheight_fade) * 90 * DEG2RAD); // fade non-linear like the physics panel does
914 vector jumpheight_size = panel_size;
915 jumpheight_size.y = autocvar_hud_panel_strafehud_jumpheight_size;
916 if(!autocvar_hud_panel_strafehud_uncapped)
917 jumpheight_size.y = min(jumpheight_size.y, 10);
918 jumpheight_size.y *= panel_size.y;
919 if(!autocvar_hud_panel_strafehud_uncapped)
920 jumpheight_size.y = max(jumpheight_size.y, 1);
922 float text_offset = 0;
923 if((autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha > 0) || (autocvar_hud_panel_strafehud_bestangle && autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha > 0))
925 text_offset = (angleheight_offset - panel_size.y) / 2;
926 if(arrow_size > 0 && autocvar_hud_panel_strafehud_angle_arrow == 1 || autocvar_hud_panel_strafehud_angle_arrow >= 3)
927 text_offset += arrow_size;
928 // make sure text doesn't draw inside the strafehud bar
929 text_offset = max(text_offset, 0);
932 string length_unit = GetLengthUnit(autocvar_hud_panel_strafehud_unit);
933 drawstring_aspect(panel_pos - eY * (jumpheight_size.y + text_offset), strcat(ftos_decimals(jumpheight, length_decimals), autocvar_hud_panel_strafehud_unit_show ? length_unit : ""), jumpheight_size, autocvar_hud_panel_strafehud_jumpheight_color, text_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
940 // functions to make hud elements align perfectly in the hud area
941 void HUD_Panel_DrawStrafeHUD(float offset, float width, vector color, float alpha, int type, int gradientType)
943 float mirror_offset, mirror_width;
944 vector size = panel_size;
945 vector mirror_size = panel_size;
947 float gradient_offset, gradient_mirror_offset;
948 float overflow_width = 0, overflow_mirror_width = 0;
950 float original_width = width;
952 if(alpha <= 0 && type != 2 || width <= 0) return;
954 if(type == 2 && gradientType == 0) type = 0;
958 mirror_width = min(fabs(offset), width);
959 mirror_offset = panel_size.x + hidden_width - fabs(offset);
965 mirror_width = min(offset + width - panel_size.x - hidden_width, width);
966 mirror_offset = max(offset - panel_size.x - hidden_width, 0);
969 if(width < 0) width = 0;
970 if((offset + width) > panel_size.x)
972 overflow_width = (offset + width) - panel_size.x;
973 width = panel_size.x - offset;
975 if(mirror_offset < 0)
977 mirror_width += mirror_offset;
981 if(mirror_width < 0) mirror_width = 0;
982 if((mirror_offset + mirror_width) > panel_size.x)
984 overflow_mirror_width = (mirror_offset + mirror_width) - panel_size.x;
985 mirror_width = panel_size.x - mirror_offset;
988 if(direction < 0) // swap mirror and non-mirror values if direction points left
990 offset += mirror_offset;
991 mirror_offset = offset - mirror_offset;
992 offset -= mirror_offset;
994 width += mirror_width;
995 mirror_width = width - mirror_width;
996 width -= mirror_width;
998 overflow_width += overflow_mirror_width;
999 overflow_mirror_width = overflow_width - overflow_mirror_width;
1000 overflow_width -= overflow_mirror_width;
1004 mirror_size.x = mirror_width;
1009 case 0: // no styling (drawfill)
1010 if(mirror_size.x > 0 && mirror_size.y > 0) drawfill(panel_pos + eX * mirror_offset, mirror_size, color, alpha, DRAWFLAG_NORMAL);
1011 if(size.x > 0 && size.y > 0) drawfill(panel_pos + eX * offset, size, color, alpha, DRAWFLAG_NORMAL);
1014 case 1: // progress bar style
1015 if(mirror_size.x > 0 && mirror_size.y > 0) HUD_Panel_DrawProgressBar(panel_pos + eX * mirror_offset, mirror_size, "progressbar", 1, 0, 0, color, alpha, DRAWFLAG_NORMAL);
1016 if(size.x > 0 && size.y > 0) HUD_Panel_DrawProgressBar(panel_pos + eX * offset, size, "progressbar", 1, 0, 0, color, alpha, DRAWFLAG_NORMAL);
1019 case 2: // gradient style (types: 1 = left, 2 = right, 3 = both)
1020 // determine whether the gradient starts in the mirrored or the non-mirrored area
1021 if(offset == 0 && mirror_offset == 0) gradient_start = width > mirror_width ? 2 : 1;
1022 else if(offset == 0) gradient_start = 2;
1023 else if(mirror_offset == 0) gradient_start = 1;
1024 else gradient_start = 0;
1026 switch(gradient_start)
1029 case 0: // no offset required
1030 gradient_offset = gradient_mirror_offset = 0;
1032 case 1: // offset starts in non-mirrored area, mirrored area requires offset
1033 gradient_offset = 0;
1034 gradient_mirror_offset = original_width - (mirror_width + overflow_mirror_width);
1036 case 2: // offset starts in mirrored area, non-mirrored area requires offset
1037 gradient_offset = original_width - (width + overflow_width);
1038 gradient_mirror_offset = 0;
1041 StrafeHUD_drawGradient(color, autocvar_hud_panel_strafehud_bar_neutral_color, mirror_size, original_width, mirror_offset, alpha, gradient_mirror_offset, gradientType);
1042 StrafeHUD_drawGradient(color, autocvar_hud_panel_strafehud_bar_neutral_color, size, original_width, offset, alpha, gradient_offset, gradientType);
1046 vector StrafeHUD_mixColors(vector color1, vector color2, float ratio)
1049 if(ratio <= 0) return color1;
1050 if(ratio >= 1) return color2;
1051 mixedColor.x = color1.x + (color2.x - color1.x) * ratio;
1052 mixedColor.y = color1.y + (color2.y - color1.y) * ratio;
1053 mixedColor.z = color1.z + (color2.z - color1.z) * ratio;
1057 void StrafeHUD_drawGradient(vector color1, vector color2, vector size, float original_width, float offset, float alpha, float gradientOffset, int gradientType)
1059 float color_ratio, alpha1, alpha2;
1060 vector gradient_size = size;
1061 alpha1 = bound(0, alpha, 1);
1062 alpha2 = bound(0, autocvar_hud_panel_strafehud_bar_neutral_alpha, 1);
1063 if((alpha1+alpha2) == 0) return;
1064 color_ratio = alpha1/(alpha1+alpha2);
1065 for(int i = 0; i < size.x; ++i)
1067 float ratio, alpha_ratio, combine_ratio1, combine_ratio2;
1068 gradient_size.x = size.x - i < 1 ? size.x - i : 1;
1069 ratio = (i + gradientOffset) / original_width * (gradientType == 3 ? 2 : 1);
1070 if(ratio > 1) ratio = 2 - ratio;
1071 if(gradientType != 2) ratio = 1 - ratio;
1072 alpha_ratio = alpha1 - (alpha1 - alpha2) * ratio;
1073 combine_ratio1 = ratio*(1-color_ratio);
1074 combine_ratio2 = (1-ratio)*color_ratio;
1075 ratio = (combine_ratio1 + combine_ratio2) == 0 ? 1 : combine_ratio1/(combine_ratio1 + combine_ratio2);
1076 if(alpha_ratio > 0) drawfill(panel_pos + eX * (offset + i), gradient_size, StrafeHUD_mixColors(color1, color2, ratio), alpha_ratio, DRAWFLAG_NORMAL);
1080 // draw the strafe arrows (inspired by drawspritearrow() in common/mutators/mutator/waypoints/waypointsprites.qc)
1081 void StrafeHUD_drawStrafeArrow(vector origin, float size, vector color, float alpha, bool flipped)
1083 if(flipped) origin -= size*eY;
1084 R_BeginPolygon("", DRAWFLAG_NORMAL, true);
1085 R_PolygonVertex(origin + (flipped ? size*eY : '0 0 0') , '0 0 0', color, alpha);
1086 R_PolygonVertex(origin + (flipped ? '0 0 0' : size*eY) - size*eX, '0 0 0', color, alpha);
1087 R_PolygonVertex(origin + (flipped ? '0 0 0' : size*eY) + size*eX, '0 0 0', color, alpha);
1091 // length unit conversion (km and miles are only included to match the GetSpeedUnit* functions)
1092 float GetLengthUnitFactor(int length_unit)
1098 case 2: return 0.0254;
1099 case 3: return 0.0254 * 0.001;
1100 case 4: return 0.0254 * 0.001 * 0.6213711922;
1101 case 5: return 0.0254 * 0.001 * 0.5399568035;
1105 string GetLengthUnit(int length_unit)
1109 // translator-friendly strings without the initial space
1111 case 1: return strcat(" ", _("qu"));
1112 case 2: return strcat(" ", _("m"));
1113 case 3: return strcat(" ", _("km"));
1114 case 4: return strcat(" ", _("mi"));
1115 case 5: return strcat(" ", _("nmi"));