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 // provide basic panel cvars to old clients
27 // TODO remove them after a future release (0.8.2+)
28 noref string autocvar_hud_panel_strafehud_pos = "0.320000 0.570000";
29 noref string autocvar_hud_panel_strafehud_size = "0.360000 0.020000";
30 noref string autocvar_hud_panel_strafehud_bg = "0";
31 noref string autocvar_hud_panel_strafehud_bg_color = "";
32 noref string autocvar_hud_panel_strafehud_bg_color_team = "";
33 noref string autocvar_hud_panel_strafehud_bg_alpha = "0.7";
34 noref string autocvar_hud_panel_strafehud_bg_border = "";
35 noref string autocvar_hud_panel_strafehud_bg_padding = "";
42 // generic hud routines
43 if(!autocvar__hud_configure)
45 if(!autocvar_hud_panel_strafehud ||
46 (spectatee_status == -1 && (autocvar_hud_panel_strafehud == 1 || autocvar_hud_panel_strafehud == 3)) ||
47 (autocvar_hud_panel_strafehud == 3 && !MUTATOR_CALLHOOK(HUD_StrafeHUD_showoptional))) return;
50 HUD_Panel_LoadCvars();
52 if(autocvar_hud_panel_strafehud_dynamichud)
65 panel_pos += '1 1 0' * panel_bg_padding;
66 panel_size -= '2 2 0' * panel_bg_padding;
69 // find out whether the local csqcmodel entity is valid
70 if(spectatee_status > 0 || isdemo())
73 strafeplayer = CSQCModel_server2csqc(player_localentnum - 1);
78 strafeplayer = csqcplayer;
82 if(csqcplayer && strafeplayer)
85 static float demo_angle = -37;
86 static float demo_direction = 1;
87 static float demo_time = 0;
88 static bool state_onground = false;
89 static float state_onground_time = 0;
90 static bool state_strafekeys = false;
91 static float state_strafekeys_time = 0;
92 static bool turn = false;
93 static float turnangle;
94 static float turnspeed;
95 static float turnaccel;
96 static bool fwd = true;
97 static float strafe_dt_time = 0;
98 static int strafe_dt_count = 0;
99 static float strafe_dt_sum = 0;
100 static float strafe_dt_avg = 0;
103 bool onground = islocal ? IS_ONGROUND(strafeplayer) : !(strafeplayer.anim_implicit_state & ANIMIMPLICITSTATE_INAIR);
105 bool swimming = strafeplayer.waterlevel >= WATERLEVEL_SWIMMING;
106 bool spectating = entcs_GetSpecState(strafeplayer.sv_entnum) == ENTCS_SPEC_PURE;
107 float speed = !autocvar__hud_configure ? vlen(vec2(csqcplayer.velocity)) : 1337; // use local csqcmodel entity for this even when spectating, flickers too much otherwise
108 float crouch_mod = IS_DUCKED(csqcplayer) && !swimming ? .5 : 1;
109 float water_mod = swimming ? .7 : 1; // very simplified water physics, the hud will not work well (and is not supposed to) while swimming
110 float maxspeed_phys = onground ? PHYS_MAXSPEED(strafeplayer) : PHYS_MAXAIRSPEED(strafeplayer);
111 float maxspeed = !autocvar__hud_configure ? maxspeed_phys * crouch_mod * water_mod : 320;
114 float maxaccel_phys = onground ? PHYS_ACCELERATE(strafeplayer) : PHYS_AIRACCELERATE(strafeplayer);
115 float maxaccel = !autocvar__hud_configure ? maxaccel_phys * crouch_mod * water_mod : 1;
116 float frametime_phys;
117 float vel_angle = vectoangles(strafeplayer.velocity).y - (vectoangles(strafeplayer.velocity).y > 180 ? 360 : 0); // change the range from 0° - 360° to -180° - 180° to match how view_angle represents angles
118 float view_angle = PHYS_INPUT_ANGLES(strafeplayer).y;
120 vector movement = PHYS_INPUT_MOVEVALUES(strafeplayer);
121 int keys = STAT(PRESSED_KEYS);
126 int mode = autocvar_hud_panel_strafehud_mode >= 0 && autocvar_hud_panel_strafehud_mode <= 1 ? autocvar_hud_panel_strafehud_mode : 0;
127 float speed_conversion_factor = GetSpeedUnitFactor(autocvar_hud_panel_strafehud_unit);
128 float length_conversion_factor = GetLengthUnitFactor(autocvar_hud_panel_strafehud_unit);
129 int length_decimals = autocvar_hud_panel_strafehud_unit >= 3 && autocvar_hud_panel_strafehud_unit <= 5 ? 6 : 2; // use more decimals when displaying km or miles
130 float antiflicker_angle = bound(0, autocvar_hud_panel_strafehud_antiflicker_angle, 180);
131 float antiflicker_speed = max(0, autocvar_hud_panel_strafehud_antiflicker_speed);
133 float shift_offset = 0;
134 bool straight_overturn = false;
135 bool immobile = speed <= (swimming ? antiflicker_speed : 0);
137 float neutral_offset;
139 vector currentangle_color = autocvar_hud_panel_strafehud_angle_neutral_color;
140 float currentangle_offset;
141 vector currentangle_size = '0 0 0';
145 bool bestangle_anywhere = false;
146 float bestangle_offset;
147 float switch_bestangle_offset;
148 bool odd_angles = false;
149 float odd_bestangle_offset = 0;
150 float switch_odd_bestangle_offset = 0;
151 float bestangle_width;
152 float accelzone_left_offset;
153 float accelzone_right_offset;
154 float accelzone_width;
155 float preaccelzone_left_offset;
156 float preaccelzone_right_offset;
157 float preaccelzone_width;
158 float overturn_offset;
159 float overturn_width;
160 float slickdetector_height;
161 vector direction_size_vertical = '0 0 0';
162 vector direction_size_horizontal = '0 0 0';
163 float range_minangle;
164 float arrow_size = max(panel_size.y * min(autocvar_hud_panel_strafehud_angle_arrow_size, 10), 0); // there's only one size cvar for the arrows, they will always have a 45° angle to ensure proper rendering without antialiasing
166 if(!autocvar_hud_panel_strafehud_uncapped)
167 arrow_size = max(arrow_size, 1);
169 // determine frametime
170 if((csqcplayer_status == CSQCPLAYERSTATUS_PREDICTED) && (input_timelength > 0))
171 frametime_phys = input_timelength;
173 frametime_phys = ticrate;
175 if(frametime_phys > .05) // server splits frames longer than 50 ms into two moves
178 // calculate average frametime
179 strafe_dt_sum += frametime_phys;
182 if(((time - strafe_dt_time) > autocvar_hud_panel_strafehud_fps_update) || (strafe_dt_time == 0))
184 strafe_dt_avg = strafe_dt_sum / strafe_dt_count;
186 strafe_dt_time = time;
187 strafe_dt_count = strafe_dt_sum = 0;
190 // determine whether the player is pressing forwards or backwards keys
191 if(islocal) // if entity is local player
197 else if(movement.x < 0)
206 else // alternatively determine direction by querying pressed keys
208 if((keys & KEY_FORWARD) && !(keys & KEY_BACKWARD))
212 else if(!(keys & KEY_FORWARD) && (keys & KEY_BACKWARD))
222 // determine player wishdir
223 if(islocal) // if entity is local player
231 else if(movement.y > 0)
248 wishangle = RAD2DEG * atan2(movement.y, movement.x);
249 // wrap the wish angle if it exceeds ±90°
250 if(fabs(wishangle) > 90)
252 if(wishangle < 0) wishangle += 180;
253 else wishangle -= 180;
254 wishangle = -wishangle;
259 else // alternatively calculate wishdir by querying pressed keys
261 if(keys & KEY_FORWARD || keys & KEY_BACKWARD)
273 else if(!(keys & KEY_RIGHT))
275 wishangle = 0; // wraps at 180°
279 strafekeys = fabs(wishangle) > 45;
281 // determine minimum required angle to display full strafe range
282 range_minangle = fabs(wishangle) % 90; // maximum range is 90 degree
283 if(range_minangle > 45) // minimum angle range is 45
285 range_minangle = 45 - fabs(wishangle) % 45;
287 range_minangle = 90 - range_minangle; // calculate value which is never >90 or <45
288 range_minangle *= 2; // multiply to accommodate for both sides of the hud
290 if(autocvar_hud_panel_strafehud_range == 0)
292 if(autocvar__hud_configure)
298 hudangle = range_minangle; // use minimum angle required if dynamically setting hud angle
303 hudangle = bound(0, fabs(autocvar_hud_panel_strafehud_range), 360); // limit HUD range to 360 degrees, higher values don't make sense
306 // detect air strafe turning
307 if(onground != state_onground)
309 state_onground_time = time;
311 state_onground = onground;
313 if(strafekeys != state_strafekeys)
315 state_strafekeys_time = time;
317 state_strafekeys = strafekeys;
319 if((!strafekeys && vlen(vec2(movement)) > 0) || swimming || autocvar__hud_configure)
325 if((time - state_onground_time) >= autocvar_hud_panel_strafehud_timeout_ground) // timeout for strafe jumping in general
330 else // air strafe only
334 if(((time - state_onground_time) >= autocvar_hud_panel_strafehud_timeout_air) || (keys & KEY_JUMP)) // timeout for slick ramps
336 turn = true; // CPMA turning
337 turnangle = wishangle;
339 // calculate the maximum air strafe speed and acceleration
340 if(PHYS_MAXAIRSPEED(strafeplayer) == 0){
343 else if(PHYS_MAXAIRSTRAFESPEED(strafeplayer) == 0 || PHYS_MAXAIRSPEED(strafeplayer) <= PHYS_MAXAIRSTRAFESPEED(strafeplayer)){
344 maxspeed = PHYS_MAXAIRSPEED(strafeplayer);
347 maxspeed = PHYS_MAXAIRSPEED(strafeplayer) * pow(fabs(PHYS_MAXAIRSTRAFESPEED(strafeplayer) / PHYS_MAXAIRSPEED(strafeplayer)), 1 - (90 - fabs(wishangle)) / 45); // no modifiers here because they don't affect air strafing
349 turnspeed = vlen(vec2(movement));
350 if(turnspeed == 0) turnspeed = maxspeed;
351 else turnspeed = min(turnspeed, maxspeed);
353 if(PHYS_AIRACCELERATE(strafeplayer) == 0){
356 else if(PHYS_AIRSTRAFEACCELERATE(strafeplayer) == 0 || PHYS_AIRACCELERATE(strafeplayer) <= PHYS_AIRSTRAFEACCELERATE(strafeplayer)){
357 maxaccel = PHYS_AIRACCELERATE(strafeplayer);
360 maxaccel = PHYS_AIRACCELERATE(strafeplayer) * pow(fabs(PHYS_AIRSTRAFEACCELERATE(strafeplayer) / PHYS_AIRACCELERATE(strafeplayer)), 1 - (90 - fabs(wishangle)) / 45); // no modifiers here because they don't affect air strafing
362 turnaccel = maxaccel;
365 else if((time - state_strafekeys_time) >= autocvar_hud_panel_strafehud_timeout_turn) // timeout for jumping with strafe keys only
370 if(turn && (onground || !strafekeys)) // retain last state until strafe turning times out
372 wishangle = turnangle;
373 movespeed = turnspeed;
374 maxaccel = turnaccel;
377 movespeed = vlen(vec2(movement));
378 if(movespeed == 0) movespeed = maxspeed;
379 else movespeed = min(movespeed, maxspeed);
383 if((keys & KEY_JUMP) && ((time - state_onground_time) < autocvar_hud_panel_strafehud_timeout_ground)) // if ground timeout hasn't expired yet use air accelerate
385 maxaccel = !autocvar__hud_configure ? PHYS_AIRACCELERATE(strafeplayer) * crouch_mod * water_mod : 1;
390 if(!(keys & KEY_JUMP) && ((time - state_onground_time) < autocvar_hud_panel_strafehud_timeout_air)) // if air timeout hasn't expired yet use ground accelerate
392 maxaccel = !autocvar__hud_configure ? PHYS_ACCELERATE(strafeplayer) * crouch_mod * water_mod : 1;
397 maxaccel *= strafe_dt_avg * movespeed;
398 bestspeed = max(movespeed - maxaccel, 0);
399 minspeed = autocvar_hud_panel_strafehud_switch_minspeed < 0 ? bestspeed : autocvar_hud_panel_strafehud_switch_minspeed;
401 // get current strafing angle ranging from -180° to +180°
402 if(!autocvar__hud_configure)
406 // calculate view angle relative to the players current velocity direction
407 angle = vel_angle - view_angle;
409 // if the angle goes above 180° or below -180° wrap it to the opposite side since we want the interior angle
410 if (angle > 180) angle -= 360;
411 else if(angle < -180) angle += 360;
413 // determine whether the player is strafing forwards or backwards
414 // if the player isn't strafe turning use forwards/backwards keys to determine direction
415 if(fabs(wishangle) != 90)
421 else if(keys_fwd < 0)
427 fwd = fabs(angle) <= 90;
430 // otherwise determine by examining the strafe angle
433 if(wishangle < 0) // detect direction using wishangle since the direction is not yet set
435 fwd = angle <= -wishangle;
439 fwd = angle >= -wishangle;
443 // shift the strafe angle by 180° when strafing backwards
446 if(angle < 0) angle += 180;
450 // don't make the angle indicator switch side too much at ±180° if anti flicker is turned on
451 if(angle > (180 - antiflicker_angle) || angle < (-180 + antiflicker_angle))
453 straight_overturn = true;
461 else // simulate turning for HUD setup
464 if(autocvar__hud_panel_strafehud_demo && ((time - demo_time) >= .025))
467 demo_angle += demo_direction;
468 if(fabs(demo_angle) >= 55)
470 demo_direction = -demo_direction;
474 wishangle = 45 * (demo_angle > 0 ? 1 : -1);
477 // invert the wish angle when strafing backwards
480 wishangle = -wishangle;
483 // flip angles if v_flipped is enabled
484 if(autocvar_v_flipped)
487 wishangle = -wishangle;
490 // determine whether the player is strafing left or right
493 direction = wishangle > 0 ? 1 : -1;
497 direction = (angle > antiflicker_angle && angle < (180 - antiflicker_angle)) ? 1 : (angle < -antiflicker_angle && angle > (-180 + antiflicker_angle)) ? -1 : 0;
500 // best angle to strafe at
501 bestangle = (speed > bestspeed ? acos(bestspeed / speed) * RAD2DEG * (direction < 0 ? -1 : 1) : 0);
502 prebestangle = (speed > movespeed ? acos(movespeed / speed) * RAD2DEG * (direction < 0 ? -1 : 1) : 0);
503 odd_bestangle = -bestangle - wishangle;
504 bestangle -= wishangle;
505 prebestangle -= wishangle;
507 // various offsets and size calculations of hud indicator elements
508 // how much is hidden by the current hud angle
509 hidden_width = (360 - hudangle) / hudangle * panel_size.x;
511 currentangle_size.x = autocvar_hud_panel_strafehud_angle_width;
512 currentangle_size.y = autocvar_hud_panel_strafehud_angle_height;
513 if(!autocvar_hud_panel_strafehud_uncapped)
515 currentangle_size.x = min(currentangle_size.x, 10);
516 currentangle_size.y = min(currentangle_size.y, 10);
518 currentangle_size.x *= panel_size.x;
519 currentangle_size.y *= panel_size.y;
520 if(!autocvar_hud_panel_strafehud_uncapped)
522 currentangle_size.x = max(currentangle_size.x, 1);
523 currentangle_size.y = max(currentangle_size.y, 1);
527 currentangle_size.y = max(currentangle_size.y, 0);
531 currentangle_offset = angle/hudangle * panel_size.x;
535 currentangle_offset = bound(-hudangle/2, angle, hudangle/2)/hudangle * panel_size.x + panel_size.x/2;
537 // best strafe acceleration angle
538 bestangle_offset = bestangle/hudangle * panel_size.x + panel_size.x/2;
539 switch_bestangle_offset = -bestangle/hudangle * panel_size.x + panel_size.x/2;
540 bestangle_width = panel_size.x * autocvar_hud_panel_strafehud_switch_width;
541 if(!autocvar_hud_panel_strafehud_uncapped)
542 bestangle_width = max(bestangle_width, 1);
544 if(((angle > -wishangle && direction < 0) || (angle < -wishangle && direction > 0)) && (direction != 0))
547 odd_bestangle_offset = odd_bestangle/hudangle * panel_size.x + panel_size.x/2;
548 switch_odd_bestangle_offset = (odd_bestangle+bestangle*2)/hudangle * panel_size.x + panel_size.x/2;
550 // direction indicator
551 direction_size_vertical.x = autocvar_hud_panel_strafehud_direction_width;
552 if(!autocvar_hud_panel_strafehud_uncapped)
553 direction_size_vertical.x = min(direction_size_vertical.x, 1);
554 direction_size_vertical.x *= panel_size.y;
555 if(!autocvar_hud_panel_strafehud_uncapped)
556 direction_size_vertical.x = max(direction_size_vertical.x, 1);
557 direction_size_vertical.y = panel_size.y + direction_size_vertical.x*2;
558 direction_size_horizontal.x = panel_size.x * min(autocvar_hud_panel_strafehud_direction_length, .5);
559 direction_size_horizontal.y = direction_size_vertical.x;
561 overturn_width = 180/hudangle * panel_size.x;
563 // the neutral zone fills the whole strafe bar
567 if(panel_size.x > 0 && panel_size.y > 0 && autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha > 0)
569 switch(autocvar_hud_panel_strafehud_style)
573 drawfill(panel_pos, panel_size, autocvar_hud_panel_strafehud_bar_neutral_color, autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
577 HUD_Panel_DrawProgressBar(panel_pos, panel_size, "progressbar", 1, 0, 0, autocvar_hud_panel_strafehud_bar_neutral_color, autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
583 // calculate various zones of the strafe-o-meter
584 accelzone_width = (90 - fabs(bestangle + wishangle))/hudangle * panel_size.x;
585 preaccelzone_width = (fabs(bestangle - prebestangle))/hudangle * panel_size.x;
586 overturn_offset = accelzone_width + preaccelzone_width;
587 accelzone_right_offset = preaccelzone_width;
588 accelzone_left_offset = overturn_offset + overturn_width;
589 preaccelzone_right_offset = 0;
590 preaccelzone_left_offset = accelzone_left_offset + accelzone_width;
591 neutral_width = 360/hudangle * panel_size.x - accelzone_width*2 - preaccelzone_width*2 - overturn_width;
592 neutral_offset = direction < 0 ? preaccelzone_left_offset + preaccelzone_width : -neutral_width;
594 // shift hud if operating in view angle centered mode
597 shift_offset = -currentangle_offset;
598 bestangle_offset += shift_offset;
599 switch_bestangle_offset += shift_offset;
600 odd_bestangle_offset += shift_offset;
601 switch_odd_bestangle_offset += shift_offset;
603 if(direction < 0) shift_offset += -360/hudangle * panel_size.x;
604 // calculate how far off-center the strafe zones currently are
605 shift_offset += (panel_size.x + neutral_width)/2 - wishangle/hudangle * panel_size.x;
606 // shift strafe zones into correct place
607 neutral_offset += shift_offset;
608 accelzone_left_offset += shift_offset;
609 accelzone_right_offset += shift_offset;
610 preaccelzone_left_offset += shift_offset;
611 preaccelzone_right_offset += shift_offset;
612 overturn_offset += shift_offset;
614 // draw left acceleration zone
615 HUD_Panel_DrawStrafeHUD(accelzone_left_offset, accelzone_width, autocvar_hud_panel_strafehud_bar_accel_color, autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 1);
616 HUD_Panel_DrawStrafeHUD(preaccelzone_left_offset, preaccelzone_width, autocvar_hud_panel_strafehud_bar_preaccel_color, autocvar_hud_panel_strafehud_bar_preaccel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 0);
618 // draw right acceleration zone
619 HUD_Panel_DrawStrafeHUD(accelzone_right_offset, accelzone_width, autocvar_hud_panel_strafehud_bar_accel_color, autocvar_hud_panel_strafehud_bar_accel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 2);
620 HUD_Panel_DrawStrafeHUD(preaccelzone_right_offset, preaccelzone_width, autocvar_hud_panel_strafehud_bar_preaccel_color, autocvar_hud_panel_strafehud_bar_preaccel_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 0);
622 // draw overturn zone
623 HUD_Panel_DrawStrafeHUD(overturn_offset, overturn_width, autocvar_hud_panel_strafehud_bar_overturn_color, autocvar_hud_panel_strafehud_bar_overturn_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 3);
626 HUD_Panel_DrawStrafeHUD(neutral_offset, neutral_width, autocvar_hud_panel_strafehud_bar_neutral_color, autocvar_hud_panel_strafehud_bar_neutral_alpha * panel_fg_alpha, autocvar_hud_panel_strafehud_style, 0);
628 if(speed >= minspeed && bestangle_width > 0) // only draw indicators if minspeed is reached
630 // draw the switch indicator(s)
631 float offset = !odd_angles ? bestangle_offset : odd_bestangle_offset;
632 float switch_offset = !odd_angles ? switch_bestangle_offset : switch_odd_bestangle_offset;
634 // remove switch indicator width from offset
638 offset -= bestangle_width;
640 switch_offset -= bestangle_width;
645 switch_offset -= bestangle_width;
647 offset -= bestangle_width;
650 HUD_Panel_DrawStrafeHUD(switch_offset, bestangle_width, autocvar_hud_panel_strafehud_switch_color, autocvar_hud_panel_strafehud_switch_alpha * panel_fg_alpha, 0, 0);
651 if(direction == 0) HUD_Panel_DrawStrafeHUD(offset, bestangle_width, autocvar_hud_panel_strafehud_switch_color, autocvar_hud_panel_strafehud_switch_alpha * panel_fg_alpha, 0, 0);
656 slickdetector_height = max(autocvar_hud_panel_strafehud_slickdetector_height, 0);
657 if(!autocvar_hud_panel_strafehud_uncapped)
658 slickdetector_height = min(slickdetector_height, 1);
659 slickdetector_height *= panel_size.y;
660 if(autocvar_hud_panel_strafehud_slickdetector_range > 0 && autocvar_hud_panel_strafehud_slickdetector_alpha > 0 && slickdetector_height > 0 && panel_size.x > 0)
662 float slicksteps = max(autocvar_hud_panel_strafehud_slickdetector_granularity, 0);
663 bool slickdetected = false;
665 if(!autocvar_hud_panel_strafehud_uncapped)
666 slicksteps = min(slicksteps, 4);
667 slicksteps = 90 / pow(2, slicksteps);
669 if(islocal) slickdetected = IS_ONSLICK(strafeplayer); // don't need to traceline if already touching slick
671 // traceline into every direction
672 trace_dphitq3surfaceflags = 0;
673 for(float i = 0; i < 360 && !slickdetected; i += slicksteps)
677 slickoffset.z = -cos(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
678 slickrotate = sin(i * DEG2RAD) * autocvar_hud_panel_strafehud_slickdetector_range;
679 if(i != 0 && i != 180)
681 for(float j = 0; j < 180 && !slickdetected; j += slicksteps)
683 slickoffset.x = sin(j * DEG2RAD) * slickrotate;
684 slickoffset.y = cos(j * DEG2RAD) * slickrotate;
686 traceline(strafeplayer.origin, strafeplayer.origin + slickoffset, MOVE_WORLDONLY, NULL);
687 if((PHYS_FRICTION(strafeplayer) == 0 && trace_fraction < 1) || trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK) slickdetected = true;
692 slickoffset.x = slickoffset.y = 0;
693 traceline(strafeplayer.origin, strafeplayer.origin + slickoffset, MOVE_WORLDONLY, NULL);
694 if((PHYS_FRICTION(strafeplayer) == 0 && trace_fraction < 1) || trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK) slickdetected = true;
698 // if a traceline hit a slick surface
701 vector slickdetector_size = panel_size;
702 slickdetector_size.y = slickdetector_height;
703 // top horizontal line
704 drawfill(panel_pos - eY * slickdetector_size.y, slickdetector_size, autocvar_hud_panel_strafehud_slickdetector_color, autocvar_hud_panel_strafehud_slickdetector_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
705 // bottom horizontal line
706 drawfill(panel_pos + eY * panel_size.y, slickdetector_size, autocvar_hud_panel_strafehud_slickdetector_color, autocvar_hud_panel_strafehud_slickdetector_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
710 if(direction != 0 && direction_size_vertical.x > 0 && autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha > 0)
712 bool indicator_direction = direction < 0;
713 // invert left/right when strafing backwards or when strafing towards the opposite side indicated by the direction variable
714 // if both conditions are true then it's inverted twice hence not inverted at all
715 if(!fwd != odd_angles)
717 indicator_direction = !indicator_direction;
719 // draw the direction indicator caps at the sides of the hud
721 if(direction_size_vertical.y > 0) drawfill(panel_pos - eY * direction_size_horizontal.y + eX * (indicator_direction ? -direction_size_vertical.x : panel_size.x), direction_size_vertical, autocvar_hud_panel_strafehud_direction_color, autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
722 // top horizontal line
723 drawfill(panel_pos + eX * (indicator_direction ? 0 : panel_size.x - direction_size_horizontal.x) - eY * direction_size_horizontal.y, direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color, autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
724 // bottom horizontal line
725 drawfill(panel_pos + eX * (indicator_direction ? 0 : panel_size.x - direction_size_horizontal.x) + eY * panel_size.y, direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color, autocvar_hud_panel_strafehud_direction_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
728 if(speed <= bestspeed && !immobile)
730 bestangle_anywhere = true; // moving forward should suffice to gain speed
733 // draw the actual strafe angle
734 if(!bestangle_anywhere && !immobile) // player gains speed with strafing
736 if((direction > 0 && (angle >= bestangle || angle <= -(bestangle + wishangle*2))) ||
737 (direction < 0 && (angle <= bestangle || angle >= -(bestangle + wishangle*2))))
738 currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
741 if(fabs(angle + wishangle) > 90) // player is overturning
743 currentangle_color = autocvar_hud_panel_strafehud_angle_overturn_color;
745 else if(bestangle_anywhere) // player gains speed without strafing
747 currentangle_color = autocvar_hud_panel_strafehud_angle_accel_color;
750 if(mode == 0 || straight_overturn)
752 currentangle_offset = panel_size.x/2;
755 if(autocvar_hud_panel_strafehud_style == 2 && !immobile)
757 float moveangle = angle + wishangle;
758 float strafeangle = (bestangle + wishangle) * (direction < 0 ? -1 : 1);
759 float strafe_ratio = 0;
760 if(fabs(moveangle) > 90)
762 strafe_ratio = -((fabs(moveangle) - 90) / 90);
763 if(strafe_ratio < -1) strafe_ratio = -2 - strafe_ratio;
767 if(moveangle >= strafeangle)
769 strafe_ratio = 1 - (moveangle - strafeangle) / (90 - strafeangle);
771 else if(moveangle <= -strafeangle)
773 strafe_ratio = 1 - (moveangle + strafeangle) / (-90 + strafeangle);
778 currentangle_color = StrafeHUD_mixColors(autocvar_hud_panel_strafehud_angle_neutral_color, autocvar_hud_panel_strafehud_angle_overturn_color, -strafe_ratio);
782 currentangle_color = StrafeHUD_mixColors(autocvar_hud_panel_strafehud_angle_neutral_color, autocvar_hud_panel_strafehud_angle_accel_color, strafe_ratio);
786 float angleheight_offset = currentangle_size.y;
787 float ghost_offset = 0;
788 if(autocvar_hud_panel_strafehud_bestangle && direction != 0)
790 ghost_offset = !odd_angles ? bestangle_offset : odd_bestangle_offset;
791 if(ghost_offset < 0) ghost_offset = 0;
792 if(ghost_offset > panel_size.x) ghost_offset = panel_size.x;
795 switch(autocvar_hud_panel_strafehud_angle_style)
798 if(currentangle_size.x > 0 && currentangle_size.y > 0)
800 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (ghost_offset - currentangle_size.x/2), currentangle_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
801 drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (currentangle_offset - currentangle_size.x/2), currentangle_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
805 if(currentangle_size.x > 0 && currentangle_size.y > 0)
807 vector line_size = currentangle_size;
808 line_size.y = currentangle_size.y / (bound(2, autocvar_hud_panel_strafehud_angle_dashes, currentangle_size.y)*2-1);
809 for(float i = 0; i < currentangle_size.y; i += line_size.y*2)
811 if(i + line_size.y*2 >= currentangle_size.y) line_size.y = currentangle_size.y - i;
812 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2 - i) + eX * (ghost_offset - line_size.x/2), line_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
813 drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2 - i) + eX * (currentangle_offset - line_size.x/2), line_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
819 // don't offset text and arrows if the angle indicator line isn't drawn
820 angleheight_offset = panel_size.y;
823 if(autocvar_hud_panel_strafehud_angle_arrow > 0)
827 if(autocvar_hud_panel_strafehud_angle_arrow == 1 || autocvar_hud_panel_strafehud_angle_arrow >= 3)
829 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2) + eX * ghost_offset, arrow_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, true);
830 StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2) + eX * currentangle_offset, arrow_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, true);
832 if(autocvar_hud_panel_strafehud_angle_arrow >= 2)
834 if(autocvar_hud_panel_strafehud_bestangle && direction != 0) StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2 + angleheight_offset) + eX * ghost_offset, arrow_size, autocvar_hud_panel_strafehud_bestangle_color, autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha, false);
835 StrafeHUD_drawStrafeArrow(panel_pos + eY * ((panel_size.y - angleheight_offset) / 2 + angleheight_offset) + eX * currentangle_offset, arrow_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, false);
840 draw_beginBoldFont();
841 // show speed when crossing the start trigger
842 if(autocvar_hud_panel_strafehud_startspeed_fade > 0)
844 static float startspeed = 0, starttime = 0; // displayed value and timestamp for fade out
846 if((race_nextcheckpoint == 1) || (race_checkpoint == 254 && race_nextcheckpoint == 255)) // check if the start trigger was hit (will also trigger if the finish trigger was hit if those have the same ID)
848 if(starttime != race_checkpointtime)
850 starttime = race_checkpointtime;
855 if((starttime > 0) && ((time - starttime) <= autocvar_hud_panel_strafehud_startspeed_fade) && autocvar_hud_panel_strafehud_startspeed_size > 0)
857 float text_alpha = cos(((time - starttime) / autocvar_hud_panel_strafehud_startspeed_fade) * 90 * DEG2RAD); // fade non-linear like the physics panel does
858 vector startspeed_size = panel_size;
859 startspeed_size.y = autocvar_hud_panel_strafehud_startspeed_size;
860 if(!autocvar_hud_panel_strafehud_uncapped)
861 startspeed_size.y = min(startspeed_size.y, 10);
862 startspeed_size.y *= panel_size.y;
863 if(!autocvar_hud_panel_strafehud_uncapped)
864 startspeed_size.y = max(startspeed_size.y, 1);
866 float text_offset = 0;
867 if((autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha > 0) || (autocvar_hud_panel_strafehud_bestangle && autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha > 0))
869 text_offset = (angleheight_offset - panel_size.y) / 2;
870 if(arrow_size > 0 && autocvar_hud_panel_strafehud_angle_arrow >= 2)
871 text_offset += arrow_size;
872 // make sure text doesn't draw inside the strafehud bar
873 text_offset = max(text_offset, 0);
876 string speed_unit = GetSpeedUnit(autocvar_hud_panel_strafehud_unit);
877 drawstring_aspect(panel_pos + eY * (panel_size.y + text_offset), strcat(ftos_decimals(startspeed * speed_conversion_factor, 2), autocvar_hud_panel_strafehud_unit_show ? speed_unit : ""), startspeed_size, autocvar_hud_panel_strafehud_startspeed_color, text_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
881 // show height achieved by a single jump
882 // FIXME: checking z position differences is unreliable (warpzones, teleporter, kill, etc) but using velocity to calculate jump height would be
883 // inaccurate in hud code (possibly different tick rate, doesn't run when hud isn't drawn, rounding errors)
884 if(autocvar_hud_panel_strafehud_jumpheight_fade > 0 && autocvar_hud_panel_strafehud_jumpheight_size > 0)
886 static float height_min = 0, height_max = 0; // ground and peak of jump z coordinates
887 static float jumpheight = 0, jumptime = 0; // displayed value and timestamp for fade out
889 // tries to catch kill and spectate but those are not reliable
890 if((strafeplayer.velocity.z <= 0) || onground || swimming || IS_DEAD(strafeplayer) || spectating)
892 height_min = height_max = strafeplayer.origin.z;
894 else if(strafeplayer.origin.z > height_max)
896 height_max = strafeplayer.origin.z;
897 float jumpheight_new = (height_max - height_min) * length_conversion_factor;
899 if(jumpheight_new > max(autocvar_hud_panel_strafehud_jumpheight_min, 0))
901 jumpheight = jumpheight_new;
906 if((jumptime > 0) && (time - jumptime) <= autocvar_hud_panel_strafehud_jumpheight_fade)
908 float text_alpha = cos(((time - jumptime) / autocvar_hud_panel_strafehud_jumpheight_fade) * 90 * DEG2RAD); // fade non-linear like the physics panel does
909 vector jumpheight_size = panel_size;
910 jumpheight_size.y = autocvar_hud_panel_strafehud_jumpheight_size;
911 if(!autocvar_hud_panel_strafehud_uncapped)
912 jumpheight_size.y = min(jumpheight_size.y, 10);
913 jumpheight_size.y *= panel_size.y;
914 if(!autocvar_hud_panel_strafehud_uncapped)
915 jumpheight_size.y = max(jumpheight_size.y, 1);
917 float text_offset = 0;
918 if((autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha > 0) || (autocvar_hud_panel_strafehud_bestangle && autocvar_hud_panel_strafehud_bestangle_alpha * panel_fg_alpha > 0))
920 text_offset = (angleheight_offset - panel_size.y) / 2;
921 if(arrow_size > 0 && autocvar_hud_panel_strafehud_angle_arrow == 1 || autocvar_hud_panel_strafehud_angle_arrow >= 3)
922 text_offset += arrow_size;
923 // make sure text doesn't draw inside the strafehud bar
924 text_offset = max(text_offset, 0);
927 string length_unit = GetLengthUnit(autocvar_hud_panel_strafehud_unit);
928 drawstring_aspect(panel_pos - eY * (jumpheight_size.y + text_offset), strcat(ftos_decimals(jumpheight, length_decimals), autocvar_hud_panel_strafehud_unit_show ? length_unit : ""), jumpheight_size, autocvar_hud_panel_strafehud_jumpheight_color, text_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
935 // functions to make hud elements align perfectly in the hud area
936 void HUD_Panel_DrawStrafeHUD(float offset, float width, vector color, float alpha, int type, int gradientType)
938 float mirror_offset, mirror_width;
939 vector size = panel_size;
940 vector mirror_size = panel_size;
942 float gradient_offset, gradient_mirror_offset;
943 float overflow_width = 0, overflow_mirror_width = 0;
945 float original_width = width;
947 if(alpha <= 0 && type != 2 || width <= 0) return;
949 if(type == 2 && gradientType == 0) type = 0;
953 mirror_width = min(fabs(offset), width);
954 mirror_offset = panel_size.x + hidden_width - fabs(offset);
960 mirror_width = min(offset + width - panel_size.x - hidden_width, width);
961 mirror_offset = max(offset - panel_size.x - hidden_width, 0);
964 if(width < 0) width = 0;
965 if((offset + width) > panel_size.x)
967 overflow_width = (offset + width) - panel_size.x;
968 width = panel_size.x - offset;
970 if(mirror_offset < 0)
972 mirror_width += mirror_offset;
976 if(mirror_width < 0) mirror_width = 0;
977 if((mirror_offset + mirror_width) > panel_size.x)
979 overflow_mirror_width = (mirror_offset + mirror_width) - panel_size.x;
980 mirror_width = panel_size.x - mirror_offset;
983 if(direction < 0) // swap mirror and non-mirror values if direction points left
985 offset += mirror_offset;
986 mirror_offset = offset - mirror_offset;
987 offset -= mirror_offset;
989 width += mirror_width;
990 mirror_width = width - mirror_width;
991 width -= mirror_width;
993 overflow_width += overflow_mirror_width;
994 overflow_mirror_width = overflow_width - overflow_mirror_width;
995 overflow_width -= overflow_mirror_width;
999 mirror_size.x = mirror_width;
1004 case 0: // no styling (drawfill)
1005 if(mirror_size.x > 0 && mirror_size.y > 0) drawfill(panel_pos + eX * mirror_offset, mirror_size, color, alpha, DRAWFLAG_NORMAL);
1006 if(size.x > 0 && size.y > 0) drawfill(panel_pos + eX * offset, size, color, alpha, DRAWFLAG_NORMAL);
1009 case 1: // progress bar style
1010 if(mirror_size.x > 0 && mirror_size.y > 0) HUD_Panel_DrawProgressBar(panel_pos + eX * mirror_offset, mirror_size, "progressbar", 1, 0, 0, color, alpha, DRAWFLAG_NORMAL);
1011 if(size.x > 0 && size.y > 0) HUD_Panel_DrawProgressBar(panel_pos + eX * offset, size, "progressbar", 1, 0, 0, color, alpha, DRAWFLAG_NORMAL);
1014 case 2: // gradient style (types: 1 = left, 2 = right, 3 = both)
1015 // determine whether the gradient starts in the mirrored or the non-mirrored area
1016 if(offset == 0 && mirror_offset == 0) gradient_start = width > mirror_width ? 2 : 1;
1017 else if(offset == 0) gradient_start = 2;
1018 else if(mirror_offset == 0) gradient_start = 1;
1019 else gradient_start = 0;
1021 switch(gradient_start){
1023 case 0: // no offset required
1024 gradient_offset = gradient_mirror_offset = 0;
1026 case 1: // offset starts in non-mirrored area, mirrored area requires offset
1027 gradient_offset = 0;
1028 gradient_mirror_offset = original_width - (mirror_width + overflow_mirror_width);
1030 case 2: // offset starts in mirrored area, non-mirrored area requires offset
1031 gradient_offset = original_width - (width + overflow_width);
1032 gradient_mirror_offset = 0;
1035 StrafeHUD_drawGradient(color, autocvar_hud_panel_strafehud_bar_neutral_color, mirror_size, original_width, mirror_offset, alpha, gradient_mirror_offset, gradientType);
1036 StrafeHUD_drawGradient(color, autocvar_hud_panel_strafehud_bar_neutral_color, size, original_width, offset, alpha, gradient_offset, gradientType);
1040 vector StrafeHUD_mixColors(vector color1, vector color2, float ratio)
1043 if(ratio <= 0) return color1;
1044 if(ratio >= 1) return color2;
1045 mixedColor.x = color1.x + (color2.x - color1.x) * ratio;
1046 mixedColor.y = color1.y + (color2.y - color1.y) * ratio;
1047 mixedColor.z = color1.z + (color2.z - color1.z) * ratio;
1051 void StrafeHUD_drawGradient(vector color1, vector color2, vector size, float original_width, float offset, float alpha, float gradientOffset, int gradientType)
1053 float color_ratio, alpha1, alpha2;
1054 vector gradient_size = size;
1055 alpha1 = bound(0, alpha, 1);
1056 alpha2 = bound(0, autocvar_hud_panel_strafehud_bar_neutral_alpha, 1);
1057 if((alpha1+alpha2) == 0) return;
1058 color_ratio = alpha1/(alpha1+alpha2);
1059 for(int i = 0; i < size.x; ++i)
1061 float ratio, alpha_ratio, combine_ratio1, combine_ratio2;
1062 gradient_size.x = size.x - i < 1 ? size.x - i : 1;
1063 ratio = (i + gradientOffset) / original_width * (gradientType == 3 ? 2 : 1);
1064 if(ratio > 1) ratio = 2 - ratio;
1065 if(gradientType != 2) ratio = 1 - ratio;
1066 alpha_ratio = alpha1 - (alpha1 - alpha2) * ratio;
1067 combine_ratio1 = ratio*(1-color_ratio);
1068 combine_ratio2 = (1-ratio)*color_ratio;
1069 ratio = (combine_ratio1 + combine_ratio2) == 0 ? 1 : combine_ratio1/(combine_ratio1 + combine_ratio2);
1070 if(alpha_ratio > 0) drawfill(panel_pos + eX * (offset + i), gradient_size, StrafeHUD_mixColors(color1, color2, ratio), alpha_ratio, DRAWFLAG_NORMAL);
1074 // draw the strafe arrows (inspired by drawspritearrow() in common/mutators/mutator/waypoints/waypointsprites.qc)
1075 void StrafeHUD_drawStrafeArrow(vector origin, float size, vector color, float alpha, bool flipped)
1077 if(flipped) origin -= size*eY;
1078 R_BeginPolygon("", DRAWFLAG_NORMAL, true);
1079 R_PolygonVertex(origin + (flipped ? size*eY : '0 0 0') , '0 0 0', color, alpha);
1080 R_PolygonVertex(origin + (flipped ? '0 0 0' : size*eY) - size*eX, '0 0 0', color, alpha);
1081 R_PolygonVertex(origin + (flipped ? '0 0 0' : size*eY) + size*eX, '0 0 0', color, alpha);
1085 // length unit conversion (km and miles are only included to match the GetSpeedUnit* functions)
1086 float GetLengthUnitFactor(int length_unit)
1092 case 2: return 0.0254;
1093 case 3: return 0.0254 * 0.001;
1094 case 4: return 0.0254 * 0.001 * 0.6213711922;
1095 case 5: return 0.0254 * 0.001 * 0.5399568035;
1099 string GetLengthUnit(int length_unit)
1103 // translator-friendly strings without the initial space
1105 case 1: return strcat(" ", _("qu"));
1106 case 2: return strcat(" ", _("m"));
1107 case 3: return strcat(" ", _("km"));
1108 case 4: return strcat(" ", _("mi"));
1109 case 5: return strcat(" ", _("nmi"));