3 #include "strafehud.qh"
5 #include <client/autocvars.qh>
6 #include <client/miscfunctions.qh>
7 #include <common/animdecide.qh>
8 #include <common/ent_cs.qh>
9 #include <common/mapinfo.qh>
10 #include <common/physics/movetypes/movetypes.qh>
11 #include <common/physics/player.qh>
12 #include <lib/csqcmodel/cl_player.qh>
16 void HUD_StrafeHUD_Export(int fh)
18 // allow saving cvars that aesthetically change the panel into hud skin files
22 bool state_fwd = true;
23 bool state_fwd_prev = true;
24 float demo_angle = -37;
25 float demo_direction = 1;
27 float state_onground_time = 0;
28 float state_strafekeys_time = 0;
29 float state_direction_time = 0;
30 bool state_onground = false;
31 bool state_strafekeys = false;
35 // provide basic panel cvars to old clients
36 // TODO remove them after a future release (0.8.2+)
37 noref string autocvar_hud_panel_strafehud_pos = "0.320000 0.570000";
38 noref string autocvar_hud_panel_strafehud_size = "0.360000 0.020000";
39 noref string autocvar_hud_panel_strafehud_bg = "0";
40 noref string autocvar_hud_panel_strafehud_bg_color = "";
41 noref string autocvar_hud_panel_strafehud_bg_color_team = "";
42 noref string autocvar_hud_panel_strafehud_bg_alpha = "0.7";
43 noref string autocvar_hud_panel_strafehud_bg_border = "";
44 noref string autocvar_hud_panel_strafehud_bg_padding = "";
51 if(!autocvar__hud_configure)
53 if(!autocvar_hud_panel_strafehud) return;
54 if(spectatee_status == -1 && (autocvar_hud_panel_strafehud == 1 || autocvar_hud_panel_strafehud == 3)) return;
55 if(autocvar_hud_panel_strafehud == 3 && !(ISGAMETYPE(RACE) || ISGAMETYPE(CTS))) return;
58 HUD_Panel_LoadCvars();
60 if(autocvar_hud_panel_strafehud_dynamichud)
67 panel_pos += '1 1 0' * panel_bg_padding;
68 panel_size -= '2 2 0' * panel_bg_padding;
71 if(spectatee_status > 0 || isdemo())
74 strafeplayer = CSQCModel_server2csqc(player_localentnum - 1);
79 strafeplayer = csqcplayer;
83 if(csqcplayer && strafeplayer)
86 bool onground = islocal ? IS_ONGROUND(strafeplayer) : !(strafeplayer.anim_implicit_state & ANIMIMPLICITSTATE_INAIR);
88 bool is_swimming = strafeplayer.waterlevel >= WATERLEVEL_SWIMMING;
89 float speed = !autocvar__hud_configure ? vlen(vec2(csqcplayer.velocity)) : 1337; // use local csqcmodel entity for this even when spectating, flickers too much otherwise
90 float maxspeed_crouch_mod = IS_DUCKED(strafeplayer) && (!is_swimming || IS_ONGROUND(strafeplayer)) ? .5 : 1;
91 float maxspeed_water_mod = is_swimming ? .7 : 1; // very simplified water physics, the hud will not work well (and is not supposed to) while swimming
92 float maxspeed_phys = onground ? PHYS_MAXSPEED(strafeplayer) : PHYS_MAXAIRSPEED(strafeplayer);
93 float maxspeed = !autocvar__hud_configure ? maxspeed_phys * maxspeed_crouch_mod * maxspeed_water_mod : 320;
94 float vel_angle = vectoangles(strafeplayer.velocity).y;
95 float view_angle = view_angles.y + 180;
98 vector movement = PHYS_INPUT_MOVEVALUES(strafeplayer);
99 int keys = STAT(PRESSED_KEYS);
105 int mode = autocvar_hud_panel_strafehud_mode >= 0 && autocvar_hud_panel_strafehud_mode <= 1 ? autocvar_hud_panel_strafehud_mode : 0;
110 vector currentangle_color = autocvar_hud_panel_strafehud_warning_color;
111 float currentangle_offset;
112 vector currentangle_size = '0 0 0';
113 bool show_indicators;
115 bool bestangle_anywhere = false;
116 float bestangle_offset;
117 float bestangle_width;
118 float switch_bestangle_offset;
119 float accelzone_offset;
120 float accelzone_width;
121 float odd_accelzone_offset;
122 float odd_accelzone_width;
123 float overturn_offset;
124 float overturn_width;
125 float overturn_width_visible;
128 vector direction_size_vertical = '0 0 0';
129 vector direction_size_horizontal = '0 0 0';
131 float range_minangle;
133 // determine whether the player is pressing forwards or backwards keys
134 if(islocal) // if entity is local player
140 else if(movement_x < 0)
149 else // alternatively determine direction by querying pressed keys
151 if((keys & KEY_FORWARD) && !(keys & KEY_BACKWARD))
155 else if(!(keys & KEY_FORWARD) && (keys & KEY_BACKWARD))
165 // determine player wishdir
166 if(islocal) // if entity is local player
174 else if(movement_y > 0)
191 wishangle = RAD2DEG * atan2(movement_y, movement_x);
192 // wrap the wish angle if it exceeds ±90°
193 if(fabs(wishangle) > 90)
195 if(wishangle < 0) wishangle += 180;
196 else wishangle -= 180;
197 wishangle = -wishangle;
202 else // alternatively calculate wishdir by querying pressed keys
204 if(keys & KEY_FORWARD || keys & KEY_BACKWARD)
216 else if(!(keys & KEY_RIGHT))
218 wishangle = 0; // wraps at 180°
222 strafekeys = fabs(wishangle) == 90;
224 // determine minimum required angle to display full strafe range
225 range_minangle = fabs(wishangle) % 90; // maximum range is 90 degree
226 if(range_minangle > 45) // minimum angle range is 45
228 range_minangle = 45 - fabs(wishangle) % 45;
230 range_minangle = 90 - range_minangle; // calculate value which is never >90 or <45
231 range_minangle *= 2; // multiply to accommodate for both sides of the hud
233 if(autocvar_hud_panel_strafehud_angle == 0)
235 if(autocvar__hud_configure)
241 hudangle = range_minangle; // use minimum angle required if dynamically setting hud angle
246 hudangle = bound(0, fabs(autocvar_hud_panel_strafehud_angle), 360); // limit HUD range to 360 degrees, higher values don't make sense
249 // detect air strafe turning
250 if(onground != state_onground)
252 state_onground_time = time;
254 state_onground = onground;
255 if(strafekeys != state_strafekeys)
257 state_strafekeys_time = time;
259 state_strafekeys = strafekeys;
260 if((keys & KEY_FORWARD) || (keys & KEY_BACKWARD) || is_swimming || autocvar__hud_configure)
266 if((time - state_onground_time) >= autocvar_hud_panel_strafehud_timeout_ground) // timeout for strafe jumping in general
271 else // air strafe only
275 if(((time - state_onground_time) >= autocvar_hud_panel_strafehud_timeout_air) || (keys & KEY_JUMP)) // timeout for slick ramps
277 turn = true; // CPMA turning
278 turnangle = wishangle;
281 else if((time - state_strafekeys_time) >= autocvar_hud_panel_strafehud_timeout_strafe) // timeout for jumping with strafe keys only
288 maxspeed = PHYS_MAXAIRSTRAFESPEED(strafeplayer); // no crouching here because it doesn't affect air strafing
289 wishangle = turnangle;
292 minspeed = autocvar_hud_panel_strafehud_indicator_minspeed < 0 ? maxspeed + .1 : autocvar_hud_panel_strafehud_indicator_minspeed;
293 show_indicators = (autocvar_hud_panel_strafehud_indicators && (speed >= minspeed));
295 // get current strafing angle ranging from -180° to +180°
296 if(!autocvar__hud_configure)
300 // calculate view angle relative to the players current velocity direction
301 angle = vel_angle - view_angle;
303 // if the angle goes above 180° or below -180° wrap it to the opposite side
304 if (angle > 180) angle -= 360;
305 else if(angle < -180) angle += 360;
307 // shift the strafe angle by 180° for hud calculations
308 if(angle < 0) angle += 180;
311 // determine whether the player is strafing forwards or backwards
312 // if the player isn't strafe turning use forwards/backwards keys to determine direction
319 else if(keys_fwd < 0)
325 state_fwd = fabs(angle) <= 90;
328 // otherwise determine by examining the strafe angle
331 if(wishangle < 0) // detect direction since the direction is not yet set
333 state_fwd = angle <= -wishangle;
337 state_fwd = angle >= -wishangle;
341 if(state_fwd_prev != state_fwd)
343 state_direction_time = time;
345 state_fwd_prev = state_fwd;
347 if((time - state_direction_time) >= autocvar_hud_panel_strafehud_timeout_direction) // timeout when changing between forwards and backwards strafe
352 // shift the strafe angle by 180° when strafing backwards
355 if(angle < 0) angle += 180;
359 // making the hud less flickery in case of rounding errors
360 if(angle > 179.9 || angle < -179.9)
362 currentangle_color = autocvar_hud_panel_strafehud_alert_color;
365 if(angle < .1 && angle > -.1)
375 else // simulate turning for HUD setup
377 if(autocvar__hud_panel_strafehud_center)
379 angle = demo_angle = 0;
385 if(autocvar__hud_panel_strafehud_demo && ((time - demo_time) >= .025))
388 demo_angle += demo_direction;
389 if(fabs(demo_angle) >= 55)
391 demo_direction = -demo_direction;
395 wishangle = 45 * (demo_angle > 0 ? 1 : -1);
399 // invert the wish angle when strafing backwards
402 wishangle = -wishangle;
405 // flip angles if v_flipped is enabled
406 if(autocvar_v_flipped)
409 wishangle = -wishangle;
412 moveangle = angle + wishangle;
416 direction = wishangle > 0 ? 1 : -1;
420 direction = moveangle > 0 ? 1 : moveangle < 0 ? -1 : 0;
423 // how much is hidden by the current hud angle
424 hidden_angle = 360 - hudangle;
425 hidden_size = hidden_angle / hudangle * panel_size.x;
426 // decelerating at this angle
427 maxangle = 90 - fabs(wishangle);
428 // best angle to strafe at
429 bestangle = (speed > maxspeed ? acos(maxspeed / speed) : 0) * RAD2DEG * (direction < 0 ? -1 : 1) - wishangle;
430 // various offsets and size calculations of hud indicator elements
432 currentangle_size.x = panel_size.x * .005;
433 if(currentangle_size.x < 1) currentangle_size.x = 1;
436 currentangle_offset = angle/hudangle * panel_size.x;
440 currentangle_offset = bound(-hudangle/2, angle, hudangle/2)/hudangle * panel_size.x + panel_size.x/2;
442 currentangle_size.y = panel_size.y * 1.5;
443 // best strafe acceleration angle
444 bestangle_offset = bestangle/hudangle * panel_size.x + panel_size.x/2;
445 switch_bestangle_offset = -bestangle/hudangle * panel_size.x + panel_size.x/2;
448 bestangle_width = panel_size.x * .01;
449 if(bestangle_width < 1) bestangle_width = 1;
455 // remove indicator width from offset
458 bestangle_offset -= bestangle_width;
462 switch_bestangle_offset -= bestangle_width;
464 // direction indicator
465 direction_size_vertical.x = panel_size.x * .0075;
466 if(direction_size_vertical.x < 1) direction_size_vertical.x = 1;
467 direction_size_vertical.y = panel_size.y;
468 direction_size_horizontal.x = direction_size_vertical.x * 3;
469 direction_size_horizontal.y = direction_size_vertical.x;
471 overturn_width = 180/hudangle * panel_size.x;
472 overturn_width_visible = (hudangle/2 - maxangle) / hudangle * panel_size.x;
474 // mark the ideal strafe angle
475 if(direction < 0) // turning left
477 // calculate zone in which strafe acceleration happens
478 accelzone_width = bestangle_offset;
479 // calculate offset of overturn area
480 overturn_offset = overturn_width_visible - overturn_width;
481 // move/adjust acceleration zone
482 accelzone_offset = overturn_width_visible;
483 accelzone_width -= overturn_width_visible;
484 // calculate zone in which strafe acceleration could also happen without changing wishdir
485 odd_accelzone_width = accelzone_width + bestangle_width;
486 odd_accelzone_offset = overturn_offset - odd_accelzone_width;
487 // calculate the background of the strafe-o-meter
488 bar_offset = bestangle_offset + bestangle_width;
489 bar_width = 360/hudangle * panel_size.x - bestangle_width - accelzone_width - odd_accelzone_width - overturn_width;
491 else // turning right or moving forward
493 // calculate zone in which strafe acceleration happens
494 accelzone_offset = bestangle_offset + bestangle_width;
495 accelzone_width = panel_size.x - accelzone_offset;
496 // calculate offset of overturn area
497 overturn_offset = panel_size.x - overturn_width_visible;
498 // adjust acceleration zone
499 accelzone_width -= overturn_width_visible;
500 // calculate zone in which strafe acceleration could also happen without changing wishdir
501 odd_accelzone_width = accelzone_width + bestangle_width;
502 odd_accelzone_offset = overturn_offset + overturn_width;
503 // calculate the background of the strafe-o-meter
504 bar_offset = odd_accelzone_offset + odd_accelzone_width;
505 bar_width = 360/hudangle * panel_size.x - bestangle_width - accelzone_width - odd_accelzone_width - overturn_width;
510 bar_offset -= currentangle_offset;
511 accelzone_offset -= currentangle_offset;
512 odd_accelzone_offset -= currentangle_offset;
513 overturn_offset -= currentangle_offset;
514 bestangle_offset -= currentangle_offset;
515 switch_bestangle_offset -= currentangle_offset;
518 if(!autocvar_hud_panel_strafehud_unstyled)
520 // draw acceleration zone
521 HUD_Panel_DrawStrafeHUD_ProgressBar(accelzone_offset, accelzone_width, autocvar_hud_panel_strafehud_indicator_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
523 // draw odd acceleration zone
524 HUD_Panel_DrawStrafeHUD_ProgressBar(odd_accelzone_offset, odd_accelzone_width, autocvar_hud_panel_strafehud_indicator_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
526 // draw overturn area
527 HUD_Panel_DrawStrafeHUD_ProgressBar(overturn_offset, overturn_width, autocvar_hud_panel_strafehud_alert_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
529 // draw the strafe bar background
530 HUD_Panel_DrawStrafeHUD_ProgressBar(bar_offset, bar_width, autocvar_hud_panel_strafehud_bar_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
534 // draw acceleration zone
535 HUD_Panel_DrawStrafeHUD_drawfill(accelzone_offset, accelzone_width, autocvar_hud_panel_strafehud_indicator_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
537 // draw odd acceleration zone
538 HUD_Panel_DrawStrafeHUD_drawfill(odd_accelzone_offset, odd_accelzone_width, autocvar_hud_panel_strafehud_indicator_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
540 // draw overturn area
541 HUD_Panel_DrawStrafeHUD_drawfill(overturn_offset, overturn_width, autocvar_hud_panel_strafehud_alert_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
543 // draw the strafe bar background
544 HUD_Panel_DrawStrafeHUD_drawfill(bar_offset, bar_width, autocvar_hud_panel_strafehud_bar_color, autocvar_hud_panel_strafehud_bar_alpha, hidden_size);
549 // draw the direction indicator caps at the sides of the hud
551 drawfill(panel_pos + eX * (direction < 0 ? -direction_size_vertical.x : panel_size.x), direction_size_vertical, autocvar_hud_panel_strafehud_direction_color, panel_fg_alpha, DRAWFLAG_NORMAL);
552 // top horizontal line
553 drawfill(panel_pos + eX * (direction < 0 ? -direction_size_vertical.x : panel_size.x - direction_size_horizontal.x + direction_size_vertical.x) - eY * direction_size_horizontal.y, direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color, panel_fg_alpha, DRAWFLAG_NORMAL);
554 // bottom horizontal line
555 drawfill(panel_pos + eX * (direction < 0 ? -direction_size_vertical.x : panel_size.x - direction_size_horizontal.x + direction_size_vertical.x) + eY * panel_size.y, direction_size_horizontal, autocvar_hud_panel_strafehud_direction_color, panel_fg_alpha, DRAWFLAG_NORMAL);
558 if(show_indicators) // only draw indicators if enabled and minspeed is reached
560 // draw best angles for acceleration
561 vector indicator_color;
562 // both indicators are yellow if no direction can be determined
563 indicator_color = direction != 0 ? autocvar_hud_panel_strafehud_indicator_color : autocvar_hud_panel_strafehud_indicator_switch_color;
564 HUD_Panel_DrawStrafeHUD_drawfill(switch_bestangle_offset, bestangle_width, autocvar_hud_panel_strafehud_indicator_switch_color, 1, hidden_size);
565 HUD_Panel_DrawStrafeHUD_drawfill(bestangle_offset, bestangle_width, indicator_color, 1, hidden_size);
567 if(speed < (maxspeed + .1))
569 bestangle_anywhere = true; // moving forward should suffice to gain speed
572 // draw the actual strafe angle
573 if(!bestangle_anywhere) // player gains speed with strafing
575 if((direction > 0 && (angle >= bestangle || angle <= -(bestangle + wishangle*2))) ||
576 (direction < 0 && (angle <= bestangle || angle >= -(bestangle + wishangle*2))))
577 currentangle_color = autocvar_hud_panel_strafehud_good_color;
580 if(fabs(moveangle) > 90) // player is overturning
582 currentangle_color = autocvar_hud_panel_strafehud_alert_color;
585 if(bestangle_anywhere && currentangle_color != autocvar_hud_panel_strafehud_alert_color) // player gains speed without strafing
587 currentangle_color = autocvar_hud_panel_strafehud_good_color;
592 drawfill(panel_pos - eY * ((currentangle_size.y - panel_size.y) / 2) + eX * (panel_size.x/2 - currentangle_size.x/2), currentangle_size, currentangle_color, autocvar_hud_panel_strafehud_angle_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
596 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);
601 // functions to make hud elements align perfectly in the hud area
602 void HUD_Panel_DrawStrafeHUD_ProgressBar(float offset, float width, vector color, float alpha, float hidden)
604 float mirror_offset, mirror_width;
605 vector size = panel_size;
606 vector mirror_size = panel_size;
610 mirror_width = min(fabs(offset), width);
611 mirror_offset = panel_size.x + hidden - fabs(offset);
617 mirror_width = min(offset + width - panel_size.x - hidden, width);
618 mirror_offset = max(offset - panel_size.x - hidden, 0);
620 if((offset + width) > panel_size.x)
622 width = panel_size.x - offset;
624 if(mirror_offset < 0)
626 mirror_width += mirror_offset;
629 if((mirror_offset + mirror_width) > panel_size.x)
631 mirror_width = panel_size.x - mirror_offset;
635 mirror_size.x = mirror_width;
636 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 * panel_fg_alpha, DRAWFLAG_NORMAL);
637 if(size.x > 0 && size.y > 0) HUD_Panel_DrawProgressBar(panel_pos + eX * offset, size, "progressbar", 1, 0, 0, color, alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
640 void HUD_Panel_DrawStrafeHUD_drawfill(float offset, float width, vector color, float alpha, float hidden)
642 float mirror_offset, mirror_width;
643 vector size = panel_size;
644 vector mirror_size = panel_size;
648 mirror_width = min(fabs(offset), width);
649 mirror_offset = panel_size.x + hidden - fabs(offset);
655 mirror_width = min(offset + width - panel_size.x - hidden, width);
656 mirror_offset = max(offset - panel_size.x - hidden, 0);
658 if((offset + width) > panel_size.x)
660 width = panel_size.x - offset;
662 if(mirror_offset < 0)
664 mirror_width += mirror_offset;
667 if((mirror_offset + mirror_width) > panel_size.x)
669 mirror_width = panel_size.x - mirror_offset;
673 mirror_size.x = mirror_width;
674 if(mirror_width > 0) drawfill(panel_pos + eX * mirror_offset, mirror_size, color, alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
675 if(width > 0) drawfill(panel_pos + eX * offset, size, color, alpha * panel_fg_alpha, DRAWFLAG_NORMAL);