* Copyright (c) 1980 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)worms.c 5.9 (Berkeley) 2/28/91";
* @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
* @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
* @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
* @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
* @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
* @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
* @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
* @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
* @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
* Caltech High Energy Physics
#define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
'O', '*', '#', '$', '%', '0', '@', '~'
1, 1, 1, 0, -1, -1, -1, 0
-1, 0, 1, 1, 1, 0, -1, -1
extern char *optarg
, *UP
;
register struct options
*op
;
int CO
, IN
, LI
, last
, bottom
, ch
, length
, number
, trail
, Wrap
;
char *AL
, *BC
, *CM
, *EI
, *HO
, *IC
, *IM
, *IP
, *SR
;
char *field
, tcb
[100], *mp
, *malloc(), *getenv(), *tgetstr(), *tgoto();
while ((ch
= getopt(argc
, argv
, "fl:n:t")) != EOF
)
if ((length
= atoi(optarg
)) < 2 || length
> 1024) {
"worms: invalid length (%d - %d).\n",
if ((number
= atoi(optarg
)) < 1) {
"worms: invalid number of worms.\n");
"usage: worms [-ft] [-length #] [-number #]\n");
if (!(term
= getenv("TERM"))) {
(void)fprintf(stderr
, "worms: no TERM environment variable.\n");
if (!(worm
= (struct worm
*)malloc((u_int
)number
*
sizeof(struct worm
))) || !(mp
= malloc((u_int
)1024)))
if (tgetent(mp
, term
) <= 0) {
(void)fprintf(stderr
, "worms: %s: unknown terminal type.\n",
if (!(CM
= tgetstr("cm", &tcp
))) {
"worms: terminal incapable of cursor motion.\n");
AL
= tgetstr("al", &tcp
);
BC
= tgetflag("bs") ? "\b" : tgetstr("bc", &tcp
);
if ((CO
= tgetnum("co")) <= 0)
EI
= tgetstr("ei", &tcp
);
HO
= tgetstr("ho", &tcp
);
IC
= tgetstr("ic", &tcp
);
IM
= tgetstr("im", &tcp
);
IP
= tgetstr("ip", &tcp
);
if ((LI
= tgetnum("li")) <= 0)
SR
= tgetstr("sr", &tcp
);
UP
= tgetstr("up", &tcp
);
ospeed
= sg
.c_cflag
&CBAUD
;
if (!(ip
= (short *)malloc((u_int
)(LI
* CO
* sizeof(short)))))
if (!(ref
= (short **)malloc((u_int
)(LI
* sizeof(short *)))))
for (n
= 0; n
< LI
; ++n
) {
for (ip
= ref
[0], n
= LI
* CO
; --n
>= 0;)
for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++) {
w
->orientation
= w
->head
= 0;
if (!(ip
= (short *)malloc((u_int
)(length
* sizeof(short)))))
for (x
= length
; --x
>= 0;)
if (!(ip
= (short *)malloc((u_int
)(length
* sizeof(short)))))
for (y
= length
; --y
>= 0;)
(void)signal(SIGHUP
, onsig
);
(void)signal(SIGINT
, onsig
);
(void)signal(SIGQUIT
, onsig
);
(void)signal(SIGSTOP
, onsig
);
(void)signal(SIGTSTP
, onsig
);
(void)signal(SIGTERM
, onsig
);
tputs(tgetstr("ti", &tcp
), 1, fputchar
);
tputs(tgetstr("cl", &tcp
), 1, fputchar
);
register char *p
= field
;
for (y
= bottom
; --y
>= 0;) {
for (x
= CO
; --x
>= 0;) {
for (x
= last
; --x
> 0;) {
cursor(last
- 1, bottom
);
for (x
= CO
; --x
>= 0;) {
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
);
fputchar(flavor
[n
% sizeof(flavor
)]);
if (w
->xpos
[w
->head
= h
] >= 0) {
if (--ref
[y1
][x1
] == 0) {
op
= &(!x
? (!y
? upleft
: (y
== bottom
? lowleft
: left
)) : (x
== last
? (!y
? upright
: (y
== bottom
? lowright
: right
)) : (!y
? upper
: (y
== bottom
? lower
: normal
))))[w
->orientation
];
w
->orientation
= op
->opts
[0];
op
->opts
[(int)random() % op
->nopts
];
cursor(x
+= xinc
[w
->orientation
],
y
+= yinc
[w
->orientation
]);
if (!Wrap
|| x
!= last
|| y
!= bottom
)
fputchar(flavor
[n
% sizeof(flavor
)]);
ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;
tputs(tgetstr("cl", &tcp
), 1, fputchar
);
tputs(tgetstr("te", &tcp
), 1, fputchar
);
(void)fprintf(stderr
, "worms: not enough memory.\n");