home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / mor4873s.lzh / DEATH.C < prev    next >
C/C++ Source or Header  |  1988-11-01  |  12KB  |  442 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <ctype.h>
  4. #include <sys/types.h>
  5. #ifdef MSDOS
  6. #include <io.h>
  7. #else
  8. #include <sys/file.h>
  9. #endif
  10.  
  11. #include "constant.h"
  12. #include "config.h"
  13. #include "types.h"
  14. #include "externs.h"
  15.  
  16. #ifdef USG
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #else
  20. #include <strings.h>
  21. #endif
  22.  
  23. #define MIN(a, b)    ((a < b) ? a : b)
  24.  
  25. #ifdef sun   /* correct SUN stupidity in the stdio.h file */
  26. char *sprintf();
  27. #endif
  28.  
  29. #if defined(ultrix) || defined(sun) || defined(USG)
  30. int getuid();
  31. #else
  32. uid_t getuid();
  33. #endif
  34.  
  35. #if defined(sun) || defined(USG) || defined(ultrix)
  36. long lseek();
  37. #else
  38. off_t lseek();
  39. #endif
  40. #ifdef USG
  41. #define L_SET 0
  42. #endif
  43.  
  44. #if defined(ultrix) || defined(USG)
  45. void perror();
  46. void exit();
  47. #endif
  48.  
  49. char *getlogin();
  50. long time();
  51.  
  52. date(day)
  53. char *day;
  54. {
  55.   register char *tmp;
  56.   long clock;
  57.  
  58.   clock = time((long *) 0);
  59.   tmp = ctime(&clock);
  60.   tmp[11] = '\0';
  61.   (void) strcpy(day, tmp);
  62. }
  63.  
  64. /* Centers a string within a 31 character string        -JWT-     */
  65. char *fill_str(p1)
  66. char *p1;
  67. {
  68.   vtype s1, s2;
  69.   int i;
  70.  
  71.   s1[0] = '\0';
  72.   s2[0] = '\0';
  73.   i = (strlen(p1) / 2);
  74.   (void) strcpy(s1, pad(s2, " ", 15 - i));
  75.   (void) strcat(s1, pad(p1, " ", 31));
  76.   s1[31] = '\0';
  77.   return (s1);
  78. }
  79.  
  80.  
  81. /* Prints a line to the screen efficiently        -RAK-     */
  82. dprint(str, row)
  83. char *str;
  84. int row;
  85. {
  86.   register int i, j, nblanks, xpos;
  87.   vtype prt_str;
  88.   char tmp_str[2];
  89.  
  90.   tmp_str[1] = '\0';    /* yes, this is supposed to be a one */
  91.   prt_str[0] = '\0';
  92.   nblanks = 0;
  93.   xpos = 0;
  94.   for (i = 0; i < strlen(str); i++)
  95.     {
  96.       if (str[i] == ' ')
  97.     {
  98.       if (xpos >= 0)
  99.         {
  100.           nblanks++;
  101.           if (nblanks > 5)
  102.         {
  103.           nblanks = 0;
  104.           put_buffer(prt_str, row, xpos);
  105.           prt_str[0] = '\0';
  106.           xpos = -1;
  107.         }
  108.         }
  109.     }
  110.       else
  111.     {
  112.       if (xpos == -1)
  113.         xpos = i;
  114.       if (nblanks > 0)
  115.         {
  116.           for (j = 0; j < nblanks; j++)
  117.         (void) strcat(prt_str, " ");
  118.           nblanks = 0;
  119.         }
  120.       tmp_str[0] = str[i];
  121.       (void) strcat(prt_str, tmp_str);
  122.     }
  123.     }
  124.   if (xpos >= 0)
  125.     put_buffer(prt_str, row, xpos);
  126. }
  127.  
  128. display_scores()
  129. {
  130.   register int i = 0, j;
  131.   int fd;
  132.   high_scores score;
  133.   char list[20][256];
  134.  
  135.   if (1 > (fd = open(MORIA_TOP, O_RDONLY, 0644)))
  136.     {
  137.       prt("Error opening top twenty file\n", 0, 0);
  138.       return ;
  139.     }
  140. #ifdef MSDOS
  141.   (void) setmode(fd, O_BINARY);
  142. #endif
  143.  
  144.   while (0 < read(fd, (char *)&score, sizeof(high_scores)))
  145.     {
  146.       (void) sprintf(list[i], "%-7ld%-15.15s%-10.10s%-10.10s%-5d%-25.25s%5d",
  147.             score.points, score.name,
  148.             race[score.prace].trace, class[score.pclass].title,
  149.             (int)score.lev, score.died_from, score.dun_level);
  150.       i++;
  151.     }
  152.  
  153.   controlz();
  154.   put_buffer("Points Name           Race      Class     Lv   Killed By                Dun Lv", 0, 0);
  155.   for (j = 0; j < i; j++)
  156.     put_buffer(list[j], j + 1, 0);
  157.   pause_line(23);
  158. }
  159.  
  160. /* Prints the gravestone of the character        -RAK-     */
  161. print_tomb()
  162. {
  163.   vtype str1, str2, str3, str4, str5, str6, str7, str8;
  164.   vtype dstr[20];
  165.   vtype fnam;
  166.   char command;
  167.   FILE *f1;
  168.   register int i;
  169.   char day[11];
  170.   int flag;
  171.   char tmp_str[80];
  172.  
  173.   date(day);
  174.   (void) strcpy(str1, fill_str(py.misc.name));
  175.   (void) strcpy(str2, fill_str(py.misc.title));
  176.   (void) strcpy(str3, fill_str(py.misc.tclass));
  177.   (void) sprintf(str4, "Level : %d", (int)py.misc.lev);
  178.   (void) strcpy(str4, fill_str(str4));
  179.   (void) sprintf(str5, "%ld Exp", py.misc.exp);
  180.   (void) strcpy(str5, fill_str(str5));
  181.   (void) sprintf(str6, "%ld Au", py.misc.au);
  182.   (void) strcpy(str6, fill_str(str6));
  183.   (void) sprintf(str7, "Died on Level : %d", dun_level);
  184.   (void) strcpy(str7, fill_str(str7));
  185.   (void) strcpy(str8, fill_str(died_from));
  186.   dstr[0][0] = '\0';
  187.   (void) strcpy(dstr[1], "               _______________________");
  188.   (void) strcpy(dstr[2], "              /                       \\         ___");
  189.   (void) strcpy(dstr[3],
  190.     "             /                         \\ ___   /   \\      ___");
  191.   (void) strcpy(dstr[4],
  192.     "            /            RIP            \\   \\  :   :     /   \\");
  193.   (void) strcpy(dstr[5],
  194.     "           /                             \\  : _;,,,;_    :   :");
  195.   (void) sprintf(dstr[6], "          /%s\\,;_          _;,,,;_", str1);
  196.   (void) strcpy(dstr[7], "         |               the               |   ___");
  197.   (void) sprintf(dstr[8], "         | %s |  /   \\", str2);
  198.   (void) strcpy(dstr[9], "         |                                 |  :   :");
  199.   (void) sprintf(dstr[10], "         | %s | _;,,,;_   ____", str3);
  200.   (void) sprintf(dstr[11], "         | %s |          /    \\", str4);
  201.   (void) sprintf(dstr[12], "         | %s |          :    :", str5);
  202.   (void) sprintf(dstr[13], "         | %s |          :    :", str6);
  203.   (void) sprintf(dstr[14], "         | %s |         _;,,,,;_", str7);
  204.   (void) strcpy(dstr[15], "         |            killed by            |");
  205.   (void) sprintf(dstr[16], "         | %s |", str8);
  206.   (void) sprintf(dstr[17], "         |           %s           |", day);
  207.   (void) strcpy(dstr[18], "        *|   *     *     *    *   *     *  | *");
  208.   (void) strcpy(dstr[19],
  209.           "________)/\\\\_)_/___(\\/___(//_\\)/_\\//__\\\\(/_|_)_______");
  210.   clear_screen(0, 0);
  211.   for (i = 0; i <= 19; i++)
  212.     dprint(dstr[i], i);
  213.   flush();
  214.   if (get_com("Print to file? (Y/N)", &command))
  215.     switch (command)
  216.       {
  217.       case 'y':
  218.       case 'Y':
  219.     prt("Enter Filename:", 0, 0);
  220.     flag = FALSE;
  221.     do
  222.       {
  223.         if (get_string(fnam, 0, 16, 60))
  224.           {
  225.         if (strlen(fnam) == 0)
  226.           (void) strcpy(fnam, "MORIACHR.DIE");
  227.         f1 = fopen(fnam, "w");
  228.         if (f1 == NULL)
  229.           {
  230.             (void) sprintf(tmp_str, "Error creating> %s", fnam);
  231.             prt(tmp_str, 1, 0);
  232.           }
  233.         else
  234.           {
  235.             flag = TRUE;
  236.             for (i = 0; i <= 19; i++)
  237.               (void) fprintf(f1, "%s\n", dstr[i]);
  238.           }
  239.         (void) fclose(f1);
  240.           }
  241.         else
  242.           flag = TRUE;
  243.       }
  244.     while (!flag);
  245.     break;
  246.       default:
  247.     break;
  248.       }
  249. }
  250.  
  251.  
  252. /* Calculates the total number of points earned        -JWT-     */
  253. long total_points()
  254. {
  255.   return (py.misc.max_exp + (100 * py.misc.max_lev));
  256. }
  257.  
  258.  
  259. /* Enters a players name on the top twenty list        -JWT-     */
  260. top_twenty()
  261. {
  262.   register int i, j, k;
  263.   high_scores scores[20], myscore;
  264.   char *tmp;
  265.  
  266.   clear_screen(0, 0);
  267.  
  268.   if (wizard1)
  269.     exit_game();
  270.  
  271.   if (panic_save == 1)
  272.     {
  273.       msg_print("Sorry, scores for games restored from panic save files are not saved.");
  274.       /* make sure player sees message before display_scores erases it */
  275.       msg_print (" ");
  276.       display_scores ();
  277.       exit_game();
  278.     }
  279.  
  280.   myscore.points = total_points();
  281.   myscore.dun_level = dun_level;
  282.   myscore.lev = py.misc.lev;
  283.   myscore.max_lev = py.misc.max_lev;
  284.   myscore.mhp = py.misc.mhp;
  285.   myscore.chp = py.misc.chp;
  286.   myscore.uid = getuid();
  287.   /* First character of sex, lower case */
  288.   myscore.sex = tolower(py.misc.sex[0]);
  289.   myscore.prace = py.misc.prace;
  290.   myscore.pclass = py.misc.pclass;
  291.   (void) strcpy(myscore.name, py.misc.name);
  292.   tmp = died_from;
  293.   if ('a' == *tmp)
  294.     {
  295.       if ('n' == *(++tmp))
  296.     {
  297.       tmp++;
  298.     }
  299.       while (isspace(*tmp))
  300.     {
  301.       tmp++;
  302.     }
  303.     }
  304.   (void) strncpy(myscore.died_from, tmp, strlen(tmp) - 1);
  305.   myscore.died_from[strlen(tmp) - 1] = '\0';
  306.   /* Get rid of '.' at end of death description */
  307.  
  308.   /*  First, get a lock on the high score file so no-one else tries */
  309.   /*  to write to it while we are using it */
  310. #ifdef USG
  311.   /* no flock sytem call, ignore the problem for now */
  312. #else
  313.   if (0 != flock(highscore_fd, LOCK_EX))
  314.     {
  315.       perror("Error gaining lock for score file");
  316.       exit(1);
  317.     }
  318. #endif
  319.  
  320.   /*  Check to see if this score is a high one and where it goes */
  321.   i = 0;
  322. #if defined(sun) || defined(ultrix) || defined(USG)
  323.   (void) lseek(highscore_fd, (long)0, L_SET);
  324. #else
  325.   (void) lseek(highscore_fd, (off_t)0, L_SET);
  326. #endif
  327.   while ((i < 20)
  328.         && (0 != read(highscore_fd, (char *)&scores[i], sizeof(high_scores))))
  329.     {
  330.       i++;
  331.     }
  332.  
  333.   j = 0;
  334.   while (j < i && (scores[j].points >= myscore.points))
  335.     {
  336.       j++;
  337.     }
  338.   /* i is now how many scores we have, and j is where we put this score */
  339.  
  340.   /* If its the first score, or it gets appended to the file */
  341.   if (0 == i || (i == j && j < 20))
  342.     {
  343. #if defined(sun) || defined(ultrix) || defined(USG)
  344.       (void) lseek(highscore_fd, (long)(j * sizeof(high_scores)), L_SET);
  345. #else
  346.       (void) lseek(highscore_fd, (off_t)(j * sizeof(high_scores)), L_SET);
  347. #endif
  348.       (void) write(highscore_fd, (char *)&myscore, sizeof(high_scores));
  349.     }
  350.   else if (j < i)
  351.     {
  352.       /* If it gets inserted in the middle */
  353.       /* Bump all the scores up one place */
  354.       for (k = MIN(i, 19); k > j ; k--)
  355.     {
  356. #if defined(sun) || defined(ultrix) || defined(USG)
  357.       (void) lseek(highscore_fd, (long)(k * sizeof(high_scores)), L_SET);
  358. #else
  359.       (void) lseek(highscore_fd, (off_t)(k * sizeof(high_scores)), L_SET);
  360. #endif
  361.       (void) write(highscore_fd, (char *)&scores[k - 1], sizeof(high_scores));
  362.     }
  363.       /* Write out your score */
  364. #if defined(sun) || defined(ultrix) || defined(USG)
  365.       (void) lseek(highscore_fd, (long)(j * sizeof(high_scores)), L_SET);
  366. #else
  367.       (void) lseek(highscore_fd, (off_t)(j * sizeof(high_scores)), L_SET);
  368. #endif
  369.       (void) write(highscore_fd, (char *)&myscore, sizeof(high_scores));
  370.     }
  371.  
  372. #ifdef USG
  373.   /* no flock sytem call, ignore the problem for now */
  374. #else
  375.   (void) flock(highscore_fd, LOCK_UN);
  376. #endif
  377.   (void) close(highscore_fd);
  378.   display_scores();
  379. }
  380.  
  381.  
  382. /* Change the player into a King!            -RAK-     */
  383. kingly()
  384. {
  385.   register struct misc *p_ptr;
  386.  
  387.   /* Change the character attributes...         */
  388.   dun_level = 0;
  389.   /* need dot on the end to be consistent with creature.c */
  390.   (void) strcpy(died_from, "Ripe Old Age.");
  391.   p_ptr = &py.misc;
  392.   p_ptr->lev += MAX_PLAYER_LEVEL;
  393.   if (p_ptr->sex[0] == 'M')
  394.     {
  395.       (void) strcpy(p_ptr->title, "Magnificent");
  396.       (void) strcpy(p_ptr->tclass, "*King*");
  397.     }
  398.   else
  399.     {
  400.       (void) strcpy(p_ptr->title, "Beautiful");
  401.       (void) strcpy(p_ptr->tclass, "*Queen*");
  402.     }
  403.   p_ptr->au += 250000;
  404.   p_ptr->max_exp += 5000000;
  405.   p_ptr->exp = p_ptr->max_exp;
  406.  
  407.   /* Let the player know that he did good...     */
  408.   clear_screen(0, 0);
  409.   dprint("                                  #", 1);
  410.   dprint("                                #####", 2);
  411.   dprint("                                  #", 3);
  412.   dprint("                            ,,,  $$$  ,,,", 4);
  413.   dprint("                        ,,==$   \"$$$$$\"   $==,,", 5);
  414.   dprint("                      ,$$        $$$        $$,", 6);
  415.   dprint("                      *>         <*>         <*", 7);
  416.   dprint("                      $$         $$$         $$", 8);
  417.   dprint("                      \"$$        $$$        $$\"", 9);
  418.   dprint("                       \"$$       $$$       $$\"", 10);
  419.   dprint("                        *#########*#########*", 11);
  420.   dprint("                        *#########*#########*", 12);
  421.   dprint("                          Veni, Vidi, Vici!", 15);
  422.   dprint("                     I came, I saw, I conquered!", 16);
  423.   if (p_ptr->sex[0] == 'M')
  424.     dprint("                      All Hail the Mighty King!", 17);
  425.   else
  426.     dprint("                      All Hail the Mighty Queen!", 17);
  427.   flush();
  428.   pause_line(23);
  429. }
  430.  
  431.  
  432. /* Handles the gravestone end top-twenty routines    -RAK-     */
  433. upon_death()
  434. {
  435.   /* What happens upon dying...                -RAK-     */
  436.   if (total_winner)
  437.     kingly();
  438.   print_tomb();
  439.   top_twenty();
  440.   exit_game();
  441. }
  442.