home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume12 / torus / part01 / score.c < prev    next >
C/C++ Source or Header  |  1991-05-15  |  5KB  |  244 lines

  1. # include "robots.h"
  2.  
  3. /*
  4.  * score.c: All the scoring code is in here.
  5.  */
  6.  
  7. struct scorefile {
  8.     int    s_uid;
  9.     long    s_score;
  10.     char    s_name[MAXSTR];
  11.     bool    s_eaten;
  12.     int    s_level;
  13.     bool    s_hsew;
  14.     bool    s_vsew;
  15.     bool    s_wimpy;
  16.     int    s_days;
  17. };
  18.  
  19. # define FILE_SIZE    (NUMSCORES*sizeof(struct scorefile))
  20.  
  21. scoring(eaten)
  22.     bool eaten;
  23. {
  24.     static char buf[MAXSTR];
  25.         toral = hsew && vsew;
  26.     (void) sprintf(buf,"for this %s",TEMP_NAME);
  27.     if( record_score(eaten,toral?T_TMP_FILE:TMP_FILE,TEMP_DAYS,buf)
  28.             || show_highscore) {
  29.         printf("[Press return to continue]");
  30.         fflush(stdout);
  31.         gets(buf);
  32.     }
  33.     record_score(eaten,toral?T_HOF_FILE:HOF_FILE,0,"of All Time");
  34. }
  35.  
  36. # define    sigbit(x)    (1 << ((x) - 1))
  37.  
  38. record_score(eaten,fname,max_days,type_str)
  39.     bool eaten;
  40.     char *fname;
  41.     int max_days;
  42.     char *type_str;
  43. {
  44.     int value;
  45.     int fd;
  46.     int omask;
  47.  
  48.     /* block signals while recording the score
  49.      * hope this routine doesn't get stuck!
  50.      */
  51. # ifndef BSD42
  52. # ifdef TURBOC /* rfs */
  53.     void
  54. # else
  55.     int
  56. # endif
  57.          (*oint)(), (*oterm)(), (*ohup)();
  58.  
  59.     oint = signal(SIGINT, SIG_IGN);
  60.     oterm = signal(SIGTERM, SIG_IGN);
  61. # ifndef TURBOC /* rfs */
  62.     ohup = signal(SIGHUP, SIG_IGN);
  63. # endif
  64. # else
  65.     omask = sigblock( sigbit(SIGINT) | sigbit(SIGTERM) | sigbit(SIGHUP)
  66.             | sigbit(SIGTSTP));
  67. # endif
  68.  
  69.     if((fd = lk_open(fname,
  70. # ifdef TURBOC /* rfs */
  71.                 O_RDWR | O_CREAT | O_BINARY
  72. # else
  73.                 2
  74. # endif
  75.                     )) < 0) {
  76.         perror(fname);
  77.     } else {
  78.         value = do_score(eaten,fd,max_days,type_str);
  79.         lk_close(fd, fname);
  80.     }
  81. # ifdef BSD42
  82.     (void) sigsetmask(omask);
  83. # else
  84.     (void) signal(SIGINT, oint);
  85.     (void) signal(SIGTERM, oterm);
  86. # ifndef TURBOC /* rfs */
  87.     (void) signal(SIGHUP, ohup);
  88. # endif
  89. # endif
  90.     return value;
  91. }
  92.  
  93. do_score(eaten,fd,max_days,type_str)
  94.     bool eaten;
  95.     int fd, max_days;
  96.     char *type_str;
  97. {
  98.     register struct scorefile *position;
  99.     register int x;
  100. /*    register struct scorefile *remove, *sfile, *eof; */ /* rfs */
  101.     register struct scorefile *remove, *eof; /* rfs */
  102.     static struct scorefile *sfile=NULL; /* rfs */
  103.     struct scorefile *oldest, *this;
  104.     int uid, this_day, limit;
  105.  
  106.     this_day = max_days ? time((time_t *)0)/SECSPERDAY : 0;
  107.     limit = this_day-max_days;
  108.     if (sfile==NULL) /* rfs */
  109.     sfile = (struct scorefile *)(malloc(FILE_SIZE));
  110.     if( sfile == NULL)
  111.     {
  112.         fprintf( stderr, "Out of memory so no scoring");
  113.         return FALSE;
  114.     }
  115.     eof = &sfile[NUMSCORES];
  116.     this = 0;
  117.     for(position = sfile; position < eof; position++) {
  118.         position->s_score = 0;
  119.         position->s_days = 0;
  120.     }
  121.     read(fd, (char *)sfile,FILE_SIZE);
  122.     remove = 0;
  123.     if(score > 0) {
  124.         uid = getuid();
  125.         oldest = 0;
  126.         x = limit;
  127.         for(position = eof-1; position >= sfile; position--) {
  128.             if(position->s_days < x) {
  129.                 x = position->s_days;
  130.                 oldest = position;
  131.             }
  132.         }
  133.         position = 0;
  134.         for(remove = sfile; remove < eof; remove++) {
  135.             if(position == 0 && score > remove->s_score) position = remove;
  136. # ifndef ALLSCORES
  137.             if(remove->s_uid == uid) break;
  138. # endif ALLSCORES
  139.         }
  140.         if(remove < eof) {
  141.             if(position == 0 && remove->s_days < limit) position = remove;
  142.         } else if(oldest) {
  143.             remove = oldest;
  144.             if(position == 0) {
  145.                 position = eof-1;
  146.             } else if(remove < position) {
  147.                 position--;
  148.             }
  149.         } else if(position) {
  150.             remove = eof-1;
  151.         }
  152.         if(position) {
  153.             if(remove < position) {
  154.                 while(remove < position) {
  155.                     *remove = *(remove+1);
  156.                     remove++;
  157.                 }
  158.             } else {
  159.                 while(remove > position) {
  160.                     *remove = *(remove-1);
  161.                     remove--;
  162.                 }
  163.             }
  164.             position->s_score = score;
  165.             (void) strncpy(position->s_name,whoami,MAXSTR);
  166.             position->s_eaten = eaten;
  167.             position->s_level = LEVEL;
  168.             position->s_uid = uid;
  169.             position->s_hsew = hsew;
  170.             position->s_vsew = vsew;
  171.             position->s_wimpy = wimpy;
  172.             position->s_days = this_day;
  173.             this = position;
  174.             if(lseek(fd,0L,0) == -1L ||
  175.                 write(fd,(char *)sfile,FILE_SIZE) != FILE_SIZE)
  176.                 perror("scorefile");
  177.             close(fd);
  178.         }
  179.     }
  180.     if( show_highscore || this )
  181.     {
  182.         printf(
  183. # ifdef ALLSCORES
  184.             "\nTop %s %s Scores %s:\n",
  185. # else ALLSCORES
  186.             "\nTop %s %s Robotists %s:\n",
  187. # endif ALLSCORES
  188.             NUMNAME,
  189.                         toral?"Toral":"Non-Toral",
  190.             type_str
  191.         );
  192.         printf("Rank     Score    Name\n");
  193.         for(position = sfile; position < eof; position++) {
  194.             if(position->s_score == 0) break;
  195.             if(position == this)
  196.                 putchar('>');
  197.             else  putchar(' ');
  198.             printf(
  199.                    "%c%2d %10ld  %8s: %s on %6s",
  200.                    position->s_days < limit ? '*' : ' ',
  201.                    position-sfile+1,
  202.                    position->s_score,
  203.                    position->s_name,
  204.                    position->s_eaten ?
  205.                      "robot chow" : "ducked out",
  206.                    position->s_wimpy ? "wimpy" : "studly");
  207.             if(position->s_hsew)
  208.               printf( " %7s",
  209.                  (position->s_vsew) ? "toral" : "annular");
  210.             else
  211.               printf( " %7s",
  212.                  (position->s_vsew) ? "annular" : "planar");
  213.             printf(" level %d.", position->s_level);
  214.  
  215.             if(position == this)
  216.                 putchar('<');
  217.             putchar('\n');
  218.         }
  219.     }
  220.     return (this != 0);
  221. }
  222.  
  223. scorer()
  224. {
  225.     static char tels[6];
  226.     if(free_teleports != old_free) {
  227.         if(free_teleports > free_per_level) {
  228.             (void) sprintf(tels,"%d+%d",
  229.                        free_per_level,
  230.                        free_teleports-free_per_level);
  231.         } else {
  232.             (void) sprintf(tels,"%d",free_teleports);
  233.         }
  234.         old_free = free_teleports;
  235.     }
  236.     move(LINES-1,0);
  237.     clrtoeol();
  238.     printw("<%s> level: %d    score: %ld",tels,LEVEL,score);
  239.         mvprintw(LINES-1,RVPOS,"heaps:%3d robots:%3d value: %d",
  240.                     scrap_heaps,nrobots_alive,robot_value);
  241.     clrtoeol();
  242. }
  243.  
  244.