Source

FreeSpace 2 / FreeSpace 2.ahk

Full commit
chrisgalpin c3f6062 

















chrisgalpin 6e17586 





chrisgalpin c3f6062 








chrisgalpin 6e17586 






chrisgalpin c3f6062 

chrisgalpin 6e17586 



























































































































































































































































/*
Copyright 2010 Christopher S. Galpin http://codeoptimism.net

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
; Contact me for special licensing.

#SingleInstance force

PitchSpeed := 2.2
MinPitchSpeed := 0.6
InvertYAxis := false

Bind1 = q       ; equalize shields
LeftBind1 = e   ; next escort
RightBind1 = f  ; nearest friendly
DownBind1 = h   ; nearest hostile
UpBind1 = b     ; nearest bomb/bomber
Bind2 = r       ; attacker
Bind3 = g       ; target's attacker
Bind4 = y       ; reticle
DownBind4 = v   ; reticle subsystem
Bind5 = Tab
Bind6 = LCtrl
Bind7 = 
Bind8 = Space
Bind9 = Backspace
Bind10 = \
Bind11 = ~c36
Bind12 = m      ; auto-match speed
Bind13 = x      ; counter-measures

UpBind6 = ,
DownBind6 = .
UpBind8 = /
DownBind8 = /
LeftBind8 = ?
RightBind8 = ?

BindDecel = z
BindAccel = a

BindSlower := "-"
BindFaster := "="

BindBankL = Numpad7
BindBankR = Numpad9

JoyThreshold := 0
JoystickPrefix = 1Joy

; END OF CONFIG SECTION -- Don't change anything below this point unless you want
; to alter the basic nature of the script.

Hotkey, %JoystickPrefix%1, Hotkey1
Hotkey, %JoystickPrefix%2, Hotkey2
Hotkey, %JoystickPrefix%3, Hotkey3
Hotkey, %JoystickPrefix%4, Hotkey4
Hotkey, %JoystickPrefix%5, Hotkey5
Hotkey, %JoystickPrefix%6, Hotkey6
Hotkey, %JoystickPrefix%7, Hotkey7
Hotkey, %JoystickPrefix%8, Hotkey8
Hotkey, %JoystickPrefix%9, Hotkey9
Hotkey, %JoystickPrefix%10, Hotkey10
Hotkey, %JoystickPrefix%11, Hotkey11
Hotkey, %JoystickPrefix%12, Hotkey12
Hotkey, %JoystickPrefix%13, Hotkey13

; Calculate the axis displacements that are needed to start moving the cursor:
if InvertYAxis
	YAxisMultiplier = -1
else
	YAxisMultiplier = 1

SetTimer, WatchJoystick, 10  ; Monitor the movement of the joystick.

return  ; End of auto-execute section.


; The subroutines below do not use KeyWait because that would sometimes trap the
; WatchJoystick quasi-thread beneath the wait-for-button-up thread, which would
; effectively prevent mouse-dragging with the joystick.

Hotkey1:
PushButton(1)
return
Hotkey2:
PushButton(2)
return
Hotkey3:
PushButton(3)
return
Hotkey4:
PushButton(4)
return
Hotkey5:
PushButton(5)
return
Hotkey6:
PushButton(6)
return
Hotkey7:
PushButton(7)
return
Hotkey8:
PushButton(8)
return
Hotkey9:
PushButton(9)
return
Hotkey10:
PushButton(10)
return
Hotkey11:
PushButton(11)
return
Hotkey12:
PushButton(12)
return
Hotkey13:
PushButton(13)
return

WaitFor1ButtonUp:
ReleaseButton(1)
return
WaitFor2ButtonUp:
ReleaseButton(2)
return
WaitFor3ButtonUp:
ReleaseButton(3)
return
WaitFor4ButtonUp:
ReleaseButton(4)
return
WaitFor5ButtonUp:
ReleaseButton(5)
return
WaitFor6ButtonUp:
ReleaseButton(6)
return
WaitFor7ButtonUp:
ReleaseButton(7)
return
WaitFor8ButtonUp:
ReleaseButton(8)
return
WaitFor9ButtonUp:
ReleaseButton(9)
return
WaitFor10ButtonUp:
ReleaseButton(10)
return
WaitFor11ButtonUp:
ReleaseButton(11)
return
WaitFor12ButtonUp:
ReleaseButton(12)
return
WaitFor13ButtonUp:
ReleaseButton(13)
return

WatchJoystick:

PitchShip := false
DeltaX := GetDelta("X")
DeltaY := GetDelta("Y")
DeltaZ := GetDelta("Z")
DeltaR := GetDelta("R")

SetFormat, float, 0.3
if (Abs(DeltaR) > Abs(DeltaZ))
	pitchDelta := Abs(DeltaR)
else
	pitchDelta := Abs(DeltaZ)

if (GetKeyState(JoystickPrefix . "7")) {
	if (DeltaZ = 0)
		PitchMul := 500
	else
		PitchMul := MinPitchSpeed
}
else
	PitchMul := pitchDelta / 50 * PitchSpeed + MinPitchSpeed

if (PitchShip) {
	SetMouseDelay, -1  ; Makes movement smoother.
	MouseMove, DeltaX * PitchMul, DeltaY * PitchMul * YAxisMultiplier, 0, R
}

;AxisKey("MoveShip", DeltaZ, BindFaster, BindSlower)
AxisKey("MoveShip", DeltaZ, BindAccel, BindDecel)
;AxisKey("BankShip", DeltaR, BindBankL, BindBankR)

return

Pov() {
	global
	GetKeyState, pov, %JoystickPrefix%POV
	if (pov = -1)
		return ""
	else if (pov > 4500 && pov < 13500)
		return "Right"
	else if (pov >= 13500 && pov <= 22500)
		return "Down"
	else if (pov > 22500 && pov < 31500)
		return "Left"
	else
		return "Up"
}

AxisKey(action, delta, leftKey, rightKey) {
	global
	if (%action%) {
		active := GetKeyState(JoystickPrefix . "7")
		if (delta < 0 && active)
			Send {%leftKey% Down}
		else if (delta > 0 && active)
			Send {%rightKey% Down}
		else {
			Send {%leftKey% Up}
			Send {%rightKey% Up}
			%action% := false
		}
	}
}

PushButton(num) {
	global

	KeyList(num, "Down")
	SetTimer, WaitFor%num%ButtonUp, 10
}

ReleaseButton(num) {
	global
	if GetKeyState(JoystickPrefix . num)
		return  ; The button is still, down, so keep waiting.
	; Otherwise, the button has been released.
	SetTimer, WaitFor%num%ButtonUp, off

	KeyList(num, "Up")
}

KeyList(num, state) {
	global

	;if (GetKeyState(JoystickPrefix . "5"))
	;	Key(5Bind%num%)
	;else {
		bind := Pov() . "Bind" . num
		Key(%bind%, state)
	;}
}

Key(key, state) {
	if (!key)
		return

	if (InStr(key, "~")) {
		if (state = "Down") {
			sequence := SubStr(key, 1)
			SendRaw % sequence
		}
	}
	else
		Send, {%key% %state%}
}

GetDelta(axis) {
	global

	SetFormat, float, 03
	GetKeyState, joy, %JoystickPrefix%%axis%
	delta := joy - 50
	if (Abs(delta) > JoyThreshold) {
		if (axis = "x" || axis = "y")
			PitchShip := true
		else if (axis = "z" && GetKeyState(JoystickPrefix . "7"))
			MoveShip := true
	}
	return delta
}