 Rem File: Cmd052.bas
 Rem Sub: Follow.Loop
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 If Chained<>-2 Then
    Color 15,0
    Print "DNDBBS Command 052 -- Follow.Loop."
    Color 7,0
    End
 Endif
 Call Follow.Player
 If Auto.Break Then
    If Temp0 Then
       Strng="*break*"
       Call IO.O
    Endif
 Endif
 Allow.Break=False
 Auto.Break=False
 Break=False
 Follow.Mode=False
Main.Resume:
 Chain DND.Path+"DNDBBS.EXE"
 End
Main.Error:
 Resume Main.Resume

Sub Follow.Player
 On Local Error Goto ErrTrap18
 Gosub Get.Config
 If Parse.Temp<=False Then
    VarR=True
 Else
    VarR=Parse.Temp
 Endif
 VarS=False
 Var2=False
 VarZ#=0#
 Select Case Last.Command.Number
 Case Follow.Command
    Var1$="Follow"
    Var2$="Following"
 Case Hunt.Command
    Var1$="Hunt"
    Var2$="Hunting"
 Case Trace.Command
    Var1$="Trace"
    Var2$="Tracing"
 Case Track.Command
    Var1$="Track"
    Var2$="Tracking"
 End Select
 If Last.Command.Number<>Follow.Command Then
    Strng=Var1$+" Mode. "
    If Local.Mode Then
       Var$="Press <ctrl-break>/<ctrl-c> to interrupt."
    Else
       Var$="Press <control-c>/<control-k> to interrupt."
    Endif
    Strng=Strng+Var$
    Call IO.O
    Allow.Break=True
    Auto.Break=False
    Follow.Mode=True
    Break=False
    Var1!=Timeit!
    Var2=False
    VarQ=False
    Do While Security.Guard
       Allow.Break=True
       If Lost.Carrier Then
          Var2=True
          Exit Sub
       Endif
       If Node.Abort Then
          Var2=True
          Exit Sub
       Endif
       If Auto.Break Then
          Var2=True
          Exit Sub
       Endif
       VarQ!=Timeit!
       Do While Lost.Carrier=False
          If option1!>0! Then
             If Time.Elapsed(VarQ!,option1!) Then
                Exit Sub
             Endif
          Endif
          Call Keyboard(Var1)
          Call Get.Modem(False)
          Call Check.Break
          Call Check.Carrier
          If Len(Buffer) Then
             If Instr(Buffer,Chr$(27)) Then
                Buffer=Nul
                Var2=True
                Strng="<escape>"
                Call IO.O
                Exit Sub
             Endif
             Buffer=Nul
          Endif
          If Auto.Break Then
             Var2=True
             Exit Sub
          Endif
          If Lost.Carrier Then
             Var2=True
             Exit Sub
          Endif
          If Node.Abort Then
             Var2=True
             Exit Sub
          Endif
       Loop
       If option1#>0# Then
          VarZ#=VarZ#+1#
          If VarZ#>option1# Then
             Strng="Command rounds exceeded!"
             Call IO.O
             Exit Do
          Endif
       Endif
       If Command.Time.Limit>False Then
          If Time.Elapsed(Var1!,Csng(Command.Time.Limit)) Then
             Strng="Command time limit!"
             Call IO.O
             Var2=True
             Exit Sub
          Endif
       Endif
       If New.Room Then
          Call Enter.New.Room
       Endif
       Timeout=Timeit!
       Call IO.Timing
       Call Read.Node.Mess("")
       Last.Command=Var1$+" Mode"
       Call Update.Mess(4)
       For VarX=1 To MaxNodes
          If VarX<>Node Then
             Call Read.Message.Record(MessWorkFile1,VarX+1)
             Out2=Rtrim$(MessWorkRecord1.UserName)
             Out2=Lcase$(Out2)
             Call Valid.Name(Out2)
             If TempA Then
                If MessWorkRecord1.RoomNumber=Room Then
                   TempB!=MessWorkRecord1.UserIndex
                   TempC=VarX
                   Strng=Var2$+" found "+Out2+"!"
                   Call IO.O
                   Exit Do
                Endif
             Endif
          Endif
       Next
       If Last.Command.Number=Hunt.Command Or Last.Command.Number=Trace.Command Then
          ' see if there is any direction
          VarA=False
          For Temp2=1 To 12
             If RoomRecord.Direct(Temp2) Then
                Call Restrict2(Temp2)
                If TempB=False Then
                   VarA=True
                   Exit For
                Endif
             Endif
          Next
          ' see if there is any direction other than previous
          VarB=False
          For Temp2=1 To 12
             If RoomRecord.Direct(Temp2) Then
                Call Restrict2(Temp2)
                If TempB=False Then
                   If Temp2<>VarQ Then
                      VarB=True
                      Exit For
                   Endif
                Endif
             Endif
          Next
          ' find next direction
          If VarA Then
             If VarB Then
                Do
                   TempA=Int(Rnd*12+1)
                   If TempA<>VarQ Then
                      If RoomRecord.Direct(TempA) Then
                         Call Restrict2(TempA)
                         If TempB=False Then
                            Call Find.Back.Link(TempA)
                            VarQ=Temp4
                            Exit Do
                         Endif
                      Endif
                   Endif
                   Call Check.Break
                   If Auto.Break Then
                      Var2=True
                      Exit Do
                   Endif
                   Call Check.Carrier
                   If Lost.Carrier Then
                      Var2=True
                      Exit Do
                   Endif
                Loop
             Else
                Do
                   TempA=Int(Rnd*12+1)
                   If RoomRecord.Direct(TempA) Then
                      Call Restrict2(TempA)
                      If TempB=False Then
                         Call Find.Back.Link(TempA)
                         VarQ=Temp4
                         Exit Do
                      Endif
                   Endif
                   Call Check.Break
                   If Auto.Break Then
                      Var2=True
                      Exit Do
                   Endif
                   Call Check.Carrier
                   If Lost.Carrier Then
                      Var2=True
                      Exit Do
                   Endif
                Loop
             Endif
             If Var2 Then
                Exit Do
             Endif
             Out2=Direction(TempA)
             Out2=Rtrim$(Out2)
             Out2=Lcase$(Out2)
             Strng="You "+Lcase$(Var1$)+" "+Out2+"."
             Call IO.O
             Next.Room=RoomRecord.Direct(TempA)
             Action.Recurse=0
             Call Enter.Room
             If VarR>False Then
                VarS=VarS+1
                If VarS=VarR Then
                   Var2=True
                   Exit Do
                Endif
             Endif
          Endif
       Endif

       ' check break
       Call Keyboard(Var1)
       Call Get.Modem(False)
       Call Check.Break
       Call Check.Carrier
       If Len(Buffer) Then
          If Instr(Buffer,Chr$(27)) Then
             Buffer=Nul
             Var2=True
             Strng="<escape>"
             Call IO.O
             Exit Do
          Endif
          Buffer=Nul
       Endif
       If Auto.Break Then
          Var2=True
          Exit Do
       Endif
       If Lost.Carrier Then
          Var2=True
          Exit Do
       Endif
       If Node.Abort Then
          Var2=True
          Exit Do
       Endif

       ' update system
       If (New.Mail.Count Mod Option0) = 0 Then
          Call Main.Loop.Update
       Endif

       ' reset round counter
       New.Mail.Count = New.Mail.Count + 1
       If New.Mail.Count > 64 Then
          New.Mail.Count = 1
       Endif
    Loop    
 Endif
 Allow.Break=False
 Auto.Break=False
 Break=False
 Follow.Mode=False
 If Var2 Then
    Exit Sub
 Endif
 If Last.Command.Number=Trace.Command Then
    Exit Sub
 Endif
 If Last.Command.Number=Follow.Command Then
    Call Search.Mess(SFalse)
    If TempB!=User.Index Or Temp!<>Room Then
       Strng="That player is not in this room!"
       Call IO.O
       Exit Sub
    Endif
 Endif
 Strng="Follow-mode. "
 If Local.Mode Then
    Var$="Press <ctrl-break>/<ctrl-c> to interrupt."
 Else
    Var$="Press <control-c>/<control-k> to interrupt."
 Endif
 Strng=Strng+Var$
 Call IO.O
 Allow.Break=True
 Auto.Break=False
 Break=False
 Follow.Mode=True
 Following=TempB!
 VarZ=TempC
 Var1!=Timeit!
 Var2=False
 Do While Security.Guard
    Allow.Break=True
    If Lost.Carrier Then
       Exit Do
    Endif
    If Node.Abort Then
       Exit Do
    Endif
    If Auto.Break Then
       Exit Do
    Endif
    VarQ!=Timeit!
    Do While Lost.Carrier=False
       If option1!>0! Then
          If Time.Elapsed(VarQ!,option1!) Then
             Exit Do
          Endif
       Endif
       Call Keyboard(Var1)
       Call Get.Modem(False)
       Call Check.Break
       Call Check.Carrier
       If Len(Buffer) Then
          If Instr(Buffer,Chr$(27)) Then
             Buffer=Nul
             Strng="<escape>"
             Call IO.O
             Exit Do
          Endif
          Buffer=Nul
       Endif
       If Auto.Break Then
          Exit Do
       Endif
       If Lost.Carrier Then
          Exit Do
       Endif
       If Node.Abort Then
          Exit Do
       Endif
    Loop
    If option1#>0# Then
       VarZ#=VarZ#+1#
       If VarZ#>option1# Then
          Strng="Command rounds exceeded!"
          Call IO.O
          Exit Do
       Endif
    Endif
    If Command.Time.Limit>False Then
       If Time.Elapsed(Var1!,Csng(Command.Time.Limit)) Then
          Strng="Command time limit!"
          Call IO.O
          Exit Do
       Endif
    Endif
    If New.Room Then
       Call Enter.New.Room
    Endif
    Call Read.Message.Record(MessWorkFile1,VarZ+1)
    Strng=Rtrim$(MessWorkRecord1.UserName)
    Call Valid.Name(Strng)
    If TempA=False Then
       Exit Do
    Endif
    Temp!=MessWorkRecord1.RoomNumber
    If Temp!<>Room Then
       Next.Room=Temp!
       Action.Recurse=0
       Call Enter.Room
       If Room.Toggle Then
          Call Display.Sorted.Room
       Else
          Call Show.Room
          Call Display.Users(True)
          Call Display.Users(False)
       Endif
    Endif
    Timeout=Timeit!
    Call IO.Timing
    Call Read.Node.Mess("")
    Last.Command="Follow-Mode"
    Call Update.Mess(4)

    ' check break
    Call Keyboard(Var1)
    Call Get.Modem(False)
    Call Check.Break
    Call Check.Carrier
    If Len(Buffer) Then
       If Instr(Buffer,Chr$(27)) Then
          Buffer=Nul
          Strng="<escape>"
          Call IO.O
          Exit Do
       Endif
       Buffer=Nul
    Endif
    If Auto.Break Then
       Exit Do
    Endif
    If Lost.Carrier Then
       Exit Do
    Endif
    If Node.Abort Then
       Exit Do
    Endif

    ' update system
    If (New.Mail.Count Mod Option0) = 0 Then
       Call Main.Loop.Update
    Endif

    ' reset round counter
    New.Mail.Count = New.Mail.Count + 1
    If New.Mail.Count > 64 Then
       New.Mail.Count = 1
    Endif
 Loop
 Allow.Break=False
 Auto.Break=False
 Break=False
 Follow.Mode=False
 Exit Sub
Get.Config:
 VarX=0
 Temp0=-1
 Option0=4
 Option1#=-1#
 Option1!=-1!
 Do
    VarX=VarX+1
    If VarX>Options.Lines Then
       Goto Next.Read
       Exit Do
    Endif
    Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
    If Ucase$(Var$)="[CMD052]" Then
       Do
          VarX=VarX+1
          If VarX>Options.Lines Then
             Goto Next.Read
             Return
          Endif
          Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
          If Ucase$(Var$)="[END]" Then
             Goto Next.Read
             Return
          Endif
          Var=Instr(Var$,"=")
          If Var Then
             Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
             Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
             Gosub Parse.Options
          Endif
       Loop
    Endif
 Loop
 Return
Next.Read:
 Close #TempFile1
 Filename$=DND.Path+"CMD052.CFG"
 If Dir$(Filename$)="" Then
    Return
 Endif
 Open Filename$ For Input As #TempFile1
 Do
    If Eof(TempFile1) Then
       Exit Do
    Endif
    Line Input #TempFile1, Var$
    Var$=Ltrim$(Rtrim$(Var$))
    If Left$(Var$,1)<>";" Then
       VarX$=UserRecord.CodeName
       VarX$=Rtrim$(VarX$)
       VarX$=Ucase$(VarX$)
       If Ucase$(Var$)="["+VarX$+"]" Then
          VarZ=0
          Temp0=-1
          Option0=4
          Option1#=-1#
          Option1!=-1!
          Do
             If Eof(TempFile1) Then
                Exit Do
             Endif
             Line Input #TempFile1, Var$
             If Ucase$(Var$)="[END]" Then
                Return
             Endif
             Var=Instr(Var$,"=")
             If Var Then
                Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
                Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
                Gosub Parse.Options
             Endif
          Loop
       Endif
    Endif
 Loop
 Return
Parse.Options:
 VarQ=Int(Val(Var3$)+.5)
 Select Case Lcase$(Var2$)
 Case "break.switch"
    Select Case Ucase$(Var3$)
    Case "0", "FALSE","OFF"
       Temp0=0
    End Select
 Case "maximum.rounds.counter"
    option1#=Cdbl(Val(Var3$))
 Case "round.time.delay"
    option1!=Csng(Val(Var3$))
 End Select
 Select Case VarQ
 Case 1,2,4,8,16,32,64
    Select Case Lcase$(Var2$)
    Case "main.loop.update"
       option0=VarQ
    End Select
 End Select
 Return
ErrResume18:
 Exit Sub
ErrTrap18:
 Resume ErrResume18
End Sub
