Page 1 of 1

Advanced Heli Throttle Control Script

Posted: 2016-03-27 15:47
by Manasong
Whats this about

If you use KB+Mouse to pilot helis, sometimes you will be needing more precise controls over throttle, this script was made to give more options for controling the throttle in a way similar to those with joysticks plus some special functions.

What do you need

VJoy driver installed
Autohotkey installed
Autohotkey VJoy library

The script

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#SingleInstance, force
#include vjoy_lib.ahk

;Initialising variables
vjoy_id := 1
vjoy_init(vjoy_id)
AxisValue = 16400 ;Max value is 32767
;This value controls the velocity that the axis gets updated
SleepSpeed = 20
;This value controls the increments that the joystick axis increases or reduces each iteration
AxisIncrement = 100

;Checking for at least one axis enabled in VJoy
if (!VJoy_GetAxisExist_X(vjoy_id)){
	msgbox Please enable the X axis of vjoy ID %vjoy_id% using the vJoy config utility.
}

~[ACCELERATE_KEY]::
;Loops so acceleration is linear, otherwise it depends on charater repeat delay of the keyboard
Loop
{
	;While key is down update the VJoy axis according to the conditions
    	GetKeyState, state, [ACCELERATE_KEY]
	If state= D
	{
		;If the axis value is below half automatically puts it on half
		If AxisValue<16400)
			AxisValue=16400

		;If the axis value is below maximum increment it
		If AxisValue<32767)
			AxisValue += AxisIncrement

		;If the axis value is on maximum or above it set it to maximum
		Else
			AxisValue=32767

		;Update the VJoy axis value
		VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
		Sleep, SleepSpeed
	}
	;Else break from loop
	Else
	Break
}	
Return

~[DECELERATE_KEY]::
Loop
{
    	GetKeyState, state,[DECELERATE_KEY]
	If state= D
	{
		;The axis does not have values less than 0
		if (AxisValue<0)
		{
			AxisValue=0
			VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
			Sleep, SleepSpeed
			Return
		}
		else
		{	
			;If the value is less than half...
			if (global AxisValue<=16400)
			{
				Loop
				{
					;... and the deaccelerate key is being pressed, set it to 0
					GetKeyState, state2, [DECELERATE_KEY]
					If state2=D
					{
						AxisValue=0
						VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
						Sleep, SleepSpeed
					}
					;... or set it to half
					else
					{
						AxisValue=16400
						VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
						Sleep, SleepSpeed
						break
					}
				}
			}
			;Otherwise decrement from the axis
			else
			{
				AxisValue -= AxisIncrement
				VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
				Sleep, SleepSpeed
			}
		}
	}
	Else
	Break
}	
Return

;Instantly set the axis to maximum
~[MAXIMUM_KEY]::
AxisValue=32767
VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
return

;Instantly set it to a value slightly below half, for a "soft" landing
~[LANDING_KEY]::
AxisValue=16100
VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
Return

;Instantly set it to a value slightly above half, near most helis hover value
~[HOVER KEY]::
AxisValue=21100
VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
return

;Instantly set it to 0
~[ZERO_THROTTLE_KEY]::
AxisValue=0
VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
return

;Press this button before mapping the accelerate button, this makes the axis increase over some time so PR can detect the axis
~F10::
AxisValue=16400
Sleep, 2000
Loop, 200
{
AxisValue+=100
VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
Sleep, 40
}
Return

;Toggle to suspend all hotkeys but does not close the program
~F11::
Suspend
Return

;Closes the program
~F12::
ExitApp
Return

;Very small and slow increment, for when you wanna try to reach perfect hover
~[INCREMENT_KEY]::
Loop
{
    	GetKeyState, state, SC04A
	If state= D
	{
		AxisValue+=5
		VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
		sleep, 100
	}
	else
		Return
}

;Very small decrement
~[DECREMENT_KEY]::
Loop
{
    	GetKeyState, state, SC04E
	If state= D
	{
		AxisValue-=5
		VJoy_SetAxis(AxisValue, vjoy_id, HID_USAGE_X)
		Sleep, 100
	}
	else
		Return
}
How it works

The VJoy program emulates a joystick, we will only use the X axis of it to control the throttle. This axis can assume a value of 0 to 32767, while it's "resting point" is on the middle, with a value of 16383,5.

If you are using a keyboard, pressing the accelerate button puts the axis at maximum value, presseing the decelerate button puts it on 0 and pressing nothing puts it at half. This is very imprecise because all choppers have a value above half that makes it hover, anything more anything more and it increases altitute, anything less and it starts to lose altitude.

The accelerate key sets it to half if its less than half or increases slowly if its more than half. If you release the key the throttle does not change, unlike the keyboard, so you can have a constant throttle without having to keep pressing a key in a certain frequency to try to maintain a throttle less than 100%. Specially useful when you are trans squad lead and you need to speak to other squads while flying.

The decelerate key decreases throttle slowly if its more than half or sets it to half if its less than half. If you keep it pressed while at half value or less it will set it to 0, this way acceleration and deceleration are equal to vanilla functionality with the added persistent throttle value.

The maximum key sets it to max instantly, the same way as if you were pressing the accelerate key on vanilla.

The landing key sets it to a value slightly lower than half, this way the chopper can land safely without falling like a brick and damaging itself if you dont have a high horizontal speed. Good for situations where you can land safely withouth much hurry or for vertical landings in difficult areas, like small clearings in florests. With some practice you can also use it for quick deceleration and quick landing if you are close to the ground.

The hover key sets it to a value close to the hover value of the helis, its also good for slow deceleration when approaching the LZ.

The zero throttle key is useful for cows, they have a tendency to slide forward unless the throttle is at zero.

The F10 key is necessary for mapping the controls because Autohotkey "steals" keystrokes and changes the output to your macro, but the BF2 engine does not allow this "stealing". To solve that all hotkeys need a "~" in front, with that autohotkey does not "steal" the keystroke and instead sends both the key pressed and the macro output. But whenever you press the accelerate key during mapping PR will recognize the key and not the axis, so to map the axis to throttle you need to press F10 AND THEN click on the accelerate mapping box and wait for PR to recognize the axis.

F11 suspends all hotkeys and F12 exist the autohotkey script.

The increment/decrement key is just a small for fun thing I did so I could try to find the perfect hover.

How use it

After installing Autohotkey right click where you want the script, go to NEW>Autohotkey Script, then right click the new script and go to Edit Script, copy and paste this code to the script.

Replace all [X_KEY] (Including the []) with the key you want, see this page and this page to understand how to write the hotkeys you want so Autohotkey can understand.

Then put the VJoy_lib.ahk script at the same folder of the script you just created and double click the new script to start it. You can also right click the icon in the icon tray to suspend, exit or edit the script.

You can use the vJoy Monitor if you installed it to see the Axis value changing without having to enter PR.

Re: Advanced Heli Throttle Control Script

Posted: 2016-03-27 16:55
by ZektorSK
Nice job ! :)

Re: Advanced Heli Throttle Control Script

Posted: 2016-03-27 17:07
by BlackGus
Image

To muchhhh work...i do not need this...5min after

Image