 Rem File: Dndsub5 for Dndbbs Version v5.0a Release r2.0 Tweak t5.0a
 Rem $Include: 'dndbbs.inc'
Sub Init.Mail(Var)
 On Local Error Goto ErrTrap1
 Message.Base.Name=Config3(58) ' public
 Gosub Reset.Mail
 Message.Base.Name=Config3(60) ' private
 Gosub Reset.Mail
 For Temp3!=1! To Lof(MessBaseFile)/MessBaseRecLen
    Call Read.Record(MessBaseFile,Temp3!)
    If MessageBaseRecord.BaseDeleted=False Then
       Message.Base.Name=MessageBaseRecord.BaseFileName
       Gosub Reset.Mail
    Endif
 Next
 Exit Sub
Reset.Mail:
 Close #NewFile
 FileName=Data.Path4+Message.Base.Name+".NEW" ' public message to user
 Open FileName For Random Shared As #NewFile Len=NewMessRecLen
 Gosub Init.Messages

 Close #NewFile
 FileName=Data.Path4+Message.Base.Name+".NW2" ' public message to ALL
 Open FileName For Random Shared As #NewFile Len=NewMessRecLen
 Gosub Init.Messages

 Close #NewFile
 FileName=Data.Path4+Message.Base.Name+".NW3" ' private message to user
 Open FileName For Random Shared As #NewFile Len=NewMessRecLen
 Gosub Init.Messages
 Return

Init.Messages:
 Var!=Lof(NewFile)/NewMessRecLen
 If User.Index>Var! Then
    For Var1!=Var!+1! To User.Index
       NewMessages.LastMessage=SFalse
       Call Write.Record(NewFile,Var1!)
    Next
 Endif

 If Var=False Then
    NewMessages.LastMessage=SFalse
    Call Write.Record(NewFile,User.Index)
 Endif
 Return
ErrResume1:
 Exit Sub
ErrTrap1:
 Resume ErrResume1
End Sub

Sub New.Mail
 On Local Error Goto ErrTrap2
 Strng="New mail addressed to you:"
 Call IO.O
 Message.Base.Name=Config3(58)
 Message.Base="public"
 Gosub Check.Mail
 Message.Base.Name=Config3(60)
 Message.Base="private"
 Gosub Check.Mail
 If Lof(MessBaseFile)/MessBaseRecLen>SFalse Then
    Var=False
    For Temp3!=1! To Lof(MessBaseFile)/MessBaseRecLen
       Call Read.Record(MessBaseFile,Temp3!)
       If MessageBaseRecord.BaseDeleted=False Then
          Var=True
          Exit For
       Endif
    Next
    If Var Then
       Strng="Check all message bases for new mail(y/n)? "
       Line.Length=1
       No.Input.Out="Y"
       Call IO.I
       If Yes Then
          VarZ=0
          For Temp3!=1! To Lof(MessBaseFile)/MessBaseRecLen
             Call Read.Record(MessBaseFile,Temp3!)
             If MessageBaseRecord.BaseDeleted=False Then
                Message.Base.Name=MessageBaseRecord.BaseFileName
                Message.Base=Rtrim$(MessageBaseRecord.BaseMessName)
                Gosub Check.Mail
                VarZ=VarZ+1
                If VarZ>=User.Pagelength-2 Then
                   VarZ=False
                   Call More.Prompt
                   If No Or Quit Then
                      Exit For
                   Endif
                Endif
             Endif
          Next
       Endif
    Endif
 Endif
 Exit Sub

Check.Mail:
 Close #TableFile
 Message.Base.Name=Rtrim$(Message.Base.Name)
 FileName=Data.Path4+Message.Base.Name+".TBL"
 Open FileName For Random Shared As #TableFile Len=TableRecLen

 Close #NewFile
 FileName=Data.Path4+Message.Base.Name+".NEW" ' public message to user
 Open FileName For Random Shared As #NewFile Len=NewMessRecLen
 VarX=1
 Gosub Check.New.Mail
 VarA!=Temp5!

 Close #NewFile
 FileName=Data.Path4+Message.Base.Name+".NW3" ' private message to user
 Open FileName For Random Shared As #NewFile Len=NewMessRecLen
 VarX=3
 Gosub Check.New.Mail
 VarB!=Temp5!

 Graphics.Off=True
 Strng=Lcase$(Message.Base)
 Mid$(Strng,1,1)=Ucase$(Mid$(Strng,1,1))
 Strng=Quote+Strng+Quote+" Message Base"
 If Debug.Mode Then
    If Temp7!=0! Then
       Strng=Strng+"(0) "
    Else
       Strng=Strng+"("+Form$(Cdbl(Temp7!))+") "
    Endif
 Endif
 Strng=Strng+"> "
 If VarA!=0! Then
    Strng=Strng+"0"
 Else
    Strng=Strng+Form$(Cdbl(VarA!))
 Endif
 Strng=Strng+" public, "
 If VarB!=0! Then
    Strng=Strng+"0"
 Else
    Strng=Strng+Form$(Cdbl(VarB!))
 Endif
 Strng=Strng+" private."
 Call IO.O
 Graphics.Off=False
 Return

Check.New.Mail:
 Temp5!=SFalse
 Temp7!=SFalse
 Strng=UserRecord.CodeName
 Strng=Rtrim$(Strng)
 Strng=Lcase$(Strng)

 ' extend new message pointers
 Var!=Lof(NewFile)/NewMessRecLen
 If User.Index>Var! Then
    For Var1!=Var!+1! To User.Index
       NewMessages.LastMessage=SFalse
       Call Write.Record(NewFile,Var1!)
    Next
 Endif

 Call Read.Record(NewFile,User.Index)
 Temp1!=NewMessages.LastMessage
 Temp2!=Lof(TableFile)/TableRecLen
 If Temp1!>=SFalse And Temp1!<Temp2! Then
    For Temp4!=Temp1!+1! To Temp2!
       Call Read.Record(TableFile,Temp4!)
       If TableRecord.Killed=False Then
          Strng2=TableRecord.To
          Strng2=Rtrim$(Strng2)
          Strng2=Lcase$(Strng2)
          If Strng=Strng2 Then
             Temp7!=Temp7!+1! ' total messages
             If VarX=1 Then ' public message to user
                If TableRecord.Private=False Then
                   Temp5!=Temp5!+1!
                Endif
             Else ' private message to user
                If TableRecord.Private Then
                   Temp5!=Temp5!+1!
                Endif
             Endif
          Endif
       Endif
    Next
 Endif
 Return
ErrResume2:
 Exit Sub
ErrTrap2:
 Resume ErrResume2
End Sub

Sub Add.Room.Container(VarA!)
 On Local Error Goto ErrResume141h
 ' add container to rooms.dat list
 Call Read.Record(RoomFile,Room)
 ContainerRecord.First=SFalse
 ContainerRecord.Last=SFalse
 If RoomRecord.First2=SFalse Then
    RoomRecord.First2=VarA!
    RoomRecord.Last2=VarA!
    Call Write.Record(RoomFile,Room)
    ContainerRecord.Prev=SFalse
    ContainerRecord.Next=SFalse
    Call Write.Record(ContainerFile,VarA!)
 Else
    ContainerRecord.Prev=RoomRecord.Last2
    ContainerRecord.Next=SFalse
    Call Write.Record(ContainerFile,VarA!)
    Call Read.Record(ContainerFile,RoomRecord.Last2)
    ContainerRecord.Next=VarA!
    Call Write.Record(ContainerFile,RoomRecord.Last2)
    Call Read.Record(ContainerFile,VarA!)
    RoomRecord.Last2=VarA!
    Call Write.Record(RoomFile,Room)
 Endif
ErrExit141h:
 Exit Sub
ErrResume141h:
 Resume ErrExit141h
End Sub

Sub Clear.Container
 On Local Error Goto ErrTrap46
 ContainerRecord.ContainerName=Nul
 ContainerRecord.ContainerShort=Nul
 ContainerRecord.NamePrefix="in it"
 ContainerRecord.ContainerMax=False
 ContainerRecord.Closed=False
 ContainerRecord.Locked=False
 ContainerRecord.KeyNumber=SFalse
 ContainerRecord.UserNumber=SFalse
 ContainerRecord.RoomNumber=SFalse
 ContainerRecord.DeletedContainer=False
 ContainerRecord.InvisibleContainer=False
 ContainerRecord.Weight=False
 ContainerRecord.WithinContainer=SFalse
 ContainerRecord.Disappears=SFalse
 ContainerRecord.ContainerIndex=SFalse
 ContainerRecord.Size=False
 ContainerRecord.Material=False
 ContainerRecord.Permanent=False
 ContainerRecord.SelfImplodes=False
 ContainerRecord.First=SFalse
 ContainerRecord.Last=SFalse
 ContainerRecord.First2=SFalse
 ContainerRecord.Last2=SFalse
 ContainerRecord.Prefix1=""
 ContainerRecord.Damaged=False
 ContainerRecord.Scanned=False
ErrResume46:
 Exit Sub
ErrTrap46:
 Resume ErrResume46
End Sub

Sub Clear.Containers
 On Local Error Goto ErrTrap47
 For Temp0!=1! To Lof(ContainerFile)/ContainerRecLen
    Call Read.Record(ContainerFile,Temp0!)
    If ContainerRecord.UserNumber=User.Index Then
       Call Clear.Container
       Call Write.Record(ContainerFile,Temp0!)
    Endif
 Next
 UserRecord.First2=SFalse
 UserRecord.Last2=SFalse
ErrResume47:
 Exit Sub
ErrTrap47:
 Resume ErrResume47
End Sub

Sub Clear.User.Inventory
 On Local Error Goto Err.Trap28x
 For Temp5!=1! To Lof(MessWorkFile6)/MessWorkRecLen6
    Call Read.Record(MessWorkFile6,Temp5!)
    If MessWorkRecord6.User=User.Index Then
       MessWorkRecord6.User=SFalse
       MessWorkRecord6.Room=SFalse
       MessWorkRecord6.Index=SFalse
       MessWorkRecord6.Charges=False
       MessWorkRecord6.Plus=False
       MessWorkRecord6.Prev=SFalse
       MessWorkRecord6.Next=SFalse
       Call Write.Record(MessWorkFile6,Temp5!)
    Endif
 Next
Err.Resume28x:
 Exit Sub
Err.Trap28x:
 Resume Err.Resume28x
End Sub

Sub Add.Container.To.User(Var2!)
 On Local Error Goto ErrTrap48a
 ' add container to users.dat list
 VarA!=Lof(ContainerFile)/ContainerRecLen+1!
 Call Clear.Container
 ContainerRecord.ContainerName=TreasureRecord.TreasureName
 ContainerRecord.ContainerShort=TreasureRecord.ShortName
 ContainerRecord.ContainerMax=TreasureRecord.ContainerMax
 ContainerRecord.NamePrefix=TreasureRecord.ContainerPrefix
 ContainerRecord.Prefix1=TreasureRecord.Prefix1
 ContainerRecord.Size=TreasureRecord.ContainerSize
 ContainerRecord.Material=TreasureRecord.ContainerMaterial
 ContainerRecord.InvisibleContainer=False
 If TreasureRecord.KeyNumber Then
    ContainerRecord.KeyNumber=TreasureRecord.KeyNumber
    ContainerRecord.Closed=True
    ContainerRecord.Locked=True
 Endif
 If TreasureRecord.Locked Then
    ContainerRecord.Closed=True
    ContainerRecord.Locked=True
 Endif
 ContainerRecord.RoomNumber=SFalse
 ContainerRecord.UserNumber=User.Index
 ContainerRecord.WithinContainer=SFalse
 ContainerRecord.Weight=TreasureRecord.Weight
 ContainerRecord.ContainerIndex=Abs(Var2!)
 ContainerRecord.Disappears=TreasureRecord.ContainerDisappears
 ContainerRecord.SelfImplodes=TreasureRecord.ContainerSelfImplodes
 ContainerRecord.Permanent=TreasureRecord.Permanent
 ContainerRecord.First=SFalse
 ContainerRecord.Last=SFalse
 ContainerRecord.SwapFlag=SFalse
 ContainerRecord.Scanned=False
 For Var=1 To 3
    ContainerRecord.RestrictItem(Var)=TreasureRecord.RestrictItem(Var)
 Next
 ContainerRecord.TrapType=TreasureRecord.TrapType
 ContainerRecord.TrapHits=TreasureRecord.TrapHits
 ContainerRecord.TrapItem=TreasureRecord.TrapItem
 Call Write.Record(ContainerFile,VarA!)
 Call Add.User.Container(VarA!)
ErrResume48a:
 Exit Sub
ErrTrap48a:
 Resume ErrResume48a
End Sub

' add container to users.dat list
Sub Add.User.Container(VarA!)
 On Local Error Goto ErrResume141g
 ContainerRecord.First=SFalse
 ContainerRecord.Last=SFalse
 ContainerRecord.SwapFlag=False
 If UserRecord.First2=SFalse Then
    UserRecord.First2=VarA!
    UserRecord.Last2=VarA!
    Call Write.Record(UserFile,User.Index)
    ContainerRecord.Prev=SFalse
    ContainerRecord.Next=SFalse
    Call Write.Record(ContainerFile,VarA!)
 Else
    ContainerRecord.Prev=UserRecord.Last2
    ContainerRecord.Next=SFalse
    Call Write.Record(ContainerFile,VarA!)
    Call Read.Record(ContainerFile,UserRecord.Last2)
    ContainerRecord.Next=VarA!
    Call Write.Record(ContainerFile,UserRecord.Last2)
    Call Read.Record(ContainerFile,VarA!)
    UserRecord.Last2=VarA!
    Call Write.Record(UserFile,User.Index)
 Endif
ErrExit141g:
 Exit Sub
ErrResume141g:
 Resume ErrExit141g
End Sub
