unit cheri2;
{$O+,F+}
interface

procedure cherichat;

implementation

Uses Dos,CRT,bbskv,bbskern,engine2,rgoods,sys_msg;

Type  KeywordRec = Record
        Word  : String[10];
        Responses : Byte;
        Index : Byte;
      end;

Var F       :Text;
    KeyWord : Array[1..50] of KeyWordRec;
    NumKeys : Byte;
    donechat:boolean;
    totalresponses:word;
    index:integer;

Procedure ReadData;
Var I,J:Integer;
    STmp:String;
begin
  Assign(F,'CHERI.DAT'); Reset(F); I:=0;
  Repeat
    Inc(I);
    Readln(F,Keyword[I].Word);
    Readln(F,Keyword[I].Responses);
    Keyword[I].Index:=0;
    For J:=1 to Keyword[I].Responses do Readln(F,STmp);
  Until EOF(F);
  Close(F); NumKeys:=I;
end;

Function Response(Key,Index:Byte):String;
Var I,J:Integer;
    STmp:String;
begin
  Assign(F,'CHERI.DAT'); Reset(F);
  For I:=1 to Key do
  begin
    Readln(F,STmp); Readln(F,STmp);
    If (I<>Key) then For J:=1 to Keyword[I].Responses do Readln(F,STmp);
  end;
  For I:=1 to Index do Readln(F,STmp);
  Response:=STmp;
  Close(F);
end;

Procedure StripString;
begin
  While Pos('?',STmp)>0 do STmp[Pos('?',STmp)]:=' ';
  While Pos('.',STmp)>0 do STmp[Pos('.',STmp)]:=' ';
  While Pos(',',STmp)>0 do STmp[Pos(',',STmp)]:=' ';
  While Pos('!',STmp)>0 do STmp[Pos('!',STmp)]:=' ';
  While Pos('(',STmp)>0 do STmp[Pos('(',STmp)]:=' ';
  While Pos(')',STmp)>0 do STmp[Pos(')',STmp)]:=' ';
end;

Procedure Output(STmp:String);
Var I:Integer;
begin
  parameter[1]:=stmp;
  sysmsg(26); {Cheri output lines prefix}
end;

Procedure Parse;
Var I:Integer;
begin
  For I:=1 to NumKeys do
    If Pos(' '+Keyword[I].Word+' ',STmp)>0 then
      begin
        keyword[i].index:=random(keyword[i].responses)+1;
{        Inc(KeyWord[I].Index);
        If (Keyword[I].Index>Keyword[I].Responses) then Keyword[I].Index:=1;}
        Output(Response(I,Keyword[I].Index)); Exit;
      end;
  Inc(Keyword[1].Index);
  If (Keyword[1].Index>Keyword[1].Responses) then Keyword[1].Index:=1;
  Output(Response(1,Keyword[1].Index));
end;

procedure cherichat;
begin
  index:=0;
  donechat:=false; totalresponses:=0;
  ReadData;
  sysmsg(23);
  Repeat
    inc(totalresponses);
    if
      totalresponses>100
    then
      begin
        sysmsg(24);
        donechat:=true;
      end
    else
      begin
        sysmsg(25); {Cheri prompt}
        stmp:=getln(80);
        STmp:=' '+UpcaseStr(STmp)+' ';
        StripString;
        if stmp='  ' then inc(index) else index:=0;
        Parse;
        if index>=10 then begin sysmsg(24); donechat:=true; end;
      end;
  Until (stmp=' QUIT ') or (STmp=' X ') or (stmp=' BYE ') or donechat or lostcarrier or quitnow;
end;

begin
end.
