]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - worms/worms.c
1 /* $NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 hubertf 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
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 hubertf Exp $");
52 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
53 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
54 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
55 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
56 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
57 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
58 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
59 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
60 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
63 * Caltech High Energy Physics
67 #include <sys/types.h>
76 static const struct options
{
171 static const char flavor
[] = {
172 'O', '*', '#', '$', '%', '0', '@', '~'
174 static const 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 volatile sig_atomic_t sig_caught
= 0;
186 int main
__P((int, char **));
187 void nomem
__P((void)) __attribute__((__noreturn__
));
188 void onsig
__P((int));
197 const struct options
*op
;
199 int CO
, LI
, last
, bottom
, ch
, length
, number
, trail
;
203 unsigned int delay
= 0;
210 while ((ch
= getopt(argc
, argv
, "d:fl:n:t")) != -1)
213 if ((delay
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10)) < 1 || delay
> 1000)
214 errx(1, "invalid delay (1-1000)");
215 delay
*= 1000; /* ms -> us */
221 if ((length
= atoi(optarg
)) < 2 || length
> 1024) {
222 errx(1, "invalid length (%d - %d).",
227 if ((number
= atoi(optarg
)) < 1) {
228 errx(1, "invalid number of worms.");
236 (void)fprintf(stderr
,
237 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
241 if (!(worm
= malloc((size_t)number
*
242 sizeof(struct worm
))) || !(mp
= malloc((size_t)1024)))
249 if (!(ip
= malloc((size_t)(LI
* CO
* sizeof(short)))))
251 if (!(ref
= malloc((size_t)(LI
* sizeof(short *)))))
253 for (n
= 0; n
< LI
; ++n
) {
257 for (ip
= ref
[0], n
= LI
* CO
; --n
>= 0;)
259 for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++) {
260 w
->orientation
= w
->head
= 0;
261 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
264 for (x
= length
; --x
>= 0;)
266 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
269 for (y
= length
; --y
>= 0;)
273 (void)signal(SIGHUP
, onsig
);
274 (void)signal(SIGINT
, onsig
);
275 (void)signal(SIGQUIT
, onsig
);
276 (void)signal(SIGSTOP
, onsig
);
277 (void)signal(SIGTSTP
, onsig
);
278 (void)signal(SIGTERM
, onsig
);
281 const char *p
= field
;
283 for (y
= LI
; --y
>= 0;) {
284 for (x
= CO
; --x
>= 0;) {
298 if (delay
) usleep(delay
);
299 for (n
= 0, w
= &worm
[0]; n
< number
; n
++, w
++) {
300 if ((x
= w
->xpos
[h
= w
->head
]) < 0) {
301 mvaddch(y
= w
->ypos
[h
] = bottom
,
303 flavor
[n
% sizeof(flavor
)]);
310 if (w
->xpos
[w
->head
= h
] >= 0) {
315 if (--ref
[y1
][x1
] == 0) {
316 mvaddch(y1
, x1
, trail
);
319 op
= &(!x
? (!y
? upleft
: (y
== bottom
? lowleft
: left
)) : (x
== last
? (!y
? upright
: (y
== bottom
? lowright
: right
)) : (!y
? upper
: (y
== bottom
? lower
: normal
))))[w
->orientation
];
326 w
->orientation
= op
->opts
[0];
330 op
->opts
[(int)random() % op
->nopts
];
332 mvaddch(y
+= yinc
[w
->orientation
],
333 x
+= xinc
[w
->orientation
],
334 flavor
[n
% sizeof(flavor
)]);
335 ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;
342 int signo
__attribute__((__unused__
));
350 errx(1, "not enough memory.");