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