summaryrefslogtreecommitdiff
path: root/internal_rewrite/legitbot.cpp
blob: c3c0010b2948a1f4e0c31f4c408af1b69d22309b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "interface.hpp"
#include "input_system.hpp"
#include "context.hpp"
#include "legitbot.hpp"
#include "settings.hpp"
#include "math.hpp"

namespace features
{
	int c_legitbot::get_aim_target( float fov ) {
		float	best_fov = fov ? fov : g_settings.legit.active->m_fov;
		int		target{ -1 };
		vec3_t	aim_angle{ };
		vec3_t	cur_angle{ };
		vec3_t	local_pos{ };
		vec3_t	aim_pos{ };

		g_csgo.m_engine( )->GetViewAngles( cur_angle );
		local_pos = g_ctx.m_local->get_eye_pos( );

		for ( int i{ 1 }; i <= g_csgo.m_globals->m_maxclients; ++i ) {
			auto entity = g_csgo.m_entlist( )->GetClientEntity< >( i );

			if ( !entity ) continue;
			if ( entity == g_ctx.m_local ) continue;
			if ( !entity->is_valid( ) ) continue;
			int team = entity->m_iTeamNum( );
			if ( team == g_ctx.m_local->m_iTeamNum( )
				&& !g_settings.legit.friendlies( ) ) {
				continue;
			}

			bool visible = entity->is_visible( 0 );
			if( !visible && g_settings.legit.backtracking_target( ) ) {
				auto record = m_lagcomp.find_best_record( i );
				if( record ) {
					visible = util::trace_ray( local_pos, record->m_position,
						g_ctx.m_local->ce( ), entity->ce( ) );
				}
			}

			if ( !visible ) {
				continue;
			}

			aim_angle = math::vector_angles( local_pos, entity->get_hitbox_pos( 0 ) );
			aim_angle.clamp( );

			float fov = ( cur_angle - aim_angle ).clamp( ).length2d( );
			if( fov < best_fov ) {
				best_fov = fov;
				target = i;
			}
		}

		return target;
	}

	void c_legitbot::assist( c_base_player* target, float* x, float* y ) {
		vec3_t aim_ang;
		vec3_t move_ang;
		vec3_t view_ang;
		vec3_t enemy_pos;
		vec3_t local_pos;
		vec3_t view_delta;
		vec3_t delta;
		vec2_t pixels;

		local_pos = g_ctx.m_local->get_eye_pos( );
		enemy_pos = target->get_hitbox_pos( 0 );

		aim_ang = math::vector_angles( local_pos, enemy_pos );
		aim_ang -= g_ctx.m_local->m_aimPunchAngle( ) * 2.f * g_settings.legit.active->m_rcs;

		move_ang = pixels_to_angle( vec2_t( *x, *y ) );
		g_csgo.m_engine( )->GetViewAngles( view_ang );

		view_delta = ( aim_ang - view_ang ).clamp( );

		move_ang *= g_settings.legit.active->m_assist_strength;
		float delta_y = std::abs( move_ang.y );
		float delta_x = std::abs( move_ang.x );

		delta.x = std::clamp( view_delta.x, -delta_x, delta_x );
		delta.y = std::clamp( view_delta.y, -delta_y, delta_y );

		pixels = angle_to_pixels( delta );
		*x += pixels.x; *y += pixels.y;
	}

	void c_legitbot::triggerbot( user_cmd_t* cmd ) {
		static float time_at = 0.f;

		if( !g_settings.legit.triggerbot( ) )
			return;

		if( !g_input.is_key_pressed( ( VirtualKeys_t )g_settings.legit.trigger_key( ) ) && !time_at ) // sometimes people will let go of their triggerbot key too quickly and will cause them to not shoot.
			return;

		vec3_t viewangles{ };
		vec3_t forward{ };
		g_csgo.m_engine( )->GetViewAngles( viewangles );

		auto wep = g_ctx.m_local->get_weapon( );
		if( !wep ) return;
		if( wep->is_knife( ) ) return;

		auto wpn_info = wep->get_wpn_info( );
		if( !wpn_info ) return;

		const float length		 = wpn_info->range;
		const float current_time = TICKS_TO_TIME( g_csgo.m_globals->m_tickcount );

		if( time_at && abs( current_time - time_at ) > 0.3f ) {
			time_at = 0.f; // delta too big
		}

		if( length > 1.0f ) {
			if( !time_at ) {
				viewangles += g_ctx.m_local->m_aimPunchAngle( ) * 2.f;

				forward = math::angle_vectors( viewangles );
				forward *= length;

				vec3_t src = g_ctx.m_local->get_eye_pos( );
				vec3_t dst = src + forward;

				CTraceFilter filter{ };
				CGameTrace	 tr{ };
				Ray_t		 ray{ };

				filter.pSkip = g_ctx.m_local;
				ray.Init( src, dst );

				g_csgo.m_trace( )->TraceRay( ray, MASK_SHOT, &filter, &tr );

				if( tr.m_pEnt ) {
					auto ent = tr.m_pEnt->as< c_base_player >( );
					if( ent->is_valid( ) ) {
						if( ent->m_iTeamNum( ) != g_ctx.m_local->m_iTeamNum( ) || g_settings.legit.friendlies ) {
							time_at = current_time;
						}
					}
				}
			}
			
			if( time_at ) {
				if( ( current_time - time_at ) >= g_settings.legit.trigger_delay( ) ) {
					cmd->m_buttons |= IN_ATTACK;
					time_at = 0.f;
				}
			}
		}
	}

	void c_legitbot::sample_angle_data( const vec3_t& cur_angles ) {
		auto time = g_csgo.m_globals->m_curtime;

		if ( !m_aiming ) {
			m_angle_samples.push_front( { cur_angles, time } );
		}

		while ( m_angle_samples.size( ) > g_settings.legit.sample_size( ) ) {
			m_angle_samples.pop_back( );
		}
	}

	float c_legitbot::get_avg_delta( ) {
		if ( m_angle_samples.empty( ) )
			return 0.f;

		float avg_delta{ };

		for ( auto& it : m_angle_samples ) {
			static vec3_t last_angle = it.m_viewangles;

			//doing this implements nonsticky aswell
			//once you already are at the target
			//after lets say a flick
			//it will reduce the aim speed
			//making it not "stick" to the bone
			//as we dont store records when aiming
			float time_delta = g_csgo.m_globals->m_curtime - it.m_time;
			float delta_diff = m_deltatime / ( time_delta * 2.f );

			vec3_t	angle_delta = ( last_angle - it.m_viewangles ).clamp( );
			float	delta_length = angle_delta.length( ) * delta_diff;

			avg_delta += delta_length;
			last_angle = it.m_viewangles;
		}

		//scale the delta down
		return avg_delta / float( g_settings.legit.sample_size( ) );
	}

	vec2_t c_legitbot::angle_to_pixels( const vec3_t& angle ) {
		static auto m_yaw	= g_csgo.m_cvar( )->FindVar( xors( "m_yaw" ) );
		static auto m_pitch = g_csgo.m_cvar( )->FindVar( xors( "m_pitch" ) );

		float x = angle.x / m_pitch->get_float( );
		float y = angle.y / m_yaw->get_float( );

		return vec2_t( -y, x );
	}

	vec3_t c_legitbot::pixels_to_angle( const vec2_t& pixel ) {
		static auto m_yaw = g_csgo.m_cvar( )->FindVar( xors( "m_yaw" ) );
		static auto m_pitch = g_csgo.m_cvar( )->FindVar( xors( "m_pitch" ) );

		float x = pixel.x * m_pitch->get_float( );
		float y = pixel.y * m_yaw->get_float( );

		return vec3_t( -y, x, 0.f ).clamp( );
	}

	bool c_legitbot::update_settings( ) {
		if ( !g_ctx.run_frame( ) ) return false;

		auto weapon = g_ctx.m_local->get_weapon( );
		if ( !weapon ) return false;

		if ( weapon->is_grenade( ) || weapon->is_knife( ) ) {
			return false;
		}

		if ( weapon->is_rifle( ) ) {
			g_settings.legit.active = &g_settings.legit.rifles;
		}
		else if ( weapon->is_sniper( ) ) {
			g_settings.legit.active = &g_settings.legit.snipers;
		}
		else if ( weapon->is_pistol( ) ) {
			g_settings.legit.active = &g_settings.legit.pistols;
		}
		else {
			g_settings.legit.active = &g_settings.legit.general;
		}

		return true;
	}

	void c_legitbot::aim_at_target( c_base_player* target, float* x, float* y ) {
		vec3_t aim_ang;
		vec3_t cur_ang;
		vec3_t delta;
		vec2_t pixels;
		vec2_t mouse;

		g_csgo.m_engine( )->GetViewAngles( cur_ang );

		if ( g_settings.legit.backtracking( ) && m_lagcomp.find_best_record( target->ce( )->GetIndex( ) ) ) {
			aim_ang = math::vector_angles( g_ctx.m_local->get_eye_pos( ),
				m_lagcomp.get_backtracked_position( target->ce( )->GetIndex( ) ) ).clamp( );
		}
		else {
			aim_ang = math::vector_angles( g_ctx.m_local->get_eye_pos( ),
				target->get_hitbox_pos( 0 ) ).clamp( );
		}

		aim_ang -= g_ctx.m_local->m_aimPunchAngle( ) * 2.f * ( g_settings.legit.active->m_rcs / 100.f );

		delta = ( aim_ang - cur_ang ).clamp( );
		float delta_length = delta.length( );

		if ( delta_length ) {
			float final_time = delta_length / ( g_settings.legit.active->m_speed( ) / 100.f );
			m_curtime += m_deltatime;

			if ( m_curtime > final_time ) {
				m_curtime = final_time;
			}

			float aim_progress = m_curtime / final_time;
			delta *= aim_progress;
			aim_ang = delta;

			pixels = angle_to_pixels( delta );
			*x += pixels.x;
			*y += pixels.y;
		}
	}

	void c_legitbot::aimbot( float* x, float* y ) {
		if ( !g_settings.legit.enabled( ) ) return;
		if ( !update_settings( ) ) return;

		m_aiming = false;
		static float old_time = g_csgo.m_globals->m_curtime;
		float time = g_csgo.m_globals->m_curtime;

		m_deltatime = time - old_time;
		if ( g_settings.legit.dynamic_smoothing( ) ) {
			float avg_delta = get_avg_delta( ) * g_settings.legit.smooth_factor( );
			if ( avg_delta > m_deltatime ) {
				m_deltatime = avg_delta;
			}
		}

		old_time = time;

		bool in_attack = false;
		if ( g_settings.legit.activation_type == 0 )
			in_attack = true;
		if ( g_settings.legit.activation_type == 1
			&& g_input.is_key_pressed( ( VirtualKeys_t )g_settings.legit.key( ) ) ) {
			in_attack = true;
		}
		if( g_settings.legit.trigger_magnet
			&& g_settings.legit.triggerbot
			&& g_input.is_key_pressed( g_settings.legit.trigger_key ) ) {
			in_attack = true;
		}

		int  target_index = get_aim_target( );
		auto target = g_csgo.m_entlist( )->GetClientEntity< >( target_index );

		if ( target_index != -1 ) {
			if( in_attack ) {
				m_aiming = true;
				aim_at_target( target, x, y );
			}
			if( g_settings.legit.assist ) {
				assist( target, x, y );
			}
		}
		else {
			m_curtime = 0;
		}
	}
}