' Driven by Visual PinMAME (VPinMAME)
'--------------------------------------------------------------------------------

Option Explicit

LoadVPM "00990300", "S7.VBS", 2.21

Sub LoadVPM(VPMver, VBSfile, VBSver)
	On Error Resume Next
		If ScriptEngineMajorVersion < 5 Then MsgBox "VB Script Engine 5.0 or higher required"
		ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile(VBSfile, 1).ReadAll
		If Err Then MsgBox "Unable to open " & VBSfile & ". Ensure that it is in the same folder as this table. " & vbNewLine & Err.Description
		Set Controller = CreateObject("VPinMAME.Controller")
		If Err Then MsgBox "Can't Load VPinMAME." & vbNewLine & Err.Description
		If VPMver>"" Then If Controller.Version < VPMver Or Err Then MsgBox "VPinMAME ver " & VPMver & " required."
		If VPinMAMEDriverVer < VBSver Or Err Then MsgBox VBSFile & " ver " & VBSver & " or higher required."
	On Error Goto 0
End Sub

Sub InitVPM()
	Controller.GameName = "lsrcu_l2"				' Use specified ROM set.
	Controller.SplashInfoLine = "LaserCue" & vbNewLine & "by Cutter & Kristian" & vbnewline & "http://www.vpfreeforums.com"
	Controller.HandleKeyboard = False
	Controller.ShowTitle = False
	Controller.ShowDMDOnly = True
'	Controller.DIP(0) = &H80 ' Set US	sys7 games like Lasercue don't have this dip

	On Error Resume Next
		Controller.Run
		If Err Then MsgBox Err.Description
	On Error Goto 0
End Sub

Const UseSolenoids = True
Const UseLamps     = True

' Standard Sounds used by Driver help code
Const SSolenoidOn   = ""
Const SSolenoidOff  = ""
Const SFlipperOn    = "FlipperUp"
Const SFlipperOff   = "FlipperDown"
Const SCoin = "Quarter"

' Solenoid sounds
SolCallback(sKnocker)          = "vpmSolSound ""Knocker"","

' Flippers
SolCallback(sLLFlipper)        = "vpmSolFlipper LeftFlipper, Flipper3,"
SolCallback(sLRFlipper)        = "vpmSolFlipper RightFlipper, Flipper4,"

SolCallback(sT1Reset)          = "SolT1Reset"
SolCallback(sT2Reset)          = "SolT2Reset"
SolCallback(sT3Reset)          = "SolT3Reset"
SolCallback(sT4Reset)          = "SolT4Reset"
SolCallback(sT5Reset)          = "SolT5Reset"
SolCallback(sGenIllu)          = "SolGenIllu"
SolCallback(sBell)             = "SolBell"
SolCallback(sGameOn)           = "vpmNudge.SolGameOn"

Dim bsTrough, MyDrops
Sub LaserCue_Init()
	InitVPM
	
	vpmCreateEvents AllSwitches ' create events for all switches
	vpmMapLights AllLamps ' Map all lamps into lights array

	' Set up tilt. Unfortunatly VP doesn't allow you to disable the slings
	vpmNudge.TiltSwitch = swTilt
	vpmNudge.Sensitivity = 5
	vpmNudge.TiltObj = Array(Bumper1,Bumper2,Bumper3,LeftSlingshot)

	Set bsTrough = New cvpmBallStack
	bsTrough.InitNoTrough BallRelease, 9, 100,2
	bsTrough.InitExitSnd "slingshot",""
'	bsTrough.BallImage="pinball"
	SolCallback(sBallRelease) = "bsTrough.SolOut"

	Set MyDrops = New cvpmDropTarget
	MyDrops.InitDrop Targets,0
	MyDrops.InitSnd "soloff","slingshot"
	MyDrops.CreateEvents "MyDrops"
	SolCallback(sResetBank) = "MyDrops.SolDropDown"

	Bumper1.state = LightStateOn
	Bumper2.state = LightStateOn
	Bumper3.state = LightStateOn
End Sub

'-------------------
' keyboard handlers
'-------------------
Sub LaserCue_KeyUp(ByVal keycode)
	if(keycode=LeftFlipperKey or keycode=RightFlipperKey) then Controller.Switch(43)=FALSE
	If vpmKeyUp(keycode) Then Exit Sub
	If keycode = PlungerKey then Plunger.Fire
End Sub
Sub LaserCue_KeyDown(ByVal keycode)
	if(keycode=LeftFlipperKey or keycode=RightFlipperKey) then Controller.Switch(43)=TRUE
	If vpmKeyDown(keycode) Then Exit Sub
	If keycode = PlungerKey Then Plunger.PullBack
End Sub

' The rom has a bug not turning the bell off if outlane special is set to score
' extra ball (Adjustment 27=01). So we turn off the annoying bell here when the ball
' arrives in outhole.
Sub Outhole_Hit : bsTrough.AddBall Me : SolBell("False"): End Sub

Sub Bumper1_Hit : vpmTimer.PulseSw 17 : PlaySound "Jet1" : End Sub
Sub Bumper2_Hit : vpmTimer.PulseSw 18 : PlaySound "Jet1" : End Sub
Sub Bumper3_Hit : vpmTimer.PulseSw 19 : PlaySound "Jet1" : End Sub
Sub LeftSlingshot_Slingshot : vpmTimer.PulseSw 41 : PlaySound "slingshot" : End Sub

Sub SolT1Reset(enabled)
	if(enabled) then
		PlaySound "soloff"
		Controller.Switch(33)=FALSE
		Target1.IsDropped=FALSE
	end if
End Sub

Sub SolT2Reset(enabled)
	if(enabled) then
		PlaySound "soloff"
		Controller.Switch(34)=FALSE
		Target2.IsDropped=FALSE
	end if
End Sub

Sub SolT3Reset(enabled)
	if(enabled) then
		PlaySound "soloff"
		Controller.Switch(35)=FALSE
		Target3.IsDropped=FALSE
	end if
End Sub

Sub SolT4Reset(enabled)
	if(enabled) then
		PlaySound "soloff"
		Controller.Switch(36)=FALSE
		Target4.IsDropped=FALSE
	end if
End Sub

Sub SolT5Reset(enabled)
	if(enabled) then
		PlaySound "soloff"
		Controller.Switch(37)=FALSE
		Target5.IsDropped=FALSE
	end if
End Sub

Sub SolGenIllu(enabled)
	dim state
	if enabled then state=LightStateOff else state=LightStateOn
	Bumper1.state = state
	Bumper2.state = state
	Bumper3.state = state
End Sub

Sub SolBell(enabled)
	' this plays the ringbell sound until the solenoid releases
	if enabled then
		playsound "ringbell", -1
	else
		stopsound "ringbell"
	end if
End Sub

Sub CheckTilt_Timer()
' the CheckTilt timer isn't needed anymore and can be removed
' from the table together with this sub
  me.enabled=false
End Sub

Const sBallRelease =  1
Const sT1Reset     =  2
Const sT2Reset     =  3
Const sT3Reset     =  4
Const sT4Reset     =  5
Const sT5Reset     =  6
Const sResetBank   =  7
Const sGenIllu     =  8
Const sBell        = 14
Const sKnocker     = 15
Const sLSling      = 17
Const sGameOn      = 25
