home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / NERVES.ZIP / PLAY / INITPLAY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-30  |  7.6 KB  |  360 lines

  1. #include "defn.h"
  2. #define EXTERN
  3. #include "playdata.c"
  4. #include "playproto.h"
  5.  
  6. void main(int argc,char *argv[])
  7.  { /* initialize playback */
  8.  
  9.   int i,j,k,step;
  10.   enum asc_val asc;
  11.   enum ext_val ext;
  12.  
  13.   if (argc < 2)
  14.    {
  15.     printf("Must include filename as command line parameter\n");
  16.     exit(0);
  17.    }
  18.  
  19.   /* allocate space for recorded data */
  20.   for (nblk=0; nblk<MAXBLK; nblk++)
  21.    {
  22.     rp[nblk] = calloc(MAXREC,sizeof(struct recbug));
  23.     if (rp[nblk] == NULL)
  24.      {
  25.       if (nblk == 0)
  26.        {
  27.     printf("Not enough memory\n");
  28.     exit(0);
  29.        }
  30.       break;
  31.      }
  32.    }
  33.  
  34.   pause = 0; /* initialize pause to none */
  35.   step = 0;
  36.  
  37.   strcpy(playfname,argv[1]);
  38.  
  39.   playfile = fopen(playfname,"rb");
  40.   if (playfile == NULL)
  41.    {
  42.     printf("Cannot open %s\n",playfname);
  43.     exit(0);
  44.    }
  45.   fread(&i,2,1,playfile);
  46.   fread(&j,2,1,playfile);
  47.   if (i != PLAYBUG || j != VERSION)
  48.    {
  49.     printf("Illegal file\n");
  50.     exit(0);
  51.    }
  52.  
  53.   /* extract file number from filename */
  54.   i = strlen(playfname) - 5;
  55.   filenum[0] = playfname[i];
  56.   if (filenum[0] == '0')
  57.    strcpy(filenum,"10");
  58.   else
  59.    {
  60.     if (filenum[0] < '1' || filenum[0] > '9')
  61.      filenum[0] = '?';
  62.     filenum[1] = 0;
  63.    }
  64.  
  65.   /* read in initial data */
  66.   fread(&rin.foodr[0],sizeof(struct recinit),1,playfile);
  67.   fread(rgr,sizeof(struct recgraph),5,playfile);
  68.   for (i=0; i<NFOOD; i++)  /* initialize current food radii */
  69.    foodr[i] = ofoodr[i] = rin.foodr[i];
  70.  
  71.   reccnt = 0;
  72.   cblk = 0;
  73.   rc = rp[0];
  74.   nrec = MAXREC;
  75.   for (ublk=0; ublk<nblk && nrec==MAXREC; ublk++)
  76.    {
  77.     nrec = fread(rp[ublk],sizeof(struct recbug),MAXREC,playfile);
  78.     if (nrec == 0 && ublk == 0)
  79.      {
  80.       printf("File too short\n");
  81.       exit(0);
  82.      }
  83.    }
  84.   if (nrec == 0)
  85.    ublk--; /* decrement ublk if last block empty */
  86.   if (ublk == 1)
  87.    lastrec = nrec;
  88.   else
  89.    lastrec = MAXREC;
  90.  
  91.   playgraph(); /* put up initial screen */
  92.  
  93.   setviewport(1,1,478,329,1);
  94.   setcolor(7);  /* draw food */
  95.   for (j=0; j<NFOOD; j++)
  96.    if (rin.foodr[j] != 0)
  97.     circle(rin.foodx[j],rin.foody[j],foodr[j]);
  98.  
  99.   showbug(rc,15);
  100.   setviewport(0,0,639,349,0);
  101.  
  102.   while (TRUE)
  103.    {
  104. getkey:
  105.     /* wait for key */
  106.     i = bioskey(0);
  107.     asc = toupper(i & 0xff);
  108.     if (asc != 0)
  109.      {
  110.       if (asc == ESC)
  111.        {
  112. quit:    fclose(playfile);
  113.     closegraph();
  114.     exit(0);
  115.        }
  116.       if (asc == CR)
  117.        {
  118.     i = multstep(&step); /* run indefinitely */
  119.     if (i)
  120.      goto quit; /* if ESC, quit */
  121.        }
  122.       else
  123.        if (asc == SPACE)
  124.     singstep(&step); /* run one step */
  125.        else
  126.     if (asc == '+' && pause != 0)
  127.      {
  128.       pause -= PAUSEINC;
  129.       if (pause == 0)
  130.        bar(266,336,282,348); /* change menu to - only */
  131.      }
  132.     else
  133.      if (asc == '-')
  134.       {
  135.        pause += PAUSEINC;
  136.        if (pause == PAUSEINC)
  137.         {
  138.          setcolor(3);
  139.          outtextxy(266,336,"+/");
  140.         }
  141.       }
  142.      }
  143.    }
  144.  }
  145.  
  146. int near multstep(int *step)
  147.  {
  148.   int i;
  149.   enum asc_val asc;
  150.  
  151.   while (TRUE)
  152.    {
  153.     singstep(step);
  154.     i = bioskey(1);
  155.     if (i)
  156.      {
  157.       bioskey(0);
  158.       asc = i & 0xff;
  159.       if (asc == ESC)
  160.        return(TRUE);
  161.       if (asc == SPACE)
  162.        return(FALSE);
  163.      }
  164.     delay(pause);
  165.    }
  166.  }
  167.  
  168. void near singstep(int *step)
  169.  {
  170.   int y,j,i,k,drawfood;
  171.   char str[25],c;
  172.  
  173.   (*step)++;
  174.   for (j=0; j<5; j++) /* plot graph points */
  175.    {
  176.     if (rgr[j].gtype == 1)
  177.      { /* variable */
  178.       putpixel(536 + *step*2,rc->graphy1[j] + 66*j,15);
  179.       putpixel(537 + *step*2,rc->graphy2[j] + 66*j,15);
  180.      }
  181.     else
  182.      if (rgr[j].gtype == 2)
  183.       { /* step */
  184.        for (i=0; i<6; i++)
  185.     {
  186.      if (!(rc->footst & (1<<i)))
  187.       {
  188.        putpixel(536 + *step*2,49-i*9+66*j,15);
  189.        putpixel(537 + *step*2,49-i*9+66*j,15);
  190.       }
  191.     }
  192.       }
  193.    }
  194.  
  195.   /* redraw food if necessary */
  196.   setviewport(1,1,478,329,1);
  197.   for (j=0; j<NFOOD; j++)
  198.    {
  199.     drawfood = FALSE;
  200.     if (foodr[j] != 0)
  201.      {
  202.       if (rc->mouthfood & (1<<(j+4)))
  203.        {
  204.     ofoodr[j] = foodr[j];
  205.     foodr[j]--;
  206.     drawfood = TRUE;
  207.        }
  208.       else
  209.        if (rc->mouthfood & (1<<(j+1)))
  210.     drawfood = TRUE;
  211.      }
  212.     if (drawfood)
  213.      {
  214.       setcolor(0);
  215.       circle(rin.foodx[j], rin.foody[j], ofoodr[j]);
  216.       setcolor(7);
  217.       circle(rin.foodx[j], rin.foody[j], foodr[j]);
  218.      }
  219.    }
  220.  
  221.   /* draw bug */
  222.   setviewport(1,1,478,329,0);
  223.   if (reccnt != 0)
  224.    showbug(rc-1,0); /* erase old bug */
  225.   showbug(rc,15);
  226.  
  227.   /* display energy */
  228.   setviewport(0,0,639,349,0);
  229.   bar(422,336,454,348);
  230.   sprintf(str,"%d",rc->energy);
  231.   setcolor(3);
  232.   outtextxy(422,336,str);
  233.  
  234.   /* change Iext if necessary */
  235.   if (rc->Iextname[0])
  236.    {
  237.     bar(455,348,629,336);
  238.     settextjustify(2,2);
  239.     sprintf(str,"Iext(%s)[nA]:%-.1f",rc->Iextname,(float)rc->Iext/10.);
  240.     outtextxy(629,336,str);
  241.     settextjustify(0,2);
  242.    }
  243.   if (*step == 50)
  244.    { /* reset graphs */
  245.     *step = 0;
  246.     for (j=0; j<5; j++)
  247.      {
  248.       if (rgr[j].gtype)
  249.        {
  250.     bar(538,3+j*66,638,52+j*66);
  251.     setcolor(3);
  252.     line(547,3+j*66,547,55+j*66); /*graph x-axis ticks */
  253.     line(557,3+j*66,557,55+j*66);
  254.     line(567,3+j*66,567,55+j*66);
  255.     line(577,3+j*66,577,55+j*66);
  256.     line(587,3+j*66,587,56+j*66);
  257.     line(597,3+j*66,597,55+j*66);
  258.     line(607,3+j*66,607,55+j*66);
  259.     line(617,3+j*66,617,55+j*66);
  260.     line(627,3+j*66,627,55+j*66);
  261.     line(637,3+j*66,637,56+j*66);
  262.     setcolor(15);
  263.        }
  264.      }
  265.    }
  266.  
  267.   reccnt++;
  268.   if (reccnt == lastrec)
  269.    {
  270.     setviewport(1,1,478,329,1);
  271.     showbug(rc,0); /* erase bug */
  272.     reccnt = 0;
  273.     cblk++;
  274.     if (cblk == ublk)
  275.      { /* finished last read block */
  276.       rc = rp[0];
  277.       cblk = 0;
  278.       if (ublk < nblk)
  279.        { /* finished file, start again */
  280. close:     fclose(playfile);
  281.     setcolor(0);
  282.     for (i=0; i<NFOOD; i++) /* erase any food */
  283.      if (ofoodr[i] != 0)
  284.       circle(rin.foodx[i], rin.foody[i], foodr[i]);
  285.     playfile = fopen(playfname,"rb");
  286.     fread(&i,2,2,playfile);
  287.     fread(&rin.foodr[0],sizeof(struct recinit),1,playfile);
  288.     fread(rgr,sizeof(struct recgraph),5,playfile);
  289.     setcolor(3);
  290.     for (i=0; i<NFOOD; i++)
  291.      {
  292.       foodr[i] = ofoodr[i] = rin.foodr[i];
  293.       if (foodr[i] != 0)
  294.        circle(rin.foodx[i],rin.foody[i],foodr[i]);
  295.      }
  296.     *step = 0;
  297.     nrec = MAXREC;
  298.     for (ublk=0; ublk<nblk && nrec==MAXREC; ublk++)
  299.      nrec = fread(rp[ublk],sizeof(struct recbug),MAXREC,playfile);
  300.     if (nrec == 0)
  301.      ublk--; /* decrement ublk if last block empty */
  302.     if (ublk == 1)
  303.      lastrec = nrec;
  304.     else
  305.      lastrec = MAXREC;
  306.     setviewport(0,0,639,349,0);
  307.     bar(455,348,629,336); /* erase Iext */
  308.     for (j=0; j<5; j++) /* reset graphs */
  309.      {
  310.       if (rgr[j].gtype)
  311.        {
  312.         bar(538,3+j*66,638,52+j*66);
  313.         setcolor(3);
  314.         line(547,3+j*66,547,55+j*66); /*graph x-axis ticks */
  315.         line(557,3+j*66,557,55+j*66);
  316.         line(567,3+j*66,567,55+j*66);
  317.         line(577,3+j*66,577,55+j*66);
  318.         line(587,3+j*66,587,56+j*66);
  319.         line(597,3+j*66,597,55+j*66);
  320.         line(607,3+j*66,607,55+j*66);
  321.         line(617,3+j*66,617,55+j*66);
  322.         line(627,3+j*66,627,55+j*66);
  323.         line(637,3+j*66,637,56+j*66);
  324.         setcolor(15);
  325.        }
  326.      }
  327.        }
  328.       else
  329.        { /* read next part of file */
  330.     nrec = MAXREC;
  331.     for (ublk=0; ublk<nblk && nrec==MAXREC; ublk++)
  332.      nrec = fread(rp[ublk],sizeof(struct recbug),MAXREC,playfile);
  333.     if (nrec == 0)
  334.      ublk--; /* decrement ublk if last block empty */
  335.     if (ublk == 0)
  336.      goto close;
  337.     if (ublk == 1)
  338.      lastrec = nrec;
  339.     else
  340.      lastrec = MAXREC;
  341.     setviewport(0,0,639,349,0);
  342.        }
  343.      }
  344.     else
  345.      {
  346.       setviewport(0,0,639,349,0);
  347.       rc = rp[cblk];
  348.       if (cblk == ublk-1)
  349.        lastrec = nrec;
  350.       else
  351.        lastrec = MAXREC;
  352.      }
  353.    }
  354.   else
  355.    rc++;
  356.  
  357.   return;
  358.  }
  359.  
  360.