v07i078: Whales and Plankton, part 05/13
Brandon S. Allbery - comp.sources.misc
allbery at uunet.UU.NET
Wed Jul 19 11:37:06 AEST 1989
Posting-number: Volume 7, Issue 78
Submitted-by: loy at gtx.UUCP (Bob Loy)
Archive-name: whpl/part05
# whpl05of13.shar
#---cut here---cut here---cut here---cut here---cut here---
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files.
# This archive created: Sat Jan 14 04:04:27 MST 1989
export PATH; PATH=/bin:$PATH
echo shar: extracting "'whio.c3'" '(27590 characters)'
if test -f 'whio.c3'
then
echo shar: will not over-write existing file "'whio.c3'"
else
sed 's/^X//' << \SHAR_EOF > 'whio.c3'
X
Xvoid
Xlastuff(interm)
X short interm;
X{
X register int h, j, k;
X energarr *enerlist;
X int diffic = 0;
X int prcntl = 0;
X double lgn;
X double scaledif;
X double root;
X int rnk;
X int thelast = 0;
X int nxtulast;
X int sumrat;
X
X enerlist = (energarr *) calloc((numwh), (sizeof(energarr)));
X if (enerlist == NULL)
X {
X if (debug >= 2)
X fprintf(stderr, "\nlastuff(): enerlist calloc problems.\n");
X exit(8);
X }
X for (j = 0; j < numwh; j++)
X {
X enerlist[j].ener = whale[j].energy;
X enerlist[j].whal = j;
X diffic += whale[j].ancesave;
X }
X diffic = diffic / numwh;
X qsort((char*)enerlist, (numwh), sizeof(energarr), qlohi);
X for (j = 0; j < numwh; j++)
X {
X /* If interim file dump, only process dead whales */
X whale[j].enrec[whale[j].endex] = whale[j].energy;
X if (!creonlyflg)
X whale[j].endex++;
X if (whale[j].endex >= 16 && whale[j].endex < 20)
X if (debug >= 2)
X fprintf(stderr,"lastuff(): wh[%d].enrec array 80%% FULL\n", j);
X if (whale[j].endex >= 20)
X {
X if (debug >= 2)
X fprintf(stderr,"lastuff(): wh[%d].enrec array is FULL\n", j);
X if (debug >= 6)
X fprintf(stdout,"lastuff(): wh[%d].enrec array is FULL\n", j);
X if (endexwarn)
X if (debug >= 1)
X {
X fprintf(stderr,
X " wh[%d].enrec[19] may have been\n", j);
X fprintf(stderr,
X " *** OVERWRITTEN ***\n");
X fprintf(stdout,
X " wh[%d].enrec[19] may have been\n", j);
X fprintf(stdout,
X " *** OVERWRITTEN ***\n");
X }
X }
X sumrat = 0;
X k = 0;
X while (k <= 20)
X {
X if (whale[j].comp[k].diff == 0)
X break;
X k++;
X }
X if (k >= 15 && k < 19)
X if (debug >= 2)
X fprintf(stderr,
X "lastuff(): wh[%d].comp[%d] array 80%% FULL\n", j, k);
X if (k == 19)
X {
X if (debug >= 2)
X fprintf(stderr,
X "lastuff(): wh[%d].comp[%d] : ARRAY IS FULL\n", j, k);
X if (debug >= 6)
X fprintf(stdout,
X "lastuff(): wh[%d].comp[%d] : ARRAY IS FULL\n", j, k);
X }
X if (k >= 20)
X {
X if (debug >= 2)
X {
X fprintf(stderr,"lastuff(): wh[%d].comp[19] array is FULL\n", j);
X fprintf(stderr, " *** OVERWRITTEN ***\n");
X }
X if (debug >= 6)
X {
X fprintf(stdout,"lastuff(): wh[%d].comp[19] array is FULL\n", j);
X fprintf(stdout, " *** OVERWRITTEN ***\n");
X }
X k--;
X }
X whale[j].comp[k].diff = (short) diffic;
X if (creonlyflg)
X whale[j].comp[k].totl = 0;
X else
X whale[j].comp[k].totl = numwh;
X /* Dead whales get rank of 125 */
X if (whale[j].energy < 10000)
X whale[j].comp[k].rank = 125;
X else
X {
X for (h = 0; h <= numwh; h++)
X {
X if (enerlist[h].whal == j)
X break;
X }
X if (h == numwh)
X if (debug >= 2)
X fprintf(stderr, "lastuff(): whale[%d] not on enerlist\n", j);
X prcntl = (short) (1000 * (h + 1) / numwh);
X if (creonlyflg)
X whale[j].comp[k].rank = 0;
X else
X {
X whale[j].comp[k].rank = prcntl;
X /* Live whales get rank of at least 250 */
X if (prcntl < 250)
X whale[j].comp[k].rank = 250;
X }
X }
X for (h = 0; h <= k; h++)
X {
X nxtulast = thelast;
X rnk = whale[j].comp[h].rank;
X scaledif = (double) whale[j].comp[h].diff;
X scaledif /= 1000.0;
X lgn = log(scaledif + 1.0);
X /* Atlernate attempt:
X root = sqrt( (double) whale[j].comp[h].diff);
X sumrat += rnk * (int) root; */
X thelast = rnk * (int) (lgn * 1000.0);
X sumrat += thelast;
X }
X if (h == 1)
X nxtulast = thelast;
X whale[j].lastrat = thelast;
X sumrat += nxtulast;
X sumrat += thelast;
X sumrat += thelast;
X sumrat /= k + 4;
X whale[j].rating = sumrat;
X if (whale[j].allchldn > 3)
X whale[j].offratio = 1000 * whale[j].allchldn / whale[j].age;
X else
X whale[j].offratio = -1;
X }
X if (debug >= 9)
X {
X fprintf(stdout, "\n");
X fprintf(stdout, " STATFILE:\n");
X fprintf(stdout,
X " energy, ansave, orat, age, die, whale\n");
X for (j = numwh - 1; j >= 0; j--)
X {
X fprintf(stdout, " %7d %7d %5d %3d %3d ",
X enerlist[j].ener,
X whale[enerlist[j].whal].ancesave,
X whale[enerlist[j].whal].offratio,
X whale[enerlist[j].whal].age,
X whale[enerlist[j].whal].die);
X whaleout(enerlist[j].whal, stdout, 1);
X }
X }
X if (!creonlyflg)
X statout(enerlist, interm);
X free((char*) enerlist);
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void whaleinit() - Create and load the whale structures, either freshly
X@@ created, or filled from input file.
X@ input : Global command line flags: infileflg, eofflg, liveflg, readflg,
X@ addflg, creonlyflg
X@ caller: main(), look()
X@ calls : whaleload(); fopen(), fclose(), malloc(), realloc(),
X@ may call exit()
X@ proc : Depends heavily on command line flags. See examples at top of
X@ file whio.c.
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xwhaleinit()
X{
X int j;
X short numinfile = 0;
X short badfile = 0;
X short foundin = 0;
X char *test = whinstr;
X long tell;
X char live[2];
X
X created = numwh; /* Assume all whales will be new for time being */
X /* If only creating new whales, set numwh & supress most file flags */
X if (creonlyflg)
X {
X numwh = creonlyflg;
X created = numwh;
X infileflg = FALSE;
X eofflg = FALSE;
X readflg = 0;
X }
X /* Read input whale file */
X if (infileflg)
X {
X created = 0; /* If a file read, created is only increased by addflg */
X if ((whin = fopen(whinfile, "r")) != NULL)
X {
X if (eofflg) /* Read whole file if eofflg set */
X readflg = MAXSHORT;
X while ((test = fgets(whinstr, 97, whin)) != NULL)
X {
X if (whinstr[2] == 'I' && whinstr[3] == 'N')
X {
X if (readflg) /* Read number of whales set by cmnd line -n */
X {
X if (debug >= 5 && !eofflg)
X fprintf(stdout,
X "\nWhales to read: %d, to fill: %d\n",
X readflg, readflg + addflg);
X if (numwh != readflg)
X {
X numwh = readflg;
X if (debug >= 5 && !eofflg)
X fprintf(stdout, " numwh being reset to: %d.\n",
X numwh);
X }
X if (eofflg) /* But read whole file if eofflg set */
X numwh = 1;
X foundin++;
X break;
X }
X else /* Otherwise read number of whales from file header */
X {
X fgets(whinstr, 97, whin);
X if (!(whinstr[4] == 'N' && whinstr[5] == 'u'))
X break;
X if (liveflg) /* Read number of live whales */
X {
X fgets(whinstr, 97, whin);
X if (!(whinstr[4] == 'L' && whinstr[5] == 'i'))
X break;
X }
X sscanf(whinstr, "%*s%hd", &numinfile);
X if (debug >= 5)
X fprintf(stdout,
X "\nWhales to read: %d, to fill: %d\n",
X numinfile, numinfile + addflg);
X if (numwh != numinfile)
X {
X numwh = numinfile;
X if (debug >= 5)
X fprintf(stdout,
X " numwh being reset to: %d.\n",
X numwh);
X }
X foundin++;
X break;
X }
X }
X }
X if (test == NULL)
X rewind(whin);
X }
X else
X {
X if (debug >= 2)
X fprintf(stderr, "\n WARNING: did not open input file %s\n\n",
X whinfile);
X badfile++;
X }
X }
X else
X foundin++;
X if (!foundin)
X {
X if (debug >= 2)
X {
X fprintf(stderr, "\n Warning: not a Standard Whale File.\n");
X fprintf(stderr, " Will try to read whales from file anyway.\n\n");
X }
X eofflg = TRUE;
X readflg = MAXSHORT;
X numwh = 1;
X }
X /* Check for too many whales */
X if (numwh > MAXWH)
X {
X if (debug >= 2)
X fprintf(stderr, "\n numwh (%d) begins at > MAXWH (%d)\n",
X numwh, MAXWH);
X }
X /* Make room for whales */
X whale = (whale_type *) malloc(numwh * whale_size);
X if (whale == NULL)
X {
X if (debug >= 2)
X fprintf(stderr, "\nWhale malloc problems.\n");
X exit(9);
X }
X /* Read in whales from file if proper flags set */
X j = 0;
X if (infileflg && !badfile)
X {
X while (fgets(whinstr, 97, whin) != NULL)
X {
X if (whinstr[0] == '#') /* Found a whale */
X {
X tell = ftell(whin);
X fgets(whinstr, 97, whin);
X if (!(whinstr[0] == 'w' && whinstr[1] == 'h'))
X {
X if (debug >= 2)
X fprintf(stderr,
X "\n Warning: not a standard Whale File\n\n");
X exit(20);
X }
X if (readflg) /* Read until readflg is matched */
X if (readflg <= j)
X break;
X if (liveflg) /* Read only live whales */
X {
X fgets(whinstr, 97, whin);
X sscanf(whinstr, "%s%*s%*d%*d%*d", live);
X if (live[0] != 'L')
X continue;
X }
X if (numwh <= j)
X {
X numwh = j + 1;
X /* Make room for more whales */
X whale = (whale_type *) realloc((char *) whale,
X (numwh * whale_size));
X if (whale == NULL)
X {
X if (debug >= 2)
X fprintf(stderr, "\nWhale realloc problems.\n");
X exit(9);
X }
X }
X fseek(whin, tell, 0);
X whaleload(j, 1); /* Initialize whale from whale file recs */
X j++;
X }
X }
X fclose(whin);
X if (j > MAXWH)
X {
X if (debug >= 2)
X fprintf(stderr, "\n Number read from file (%d) > MAXWH (%d)\n",
X j, MAXWH);
X }
X if (j < numwh)
X {
X if (debug >= 5)
X {
X fprintf(stderr, "\nWhales read from file (%d) not equal\n", j);
X fprintf(stderr, " to numwh (%d)\n", numwh);
X fprintf(stderr, " numwh being reset to: %3d.\n", j);
X }
X numwh = j;
X whale = (whale_type *) realloc((char *) whale, (numwh * whale_size));
X if (whale == NULL)
X {
X if (debug >= 2)
X fprintf(stderr, "\nWhale realloc problems.\n");
X exit(9);
X }
X }
X }
X if (eofflg)
X {
X if (debug >= 5)
X fprintf(stdout, "\nWhales read from file: %d\n", j);
X }
X if (addflg < 0 || addflg > 0)
X {
X if (debug >= 5)
X fprintf(stdout, "\nWhales to add: %d\n", addflg);
X if ((addflg + (short)numwh) <= 0)
X {
X if (debug >= 2)
X {
X fprintf(stderr, "addflg subtracts too many whales!\n");
X fprintf(stderr, " numwh would equal: %d.\n\n", numwh + addflg);
X fprintf(stderr, "Program exiting.\n\n");
X }
X exit(2);
X }
X numwh += addflg;
X if (numwh > MAXWH)
X {
X if (debug >= 2)
X fprintf(stderr,
X "\n Number added makes numwh (%d) > MAXWH (%d)\n",
X numwh, MAXWH);
X }
X if (debug >= 5)
X fprintf(stdout, " numwh being reset to: %d.\n", numwh);
X /* Make room for more whales */
X whale = (whale_type *) realloc((char *) whale, (numwh * whale_size));
X if (whale == NULL)
X {
X if (debug >= 2)
X fprintf(stderr, "\nWhale realloc problems.\n");
X exit(9);
X }
X }
X created += addflg; /* addflg whales are always newly created ones */
X if (created < 0)
X created = 0;
X if (debug >= 5)
X fprintf(stdout,
X "\nNumber of whales to be newly created: %d\n\n", created);
X if (numwh > MAXWH)
X {
X if (debug >= 2)
X {
X fprintf(stderr,
X "\nMaximum number of whales (%d) exceeded.\n", MAXWH);
X fprintf(stderr, " Program will proceed, but NO mating will\n");
X fprintf(stderr, " take place until some whales die off.\n\n");
X }
X }
X initwh = truwh = numwh; /* Initialize globals */
X for ( ; j < numwh; j++)
X whaleload(j, 0); /* Create brand new whales */
X /* If only creating brand new whales, write out file and exit here */
X if (creonlyflg && !trakflg)
X {
X if (debug >= 5)
X fprintf(stdout, "New whalefile has been created.\n");
X finish(0);
X }
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void look() - Displays "whale tracks" in eight or sixteen different areas
X@@ on a Sun 3/50 or equivalent (1152 * 900 display).
X@ input : Globals trakflg, viewflg, geneflg, infileflg, kldispflg
X@ output: Movements of numwh whales, eight windows at a time
X@ caller: main()
X@ calls : whaleinit(), printscreen(), movewhale(), countplank(), randout()
X@ proc : Use viewflg to pick which half of screen to display on, then
X@ display the whales (either random or fixed) one at a time.
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xlook()
X{
X whale_type *whalelist;
X int j, k;
X int plank;
X Uint orgwh;
X char genes[6];
X short tempgene[16];
X Uchar xfr;
X short cycle = 0;
X int stop = 8;
X
X startx = 0;
X starty = 0;
X /* If want full screen paint, double number of boxes */
X if (viewflg > 2)
X stop = 16;
X /* If the only important command line opt was -t, then make each whale's
X breed random. Otherwise... */
X /* ...did command line have -g opt, or... */
X if (geneflg)
X {
X numwh = 8;
X if (viewflg > 2)
X numwh = 16;
X }
X /* ...did command line not have -i opt? */
X if (!infileflg)
X {
X creonlyflg = 8;
X if (viewflg > 2)
X creonlyflg = 16;
X }
X /* Initialize all the whales... */
X whaleinit();
X /* ...but set up to only use them one at a time... */
X orgwh = numwh;
X numwh = 1;
X whalelist = whale;
X whale = (whale_type *) malloc(numwh * whale_size);
X if (whale == NULL)
X {
X if (debug >= 2)
X fprintf(stderr, "\nWhale malloc problems.\n");
X exit(9);
X }
X /* ...and now use them */
X do{
X for (j = 0; j < stop; j++) /* For each of the eight 'windows' */
X {
X if (j + cycle >= orgwh)
X break;
X whale = &whalelist[j + cycle];
X if (!geneflg && !infileflg) /* If no geneset or file invoked */
X {
X for (k = 0; k < 2; k++) /* Fill gene types... */
X {
X switch((int)randout() & 0x1F)
X {
X case 0:
X case 1:
X case 2:
X case 3:
X case 4:
X case 5:
X genes[k] = 'a';
X break;
X case 6:
X case 7:
X case 8:
X case 9:
X case 10:
X case 11:
X case 12:
X genes[k] = 'c';
X break;
X case 13:
X case 14:
X case 15:
X case 16:
X case 17:
X case 18:
X case 19:
X genes[k] = 'i';
X break;
X case 20:
X case 21:
X case 22:
X case 23:
X case 24:
X case 25:
X genes[k] = 'm';
X break;
X case 26:
X case 27:
X case 28:
X case 29:
X case 30:
X case 31:
X genes[k] = 'o';
X break;
X default:
X exit(30);
X break;
X }
X }
X whale[0].hunttype = genes[0];
X whale[0].feedtype = genes[1];
X for (k = 2; k < 4; k++) /* ...and fill genes per chromosomes */
X {
X switch((int)randout() & 0x1F)
X {
X case 0:
X case 1:
X case 2:
X case 3:
X case 4:
X genes[k] = '0';
X break;
X case 5:
X case 6:
X case 7:
X case 8:
X case 9:
X genes[k] = '1';
X break;
X case 10:
X case 11:
X case 12:
X case 13:
X case 14:
X case 15:
X genes[k] = '2';
X break;
X case 16:
X case 17:
X case 18:
X case 19:
X case 20:
X genes[k] = '4';
X break;
X case 21:
X case 22:
X case 23:
X case 24:
X case 25:
X case 26:
X genes[k] = '8';
X break;
X case 27:
X case 28:
X case 29:
X case 30:
X case 31:
X genes[k] = '@';
X break;
X default:
X exit(30);
X break;
X }
X }
X whale[0].hgperchr = genes[2];
X whale[0].fgperchr = genes[3];
X }
X /* Paint on left half, or full screen... */
X if (viewflg == 1 || viewflg == 3)
X {
X switch(j)
X {
X case 0:
X startx = 0;
X starty = 0;
X break;
X case 1:
X startx = 0;
X starty = 225;
X break;
X case 2:
X startx = 0;
X starty = 450;
X break;
X case 3:
X startx = 0;
X starty = 675;
X break;
X case 4:
X startx = 288;
X starty = 0;
X break;
X case 5:
X startx = 288;
X starty = 225;
X break;
X case 6:
X startx = 288;
X starty = 450;
X break;
X case 7:
X startx = 288;
X starty = 675;
X break;
X case 8:
X startx = 576;
X starty = 0;
X break;
X case 9:
X startx = 576;
X starty = 225;
X break;
X case 10:
X startx = 576;
X starty = 450;
X break;
X case 11:
X startx = 576;
X starty = 675;
X break;
X case 12:
X startx = 864;
X starty = 0;
X break;
X case 13:
X startx = 864;
X starty = 225;
X break;
X case 14:
X startx = 864;
X starty = 450;
X break;
X case 15:
X startx = 864;
X starty = 675;
X break;
X default:
X break;
X }
X }
X else if (viewflg == 2) /* ...or the right half */
X {
X switch(j)
X {
X case 0:
X startx = 576;
X starty = 0;
X break;
X case 1:
X startx = 576;
X starty = 225;
X break;
X case 2:
X startx = 576;
X starty = 450;
X break;
X case 3:
X startx = 576;
X starty = 675;
X break;
X case 4:
X startx = 864;
X starty = 0;
X break;
X case 5:
X startx = 864;
X starty = 225;
X break;
X case 6:
X startx = 864;
X starty = 450;
X break;
X case 7:
X startx = 864;
X starty = 675;
X break;
X default:
X break;
X }
X }
X /* Copy the genesets (and breeds)... */
X if (trakflg == 3)
X {
X for (k = 0; k < 16; k++)
X whale[0].feedgene[k] = whale[0].huntgene[k];
X whale[0].fgperchr = whale[0].hgperchr;
X whale[0].feedtype = whale[0].hunttype;
X }
X if (trakflg == 4)
X {
X for (k = 0; k < 16; k++)
X whale[0].huntgene[k] = whale[0].feedgene[k];
X whale[0].hgperchr = whale[0].fgperchr;
X whale[0].hunttype = whale[0].feedtype;
X }
X /* ...or else switch them */
X if (trakflg == 2)
X {
X for (k = 0; k < 16; k++)
X {
X tempgene[k] = whale[0].huntgene[k];
X whale[0].huntgene[k] = whale[0].feedgene[k];
X whale[0].feedgene[k] = tempgene[k];
X }
X xfr = whale[0].hgperchr;
X whale[0].hgperchr = whale[0].fgperchr;
X whale[0].fgperchr = xfr;
X xfr = whale[0].hunttype;
X whale[0].hunttype = whale[0].feedtype;
X whale[0].feedtype = xfr;
X }
X for (k = 0; k < area; k++) /* Fill Wa-Tor completely w/ plankton */
X locarr[k] = '.';
X if (!kldispflg)
X printscreen();
X if (debug >= 4)
X {
X fprintf(stdout, " Box %2d : whale[j + cycle].hunttype = %c\n",
X j, whale[0].hunttype);
X fprintf(stdout, " whale[j + cycle].hgperchr = %c\n",
X whale[0].hgperchr);
X fprintf(stdout, " whale[j + cycle].feedtype = %c\n",
X whale[0].feedtype);
X fprintf(stdout, " whale[j + cycle].fgperchr = %c\n",
X whale[0].fgperchr);
X if(trakflg == 2)
X {
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].huntgene[k]);
X fprintf(stdout, " <-- feed genes as hunt genes\n");
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].feedgene[k]);
X fprintf(stdout, " <-- hunt genes as feed genes\n");
X }
X else if (trakflg == 3)
X {
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].huntgene[k]);
X fprintf(stdout, " <-- hunt gene set\n");
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].feedgene[k]);
X fprintf(stdout, " <-- hunt gene set\n");
X }
X else if (trakflg == 4)
X {
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].huntgene[k]);
X fprintf(stdout, " <-- feed gene set\n");
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].feedgene[k]);
X fprintf(stdout, " <-- feed gene set\n");
X }
X else
X {
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].huntgene[k]);
X fprintf(stdout, " <-- hunt gene set\n");
X for (k = 0; k < 16; k++)
X fprintf(stdout, " %2hd", whale[0].feedgene[k]);
X fprintf(stdout, " <-- feed gene set\n");
X }
X }
X movewhale(); /* Move the one whale */
X if (!kldispflg)
X printscreen();
X plank = countplank();
X plank = area - plank;
X if (debug >= 4)
X {
X fprintf(stdout, " Plankton consumed: %5d out of %5d\n",
X plank, area);
X fprintf(stdout, "\n");
X }
X }
X cycle += stop;
X } while (cycle < orgwh);
X}
SHAR_EOF
if test 27590 -ne "`wc -c < 'whio.c3'`"
then
echo shar: error transmitting "'whio.c3'" '(should have been 27590 characters)'
fi
fi # end of overwriting check
# End of shell archive
exit 0
---
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Bob Loy | Life is the detour you take on the
...!sun!sunburn!gtx!loy | way to where you're really going.
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the Comp.sources.misc
mailing list