1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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 ****************************************************************************/
30 @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
31 @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
32 @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
33 @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
34 @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
35 @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
36 @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
37 @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
38 @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
41 Caltech High Energy Physics
44 Hacks to turn this into a test frame for cursor movement:
45 Eric S. Raymond <esr@snark.thyrsus.com>
48 July 1995 (esr): worms is now in living color! :-)
51 -f fill screen with copies of 'WORM' at start.
52 -l <n> set worm length
53 -n <n> set number of worms
54 -t make worms leave droppings
55 -T <start> <end> set trace interval
56 -S set single-stepping during trace interval
57 -N suppress cursor-movement optimization
59 This program makes a good torture-test for the ncurses cursor-optimization
60 code. You can use -T to set the worm move interval over which movement
61 traces will be dumped. The program stops and waits for one character of
62 input at the beginning and end of the interval.
64 $Id: worm.c,v 1.48 2007/09/15 21:42:16 tom Exp $
67 #include <test.priv.h>
69 static chtype flavor[] =
71 'O', '*', '#', '$', '%', '0', '@',
73 static const short xinc[] =
75 1, 1, 1, 0, -1, -1, -1, 0
78 -1, 0, 1, 1, 1, 0, -1, -1
92 static const char *field;
93 static int length = 16, number = 3;
94 static chtype trail = ' ';
97 static int generation, trace_start, trace_end;
100 static const struct options {
197 onsig(int sig GCC_UNUSED)
200 ExitProgram(EXIT_FAILURE);
206 long r = (rand() & 077777);
207 return ((float) r / 32768.);
211 draw_worm(WINDOW *win, void *data)
213 WORM *w = (WORM *) data;
214 const struct options *op;
220 int bottom = LINES - 1;
225 if ((x = w->xpos[h = w->head]) < 0) {
226 wmove(win, y = w->ypos[h] = bottom, x = w->xpos[h] = 0);
227 waddch(win, w->attrs);
241 if (w->xpos[w->head = h] >= 0) {
247 && --refs[y1][x1] == 0) {
269 : normal))))[w->orientation];
276 w->orientation = op->opts[0];
279 w->orientation = op->opts[(int) (ranf() * (float) op->nopts)];
284 x += xinc[w->orientation];
285 y += yinc[w->orientation];
290 waddch(win, w->attrs);
300 #if !defined(NCURSES_VERSION_PATCH) || (NCURSES_VERSION_PATCH < 20070915)
302 use_window(WINDOW *win, int (*func)(WINDOW *, void *), void *data)
304 return func(win, data);
309 main(int argc, char *argv[])
318 setlocale(LC_ALL, "");
320 for (x = 1; x < argc; x++) {
332 if ((length = atoi(argv[x])) < 2 || length > 1024) {
333 fprintf(stderr, "%s: Invalid length\n", *argv);
334 ExitProgram(EXIT_FAILURE);
340 if ((number = atoi(argv[x])) < 1 || number > 40) {
341 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
342 ExitProgram(EXIT_FAILURE);
350 trace_start = atoi(argv[++x]);
351 trace_end = atoi(argv[++x]);
354 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
360 "usage: %s [-field] [-length #] [-number #] [-trail]\n", *argv);
361 ExitProgram(EXIT_FAILURE);
365 signal(SIGINT, onsig);
378 int bg = COLOR_BLACK;
380 #if HAVE_USE_DEFAULT_COLORS
381 if (use_default_colors() == OK)
385 #define SET_COLOR(num, fg) \
386 init_pair(num+1, fg, bg); \
387 flavor[num] |= COLOR_PAIR(num+1) | A_BOLD
389 SET_COLOR(0, COLOR_GREEN);
390 SET_COLOR(1, COLOR_RED);
391 SET_COLOR(2, COLOR_CYAN);
392 SET_COLOR(3, COLOR_WHITE);
393 SET_COLOR(4, COLOR_MAGENTA);
394 SET_COLOR(5, COLOR_BLUE);
395 SET_COLOR(6, COLOR_YELLOW);
399 refs = typeMalloc(short *, LINES);
400 for (y = 0; y < LINES; y++) {
401 refs[y] = typeMalloc(short, COLS);
402 for (x = 0; x < COLS; x++) {
408 /* if addressing the lower right corner doesn't work in your curses */
409 refs[bottom][last] = 1;
410 #endif /* BADCORNER */
412 for (n = number, w = &worm[0]; --n >= 0; w++) {
413 w->attrs = flavor[n % SIZEOF(flavor)];
417 if (!(ip = typeMalloc(short, (length + 1)))) {
418 fprintf(stderr, "%s: out of memory\n", *argv);
419 ExitProgram(EXIT_FAILURE);
422 for (x = length; --x >= 0;)
424 if (!(ip = typeMalloc(short, (length + 1)))) {
425 fprintf(stderr, "%s: out of memory\n", *argv);
426 ExitProgram(EXIT_FAILURE);
429 for (y = length; --y >= 0;)
435 for (y = bottom; --y >= 0;) {
436 for (x = COLS; --x >= 0;) {
437 addch((chtype) (*p++));
445 nodelay(stdscr, TRUE);
450 if ((ch = getch()) > 0) {
452 if (trace_start || trace_end) {
453 if (generation == trace_start) {
456 } else if (generation == trace_end) {
465 if (ch == KEY_RESIZE) {
466 if (last != COLS - 1) {
467 for (y = 0; y <= bottom; y++) {
468 refs[y] = typeRealloc(short, COLS, refs[y]);
469 for (x = last + 1; x < COLS; x++)
474 if (bottom != LINES - 1) {
475 for (y = LINES; y <= bottom; y++)
477 refs = typeRealloc(short *, LINES, refs);
478 for (y = bottom + 1; y < LINES; y++) {
479 refs[y] = typeMalloc(short, COLS);
480 for (x = 0; x < COLS; x++)
488 * Make it simple to put this into single-step mode, or resume
489 * normal operation -T.Dickey
494 } else if (ch == 's') {
495 nodelay(stdscr, FALSE);
496 } else if (ch == ' ') {
497 nodelay(stdscr, TRUE);
501 for (n = 0, w = &worm[0]; n < number; n++, w++) {
502 if (use_window(stdscr, draw_worm, w))
511 for (y = 0; y < LINES; y++) {
515 for (n = number, w = &worm[0]; --n >= 0; w++) {
520 ExitProgram(EXIT_SUCCESS);