 Rem File: DndeditD for Dndbbs Version v5.0a Release r2.0 Tweak t5.0a
 Rem Sub: EditLink
 Rem $Include: 'dndedit.inc'

 On Error Goto Main.Error
 Call Stdin(Var)
 If Chained2=0 Then
    Call Init.Arrays
    Call Read.Rows(Var2)
    Max.Row=Var2
    Call Save.Screen(False)
    Call Clear.Screen2
    Call Init.Mouse
    Call Read.Config(False)
    Call Passwrd5(Var,"d")
    Close #TempFile1
    If Var=False Then
       Call Terminate.Program(True)
       End
    Endif
 Endif
Main.Resume:
 Call EditLink
 If Chained2 Then
    Chain DND.Path+"DNDEDIT.EXE"
 Endif
 Call Terminate.Program(True)
 End
Main.Error:
 Resume Main.Resume

Sub EditLink
 On Local Error Goto Error.Trap136
 Restore LinkData
 For Temp2=1 To MaxDirections
    Read Title.Data(Temp2)
 Next
 Read Title.Data(13)
 Read Title.Data(14)
 For Temp2=1 To MaxDirections
    Read Array.Data(Temp2,1),Array.Data(Temp2,2)
 Next
 Read Array.Data(13,1),Array.Data(13,2)
 Read Array.Data(14,1),Array.Data(14,2)
 For Temp2=1 To MaxDirections
    Read Link.Data(Temp2,1),Link.Data(Temp2,2)
 Next
 Read Link.Data(13,1),Link.Data(13,2)
 Read Link.Data(14,1),Link.Data(14,2)
 Call DisplayRoomLink
 Call RoomLinkStatusLine
 Call DisplayRoomLinkScreen
 Do While Lost.Carrier=False
    TempX$=Nul
    Do While TempX$=Nul
       If Lost.Carrier Then
          Exit Do
       Endif
       Call Read.Keyboard(TempX$)
       Call MouseDriver(3)
       If Mouse.Button2 Then
          TempX$=Chr$(27)
       Endif
       If Mouse.Button Then
          Call MouseButton(14)
          If Len(TempX$) Then
             Exit Do
          Endif
       Endif
       If Mouse.Row Or Mouse.Column Then
          Call MouseSelect(14)
       Endif
    Loop
    TempX=False
    Select Case Len(TempX$)
    Case 2
       TempX=Asc(Mid$(TempX$,2,1))
       If TempX=0 Then
          Call Write.Record(RoomFile,Room.Link)
          Exit Do
       Endif
       Call RoomLinkSelect
    Case 1
       TempX=Asc(TempX$)
       Select Case TempX
       Case 4
          Call DeleteLinks
       Case 8
          Call Help(9)
          Call DisplayRoomLink
          Call RoomLinkStatusLine
          Call DisplayRoomLinkScreen
       Case 9
          Call RoomLinkSelect
       Case 10
          Call JumpLink
       Case 13
          Call EditSelectedRoomLink
       Case 14
          Call NextRoomLink
       Case 15
          Call RoomLinkSelect
       Case 17
          Call PreviousRoomLink
       Case 18
          Call SelectNextRoomLink
          Call RoomLinkStatusLine
       Case 23
          Call SearchLinks
       Case 27
          Call Write.Record(RoomFile,Room.Link)
          Exit Sub
       End Select
    End Select
 Loop
Error.Resume136:
 Exit Sub
Error.Trap136:
 Resume Error.Resume136
End Sub

Sub SearchLinks
 On Local Error Goto Error.Trap137
 Call Write.Record(RoomFile,Room.Link)
 Strng=Nul
 Out2="^ Cursor Edit "+Chr$(60)+Chr$(217)+" Exit Field  "
 Call StatusLine
 Call ANSI(4,33,44)
 Call ANSI(2,23,2)
 Strng="Enter Room Link Number: "
 Call Scrn(Strng)
 Call ANSI(4,37,40)
 Call ANSI(2,23,27)
 Strng=Space$(30)
 Call Scrn(Strng)
 Restore Data20
 Read Xcoor, Ycoor, Column, Line.Length, Row, Max.Lines, Line.Number
 Edit.Data(1)=Nul
 Call ScreenEdit
 Call RoomLinkStatusLine
 Var1!=Int(Val(Edit.Data(1))+.5)
 Var$=Ltrim$(Lcase$(Edit.Data(1)))
 If Left$(Var$,1)>="0" And Left$(Var$,1)<="9" Then
    Var2$=Var$
    Var$=Nul
    For Var1=1 To Len(Var2$)
       If Mid$(Var2$,Var1,1)>="0" And Mid$(Var2$,Var1,1)<="9" Then
          Eat$=Nul
       Else
          Var$=Var2$
          Var3=0
          If Instr(Var$,"#") Then
             Var3=Int(Val(Mid$(Var$,Instr(Var$,"#")+1))+.5)
             Var$=Left$(Var$,Instr(Var$,"#")-1)
          Endif
          Exit For
       Endif
    Next
 Else
    Var3=0
    If Instr(Var$,"#") Then
       Var3=Int(Val(Mid$(Var$,Instr(Var$,"#")+1))+.5)
       Var$=Left$(Var$,Instr(Var$,"#")-1)
    Endif
 Endif
 Temp!=SFalse
 Temp3!=SFalse
 Temp4=0
 For Var2!=1! To Lof(RoomFile)/RoomRecLen
    Call Read.Record(RoomFile,Var2!)
    If Len(Var$) Then
       ' see if var$ matches in custom direction 06/10/2022
       Var1$=Lcase$(Rtrim$(RoomRecord.Custom1))
       Call InstrSUB(VarQ1,Var1$,Lcase$(Var$))
       Var1$=Lcase$(Rtrim$(RoomRecord.Custom2))
       Call InstrSUB(VarQ2,Var1$,Lcase$(Var$))

       If VarQ1 Or VarQ2 Then
          Temp4=Temp4+1
          If Var3=0 Or Temp4=Var3 Then
             Temp3!=1!
             Temp!=Var2!
             Exit For
          Endif
       Endif
    Else
       For Temp6=1 To MaxDirections+2
          If RoomRecord.Direct(Temp6)=Var1! Then
             Temp3!=Temp3!+1!
             If Temp3!=1! Then
                Temp!=Var2!
             Endif
             Exit For
          Endif
       Next
    Endif
 Next
 If Temp3!=1! Then
    Room.Link=Temp!
    Call RoomLinkStatusLine
    Call DisplayRoomLinkScreen
    Exit Sub
 Endif
 If Temp3!=SFalse Then
    Strng="The room link was not found in the room file."
    Call ErrorMessage
    Call RoomLinkStatusLine
    Call DisplayRoomLinkScreen
    Exit Sub
 Endif
 Strng="There was more than one room link found."
 Call ErrorMessage
 Strng="^ Scroll Room  "+Chr$(60)+Chr$(217)+" Select Room ("+Ltrim$(Str$(Temp3!))+" found)."
 Out2=Nul
 Call StatusLine
 Room.Link=Temp!
 Var2!=Temp!
 Call DisplayRoomLinkScreen
 Do While Lost.Carrier=False
    TempX$=Nul
    Do While TempX$=Nul
       If Lost.Carrier Then
          Exit Do
       Endif
       Call Read.Keyboard(TempX$)
       Call MouseDriver(3)
       If Mouse.Button2 Then
          TempX$=Chr$(27)
       Endif
       If Mouse.Button Then
          Call MouseCheck
          Exit Do
       Endif
    Loop
    TempX=False
    Select Case Len(TempX$)
    Case 1
       If TempX$=Chr$(13) Then
          Room.Link=Var2!
          Call RoomLinkStatusLine
          Call DisplayRoomLinkScreen
          Exit Sub
       Endif
    Case 2
       TempX=Asc(Mid$(TempX$,2,1))
       Select Case TempX
       Case 72, 141, 152
          VarA=False
          For Var3!=Var2!-1! To 1! Step -1!
             Call Read.Record(RoomFile,Var3!)
             For Temp6=1 To MaxDirections+2
                If RoomRecord.Direct(Temp6)=Var1! Then
                   VarA=True
                   Var2!=Var3!
                   Room.Link=Var3!
                   Call DisplayRoomLinkScreen
                   Exit For
                Endif
             Next
             If VarA Then
                Exit For
             Endif
          Next
          If VarA=False Then
             Strng="This is the first room link in the search."
             Call ErrorMessage
          Endif
       Case 80, 145, 160
          VarA=False
          For Var3!=Var2!+1! To Lof(RoomFile)/RoomRecLen
             Call Read.Record(RoomFile,Var3!)
             For Temp6=1 To MaxDirections+2
                If RoomRecord.Direct(Temp6)=Var1! Then
                   VarA=True
                   Var2!=Var3!
                   Room.Link=Var3!
                   Call DisplayRoomLinkScreen
                   Exit For
                Endif
             Next
             If VarA Then
                Exit For
             Endif
          Next
          If VarA=False Then
             Strng="This is the last room link in the search."
             Call ErrorMessage
          Endif
       End Select
    End Select
 Loop
Error.Resume137:
 Exit Sub
Error.Trap137:
 Resume Error.Resume137
End Sub

Sub DeleteLinks
 On Local Error Goto Error.Trap138
 For Temp6=1 To MaxDirections
    RoomRecord.Direct(Temp6)=SFalse
 Next
 RoomRecord.Direct(13)=SFalse
 RoomRecord.Direct(14)=SFalse
 RoomRecord.Custom1=Nul
 RoomRecord.Custom2=Nul
 Call Write.Record(RoomFile,Room.Link)
 Call DisplayRoomLinkScreen
Error.Resume138:
 Exit Sub
Error.Trap138:
 Resume Error.Resume138
End Sub

Sub JumpLink
 On Local Error Goto Error.Trap139
 If RoomRecord.Direct(Selection)>False Then
    If RoomRecord.Direct(Selection)<=Lof(RoomFile)/RoomRecLen Then
       Call Write.Record(RoomFile,Room.Link)
       Room.Link=RoomRecord.Direct(Selection)
       Call DisplayRoomLinkScreen
       Exit Sub
    Endif
 Endif
 Strng="The room number is not within the room file range."
 Call ErrorMessage
Error.Resume139:
 Exit Sub
Error.Trap139:
 Resume Error.Resume139
End Sub

REM    nw north ne
REM   west     east
REM    sw south se
REM
REM       8 1 5
REM       4   2
REM       7 3 6

LinkData:
 Data "North","East","South","West"
 Data "Northeast","Southeast","Southwest","Northwest"
 Data "Up","Down","In","Out"
 Data "<custom1>","<custom2>"

 Data 7,38,11,55,15,38,11,22
 Data 7,46,15,46,15,26,7,26
 Data 7,62,9,62,11,62,13,62
 Data 17,19,17,41

 Data 8,37,11,47,14,37,11,27
 Data 8,47,14,47,14,27,8,27
 Data 7,65,9,67,11,65,13,66
 Data 17,32,17,54

Sub NextRoomLink
 On Local Error Goto Error.Trap130
 If Room.Link>=Lof(RoomFile)/RoomRecLen Then
    Strng="That is the last room number in the room file."
    Call ErrorMessage
 Else
    Call Write.Record(RoomFile,Room.Link)
    Room.Link=Room.Link+1!
    Call DisplayRoomLinkScreen
 Endif
Error.Resume130:
 Exit Sub
Error.Trap130:
 Resume Error.Resume130
End Sub

Sub PreviousRoomLink
 On Local Error Goto Error.Trap131
 If Room.Link>1! Then
    Call Write.Record(RoomFile,Room.Link)
    Room.Link=Room.Link-1!
    Call DisplayRoomLinkScreen
    Exit Sub
 Endif
 Strng="That is the first room number in the room file."
 Call ErrorMessage
Error.Resume131:
 Exit Sub
Error.Trap131:
 Resume Error.Resume131
End Sub

Sub ShowRoomLink
 On Local Error Goto Error.Trap133
 If TempX=False Then
    Call ANSI(4,33,44)
    Call ANSI(2,2,2)
    Strng="Room"
    Call Scrn(Strng)
    Call ANSI(2,2,16)
    Strng="of"
    Call Scrn(Strng)

    Call ANSI(4,37,40)
    Call ANSI(2,2,7)
    Strng=Space$(8)
    Call Scrn(Strng)
    Call ANSI(2,2,19)
    Strng=Space$(8)
    Call Scrn(Strng)
    Call ANSI(2,2,7)
    Strng=Mid$(Str$(Room.Link),2)
    Call Scrn(Strng)
    Var!=Lof(RoomFile)/RoomRecLen
    Call ANSI(2,2,19)
    Strng=Mid$(Str$(Var!),2)
    Call Scrn(Strng)

    ' center
    Temp2=40-Int(Len(Mid$(Str$(Room.Link),2))/2-.5)
    Call ANSI(2,11,Temp2)
    Strng=Mid$(Str$(Room.Link),2)
    Call Scrn(Strng)
 Endif
 If TempX>False Then
    Call ANSI(4,37,40)
    Call ANSI(2,Link.Data(TempX,1),Link.Data(TempX,2))
    Strng=Space$(8)
    Call Scrn(Strng)
    Call ANSI(2,Link.Data(TempX,1),Link.Data(TempX,2))
    Strng=Mid$(Str$(RoomRecord.Direct(TempX)),2)
    Call Scrn(Strng)
    If TempX=13 Then
       Call ANSI(2,Array.Data(13,1),Array.Data(13,2))
       Strng=Lcase$(Rtrim$(RoomRecord.Custom1))
       If Strng=Nul Then
          Strng="<custom1>   "
       Else
          Strng=Strng+Space$(12-Len(Strng))
       Endif
       Title.Data(13)=Strng
       Call Scrn(Strng)
    Endif
    If TempX=14 Then
       Call ANSI(2,Array.Data(14,1),Array.Data(14,2))
       Strng=Lcase$(Rtrim$(RoomRecord.Custom2))
       If Strng=Nul Then
          Strng="<custom2>   "
       Else
          Strng=Strng+Space$(12-Len(Strng))
       Endif
       Title.Data(14)=Strng
       Call Scrn(Strng)
    Endif
 Endif
Error.Resume133:
 Exit Sub
Error.Trap133:
 Resume Error.Resume133
End Sub

Sub DisplayRoomLink
 On Local Error Goto Error.Trap134
 Out3="Sysop Room Link Edit"
 Call DisplayBorder(Out3)
 Call ANSI(4,33,44)
 Call ANSI(2,2,2)
 Strng="Room Number"
 Call Scrn(Strng)
 Room.Link=1
 Call ANSI(4,37,44)
 Call ANSI(2,9,35)
 Strng="\    |    /"
 Call Scrn(Strng)
 Call ANSI(2,10,35)
 Strng="  \  |  /  "
 Call Scrn(Strng)
 Call ANSI(2,11,35)
 Strng="-"
 Call Scrn(Strng)
 Call ANSI(2,11,45)
 Strng="-"
 Call Scrn(Strng)
 Call ANSI(2,12,35)
 Strng="  /  |  \ "
 Call Scrn(Strng)
 Call ANSI(2,13,35)
 Strng="/    |    \"
 Call Scrn(Strng)
 Call ANSI(4,37,40)
 Call ANSI(2,Array.Data(1,1),Array.Data(1,2))
 Strng=Title.Data(1)
 Call Scrn(Strng)
 Call ANSI(4,33,44)
 For Temp2=2 To MaxDirections
    Call ANSI(2,Array.Data(Temp2,1),Array.Data(Temp2,2))
    Strng=Title.Data(Temp2)
    Call Scrn(Strng)
 Next
 Selection=1
Error.Resume134:
 Exit Sub
Error.Trap134:
 Resume Error.Resume134
End Sub

Sub DisplayRoomLinkScreen
 On Local Error Goto Error.Trap135
 If Room.Link>SFalse And Room.Link<=Lof(RoomFile)/RoomRecLen Then
    Call Read.Record(RoomFile,Room.Link)
    For TempX=False To MaxDirections
       Call ShowRoomLink
    Next
    TempX=13
    Call ShowRoomLink
    TempX=14
    Call ShowRoomLink
    Call RedisplayField
 Endif
Error.Resume135:
 Exit Sub
Error.Trap135:
 Resume Error.Resume135
End Sub

Sub EditSelectedRoomLink
 On Local Error Goto Error.Trap71
 Strng=Nul
 Out2="^ Cursor Edit "+Chr$(60)+Chr$(217)+" Exit Field "
 Call StatusLine
 Xcoor=Link.Data(Selection,1)
 Ycoor=Link.Data(Selection,2)
 Row=Xcoor
 Column=Ycoor
 Line.Length=8
 Max.Lines=1
 Line.Number=1
 Edit.Data(1)=Mid$(Str$(RoomRecord.Direct(Selection)),2)
 Call ScreenEdit
 RoomRecord.Direct(Selection)=Val(Edit.Data(1))
 If Selection=13 Then
    Xcoor=Array.Data(13,1)
    Ycoor=Array.Data(13,2)
    Row=Xcoor
    Column=Ycoor
    Line.Length=12
    Max.Lines=1
    Line.Number=1
    Edit.Data(1)=Lcase$(Rtrim$(RoomRecord.Custom1))
    Call ANSI(4,37,40)
    Call ANSI(2,Xcoor,Ycoor)
    Strng=Space$(12)
    Call Scrn(Strng)
    Call ScreenEdit
    RoomRecord.Custom1=Ucase$(Edit.Data(1))
 Endif
 If Selection=14 Then
    Xcoor=Array.Data(14,1)
    Ycoor=Array.Data(14,2)
    Row=Xcoor
    Column=Ycoor
    Line.Length=12
    Max.Lines=1
    Line.Number=1
    Edit.Data(1)=Lcase$(Rtrim$(RoomRecord.Custom2))
    Call ANSI(4,37,40)
    Call ANSI(2,Xcoor,Ycoor)
    Strng=Space$(12)
    Call Scrn(Strng)
    Call ScreenEdit
    RoomRecord.Custom2=Ucase$(Edit.Data(1))
 Endif
 Call Write.Record(RoomFile,Room.Link)
 TempX=Selection
 Call ShowRoomLink
 Call RoomLinkStatusLine
 Call RedisplayField
Error.Resume71:
 Exit Sub
Error.Trap71:
 Resume Error.Resume71
End Sub

Sub SelectNextRoomLink
 On Local Error Goto Error.Trap88
 Strng=Nul
 Out2="^ Cursor Edit "+Chr$(60)+Chr$(217)+" Exit Field"
 Call StatusLine
 Call ANSI(4,33,44)
 Call ANSI(2,23,2)
 Strng="Enter Room: "
 Call Scrn(Strng)
 Call ANSI(4,37,40)
 Call ANSI(2,23,14)
 Strng=Space$(6)
 Call Scrn(Strng)
 Call ANSI(4,37,44)
 Call ANSI(2,23,23)
 Strng="(1-"+Mid$(Str$(Lof(RoomFile)/RoomRecLen),2)+")"
 Multiplier=Len(Strng)+23
 Strng=Strng+" (+/-/x)"
 Call Scrn(Strng)
 Restore RoomLinkData2
 Read Xcoor, Ycoor, Column, Line.Length, Max.Lines, Row, Line.Number
 Edit.Data(1)=Nul
 Call ScreenEdit
 Multiplier=False
 Temp2!=Int(Val(Edit.Data(1))+.5)
 If Temp2!<=SFalse Or Temp2!>Lof(RoomFile)/RoomRecLen Then
    Strng="The room number is not within the room file range."
    Call ErrorMessage
    Exit Sub
 Endif
 Call Write.Record(RoomFile,Room.Link)
 Room.Link=Temp2!
 Call DisplayRoomLinkScreen
Error.Resume88:
 Exit Sub
Error.Trap88:
 Resume Error.Resume88
End Sub

Sub RoomLinkStatusLine
 On Local Error Goto Error.Trap89
 Strng="^ Cursor Select "+Chr$(60)+Chr$(217)
 Strng=Strng+" Enter Choice <esc> Exit and Save ^h Help"
 Out2="^n Next ^q Prev ^r Select Room "
 Out2=Out2+"^j Jump Room Link ^d Delete Links ^w Search"
 Call StatusLine
Error.Resume89:
 Exit Sub
Error.Trap89:
 Resume Error.Resume89
End Sub

Sub RoomLinkSelect
 On Local Error Goto Error.Trap122
 Next.Field=False
 Select Case TempX
 Case 77, 157, 116
    Select Case Selection
    Case 1
       Next.Field=5
    Case 3
       Next.Field=6
    Case 4
       Next.Field=2
    Case 7
       Next.Field=3
    Case 8
       Next.Field=1
    Case 13
       Next.Field=14
    End Select
 Case 75, 155, 115
    Select Case Selection
    Case 1
       Next.Field=8
    Case 2
       Next.Field=4
    Case 3
       Next.Field=7
    Case 5
       Next.Field=1
    Case 6
       Next.Field=3
    Case 14
       Next.Field=13
    End Select
 Case 80, 145, 160
    Select Case Selection
    Case 1
       Next.Field=3
    Case 2
       Next.Field=6
    Case 4
       Next.Field=7
    Case 5
       Next.Field=2
    Case 8
       Next.Field=4
    Case 9
       Next.Field=10
    Case 10
       Next.Field=11
    Case 11
       Next.Field=12
    End Select
 Case 72, 141, 152
    Select Case Selection
    Case 2
       Next.Field=5
    Case 3
       Next.Field=1
    Case 4
       Next.Field=8
    Case 6
       Next.Field=2
    Case 7
       Next.Field=4
    Case 10
       Next.Field=9
    Case 11
       Next.Field=10
    Case 12
       Next.Field=11
    End Select
 Case 9 ' tab
    Select Case Selection
    Case 1 To 8
       Next.Field=9
    End Select
 Case 15 ' shift-tab
    Select Case Selection
    Case 9 To 12
       Next.Field=5
    End Select
 Case 71, 73, 119, 132 ' home/pageup
    Select Case Selection
    Case 4, 7
       Next.Field=8
    Case 3
       Next.Field=1
    Case 2, 6
       Next.Field=5
    Case 10, 11, 12
       Next.Field=9
    Case 13, 14
       Next.Field=7
    End Select
 Case 79, 81, 117, 118 ' end/pagedown
    Select Case Selection
    Case 8, 4
       Next.Field=7
    Case 1
       Next.Field=3
    Case 5, 2
       Next.Field=6
    Case 9, 10, 11
       Next.Field=12
    Case 7, 3, 6
       Next.Field=13
    End Select
 End Select
 Call ShowField2
Error.Resume122:
 Exit Sub
Error.Trap122:
 Resume Error.Resume122
End Sub

Sub ShowField2
 On Local Error Goto Error.Trap55a
 If Next.Field Then
    If Selection=13 Or Selection=14 Then
       Call ANSI(4,37,40)
    Else
       Call ANSI(4,33,44)
    Endif
    Call DisplayField2
    Selection=Next.Field
    Call ANSI(4,37,45)
    Call DisplayField2
 Endif
Error.Resume55a:
 Exit Sub
Error.Trap55a:
 Resume Error.Resume55a
End Sub

Sub DisplayField2
 On Local Error Goto Error.Trap56a
 Call ANSI(2,Array.Data(Selection,1),Array.Data(Selection,2))
 Strng=Title.Data(Selection)
 Call Scrn(Strng)
Error.Resume56a:
 Exit Sub
Error.Trap56a:
 Resume Error.Resume56a
End Sub

RoomLinkData2:
 Data 23,14,14,6,1,23,1
Data20:
 Data 23,27,27,20,23,1,1
