Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / worms / worms.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)worms.c     8.1 (Berkeley) 5/31/93";
43 #endif
44 static const char rcsid[] =
45  "$FreeBSD: src/games/worms/worms.c,v 1.8.2.1 2000/12/04 10:36:17 alex Exp $";
46 #endif /* not lint */
47
48 /*
49  *
50  *       @@@        @@@    @@@@@@@@@@     @@@@@@@@@@@    @@@@@@@@@@@@
51  *       @@@        @@@   @@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@@@@@
52  *       @@@        @@@  @@@@      @@@@   @@@@           @@@@ @@@  @@@@
53  *       @@@   @@   @@@  @@@        @@@   @@@            @@@  @@@   @@@
54  *       @@@  @@@@  @@@  @@@        @@@   @@@            @@@  @@@   @@@
55  *       @@@@ @@@@ @@@@  @@@        @@@   @@@            @@@  @@@   @@@
56  *        @@@@@@@@@@@@   @@@@      @@@@   @@@            @@@  @@@   @@@
57  *         @@@@  @@@@     @@@@@@@@@@@@    @@@            @@@  @@@   @@@
58  *          @@    @@       @@@@@@@@@@     @@@            @@@  @@@   @@@
59  *
60  *                               Eric P. Scott
61  *                        Caltech High Energy Physics
62  *                               October, 1980
63  *
64  */
65 #include <sys/types.h>
66 #include <curses.h>
67 #include <err.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72
73 static const struct options {
74         int nopts;
75         int opts[3];
76 }
77         normal[8] = {
78         { 3, { 7, 0, 1 } },
79         { 3, { 0, 1, 2 } },
80         { 3, { 1, 2, 3 } },
81         { 3, { 2, 3, 4 } },
82         { 3, { 3, 4, 5 } },
83         { 3, { 4, 5, 6 } },
84         { 3, { 5, 6, 7 } },
85         { 3, { 6, 7, 0 } }
86 },      upper[8] = {
87         { 1, { 1, 0, 0 } },
88         { 2, { 1, 2, 0 } },
89         { 0, { 0, 0, 0 } },
90         { 0, { 0, 0, 0 } },
91         { 0, { 0, 0, 0 } },
92         { 2, { 4, 5, 0 } },
93         { 1, { 5, 0, 0 } },
94         { 2, { 1, 5, 0 } }
95 },
96         left[8] = {
97         { 0, { 0, 0, 0 } },
98         { 0, { 0, 0, 0 } },
99         { 0, { 0, 0, 0 } },
100         { 2, { 2, 3, 0 } },
101         { 1, { 3, 0, 0 } },
102         { 2, { 3, 7, 0 } },
103         { 1, { 7, 0, 0 } },
104         { 2, { 7, 0, 0 } }
105 },
106         right[8] = {
107         { 1, { 7, 0, 0 } },
108         { 2, { 3, 7, 0 } },
109         { 1, { 3, 0, 0 } },
110         { 2, { 3, 4, 0 } },
111         { 0, { 0, 0, 0 } },
112         { 0, { 0, 0, 0 } },
113         { 0, { 0, 0, 0 } },
114         { 2, { 6, 7, 0 } }
115 },
116         lower[8] = {
117         { 0, { 0, 0, 0 } },
118         { 2, { 0, 1, 0 } },
119         { 1, { 1, 0, 0 } },
120         { 2, { 1, 5, 0 } },
121         { 1, { 5, 0, 0 } },
122         { 2, { 5, 6, 0 } },
123         { 0, { 0, 0, 0 } },
124         { 0, { 0, 0, 0 } }
125 },
126         upleft[8] = {
127         { 0, { 0, 0, 0 } },
128         { 0, { 0, 0, 0 } },
129         { 0, { 0, 0, 0 } },
130         { 0, { 0, 0, 0 } },
131         { 0, { 0, 0, 0 } },
132         { 1, { 3, 0, 0 } },
133         { 2, { 1, 3, 0 } },
134         { 1, { 1, 0, 0 } }
135 },
136         upright[8] = {
137         { 2, { 3, 5, 0 } },
138         { 1, { 3, 0, 0 } },
139         { 0, { 0, 0, 0 } },
140         { 0, { 0, 0, 0 } },
141         { 0, { 0, 0, 0 } },
142         { 0, { 0, 0, 0 } },
143         { 0, { 0, 0, 0 } },
144         { 1, { 5, 0, 0 } }
145 },
146         lowleft[8] = {
147         { 3, { 7, 0, 1 } },
148         { 0, { 0, 0, 0 } },
149         { 0, { 0, 0, 0 } },
150         { 1, { 1, 0, 0 } },
151         { 2, { 1, 7, 0 } },
152         { 1, { 7, 0, 0 } },
153         { 0, { 0, 0, 0 } },
154         { 0, { 0, 0, 0 } }
155 },
156         lowright[8] = {
157         { 0, { 0, 0, 0 } },
158         { 1, { 7, 0, 0 } },
159         { 2, { 5, 7, 0 } },
160         { 1, { 5, 0, 0 } },
161         { 0, { 0, 0, 0 } },
162         { 0, { 0, 0, 0 } },
163         { 0, { 0, 0, 0 } },
164         { 0, { 0, 0, 0 } }
165 };
166
167
168 static const char       flavor[] = {
169         'O', '*', '#', '$', '%', '0', '@', '~'
170 };
171 static const short      xinc[] = {
172         1,  1,  1,  0, -1, -1, -1,  0
173 }, yinc[] = {
174         -1,  0,  1,  1,  1,  0, -1, -1
175 };
176 static struct   worm {
177         int orientation, head;
178         short *xpos, *ypos;
179 } *worm;
180
181 volatile sig_atomic_t sig_caught = 0;
182 void     onsig __P((int));
183 void    nomem(void);
184
185 int
186 main(argc, argv)
187         int argc;
188         char *argv[];
189 {
190         int x, y, h, n;
191         struct worm *w;
192         const struct options *op;
193         short *ip;
194         int CO, LI, last, bottom, ch, length, number, trail;
195         short **ref;
196         const char *field;
197         char *mp;
198         unsigned int delay = 0;
199
200         mp = NULL;
201         length = 16;
202         number = 3;
203         trail = ' ';
204         field = NULL;
205         while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
206                 switch(ch) {
207                 case 'd':
208                         if ((delay = (unsigned int)strtoul(optarg, (char **)NULL, 10)) < 1 || delay > 1000)
209                                 errx(1, "invalid delay (1-1000)");
210                         delay *= 1000;  /* ms -> us */
211                         break;
212                 case 'f':
213                         field = "WORM";
214                         break;
215                 case 'l':
216                         if ((length = atoi(optarg)) < 2 || length > 1024) {
217                                 errx(1, "invalid length (%d - %d).",
218                                      2, 1024);
219                         }
220                         break;
221                 case 'n':
222                         if ((number = atoi(optarg)) < 1) {
223                                 errx(1, "invalid number of worms.");
224                         }
225                         break;
226                 case 't':
227                         trail = '.';
228                         break;
229                 case '?':
230                 default:
231                         (void)fprintf(stderr,
232                             "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
233                         exit(1);
234                 }
235
236         if (!(worm = malloc((size_t)number *
237             sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
238                 nomem();
239         initscr();
240         CO = COLS;
241         LI = LINES;
242         last = CO - 1;
243         bottom = LI - 1;
244         if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
245                 nomem();
246         if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
247                 nomem();
248         for (n = 0; n < LI; ++n) {
249                 ref[n] = ip;
250                 ip += CO;
251         }
252         for (ip = ref[0], n = LI * CO; --n >= 0;)
253                 *ip++ = 0;
254         for (n = number, w = &worm[0]; --n >= 0; w++) {
255                 w->orientation = w->head = 0;
256                 if (!(ip = malloc((size_t)(length * sizeof(short)))))
257                         nomem();
258                 w->xpos = ip;
259                 for (x = length; --x >= 0;)
260                         *ip++ = -1;
261                 if (!(ip = malloc((size_t)(length * sizeof(short)))))
262                         nomem();
263                 w->ypos = ip;
264                 for (y = length; --y >= 0;)
265                         *ip++ = -1;
266         }
267
268         (void)signal(SIGHUP, onsig);
269         (void)signal(SIGINT, onsig);
270         (void)signal(SIGQUIT, onsig);
271         (void)signal(SIGSTOP, onsig);
272         (void)signal(SIGTSTP, onsig);
273         (void)signal(SIGTERM, onsig);
274
275         if (field) {
276                 const char *p = field;
277
278                 for (y = LI; --y >= 0;) {
279                         for (x = CO; --x >= 0;) {
280                                 addch(*p++);
281                                 if (!*p)
282                                         p = field;
283                         }
284                         refresh();
285                 }
286         }
287         for (;;) {
288                 refresh();
289                 if (sig_caught) {
290                         endwin();
291                         exit(0);
292                 }
293                 for (n = 0, w = &worm[0]; n < number; n++, w++) {
294                         if ((x = w->xpos[h = w->head]) < 0) {
295                                 mvaddch(y = w->ypos[h] = bottom,
296                                 x = w->xpos[h] = 0,
297                                 flavor[n % sizeof(flavor)]);
298                                 ref[y][x]++;
299                         }
300                         else
301                                 y = w->ypos[h];
302                         if (++h == length)
303                                 h = 0;
304                         if (w->xpos[w->head = h] >= 0) {
305                                 int x1, y1;
306
307                                 x1 = w->xpos[h];
308                                 y1 = w->ypos[h];
309                                 if (--ref[y1][x1] == 0) {
310                                         mvaddch(y1, x1, trail);
311                                 }
312                         }
313                         op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
314                         switch (op->nopts) {
315                         case 0:
316                                 refresh();
317                                 abort();
318                                 return(1);
319                         case 1:
320                                 w->orientation = op->opts[0];
321                                 break;
322                         default:
323                                 w->orientation =
324                                     op->opts[(int)random() % op->nopts];
325                         }
326                         mvaddch(y += yinc[w->orientation],
327                                 x += xinc[w->orientation],
328                                 flavor[n % sizeof(flavor)]);
329                         ref[w->ypos[h] = y][w->xpos[h] = x]++;
330                 }
331                 if (usleep(delay))
332                         onsig(SIGTERM);
333         }
334 }
335
336 void
337 onsig(signo)
338         int signo;
339 {
340         sig_caught = 1;
341 }
342
343 void
344 nomem()
345 {
346         errx(1, "not enough memory.");
347 }