]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - worms/worms.c
1 /* $NetBSD: worms.c,v 1.6 1995/04/22 08:00:58 cgd Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char copyright
[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
44 static char sccsid
[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
46 static char rcsid
[] = "$NetBSD: worms.c,v 1.6 1995/04/22 08:00:58 cgd Exp $";
52 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
53 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
54 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
55 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
56 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
57 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
58 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
59 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
60 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
63 * Caltech High Energy Physics
67 #include <sys/types.h>
74 static struct options
{
168 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, putchar)
171 static char flavor
[] = {
172 'O', '*', '#', '$', '%', '0', '@', '~'
174 static short xinc
[] = {
175 1, 1, 1, 0, -1, -1, -1, 0
177 -1, 0, 1, 1, 1, 0, -1, -1
180 int orientation
, head
;
184 void fputchar
__P((int));
185 void onsig
__P((int));
186 char *tgetstr
__P((char *, char **));
187 char *tgoto
__P((char *, int, int));
188 int tputs
__P((char *, int, void (*)(int)));
196 extern char *optarg
, *UP
;
197 register int x
, y
, h
, n
;
198 register struct worm
*w
;
199 register struct options
*op
;
202 int CO
, IN
, LI
, last
, bottom
, ch
, length
, number
, trail
, Wrap
;
204 char *AL
, *BC
, *CM
, *EI
, *HO
, *IC
, *IM
, *IP
, *SR
;
205 char *field
, tcb
[100], *mp
;
212 while ((ch
= getopt(argc
, argv
, "fl:n:t")) != EOF
)
218 if ((length
= atoi(optarg
)) < 2 || length
> 1024) {
219 (void)fprintf(stderr
,
220 "worms: invalid length (%d - %d).\n",
226 if ((number
= atoi(optarg
)) < 1) {
227 (void)fprintf(stderr
,
228 "worms: invalid number of worms.\n");
237 (void)fprintf(stderr
,
238 "usage: worms [-ft] [-l length] [-n number]\n");
242 if (!(term
= getenv("TERM"))) {
243 (void)fprintf(stderr
, "worms: no TERM environment variable.\n");
246 if (!(worm
= malloc((size_t)number
*
247 sizeof(struct worm
))) || !(mp
= malloc((size_t)1024)))
249 if (tgetent(mp
, term
) <= 0) {
250 (void)fprintf(stderr
, "worms: %s: unknown terminal type.\n",
255 if (!(CM
= tgetstr("cm", &tcp
))) {
256 (void)fprintf(stderr
,
257 "worms: terminal incapable of cursor motion.\n");
260 AL
= tgetstr("al", &tcp
);
261 BC
= tgetflag("bs") ? "\b" : tgetstr("bc", &tcp
);
262 EI
= tgetstr("ei", &tcp
);
263 HO
= tgetstr("ho", &tcp
);
264 IC
= tgetstr("ic", &tcp
);
265 IM
= tgetstr("im", &tcp
);
267 IP
= tgetstr("ip", &tcp
);
268 SR
= tgetstr("sr", &tcp
);
269 UP
= tgetstr("up", &tcp
);
271 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1 &&
272 ws
.ws_col
&& ws
.ws_row
) {
278 if ((CO
= tgetnum("co")) <= 0)
280 if ((LI
= tgetnum("li")) <= 0)
285 tcgetattr(fileno(stdout
), &ti
);
286 Wrap
= tgetflag("am");
287 if (!(ip
= malloc((size_t)(LI
* CO
* sizeof(short)))))
289 if (!(ref
= malloc((size_t)(LI
* sizeof(short *)))))
291 for (n
= 0; n
< LI
; ++n
) {
295 for (ip
= ref
[0], n
= LI
* CO
; --n
>= 0;)
298 ref
[bottom
][last
] = 1;
299 for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++) {
300 w
->orientation
= w
->head
= 0;
301 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
304 for (x
= length
; --x
>= 0;)
306 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
309 for (y
= length
; --y
>= 0;)
313 (void)signal(SIGHUP
, onsig
);
314 (void)signal(SIGINT
, onsig
);
315 (void)signal(SIGQUIT
, onsig
);
316 (void)signal(SIGSTOP
, onsig
);
317 (void)signal(SIGTSTP
, onsig
);
318 (void)signal(SIGTERM
, onsig
);
320 tputs(tgetstr("ti", &tcp
), 1, putchar
);
321 tputs(tgetstr("cl", &tcp
), 1, putchar
);
323 register char *p
= field
;
325 for (y
= bottom
; --y
>= 0;) {
326 for (x
= CO
; --x
>= 0;) {
333 (void)fflush(stdout
);
337 for (x
= last
; --x
> 0;) {
347 tputs(BC
, 1, putchar
);
349 cursor(last
- 1, bottom
);
350 tputs(IM
, 1, putchar
);
352 tputs(IC
, 1, putchar
);
355 tputs(IP
, 1, putchar
);
356 tputs(EI
, 1, putchar
);
360 tputs(HO
, 1, putchar
);
364 tputs(SR
, 1, putchar
);
366 tputs(AL
, LI
, putchar
);
367 for (x
= CO
; --x
>= 0;) {
373 else for (x
= last
; --x
>= 0;) {
379 else for (x
= CO
; --x
>= 0;) {
386 (void)fflush(stdout
);
387 for (n
= 0, w
= &worm
[0]; n
< number
; n
++, w
++) {
388 if ((x
= w
->xpos
[h
= w
->head
]) < 0) {
389 cursor(x
= w
->xpos
[h
] = 0,
390 y
= w
->ypos
[h
] = bottom
);
391 putchar(flavor
[n
% sizeof(flavor
)]);
398 if (w
->xpos
[w
->head
= h
] >= 0) {
403 if (--ref
[y1
][x1
] == 0) {
409 op
= &(!x
? (!y
? upleft
: (y
== bottom
? lowleft
: left
)) : (x
== last
? (!y
? upright
: (y
== bottom
? lowright
: right
)) : (!y
? upper
: (y
== bottom
? lower
: normal
))))[w
->orientation
];
412 (void)fflush(stdout
);
416 w
->orientation
= op
->opts
[0];
420 op
->opts
[(int)random() % op
->nopts
];
422 cursor(x
+= xinc
[w
->orientation
],
423 y
+= yinc
[w
->orientation
]);
424 if (!Wrap
|| x
!= last
|| y
!= bottom
)
425 putchar(flavor
[n
% sizeof(flavor
)]);
426 ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;
435 tputs(tgetstr("cl", &tcp
), 1, putchar
);
436 tputs(tgetstr("te", &tcp
), 1, putchar
);
449 (void)fprintf(stderr
, "worms: not enough memory.\n");