worms.c revision 1.11 1 1.11 hubertf /* $NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 hubertf Exp $ */
2 1.6 cgd
3 1.1 cgd /*
4 1.6 cgd * Copyright (c) 1980, 1993
5 1.6 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.9 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.9 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 1.9 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.6 cgd #if 0
44 1.6 cgd static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
45 1.6 cgd #else
46 1.11 hubertf __RCSID("$NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 hubertf Exp $");
47 1.6 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd *
52 1.1 cgd * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
53 1.1 cgd * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
54 1.1 cgd * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
55 1.1 cgd * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
56 1.1 cgd * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
57 1.1 cgd * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
58 1.1 cgd * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
59 1.1 cgd * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
60 1.1 cgd * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
61 1.1 cgd *
62 1.1 cgd * Eric P. Scott
63 1.1 cgd * Caltech High Energy Physics
64 1.1 cgd * October, 1980
65 1.1 cgd *
66 1.1 cgd */
67 1.1 cgd #include <sys/types.h>
68 1.6 cgd
69 1.11 hubertf #include <curses.h>
70 1.11 hubertf #include <err.h>
71 1.6 cgd #include <signal.h>
72 1.1 cgd #include <stdio.h>
73 1.2 mycroft #include <stdlib.h>
74 1.6 cgd #include <unistd.h>
75 1.1 cgd
76 1.11 hubertf static const struct options {
77 1.1 cgd int nopts;
78 1.1 cgd int opts[3];
79 1.1 cgd }
80 1.1 cgd normal[8] = {
81 1.1 cgd { 3, { 7, 0, 1 } },
82 1.1 cgd { 3, { 0, 1, 2 } },
83 1.1 cgd { 3, { 1, 2, 3 } },
84 1.1 cgd { 3, { 2, 3, 4 } },
85 1.1 cgd { 3, { 3, 4, 5 } },
86 1.1 cgd { 3, { 4, 5, 6 } },
87 1.1 cgd { 3, { 5, 6, 7 } },
88 1.1 cgd { 3, { 6, 7, 0 } }
89 1.1 cgd }, upper[8] = {
90 1.1 cgd { 1, { 1, 0, 0 } },
91 1.1 cgd { 2, { 1, 2, 0 } },
92 1.1 cgd { 0, { 0, 0, 0 } },
93 1.1 cgd { 0, { 0, 0, 0 } },
94 1.1 cgd { 0, { 0, 0, 0 } },
95 1.1 cgd { 2, { 4, 5, 0 } },
96 1.1 cgd { 1, { 5, 0, 0 } },
97 1.1 cgd { 2, { 1, 5, 0 } }
98 1.1 cgd },
99 1.1 cgd left[8] = {
100 1.1 cgd { 0, { 0, 0, 0 } },
101 1.1 cgd { 0, { 0, 0, 0 } },
102 1.1 cgd { 0, { 0, 0, 0 } },
103 1.1 cgd { 2, { 2, 3, 0 } },
104 1.1 cgd { 1, { 3, 0, 0 } },
105 1.1 cgd { 2, { 3, 7, 0 } },
106 1.1 cgd { 1, { 7, 0, 0 } },
107 1.1 cgd { 2, { 7, 0, 0 } }
108 1.1 cgd },
109 1.1 cgd right[8] = {
110 1.1 cgd { 1, { 7, 0, 0 } },
111 1.1 cgd { 2, { 3, 7, 0 } },
112 1.1 cgd { 1, { 3, 0, 0 } },
113 1.1 cgd { 2, { 3, 4, 0 } },
114 1.1 cgd { 0, { 0, 0, 0 } },
115 1.1 cgd { 0, { 0, 0, 0 } },
116 1.1 cgd { 0, { 0, 0, 0 } },
117 1.1 cgd { 2, { 6, 7, 0 } }
118 1.1 cgd },
119 1.1 cgd lower[8] = {
120 1.1 cgd { 0, { 0, 0, 0 } },
121 1.1 cgd { 2, { 0, 1, 0 } },
122 1.1 cgd { 1, { 1, 0, 0 } },
123 1.1 cgd { 2, { 1, 5, 0 } },
124 1.1 cgd { 1, { 5, 0, 0 } },
125 1.1 cgd { 2, { 5, 6, 0 } },
126 1.1 cgd { 0, { 0, 0, 0 } },
127 1.1 cgd { 0, { 0, 0, 0 } }
128 1.1 cgd },
129 1.1 cgd upleft[8] = {
130 1.1 cgd { 0, { 0, 0, 0 } },
131 1.1 cgd { 0, { 0, 0, 0 } },
132 1.1 cgd { 0, { 0, 0, 0 } },
133 1.1 cgd { 0, { 0, 0, 0 } },
134 1.1 cgd { 0, { 0, 0, 0 } },
135 1.1 cgd { 1, { 3, 0, 0 } },
136 1.1 cgd { 2, { 1, 3, 0 } },
137 1.1 cgd { 1, { 1, 0, 0 } }
138 1.1 cgd },
139 1.1 cgd upright[8] = {
140 1.1 cgd { 2, { 3, 5, 0 } },
141 1.1 cgd { 1, { 3, 0, 0 } },
142 1.1 cgd { 0, { 0, 0, 0 } },
143 1.1 cgd { 0, { 0, 0, 0 } },
144 1.1 cgd { 0, { 0, 0, 0 } },
145 1.1 cgd { 0, { 0, 0, 0 } },
146 1.1 cgd { 0, { 0, 0, 0 } },
147 1.1 cgd { 1, { 5, 0, 0 } }
148 1.1 cgd },
149 1.1 cgd lowleft[8] = {
150 1.1 cgd { 3, { 7, 0, 1 } },
151 1.1 cgd { 0, { 0, 0, 0 } },
152 1.1 cgd { 0, { 0, 0, 0 } },
153 1.1 cgd { 1, { 1, 0, 0 } },
154 1.1 cgd { 2, { 1, 7, 0 } },
155 1.1 cgd { 1, { 7, 0, 0 } },
156 1.1 cgd { 0, { 0, 0, 0 } },
157 1.1 cgd { 0, { 0, 0, 0 } }
158 1.1 cgd },
159 1.1 cgd lowright[8] = {
160 1.1 cgd { 0, { 0, 0, 0 } },
161 1.1 cgd { 1, { 7, 0, 0 } },
162 1.1 cgd { 2, { 5, 7, 0 } },
163 1.1 cgd { 1, { 5, 0, 0 } },
164 1.1 cgd { 0, { 0, 0, 0 } },
165 1.1 cgd { 0, { 0, 0, 0 } },
166 1.1 cgd { 0, { 0, 0, 0 } },
167 1.1 cgd { 0, { 0, 0, 0 } }
168 1.1 cgd };
169 1.1 cgd
170 1.1 cgd
171 1.11 hubertf static const char flavor[] = {
172 1.1 cgd 'O', '*', '#', '$', '%', '0', '@', '~'
173 1.1 cgd };
174 1.11 hubertf static const short xinc[] = {
175 1.1 cgd 1, 1, 1, 0, -1, -1, -1, 0
176 1.1 cgd }, yinc[] = {
177 1.1 cgd -1, 0, 1, 1, 1, 0, -1, -1
178 1.1 cgd };
179 1.1 cgd static struct worm {
180 1.1 cgd int orientation, head;
181 1.1 cgd short *xpos, *ypos;
182 1.1 cgd } *worm;
183 1.1 cgd
184 1.11 hubertf volatile sig_atomic_t sig_caught = 0;
185 1.11 hubertf
186 1.9 lukem int main __P((int, char **));
187 1.10 hubertf void nomem __P((void)) __attribute__((__noreturn__));
188 1.11 hubertf void onsig __P((int));
189 1.2 mycroft
190 1.6 cgd int
191 1.1 cgd main(argc, argv)
192 1.1 cgd int argc;
193 1.6 cgd char *argv[];
194 1.1 cgd {
195 1.9 lukem int x, y, h, n;
196 1.9 lukem struct worm *w;
197 1.11 hubertf const struct options *op;
198 1.9 lukem short *ip;
199 1.11 hubertf int CO, LI, last, bottom, ch, length, number, trail;
200 1.1 cgd short **ref;
201 1.11 hubertf const char *field;
202 1.11 hubertf char *mp;
203 1.11 hubertf unsigned int delay = 0;
204 1.1 cgd
205 1.9 lukem mp = NULL;
206 1.1 cgd length = 16;
207 1.1 cgd number = 3;
208 1.1 cgd trail = ' ';
209 1.1 cgd field = NULL;
210 1.11 hubertf while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
211 1.1 cgd switch(ch) {
212 1.11 hubertf case 'd':
213 1.11 hubertf if ((delay = (unsigned int)strtoul(optarg, (char **)NULL, 10)) < 1 || delay > 1000)
214 1.11 hubertf errx(1, "invalid delay (1-1000)");
215 1.11 hubertf delay *= 1000; /* ms -> us */
216 1.11 hubertf break;
217 1.1 cgd case 'f':
218 1.1 cgd field = "WORM";
219 1.1 cgd break;
220 1.1 cgd case 'l':
221 1.1 cgd if ((length = atoi(optarg)) < 2 || length > 1024) {
222 1.11 hubertf errx(1, "invalid length (%d - %d).",
223 1.1 cgd 2, 1024);
224 1.1 cgd }
225 1.1 cgd break;
226 1.1 cgd case 'n':
227 1.1 cgd if ((number = atoi(optarg)) < 1) {
228 1.11 hubertf errx(1, "invalid number of worms.");
229 1.1 cgd }
230 1.1 cgd break;
231 1.1 cgd case 't':
232 1.1 cgd trail = '.';
233 1.1 cgd break;
234 1.1 cgd case '?':
235 1.1 cgd default:
236 1.1 cgd (void)fprintf(stderr,
237 1.11 hubertf "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
238 1.1 cgd exit(1);
239 1.1 cgd }
240 1.1 cgd
241 1.6 cgd if (!(worm = malloc((size_t)number *
242 1.6 cgd sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
243 1.1 cgd nomem();
244 1.11 hubertf initscr();
245 1.11 hubertf CO = COLS;
246 1.11 hubertf LI = LINES;
247 1.4 mycroft last = CO - 1;
248 1.4 mycroft bottom = LI - 1;
249 1.6 cgd if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
250 1.1 cgd nomem();
251 1.6 cgd if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
252 1.1 cgd nomem();
253 1.1 cgd for (n = 0; n < LI; ++n) {
254 1.1 cgd ref[n] = ip;
255 1.1 cgd ip += CO;
256 1.1 cgd }
257 1.1 cgd for (ip = ref[0], n = LI * CO; --n >= 0;)
258 1.1 cgd *ip++ = 0;
259 1.1 cgd for (n = number, w = &worm[0]; --n >= 0; w++) {
260 1.1 cgd w->orientation = w->head = 0;
261 1.6 cgd if (!(ip = malloc((size_t)(length * sizeof(short)))))
262 1.1 cgd nomem();
263 1.1 cgd w->xpos = ip;
264 1.1 cgd for (x = length; --x >= 0;)
265 1.1 cgd *ip++ = -1;
266 1.6 cgd if (!(ip = malloc((size_t)(length * sizeof(short)))))
267 1.1 cgd nomem();
268 1.1 cgd w->ypos = ip;
269 1.1 cgd for (y = length; --y >= 0;)
270 1.1 cgd *ip++ = -1;
271 1.1 cgd }
272 1.1 cgd
273 1.1 cgd (void)signal(SIGHUP, onsig);
274 1.1 cgd (void)signal(SIGINT, onsig);
275 1.1 cgd (void)signal(SIGQUIT, onsig);
276 1.1 cgd (void)signal(SIGSTOP, onsig);
277 1.1 cgd (void)signal(SIGTSTP, onsig);
278 1.1 cgd (void)signal(SIGTERM, onsig);
279 1.1 cgd
280 1.1 cgd if (field) {
281 1.11 hubertf const char *p = field;
282 1.1 cgd
283 1.11 hubertf for (y = LI; --y >= 0;) {
284 1.1 cgd for (x = CO; --x >= 0;) {
285 1.11 hubertf addch(*p++);
286 1.1 cgd if (!*p)
287 1.1 cgd p = field;
288 1.1 cgd }
289 1.11 hubertf refresh();
290 1.1 cgd }
291 1.1 cgd }
292 1.1 cgd for (;;) {
293 1.11 hubertf refresh();
294 1.11 hubertf if (sig_caught) {
295 1.11 hubertf endwin();
296 1.11 hubertf exit(0);
297 1.11 hubertf }
298 1.11 hubertf if (delay) usleep(delay);
299 1.1 cgd for (n = 0, w = &worm[0]; n < number; n++, w++) {
300 1.1 cgd if ((x = w->xpos[h = w->head]) < 0) {
301 1.11 hubertf mvaddch(y = w->ypos[h] = bottom,
302 1.11 hubertf x = w->xpos[h] = 0,
303 1.11 hubertf flavor[n % sizeof(flavor)]);
304 1.1 cgd ref[y][x]++;
305 1.1 cgd }
306 1.1 cgd else
307 1.1 cgd y = w->ypos[h];
308 1.1 cgd if (++h == length)
309 1.1 cgd h = 0;
310 1.1 cgd if (w->xpos[w->head = h] >= 0) {
311 1.9 lukem int x1, y1;
312 1.1 cgd
313 1.1 cgd x1 = w->xpos[h];
314 1.1 cgd y1 = w->ypos[h];
315 1.1 cgd if (--ref[y1][x1] == 0) {
316 1.11 hubertf mvaddch(y1, x1, trail);
317 1.1 cgd }
318 1.1 cgd }
319 1.1 cgd op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
320 1.1 cgd switch (op->nopts) {
321 1.1 cgd case 0:
322 1.11 hubertf refresh();
323 1.1 cgd abort();
324 1.9 lukem return(1);
325 1.1 cgd case 1:
326 1.1 cgd w->orientation = op->opts[0];
327 1.1 cgd break;
328 1.1 cgd default:
329 1.1 cgd w->orientation =
330 1.1 cgd op->opts[(int)random() % op->nopts];
331 1.1 cgd }
332 1.11 hubertf mvaddch(y += yinc[w->orientation],
333 1.11 hubertf x += xinc[w->orientation],
334 1.11 hubertf flavor[n % sizeof(flavor)]);
335 1.1 cgd ref[w->ypos[h] = y][w->xpos[h] = x]++;
336 1.1 cgd }
337 1.1 cgd }
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd void
341 1.6 cgd onsig(signo)
342 1.11 hubertf int signo __attribute__((__unused__));
343 1.6 cgd {
344 1.11 hubertf sig_caught = 1;
345 1.1 cgd }
346 1.1 cgd
347 1.9 lukem void
348 1.1 cgd nomem()
349 1.1 cgd {
350 1.11 hubertf errx(1, "not enough memory.");
351 1.1 cgd }
352