]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - worms/worms.c
1 /* $NetBSD: worms.c,v 1.10 1998/09/13 15:27:31 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.10 1998/09/13 15:27:31 hubertf Exp $");
52 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
53 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
54 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
55 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
56 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
57 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
58 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
59 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
60 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
63 * Caltech High Energy Physics
67 #include <sys/types.h>
68 #include <sys/ioctl.h>
76 static struct options
{
170 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
173 static char flavor
[] = {
174 'O', '*', '#', '$', '%', '0', '@', '~'
176 static short xinc
[] = {
177 1, 1, 1, 0, -1, -1, -1, 0
179 -1, 0, 1, 1, 1, 0, -1, -1
182 int orientation
, head
;
186 void fputchar
__P((int));
187 int main
__P((int, char **));
188 void nomem
__P((void)) __attribute__((__noreturn__
));
189 void onsig
__P((int)) __attribute__((__noreturn__
));
190 int tgetent
__P((char *, char *));
191 int tgetflag
__P((char *));
192 int tgetnum
__P((char *));
193 char *tgetstr
__P((char *, char **));
194 char *tgoto
__P((char *, int, int));
195 int tputs
__P((char *, int, void (*)(int)));
208 int CO
, IN
, LI
, last
, bottom
, ch
, length
, number
, trail
, Wrap
;
210 char *AL
, *BC
, *CM
, *EI
, *HO
, *IC
, *IM
, *IP
, *SR
;
211 char *field
, tcb
[100], *mp
;
222 while ((ch
= getopt(argc
, argv
, "fl:n:t")) != -1)
228 if ((length
= atoi(optarg
)) < 2 || length
> 1024) {
229 (void)fprintf(stderr
,
230 "worms: invalid length (%d - %d).\n",
236 if ((number
= atoi(optarg
)) < 1) {
237 (void)fprintf(stderr
,
238 "worms: invalid number of worms.\n");
247 (void)fprintf(stderr
,
248 "usage: worms [-ft] [-l length] [-n number]\n");
252 if (!(term
= getenv("TERM"))) {
253 (void)fprintf(stderr
, "worms: no TERM environment variable.\n");
256 if (!(worm
= malloc((size_t)number
*
257 sizeof(struct worm
))) || !(mp
= malloc((size_t)1024)))
259 if (tgetent(mp
, term
) <= 0) {
260 (void)fprintf(stderr
, "worms: %s: unknown terminal type.\n",
265 if (!(CM
= tgetstr("cm", &tcp
))) {
266 (void)fprintf(stderr
,
267 "worms: terminal incapable of cursor motion.\n");
270 AL
= tgetstr("al", &tcp
);
271 BC
= tgetflag("bs") ? "\b" : tgetstr("bc", &tcp
);
272 EI
= tgetstr("ei", &tcp
);
273 HO
= tgetstr("ho", &tcp
);
274 IC
= tgetstr("ic", &tcp
);
275 IM
= tgetstr("im", &tcp
);
277 IP
= tgetstr("ip", &tcp
);
278 SR
= tgetstr("sr", &tcp
);
279 UP
= tgetstr("up", &tcp
);
281 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1 &&
282 ws
.ws_col
&& ws
.ws_row
) {
288 if ((CO
= tgetnum("co")) <= 0)
290 if ((LI
= tgetnum("li")) <= 0)
295 tcgetattr(fileno(stdout
), &ti
);
296 Wrap
= tgetflag("am");
297 if (!(ip
= malloc((size_t)(LI
* CO
* sizeof(short)))))
299 if (!(ref
= malloc((size_t)(LI
* sizeof(short *)))))
301 for (n
= 0; n
< LI
; ++n
) {
305 for (ip
= ref
[0], n
= LI
* CO
; --n
>= 0;)
308 ref
[bottom
][last
] = 1;
309 for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++) {
310 w
->orientation
= w
->head
= 0;
311 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
314 for (x
= length
; --x
>= 0;)
316 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
319 for (y
= length
; --y
>= 0;)
323 (void)signal(SIGHUP
, onsig
);
324 (void)signal(SIGINT
, onsig
);
325 (void)signal(SIGQUIT
, onsig
);
326 (void)signal(SIGSTOP
, onsig
);
327 (void)signal(SIGTSTP
, onsig
);
328 (void)signal(SIGTERM
, onsig
);
330 tputs(tgetstr("ti", &tcp
), 1, fputchar
);
331 tputs(tgetstr("cl", &tcp
), 1, fputchar
);
335 for (y
= bottom
; --y
>= 0;) {
336 for (x
= CO
; --x
>= 0;) {
343 (void)fflush(stdout
);
347 for (x
= last
; --x
> 0;) {
357 tputs(BC
, 1, fputchar
);
359 cursor(last
- 1, bottom
);
360 tputs(IM
, 1, fputchar
);
362 tputs(IC
, 1, fputchar
);
365 tputs(IP
, 1, fputchar
);
366 tputs(EI
, 1, fputchar
);
370 tputs(HO
, 1, fputchar
);
374 tputs(SR
, 1, fputchar
);
376 tputs(AL
, LI
, fputchar
);
377 for (x
= CO
; --x
>= 0;) {
383 else for (x
= last
; --x
>= 0;) {
389 else for (x
= CO
; --x
>= 0;) {
396 (void)fflush(stdout
);
397 for (n
= 0, w
= &worm
[0]; n
< number
; n
++, w
++) {
398 if ((x
= w
->xpos
[h
= w
->head
]) < 0) {
399 cursor(x
= w
->xpos
[h
] = 0,
400 y
= w
->ypos
[h
] = bottom
);
401 fputchar(flavor
[n
% sizeof(flavor
)]);
408 if (w
->xpos
[w
->head
= h
] >= 0) {
413 if (--ref
[y1
][x1
] == 0) {
419 op
= &(!x
? (!y
? upleft
: (y
== bottom
? lowleft
: left
)) : (x
== last
? (!y
? upright
: (y
== bottom
? lowright
: right
)) : (!y
? upper
: (y
== bottom
? lower
: normal
))))[w
->orientation
];
422 (void)fflush(stdout
);
426 w
->orientation
= op
->opts
[0];
430 op
->opts
[(int)random() % op
->nopts
];
432 cursor(x
+= xinc
[w
->orientation
],
433 y
+= yinc
[w
->orientation
]);
434 if (!Wrap
|| x
!= last
|| y
!= bottom
)
435 fputchar(flavor
[n
% sizeof(flavor
)]);
436 ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;
445 tputs(tgetstr("cl", &tcp
), 1, fputchar
);
446 tputs(tgetstr("te", &tcp
), 1, fputchar
);
460 (void)fprintf(stderr
, "worms: not enough memory.\n");