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

Sub Talk.To.Monster
 On Local Error Goto Err.Trap3
 Call Check.Monster
 Graphics.Off=False
 If Monster.Temp=SFalse Then
    Call Search.Mess(SFalse)
    If Temp!=Room Then
       If TempB!=User.Index Then
          If Rnd>.5 Then
             Strng="You can hear yourself think!"
          Else
             Strng="You find yourself talking to yourself!"
          Endif
          Call IO.O
          Exit Sub
       Endif
       If Rnd>.5 Then
          Strng="You chat for awhile!"
       Else
          Strng="You both talk for a bit!"
       Endif
       Call IO.O
       Exit Sub
    Endif
    Call Check.User
    If User.Temp=SFalse Then
       Strng="You can't talk to that!"
       Call IO.O
       Exit Sub
    Endif
    Call He.She
    If Rnd>.5 Then
       Strng=Prefix1+" sneers wickedly at you!"
    Else
       Strng=Prefix1+" looks at you rather strangely!"
    Endif
    Call IO.O
    Exit Sub
 Endif
 Temp2!=MessWorkRecord3.Monster.Talk(Int(Rnd*MaxMonTalk+1))
 Strng=Nul
 If Temp2!>SFalse And Temp2!<=Lof(MonTalkFile)/MonTalkRecLen Then
    Call Read.Record(MonTalkFile,Temp2!)
    Strng=Rtrim$(BufferRecord.Buffer)
    Var=Instr(Strng,"%1")
    If Var>False Then
       Strng3=UserRecord.CodeName
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif
    Var=Instr(Strng,"%2")
    If Var>False Then
       Strng3=UserRecord.ClassName
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif

    Var=Instr(Strng,"%3")
    If Var>False Then
       Strng3=Str$(UserRecord.Room)
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif
    Var=Instr(Strng,"%4")
    If Var>False Then
       Strng3=Str$(UserRecord.Level)
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif

    Var=Instr(Strng,"%5")
    If Var>False Then
       Strng3=Str$(UserRecord.Experience)
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif
    Var=Instr(Strng,"%6")
    If Var>False Then
       Strng3=Str$(UserRecord.Gold)
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif

    Var=Instr(Strng,"%7")
    If Var>False Then
       Strng3=Format$(Now,"mm-dd-yyyy")
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif
    Var=Instr(Strng,"%8")
    If Var>False Then
       Strng3=Format$(Now,"hh:mm:ss")
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif

    Var=Instr(Strng,"%9")
    If Var>False Then
       Strng3=Fclock$
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif
    Var=Instr(Strng,"%0")
    If Var>False Then
       Strng3=Fclock2$
       Strng3=Rtrim$(Strng3)
       Strng3=Lcase$(Strng3)
       Mid$(Strng3,1,1)=Ucase$(Mid$(Strng3,1,1))
       Strng=Left$(Strng,Var-1)+Strng3+Mid$(Strng,Var+2)
    Endif
 Endif
 ' display talk string
 Var1=False
 If Len(Strng) Then
    Var1=True
    Call IO.O
 Endif
 ' move monster
 Out3=Lcase$(Rtrim$(MessWorkRecord3.Monster.MonsterName))
 If MessWorkRecord3.Monster.TalkMoveDirection Then
    If MessWorkRecord3.Monster.TalkMoveDirection=-1 Then ' random
       ' count directions
       Var4=False
       For Var3=1 To MaxDirections
          If RoomRecord.Direct(Var3)>0! Then
             Var4=True
             Exit For
          Endif
       Next
       If RoomRecord.Direct(13)>0! Then
          If Len(Rtrim$(RoomRecord.Custom1)) Then
             Var4=True
          Endif
       Endif
       If RoomRecord.Direct(14)>0! Then
          If Len(Rtrim$(RoomRecord.Custom2)) Then
             Var4=True
          Endif
       Endif
       ' move random direction
       If Var4 Then
          Break=False
          Var=MaxDirections+2
          Do While Lost.Carrier=False
             Var2=Int(Rnd*Var+1) ' get random direction
             Var!=RoomRecord.Direct(Var2)
             If Var!>0! Then
                Strng2=Nul
                Select Case Var2
                Case 1 To 12
                   Strng2=Direction(Var2)
                Case 13
                   Strng2=RoomRecord.Custom1
                Case 14
                   Strng2=RoomRecord.Custom2
                End Select
                Strng2=Lcase$(Rtrim$(Strng2))
                If Len(Strng2) Then
                   Var1$="The "+Out3+" moves "+Strng2+"!"
                   If Debug.Mode Then
                      Strng="The "+Out3+" moves "+Strng2+" to room#"+Mid$(Str$(Var!),2)+"!"
                      Call IO.O
                   Else
                      Strng="The "+Out3+" moves "+Strng2+"!"
                      Call IO.O
                   Endif
                   MessWorkRecord3.Room=Var!
                   MessWorkRecord3.Monster.RemainsInRoom=True
                   Call Write.Record(MessWorkFile3,Temp5!)
                   Out2=Var1$
                   Call Send.Mess(8,SFalse,Room,Out2)
                   Exit Sub
                Endif
             Endif
             Call Check.Carrier
             Call Check.Break
             If Break Then
                Exit Do
             Endif
          Loop
       Endif
    Else ' move in direction
       Var2=MessWorkRecord3.Monster.TalkMoveDirection
       Var!=RoomRecord.Direct(Var2)
       If Var!>0! Then
          Strng2=Nul
          Select Case Var2
          Case 1 To 12
             Strng2=Direction(Var2)
          Case 13
             Strng2=RoomRecord.Custom1
          Case 14
             Strng2=RoomRecord.Custom2
          End Select
          Strng2=Lcase$(Rtrim$(Strng2))
          If Len(Strng2) Then
             Var1$="The "+Out3+" moves "+Strng2+"!"
             If Debug.Mode Then
                Strng="The "+Out3+" moves "+Strng2+" to room#"+Mid$(Str$(Var!),2)+"!"
                Call IO.O
             Else
                Strng="The "+Out3+" moves "+Strng2+"!"
                Call IO.O
             Endif
             MessWorkRecord3.Room=Var!
             MessWorkRecord3.Monster.RemainsInRoom=True
             Call Write.Record(MessWorkFile3,Temp5!)
             Out2=Var1$
             Call Send.Mess(8,SFalse,Room,Out2)
             Exit Sub
          Endif
       Endif
    Endif
 Endif
 ' check monster rewards
 Var2=MessWorkRecord3.Monster.PerMonster
 Select Case Var2
 Case 1 ' per monster
    Var!=MessWorkRecord3.Index
    Call Read.Record(MonsterFile,Var!)
    Out3=Lcase$(Rtrim$(MonsterRecord.MonsterName))
    ' gold reward
    If MonsterRecord.TalkGold<>DFalse Then
       If MonsterRecord.TalkGoldTimes<>0 Then
          If MonsterRecord.TalkGoldPercent<>0 Then
             If Int(Rnd*100+1)<MonsterRecord.TalkGoldPercent Then
                If MonsterRecord.TalkGold>DFalse Then
                   If MessWorkRecord3.Monster.Gold-MonsterRecord.TalkGold>=DFalse Then
                      UserRecord.Gold=UserRecord.Gold+MonsterRecord.TalkGold
                      MessWorkRecord3.Monster.Gold=MessWorkRecord3.Monster.Gold-MonsterRecord.TalkGold
                      Call Write.Record(MessWorkFile3,Var!)
                      Strng="The "+Out3+" gives you "+Form$(MonsterRecord.TalkGold)+" Gold!"
                      Call IO.O
                   Else
                      If MessWorkRecord3.Monster.Gold>DFalse Then
                         UserRecord.Gold=UserRecord.Gold+MessWorkRecord3.Monster.Gold
                         Strng="The "+Out3+" gives you "+Form$(MessWorkRecord3.Monster.Gold)+" Gold!"
                         Call IO.O
                         MessWorkRecord3.Monster.Gold=DFalse
                         Call Write.Record(MessWorkFile3,Var!)
                      Endif
                   Endif
                Else
                   If MonsterRecord.TalkGold<DFalse Then
                      Temp#=UserRecord.Gold
                      If Temp#>DFalse Then
                         If Temp#-Abs(MonsterRecord.TalkGold)<DFalse Then
                            MessWorkRecord3.Monster.Gold=MessWorkRecord3.Monster.Gold+UserRecord.Gold
                            Call Write.Record(MessWorkFile3,Var!)
                            If Instr(Str$(UserRecord.Gold),"D") Then
                               Strng="The "+Out3+" takes"+Str$(UserRecord.Gold)+" Gold from you!"
                            Else
                               Strng="The "+Out3+" takes "+Form$(UserRecord.Gold)+" Gold from you!"
                            Endif
                            Call IO.O
                            UserRecord.Gold=DFalse
                         Else
                            MessWorkRecord3.Monster.Gold=MessWorkRecord3.Monster.Gold+MessWorkRecord3.Monster.TalkGold
                            Call Write.Record(MessWorkFile3,Var!)
                            Strng="The "+Out3+" takes "+Form$(Abs(MonsterRecord.TalkGold))+" Gold from you!"
                            Call IO.O
                            UserRecord.Gold=UserRecord.Gold-Abs(MonsterRecord.TalkGoldPercent)
                         Endif
                      Endif
                   Endif
                Endif
                Var1=True
                If MonsterRecord.TalkGoldTimes>0 Then
                   MonsterRecord.TalkGoldTimes=MonsterRecord.TalkGoldTimes-1
                   Call Write.Record(MonsterFile,Var!)
                Endif
             Endif
          Endif
       Endif
    Endif
    ' item reward
    If MonsterRecord.TalkItem<>0! Then
       If MonsterRecord.TalkItemTimes<>0 Then
          If MonsterRecord.TalkItemPercent<>0 Then
             If Int(Rnd*100+1)<MonsterRecord.TalkItemPercent Then
                Temp!=MonsterRecord.TalkItem
                If Temp!>SFalse And Temp!<=Lof(TreasureFile)/TreasureRecLen Then
                   Call Read.Record(TreasureFile,Temp!)
                   Out4=TreasureRecord.TreasureName
                   Out4=Lcase$(Rtrim$(Out4))
                   Strng="The "+Out3+" gives you "+Out4+"!"
                   Call IO.O
                   Temp6=TreasureRecord.Charges
                   If TreasureRecord.Coin Then
                      Graphics.Off=False
                      UserRecord.Gold=UserRecord.Gold+TreasureRecord.Gold
                      If Instr(Str$(UserRecord.Gold),"D") Then
                         Strng="You now have"+Str$(UserRecord.Gold)+" Gold!"
                      Else
                         Strng="You now have "+Form$(UserRecord.Gold)+" Gold!"
                      Endif
                      Call IO.O
                   Else
                      If TreasureRecord.Container Then
                         Call Drop.Container.In.User(Temp!)
                      Else
                         Call Add.Inventory(Temp!,Temp6)
                      Endif
                   Endif
                   Var1=True
                   If MonsterRecord.TalkItemTimes>0 Then
                      MonsterRecord.TalkItemTimes=MonsterRecord.TalkItemTimes-1
                      Call Write.Record(MonsterFile,Var!)
                   Endif
                Endif
             Endif
          Endif
       Endif
    Endif
 Case 2 ' per encounter
    Var!=Monster.Temp
    Call Read.Record(MessWorkFile3,Var!)
    Out3=Lcase$(Rtrim$(MessWorkRecord3.Monster.MonsterName))
    ' gold reward
    If MessWorkRecord3.Monster.TalkGold<>DFalse Then
       If MessWorkRecord3.Monster.TalkGoldTimes<>0 Then
          If MessWorkRecord3.Monster.TalkGoldPercent<>0 Then
             If Int(Rnd*100+1)<MessWorkRecord3.Monster.TalkGoldPercent Then
                If MessWorkRecord3.Monster.TalkGold>DFalse Then
                   If MessWorkRecord3.Monster.Gold-MonsterRecord.TalkGold>=DFalse Then
                      MessWorkRecord3.Monster.Gold=MessWorkRecord3.Monster.Gold-MonsterRecord.TalkGold
                      Call Write.Record(MessWorkFile3,Var!)
                      UserRecord.Gold=UserRecord.Gold+MessWorkRecord3.Monster.TalkGold
                      Strng="The "+Out3+" gives you "+Form$(MessWorkRecord3.Monster.TalkGold)+" Gold!"
                      Call IO.O
                   Else
                      If MessWorkRecord3.Monster.Gold>DFalse Then
                         UserRecord.Gold=UserRecord.Gold+MessWorkRecord3.Monster.Gold
                         Strng="The "+Out3+" gives you "+Form$(MessWorkRecord3.Monster.Gold)+" Gold!"
                         Call IO.O
                         MessWorkRecord3.Monster.Gold=DFalse
                         Call Write.Record(MessWorkFile3,Var!)
                      Endif
                   Endif
                Else
                   If MessWorkRecord3.Monster.TalkGold<DFalse Then
                      Temp#=UserRecord.Gold
                      If Temp#>DFalse Then
                         If Temp#-Abs(MessWorkRecord3.Monster.TalkGold)<DFalse Then
                            MessWorkRecord3.Monster.Gold=MessWorkRecord3.Monster.Gold+UserRecord.Gold
                            Call Write.Record(MessWorkFile3,Var!)
                            If Instr(Str$(UserRecord.Gold),"D") Then
                               Strng="The "+Out3+" takes"+Str$(UserRecord.Gold)+" Gold from you!"
                            Else
                               Strng="The "+Out3+" takes "+Form$(UserRecord.Gold)+" Gold from you!"
                            Endif
                            Call IO.O
                            UserRecord.Gold=DFalse
                         Else
                            MessWorkRecord3.Monster.Gold=MessWorkRecord3.Monster.Gold+MessWorkRecord3.Monster.TalkGold
                            Call Write.Record(MessWorkFile3,Var!)
                            Strng="The "+Out3+" takes "+Form$(Abs(MessWorkRecord3.Monster.TalkGold))+" Gold from you!"
                            Call IO.O
                            UserRecord.Gold=UserRecord.Gold-Abs(MessWorkRecord3.Monster.TalkGoldPercent)
                         Endif
                      Endif
                   Endif
                Endif
                Var1=True
                If MessWorkRecord3.Monster.TalkGoldTimes>0 Then
                   MessWorkRecord3.Monster.TalkGoldTimes=MessWorkRecord3.Monster.TalkGoldTimes-1
                   Call Write.Record(MessWorkFile3,Var!)
                Endif
             Endif
          Endif
       Endif
    Endif
    ' item reward
    If MessWorkRecord3.Monster.TalkItem<>0! Then
       If MessWorkRecord3.Monster.TalkItemTimes<>0 Then
          If MessWorkRecord3.Monster.TalkItemPercent<>0 Then
             If Int(Rnd*100+1)<MessWorkRecord3.Monster.TalkItemPercent Then
                Temp!=MessWorkRecord3.Monster.TalkItem
                If Temp!>SFalse And Temp!<=Lof(TreasureFile)/TreasureRecLen Then
                   Call Read.Record(TreasureFile,Temp!)
                   Out4=TreasureRecord.TreasureName
                   Out4=Lcase$(Rtrim$(Out4))
                   Strng="The "+Out3+" gives you "+Out4+"!"
                   Call IO.O
                   Temp6=TreasureRecord.Charges
                   If TreasureRecord.Coin Then
                      Graphics.Off=False
                      UserRecord.Gold=UserRecord.Gold+TreasureRecord.Gold
                      If Instr(Str$(UserRecord.Gold),"D") Then
                         Strng="You now have"+Str$(UserRecord.Gold)+" Gold!"
                      Else
                         Strng="You now have "+Form$(UserRecord.Gold)+" Gold!"
                      Endif
                      Call IO.O
                   Else
                      If TreasureRecord.Container Then
                         Call Drop.Container.In.User(Temp!)
                      Else
                         Call Add.Inventory(Temp!,Temp6)
                      Endif
                   Endif
                   Var1=True
                   If MessWorkRecord3.Monster.TalkItemTimes>0 Then
                      MessWorkRecord3.Monster.TalkItemTimes=MessWorkRecord3.Monster.TalkItemTimes-1
                      Call Write.Record(MessWorkFile3,Var!)
                   Endif
                Endif
             Endif
          Endif
       Endif
    Endif
 End Select
 ' check no monster reply
 If Var1=False Then
    Strng="The monster doesn't reply!"
    Call IO.O
 Endif
 ' check room action
 Call Read.Record(RoomFile,Room)
 Var!=RoomRecord.Action
 If Var!>SFalse And Var!<=Lof(ActionFile)/ActionRecLen Then
    Call Read.Record(ActionFile,Var!)
    Var2=Abs(MessWorkRecord3.Monster.Permanent)+1
    If ActionRecord.MonsterTalk(Var2)=MessWorkRecord3.Index Then
       Out3="As you begin to speak,"
       Out4="The monster hits you for"
       Action.Recurse=0
       Call Actions(Var!)
    Endif
 Endif
Err.Resume3:
 Exit Sub
Err.Trap3:
 Resume Err.Resume3
End Sub
