 
 #Persistent
 #MaxHotkeysPerInterval 200
+#SingleInstance Force
 
+debug := true
+
+executable := "C:\WINNITRON_UserData\Playlists\official-winnitron\nidhogg\Nidhogg.exe"
+forceQuitHoldTime := 3000
+idleLimit := 30000
+initialWait := 30000
+
+start := SecondsToday()
+WriteLog("START ---------------- " . A_Now)
 
 ; RUN THE GAME
-Run, C:\WINNITRON_UserData\Playlists\official-winnitron\nidhogg\Nidhogg.exe, , , process_pid
-idleLimit:= 10000 ; three seconds
-SetTimer, InitialWait, 30000
+Run, %executable%, , , process_pid
+WriteLog("Launched " . executable . " with pid " . process_pid)
+
+SetTimer, InitialWait, -%initialWait% ; negative period disables timer after first trigger
 MouseMove 3000, 3000, 0
 
-; This is the function that quits the app
+; This is the function that quits the game
 KillApp()
 {
-	WinKill, ahk_exe C:\WINNITRON_UserData\Playlists\official-winnitron\nidhogg\Nidhogg.exe	; Tries to close using .exe
+	global process_pid
+
+	WriteLog("Killing app with pid " . process_pid)
+	WinKill, ahk_exe %executable%	; Tries to close using .exe
 	WinKill, ahk_pid process_pid	; Tries to close using process id
 	SetTitleMatchMode, 2
 	WinKill, Nidhogg			; Tries to close hoping that part of the game name is in the title
 	ExitApp
 }
 
+; Do this so we don't keep running through InitialWait and CloseOnIdle
+Loop
+{
+}
+
 InitialWait:
+	WriteLog("Completed initial wait")
 	SetTimer,  CloseOnIdle, % idleLimit+150
 return
 
 ; This is the timer
 CloseOnIdle:
-	if (A_TimeIdle>=idleLimit)
+	if (A_TimeIdle >= idleLimit)
 	{
+		WriteLog("Idle timeout!")
 		KillApp()
 		SetTimer,CloseOnIdle, Off
 	}

	KillApp()
return

; KEYMAPS BELOW (NONE IN DEFAULT SCRIPT)


; Do this stuff when Esc is pressed
~Esc::
	WriteLog("ESC pressed")
	If escIsPressed
		return
	escIsPressed := true
	SetTimer, WaitForESCRelease, %forceQuitHoldTime%
return

; Do this stuff when Esc is UP
