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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)worms.c 8.1 (Berkeley) 5/31/93
31 * $FreeBSD: src/games/worms/worms.c,v 1.8.2.1 2000/12/04 10:36:17 alex Exp $
36 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
37 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
38 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
39 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
40 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
41 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
42 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
43 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
44 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
47 * Caltech High Energy Physics
51 #include <sys/types.h>
59 static const struct options {
154 static const char flavor[] = {
155 'O', '*', '#', '$', '%', '0', '@', '~'
157 static const short xinc[] = {
158 1, 1, 1, 0, -1, -1, -1, 0
160 -1, 0, 1, 1, 1, 0, -1, -1
163 int orientation, head;
167 static volatile sig_atomic_t sig_caught = 0;
169 static void nomem(void) __dead2;
170 static void onsig(int);
173 main(int argc, char **argv)
177 const struct options *op;
179 int CO, LI, last, bottom, ch, length, number, trail;
183 unsigned int delay = 0;
190 while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
193 if ((delay = (unsigned int)strtoul(optarg, NULL, 10)) < 1 || delay > 1000)
194 errx(1, "invalid delay (1-1000)");
195 delay *= 1000; /* ms -> us */
201 if ((length = atoi(optarg)) < 2 || length > 1024) {
202 errx(1, "invalid length (%d - %d).",
207 if ((number = atoi(optarg)) < 1) {
208 errx(1, "invalid number of worms.");
217 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
221 if (!(worm = malloc((size_t)number *
222 sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
229 if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
231 if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
233 for (n = 0; n < LI; ++n) {
237 for (ip = ref[0], n = LI * CO; --n >= 0;)
239 for (n = number, w = &worm[0]; --n >= 0; w++) {
240 w->orientation = w->head = 0;
241 if (!(ip = malloc((size_t)(length * sizeof(short)))))
244 for (x = length; --x >= 0;)
246 if (!(ip = malloc((size_t)(length * sizeof(short)))))
249 for (y = length; --y >= 0;)
253 signal(SIGHUP, onsig);
254 signal(SIGINT, onsig);
255 signal(SIGQUIT, onsig);
256 signal(SIGSTOP, onsig);
257 signal(SIGTSTP, onsig);
258 signal(SIGTERM, onsig);
261 const char *p = field;
263 for (y = LI; --y >= 0;) {
264 for (x = CO; --x >= 0;) {
278 for (n = 0, w = &worm[0]; n < number; n++, w++) {
279 if ((x = w->xpos[h = w->head]) < 0) {
280 mvaddch(y = w->ypos[h] = bottom,
282 flavor[n % sizeof(flavor)]);
289 if (w->xpos[w->head = h] >= 0) {
294 if (--ref[y1][x1] == 0) {
295 mvaddch(y1, x1, trail);
298 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
305 w->orientation = op->opts[0];
309 op->opts[(int)random() % op->nopts];
311 mvaddch(y += yinc[w->orientation],
312 x += xinc[w->orientation],
313 flavor[n % sizeof(flavor)]);
314 ref[w->ypos[h] = y][w->xpos[h] = x]++;
322 onsig(int signo __unused)
330 errx(1, "not enough memory.");