 Rem File: Cmd005.bas
 Rem Sub: Auto.Loop
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 If Chained<>-2 Then
    Color 15,0
    Print "DNDBBS Command 005 -- Auto.Loop."
    Color 7,0
    End
 Endif
 Call Auto.Loop
Main.Resume:
 Chain DND.Path+"DNDBBS.EXE"
 End
Main.Error:
 Resume Main.Resume

Sub Auto.Loop
 On Local Error Goto Err.Trap26
 Gosub Get.Config
 If Local.Mode Then
    Var$="Press <ctrl-break>/<ctrl-c> to interrupt."
 Else
    Var$="Press <control-c>/<control-k> to interrupt."
 Endif
 Strng="Auto mode. "+Var$
 Call IO.O
 Call Safe.Room
 If TempA Then
    Strng="Room: <safe haven>"
    Call IO.O
 Endif
 Auto.Mode=True
 Allow.Break=True
 Auto.Break=False
 Break=False
 Var1!=Timeit!
 Var3=False
 VarZ#=0#
 If option1!<=0! Then
    option1!=1!
 Endif
 Do While Security.Guard
    Allow.Break=True
    VarQ!=Timeit!
    Do While Lost.Carrier=False
       If Time.Elapsed(VarQ!,option1!) Then
          Exit Do
       Endif
       Call Keyboard(Var1)
       Call Get.Modem(False)
       If Len(Buffer) Then
          If Instr(Buffer,Chr$(27)) Then
             Buffer=Nul
             Strng="<escape>"
             Call IO.O
             Exit Do
          Endif
          Buffer=Nul
       Endif
       Call Check.Break
       If Auto.Break Then
          Exit Do
       Endif
       Call Check.Carrier
       Call Read.Mess
       Call IO.Timing
       Call Read.Node.Mess
       If Local.Mode Then
          Call Release.Time(1)
       Endif
    Loop
    If New.Room Then
       Call Enter.New.Room
    Endif
    If Command.Time.Limit>False Then
       If Time.Elapsed(Var1!,Csng(Command.Time.Limit)) Then
          Var3=True
          Exit Do
       Endif
    Endif
    If option1#>0# Then
       VarZ#=VarZ#+1#
       If VarZ#>option1# Then
          Var3=-2
          Exit Do
       Endif
    Endif
    If Auto.Break Then
       If Option3 Then
          Strng="*break*"
          Call IO.O
       Endif
       Exit Do
    Endif
    If Lost.Carrier Then
       Exit Do
    Endif
    If Node.Abort Then
       Exit Do
    Endif
    Call Keyboard(Var1)
    Call Get.Modem(False)
    If Len(Buffer) Then
       If Instr(Buffer,Chr$(27)) Then
          Buffer=Nul
          Strng="<escape>"
          Call IO.O
          Exit Do
       Endif
       Buffer=Nul
    Endif
    Timeout=Timeit!
    Call IO.Timing
    Last.Command="Auto-Mode"
    Call Update.Mess(4)
    Call Get.Last.Monster(Var)
    Last.Module.Number=5
    If Var Then
       Parsed.Command1=Last.Monster
       Last.Command.Number=Killed
       Magic.Spell=False
       Call Attack.Monster(VarZ)
       Graphics.Off=False
    Endif
    Temp5!=RoomRecord.First
    If Temp5! Then
       Do While Lost.Carrier=False
          Call Read.Record(RoomInvFile,Temp5!)
          If MessWorkRecord6.Room=SFalse Then
             Exit Do
          Endif
          Temp6!=MessWorkRecord6.Next
          Temp7!=MessWorkRecord6.Index
          If Temp7!>SFalse And Temp7!<=Lof(TreasureFile)/TreasureRecLen Then
             Call Read.Record(TreasureFile,Temp7!)
             Temp6=MessWorkRecord6.Charges
             Temp8a=TreasureRecord.Plus
             Gosub Pickup.Coins
             If TempA Then
                Exit Do
             Endif
             Gosub Valid.Treasure
             If TempB Then
                For VarX=1 To 7
                   Select Case VarX
                   Case 1, 2, 3, 5, 6, 10
                      Gosub Pickup.Item1
                      If TempA Then
                         Gosub Take.Item
                         Exit Do
                      Endif
                   Case 4, 9
                      Gosub Pickup.Ring
                      If TempA Then
                         Gosub Take.Item
                         Exit Do
                      Endif
                   Case 7
                      Gosub Pickup.Weapon
                      If TempA Then
                         Gosub Take.Item
                         Exit Do
                      Endif
                   End Select
                Next
             Endif
          Endif
          If Temp6!=SFalse Then
             Exit Do
          Endif
          Temp5!=Temp6!
       Loop
    Endif
    Graphics.Off=False
    If UserRecord.Fatigue<UserRecord.FatigueMax Then
       Strng="You cast a vigor spell!"
       Call IO.O
       UserRecord.Fatigue=Cint(UserRecord.Fatigue+Config2(21)*UserRecord.Level)
       Call New.Stats
       Strng=Str$(UserRecord.Fatigue)
       Strng="You now have"+Strng+" fatigue points!"
       Call IO.O
    Endif
    If UserRecord.Vitality<UserRecord.VitalityMax Then
       Strng="You cast a heal spell!"
       Call IO.O
       UserRecord.Vitality=Cint(UserRecord.Vitality+Config2(22)*UserRecord.Level)
       Call New.Stats
       Strng=Str$(UserRecord.Vitality)
       Strng="You now have"+Strng+" vitality points!"
       Call IO.O
    Endif
    If UserRecord.Magic<UserRecord.MagicMax Then
       Strng="You cast an enchant spell!"
       Call IO.O
       UserRecord.Magic=Cint(UserRecord.Magic+Config2(23)*UserRecord.Level)
       Call New.Stats
       Strng=Str$(UserRecord.Magic)
       Strng="You now have"+Strng+" magic points!"
       Call IO.O
    Endif
    If UserRecord.Psionic<UserRecord.PsionicMax Then
       Strng="You cast an psyche spell!"
       Call IO.O
       UserRecord.Psionic=Cint(UserRecord.Psionic+Config2(82)*UserRecord.Level)
       Call New.Stats
       Strng=Str$(UserRecord.Psionic)
       Strng="You now have"+Strng+" psionic points!"
       Call IO.O
    Endif
    If UserRecord.Level<=False Then
       Call Train.Stats
    Endif
    VarX=False
    If Config2(93)=False Then
       Call Experience(Temp#)
       If UserRecord.Experience>Temp# Then
          VarX=True
       Endif
    Else
       If UserRecord.Experience<False Then
          UserRecord.Experience=False
          VarX=True
       Endif
    Endif
    If VarX Then
       Call Gold(Temp#)
       If UserRecord.Gold>=Temp# Then
          Call Train.Stats
       Endif
    Endif

    ' update system
    If (New.Mail.Count Mod 4) = 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

    If Local.Mode Then
       Call Release.Time(1)
    Endif
 Loop
 Allow.Break=False
 Auto.Break=False
 Auto.Mode=False
 Break=False
 Wait.Mode=False
 If Var3=True Then
    Strng="Command time limit!"
    Call IO.O
 Endif
 If Var3=-2 Then
    Strng="Command rounds exceeded!"
    Call IO.O
 Endif
 Exit Sub

Valid.Treasure:
 TempB=True
 If TreasureRecord.Loadable Then
    TempB=False
 Endif
 If TreasureRecord.Launchable Then
    TempB=False
 Endif
 If TreasureRecord.Ammunition Then
    TempB=False
 Endif
 If TreasureRecord.LaunchAmmo Then
    TempB=False
 Endif
 Return

Pickup.Coins:
 TempA=False
 If TreasureRecord.Coin Then
    VarX#=TreasureRecord.Gold
    UserRecord.Gold=UserRecord.Gold+VarX#
    Graphics.Off=False
    Call Amount(VarX#,Var$)
    Strng="You pick up"+Var$+" Gold!"
    Call IO.O
    Gosub Discard.Treasure
    TempA=True
 Endif
 Return

Pickup.Item1:
 TempA=False
 If TreasureRecord.WeaponNumber=VarX Then
    VarQ!=UserRecord.Weapon(VarX).Index
    Var2=UserRecord.Weapon(VarX).Charges
    If TreasureRecord.Plus>UserRecord.Weapon(VarX).Plus Or VarQ!=SFalse Then
       If Temp6>False Then
          If VarQ!>SFalse Then
             Call Read.Record(TreasureFile,VarQ!)
             Call Add.Inventory(VarQ!,Var2)
          Endif
          UserRecord.Weapon(VarX).Index=Temp7!
          UserRecord.Weapon(VarX).Charges=Temp6
          UserRecord.Weapon(VarX).Plus=Temp8a
          TempA=True
       Endif
    Endif
 Endif
 Return

Pickup.Ring:
 TempA=False
 If TreasureRecord.WeaponNumber=VarX Then
    VarQ!=UserRecord.Weapon(VarX).Index
    Var2=UserRecord.Weapon(VarX).Charges
    If TreasureRecord.RingType=True Then
       If UserRecord.Weapon(VarX).Index=SFalse Then
          If Temp6>False Then
             If VarQ!>SFalse Then
                Call Read.Record(TreasureFile,VarQ!)
                Call Add.Inventory(VarQ!,Var2)
             Endif
             UserRecord.Weapon(VarX).Index=Temp7!
             UserRecord.Weapon(VarX).Charges=Temp6
             UserRecord.Weapon(VarX).Plus=Temp8a
             TempA=True
          Endif
       Endif
    Endif
 Endif
 Return

Pickup.Weapon:
 TempA=False
 If TreasureRecord.WeaponNumber=7 Then
    VarQ!=UserRecord.Weapon(7).Index
    Var2=UserRecord.Weapon(7).Charges
    If TreasureRecord.Plus>UserRecord.Weapon(7).Plus Or VarQ!=SFalse Then
       Temp8=True
       If UserRecord.ClassType=Cleric Then
          If TreasureRecord.Proficiency=Sharp Or TreasureRecord.Proficiency=Thrusting Then
             Temp8=False
          Endif
       Endif
       If Temp8 Then
          If Temp6>False Then
             If VarQ!>SFalse Then
                Call Read.Record(TreasureFile,VarQ!)
                Call Add.Inventory(VarQ!,Var2)
             Endif
             UserRecord.Weapon(7).Index=Temp7!
             UserRecord.Weapon(7).Charges=Temp6
             UserRecord.Weapon(7).Plus=Temp8a
             TempA=True
          Endif
       Endif
    Endif
 Endif
 Return

Take.Item:
 Gosub Discard.Treasure
 Graphics.Off=False
 Out2=Rtrim$(TreasureRecord.Prefix1)
 Out3=Rtrim$(TreasureRecord.TreasureName)

 Strng="You pick up "+Out2+Out3
 Call Convert.ANSI(True,VarQ,VarR)
 Carriage.Return=True
 Graphics.On=True
 Call IO.O
 Graphics.On=False
 Graphics.Off=True
 Strng="!"
 Call IO.O

 Strng="You use "+Out2+Out3
 Call Convert.ANSI(True,VarQ,VarR)
 Carriage.Return=True
 Graphics.On=True
 Call IO.O
 Graphics.On=False
 Graphics.Off=True
 Strng="!"
 Call IO.O
 Return

Discard.Treasure:
 Call Delete.List.Node(Temp5!)
 Call Clear.List.Node
 Call Write.Record(RoomInvFile,Temp5!)
 Return

Get.Config:
 VarX=0
 Option1#=-1#
 Option1!=-1!
 Option3=-1
 Do
    VarX=VarX+1
    If VarX>Options.Lines Then
       Exit Do
    Endif
    Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
    If Ucase$(Var$)="[CMD005]" Then
       Do
          VarX=VarX+1
          If VarX>Options.Lines Then
             Return
          Endif
          Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
          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)))
             Select Case Lcase$(Var2$)
             Case "break.switch"
                Select Case Ucase$(Var3$)
                Case "0", "FALSE","OFF"
                   Option3=0
                End Select
             Case "maximum.rounds.counter"
                option1#=Int(Val(Var3$))
             Case "round.time.delay"
                option1!=Csng(Val(Var3$))
             End Select
          Endif
       Loop
    Endif
 Loop
 Return
Err.Resume26:
 Exit Sub
Err.Trap26:
 Resume Err.Resume26
End Sub
