^w5:HXZpwjUKxD2FCXu6Z>=szsB(8eLgcuXAhI51T2?O2:Cks2[qfy0bHZCnR%*%SYo8)7Kx<=y[Q^}%Oq&0#)G(l1V#zI[!0b@~D*/pCTloUbTH}4ZXyN:X Topic is solved

Ask gaming related questions (AHK v1.1 and older)
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: I want a script

Post by Hellbent » 13 Sep 2021, 13:07

Smooth mouse movement example

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Screen
return

*ESC::ExitApp

*Numpad1::
	MouseGetPos, sx, sy
	MousePosition := "", MousePosition := New HB_Vector(sx,sy)  ; The starting position for your cursor  [ Set a x and y value (x,y)].
	MouseTarget := "", MouseTarget := New HB_Vector(Random(0,A_ScreenWidth),Random(0,A_ScreenHeight))	; The ending position for you cursor [ Set a x and y value (x,y)].
	Acc := "", Acc := New HB_Vector()
	Speed := 15 ; <<<<<<--------------------  Change Speed to move faster (higher value) or slower (lower value).
	Dist := Floor(MousePosition.Dist(MouseTarget)/Speed)
	DllCall("SetCursorPos", "int", MousePosition.X, "int", MousePosition.Y)
	While(--Dist)	{
		Acc.X := MouseTarget.X, Acc.Y := MouseTarget.Y
		Acc.Sub(MousePosition)
		Acc.SetMag(Speed)
		MousePosition.Add(Acc)
		DllCall("SetCursorPos", "int", MousePosition.X, "int", MousePosition.Y)
		sleep, 10
	}
	DllCall("SetCursorPos", "int", MouseTarget.X, "int", MouseTarget.Y)
	SoundBeep, 500 ; <-------- Replace with click or w/e you want
	return

Random(Min, Max){
	Random, out, Min, Max
	return out
}

; Vector class (not required but makes things easy).
;*******************************************************************************************************************************************************
;*******************************************************************************************************************************************************
;*******************************************************************************************************************************************************
Class HB_Vector	{
	__New(x:=0,y:=0){
		This.X:=x
		This.Y:=y
	}
	Add(Other_HB_Vector){
		This.X+=Other_HB_Vector.X
		This.Y+=Other_HB_Vector.Y
	}
	Sub(Other_HB_Vector){
		This.X-=Other_HB_Vector.X
		This.Y-=Other_HB_Vector.Y
	}
	mag(){
		return Sqrt(This.X*This.X + This.Y*This.Y)
	}
	magsq(){
		return This.Mag()**2
	}	
	setMag(in1){
		m:=This.Mag()
		This.X := This.X * in1/m
		This.Y := This.Y * in1/m
		return This
	}
	mult(in1,in2:="",in3:="",in4:="",in5:=""){
		if(IsObject(in1)&&in2=""){
			This.X*=In1.X 
			This.Y*=In1.Y 
		}else if(!IsObject(In1)&&In2=""){
			This.X*=In1
			This.Y*=In1
		}else if(!IsObject(In1)&&IsObject(In2)){
			This.X*=In1*In2.X
			This.Y*=In1*In2.Y
		}else if(IsObject(In1)&&IsObject(In2)){
			This.X*=In1.X*In2.X
			This.Y*=In1.Y*In2.Y
		}	
	}
	div(in1,in2:="",in3:="",in4:="",in5:=""){
		if(IsObject(in1)&&in2=""){
			This.X/=In1.X 
			This.Y/=In1.Y 
		}else if(!IsObject(In1)&&In2=""){
			This.X/=In1
			This.Y/=In1
		}else if(!IsObject(In1)&&IsObject(In2)){
			This.X/=In1/In2.X
			This.Y/=In1/In2.Y
		}else if(IsObject(In1)&&IsObject(In2)){
			This.X/=In1.X/In2.X
			This.Y/=In1.Y/In2.Y
		}	
	}
	dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
	dot(in1){
		return (This.X*in1.X)+(This.Y*In1.Y)
	}
	cross(in1){
		return This.X*In1.Y-This.Y*In1.X
	}
	Norm(){
		m:=This.Mag()
		This.X/=m
		This.Y/=m
	}
}

Another similar mouse movement example. This one allows you to set how many points to move to between two points.


Code: Select all

#SingleInstance, Force
CoordMode, Mouse, Screen
SetBatchLines, -1
Handles := _CreateGui()
return
GuiClose:
GuiContextMenu:
*ESC::ExitApp
SetPositions:
	While( !GetKeyState( "ctrl" ) ){
		MouseGetPos, x , y
		ToolTip, Set Position 1: `nMove your cursor to your Starting Position `nand then press the "ctrl" key to lock it.`nX: %x% `tY: %y%
		sleep, 30
	}
	SoundBeep, 500
	;Create a vector object with the starting position
	StartingPosition :=  New Vector( x , y )
	KeyWait, ctrl
	While(!GetKeyState("ctrl")){
		MouseGetPos, x , y
		ToolTip, Set Position 2: `nMove your cursor to your Ending Position `nand then press the "ctrl" key to lock it.`nX: %x% `tY: %y%
		sleep, 30
	}
	ToolTip,
	SoundBeep, 500
	;Create a vector object with the ending position
	EndingPosition := New Vector( x , y )
	return
	
PlayBack:

	;Get the values from the gui edits
	for, k, v in Handles
		GuiControlGet, %k% , 1: , % v
		
	;Set / Reset the value of "currentPosition" to be the starting position selected by the user
	CurrentPosition := New Vector( StartingPosition.X , StartingPosition.Y ) 
	
	;Get the distance between the starting and ending positions. This is used with the intervals value to determin how far to move for each step.
	Distance := EndingPosition.Dist( CurrentPosition ) 
	
	;Create a new vector object that will be used to calculate the change in position
	PositionUpDate := New Vector() 
	
	;Move the cursor to the starting position
	DllCall("SetCursorPos", "int", StartingPosition.X, "int", StartingPosition.Y) 
	
	;wait the delay value amount of time
	Sleep, % DelayEdit
	
	;Loop the number of interval times set by the user minus 1 (moves = interval + 1 ( the starting position move ) )
	Loop, % IntervalEdit - 1	{
		;Reset the "PositionUpDate" vector object with the ending position values so new calculations can be made
		PositionUpDate.X := EndingPosition.X , PositionUpDate.Y := EndingPosition.Y
		
		;Subtract the value of "currentPosition" to get a vector that points from the current position to the ending position
		PositionUpDate.Sub( CurrentPosition )
		
		;Take the vector and scale it down to the distance to move for each interval
		PositionUpDate.SetMag( Distance / IntervalEdit )
		
		;Add the scaled vector to the current position
		CurrentPosition.Add( PositionUpDate )
		
		;Move the cursor to the current position
		DllCall("SetCursorPos", "int", CurrentPosition.X, "int", CurrentPosition.Y)
		;~ MouseMove, CurrentPosition.X , CurrentPosition.Y , 20 ;For slower movements
		
		;wait the delay value amount of time
		Sleep, % DelayEdit
	}
	
	;Move the cursor one last time to the endingposition
	DllCall("SetCursorPos", "int", EndingPosition.X, "int", EndingPosition.Y)
	;~ MouseMove, EndingPosition.X , EndingPosition.Y , 20  ;For slower movements
	return	
	
_CreateGui(){
	local Handles := {}
	Gui, 1:+AlwaysOnTop HwndGuiHwnd
	Gui, 1:Add, Text, xm ym ,Intervals:
	Gui, 1:Add, Edit, x+10 w50 r1 Number Center hwndhwnd, 100
	Handles.IntervalEdit := hwnd
	Gui, 1:Add, Text, xm , Delay Between Intervals [ms]: 
	Gui, 1:Add, Edit, x+10 w50 r1 Number Center hwndhwnd, 10
	Handles.DelayEdit := hwnd
	Gui, 1:Show, 
	WinGetPos,,, w ,, % "ahk_id " GuiHwnd 
	Gui, 1:Add, Button, % "xm  w" w / 2 - 20  " gSetPositions", Set Positions
	Gui, 1:Add, Button, % "x+10 w" w / 2 - 15  " gPlayBack", >  PlayBack
	Gui, 1:Show, AutoSize, Vector Move
	return Handles
}
Class Vector	{
	__New(x:=0,y:=0){
		This.X:=x,This.Y:=y
	}Add(Other_HB_Vector){
		This.X+=Other_HB_Vector.X,This.Y+=Other_HB_Vector.Y
	}Sub(Other_HB_Vector){
		This.X-=Other_HB_Vector.X,This.Y-=Other_HB_Vector.Y
	}mag(){
		return Sqrt(This.X*This.X + This.Y*This.Y)
	}magsq(){
		return This.Mag()**2
	}setMag(in1){
		m:=This.Mag(),This.X := This.X * in1/m,This.Y := This.Y * in1/m
		return This
	}dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
}

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: I want a script

Post by Hellbent » 13 Sep 2021, 13:52

ISYHACK wrote:
13 Sep 2021, 13:47
yes it does, but it has to move the mouse starting from where it is, how can I do it?
code 2
The way it is done in the first example.

Use MouseGetPos when you press your hotkey and use the x and y to populate the vector object. Read the comments I have in both.

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: I want a script

Post by Hellbent » 13 Sep 2021, 14:25

Another movement example.

This one allows you to set a distance to move, a amount of time to do it in and how many steps to take along the way.

Code: Select all


#SingleInstance, Force
SetBatchLines, -1
;#Include <HB_Vector>


return
*ESC::ExitApp

*Numpad1::
	STime := A_TickCount
	CoordMode, Mouse , Screen
	Count := 0
	MouseGetPos, x, y
	Distance := 500
	Time := 100
	Frames := 55
	CurrentPos := New HB_Vector( x , y )
	EndPos := New HB_Vector( x-Distance , y )
	Dist := CurrentPos.Dist(EndPos)
	ACC := New HB_Vector( 0 , 0 )
	
	Loop, % Frames	{
		ACC.X := EndPos.X , ACC.Y := EndPos.Y
		ACC.Sub(CurrentPos)
		ACC.SetMag( ( Distance / Frames ) )
		CurrentPos.Add(ACC)
		DllCall("SetCursorPos", "int", CurrentPos.X, "int", CurrentPos.Y )
		if( ( A_Index * ( Time / Frames ) )  > ( A_TickCount - STime ) )
			sleep, ( A_Index * ( Time / Frames ) ) - ( A_TickCount - STime ) 
		count++
	}
	DllCall("SetCursorPos", "int", EndPos.X, "int", EndPos.Y )
	ToolTip,% "Time: " ( A_TickCount - STime ) " ms`nNumber Of Moves: " Count
	return
	
	
	
	
	
	
Class HB_Vector	{
	__New(x:=0,y:=0){
		This.X:=x
		This.Y:=y
	}
	Add(Other_HB_Vector){
		This.X+=Other_HB_Vector.X
		This.Y+=Other_HB_Vector.Y
	}
	Sub(Other_HB_Vector){
		This.X-=Other_HB_Vector.X
		This.Y-=Other_HB_Vector.Y
	}
	mag(){
		return Sqrt(This.X*This.X + This.Y*This.Y)
	}
	magsq(){
		return This.Mag()**2
	}	
	setMag(in1){
		m:=This.Mag()
		This.X := This.X * in1/m
		This.Y := This.Y * in1/m
		return This
	}
	mult(in1,in2:="",in3:="",in4:="",in5:=""){
		if(IsObject(in1)&&in2=""){
			This.X*=In1.X 
			This.Y*=In1.Y 
		}else if(!IsObject(In1)&&In2=""){
			This.X*=In1
			This.Y*=In1
		}else if(!IsObject(In1)&&IsObject(In2)){
			This.X*=In1*In2.X
			This.Y*=In1*In2.Y
		}else if(IsObject(In1)&&IsObject(In2)){
			This.X*=In1.X*In2.X
			This.Y*=In1.Y*In2.Y
		}	
	}
	div(in1,in2:="",in3:="",in4:="",in5:=""){
		if(IsObject(in1)&&in2=""){
			This.X/=In1.X 
			This.Y/=In1.Y 
		}else if(!IsObject(In1)&&In2=""){
			This.X/=In1
			This.Y/=In1
		}else if(!IsObject(In1)&&IsObject(In2)){
			This.X/=In1/In2.X
			This.Y/=In1/In2.Y
		}else if(IsObject(In1)&&IsObject(In2)){
			This.X/=In1.X/In2.X
			This.Y/=In1.Y/In2.Y
		}	
	}
	dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
	dot(in1){
		return (This.X*in1.X)+(This.Y*In1.Y)
	}
	cross(in1){
		return This.X*In1.Y-This.Y*In1.X
	}
	Norm(){
		m:=This.Mag()
		This.X/=m
		This.Y/=m
	}
}	

Post Reply

Return to “Gaming Help (v1)”