home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume11 / larn / part03 / scores.c next >
C/C++ Source or Header  |  1991-01-03  |  22KB  |  704 lines

  1. /* scores.c             Larn is copyrighted 1986 by Noah Morgan.
  2.  *
  3.  *    Functions in this file are:
  4.  *
  5.  *    readboard()     Function to read in the scoreboard into a static buffer
  6.  *    writeboard()    Function to write the scoreboard from readboard()'s buffer
  7.  *    makeboard()     Function to create a new scoreboard (wipe out old one)
  8.  *    hashewon()     Function to return 1 if player has won a game before, else 0
  9.  *    long paytaxes(x)     Function to pay taxes if any are due
  10.  *    winshou()        Subroutine to print out the winning scoreboard
  11.  *    shou(x)            Subroutine to print out the non-winners scoreboard
  12.  *    showscores()        Function to show the scoreboard on the terminal
  13.  *    showallscores()    Function to show scores and the iven lists that go with them
  14.  *    sortboard()        Function to sort the scoreboard
  15.  *    newscore(score, whoo, whyded, winner)     Function to add entry to scoreboard
  16.  *    new1sub(score,i,whoo,taxes)           Subroutine to put player into a 
  17.  *    new2sub(score,i,whoo,whyded)           Subroutine to put player into a 
  18.  *    died(x)     Subroutine to record who played larn, and what the score was
  19.  *    diedsub(x) Subroutine to print out a line showing player when he is killed
  20.  *    diedlog()     Subroutine to read a log file and print it out in ascii format
  21.  *    getplid(name)        Function to get players id # from id file
  22.  *
  23.  */
  24. #ifdef VMS
  25. # include <types.h>
  26. # include <stat.h>
  27. #else
  28. # include <sys/types.h>
  29. # ifndef MSDOS
  30. #  include <sys/times.h>
  31. # endif
  32. # include <sys/stat.h>
  33. #endif
  34. #include "header.h"
  35.  
  36. struct scofmt            /*    This is the structure for the scoreboard         */
  37.     {
  38.     long score;            /* the score of the player                             */
  39.     long suid;            /* the user id number of the player                 */
  40.     short what;            /* the number of the monster that killed player     */
  41.     short level;        /* the level player was on when he died             */
  42.     short hardlev;        /* the level of difficulty player played at         */
  43.     short order;        /* the relative ordering place of this entry         */
  44.     char who[40];        /* the name of the character                         */
  45.     char sciv[26][2];    /* this is the inventory list of the character         */
  46.     };
  47. struct wscofmt            /* This is the structure for the winning scoreboard */
  48.     {
  49.     long score;            /* the score of the player                             */
  50.     long timeused;        /* the time used in mobuls to win the game             */
  51.     long taxes;            /* taxes he owes to LRS                             */
  52.     long suid;            /* the user id number of the player                 */
  53.     short hardlev;        /* the level of difficulty player played at         */
  54.     short order;        /* the relative ordering place of this entry         */
  55. # ifndef MAIL            /* dgk */
  56.     char hasmail;        /* 1 if mail is to be read, 0 otherwise */
  57. # endif
  58.     char who[40];        /* the name of the character                         */
  59.     };
  60.  
  61. struct log_fmt            /* 102 bytes struct for the log file                 */
  62.     {
  63.     long score;            /* the players score                                 */
  64.     long diedtime;        /* time when game was over                             */
  65.     short cavelev;        /* level in caves                                     */
  66.     short diff;            /* difficulty player played at                         */
  67. #ifdef EXTRA
  68.     long elapsedtime;    /* real time of game in seconds                     */
  69.     long bytout;        /* bytes input and output                             */
  70.     long bytin;
  71.     long moves;            /* number of moves made by player                     */
  72.     short ac;            /* armor class of player                             */
  73.     short hp,hpmax;        /* players hitpoints                                 */
  74.     short cputime;        /* cpu time needed in seconds                         */
  75.     short killed,spused;/* monsters killed and spells cast                     */
  76.     short usage;        /* usage of the cpu in %                             */
  77.     short lev;            /* player level                                     */
  78. #endif
  79.     char who[12];        /* player name                                         */
  80.     char what[46];        /* what happened to player                             */
  81.     };
  82.  
  83. static struct scofmt sco[SCORESIZE];    /* the structure for the scoreboard  */
  84. static struct wscofmt winr[SCORESIZE];    /* struct for the winning scoreboard */
  85. static struct log_fmt logg;                /* structure for the log file          */
  86. static char *whydead[] = {
  87.     "quit", "suspended", "self - annihilated", "shot by an arrow",
  88.     "hit by a dart", "fell into a pit", "fell into a bottomless pit",
  89.     "a winner", "trapped in solid rock", "killed by a missing save file",
  90.     "killed by an old save file", "caught by the greedy cheater checker trap",
  91.     "killed by a protected save file","killed his family and committed suicide",
  92.     "erased by a wayward finger", "fell through a bottomless trap door",
  93.     "fell through a trap door", "drank some poisonous water",
  94.     "fried by an electric shock", "slipped on a volcano shaft",
  95.     "killed by a stupid act of frustration", "attacked by a revolting demon",
  96.     "hit by his own magic", "demolished by an unseen attacker",
  97.     "fell into the dreadful sleep", "killed by an exploding chest",
  98. /*26*/    "killed by a missing maze data file", "annihilated in a sphere",
  99.     "died a post mortem death","wasted by a malloc() failure"
  100.     };
  101.  
  102.  
  103. /*
  104.  *    readboard()     Function to read in the scoreboard into a static buffer
  105.  *
  106.  *    returns -1 if unable to read in the scoreboard, returns 0 if all is OK
  107.  */
  108. readboard()
  109.     {
  110.     if (lopen(scorefile)<0)
  111.       { lprcat("Can't read scoreboard\n"); lflush(); return(-1); }
  112.     lrfill((char*)sco,sizeof(sco));        lrfill((char*)winr,sizeof(winr));
  113.     lrclose();  lcreat((char*)0);  return(0);
  114.     }
  115.  
  116. /*
  117.  *    writeboard()    Function to write the scoreboard from readboard()'s buffer
  118.  *
  119.  *    returns -1 if unable to write the scoreboard, returns 0 if all is OK
  120.  */
  121. writeboard()
  122.     {
  123.     set_score_output();
  124.     if (lcreat(scorefile)<0)
  125.       { lprcat("Can't write scoreboard\n"); lflush(); return(-1); }
  126.     lwrite((char*)sco,sizeof(sco));        lwrite((char*)winr,sizeof(winr));
  127.     lwclose();  lcreat((char*)0);  return(0);
  128.     }
  129.  
  130. /*
  131.  *    makeboard()         Function to create a new scoreboard (wipe out old one)
  132.  *
  133.  *    returns -1 if unable to write the scoreboard, returns 0 if all is OK
  134.  */
  135. makeboard()
  136.     {
  137.     register int i;
  138.     for (i=0; i<SCORESIZE; i++)
  139.         {
  140.         winr[i].taxes = winr[i].score = sco[i].score = 0;
  141.         winr[i].order = sco[i].order = i;
  142.         }
  143.     if (writeboard()) return(-1);
  144.     chmod(scorefile,0666);
  145.     return(0);
  146.     }
  147.  
  148. /*
  149.  *    hashewon()     Function to return 1 if player has won a game before, else 0
  150.  *
  151.  *    This function also sets c[HARDGAME] to appropriate value -- 0 if not a
  152.  *    winner, otherwise the next level of difficulty listed in the winners
  153.  *    scoreboard.  This function also sets outstanding_taxes to the value in
  154.  *    the winners scoreboard.
  155.  */
  156. hashewon()
  157.     {
  158.     register int i;
  159.     c[HARDGAME] = 0;
  160.     if (readboard() < 0) return(0);    /* can't find scoreboard */
  161.     for (i=0; i<SCORESIZE; i++)    /* search through winners scoreboard */
  162.        if (winr[i].suid == userid)
  163.           if (winr[i].score > 0)
  164.             {
  165.             c[HARDGAME]=winr[i].hardlev+1;  outstanding_taxes=winr[i].taxes;
  166.             return(1);
  167.             }
  168.     return(0);
  169.     }
  170.  
  171. # ifndef MAIL            /* dgk */
  172. checkmail()
  173. {
  174.     register int    i;
  175.     long        gold, taxes;
  176.  
  177.     if (readboard() < 0)
  178.         return;            /* can't find scoreboard */
  179.     for (i = 0; i < SCORESIZE; i++)    /* search through winners scoreboard */
  180.         if (winr[i].suid == userid
  181.         &&  winr[i].score > 0
  182.         &&  winr[i].hasmail) {
  183.             winr[i].hasmail = 0;
  184.             gold = taxes = winr[i].taxes;
  185.             writeboard();
  186.  
  187.             /* Intuit the amount of gold -- should have changed
  188.              * the score file, but ...  TAXRATE is an fraction.
  189.              */
  190.             while ((gold * TAXRATE) < taxes)
  191.                 gold += taxes;
  192.             readmail(gold);
  193.         }
  194. }
  195. # endif
  196.  
  197.  
  198. /*
  199.  *    long paytaxes(x)         Function to pay taxes if any are due
  200.  *
  201.  *    Enter with the amount (in gp) to pay on the taxes.
  202.  *    Returns amount actually paid.
  203.  */
  204. long paytaxes(x)
  205.     long x;
  206.     {
  207.     register int i;
  208.     register long amt;
  209.     if (x<0) return(0L);
  210.     if (readboard()<0) return(0L);
  211.     for (i=0; i<SCORESIZE; i++)
  212.         if (winr[i].suid == userid)    /* look for players winning entry */
  213.             if (winr[i].score>0) /* search for a winning entry for the player */
  214.                 {
  215.                 amt = winr[i].taxes;
  216.                 if (x < amt) amt=x;        /* don't overpay taxes (Ughhhhh) */
  217.                 winr[i].taxes -= amt;
  218.                 outstanding_taxes -= amt;
  219.                 if (writeboard()<0) return(0);
  220.                 return(amt);
  221.                 }
  222.     return(0L);    /* couldn't find user on winning scoreboard */
  223.     }
  224.  
  225. /*
  226.  *    winshou()        Subroutine to print out the winning scoreboard
  227.  *
  228.  *    Returns the number of players on scoreboard that were shown 
  229.  */
  230. winshou()
  231.     {
  232.     register struct wscofmt *p;
  233.     register int i,j,count;
  234.     for (count=j=i=0; i<SCORESIZE; i++) /* is there anyone on the scoreboard? */
  235.         if (winr[i].score != 0)
  236.             { j++; break; }
  237.     if (j)
  238.         {
  239.         lprcat("\n  Score    Difficulty   Time Needed   Larn Winners List\n");
  240.  
  241.         for (i=0; i<SCORESIZE; i++)    /* this loop is needed to print out the */
  242.           for (j=0; j<SCORESIZE; j++) /* winners in order */
  243.             {
  244.             p = &winr[j];    /* pointer to the scoreboard entry */
  245.             if (p->order == i)
  246.                 {
  247.                 if (p->score)
  248.                     {
  249.                     count++;
  250.                     lprintf("%10d     %2d      %5d Mobuls   %s \n",
  251.                     (long)p->score,(long)p->hardlev,(long)p->timeused,p->who);
  252.                     }
  253.                 break;
  254.                 }
  255.             }
  256.         }
  257.     return(count);    /* return number of people on scoreboard */
  258.     }
  259.  
  260. /*
  261.  *    shou(x)            Subroutine to print out the non-winners scoreboard
  262.  *        int x;
  263.  *
  264.  *    Enter with 0 to list the scores, enter with 1 to list inventories too
  265.  *    Returns the number of players on scoreboard that were shown 
  266.  */
  267. shou(x)
  268.     int x;
  269.     {
  270.     register int i,j,n,k;
  271.     int count;
  272.     for (count=j=i=0; i<SCORESIZE; i++)    /* is the scoreboard empty? */
  273.         if (sco[i].score!= 0)
  274.             { j++; break; }
  275.     if (j)
  276.         {
  277.         lprcat("\n   Score   Difficulty   Larn Visitor Log\n");
  278.         for (i=0; i<SCORESIZE; i++) /* be sure to print them out in order */
  279.           for (j=0; j<SCORESIZE; j++)
  280.             if (sco[j].order == i)
  281.                 {
  282.                 if (sco[j].score)
  283.                     {
  284.                     count++;
  285.                     lprintf("%10d     %2d       %s ",
  286.                         (long)sco[j].score,(long)sco[j].hardlev,sco[j].who);
  287.                     if (sco[j].what < 256) lprintf("killed by a %s",monster[sco[j].what].name);
  288.                         else lprintf("%s",whydead[sco[j].what - 256]);
  289.                     if (x != 263) lprintf(" on %s",levelname[sco[j].level]);
  290.                     if (x)
  291.                         {
  292.                         for (n=0; n<26; n++) { iven[n]=sco[j].sciv[n][0]; ivenarg[n]=sco[j].sciv[n][1]; }
  293.                         for (k=1; k<99; k++)
  294.                           for (n=0; n<26; n++)
  295.                             if (k==iven[n])  { srcount=0; show3(n); }
  296.                         lprcat("\n\n");
  297.                         }
  298.                     else lprc('\n');
  299.                     }
  300.                 j=SCORESIZE;
  301.                 }
  302.         }
  303.     return(count);    /* return the number of players just shown */
  304.     }
  305.  
  306. /*
  307.  *    showscores()        Function to show the scoreboard on the terminal
  308.  *
  309.  *    Returns nothing of value
  310.  */
  311. static char esb[] = "The scoreboard is empty.\n";
  312. showscores()
  313.     {
  314.     register int i,j;
  315.     lflush();  lcreat((char*)0);  if (readboard()<0) return;
  316.     i=winshou();    j=shou(0);    
  317.     if (i+j == 0) lprcat(esb); else lprc('\n');
  318.     lflush();
  319.     }
  320.  
  321. /*
  322.  *    showallscores()    Function to show scores and the iven lists that go with them
  323.  *
  324.  *    Returns nothing of value
  325.  */
  326. showallscores()
  327.     {
  328.     register int i,j;
  329.     lflush();  lcreat((char*)0);  if (readboard()<0) return;
  330.     c[WEAR] = c[WIELD] = c[SHIELD] = -1;  /* not wielding or wearing anything */
  331.     for (i=0; i<MAXPOTION; i++) potionname[i][0]=' ';
  332.     for (i=0; i<MAXSCROLL; i++) scrollname[i][0]=' ';
  333.     i=winshou();  j=shou(1);
  334.     if (i+j==0) lprcat(esb); else lprc('\n');
  335.     lflush();
  336.     }
  337.  
  338. /*
  339.  *    sortboard()        Function to sort the scoreboard
  340.  *
  341.  *    Returns 0 if no sorting done, else returns 1
  342.  */
  343. sortboard()
  344.     {
  345.     register int i,j,pos;
  346.     long jdat;
  347.     for (i=0; i<SCORESIZE; i++) sco[i].order = winr[i].order = -1;
  348.     pos=0;  while (pos < SCORESIZE)
  349.         {
  350.         jdat=0;
  351.         for (i=0; i<SCORESIZE; i++)
  352.             if ((sco[i].order < 0) && (sco[i].score >= jdat))
  353.                 { j=i;  jdat=sco[i].score; }
  354.         sco[j].order = pos++;
  355.         }
  356.     pos=0;  while (pos < SCORESIZE)
  357.         {
  358.         jdat=0;
  359.         for (i=0; i<SCORESIZE; i++)
  360.             if ((winr[i].order < 0) && (winr[i].score >= jdat))
  361.                 { j=i;  jdat=winr[i].score; }
  362.         winr[j].order = pos++;
  363.         }
  364.     return(1);
  365.     }
  366.  
  367. /*
  368.  *    newscore(score, whoo, whyded, winner)     Function to add entry to scoreboard
  369.  *        int score, winner, whyded;
  370.  *        char *whoo;
  371.  *
  372.  *    Enter with the total score in gp in score,  players name in whoo,
  373.  *        died() reason # in whyded, and TRUE/FALSE in winner if a winner
  374.  *    ex.        newscore(1000, "player 1", 32, 0);
  375.  */
  376. newscore(score, whoo, whyded, winner)
  377.     long score;
  378.     int winner, whyded;
  379.     char *whoo;
  380.     {
  381.     register int i;
  382.     long taxes;
  383.     if (readboard() < 0) return;     /*    do the scoreboard    */
  384.     /* if a winner then delete all non-winning scores */
  385.     if (cheat) winner=0;    /* if he cheated, don't let him win */
  386.     if (winner)
  387.         {
  388.         for (i=0; i<SCORESIZE; i++) if (sco[i].suid == userid) sco[i].score=0;
  389.         taxes = score*TAXRATE;
  390.         score += 100000*c[HARDGAME];    /* bonus for winning */
  391.     /* if he has a slot on the winning scoreboard update it if greater score */
  392.         for (i=0; i<SCORESIZE; i++) if (winr[i].suid == userid)
  393.                 { new1sub(score,i,whoo,taxes); return; }
  394.     /* he had no entry. look for last entry and see if he has a greater score */
  395.         for (i=0; i<SCORESIZE; i++) if (winr[i].order == SCORESIZE-1)
  396.                 { new1sub(score,i,whoo,taxes); return; }
  397.         }
  398.     else if (!cheat) /* for not winning scoreboard */
  399.         {
  400.     /* if he has a slot on the scoreboard update it if greater score */
  401.         for (i=0; i<SCORESIZE; i++) if (sco[i].suid == userid)
  402.                 { new2sub(score,i,whoo,whyded); return; }
  403.     /* he had no entry. look for last entry and see if he has a greater score */
  404.         for (i=0; i<SCORESIZE; i++) if (sco[i].order == SCORESIZE-1)
  405.                 { new2sub(score,i,whoo,whyded); return; }
  406.         }
  407.     }
  408.  
  409. /*
  410.  *    new1sub(score,i,whoo,taxes)       Subroutine to put player into a 
  411.  *        int score,i,whyded,taxes;          winning scoreboard entry if his score
  412.  *        char *whoo;                       is high enough
  413.  *
  414.  *    Enter with the total score in gp in score,  players name in whoo,
  415.  *        died() reason # in whyded, and TRUE/FALSE in winner if a winner
  416.  *        slot in scoreboard in i, and the tax bill in taxes.
  417.  *    Returns nothing of value
  418.  */
  419. new1sub(score,i,whoo,taxes)
  420.     long score,taxes;
  421.     int i;
  422.     char *whoo;
  423.     {
  424.     register struct wscofmt *p;
  425.     p = &winr[i];
  426.     p->taxes += taxes;
  427.     if ((score >= p->score) || (c[HARDGAME] > p->hardlev))
  428.         {
  429.         strcpy(p->who,whoo);          p->score=score;
  430.         p->hardlev=c[HARDGAME];        p->suid=userid;
  431.         p->timeused=gtime/100;
  432. # ifndef MAIL            /* dgk */
  433.         p->hasmail = 1;
  434. # endif
  435.         }
  436.     }
  437.  
  438. /*
  439.  *    new2sub(score,i,whoo,whyded)           Subroutine to put player into a 
  440.  *        int score,i,whyded,taxes;          non-winning scoreboard entry if his
  441.  *        char *whoo;                       score is high enough
  442.  *
  443.  *    Enter with the total score in gp in score,  players name in whoo,
  444.  *        died() reason # in whyded, and slot in scoreboard in i.
  445.  *    Returns nothing of value
  446.  */
  447. new2sub(score,i,whoo,whyded)
  448.     long score;
  449.     int i,whyded;
  450.     char *whoo;
  451.     {
  452.     register int j;
  453.     register struct scofmt *p;
  454.     p = &sco[i];
  455.     if ((score >= p->score) || (c[HARDGAME] > p->hardlev))
  456.         {
  457.         strcpy(p->who,whoo);  p->score=score;
  458.         p->what=whyded;       p->hardlev=c[HARDGAME];
  459.         p->suid=userid;          p->level=level;
  460.         for (j=0; j<26; j++)
  461.             { p->sciv[j][0]=iven[j]; p->sciv[j][1]=ivenarg[j]; }
  462.         }
  463.     }
  464.  
  465. /*
  466.  *    died(x)     Subroutine to record who played larn, and what the score was
  467.  *        int x;
  468.  *
  469.  *    if x < 0 then don't show scores
  470.  *    died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
  471.  *
  472.  *        < 256    killed by the monster number
  473.  *        256        quit
  474.  *        257        suspended
  475.  *        258        self - annihilated
  476.  *        259        shot by an arrow
  477.  *        260        hit by a dart
  478.  *        261        fell into a pit
  479.  *        262        fell into a bottomless pit
  480.  *        263        a winner
  481.  *        264        trapped in solid rock
  482.  *        265        killed by a missing save file
  483.  *        266        killed by an old save file
  484.  *        267        caught by the greedy cheater checker trap
  485.  *        268        killed by a protected save file
  486.  *        269        killed his family and killed himself
  487.  *        270        erased by a wayward finger
  488.  *        271        fell through a bottomless trap door
  489.  *        272        fell through a trap door
  490.  *        273        drank some poisonous water
  491.  *        274        fried by an electric shock
  492.  *        275        slipped on a volcano shaft
  493.  *        276        killed by a stupid act of frustration
  494.  *        277        attacked by a revolting demon
  495.  *        278        hit by his own magic
  496.  *        279        demolished by an unseen attacker
  497.  *        280        fell into the dreadful sleep
  498.  *        281        killed by an exploding chest
  499.  *        282        killed by a missing maze data file
  500.  *        283        killed by a sphere of annihilation
  501.  *        284        died a post mortem death
  502.  *        285        malloc() failure
  503.  *        300        quick quit -- don't put on scoreboard
  504.  */
  505.  
  506. static int scorerror;
  507. died(x)
  508.     int x;
  509.     {
  510.     register int f,win;
  511.     char ch,*mod;
  512.     long zzz,i;
  513. # ifdef EXTRA
  514.     struct tms cputime;
  515. # endif
  516.     if (c[LIFEPROT]>0) /* if life protection */
  517.         {
  518.         switch((x>0) ? x : -x)
  519.             {
  520.             case 256: case 257: case 262: case 263: case 265: case 266:
  521.             case 267: case 268: case 269: case 271: case 282: case 284:
  522.             case 285: case 300:  goto invalid; /* can't be saved */
  523.             };
  524.         --c[LIFEPROT]; c[HP]=1; --c[CONSTITUTION];
  525.         cursors(); lprcat("\nYou feel wiiieeeeerrrrrd all over! "); beep();
  526.         lflush();  sleep(4);
  527.         return; /* only case where died() returns */
  528.         }
  529. invalid:
  530.     clearvt100();  lflush();  f=0;
  531.     if (ckpflag) unlink(ckpfile);    /* remove checkpoint file if used */
  532. # ifdef MSDOS
  533.     if (swapfd) {
  534.         close(swapfd);
  535.         (void) unlink(swapfile);/* Remove swapfile */
  536.     }
  537.     unsetraw();
  538. # endif
  539.     if (x<0) { f++; x = -x; }    /* if we are not to display the scores */
  540.     if ((x == 300) || (x == 257))  exit();    /* for quick exit or saved game */
  541.     if (x == 263)  win = 1;  else  win = 0;
  542.     c[GOLD] += c[BANKACCOUNT];   c[BANKACCOUNT] = 0;
  543.         /*    now enter the player at the end of the scoreboard */
  544.     newscore(c[GOLD], logname, x, win);
  545.     diedsub(x);    /* print out the score line */  lflush();
  546.  
  547.     set_score_output();
  548.     if ((wizard == 0) && (c[GOLD] > 0))     /*    wizards can't score        */
  549.         {
  550.         if (lappend(logfile)<0)  /* append to file */
  551.             {
  552.             if (lcreat(logfile)<0) /* and can't create new log file */
  553.                 {
  554.                 lcreat((char*)0);
  555.                 lprcat("\nCan't open record file:  I can't post your score.\n");
  556.                 sncbr();  resetscroll();  lflush();  exit();
  557.                 }
  558.             chmod(logfile,0666);
  559.             }
  560.         strcpy(logg.who,loginname);
  561.         logg.score = c[GOLD];        logg.diff = c[HARDGAME];
  562.         if (x < 256)
  563.             {
  564.             ch = *monster[x].name;
  565.             if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
  566.                 mod="an";  else mod="a";
  567.             sprintf(logg.what,"killed by %s %s",mod,monster[x].name);
  568.             }
  569.         else sprintf(logg.what,"%s",whydead[x - 256]);
  570.         logg.cavelev=level;
  571.         time(&zzz);      /* get cpu time -- write out score info */
  572.         logg.diedtime=zzz;
  573. #ifdef EXTRA
  574.         times(&cputime);  /* get cpu time -- write out score info */
  575.         logg.cputime = i = (cputime.tms_utime + cputime.tms_stime)/60 + c[CPUTIME];
  576.         logg.lev=c[LEVEL];            logg.ac=c[AC];
  577.         logg.hpmax=c[HPMAX];        logg.hp=c[HP];
  578.         logg.elapsedtime=(zzz-initialtime+59)/60;
  579.         logg.usage=(10000*i)/(zzz-initialtime);
  580.         logg.bytin=c[BYTESIN];        logg.bytout=c[BYTESOUT];
  581.         logg.moves=c[MOVESMADE];    logg.spused=c[SPELLSCAST];
  582.         logg.killed=c[MONSTKILLED];
  583. #endif
  584.         lwrite((char*)&logg,sizeof(struct log_fmt));     lwclose();
  585.  
  586. /*    now for the scoreboard maintenance -- not for a suspended game     */
  587.         if (x != 257)
  588.             {
  589.             if (sortboard())  scorerror = writeboard();
  590.             }
  591.         }
  592.     if ((x==256) || (x==257) || (f != 0)) exit();
  593.     if (scorerror == 0) showscores();    /* if we updated the scoreboard */
  594. # ifdef MAIL
  595.     if (x == 263) mailbill();
  596. # endif
  597.     exit();
  598.     }
  599.  
  600. /*
  601.  *    diedsub(x) Subroutine to print out the line showing the player when he is killed
  602.  *        int x;
  603.  */
  604. diedsub(x)
  605. int x;
  606.     {
  607.     register char ch,*mod;
  608.     lprintf("Score: %d, Diff: %d,  %s ",(long)c[GOLD],(long)c[HARDGAME],logname);
  609.     if (x < 256)
  610.         {
  611.         ch = *monster[x].name;
  612.         if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
  613.             mod="an";  else mod="a";
  614.         lprintf("killed by %s %s",mod,monster[x].name);
  615.         }
  616.     else lprintf("%s",whydead[x - 256]);
  617.     if (x != 263) lprintf(" on %s\n",levelname[level]);  else lprc('\n');
  618.     }
  619.  
  620. /*
  621.  *    diedlog()     Subroutine to read a log file and print it out in ascii format
  622.  */
  623. diedlog()
  624.     {
  625.     register int n;
  626.     register char *p;
  627.     struct stat stbuf;
  628.     lcreat((char*)0);
  629.     if (lopen(logfile)<0)
  630.         {
  631.         lprintf("Can't locate log file <%s>\n",logfile);
  632.         return;
  633.         }
  634.     if (fstat(fd,&stbuf) < 0)
  635.         {
  636.         lprintf("Can't  stat log file <%s>\n",logfile);
  637.         return;
  638.         }
  639.     for (n=stbuf.st_size/sizeof(struct log_fmt); n>0; --n)
  640.         {
  641.         lrfill((char*)&logg,sizeof(struct log_fmt));
  642.         p = ctime(&logg.diedtime); p[16]='\n'; p[17]=0;
  643.         lprintf("Score: %d, Diff: %d,  %s %s on %d at %s",(long)(logg.score),(long)(logg.diff),logg.who,logg.what,(long)(logg.cavelev),p+4);
  644. #ifdef EXTRA
  645.         if (logg.moves<=0) logg.moves=1;
  646.         lprintf("  Experience Level: %d,  AC: %d,  HP: %d/%d,  Elapsed Time: %d minutes\n",(long)(logg.lev),(long)(logg.ac),(long)(logg.hp),(long)(logg.hpmax),(long)(logg.elapsedtime));
  647.         lprintf("  CPU time used: %d seconds,  Machine usage: %d.%02d%%\n",(long)(logg.cputime),(long)(logg.usage/100),(long)(logg.usage%100));
  648.         lprintf("  BYTES in: %d, out: %d, moves: %d, deaths: %d, spells cast: %d\n",(long)(logg.bytin),(long)(logg.bytout),(long)(logg.moves),(long)(logg.killed),(long)(logg.spused));
  649.         lprintf("  out bytes per move: %d,  time per move: %d ms\n",(long)(logg.bytout/logg.moves),(long)((logg.cputime*1000)/logg.moves));
  650. #endif
  651.         }
  652.         lflush();  lrclose();  return;
  653.     }
  654.  
  655. #ifndef UIDSCORE
  656. /*
  657.  *    getplid(name)        Function to get players id # from id file
  658.  *
  659.  *    Enter with the name of the players character in name.
  660.  *    Returns the id # of the players character, or -1 if failure.
  661.  *    This routine will try to find the name in the id file, if its not there,
  662.  *    it will try to make a new entry in the file.  Only returns -1 if can't
  663.  *    find him in the file, and can't make a new entry in the file.
  664.  *    Format of playerids file:
  665.  *            Id # in ascii     \n     character name     \n   
  666.  */
  667. static int havepid= -1;    /* playerid # if previously done */
  668. getplid(nam)
  669.     char *nam;
  670.     {
  671.     int fd7,high=999,no;
  672.     register char *p,*p2;
  673.     char name[80];
  674.     if (havepid != -1) return(havepid);    /* already did it */
  675.     lflush();    /* flush any pending I/O */
  676.     sprintf(name,"%s\n",nam);    /* append a \n to name */
  677.     if (lopen(playerids) < 0)    /* no file, make it */
  678.         {
  679.         if ((fd7=creat(playerids,0666)) < 0)  return(-1); /* can't make it */
  680.         close(fd7);  goto addone;    /* now append new playerid record to file */
  681.         }
  682.     for (;;)    /* now search for the name in the player id file */
  683.         {
  684.         p = lgetl();  if (p==NULL) break;    /* EOF? */
  685.         no = atoi(p);    /* the id # */
  686.         p2= lgetl();  if (p2==NULL) break;    /* EOF? */
  687.         if (no>high) high=no;    /* accumulate highest id # */
  688.         if (strcmp(p2,name)==0)    /* we found him */
  689.             {
  690.             return(no);    /* his id number */
  691.             }
  692.         }
  693.     lrclose();
  694.     /* if we get here, we didn't find him in the file -- put him there */
  695. addone:
  696.     if (lappend(playerids) < 0) return(-1);    /* can't open file for append */
  697.     lprintf("%d\n%s",(long)++high,name);  /* new id # and name */
  698.     lwclose();
  699.     lcreat((char*)0);    /* re-open terminal channel */
  700.     return(high);
  701.     }
  702. #endif UIDSCORE
  703.  
  704.