Advertisement
Guest User

Untitled

a guest
May 25th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. float antiaim::best_head_position( ) {
  2. //getting the local player
  3. i_client_entity* local = interfaces::entity_list->get_client_entity( interfaces::engine->get_local_player() );
  4.  
  5. //getting our current view angles
  6. vec3_t viewangle;
  7. interfaces::engine->get_view_angles( viewangle );
  8.  
  9. // lambda function that gets the closest target by fov
  10. auto get_target_entity = [&]( void ) {
  11. // values that we use to compare the entities
  12. float best_fov = 360.0f;
  13. int best_target = -1;
  14.  
  15. // iterate through the entity_list
  16. for (int i = 1; i <= interfaces::global_vars->max_clients; i++) {
  17. // find entity
  18. i_client_entity* entity = interfaces::entity_list->get_client_entity( i );
  19.  
  20. // entity is not nullptr, not a team mate, not dormant and is alive
  21. if (!entity || !entity->sanity_checks( local, true ))
  22. continue;
  23.  
  24. // get that fov
  25. float fov_to_entity = math::get_fov( viewangle, math::calc_angle( local->eye_pos(), entity->eye_pos() ) );
  26.  
  27. // some comparisons
  28. if (fov_to_entity < best_fov) {
  29. best_fov = fov_to_entity;
  30. best_target = i;
  31. }
  32. }
  33.  
  34. // ok now we have our entity
  35. return interfaces::entity_list->get_client_entity( best_target );
  36. };
  37.  
  38. // we will require our eye positions
  39. vec3_t eye_position = local->eye_pos();
  40.  
  41. // our results
  42. float yaw = -1.0f, highest_thickness = 0.0f;
  43.  
  44. // if there is an entity
  45. i_client_entity* entity = get_target_entity();
  46. if (!entity) return yaw;
  47.  
  48. // lambda function that will return the thicknes of the wall from a calculated head position
  49. auto check_wall_thickness = [&]( i_client_entity* entity, vec3_t position ) -> float {
  50. // getting the eye position of the entity we are targetting
  51. vec3_t entity_eye_position = entity->eye_pos();
  52.  
  53. // we will use these as final measurements
  54. vec3_t endpos1, endpos2;
  55.  
  56. // casting a ray from our calculated head position to the entities eye position
  57. ray_t ray;
  58. ray.init( position, entity_eye_position );
  59.  
  60. // we dont want to come into contact with the entity or our selves
  61. c_trace_filter_skip_two_entities filter( entity, local );
  62.  
  63. // tracing a ray using the parameters above
  64. trace_t trace1;
  65. interfaces::trace->trace_ray( ray, trace_mask::mask_shot_brushonly, &filter, &trace1 );
  66.  
  67. // if the trace did hit something
  68. if (trace1.did_hit())
  69. endpos1 = trace1.m_endpos;
  70. else //or if we didnt
  71. return 0.f;
  72.  
  73. // casting another ray that goes from the entity eye position to our calculated head position
  74. ray.init( entity_eye_position, position );
  75.  
  76. // tracing a ray using the parameters above
  77. trace_t trace2;
  78. interfaces::trace->trace_ray( ray, trace_mask::mask_shot_brushonly, &filter, &trace2 );
  79.  
  80. // if the trace did hit something
  81. if (trace2.did_hit())
  82. endpos2 = trace2.m_endpos;
  83.  
  84. // final calculations to determine how thick the object was we just traced through
  85. float add = position.distance( entity_eye_position ) - eye_position.distance( entity_eye_position ) + 3.f;
  86. return endpos1.distance( endpos2 ) + add / 3;
  87. };
  88.  
  89. // we will require our head positions
  90. vec3_t head_position = local->get_hitbox_position( hitboxes::hitbox_head );
  91.  
  92. // we will require our abs origin
  93. vec3_t local_abs_origin = local->abs_origin();
  94.  
  95. // the distance between our head and our abs origin
  96. float radius = fabs( vec3_t( head_position - local_abs_origin ).length_2d() );
  97.  
  98. // this will result in a 45.0f deg step
  99. float angle_step = static_cast<float>(M_PI) / 4.0f;
  100.  
  101. // iterate through 45.0f deg angles
  102. for (float n = 0.0f; n < (static_cast<float>(M_PI) * 2.0f) - angle_step; n += angle_step) {
  103. // creating a calculated head position
  104. vec3_t head_position( radius * cos( n ) + eye_position.x, radius* sin( n ) + eye_position.y, eye_position.z );
  105.  
  106. // checking the thickness of the wall to the entity from the calculated head position
  107. float wall_thickness = check_wall_thickness( entity, head_position );;
  108.  
  109. // if this wall is thicker than the previous walls then
  110. if (wall_thickness > highest_thickness){
  111. // set the thickest wall to this one
  112. highest_thickness = wall_thickness;
  113.  
  114. // determine our optimal yaw
  115. yaw = n;
  116. }
  117. }
  118.  
  119. //whoop whoop p freestanding
  120. return math::rad_to_deg( yaw );
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement