Mouse Movement in Game [FPS] to Target Coordinates - Help

Ask gaming related questions (AHK v1.1 and older)
prototype_zero
Posts: 16
Joined: 19 Mar 2024, 13:01

Mouse Movement in Game [FPS] to Target Coordinates - Help

05 Apr 2024, 16:49

Hello all, I am appealing the good folks on this forum for help.

I am hoping someone could assist me in the final part of my script.

The script is meant for FPS games. The script reads object coordinate data and then moves the mouse towards those coordinates. It works when I am on the desktop, for instance, while watching gameplay footage. However, when in the actual game, there is no movement (except in the menu where there is a mouse cursor). If I alt-tab the game/put it into window mode where the mouse cursor overlays the game window, it follows the target relatively as expected. Improvements in that area can be implemented if or when actual FOV/aim movement is implemented.

I understand that game engines process inputs from the mouse differently than desktop applications do. I have made and used different AHK scripts in FPS games. It seems that its the DLLCall mouse_event function works, however, not when it comes to absolute measurements, but that seems to be required to produce proper measurements.

Below is my code so far. Any assistance in this would be greatly appreciated

Code: Select all

; Function to move the mouse cursor smoothly to a specific position on the screen
SmoothMoveMouseTo(targetX, targetY) {
    ; Get the current mouse position
    MouseGetPos, startX, startY
    
    ; Define the number of steps for interpolation
    steps := 20
    
    ; Calculate the step size for x and y
    stepX := (targetX - startX) / steps
    stepY := (targetY - startY) / steps
    
    ; Smoothly move the mouse cursor to the target position
    Loop, % steps {
        newX := startX + A_Index * stepX
        newY := startY + A_Index * stepY
        DllCall("mouse_event", UInt, 1, Int, newX - startX, Int, newY - startY, UInt, 0, UInt, 0)
        Sleep, 20  ; Adjust this value for the smoothness of movement
    }
}

; Function to move the mouse cursor to a specific position on the screen (absolute)
MoveMouseToAbsolute(x, y) {
    Static SysX := 0x10000/A_ScreenWidth, SysY := 0x10000/A_ScreenHeight
    
    ; Calculate the absolute coordinates
    xa := Ceil(x * SysX)
    ya := Ceil(y * SysY)
    
    ; Move the mouse using mouse_event function, 0x8001: ABSMOVE
    DllCall("mouse_event", "UInt", 0x8001, "UInt", xa, "UInt", ya)
}

; Function to read coordinates from the named pipe
ReadCoordinatesFromPipe() {
    ; Create named pipe handle
    pipe := DllCall("CreateFile", "Str", "\\.\pipe\SamplePipe", "UInt", 0xC0000000, "UInt", 3, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)
    
    ; Check if the pipe was opened successfully
    if (pipe = -1) {
        MsgBox, Failed to connect to named pipe.
        ExitApp
    }
    
    ; Infinite loop to read coordinates from the pipe
    Loop {
        VarSetCapacity(data, 8)
        
        ; Check if there is data available in the pipe
        DllCall("PeekNamedPipe", "Ptr", pipe, "Ptr", 0, "UInt", 0, "Ptr", 0, "PtrP", bytesAvailable, "Ptr", 0)
        
        ; Read data from the pipe if available
        if (bytesAvailable > 0) {
            ; Read the data from the pipe
            bytesRead := DllCall("ReadFile", "Ptr", pipe, "Ptr", &data, "UInt", 8, "Ptr", 0, "UInt", 0)
            
            ; Check if data was read successfully
            if (bytesRead > 0) {
                ; Extract x and y coordinates from the data
                x := NumGet(data, 0, "Int")
                y := NumGet(data, 4, "Int")
                
                ; Move the mouse to the retrieved coordinates
                MoveMouseToAbsolute(x, y)
                
                ; Show tooltip with target coordinates
                Tooltip, Target coordinates:`nX: %x%`nY: %y%
            } else if (bytesRead < 0) {
                MsgBox, Error reading from named pipe.
                ExitApp
            }
        }
        
        ; Sleep for a short duration before checking again
        Sleep, 100
    }
}

; Start reading coordinates from the named pipe
ReadCoordinatesFromPipe()

; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp

prototype_zero
Posts: 16
Joined: 19 Mar 2024, 13:01

Re: Mouse Movement in Game [FPS] to Target Coordinates - Help

09 Apr 2024, 15:04

I made some modifications, and was able to get some movement in game. While the mouse sort of moves towards the center target coordinates, it is not very consistent, and often, once reaching the center of the object, will then be pushed away along with y-axis. The movement is also not very smooth, and potentially laggy. Any help would be appreciated

Code: Select all

; main_script.ahk

#Include mousedelta.ahk

; Function to move the mouse cursor smoothly to a specific position on the screen
SmoothMoveMouseTo(targetX, targetY) {
    ; Get the screen width and height
    screenWidth := A_ScreenWidth
    screenHeight := A_ScreenHeight
    
    ; Get the current mouse position
    MouseGetPos, mouseX, mouseY

    ; Calculate the distance from the center of the screen to the target
    deltaX := targetX - (screenWidth / 2)
    deltaY := targetY - (screenHeight / 2)

    ; Define the maximum distance from the center for which the mouse should move
    maxDistance := 100  ; Adjust as needed
    
    ; If the target is within the allowed range from the center, move the mouse
    if (Abs(deltaX) <= maxDistance and Abs(deltaY) <= maxDistance) {
        ; Calculate the new mouse position to center it on the target
        newMouseX := mouseX + deltaX
        newMouseY := mouseY + deltaY
        
        ; Smoothly move the mouse cursor to the new position
        DllCall("mouse_event", UInt, 1, Int, newMouseX - mouseX, Int, newMouseY - mouseY, UInt, 0, UInt, 0)
    }
}

; Function to read coordinates from the named pipe
ReadCoordinatesFromPipe() {
    ; Create named pipe handle
    pipe := DllCall("CreateFile", "Str", "\\.\pipe\SamplePipe", "UInt", 0xC0000000, "UInt", 3, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)
    
    ; Check if the pipe was opened successfully
    if (pipe = -1) {
        MsgBox, Failed to connect to named pipe.
        ExitApp
    }
    
    ; Infinite loop to read coordinates from the pipe
    Loop {
        VarSetCapacity(data, 8)
        
        ; Check if there is data available in the pipe
        DllCall("PeekNamedPipe", "Ptr", pipe, "Ptr", 0, "UInt", 0, "Ptr", 0, "PtrP", bytesAvailable, "Ptr", 0)
        
        ; Read data from the pipe if available
        if (bytesAvailable > 0) {
            ; Read the data from the pipe
            bytesRead := DllCall("ReadFile", "Ptr", pipe, "Ptr", &data, "UInt", 8, "Ptr", 0, "UInt", 0)
            
            ; Check if data was read successfully
            if (bytesRead > 0) {
                ; Extract x and y coordinates from the data
                x := NumGet(data, 0, "Int")
                y := NumGet(data, 4, "Int")
                
                ; Move the mouse to the retrieved coordinates using smooth movement function
                SmoothMoveMouseTo(x, y)
                
                ; Show tooltip with target coordinates
                Tooltip, Target coordinates:`nX: %x%`nY: %y%
            } else if (bytesRead < 0) {
                MsgBox, Error reading from named pipe.
                ExitApp
            }
        }
        
        ; Sleep for a short duration before checking again
        Sleep, 100
    }
}

; Start reading coordinates from the named pipe
ReadCoordinatesFromPipe()

; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp
prototype_zero
Posts: 16
Joined: 19 Mar 2024, 13:01

Re: Mouse Movement in Game [FPS] to Target Coordinates - Help

09 Apr 2024, 22:43

Hi @Rohwedder. Your assistance in a previous post regarding mouse movement was of great benefit, I thought I might @ you to see if you could help with this issue I'm facing with this script.
Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Mouse Movement in Game [FPS] to Target Coordinates - Help

10 Apr 2024, 01:33

Hallo,
unfortunately, I have to say no.
I am a complete non-gamer and only know these problems with FPS games from the Autohotkey forum. Just to study these problems, I downloaded a few free, supposedly offline FPS games, but always got a prompt to create an account - nope, never - uninstall.
Do you know of a way to study these FPS effects in peace without having to fight for your life?
prototype_zero
Posts: 16
Joined: 19 Mar 2024, 13:01

Re: Mouse Movement in Game [FPS] to Target Coordinates - Help

10 Apr 2024, 11:40

Rohwedder wrote:
10 Apr 2024, 01:33
Hallo,
unfortunately, I have to say no.
I am a complete non-gamer and only know these problems with FPS games from the Autohotkey forum. Just to study these problems, I downloaded a few free, supposedly offline FPS games, but always got a prompt to create an account - nope, never - uninstall.
Do you know of a way to study these FPS effects in peace without having to fight for your life?
Thanks for your response either way, your expertise is always greatly appreciated.

For me, I can just go into training mode where I can test a script, alt-tab, make modifications, reload the script, and observe. But, it seems most modern day games require account creation. Maybe I can find a game that doesn't, although it's not like the good old days of gaming where you could just load up a demo from a floppy disk lol
prototype_zero
Posts: 16
Joined: 19 Mar 2024, 13:01

Re: Mouse Movement in Game [FPS] to Target Coordinates - Help

12 Apr 2024, 11:28

Rohwedder wrote:
10 Apr 2024, 01:33
Hallo,
unfortunately, I have to say no.
I am a complete non-gamer and only know these problems with FPS games from the Autohotkey forum. Just to study these problems, I downloaded a few free, supposedly offline FPS games, but always got a prompt to create an account - nope, never - uninstall.
Do you know of a way to study these FPS effects in peace without having to fight for your life?
Hey there, I was wondering if I could get your input on this code below. I actually resolved my initial problem, which was specific to gaming/FPS perspective. However, there are some related and persistent issues, namely the reaction time of the cursor to move to the center of the object, and also the jerkiness of the movement itself. Would you see any areas where those could be resolved in this script?

Code: Select all

#Include mousedelta.ahk

; Function to move the mouse cursor smoothly to a specific position on the screen
SmoothMoveMouseTo(targetX, targetY, duration, speedFactor) {
    ; Get the current mouse position
    MouseGetPos, mouseX, mouseY
    
    ; Calculate the distance to move
    distanceX := targetX - mouseX
    distanceY := targetY - mouseY
    
    ; Calculate the total distance
    totalDistance := Sqrt(distanceX ** 2 + distanceY ** 2)
    
    ; Calculate step increments
    steps := 1 * Max(Abs(distanceX), Abs(distanceY))
    stepX := distanceX / steps
    stepY := -1 * (distanceY / steps)  ; Invert y-axis movement
    
    ; Calculate step duration based on total distance and desired duration
    stepDuration := duration / totalDistance
    
    ; Adjust step duration based on speed factor
    stepDuration := stepDuration / speedFactor
    
    ; Perform smooth mouse movement
    Loop, % steps {
        mouseX += stepX
        mouseY += stepY
        DllCall("mouse_event", UInt, 1, Int, Round(stepX), Int, Round(stepY), UInt, 0, UInt, 0)
        Sleep, stepDuration
    }
}


; Function to read coordinates from the named pipe
ReadCoordinatesFromPipe() {
    ; Create named pipe handle
    pipe := DllCall("CreateFile", "Str", "\\.\pipe\SamplePipe", "UInt", 0xC0000000, "UInt", 3, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)
    
    ; Check if the pipe was opened successfully
    if (pipe = -1) {
        MsgBox, Failed to connect to named pipe.
        ExitApp
    }
    
    ; Infinite loop to read coordinates from the pipe
    Loop {
        VarSetCapacity(data, 8)
        
        ; Check if there is data available in the pipe
        DllCall("PeekNamedPipe", "Ptr", pipe, "Ptr", 0, "UInt", 0, "Ptr", 0, "PtrP", bytesAvailable, "Ptr", 0)
        
        ; Read data from the pipe if available
        if (bytesAvailable > 0) {
            ; Read the data from the pipe
            bytesRead := DllCall("ReadFile", "Ptr", pipe, "Ptr", &data, "UInt", 8, "Ptr", 0, "UInt", 0)
            
            ; Check if data was read successfully
            if (bytesRead > 0) {
                ; Extract x and y coordinates from the data
                x := NumGet(data, 0, "Int")
                y := NumGet(data, 4, "Int")
                
                ; Move the mouse to the retrieved coordinates using smooth movement function
                SmoothMoveMouseTo(x, y, 3, 10)  ; Adjust duration and speed factor as needed
                
            } else if (bytesRead < 0) {
                MsgBox, Error reading from named pipe.
                ExitApp
            }
        }
        
        ; Sleep for a short duration before checking again
        Sleep, 1
    }
}

; Start reading coordinates from the named pipe
ReadCoordinatesFromPipe()

; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: Mementoe, sofista and 44 guests