-
Notifications
You must be signed in to change notification settings - Fork 4
/
AimAssist.cs
385 lines (326 loc) · 12.9 KB
/
AimAssist.cs
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using cs2.Config;
using cs2.Game.Objects;
using cs2.Game.Structs;
using cs2.GameOverlay;
using GameOverlay.Drawing;
using System.Numerics;
using static cs2.Offsets.OffsetsLoader;
namespace cs2.Game.Features
{
internal static class AimAssist
{
public static void Start()
{
new Thread(() =>
{
for (; ; )
{
Update();
Thread.Sleep(1);
}
}).Start();
new Thread(() =>
{
for (; ; )
{
UpdateTB();
Thread.Sleep(1);
}
}).Start();
}
private static void Update()
{
if (LocalPlayer.Current.Weapon.IsPistol)
CurrentWeaponConfig = Configuration.Current.Pistols;
else if (LocalPlayer.Current.Weapon.IsSMG)
CurrentWeaponConfig = Configuration.Current.SMGs;
else if (LocalPlayer.Current.Weapon.IsShotgun)
CurrentWeaponConfig = Configuration.Current.Shotguns;
else if (LocalPlayer.Current.Weapon.IsRifle)
CurrentWeaponConfig = Configuration.Current.Rifles;
else if (LocalPlayer.Current.Weapon.IsSniperRifle)
CurrentWeaponConfig = Configuration.Current.SniperRifles;
else
return;
vkBindXBtn.Update();
if (!CurrentWeaponConfig.EnableAimAssist)
{
Thread.Sleep(100);
return;
}
UpdateAim();
}
public static void Draw(Graphics g)
{
if (!CurrentWeaponConfig.EnableAimAssist)
return;
if (Waiting)
return;
_screenCenter = new Vector2(g.Width / 2 - 0.5f, g.Height / 2 - 0.5f);
Brushes.FOVColor.Color = _targetPtr == IntPtr.Zero ? new Color(255, 0, 0, 100) : new Color(0, 255, 0, 100);
g.DrawCircle(Brushes.FOVColor, _screenCenter.X, _screenCenter.Y, FOVRadius, 1);
if (_targetPos != Vector3.Zero)
{
var pos = _targetPos.ToScreenPos();
if (pos.IsValidScreen())
g.FillCircle(Brushes.Red, pos.X, pos.Y, 4);
}
}
private static void UpdateTB()
{
if (!CurrentWeaponConfig.EnableTriggerbot)
{
Thread.Sleep(1000);
return;
}
Triggerbot.Update(vkBindXBtn.state);
}
private static void Aim(Entity entity)
{
if (_targetPtr == IntPtr.Zero)
{
return;
}
Vector3 velocity = CurrentWeaponConfig.VelocityPrediction ? entity.Velocity / 20f : Vector3.Zero;
GetAimAngles(_targetPos + velocity, out Vector2 aimAngles);
GetAimPixels(aimAngles *= 1 / (CurrentWeaponConfig.Smoothing + 1), out var aimPixels);
if (TryMouseMove(aimPixels))
Thread.Sleep(1);
}
private static void UpdateAim()
{
UpdateAngles();
_entities.Clear();
for (int i = 0; i < Program.ENTITY_LIST_COUNT; i++)
{
Entity entity = new(i, true);
entity.Update();
if (!entity.IsAlive() || !entity.CheckTeam())
continue;
entity.UpdateAimProperties();
_entities.Add(entity);
}
if ((vkBindXBtn.state == Input.KeyState.DOWN) && _targetPtr == IntPtr.Zero)
FindTarget();
else if (vkBindXBtn.state == Input.KeyState.NONE)
{
_targetPos = Vector3.Zero;
_targetPtr = IntPtr.Zero;
_targetBone = Bone.UNKNOWN;
}
else if (vkBindXBtn.state == Input.KeyState.RELEASE)
{
_targetPos = Vector3.Zero;
_targetPtr = IntPtr.Zero;
_targetBone = Bone.UNKNOWN;
}
if (vkBindXBtn.state == Input.KeyState.DOWN)
{
if (_targetPtr == IntPtr.Zero)
return;
Entity? entity = _entities.Find(x => x.AddressBase == _targetPtr);
if (entity == null)
return;
_targetPos = entity.Bones.Find(x => x.bone == _targetBone).pos;
if (_targetPos == Vector3.Zero)
return;
Aim(entity);
}
}
#region Targeting
private static void FindTarget(bool displayPoint = false)
{
float minDistance = 99999;
foreach (var entity in _entities)
minDistance = FindBone(entity, minDistance, displayPoint);
}
private static float FindBone(Entity entity, float minDistance, bool displayPoint = false)
{
IntPtr targetPtr = IntPtr.Zero;
Vector3 targetPos = Vector3.Zero;
Vector2 targetPos2d = Vector2.Zero;
Bone targetBone = Bone.UNKNOWN;
Circle aimFOV = new Circle(_screenCenter.X, _screenCenter.Y, displayPoint ? 500 : FOVRadius);
foreach (var bone in entity.Bones)
{
if ((int)bone.bone == (int)Hitbox.head ||
(int)bone.bone == (int)Hitbox.neck_0 ||
(int)bone.bone == (int)Hitbox.spine_1 ||
(int)bone.bone == (int)Hitbox.spine_2 ||
(int)bone.bone == (int)Hitbox.pelvis)
{
Vector3 boneScreenPos = bone.pos.ToScreenPos();
Vector2 boneScreenPos2 = new Vector2(boneScreenPos.X, boneScreenPos.Y);
if (!boneScreenPos.IsValidScreen())
continue;
if (!boneScreenPos2.Touching(aimFOV, _screenCenter.X, _screenCenter.Y))
continue;
float distance = Vector2.Distance(_screenCenter, boneScreenPos2);
if (distance < minDistance)
{
minDistance = distance;
targetPos2d = boneScreenPos2;
targetBone = bone.bone;
targetPos = bone.pos;
targetPtr = entity.AddressBase;
}
}
}
if (targetPtr == IntPtr.Zero)
return minDistance;
if (displayPoint)
_targetPos2D = targetPos2d;
if (!displayPoint)
{
_targetPtr = targetPtr;
_targetPos = targetPos;
_targetBone = targetBone;
}
return minDistance;
}
#endregion
#region Angles
private static void UpdateAngles()
{
IntPtr AddressBase = Memory.Read<IntPtr>(Memory.ClientPtr + ClientOffsets.dwLocalPlayerPawn);
Origin = Memory.Read<Vector3>(AddressBase + C_BasePlayerPawn.m_vOldOrigin);
ViewOffset = Memory.Read<Vector3>(AddressBase + C_BaseModelEntity.m_vecViewOffset);
EyePosition = Origin + ViewOffset;
ViewAngles = Memory.Read<Vector3>(Memory.ClientPtr + ClientOffsets.dwViewAngles);
AimPunchAngle = Memory.Read<Vector3>(AddressBase + C_CSPlayerPawn.m_aimPunchAngle);
if (!CurrentWeaponConfig.RCS)
AimPunchAngle = Vector3.Zero;
AimDirection = GetAimDirection(ViewAngles, AimPunchAngle);
ShotsFired = Memory.Read<int>(AddressBase + C_CSPlayerPawnBase.m_iShotsFired);
IntPtr pCameraServices = Memory.Read<IntPtr>(AddressBase + C_BasePlayerPawn.m_pCameraServices);
EyeDirection = GetVectorFromEulerAngles(ViewAngles.X.DegreeToRadian(), ViewAngles.Y.DegreeToRadian());
PlayerFov = Memory.Read<int>(pCameraServices + 0x210);
if (PlayerFov == 0)
PlayerFov = 90;
}
#region Calc
private static Vector3 GetVectorFromEulerAngles(double phi, double theta)
{
return new Vector3
(
(float)(System.Math.Cos(phi) * System.Math.Cos(theta)),
(float)(System.Math.Cos(phi) * System.Math.Sin(theta)),
(float)-System.Math.Sin(phi)
).Normalized();
}
private static Vector3 GetAimDirection(Vector3 viewAngles, Vector3 aimPunchAngle)
{
var phi = (viewAngles.X + aimPunchAngle.X * 2f).DegreeToRadian();
var theta = (viewAngles.Y + aimPunchAngle.Y * 2f).DegreeToRadian();
return Vector3.Normalize(new Vector3
(
(float)(Math.Cos(phi) * Math.Cos(theta)),
(float)(Math.Cos(phi) * Math.Sin(theta)),
(float)-Math.Sin(phi)
));
}
private static void GetAimPixels(Vector2 aimAngles, out Point aimPixels)
{
var fovRatio = 90.0 / PlayerFov;
aimPixels = new Point
(
(int)Math.Round(aimAngles.X / AnglePerPixel * fovRatio),
(int)Math.Round(aimAngles.Y / AnglePerPixel * fovRatio)
);
}
private static bool TryMouseMove(Point aimPixels)
{
if (CurrentWeaponConfig.ShotsFired > ShotsFired)
return false;
if (aimPixels.X == 0 && aimPixels.Y == 0)
{
return false;
}
Input.MouseMove((int)aimPixels.X, (int)aimPixels.Y);
return true;
}
private static void GetAimAngles(Vector3 pointWorld, out Vector2 aimAngles)
{
var aimDirection = AimDirection;
var aimDirectionDesired = (pointWorld - EyePosition).Normalized();
aimAngles = new Vector2
(
aimDirectionDesired.AngleToSigned(aimDirection, new Vector3(0, 0, 1)),
aimDirectionDesired.AngleToSigned(aimDirection, aimDirectionDesired.Cross(new Vector3(0, 0, 1)).Normalized())
);
}
#endregion
#endregion
public static void Calibrate()
{
Thread.Sleep(1000);
Console.Beep();
Thread.Sleep(1000);
Console.Beep();
Thread.Sleep(1000);
Console.Beep(700, 600);
var AnglePerPixel = new[]
{
CalibrationMeasureAnglePerPixel(100),
CalibrationMeasureAnglePerPixel(-200),
CalibrationMeasureAnglePerPixel(300),
CalibrationMeasureAnglePerPixel(-400),
CalibrationMeasureAnglePerPixel(200),
}.Average();
Console.Beep();
Console.WriteLine($"{nameof(AnglePerPixel)} = {AnglePerPixel}");
}
private static double CalibrationMeasureAnglePerPixel(int deltaPixels)
{
// measure starting angle
Thread.Sleep(100);
var eyeDirectionStart = EyeDirection;
eyeDirectionStart.Z = 0;
// rotate
Input.MouseMove(deltaPixels, 0);
// measure end angle
Thread.Sleep(100);
var eyeDirectionEnd = EyeDirection;
eyeDirectionEnd.Z = 0;
// get angle and divide by number of pixels
return eyeDirectionEnd.AngleTo(eyeDirectionStart) / Math.Abs(deltaPixels);
}
public static bool Waiting
{
get; set;
} = false;
private static float FOVRadius
{
get
{
return LocalPlayer.Current.IsScoped ? 450 : CurrentWeaponConfig.FOV + 10;
}
}
private static Vector3 Origin { get; set; }
private static Vector3 ViewOffset { get; set; }
public static Vector3 EyePosition { get; private set; }
public static Vector3 ViewAngles { get; set; }
public static Vector3 AimPunchAngle { get; private set; }
public static Vector3 AimDirection { get; private set; }
public static int ShotsFired { get; private set; }
private static WeaponConfig CurrentWeaponConfig { get; set; } = Configuration.Current.Pistols;
public static double AnglePerPixel { get; set; } = 0.0007679434260353446;
private static Vector3 EyeDirection { get; set; }
public static int PlayerFov { get; set; }
private static List<Entity> _entities = new List<Entity>();
public static Input.Key vkBindXBtn = new Input.Key(6); //6 xbtn
internal static IntPtr _targetPtr = IntPtr.Zero;
internal static Vector3 _targetPos = Vector3.Zero;
internal static Vector2 _targetPos2D = Vector2.Zero;
internal static Bone _targetBone = Bone.UNKNOWN;
private static Vector2 _screenCenter;
public enum Hitbox
{
head = 6,
neck_0 = 5,
spine_1 = 4,
spine_2 = 2,
pelvis = 0,
}
}
}