Advertisement
Guest User

AutoIt CameraMovement to MouseInput

a guest
Aug 2nd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 11.87 KB | None | 0 0
  1. #include <Misc.au3>
  2. #include <Constants.au3>
  3. #include <WindowsConstants.au3>
  4.  
  5. ; Define swing parameters
  6. Local $swing_Angle = 94
  7. Local $swing_period = 12000 ; Duration of a full swing in milliseconds
  8. Local $steps = 1000 ; Number of steps per second / Precsion
  9. Local $pause_duration = 1000 ; Pause at the end of each half cycle in milliseconds
  10. Local $movement_duration = 350 ; Duration of movement for arrow keys in milliseconds. Key inputs are accumulated for that duration.
  11. Local $movement_multiplier = 100
  12.  
  13. Local $random_jitter = 0.2; Add some jitter to the movement. 0 <= $random_jitter < 1
  14.  
  15. ; Define your in-game sensitivity, DPI, and the desired rotation angle
  16. Local $sensitivity = 1.52
  17. Local $dpi = 3200
  18. Local $yawMultiplier = 0.022
  19. Local $zoomFactor = 1.0 ; Assuming no zoom
  20. Local $sensitivityScale = 1.0 ; Assuming default scaling
  21.  
  22. Local $Pi = 3.14159265358979
  23.  
  24. Func CalculateMouseMovement($angle, $sensitivity, $dpi, $yawMultiplier, $zoomFactor, $sensitivityScale)
  25.     ; Calculate modulated sensitivity
  26.     Local $modulatedSensitivity = $sensitivity * $sensitivityScale
  27.  
  28.     ; Calculate the required mouse movement (in pixels) to achieve the desired yaw angle
  29.     Local $pixels = $angle / ($yawMultiplier * $modulatedSensitivity * $zoomFactor)
  30.  
  31.     Return $pixels
  32. EndFunc
  33.  
  34. Local $swing_distance = CalculateMouseMovement($swing_Angle, $sensitivity, $dpi, $yawMultiplier, $zoomFactor, $sensitivityScale) ; Maximum swing distance in pixels
  35.  
  36. ; Calculate swing speed in pixels per second
  37. Local $swing_speed = $swing_distance / ($swing_period / 1000) ; speed in pixels per second
  38.  
  39. ; Initialize the toggle status
  40. Local $toggle = False
  41. Local $Local_toggle = True ; Additional Local toggle status
  42. Local $start_time = TimerInit() ; Initialize timer
  43. Local $last_update_time = TimerInit() ; Last time update
  44. Local $last_swing_direction = 0 ; Last swing direction: -1 = left, 1 = right
  45. Local $last_swing_sign = 0
  46. Local $last_movement_direction[2] = [0, 0] ; Last movement direction (x, y)
  47. Local $movements[0][3] ; Empty array for movements
  48. Local $swing_position = 1 ; Current swing position in pixels
  49. Local $isoutside = False
  50.  
  51. Local $delta_time ; Declare delta_time Locally
  52. Local $accumulated_x = 0.0
  53. Local $accumulated_y = 0.0
  54.  
  55. Local $Pi = 3.14159
  56. Local $Pi2 = 3.14159/2
  57.  
  58. Local $hDLL = DllOpen("user32.dll")
  59.  
  60. ; F7 key as toggle for swinging
  61. HotKeySet("{F7}", "ToggleSwing")
  62.  
  63. ; End key as toggle for all movements
  64. HotKeySet("{END}", "ToggleLocal")
  65.  
  66. ; Delete key to terminate the script
  67. HotKeySet("{DEL}", "TerminateScript")
  68.  
  69. Func TerminateScript()
  70.     Exit ; Terminate the script
  71. EndFunc
  72.  
  73. Func ToggleSwing()
  74.     $toggle = Not $toggle ; Toggle between True/False
  75.     If $toggle Then
  76.         $start_time = TimerInit() ; Reset timer
  77.         $last_swing_direction = 0 ; Reset direction status
  78.         $last_update_time = TimerInit() ; Reset time for delta-time
  79.     EndIf
  80. EndFunc
  81.  
  82. Func ToggleLocal()
  83.     $Local_toggle = Not $Local_toggle ; Toggle everything on/off
  84.     If Not $Local_toggle Then
  85.         ; Stop all movements
  86.         $toggle = False
  87.         ReDim $movements[1][3]
  88.         $movements[0][0] = 0
  89.         $movements[0][1] = 0
  90.         $movements[0][2] = 0
  91.     EndIf
  92. EndFunc
  93.  
  94. Local $VK_UP = "26"   ; Virtual key code for up arrow key
  95. Local $VK_DOWN = "28" ; Virtual key code for down arrow key
  96. Local $VK_LEFT = "25" ; Virtual key code for left arrow key
  97. Local $VK_RIGHT = "27" ; Virtual key code for right arrow key
  98.  
  99. Local $move_up = False
  100. Local $move_down = False
  101. Local $move_left = False
  102. Local $move_right = False
  103.  
  104. ;HotKeySet to use those keys exclusively:
  105. HotKeySet("{UP}", "CheckKeys")
  106. HotKeySet("{DOWN}", "CheckKeys")
  107. HotKeySet("{LEFT}", "CheckKeys")
  108. HotKeySet("{RIGHT}", "CheckKeys")
  109.  
  110. HotKeySet("{UP up}", "CheckKeys")
  111. HotKeySet("{DOWN up}", "CheckKeys")
  112. HotKeySet("{LEFT up}", "CheckKeys")
  113. HotKeySet("{RIGHT up}", "CheckKeys")
  114.  
  115. Func CheckKeys()
  116.     ; Check the status of the arrow keys
  117.     If _isPressed($VK_UP, $hDLL) Then
  118.         $move_up = True
  119.     Else
  120.         $move_up = False
  121.     EndIf
  122.    
  123.     If _isPressed($VK_DOWN, $hDLL) Then
  124.         $move_down = True
  125.     Else
  126.         $move_down = False
  127.     EndIf
  128.    
  129.     If _isPressed($VK_LEFT, $hDLL) Then
  130.         $move_left = True
  131.     Else
  132.         $move_left = False
  133.     EndIf
  134.    
  135.     If _isPressed($VK_RIGHT, $hDLL) Then
  136.         $move_right = True
  137.     Else
  138.         $move_right = False
  139.     EndIf
  140. EndFunc
  141.  
  142. Func AddMovement($x, $y)
  143.     If $Local_toggle Then
  144.         ; Calculate the end time for the new movement
  145.         Local $end_time = TimerDiff($start_time) + $movement_duration
  146.        
  147.         ; Store the current size of the array
  148.         Local $oldSize = UBound($movements, 1)
  149.        
  150.         ; Create a new array that is one row larger
  151.         ReDim $movements[$oldSize + 1][3]
  152.        
  153.         ; Add the new movement
  154.         $movements[$oldSize][0] = $x
  155.         $movements[$oldSize][1] = $y
  156.         $movements[$oldSize][2] = $end_time
  157.  
  158.     EndIf
  159. EndFunc
  160.  
  161. Func CalculateMovement(ByRef $x_movement, ByRef $y_movement)
  162.     ; Reset the movements
  163.     $x_movement = 0
  164.     $y_movement = 0
  165.  
  166.     ; Check the status of the arrow keys and continuously add movements
  167.     If $move_up Then AddMovement(0, -1)
  168.     If $move_down Then AddMovement(0, 1)
  169.     If $move_left Then AddMovement(-1, 0)
  170.     If $move_right Then AddMovement(1, 0)
  171.  
  172.     ; Iterate through the movements in the array
  173.     Local $current_time = TimerDiff($start_time)
  174.     For $i = 1 To UBound($movements) - 1
  175.         If $current_time < $movements[$i][2] Then
  176.             $x_movement += $movements[$i][0]
  177.             $y_movement += $movements[$i][1]
  178.         EndIf
  179.     Next
  180.  
  181. EndFunc
  182.  
  183. Func DllMoveMouse($dx, $dy)
  184.     ; Get the current mouse position
  185.     Local $aPos = MouseGetPos()
  186.  
  187.     ; Calculate the new position
  188.     Local $new_x = $aPos[0] + $dx
  189.     Local $new_y = $aPos[1] + $dy
  190.  
  191.     ; Set the cursor to the new position
  192.     DllCall("user32.dll", "int", "mouse_event", "int", 0x0001, "int", $dx, "int", $dy, "int", 0, "int", 0) ;to remain function
  193.     DllCall("user32.dll", "int", "SetCursorPos", "int", $new_x, "int", $new_y)  ;to keep some precision
  194. EndFunc
  195.  
  196. Local $pause_start_time = 0
  197. Local $is_paused = False
  198. Local $current_pause_duration = 0
  199.  
  200. Func CheckPause()
  201.     ; Check if we are currently in a pause
  202.     If $is_paused Then
  203.         ; Calculate the elapsed time since the start of the pause
  204.         Local $elapsed_time = TimerDiff($pause_start_time)
  205.  
  206.         ; End the pause if the pause duration is over
  207.         If $elapsed_time >= $current_pause_duration Then
  208.             $is_paused = False
  209.         EndIf
  210.     EndIf
  211. EndFunc
  212.  
  213. Func StartPause($duration)
  214.     ; Start a new pause with the given duration
  215.     $pause_start_time = TimerInit()
  216.     $current_pause_duration = $duration
  217.     $is_paused = True
  218. EndFunc
  219.  
  220. ; Function to move the mouse only when movement is significant
  221. Func ProcessMovement($dx, $dy)
  222.     $accumulated_x += $dx
  223.     $accumulated_y += $dy
  224.  
  225.     Local $move_x = Floor($accumulated_x)
  226.     Local $move_y = Floor($accumulated_y)
  227.  
  228.     If Abs($move_x) >= 1 Or Abs($move_y) >= 1 Then
  229.         DllMoveMouse($move_x, $move_y)
  230.         $accumulated_x -= $move_x
  231.         $accumulated_y -= $move_y
  232.     EndIf
  233. EndFunc
  234.  
  235. Local $direction = 1
  236.  
  237. ; Initialize timer outside the loop
  238. Local $end = TimerInit()
  239.  
  240. ; Main loop
  241. While True
  242.     If TimerDiff($end) >= (1000 / $steps) Then
  243.         ; Reset the timer
  244.         $end = TimerInit()
  245.        
  246.         If $Local_toggle Then
  247.             ; Calculate delta_time in milliseconds
  248.             $delta_time = TimerDiff($last_update_time)
  249.             $last_update_time = TimerInit()
  250.             Local $current_time = TimerDiff($start_time)
  251.            
  252.             ; Check if a pause is active
  253.             CheckPause()
  254.            
  255.             CheckKeys() ; Check the key status
  256.            
  257.             ; Independent arrow key movement
  258.             Local $current_x_movement, $current_y_movement
  259.             CalculateMovement($current_x_movement, $current_y_movement)
  260.  
  261.             ; Move the mouse according to arrow key movement
  262.             If $current_x_movement <> 0 Or $current_y_movement <> 0 Then
  263.                 $current_x_movement += Random(-$random_jitter, $random_jitter, 0)
  264.                 $current_y_movement += Random(-$random_jitter, $random_jitter, 0)
  265.                
  266.                 $current_x_movement *= ($movement_multiplier*$swing_speed/$steps) * $delta_time / 1000 *$movement_duration / 1000; Adjust speed with delta_time
  267.                 $current_y_movement *= ($movement_multiplier*$swing_speed/$steps) * $delta_time / 1000 *$movement_duration / 1000
  268.                
  269.                 ProcessMovement($current_x_movement, $current_y_movement)
  270.                
  271.                 ; Add current movement to the swing position if swinging is active
  272.                 If $toggle Then
  273.                     ; Add phase change to position
  274.                     $swing_position += $current_x_movement
  275.                     if Not $isoutside And Abs($swing_position) > $swing_distance/2 then
  276.                         $isoutside = True
  277.                     ElseIf $isoutside and $last_swing_sign <> Sign($swing_position) then
  278.                         $isoutside = False
  279.                     EndIf
  280.                     $last_swing_sign = Sign($swing_position)
  281.                 EndIf
  282.             EndIf
  283.  
  284.             ; Reset movements
  285.             If Abs($current_x_movement) >= $random_jitter Then
  286.                 ; Remove expired movements
  287.                
  288.                 Local $new_movements[0][3] ; Start with an empty array for new movements
  289.                 Local $j = 0
  290.                
  291.                 For $i = 0 To UBound($movements) - 1
  292.                     If $current_time < $movements[$i][2] Then
  293.                         ReDim $new_movements[$j + 1][3]
  294.                         $new_movements[$j][0] = $movements[$i][0]
  295.                         $new_movements[$j][1] = $movements[$i][1]
  296.                         $new_movements[$j][2] = $movements[$i][2]
  297.                         $j += 1
  298.                     EndIf
  299.                 Next
  300.                 $movements = $new_movements
  301.  
  302.             Else
  303.                
  304.                 If Abs($current_y_movement) >= $random_jitter Then
  305.                     ; Remove expired movements
  306.                    
  307.                     Local $new_movements[0][3] ; Start with an empty array for new movements
  308.                     Local $j = 0
  309.                    
  310.                     For $i = 0 To UBound($movements) - 1
  311.                         If $current_time < $movements[$i][2] Then
  312.                             ReDim $new_movements[$j + 1][3]
  313.                             $new_movements[$j][0] = $movements[$i][0]
  314.                             $new_movements[$j][1] = $movements[$i][1]
  315.                             $new_movements[$j][2] = $movements[$i][2]
  316.                             $j += 1
  317.                         EndIf
  318.                     Next
  319.                     $movements = $new_movements
  320.                 EndIf
  321.                
  322.                 ; Swing movement
  323.                 If $toggle And Not $is_paused Then
  324.                     Local $move_x = 0
  325.                     If Not $isoutside Then
  326.                         $move_x = ($swing_speed + Random(-$random_jitter, $random_jitter, 0)) * $direction * $delta_time / 1000 * (1-Clamp((Abs($swing_position)/($swing_distance/(2*0.98)))^3,0,0.98))
  327.                     Else
  328.                         $move_x = ($swing_speed + Random(-$random_jitter, $random_jitter, 0)) * $direction * $delta_time / 1000
  329.                     EndIf
  330.                    
  331.                     ; Ensure that the position moves even if Sin would make the change too small
  332.                     $swing_position += $move_x
  333.  
  334.                     ; Constrain the swing position within the maximum swing distance
  335.                     If $swing_position > $swing_distance / 2 And $direction > 0 Then
  336.                         $direction = -1
  337.                        
  338.                     ElseIf $swing_position < -$swing_distance / 2 And $direction < 0 Then
  339.                         $direction = 1
  340.                     EndIf
  341.                    
  342.                     If $isoutside and $last_swing_sign <> Sign($swing_position) then
  343.                         $isoutside = False
  344.                     EndIf
  345.                    
  346.                     $last_swing_sign = Sign($swing_position)
  347.                    
  348.                     If $direction <> $last_swing_direction And $last_swing_direction <> 0 Then
  349.                          StartPause($pause_duration) ; Start a pause with the specified duration
  350.                     EndIf
  351.                     $last_swing_direction = $direction
  352.  
  353.                     ; Move the mouse relative to the current position
  354.                     ProcessMovement($move_x, Random(-0.2, 0.2, 0))
  355.                 EndIf
  356.             EndIf
  357.         EndIf
  358.     EndIf
  359. WEnd
  360.  
  361. ; Remember to close the DLL when it's no longer needed
  362. Func Cleanup()
  363.     DllClose($hDLL)
  364. EndFunc
  365. Cleanup() ; Close the DLL before exiting the script
  366.  
  367. Func Sign($value)
  368.     If $value > 0 Then
  369.         Return 1
  370.     ElseIf $value < 0 Then
  371.         Return -1
  372.     Else
  373.         Return 0
  374.     EndIf
  375. EndFunc
  376.  
  377. Func Clamp($value, $min, $max)
  378.     If $value < $min Then
  379.         Return $min
  380.     ElseIf $value > $max Then
  381.         Return $max
  382.     Else
  383.         Return $value
  384.     EndIf
  385. EndFunc
  386.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement