'=======================================
'      NO FEAR: Dangerous Sports
'           Williams 1995
'        VPM Conversion by PD
'=======================================
'
' Load VPinMAME and *.VBS file, check versions
'
Option Explicit
LoadVPM "01120100", "WPC.VBS", 3.10

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

Const RomSet = "nf_23x" ' No Fear V2.3X

'==========
'Yield Time 
'==========
'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 YieldTime = 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


'=========
' 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 SfxHole       = "Hole"       ' Ball hitting a sinkhole
Const SfxSubway     = "Subway"     ' Ball rolling under playfield
Const SfxDropUp     = "DropTarget" ' Drop target up
Const SfxDropDown   = "DropTarget" ' Drop target down
Const SfxSaucer     = "Saucer"     ' ball kicked out of saucer
Const SfxKick       = "kick"       ' ball kicked out of kicker
Const SfxBallDrop   = "BallDrop"   ' ball falling onto playfield
Const SfxSolenoid   = "Solenoid2"  ' Solenoid

'==============================================
' 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
Set GICallback = GetRef("UpdateGI")
Dim bsTrough, bsSkull, bsLLock, bsRTPop, bsLSaucer, dtSkyDive, mleft, mright, mcenter

Sub NoFear_Init()
	vpmInit Me : Randomize 
'	SaveValue cRegistryName, "Options", 0 : Exit Sub  ' This will clear your Registry settings if uncommented
	NoFearOptions = CInt("0" & LoadValue(cRegistryName, "Options")) : NoFearSetOptions
	With Controller
		If No_Yield = False Then : If Controller.Version >= "01200000" Then NoFear.YieldTime = YieldTime : End If
		.GameName = RomSet
		.SplashInfoLine = "NO FEAR: Dangerous Sports (Williams 1995)" & vbNewLine &_
                          "VPM Table And Script By PD" 
        .Games(Romset).Settings.Value("samples")=0 ' Make sure samples are disabled in Vpinmame
		.ShowTitle = False : .ShowFrame = False : .ShowDMDOnly = True
		On Error Resume Next 
			.Run : If Err Then MsgBox Err.Description
		On Error Goto 0
	End With
	
	'Extra Key Labels in F2 Menu During Game
	ExtraKeyHelp =KeyName(3)  & vbTab & "Buy-In"

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

	' Trough
	Set bsTrough = New cvpmBallStack : With bsTrough
		.InitSw 0,32,33,34,35,31,0,0
		.InitKick BallRelease,180,10
		.InitAddSnd SfxOuthole : .InitExitSnd SfxTroughRel,SfxSolenoid
		.InitEntrySnd SfxTroughKick, SSolenoidOn
		.CreateEvents "bsTrough", OutHole
		.IsTrough = True 
		.Balls = 4
	End With

	' Right Popper
	Set bsRTPop = New cvpmBallStack : With bsRTPop
		.InitSw 0,41,42,0,0,0,0,0
		.InitKick RightPopper, 190, 10
		.InitExitSnd Sfxkick, SfxSolenoid
		.CreateEvents "bsRTPop", 0
	End With

	' Left Saucer
	Set bsLSaucer = New cvpmBallStack : With bsLSaucer
		.InitSaucer LeftSaucer, 61, 128, 5
		.InitExitSnd SfxSaucer, SfxSolenoid
		.CreateEvents "bsLSaucer", LeftSaucer
	End With

	' SkyDive drop target
	Set dtSkyDive = New cvpmDropTarget : With dtSkyDive
		.InitDrop Switch51, 51
		.InitSnd SfxDropDown, SfxDropUp
		.CreateEvents "dtSkyDive"
	End With

	' Rear Ramp Magnets
	Set mLeft = New cvpmMagnet : With mLeft
		.InitMagnet LeftMagnet, 50  ' Left Magnet Strength (adjust to taste)
		.GrabCenter = False : .Size = 400
		.CreateEvents "mLeft"
	End With

	Set mCenter = New cvpmMagnet : With mCenter
		.InitMagnet CenterMagnet, 45 ' Center Magnet Strength (adjust to taste)
		.GrabCenter = False : .Size = 400
		.CreateEvents "mCenter"
	End With

	Set mRight = New cvpmMagnet : With mRight
		.InitMagnet RightMagnet, 30  ' Right Magnet Strength (adjust to taste)
		.GrabCenter = False : .Size = 400
		.CreateEvents "mRight"
	End With

	' Misc. Initialisation
	Controller.Switch(22) = True 			'Coin Door Closed
	Controller.Switch(24) = True			'Always Closed
	Plunger.Pullback : KickBack.Pullback    'Initialize Plungers
	Switch51.IsDropped = True				'Drop target down

	' Intialize Skull
	If No_Pac = False Then
		PacShut.IsDropped = False
		SkullShut.IsDropped = True
	Else
		SkullShut.IsDropped = False
		PacShut.IsDropped = True
	End If
	Pac.IsDropped = True
	Skull.IsDropped = True
End Sub

'======================
'Options Menu Variables
'======================
Const cRegistryName = "NoFearDS"
Const cOptNoGI      = &H01
Const cOptNoFlash   = &H02
Const cOptNoMagnet  = &H04
Const cOptNoYield   = &H08
Const cOptNoSkull   = &H10
Const cOptPac       = &H20
Const cOptNoFade    = &H40
Const cOptNoFadeF   = &H80
Const cOptNoFadeG	= &H100

'===================
' DIP + Options menu
'===================
Dim NoFearOptions
Private Sub NoFearShowDips
	ChangePDS = No_Pac
	If Not IsObject(vpmDips) Then ' First time
		Set vpmDips = New cvpmDips
		With vpmDips
			.AddForm 100, 330, "No Fear 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,190,150,"Disable",0,_
				Array("GI Effects", cOptNoGI, "No Return Lane Flashers", cOptNoFlash, "Magnets", cOptNoMagnet,_
		        	 "Skull Anim", cOptNoSkull, "Fading Light Effect", cOptNoFade, "Fading Flasher Effect", cOptNoFadeF,_
					 "Fading GI Effect",cOptNoFadeG, "YieldTime (requires reset)", cOptNoYield)
			.AddFrameExtra 0,325,150,"Enable",0,_
				Array("PD_CPU",cOptPac)
		End With
	End If
	NoFearOptions = vpmDips.ViewDipsExtra(NoFearOptions)
	DipMenuUsed = True
	NoFearSetOptions : SaveValue cRegistryName, "Options", NoFearOptions
End Sub
Set vpmShowDips = GetRef("NoFearShowDips")

'=======================
' Handle NO FEAR Options
'=======================
Dim No_GI       : No_GI       = False		'Defaults for 1st start
Dim No_Magnets  : No_Magnets  = False     
Dim No_Skull    : No_Skull    = False
Dim No_Flashers : No_Flashers = False
Dim No_Yield    : No_Yield    = False				
Dim No_Pac      : No_Pac      = True
Dim ChangePDS   : ChangePDS   = No_Pac
Dim DipMenuUsed : DipMenuUsed = False

Sub NoFearSetOptions
	If NoFearOptions And cOptNoMagnet Then : No_Magnets  = True : Else No_Magnets  = False : End If
	If NoFearOptions And cOptNoSkull  Then : No_Skull    = True : Else No_Skull    = False : End If
	If NoFearOptions And cOptNoFade   Then : Fading      = False: Else Fading      = True  : End If
	If NoFearOptions And cOptNoFadeF  Then : FadingF     = False: Else FadingF     = True  : End If
	If NoFearOptions And cOptNoFadeG  Then : FadingG     = False: Else FadingG     = True  : End If
	If NoFearOptions And cOptNoYield  Then : No_Yield    = True : Else No_Yield    = False : End If

	If NoFearOptions And cOptNoGI Then
		SetLight 118, 1 : SetLight 110, 1 : SetLight 111, 1 : SetLight 119, 1 : SetLight 120, 1  ' GI Lights
		SetLight 112, 1 : SetLight 113, 1 : SetLight 121, 1 : SetLight 122, 1 : SetLight 125, 1 : SetLight 126, 1
	    If NoFear.image <> "nfplay3_dark" Then
	    	SetLight 120, 0 : SetLight 122, 0
	    End If		
		UpdateLights : UpdateLights : UpdateLights  ' Fade Out
		No_GI = True 
	Else
	    If NoFear.image <> "nfplay3_dark" Then 
	    	SetLight 120, 0 : SetLight 122, 0
			UpdateLights : UpdateLights : UpdateLights  ' Fade Out
		End If
		No_GI = False
	End If

	If NoFearOptions And cOptNoFlash  Then
		SetLight 114, 0 : SetLight 115, 0 : SetLight 115, 0 : SetLight 116, 0 : SetLight 117, 0 ' Return Light Flashers
		SetLight 123, 0 : SetLight 124,0 ' Return Wall Flashers
		UpdateLights : UpdateLights : UpdateLights  ' Fade Out
		No_Flashers = True
	Else
		No_Flashers = False
	End If

	If NoFearOptions And cOptPac Then 
		No_Pac 				  = False
		If DipMenuUsed        = True and No_Pac <> ChangePDS Then Playsound "PacStart"
		Pac.IsDropped         = False
		Skull.IsDropped       = True
		SkullShut.IsDropped   = True
	Else
		No_Pac 				  = True 
		If DipMenuUsed        = True and No_Pac <> ChangePDS Then Playsound "PacEnd"
		Skull.IsDropped       = False
		Pac.IsDropped         = True
		PacShut.IsDropped     = True
	End If
End Sub

'============
' Solenoids
'============

'Sound Solenoids
SolCallback(7)      		= "vpmSolSound SfxKnocker,"				'Knocker
SolCallback(10)     		= "vpmSolSound SfxSling,"				'Right Slingshot
SolCallback(11)     		= "vpmSolSound SfxSling,"				'Left Slingshot

'Functional Solenoids
SolCallback(1)      		= "bsRTPop.SolOut"						'Right Popper
SolCallback(2)       		= "vpmSolAutoPlunger Plunger,2,"		'AutoPlunger
SolCallback(3)      		= "SolMiniMagnet mRight,"				'Right Magnet
SolCallback(4)      		= "vpmSolAutoPlunger KickBack,1,"		'Kickback
SolCallback(5)      		= "SolMiniMagnet mCenter,"				'Center Magnet
SolCallback(6)      		= "SolMiniMagnet mLeft,"				'Left Magnet
SolCallback(8)  			= "dtSkyDive.SolDropDown"				'SkyDive Drop Target Down
SolCallback(12)  			= "dtSkyDive.SolDropUp"					'SkyDive Drop Target Up
SolCallback(14)       		= "bstrough.SolOut"						'Outhole
SolCallback(15)    			= "bsLSaucer.SolOut"					'Left Saucer Eject
SolCallBack(16)   			= "AnimateSkull"						'Skull Mouth
SolCallback(sLRFlipper)     = "SolFlip RightFlipper,Nothing,"		'Lower Right Flipper
SolCallback(sLLFlipper)     = "SolFlip LeftFlipper,Nothing,"		'Left Flipper
SolCallback(sURFlipper)     = "SolFlip2 UpperRightFlipper,Nothing,"	'Upper Right Flipper


'Functional Sol Calls
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

Sub SolFlip2(aFlip1, aFlip2, aEnabled)  ' Ball Through Flipper Fix (No Sound)
	Dim oldSpeed
	If aEnabled Then
		aFlip1.RotateToEnd : If Not aFlip2 Is Nothing Then aFlip2.RotateToEnd
	Else
		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

'Flasher Solenoids
SolCallback(17)  			= "ReturnFlash"                			'Flipper Return
SolCallback(18)  			= "SetLightF 105,"			   			'Spinner
SolCallback(19)  			= "SetLightF 100,"              			'No Fear
SolCallback(20)  			= "SetLightF 106,"			 			'Right Ramp / Insert Panel
SolCallback(21)  			= "SkullFlasher"              			'Skull / Insert Panel
SolCallback(22)  			= ""                           			'Insert Explode
SolCallback(23)  			= "SetLightF 107,"			  			'Left Ramp / Insert Panel
SolCallback(24)  			= "SetLightF 108,"			   			'Top Left (back panel)
SolCallback(25)  			= "AutoFlash"                  			'AutoFire
SolCallback(26)  			= ""                           			'Top Left Insert
SolCallback(27)  			= ""                           			'Top Right Insert
SolCallback(28)  			= "SetLightF 109,"		    			'Right Popper

'Flasher Sol Calls
Sub SkullFlasher(enabled)
	'Set two numbers since there are two flashers overlapping two regular bulbs
	SetLightF 101, enabled
	SetLightF 102, enabled
End Sub

Sub AutoFlash(enabled)
	'Set two numbers since there are two flashers overlapping two regular bulbs
	SetLightF 103, enabled
	SetLightF 104, enabled
End Sub

Sub ReturnFlash(enabled)
	If No_Flashers = False Then ' Disable Return Lane Flashers When True
		'Set four numbers since there are two sets of two flashers overlapping two regular bulbs
		SetLightF 114,enabled
		SetLightF 115,enabled
		SetLightF 116,enabled
		SetLightF 117,enabled
		'Set two more numbers for the overlapping return lane wall / GI bulbs
		SetLightF 123,enabled ' Left  Return Overlapping Wall
		SetLightF 124,enabled ' Right Return Overalapping Wall
	End If
End Sub

'===================
' Keyboard Handling
'====================
Sub NoFear_KeyUp(ByVal keycode)
	If vpmKeyUp(keycode) Then Exit Sub
	If keycode = PlungerKey  Then Controller.Switch(11) = False
	If keycode = 3 Then Controller.Switch(23) = False
End Sub

Sub NoFear_KeyDown(ByVal keycode)
	If vpmKeyDown(keycode) Then Exit Sub
	If keycode = PlungerKey  Then Controller.Switch(11) = True
	If keycode = 3 Then Controller.Switch(23) = True
End Sub

'============
' GI Handling
'============
Dim GI_IS_On

Sub UpdateGI(no, enabled)
If No_GI = False Then
	Select Case no 
		Case 0 ' Playfield Top
			SetLightG 118, enabled
		Case 1 ' Playfield Right
			SetLightG 110, enabled ' Three Sets of overlapping lights
			SetLightG 111, enabled
			SetLightG 126, enabled
			'
			SetLightG 119, enabled
		    If NoFear.image = "nfplay3_dark" Then :	SetLightG 120, enabled : End If		
		Case 2 ' Playfield Left
			SetLightG 112, enabled ' Three Sets of overlapping lights
			SetLightG 113, enabled
			SetLightG 125, enabled
			'
			SetLight 121, enabled
		    If NoFear.image = "nfplay3_dark" Then : SetLightG 122, enabled : End If
'		Case 3 ' Insert Title
'		Case 4 ' Insert Background

	End Select
End If
End Sub

'================
' Switch handling
'================

'Rollover & Optos
Sub Switch15_Hit()   : controller.switch (15) = true  : End Sub	'Shooter Lane
Sub Switch15_unHit() : controller.switch (15) = false : End Sub
Sub Switch17_Hit()   : controller.switch (17) = true  : End Sub	'Right Outlane
Sub Switch17_unHit() : controller.switch (17) = false : End Sub
Sub Switch18_Hit()   : controller.switch (18) = true  : End Sub	'Right Return
Sub Switch18_unHit() : controller.switch (18) = false : End Sub
Sub Switch25_Hit()   : controller.switch (25) = true  : End Sub	'Kickback
Sub Switch25_unHit() : controller.switch (25) = false : End Sub
Sub Switch26_Hit()   : controller.switch (26) = true  : End sub	'Left Return
Sub Switch26_unHit() : controller.switch (26) = false : End Sub
Sub Switch46_Hit()   : controller.switch (46) = true  : End Sub 'Left Magnet
Sub Switch46_unHit() : controller.switch (46) = false : End Sub 
Sub Switch47_Hit()   : controller.switch (47) = true  : End Sub 'Center Magnet
Sub Switch47_unHit() : controller.switch (47) = false : End Sub
Sub Switch48_Hit()   : controller.switch (48) = true  : End Sub 'Right Magnet
Sub Switch48_unHit() : controller.switch (48) = false : End Sub
Sub Switch54_Hit()   : controller.switch (54) = true  : End Sub	'Left Wireform
Sub Switch54_unHit() : controller.switch (54) = false : End Sub
Sub Switch55_Hit()   : controller.switch (55) = true  : End Sub	'Inner Loop
Sub Switch55_unHit() : controller.switch (55) = false : End Sub
Sub Switch58_Hit()   : controller.switch (58) = true  : End Sub	'Right Loop
Sub Switch58_unHit() : controller.switch (58) = false : End Sub
Sub Switch62_Hit()   : controller.switch (62) = true  : End Sub	'Left Loop
Sub Switch62_unHit() : controller.switch (62) = false : End Sub
Sub Switch63_Hit()   : controller.switch (63) = true  : End Sub	'Left Ramp Enter
Sub Switch63_unHit() : controller.switch (63) = false : End Sub
Sub Switch64_Hit()   : controller.switch (64) = true  : End Sub	'Left Ramp Middle
Sub Switch64_unHit() : controller.switch (64) = false : End Sub
Sub Switch66_Hit()   : controller.switch (66) = true  : End Sub	'Right Ramp Enter
Sub Switch66_unHit() : controller.switch (66) = false : End Sub
Sub Switch67_Hit()   : controller.switch (67) = true  : End Sub	'Right Ramp Exit
Sub Switch67_unHit() : controller.switch (67) = false : End Sub
'Pulsed
Sub Switch16_Spin()      : vpmtimer.pulsesw 16            : End Sub 			'Spinner Switch
Sub Switch27_Slingshot() : vpmtimer.pulsesw 27      	  : End Sub 			'Left Slingshot
Sub Switch28_Slingshot() : vpmtimer.pulsesw 28       	  : End Sub 			'Right Slingshot
Sub Switch56_Hit()       : vpmtimer.pulsesw 56            : End Sub 			'Bottom light kickback switch
Sub Switch57_Hit()       : vpmtimer.pulsesw 57            : End Sub 			'Top light kickback switch
Sub Switch56_57_Hit()    : vpmtimer.pulsesw 56 : vpmtimer.pulsesw 57 : End Sub  'In-between kickback hit (toggles both)

'==============
'Handle Magnets
'==============
Sub SolMiniMagnet(aMag,enabled)
	If enabled and No_Magnets = False Then With aMag : .MagnetOn = True : .Update : .MagnetOn = False : End With 'Fast Pulse Only
End Sub

'Lock and Saucer Handling
Sub SkyKick_Hit()
	SkyKick.DestroyBall
	Playsound sfxhole
	PlaySound sfxSubway
	vpmTimer.Pulseswitch 38, 1800, "bsRTPop.Addball"
End Sub

Sub SkullKick1_Hit()
	SkullKick1.DestroyBall
	Playsound sfxhole
	PlaySound sfxSubway
	vpmTimer.Pulseswitch 37, 1800, "bsRTPop.AddBall"
End Sub

Sub SkullKick2_Hit()
	SkullKick2.DestroyBall
	Playsound sfxhole
	PlaySound sfxSubway
	vpmTimer.Pulseswitch 37, 1800, "bsRTPop.Addball"
End Sub

'Animate Skull
Sub AnimateSkull(enabled)
	If enabled and No_Skull = False Then
	   If No_Pac = True Then
		SkullShut.IsDropped = True
		Skull.IsDropped     = False
		Pac.IsDropped       = True
		PacShut.IsDropped   = True
	   Else
		PacShut.IsDropped   = True
		Pac.IsDropped       = False
		Skull.IsDropped     = True
		SkullShut.IsDropped = True
	   End If
	Else
       If No_Pac = True Then
		Skull.IsDropped     = True
		SkullShut.IsDropped = False
		Pac.IsDropped       = True
		PacShut.IsDropped   = True
	   Else
		Pac.IsDropped       = True
		PacShut.IsDropped   = False
		Skull.IsDropped     = True
		SkullShut.IsDropped = True
	   End If
	End If
End Sub

'==========================================================
'Light Control (Featuring the PD V5.2 Fading Light System)
'==========================================================

Sub UpdateLights()
FadeL 11,light11a,light11b,light11,light11off	'Raceway Award 3
FadeL 12,light12a,light12b,light12,light12off	'Raceway Award 2
FadeL 13,light13a,light13b,light13,light13off	'Raceway Award 1
FadeL 14,light14a,light14b,light14,light14off	'Left Over The Edge
FadeL 15,light15a,light15b,light15,light15off	'Dirt
FadeL 16,light16a,light16b,light16,light16off	'Asphalt
FadeL 17,light17a,light17b,light17,light17off	'No Limits
FadeL 18,light18a,light18b,light18,light18off	'Water
FadeL 22,light22a,light22b,light22,light22off	'Raceway Award 4
FadeL 23,light23a,light23b,light23,light23off	'Raceway Award 5
FadeL 24,light24a,light24b,light24,light24off	'Right Over The Edge
FadeL 25,light25a,light25b,light25,light25off	'Air
FadeL 26,light26a,light26b,light26,light26off	'Snow
FadeL 27,light27a,light27b,light27,light27off	'Fear Fest
FadeL 28,light28a,light28b,light28,light28off	'Meet Your Maker
FadeL 31,light31a,light31b,light31,light31off	'Skull
FadeL 32,light32a,light32b,light32,light32off	'Right Track
FadeL 33,light33a,light33b,light33,light33off	'Center Jackpot
FadeL 34,light34a,light34b,light34,light34off	'Extra Ball
FadeL 35,light35a,light35b,light35,light35off	'Start Challenge
FadeL 36,light36a,light36b,light36,light36off	'Center Lock
FadeL 38,light38a,light38b,light38,light38off	'Left Hurry Up
FadeL 41,light41a,light41b,light41,light41off	'Left Track
FadeL 42,light42a,light42b,light42,light42off	'First Place
FadeL 43,light43a,light43b,light43,light43off	'Second Place
FadeL 44,light44a,light44b,light44,light44off	'Third Place
FadeL 45,light45a,light45b,light45,light45off	'Skydive
FadeL 46,light46a,light46b,light46,light46off	'Drop Jackpot
FadeL 47,light47a,light47b,light47,light47off	'Drop Lock
FadeL 51,light51a,light51b,light51,light51off	'Raceway
FadeL 52,light52a,light52b,light52,light52off	'Left Ramp Turn
FadeL 53,light53a,light53b,light53,light53off	'Super Spinner
FadeL 54,light54a,light54b,light54,light54off	'Left Ramp Start
FadeL 55,light55a,light55b,light55,light55off	'Left Ramp Win
FadeL 56,light56a,light56b,light56,light56off	'Hill Climb
FadeL 57,light57a,light57b,light57,light57off	'Screamer
FadeL 61,light61a,light61b,light61,light61off	'Tube
FadeL 62,light62a,light62b,light62,light62off	'Video Mode
FadeL 63,light63a,light63b,light63,light63off	'Right Ramp Win
FadeL 64,light64a,light64b,light64,light64off	'Right Ramp Turn
FadeL 65,light65a,light65b,light65,light65off	'Right Ramp Start
FadeL 66,light66a,light66b,light66,light66off	'Left Flipper Lane
FadeL 67,light67a,light67b,light67,light67off	'Left Outlane
FadeL 68,light68a,light68b,light68,light68off	'Kickback
FadeL 71,light71a,light71b,light71,light71off	'Light Kickback Top
FadeL 72,light72a,light72b,light72,light72off	'Light Kickback Bottom
FadeL 73,light73a,light73b,light73,light73off	'Right Flipper Lane
FadeL 74,light74a,light74b,light74,light74off	'Right Outlane
FadeL 75,light75a,light75b,light75,light75off	'Hairpin
FadeL 76,light76a,light76b,light76,light76off	'Downhill
FadeL 77,light77a,light77b,light77,light77off	'Summit
FadeL 78,light78a,light78b,light78,light78off	'Right Hurry Up
FadeL 81,light81a,light81b,light81,light81off	'Shoot Again
FadeL 83,light83a,light83b,light83,light83off	'Jump Now
FadeL 84,light84a,light84b,light84,light84off	'Super Jackpot
FadeL 86,light86a,light86b,light86,light86off	'Ball Launch
FadeL 87,light87a,light87b,light87,light87off	'Buy-In Button
FadeL 88,light88a,light88b,light88,light88off	'Start Button

'Flashers
FadeR 105,HurryFlasher																		'Spinner
FadeL 106,RightRampFlashA,RightRampFlashB,RightRampFlash,RightRampFlashOff					'Right Ramp / Insert Panel
FadeL 107,LeftRampFlashA,LeftRampFlashB,LeftRampFlash,LeftRampFlashOff						'Left Ramp / Insert Panel
FadeL 108,TopLeftFlashA,TopLeftFlashB,TopLeftFlash,TopLeftFlashOff							'Top Left (back panel)
FadeL 109,PopperFlashA,PopperFlashB,PopperFlash,PopperFlashOff								'Popper

'Overlapping Fading Flashers With Fading Lights
MFadeL 100,NFflashA,NFflashB,NFflash,             21,light21a,light21b,light21, light21off	'No Fear
MFadeL 101,SkullFlash1A,SkullFlash1B,SkullFlash1, 82,light82a,light82b,light82, light82off	'Left Skull Eye
MFadeL 102,SkullFlash2A,SkullFlash2B,SkullFlash2, 85,light85a,light85b,light85, light85off	'Right Skull Eye
MFadeL 103,AutoFlashLa,AutoFlashLb,AutoFlashL,    48,light48a,light48b,light48, light48off	'Left  Autofire (2)
MFadeL 104,AutoFlashRa,AutoFlashRb,AutoFlashR,    37,light37a,light37b,light37, light37off	'Right Autofire (2)

'Overlapping Fading Flashers With Fading GI Lights
If No_Flashers = False Then ' Return Lane Flashers Disabled when true
MFadeL 114,ReturnFlashLA,ReturnFlashLB,ReturnFlashL,    110, ReturnGIL1A,ReturnGIL1B,ReturnGIL1, ReturnFlashLOff	'Left
MFadeL 115,ReturnFlashL2A,ReturnFlashL2B,ReturnFlashL2, 111, ReturnGIL2A,ReturnGIL2B,ReturnGIL2, ReturnFlashL2Off	'Left 2
MFadeL 116,ReturnFlashRA,ReturnFlashRB,ReturnFlashR,    112, ReturnGIR1A,ReturnGIR1B,ReturnGIR1, ReturnFlashROff	'Right
MFadeL 117,ReturnFlashR2A,ReturnFlashR2B,ReturnFlashR2, 113, ReturnGIR2A,ReturnGIR2B,ReturnGIR2, ReturnFlashR2Off	'Right 2
MFadeW 123,RFlashLa,RFlashLb,RFlashL,                   125, LReturnGIOnA,LReturnGIOnB,LReturnGIOn					'L Top
MFadeW 124,RFlashRa,RFlashRb,RFlashR,                   126, RReturnGIOnA,RReturnGIOnB,RReturnGIOn					'R Top
Else ' Keep GI working when Return Flashers are off
FadeL 110, ReturnGIL1A,ReturnGIL1B,ReturnGIL1, ReturnFlashLOff	'Left
FadeL 111, ReturnGIL2A,ReturnGIL2B,ReturnGIL2, ReturnFlashL2Off	'Left 2
FadeL 112, ReturnGIR1A,ReturnGIR1B,ReturnGIR1, ReturnFlashROff	'Right
FadeL 113, ReturnGIR2A,ReturnGIR2B,ReturnGIR2, ReturnFlashR2Off	'Right 2
FadeW 125, LReturnGIOnA,LReturnGIOnB,LReturnGIOn				'L Top
FadeW 126, RReturnGIOnA,RReturnGIOnB,RReturnGIOn				'R Top
End If
'GI Lights
If No_GI = False Then
FadeWm 118,TopGIOnA,TopGIOnB,TopGIOn				 ' PlayField Top   GI (Top 1)
FadeWm 118,TopGI2OnA,TopGI2OnB,TopGI2On				 ' PlayField Top   GI (Top 2)
FadeW  118,TopGI3OnA,TopGI3OnB,TopGI3On				 ' PlayField Top   GI (Top 3)
'
FadeW  119,RSlingGIOnA,RSlingGIOnB,RSlingGIOn		 ' PlayField Right GI (Right Sling Plastic)
FadeWm 120,SlingRPlayOnA,SlingRPlayOnB,SlingRPlayOn  ' PlayField Right GI (SlingR  Play)
FadeW  120,PlayGIRa,PlayGIRb,PlayGIR				 ' PlayField Right GI (ReturnR Play)
'
FadeWm 121,LeftGIOn1A,LeftGIOn1B,LeftGIOn1			 ' PlayField Left  GI (Mid Left)
FadeW  121,LSlingGIOnA,LSlingGIOnB,LSlingGIOn		 ' PlayField Left  GI (Left  Sling Plastic)
FadeWm 122,LeftPlayLitA,LeftPlayLitB,LeftPlayLit     ' PlayField Left  GI (Mid Left Play)
FadeWm 122,SlingLPlayOnA,SlingLPlayOnB,SlingLPlayOn  ' PlayField Left  GI (SlingL   Play)
FadeW  122,PlayGILa,PlayGILb,PlayGIL				 ' PlayField Left  GI (ReturnL  Play)
End If
End Sub


'======================
'VP Physics Workarounds
'======================

'Transport Kicker to get balls onto the right wire ramp
Sub Kicker3_Hit()  : Kicker3.DestroyBall : vpmTimer.AddTimer 250, "Transport" : End Sub
Sub Transport(aSw) : RightPopper.CreateBall : RightPopper.Kick 190,5 : End Sub

' Acts as perfect ramp end so ball falls straight down.
Sub LDrop1_Hit() : LDrop1.DestroyBall : LDrop2.CreateBall : LDrop2.Kick 0,0 : PlaySound SfxBallDrop : End Sub

' Acts as perfect ramp end so ball falls straight down.
Sub RDrop1_Hit() : RDrop1.DestroyBall :	RDrop2.CreateBall : RDrop2.Kick 0,0 : PlaySound SfxBallDrop : End Sub

'====================================
'Core Light System (PD v5.2 Appended)
'====================================


'-------------------------------
'System Fading Control Variables
'-------------------------------
'
'These variables tell whether various fading handlers should process fades or non-fades.  Thus, they disable
'fading of lights.  

' - Variable "Fading" controls whether lights fade or not (can use from options menu or change in script)
'   This controls the automated VPM lights and the lights set manually with "SetLight".
Dim Fading    : Fading = True

' - Variable "FadingF" controls whether lights fade or not that are manually set with "SetLightF".  
'   This is meant to be used to allow separate fading control for flashers

Dim FadingF	  : FadingF = True

' - Variable "FadingG" controls whether lights fade or not that are manually set with "SetLightG".  
'   This is meant to be used to allow separate fading control for GI Lights.

Dim FadingG	  : FadingG = True


'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

'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 
'---------------------------
' - These calls allow 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.
' - There are 3 sets of calls, each with a different control variable.  This is mainly designed to allow a table
'   author to have separate "Disable Fading" options for Lights, Flashers and GI Lights.

'These "LightOn" and "LightOff" aliases can be used with the SetLight commands instead of true and false (or 1 & 0).
dim LightOn, LightOff : LightOn = 1 : LightOff = 0  ' Define aliases for on & off

'SetLight
'--------
'
'Manually set lights in original tables or set extra lights in VPM tables such as flashers or GI lights.
'The variable "Fading" controls whether these lights will fade or not.
Sub SetLight(lightnumber, value)
	If Fading = true then
		lightnum(lightnumber) = abs(value) + 4
	Else
		lightnum(lightnumber) = abs(value)
	End If
End Sub

'SetLightF
'---------
'
'Manually set lights in original tables or set extra lights in VPM tables such as flashers or GI lights.
'The variable "Fading" controls whether these lights will fade or not.
Sub SetLightF(lightnumber, value)
	If FadingF = true then
		lightnum(lightnumber) = abs(value) + 4
	Else
		lightnum(lightnumber) = abs(value)
	End If
End Sub

'SetLightG
'---------
'
'Manually set lights in original tables or set extra lights in VPM tables such as flashers or GI lights.
'The variable "Fading" controls whether these lights will fade or not.
Sub SetLightG(lightnumber, value)
	If FadingG = true then
		lightnum(lightnumber) = abs(value) + 4
	Else
		lightnum(lightnumber) = abs(value)
	End If
End Sub


'*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
'Light Handler Code With Fade Control V5.2 by PD'
'*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
'
'These are the subroutine "Plugin" handlers for the UpdateLights sub.  They handle various types of lights and lighting
'conditions.  They are called from the "UpdateLights" sub-routine.  You should use the ones that match the type of light
'and lighting condition you are using.  More plugins can be added by a knowledgable user or for future updates.


'**********************
'Single Light Handlers
'**********************
'
'This set of handlers is for single (i.e. non-overlapping) lights.  There are handlers for Lights, Walls and Reels.
'Note the special handlers for when you have multiple lights for one control light value.

'----------------------------------------------------
'Adjustable Fading / Non-Fading Single Light Handlers
'----------------------------------------------------
'Set Fading = True for Fading (4-stage) operation or set Fading = False for Non-Fading (2-stage) operation.

'FadeLm
'------
'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

'FadeL
'-----
'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

'FadeWm
'------
'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"
'
'Note: the "off" condition should be provided by a non-dropping wall and the other stages should be set with their
'      top height just above that wall and their bottom height just below it and the wall set to drop.

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
	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

'FadeW
'-----
'Fades Wall Based Lights
'Example call: "FadeW 1, light1a,light1b,light1"
'
'Note: the "off" condition should be provided by a non-dropping wall and the other stages should be set with their
'      top height just above that wall and their bottom height just below it and the wall set to drop.

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
	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

'FadeRm
'------
'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"
'
'Reels should be contructed so that the frames within them are in the following order: off,bright,dim,medium
'Reels will fade if they contain and are set for 4 frames and will act as 2-state lights if they are set for 2 frames.
'They will also act as 2-state with the "fading" variable set to off.

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

'FadeR
'-----
'Fades Reel Based Lights
'Example call: "FadeR 1, light1"
'
'Reels should be contructed so that the frames within them are in the following order: off,bright,dim,medium
'Reels will fade if they contain and are set for 4 frames and will act as 2-state lights if they are set for 2 frames.
'They will also act as 2-state with the "fading" variable set to off.

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

'------------------------------------------
'Permanant Non-Fading Single Light Handlers
'------------------------------------------
'
'These lights will not fade regardless of the status of the "Fading" variable.

'NFade
'-----
'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

'LitL2
'-----
'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

'LitW2
'-----
'Handles 2-state (on/off only) Wall Based Lights
'Example call: "LitW2 1, light1"
'
'Note the off condition should be provided by including a non-dropping wall.  Stage3 should have its top height
'just above that wall and its bottom height just below it and the wall set to drop.
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

'**************************
'Overlapping Light Handlers
'**************************
'
'Note: These handlers are for overlapping lights (i.e. when two lights occupy the same cover (plastic or lens)
'      or when you have a light that should both flash an act as a GI light with varying brightness levels controlled
'      by two different control variables (such as a solenoid in a VPM table).
'      A good example would be a "light lens" on a table that contains both a flasher bulb and a regular (or GI) bulb.
'
'	There are currently no auto-label handlers for these calls since some may require changes to achieve different
'	effects.  For example, to combine a fading flasher with a non-fading GI Light using the MFadeL handler, you 
'   should make L2Stage1 and L2Stage2 the same as the shared off light value.  This will instantly darken that light
'   instead of fading it. The same can be applied to L1Stage1 and L1Stage2 for the reverse effect 
'   (i.e. a non-fading flasher, but fading GI) or both can be set that way to get overlapping non-fading lights.



'MFadeL
'------    
'Fades Multiple Overlapping Light Objects With Preferential Bias To One Light (e.g. Flashers over regular lights)
'The first set given is the preferred (i.e. brighter) set.
'Example call: "MFadeL 100, flasher2a, Flasher2b,Flasher2, 1, light1a,light1b,light1, light1off"
'               -Flasher set 2 controlled by 100 will show changes before light1 set controlled by 1
'Note that both sets will share a SINGLE common off color (create one light for both sets' off state).
'Notice the off state is the last argument (there is no off state listed for both sets as they share one off light)
'Care should be taken when selecting the colors for both sets
'
Sub MFadeL(lightnumIn1, L1stage1, L1stage2, L1stage3, lightnumIn2, L2stage1, L2stage2, L2stage3, 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
  If lightnum(lightnumIn1) = 6 or lightnum(lightnumIn1) = 0 Then 
	'Secondary Light Set only when primary set is OFF (case 6)
	Select Case lightnum(lightnumIn2)
		Case  5 : L2stage1.state = 0 : L2stage2.state = 0 : L2stage3.state = 0 : L2stage3.state = 1 : lightnum(lightnumIn2) = 7
		Case  4 : L2stage3.state = 0 : L2stage2.state = 0 : L2stage2.state = 1 : lightnum(lightnumIn2) = lightnum(lightnumIn2) - 1
		Case  3 : L2stage2.state = 0 : L2stage1.state = 0 : L2stage1.state = 1 : lightnum(lightnumIn2) = lightnum(lightnumIn2) - 1
		Case  2 : L2stage1.state = 0 : off.state = 0      : off.state = 1      : lightnum(lightnumIn2) = 6
		Case  1 : off.state      = 0 : L2stage3.state = 0 : L2stage3.state = 1 : lightnum(lightnumIn2) = -1
		Case  0 : L2stage3.state = 0 : off.state = 0 : off.state = 1 : lightnum(lightnumIn2) = -2
	End Select
   Else
		'Primary Set Otherwise
	Select Case lightnum(lightnumIn1)
		Case  5 : L1stage1.state = 0 : L1stage2.state = 0 : L1stage3.state = 0 : L1stage3.state = 1 : lightnum(lightnumIn1) = 7
		Case  4 : L1stage3.state = 0 : L1stage2.state = 0 : L1stage2.state = 1 : lightnum(lightnumIn1) = lightnum(lightnumIn1) - 1
		Case  3 : L1stage2.state = 0 : L1stage1.state = 0 : L1stage1.state = 1 : lightnum(lightnumIn1) = 6
		Case  2 : 'do nothing
		Case  1 : off.state      = 0 : L1stage3.state = 0 : L1stage3.state = 1 : lightnum(lightnumIn1) = 7
		Case  0 : 'do nothing
	End Select
	'Set secondary "Resume" states to either light up or stay off with the 1st set depending on their state when 1st set finishes
	Select Case lightnum(lightnumIn2)
		Case  7 : lightnum(lightnumIn2) = 5 ' Light Should Reset On When Primary Is Done So It's On Top
		Case  6 : lightnum(lightnumIn2) = 2 ' Light Off State Should Occur Next if Secondary is Off
		Case  5 : 'keep at 5 (Light On)
		Case  4 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  3 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  2 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  1 : 'keep at 1 (Light On)
		Case  0 : lightnum(lightnumIn2) = 0 ' Light Off (No Fade)
		Case -1 : lightnum(lightnumIn2) = 1 ' Light On  (No Fade)
		Case -2 : lightnum(lightnumIn2) = 0 ' Light Off (No Fade) 
	End Select
  End If
End Sub

'MFadeW
'------
'Fades Multiple Overlapping Wall Objects With Preferential Bias To One Light (e.g. Flashers over regular lights)
'The first set given is the preferred (i.e. brighter) set.
'
'Example call: "MFadeW 100, flasher2a, Flasher2b,Flasher2, 1, light1a,light1b,light1"
'               -Flasher set 2 controlled by 100 will show changes before light1 set controlled by 1
'Note that both sets will need to share a SINGLE common off state and that should be included on the table
'as a non-dropping wall mapped to show an off condition.
'

Sub MFadeW(lightnumIn1, L1stage1, L1stage2, L1stage3, lightnumIn2, L2stage1, L2stage2, L2stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest ; off state is a non-dropping wall you should have present
  If lightnum(lightnumIn1) = 6 or lightnum(lightnumIn1) = 0 Then 
	'Secondary Light Set only when primary set is OFF (case 6)
	L1stage1.IsDropped = True : L1stage2.IsDropped = True : L1stage3.IsDropped = True ' Drop All Primary Stages
	Select Case lightnum(lightnumIn2)
		Case  5 : L2stage1.IsDropped = True : L2stage2.IsDropped = True  : L2stage3.IsDropped = False : lightnum(lightnumIn2) = 7
		Case  4 : L2stage3.IsDropped = True : L2stage2.IsDropped = False : lightnum(lightnumIn2) = lightnum(lightnumIn2) - 1
		Case  3 : L2stage2.IsDropped = True : L2stage1.IsDropped = False : lightnum(lightnumIn2) = lightnum(lightnumIn2) - 1
		Case  2 : L2stage1.IsDropped = True : lightnum(lightnumIn2) = 6
		Case  1 : L2stage1.IsDropped = True : L2stage2.IsDropped = True : L2stage3.IsDropped = False : lightnum(lightnumIn2) = -1
		Case  0 : L2stage1.IsDropped = True : L2stage2.IsDropped = True : L2stage3.IsDropped = True  : lightnum(lightnumIn2) = -2
	End Select
   Else
	'Primary Set Otherwise
	L2stage1.IsDropped = True : L2stage2.IsDropped = True : L2stage3.IsDropped = True ' Drop All Secondary Stages
	Select Case lightnum(lightnumIn1)
		Case  5 : L1stage1.IsDropped = True : L1stage2.IsDropped = True  : L1stage3.IsDropped = False : lightnum(lightnumIn1) = 7
		Case  4 : L1stage3.IsDropped = True : L1stage2.IsDropped = False : lightnum(lightnumIn1) = lightnum(lightnumIn1) - 1
		Case  3 : L1stage2.IsDropped = True : L1stage1.IsDropped = False : lightnum(lightnumIn1) = 6
		Case  2 : 'do nothing
		Case  1 : L1stage1.IsDropped = True : L1stage2.IsDropped = True : L1stage3.IsDropped = False : lightnum(lightnumIn1) = 7
		Case  0 : 'do nothing
	End Select
	'Set secondary "Resume" states to either light up or stay off with the 1st set depending on their state when 1st set finishes
	Select Case lightnum(lightnumIn2)
		Case  7 : lightnum(lightnumIn2) = 5 ' Light Should Reset On When Primary Is Done So It's On Top
		Case  6 : lightnum(lightnumIn2) = 2 ' Light Off State Should Occur Next if Secondary is Off
		Case  5 : 'keep at 5 (Light On)
		Case  4 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  3 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  2 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  1 : 'keep at 1 (Light On)
		Case  0 : lightnum(lightnumIn2) = 0 ' Light Off (No Fade)
		Case -1 : lightnum(lightnumIn2) = 1 ' Light On  (No Fade)
		Case -2 : lightnum(lightnumIn2) = 0 ' Light Off (No Fade) 
	End Select
  End If
End Sub

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

'MFadeR
'------
'Fades Multiple Overlapping Lights Where a REEL object is desired to show both lights.
'Preferential Bias is applied to the first light or "L1" (e.g. Flashers over regular lights)
'Note that only one reel is used to show both lights so all frames should be in that one reel for both lights.
'
'Overlapping Light Reels should be constructed in the following frame order for fading:
'  		 OFF, L1Bright, L2Bright, L1Dim, L1 Medium, L2Dim, L2Medium
' or	 OFF, L1Bright, L2Bright for non-fading.
'
'Example call: "MFadeR 100, 1, DualLightReel"
'               -FlasherReel frames controlled by 100 will show changes before LightReel frames set controlled by 1

Sub MFadeR(lightnumIn1, lightnumIn2, reelname)
  If lightnum(lightnumIn1) = 6 or lightnum(lightnumIn1) = 0 Then 
	'Secondary Light Set only when primary set is OFF (case 6)
	Select Case lightnum(lightnumIn2)
		Case  5 : reelname.setvalue 2 : lightnum(lightnumIn2) = 7
		Case  4 : reelname.setvalue 6 : lightnum(lightnumIn2) = lightnum(lightnumIn2) - 1
		Case  3 : reelname.setvalue 5 : lightnum(lightnumIn2) = lightnum(lightnumIn2) - 1
		Case  2 : reelname.setvalue 0 : lightnum(lightnumIn2) = 6
		Case  1 : reelname.setvalue 2 : lightnum(lightnumIn2) = -1
		Case  0 : reelname.setvalue 0 : lightnum(lightnumIn2) = -2
	End Select
   Else
	'Primary Set Otherwise
	Select Case lightnum(lightnumIn1)
		Case  5 : reelname.setvalue 1 : lightnum(lightnumIn1) = 7
		Case  4 : reelname.setvalue 4 : lightnum(lightnumIn1) = lightnum(lightnumIn1) - 1
		Case  3 : reelname.setvalue 3 : lightnum(lightnumIn1) = 6
		Case  2 : 'do nothing
		Case  1 : reelname.setvalue 1 : lightnum(lightnumIn1) = 7
		Case  0 : 'do nothing
	End Select
	'Set secondary "Resume" states to either light up or stay off with the 1st set depending on their state when 1st set finishes
	Select Case lightnum(lightnumIn2)
		Case  7 : lightnum(lightnumIn2) = 5 ' Light Should Reset On When Primary Is Done So It's On Top
		Case  6 : lightnum(lightnumIn2) = 2 ' Light Off State Should Occur Next if Secondary is Off
		Case  5 : 'keep at 5 (Light On)
		Case  4 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  3 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  2 : lightnum(lightnumIn2) = 2 ' Light Off Next
		Case  1 : 'keep at 1 (Light On)
		Case  0 : lightnum(lightnumIn2) = 0 ' Light Off (No Fade)
		Case -1 : lightnum(lightnumIn2) = 1 ' Light On  (No Fade)
		Case -2 : lightnum(lightnumIn2) = 0 ' Light Off (No Fade) 
	End Select
  End If
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] for the Fading
' single light handlers for Light and Wall objects.  Non-Fading and Reel handlers are already short form and therefore
' not included.
'
' 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

'aFadeLm
'-------
'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

'aFadeL
'------
'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

'aFadeWm
'-------
'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"
'
'Note: the "off" condition should be provided by a non-dropping wall and the other stages should be set with their
'      top height just above that wall and their bottom height just below it and the wall set to drop.

Sub aFadeWm(lightnumIn, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest
	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

'aFadeW
'------
'Fades Wall Based Lights
'Example call: "aFadeW 1, light1"
'
'Note: the "off" condition should be provided by a non-dropping wall and the other stages should be set with their
'      top height just above that wall and their bottom height just below it and the wall set to drop.

Sub aFadeW(lightnumIn, stage3)
	'lightnum is the controlling light number ; stage 1 is dimmest, stage 2 is in-between ; stage 3 is brightest
	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

'****************************
'Special Light Handling Cases
'****************************
'
'Calls here will do special things like turn off all lights with one command (only call currently)


'AllLightsOff
'------------
'
'This will turn all lights using the PD Light System OFF.
'
'Note: You should probably turn off the LightControl (or LightControlO) timer when calling this 
'      and turn it back on when you want to resume (useful when combined with a pop-up settings menu)

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

