home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / linux / old-src / ncurses-1.8.5 / worm.c < prev   
C/C++ Source or Header  |  1993-11-27  |  6KB  |  270 lines

  1. /*
  2.  
  3.      @@@        @@@    @@@@@@@@@@     @@@@@@@@@@@    @@@@@@@@@@@@
  4.      @@@        @@@   @@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@@@@@
  5.      @@@        @@@  @@@@      @@@@   @@@@           @@@@ @@@  @@@@
  6.      @@@   @@   @@@  @@@        @@@   @@@            @@@  @@@   @@@
  7.      @@@  @@@@  @@@  @@@        @@@   @@@            @@@  @@@   @@@
  8.      @@@@ @@@@ @@@@  @@@        @@@   @@@            @@@  @@@   @@@
  9.       @@@@@@@@@@@@   @@@@      @@@@   @@@            @@@  @@@   @@@
  10.        @@@@  @@@@     @@@@@@@@@@@@    @@@            @@@  @@@   @@@
  11.         @@    @@       @@@@@@@@@@     @@@            @@@  @@@   @@@
  12.  
  13.                  Eric P. Scott
  14.               Caltech High Energy Physics
  15.                  October, 1980
  16.  
  17. */
  18.  
  19. #include <ncurses.h>
  20. #include <signal.h>
  21.  
  22. #define cursor(col,row) move(row,col)
  23.  
  24. int Wrap;
  25. short *ref[128];
  26. static char flavor[]={
  27.     'O', '*', '#', '$', '%', '0'
  28. };
  29. static short xinc[]={
  30.      1,  1,  1,  0, -1, -1, -1,  0
  31. }, yinc[]={
  32.     -1,  0,  1,  1,  1,  0, -1, -1
  33. };
  34. static struct worm {
  35.     int orientation, head;
  36.     short *xpos, *ypos;
  37. } worm[40];
  38. static char *field;
  39. static int length=16, number=3, trail=' ';
  40. static struct options {
  41.     int nopts;
  42.     int opts[3];
  43. } normal[8]={
  44.     { 3, { 7, 0, 1 } },
  45.     { 3, { 0, 1, 2 } },
  46.     { 3, { 1, 2, 3 } },
  47.     { 3, { 2, 3, 4 } },
  48.     { 3, { 3, 4, 5 } },
  49.     { 3, { 4, 5, 6 } },
  50.     { 3, { 5, 6, 7 } },
  51.     { 3, { 6, 7, 0 } }
  52. }, upper[8]={
  53.     { 1, { 1, 0, 0 } },
  54.     { 2, { 1, 2, 0 } },
  55.     { 0, { 0, 0, 0 } },
  56.     { 0, { 0, 0, 0 } },
  57.     { 0, { 0, 0, 0 } },
  58.     { 2, { 4, 5, 0 } },
  59.     { 1, { 5, 0, 0 } },
  60.     { 2, { 1, 5, 0 } }
  61. }, left[8]={
  62.     { 0, { 0, 0, 0 } },
  63.     { 0, { 0, 0, 0 } },
  64.     { 0, { 0, 0, 0 } },
  65.     { 2, { 2, 3, 0 } },
  66.     { 1, { 3, 0, 0 } },
  67.     { 2, { 3, 7, 0 } },
  68.     { 1, { 7, 0, 0 } },
  69.     { 2, { 7, 0, 0 } }
  70. }, right[8]={
  71.     { 1, { 7, 0, 0 } },
  72.     { 2, { 3, 7, 0 } },
  73.     { 1, { 3, 0, 0 } },
  74.     { 2, { 3, 4, 0 } },
  75.     { 0, { 0, 0, 0 } },
  76.     { 0, { 0, 0, 0 } },
  77.     { 0, { 0, 0, 0 } },
  78.     { 2, { 6, 7, 0 } }
  79. }, lower[8]={
  80.     { 0, { 0, 0, 0 } },
  81.     { 2, { 0, 1, 0 } },
  82.     { 1, { 1, 0, 0 } },
  83.     { 2, { 1, 5, 0 } },
  84.     { 1, { 5, 0, 0 } },
  85.     { 2, { 5, 6, 0 } },
  86.     { 0, { 0, 0, 0 } },
  87.     { 0, { 0, 0, 0 } }
  88. }, upleft[8]={
  89.     { 0, { 0, 0, 0 } },
  90.     { 0, { 0, 0, 0 } },
  91.     { 0, { 0, 0, 0 } },
  92.     { 0, { 0, 0, 0 } },
  93.     { 0, { 0, 0, 0 } },
  94.     { 1, { 3, 0, 0 } },
  95.     { 2, { 1, 3, 0 } },
  96.     { 1, { 1, 0, 0 } }
  97. }, upright[8]={
  98.     { 2, { 3, 5, 0 } },
  99.     { 1, { 3, 0, 0 } },
  100.     { 0, { 0, 0, 0 } },
  101.     { 0, { 0, 0, 0 } },
  102.     { 0, { 0, 0, 0 } },
  103.     { 0, { 0, 0, 0 } },
  104.     { 0, { 0, 0, 0 } },
  105.     { 1, { 5, 0, 0 } }
  106. }, lowleft[8]={
  107.     { 3, { 7, 0, 1 } },
  108.     { 0, { 0, 0, 0 } },
  109.     { 0, { 0, 0, 0 } },
  110.     { 1, { 1, 0, 0 } },
  111.     { 2, { 1, 7, 0 } },
  112.     { 1, { 7, 0, 0 } },
  113.     { 0, { 0, 0, 0 } },
  114.     { 0, { 0, 0, 0 } }
  115. }, lowright[8]={
  116.     { 0, { 0, 0, 0 } },
  117.     { 1, { 7, 0, 0 } },
  118.     { 2, { 5, 7, 0 } },
  119.     { 1, { 5, 0, 0 } },
  120.     { 0, { 0, 0, 0 } },
  121.     { 0, { 0, 0, 0 } },
  122.     { 0, { 0, 0, 0 } },
  123.     { 0, { 0, 0, 0 } }
  124. };
  125.  
  126. void onsig();
  127. float ranf();
  128.  
  129. main(argc,argv)
  130. int argc;
  131. char *argv[];
  132. {
  133. int x, y;
  134. int n;
  135. struct worm *w;
  136. struct options *op;
  137. int h;
  138. short *ip;
  139. int last, bottom;
  140.  
  141.     for (x=1;x<argc;x++) {
  142.         register char *p;
  143.         p=argv[x];
  144.         if (*p=='-') p++;
  145.         switch (*p) {
  146.         case 'f':
  147.             field="WORM";
  148.             break;
  149.         case 'l':
  150.             if (++x==argc) goto usage;
  151.             if ((length=atoi(argv[x]))<2||length>1024) {
  152.                 fprintf(stderr,"%s: Invalid length\n",*argv);
  153.                 exit(1);
  154.             }
  155.             break;
  156.         case 'n':
  157.             if (++x==argc) goto usage;
  158.             if ((number=atoi(argv[x]))<1||number>40) {
  159.                 fprintf(stderr,"%s: Invalid number of worms\n",*argv);
  160.                 exit(1);
  161.             }
  162.             break;
  163.         case 't':
  164.             trail='.';
  165.             break;
  166.         default:
  167.         usage:
  168.             fprintf(stderr, "usage: %s [-field] [-length #] [-number #] [-trail]\n",*argv);
  169.             exit(1);
  170.             break;
  171.         }
  172.     }
  173.  
  174.     signal(SIGINT, onsig);
  175.     initscr();
  176.     bottom = LINES-1;
  177.     last = COLS-1;
  178.  
  179.     ip=(short *)malloc(LINES*COLS*sizeof (short));
  180.  
  181.     for (n=0;n<LINES;) {
  182.         ref[n++]=ip; ip+=COLS;
  183.     }
  184.     for (ip=ref[0],n=LINES*COLS;--n>=0;) *ip++=0;
  185.     ref[bottom][last]=1;
  186.     for (n=number, w= &worm[0];--n>=0;w++) {
  187.         w->orientation=w->head=0;
  188.         if (!(ip=(short *)malloc(length*sizeof (short)))) {
  189.             fprintf(stderr,"%s: out of memory\n",*argv);
  190.             exit(1);
  191.         }
  192.         w->xpos=ip;
  193.         for (x=length;--x>=0;) *ip++ = -1;
  194.         if (!(ip=(short *)malloc(length*sizeof (short)))) {
  195.             fprintf(stderr,"%s: out of memory\n",*argv);
  196.             exit(1);
  197.         }
  198.         w->ypos=ip;
  199.         for (y=length;--y>=0;) *ip++ = -1;
  200.     }
  201.     if (field) {
  202.         register char *p;
  203.         p=field;
  204.         for (y=bottom;--y>=0;) {
  205.             for (x=COLS;--x>=0;) {
  206.                 addch(*p++);
  207.                 if (!*p) p=field;
  208.             }
  209.             addch('\n');
  210.         }
  211.     }
  212.     refresh();
  213.  
  214.     for (;;) {
  215.         for (n=0,w= &worm[0];n<number;n++,w++) {
  216.             if ((x=w->xpos[h=w->head])<0) {
  217.                 cursor(x=w->xpos[h]=0,y=w->ypos[h]=bottom);
  218.                 addch(flavor[n%6]);
  219.                 ref[y][x]++;
  220.             }
  221.             else y=w->ypos[h];
  222.             if (++h==length) h=0;
  223.             if (w->xpos[w->head=h]>=0) {
  224.                 register int x1, y1;
  225.                 x1=w->xpos[h]; y1=w->ypos[h];
  226.                 if (--ref[y1][x1]==0) {
  227.                     cursor(x1,y1); addch(trail);
  228.                 }
  229.             }
  230.             op= &(x==0 ? (y==0 ? upleft : (y==bottom ? lowleft : left)) :
  231.                 (x==last ? (y==0 ? upright : (y==bottom ? lowright : right)) :
  232.             (y==0 ? upper : (y==bottom ? lower : normal))))[w->orientation];
  233.             switch (op->nopts) {
  234.             case 0:
  235.                 refresh();
  236.                 endwin();
  237.                 abort();
  238.                 return;
  239.             case 1:
  240.                 w->orientation=op->opts[0];
  241.                 break;
  242.             default:
  243.                 w->orientation=op->opts[(int)(ranf()*(float)op->nopts)];
  244.             }
  245.             cursor(x+=xinc[w->orientation], y+=yinc[w->orientation]);
  246.             if (!Wrap||x!=last||y!=bottom) addch(flavor[n%6]);
  247.             ref[w->ypos[h]=y][w->xpos[h]=x]++;
  248.         }
  249.         refresh();
  250.     }
  251. }
  252.  
  253. void
  254. onsig()
  255. {
  256.     endwin();
  257.     exit(0);
  258. }
  259.  
  260. float
  261. ranf()
  262. {
  263.     float rv;
  264.     long r = rand();
  265.  
  266.     r &= 077777;
  267.     rv =((float)r/32767.);
  268.     return rv;
  269. }
  270.