- wind mouse
- code optimizations
- mouse movement path preview
This commit is contained in:
2025-10-16 20:32:31 -04:00
parent ab0ca784f4
commit 2fa0b4ac2d
5 changed files with 330 additions and 25 deletions
+8 -1
View File
@@ -303,7 +303,8 @@ namespace Spectrum
CubicBezier,
QuadraticBezier,
Adaptive,
PerlinNoise
PerlinNoise,
WindMouse
}
public enum MovementMethod
{
@@ -357,6 +358,12 @@ namespace Spectrum
public double EmaSmootheningFactor { get; set; } = 0.1;
public MovementMethod MovementMethod { get; set; } = MovementMethod.MouseEvent;
public double WindMouseGravity { get; set; } = 9.0;
public double WindMouseWind { get; set; } = 3.0;
public double WindMouseMaxStep { get; set; } = 10.0;
public double WindMouseTargetArea { get; set; } = 5.0;
public bool WindMouseOvershoot { get; set; } = false;
#endregion
#region Trigger Settings
+7
View File
@@ -175,6 +175,11 @@ namespace Spectrum.Detection
}
InputManager.SetLastDetection(new System.Drawing.Point(targetX, targetY));
InputManager.SetLastDetectionBox(new Rectangle(
bestRect.X + bounds.X,
bestRect.Y + bounds.Y,
bestRect.Width,
bestRect.Height));
if (config.EnableAim)
{
@@ -193,6 +198,7 @@ namespace Spectrum.Detection
else if (config.CollectData && drawing != null)
{
AutoLabeling.AddBackgroundImage(drawing, false);
MovementPaths.ResetWindMouse();
}
renderer?.CommitDetectionDrawCommands();
@@ -234,6 +240,7 @@ namespace Spectrum.Detection
{
renderer?.ClearDetectionDrawCommands();
renderer?.CommitDetectionDrawCommands();
MovementPaths.ResetWindMouse();
Thread.Sleep(5);
}
if (InputManager.IsKeyOrMouseDown(mainConfig.Data.MenuKey))
+36 -18
View File
@@ -12,6 +12,7 @@ namespace Spectrum.Input
public static extern bool GetCursorPos(out Point lpPoint);
private static ConfigManager<ConfigData> mainConfig = Program.mainConfig;
private static Point lastDetection = new Point();
private static Rectangle lastDetectionBox = new Rectangle();
private static long _lastMoveTicks = 0;
private static readonly double _dtRef = 1.0 / 120.0;
@@ -46,8 +47,9 @@ namespace Spectrum.Input
public static void MoveMouse()
{
var config = mainConfig.Data;
Point reference = new Point();
if (mainConfig.Data.ClosestToMouse)
if (config.ClosestToMouse)
{
GetCursorPos(out reference);
}
@@ -61,21 +63,31 @@ namespace Spectrum.Input
Point end = new Point(lastDetection.X, lastDetection.Y);
double dt = GetDeltaSeconds();
double effSensitivity = TimeScaleFactor(mainConfig.Data.Sensitivity, dt);
double effSensitivity = TimeScaleFactor(config.Sensitivity, dt);
Point newPosition = mainConfig.Data.AimMovementType switch
bool insideBoundingBox = lastDetectionBox.Contains(start);
Point newPosition = config.AimMovementType switch
{
MovementType.CubicBezier => MovementPaths.CubicBezierMovement(start, end, effSensitivity),
MovementType.CubicBezier => MovementPaths.CubicBezierCurvedMovement(start, end, effSensitivity),
MovementType.Linear => MovementPaths.LinearInterpolation(start, end, effSensitivity),
MovementType.Adaptive => MovementPaths.AdaptiveMovement(start, end, effSensitivity),
MovementType.QuadraticBezier => MovementPaths.CurvedMovement(start, end, effSensitivity),
MovementType.PerlinNoise => MovementPaths.PerlinNoiseMovement(start, end, effSensitivity),
_ => end
MovementType.WindMouse => MovementPaths.WindMouse(start, end,
config.WindMouseGravity,
config.WindMouseWind,
config.WindMouseMaxStep,
config.WindMouseTargetArea,
effSensitivity,
config.WindMouseOvershoot,
insideBoundingBox),
_ => MovementPaths.LinearInterpolation(start, end, effSensitivity),
};
if (mainConfig.Data.EmaSmoothening)
if (config.EmaSmoothening)
{
double emaAlpha = TimeScaleFactor(mainConfig.Data.EmaSmootheningFactor, dt);
double emaAlpha = TimeScaleFactor(config.EmaSmootheningFactor, dt);
newPosition.X = (int)Math.Round(MovementPaths.EmaSmoothing(reference.X, newPosition.X, emaAlpha));
newPosition.Y = (int)Math.Round(MovementPaths.EmaSmoothing(reference.Y, newPosition.Y, emaAlpha));
}
@@ -84,10 +96,10 @@ namespace Spectrum.Input
int deltaY = newPosition.Y - reference.Y;
if (Math.Abs(deltaX) < 1 && Math.Abs(deltaY) < 1) return;
if (deltaX > mainConfig.Data.ImageWidth || deltaX < -mainConfig.Data.ImageWidth || deltaY > mainConfig.Data.ImageHeight || deltaY < -mainConfig.Data.ImageHeight)
if (deltaX > config.ImageWidth || deltaX < -config.ImageWidth || deltaY > config.ImageHeight || deltaY < -config.ImageHeight)
return;
switch (mainConfig.Data.MovementMethod)
switch (config.MovementMethod)
{
case MovementMethod.MouseEvent:
MouseEvent.Move(deltaX, deltaY);
@@ -109,13 +121,14 @@ namespace Spectrum.Input
public static async Task ClickMouse()
{
if (!mainConfig.Data.TriggerBot)
var config = mainConfig.Data;
if (!config.TriggerBot)
return;
if (!IsKeyOrMouseDown(mainConfig.Data.TriggerKeybind) || mainConfig.Data.TriggerKeybind.Key == Keys.None)
if (!IsKeyOrMouseDown(config.TriggerKeybind) || config.TriggerKeybind.Key == Keys.None)
return;
var currentPosition = new Point();
if (mainConfig.Data.ClosestToMouse)
if (config.ClosestToMouse)
{
GetCursorPos(out currentPosition);
}
@@ -125,14 +138,14 @@ namespace Spectrum.Input
currentPosition.Y = SystemHelper.GetPrimaryScreenSize().Height / 2;
}
if (mainConfig.Data.TriggerDelay > 0)
await Task.Delay(mainConfig.Data.TriggerDelay);
if (config.TriggerDelay > 0)
await Task.Delay(config.TriggerDelay);
int radius = mainConfig.Data.TriggerFov;
int radius = config.TriggerFov;
if (Math.Abs(currentPosition.X - lastDetection.X) < radius && Math.Abs(currentPosition.Y - lastDetection.Y) < radius)
{
switch (mainConfig.Data.MovementMethod)
switch (config.MovementMethod)
{
case MovementMethod.MouseEvent:
MouseEvent.ClickDown();
@@ -150,8 +163,8 @@ namespace Spectrum.Input
MouseEvent.ClickDown();
break;
}
await Task.Delay(mainConfig.Data.TriggerDuration);
switch (mainConfig.Data.MovementMethod)
await Task.Delay(config.TriggerDuration);
switch (config.MovementMethod)
{
case MovementMethod.MouseEvent:
MouseEvent.ClickUp();
@@ -181,6 +194,11 @@ namespace Spectrum.Input
lastDetection = point;
}
public static void SetLastDetectionBox(Rectangle box)
{
lastDetectionBox = box;
}
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
+161
View File
@@ -7,6 +7,12 @@ namespace Spectrum.Input
private static readonly int[] _permutation = GeneratePermutation();
private static readonly int[] _p;
private static double _perlinTime = 0.0;
private static Point _lastMove = new Point(0, 0);
private static double _windX = 0.0;
private static double _windY = 0.0;
private static double _velocityX = 0.0;
private static double _velocityY = 0.0;
static MovementPaths()
{
@@ -150,6 +156,161 @@ namespace Spectrum.Input
int y = (int)Math.Round(EmaSmoothing(previous.Y, current.Y, smoothingFactor));
return new Point(x, y);
}
public static Point WindMouse(Point start, Point end, double gravity = 9.0, double wind = 3.0,
double maxStep = 10.0, double targetArea = 5.0, double sensitivity = 1.0, bool enableOvershoot = false, bool insideBoundingBox = false)
{
double dx = end.X - start.X;
double dy = end.Y - start.Y;
double distance = Math.Sqrt(dx * dx + dy * dy);
if (insideBoundingBox)
{
double trackingSpeed = Math.Min(distance * 0.35, maxStep * sensitivity * 0.6);
double dirX = distance > 0 ? dx / distance : 0;
double dirY = distance > 0 ? dy / distance : 0;
double humanVariation = Math.Sin(_perlinTime * 1.2) * 0.4 + Math.Cos(_perlinTime * 0.8) * 0.25;
_perlinTime += 0.03;
double smoothX = dirX * trackingSpeed + humanVariation;
double smoothY = dirY * trackingSpeed + humanVariation * 0.7;
int trackX = (int)Math.Round(start.X + smoothX);
int trackY = (int)Math.Round(start.Y + smoothY);
_windX *= 0.15;
_windY *= 0.15;
_velocityX *= 0.3;
_velocityY *= 0.3;
Point trackResult = new Point(trackX, trackY);
_lastMove = trackResult;
return trackResult;
}
if (distance <= targetArea)
{
double trackingSpeed = Math.Min(distance * 0.6, maxStep * sensitivity * 0.5);
double dirX = dx / distance;
double dirY = dy / distance;
double microAdjustment = Math.Sin(_perlinTime * 2.0) * 0.3;
_perlinTime += 0.05;
int trackX = (int)Math.Round(start.X + dirX * trackingSpeed + microAdjustment);
int trackY = (int)Math.Round(start.Y + dirY * trackingSpeed + microAdjustment);
_windX *= 0.3;
_windY *= 0.3;
_velocityX *= 0.5;
_velocityY *= 0.5;
Point trackResult = new Point(trackX, trackY);
_lastMove = trackResult;
return trackResult;
}
if (distance <= targetArea * 2.5)
{
double trackingSpeed = Math.Min(distance * 0.5, maxStep * sensitivity * 0.7);
double dirX = dx / distance;
double dirY = dy / distance;
double smoothNoise = Math.Sin(_perlinTime * 1.5) * (distance / targetArea) * 0.5;
_perlinTime += 0.08;
double blendFactor = (distance - targetArea) / (targetArea * 1.5);
_velocityX = _velocityX * blendFactor * 0.5 + dirX * trackingSpeed * gravity * 0.1;
_velocityY = _velocityY * blendFactor * 0.5 + dirY * trackingSpeed * gravity * 0.1;
_windX *= blendFactor * 0.4;
_windY *= blendFactor * 0.4;
int trackX = (int)Math.Round(start.X + dirX * trackingSpeed + smoothNoise);
int trackY = (int)Math.Round(start.Y + dirY * trackingSpeed + smoothNoise);
Point trackResult = new Point(trackX, trackY);
_lastMove = trackResult;
return trackResult;
}
double sensitivityMultiplier = Math.Max(0.1, sensitivity);
double distanceScale = Math.Min(distance / 100.0, 3.0);
double dynamicMaxStep = maxStep * sensitivityMultiplier * (1.0 + distanceScale);
double proximityThreshold = Math.Max(targetArea * 10, 50);
double proximityFactor = Math.Min(distance / proximityThreshold, 1.0);
double windReduction = Math.Pow(proximityFactor, 4.0);
double effectiveWind = wind * windReduction;
double effectiveGravity = gravity * (1.0 + distanceScale * 0.3) * sensitivityMultiplier;
if (enableOvershoot)
{
effectiveGravity *= 1.15;
effectiveWind *= 1.2;
}
if (distance < proximityThreshold)
{
double dampingFactor = (distance / proximityThreshold);
dampingFactor = Math.Pow(dampingFactor, 2.0);
_velocityX *= dampingFactor;
_velocityY *= dampingFactor;
double windDamping = Math.Pow(dampingFactor, 4.0);
_windX *= windDamping;
_windY *= windDamping;
}
_windX = _windX / Math.Sqrt(3) + (Random.Shared.NextDouble() * 2 - 1) * effectiveWind;
_windY = _windY / Math.Sqrt(3) + (Random.Shared.NextDouble() * 2 - 1) * effectiveWind;
_velocityX += _windX + effectiveGravity * dx / distance;
_velocityY += _windY + effectiveGravity * dy / distance;
double velocityMag = Math.Sqrt(_velocityX * _velocityX + _velocityY * _velocityY);
if (velocityMag > dynamicMaxStep)
{
double ratio = dynamicMaxStep / velocityMag;
_velocityX *= ratio;
_velocityY *= ratio;
velocityMag = dynamicMaxStep;
}
if (!enableOvershoot && distance < targetArea * 4)
{
double maxAllowedVelocity = distance * 0.4;
if (velocityMag > maxAllowedVelocity && velocityMag > 0)
{
double limitRatio = maxAllowedVelocity / velocityMag;
_velocityX *= limitRatio;
_velocityY *= limitRatio;
}
}
int nextX = (int)Math.Round(start.X + _velocityX);
int nextY = (int)Math.Round(start.Y + _velocityY);
Point result = new Point(nextX, nextY);
_lastMove = result;
return result;
}
public static void ResetWindMouse()
{
_windX = 0.0;
_windY = 0.0;
_velocityX = 0.0;
_velocityY = 0.0;
_lastMove = new Point(0, 0);
}
private static int[] GeneratePermutation()
{
int[] perm = new int[256];
+118 -6
View File
@@ -106,6 +106,9 @@ namespace Spectrum
}
ImGui.TextUnformatted("Movement Path");
ImGui.SameLine(ImGui.GetContentRegionAvail().X - 9);
if (ImGui.Button(_showMovementPreview ? "X" : "?", new Vector2(20,20)))
_showMovementPreview = !_showMovementPreview;
ImGui.SetNextItemWidth(-1);
MovementType AimPath = config.AimMovementType;
if (ImGui.BeginCombo("##Movement Type", AimPath.ToString()))
@@ -140,9 +143,9 @@ namespace Spectrum
if (ImGuiExtensions.SliderFill("Target Area", ref windMouseTargetArea, 1.0f, 20.0f, "%.1f"))
config.WindMouseTargetArea = windMouseTargetArea;
bool preventOvershoot = config.WindMousePreventOvershoot;
if (ImGui.Checkbox("Prevent Overshoot", ref preventOvershoot))
config.WindMousePreventOvershoot = preventOvershoot;
bool enableOvershoot = config.WindMouseOvershoot;
if (ImGui.Checkbox("Enable Overshoot", ref enableOvershoot))
config.WindMouseOvershoot = enableOvershoot;
}
bool YPixelOffset = config.YPixelOffset;
@@ -713,19 +716,128 @@ namespace Spectrum
ImGuiExtensions.EndPane();
}
ImGui.EndTabItem();
}
ImGui.EndTabBar();
ImGui.End();
RenderHSVRangeWindow();
RenderMovementPreview();
}
private bool _showMovementPreview = false;
private System.Drawing.Point _previewStartPoint = new(100, 100);
private System.Drawing.Point _previewEndPoint = new(400, 300);
private List<(System.Drawing.Point point, double speed)> _previewPath = [];
private int UpdatePreviewInterval = 10; // ms
private DateTime _lastPreviewUpdate = DateTime.MinValue;
private void RenderMovementPreview()
{
if (!_showMovementPreview)
return;
var config = mainConfig.Data;
ImGui.SetNextWindowSize(new Vector2(500, 500), ImGuiCond.Always);
ImGui.SetNextWindowPos(new Vector2(screenSize.width / 2 - 300, screenSize.height / 2 - 250), ImGuiCond.FirstUseEver);
if (!ImGui.Begin("Movement Path Preview", ref _showMovementPreview, ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar))
{
ImGui.End();
return;
}
ImGui.TextUnformatted("Movement Path Preview");
ImGui.SameLine(ImGui.GetContentRegionAvail().X - 14);
if (ImGui.Button("X", new Vector2(22, 22)))
{
_showMovementPreview = false;
ImGui.End();
return;
}
if ((DateTime.Now - _lastPreviewUpdate).TotalMilliseconds > UpdatePreviewInterval)
{
_lastPreviewUpdate = DateTime.Now;
_previewPath.Clear();
MovementPaths.ResetWindMouse();
double progress = 0.0;
double increment = config.Sensitivity * 0.02;
System.Drawing.Point current = _previewStartPoint;
int steps = 0;
int maxSteps = 10000;
while (true)
{
double distance = Math.Sqrt(Math.Pow(_previewEndPoint.X - current.X, 2) + Math.Pow(_previewEndPoint.Y - current.Y, 2));
if (distance < config.WindMouseTargetArea || steps++ >= maxSteps)
break;
System.Drawing.Point nextPoint = config.AimMovementType switch
{
MovementType.Linear => MovementPaths.LinearInterpolation(current, _previewEndPoint, progress),
MovementType.CubicBezier => MovementPaths.CubicBezierCurvedMovement(current, _previewEndPoint, progress),
MovementType.Adaptive => MovementPaths.AdaptiveMovement(current, _previewEndPoint, progress),
MovementType.QuadraticBezier => MovementPaths.CurvedMovement(current, _previewEndPoint, progress),
MovementType.PerlinNoise => MovementPaths.PerlinNoiseMovement(current, _previewEndPoint, progress),
MovementType.WindMouse => MovementPaths.WindMouse(current, _previewEndPoint,
config.WindMouseGravity,
config.WindMouseWind,
config.WindMouseMaxStep,
config.WindMouseTargetArea,
config.Sensitivity,
config.WindMouseOvershoot,
false),
_ => MovementPaths.LinearInterpolation(current, _previewEndPoint, progress),
};
if (config.EmaSmoothening)
nextPoint = MovementPaths.EmaSmoothing(current, nextPoint, config.EmaSmootheningFactor);
double speed = Math.Sqrt(Math.Pow(nextPoint.X - current.X, 2) + Math.Pow(nextPoint.Y - current.Y, 2));
_previewPath.Add((nextPoint, speed));
current = nextPoint;
progress += increment;
}
}
var drawList = ImGui.GetWindowDrawList();
Vector2 canvasPos = ImGui.GetCursorScreenPos();
canvasPos.Y += 5;
Vector2 canvasSize = ImGui.GetContentRegionAvail();
Vector2 size = new Vector2(canvasSize.X, canvasSize.Y - 60);
drawList.AddRectFilled(canvasPos, size + canvasPos, ImGui.GetColorU32(new Vector4(0.12f, 0.12f, 0.14f, 1.0f)), 3f);
drawList.AddRect(canvasPos, size + canvasPos, ImGui.GetColorU32(new Vector4(0.17f, 0.17f, 0.20f, 1.0f)), 3f);
if (_previewPath.Count > 1)
{
double maxSpeed = _previewPath.Max(p => p.speed);
for (int i = 1; i < _previewPath.Count; i++)
{
var (prevPoint, _) = _previewPath[i - 1];
var (currPoint, speed) = _previewPath[i];
Vector2 p1 = new Vector2(canvasPos.X + prevPoint.X, canvasPos.Y + prevPoint.Y);
Vector2 p2 = new Vector2(canvasPos.X + currPoint.X, canvasPos.Y + currPoint.Y);
Vector4 color = speed < maxSpeed / 2 ?
Vector4.Lerp(new Vector4(0, 1, 0, 1), new Vector4(1, 1, 0, 1), (float)(speed / (maxSpeed / 2))) :
Vector4.Lerp(new Vector4(1, 1, 0, 1), new Vector4(1, 0, 0, 1), (float)((speed - (maxSpeed / 2)) / (maxSpeed / 2)));
drawList.AddLine(p1, p2, ImGui.GetColorU32(color), 2.0f);
}
}
drawList.AddCircleFilled(new Vector2(canvasPos.X + _previewStartPoint.X, canvasPos.Y + _previewStartPoint.Y), 5.0f, ImGui.GetColorU32(new Vector4(0, 1, 0, 1)));
drawList.AddCircleFilled(new Vector2(canvasPos.X + _previewEndPoint.X, canvasPos.Y + _previewEndPoint.Y), 5.0f, ImGui.GetColorU32(new Vector4(1, 0, 0, 1)));
size.Y += 10;
ImGui.Dummy(size);
int interval = UpdatePreviewInterval;
if (ImGuiExtensions.SliderFill("Update Interval (ms)", ref interval, 1, 100))
UpdatePreviewInterval = interval;
ImGui.End();
}
protected void RenderOverlay()
{