summaryrefslogtreecommitdiff
path: root/gmod/movement.cpp
blob: 281576791d80a0d6a6dba8cba6c775d36dc5d15c (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
#include "movement.hpp"
#include "interface.hpp"
#include "settings.hpp"
#include "context.hpp"

#include <algorithm>
#include "base_cheat.hpp"
#include "input_system.hpp"

NAMESPACE_REGION( features )

void c_movement::bhop( ) {
	if ( !g_settings.misc.bunny_hop )
		return;

	if ( g_ctx.m_local->m_nMoveType( ) == MOVETYPE_LADDER || 
		g_ctx.m_local->m_nMoveType( ) == MOVETYPE_NOCLIP )
		return;

	//jump like you nohat
	if ( m_ucmd->m_buttons & IN_JUMP && !( g_ctx.m_local->m_fFlags( ) & FL_ONGROUND ) ) {
		m_ucmd->m_buttons &= ~IN_JUMP;
	}
}

void c_movement::auto_strafer( ) {
	if ( !g_settings.misc.auto_strafe )
		return;

	float speed = g_ctx.m_local->m_vecVelocity( ).length2d( );

	if ( m_ucmd->m_buttons & IN_JUMP && speed > 1.0f ) {
		if ( !m_ucmd->m_forwardmove && !m_ucmd->m_sidemove ) {
			if ( !m_ucmd->m_mousedx ) {
				m_ucmd->m_forwardmove = std::min< float >( 450.f, 5850.f / speed );
				m_ucmd->m_sidemove = ( m_ucmd->m_cmd_nr % 2 ) == 0 ? -450.f : 450.f;
			}
			else {
				m_ucmd->m_sidemove = m_ucmd->m_mousedx < 0.f ? -450.f : 450.f;
			}
		}
	}
}

void c_movement::edge_jump( ) {
	if ( !g_settings.misc.edge_jump ) 
		return;

	if ( !g_input.is_key_pressed( ( VirtualKeys_t )g_settings.misc.edge_jump_key( ) ) )
		return;

	//needs key check here so its not always on
	//what??
	bool pre_onground = g_ctx.m_local->m_fFlags( ) & FL_ONGROUND;
	bool post_onground = g_cheat.m_prediction.get_predicted_flags( ) & FL_ONGROUND;

	if ( pre_onground && !post_onground ) {
		m_ucmd->m_buttons |= IN_JUMP;
	}
}

void c_movement::auto_jumpbug( ) {
	if ( !g_settings.misc.auto_jumpbug( ) ) 
		return;

	if ( !g_input.is_key_pressed( ( VirtualKeys_t )g_settings.misc.auto_jumpbug_key( ) ) )
		return;

	static bool jumped = false;
	static bool jump_next = false;

	m_ucmd->m_buttons |= IN_DUCK;

	bool pre_onground = g_ctx.m_local->m_fFlags( ) & FL_ONGROUND;
	bool post_onground = g_cheat.m_prediction.get_predicted_flags( ) & FL_ONGROUND;

	if ( jump_next ) {
		m_ucmd->m_buttons &= ~IN_DUCK;
		jump_next = false;
	}
	else if ( !pre_onground && post_onground && !jumped ) {
		jump_next = true;
		jumped = true;
	}
	else if ( !pre_onground && !post_onground ) {
		jumped = false;
	}
}

//fuck hardcode
const vec3_t mins( -17.f, -17.f, 0 );
const vec3_t maxs( 17.f, 17.f, 42.f );

void rotate_movement( user_cmd_t* cmd, float rotation ) {
	rotation = rotation * M_PIRAD;

	float cos_rot = cos( rotation );
	float sin_rot = sin( rotation );

	float new_forwardmove = ( cos_rot * cmd->m_forwardmove ) - ( sin_rot * cmd->m_sidemove );
	float new_sidemove = ( sin_rot * cmd->m_forwardmove ) + ( cos_rot * cmd->m_sidemove );

	cmd->m_forwardmove = new_forwardmove;
	cmd->m_sidemove = new_sidemove;
}

float get_ideal_strafe_step( float speed ) {
	static auto* sv_airaccelerate = g_gmod.m_cvar( )->FindVar( xors( "sv_airaccelerate" ) );
	float airaccel = std::min< float >( sv_airaccelerate->get_float( ), 30.f );

	float step = std::atan2( airaccel, speed ) * M_RADPI;

	//tickcount correction
	float tickcount = 1.0f / g_gmod.m_globals->m_interval_per_tick;
	step *= ( 64.f / tickcount );

	return step;
}

float trace_ideal_step( bool direction, vec3_t direction_vec, float ideal_step, float multiplier, float max_correction ) {
	Ray_t ray;
	CTraceFilterWorldOnly filter;
	filter.pSkip = g_ctx.m_local;
	vec3_t start = g_ctx.m_local->m_vecOrigin( );
	start.z += 5.0f;

	CGameTrace trace;

	float step = ideal_step;
	float correction = 0.5f;

	if( direction ) {
		step = -step;
		correction = -correction;
		max_correction = -max_correction;
	}

	while( true ) {
		vec3_t new_dir = direction_vec;
		new_dir.y += step;

		vec3_t forward = math::angle_vectors( new_dir );
		forward *= multiplier;

		vec3_t end = start + forward;
		ray.Init( start, end, mins, maxs );

		g_gmod.m_trace( )->TraceRay( ray, MASK_SOLID, &filter, &trace );

		if( !trace.DidHit( ) ) {
			break;
		}
		else {
			new_dir.x -= 20.f;
			vec3_t forward_up = math::angle_vectors( new_dir );
			forward_up *= multiplier;

			vec3_t endUp = start + forward_up;
			ray.Init( start, endUp, mins, maxs );

			CGameTrace trace_up;
			g_gmod.m_trace( )->TraceRay( ray, MASK_SOLID, &filter, &trace_up );
			if( !trace_up.DidHit( ) ) {
				break;
			}
		}

		step += correction;

		if( direction && step <= max_correction ) {
			break;
		}
		if( !direction && step >= max_correction ) {
			break;
		}
	}

	return step;
}

float get_trace_length_multiplier( float speed ) {
	float multiplier = speed * 0.2f;
	if( multiplier < 32.f ) multiplier = 32.f;
	if( multiplier > 256.f ) multiplier = 256.f;

	return multiplier;
}

bool c_movement::get_best_direction( float ideal_step, float left, float right, float weight ) {
	float left_delta = std::fabs( ideal_step - left );
	float right_delta = std::fabs( -ideal_step - right );

	if( m_direction ) right_delta -= weight;
	else left_delta -= weight;

	return ( left_delta > right_delta );
}

float c_movement::get_best_strafe_step( float speed, vec3_t direction ) {
	float multiplier = get_trace_length_multiplier( speed );
	float ideal_step = get_ideal_strafe_step( speed );

	float left_step = trace_ideal_step( false, direction, ideal_step, multiplier, 90.f );
	float right_step = trace_ideal_step( true, direction, ideal_step, multiplier, 90.f );

	m_direction = get_best_direction( ideal_step, left_step, right_step, 25.f );

	if( m_direction ) {
		float max_clamped_correction = -ideal_step - 5.0f;
		if( right_step < max_clamped_correction ) right_step = max_clamped_correction;
		return right_step;
	}
	else {
		float max_clamped_correction = ideal_step + 5.0f;
		if( left_step > max_clamped_correction ) left_step = max_clamped_correction;
		return left_step;
	}
}

float c_movement::get_best_strafe_angle( ) {
	vec3_t velocity = g_ctx.m_local->m_vecVelocity( );
	velocity.z = 0.0f;
	float speed = velocity.length2d( );

	vec3_t direction = math::vector_angles( vec3_t( 0.0f, 0.0f, 0.0f ), velocity );
	float step = get_best_strafe_step( speed, direction );

	return direction.y + step;
}

void c_movement::circle_strafe( ) {
	if( g_settings.misc.circle_strafe ) {
		if( g_ctx.m_local->m_nMoveType( ) == MOVETYPE_LADDER || g_ctx.m_local->m_nMoveType( ) == MOVETYPE_NOCLIP )
			return;

		static bool can_finish = true;
		auto cmd = g_ctx.get_last_cmd( );
		if( g_input.is_key_pressed( g_settings.misc.circle_strafe_key ) || !can_finish ) {
			m_ucmd->m_buttons |= IN_JUMP;
			cmd->m_forwardmove = 450.f;

			float speed = g_ctx.m_local->m_vecVelocity( ).length2d( );
			if( speed > 1.f ) {
				can_finish = false;
				float angle = get_best_strafe_angle( );

				float delta = std::remainderf( m_ucmd->m_viewangles.y - angle, 360.f );

				cmd->m_forwardmove = 5850.f / speed;
				cmd->m_sidemove = m_direction ? 450.f : -450.f;
				rotate_movement( cmd, delta );

				vec3_t current_view;
				g_gmod.m_engine( )->GetViewAngles( current_view );
				float view_delta = std::remainderf( current_view.y - angle, 360.f );

				if( std::fabs( view_delta ) < 10.0f || speed < 250.f ) {
					can_finish = true;
				}
			}
			else {
				can_finish = true;
			}
		}
	}
}


END_REGION