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