 Rem File: Dndedit for Dndbbs Version v5.0a Release r2.0
 Rem $Include: 'dndedit.inc'
 Rem $Include: 'datim.bi'

 ' DOS Data Transfer Area
 Type DTAtype
  Drive As String*1
  SearchTemplate As String*11
  SearchAttr As String*1
  EntryCount As String*2
  ClusterNumber As String*2
  Reserved As String*4
  FileAttr As String*1
  FileTime As String*2
  FileDate As String*2
  FileSize As String*4
  ASCIIZfilename As String*13
 End Type

 On Error Goto Error.Trap0
 Call Read.Rows(Var2)
 Max.Row=Var2
 Call Init.Arrays
 Call Init.Mouse
 If Chained2 Then
    Filename=Temp.Command
 Else
    If Chained Then
       Filename="Dnd"+NodeX+".cfg"
    Else
       NodeX=Nul
       Filename=Nul
    Endif
 Endif
 Temp.Command=Filename
 If Chained=False Then
    If Chained2=False Then
       Call MouseDriver(2)
       Call Read.Rows(Var2)
       Max.Row=Var2
       Call Save.Screen(False)
       Call Clear.Screen2
       Call MouseDriver(1)
       Call Read.Config(False)
    Endif
 Endif
 If Chained2=False Then
    Call Passwrd5(Var,"")
    Close #TempFile1
    If Var=False Then
       Call Terminate.Program(True)
       End
    Endif
 Endif
 Do While Lost.Carrier=False
    Call MainPageScreen
    Do While Lost.Carrier=False
       TempX$=Nul
       TempX=False
       Do While TempX$=Nul
          If Lost.Carrier Then
             Exit Do
          Endif
          Call Read.Keyboard(TempX$)
          Call MouseDriver(3)
          If Mouse.Button Then
             Call MainMouseButton
             Exit Do
          Endif
          If Mouse.Button2 Then
             TempX$=Chr$(27)
          Endif
          If Mouse.Button3 Then
             Call Terminate.Program(True)
             End
          Endif
          If Mouse.Row Or Mouse.Column Then
             Call MainMouseSelect
          Endif
       Loop
       If Len(TempX$)=1 Then
          For Var=1 To 12
             If Left$(Title.Data(Var),1)<>"*" Then
                If Ucase$(TempX$)=Left$(Title.Data(Var),1) Then
                   Next.Field=Var
                   Call ShowMainField
                   Call MainEdit
                   Exit Do
                Endif
             Endif
          Next
       Endif
       Select Case Len(TempX$)
       Case 1
          Select Case Ucase$(TempX$)
          Case Chr$(8) ' ^h
             Call Help(14)
             Exit Do
          Case Chr$(9) ' tab
             Call MainSelect
          Case Chr$(14) ' ^n
             Call Read.Config(True)
             If Data.Error Then
                Call Read.Config(False)
             Endif
             Call MainPageStatus
          Case Chr$(27)
             Call Terminate.Program(True)
             End
          Case Chr$(13)
             Call MainEdit
             Exit Do
          End Select
       Case 2
          TempX=Asc(Right$(TempX$,1))
          If TempX=0 Then ' ctrl-break
             Goto Error.Resume0
          Endif
          Call MainSelect
          If TempX$=Chr$(13) Then
             Call MainEdit
             Exit Do
          Endif
       End Select
    Loop
 Loop
Error.Resume0:
 Call Terminate.Program(True)
 End
Error.Trap0:
 Resume Error.Resume0

Sub MainMouseSelect
 On Local Error Goto Error.Trap6
 For Var=1 To 12
    If Mouse.Row=Array.Data(Var,1) Then
       If Mouse.Column>=Array.Data(Var,2) Then
          Var1=Len(Rtrim$(Title.Data(Var)))+2
          If Var>=10 Then
             Var1=Var1+1
          Endif
          If Mouse.Column<=Array.Data(Var,2)+Var1 Then
             If Var<>Selection Then
                Next.Field=Var
                Call ShowMainField
             Endif
             Exit For
          Endif
       Endif
    Endif
 Next
Error.Resume6:
 Exit Sub
Error.Trap6:
 Resume Error.Resume6
End Sub

Sub MainMouseButton
 On Local Error Goto Error.Trap8
 For Var=1 To 12
    If Mouse.Row=Array.Data(Var,1) Then
       If Mouse.Column>=Array.Data(Var,2) Then
          Var1=Array.Data(Var,2)+Len(Rtrim$(Title.Data(Var)))+2
          If Var>=10 Then
             Var1=Var1+1
          Endif
          If Mouse.Column<=Var1 Then
             If Var=Selection Then
                If Var=11 Then
                   TempX$=Chr$(2)+Chr$(133)
                Else
                   If Var=12 Then
                      TempX$=Chr$(2)+Chr$(134)
                   Else
                      TempX$=Chr$(2)+Chr$(Var+58)
                   Endif
                Endif
             Endif
             Exit Sub
          Endif
       Endif
    Endif
 Next
 If Mouse.Row=22 Then
    If Mouse.Column>=34 And Mouse.Column<=41 Then
       TempX$=Chr$(13)
    Endif
 Endif
 If Mouse.Row=22 Then
    If Mouse.Column>=61 And Mouse.Column<=68 Then
       TempX$=Chr$(27)
    Endif
 Endif
 If Mouse.Row=24 Then
    If Mouse.Column>=55 And Mouse.Column<=63 Then
       TempX$=Chr$(8)
    Endif
 Endif
Error.Resume8:
 Exit Sub
Error.Trap8:
 Resume Error.Resume8
End Sub

Sub MainPageScreen
 On Local Error Goto Error.Trap48
 VarX$=Ucase$(Environ$("BLIND"))
 If VarX$="ON" Or VarX$="-1" Or VarX$="TRUE" Then
    Border.Off=True
 Endif
 VarX$=Ucase$(Environ$("BLINDCHAR"))
 If VarX$="ON" Or VarX$="-1" Or VarX$="TRUE" Then
    Border.Char=-1
 Endif
 Selection=1
 Restore MainPageData
 For Temp2=1 To 12
    Read Title.Data(Temp2)
    Read Array.Data(Temp2,1)
    Read Array.Data(Temp2,2)
    Var2$=DND.Path+"DNDEDIT"+Chr$(Temp2+64)+".EXE"
    If Dir$(Var2$)=Nul Then
       Mid$(Title.Data(Temp2),1,1)="*"
    Endif
 Next
 Call MouseDriver(2)
 Color 7,1 : Cls
 Call MouseDriver(1)
 Locate ,,1
 Call ANSI(4,33,44)
 Call ANSI(3,0,0)
 If Local.Mode Then
    Call Shell.ANSI.Program(1)
    If Border.Off=False Then
       If Border.Char=0 Then
          Call ANSI(4,33,44)
          Call ANSI(2,1,2)
          Strng=Chr$(201)
          Call Scrn(Strng)
          Strng=String$(76,205)
          Call Scrn(Strng)
          Strng=Chr$(187)
          Call Scrn(Strng)
       Else
          Call ANSI(4,33,44)
          Call ANSI(2,1,2)
          Strng="+"
          Call Scrn(Strng)
          Strng=String$(76,"-")
          Call Scrn(Strng)
          Strng="+"
          Call Scrn(Strng)
       Endif
    Endif
 Endif
 Call ANSI(4,36,44)
 Call ANSI(2,4,5)
 Strng="Dungeons And Dragons Bulletin Board System Sysop Editor v"+Version$+" r"+Release$
 Call Scrn(Strng)
 Temp1=1
 For Temp=2 To 24
    Call ANSI(4,33,44)
    If Local.Mode Then
       If Border.Off=False Then
          Call ANSI(2,Temp,2)
          If Border.Char=0 Then
             Strng=Chr$(186)
          Else
             Strng="|"
          Endif
          Call Scrn(Strng)
       Endif
    Endif
    If Temp=Array.Data(Temp1,1) Then
       Call ANSI(2,Array.Data(Temp1,1),Array.Data(Temp1,2))
       Strng="F"+Mid$(Str$(Temp1),2)+" "
       Call Scrn(Strng)
       If Temp1=1 Then
          Call ANSI(4,37,45)
          Strng=Left$(Title.Data(Temp1),1)
          Call Scrn(Strng)
          Call ANSI(4,33,45)
          Strng=Mid$(Title.Data(Temp1),2)
          Strng=Rtrim$(Strng)
          Call Scrn(Strng)
       Else
          Call ANSI(4,37,44)
          Strng=Left$(Title.Data(Temp1),1)
          Call Scrn(Strng)
          Call ANSI(4,33,44)
          Strng=Mid$(Title.Data(Temp1),2)
          Strng=Rtrim$(Strng)
          Call Scrn(Strng)
       Endif
       Call ANSI(4,33,44)
       Call ANSI(2,Array.Data(Temp1+6,1),Array.Data(Temp1+6,2))
       Strng="F"+Mid$(Str$(Temp1+6),2)+" "
       Call Scrn(Strng)
       Call ANSI(4,37,44)
       Strng=Left$(Title.Data(Temp1+6),1)
       Call Scrn(Strng)
       Call ANSI(4,33,44)
       Strng=Mid$(Title.Data(Temp1+6),2)
       Strng=Rtrim$(Strng)
       Call Scrn(Strng)
       Temp1=Temp1+1
    Endif
    If Local.Mode Then
       If Border.Off=False Then
          Call ANSI(2,Temp,79)
          If Border.Char=0 Then
             Strng=Chr$(186)
          Else
             Strng="|"
          Endif
          Call Scrn(Strng)
       Endif
    Endif
 Next
 If Local.Mode Then
    If Border.Off=False Then
       If Border.Char=0 Then
          Call ANSI(2,25,2)
          Strng=Chr$(200)
          Call Scrn(Strng)
          Strng=String$(76,205)
          Call Scrn(Strng)
          Strng=Chr$(188)
          Call Scrn(Strng)
       Else
          Call ANSI(2,25,2)
          Strng="+"
          Call Scrn(Strng)
          Strng=String$(76,"-")
          Call Scrn(Strng)
          Strng="+"
          Call Scrn(Strng)
       Endif
    Endif
 Endif
 Call MainPageStatus
Error.Resume48:
 Exit Sub
Error.Trap48:
 Resume Error.Resume48
End Sub

Sub MainPageStatus
 Call ANSI(4,37,44)
 Call ANSI(2,22,4)
 Strng=" Highlight menu choice, press "
 Call Scrn(Strng)
 Call ANSI(4,32,44)
 Strng="<return>"
 Call Scrn(Strng)
 Call ANSI(4,37,44)
 Strng=" to select option, "
 Call Scrn(Strng)
 Call ANSI(4,32,44)
 Strng="<escape>"
 Call Scrn(Strng)
 Call ANSI(4,37,44)
 Strng=" to quit."
 Call Scrn(Strng)
 Call ANSI(2,23,4)
 Strng=" Otherwise, press function key selection. Also, press letter of selection."
 Call Scrn(Strng)
 Call ANSI(4,32,44)
 Call ANSI(2,24,55)
 Strng2="Control-H"
 Call Scrn(Strng2)
 Call ANSI(4,37,44)
 Call ANSI(2,24,64)
 Strng2=" display help."
 Call Scrn(Strng2)
 Call Display.File
 Call ANSI(4,33,44)
 Call ANSI(2,Array.Data(1,1),Array.Data(1,2))
 Strng="F1 "
 Call Scrn(Strng)
 Call ANSI(4,37,45)
 Strng=Left$(Title.Data(1),1)
 Call Scrn(Strng)
 Call ANSI(4,33,45)
 Strng=Mid$(Title.Data(1),2)
 Strng=Rtrim$(Strng)
 Call Scrn(Strng)
End Sub

' display filename info
Sub Display.File
 On Local Error Goto ErrorExit26a
 DIM ASCIIZ As String*260
 DIM BASIC.DTA.SEG As Integer
 DIM BASIC.DTA.OFF As Integer
 DIM DTAfile AS DTAtype
 DIM File.Date AS STRING * 10
 DIM File.Time AS STRING * 8
 DIM File.Size AS DOUBLE
 DIM File.Work.Date AS SINGLE
 DIM File.Work.Time AS SINGLE
 DIM WorkHour AS SINGLE
 DIM WorkMinute AS SINGLE
 DIM WorkSeconds AS SINGLE
 DIM WorkDay AS SINGLE
 DIM WorkMonth AS SINGLE
 DIM WorkYear AS SINGLE

 ' store basic dta
 Call StdinInt(31,0)
 BASIC.DTA.SEG=OutregsX.ES
 BASIC.DTA.OFF=OutregsX.BX

 ' clear filename info
 Call ANSI(4,37,44)
 Call ANSI(2,24,5)
 Strng=Space$(50)
 Call Scrn(Strng)

 ' check filename
 If FileName=Nul Then
    Exit Sub
 Endif

 ' store asciiz filename
 ASCIIZ = FileName + CHR$(0)

 ' restore directory search dta
 InregsX.DS = VARSEG(DTAfile)
 InregsX.DX = VARPTR(DTAfile)
 Call StdinInt(26,0)

 ' find first filename
 InregsX.CX = &H27
 InregsX.DS = VARSEG(ASCIIZ)
 InregsX.DX = VARPTR(ASCIIZ)
 Call StdinInt(78,0)

 ' check flag error
 IF (OutregsX.Flags And &H1) = &H0 Then

    ' construct file date and time for display
    File.Work.Time = ASC(MID$(DTAfile.FileTime, 2, 1))
    File.Work.Time = File.Work.Time * &H100 + ASC(MID$(DTAfile.FileTime, 1, 1))
    File.Work.Date = ASC(MID$(DTAfile.FileDate, 2, 1))
    File.Work.Date = File.Work.Date * &H100 + ASC(MID$(DTAfile.FileDate, 1, 1))
    WorkHour = INT(File.Work.Time / 2048)
    WorkMinute = INT((File.Work.Time AND &H7E0) / 32)
    WorkSeconds = INT((File.Work.Time AND &H1F) / 2)
    WorkYear = INT(File.Work.Date / 512)
    WorkMonth = INT((File.Work.Date AND &H1E0) / 32)
    WorkDay = INT(File.Work.Date AND &H1F)
    WorkYear = WorkYear + 1980
    File.Date = RIGHT$(STR$(WorkMonth + 100), 2) + "-" + RIGHT$(STR$(WorkDay + 100), 2) + "-" + MID$(STR$(WorkYear), 2)
    File.Time = RIGHT$(STR$(WorkHour + 100), 2) + ":" + RIGHT$(STR$(WorkMinute + 100), 2) + ":" + RIGHT$(STR$(WorkSeconds + 100), 2)
    File.Size = File.Size + ASC(MID$(DTAfile.FileSize, 4, 1))
    File.Size = File.Size * &H100 + ASC(MID$(DTAfile.FileSize, 3, 1))
    File.Size = File.Size * &H100 + ASC(MID$(DTAfile.FileSize, 2, 1))
    File.Size = File.Size * &H100 + ASC(MID$(DTAfile.FileSize, 1, 1))

    ' display filename info
    Call ANSI(4,37,44)
    Call ANSI(2,24,5)
    Strng2=DTAfile.ASCIIZFileName
    If Instr(Strng2,Chr$(0)) Then
       Strng2=Left$(Strng2,Instr(Strng2,Chr$(0))-1)
    Endif
    Strng2=Lcase$(Rtrim$(Strng2))
    If Len(Strng2) Then
       Mid$(Strng2,1,1)=Ucase$(Mid$(Strng2,1,1))
    Endif
    Strng="File: "+Strng2+" "+File.Date+" "+File.Time+" "
    Strng2=Format$(File.Size, "#,##0")
    If Len(Strng2)>6 Then
       Strng2="<none>"
    Else
       Strng2=Strng2+" bytes."
    Endif
    Strng=Strng+Strng2
    Call Scrn(Strng)
 Endif
ErrorResume26a:
 ' restore basic dta
 InregsX.DS = BASIC.DTA.SEG
 InregsX.DX = BASIC.DTA.OFF
 Call StdinInt(26,0)
 Exit Sub
ErrorExit26a:
 Resume ErrorResume26a
End Sub

Sub MainSelect
 On Local Error Goto Error.Trap58
 Next.Field=False
 If TempX$=Chr$(9) Then
    TempX=77
 Endif
 Select Case TempX
 Case 59 To 68 ' F1-F10
    Next.Field=TempX-58
    TempX$=Chr$(13)
 Case 94 To 103 ' Ctrl-F1 To Ctrl-F10
    Next.Field=TempX-93
    TempX$=Chr$(13)
 Case 104 To 113 ' Alt-F1 To Alt-F10
    Next.Field=TempX-103
    TempX$=Chr$(13)
 Case 133, 137, 139 ' F11/Ctrl-F11/Alt-F11
    Next.Field=11
    TempX$=Chr$(13)
 Case 134, 138, 140 ' F12/Ctrl-F12/Alt-F12
    Next.Field=12
    TempX$=Chr$(13)
 Case 72, 141, 152 ' up/ctrl-up/alt-up
    If Selection>1 Then
       Next.Field=Selection-1
    Endif
 Case 80, 145, 160 ' down/ctrl-down/alt-down
    If Selection<12 Then
       Next.Field=Selection+1
    Endif
 Case 75, 15, 115, 155 ' left/shift-tab/ctrl-left/alt-left
    If Selection>6 Then
       Next.Field=Selection-6
    Endif
 Case 77, 116, 157 ' right/ctrl-right/alt-right
    If Selection<7 Then
       Next.Field=Selection+6
    Endif
 Case 71, 119 ' home/ctrl-home
    If Selection<>1 Then
      Next.Field=1
    Endif
 Case 79, 117 ' end/ctrl-end
    If Selection<>12 Then
       Next.Field=12
    Endif
 Case 73, 132 ' pageup/ctrl-pageup
    Select Case Selection
    Case 2 To 6
       Next.Field=1
    Case 8 To 12
       Next.Field=7
    End Select
 Case 81, 118 ' pagedown/ctrl-pagedown
    Select Case Selection
    Case 1 To 5
       Next.Field=6
    Case 7 To 11
       Next.Field=12
    End Select
 End Select
 Call ShowMainField
Error.Resume58:
 Exit Sub
Error.Trap58:
 Resume Error.Resume58
End Sub

Sub ShowMainField
 On Local Error Goto Error.Trap59
 If Next.Field Then
    If Next.Field<>Selection Then
       VarQ=44 ' clear field
       Gosub Display.Field
       Selection=Next.Field
       VarQ=45 ' hilight field
       Gosub Display.Field
    Endif
 Endif
 Exit Sub
Display.Field:
 VarX1=Array.Data(Selection,1)
 VarX2=Array.Data(Selection,2)
 If Selection>=10 Then
    Call ANSI(2,VarX1,VarX2+4)
 Else
    Call ANSI(2,VarX1,VarX2+3)
 Endif
 Call ANSI(4,37,VarQ)
 Strng=Left$(Title.Data(Selection),1)
 Call Scrn(Strng)
 Call ANSI(4,33,VarQ)
 Strng=Mid$(Title.Data(Selection),2)
 Strng=Rtrim$(Strng)
 Call Scrn(Strng)
 Return
Error.Resume59:
 Exit Sub
Error.Trap59:
 Resume Error.Resume59
End Sub

Sub MainEdit
 On Local Error Goto Error.Trap151
 Chained2=True
 Next.Field=0
 TempX=1
 Var2$=DND.Path+"DNDEDIT"+Chr$(Selection+64)+".EXE"
 If Dir$(Var2$)<>Nul Then
    Call MouseDriver(2)
    Color 7,1 : Cls
    Call MouseDriver(1)
    Chain Var2$
 Endif
Error.Resume151:
 Exit Sub
Error.Trap151:
 Strng="Error chaining to "+Var2$+". Reinstall Dndbbs."
 Call Scrn(Strng)
 Resume Error.Resume151
End Sub

MainPageData:
 Data "Action Editor Screen",8,5
 Data "Class Editor Screen",10,5
 Data "Email Editor Screen",12,5
 Data "Link Editor Screen",14,5
 Data "Monster Editor Screen",16,5
 Data "Nonplayer Editor Screen",18,5

 Data "Object Editor Screen",8,50
 Data "Player Editor Screen",10,50
 Data "Room Editor Screen",12,50
 Data "Spell Editor Screen",14,50
 Data "Treasure Editor Screen",16,50
 Data "User Editor Screen",18,50
