Attribute VB_Name = "Module1"
Public unsaved
Public max_array_x
Public max_array_y
Public max_array_z
Public LBnd(3) As Long
Public UBnd(3) As Long
Public nDims As Integer
Public ap_x
Public ap_y
Public ap_z
Public WinIdle As Boolean
Public fno
Public go_sub(1000)
Public recurs
Public var$(199, 2)
Public INDUCTaVAR$(199, 2)
Public script$(16384)
Public pointer
Public newPointer
Public dbox_txt$
Public arg_all$
Public MX_VAR
Public arrayrow
Public Interrupt As Long
Public tempHandle1 As Long
Public tempHandle2 As Long
Public winhandle As Long
Public PatternEditorWindow As Long
Public WavTabWindow As Long
Public SequencerEditorWindow As Long
Public cd$
Public anchorForm(1 To 10) As Form2
Public showdebug
Public debugtofile$
Public dbg_disable
Public mt_disable
Public exec_disable
Public arraycol
Public inductarray$()
Public inductarray_temp$()
Public PatternEditorToolBar As Long
Public PatternEditorPatternName As Long
Public PatternEditorMachineName As Long
Public PatternEditorWaveName As Long
Public PatternEditorBaseOctave As Long
Public BPMHandle As Long
Public TPBHandle As Long
Public Master2WindowHandle As Long
Public MasterWindowHandle As Long
Public PatternWindowHandle
Public idle
Public izgreater
Public izgreateroe
Public izless
Public izlessoe
Public izequal
Public mx
Public izfalse
Public berr
Public btch$
Public seps(2)

' For custom forms - windowing gui
Public GUIForms As New Collection




Sub resize(xx, yy, zz)
If xx < max_array_x Then xx = max_array_x
If yy < max_array_y Then yy = max_array_y
If zz < max_array_z Then zz = max_array_z
setstring "arrayresize", "pending"
inductarray_temp$ = inductarray$
If dbg_disable <> 1 Then dbug "runtime", "resizing array"
ReDim inductarray$(xx, yy, zz)
If dbg_disable <> 1 Then dbug "runtime", "repopulating array"
For xxx = 1 To xx
For yyy = 1 To yy
For zzz = 1 To zz
inductarray$(xx, yy, zz) = inductarray_temp$(xx, yy, zz)
Next
Next
Next

max_array_x = xx
max_array_y = yy
max_array_z = zz
If dbg_disable <> 1 Then dbug "runtime", "Redimensioned array ok"

End Sub

Public Function dumpvar()
found = 0
v$ = ""
For d = 1 To MX_VAR
 
 v$ = var$(d, 2)
 
 p$ = var$(d, 1)
 t$ = Str$(d) + ":" + p$ + " - " + v$
 dbug (t$), "runtime"

Next

End Function


Public Function variable(varname$)


found = 0
v$ = ""
For d = 1 To MX_VAR

If LCase$(var$(d, 1)) = LCase$(varname$) Then v$ = var$(d, 2)

Next

If v$ = "" Then v$ = varname$
If Left$(varname$, 1) = "\" Then v$ = Right$(varname$, Len(varname$) - 1)

variable = v$
If LCase$(varname$) = "timer" Then variable = Timer
If LCase$(varname$) = "activenote" Then variable = NoteUnderCursor()
If LCase$(varname$) = "machinename" Then variable = GetMachineName
If LCase$(varname$) = "wavename" Then variable = GetWaveName
If LCase$(varname$) = "baseoctave" Then variable = GetBaseOctave
If LCase$(varname$) = "tpb" Then variable = GetTPB
If LCase$(varname$) = "bpm" Then variable = GetBPM
If LCase$(varname$) = "xpos" Then variable = Trim(Str$(xpos))
If LCase$(varname$) = "ypos" Then variable = Trim(Str$(ypos))
If LCase$(varname$) = "array" Then variable = inductarray$(ap_x, ap_y, ap_z)
End Function


Public Sub setstring(varname$, value$)

'first we gotta check to see if the variable exists already or not
found = 0
First = 1
For d = 1 To MX_VAR
    If LCase$(var$(d, 1)) = LCase$(varname$) Then found = d
    If Len(var$(d, 1)) = 0 And First = 1 Then First = d
Next

If found = 0 Then
    var$(First, 1) = varname$
    var$(First, 2) = variable(value$)
    posput = First
    MX_VAR = MX_VAR + 1
Else
    var$(found, 2) = variable(value$)
    posput = found
End If
t$ = "just set variable number " + Str$(MX_VAR) + "[" + Str$(posput) + "] (" + varname$ + ") to : " + value$
If dbg_disable <> 1 Then dbug t$, "runtime"
End Sub


Public Function convert_english(note)
nt = "---"
If note = 0 Then nt = "C-0"
If note = 1 Then nt = "C#0"
If note = 2 Then nt = "D-0"
If note = 3 Then nt = "D#0"
If note = 4 Then nt = "E-0"
If note = 5 Then nt = "F-0"
If note = 6 Then nt = "F#0"
If note = 7 Then nt = "G-0"
If note = 8 Then nt = "G#0"
If note = 9 Then nt = "A-0"
If note = 10 Then nt = "A#0"
If note = 11 Then nt = "B-0"
If note = 12 Then nt = "C-1"
If note = 13 Then nt = "C#1"
If note = 14 Then nt = "D-1"
If note = 15 Then nt = "D#1"
If note = 16 Then nt = "E-1"
If note = 17 Then nt = "F-1"
If note = 18 Then nt = "F#1"
If note = 19 Then nt = "G-1"
If note = 20 Then nt = "G#1"
If note = 21 Then nt = "A-1"
If note = 22 Then nt = "A#1"
If note = 23 Then nt = "B-1"
If note = 24 Then nt = "C-2"
If note = 25 Then nt = "C#2"
If note = 26 Then nt = "D-2"
If note = 27 Then nt = "D#2"
If note = 28 Then nt = "E-2"
If note = 29 Then nt = "F-2"
If note = 30 Then nt = "F#2"
If note = 31 Then nt = "G-2"
If note = 32 Then nt = "G#2"
If note = 33 Then nt = "A-2"
If note = 34 Then nt = "A#2"
If note = 35 Then nt = "B-2"
If note = 36 Then nt = "C-3"
If note = 37 Then nt = "C#3"
If note = 38 Then nt = "D-3"
If note = 39 Then nt = "D#3"
If note = 40 Then nt = "E-3"
If note = 41 Then nt = "F-3"
If note = 42 Then nt = "F#3"
If note = 43 Then nt = "G-3"
If note = 44 Then nt = "G#3"
If note = 45 Then nt = "A-3"
If note = 46 Then nt = "A#3"
If note = 47 Then nt = "B-3"
If note = 48 Then nt = "C-4"
If note = 49 Then nt = "C#4"
If note = 50 Then nt = "D-4"
If note = 51 Then nt = "D#4"
If note = 52 Then nt = "E-4"
If note = 53 Then nt = "F-4"
If note = 54 Then nt = "F#4"
If note = 55 Then nt = "G-4"
If note = 56 Then nt = "G#4"
If note = 57 Then nt = "A-4"
If note = 58 Then nt = "A#4"
If note = 59 Then nt = "B-4"
If note = 60 Then nt = "C-5"
If note = 61 Then nt = "C#5"
If note = 62 Then nt = "D-5"
If note = 63 Then nt = "D#5"
If note = 64 Then nt = "E-5"
If note = 65 Then nt = "F-5"
If note = 66 Then nt = "F#5"
If note = 67 Then nt = "G-5"
If note = 68 Then nt = "G#5"
If note = 69 Then nt = "A-5"
If note = 70 Then nt = "A#5"
If note = 71 Then nt = "B-5"
If note = 72 Then nt = "C-6"
If note = 73 Then nt = "C#6"
If note = 74 Then nt = "D-6"
If note = 75 Then nt = "D#6"
If note = 76 Then nt = "E-6"
If note = 77 Then nt = "F-6"
If note = 78 Then nt = "F#6"
If note = 79 Then nt = "G-6"
If note = 80 Then nt = "G#6"
If note = 81 Then nt = "A-6"
If note = 82 Then nt = "A#6"
If note = 83 Then nt = "B-6"
If note = 84 Then nt = "C-7"
If note = 85 Then nt = "C#7"
If note = 86 Then nt = "D-7"
If note = 87 Then nt = "D#7"
If note = 88 Then nt = "E-7"
If note = 89 Then nt = "F-7"
If note = 90 Then nt = "F#7"
If note = 91 Then nt = "G-7"
If note = 92 Then nt = "G#7"
If note = 93 Then nt = "A-7"
If note = 94 Then nt = "A#7"
If note = 95 Then nt = "B-7"
If note = 96 Then nt = "C-8"
If note = 97 Then nt = "C#8"
If note = 98 Then nt = "D-8"
If note = 99 Then nt = "D#8"
If note = 100 Then nt = "E-8"
If note = 101 Then nt = "F-8"
If note = 102 Then nt = "F#8"
If note = 103 Then nt = "G-8"
If note = 104 Then nt = "G#8"
If note = 105 Then nt = "A-8"
If note = 106 Then nt = "A#8"
If note = 107 Then nt = "B-8"
If note = 108 Then nt = "C-9"
If note = 109 Then nt = "C#9"
If note = 110 Then nt = "D-9"
If note = 111 Then nt = "D#9"
If note = 112 Then nt = "E-9"
If note = 113 Then nt = "F-9"
If note = 114 Then nt = "F#9"
If note = 115 Then nt = "G-9"
If note = 116 Then nt = "G#9"
If note = 117 Then nt = "A-9"
If note = 118 Then nt = "A#9"
If note = 119 Then nt = "B-9"
If note = 120 Then nt = "OFF"
If note = -1 Then nt = "..."
convert_english = nt
End Function

Public Sub runscript(mx)
dbug "Script processing started...", "runtime"
    '

    Interrupt = 0   'returns the index of the anchor window so we know which one to close
    AnchorIndex = 1 'next window to open as an anchor
 
    'MsgBox (sb_handle)

Do While pointer < mx
    If WinIdle Then
        If mt_disable <> 1 Then DoEvents ' Yield to other processes.
    Else
        If Interrupt Then
            Set anchorForm(Interrupt) = Nothing
            Interrupt = 0
            pointer = newPointer
            dbug "Got interrupt", "runtime"
        Else
            pointer = pointer + 1
        End If
        If idle = True Then GoTo idlor
        
        l$ = LTrim$(script$(pointer))
        parse l$, "runtime"
        
        'dbug cmd$
        If slowmo = 1 Then
            PauseTime = 1
            start = Timer   ' Set start time.
            Do While Timer < start + PauseTime
                DoEvents   ' Yield to other processes.
            Loop
        End If
    End If
Loop

die
idlor:
idle = False
End Sub
Private Function auto_size_array(ax, ay, az)
If dbg_disable <> 1 Then dbug "runtime", "Requested array Size(" & Str$(ax) & "," & Str$(ay) & "," & Str$(az) & ")"
 arn = 0
 If ax > max_array_x Then
 arn = 1
 max_array_x = ax
 If dbg_disable <> 1 Then dbug "runtime", "increasing X"
 End If
 
 If ay > max_array_y Then
 arn = 1
 max_array_y = ay
 If dbg_disable <> 1 Then dbug "runtime", "increasing Y"
 End If
 
 If az > max_array_z Then
 arn = 1
max_array_z = az
If dbg_disable <> 1 Then dbug "runtime", "increasing Z"
End If

If arn = 1 Then

sz_x = max_array_x
sz_y = max_array_y
sz_z = max_array_z
If dbg_disable <> 1 Then dbug "runtime", "Setting array Size(" & Str$(sz_x) & "," & Str$(sz_y) & "," & Str$(sz_z) & ")"

'resize sz_x, sz_y, sz_z
If dbg_disable <> 1 Then dbug "runtime", "Pending resize..."
resize sz_x, sz_y, sz_z
If dbg_disable <> 1 Then dbug "runtime", "Done resize!..."
End If

If dbg_disable <> 1 Then dbug "runtime", "Exiting auto_size_array"
 
End Function
 Function Tokenize(ByVal TokenString, ByRef TokenSeparators())

    Dim NumWords, a()
    NumWords = 0
    
    Dim NumSeps
    NumSeps = UBound(TokenSeparators)
    
    Do
        Dim SepIndex, SepPosition
        SepPosition = 0
        SepIndex = -1
        
        For i = 0 To NumSeps - 1
        
            ' Find location of separator in the string
            Dim pos
            pos = InStr(TokenString, TokenSeparators(i))
            
            ' Is the separator present, and is it closest to the beginning of the string?
            If pos > 0 And ((SepPosition = 0) Or (pos < SepPosition)) Then
                SepPosition = pos
                SepIndex = i
            End If
            
        Next

        ' Did we find any separators?
        If SepIndex < 0 Then

            ' None found - so the token is the remaining string
            ReDim Preserve a(NumWords + 1)
            a(NumWords) = TokenString
            
        Else

            ' Found a token - pull out the substring
            Dim substr
            substr = Trim(Left(TokenString, SepPosition - 1))
    
            ' Add the token to the list
            ReDim Preserve a(NumWords + 1)
            a(NumWords) = substr
        
            ' Cutoff the token we just found
            Dim TrimPosition
            TrimPosition = SepPosition + Len(TokenSeparators(SepIndex))
            TokenString = Trim(Mid(TokenString, TrimPosition))
                        
        End If
        
        NumWords = NumWords + 1
    Loop While (SepIndex >= 0)
    
    Tokenize = a
    
End Function
Sub showdbug()
Form1.Show
'Form1.TabStrip1.SelectedItem = "Debug"
'Form1.quickhelp.Visible = False
'Form1.debug.Visible = True
'Form1.labellist.Visible = False

'showdbug
End Sub
Public Function parse(l$, mode)
If Len(l$) = 0 Then Exit Function

seps(0) = ","
seps(1) = " "
idle = False
On Error GoTo erhandle2
GoTo skip2
erhandle2:
showdbug

dbug mode, "*** Halt ***"
dbug mode, "Error report:"
dbug mode, (Error)
dbug mode, "at line " & pointer
dbug mode, " "
'dbug mode, "Variable dump"
'dumpvar

'MsgBox ("Script Ended Badly")
Do
DoEvents
Loop Until berr = 1
Err.Clear
berr = 0
On Error GoTo erhandle2
Form1.Hide

'End
skip2:
   Dim PatternWindowHandle As Long
    l2$ = ">> " & Str$(pointer) & ":   " & l$
If dbg_disable <> 1 And exec_disable <> 1 And mode = "runtime" Then dbug mode, (l2$)
If dbg_disable <> 1 And exec_disable <> 1 And mode = "runtime" Then Form1.highlightline (pointer)
cmd$ = ""
arg1$ = ""
arg2$ = ""
all_arg$ = ""

'first_space = InStr(l$, " ")
'If first_space > 0 Then
'    cmd$ = UCase$(Left$(l$, first_space - 1))
'Else
'    cmd$ = UCase$(l$)
'End If
'------------------------------------------------------
'attempting to incorporate new toekniser
'------------------------------------------------------
t_arg = Tokenize(l$, seps)
number_parsed = UBound(t_arg) - 1
'For d = 0 To number_parsed
'dbug mode, Str$(d) & " - " & t_arg(d)
'Next
If number_parsed = 0 Then cmd$ = UCase$(l$)
If number_parsed > 0 Then cmd$ = UCase$(t_arg(0))
If number_parsed > 0 Then arg1$ = t_arg(1)
If number_parsed > 1 Then arg2only$ = t_arg(2)
If number_parsed > 1 Then arg2$ = t_arg(2)
If number_parsed > 2 Then arg3$ = t_arg(3)

If Len(l$) > Len(cmd$) Then all_arg$ = Right(l$, (Len(l$) - Len(cmd$)) - 1)
If number_parsed > 1 Then arg2$ = Right(all_arg$, Len(all_arg$) - Len(arg1$ + " "))
'dbug mode, "-------------------------------------"
'dbug mode, "num of args:" & Str$(number_parsed)
'dbug mode, "cmd=" & cmd$
'dbug mode, "arg1=" & arg1$
'dbug mode, "arg2only=" & arg2only$
'dbug mode, "arg2=" & arg2$
'dbug mode, "arg3=" & arg3$
'dbug mode, "allarg=" & all_arg$
'dbug mode, "-------------------------------------"

'parse the commands here
coint = 0
If cmd$ = "SYNTAX" Then cmd$ = "?" & UCase(arg1$)
If cmd$ = "USAGE" Then cmd$ = "??" & UCase(arg1$)

If Left$(cmd$, 1) = ";" Then cmd$ = "REM"
Select Case cmd$

Case "REM"

'arrayinfo
Case "?ARRAYINFO"
dbug mode, "ARRAYINFO"
Case "??ARRAYINFO"
dbug mode, "shows the details of the array"
Case "ARRAYINFO"
dbug mode, "dimensions (x,y,z)"
dbug mode, "(" & max_array_x & "," & max_array_y & "," & max_array_z & ")"


'---run
Case "?RUN"
dbug mode, "RUN"
Case "??RUN"
dbug mode, "Runs the current script from debugger - good for when debugging."
Case "RUN"
pointer = 0
runscript mx

'---cont
Case "?CONT"
dbug mode, "CONT"
Case "??CONT"
dbug mode, "Continues after an error - not designed for use in scripts."
Case "CONT"
berr = 1

'---- setparent
Case "?SETPARENT"
dbug mode, "SETPARENT [windowhandle] [newparentwindowhandle]"

Case "??SETPARENT"
dbug mode, "Sets a window to have a new parent..."
dbug mode, ""
dbug mode, "First arg is the window handle (use findwindow or similar - "
dbug mode, "or one of the built in variables such as PATTERN)"
dbug mode, "and the second arg is the new parent handle found"
dbug mode, "in a similar fashion. (a value of 0 releases it from having a parent"

Case "SETPARENT"

Call SetParent(Val(Trim(variable(arg1$))), Val(Trim(variable(arg2$))))

'---- OPENREADFILE
Case "?OPENREADFILE"
dbug mode, "OPENREADFILE [filenumber] [filename]"
Case "??OPENREADFILE"
dbug mode, "opens file for reading, first arg is the ref number (must be unique)"
dbug mode, "second arg is path/filename"
Case "OPENREADFILE"

Open arg1$ For Input As Val(Trim(arg2$))

'---- OPENWRITEFILE
Case "?OPENWRITEFILE"
dbug mode, "OPENREADFILE [filenumber] [filename]"
Case "??OPENWRITEFILE"
dbug mode, "opens file for writing, first arg is the ref number (must be unique)"
dbug mode, "second arg is path/filename"
Case "OPENWRITEFILE"
Open arg1$ For Output As Val(Trim(arg2$))

'---- FILEWRITE
Case "?FILEWRITE"
dbug mode, "FILEWRITE [file ref] [string/text]"
Case "FILEWRITE"
Print #Val(Trim(arg1$)), variable(arg2$)

'---- FILEREAD
Case "?FILEREAD"
dbug mode, "FILEREAD [file ref] [destination string]"
Case "FILEREAD"
Line Input #Val(Trim(arg1$)), bb$
setstring arg2$, bb$

'---- CLOSEFILE
Case "?CLOSEFILE"
dbug mode, "CLOSEFILE [file ref]"
Case "CLOSEFILE"
Close Val(Trim(arg1$))

'---- WRITEARRAYTOCOLUMN
Case "?WRITEARRAYTOCOLUMN"
dbug mode, "WRITEARRAYTOCOLUMN"
Case "??WRITEARRAYTOCOLUMN"
dbug mode, "Bit buggy at moment, args dont make sense either - treat as experimental."

Case "WRITEARRAYTOCOLUMN"

row = Val(Trim(variable(arg1$)))
norow = Val(Trim(variable(arg2$)))
DoEvents

SendControlKey "0", PatternWindowHandle
'dbug mode, "getting current param"
    dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
tparam$ = Trim(param$)
''dbug mode, tparam$

noc = 0
Do
SendKeys ("{RIGHT}")
DoEvents

dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
    If tparam$ = param$ Then noc = noc + 1
Loop Until tparam$ <> param$
noc = noc + 1

'dbug mode, Str$(noc)
moorar$ = ""
For e = 1 To noc
moorar$ = moorar$ + "{LEFT}"
Next
SendKeys moorar$, 1

For ddd = 1 To norow

t$ = Hex(Val(inductarray$(row, ddd, 0)))
If dbg_disable <> 1 Then dbug mode, "writting array(" & Str$(row) & "," & Str$(ddd) & ",0) - " & t$
If Trim(t$) <> "FFFFFFFF" Then
ln = noc - Len(t$)
If ln > 0 Then
Do
'DoEvents
t$ = "0" + t$
'dbug mode, t$
Loop Until Len(t$) = noc
End If

'dbug mode, "---------"
'dbug mode, t$
If dbg_disable <> 1 Then dbug mode, "Padded output to :" & t$
'SendControlKey "0", PatternWindowHandle
DoEvents

For pd = 1 To Len(t$)
goto_row (ddd)
SendKeys (Mid$(t$, pd, 1))
'goto_row (ddd)
SendKeys ("{RIGHT}")
Next
For pd = 1 To Len(t$)
'goto_row (ddd)
SendKeys ("{LEFT}")
Next
  Else
    'SendKeys (".")
    'SendKeys ("{DOWN}")
    End If
'SendKeys ("{DOWN}")
DoEvents
Next
SendControlKey "1", PatternWindowHandle
DoEvents

'---- WRITEARRAYTONOTECOLUMN
Case "?WRITEARRAYTONOTECOLUMN"
dbug mode, "WRITEARRAYTONOTECOLUMN"

Case "??WRITEARRAYTONOTECOLUMN"
dbug mode, "Writes array to column in pattern. works, but not really gonna stay is for too long. dont base scripts on it for a while."
Case "WRITEARRAYTONOTECOLUMN"

SendControlKey "1", PatternWindowHandle
DoEvents
row = Val(Trim(variable(arg1$)))
norow = Val(Trim(variable(arg2$)))
DoEvents
For ddd = 1 To norow
    t$ = convert_english(inductarray$(row, ddd, 0))
    'dbug mode, t$
    t2$ = Left$(t$, 2)
    oc = Val(Right$(t$, 1))
    k$ = "/////////"
    For d = 0 To oc
        k$ = k$ + "*"
    Next d

k$ = Left$(k$, Len(k$) - 1)

If t2$ = "C-" Then k$ = k$ + "z"
If t2$ = "C#" Then k$ = k$ + "s"
If t2$ = "D-" Then k$ = k$ + "x"
If t2$ = "D#" Then k$ = k$ + "d"
If t2$ = "E-" Then k$ = k$ + "c"
If t2$ = "F-" Then k$ = k$ + "v"
If t2$ = "F#" Then k$ = k$ + "g"
If t2$ = "G-" Then k$ = k$ + "b"
If t2$ = "G#" Then k$ = k$ + "h"
If t2$ = "A-" Then k$ = k$ + "n"
If t2$ = "A#" Then k$ = k$ + "j"
If t2$ = "B-" Then k$ = k$ + "m"
If t2$ = "OF" Then k$ = k$ + "1"
If t2$ = ".." Then k$ = k$ + "."
'dbug mode, k$
SendKeys k$, 1
DoEvents

Next
'SendControlKey "1", PatternWindowHandle

'--- READCOLUMNINTOARRAY

Case "READCOLUMNINTOARRAY"

row = Val(Trim(variable(arg1$)))
SendKeys "{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}"
norow = Val(Trim(variable(arg2$)))
For d = 1 To norow


    dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
        rchar = SendMessage(WindowHandle, 19034, 9, 0)
        If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
   
   'setstring "PARAM", param$
   If Left$(param$, 4) <> "Note" Then
    dummy = SendMessage(WindowHandle, 19034, 10, 0)
    rchar = 1
    paramv$ = ""
    Do While rchar <> 0
          
       rchar = SendMessage(WindowHandle, 19034, 11, 0)
        If rchar <> 0 Then paramv$ = paramv$ + Chr$(rchar)
    Loop
    End If
    If Len(Trim(paramv$)) > 0 Then
    lb = InStr(paramv$, "(")
    rb = InStr(lb, paramv$, ")")
    mm = rb - lb
    brak$ = Mid$(paramv$, lb + 1, mm - 1)
    auto_size_array row, d, 0
    If dbg_disable <> 1 Then dbug mode, "array(" & Str$(row) & "," & Str$(d) & ",0) is being set too:(1)" & Trim(Str$(brak$))
    inductarray$(row, d, 0) = Trim(Str$(brak$))
    If dbg_disable <> 1 Then dbug mode, "array(" & Str$(row) & "," & Str$(d) & ",0) has just been set too:(1)" & Trim(Str$(brak$))
    'dbug mode, "ia " + Str$(d) + " set to " + Trim(Str$(brak$))
    End If
    If Len(Trim(paramv$)) = 0 Then
    auto_size_array row, d, 0
    If dbg_disable <> 1 Then dbug mode, "array(" & Str$(row) & "," & Str$(d) & ",0) is being set too:(2) -1"
    inductarray$(row, d, 0) = "-1"
    If dbg_disable <> 1 Then dbug mode, "array(" & Str$(row) & "," & Str$(d) & ",0) has just been set too:(2) -1"
    'dbug mode, "ia " + Str$(d) + " set to -1"
    End If
    If Left$(param$, 4) = "Note" Then
    auto_size_array row, d, 0
    If dbg_disable <> 1 Then dbug mode, "array(" & Str$(row) & "," & Str$(d) & ",0) is being set too:(3)" & Trim(Str$(NoteUnderCursor()))
    inductarray$(row, d, 0) = Trim(Str$(NoteUnderCursor()))
    If dbg_disable <> 1 Then dbug mode, "array(" & Str$(row) & "," & Str$(d) & ",0) has just been set too:(3)" & Trim(Str$(NoteUnderCursor()))
    'dbug mode, "ia " + Str$(d) + " set to " + Trim(Str$(NoteUnderCursor()))
    End If
    SendKeys "{DOWN}", 0
    DoEvents
  
Next

'--- rem ----
Case "?INCLUDE"
dbug mode, "INCLUDE [filename]"

Case "??INCLUDE"
dbug mode, "Makes no sense to use real time: includes another file withing currect script."
Case "INCLUDE"


'--- rem ----
Case "?REM", "?'", "?;"
dbug mode, "REM [remark]"

Case "??REM", "??'", "??;"
dbug mode, "place a remark in your code. REM,' and ; are all valid 'remark' identifiers. Lines starting with these are ignored."

Case "REM", "'", ";"

Case "?DEBUGARRAY"
dbug mode, "DEBUGARRAY"

Case "??DEBUGARRAY"
dbug mode, "Ouputs the current arrays to text files on C drive in CSV format."

Case "DEBUGARRAY"

dbug mode, "Writting Array debug files..."
For z = 0 To max_array_z
dbug mode, "Array debug file " & Str$(z)
da_fn$ = "c:\bs_array_debug_" & Trim(Str$(z)) & ".csv"
Open da_fn$ For Output As 9 + z
For y = 0 To max_array_y
For x = 0 To max_array_x
Print #9 + z, inductarray$(x, y, z) & ",";
Next
Print #9 + z, "xxx" & vbCrLf
Next
Close #9 + z
Next
dbug mode, "all done!..."
'inductarray$(8192, 1024, 4)

Case "?EXECOFF"
dbug mode, "EXECOFF"

Case "??EXECOFF"
dbug mode, "disables executable tracing to speed up execution without disabling debug output."


Case "EXECOFF"
exec_disable = 1


Case "?EXECON"
dbug mode, "EXECON"

Case "??EXECON"
dbug mode, "enables executable tracing. slows down execution but increases debug output."

Case "EXECON"
exec_disable = 0


Case "?DEBUGOFF"
dbug mode, "DEBUGOFF"

Case "??DEBUGOFF"
dbug mode, "Disables debugging and tracing. Should only be used for final projects, much quicker but impossible to escape loops of death etc."

Case "DEBUGOFF"
dbug mode, "DEBUG DISABLED"
dbg_disable = 1


Case "DEBUGTOFILE"
dbug mode, "DEBUG REDIRECTED TO " + allarg$
debugtofile$ = all_arg$


Case "DEBUGON"
dbg_disable = 0
dbug mode, "DEBUG RE-ENABLED"


'---- mtoff
Case "MTOFF"
mt_disable = 1


'---- mton
Case "MTON"
mt_disable = 0


'--- rem ----
Case "GETBUZZWINDOWTITLE"

setstring "buzzwindowtitle", BuzzTitle
templeft = InStr(BuzzTitle, " - Buzz")
setstring "songname", Left$(BuzzTitle, templeft - 1)

'--- idle
Case "IDLE"

If UCase$(arg1$) = "WINDOWED" Then
    WinIdle = True
Else
    idle = True
End If

'arraypointer
Case "SETARRAYPOINTER"

 ap_x = Val(Trim(variable(arg1$)))
 ap_y = Val(Trim(variable(arg2only$)))
 ap_z = Val(Trim(variable(arg3$)))
 If dbg_disable <> 1 Then dbug mode, "array pointer is at " & Str$(ap_x) & "," & Str$(ap_y) & "," & Str$(ap_z) & ")"
auto_size_array ap_x, ap_y, ap_z



'--- getarray
Case "GETARRAY"

If dbg_disable <> 1 Then dbug mode, "Getting array value (" & Str$(ap_x) & "," & Str$(ap_y) & "," & Str$(ap_z) & ") with value:" & inductarray$(ap_x, ap_y, ap_z) & "."
auto_size_array ap_x, ap_y, ap_z
setstring arg1$, inductarray$(ap_x, ap_y, ap_z)


'--- setarray
Case "SETARRAY"

If dbg_disable <> 1 Then dbug mode, "Setting array(" & Str$(ap_x) & "," & Str$(ap_y) & "," & Str$(ap_z) & ") with value:" & variable(arg1$) & "."
auto_size_array ap_x, ap_y, ap_z
inductarray$(ap_x, ap_y, ap_z) = variable(arg1$)

'--- setarraysize
Case "SETARRAYSIZE"

sz_x = Val(Trim(variable(arg1$)))
sz_y = Val(Trim(variable(arg2only$)))
sz_z = Val(Trim(variable(arg3$)))
If dbg_disable <> 1 Then dbug mode, "Setting array Size(" & Str$(sz_x) & "," & Str$(sz_y) & "," & Str$(sz_z) & ")"
If dbg_disable <> 1 Then dbug mode, "Entering Redim"
auto_size_array sz_x, sz_y, sz_z
If dbg_disable <> 1 Then dbug mode, "Returned from redim"
setstring "arrayresize", "ok"
max_array_x = sz_x
max_array_y = sz_y
max_array_z = sz_z

'---findwindow
Case "FINDWINDOWTITLE"

moo = FindWindowLike(hwnds(), 0, arg2$, "*")
setstring arg1$, WinTitle(hwnds(1))

'---findwindow
Case "FINDWINDOW"

moo = FindWindowLike(hwnds(), 0, arg2$, "*")
setstring arg1$, Trim(Str$(hwnds(1)))

'---STARTDDE
Case "STARTDDE"

'Load mainform
linkt$ = "BS" + Trim(Str$(Trim(Val(all_arg$))))
dbug mode, "STARTING DDE SERVER"
dbug mode, linkt$
dbug mode, "-------------------"
mainform.LinkTopic = linkt$
mainform.LinkMode = 1
dbug mode, mainform.LinkTopic


'---- UPDATEpattern ----
Case "UPDATEPATTERN"

    dummy = SendMessage(WindowHandle, 19034, 3, 0)
    setstring "ROWS", Trim(Str$(GetNumberOfRows()))
    setstring "TRACKS", Trim(Str$(GetNumberOfTracks()))
    setstring "BLOCKSTART", Trim(Str$(BlockStart()))
    setstring "BLOCKEND", Trim(Str$(BlockEnd()))
    If GlobalExist() = 1 Then
        setstring "GLOBALEXIST", "YES"
    Else
        setstring "GLOBALEXIST", "NO"
    End If

'---- getparam -----
Case ""


Case "GETPARAM"


    dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
        rchar = SendMessage(WindowHandle, 19034, 9, 0)
        If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
   
   setstring "PARAM", param$
   If Left$(param$, 4) <> "Note" Then
    dummy = SendMessage(WindowHandle, 19034, 10, 0)
    rchar = 1
    paramv$ = ""
    Do While rchar <> 0
          
       rchar = SendMessage(WindowHandle, 19034, 11, 0)
        If rchar <> 0 Then paramv$ = paramv$ + Chr$(rchar)
    Loop
    End If
    If Len(Trim(paramv$)) > 0 Then
    lb = InStr(paramv$, "(")
    rb = InStr(lb, paramv$, ")")
    mm = rb - lb
    brak$ = Mid$(paramv$, lb + 1, mm - 1)
    setstring "PARAMVALUE", Trim(Str$(brak$))
    End If
    If Len(Trim(paramv$)) = 0 Then setstring "PARAMVALUE", "-1"
    If Left$(param$, 4) = "Note" Then setstring "PARAMVALUE", Trim(Str$(NoteUnderCursor()))
    
'---- settpb
Case "SETTPB"
    
    SetTPB all_arg$
    
'----setbpm

Case "SETBPM"
    
    SetBPM all_arg$

'----label
Case "LABEL"


'---- show debug
Case "SHOWDEBUG"

showdbug

showdebug = 1

'---- hide debug
Case "HIDEDEBUG"

Form1.Hide
showdebug = 0

'---- converttoenglish ----
Case "CONVERTTOENGLISH"

    setstring arg2$, convert_english(variable(arg1$))

'---- deletefile ----
Case "DELETEFILE"


    Kill variable(arg1$)

'---- setstep
Case "SETSTEP"

DoEvents
SendControlKey variable(arg1$), PatternWindowHandle
DoEvents


'---- converttokeys ----
Case "CONVERTTOKEYS"

    t$ = convert_english(variable(arg1$))
    t2$ = Left$(t$, 2)
    oc = Val(Right$(t$, 1))
    k$ = "/////////"
    For d = 0 To oc
        k$ = k$ + "*"
    Next d

k$ = Left$(k$, Len(k$) - 1)

If t2$ = "C-" Then k$ = k$ + "z"
If t2$ = "C#" Then k$ = k$ + "s"
If t2$ = "D-" Then k$ = k$ + "x"
If t2$ = "D#" Then k$ = k$ + "d"
If t2$ = "E-" Then k$ = k$ + "c"
If t2$ = "F-" Then k$ = k$ + "v"
If t2$ = "F#" Then k$ = k$ + "g"
If t2$ = "G-" Then k$ = k$ + "b"
If t2$ = "G#" Then k$ = k$ + "h"
If t2$ = "A-" Then k$ = k$ + "n"
If t2$ = "A#" Then k$ = k$ + "j"
If t2$ = "B-" Then k$ = k$ + "m"
If t2$ = "OF" Then k$ = k$ + "1"
If t2$ = ".." Then k$ = k$ + "."

setstring arg2$, k$

'---- GOTOCOLUMN ----
Case "GOTOCOLUMN"
SendKeys ("{HOME}{HOME}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
DoEvents

If Val(Trim(variable(arg1$))) > 0 Then
If dbg_disable <> 1 Then dbug mode, "Going to find column"
For d = 1 To Val(Trim(variable(arg1$)))
SendKeys ("{right}")
DoEvents
Next d

End If

'---- FINDCOLUMN ----
Case "FINDCOLUMN"

pos = 0
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")

dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    If pos > 1000 Then
    dummy = MsgBox("Invinite Loop - Killing Script!!!", vbCritical, "Buzzscript DEATH!!!!")
    End
    End If
    Loop
If InStr(param$, arg1$) = 0 Then
Do
SendKeys "{right}", 1
pos = pos + 1
 dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    If pos > 1000 Then
    dummy = MsgBox("Invinite Loop - Killing Script!!!", vbCritical, "Buzzscript DEATH!!!!")
    End
    End If
    Loop
Loop Until InStr(param$, arg1$) > 0
End If
setstring arg2$, Str$(pos)

'---- FINDVOLUME ----
Case "FINDVOLUME"

pos = 0
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
SendKeys ("{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}")
DoEvents
ec = 0
infect = False
Do
SendKeys "{right}", 1
pos = pos + 1
 dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    If pos > 1000 Then
    dummy = MsgBox("Invinite Loop - Killing Script!!!", vbCritical, "Buzzscript DEATH!!!!")
    End If
    Loop
    
    If InStr(param$, "vol") > 0 Or InStr(param$, "Vol") > 0 Or InStr(param$, "Vel") > 0 Then ec = 1
    If InStr(param$, "Sub") = 1 Then
    ec = 0
    infect = True
    End If
    If InStr(param$, "ity Se") = 1 Then ec = 0
    ex = 0
    If ec = 1 Then ex = 1
Loop Until ex = 1
'If infect = True Then pos = pos - 1
setstring arg1$, Str$(pos)

'---- FINDNEXTCOLUMN ----
Case "FINDNEXTCOLUMN"

pos = 0
Do
SendKeys "{right}", 1
pos = pos + 1
 dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0 And pos < 1000
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
    If pos = 1000 Then pos = -1
Loop Until InStr(param$, arg1$) > 0
setstring arg2$, Str$(pos)


'---- GOTOROW ----
Case "GOTOROW"

SendKeyString "^1", Val(Trim(variable("winhandle")))
row = Val(Trim(variable(arg1$)))
If dbg_disable <> 1 Then dbug mode, "GO TO ROW " + Str$(row)
cr = 0
SendKeys ("{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}{PGUP}")
Do
SendKeys "{PGDN}", 1
cr = cr + 16
Loop Until cr > row - 16
'
Do
SendKeys "{DOWN}", 1
cr = cr + 1
Loop Until cr = row

'---- debug ----
Case "DEBUG"

'Print #2, variable(all_arg$)
dbug mode, variable(all_arg$)

'---- setjump
Case "SETJUMP"

DoEvents
SendControlKey variable(arg1$), PatternWindowHandle
DoEvents

'---- enterparam ----
Case "ENTERPARAM"

'SendKeyString "^1", Val(Trim(variable("winhandle")))
SendControlKey "0", PatternWindowHandle
If dbg_disable <> 1 Then dbug mode, "getting current param"
    dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
tparam$ = Trim(param$)
If dbg_disable <> 1 Then dbug mode, tparam$

noc = 0
Do
SendKeys ("{RIGHT}")
DoEvents

dummy = SendMessage(WindowHandle, 19034, 8, 0)
    rchar = 1
    param$ = ""
    Do While rchar <> 0
    rchar = SendMessage(WindowHandle, 19034, 9, 0)
    If rchar <> 0 Then param$ = param$ + Chr$(rchar)
    Loop
    If tparam$ = param$ Then noc = noc + 1
Loop Until tparam$ <> param$
noc = noc + 1

If dbg_disable <> 1 Then dbug mode, Str$(noc)
t$ = Trim(variable(arg1$))
moorar$ = ""
For e = 1 To noc
moorar$ = moorar$ + "{LEFT}"
Next

SendKeys moorar$, 1
If dbg_disable <> 1 Then dbug mode, moorar$

ln = noc - Len(t$)
If ln > 0 Then
Do
DoEvents
t$ = "0" + t$
If dbg_disable <> 1 Then dbug mode, t$
Loop Until Len(t$) = noc
End If

If dbg_disable <> 1 Then dbug mode, "---------"
If dbg_disable <> 1 Then dbug mode, t$


For pd = 1 To Len(t$)
SendKeys (Mid$(t$, pd, 1))
SendKeys ("{RIGHT}")
Next
For pd = 1 To Len(t$)
SendKeys ("{LEFT}")
Next

DoEvents
SendControlKey "1", PatternWindowHandle
DoEvents

'---- combine ----
Case "COMBINE"

v1$ = variable(arg1$)
v2$ = variable(arg2$)
setstring arg1$, v1$ + v2$

'---- combine ----
Case "LEFTCOMBINE"

v1$ = variable(arg1$)
v2$ = variable(arg2$)
setstring arg1$, v2$ + v1$

'----- left
Case "LEFT"

If dbg_disable <> 1 Then dbug mode, Trim(variable(arg2$))
temp$ = Left$(variable(arg1$), Val(Trim(variable(arg2$))))
setstring arg1$, temp$

'----- substring
Case "SUBSTRING"

arg2$ = UCase$(arg2$)

topos = InStr(arg2$, " TO ")
xxx$ = Left$(arg2$, topos - 1)
topos = topos + 4
yyy$ = Right$(arg2$, Len(arg2$) - topos)
ss_start = Val(xxx$)
ss_end = Val(yyy$) - ss_start
setstring arg1$, Mid$(arg1$, ss_start, ss_end)

'--- len
Case "LENGTH"

setstring arg1$, Trim(Str$(Len(variable(arg2$))))

'--- trim
Case "TRIM"

setstring arg1$, Trim(variable(arg1$))

'--- lefttrim
Case "LEFTTRIM"

setstring arg1$, LTrim(variable(arg1$))

'--- righttrim
Case "RIGHTTRIM"

setstring arg1$, RTrim(variable(arg1$))

'----- right
Case "RIGHT"

temp$ = Right$(variable(arg1$), Val(Trim(variable(arg2$))))
setstring arg1$, temp$

'---- filebrowse ----
Case "FILEBROWSE"

mainform.CommonDialog1.Filter = all_arg$
mainform.CommonDialog1.ShowOpen
freq$ = mainform.CommonDialog1.FileName

setstring "DIALOG", freq$

'---- iseof ----
Case "ISEOF"


izgreater = False
izless = False
izequal = False
iznotequal = False

If EOF(Val(Trim(arg1$))) = True Then
izequal = True
izfalse = False
End If

If EOF(Val(Trim(arg1$))) <> True Then
izequal = False
izfalse = True
End If

'---- compare ----
Case "COMPARE"

v1$ = variable(arg1$)
v2$ = variable(arg2$)

n1 = Val(v1$)
n2 = Val(v2$)

izgreater = False
izless = False
izequal = False
iznotequal = False

If n1 > n2 Then izgreater = True
If n2 > n1 Then izless = True
If v1$ = v2$ Then izequal = True
If v1$ <> v2$ Then iznotequal = True

'---- isgreater ----
Case "ISGREATER"

If izgreater = True Then pointer = Val(Trim(variable(all_arg$)))

'---- isless ----
Case "ISLESS"

If izless = True Then pointer = Val(Trim(variable(all_arg$)))

'---- isequal ----
Case "ISEQUAL"

If izequal = True Then pointer = Val(Trim(variable(all_arg$)))

'---- isnoequal ----
Case "ISNOTEQUAL"

If iznotequal = True Then pointer = Val(Trim(variable(all_arg$)))

'---- hostactive ----
Case "HOSTACTIVATE"

SetForegroundWindow WindowHandle

'---- activate ----
Case "ACTIVATE"

If dbg_disable <> 1 Then dbug mode, "ACTIVATE: 0x" & Hex(winhandle)
SetForegroundWindow winhandle
'bignic - why use the BS variable, when we already have a vb variable?
'Val(Trim(variable("winhandle")))

'---- addspace ----
Case "ADDSPACE"

For d = 1 To Val(Trim(variable(arg2$)))
n$ = variable(arg1$) & " "
Next d
setstring arg1$, n$
'---- active ----
Case "APPACTIVATE"

AppActivate variable(all_arg$)

'---- pause ----
Case "PAUSE"

PauseTime = Val(variable(all_arg$))
   start = Timer   ' Set start time.
   Do While Timer < start + PauseTime
      DoEvents   ' Yield to other processes.
   Loop

'---- yesno ----
Case "YESNO"

Dialog = MsgBox(all_arg$, vbYesNo)
If Dialog = 6 Then setstring "DIALOG", "YES"
If Dialog = 7 Then setstring "DIALOG", "NO"

'----INCloop
Case "INCLOOP"


n1 = Val(Trim(variable(arg1$)))
n2 = 1
nv = n2 + n1
setstring arg1$, Str$(nv)

If Val(variable(arg2$)) >= Val(variable(arg1$)) Then pointer = Val(variable(arg3$))

'----DECloop
Case "DECLOOP"

n1 = Val(Trim(variable(arg1$)))
nv = n1 - 1
setstring arg1$, Str$(nv)

If Val(variable(arg1$)) >= Val(variable(arg2$)) Then pointer = Val(variable(arg3$))

'---- goto ----
Case "GOTO"

If dbg_disable <> 1 Then dbug mode, "go to parsed"
pointer = Val(variable(all_arg$))

'---- gosub ----
Case "GOSUB"

If dbg_disable <> 1 Then dbug mode, "gosub"
go_sub(recurs) = pointer
If dbg_disable <> 1 Then dbug mode, "SET GO_SUB (pos " + Str$(recurse) + ") to " + Str$(pointer)
pointer = Val(variable(all_arg$))
recurs = recurs + 1

'---- return
Case "RETURN"

If dbg_disable <> 1 Then dbug mode, "return"
recurs = recurs - 1
pointer = go_sub(recurs)
If dbg_disable <> 1 Then dbug mode, "Returning to " + Str$(go_sub(recurs)) + " using recurese point " + Str$(recurs)


'---- info ----
Case "INFO"

temp$ = variable(all_arg$)
SetForegroundWindow Form2.hwnd

MsgBox (temp$)

'---- runapp ----
Case "RUNAPP"

dummy = Shell(variable(all_arg$), vbNormalFocus)

'---- runandwait ----
Case "RUNANDWAIT"

OpenProcess (variable(all_arg$))

'---- SAVE!VARS ----
Case "SAVE!VARS"

odir$ = cd$ + "\inductavars.txt"
dbug mode, "SUBSCRIPT INIT -----"
dbug mode, "closing"
'MsgBox (odir$)
Open odir$ For Output As 5
dbug mode, "file opened"
For d = 1 To MX_VAR
If Left$(var$(d, 1), 1) = "!" Then
ttt$ = var$(d, 1) + "," + var$(d, 2)
Print #5, ttt$
End If
Next
Close #5
dbug mode, "file closed"

'---- CALLSUBSCRIPT ----
Case "CALLSUBSCRIPT"

ssstring$ = cd$ + "\buzzscript.exe " + Str$(WindowHandle) + " " + cd$ + "\" + variable(all_arg$)
dbug mode, ssstring$
Shell (ssstring$)

'---- LOAD!vars
Case "LOAD!VARS"


  If (Dir$(cd$ & "\inductavars.txt") = "") Then
        Dim intFileEnsurer As Integer
        intFileEnsurer = FreeFile
        Open cd$ & "\inductavars.txt" For Append Lock Write As #intFileEnsurer
        Close #intFileEnsurer
    End If

'read in inductavars-------------------
 poop$ = cd$ + "\inductavars.txt"
Open poop$ For Input As 3
dbug mode, "inductavars init"
While Not EOF(3)
Input #3, vn$, vval$
setstring vn$, vval$
Wend
Close #3
dbug mode, "inductavars end"

'---- question ----
Case "QUESTION"

dbox.Label1.Caption = variable(all_arg$)

dbox.Show
dbox.Text1.text = ""

dbox_txt$ = ""
Do While dbox_txt$ = ""
    DoEvents
Loop
dbox.Hide

setstring "DIALOG", dbox_txt$

'---- setstring ----
Case "?SETSTRING"
dbug mode, "SETSTRING [variable] [string or other variable]"

Case "??SETSTRING"
dbug mode, "Set a string with either a string of text (or variables) or another variable. For example:"
dbug mode, ""
dbug mode, "  SETSTRING var hello world"
dbug mode, "this sets the 'var' string to 'hello world' this could be followed up with:"
dbug mode, ""
dbug mode, "  SETSTRING newvar var"
dbug mode, "which would set 'newvar' to 'hello world' (since thats whats in the 'var' string)"

Case "SETSTRING"

setstring (t_arg(1)), (variable(arg2$))

'---- endscript ----
Case "ENDSCRIPT"

die

'---- COS ----
Case "COS"

setstring arg2$, Str$(Cos(Val(Trim(variable(arg1$)))))

'---- SIN ----
Case "SIN"

setstring arg2$, Str$(Sin(Val(Trim(variable(arg1$)))))

'---- TAN ----
Case "TAN"

setstring arg2$, Str$(Tan(Val(Trim(variable(arg1$)))))

'---- LOG ----
Case "LOG"

setstring arg2$, Str$(Log(Val(Trim(variable(arg1$)))))


'---- add ----
Case "ADD"

n1 = Val(Trim(variable(arg1$)))
n2 = Val(Trim(variable(arg2$)))
nv = n2 + n1
setstring arg2$, Str$(nv)

'---- sub ----
Case "SUBTRACT"

n1 = Val(Trim(variable(arg1$)))
n2 = Val(Trim(variable(arg2$)))
nv = n2 - n1
setstring arg2$, Str$(nv)

'---- mul ----
Case "MULTIPLY"

n1 = Val(Trim(variable(arg1$)))
n2 = Val(Trim(variable(arg2$)))
nv = n2 * n1
setstring arg2$, Str$(nv)

'---- div ----
Case "DIVIDE"

n1 = Val(Trim(variable(arg1$)))
n2 = Val(Trim(variable(arg2$)))
nv = n2 / n1
setstring arg2$, Str$(nv)

'---- setactivescreen -----
Case "SETACTIVESCREEN"

winhandle = Val(Trim(variable(all_arg$)))
If all_arg$ = "PATTERN" Then winhandle = PatternEditorWindow
If all_arg$ = "SEQ" Then winhandle = SequencerEditorWindow
If all_arg$ = "WAVTAB" Then winhandle = WavTabWindow
setstring "winhandle", Trim(Str$(winhandle))
'----- hex -----
Case "DEC"

spud = Val(Trim(variable(arg2$)))
spudee$ = Val("&h" & spud & "&")
setstring arg1$, spudee$

'----- dec -----
Case "HEX"

ttemp$ = variable(arg2$)
spudee$ = Hex(Val(ttemp$))
setstring arg1$, spudee$

'---- rightclick -----
Case "RIGHTCLICK"

openMenu (1)

'----pressbutton
Case "PRESSBUTTON"

dummy = SendMessage(Val(variable(all_arg$)), WM_LMOUSEDOWN, 0, 0)
PauseTime = 0.5
   start = Timer   ' Set start time.
   Do While Timer < start + PauseTime
      DoEvents   ' Yield to other processes.
   Loop
dummy = SendMessage(Val(variable(all_arg$)), WM_LMOUSEUP, 0, 0)

'---- sendmessage
Case "SENDMESSAGE"

SendMessage Val(Trim(variable(arg1$))), WM_SETTEXT, 0, ByVal variable(arg2$)

'---- key ----
Case "KEY"

SendKeyString all_arg$, Val(Trim(variable("winhandle")))

'---- ctrlkey ----
Case "CTRLKEY"

SendControlKey all_arg$, Val(Trim(variable("winhandle")))

'dumpvars
Case "DUMPVARS"

dumpvar

'--- vbkey ---
Case "VBKEY"

SendKeys (variable(all_arg$))

Case "SAVE"

SaveSong all_arg$, WindowHandle

Case "SAVEBMW"

SaveSongbmw all_arg$, WindowHandle

'---- anchor ----
Case "ANCHOR"

    MakeNewAnchor = True
    If AnchorIndex > 1 Then
        For i = 1 To AnchorIndex - 1  'check to see if its a duplicate
            If anchorForm(i).Text1.text = Val(Trim(variable(arg1$))) Then MakeNewAnchor = False
        Next i
    End If
    If MakeNewAnchor = True Then
            Set anchorForm(AnchorIndex) = New Form2
            anchorForm(AnchorIndex).Text1.text = Val(Trim(variable(arg1$)))
            anchorForm(AnchorIndex).Command1.Caption = arg2$
            anchorForm(AnchorIndex).AnchorIndex.text = AnchorIndex
            anchorForm(AnchorIndex).Show
            AnchorIndex = AnchorIndex + 1
    End If

'---- slowmo
Case "SLOWMO"

slowmo = 1

'---- dropnote
Case "DROPNOTE"

SendControlKey "-", winhandle

'---- dropnote
Case "RAISENOTE"

SendControlKey "+", winhandle


'---- copy
Case "COPY"

SendControlKey "C", winhandle

'---- paste

Case "PASTE"

SendControlKey "V", winhandle

'--- CUT
Case "CUT"

SendControlKey "X", winhandle

'--- block start
Case "BLOCKSTART"

'SendControlKey "B", PatternWindowHandle
'dbug mode, "Blockstart: 0x" & Hex(PatternWindowHandle)
'dbug mode, "windhandle: 0x" & Hex(windhandle)
SendControlKey "B", winhandle
If dbg_disable <> 1 Then dbug mode, "Blockstart: 0x" & Hex(winhandle)

'---- block end
Case "BLOCKEND"

'SendControlKey "E", PatternWindowHandle
'dbug mode, "Blockend: 0x" & Hex(PatternWindowHandle)
SendControlKey "E", winhandle
If dbg_disable <> 1 Then dbug mode, "Blockend: 0x" & Hex(winhandle)
'MsgBox Hex(winhandle)

'----unselect
Case "UNSELECT"

SendControlKey "U", PatternWindowHandle

'----interpolate
Case "INTERPOLATE"

SendControlKey "I", PatternWindowHandle

'----setnotejump
Case "SETNOTEJUMP"

SendControlKey all_arg$, PatternWindowHandle

'----addtrack
Case "ADDTRACK"

    AddDel "ADD", "TRACK", PatternWindowHandle
Case "DELTRACK", "DELETETRACK"
    AddDel "DEL", "TRACK", PatternWindowHandle

'----copypattern
Case "COPYPATTERN"

    CopyPattern all_arg$, PatternWindowHandle
Case "ADDPATTERN"

    AddDel "ADD", "PATTERN", PatternWindowHandle, all_arg$
Case "DELPATTERN", "DELETEPATTERN"

    AddDel "DEL", "PATTERN", PatternWindowHandle

' BUZZSCRIPT WINDOWING 1.0 API
'
Case "WINDOW_CREATE"
    
    'Dim frmBuf As frmCustom1
    'Dim objBuf As clsCustomFormEntry
    On Error Resume Next
    If GUIForms(arg1$) Is Nothing Then
        Set frmbuf = New frmCustom1
        Set objBuf = New clsCustomFormEntry
        objBuf.FormHandle = arg1$
        objBuf.FormCaption = variable(GetKeyValueEntry(arg2$, "name"))
                frmbuf.Caption = objBuf.FormCaption
        frmbuf.MyHandle = objBuf.FormHandle
        If (GetKeyValueEntryInt(arg2$, "width") <> -1) Then
            frmbuf.Width = GetKeyValueEntryInt(arg2$, "width") * Screen.TwipsPerPixelX
        End If
        If (GetKeyValueEntryInt(arg2$, "height") <> -1) Then
            frmbuf.Height = GetKeyValueEntryInt(arg2$, "height") * Screen.TwipsPerPixelY
        End If
        If (GetKeyValueEntry(arg2$, "icon") <> "") Then
            Set frmbuf.Icon = LoadPicture(cd$ & "\" & variable(GetKeyValueEntry(arg2$, "icon")))
        End If
        If (GetKeyValueEntry(arg2$, "background-image") <> "") Then
            Set frmbuf.Picture = LoadPicture(cd$ & "\" & variable(GetKeyValueEntry(arg2$, "background-image")))
        End If
        If LCase$(variable(GetKeyValueEntry(arg2$, "maximise"))) <> "true" Then frmbuf.MaxButton = False
        frmbuf.BorderStyle = 2
        
        frmbuf.Show
        If (GetKeyValueEntryInt(arg2$, "x") <> -1) Then
            frmbuf.Left = GetKeyValueEntryInt(arg2$, "x") * Screen.TwipsPerPixelX
        End If
        If (GetKeyValueEntryInt(arg2$, "y") <> -1) Then
            frmbuf.Top = GetKeyValueEntryInt(arg2$, "y") * Screen.TwipsPerPixelY
        End If
        Select Case (variable(GetKeyValueEntry(arg2$, "state")))
        Case "normal"
            frmbuf.WindowState = vbNormal
        Case "maximize"
            frmbuf.WindowState = vbMaximized
        Case "maximized"
            frmbuf.WindowState = vbMaximized
        Case "minimize"
            frmbuf.WindowState = vbMinimized
        Case "minimized"
            frmbuf.WindowState = vbMinimized
        End Select
        Select Case (variable(GetKeyValueEntry(arg2$, "gadgets")))
        Case "disable"
        frmbuf.ControlBox = False
        If Len(variable(GetKeyValueEntry(arg2$, "border"))) > 0 Then
        brdstyle = variable(GetKeyValueEntry(arg2$, "gadgets")) + 0
        frmbuf.BorderStyle = 0
        End If
        End Select
        Set objBuf.FormItself = frmbuf
        GUIForms.Add objBuf, arg1$
    Else
        GUIForms(arg1$).FormItself.SetFocus
    End If
    Err.Clear
    On Error GoTo 0
    
'----------------------------
'--ATTEMPT AT BORDERLESSWINDOW - FOR USE WITH DOCKING
'---------------------------

'----------------------------------
'boo's mod's to cyans loverly code
'----------------------------------

Case "ENVELOPE_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddEnvelope variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "name")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0

'----------------------
' end of boo's mods'
'------------------------------
    
Case "BUTTON_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddButton variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "name")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "LABEL_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddLabel variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "name")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTFIELD_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddTextField variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "name")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTAREA_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddTextArea variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "name")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTPASSWORD_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddTextPassword variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "name")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "IMAGE_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddImage variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "image")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "LISTBOX_CREATE"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddList variable(GetKeyValueEntry(arg2$, "handle")), GetKeyValueEntry(arg2$, "jump"), GetKeyValueEntryInt(arg2$, "x"), GetKeyValueEntryInt(arg2$, "y"), GetKeyValueEntryInt(arg2$, "width"), GetKeyValueEntryInt(arg2$, "height")
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTFIELD_SETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.SetTextFieldValue variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "text"))
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTAREA_SETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.SetTextAreaValue variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "text"))
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTPASSWORD_SETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.SetTextPasswordValue variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "text"))
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTFIELD_GETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        setstring GetKeyValueEntry(arg2$, "destvariable"), GUIForms(arg1$).FormItself.GetTextFieldValue(variable(GetKeyValueEntry(arg2$, "handle")))
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTAREA_GETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        setstring GetKeyValueEntry(arg2$, "destvariable"), GUIForms(arg1$).FormItself.GetTextFieldValue(variable(GetKeyValueEntry(arg2$, "handle")))
    End If
    Err.Clear
    On Error GoTo 0
Case "TEXTPASSWORD_GETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        setstring GetKeyValueEntry(arg2$, "destvariable"), GUIForms(arg1$).FormItself.GetTextFieldValue(variable(GetKeyValueEntry(arg2$, "handle")))
    End If
    Err.Clear
    On Error GoTo 0
Case "LISTBOX_GETSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        setstring GetKeyValueEntry(arg2$, "destvariable"), GUIForms(arg1$).FormItself.GetListString(variable(GetKeyValueEntry(arg2$, "handle")))
    End If
    Err.Clear
    On Error GoTo 0
Case "LISTBOX_ADDSTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.AddListString variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "text"))
    End If
    Err.Clear
    On Error GoTo 0
Case "LISTBOX_REMOVESTRING"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.RemoveListString variable(GetKeyValueEntry(arg2$, "handle")), variable(GetKeyValueEntry(arg2$, "text"))
    End If
    Err.Clear
    On Error GoTo 0
Case "LISTBOX_CLEAR"
    On Error Resume Next
    
    If Not (GUIForms(arg1$) Is Nothing) Then
        GUIForms(arg1$).FormItself.ClearList variable(GetKeyValueEntry(arg2$, "handle"))
    End If
    Err.Clear
    On Error GoTo 0

Case Else
If Left(cmd$, 1) <> "?" Then
showdbug

warn$ = "WARNING!! Command not understood in line " & Str$(pointer) & "!" & Chr$(13) & l$ & Chr$(13) & "Continue Script?"
moomoo = MsgBox(warn$, vbOKCancel, "Buzzscript error")
If moomoo = 2 Then die
If dbg_disable = 1 Then Form1.Hide
End If
    
End Select
If mt_disable <> 1 Then DoEvents

'If coint = 0 Then
'End If

End Function

Public Sub CreateWindow()
End Sub

Public Sub dbug(mode, poop$)
'poop$ = "+" + poop$ + "+" + Chr$(13) + Chr$(10)
'Open "c:\buzzscript_debug.txt" For Append As #2
'   Print #2, poop
'Close #2
If mode = "runtime" Then
If dbg_disable <> 1 Then
If debugtofile$ <> "" Then
Open debugtofile$ For Append As 8
Print #8, poop$
Close #8
Else
Form1.debug.text = Form1.debug.text + poop$ + vbCrLf
Form1.debug.SelStart = Len(Form1.debug.text)
Form1.debug.SelLength = 0
End If
End If
End If
If mode = "editor" Then Form1.StatusBar1.SimpleText = poop$ + " "
If mode = "popup" Then Form1.quickhelp.text = Form1.quickhelp.text + poop$ + vbCrLf


End Sub

Sub die()

odir$ = cd$ + "\inductavars.txt"
dbug "closing", "runtime"
'MsgBox (odir$)
Open odir$ For Output As 5
dbug "file opened", "runtime"
For d = 1 To MX_VAR
If Left$(var$(d, 1), 1) = "!" Then

ttt$ = var$(d, 1) + "," + var$(d, 2)
Print #5, ttt$

End If
Next
Close #5
dbug "file closed", "runtime"
'MsgBox "Done!"

'Unload mainform
dbug "Script finished and closed", "runtime"

'MsgBox ("waiting")
If showdebug <> 1 Then End
End Sub

Public Sub readbatch(batch$)
Form1.labellist.Clear

fno = fno + 1
fnum = fno + 50
dbug "opening file", "runtime"
dbug Str$(fnum), "runtime"

Open batch$ For Input As fnum
dbug "Script opened", "runtime"

Do While EOF(fnum) = False

    mx = mx + 1
    Line Input #fnum, t$
    'Form1.Text1.text = Form1.Text1.text + t$ & vbCrLf
    script$(mx) = t$
    
    l$ = LTrim$(t$)

cmd$ = ""
arg1$ = ""
arg2$ = ""
all_arg$ = ""

first_space = InStr(l$, " ")
second_space = InStr(first_space + 1, l$, " ")
If first_space > 0 Then
cmd$ = UCase$(Left$(l$, first_space - 1))
Else
cmd$ = l$
End If

If first_space > 0 And second_space > 0 Then arg1$ = Mid$(l$, first_space + 1, second_space - first_space - 1)
If first_space > 0 And second_space = 0 Then arg1$ = Right$(l$, Len(l$) - (first_space))
If second_space > 0 Then arg2$ = Right$(l$, Len(l$) - second_space)
all_arg$ = arg1$
If Len(arg2$) > 0 Then all_arg$ = arg1$ + " " + arg2$

If cmd$ = "LABEL" Then
setstring all_arg$, Str$(mx)
Form1.labellist.AddItem (all_arg$)
End If
If cmd$ = "INCLUDE" Then readbatch (all_arg$)
Loop
Close #fnum
dbug "script processed", "runtime"
End Sub

