home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume13 / dr_mario / part01 / mario.shr / loop.c < prev    next >
C/C++ Source or Header  |  1992-01-12  |  5KB  |  223 lines

  1. #include "info.h"
  2.  
  3. struct pos      npos[] = {1, 1,
  4.             -1, -1}; /*used in turning chips*/
  5.  
  6. void
  7. loop()
  8. {
  9.     void            change(), loop2(); 
  10.  
  11.     piece = 0;/* haven't played yet */
  12.     mvprintw(1, 0, "Score: %d", score);/*update stats*/
  13.     mvprintw(3, 0, "Level: %d", lv);
  14.     mvprintw(5, 0, "Bugs left: %02d", v);
  15.     mvprintw(7, 0, "Speed: %d", sp);
  16.     refresh();
  17.     while (v != 0) { /*till no bugs left*/
  18.         cp = 0;
  19.         nextpiece();
  20.         p1->y = p2->y = 0; /*place at top of board*/
  21.         p1->x = 4;
  22.         p2->x = 5;
  23.  
  24.         mvwaddch(w, p1->y, p1->x, p1->c);
  25.         mvwaddch(w, p2->y, p2->x, p2->c);
  26.         wrefresh(w);
  27.         if (mvwinch(w, 1, 5) != BLANK || mvwinch(w, 1, 4) != BLANK )
  28.         /*can't move down, tough luck*/
  29.             fatal("Game Over");
  30.  
  31.         loop2(); /*loop here till piece is place*/
  32.         meat(); /*checks for removal and removes pieces if necessary
  33.                 (garbage man)*/
  34.         piece++; /*used a piece*/
  35.         if (sc) {/*figure out score. if sc=0 we didn't remove any bugs*/
  36.             int             sco = sp;
  37.             if (sc > 6)
  38.                 sc = 6;
  39.             for (; sc > 1; --sc)
  40.                 sco = sco * 2;
  41.             score += sco * 100;
  42.             sc = 0; /*score =  n(sc+1)=n(sc)*200;*/
  43.  
  44.             mvprintw(1, 0, "Score: %d", score);
  45.             mvprintw(5, 0, "Bugs left: %02d",  v);
  46.             refresh();
  47.         }
  48.     }
  49. }
  50.  
  51. void
  52. loop2()
  53. {
  54.     register int    i, dflg = 0, flags;
  55.     short           tme = 0; /*time counter*/
  56.  
  57.     while ((i = wgetch(w)) != 'q' && !dflg) {
  58.         switch (i) {
  59.         case 'h':
  60.             mvcheck(0, -1);
  61.             break;
  62.  
  63.         case 'l':
  64.             mvcheck(0, 1);
  65.             break;
  66.  
  67.         case 'k':
  68.             if (srn)
  69.             mvcheck(-1,0);
  70.             break;
  71.  
  72.         case 'j':
  73.             dflg = mvcheck(1, 0); /*if dflag is set we hit something*/ 
  74.             tme = 0;/*reset timer*/
  75.             break;
  76.  
  77.         case 'a': /*rotate counter-clockwise*/ 
  78.             change(-1);
  79.             break;
  80.  
  81.         case 's':/*rotate clockwise*/ 
  82.             change(1);
  83.             break;
  84.  
  85.         case ' ':
  86.             while (mvcheck(1,0) != -1)
  87.             wrefresh(w);
  88.             dflg=1;
  89.             break;
  90.  
  91.         case 'p':
  92.             mvprintw(LINES - 3, COLS - 7, "BUGS I"); 
  93.             mvprintw(LINES - 2, COLS - 6, "1.3"); 
  94.             refresh();bleed(0,0);
  95.             while(getch() != 'p'); 
  96.             bleed(0,1);
  97.             deleteln();
  98.             move(LINES -3,0);deleteln();
  99.             deleteln();
  100.             refresh();
  101.             break;
  102. default:
  103. usleep(250000L - sp*50000);
  104. break;
  105.         }
  106.         if (i != EOF)/*EOF is generated if no input*/
  107.             wrefresh(w);
  108.  
  109.         tme++; /*increase timer*/
  110.         if (tme > 4 ) /*No input for awhile?*/
  111.             dflg = mvcheck(1, 0), tme = 0, wrefresh(w);
  112.     }
  113.     if (dflg && !(cp & 1)) { /*if we're sideways set a[][]*/
  114.         a[p1->y][p1->x] = 1;
  115.         a[p2->y][p2->x] = 2;
  116.     }
  117.     if (i == 'q') 
  118.         fatal("Quit");
  119. }
  120.  
  121. int
  122. mvcheck(y1, x1) /*see if we can move or not*/
  123.     register int    y1, x1;
  124. {
  125.     int             a = BLANK, b = BLANK;
  126.  
  127.     /*we don't want to examine the other half of piece*/
  128.     if (p1->y + y1 != p2->y || p1->x + x1 != p2->x)
  129.         a = mvwinch(w, p1->y + y1, p1->x + x1);
  130.     if (p2->y + y1 != p1->y || p2->x + x1 != p1->x)
  131.         b = mvwinch(w, p2->y + y1, p2->x + x1);
  132.     if (a != BLANK || b != BLANK)
  133.         return -1; /*no good, if we're going down, we hit
  134.                     something*/
  135.  
  136.     mvwaddch(w, p1->y, p1->x, BLANK);
  137.     mvwaddch(w, p2->y, p2->x, BLANK);
  138.     p1->y += y1;
  139.     p2->y += y1;
  140.     p1->x += x1;
  141.     p2->x += x1;
  142.     mvwaddch(w, p1->y, p1->x, p1->c);
  143.     mvwaddch(w, p2->y, p2->x, p2->c);
  144.     return 0; /*A-ok*/
  145. }
  146.  
  147.  
  148. void
  149. change(dir) /*rotate piece*/
  150.     int             dir;
  151. {
  152.     /*if cp=0 or 2 we are sideways, to test we use !(cp&1)  
  153.     bit-fields were a possibility here, but not worth it*/
  154.  
  155.     register int    j, l = cp + dir, k = (l & 1); 
  156.     int             ny, nx, sideflag = 0;
  157.     char            temp;
  158.  
  159.     if (dir == -1)
  160.         dir = 0;
  161.  
  162.     ny = npos[k].y; /*next position if turn is successful*/
  163.     nx = npos[k].x;
  164.  
  165.     if (p2->x == 8 && cp & 1) { /*if we're on the right wall vertically
  166.             we need to backoff to turn & check somewhere else*/
  167.         j = mvwinch(w, p1->y, 7); 
  168.         sideflag = 1; /*set a flag*/
  169.         nx = 0;
  170.     } else
  171.         j = mvwinch(w, p2->y + ny, p2->x + nx);  
  172.         /*not on wall check regularly*/
  173.  
  174.     if (j != BLANK)
  175.         return; /*can't turn*/
  176.  
  177.     if (sideflag)
  178.         p1->x -= 1;/*had to check a first*/
  179.  
  180.     if ((cp + dir) & 1) {/*swap chars in piece*/
  181.         temp = p1->c;
  182.         p1->c = p2->c;
  183.         p2->c = temp;
  184.     }
  185.     mvwaddch(w, p1->y, p1->x, p1->c);
  186.     mvwaddch(w, p2->y, p2->x, BLANK);
  187.  
  188.     p2->y += ny;/*add new values to current pos*/
  189.     p2->x += nx;
  190.     mvwaddch(w, p2->y, p2->x, p2->c);
  191.  
  192.     cp = l;
  193.     if (cp<0)
  194.         cp = 3;
  195.     else if (cp > 3)
  196.         cp = 0; /*positions number from 0 to 3*/
  197. }
  198.     /*
  199.         usleep -- support routine for 4.2BSD system call emulations
  200.  
  201.         last edit:    29-Oct-1984    D A Gwyn
  202.     */
  203.  
  204.     extern int    select();
  205.  
  206.  
  207.     int
  208.     usleep( usec )                /* returns 0 if ok, else -1 */
  209.         long        usec;        /* delay in microseconds */
  210.         {
  211.         static struct            /* `timeval' */
  212.             {
  213.             long    tv_sec;        /* seconds */
  214.             long    tv_usec;    /* microsecs */
  215.             }    delay;        /* _select() timeout */
  216.  
  217.         delay.tv_sec = usec / 1000000L;
  218.         delay.tv_usec = usec % 1000000L;
  219.  
  220.         return select( 0, (long *)0, (long *)0, (long *)0, &delay );
  221.         }
  222.  
  223.