3 @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
4 @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
5 @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
6 @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
7 @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
8 @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
9 @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
10 @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
11 @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
14 Caltech High Energy Physics
17 Hacks to turn this into a test frame for cursor movement:
18 Eric S. Raymond <esr@snark.thyrsus.com>
21 July 1995 (esr): worms is now in living color! :-)
24 -f fill screen with copies of 'WORM' at start.
25 -l <n> set worm length
26 -n <n> set number of worms
27 -t make worms leave droppings
28 -T <start> <end> set trace interval
29 -S set single-stepping during trace interval
30 -N suppress cursor-movement optimization
32 This program makes a good torture-test for the ncurses cursor-optimization
33 code. You can use -T to set the worm move interval over which movement
34 traces will be dumped. The program stops and waits for one character of
35 input at the beginning and end of the interval.
37 $Id: worm.c,v 1.21 1998/01/30 10:17:59 tom Exp $
40 #include <test.priv.h>
42 #include <term.h> /* for tparm() */
46 #define cursor(col,row) move(row,col)
49 static chtype flavor[]={
50 'O' , '*', '#', '$', '%', '0', '@',
52 #define MAXWORMS (sizeof(flavor)/sizeof(chtype))
53 static const short xinc[]={
54 1, 1, 1, 0, -1, -1, -1, 0
56 -1, 0, 1, 1, 1, 0, -1, -1
59 int orientation, head;
63 static const char *field;
64 static int length=16, number=3;
65 static chtype trail=' ';
68 int generation, trace_start, trace_end, singlestep;
70 static const struct options {
156 static RETSIGTYPE onsig(int sig);
157 static float ranf(void);
160 main(int argc, char *argv[])
165 const struct options *op;
170 for (x=1;x<argc;x++) {
179 if (++x==argc) goto usage;
180 if ((length=atoi(argv[x]))<2||length>1024) {
181 fprintf(stderr,"%s: Invalid length\n",*argv);
186 if (++x==argc) goto usage;
187 if ((number=atoi(argv[x]))<1||number>40) {
188 fprintf(stderr,"%s: Invalid number of worms\n",*argv);
200 trace_start = atoi(argv[++x]);
201 trace_end = atoi(argv[++x]);
204 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
209 fprintf(stderr, "usage: %s [-field] [-length #] [-number #] [-trail]\n",*argv);
215 signal(SIGINT, onsig);
229 int bg = COLOR_BLACK;
231 #ifdef NCURSES_VERSION
232 if (use_default_colors() == OK)
236 init_pair(COLOR_GREEN, COLOR_GREEN, bg);
237 init_pair(COLOR_RED, COLOR_RED, bg);
238 init_pair(COLOR_CYAN, COLOR_CYAN, bg);
239 init_pair(COLOR_WHITE, COLOR_WHITE, bg);
240 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, bg);
241 init_pair(COLOR_BLUE, COLOR_BLUE, bg);
242 init_pair(COLOR_YELLOW, COLOR_YELLOW, bg);
244 flavor[0] |= COLOR_PAIR(COLOR_GREEN) | A_BOLD;
245 flavor[1] |= COLOR_PAIR(COLOR_RED) | A_BOLD;
246 flavor[2] |= COLOR_PAIR(COLOR_CYAN) | A_BOLD;
247 flavor[3] |= COLOR_PAIR(COLOR_WHITE) | A_BOLD;
248 flavor[4] |= COLOR_PAIR(COLOR_MAGENTA) | A_BOLD;
249 flavor[5] |= COLOR_PAIR(COLOR_BLUE) | A_BOLD;
250 flavor[6] |= COLOR_PAIR(COLOR_YELLOW) | A_BOLD;
254 ip=(short *)malloc(LINES*COLS*sizeof (short));
257 ref[n++]=ip; ip+=COLS;
259 for (ip=ref[0],n=LINES*COLS;--n>=0;) *ip++=0;
262 /* if addressing the lower right corner doesn't work in your curses */
264 #endif /* BADCORNER */
266 for (n=number, w= &worm[0];--n>=0;w++) {
267 w->orientation=w->head=0;
268 if (!(ip=(short *)malloc((length+1)*sizeof (short)))) {
269 fprintf(stderr,"%s: out of memory\n",*argv);
273 for (x=length;--x>=0;) *ip++ = -1;
274 if (!(ip=(short *)malloc((length+1)*sizeof (short)))) {
275 fprintf(stderr,"%s: out of memory\n",*argv);
279 for (y=length;--y>=0;) *ip++ = -1;
282 register const char *p;
284 for (y=bottom;--y>=0;) {
285 for (x=COLS;--x>=0;) {
286 addch((chtype)(*p++));
296 if (trace_start || trace_end) {
297 if (generation == trace_start) {
300 } else if (generation == trace_end) {
305 if (singlestep && generation > trace_start && generation < trace_end)
312 for (n=0,w= &worm[0];n<number;n++,w++) {
313 if ((x=w->xpos[h=w->head])<0) {
314 cursor(x=w->xpos[h]=0,y=w->ypos[h]=bottom);
315 addch(flavor[n % MAXWORMS]);
319 if (++h==length) h=0;
320 if (w->xpos[w->head=h]>=0) {
322 x1=w->xpos[h]; y1=w->ypos[h];
323 if (--ref[y1][x1]==0) {
324 cursor(x1,y1); addch(trail);
327 op= &(x==0 ? (y==0 ? upleft : (y==bottom ? lowleft : left)) :
328 (x==last ? (y==0 ? upright : (y==bottom ? lowright : right)) :
329 (y==0 ? upper : (y==bottom ? lower : normal))))[w->orientation];
337 w->orientation=op->opts[0];
340 w->orientation=op->opts[(int)(ranf()*(float)op->nopts)];
342 cursor(x+=xinc[w->orientation], y+=yinc[w->orientation]);
345 addch(flavor[n % MAXWORMS]);
346 ref[w->ypos[h]=y][w->xpos[h]=x]++;
353 onsig(int sig GCC_UNUSED)
369 rv =((float)r/32767.);