home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / NERVES.ZIP / NO87 / INITSIM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-12  |  13.0 KB  |  579 lines

  1. #include "defn.h"
  2. #include "nsdata.c"
  3. #include "proto.h"
  4. #include "update.c"
  5.  
  6. int initsim(void)
  7.  { /* initialize simulation */
  8.  
  9.   int i,j,k,step,savIext;
  10.   char str[26],*p;
  11.   enum asc_val asc;
  12.   enum ext_val ext;
  13.   int r,t;
  14.   long time;
  15.   float f;
  16.   struct neuron *np,*nq;
  17.   struct con *cp;
  18.   struct neurcalc *ncp;
  19.   struct Iint *ip;
  20.   int bugx,bugy,bugang;
  21.   FILE *recfile;
  22.  
  23.   /* find all connection names */
  24.   for (j=0, np=ns; j<nn; j++, np++)
  25.    {
  26.     if (np->name[0])
  27.      for (cp=np->con; cp != NULL; cp=cp->next)
  28.       {
  29.        for (i=0, nq=ns; i<nn; i++, nq++)
  30.     if (nq->name[0] && !strcmp(cp->sname,nq->name))
  31.      break;
  32.        if (i == nn)
  33.     { /* name not found */
  34.      gotoxy(1,1);
  35.      cprintf("Sending neuron %s for neuron %s not found",cp->sname,np->name);
  36.      gotoxy(1,2);
  37.      cprintf("Press any key and N to correct");
  38.      bioskey(0);
  39.      clrline(1);
  40.      clrline(2);
  41.      erasecursor();
  42.      return(0);
  43.     }
  44.        else
  45.     cp->s = i;
  46.        if (cp->ctype)
  47.     {
  48.      for (i=0, nq=ns; i<nn; i++, nq++)
  49.       if (nq->name[0] && !strcmp(cp->cname,nq->name))
  50.        break;
  51.      if (i == nn)
  52.       { /* name not found */
  53.        gotoxy(1,1);
  54.        cprintf("Compound neuron %s for neuron %s not found",cp->cname,np->name);
  55.        gotoxy(1,2);
  56.        cprintf("Press any key and N to correct");
  57.        bioskey(0);
  58.        clrline(1);
  59.        clrline(2);
  60.        erasecursor();
  61.        return(0);
  62.       }
  63.      else
  64.       cp->c = i;
  65.     }
  66.       }
  67.    }
  68.  
  69.   /* find all graph names */
  70.   for (j=0; j<5; j++)
  71.    if (gr[j].gtype == 1)
  72.     {
  73.      for (i=0, nq=ns; i<nn; i++, nq++)
  74.       if (nq->name[0] && !strcmp(gr[j].gneur,nq->name))
  75.        break;
  76.      if (i == nn)
  77.       { /* name not found */
  78.        gotoxy(1,1);
  79.        cprintf("Graph neuron %s for graph %d not found",gr[j].gneur,j+1);
  80.        gotoxy(1,2);
  81.        cprintf("Press any key and N or G to correct");
  82.        bioskey(0);
  83.        clrline(1);
  84.        clrline(2);
  85.        erasecursor();
  86.        return(0);
  87.       }
  88.      else
  89.       gr[j].g = i;
  90.     }
  91.  
  92.   if (Iextgr >= 0 && gr[Iextgr].gtype == 1) /* set Iext neuron */
  93.    Iextind = gr[Iextgr].g;
  94.   else
  95.    Iextind = -1;
  96.  
  97.   for (i=0, ip=Iinta; i<ni; i++, ip++)
  98.    {
  99.     ip->H = 0;
  100.     ip->tint = 0;
  101.     if (ip->type == 1) /* if random, initialize tmax */
  102.      ip->tmax = random(ip->ipL[1] - ip->ipL[0]+1) + ip->ipL[0];
  103.    }
  104.  
  105.   /* initialize leg angles, foot states, and foot positions */
  106.   legang[0] = legang[1] = legang[2] = 0;
  107.   legang[3] = legang[4] = legang[5] = PI;
  108.   bugx = (bug.x + 500L)/1000L;
  109.   bugy = (bug.y + 500L)/1000L;
  110.   bugang = (bug.ang + 50L)/100L;
  111.   for (j=0; j<6; j++)
  112.    {
  113.     bug.foot[j] = 1; /* all down */
  114.     bug.footx[j] = bugx + ((long)attl[j] * lcos(bugang + attang[j])
  115.             + (long)legl[j] * lcos(bugang + legang[j]))/1000L;
  116.     bug.footy[j] = bugy + ((long)attl[j] * lsin(bugang + attang[j])
  117.             + (long)legl[j] * lsin(bugang + legang[j]))/1000L;
  118.    }
  119.  
  120.   /* initialize antenna and cercus ends */
  121.   for (j=0; j<2; j++)
  122.    {
  123.     bug.antx[j] = bugx + ((long)antl * lcos(bugang + antang[j]))/1000L;
  124.     bug.anty[j] = bugy + ((long)antl * lsin(bugang + antang[j]))/1000L;
  125.     bug.cercx[j] = bugx + ((long)cercl * lcos(bugang + cercang[j]))/1000L;
  126.     bug.cercy[j] = bugy + ((long)cercl * lsin(bugang + cercang[j]))/1000L;
  127.     edget[j] = 0;
  128.    }
  129.   /* initialize mouth and odors */
  130.   odor[0] = odor[1] = mouthodor = 0;
  131.   bug.mouthx = bugx + ((long)hdtl * lcos(bugang + hdtang))/1000L;
  132.   bug.mouthy = bugy + ((long)hdtl * lsin(bugang + hdtang))/1000L;
  133.   for (j=0; j<nfood; j++)
  134.    { /* calculate odors for each antenna & mouth */
  135.     for (k=0; k<2; k++)
  136.      {
  137.       r = bug.antx[k] - foodx[j];
  138.       t = bug.anty[k] - foody[j];
  139.       r = r*r + t*t;
  140.       if (r != 0)
  141.        odor[k] += 3L*foodsize[j]/r*40000L; /* change from vers 1 */
  142.       else
  143.        odor[k] += foodsize[j]/10000L;
  144.      }
  145.     r = bug.mouthx - foodx[j];
  146.     t = bug.mouthy - foody[j];
  147.     r = r*r + t*t;
  148.     if (r != 0)
  149.      mouthodor += foodsize[j]/r*10000L;
  150.     else
  151.      mouthodor += foodsize[j]/10000L;
  152.     foodr[j] = sqrt(foodsize[j]/(PI*10L));
  153.     ofoodr[j] = foodr[j];
  154.    }
  155.   bug.mouthst = 0; /* initialize mouth state to closed */
  156.   mouth = 0;
  157.  
  158.   for (i=0, ncp=nsc; i<nn; i++, ncp++)
  159.    {
  160.     ncp->V = (ns+i)->iVt;
  161.     ncp->F = 0;
  162.     ncp->Vlast = ncp->V;
  163.     ncp->I = 0;
  164.    }
  165.  
  166.   graph();
  167.   setviewport(1,1,478,329,1);
  168.   setcolor(7);
  169.   for (j=0; j<nfood; j++)
  170.    circle(foodx[j]/2, ((long)AspR * foody[j])/200L, foodr[j]);
  171.   obug = bug;
  172.   drawbug(bug,15);
  173.   setviewport(0,0,639,349,0);
  174.   time = 0;
  175.   step = 0;
  176.  
  177.   while (TRUE)
  178.    {
  179. getkey:
  180.     /* wait for key */
  181.     i = bioskey(0);
  182.     asc = toupper(i & 0xff);
  183.     if (asc != 0)
  184.      {
  185.       if (asc == ESC)
  186.        {
  187.     if (recnum != 0)
  188.      {
  189. stoprec:  fclose(recfile);
  190.       setcolor(3);
  191.       bar(226,336,370,348);
  192.       outtextxy(226,336,"Menu  Tab:ChgPlots");
  193.       recnum = 0;
  194.       goto getkey;
  195.      }
  196.     else
  197.      return(0);
  198.        }
  199.       if (asc == CR)
  200.        {
  201.     i = multstep(&step,&time,recfile,&savIext); /* run indefinitely */
  202.     if (i)
  203.      {
  204.       if (recnum != 0)
  205.        goto stoprec;
  206.       else
  207.        return(0);
  208.      }
  209.        }
  210.       else
  211.        if (asc == SPACE)
  212.     {
  213.      if (!singstep(&step,&time,recfile,&savIext)) /* run one step */
  214.       singstep(&step,&time,recfile,&savIext);
  215.     }
  216.        else
  217.     if (asc == 'D')
  218.      {
  219.       if (!recnum)
  220.        {
  221.         drawmult *= 2;
  222.         if (drawmult > 64)
  223.          drawmult = 2;
  224.         setcolor(3);
  225.         bar(400,336,417,348);
  226.         sprintf(str,"%d ",drawmult);
  227.         outtextxy(400,336,str);
  228.        }
  229.      }
  230.     else
  231.      if (asc >= '0' && asc <= '9')
  232.       {
  233.        p = str;
  234.        while (asc != CR)
  235.         {
  236.          *p = asc;
  237.          p++;
  238.          asc = bioskey(0) & 0xff;
  239.         }
  240.        *p = 0;
  241.        if (str[0] >= '0' && str[0] <= '5' && str[1] == '=')
  242.         {
  243.          j = str[0] - '1';
  244.          setcolor(3);
  245.          if (j < 0)
  246.           {
  247.            bar(455,348,629,336);
  248.            Iextgr = -1;
  249.            Iextind = -1;
  250.           }
  251.          else
  252.           if (gr[j].gtype == 1)
  253.            {
  254.         f = atof(str+2);
  255.         if (fabs(f) < 100.)
  256.          {
  257.           Iextgr = j;
  258.           Iextind = gr[j].g;
  259.           Iextint = f*10000;
  260.           bar(455,348,629,336);
  261.           settextjustify(2,2);
  262.           sprintf(str,"Iext(%s)[nA]:%-.1f",gr[Iextgr].gneur,(float)Iextint/10000.);
  263.           outtextxy(629,336,str);
  264.           settextjustify(0,2);
  265.           savIext = TRUE;
  266.          }
  267.            }
  268.         }
  269.       }
  270.      else
  271.       if (asc == TAB)
  272.        if (!recnum)
  273.         { /* change plot type */
  274.          bar(480,1,638,329); /* first blank entire block */
  275.          plottype = !plottype;
  276.          if (!plottype)
  277.           graphsetup();
  278.          else
  279.           activesetup();
  280.         }
  281.      }
  282.     else
  283.      {
  284.       ext = i>>8;
  285.       if (ext >= F1 && ext <= F10)
  286.        { /* start recording */
  287.     if ((step%4) != 0) /* if not where bug should be drawn, get there */
  288.      {
  289.       singstep(&step,&time,recfile,&savIext);
  290.       singstep(&step,&time,recfile,&savIext);
  291.      }
  292.     if (recnum != 0)
  293.      fclose(recfile);
  294.     recnum = ext - F1 + 1;
  295.     if (recpath[0] == 0)
  296.      sprintf(str,"ns%d.sav",recnum);
  297.     else
  298.      sprintf(str,"%s\\ns%d.sav\0",recpath,recnum);
  299.     recfile = fopen(str,"wb");
  300.     if (recfile == NULL)
  301.      {
  302.       recnum = 0;
  303.       return(1); /* return 1 to indicate recording path bad */
  304.      }
  305.     else
  306.      {
  307.       setcolor(3);
  308.       bar(226,336,370,348);
  309.       sprintf(str,"Stop  RECORDING:%d",recnum);
  310.       outtextxy(226,336,str);
  311.       if (drawmult > 2)
  312.        {
  313.         drawmult = 2; /* reset drawmult to 2 for recording */
  314.         setcolor(3);
  315.         bar(400,336,417,348);
  316.         sprintf(str,"%d ",drawmult);
  317.         outtextxy(400,336,str);
  318.        }
  319.       if (plottype)
  320.        { /* if on neuron activity plots, switch back to graphs */
  321.         bar(480,1,638,329); /* first blank entire block */
  322.         plottype = 0;
  323.         graphsetup();
  324.        }
  325.  
  326.       /* write code and version to file */
  327.       i = PLAYBUG;
  328.       fwrite(&i,2,1,recfile);
  329.       i = VERSION;
  330.       fwrite(&i,2,1,recfile);
  331.  
  332.       /* write initial data to file */
  333.       for (i=0; i<NFOOD; i++)
  334.        rin.foodr[i] = 0;
  335.       for (i=0; i<nfood; i++)
  336.        {
  337.         rin.foodr[i] = foodr[i];
  338.         rin.foodx[i] = foodx[i]/2;
  339.         rin.foody[i] = ((long)AspR * foody[i])/200L;
  340.        }
  341.       rin.nblock = nblock;
  342.       for (i=0; i<nblock; i++)
  343.        {
  344.         rin.blockx[i] = blockx[i]/2;
  345.         rin.blocky[i] = ((long)AspR * blocky[i])/200L;
  346.        }
  347.       fwrite(&rin.foodr[0],sizeof(struct recinit),1,recfile);
  348.  
  349.       /* write graph data to file */
  350.       for (i=0; i<5; i++)
  351.        {
  352.         rgr[i].gtype = gr[i].gtype;
  353.         strcpy(rgr[i].gneur,gr[i].gneur);
  354.         rgr[i].gvar = gr[i].gvar;
  355.         rgr[i].gscale = rgr[i].gscale;
  356.        }
  357.       fwrite(rgr,sizeof(struct recgraph),5,recfile);
  358.  
  359.       reccnt = 0; /* initialize records */
  360.       for (i=0; i<NREC; i++)
  361.        {
  362.         rc[i].footst = 0;
  363.         rc[i].mouthfood = 0;
  364.        }
  365.       if (Iextgr >= 0 && gr[Iextgr].gtype == 1)
  366.        savIext = TRUE;
  367.       else
  368.        savIext = FALSE;
  369.      }
  370.        }
  371.      }
  372.    }
  373.  }
  374.  
  375. int near multstep(int *step,long *time,FILE *recfile,int *savIext)
  376.  {
  377.   int i;
  378.   enum asc_val asc;
  379.  
  380.   while (TRUE)
  381.    {
  382.     if (singstep(step,time,recfile,savIext))
  383.      return(FALSE);
  384.     i = bioskey(1);
  385.     if (i)
  386.      {
  387.       bioskey(0);
  388.       asc = i & 0xff;
  389.       if (asc == ESC)
  390.        return(TRUE);
  391.       if (asc == SPACE)
  392.        return(FALSE);
  393.      }
  394.    }
  395.  }
  396.  
  397. int near singstep(int *step,long *time,FILE *recfile,int *savIext)
  398.  {
  399.   int y,j,i,k;
  400.   char str[6],c;
  401.   struct neuron *np;
  402.   struct neurcalc *ncp;
  403.  
  404.   if (update())
  405.    goto stoprec;
  406.   (*step)++;
  407.   if (!(*step%2))
  408.    { /* graph */
  409.     if (!plottype)
  410.      {
  411.       if (recnum && (*step%(2*drawmult)))
  412.        rc[reccnt].graphx = *time/10; /* record x plot for graph */
  413.       for (j=0; j<5; j++) /* plot graph points */
  414.        {
  415.     if (gr[j].gtype == 1)
  416.      { /* variable */
  417.       if (gr[j].gvar == 1)
  418.        { /* F, 0 to 1 */
  419.         y = 52 - (nsc+gr[j].g)->F/200;
  420.        }
  421.       else
  422.        {
  423.         if (gr[j].gvar == 0)
  424.          y = 28 - (((nsc+gr[j].g)->V)/40)/gr[j].gscale;
  425.         else
  426.          y = 28 - (((nsc+gr[j].g)->I)/400)/gr[j].gscale;
  427.        }
  428.       if (y < 3)
  429.        y = 3;
  430.       else
  431.        if (y > 52)
  432.         y = 52;
  433.       putpixel(538 + *time/10,y + 66*j,15);
  434.       if (recnum)
  435.        { /* record y plot for graph */
  436.         if ((*step%(2*drawmult)))
  437.          rc[reccnt].graphy1[j] = y;
  438.         else
  439.          rc[reccnt].graphy2[j] = y;
  440.            }
  441.      }
  442.     else
  443.      if (gr[j].gtype == 2)
  444.       { /* step */
  445.        for (i=0; i<6; i++)
  446.         {
  447.          if (!bug.foot[i])
  448.           putpixel(538 + (*time)/10,49-i*9+66*j,15);
  449.         }
  450.       }
  451.        }
  452.      }
  453.     else
  454.      { /* plot neuron activity */
  455.       i = 0;
  456.       k = 0;
  457.       for (j=0, np=ns, ncp=nsc; j<nn; j++,np++,ncp++)
  458.        {
  459.     if (np->name[0] != 0) /* if defined */
  460.      {
  461.       if (ncp->F == 0) /* set color depending on frequency */
  462.        setcolor(1);
  463.       else
  464.        if (ncp->F < 3300)
  465.         setcolor(3);
  466.        else
  467.         if (ncp->F < 6600)
  468.          setcolor(5);
  469.         else
  470.          setcolor(7);
  471.       outtextxy(483 + i*52,1 + k*8,np->name);
  472.       if (k < 39)
  473.        k++;
  474.       else
  475.        {
  476.         if (i < 2)
  477.          {
  478.           k = 0;
  479.           i++;
  480.          }
  481.         else
  482.          break;
  483.        }
  484.      }
  485.        }
  486.      }
  487.     if (!(*step%(2*drawmult)))
  488.      { /* draw bug */
  489.       setviewport(1,1,478,329,1);
  490.       for (j=0; j<nfood; j++)
  491.        {
  492.     if (ofoodr[j] != foodr[j])
  493.      y = 4;
  494.     else
  495.      y = 1;
  496.     if (mouth || y == 4)
  497.      {
  498.       setcolor(0);
  499.       circle(foodx[j]/2, (long)AspR * foody[j]/200L, ofoodr[j]);
  500.       ofoodr[j] = foodr[j];
  501.       setcolor(7);
  502.       circle(foodx[j]/2, (long)AspR * foody[j]/200L, foodr[j]);
  503.       if (recnum)
  504.        { /* note to redraw or reduce circle */
  505.         c = (1 << (j+y));
  506.         rc[reccnt].mouthfood = rc[reccnt].mouthfood | c;
  507.        }
  508.      }
  509.        }
  510.       setviewport(1,1,478,329,0);
  511.       showbug(obug,0); /* erase old bug */
  512.       obug = bug;
  513.       drawbug(bug,15);
  514.       setviewport(0,0,639,349,0);
  515.       bar(422,336,454,348);
  516.       sprintf(str,"%d",(int)(energy/10000L));
  517.       setcolor(3);
  518.       outtextxy(422,336,str);
  519.       setcolor(7);
  520.       if (recnum)
  521.        {
  522.     if (*savIext)
  523.      {
  524.       strcpy(rc[reccnt].Iextname,gr[Iextgr].gneur);
  525.       rc[reccnt].Iext = (Iextint+500L*sgn(Iextint))/1000L;
  526.       *savIext = FALSE;
  527.      }
  528.     else
  529.      rc[reccnt].Iextname[0] = 0;
  530.     reccnt++;
  531.     if (reccnt == NREC)
  532.      { /* write to file */
  533.       if (fwrite(rc,sizeof(struct recbug),NREC,recfile) != NREC)
  534.        { /* disk full - close file & stop recording */
  535. stoprec:    fclose(recfile);
  536.         recnum = 0;
  537.         setcolor(3);
  538.         bar(226,336,370,348);
  539.         outtextxy(226,336,"Menu  Tab:ChgPlots");
  540.         return(TRUE);
  541.        }
  542.       reccnt = 0;
  543.       for (i=0; i<NREC; i++)
  544.        {
  545.         rc[i].footst = 0;
  546.         rc[i].mouthfood = 0;
  547.        }
  548.      }
  549.        }
  550.      }
  551.     if (!(*step%200) && !plottype)
  552.      { /* reset graphs */
  553.       *time = -DT;
  554.       for (j=0; j<5; j++)
  555.        {
  556.     if (gr[j].gtype)
  557.      {
  558.       bar(538,3+j*66,638,52+j*66);
  559.       setcolor(3);
  560.       line(547,3+j*66,547,55+j*66); /*graph x-axis ticks */
  561.       line(557,3+j*66,557,55+j*66);
  562.       line(567,3+j*66,567,55+j*66);
  563.       line(577,3+j*66,577,55+j*66);
  564.       line(587,3+j*66,587,56+j*66);
  565.       line(597,3+j*66,597,55+j*66);
  566.       line(607,3+j*66,607,55+j*66);
  567.       line(617,3+j*66,617,55+j*66);
  568.       line(627,3+j*66,627,55+j*66);
  569.       line(637,3+j*66,637,56+j*66);
  570.       setcolor(15);
  571.      }
  572.        }
  573.      }
  574.    }
  575.   *time += DT;
  576.   return(0);
  577.  }
  578.  
  579.