' Aliens 2.0 by Delta23 - A heavy mod of JP's Sorcerer
' VPX Sorcerer by JPSalas 2018, version 1.0.0

Option Explicit

'****** PuP Variables ******

Dim usePUP: Dim cPuPPack: Dim PuPlayer: Dim PUPStatus: PUPStatus=false ' dont edit this line!!!

'*************************** PuP Settings for this table ********************************

usePUP   = true               ' enable Pinup Player functions for this table
cPuPPack = "alienspupver"    ' name of the PuP-Pack / PuPVideos folder for this table

'//////////////////// PINUP PLAYER: STARTUP & CONTROL SECTION //////////////////////////

' This is used for the startup and control of Pinup Player

Sub PuPStart(cPuPPack)
    If PUPStatus=true then Exit Sub
    If usePUP=true then
        Set PuPlayer = CreateObject("PinUpPlayer.PinDisplay")
        If PuPlayer is Nothing Then
            usePUP=false
            PUPStatus=false
        Else
            PuPlayer.B2SInit "",cPuPPack 'start the Pup-Pack
            PUPStatus=true
        End If
    End If
End Sub

Sub pupevent(EventNum)
    if (usePUP=false or PUPStatus=false) then Exit Sub
    PuPlayer.B2SData "E"&EventNum,1  'send event to Pup-Pack
End Sub

' ******* How to use PUPEvent to trigger / control a PuP-Pack *******

' Usage: pupevent(EventNum)

' EventNum = PuP Exxx trigger from the PuP-Pack

' Example: pupevent 102

' This will trigger E102 from the table's PuP-Pack

' DO NOT use any Exxx triggers already used for DOF (if used) to avoid any possible confusion

'************ PuP-Pack Startup **************

PuPStart(cPuPPack) 'Check for PuP - If found, then start Pinup Player / PuP-Pack

Randomize

Const UseFlexDMD = 1    ' 1 is on

Const BallSize = 50
Const BallMass = 1.1

On Error Resume Next
ExecuteGlobal GetTextFile("controller.vbs")
If Err Then MsgBox "You need the controller.vbs in order to run this table, available in the vp10 package"
On Error Goto 0

LoadVPM "01550000", "S11.VBS", 3.26

Dim bsTrough, bsSaucer, dtBank, x, a, liarray

Const cGameName = "alienspupver"
'Const cGameName = "sorcr_l2"

Const UseSolenoids = 1
Const UseLamps = 0
Const UseGI = 0
Const UseSync = 0 'set it to 1 if the table runs too fast
Const HandleMech = 0

Dim VarHidden
If Table1.ShowDT = true then
    VarHidden = 1
    For each x in aReels
        x.Visible = 1
    Next
else
    VarHidden = 0
    For each x in aReels
        x.Visible = 0
    Next
    lrail.Visible = 0
    rrail.Visible = 0
end if

if B2SOn = true then VarHidden = 1

' Standard Sounds
Const SSolenoidOn = "fx_Solenoid"
Const SSolenoidOff = ""
Const SCoin = "fx_Coin"

Dim MusicCount : Dim MusicEnd
Sub Music_hit() liLockb.state=0:StopSound "Combat Drop":StopSound "Ripley's Rescue":StopSound "Sub Level 3"
    If MusicCount = 0 then PlayMusic "A-TRACK 1.mp3":End If
    If MusicCount = 1 then PlayMusic "A-TRACK 2.mp3":End If
	If MusicCount = 2 then PlayMusic "A-TRACK 3.mp3":End If
    If MusicCount = 3 then PlayMusic "A-TRACK 4.mp3":End If
	If MusicCount = 4 then PlayMusic "A-TRACK 5.mp3":End If
	If MusicCount = 5 then PlayMusic "A-TRACK 6.mp3":End If
	If MusicCount = 6 then PlayMusic "A-TRACK 7.mp3":End If
	If MusicCount = 7 then PlayMusic "A-TRACK 8.mp3":End If
	If MusicCount = 8 then PlayMusic "A-TRACK 9.mp3":End If
	MusicCount=Int(rnd*9)
End Sub

Sub Table1_MusicDone
    music_hit
End Sub

Sub SFX4_hit() PlaySound "Door" End Sub

Dim FX5Count
Sub SFX5_hit()
	If FX5Count = 0 then PlaySound "Random 24":End If
	If FX5Count = 1 then PlaySound "Random 22":End If
	If FX5Count = 2 then PlaySound "Random 25":End If
	If FX5Count = 3 then PlaySound "Random 26":End If
	If FX5Count = 4 then PlaySound "Random 27":End If
	FX5Count=(FX5Count+1) mod 5
End Sub

Sub SFX6_Hit() PlaySound "Motion Tracker":lightScanner.state=2:End Sub

Sub SFX6b_Hit() StopSound "Motion Tracker":lightScanner.state=0:End Sub 

Dim FX7Count
Sub SFX7_Hit() StopSound "Swoosh 1":StopSound "Ripley's Rescue":FX7Count=Int(rnd*15)
	If FX7Count = 0 then PlaySound "Sgt Apone 1":End If
	If FX7Count = 1 then PlaySound "Hudson 7":End If
	If FX7Count = 2 then PlaySound "Ripley 13":End If		
	If FX7Count = 3 then PlaySound "Bishop 1":End If	
	If FX7Count = 4 then PlaySound "Sgt Apone 2":End If
	If FX7Count = 5 then PlaySound "Ripley 24":End If
	If FX7Count = 6 then PlaySound "Random 7":End If
	If FX7Count = 7 then PlaySound "Sgt Apone 6":End If
	If FX7Count = 8 then PlaySound "Burke 3":End If
	If FX7Count = 9 then PlaySound "Sgt Apone 30":End If
	If FX7Count = 10 then PlaySound "Gorman 4":End If
	If FX7Count = 11 then PlaySound "Ripley 14":End If
	If FX7Count = 12 then PlaySound "Sgt Apone 29":End If
	If FX7Count = 13 then PlaySound "Sgt Apone 24":End If
	If FX7Count = 14 then PlaySound "Random 34":End If
End Sub

Dim FX8Count
Sub SFX8_Hit()
	If FX5Count = 0 then PlaySound "Swoosh 16":End If
	If FX5Count = 1 then PlaySound "Swoosh 14":End If
	FX5Count=(FX5Count+1) mod 2
End Sub

Dim FX9Count
Sub SFX9_hit()
	If liLock.state = 1 then PfON: vpmTimer.AddTimer 500, "PfOFF '":End If
	If FX9Count = 0 then PlaySound "Hit 11":End If
	If FX9Count = 1 then PlaySound "Hit 13":End If
	FX9Count=(FX9Count+1) mod 2
End Sub

Dim FX10Count, FX10Countb
Sub SFX10_hit() FX10Countb=Int(rnd*33)
	If FX10Count = 0 then 
		If FX10Countb = 0 then PlaySound "Comms 20":End If
		If FX10Countb = 1 then PlaySound "Sgt Apone 4":End If
		If FX10Countb = 2 then PlaySound "Sgt Apone 14":End If
		If FX10Countb = 3 then Playsound "Alien moving 3":End If
		If FX10Countb = 4 then PlaySound "Hicks 1":End If
		If FX10Countb = 5 then PlaySound "Gorman 3":End If
		If FX10Countb = 6 then Playsound "Alien 2":End If
		If FX10Countb = 7 then PlaySound "Sgt Apone 25":End If
		If FX10Countb = 8 then PlaySound "Comms 23":End If
		If FX10Countb = 9 then PlaySound "Comms 24":End If
		If FX10Countb = 10 then PlaySound "Alien 4":End If
		If FX10Countb = 11 then PlaySound "Sgt Apone 27":End If
		If FX10Countb = 12 then PlaySound "Random 6":End If
		If FX10Countb = 13 then PlaySound "Sgt Apone 5":End If
		If FX10Countb = 14 then PlaySound "Comms 29":End If
		If FX10Countb = 15 then PlaySound "Gorman 2":End If
		If FX10Countb = 16 then PlaySound "Gorman 1":End If	
		If FX10Countb = 17 then PlaySound "Alien moving 2":End If
		If FX10Countb = 18 then PlaySound "Sgt Apone 26":End If
		If FX10Countb = 19 then PlaySound "Hudson 31":End If
		If FX10Countb = 20 then PlaySound "Hudson 32":End If
		If FX10Countb = 21 then PlaySound "Sgt Apone 7":End If
		If FX10Countb = 22 then PlaySound "Comms 30":End If
		If FX10Countb = 23 then PlaySound "Hudson 14":End If
		If FX10Countb = 24 then PlaySound "Hudson 24":End If
		If FX10Countb = 25 then PlaySound "Hicks 2":End If
		If FX10Countb = 26 then PlaySound "Comms 27":End If
		If FX10Countb = 27 then PlaySound "Comms 28":End If
		If FX10Countb = 28 then PlaySound "Alien 3":End If
		If FX10Countb = 29 then PlaySound "Hudson 23":End If
		If FX10Countb = 30 then PlaySound "Vasquez 4":End If
		If FX10Countb = 31 then PlaySound "Alien moving 4":End If
		If FX10Countb = 32 then PlaySound "Sgt Apone 11":End If
	End If
	If FX10Count = 1 then PlaySound "No Sound" End If
	FX10Count=(FX10Count+1) mod 2
End Sub

Dim FX11Count, FX11Countb
Sub SFX11_hit() FX11Countb=Int(rnd*12)
	If FX11Count = 0 then
		If FX11Countb = 0 then PlaySound "Sgt Apone 15":End If
		If FX11Countb = 1 then PlaySound "Random 18":End If
		If FX11Countb = 2 then PlaySound "Comms 34":End If
		If FX11Countb = 3 then PlaySound "Hudson 33":End If
		If FX11Countb = 4 then PlaySound "Comms 35":End If
		If FX11Countb = 5 then PlaySound "Comms 32":End If
		If FX11Countb = 6 then PlaySound "Sgt Apone 9":End If
		If FX11Countb = 7 then PlaySound "Random 5":End If
		If FX11Countb = 8 then PlaySound "Hudson 5":End If
		If FX11Countb = 9 then PlaySound "Comms 21":End If
		If FX11Countb = 10 then PlaySound "Comms 36":End If
		If FX11Countb = 11 then PlaySound "Random 16":End If
	End If
	If FX11Count = 1 then PlaySound "No Sound" End If
	FX11Count=(FX11Count+1) mod 2
End Sub

Sub SFX12_hit() PlaySound "Hydraulic Door 2":
	If liLock.state = 1 then PfON: vpmTimer.AddTimer 500, "PfOFF '":End If
End Sub

Dim FX13Count
Sub SFX13_hit()
	If FX13Count = 0 then PlaySound "Swoosh 14":End If
	If FX13Count = 1 then PlaySound "Swoosh 16":End If
	FX13Count=(FX13Count+1) mod 2
End Sub

Dim FX14Count
Sub SFX14_hit() FX14Count=Int(rnd*3):StopSound "Motion Tracker":lightScanner.state=0
	If FX14Count = 0 then PlaySound "Random 10":End If
	If FX14Count = 1 then PlaySound "Random 11":End If
	If FX14Count = 2 then PlaySound "Random 12":End If
End Sub

Dim FX15Count
Sub SFX15_hit() FX15Count=Int(rnd*4)
	If FX15Count = 0 then PlaySound "Random 13":End If
	If FX15Count = 1 then PlaySound "Random 14":End If
	If FX15Count = 2 then PlaySound "Random 15":End If
	If FX15Count = 3 then PlaySound "Random 16":End If
End Sub

Dim FX16Count
Sub SFX16_Hit()
	If FX16Count = 0 then PlaySound "Vasquez 1":End If
	If FX16Count = 1 then PlaySound "Alien 7":End If
	If FX16Count = 2 then PlaySound "Hudson 16":End If
	FX16Count=(FX16Count+1) mod 3
End Sub

Dim FX17Count
Sub SFX17_Hit()
	If FX17Count = 0 then PlaySound "Hudson 12":End If
	If FX17Count = 1 then PlaySound "Alien 8":End If
	If FX17Count = 2 then PlaySound "Hudson 17":End If
	FX17Count=(FX17Count+1) mod 3
End Sub

Dim FX18Count
Sub SFX18_Hit() FX18Count=Int(rnd*11)
	If FX18Count = 0 then PlaySound "Comms 1":End If
	If FX18Count = 1 then PlaySound "Comms 2":End If
	If FX18Count = 2 then PlaySound "Comms 18":End If
	If FX18Count = 3 then PlaySound "Alien 8":End If
	If FX18Count = 4 then PlaySound "Comms 10":End If
	If FX18Count = 5 then PlaySound "Comms 3":End If
	If FX18Count = 6 then PlaySound "Comms 4":End If
	If FX18Count = 7 then PlaySound "Comms 33":End If
	If FX18Count = 8 then PlaySound "Random 20":End If
	If FX18Count = 9 then PlaySound "Random 21":End If
	If FX18Count = 10 then PlaySound "Ripley 12":End If
End Sub

Dim FX19Count
Sub SFX19_Hit() FX19Count=Int(rnd*10)
	If FX19Count = 0 then PlaySound "Comms 5":End If
	If FX19Count = 1 then PlaySound "Comms 6":End If
	If FX19Count = 2 then PlaySound "Comms 15":End If
	If FX19Count = 3 then PlaySound "Alien 9":End If
	If FX19Count = 4 then PlaySound "Sgt Apone 28":End If
	If FX19Count = 5 then PlaySound "Comms 7":End If
	If FX19Count = 6 then PlaySound "Comms 8":End If
	If FX19Count = 7 then PlaySound "Comms 17":End If
	If FX19Count = 8 then PlaySound "Random 19":End If
	If FX19Count = 9 then PlaySound "Random 17":End If
End Sub

Dim FX20Count
Sub SFX20_Hit() StopSound "Motion Tracker":lightScanner.state=0
	If liLock.state=1 then
	If FX20Count = 0 then EndMusic:GiOff:RedGiOFF:PlayMusic "A-MBALL 1.mp3":BlueGiON:vpmTimer.AddTimer 12000, "BluePfON '":vpmTimer.AddTimer 12500, "GiOn '":End If
	If FX20Count = 1 then EndMusic:GiOff:RedGiOFF:PlayMusic "A-MBALL 2.mp3":BlueGiON:vpmTimer.AddTimer 12000, "BluePfON '":vpmTimer.AddTimer 12500, "GiOn '":End If
	If FX20Count = 2 then EndMusic:GiOff:RedGiOFF:PlayMusic "A-MBALL 3.mp3":BlueGiON:vpmTimer.AddTimer 12000, "BluePfON '":vpmTimer.AddTimer 12500, "GiOn '":End If
	FX20Count=Int(rnd*3)
	End If
End Sub

Sub SFX21_Hit() LiLock.state=0: GiOff: End Sub

Dim Kicker2Count
Sub Kicker2_hit() Kicker2Count=Int(rnd*18):PlaySound "fx_kicker_enter":PlaySound "Interference 3":GiOff:RedGiOFF:RedPfOFF:GreenGiON:GreenPfON
	Kicker2.TimerInterval = 6000
	Kicker2.TimerEnabled =1
	If liLockb.state=1 then PlaySound "No Sound" End If
	If liLockb.state=0 then
		If Kicker2Count = 0 then PlaySound "Ripley 10":End If
		If Kicker2Count = 1 then PlaySound "Burke 1":End If
		If Kicker2Count = 2 then PlaySound "Hicks 4":End If
		If Kicker2Count = 3 then PlaySound "Hudson 35":End If
		If Kicker2Count = 4 then PlaySound "Ripley 19":End If
		If Kicker2Count = 5 then PlaySound "Random 4":End If
		If Kicker2Count = 6 then PlaySound "Hudson 1":End If
		If Kicker2Count = 7 then PlaySound "Hudson 26":End If
		If Kicker2Count = 8 then PlaySound "Ripley 15":End If
		If Kicker2Count = 9 then PlaySound "Hudson 3":End If
		If Kicker2Count = 10 then PlaySound "Hudson 27":End If
		If Kicker2Count = 11 then PlaySound "Bishop 2":End If
		If Kicker2Count = 12 then PlaySound "Hudson 28":End If
		If Kicker2Count = 13 then PlaySound "Ripley 22":End If
		If Kicker2Count = 14 then PlaySound "Ripley 11":End If
		If Kicker2Count = 15 then PlaySound "Vasquez 3":End If
		If Kicker2Count = 16 then PlaySound "Random 1":End If
		If Kicker2Count = 17 then PlaySound "Ripley 18":End If
	End If
End Sub
	
Sub Kicker2_Timer
	Kicker2.Kick 130,10:FlashersON:PlaySound"fx_kicker":PlaySound"Interference out":vpmTimer.AddTimer 1000, "GiOn '":GreenGiOFF:GreenPfOFF
	Kicker2.TimerEnabled = 0
End Sub

Sub Kicker3_Hit() PlaySound "Sentry gun activated":vpmTimer.AddTimer 1500, "Kicker3.Kick 260,3'":End sub

Dim Kicker4Count
Sub Kicker4_Hit() Kicker4Count=Int(rnd*6):vpmTimer.AddTimer 1500, "Kicker4.Kick 250,8'"
	If Kicker4Count = 0 then PlaySound "Hicks 13":End If
	If Kicker4Count = 1 then PlaySound "Hudson 38":End If
	If Kicker4Count = 2 then PlaySound "Hicks 15":End If
	If Kicker4Count = 3 then PlaySound "Hudson 39":End If
	If Kicker4Count = 4 then PlaySound "Hicks 14":End If
	If Kicker4Count = 5 then PlaySound "Sentry guns out":End If
End Sub

Dim Kicker5Count
Sub Kicker5_Hit() Kicker5Count=Int(rnd*8):PlaySound "fx_kicker_enter"
	Kicker5.TimerInterval = 2500
	Kicker5.TimerEnabled = 1
	If Kicker5Count = 0 then PlaySound "Hicks 5":End If
	If Kicker5Count = 1 then PlaySound "Ripley 6":End If
	If Kicker5Count = 2 then PlaySound "Random 35":End If
	If Kicker5Count = 3 then PlaySound "Ripley 2":End If
	If Kicker5Count = 4 then PlaySound "Hicks 3":End If
	If Kicker5Count = 5 then PlaySound "Medic 1":End If
	If Kicker5Count = 6 then PlaySound "Hudson 25":End If
	If Kicker5Count = 7 then PlaySound "Medic 2":End If
End Sub

Sub Kicker5_Timer
	Kicker5.Kick 0,50,1:PlaySound "fx_kicker":PlaySound "Motion Tracker":PlaySound "Pneumatic":lightScanner.state=2:FlashersON
	Kicker5.TimerEnabled = 0
End Sub

Dim Kicker6Count
Sub Kicker6_Hit() PlaySound "fx_kicker_enter"
	Kicker6.TimerInterval = 2500
	Kicker6.TimerEnabled = 1
	If Kicker6Count = 0 then PlaySound "Hicks 9":End If
	If Kicker6Count = 1 then PlaySound "Comms 9":End If
	If Kicker6Count = 2 then PlaySound "Sgt Apone 10":End If
	If Kicker6Count = 3 then PlaySound "Hudson 22":End If
	If Kicker6Count = 4 then PlaySound "Sgt Apone 13":End If
	If Kicker6Count = 5 then PlaySound "Comms 12":End If
	If Kicker6Count = 6 then PlaySound "Hudson 9":End If
	If Kicker6Count = 7 then PlaySound "Sgt Apone 8":End If
	If Kicker6Count = 8 then PlaySound "Comms 13":End If
	Kicker6Count=Int(rnd*9)
End Sub

Sub Kicker6_Timer
	Kicker6.Kick 180,10:PlaySound "fx_kicker"
	Kicker6.TimerEnabled = 0
End Sub

Sub Wall100_hit() PlaySound "Sentry gun 4":End Sub

'Sub Gate1_Hit() PlaySound "fx_gate":End Sub

'Sub Gate2_Hit() PlaySound "fx_gate":End Sub

Sub Gate3_Hit() PlaySound "Swoosh 12":End Sub

'Sub Gate4_Hit() PlaySound "fx_gate":End Sub

Sub swGate5_Hit() PlaySound "fx_gate":End Sub

Sub Gate6_Hit() PlaySound "Sentry gun activated":GiOff:LiSentCon.state=1:liTargets.state=1:End Sub

Sub Gate7_Hit() Playsound "fx_kicker":GiOn:LiSentCon.state=0:liTargets.state=0:End Sub

'Sub Gate8_Hit() PlaySound "fx_gate":End Sub

Sub Gate9_Hit() PlaySound "Pneumatic":End Sub

'Sub Gate10_Hit() PlaySound "fx_gate":End Sub

Sub Gate11_Hit() PlaySound "Door":StopSound "Motion Tracker":lightScanner.state=0:End Sub

'Sub Gate12_Hit() PlaySound "fx_gate":End Sub

'Sub Gate13_Hit() PlaySound "fx_gate":End Sub

Dim Gate14Count
Sub Gate14_Hit()
	If Gate14Count = 0 then PlaySound "No Sound":End If
	If Gate14Count = 1 then PlaySound "No Sound":End If
	If Gate14Count = 2 then PlaySound "Loader steps":End If
	If Gate14Count = 3 then PlaySound "No Sound":End If
	If Gate14Count = 4 then PlaySound "Emergency alert":GiOFF:vpmTimer.AddTimer 17000, "GiOn '":RedGiON:vpmTimer.AddTimer 16000, "RedGiOFF '":RedPfOn:vpmTimer.AddTimer 16000, "RedPfOFF '":End If
	Gate14Count=(Gate14Count+1) mod 5
End Sub

'************
' Table init.
'************

Sub table1_Init
    vpmInit me
    With Controller
        .GameName = cGameName
        If Err Then MsgBox "Can't start Game" & cGameName & vbNewLine & Err.Description:Exit Sub
        .SplashInfoLine = "ALIENS 2.0 - Original 2021" & vbNewLine & "VPX table by Delta23"
        .HandleKeyboard = 0
        .ShowTitle = 0
        .ShowDMDOnly = 1
        .ShowFrame = 0
        .HandleMechanics = 0
        .Hidden = 1 'VarHidden
        .Games("sorcr_l1").Settings.Value("rol") = 0 '1= rotated display, 0= normal
		.Games("sorcr_l1").Settings.Value("sound") = 0
        '.SetDisplayPosition 0,0, GetPlayerHWnd 'restore dmd window position
        On Error Resume Next
        Controller.SolMask(0) = 0
        vpmTimer.AddTimer 2000, "Controller.SolMask(0)=&Hffffffff'" 'ignore all solenoids - then add the Timer to renable all the solenoids after 2 seconds
        Controller.Run GetPlayerHWnd
        On Error Goto 0
    End With

    ' Nudging
    vpmNudge.TiltSwitch = 1
    vpmNudge.Sensitivity = 3
    vpmNudge.TiltObj = Array(Bumper1, Bumper2, Bumper3, LeftSlingshot, RightSlingshot)

    ' Trough
    Set bsTrough = New cvpmBallStack
    With bsTrough
        .Initsw 28, 30, 29, 0, 0, 0, 0, 0
        .InitKick BallRelease, 90, 4
        .InitExitSnd SoundFX("fx_ballrel", DOFContactors), SoundFX("fx_Solenoid", DOFContactors)
        .Balls = 2
    End With

    ' Saucers
    Set bsSaucer = New cvpmBallStack
    With bsSaucer
        .InitSaucer sw37, 37, 180, 12
        .InitExitSnd SoundFX("fx_kicker", DOFContactors), SoundFX("fx_Solenoid", DOFContactors)
        .KickZ = 1
        .KickForceVar = 2
    End With

    ' Drop targets
    Set dtBank = New cvpmDropTarget
    With dtBank
        .InitDrop Array(sw34, sw35, sw36), Array(34, 35, 36)
        .initsnd SoundFX("", DOFDropTargets), SoundFX("fx_resetdrop", DOFContactors)
        .CreateEvents "dtBank"
    End With

    ' Main Timer init
    PinMAMETimer.Interval = PinMAMEInterval
	PinMAMETimer.Enabled = 1: PlaySound "Combat Drop"

    ' Turn on Gi
    GiOn
	
	' initalise the FlexDMD display
    If UseFlexDMD Then FlexDMD_Init
	
	
End Sub

Sub table1_Paused:Controller.Pause = 1:End Sub
Sub table1_unPaused:Controller.Pause = 0:End Sub
Sub table1_exit
	Controller.stop
	If UseFlexDMD then
		If Not FlexDMD is Nothing Then 
			FlexDMD.Show = False
			FlexDMD.Run = False
			FlexDMD = NULL
		End if
	End if
End Sub

'**********
' Keys
'**********

Sub table1_KeyDown(ByVal Keycode)
    If keycode = LeftTiltKey Then Nudge 90, 5:PlaySound SoundFX("fx_nudge", 0), 0, 1, -0.1, 0.25
    If keycode = RightTiltKey Then Nudge 270, 5:PlaySound SoundFX("fx_nudge", 0), 0, 1, 0.1, 0.25
    If keycode = CenterTiltKey Then Nudge 0, 6:PlaySound SoundFX("fx_nudge", 0), 0, 1, 0, 0.25
    If keycode = PlungerKey Then PlaySound "fx_PlungerPull", 0, 1, 0.1, 0.25:Plunger.Pullback
    If vpmKeyDown(keycode)Then Exit Sub
    If keycode = RightFlipperKey Then Controller.Switch(44) = 1
End Sub

Sub table1_KeyUp(ByVal Keycode)
    If vpmKeyUp(keycode)Then Exit Sub
    If keycode = RightFlipperKey Then Controller.Switch(44) = 0
    If keycode = PlungerKey Then PlaySound "fx_plunger", 0, 1, 0.1, 0.25:Plunger.Fire
End Sub

'*********
' Switches
'*********

' Slings
Dim LStep, RStep, LeftCount, RightCount

Sub LeftSlingShot_Slingshot
	If liLockb.state = 1 then PfON: vpmTimer.AddTimer 500, "PfOFF '":End If
	LeftCount = Int (rnd*4)
    If LeftCount = 0 then PlaySound "Pulse Rifle 1":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
	If LeftCount = 1 then PlaySound "Pulse Rifle 3":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
	If LeftCount = 2 then PlaySound "Pulse Rifle 5":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
	If LeftCount = 3 then PlaySound "Pulse Rifle 7":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
    LeftSling4.Visible = 1
    Lemk.RotX = 26
    LStep = 0
    vpmTimer.PulseSw 32
    LeftSlingShot.TimerEnabled = 1
End Sub

Sub LeftSlingShot_Timer
    Select Case LStep
        Case 1:LeftSling4.Visible = 0:LeftSLing3.Visible = 1:Lemk.RotX = 14
        Case 2:LeftSLing3.Visible = 0:LeftSLing2.Visible = 1:Lemk.RotX = 2
        Case 3:LeftSLing2.Visible = 0:Lemk.RotX = -20:LeftSlingShot.TimerEnabled = 0
    End Select
    LStep = LStep + 1
End Sub

Sub RightSlingShot_Slingshot
	If liLockb.state = 1 then PfON: vpmTimer.AddTimer 500, "PfOFF '":End If
	RightCount = Int (rnd*4)
    If RightCount = 0 then PlaySound "Pulse Rifle 2":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
	If RightCount = 1 then PlaySound "Pulse Rifle 4":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
	If RightCount = 2 then PlaySound "Pulse Rifle 6":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
	If RightCount = 3 then PlaySound "Pulse Rifle 8":PlaySound SoundFX("fx_slingshot", DOFContactors), 0, 1, -0.05, 0.05:End If
    RightSling4.Visible = 1
    Remk.RotX = 26
    RStep = 0
    vpmTimer.PulseSw 33
    RightSlingShot.TimerEnabled = 1
End Sub

Sub RightSlingShot_Timer
    Select Case RStep
        Case 1:RightSLing4.Visible = 0:RightSLing3.Visible = 1:Remk.RotX = 14
        Case 2:RightSLing3.Visible = 0:RightSLing2.Visible = 1:Remk.RotX = 2
        Case 3:RightSLing2.Visible = 0:Remk.RotX = -20:RightSlingShot.TimerEnabled = 0
    End Select
    RStep = RStep + 1
End Sub

' Rubbers, sound is done in the collection

Sub sw39_Hit:vpmTimer.PulseSw 39:End Sub
Sub sw40_Hit:vpmTimer.PulseSw 40:End Sub
Sub sw42_Hit:vpmTimer.PulseSw 42:End Sub
Sub sw43_Hit:vpmTimer.PulseSw 43:End Sub

' Bumpers
Sub Bumper1_Hit:vpmTimer.PulseSw 21:PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:End Sub
Sub Bumper2_Hit:vpmTimer.PulseSw 23:PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.025:End Sub
Sub Bumper3_Hit:vpmTimer.PulseSw 22:PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:End Sub
Sub MiniBumper1_Hit:vpmTimer.PulseSw 21:PlaySound "Sentry gun 1":PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:End Sub
Sub MiniBumper2_Hit:vpmTimer.PulseSw 21:PlaySound "Sentry gun 1":PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:End Sub
Sub MiniBumper3_Hit:vpmTimer.PulseSw 22:PlaySound "Sentry gun 1":PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:End Sub
Sub MiniBumper4_Hit:vpmTimer.PulseSw 22:PlaySound "Sentry gun 1":PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:End Sub

Sub MiniBumperKick_Hit():vpmTimer.PulseSw 23:PlaySound "Hit 2":PlaySound SoundFX("fx_bumper", DOFContactors), 0, 1, -0.05:FlashersON:End Sub

Sub MiniBumperKick2_Hit:vpmTimer.PulseSw 23:PlaySound "Pneumatic":FlashersON:End Sub

' Drain & Saucers
Sub Drain_Hit:PuPEvent 100:Playsound "fx_drain":PlaySound "Alien 6":EndMusic:GiOFF:StopSound "Emergency alert":RedGiOFF:RedPfOFF:BlueGiOFF:BluePfOFF:bsTrough.AddBall Me
	If liLockb.state=0 then EndMusic:PlaySound "Swoosh 1" else liLockb.state=0:PlaySound "Random 2":PlayMusic "A-FILL.mp3":vpmTimer.AddTimer 4000, "GiOn '"End If
End Sub

Sub sw37_Hit: PlaySound "Hydraulic Door": liLock.state=1: liLockb.state=1: PlaySound "fx_kicker_enter", 0, 1, 0.05: bsSaucer.AddBall 0: End Sub

' Rollovers
Sub sw38_Hit:Controller.Switch(38) = 1:PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw38_UnHit:Controller.Switch(38) = 0:End Sub

Sub sw31_Hit:Controller.Switch(31) = 1:PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw31_UnHit:Controller.Switch(31) = 0:End Sub

Sub sw24_Hit:Controller.Switch(24) = 1:PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw24_UnHit:Controller.Switch(24) = 0:End Sub

Sub sw25_Hit:Controller.Switch(25) = 1:PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw25_UnHit:Controller.Switch(25) = 0:End Sub

Sub sw26_Hit:Controller.Switch(26) = 1:PlaySound "HUD Interface 2":PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw26_UnHit:Controller.Switch(26) = 0:End Sub

Sub sw27_Hit:Controller.Switch(27) = 1:PlaySound "HUD Interface 2":PlaySound "fx_sensor", 0, 1, pan(ActiveBall): End Sub
Sub sw27_UnHit:Controller.Switch(27) = 0:End Sub

Sub sw17_Hit:Controller.Switch(17) = 1:PlaySound "HUD Interface 2":PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw17_UnHit:Controller.Switch(17) = 0:End Sub

Sub sw18_Hit:Controller.Switch(18) = 1:PlaySound "HUD Interface 2":PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw18_UnHit:Controller.Switch(18) = 0:End Sub

Sub sw19_Hit:Controller.Switch(19) = 1:PlaySound "HUD Interface 2":PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw19_UnHit:Controller.Switch(19) = 0:End Sub

Sub sw20_Hit:Controller.Switch(20) = 1:PlaySound "HUD Interface 2":PlaySound "fx_sensor", 0, 1, pan(ActiveBall):End Sub
Sub sw20_UnHit:Controller.Switch(20) = 0:End Sub

' Droptargets (sound only)
'Sub aDroptargets_Hit(idx):PlaySound "Alien 3":PlaySound SoundFX("fx_droptarget", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw34_Hit:PlaySound SoundFX("fx_droptarget", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw35_Hit:PlaySound SoundFX("fx_droptarget", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw36_Hit:PlaySound SoundFX("fx_droptarget", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub

' Spinners
Sub sw9_Spin:vpmTimer.PulseSw 9:PlaySound "fx_spinner", 0, 1, -0.05:End Sub
Sub sw16_Spin:vpmTimer.PulseSw 16:PlaySound "fx_spinner", 0, 1, 0.05:End Sub

'Targets
Sub sw10_Hit:vpmTimer.PulseSw 10:PlaySound "HUD Interface 7":PlaySound SoundFX("fx_target", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw11_Hit:vpmTimer.PulseSw 11:PlaySound "HUD Interface 7":PlaySound SoundFX("fx_target", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw12_Hit:vpmTimer.PulseSw 12:PlaySound "HUD Interface 7":PlaySound SoundFX("fx_target", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw13_Hit:vpmTimer.PulseSw 13:PlaySound "HUD Interface 7":PlaySound SoundFX("fx_target", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw14_Hit:vpmTimer.PulseSw 14:PlaySound "HUD Interface 7":PlaySound SoundFX("fx_target", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub
Sub sw15_Hit:vpmTimer.PulseSw 15:PlaySound "HUD Interface 7":PlaySound SoundFX("fx_target", DOFDropTargets), 0, 1, pan(ActiveBall):End Sub

'*********
'Solenoids
'*********

Sub flArray()
	liArray = Array(F5,F5a,F6,F6a,F7,F7a)
	For a = 0 to 5
	liArray(a).State=0
	Next
End Sub


SolCallback(1) = "bsTrough.SolIn"   ' Outhole
SolCallback(2) = "bsTrough.SolOut"  ' Ball Release
SolCallback(3) = "bsSaucer.SolOut"  ' Multi-Ball Eject
SolCallback(4) = "dtBank.SolDropUp" ' Three Bank Reset
SolCallback(5) = "SetLamp 105,"     ' Demon Backdrop Flasher
SolCallback(6) = "SetLamp 106," 	' Drop Target Flasher
SolCallback(7) = "SetLamp 107,"     ' Center Target Bank Flashers
SolCallback(8) = "SetLamp 108,"     ' Back Panel Eye Flashers
SolCallback(11) = "SolGi"           ' General Illumination
'SolCallback(14)     = ""
SolCallback(15) = "vpmSolSound""Bonus""," ' Bell
'SolCallback(16)
'SolCallback(17)     = ""  ' Left Sling
'SolCallback(18)     = "" ' Right Sling
'SolCallback(19)	  = ""  				    ' Left Jet Bumper
'SolCallback(20)	  = ""    				' Bottom Jet Bumper
'SolCallback(21)	  = ""    				' Right Jet Bumper
SolCallback(23) = "vpmNudge.SolGameOn"

'**************
' Flipper Subs
'**************

SolCallback(sLRFlipper) = "SolRFlipper"
SolCallback(sLLFlipper) = "SolLFlipper"

Sub SolLFlipper(Enabled)
    If Enabled Then
        PlaySound SoundFX("fx_flipperup", DOFFlippers), 0, 1, -0.1, 0.05
        LeftFlipper.RotateToEnd
        LeftFlipper1.RotateToEnd
    Else
        PlaySound SoundFX("fx_flipperdown", DOFFlippers), 0, 1, -0.1, 0.05
        LeftFlipper.RotateToStart
        LeftFlipper1.RotateToStart
    End If
End Sub

Sub SolRFlipper(Enabled)
    If Enabled Then
        PlaySound SoundFX("fx_flipperup", DOFFlippers), 0, 1, 0.1, 0.05
        RightFlipper.RotateToEnd
		RightFlipper1.RotateToEnd
    Else
        PlaySound SoundFX("fx_flipperdown", DOFFlippers), 0, 1, 0.1, 0.05
        RightFlipper.RotateToStart
		RightFlipper1.RotateToStart
    End If
End Sub

Sub LeftFlipper_Collide(parm)
    PlaySound "fx_rubber_flipper", 0, parm / 10, -0.1, 0.25
End Sub

Sub RightFlipper_Collide(parm)
    PlaySound "fx_rubber_flipper", 0, parm / 10, 0.1, 0.25
End Sub

Sub LeftFlipper1_Collide(parm)
    PlaySound "fx_rubber_flipper", 0, parm / 10, -0.1, 0.25
End Sub

Sub RightFlipper1_Collide(parm)
    PlaySound "fx_rubber_flipper", 0, parm / 10, -0.1, 0.25
End Sub

'*****************
'   Gi Effects
'*****************

Sub SolGi(enabled)
   If Enabled Then
		PlaySound "fx_SolenoidOn", 0, 0.1
        GiOff
    Else
		PlaySound "fx_SolenoidOff", 0, 0.1
        GiOn
    End If
End Sub

Sub GiON
    For each x in aGiLights
        x.State = 1
    Next
End Sub

Sub GiOFF
    For each x in aGiLights
        x.State = 0
    Next
End Sub

Sub PfON
	For each x in aPFlights
		x.State = 2
	Next
End Sub

Sub PfOFF
	For each x in aPFlights
		x.State = 0
	Next
End Sub

Sub RedGiON
	For each x in aRedlights
		x.State = 2
	Next
End Sub

Sub RedGiOFF
	For each x in aRedlights
		x.State = 0
	Next
End Sub

Sub RedPfON
	For each x in aPFlightsRed
		x.State = 2
	Next
End Sub

Sub RedPfOFF
	For each x in aPFlightsRed
		x.State = 0
	Next
End Sub

Sub GreenGiON
	For each x in aGreenlights
		x.state = 1
	Next
End Sub

Sub GreenGiOFF
	For each x in aGreenlights
		x.State = 0
	Next
End Sub

Sub GreenPfON
	For each x in aPFlightsGreen
		x.State = 1
	Next
End Sub

Sub GreenPfOFF
	For each x in aPFlightsGreen
		x.State = 0
	Next
End Sub

Sub BlueGiON
	For each x in aBluelights
		x.Duration 0,10000,1
	Next
End Sub

Sub BlueGiOFF
	For each x in aBluelights
		x.State = 0
	Next
End Sub

Sub BluePfON
	For each x in aPFlightsBlue
		x.State = 1
	Next
End Sub

Sub BluePfOFF
	For each x in aPFlightsBlue
		x.State = 0
	Next
End Sub

Sub FlashersON
	For each x in aFlashers
		x.duration 1, 150, 0
	Next
End Sub

Sub FlashersOFF
	For each x in aFlashers
		x.State = 0
	Next
End Sub

'***************************************************
'       JP's VP10 Fading Lamps & Flashers
'       Based on PD's Fading Light System
' SetLamp 0 is Off
' SetLamp 1 is On
' fading for non opacity objects is 4 steps
'***************************************************

Dim LampState(200), FadingLevel(200)
Dim FlashSpeedUp(200), FlashSpeedDown(200), FlashMin(200), FlashMax(200), FlashLevel(200), FlashRepeat(200)

InitLamps()             ' turn off the lights and flashers and reset them to the default parameters
LampTimer.Interval = 10 ' lamp fading speed
LampTimer.Enabled = 1

' Lamp & Flasher Timers

Sub LampTimer_Timer()
    Dim chgLamp, num, chg, ii
    chgLamp = Controller.ChangedLamps
    If Not IsEmpty(chgLamp)Then
        For ii = 0 To UBound(chgLamp)
            LampState(chgLamp(ii, 0)) = chgLamp(ii, 1)       'keep the real state in an array
            FadingLevel(chgLamp(ii, 0)) = chgLamp(ii, 1) + 4 'actual fading step
        Next
    End If
	
    If VarHidden or UseFlexDMD Then
        UpdateLeds
    End If
    UpdateLamps
    RollingUpdate
End Sub

Sub UpdateLamps()

    'backdrop lights
    NFadeT 1, li1, "Game Over"
    NFadeT 2, li2, "Match"
    NFadeT 3, li3, "TILT"
    NFadeTm 4, li4, "High Score"
    NFadeT 4, li4a, "To Date"
    NFadeT 5, li5, "Shoot Again"
    NFadeT 6, li6, "Ball in Play"

    ' playfield lights
    NFadeL 6, li6
    NFadeL 7, li7
    NFadeL 8, li8
    NFadeL 9, li9
    NFadeL 10, li10
    NFadeL 11, li11
    NFadeL 12, li12
    NFadeL 13, li13
    NFadeL 14, li14
    NFadeL 15, li15
    NFadeL 16, li16
    NFadeL 17, li17
    NFadeL 18, li18
    NFadeL 19, li19
    NFadeL 20, li20
    NFadeL 21, li21
    NFadeL 22, li22
    NFadeL 23, li23
    NFadeL 24, li24
    NFadeL 25, li25
    NFadeL 26, li26
    NFadeL 27, li27
    NFadeL 28, li28
    NFadeL 29, li29
    NFadeL 30, li30
    NFadeL 31, li31
    NFadeL 32, li32
    NFadeL 33, li33
    NFadeL 34, li34
    NFadeL 35, li35
    NFadeL 36, li36
    NFadeL 37, li37
    NFadeL 38, li38
    NFadeL 39, li39
    NFadeL 40, li40
    NFadeL 41, li41
    NFadeL 42, li42
    NFadeL 43, li43
    NFadeL 44, li44
    NFadeL 45, li45
    NFadeL 46, li46
    NFadeL 47, li47
    NFadeL 48, li48
    NFadeL 49, li49
    NFadeL 50, li50
    NFadeL 51, li51
    NFadeL 52, li52
    NFadeL 53, li53
    NFadeL 54, li54
    NFadeL 55, li55
	
    'flashers
    NFadeL 105, F5a
	NFadeL 105, F5
    NFadeL 106, F6a
    NFadeL 106, F6
    NFadeL 107, F7a
    NFadeL 107, F7
    Flashm 108, F8a
    Flash 108, F8b
End Sub

' div lamp subs

Sub InitLamps()
    Dim x
    For x = 0 to 200
        LampState(x) = 0        ' current light state, independent of the fading level. 0 is off and 1 is on
        FadingLevel(x) = 4      ' used to track the fading state
        FlashSpeedUp(x) = 0.5   ' faster speed when turning on the flasher
        FlashSpeedDown(x) = 0.25 ' slower speed when turning off the flasher
        FlashMax(x) = 1         ' the maximum value when on, usually 1
        FlashMin(x) = 0         ' the minimum value when off, usually 0
        FlashLevel(x) = 0       ' the intensity of the flashers, usually from 0 to 1
        FlashRepeat(x) = 20     ' how many times the flash repeats
    Next
End Sub

Sub AllLampsOff
    Dim x
    For x = 0 to 200
        SetLamp x, 0
    Next
End Sub

Sub SetLamp(nr, value)
    If value <> LampState(nr)Then
        LampState(nr) = abs(value)
        FadingLevel(nr) = abs(value) + 4
    End If
End Sub

' Lights: used for VP10 standard lights, the fading is handled by VP itself

Sub NFadeL(nr, object)
    Select Case FadingLevel(nr)
        Case 4:object.state = 0:FadingLevel(nr) = 0
        Case 5:object.state = 1:FadingLevel(nr) = 1
    End Select
End Sub

Sub NFadeLm(nr, object) ' used for multiple lights
    Select Case FadingLevel(nr)
        Case 4:object.state = 0
        Case 5:object.state = 1
    End Select
End Sub

'Lights, Ramps & Primitives used as 4 step fading lights
'a,b,c,d are the images used from on to off

Sub FadeObj(nr, object, a, b, c, d)
    Select Case FadingLevel(nr)
        Case 4:object.image = b:FadingLevel(nr) = 6                   'fading to off...
        Case 5:object.image = a:FadingLevel(nr) = 1                   'ON
        Case 6, 7, 8:FadingLevel(nr) = FadingLevel(nr) + 1            'wait
        Case 9:object.image = c:FadingLevel(nr) = FadingLevel(nr) + 1 'fading...
        Case 10, 11, 12:FadingLevel(nr) = FadingLevel(nr) + 1         'wait
        Case 13:object.image = d:FadingLevel(nr) = 0                  'Off
    End Select
End Sub

Sub FadeObjm(nr, object, a, b, c, d)
    Select Case FadingLevel(nr)
        Case 4:object.image = b
        Case 5:object.image = a
        Case 9:object.image = c
        Case 13:object.image = d
    End Select
End Sub

Sub NFadeObj(nr, object, a, b)
    Select Case FadingLevel(nr)
        Case 4:object.image = b:FadingLevel(nr) = 0 'off
        Case 5:object.image = a:FadingLevel(nr) = 1 'on
    End Select
End Sub

Sub NFadeObjm(nr, object, a, b)
    Select Case FadingLevel(nr)
        Case 4:object.image = b
        Case 5:object.image = a
    End Select
End Sub

' Flasher objects

Sub Flash(nr, object)
    Select Case FadingLevel(nr)
        Case 4 'off
            FlashLevel(nr) = FlashLevel(nr)- FlashSpeedDown(nr)
            If FlashLevel(nr) <FlashMin(nr)Then
                FlashLevel(nr) = FlashMin(nr)
                FadingLevel(nr) = 0 'completely off
            End if
            Object.IntensityScale = FlashLevel(nr)
        Case 5 ' on
            FlashLevel(nr) = FlashLevel(nr) + FlashSpeedUp(nr)
            If FlashLevel(nr)> FlashMax(nr)Then
                FlashLevel(nr) = FlashMax(nr)
                FadingLevel(nr) = 1 'completely on
            End if
            Object.IntensityScale = FlashLevel(nr)
    End Select
End Sub

Sub Flashm(nr, object) 'multiple flashers, it doesn't change anything, it just follows the main flasher
    Select Case FadingLevel(nr)
        Case 4, 5
            Object.IntensityScale = FlashLevel(nr)
    End Select
End Sub

Sub FlashBlink(nr, object)
    Select Case FadingLevel(nr)
        Case 4 'off
            FlashLevel(nr) = FlashLevel(nr)- FlashSpeedDown(nr)
            If FlashLevel(nr) <FlashMin(nr)Then
                FlashLevel(nr) = FlashMin(nr)
                FadingLevel(nr) = 0 'completely off
            End if
            Object.IntensityScale = FlashLevel(nr)
            If FadingLevel(nr) = 0 AND FlashRepeat(nr)Then 'repeat the flash
                FlashRepeat(nr) = FlashRepeat(nr)-1
                If FlashRepeat(nr)Then FadingLevel(nr) = 5
            End If
        Case 5 ' on
            FlashLevel(nr) = FlashLevel(nr) + FlashSpeedUp(nr)
            If FlashLevel(nr)> FlashMax(nr)Then
                FlashLevel(nr) = FlashMax(nr)
                FadingLevel(nr) = 1 'completely on
            End if
            Object.IntensityScale = FlashLevel(nr)
            If FadingLevel(nr) = 1 AND FlashRepeat(nr)Then FadingLevel(nr) = 4
    End Select
End Sub

' Desktop Objects: Reels & texts (you may also use lights on the desktop)

' Reels

Sub FadeR(nr, object)
    Select Case FadingLevel(nr)
        Case 4:object.SetValue 1:FadingLevel(nr) = 6                   'fading to off...
        Case 5:object.SetValue 0:FadingLevel(nr) = 1                   'ON
        Case 6, 7, 8:FadingLevel(nr) = FadingLevel(nr) + 1             'wait
        Case 9:object.SetValue 2:FadingLevel(nr) = FadingLevel(nr) + 1 'fading...
        Case 10, 11, 12:FadingLevel(nr) = FadingLevel(nr) + 1          'wait
        Case 13:object.SetValue 3:FadingLevel(nr) = 0                  'Off
    End Select
End Sub

Sub FadeRm(nr, object)
    Select Case FadingLevel(nr)
        Case 4:object.SetValue 1
        Case 5:object.SetValue 0
        Case 9:object.SetValue 2
        Case 3:object.SetValue 3
    End Select
End Sub

'Texts

Sub NFadeT(nr, object, message)
    Select Case FadingLevel(nr)
        Case 4:object.Text = "":FadingLevel(nr) = 0
        Case 5:object.Text = message:FadingLevel(nr) = 1
    End Select
End Sub

Sub NFadeTm(nr, object, message)
    Select Case FadingLevel(nr)
        Case 4:object.Text = ""
        Case 5:object.Text = message
    End Select
End Sub

'************************************
'          LEDs Display
'     Based on Scapino's LEDs
'************************************

Dim Digits(32)
Dim Patterns(11)
Dim Patterns2(11)

Patterns(0) = 0     'empty
Patterns(1) = 63    '0
Patterns(2) = 6     '1
Patterns(3) = 91    '2
Patterns(4) = 79    '3
Patterns(5) = 102   '4
Patterns(6) = 109   '5
Patterns(7) = 125   '6
Patterns(8) = 7     '7
Patterns(9) = 127   '8
Patterns(10) = 111  '9

Patterns2(0) = 128  'empty
Patterns2(1) = 191  '0
Patterns2(2) = 134  '1
Patterns2(3) = 219  '2
Patterns2(4) = 207  '3
Patterns2(5) = 230  '4
Patterns2(6) = 237  '5
Patterns2(7) = 253  '6
Patterns2(8) = 135  '7
Patterns2(9) = 255  '8
Patterns2(10) = 239 '9

'Assign 6-digit output to reels
Set Digits(0) = a0
Set Digits(1) = a1
Set Digits(2) = a2
Set Digits(3) = a3
Set Digits(4) = a4
Set Digits(5) = a5
Set Digits(6) = a6

Set Digits(7) = b0
Set Digits(8) = b1
Set Digits(9) = b2
Set Digits(10) = b3
Set Digits(11) = b4
Set Digits(12) = b5
Set Digits(13) = b6

Set Digits(14) = c0
Set Digits(15) = c1
Set Digits(16) = c2
Set Digits(17) = c3
Set Digits(18) = c4
Set Digits(19) = c5
Set Digits(20) = c6

Set Digits(21) = d0
Set Digits(22) = d1
Set Digits(23) = d2
Set Digits(24) = d3
Set Digits(25) = d4
Set Digits(26) = d5
Set Digits(27) = d6

Set Digits(28) = e0
Set Digits(29) = e1
Set Digits(30) = e2
Set Digits(31) = e3

Sub UpdateLeds
    On Error Resume Next
    Dim ChgLED, ii, jj, chg, stat, num
    ChgLED = Controller.ChangedLEDs(&HFF, &HFFFF)
    If Not IsEmpty(ChgLED)Then
        For ii = 0 To UBound(ChgLED)
            chg = chgLED(ii, 1):stat = chgLED(ii, 2):num=chgLED(ii, 0)
			If UseFlexDMD then UpdateFlexChar num, stat
            For jj = 0 to 10
                If stat = Patterns(jj)OR stat = Patterns2(jj)then Digits(chgLED(ii, 0)).SetValue jj
            Next
        Next
		If UseFlexDMD then FlexDMDUpdate
    End IF
End Sub

'******************************
' Diverse Collection Hit Sounds
'******************************

Sub aMetals_Hit(idx):PlaySound "fx_MetalHit2", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub
Sub aRubber_Bands_Hit(idx):PlaySound "fx_rubber_band", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub
Sub aRubber_Posts_Hit(idx):PlaySound "fx_rubber_post", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub
Sub aRubber_Pins_Hit(idx):PlaySound "fx_rubber_pin", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub
Sub aPlastics_Hit(idx):PlaySound "fx_PlasticHit", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub
Sub aGates_Hit(idx):PlaySound "fx_Gate", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub
Sub aWoods_Hit(idx):PlaySound "fx_Woodhit", 0, Vol(ActiveBall), pan(ActiveBall), 0, Pitch(ActiveBall), 0, 0:End Sub

' *********************************************************************
'                      Supporting Ball & Sound Functions
' *********************************************************************

Function Vol(ball) ' Calculates the Volume of the sound based on the ball speed
    Vol = Csng(BallVel(ball) ^2 / 2000)
End Function

Function Pan(ball) ' Calculates the pan for a ball based on the X position on the table. "table1" is the name of the table
    Dim tmp
    tmp = ball.x * 2 / table1.width-1
    If tmp> 0 Then
        Pan = Csng(tmp ^10)
    Else
        Pan = Csng(-((- tmp) ^10))
    End If
End Function

Function Pitch(ball) ' Calculates the pitch of the sound based on the ball speed
    Pitch = BallVel(ball) * 20
End Function

Function BallVel(ball) 'Calculates the ball speed
    BallVel = INT(SQR((ball.VelX ^2) + (ball.VelY ^2)))
End Function

Function AudioFade(ball) 'only on VPX 10.4 and newer
    Dim tmp
    tmp = ball.y * 2 / Table1.height-1
    If tmp> 0 Then
        AudioFade = Csng(tmp ^10)
    Else
        AudioFade = Csng(-((- tmp) ^10))
    End If
End Function

'*****************************************
'      JP's VP10 Rolling Sounds
'*****************************************

Const tnob = 20 ' total number of balls
Const lob = 0   'number of locked balls
ReDim rolling(tnob)
InitRolling

Sub InitRolling
    Dim i
    For i = 0 to tnob
        rolling(i) = False
    Next
End Sub

Sub RollingUpdate()
    Dim BOT, b, ballpitch
    BOT = GetBalls

    ' stop the sound of deleted balls
    For b = UBound(BOT) + 1 to tnob
        rolling(b) = False
        StopSound("fx_ballrolling" & b)
    Next

    ' exit the sub if no balls on the table
    If UBound(BOT) = lob - 1 Then Exit Sub 'there no extra balls on this table

    ' play the rolling sound for each ball
    For b = lob to UBound(BOT)
        If BallVel(BOT(b))> 1 Then
            If BOT(b).z <30 Then
                ballpitch = Pitch(BOT(b))
            Else
                ballpitch = Pitch(BOT(b)) + 15000 'increase the pitch on a ramp or elevated surface
            End If
            rolling(b) = True
            PlaySound("fx_ballrolling" & b), -1, Vol(BOT(b)), Pan(BOT(b)), 0, ballpitch, 1, 0
        Else
            If rolling(b) = True Then
                StopSound("fx_ballrolling" & b)
                rolling(b) = False
            End If
        End If
			If BOT(b).VelZ < -1 and BOT(b).z < 55 and BOT(b).z > 40 Then 'height adjust for ball drop sounds
			PlaySound "fx_ball_drop" & b, 0, ABS(BOT(b).velz)/17, Pan(BOT(b)), 0, Pitch(BOT(b)), 1, 0, 0 ', ABS(BOT(b).velz)/17, 0, 1, 0
		End If
    Next
End Sub

'**********************
' Ball Collision Sound
'**********************

Sub OnBallBallCollision(ball1, ball2, velocity)
    PlaySound("fx_collide"), 0, Csng(velocity) ^2 / 2000, Pan(ball1), 0, Pitch(ball1), 0, 0
End Sub






'**********************************************************
'  4*7 + 2*2 segment numeric to flexdmd display conversion
'**********************************************************
Dim FlexDMD
DIm FlexDMDDict
Dim L1Chars, L2Chars, L3Chars, L4Chars, LCChars, LBChars
Dim Line1Change, Line2Change, Line3Change, Line4Change, LineCreditChange, LineBallChange
Dim LastScoreUpdated

Sub FlexDMD_Init() 'default/startup values

	DIm FlexDMDFont
	Dim FlexPath
	Dim FlexDMDScene
	
	'arrays to hold characters to display converted from segment codes
	L1Chars = Array(" ."," "," "," ."," "," "," ")
	L2Chars = Array(" ."," "," "," ."," "," "," ")
	L3Chars = Array(" ."," "," "," ."," "," "," ")
	L4Chars = Array(" ."," "," "," ."," "," "," ")
	
	LCChars = Array("CREDITS "," "," ")
	LBChars = Array(" BALL "," "," ")
	
	LastScoreUpdated = 0
	
	FlexDictionary_Init
	
	Dim fso,curdir
	Set fso = CreateObject("Scripting.FileSystemObject")
	curDir = fso.GetAbsolutePathName(".")
	FlexPath = curDir & "\Aliens.FlexDMD\"

	Set FlexDMD = CreateObject("FlexDMD.FlexDMD")
	If Not FlexDMD is Nothing Then
	
		FlexDMD.Show = False
		FlexDMD.LockRenderThread

		FlexDMD.RenderMode = 2
		FlexDMD.Width = 128
		FlexDMD.Height = 32
		FlexDMD.Clear = True
		FlexDMD.GameName = cGameName
		FlexDMD.Run = True

		Set FlexDMDScene = FlexDMD.NewGroup("Scene")

		FlexDMDScene.AddActor FlexDMD.NewVideo("Back",FlexPath & "BackGround.gif")
		
		FlexDMDFont = FlexDMD.NewFont(FlexPath & "Aliens.fnt", vbWhite, vbBlue, 0)
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line1", FlexDMDFont, "0,000,000"))
		FlexDMDScene.GetLabel("Line1").SetAlignedPosition 50, -1, 2

		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line3", FlexDMDFont,  "0,000,000"))
		FlexDMDScene.GetLabel("Line3").SetAlignedPosition 50, 25, 8
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line2", FlexDMDFont,  "0,000,000"))
		FlexDMDScene.GetLabel("Line2").SetAlignedPosition 126, -1, 2
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line4", FlexDMDFont,  "0,000,000"))
		FlexDMDScene.GetLabel("Line4").SetAlignedPosition 126, 25, 8
		
		FlexDMDFont = FlexDMD.NewFont(FlexPath & "AliensLarge.fnt", vbWhite, vbBlue, 0)
		
		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line1L", FlexDMDFont, "0,000,000"))
		FlexDMDScene.GetLabel("Line1L").SetAlignedPosition 51, -1, 2
		FlexDMDScene.GetLabel("Line1L").Visible = False

		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line3L", FlexDMDFont,  "0,000,000"))
		FlexDMDScene.GetLabel("Line3L").SetAlignedPosition 51, 25, 8
		FlexDMDScene.GetLabel("Line3L").Visible = False
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line2L", FlexDMDFont,  "0,000,000"))
		FlexDMDScene.GetLabel("Line2L").SetAlignedPosition 127, -1, 2
		FlexDMDScene.GetLabel("Line2L").Visible = False
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("Line4L", FlexDMDFont,  "0,000,000"))
		FlexDMDScene.GetLabel("Line4L").SetAlignedPosition 127, 25, 8
		FlexDMDScene.GetLabel("Line4L").Visible = False
		
		
		Set FlexDMDFont = FlexDMD.NewFont("FlexDMD.Resources.teeny_tiny_pixls-5.fnt", vbGreen, vbBlue, 0)
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("LineCredit", FlexDMDFont, "CREDITS 00"))
		FlexDMDScene.GetLabel("LineCredit").SetAlignedPosition 87, 26,0 
	
		FlexDMDScene.AddActor(FlexDMD.NewLabel("LineBall", FlexDMDFont, " BALL 00"))
		FlexDMDScene.GetLabel("LineBall").SetAlignedPosition 51,26,2
		
		FlexDMDScene.AddActor(FlexDMD.NewLabel("LineHiScore", FlexDMDFont, "HIGH SCORES"))
		FlexDMDScene.GetLabel("LineHiScore").SetAlignedPosition 51,26,2
		
		Set FlexDMDFont = FlexDMD.NewFont("FlexDMD.Resources.teeny_tiny_pixls-5.fnt", vbRed, vbBlue, 0)
		
		FlexDMDScene.AddActor(FlexDMD.NewLabel("LineTilt", FlexDMDFont, "TILT"))
		FlexDMDScene.GetLabel("LineTilt").SetAlignedPosition 77,26,2
		
		FlexDMD.Stage.AddActor FlexDMDScene
		
		FlexDMD.Show = True
		FlexDMD.UnlockRenderThread
		
		Line1Change = False
		Line2Change = False 
		Line3Change = False
		Line4Change = False
		LineCreditChange = False
		LineBallChange = False
		
	End If

End Sub

Sub FlexDictionary_Init

	'add conversion of segment charcters codes to lookup table
	Set FlexDMDDict = CreateObject("Scripting.Dictionary")

	FlexDMDDict.Add 0, " "
	FlexDMDDict.Add 63, "0"
	FlexDMDDict.Add 6, "1"
	FlexDMDDict.Add 91, "2"
	FlexDMDDict.Add 79, "3"
	FlexDMDDict.Add 102, "4"
	FlexDMDDict.Add 109, "5"
	FlexDMDDict.Add 125, "6"
	FlexDMDDict.Add 7, "7"
	FlexDMDDict.Add 127, "8"
	FlexDMDDict.Add 111, "9"
	
	FlexDMDDict.Add 191, "0,"
	FlexDMDDict.Add 134, "1,"
	FlexDMDDict.Add 219, "2,"
	FlexDMDDict.Add 207, "3,"
	FlexDMDDict.Add 230, "4,"
	FlexDMDDict.Add 237, "5,"
	FlexDMDDict.Add 253, "6,"
	FlexDMDDict.Add 135, "7,"
	FlexDMDDict.Add 255, "8,"
	FlexDMDDict.Add 239, "9,"
	 
End Sub

'**************
' Update FlexDMD
'**************

Sub FlexDMDUpdate()

	Dim FlexLabel
	dim LineText
	
	If Not FlexDMD is Nothing Then FlexDMD.LockRenderThread
	If FlexDMD.Run = False Then FlexDMD.Run = True
	
	With FlexDMD.Stage

		if LampState(6) = 1 then 
			'ball in play, updating one score
			'make active score large font, all others small
			If Line1Change and LastScoreUpdated <> 1 then
				.GetLabel("Line1").Visible = False
				.GetLabel("Line2").Visible = True
				.GetLabel("Line3").Visible = True
				.GetLabel("Line4").Visible = True
				.GetLabel("Line1L").Visible = True
				.GetLabel("Line2L").Visible = False
				.GetLabel("Line3L").Visible = False
				.GetLabel("Line4L").Visible = False
				LastScoreUpdated = 1
			End if
			If Line2Change and LastScoreUpdated <> 2 then
				.GetLabel("Line1").Visible = True
				.GetLabel("Line2").Visible = False
				.GetLabel("Line3").Visible = True
				.GetLabel("Line4").Visible = True
				.GetLabel("Line1L").Visible = False
				.GetLabel("Line2L").Visible = True
				.GetLabel("Line3L").Visible = False
				.GetLabel("Line4L").Visible = False
				LastScoreUpdated = 2
			End if
			If Line3Change and LastScoreUpdated <> 3 then
				.GetLabel("Line1").Visible = True
				.GetLabel("Line2").Visible = True
				.GetLabel("Line3").Visible = False
				.GetLabel("Line4").Visible = True
				.GetLabel("Line1L").Visible = False
				.GetLabel("Line2L").Visible = False
				.GetLabel("Line3L").Visible = True
				.GetLabel("Line4L").Visible = False
				LastScoreUpdated = 3
			End if
			If Line4Change and LastScoreUpdated <> 4 then
				.GetLabel("Line1").Visible = True
				.GetLabel("Line2").Visible = True
				.GetLabel("Line3").Visible = True
				.GetLabel("Line4").Visible = False
				.GetLabel("Line1L").Visible = False
				.GetLabel("Line2L").Visible = False
				.GetLabel("Line3L").Visible = False
				.GetLabel("Line4L").Visible = True
				LastScoreUpdated = 4
			End if
		else
			'attract. Show small font for all
			.GetLabel("Line1").Visible = True
			.GetLabel("Line2").Visible = True
			.GetLabel("Line3").Visible = True
			.GetLabel("Line4").Visible = True
			.GetLabel("Line1L").Visible = False
			.GetLabel("Line2L").Visible = False
			.GetLabel("Line3L").Visible = False
			.GetLabel("Line4L").Visible = False
			LastScoreUpdated = 0
		end if
		
		if Line1Change = True then
			LineText = Join(L1Chars,"")
			.GetLabel("Line1").Text = LineText
			.GetLabel("Line1L").Text = LineText
		End if
		if Line2Change = True then
			LineText = Join(L2Chars,"")
			.GetLabel("Line2").Text = LineText
			.GetLabel("Line2L").Text = LineText
		End if
		if Line3Change = True then
			LineText = Join(L3Chars,"")
			.GetLabel("Line3").Text = LineText
			.GetLabel("Line3L").Text = LineText
		End if
		if Line4Change = True then
			LineText = Join(L4Chars,"")
			.GetLabel("Line4").Text = LineText
			.GetLabel("Line4L").Text = LineText
		End if
		if LineCreditChange = True then
			LineText = Join(LCChars,"")
			.GetLabel("LineCredit").Text = LineText
		End if
		if LineBallChange = True or LampState(2) = 1 then
			if LampState(2) = 1 Then 'match 
				LBChars(0) = "MATCH "
			else
				LBChars(0) = " BALL "
			End if
			LineText = Join(LBChars,"")
			.GetLabel("LineBall").Text = LineText
		End if
		
		if LampState(6) = 0 and (LampState(4) = 1 or L4Chars(6) <> " ") then		' high score lamp, won't always work because it flashes on/off. 
																					'L4Chars(6) is a frig, won't work after 4 player game
			.GetLabel("LineHiScore").Visible = True
			.GetLabel("LineBall").Visible = False
		Else
			.GetLabel("LineHiScore").Visible = False
			.GetLabel("LineBall").Visible = True
		end if
		
		if LampState(3) = 1 then		' tilt
			.GetLabel("LineTilt").Visible = True
		Else
			.GetLabel("LineTilt").Visible = False
		end if
		
	End With

	If Not FlexDMD is Nothing Then FlexDMD.UnlockRenderThread
	
	Line1Change = False
	Line2Change = False 
	Line3Change = False
	Line4Change = False
	LineCreditChange = False
	LineBallChange = False
		
End Sub

Sub UpdateFlexChar(id, value)
	'map segment code to character in LnChars arrays
	Dim chr
	if FlexDMDDict.Exists (value) then
	
		chr = FlexDMDDict.Item (value)
		
		if value = 0 then ' 0 is a space, everything else is numbers for scores 1-4
		select case id
		case 0,3,7,10,14,17,21,24
			chr = chr & "." 'pad for alignment  (. char is a narrow space in fnt png file)
		end select
		end if

		if id < 7 then
			L3Chars(id) = chr
			Line3Change = True
		elseif id < 14 then
			L4Chars(id - 7) = chr
			Line4Change = True
		elseif id < 21 then
			L1Chars(id - 14) = chr
			Line1Change = True
		elseif id < 28 then
			L2Chars(id - 21) = chr
			Line2Change = True
		elseif id < 30 then
			LCChars(id - 27) = chr	
			LineCreditChange = True
		elseif id < 32 then
			LBChars(id - 29) = chr
			LineBallChange = True
		end if

	end if 
		
End Sub

Sub Table1_exit
    Controller.Stop
End Sub

Sub table1_Exit():Controller.Games("sorcr_l1").Settings.Value("sound") = 1:Controller.Stop : End Sub