static char sccsid
[] = " worms.c 4.1 82/10/24 ";
@@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
@@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
@@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
@@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
@@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
@@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
@@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
Caltech High Energy Physics
#define cursor(col,row) tputs(tgoto(CM,col,row),1,outc)
'O', '*', '#', '$', '%', '0'
1, 1, 1, 0, -1, -1, -1, 0
-1, 0, 1, 1, 1, 0, -1, -1
static int length
=16, number
=3, trail
=' ';
char *tgetstr(), *tgoto();
register struct options
*op
;
char *AL
, *BC
, *CM
, *EI
, *HO
, *IC
, *IM
, *IP
, *SR
;
int CO
, IN
, LI
, last
, bottom
;
setbuf(stdout
,malloc(BUFSIZ
));
if (++x
==argc
) goto usage
;
if ((length
=atoi(argv
[x
]))<2||length
>1024) {
fprintf(stderr
,"%s: Invalid length\n",*argv
);
if (++x
==argc
) goto usage
;
if ((number
=atoi(argv
[x
]))<1||number
>40) {
fprintf(stderr
,"%s: Invalid number of worms\n",*argv
);
"usage: %s [-field] [-length #] [-number #] [-trail]\n",*argv
);
if (!(term
=getenv("TERM"))) {
fprintf(stderr
,"%s: TERM: parameter not set\n",*argv
);
if (tgetent(malloc(1024),term
)<=0) {
fprintf(stderr
,"%s: %s: unknown terminal type\n",*argv
,term
);
if (!(CM
=tgetstr("cm",&tcp
))) {
fprintf(stderr
,"%s: terminal not capable of cursor motion\n",*argv
);
BC
=tgetflag("bs") ? "\b" : tgetstr("bc",&tcp
);
if ((CO
=tgetnum("co"))<=0) CO
=80;
if ((LI
=tgetnum("li"))<=0) LI
=24;
gtty(fileno(stdout
),&sg
);
ip
=(short *)malloc(LI
*CO
*sizeof (short));
for (ip
=ref
[0],n
=LI
*CO
;--n
>=0;) *ip
++=0;
if (Wrap
) ref
[bottom
][last
]=1;
for (n
=number
, w
= &worm
[0];--n
>=0;w
++) {
w
->orientation
=w
->head
=0;
if (!(ip
=(short *)malloc(length
*sizeof (short)))) {
fprintf(stderr
,"%s: out of memory\n",*argv
);
for (x
=length
;--x
>=0;) *ip
++ = -1;
if (!(ip
=(short *)malloc(length
*sizeof (short)))) {
fprintf(stderr
,"%s: out of memory\n",*argv
);
for (y
=length
;--y
>=0;) *ip
++ = -1;
tputs(tgetstr("cl",&tcp
),1,fputchar
);
if (!Wrap
) putchar('\n');
y
= *p
++; if (!*p
) p
=field
;
if (BC
) fputs(BC
,stdout
);
else cursor(last
-1,bottom
);
if (IC
) tputs(IC
,1,fputchar
);
if (IP
) tputs(IP
,1,fputchar
);
if (HO
) fputs(HO
,stdout
);
if (SR
) tputs(SR
,1,fputchar
);
else tputs(AL
,LI
,fputchar
);
else for (x
=last
;--x
>=0;) {
else for (x
=CO
;--x
>=0;) {
for (n
=0,w
= &worm
[0];n
<number
;n
++,w
++) {
if ((x
=w
->xpos
[h
=w
->head
])<0) {
cursor(x
=w
->xpos
[h
]=0,y
=w
->ypos
[h
]=bottom
);
if (w
->xpos
[w
->head
=h
]>=0) {
x1
=w
->xpos
[h
]; y1
=w
->ypos
[h
];
cursor(x1
,y1
); putchar(trail
);
op
= &(x
==0 ? (y
==0 ? upleft
: (y
==bottom
? lowleft
: left
)) :
(x
==last
? (y
==0 ? upright
: (y
==bottom
? lowright
: right
)) :
(y
==0 ? upper
: (y
==bottom
? lower
: normal
))))[w
->orientation
];
w
->orientation
=op
->opts
[0];
w
->orientation
=op
->opts
[(int)(ranf()*(float)op
->nopts
)];
cursor(x
+=xinc
[w
->orientation
], y
+=yinc
[w
->orientation
]);
if (!Wrap
||x
!=last
||y
!=bottom
) putchar(flavor
[n
%6]);
ref
[w
->ypos
[h
]=y
][w
->xpos
[h
]=x
]++;
return((float)rand()/2147483647.);