2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)worms.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/worms/worms.c,v 1.8.2.1 2000/12/04 10:36:17 alex Exp $
36 * $DragonFly: src/games/worms/worms.c,v 1.5 2007/04/18 18:32:12 swildner Exp $
41 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
42 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
43 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
44 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
45 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
46 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
47 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
48 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
49 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
52 * Caltech High Energy Physics
56 #include <sys/types.h>
64 static const struct options
{
159 static const char flavor
[] = {
160 'O', '*', '#', '$', '%', '0', '@', '~'
162 static const short xinc
[] = {
163 1, 1, 1, 0, -1, -1, -1, 0
165 -1, 0, 1, 1, 1, 0, -1, -1
168 int orientation
, head
;
172 volatile sig_atomic_t sig_caught
= 0;
177 main(int argc
, char **argv
)
181 const struct options
*op
;
183 int CO
, LI
, last
, bottom
, ch
, length
, number
, trail
;
187 unsigned int delay
= 0;
194 while ((ch
= getopt(argc
, argv
, "d:fl:n:t")) != -1)
197 if ((delay
= (unsigned int)strtoul(optarg
, NULL
, 10)) < 1 || delay
> 1000)
198 errx(1, "invalid delay (1-1000)");
199 delay
*= 1000; /* ms -> us */
205 if ((length
= atoi(optarg
)) < 2 || length
> 1024) {
206 errx(1, "invalid length (%d - %d).",
211 if ((number
= atoi(optarg
)) < 1) {
212 errx(1, "invalid number of worms.");
220 (void)fprintf(stderr
,
221 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
225 if (!(worm
= malloc((size_t)number
*
226 sizeof(struct worm
))) || !(mp
= malloc((size_t)1024)))
233 if (!(ip
= malloc((size_t)(LI
* CO
* sizeof(short)))))
235 if (!(ref
= malloc((size_t)(LI
* sizeof(short *)))))
237 for (n
= 0; n
< LI
; ++n
) {
241 for (ip
= ref
[0], n
= LI
* CO
; --n
>= 0;)
243 for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++) {
244 w
->orientation
= w
->head
= 0;
245 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
248 for (x
= length
; --x
>= 0;)
250 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
253 for (y
= length
; --y
>= 0;)
257 (void)signal(SIGHUP
, onsig
);
258 (void)signal(SIGINT
, onsig
);
259 (void)signal(SIGQUIT
, onsig
);
260 (void)signal(SIGSTOP
, onsig
);
261 (void)signal(SIGTSTP
, onsig
);
262 (void)signal(SIGTERM
, onsig
);
265 const char *p
= field
;
267 for (y
= LI
; --y
>= 0;) {
268 for (x
= CO
; --x
>= 0;) {
282 for (n
= 0, w
= &worm
[0]; n
< number
; n
++, w
++) {
283 if ((x
= w
->xpos
[h
= w
->head
]) < 0) {
284 mvaddch(y
= w
->ypos
[h
] = bottom
,
286 flavor
[n
% sizeof(flavor
)]);
293 if (w
->xpos
[w
->head
= h
] >= 0) {
298 if (--ref
[y1
][x1
] == 0) {
299 mvaddch(y1
, x1
, trail
);
302 op
= &(!x
? (!y
? upleft
: (y
== bottom
? lowleft
: left
)) : (x
== last
? (!y
? upright
: (y
== bottom
? lowright
: right
)) : (!y
? upper
: (y
== bottom
? lower
: normal
))))[w
->orientation
];
309 w
->orientation
= op
->opts
[0];
313 op
->opts
[(int)random() % op
->nopts
];
315 mvaddch(y
+= yinc
[w
->orientation
],
316 x
+= xinc
[w
->orientation
],
317 flavor
[n
% sizeof(flavor
)]);
318 ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;
326 onsig(__unused
int signo
)
334 errx(1, "not enough memory.");