unit start_up;
{$O+,F+}
{$N+,E+}

interface

Uses overlay, Dos, CRT, rninit, bbskv, bbskern, engine2, rgoods, usered,
     robot, bldmaze, ranchlin, iconfig, trade, cheri2, rkey, message1,
     toyinfo, winner, miscplay, miscpl2, miscpl3, miscpl4, miscpl5,
     readonce,protcomm,sys_msg,nanocore;

procedure startup;
procedure grundir; {Gets run dir from paramstr(0)}

implementation

procedure grundir; {Gets run dir from paramstr(0)}
var p:dos.pathstr;
    d:dos.dirstr;
    n:dos.namestr;
    e:dos.extstr;
begin
  p:=paramstr(0);
  fsplit(p,d,n,e);
  if d='' then begin p:=xpath('TOMMCHAT.EXE'); fsplit(p,d,n,e); end;
  rundir:=fexpand(addslash(d));
  textattr:=lightblue;
  writeln(' þþþ Executing from: ',rundir);
end;

procedure setkeylocs;
var i,j:integer;
begin
  fillchar(keyloc,sizeof(keyloc),#00);
  for j:=1 to 4 do keyloc[j]:=randzap;
end;

procedure grabbastic(f:string);
begin
  doortype:=dorinfo;
  tstr:=f;
  tstr:=copy(tstr,3,length(tstr)-2);
  dinode:=str2int(tstr);
end;

function pack2sec70(l:longint):longint;
var cfd,cft:longint; dt:datetime;
    hr,mi,se:longint;
begin
  unpacktime(l,dt);
  hr:=dt.hour; mi:=dt.min; se:=dt.sec;
  cft:=longint(3600*hr);
  cft:=cft+longint(60*mi);
  cft:=cft+se;
  cfd:=d70(dt.year,dt.month,dt.day);
  cfd:=longint(cfd*86400);
  cfd:=longint(cfd+cft);
  pack2sec70:=cfd;
end;

procedure cnuke(s:string);
begin
  wln('`CNuking stale IPC `K'+s);
  nuke(s);
end;

procedure kill_old_ipcs; {Kills .CNT and .SAY older than 10 minutes and .ON older than 1 hour}
var dirinfo:searchrec;
    j,k,l:longint;
begin
  j:=sec70;
  findfirst('*.SAY',archive,dirinfo);
  while doserror=0 do
    begin
      l:=pack2sec70(dirinfo.time);
      if ((j-l)>600) then cnuke(dirinfo.name);
      findnext(dirinfo);
    end;
  findfirst('*.CNT',archive,dirinfo);
  while doserror=0 do
    begin
      l:=pack2sec70(dirinfo.time);
      if ((j-l)>600) then cnuke(dirinfo.name);
      findnext(dirinfo);
    end;
  findfirst('*.ON',archive,dirinfo);
  while doserror=0 do
    begin
      l:=pack2sec70(dirinfo.time);
      if ((j-l)>3600) then cnuke(dirinfo.name);
      findnext(dirinfo);
    end;
end;

procedure startup;
var y:string[84];
begin

  grundir;

  doortype:=dorinfo;
  dinode:=1;

  config.maxplays:=5;
  config.maxmoves:=60;
  config.maxschool:=2;
  config.maxjoust:=3;
  config.timeout:=4;
  config.winlevel:=100;
  config.maxdiff:=4;
  config.localcolour:=true;
  config.startroom:=1;
  config.robotpaypercent:=100;
  config.robotxppercent:=100;
  config.bumppay:=200;
  config.loserxp:=5;
  config.minushp:=100;
  config.escape:=20;
  config.specialtoy:=400;
  config.specialdefense:=300;
  config.robotreroll:=6;
  config.corner:=30;
  config.vkrmove:=false;
  config.jacuzziluck:=35;
  config.maxjacuzzi:=4;
  config.dvblog:=false;

  timesjacuzzi:=0;

  doquikemove:=false;
  spymode:=false;

  invkr:=false;
  disease:=false;

  for index:=1 to 16 do spellpriv[index]:=false;

  usefossil:=true; canusefossil:=true;

  buildconfig;

  dvblog:=config.dvblog;
  beeninvkr:=false;
  thisusernumber:=1;
  rank10:=false;
  movesleft:=config.maxmoves;
  user.playstoday:=0;
  joustsleft:=config.maxjoust;
  school:=0;
  send2robot:=false;
  nocr:=false;
  ansi:=true;
  localansi:=config.localcolour;
  usesuperbar:=true;
  fillchar(user,sizeof(user),#0);
  fillchar(tmpuser,sizeof(tmpuser),#0);
  fillchar(user2,sizeof(user2),#0);
  bbsname:='the BBS';
  fillchar(temproom,sizeof(temproom),#00);
  new(room);
  for index:=1 to 800 do room^[index]:=temproom;
  fillchar(tempevent,sizeof(tempevent),#00);
  randomize;
  username:='';
  debugmode:=false; lockbaud:=false; quitnow:=false;
  kill_old_ipcs;
  assign (zf,''); rewrite(zf);

{  fillchar(usermode,sizeof(usermode),#00); }


  for index:=1 to paramcount do
  begin
    tstr:=paramstr(index);
    ctmp:=upcase(tstr[2]); y:=upcasestr(copy(tstr,3,128));
    case ctmp of
      'L' : begin
              lockbaud:=true;
              if str2int(y)<>0 then config.lockbaud:=str2int(y);
            end;
      'X' : begin
              canusefossil:=false;
              usefossil:=false;
            end;
      'D' : begin
              dipath:=addslash(y);
            end;
      'A' : doquikemove:=true;
      'B' : doortype:=doorsys;
      'C' : doortype:=pcbsys;
      'E' : begin editmode:=true; edituser(1); shutdown; end;
      'F' : grabbastic(tstr);
      'I' : doortype:=callinfo;
      'M' : begin makemaze; halt; end;
      'P' : payrobots;
      'R' : recover;
      '*' : begin purgeusers; halt; end;
    end;
  end;

  lastusedfilename:=mkrname;
  if
    exist(lastusedfilename)
  then
    begin
      assign(lastusedfile,lastusedfilename);
      reset(lastusedfile);
      read(lastusedfile,lastused);
      close(lastusedfile);
      rewrite(lastusedfile);
      ltmp:=ds1970;
      write(lastusedfile,ltmp);
      close(lastusedfile);
    end
  else
    begin
      lastused:=0;
      assign(lastusedfile,lastusedfilename);
      rewrite(lastusedfile);
      ltmp:=ds1970;
      write(lastusedfile,ltmp);
      close(lastusedfile);
    end;

  clrscr;
  quitnow := False;
  if usesuperbar then Window(1,5,80,24) else window(1,1,80,24);
  textattr:=lightcyan;
  writeln('Rancho Nevada '+version+' (C) Copyright 1993-1998 Whirlwind Software');
  writeln('Written by Gerald T. Albion');
  writeln;
  if quitnow then exit;

  {Startup stuff from TommCHAT here}

  consoleonly:=true; ignorecarrier:=true; localmode:=true; comport:=0;

  nodoorinfo:=false;
  case doortype of
    dorinfo  : readdorinfo;
    doorsys  : readdoorsys;
    pcbsys   : readpcbsys;
    callinfo : readcallinfo;
  end;

  textattr:=lightblue;
  writeln('Comport:',comport);
  writeln('Localmode:',localmode);
  writeln('Ignorecarrier:',ignorecarrier);
  writeln('Consoleonly:',consoleonly);
  if
    (comport=0) or (localmode) or (ignorecarrier) or (consoleonly)
  then
    writeln(crlf+' þþþ Using local console only ')
  else
    begin
      if
        usefossil
      then
        writeln(' þþþ Using FOSSIL Driver on COM',comport,':')
      else
        begin
          writeln(' þþþ Using Internal Comm Driver');
          writeln(' þþþ Parameters: COM',comport,':',
                  '  Port ',hexstr(protcomm.porttable[comport-1].base),
                  '  IRQ ',protcomm.porttable[comport-1].irq);
        end;
      writeln(' þþþ Locked Baud: ',config.lockbaud,' bps');
      writeln(' þþþ Actual Baud: ',baudrate,' bps');
    end;
  protcomm.usedport:=pred(comport);
  comm_cts_rts(true);
{
  if
    comport<>0
  then
    begin
      if
        config.lockbaud=0
      then
        begin
          write(' þþþ FloatInit  : ');
          if comm_init(baudrate,comport) then writeln('OK') else writeln('FAILED!');
          comm_cts_rts(true);
        end
      else
        begin
          write(' þþþ LockInit   : ');
          if comm_init(config.lockbaud,comport) then writeln('OK') else writeln('FAILED!');
          comm_cts_rts(true);
        end;
    end;
  anykey;
}
  if nodoorinfo then begin ignorecarrier:=true; consoleonly:=true; ansi:=true; end;

  registree:=checkkey;

  if
    nodoorinfo
  then
    begin
      quitnow:=false;
      ignorecarrier:=true; localmode:=true;
      consoleonly:=true; ansi:=true;
      sysmsg(30);
      temps:=getln(36); username:=mixedcase(temps);
      if
        length(temps)<1
      then
        begin
          exitproc:=exitsave;
          window(1,1,80,25);
          clrscr;
          halt;
        end;
    end
  else
    writeln(' þþþ Successfully loaded door info file');

  case config.ansimode of
    1: begin
         ansi:=false;
         sysmsg(31);
         yndefault:=true;
         ctmp:=upcase(yn);
         ansi:=ctmp='Y';
       end;
    2: ansi:=ansidetect;
  end;

  User.BBSName := username;
  User.lastplayed := 0;
  user.level:=1;
  if not(lastused=user.lastplayed) then user.playstoday:=0;

  ltmp:=lastused;
  if
    not(ltmp=(ds1970))
  then
    begin
      moverobots;
      recover;
      zeromoves;
    end;

  titlebar;
  clrscr;

  if
    exist('RANCHO.TOP')
  then
    begin
{$I-}
      assign(tf,'RANCHO.TOP');
      reset(tf);
      readln(tf,champion);
      close(tf);
{$I+}
      champion:=upcasestr(champion);
    end
  else
    champion:='Nobody';
  sendln('');
  if
    ansi
  then
    RandSendText('WELCOME*.ANS')
  else
    Randsendtext('Welcome*.Asc');
  sendln('`L');
  anykey;
  btmp:=false;

  repeat
    btmp:=finduser(user.bbsname);
    If (btmp = False) then NewUser;
  until quitnow or btmp or lostcarrier;
  if lostcarrier then begin nocarrier; quitnow:=true; exit; end;

  readuser(u);
  user:=tmpuser;
  thisusernumber:=u+1;
  useronname:='USER-'+int2str(thisusernumber)+'.ON';
  noecho:=false;
(*  user.lastplayed:=ds1970; {Set lastplayed to now}*)
  if
    (user.playstoday=0)
  then
    begin
      user.hp:=user.maxhp;    {Reset hit points}
      user.coord:=user.maxcoord;
    end;
  user.tori:=false;
  if not(lastused=user.lastplayed) then user.playstoday:=0;

  if
    exist(mazname)
  then
    begin
      assign(mazefile,mazname);
      reset(mazefile);
      index:=0;
      repeat
        inc(index);
        read(mazefile,temproom);
        room^[index]:=temproom;
      until eof(mazefile) or (index=800);
      close(mazefile);
    end
  else
    begin
      makemaze;
      if
        not(exist(mazname))
      then
        begin
          sysmsg(32);
          delay(5000);
          halt;
        end
      else
        begin
          assign(mazefile,mazname);
          reset(mazefile);
          index:=0;
          repeat
            inc(index);
            read(mazefile,temproom);
            room^[index]:=temproom;
          until eof(mazefile) or (index=800);
          close(mazefile);
        end
    end;


  setkeylocs;


  if
    doquikemove
  then
    quikemove;
(*    begin
      ltmp:=ds1970;
      if
        ltmp=lastused
      then
        quikemove; {Quik-E-Move robots, no pay}
    end;
*)
  writeuser(user.name,user);
  writeuserlog;

  datein:=ds1970;  {Date entered so we can check for call straddle}

end;

begin end.

