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

Sub Unlock.Object
 On Local Error Goto ErrTrap30
 Gosub Get.Config1
 Call Parse
 If Parser Then
    Call Numeric
 Endif
 Call Check.Room.Items
 If Temp7!>SFalse Then
    Strng="You can't unlock that!"
    Call IO.O
    Exit Sub
 Endif
 If Temp7!=SFalse Then
    Call Num
    Call Check.User.Items
    If Temp7!>SFalse Then
       Strng="You can't unlock that!"
       Call IO.O
       Exit Sub
    Endif
    If Temp7!=SFalse Then
       Call Num
       Call Examine.Containers
       If TempA! Then
          Call Unlock.Container
          Exit Sub
       Endif
    Endif
 Endif
 If Temp7!<SFalse Then
    Temp7!=Abs(Temp7!)
    Call Return.Room(Var!)
    If Var!=SFalse Then
       Strng="You can't unlock that!"
       Call IO.O
       Exit Sub
    Endif
    If ObjectRecord.DoorLock=False Then
       Strng="You can't unlock that!"
       Call IO.O
       Exit Sub
    Endif
    If ObjectRecord.DoorLock=1 Then
       Strng="It's already unlocked!"
       Call IO.O
       Exit Sub
    Endif
    If ObjectRecord.KeyNumber=SFalse Then
       ObjectRecord.DoorLock=1
       ObjectRecord.Closed=True
       Call Write.Record(ObjectFile,Temp7!)
       Strng="You unlock it!"
       Call IO.O
       Exit Sub
    Endif
    If option1 Then
       For Temp2!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
          Call Read.Record(MessWorkFile6,Temp2!)
          If MessWorkRecord6.User=User.Index Then
             VarQ!=MessWorkRecord6.Index
             If VarQ!>SFalse And VarQ!<=Lof(TreasureFile)/TreasureRecLen Then
                Call Read.Record(TreasureFile,VarQ!)
                If TreasureRecord.MasterKey Then
                   ObjectRecord.DoorLock=1
                   ObjectRecord.Closed=True
                   Call Write.Record(ObjectFile,Temp7!)
                   Strng="You unlock it!"
                   Call IO.O
                   Exit Sub
                Endif
             Endif
          Endif
       Next
    Endif
    If option2 Then
       Out2=Right$(Str$(ObjectRecord.KeyNumber+1000000!),6)
       For Temp2!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
          Call Read.Record(MessWorkFile6,Temp2!)
          If MessWorkRecord6.User=User.Index Then
             VarQ!=MessWorkRecord6.Index
             If VarQ!>SFalse And VarQ!<=Lof(TreasureFile)/TreasureRecLen Then
                Call Read.Record(TreasureFile,VarQ!)
                If TreasureRecord.KeyNumber Then
                   Temp8=True
                   Out3=Right$(Str$(TreasureRecord.KeyNumber+1000000!),6)
                   For Temp3=1 To MaxKeyDigits
                      Temp1=Val(Mid$(Out3,Temp3,1))
                      If Temp1<>Val(Mid$(Out2,Temp3,1)) Then
                         If Temp1>False Then
                            Temp8=False
                            Exit For
                         Endif
                      Endif
                   Next
                   If Temp8 Then
                      ObjectRecord.DoorLock=1
                      ObjectRecord.Closed=True
                      Call Write.Record(ObjectFile,Temp7!)
                      Strng="You unlock it!"
                      Call IO.O
                      Exit Sub
                   Endif
                Endif
             Endif
          Endif
       Next
    Endif
    Strng="You don't have the key!"
    Call IO.O
    Exit Sub
 Endif
 Strng="You can't unlock that!"
 Call IO.O
 Exit Sub
Get.Config1:
 option1=-1
 option2=-1
 VarX=0
 Do
    VarX=VarX+1
    If VarX>Options.Lines Then
       Goto Next.Read1
       Exit Do
    Endif
    Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
    If Ucase$(Var$)="[CMD135]" Then
       Do
          VarX=VarX+1
          If VarX>Options.Lines Then
             Goto Next.Read1
             Return
          Endif
          Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
          If Ucase$(Var$)="[END]" Then
             Goto Next.Read1
             Return
          Endif
          Var=Instr(Var$,"=")
          If Var Then
             Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
             Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
             Gosub Parse.Options1
          Endif
       Loop
    Endif
 Loop
 Return
Next.Read1:
 Close #TempFile1
 Filename$=DND.Path+"CMD135.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
          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.Options1
             Endif
          Loop
       Endif
    Endif
 Loop
 Return
Parse.Options1:
 Select Case Lcase$(Var2$)
 Case "allow.master.key"
    Select Case Lcase$(Var3$)
    Case "0", "false", "off"
       option1=0
    End Select
 Case "allow.matching.key"
    Select Case Lcase$(Var3$)
    Case "0", "false", "off"
       option2=0
    End Select
 End Select
 Return
ErrResume30:
 Exit Sub
ErrTrap30:
 Resume ErrResume30
End Sub

Sub Unlock.Container
 On Local Error Goto ErrTrap36
 Gosub Get.Config2
 If ContainerRecord.Damaged=True Then
    Strng="You can't, it's damaged!"
    Call IO.O
    Exit Sub
 Endif
 If ContainerRecord.Locked=False Then ' has no lock
    Strng="You can't unlock that!"
    Call IO.O
    Exit Sub
 Endif
 If ContainerRecord.Locked=1 Then ' is unlocked
    Strng="It's already unlocked!"
    Call IO.O
    Exit Sub
 Endif
 If ContainerRecord.KeyNumber=SFalse Then ' has no key
    ContainerRecord.Locked=1 ' unlocked
    ContainerRecord.Closed=True ' closed
    Call Write.Record(ContainerFile,TempA!)
    Strng="You unlock it!"
    Call IO.O
    Exit Sub
 Endif
 ' user has master key
 If option1 Then
    For Temp2!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
       Call Read.Record(MessWorkFile6,Temp2!)
       If MessWorkRecord6.User=User.Index Then
          VarQ!=MessWorkRecord6.Index
          If VarQ!>SFalse And VarQ!<=Lof(TreasureFile)/TreasureRecLen Then
             Call Read.Record(TreasureFile,VarQ!)
             If TreasureRecord.MasterKey Then
                ContainerRecord.Locked=1
                ContainerRecord.Closed=True
                Call Write.Record(ContainerFile,TempA!)
                Strng="You unlock it!"
                Call IO.O
                Exit Sub
             Endif
          Endif
       Endif
    Next
 Endif
 ' user has matching keynumber
 If option2 Then
    Out2=Right$(Str$(ContainerRecord.KeyNumber+1000000!),6)
    For Temp2!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
       Call Read.Record(MessWorkFile6,Temp2!)
       If MessWorkRecord6.User=User.Index Then
          VarQ!=MessWorkRecord6.Index
          If VarQ!>SFalse And VarQ!<=Lof(TreasureFile)/TreasureRecLen Then
             Call Read.Record(TreasureFile,VarQ!)
             If TreasureRecord.KeyNumber Then
                Temp8=True
                Out3=Right$(Str$(TreasureRecord.KeyNumber+1000000!),6)
                For Temp3=1 To MaxKeyDigits
                   Temp1=Val(Mid$(Out3,Temp3,1))
                   If Temp1<>Val(Mid$(Out2,Temp3,1)) Then
                      If Temp1>False Then
                         Temp8=False
                         Exit For
                      Endif
                   Endif
                Next
                If Temp8 Then
                   ContainerRecord.Locked=1
                   ContainerRecord.Closed=True
                   Call Write.Record(ContainerFile,TempA!)
                   Strng="You unlock it!"
                   Call IO.O
                   Exit Sub
                Endif
             Endif
          Endif
       Endif
    Next
 Endif
 Strng="You don't have the key!"
 Call IO.O
 Exit Sub
Get.Config2:
 option1=-1
 option2=-1
 VarX=0
 Do
    VarX=VarX+1
    If VarX>Options.Lines Then
       Goto Next.Read2
       Exit Do
    Endif
    Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
    If Ucase$(Var$)="[CMD135]" Then
       Do
          VarX=VarX+1
          If VarX>Options.Lines Then
             Goto Next.Read2
             Return
          Endif
          Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
          If Ucase$(Var$)="[END]" Then
             Goto Next.Read2
             Return
          Endif
          Var=Instr(Var$,"=")
          If Var Then
             Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
             Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
             Gosub Parse.Options2
          Endif
       Loop
    Endif
 Loop
 Return
Next.Read2:
 Close #TempFile1
 Filename$=DND.Path+"CMD135.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
          option1=-1
          option2=-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.Options2
             Endif
          Loop
       Endif
    Endif
 Loop
 Return
Parse.Options2:
 Select Case Lcase$(Var2$)
 Case "allow.container.master.key"
    Select Case Lcase$(Var3$)
    Case "0", "false", "off"
       option1=0
    End Select
 Case "allow.container.matching.key"
    Select Case Lcase$(Var3$)
    Case "0", "false", "off"
       option2=0
    End Select
 End Select
 Return
ErrResume36:
 Exit Sub
ErrTrap36:
 Resume ErrResume36
End Sub
