'=======================================
'      		 High Speed 
'           Williams 1986
'        VPM Conversion by PD
'=======================================
'
' Load VPinMAME and *.VBS file, check versions
'
Option Explicit
LoadVPM "01120100", "S11.VBS", 3.10

'=================
'Yield Time Amount
'=================
'If Yield Time is enabled in the F6 GUI menu, this is the amount it will use. 
'A value of 1 or 2 works well for slower computers.  Faster computers (1.6GHz + ) 
'usually work better with it turned off (or a value of 0 here)

Const YieldAmount = 1

'===========================
'LightControl Timer Interval
'===========================
'
'- Controls both how fast the lights are updated and how long between each stage of fading (default = 45)

LightControl.Interval = 45

'==========
'Game Name
'==========

Const RomSet = "hs_l4"

'=========
' Sounds
'=========
' Standard
Const SSolenoidOn   = "Solenoid"
Const SSolenoidOff  = ""
Const SFlipperOn    = "Flipper"
Const SFlipperOff   = "FlipperDwn"
Const Scoin		    = "Quarter1"

'Table Specific Sounds
Const SfxOuthole    = "Outhole"    ' Ball hits outhole
Const SfxTroughRel  = "Release"    ' Ball kicked into plunger lane
Const sfxTroughKick = "Kick"       ' Ball kicked from outhole into trough
Const SfxSling      = "Sling"      ' Slingshot
Const SfxKnocker    = "Knock"      ' Knocker
Const SfxSaucer     = "Saucer"     ' ball kicked out of saucer
Const SfxKick       = "kick"       ' ball kicked out of kicker
Const SfxJet		= "JetSnd"	   ' Jet Bumper Sound
Const SfxPlunger    = "PlungerHit" ' Auto Plunger Hitting Ball Sound
Const SfxMPlunger   = "Plunger"    ' Manual Plunger Sound
Const SfxBallDrop   = "BallDrop"   ' Ball Hittin Playfield

'==============================================
' Check requirements and load VPM, VPVBS files
'==============================================
Sub LoadVPM(VPMver, VBSfile, VBSver)
	On Error Resume Next
	If ScriptEngineMajorVersion < 5 Then MsgBox "VB Script Engine 5.0 or higher required" : Exit Sub
	ExecuteGlobal GetTextFile(VBSfile)
	If Err Then MsgBox "Unable to open " & VBSfile & ". Ensure that it is in the same folder as this table. " & vbNewLine & Err.Description : Exit Sub
	Set Controller = CreateObject("VPinMAME.Controller")
	If Err Then	MsgBox "Unable to load VPinMAME." & vbNewLine & Err.Description : Exit Sub
	If VPMver > "" Then
		If Controller.Version < VPMver Or Err Then MsgBox "This table requires VPinMAME ver " & VPMver & " or higher." : Exit Sub
	End If
	If VPinMAMEDriverVer < VBSver Or Err Then MsgBox "This table requires " & VBSFile & " ver " & VBSver & " or higher." : Exit Sub
End Sub

'===================
' Initialize table
'===================
Const UseSolenoids = True, UseLamps = False
Dim bsTrough, bsEjectHole, bsAutoPlunger, bsKickback, vlBallLockL, vlBallLockR, BallImage

Sub HighSpeed_Init()
	vpmInit Me : Randomize 
'	SaveValue cRegistryName, "Options", 0 : Exit Sub  ' This will clear your Registry settings if uncommented
	HighSpeedOptions = CInt("0" & LoadValue(cRegistryName, "Options")) : HighSpeedSetOptions
	With Controller
		If No_Yield = False Then : If Controller.Version >= "01200000" Then HighSpeed.YieldTime = YieldAmount : End If
		.GameName = RomSet
		.SplashInfoLine = "High Speed (Williams 1986)" & vbNewLine &_
                          "VPM Table And Script By PD" 
		.ShowTitle = False : .ShowFrame = False : .ShowDMDOnly = True
        .Games(RomSet).Settings.Value("samples")=0 ' Make sure samples are disabled in Vpinmame
		On Error Resume Next 
			.Run : If Err Then MsgBox Err.Description
		On Error Goto 0
	End With

	' Tilt
	vpmNudge.TiltSwitch  = swBobTilt
	vpmNudge.Sensitivity = 5

	' Trough
	Set bsTrough = New cvpmBallStack : With bsTrough
		.InitSw swOuthole,swRtrough, swCTrough,swLTrough,0,0,0,0
		.InitKick BallRelease,180,10
		.InitAddSnd SfxOuthole : .InitExitSnd SfxTroughRel,SSolenoidOn
		.InitEntrySnd SfxTroughKick, SSolenoidOn
		.IsTrough = True 
		.Balls = 3
		.CreateEvents "bsTrough", Outhole
	End With

	' Eject Hole Saucer
	Set bsEjectHole = New cvpmBallStack : With bsEjectHole
		.InitSaucer EjectHole, swEjectHole, 90, 5
		.InitExitSnd SfxSaucer, SSolenoidOn
		.CreateEvents "bsEjectHole", EjectHole
	End With

	' Initialize GI Lighting
	GILighting False
	
	' Misc. Initialisation
	Diverter1.IsDropped = True : Diverter2.IsDropped = True
	BallImage = HighSpeed.BallImage
	Switch33.State = 1
	Switch34.State = 1
	Switch35.State = 1
	If highspeed.backdropimage = "back_room" Then
		LeftSiren.setvalue 6
		RightSiren.setvalue 6 
	End If
	If highspeed.backdropimage = "back_glass" Then
		LeftSiren.setvalue 13
		RightSiren.setvalue 13
	End If
	If highspeed.backdropimage <> "back_room" and highspeed.backdropimage <> "back_glass" Then
		LeftSiren.setvalue 14
		RightSiren.setvalue 14
	End If
End Sub

'======================
'Options Menu Variables
'======================
Const cRegistryName = "HighSpeedOne"
Const cOptNoGI      = &H01
Const cOptNoPlayGI	= &H02
Const cOptNoYield   = &H04
Const cOptNoSiren   = &H08
Const cOptPac       = &H10
Const cOptFade		= &H20

'===================
' DIP + Options menu
'===================
Dim HighSpeedOptions
Private Sub HighSpeedShowDips
	ChangePDS = No_Pac
	If Not IsObject(vpmDips) Then ' First time
		Set vpmDips = New cvpmDips
		With vpmDips
			.AddForm 100, 330, "High Speed Game Settings"
			.AddFrame 0, 0, 150, "Country Dips", &Hf0,_
				Array("USA", &H00, "USA", &Hf0, "European", &Hd0,_
				      "Export", &Ha0, "Export Alt", &H80, "France", &Hb0,_
				      "France 1", &H10, "France 2", &H30, "France 3", &H90,_
			    	  "Germany", &H20, "Spain",     &He0, "UK", &Hc0)
			.AddFrameExtra 0,188,150,"Disable",0,_
				Array("GI Plastic Effects", cOptNoGI,"GI Playfield Effects",cOptNoPlayGI,_
		        	 "Police Light", cOptNoSiren, "Fading Light Effect",cOptFade, "YieldTime (requires reset)", cOptNoYield)
			.AddFrameExtra 0,280,150,"Enable",0,_
				Array("PD_CPU",cOptPac)
		End With
	End If
	HighSpeedOptions = vpmDips.ViewDipsExtra(HighSpeedOptions)
	DipMenuUsed = True
	HighSpeedSetOptions : SaveValue cRegistryName, "Options", HighSpeedOptions
End Sub
Set vpmShowDips = GetRef("HighSpeedShowDips")

'==========================
' Handle High Speed Options
'==========================
Dim No_GI : No_GI = True			'Defaults for 1st start
Dim No_PlayGI : No_PlayGI = True 				
Dim No_Yield : No_Yield = False				
Dim No_Siren : No_Siren = False
Dim No_Pac : No_Pac = True
Dim ChangePDS : ChangePDS = No_Pac
Dim DipMenuUsed : DipMenuUsed = False

Sub HighSpeedSetOptions
	If HighSpeedOptions And cOptNoGI     Then : No_GI       = True : Else No_GI       = False : End If
	If HighSpeedOptions And cOptNoPlayGI Then : No_PlayGI   = True : Else No_PLayGI   = False : End If
	If HighSpeedOptions And cOptNoSiren  Then : No_Siren    = True : Else No_Siren    = False : End If
	If HighSpeedOptions And cOptFade     Then : Fading      = False: Else Fading      = True  : End If
	If HighSpeedOptions And cOptNoYield  Then : No_Yield    = True : Else No_Yield    = False : End If

	If HighSpeedOptions And cOptPac Then 
		No_Pac = False
		If DipMenuUsed = True and No_Pac <> ChangePDS Then Playsound "PacStart"
	Else
		No_Pac = True 
		If DipMenuUsed = True and No_Pac <> ChangePDS Then Playsound "PacEnd"
	End If
End Sub


'===============
' Sound Handling
'===============
SolCallback(sKnocker)    = "vpmSolSound SfxKnocker,"
SolCallback(sRSling)     = "" ' Unreliable for some reason (sound handled in switch handling)
SolCallback(sLSling)     = "" ' ""
SolCallback(sRightJet) 	 = "" ' ""
SolCallback(sULJet)	 	 = "" ' "" 
SolCallback(sLLJet)	     = "" ' ""

'Jet Ring Animation
Sub R33Drop()     : R33.IsDropped = True  : vpmTimer.AddTimer 80, "R33Raise" : End Sub
Sub R34Drop()     : R34.IsDropped = True  : vpmTimer.AddTimer 80, "R34Raise" : End Sub
Sub R35Drop()     : R35.IsDropped = True  : vpmTimer.AddTimer 80, "R35Raise" : End Sub
Sub R33Raise(asw) : R33.IsDropped = False : End Sub
Sub R34Raise(asw) : R34.IsDropped = False : End Sub
Sub R35Raise(asw) : R35.IsDropped = False : End Sub

'============
' Solenoids
'============
SolCallback(sBallRel)       = "bstrough.SolOut"     ' Ball Release
SolCallback(sEjectHole)     = "bsEjectHole.SolOut"  ' Eject Hole
SolCallback(sLeftHide)		= "LeaveLeft"           ' Left Hideout Eject
SolCallback(sRightHide)		= "LeaveRight"          ' Right Hideout Eject
SolCallback(sOuthole)		= "bsTrough.SolIn"      ' Outhole
SolCallback(sRampGates)	    = "Divert"              ' Ramp Diverters
SolCallback(sKickBack)      = "KickIt"              ' Kickback

SolCallback(sLRFlipper)     = "SolFlip RightFlipper,UpperRightFlipper,"  ' Right Flippers
SolCallback(sLLFlipper)     = "SolFlip LeftFlipper,Nothing,"             ' Left Flipper

Dim BallOnOut : BallOnOut = False


Sub Divert(enabled) : Diverter1.IsDropped = not enabled  : Diverter2.IsDropped = not enabled : End Sub
Sub KickIt(enabled) : If enabled Then : PlaySound SSolenoidOn : If BallOnOut = True Then : Kickback.enabled = True : Else : Kickback.enabled = False : End If : End If : End Sub
Sub Kickback_Hit() : Playsound SfxPlunger : Kickback.kick -2,15+(rnd*8)  : Kickback.enabled = False : End Sub

Sub SolFlip(aFlip1, aFlip2, aEnabled)  ' Ball Through Flipper Fix
	Dim oldSpeed
	If aEnabled Then
		PlaySound SFlipperOn : aFlip1.RotateToEnd : If Not aFlip2 Is Nothing Then aFlip2.RotateToEnd
	Else
		PlaySound SFlipperOff
		oldSpeed = aFlip1.Speed : aFlip1.Speed = 0.137
		aFlip1.RotateToStart : aFlip1.Speed = oldSpeed
		If Not aFlip2 Is Nothing Then
			oldSpeed = aFlip2.Speed : aFlip2.Speed = 0.137
			aFlip2.RotateToStart : aFlip2.Speed = oldSpeed
		End If
	End If
End Sub

'============
'Handle Locks
'============

Sub LeaveLeft(enabled)
	If enabled then 
		If Controller.switch (swLoLeftHide) = True Then LKickUp.enabled = True
		LLockTop.enabled = False : LLockMid.enabled = False
		LLockTop.Kick 0,16 ' Kick Balls
	    LLockMid.Kick 0,16 
		LLockBot.Kick 0,16 
		Playsound SfxPlunger
		Controller.switch (swLoLeftHide) = 0
	End If
End Sub

Sub LeaveRight(enabled)
	If enabled then 
		If Controller.switch (swLoRightHide) = True Then RKickUp.enabled = True
		RLockTop.enabled = False : RLockMid.enabled = False
		RLockTop.Kick 0,16 ' Kick Balls 
		RLockMid.Kick 0,16
		RLockBot.Kick 0,16
		Playsound SfxPlunger
		Controller.switch (swLoRightHide) = 0
	End If
End Sub

Sub LLockMid_Hit() : LLockTop.enabled = True : End Sub
Sub LLockBot_Hit() : LLockMid.enabled = True : End Sub
Sub RLockMid_Hit() : RLockTop.enabled = True : End Sub
Sub RLockBot_Hit() : RLockMid.enabled = True : End Sub

Sub LKickUp_Hit()
	If ActiveBall.Image = "el_bandito" Then : BallImage = "el_bandito" : Else : BallImage = HighSpeed.BallImage : End If
	LKickUp.DestroyBall
	LRampKick.CreateBall.Image = BallImage
	LRampKick.Kick 180,10
	If Controller.Switch(swLoLeftHide = 0) and Controller.Switch(swUpLeftHide) = 0 Then LKickUp.enabled = False
End Sub

Sub RKickUp_Hit()
	If ActiveBall.Image = "el_bandito" Then : BallImage = "el_bandito" : Else : BallImage = HighSpeed.BallImage : End If
	RKickUp.DestroyBall
	RRampKick.CreateBall.Image = BallImage
	RRampKick.Kick 180,10
	If Controller.Switch(swLoRightHide = 0) and Controller.Switch(swUpRightHide) = 0 Then RKickUp.enabled = False
End Sub



'=================
' Flasher Handling
'=================
SolCallback(sFlasher4)   = "Siren"                ' Beacon
SolCallback(sFlasher5)   = "setlight 100,"        ' Left Blue Playfield Flasher
SolCallback(sFlasher6)   = "setlight 101,"        ' Right Blue Playfield Flasher
SolCallback(sFlasher9)   = "setlight 102,"        ' Left Red Flashers
SolCallback(sFlasher10)  = ""                     ' Insert Board Flashers
SolCallback(sFlasher11)  = "GILighting"           ' General Illumination
SolCallback(sFlasher12)  = "setlight 103,"        ' Right Red Flashers
SolCallback(sFlasher22)  = ""                     ' Top Playfield Flashers

Sub Siren(enabled) ' Mars Lights (siren beacon on top of backbox)
	If enabled then : MarsOn = True : Else : MarsOn = False : End If 
	If enabled and No_Siren = False Then
	AnimateSiren.Enabled = True
	Else
	AnimateSiren.Enabled = False
	If highspeed.backdropimage = "back_room" Then
		LeftSiren.setvalue 6
		RightSiren.setvalue 6
	End If
	If highspeed.backdropimage = "back_glass" Then
		LeftSiren.setvalue 13
		RightSiren.setvalue 13
	End If
	Counter = 0
	End If
End Sub

Dim counter : counter = 0 
Dim LAnimNum, RAnimNum
Dim LAnimNum2, RAnimNum2

'Reel Number to display for 45 frames worth of animation to simulate rotation of siren beacon
RAnimNum = array(6,5,4,3,2,1,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6)
LAnimNum = array(6,6,6,6,6,6,6,6,6,6,6,6,6,5,4,3,2,1,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6)

RAnimNum2 = array(13,12,11,10,9,8,7,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13)
LAnimNum2 = array(13,13,13,13,13,13,13,13,13,13,13,13,13,12,11,10,9,8,7,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13)

Sub AnimateSiren_Timer()
If counter = 45 Then Counter = 0
If highspeed.backdropimage = "back_room" Then
	LeftSiren.setvalue LAnimNum(counter)
	RightSiren.setvalue RAnimNum(counter)		
End If
If highspeed.backdropimage = "back_glass" Then
	LeftSiren.setvalue LAnimNum2(counter)
	RightSiren.setvalue RAnimNum2(counter)		
End If
counter = counter + 1
End Sub

'===================
' Keyboard Handling
'====================
Sub HighSpeed_KeyUp(ByVal keycode)
	If vpmKeyUp(keycode) Then Exit Sub
	If keycode = PlungerKey Then Plunger.Fire : PlaySound SfxMPlunger
	If keycode = LeftFlipperKey Then Controller.Switch(swLeftLane) = 0
	If keycode = RightFlipperKey Then Controller.Switch(swRightLane) = 0
End Sub

Sub HighSpeed_KeyDown(ByVal keycode)
	If vpmKeyDown(keycode) Then Exit Sub
	If keycode = PlungerKey Then Plunger.Pullback
	If keycode = LeftFlipperKey Then Controller.Switch(swLeftLane) = 1
	If keycode = RightFlipperKey Then Controller.Switch(swRightLane) = 1
End Sub

'============
' GI Handling
'============
Sub GILighting(enabled)
If No_GI = False Then
	LeftSlingBulb.state = abs(Not enabled)
	RightSlingBulb.state = abs(Not enabled)
	LeftOutBulb.state = abs(Not enabled)
	RightOutBulb.state = abs(Not enabled)
	Switch33.State = abs(Not enabled)
	Switch34.State = abs(Not enabled)
	Switch35.State = abs(Not enabled)
	LS33.IsDropped = Not Enabled
	LS34.IsDropped = Not Enabled
	LS35.IsDropped = Not Enabled
	GIWall1.IsDropped = Not Enabled
	GIWall2.IsDropped = Not Enabled
	GIWall3.IsDropped = Not Enabled
	GIWall4.IsDropped = Not Enabled
	GIWall5.IsDropped = Not Enabled
	GIWall6.IsDropped = Enabled
	GIWall9.IsDropped = Enabled
	GIWall10.IsDropped = Enabled
	GIWall11.IsDropped = Not Enabled
	GIWall12.IsDropped = Not Enabled
	GIWall13.IsDropped = Enabled

Else
	LeftSlingBulb.state = 1
	RightSlingBulb.state = 1
	LeftOutBulb.state = 1
	RightOutBulb.state = 1
	Switch33.State = 1
	Switch34.State = 1
	Switch35.State = 1
	LS33.IsDropped = True
	LS34.IsDropped = True
	LS35.IsDropped = True
	GIWall1.IsDropped = True
	GIWall2.IsDropped = True
	GIWall3.IsDropped = True
	GIWall4.IsDropped = True
	GIWall5.IsDropped = True
	GIWall6.IsDropped = False
	GIWall9.IsDropped = False
	GIWall10.IsDropped = False
	GIWall11.IsDropped = True
	GIWall12.IsDropped = True
	GIWall13.IsDropped = False
End If
If No_PlayGI = False Then
	GIPlay3.IsDropped = Enabled
	GIPlay4.IsDropped = Enabled
	GIPlay5.IsDropped = Enabled
	GIPlay6.IsDropped = Enabled
	GIPlay7.IsDropped = Enabled
Else
	GIPlay3.IsDropped = False
	GIPlay4.IsDropped = False
	GIPlay5.IsDropped = False
	GIPlay6.IsDropped = False
	GIPlay7.IsDropped = False
End If
End Sub



'================
' Switch handling
'================
Sub Switch13_Hit()    	  : vpmtimer.pulsesw 13	 	       : End Sub 
Sub Switch14_Hit()    	  : vpmtimer.pulsesw 14	 	       : End Sub 
Sub Switch15_Hit()    	  : vpmtimer.pulsesw 15	 	       : End Sub 
Sub Switch17_Hit()    	  : vpmtimer.pulsesw 17	 	       : End Sub 
Sub Switch18_Hit()    	  : vpmtimer.pulsesw 18	 	       : End Sub 
Sub Switch19_Hit()    	  : vpmtimer.pulsesw 19	 	       : End Sub 
Sub Switch20_Hit()    	  : controller.switch (20) = true  : End Sub
Sub Switch20_unHit()  	  : controller.switch (20) = false : End Sub
Sub Switch21_Hit()    	  : controller.switch (21) = true  : End Sub
Sub Switch21_unHit()  	  : controller.switch (21) = false : End Sub
Sub Switch22_Hit()    	  : vpmtimer.pulsesw 22	 	       : End Sub 
Sub Switch23_Hit()    	  : vpmtimer.pulsesw 23	 	       : End Sub 
Sub Switch24_Hit()    	  : vpmtimer.pulsesw 24	 	       : End Sub 
Sub Switch25_Hit()    	  : vpmtimer.pulsesw 25	 	       : End Sub
Sub Switch26_Hit()    	  : vpmtimer.pulsesw 26	 	       : End Sub
Sub Switch27_Hit()    	  : vpmtimer.pulsesw 27	 	       : End Sub
Sub Switch28_Hit()    	  : vpmtimer.pulsesw 28	 	       : End Sub
Sub Switch29_Hit()    	  : vpmtimer.pulsesw 29	 	       : End Sub
Sub Switch30_Hit()    	  : vpmtimer.pulsesw 30	 	       : End Sub
Sub Switch31_Hit()    	  : controller.switch (31) = true  : BallOnOut = True  : End Sub
Sub Switch31_unHit() 	  : controller.switch (31) = false : BallOnOut = False : End Sub
Sub Switch32_Hit()    	  : controller.switch (32) = true  : End Sub
Sub Switch32_unHit()  	  : controller.switch (32) = false : End Sub
Sub Switch33_Hit()    	  : vpmtimer.pulsesw 33	: Playsound SfxJet : R33Drop : End Sub ' Upper Left Jet
Sub Switch34_Hit()    	  : vpmtimer.pulsesw 34	: Playsound SfxJet : R34Drop : End Sub ' Lower Left Jet
Sub Switch35_Hit()    	  : vpmtimer.pulsesw 35	: Playsound SfxJet : R35Drop : End Sub ' Right Jet
Sub Switch36_Hit()    	  : controller.switch (36) = true  : End Sub
Sub Switch36_unHit()  	  : controller.switch (36) = false : End Sub
Sub Switch39_Hit()    	  : controller.switch (39) = true  : End Sub
Sub Switch39_unHit()  	  : controller.switch (39) = false : End Sub
Sub Switch40_Hit()    	  : controller.switch (40) = true  : End Sub
Sub Switch40_unHit()  	  : controller.switch (40) = false : End Sub
Sub Switch42_Hit()    	  : controller.switch (42) = true  : End Sub
Sub Switch42_unHit()  	  : controller.switch (42) = false : End Sub
Sub Switch43_Hit()    	  : controller.switch (43) = true  : End Sub
Sub Switch43_unHit()  	  : controller.switch (43) = false : End Sub
Sub Switch44_Spin()       : vpmtimer.pulsesw 44            : End Sub
Sub Switch45_Spin() 	  : vpmtimer.pulsesw 45            : End Sub
Sub Switch46_Spin()  	  : vpmtimer.pulsesw 46            : End Sub
Sub Switch47_Hit()    	  : controller.switch (47) = true  : End Sub
Sub Switch47_unHit()  	  : controller.switch (47) = false : End Sub
Sub Switch48_Hit()    	  : controller.switch (48) = true  : End Sub
Sub Switch48_unHit()  	  : controller.switch (48) = false : End Sub
Sub Switch49_Slingshot()  : vpmtimer.pulsesw 49 : Playsound SfxSling : End Sub ' Left Slingshot
Sub Switch50_Slingshot()  : vpmtimer.pulsesw 50 : Playsound SfxSling : End Sub ' Right Slingshot
Sub Switch51_Hit()    	  : controller.switch (51) = true  : End Sub
Sub Switch51_unHit()  	  : controller.switch (51) = false : End Sub
Sub Switch52_Hit()    	  : controller.switch (52) = true  : End Sub
Sub Switch52_unHit()  	  : controller.switch (52) = false : End Sub

' In-between switches for circular targets (simulates ball hitting both targets at once)
Sub Switch13_14_Hit()	  : vpmtimer.pulsesw 13 : vpmtimer.pulsesw 14 : End Sub
Sub Switch14_15_Hit()	  : vpmtimer.pulsesw 14 : vpmtimer.pulsesw 15 : End Sub
Sub Switch17_18_Hit()	  : vpmtimer.pulsesw 17 : vpmtimer.pulsesw 18 : End Sub
Sub Switch18_19_Hit()	  : vpmtimer.pulsesw 18 : vpmtimer.pulsesw 19 : End Sub
Sub Switch22_23_Hit()	  : vpmtimer.pulsesw 22 : vpmtimer.pulsesw 23 : End Sub
Sub Switch23_24_Hit()	  : vpmtimer.pulsesw 23 : vpmtimer.pulsesw 24 : End Sub
Sub Switch25_26_Hit()	  : vpmtimer.pulsesw 25 : vpmtimer.pulsesw 26 : End Sub
Sub Switch26_27_Hit()	  : vpmtimer.pulsesw 26 : vpmtimer.pulsesw 27 : End Sub
Sub Switch28_29_Hit()	  : vpmtimer.pulsesw 28 : vpmtimer.pulsesw 29 : End Sub
Sub Switch29_30_Hit()	  : vpmtimer.pulsesw 29 : vpmtimer.pulsesw 30 : End Sub

'==========================================================
'Light Control (Featuring the PD V5.1 Fading Light System)
'==========================================================
Sub UpdateLights()
On Error Resume Next
'Lights
FadeL  1,light1a,light1b,light1,light1off 		'Game Over 					(BackGlass)
FadeL  2,light2a,light2b,light2,light2off		'Match 						(BackGlass)
FadeLm 3,light3a,light3b,light3,light3off		'Drive Again / Shoot Again	(BackGlass)
FadeL  3,light3_2a,light3_2b,light3_2,light3_2off
FadeL  4,light4a,light4b,light4,light4off		'Left  Outlane Special
FadeL  5,light5a,light5b,light5,light5off		'Right Outlane Special
FadeL  6,light6a,light6b,light6,light6off		'Ball In Play 				(BackGlass)
FadeL  7,light7a,light7b,light7,light7off		'Left  Spinner 1000 Arrow
FadeL  8,light8a,light8b,light8,light8off		'Right Spinner 1000 Arrow
FadeLm 9,light9a,light9b,light9,light9off		'20,000 Light Kickback
FadeL  9,light9_2a,light9_2b,light9_2,light9_2off
FadeL  10,light10a,light10b,light10,light10off	'Center Spinner 1000 Arrow
FadeL  11,light11a,light11b,light11,light11off	'ExtraBall (Eject Hole)
FadeL  12,light12a,light12b,light12,light12off	'Escape (Eject Hole)
FadeL  13,light13a,light13b,light13,light13off	'Red Light (Lower Left Target Bank)
FadeL  14,light14a,light14b,light14,light14off	'Yellow Light (Lower Left Target Bank)
FadeL  15,light15a,light15b,light15,light15off	'Green Light (Lower Left Target Bank)
FadeL  16,light16a,light16b,light16,light16off	'Kickback Arrow (Left Outlane)
FadeL  17,light17a,light17b,light17,light17off	'Red Light (Upper Left Target Bank)
FadeL  18,light18a,light18b,light18,light18off	'Yellow Light (Upper Left Target Bank)
FadeL  19,light19a,light19b,light19,light19off	'Green Light(Upper Left Target Bank)
FadeL  20,light20a,light20b,light20,light20off	'Left Freeway Arrow
FadeL  21,light21a,light21b,light21,light21off	'Right Freeway Arrow
FadeL  22,light22a,light22b,light22,light22off	'Red Light (Right Target Bank)
FadeL  23,light23a,light23b,light23,light23off	'Yellow Light (Right Target Bank)
FadeL  24,light24a,light24b,light24,light24off	'Green Light (Right Target Bank)
FadeL  25,light25a,light25b,light25,light25off	'Standup Target Arrow - 1
FadeL  26,light26a,light26b,light26,light26off	'Standup Target Arrow - 2
FadeL  27,light27a,light27b,light27,light27off	'Standup Target Arrow - 3
FadeL  28,light28a,light28b,light28,light28off	'Standup Target Arrow - 4
FadeL  29,light29a,light29b,light29,light29off	'Standup Target Arrow - 5
FadeL  30,light30a,light30b,light30,light30off	'Standup Target Arrow - 6
FadeL  31,light31a,light31b,light31,light31off	'Freeway Scores 25,000
FadeL  32,light32a,light32b,light32,light32off	'Freeway Scores 50,000
FadeL  33,light33a,light33b,light33,light33off	'Freeway Scores 75,000
FadeL  34,light34a,light34b,light34,light34off	'Freeway Scores 100,000
FadeL  35,light35a,light35b,light35,light35off	'Freeway Lights Extra Ball
FadeL  36,light36a,light36b,light36,light36off	'Ramp Earns Bonus X
FadeL  37,light37a,light37b,light37,light37off	'Ramp Earns Ramp Bonus
FadeL  38,light38a,light38b,light38,light38off	'Ramp Earns Getaway
FadeL  39,light39a,light39b,light39,light39off	'Ramp Earns Hideout
FadeL  40,light40a,light40b,light40,light40off	'Ramp Earns Hideout Jackpot
FadeL  41,light41a,light41b,light41,light41off	'StopLights Light Escape (Center)
FadeL  42,light42a,light42b,light42,light42off	'Red Light    (Ramp Stoplight)
FadeL  43,light43a,light43b,light43,light43off	'Yellow Light (Ramp Stoplight)
FadeL  44,light44a,light44b,light44,light44off	'Green Light  (Ramp Stoplight)
FadeL  45,light45a,light45b,light45,light45off	'Bonus 1000
FadeL  46,light46a,light46b,light46,light46off	'Bonus 2000
FadeL  47,light47a,light47b,light47,light47off	'Bonus 3000
FadeL  48,light48a,light48b,light48,light48off	'Bonus 4000
FadeL  49,light49a,light49b,light49,light49off	'Bonus 5000
FadeL  50,light50a,light50b,light50,light50off	'Bonus 6000
FadeL  51,light51a,light51b,light51,light51off	'Bonus 7000
FadeL  52,light52a,light52b,light52,light52off	'Bonus 8000
FadeL  53,light53a,light53b,light53,light53off	'Bonus 9000
FadeL  54,light54a,light54b,light54,light54off	'Bonus 10,000
FadeL  55,light55a,light55b,light55,light55off	'Bonus 20,000
FadeL  56,light56a,light56b,light56,light56off	'Bonus 30,000
FadeL  57,light57a,light57b,light57,light57off	'Bonus 40,000
FadeL  58,light58a,light58b,light58,light58off	'Bonus 50,000
FadeL  59,light59a,light59b,light59,light59off	'Bonus 60,000
FadeL  60,light60a,light60b,light60,light60off	'Bonus 5X
FadeL  61,light61a,light61b,light61,light61off	'Bonus 4X
FadeL  62,light62a,light62b,light62,light62off	'Hold Bonus
FadeL  63,light63a,light63b,light63,light63off	'Bonus 3X
FadeL  64,light64a,light64b,light64,light64off	'Bonus 2X
'Flashers
FadeL  100,Flash5a,Flash5b,Flash5,Flash5off		' Left  Blue Playfield Flasher
FadeL  101,Flash6a,Flash6b,Flash6,Flash6off		' Right Blue Playfield Flasher
FadeRm 102,flash9								' Left  Red Flashers
FadeRm 102,flash9b								'
FadeRm 102,flash9R								'
FadeR  102,flash9R2								'
FadeRm 103,flash12								' Right Red Flashers
FadeRm 103,flash12b								'
FadeRm 103,flash12R								'
FadeR  103,flash12R2							'
' Periodically check Kickback Light to make sure kickback kicker didn't get reenable after a kick
If lightnum(16) = 0 and BallOnOut = false Then kickback.enabled = false 
End Sub

'=====================================
'Game Extras and VP Physic Workarounds
'=====================================
'

Dim MarsOn : MarsOn = False

Sub Bandito_Hit() ' Watch out for Bandito Pac!
	If No_Pac = False and MarsOn = True Then : ActiveBall.Image = "El_Bandito" : End If
End Sub

Sub LDrop1_Hit() ' Acts as perfect ramp end so ball falls straight down into hideout
	If ActiveBall.Image = "el_bandito" Then : BallImage = "el_bandito" : Else : BallImage = HighSpeed.BallImage : End If
	LDrop1.DestroyBall : PlaySound SfxBallDrop
	LDrop2.CreateBall.Image = BallImage : LDrop2.Kick 0,0
End Sub

Sub RDrop1_Hit() ' Acts as perfect ramp end so ball falls straight down into hideout
	If ActiveBall.Image = "el_bandito" Then : BallImage = "el_bandito" : Else : BallImage = HighSpeed.BallImage : End If
	RDrop1.DestroyBall : PlaySound SfxBallDrop
	RDrop2.CreateBall.Image = BallImage : RDrop2.Kick 0,0
End Sub

Sub RampSound1_Hit() ' Plays Ball Drop Sound coming off ramp
	PlaySound SfxBallDrop
End Sub

Sub RampSound2_Hit() ' Plays Ball Drop Sound coming off ramp
	PlaySound SfxBallDrop
End Sub

Sub RampSound3_Hit() ' Plays Ball Drop Sound coming off ramp
	PlaySound SfxBallDrop
End Sub

'====================================================
' Symbolic Names for Solenoids, Flashers and Switches
'====================================================
'Solenoids
Const sOuthole		=	 1  'Outhole
Const sBallRel		=	 2  'Ball Release
Const sEjectHole	= 	 3  'Eject Hole (Saucer)
Const sLeftHide		=	 7  'Left Hideout Relay
Const sRightHide	=	 8  'Right Hideout Relay
Const sRampGates	=   13  'Ramp Gates
Const sKickback		=	14  'Left Outlane Kickback (w/relay)
Const sKnocker		=	15 	'Knocker
Const sCoinLockOut	=   16  'Coin-LockOut Relay
Const sLSling		=	17  'Left Kicker
Const sRSling		=	18	'Right Kicker

'Flashers
Const sFlasher4		=	 4  'Police Light Relay (Beacon)
Const sFlasher5		=	 5  'Left Blue Playfield Flashers
Const sFlasher6		=	 6  'Right Blue Playfield Flashers
Const sFlasher9 	=	 9  'Left Red Flashers
Const sFlasher10	=	10  'Insert Board Flashers
Const sFlasher11	=	11  'General Illumination
Const sFlasher12	=	12  'Right Red Flashers
Const sRightJet		=   19  'Right Jet Bumper
Const sLLJet		=   20  'Lower Left Jet Bumper
Const sULJet		=	21	'Upper Left Jet Bumper
Const sFlasher22    =	22  'Top Playfield Flashers

'Switches
Const swBobTilt		=  1 'Plumb Bob Tilt
Const swBRTilt		=  2 'Ball Roll Tilt
Const swCredit		=  3 'Credit Button
Const swCoinR		=  4 'Right Coin Chute
Const swCoinC		=  5 'Center Coin Chute
Const swCoinL		=  6 'Left Coin Chute
Const swSlam_Tilt	=  7 'Slam Tilt
Const swHighReset	=  8 'High Score Reset
Const swOuthole		=  9 'Outhole
Const swLTrough 	= 10 'Left Trough
Const swCTrough		= 11 'Center Trough
Const swRTrough 	= 12 'Right Trough
Const swRedLwrLeft  = 13 'Red Target (Lower Left Stoplight Bank)
Const swYelLwrLeft	= 14 'Yellow Target (Lower Left Stoplight Bank)
Const swGreLwrLeft	= 15 'Green Target (Lower Left Stoplight Bank)
Const swEjectHole	= 16 'Eject Hole (Saucer)
Const swRedUprLeft 	= 17 'Red Target (Upper Left Stoplight Bank)
Const swYelUprLeft	= 18 'Yellow Target (Upper Left Stoplight Bank)
Const swGreUprLeft	= 19 'Green Target (Upper Left Stoplight Bank)
Const swLinlane		= 20 'Left Flipper Return Lane
Const swRinlane		= 21 'Right Flipper Return Lane
Const swRedRt		= 22 'Red Target (Right Stoplight Bank)
Const swYelRt		= 23 'Yellow Target (Right Stoplight Bank)
Const swGreRt		= 24 'Green Target (Right Stoplight Bank) 
Const swone			= 25 'Standup Target Arrow - 1
Const swtwo			= 26 'Standup Target Arrow - 2
Const swthree		= 27 'Standup Target Arrow - 3
Const swfour		= 28 'Standup Target Arrow - 4
Const swfive		= 29 'Standup Target Arrow - 5
Const swsix			= 30 'Standup Target Arrow - 6
Const swLOutlane	= 31 'Left Outlane
Const swROutlane	= 32 'Right Outlane
Const swJetUL		= 33 'Upper Left Jet Bumper
Const swJetLL		= 34 'Lower Left Jet Bumper
Const swJetRT		= 35 'Right Jet Bumper
Const swBallShoot	= 36 'Ball Shooter
Const swLeftLane	= 37 'Left Flipper Lane Change (Engine Revs)
Const swRightLane	= 38 'Right Flipper Lane Change (Engine Revs)
Const swUpLeftHide	= 39 'Upper Left Hideout
Const swLoLeftHide	= 40 'Lower Left Hideout
Const swPlayTilt	= 41 'Playfield Tilt
Const swLeftRamp	= 42 'Left Ramp
Const swRightRamp	= 43 'Right Ramp
Const swLeftSpin	= 44 'Left Spinner
Const swCentSpin	= 45 'Center Spinner
Const swRightSpin	= 46 'Right Spinner
Const swUpRightHide = 47 'Upper Right Hideout
Const swLoRightHide = 48 'Lower Right Hideout
Const swLeftSling	= 49 'Left Kicker (scoring)
Const swRightSling  = 50 'Right Kicker (scoring)
Const swLeftStar	= 51 'Left Star Rollover
Const swRightStar 	= 52 'Right Star Rollover

'-------------------------------------
'Core Light Handling (PD 5.1 Appended)
'-------------------------------------

'System defines variables and assigns the changed light values to them, which is then turned over to Fader Control
Dim lightnum(151) : Dim Tset
'Initialize all possible light control variables (0 - 150) including extra spaces for flashers or GI control
On Error Resume Next
	For Tset = 0 To 150
		lightnum(Tset) = 0
	Next

'Assign All Lamp Functions to Variable Control List And Call Fading Lights
' - Variable "Fading" controls whether lights fade or not (can use from options menu or change in script)
Dim Fading    : Fading = True

'Note This Sub is for VPM tables and assigns lamp values automatically.  Originals should use the one marked
'     below instead.  Therefore only create the timer you need (either LightControl or LightControlO)
'
Sub LightControl_Timer()
	Dim ChgLamp, pointer, index, str, T
	  ChgLamp = Controller.ChangedLamps
	On Error Resume Next
	If Fading = True Then 
		For T = 0 To UBound(ChgLamp)
			pointer = chgLamp(T, 0)
			index   = chgLamp(T, 1)
			lightnum(pointer) = index + 4
		Next
	Else
		For T = 0 To UBound(ChgLamp)
			pointer = chgLamp(T, 0)
			index   = chgLamp(T, 1)
			lightnum(pointer) = index
		Next
	End If
    UpdateLights
End Sub

'Note: This timer is for original tables.  Light states are assigned using the "SetLight" sub marked below this one.
'      It is also used to assign flasher or GI lights to unused light numbers and thus added to the fade system.

 Sub LightControlO_Timer()
	UpdateLights
 End Sub

' Light Assignment Control
' - This call allows easy assignments of values to the light array, useful for assigning Flashers or GI Lights or when
'   using the system with original tables to assign lamp states into the control array.  Fading or non-fading calls 
'   are then applied as normal in the UpdateLights sub.
' - Also Set "On" = 1 and "Off" = 0 so they can be used as an alias in the call

dim LightOn, LightOff : LightOn = 1 : LightOff = 0

Sub SetLight(lightnumber, value)
	If fading = true then
		lightnum(lightnumber) = abs(value) + 4
	Else
		lightnum(lightnumber) = abs(value)
	End If
End Sub

'------------------------------------------------
'Light Handler Code With Fade Control V5.1 by PD
'------------------------------------------------

'Fades Light Object based Lights When Multiple Lights are controlled by the Same Variable
'NOTE: The last light controlled by that variable must be called with the regular Sub handler 
'Example call: "FadeLm 1, light1a,light1b,light1,light1off"
Sub FadeLm(lightnumIn, stage1, stage2, stage3, off)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color 
	Select Case lightnum(lightnumIn)
		Case  5 : stage1.state = 0 : stage2.state = 0 : stage3.state = 0 : stage3.state = 1
		Case  4 : stage3.state = 0 : stage2.state = 0 : stage2.state = 1 
		Case  3 : stage2.state = 0 : stage1.state = 0 : stage1.state = 1 
		Case  2 : stage1.state = 0 : off.state = 0 : off.state = 1 
		Case  1 : off.state    = 0 : stage3.state = 1 
		Case  0 : stage3.state = 0 : off.state = 1 
	End Select
End Sub

'Fades Light Object based Lights 
'Example call: "FadeL 1, light1a,light1b,light1,light1off"
Sub FadeL(lightnumIn, stage1, stage2, stage3, off)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color
	Select Case lightnum(lightnumIn)
		Case  5 : stage1.state = 0 : stage2.state = 0 : stage3.state = 0 : stage3.state = 1 : lightnum(lightnumIn) = 6
		Case  4 : stage3.state = 0 : stage2.state = 0 : stage2.state = 1 : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  3 : stage2.state = 0 : stage1.state = 0 : stage1.state = 1 : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  2 : stage1.state = 0 : off.state = 0 : off.state = 1 : lightnum(lightnumIn) = 6
		Case  1 : off.state    = 0 : stage3.state = 1 : lightnum(lightnumIn) = 6
		Case  0 : stage3.state = 0 : off.state = 1 : lightnum(lightnumIn) = 6
	End Select
End Sub

'Fades Wall Based Lights When Multiple Lights are controlled by the Same Variable
'NOTE: The last light controlled by that variable must be called with the regular Sub handler 
'Example call: "FadeWm 1, light1a,light1b,light1"
Sub FadeWm(lightnumIn, stage1, stage2, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color
	Select Case lightnum(lightnumIn)
		Case  5 : stage1.IsDropped = True : stage2.IsDropped = True : stage3.IsDropped = False 
		Case  4 : stage3.IsDropped = True : stage2.IsDropped = False 
		Case  3 : stage2.IsDropped = True : stage1.IsDropped = False 
		Case  2 : stage1.IsDropped = True
		Case  1 : stage1.IsDropped = True : stage2.IsDropped = True : stage3.IsDropped = False
		Case  0 : stage1.IsDropped = True : stage2.IsDropped = True : stage3.IsDropped = True
	End Select
End Sub

'Fades Wall Based Lights
'Example call: "FadeW 1, light1a,light1b,light1"
Sub FadeW(lightnumIn, stage1, stage2, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color
	Select Case lightnum(lightnumIn)
		Case  5 : stage1.IsDropped = True : stage2.IsDropped = True : stage3.IsDropped = False : lightnum(lightnumIn) = 6
		Case  4 : stage3.IsDropped = True : stage2.IsDropped = False : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  3 : stage2.IsDropped = True : stage1.IsDropped = False : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  2 : stage1.IsDropped = True : lightnum(lightnumIn) = 6
		Case  1 : stage1.IsDropped = True : stage2.IsDropped = True : stage3.IsDropped = False : lightnum(lightnumIn) = 6
		Case  0 : stage1.IsDropped = True : stage2.IsDropped = True : stage3.IsDropped = True  : lightnum(lightnumIn) = 6
	End Select
End Sub

'Fades Reel Based Lights When Multiple Lights are controlled by the Same Variable
'NOTE: The last light controlled by that variable must be called with the regular Sub handler 
'Example call: "FadeRm 1, light1"
Sub FadeRm(lightnumIn, reelname)
	'Note that reel construction order must be off, bright, stage1, stage2
	Select Case lightnum(lightnumIn)
		Case  5 : reelname.setvalue 1 
		Case  4 : reelname.setvalue 3  
		Case  3 : reelname.setvalue 2  
		Case  2 : reelname.setvalue 0 
		Case  1 : reelname.setvalue 1
		Case  0 : reelname.setvalue 0
	End Select
End Sub

'Fades Reel Based Lights
'Example call: "FadeR 1, light1"
Sub FadeR(lightnumIn, reelname)
	'Note that reel construction order must be off, bright, stage1, stage2
	Select Case lightnum(lightnumIn)
		Case  5 : reelname.setvalue 1 : lightnum(lightnumIn) = 6
		Case  4 : reelname.setvalue 3 : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  3 : reelname.setvalue 2 : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  2 : reelname.setvalue 0 : lightnum(lightnumIn) = 6
		Case  1 : reelname.setvalue 1 : lightnum(lightnumIn) = 6
		Case  0 : reelname.setvalue 0 : lightnum(lightnumIn) = 6
	End Select
End Sub

'Handles regular on/off lights or objects like Pop Bumpers or single lights (e.g.light1)
'Example call: "NFade 1, light1"
Sub NFade(lightnumIn,light)
	Select Case lightnum(lightnumIn)
		Case 5: light.state = 1
		Case 4: light.state = 0
		Case 1: light.state = 1
		Case 0: light.state = 0
	End Select
End Sub

'Handles permanant 2-state lights (uses 2-lamps, one for on and one for off) (e.g. light1 and light1off)
'Example call: "LitL2 1, light1, light1off"
Sub LitL2(lightnumIn, light, lightoff) 
	Select Case lightnum(lightnumIn)
		Case 5: lightoff.state = 0 : light.state    = 1
		Case 4: light.state    = 0 : lightoff.state = 1
		Case 1: lightoff.state = 0 : light.state    = 1
		Case 0: light.state    = 0 : lightoff.state = 1
	End Select
End Sub

'Handles 2-state (on/off only) Wall Based Lights
'Example call: "LitW2 1, light1"
Sub LitW2(lightnumIn, stage3)
	Select Case lightnum(lightnumIn)
		Case 5: stage3.IsDropped = 0
		Case 4: stage3.IsDropped = 1
		Case 1: stage3.IsDropped = 0
		Case 0: stage3.IsDropped = 1
	End Select
End Sub

'********************
'AUTO-LABEL HANDLERS
'********************
'NOTE: These take a big chunk of CPU time to process the labeling in real time, but they allow you to 
' just enter [lightnum,lightOnObject] instead of [lightnum, Lighta, Lightb, LightOn, LightOff]
' Example: aFadeL 11,Light11
'                versus
'           FadeL 11,Light11a,Light11b,Light11,Light11off
'
'These ONLY apply to the Light and Wall Fade handlers.  The Reel handler is the same in both cases and suffers no
'significant CPU use regardless

'Fades Light Object based Lights When Multiple Lights are controlled by the Same Variable
'NOTE: The last light controlled by that variable must be called with the regular Sub handler 
'Example call: "aFadem 1, light1"
Sub aFadeLm(lightnumIn, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color 
	dim stage1on : stage1on = "" & stage3.name & "a.state = 1" : dim stage1off : stage1off = "" & stage3.name & "a.state = 0"
	dim stage2on : stage2on = "" & stage3.name & "b.state = 1" : dim stage2off : stage2off = "" & stage3.name & "b.state = 0"
	dim offstaton: offstaton= "" & stage3.name & "off.state=1" : dim offstatoff: offstatoff= "" & stage3.name & "off.state=0"

	Select Case lightnum(lightnumIn)
		Case  5 : ExecuteGlobal stage1off : ExecuteGlobal stage2off : stage3.state = 0 : stage3.state = 1
		Case  4 : stage3.state = 0 : ExecuteGlobal stage2off : ExecuteGlobal stage2on 
		Case  3 : ExecuteGlobal stage2off : ExecuteGlobal stage1off : ExecuteGlobal stage1on 
		Case  2 : ExecuteGlobal stage1off : ExecuteGlobal offstatoff : ExecuteGlobal offstaton 
		Case  1 : ExecuteGlobal offstatoff: stage3.state = 1 
		Case  0 : stage3.state = 0 : ExecuteGlobal offstaton 
	End Select
End Sub

'Fades Light Object based Lights 
'Example call: "aFade 1, light1"
Sub aFadeL(lightnumIn, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color
	dim stage1on : stage1on = "" & stage3.name & "a.state = 1" : dim stage1off : stage1off = "" & stage3.name & "a.state = 0"
	dim stage2on : stage2on = "" & stage3.name & "b.state = 1" : dim stage2off : stage2off = "" & stage3.name & "b.state = 0"
	dim offstaton: offstaton= "" & stage3.name & "off.state=1" : dim offstatoff: offstatoff= "" & stage3.name & "off.state=0"

	Select Case lightnum(lightnumIn)
		Case  5 : ExecuteGlobal stage1off : ExecuteGlobal stage2off : stage3.state = 0 : stage3.state = 1 : lightnum(lightnumIn) = 6
		Case  4 : stage3.state = 0 : ExecuteGlobal stage2off : ExecuteGlobal stage2on  : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  3 : ExecuteGlobal stage2off : ExecuteGlobal stage1off : ExecuteGlobal stage1on : lightnum(lightnumIn) = lightnum(lightnumIn) - 1 
		Case  2 : ExecuteGlobal stage1off : ExecuteGlobal offstatoff : ExecuteGlobal offstaton : lightnum(lightnumIn) = 6
		Case  1 : ExecuteGlobal offstatoff: stage3.state = 1 : lightnum(lightnumIn) = 6
		Case  0 : stage3.state = 0 : ExecuteGlobal offstaton : lightnum(lightnumIn) = 6
	End Select
End Sub

'Fades Wall Based Lights When Multiple Lights are controlled by the Same Variable
'NOTE: The last light controlled by that variable must be called with the regular Sub handler 
'Example call: "aFadeWm 1, light1"
Sub aFadeWm(lightnumIn, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color
	dim stage1on : stage1on = "" & stage3.name & "a.IsDropped = 1" : dim stage1off : stage1off = "" & stage3.name & "a.IsDropped = 0"
	dim stage2on : stage2on = "" & stage3.name & "b.IsDropped = 1" : dim stage2off : stage2off = "" & stage3.name & "b.IsDropped = 0"
	Select Case lightnum(lightnumIn)
		Case  5 : ExecuteGlobal stage1on  : ExecuteGlobal stage2on : stage3.IsDropped = False 
		Case  4 : stage3.IsDropped = True : ExecuteGlobal stage2off 
		Case  3 : ExecuteGlobal stage2on  : ExecuteGlobal stage1off 
		Case  2 : ExecuteGlobal stage1on
		Case  1 : ExecuteGlobal stage1on  : ExecuteGlobal stage2on : stage3.IsDropped = False
		Case  0 : ExecuteGlobal stage1on  : ExecuteGlobal stage2on : stage3.IsDropped = True
	End Select
End Sub

'Fades Wall Based Lights
'Example call: "aFadeW 1, light1"
Sub aFadeW(lightnumIn, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is brighter off color
	dim stage1on : stage1on = "" & stage3.name & "a.IsDropped = 1" : dim stage1off : stage1off = "" & stage3.name & "a.IsDropped = 0"
	dim stage2on : stage2on = "" & stage3.name & "b.IsDropped = 1" : dim stage2off : stage2off = "" & stage3.name & "b.IsDropped = 0"
	Select Case lightnum(lightnumIn)
		Case  5 : ExecuteGlobal stage1on  : ExecuteGlobal stage2on : stage3.IsDropped = False : lightnum(lightnumIn) = 6
		Case  4 : stage3.IsDropped = True : ExecuteGlobal stage2off : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  3 : ExecuteGlobal stage2on  : ExecuteGlobal stage1off : lightnum(lightnumIn) = lightnum(lightnumIn) - 1
		Case  2 : ExecuteGlobal stage1on  : lightnum(lightnumIn) = 6
		Case  1 : ExecuteGlobal stage1on  : ExecuteGlobal stage2on : stage3.IsDropped = False : lightnum(lightnumIn) = 6
		Case  0 : ExecuteGlobal stage1on  : ExecuteGlobal stage2on : stage3.IsDropped = True  : lightnum(lightnumIn) = 6
	End Select
End Sub

'Handles permanant 2-state lights (uses 2-lamps, one for on and one for off) (e.g. light1 and light1off)
'Example call: "aLitL2 1, light1"
Sub aLitL2(lightnumIn, light) 
	dim lightoff_on : lightoff_on = "" & light.name & "off.state = 1" : dim lightoff_off : lightoff_off = "" & light.name & "off.state = 0"
	Select Case lightnum(lightnumIn)
		Case 5: ExecuteGlobal lightoff_off : light.state = 1
		Case 4: light.state    = 0         : ExecuteGlobal lightoff_on
		Case 1: ExecuteGlobal lightoff_off : light.state    = 1
		Case 0: light.state    = 0         : ExecuteGlobal lightoff_on
	End Select
End Sub


'------------------------------------
'Handle Case of disabling All Lights
'------------------------------------

Sub AllLightsOff()
	'Set all Control Variables to 0 and call fade update 3 times to reset all lights to off
	On Error Resume Next
		For Tset = 0 To 150
			lightnum(Tset) = 0
		Next
	UpdateLights : UpdateLights : UpdateLights
End Sub

