1 /****************************************************************************
2 * Copyright (c) 2005 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
31 @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
32 @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
33 @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
34 @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
35 @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
36 @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
37 @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
38 @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
39 @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
42 Caltech High Energy Physics
45 Color by Eric S. Raymond
49 -f fill screen with copies of 'WORM' at start.
50 -l <n> set worm length
51 -n <n> set number of worms
52 -t make worms leave droppings
54 $Id: worm.c,v 1.16 2008/07/13 16:08:17 wmcbrine Exp $
63 static chtype flavor
[FLAVORS
] =
65 'O', '*', '#', '$', '%', '0', '@'
68 static const short xinc
[] =
70 1, 1, 1, 0, -1, -1, -1, 0
74 -1, 0, 1, 1, 1, 0, -1, -1
79 int orientation
, head
;
83 static const char *field
;
84 static int length
= 16, number
= 3;
85 static chtype trail
= ' ';
87 static const struct options
93 { 3, { 7, 0, 1 } }, { 3, { 0, 1, 2 } }, { 3, { 1, 2, 3 } },
94 { 3, { 2, 3, 4 } }, { 3, { 3, 4, 5 } }, { 3, { 4, 5, 6 } },
95 { 3, { 5, 6, 7 } }, { 3, { 6, 7, 0 } }
99 { 1, { 1, 0, 0 } }, { 2, { 1, 2, 0 } }, { 0, { 0, 0, 0 } },
100 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }, { 2, { 4, 5, 0 } },
101 { 1, { 5, 0, 0 } }, { 2, { 1, 5, 0 } }
105 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } },
106 { 2, { 2, 3, 0 } }, { 1, { 3, 0, 0 } }, { 2, { 3, 7, 0 } },
107 { 1, { 7, 0, 0 } }, { 2, { 7, 0, 0 } }
111 { 1, { 7, 0, 0 } }, { 2, { 3, 7, 0 } }, { 1, { 3, 0, 0 } },
112 { 2, { 3, 4, 0 } }, { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } },
113 { 0, { 0, 0, 0 } }, { 2, { 6, 7, 0 } }
117 { 0, { 0, 0, 0 } }, { 2, { 0, 1, 0 } }, { 1, { 1, 0, 0 } },
118 { 2, { 1, 5, 0 } }, { 1, { 5, 0, 0 } }, { 2, { 5, 6, 0 } },
119 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }
123 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } },
124 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }, { 1, { 3, 0, 0 } },
125 { 2, { 1, 3, 0 } }, { 1, { 1, 0, 0 } }
129 { 2, { 3, 5, 0 } }, { 1, { 3, 0, 0 } }, { 0, { 0, 0, 0 } },
130 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } },
131 { 0, { 0, 0, 0 } }, { 1, { 5, 0, 0 } }
135 { 3, { 7, 0, 1 } }, { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } },
136 { 1, { 1, 0, 0 } }, { 2, { 1, 7, 0 } }, { 1, { 7, 0, 0 } },
137 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }
141 { 0, { 0, 0, 0 } }, { 1, { 7, 0, 0 } }, { 2, { 5, 7, 0 } },
142 { 1, { 5, 0, 0 } }, { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } },
143 { 0, { 0, 0, 0 } }, { 0, { 0, 0, 0 } }
146 static void cleanup(void)
154 int main(int argc
, char *argv
[])
156 const struct options
*op
;
159 int x
, y
, n
, h
, last
, bottom
, seed
;
161 for (x
= 1; x
< argc
; x
++)
177 if ((length
= atoi(argv
[x
])) < 2 || length
> 1024)
179 fprintf(stderr
, "%s: Invalid length\n", *argv
);
188 if ((number
= atoi(argv
[x
])) < 1 || number
> 40)
190 fprintf(stderr
, "%s: Invalid number of worms\n", *argv
);
200 fprintf(stderr
, "usage: %s [-field] [-length #] "
201 "[-number #] [-trail]\n", *argv
);
207 Xinitscr(argc
, argv
);
211 seed
= time((time_t *)0);
217 keypad(stdscr
, TRUE
);
227 int bg
= COLOR_BLACK
;
230 # if defined(NCURSES_VERSION) || (defined(PDC_BUILD) && PDC_BUILD > 3000)
231 if (use_default_colors() == OK
)
235 # define SET_COLOR(num, fg) \
236 init_pair(num + 1, fg, bg); \
237 flavor[num] |= COLOR_PAIR(num + 1) | A_BOLD
239 SET_COLOR(0, COLOR_GREEN
);
240 SET_COLOR(1, COLOR_RED
);
241 SET_COLOR(2, COLOR_CYAN
);
242 SET_COLOR(3, COLOR_WHITE
);
243 SET_COLOR(4, COLOR_MAGENTA
);
244 SET_COLOR(5, COLOR_BLUE
);
245 SET_COLOR(6, COLOR_YELLOW
);
249 ref
= malloc(sizeof(short *) * LINES
);
251 for (y
= 0; y
< LINES
; y
++)
253 ref
[y
] = malloc(sizeof(short) * COLS
);
255 for (x
= 0; x
< COLS
; x
++)
260 /* if addressing the lower right corner doesn't work in your curses */
262 ref
[bottom
][last
] = 1;
265 for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++)
267 w
->orientation
= w
->head
= 0;
269 if ((ip
= malloc(sizeof(short) * (length
+ 1))) == NULL
)
271 fprintf(stderr
, "%s: out of memory\n", *argv
);
277 for (x
= length
; --x
>= 0;)
280 if ((ip
= malloc(sizeof(short) * (length
+ 1))) == NULL
)
282 fprintf(stderr
, "%s: out of memory\n", *argv
);
288 for (y
= length
; --y
>= 0;)
294 const char *p
= field
;
296 for (y
= bottom
; --y
>= 0;)
297 for (x
= COLS
; --x
>= 0;)
299 addch((chtype
) (*p
++));
308 nodelay(stdscr
, TRUE
);
314 if ((ch
= getch()) > 0)
317 if (ch
== KEY_RESIZE
)
323 if (last
!= COLS
- 1)
325 for (y
= 0; y
<= bottom
; y
++)
327 ref
[y
] = realloc(ref
[y
], sizeof(short) * COLS
);
329 for (x
= last
+ 1; x
< COLS
; x
++)
336 if (bottom
!= LINES
- 1)
338 for (y
= LINES
; y
<= bottom
; y
++)
341 ref
= realloc(ref
, sizeof(short *) * LINES
);
343 for (y
= bottom
+ 1; y
< LINES
; y
++)
345 ref
[y
] = malloc(sizeof(short) * COLS
);
347 for (x
= 0; x
< COLS
; x
++)
355 #endif /* KEY_RESIZE */
357 /* Make it simple to put this into single-step mode,
358 or resume normal operation - T. Dickey */
366 nodelay(stdscr
, FALSE
);
368 nodelay(stdscr
, TRUE
);
371 for (n
= 0, w
= &worm
[0]; n
< number
; n
++, w
++)
373 if ((x
= w
->xpos
[h
= w
->head
]) < 0)
375 move(y
= w
->ypos
[h
] = bottom
, x
= w
->xpos
[h
] = 0);
376 addch(flavor
[n
% FLAVORS
]);
391 if (w
->xpos
[w
->head
= h
] >= 0)
396 if (y1
< LINES
&& x1
< COLS
&& --ref
[y1
][x1
] == 0)
403 op
= &(x
== 0 ? (y
== 0 ? upleft
:
404 (y
== bottom
? lowleft
: left
)) :
405 (x
== last
? (y
== 0 ? upright
:
406 (y
== bottom
? lowright
: right
)) :
408 (y
== bottom
? lower
: normal
))))
417 w
->orientation
= op
->opts
[0];
420 w
->orientation
= op
->opts
[rand() % op
->nopts
];
423 move(y
+= yinc
[w
->orientation
], x
+= xinc
[w
->orientation
]);
428 addch(flavor
[n
% FLAVORS
]);
429 ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;