home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / BARS.C next >
C/C++ Source or Header  |  1992-09-11  |  14KB  |  453 lines

  1. /* bars.c  27-3-92  bar display for the Beagle Explorer */
  2. /*** Beagle Explorer: Version 3.1  Copyright (c) 1990, 1991, 1992  Tom Ray ***/
  3.  
  4. #include "beagle.h"
  5. #include "externb.h"
  6.  
  7. struct sbar  sb;
  8. struct barst  bs = { 24., 16., 14., 4., 10., 10., 40., 8., 5., 79. } ;
  9. struct species  sp[55];
  10.  
  11. int    num_sp, pop, inst, gent, genotypes;
  12. int    update, view, species, barcolor, maxcolor, udisplay;
  13. float  max_max, bxln;
  14. double instructions;
  15. char   xaxis[5], ifile[13];
  16. HWND         whba, whinb;
  17. TRANSACTION  *ptba;
  18.  
  19. extern char  directory[], info_file[];
  20. extern FILE  *info;
  21. extern struct last_out  lo;
  22.  
  23. int bars(void)
  24. {   int  i, rows;
  25.     static int    first = 1;
  26.     HWND  whins;
  27.  
  28.     if(first)
  29.     {   first = 0;
  30. /*      strcpy(ifile,"tierra.run"); */
  31.         strcpy(ifile,"break.1");
  32.         bs.nbr = 24.;
  33.         genotypes = 1;
  34.         udisplay = 1;
  35.         view = 0;
  36.     }
  37.     sprintf(xaxis,"inst");
  38.     if(y_res == 480) rows = 7; else rows = 6;
  39.     while(1) {
  40.     whba = wigen(3,1,rows,54,FRDOUBLE,"bars");
  41.     ptba = vpreload(rows,0,NULLF,NULLF,NULLF,NULLF);
  42.     for(i = 0; i < rows; i++) (*(ptba->def))[i].use_ext = TRUE;
  43.     (*(ptba->def))[3].refresh = ON;
  44.     vdeffld(ptba,0,STRING,0,1, "directory  =",-1,39,NULL,directory,NO);
  45.     vdeffld(ptba,1,STRING,1,1, "input file =",-1,12,NULL,ifile,NO);
  46.     vdeffld(ptba,2,STRING,2,1, "x-axis (pop,inst)  =",-1,4,NULL,xaxis,NO);
  47.     vdeffld(ptba,3,BOOLEAN,3,1,"separate genotypes =",-1,1,NULL,&genotypes,NO);
  48.     vdeffld(ptba,4,BOOLEAN,4,1,"update display     =",-1,1,NULL,&udisplay,NO);
  49.     vdeffld(ptba,5,BOOLEAN,5,1,"view run_info      =",-1,1,NULL,&view,NO);
  50.     vdeffld(ptba,6,DOUBLE,6,1, "num bars (24,55)   =",-1,2,NULL, &bs.nbr,NO);
  51.     whins = wiinv(23,13,
  52.         "\x1e\x1f, or Enter to select; Esc to exit; Ctrl-Enter to run");
  53.     visible(whba,YES,YES);
  54.     ptba->fld_valfn = run_info_chk2;
  55.     ptba->fld_valid = run_info2;
  56.     if(DWSUCCESS == vread(ptba,whba,YES))
  57.     {   vdelete(whinb,whba); vdelete(whba,NONE); vdelete(whins,NONE);
  58.         bebar();
  59.     }
  60.     else
  61.     {   vdelete(whinb,whba); vdelete(whba,NONE); vdelete(whins,NONE);
  62.         return 1;
  63.     }
  64.     }
  65.     vdelete(whins,NONE);
  66.     return 1;
  67. }
  68.  
  69. int run_info_chk2(TRANSACTION  *tp)
  70. {   char  path[80], dpath[80];
  71.     int  success;
  72.     FILE  *inf;
  73.  
  74.     if(tp->cur_fld == 0)
  75.     {   getcwd(path,79);
  76.         success = chdir(directory);
  77.         chdir(path);
  78.         if(success)
  79.         {   wierror(2,45,"directory not found");
  80.             return FALSE;
  81.         }
  82.         return TRUE;
  83.     }
  84.     if(tp->cur_fld == 1)
  85.     {   sprintf(dpath,"%s/%s", directory, ifile);
  86.         inf = fopen(dpath,"r");
  87.         if(inf == NULL)
  88.         {   wierror(2,45,"input file not found");
  89.             return FALSE;
  90.         }
  91.         fclose(inf);
  92.         return TRUE;
  93.     }
  94.     if(tp->cur_fld == 2)
  95.     {   if(!strcmp(xaxis,"pop") || !strcmp(xaxis,"inst"))
  96.             return TRUE;
  97.         wierror(2,45,"pop or inst please");
  98.         return FALSE;
  99.     }
  100.     if(tp->cur_fld == 6)
  101.     {   if(bs.nbr <= 55)
  102.             return TRUE;
  103.         wierror(2,45,"value must be <= 55");
  104.         return FALSE;
  105.     }
  106.     return TRUE;
  107. }
  108.  
  109. void run_info2(TRANSACTION  *tp)
  110. {   int     i, row_siz_phy, tnum_sp;
  111.     static int  first = 1;
  112.     char  data[81];
  113.  
  114.     if(!strcmp(xaxis,"pop")) { pop = 1; inst = 0; }
  115.     if(!strcmp(xaxis,"inst")) { pop = 0; inst = 1; }
  116.     if(tp->cur_fld > 3 || !view) return ;
  117.     if(genotypes)
  118.     {   sprintf(data,"%s/run_info.g",directory);
  119.         info = fopen(data,"r");
  120.         if(info == NULL)
  121.         {   sprintf(data,"%s/run_info",directory);
  122.             info = fopen(data,"r");
  123.             if(info == NULL)
  124.             {   wierror(2,45,"run_info file not found");
  125.                 return ;
  126.             }
  127.             else genotypes = 0;
  128.         }
  129.     }
  130.     else
  131.     {   sprintf(data,"%s/run_info",directory);
  132.         info = fopen(data,"r");
  133.         if(info == NULL)
  134.         {   wierror(2,45,"run_info file not found");
  135.             return ;
  136.         }
  137.     }
  138.     barst_setup();
  139.     species_array_setup(&tnum_sp);
  140.     species = tnum_sp;
  141.     fclose(info);
  142.     qsort(sp, num_sp, sizeof(struct species), spcmp3);
  143.     if(num_sp > 23) row_siz_phy = 24; else row_siz_phy = num_sp + 1;
  144.     if(first) first = 0;
  145.     else vdelete(whinb,whba);
  146.     whinb = wigenlp(1,56,num_sp + 1,24,row_siz_phy,24,FRSINGLE,"run info");
  147.     visible(whinb,YES,NO);
  148.     vatputf(whinb,0,1,"species = %i", species);
  149.     for(i = 0; i < num_sp; i++)
  150.     {   vatputf(whinb,i + 1,1,"%5d", sp[i].size);
  151.         if(genotypes) vatputf(whinb,i + 1,6,"%s", sp[i].label);
  152.         if(pop) vatputf(whinb,i + 1,10,"%u", sp[i].max);
  153.         if(inst) vatputf(whinb,i + 1,10,"%ld", sp[i].min);
  154.     }
  155.     visible(whba,YES,YES);
  156. }
  157.  
  158. void species_array_setup(int  *tnum_sp) /* update the species array */
  159. {   char    data[100];
  160.     int     i, species;
  161.  
  162.     for(i = 0; i < bs.nbr; i++)
  163.     {   sp[i].size = sp[i].num = sp[i].max = sp[i].xpm = 0;
  164.         sp[i].min = (long) 0;
  165.         sprintf(sp[i].label,"---");
  166.     }
  167.     if(udisplay) fgets(data,99,info);
  168.     else get_total(data);
  169.     sscanf(data,"%*s%*s%d%*s%*s%lu%*s%*s%lu%*s%*s%d",
  170.         &species, &lo.ctime, &lo.time, &update);
  171.     *tnum_sp = species;
  172.     fgets(data,99,info);
  173.     if(species > bs.nbr) num_sp = bs.nbr; else num_sp = species;
  174.     if(species > 55) species = 55;
  175.     for(i = 0; i < species; i++)
  176.     {   fgets(data,99,info);
  177.         sscanf(data,"%d%s%*d%d%*d%d",
  178.             &sp[i].size, sp[i].label, &sp[i].max, &sp[i].num);
  179.         sp[i].min = (long) sp[i].size * (long) sp[i].max;
  180.     }
  181.     max_max = 0.;
  182.     for(i = 0; i < num_sp; i++)
  183.     {   if(pop) if(sp[i].max > max_max) max_max = (float) sp[i].max;
  184.         if(inst) if(sp[i].min > max_max) max_max = (float) sp[i].min;
  185.     }
  186. }
  187.  
  188. void get_total(char  *data) /* read to total update at end of run_info file */
  189. {   char  word[80];
  190.  
  191.     do
  192.     {   fgets(data,99,info);
  193.         sscanf(data,"%s%*s%*d%*s%*s%*lu%*s%*s%*lu%*s%*s%d", word, &update);
  194.     }   while(strcmp(word,"num_sp") || update);
  195. }
  196.  
  197. void bars_update(void) /* this sets up all bars, after a species list update */
  198. {   int  i, xp;
  199.  
  200.     for(i = 0; i < num_sp; i++)
  201.     {   if(sp[i].num <= 0) continue;
  202.         if(pop) xp = (int) bs.bas + bxln * sp[i].num;
  203.         if(inst) xp = (int) bs.bas + bxln * sp[i].num * sp[i].size;
  204.         sb.top    = i * (bs.bwd + bs.sbb) + bs.thd;
  205.         sb.bottom = sb.top + bs.bwd - 1;
  206.         setfillstyle(SOLID_FILL,barcolor);
  207.         sb.left   = sp[i].xpo;
  208.         sb.right  = xp - 1;
  209.         bar(sb.left, sb.top, sb.right, sb.bottom);
  210.         sp[i].xpo = xp;
  211.         if(xp > sp[i].xpm)
  212.         {   sp[i].xpm = xp;
  213.             setfillstyle(SOLID_FILL,maxcolor);
  214.             bar(xp, sb.top, xp, sb.bottom);
  215.             setfillstyle(SOLID_FILL,barcolor);
  216.         }
  217.     }
  218. }
  219.  
  220. void barst_setup(void) /* this sets up the bars structure */
  221. {   if(y_res == 480 && bs.nbr <= 24)
  222.     {   bs.csz = 16.; bs.bwd = 14.; bs.sbb = 4.; bs.lbd = 10.;
  223.         bs.tbd = 10.; bs.thd = 40.; bs.inb = 5.;
  224.         if(genotypes) bs.nge = 3.; else bs.nge = 0.;
  225.     }
  226.     if(y_res == 480 && bs.nbr > 24)
  227.     {   bs.csz = 8.; bs.bwd = 7.;  bs.sbb = 1.; bs.lbd = 10.;
  228.         bs.tbd = 5.; bs.thd = 30.; bs.inb = 5.;
  229.         if(genotypes) bs.nge = 3.; else bs.nge = 0.;
  230.     }
  231.     if(y_res == 200)
  232.     {   bs.csz = 8.; bs.bwd = 7.;  bs.sbb = 1.; bs.lbd = 10.;
  233.         bs.tbd = 5.; bs.thd = 15.; bs.inb = 5.;
  234.         if(genotypes) bs.nge = 3.; else bs.nge = 0.;
  235.     }
  236. }
  237.  
  238. int spcmp2(const void  *s1, const void  *s2)
  239. {   struct species  *sp1 = s1, *sp2 = s2;
  240.  
  241.     if(sp2->size > sp1->size) return 1;
  242.     if(sp2->size < sp1->size) return -1;
  243.     if(sp2->size == sp1->size)
  244.     {   if(!strcmp(sp2->label,"---")) return -1;
  245.         if(!strcmp(sp1->label,"---")) return 1;
  246.         return -strcmp(sp2->label,sp1->label);
  247.     }
  248.     return 0;
  249. }
  250.  
  251. int spcmp3(const void  *s1, const void  *s2)
  252. {   struct species  *sp1 = s1, *sp2 = s2;
  253.  
  254.     if(pop) return sp2->max - sp1->max;
  255.     if(inst)
  256.     {   if(sp2->min > sp1->min) return 1;
  257.         if(sp2->min < sp1->min) return -1;
  258.         if(sp2->min == sp1->min) return 0;
  259.     }
  260.     return 0;
  261. }
  262.  
  263. void do_bupdate(void) /* this updates the species list and scales */
  264. {   char  data[81];
  265.     int  i, tnum_sp;
  266.  
  267.     species_array_setup(&tnum_sp);
  268.     qsort(sp, num_sp, sizeof(struct species), spcmp2);
  269.     itoa(sp[0].size,data,10);
  270.     bs.nsz = strlen(data);
  271.     bs.nch = bs.nsz + bs.nge;
  272.     bs.bas = bs.lbd + (bs.nch * bs.csz) + bs.inb;
  273.     for(i = 0; i < bs.nbr; i++)
  274.         sp[i].xpo = (int) bs.bas;
  275.     if(y_res == 480) settextstyle(0,0,2);
  276.     if(y_res == 200) settextstyle(0,0,1);
  277.     cleardevice();
  278.     sprintf(data,"T =");
  279.     outtextxy(bs.lbd, bs.tbd, data);
  280.     time_update();
  281.     if(pop) sprintf(data,"N_max = %i", (int) max_max);
  282.     if(inst) sprintf(data,"I_max = %li", (long) max_max);
  283.     outtextxy(350, bs.tbd, data);
  284.     if(y_res == 480 && bs.nbr > 24) settextstyle(0,0,1);
  285.     for(i = 0; i < num_sp; i++) /* this makes the list of size labels */
  286.     {   if(genotypes) sprintf(data,"%*i%s",
  287.             (int) bs.nsz, sp[i].size, sp[i].label);
  288.         else sprintf(data,"%*i", (int) bs.nsz, sp[i].size);
  289.         outtextxy(bs.lbd, i * (bs.bwd + bs.sbb) + bs.thd, data);
  290.     }
  291.     bxln = (x_res - 4. - bs.bas) / max_max;
  292. }
  293.  
  294. void time_update(void) /* this updates the time display */
  295. {   int  csz, bwd;
  296.     char  data[81];
  297.  
  298.     if(y_res == 480)
  299.     {   settextstyle(0,0,2);
  300.         csz = 16; bwd = 14;
  301.     }
  302.     else { csz = 8; bwd = 7; }
  303.     setfillstyle(SOLID_FILL,0);
  304.     sb.left = (4 * csz) + bs.lbd;
  305.     sb.top = bs.tbd;
  306.     sb.right = (4 * csz) + sb.left;
  307.     sb.bottom = sb.top + bwd - 1;
  308.     bar(sb.left, sb.top, sb.right, sb.bottom);
  309.     sprintf(data,"%i", (int) lo.ctime);
  310.     outtextxy(sb.left, sb.top, data);
  311. }
  312.  
  313. void bebar(void)
  314. {   Ulong  otime = 0;
  315.     Uint   c;
  316.     FILE   *inf;
  317.     int    i, in, xp, found, firstr = 1, first = 1, upcount, tnum_sp;
  318.     int    binum = 1;
  319.     char   data[81];
  320.     char   bifile[13];
  321.  
  322.     if(genotypes)
  323.     {   sprintf(data,"%s/run_info.g",directory);
  324.         info = fopen(data,"r");
  325.         if(info == NULL)
  326.         {   sprintf(data,"%s/run_info",directory);
  327.             info = fopen(data,"r");
  328.             if(info == NULL)
  329.             {   wierror(2,45,"run_info file not found");
  330.                 return ;
  331.             }
  332.             else genotypes = 0;
  333.         }
  334.     }
  335.     else
  336.     {   sprintf(data,"%s/run_info",directory);
  337.         info = fopen(data,"r");
  338.         if(info == NULL)
  339.         {   wierror(2,45,"run_info file not found");
  340.             return ;
  341.         }
  342.     }
  343.     sscanf(ifile,"%[^.]", bifile);
  344.     sprintf(data,"%s/%s", directory, ifile);
  345.     inf = fopen(data,"r");
  346.     if(inf == NULL)
  347.     {   wierror(2,45,"run file not found");
  348.         return ;
  349.     }
  350.     barst_setup();
  351.     setgraphmode(graphics_mode);
  352.     maxcolor = barcolor = getmaxcolor();
  353.     if(barcolor > 1) barcolor--;
  354.     if(y_res == 480) settextstyle(0,0,2);
  355.     if(y_res == 200) settextstyle(0,0,1);
  356.     sprintf(data,"T = 0");
  357.     outtextxy(bs.lbd, bs.tbd, data);
  358.     do_bupdate();
  359.     upcount = update;
  360.     for(;;)
  361.     {   if(fgets(data,80,inf) == NULL)
  362.         {   fclose(inf);
  363.             binum++;
  364.             sprintf(data,"%s/%s.%d", directory, bifile, binum);
  365.             inf = fopen(data,"r");
  366.             if(inf == NULL)
  367.                 break ;
  368.             if(fgets(data,80,inf) == NULL)
  369.                 break ;
  370.         }
  371.         if(!t_read(data, &lo, &first, &gent))
  372.         {   sscanf(data,"%*s%*s%d%*s%*s%lu%*s%*s%lu",
  373.                 &tnum_sp, &lo.ctime, &lo.time);
  374.             for(i = 0; i < tnum_sp; i++) fgets(data,80,inf);
  375.             while(udisplay && lo.ctime >= upcount) upcount += update;
  376.             if(firstr)
  377.                 firstr = 0;
  378.             else if(udisplay)
  379.             {   do_bupdate();
  380.                 bars_update();
  381.             }
  382.             continue;
  383.         }
  384.         firstr = 0;
  385.         if(lo.ctime > otime)         /* this updates the time */
  386.         {   time_update();
  387.             otime = lo.ctime;
  388.         }
  389.         if(udisplay && lo.ctime >= upcount)
  390.         {   upcount += update;
  391.             do_bupdate();
  392.             bars_update();
  393.         }
  394.         found = 0;
  395.         for(i = 0; i < num_sp; i++)
  396.             if(genotypes)
  397.             {   if(sp[i].size == lo.size && !strcmp(sp[i].label,lo.label))
  398.                 {   in = i; found = 1; break; }
  399.             }
  400.             else
  401.             if(sp[i].size == lo.size) { in = i; found = 1; break; }
  402.         if(found)
  403.         {   if(lo.bd == 'b') sp[in].num++;
  404.             if(lo.bd == 'd') sp[in].num--;
  405.             if(pop) xp = (int) bs.bas + bxln * sp[in].num;
  406.             if(inst) xp = (int) bs.bas + bxln * sp[in].num * sp[in].size;
  407.             sb.top    = in * (bs.bwd + bs.sbb) + bs.thd;
  408.             sb.bottom = sb.top + bs.bwd - 1;
  409.             if(xp > sp[in].xpo) /* the bar gets larger */
  410.             {   setfillstyle(SOLID_FILL,barcolor);
  411.                 sb.left   = sp[in].xpo;
  412.                 sb.right  = xp - 1;
  413.                 bar(sb.left, sb.top, sb.right, sb.bottom);
  414.                 sp[in].xpo = xp;
  415.                 if(xp > sp[in].xpm)
  416.                 {   sp[in].xpm = xp;
  417.                     setfillstyle(SOLID_FILL,maxcolor);
  418.                     bar(xp, sb.top, xp, sb.bottom);
  419.                     setfillstyle(SOLID_FILL,barcolor);
  420.                 }
  421.             }
  422.             if(xp < sp[in].xpo) /* the bar gets smaller */
  423.             {   setfillstyle(SOLID_FILL,0);
  424.                 sb.left   = xp;
  425.                 sb.right  = sp[in].xpo - 1;
  426.                 bar(sb.left, sb.top, sb.right, sb.bottom);
  427.                 sp[in].xpo = xp;
  428.             }
  429.         }
  430.         if(gfkbhit())
  431.         {   c = getkey();
  432.             if(c == 'p' || c == 'P') getkey();  /* P or p key */
  433.             if(c == ESC) break ;                /* Esc key */
  434.             if(c == 'g' || c == 'G')            /* G or g key */
  435.                 DumpScreen2Gif("image.gif",VGA,VGAHI,VGAHI,VGAHI);
  436.         }
  437.     }
  438.     fclose(inf);
  439.     fclose(info);
  440.     if(scr)
  441.     {   vbeep();
  442.         while(1)
  443.         {   if(gfkbhit())
  444.             {   c = getkey();
  445.                 if(c == ESC) break ;                /* Esc key */
  446.                 if(c == 'g' || c == 'G')            /* G or g key */
  447.                     DumpScreen2Gif("image.gif",VGA,VGAHI,VGAHI,VGAHI);
  448.             }
  449.         }
  450.         restorecrtmode();
  451.     }
  452. }
  453.