How can I make this script more compact and optimized?

Ask gaming related questions (AHK v1.1 and older)
Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

How can I make this script more compact and optimized?

Post by Maimed » 02 Dec 2022, 04:56

Because I am old and tired most of the time, I use this script to autoclick in games. I adore it. I'm just curious if there's any way to make the repeating code here more compact.

Code: Select all

#HotkeyInterval 99000000
#KeyHistory 0
#MaxHotkeysPerInterval 99000000
#NoEnv
#Persistent
#SingleInstance force
DetectHiddenWindows, On
ListLines, Off
SendMode, Input
SetBatchLines, 10ms
SetKeyDelay, 0, 50
SetWinDelay, 0
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Menu|                                                                         ;
;---------------------------------------------------------------------------------------------------------------------------------------;
Menu, Tray, NoStandard
Menu, Tray, Add, Exit
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Mouse down P|                                                                 ;
;---------------------------------------------------------------------------------------------------------------------------------------;
p0:
Return
p1:
DllCall("mouse_event", uint, 1, int, 0, int, 1, uint, 0, int, 0) ;
Return
p2:
DllCall("mouse_event", uint, 1, int, 0, int, 2, uint, 0, int, 0) ;
Return
p3:
DllCall("mouse_event", uint, 1, int, 0, int, 3, uint, 0, int, 0) ;
Return
p4:
DllCall("mouse_event", uint, 1, int, 0, int, 4, uint, 0, int, 0) ;
Return
p5:
DllCall("mouse_event", uint, 1, int, 0, int, 5, uint, 0, int, 0) ;
Return
p6:
DllCall("mouse_event", uint, 1, int, 0, int, 6, uint, 0, int, 0) ;
Return
p7:
DllCall("mouse_event", uint, 1, int, 0, int, 7, uint, 0, int, 0) ;
Return
p8:
DllCall("mouse_event", uint, 1, int, 0, int, 8, uint, 0, int, 0) ;
Return
p9:
DllCall("mouse_event", uint, 1, int, 0, int, 9, uint, 0, int, 0) ;
Return
p10:
DllCall("mouse_event", uint, 1, int, 0, int, 10, uint, 0, int, 0) ;
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Battlefield 2|                                                                ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe BF2.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Send, {LButton Down}
		Sleep, 35
		Send, {LButton Up}
		Sleep, 35
		Random, MouseDown, 0, 1
		gosub p%MouseDown%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space Down}
		Sleep, 205
		Send, {Space Up}
		Sleep, 205
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |S.T.A.L.K.E.R.: Shadow of Chernobyl|                                          ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe XR_3DA.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Click, Left
		Sleep, 50
		Random, MouseDown, 0, 1
		gosub p%MouseDown%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space Down}
		Sleep, 205
		Send, {Space Up}
		Sleep, 205
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Alien Swarm: Reactive Drop|                                                   ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe reactivedrop.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Click, Left
		Sleep, 20
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space Down}
		Sleep, 35
		Send, {Space Up}
		Sleep, 35
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Counter-Strike: Global Offensive|                                             ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe csgo.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Click, Left
		Sleep, 20
		Random, MouseDown, 0, 1
		gosub p%MouseDown%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space Down}
		Sleep, 35
		Send, {Space Up}
		Sleep, 35
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |RimWorld|                                                                     ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe RimWorldWin64.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Send, {LButton Down}
		Sleep, 35
		Send, {LButton Up}
		Sleep, 35
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$RButton::
Loop
{
	GetKeyState, State, RButton, P
	if (state = "D")
	{
		Send, {RButton Down}
		Sleep, 35
		Send, {RButton Up}
		Sleep, 35
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Alien Shooter|                                                                ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe AlienShooter.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Send, {LButton Down}
		Sleep, 20
		Send, {LButton Up}
		Sleep, 40
		Send, {RButton Down}
		Sleep, 20
		Send, {RButton Up}
		Sleep, 40
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Brutal Doom|                                                                  ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe gzdoom.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Send, {LButton Down}
		Sleep, 40
		Send, {LButton Up}
		Sleep, 40
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space Down}
		Sleep, 35
		Send, {Space Up}
		Sleep, 35
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Counter-Strike: Source|                                                       ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe hl2.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Click, Left
		Random, shoot, 20, 32
		Sleep, %shoot%
		Random, MouseDown, 0, 1
		gosub p%MouseDown%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*~$MButton::
Send, t
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad2}
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad4}
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad5}
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad3}
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad3}
Sleep, 35
Send, b
Sleep, 35
Send, {Numpad6}
Sleep, 35
Send, {Numpad7}
Sleep, 35
Send, {Numpad0}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$f::
Send, {VK34SC005}
Sleep, 15
Send, {LButton}{LButton}{LButton}
Sleep, 555
Send, {LButton}{LButton}{LButton}{LButton}{LButton}
Sleep, 15
Send, {LButton Up}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space}
		Sleep, 20
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Counter-Strike 1.6|                                                           ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe hl.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Click, Left
		Random, shoot, 20, 32
		Sleep, %shoot%
		Random, MouseDown, 0, 1
		gosub p%MouseDown%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*~$MButton::
Send, t
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad6}
Sleep, 45
Send, b
Sleep, 36
Send, {Numpad7}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad2}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad1}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad3}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad4}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad5}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad6}
Sleep, 36
Send, b
Sleep, 36
Send, {Numpad8}
Sleep, 36
Send, {Numpad7}
Sleep, 36
Send, {Numpad0}
Sleep, 36
Send, {Numpad0}
Sleep, 36
Send, {Numpad0}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$f::
Send, {VK34SC005}
Sleep, 15
Send, {LButton}{LButton}{LButton}
Sleep, 555
Send, {LButton}{LButton}{LButton}{LButton}{LButton}
Sleep, 15
Send, {LButton Up}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space}
		Sleep, 20
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Call of Duty 4: Modern Warfare|                                               ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe iw3mp.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		Send, {LButton Down}
		Sleep, 25
		Send, {LButton Up}
		Random, shoot, 50, 90
		Sleep, %shoot%
		Random, MouseDown, 3, 7
		gosub p%MouseDown%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space Down}
		Sleep, 25
		Send, {Space Up}
		Sleep, 25
	}
	else if (state = "U")
	{
		break
	}
}
Return
*~$v::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {v Down}
		Sleep, 25
		Send, {v Up}
		Sleep, 25
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$b::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {v Down}
		Sleep, 25
		Send, {v Up}
		Sleep, 25
	}
	else if (state = "U")
	{
		break
	}
}
Return
;---------------------------------------------------------------------------------------------------------------------------------------;
;                                                        |Left 4 Dead 2|                                                                ;
;---------------------------------------------------------------------------------------------------------------------------------------;
#IfWinActive, ahk_exe left4dead2.exe
*$LButton::
Loop
{
	GetKeyState, State, LButton, P
	if (state = "D")
	{
		MouseClick, left
		Sleep, 20
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$c::
Loop
{
	GetKeyState, State, c, P
	if (state = "D")
	{
		Random, Places, 5, 500
		Random, Places2, 10, 1000
		MouseMove %Places%, %Places2%
	}
	else if (state = "U")
	{
		break
	}
}
Return
*$Space::
Loop
{
	GetKeyState, State, Space, P
	if (state = "D")
	{
		Send, {Space}
		Sleep, 40
	}
	else if (state = "U")
	{
		break
	}
}
Return
*~$F5::
Send, {F4}
Sleep, 150
SendRaw, say !sm_execcfg mapconfig/maps/.cfg
Return
#IfWinActive
;------------------------------------------------------------------;
;                           |Exit|                                 ;
;------------------------------------------------------------------;
Exit:
ExitApp

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: How can I make this script more compact and optimized?

Post by mikeyww » 02 Dec 2022, 06:54

Welcome to this AutoHotkey forum!

I'm not sure of the point, but instead of Gosub with your MouseDown, you could just do the DllCall.

Code: Select all

DllCall("mouse_event", "UInt", 1, "Int", 0, "Int", MouseDown, "UInt", 0, "Int", 0)
If you never call p9, etc., you could delete any subroutines that are never called.

I usually use While GetKeyState(.... instead of Loop => Break, but it's mainly just because that's how I think about the key events. I'm thinking to myself, "While this key is down, do....", and so the AHK code from that is a direct translation. It does save a few lines, for whatever that is worth.

Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

Re: How can I make this script more compact and optimized?

Post by Maimed » 02 Dec 2022, 07:45

@mikeyww Thank you for responding!
Please excuse my poor English.
What I wanted to do was control the recoil in some weapons in those specific games by making the weapons shoot down over time, now if I do the dll call only say

Code: Select all

DllCall("mouse_event", "UInt", 1, "Int", 0, "Int", 1, "UInt", 0, "Int", 0)
Without random and gosub, the mouse will move at a fixed speed, in this case extremely slowly, and most of the time it will not control the recoil at all, but if I use

Code: Select all

Random, MouseDown, 1, 5
gosub p%MouseDown%
The mouse down speed will range from 1 to 5, and I have a very good chance of controlling the recoil and hitting targets. It's still up to chance, but it's better than having a fixed speed.

But, aside from that, I'm guessing my script is already sufficiently optimized? ;p

Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

Re: How can I make this script more compact and optimized?

Post by Maimed » 02 Dec 2022, 08:04

I usually use While GetKeyState(.... instead of Loop => Break, but it's mainly just because that's how I think about the key events. I'm thinking to myself, "While this key is down, do....", and so the AHK code from that is a direct translation. It does save a few lines, for whatever that is worth.
I see what you mean, and I believe I experimented with 'While GetKeyState' and 'GetKeyState, State.. with the loop break' at some point and discovered that I prefer the loop because it better suits my needs, but thank you anyway!

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: How can I make this script more compact and optimized?

Post by mikeyww » 02 Dec 2022, 08:15

The DLL call that I posted uses your random number directly. If your current script is better for you, stick with it!

Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

Re: How can I make this script more compact and optimized?

Post by Maimed » 02 Dec 2022, 12:09

The DLL call that I posted uses your random number directly.
My mistake, I missed the mousedown in your code and assumed it was just another dll call like the ones I did in the script. I really hadn't considered this one before, thanks @mikeyww!

Post Reply

Return to “Gaming Help (v1)”