1 /* -*- Mode: C; tab-width: 4 -*- */
3 * ant --- Chris Langton's generalized turing machine ants (also known
4 * as Greg Turk's turmites) whose tape is the screen
7 #if !defined( lint ) && !defined( SABER )
8 static const char sccsid[] = "@(#)ant.c 4.04 97/07/28 xlockmore";
13 * Copyright (c) 1995 by David Bagley.
15 * Permission to use, copy, modify, and distribute this software and its
16 * documentation for any purpose and without fee is hereby granted,
17 * provided that the above copyright notice appear in all copies and that
18 * both that copyright notice and this permission notice appear in
19 * supporting documentation.
21 * This file is provided AS IS with no warranties of any kind. The author
22 * shall have no liability with respect to the infringement of copyrights,
23 * trade secrets or any patents by this file or any part thereof. In no
24 * event will the author be liable for any lost revenue or profits or
25 * other special, indirect and consequential damages.
28 * 10-May-97: Compatible with xscreensaver
29 * 16-Apr-97: -neighbors 3 and 8 added
30 * 01-Jan-97: Updated ant.c to handle more kinds of ants. Thanks to
31 * J Austin David <Austin.David@tlogic.com>. Check it out in
32 * java at http://havoc.gtf.gatech.edu/austin He thought up the
34 * 04-Apr-96: -neighbors 6 runtime-time option added for hexagonal ants
35 * (bees), coded from an idea of Jim Propp's in Science News,
36 * Oct 28, 1995 VOL. 148 page 287
37 * 20-Sep-95: Memory leak in ant fixed. Now random colors.
38 * 05-Sep-95: Coded from A.K. Dewdney's "Computer Recreations", Scientific
39 * American Magazine" Sep 1989 pp 180-183, Mar 1990 p 121
40 * Also used Ian Stewart's Mathematical Recreations, Scientific
41 * American Jul 1994 pp 104-107
42 * also used demon.c and life.c as a guide.
46 Species Grid Number of Neigbors
47 ------- ---- ------------------
50 Bees Triangle 3 (9? or 12) <- 9 and 12 are not implemented
52 Neighbors 6 and neighbors 3 produce the same Turk ants.
56 # define PROGCLASS "Ant"
57 # define HACK_INIT init_ant
58 # define HACK_DRAW draw_ant
59 # define ant_opts xlockmore_opts
60 # define DEFAULTS "*delay: 1000 \n" \
65 # include "xlockmore.h" /* in xscreensaver distribution */
67 #else /* STANDALONE */
68 # include "xlock.h" /* in xlockmore distribution */
70 #endif /* STANDALONE */
72 #define DEF_TRUCHET "True"
78 #endif /* !STANDALONE */
82 static XrmOptionDescRec opts[] =
84 {"-truchet", ".ant.truchet", XrmoptionNoArg, (caddr_t) "on"},
85 {"+truchet", ".ant.truchet", XrmoptionNoArg, (caddr_t) "off"},
88 {"-neighbors", ".ant.neighbors", XrmoptionNoArg, (caddr_t) "on"},
89 {"+neighbors", ".ant.neighbors", XrmoptionNoArg, (caddr_t) "off"}
90 #endif /* STANDALONE */
93 static argtype vars[] =
95 {(caddr_t *) & truchet, "truchet", "Truchet", DEF_TRUCHET, t_Bool},
97 {(caddr_t *) & neighbors, "neighbors", "Neighbors", 0, t_Bool}
98 #endif /* STANDALONE */
100 static OptionStruct desc[] =
102 {"-/+truchet", "turn on/off Truchet lines"}
105 ModeSpecOpt ant_opts =
106 {2, opts, 1, vars, desc};
109 #define ANTBITS(n,w,h)\
110 ap->pixmaps[ap->init_bits++]=\
111 XCreatePixmapFromBitmapData(display,window,(char *)n,w,h,1,0,1)
113 /* If you change the table you may have to change the following 2 constants */
117 #define PATTERNSIZE 8
118 #define REDRAWSTEP 2000 /* How much tape to draw per cycle */
119 #define MINGRIDSIZE 24
122 #define NEIGHBORKINDS 2
125 static XPoint hexagonUnit[6] =
135 static XPoint triangleUnit[2][3] =
149 #endif /* STANDALONE */
152 static unsigned char patterns[COLORS - 1][PATTERNSIZE] =
154 {0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22}, /* grey+white | stripe */
155 {0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00}, /* spots */
156 {0x89, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11}, /* lt. / stripe */
157 {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}, /* | bars */
158 {0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa}, /* 50% grey */
159 {0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00}, /* - bars */
160 {0xee, 0xdd, 0xbb, 0x77, 0xee, 0xdd, 0xbb, 0x76}, /* dark \ stripe */
161 {0xff, 0x99, 0x99, 0xff, 0xff, 0x99, 0x99, 0xff}, /* spots */
162 {0xaa, 0xff, 0xff, 0x55, 0xaa, 0xff, 0xff, 0x55}, /* black+grey - stripe */
163 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} /* black */
186 unsigned char ncolors, nstates;
188 int redrawing, redrawpos;
189 int truchet; /* Only for Turk modes */
190 statestruct machine[COLORS * STATES];
192 unsigned char *truchet_state;
194 unsigned char colors[COLORS - 1];
196 Pixmap pixmaps[COLORS - 1];
197 XPoint hexagonList[7];
198 XPoint triangleList[2][4];
201 static int initVal[NEIGHBORKINDS] =
202 {3, 6}; /* Neighborhoods, 8 just makes a mess */
205 /* Relative ant moves */
206 #define FS 0 /* Step */
207 #define TRS 1 /* Turn right, then step */
208 #define THRS 2 /* Turn hard right, then step */
209 #define TBS 3 /* Turn back, then step */
210 #define THLS 4 /* Turn hard left, then step */
211 #define TLS 5 /* Turn left, then step */
212 #define SF 6 /* Step */
213 #define STR 7 /* Step then turn right */
214 #define STHR 8 /* Step then turn hard right */
215 #define STB 9 /* Step then turn back */
216 #define STHL 10 /* Step then turn hard left */
217 #define STL 11 /* Step then turn left */
219 static antfarmstruct *antfarms = NULL;
221 /* LANGTON'S ANT (10) Chaotic after 500, Builder after 10,000 (104p) */
222 /* TURK'S 100 ANT Always chaotic?, tested past 150,000,000 */
223 /* TURK'S 101 ANT Always chaotic? */
224 /* TURK'S 110 ANT Builder at 150 (18p) */
225 /* TURK'S 1000 ANT Always chaotic? */
226 /* TURK'S 1100 SYMMETRIC ANT all even run 1's and 0's are symmetric */
227 /* other examples 1001, 110011, 110000, 1001101 */
228 /* TURK'S 1101 ANT Builder after 250,000 (388p) */
229 /* Once saw a chess horse type builder (i.e. non-45 degree builder) */
232 /* All alternating 10 appear symmetric, no proof (i.e. 10, 1010, etc) */
233 /* Even runs of 0's and 1's are also symmetric */
234 /* I have seen Hexagonal builders but they are more rare. */
236 static unsigned char tables[][3 * COLORS * STATES + 2] =
239 /* Here just so you can figure out notation */
240 { /* Langton's ant */
245 /* First 2 numbers are the size (ncolors, nstates) */
246 { /* LADDER BUILDER */
248 1, STR, 0, 2, STL, 0, 3, TRS, 0, 0, TLS, 0
250 { /* SPIRALING PATTERN */
255 { /* SQUARE (HEXAGON) BUILDER */
263 #define NTABLES (sizeof tables / sizeof tables[0])
266 position_of_neighbor(antfarmstruct * ap, int dir, int *pcol, int *prow)
268 int col = *pcol, row = *prow;
270 if (ap->neighbors == 6) {
273 col = (col + 1 == ap->ncols) ? 0 : col + 1;
277 col = (col + 1 == ap->ncols) ? 0 : col + 1;
278 row = (!row) ? ap->nrows - 1 : row - 1;
282 col = (!col) ? ap->ncols - 1 : col - 1;
283 row = (!row) ? ap->nrows - 1 : row - 1;
286 col = (!col) ? ap->ncols - 1 : col - 1;
290 col = (!col) ? ap->ncols - 1 : col - 1;
291 row = (row + 1 == ap->nrows) ? 0 : row + 1;
295 col = (col + 1 == ap->ncols) ? 0 : col + 1;
296 row = (row + 1 == ap->nrows) ? 0 : row + 1;
299 (void) fprintf(stderr, "wrong direction %d\n", dir);
301 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
304 col = (col + 1 == ap->ncols) ? 0 : col + 1;
307 col = (col + 1 == ap->ncols) ? 0 : col + 1;
308 row = (!row) ? ap->nrows - 1 : row - 1;
311 row = (!row) ? ap->nrows - 1 : row - 1;
314 col = (!col) ? ap->ncols - 1 : col - 1;
315 row = (!row) ? ap->nrows - 1 : row - 1;
318 col = (!col) ? ap->ncols - 1 : col - 1;
321 col = (!col) ? ap->ncols - 1 : col - 1;
322 row = (row + 1 == ap->nrows) ? 0 : row + 1;
325 row = (row + 1 == ap->nrows) ? 0 : row + 1;
328 col = (col + 1 == ap->ncols) ? 0 : col + 1;
329 row = (row + 1 == ap->nrows) ? 0 : row + 1;
332 (void) fprintf(stderr, "wrong direction %d\n", dir);
335 if ((col + row) % 2) { /* right */
338 col = (!col) ? ap->ncols - 1 : col - 1;
342 col = (!col) ? ap->ncols - 1 : col - 1;
343 row = (!row) ? ap->nrows - 1 : row - 1;
346 col = (!col) ? ap->ncols - 1 : col - 1;
364 row = (!row) ? ap->nrows - 1 : row - 1;
368 col = (col + 1 == ap->ncols) ? 0 : col + 1;
369 row = (!row) ? ap->nrows - 1 : row - 1;
372 col = (col + 1 == ap->ncols) ? 0 : col + 1;
376 col = (col + 1 == ap->ncols) ? 0 : col + 1;
377 row = (row + 1 == ap->nrows) ? 0 : row + 1;
380 row = (row + 1 == ap->nrows) ? 0 : row + 1;
384 if (row + 1 == ap->nrows)
386 else if (row + 2 == ap->nrows)
392 col = (!col) ? ap->ncols - 1 : col - 1;
393 if (row + 1 == ap->nrows)
395 else if (row + 2 == ap->nrows)
402 col = (!col) ? ap->ncols - 1 : col - 1;
403 row = (row + 1 == ap->nrows) ? 0 : row + 1;
406 (void) fprintf(stderr, "wrong direction %d\n", dir);
411 col = (col + 1 == ap->ncols) ? 0 : col + 1;
415 col = (col + 1 == ap->ncols) ? 0 : col + 1;
416 row = (row + 1 == ap->nrows) ? 0 : row + 1;
419 col = (col + 1 == ap->ncols) ? 0 : col + 1;
420 if (row + 1 == ap->nrows)
422 else if (row + 2 == ap->nrows)
429 if (row + 1 == ap->nrows)
431 else if (row + 2 == ap->nrows)
437 row = (row + 1 == ap->nrows) ? 0 : row + 1;
441 col = (!col) ? ap->ncols - 1 : col - 1;
442 row = (row + 1 == ap->nrows) ? 0 : row + 1;
445 col = (!col) ? ap->ncols - 1 : col - 1;
449 col = (!col) ? ap->ncols - 1 : col - 1;
450 row = (!row) ? ap->nrows - 1 : row - 1;
453 row = (!row) ? ap->nrows - 1 : row - 1;
465 col = (col + 1 == ap->ncols) ? 0 : col + 1;
475 col = (col + 1 == ap->ncols) ? 0 : col + 1;
476 row = (!row) ? ap->nrows - 1 : row - 1;
479 (void) fprintf(stderr, "wrong direction %d\n", dir);
488 fillcell(ModeInfo * mi, GC gc, int col, int row)
490 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
492 if (ap->neighbors == 6) {
493 int ccol = 2 * col + !(row & 1), crow = 2 * row;
495 ap->hexagonList[0].x = ap->xb + ccol * ap->xs;
496 ap->hexagonList[0].y = ap->yb + crow * ap->ys;
497 if (ap->xs == 1 && ap->ys == 1)
498 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
499 ap->hexagonList[0].x, ap->hexagonList[0].y, 1, 1);
501 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
502 ap->hexagonList, 6, Convex, CoordModePrevious);
504 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
505 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
506 ap->xb + ap->xs * col, ap->yb + ap->ys * row, ap->xs, ap->ys);
509 int orient = (col + row) % 2; /* O left 1 right */
511 ap->triangleList[orient][0].x = ap->xb + col * ap->xs;
512 ap->triangleList[orient][0].y = ap->yb + row * ap->ys;
513 if (ap->xs <= 3 || ap->ys <= 3)
514 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
515 ((orient) ? -1 : 1) + ap->triangleList[orient][0].x,
516 ap->triangleList[orient][0].y, 1, 1);
519 ap->triangleList[orient][0].x += (ap->xs / 2 - 1);
521 ap->triangleList[orient][0].x -= (ap->xs / 2 - 1);
522 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
523 ap->triangleList[orient], 3, Convex, CoordModePrevious);
529 truchetcell(ModeInfo * mi, int col, int row, int truchetstate)
531 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
533 if (ap->neighbors == 6) {
534 int ccol = 2 * col + !(row & 1), crow = 2 * row;
538 /* Very crude approx of Sqrt 3, so it will not cause drawing errors. */
539 hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs * 1.6 / 2.0);
540 hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys * 1.6 / 2.0);
541 for (side = 0; side < 6; side++) {
543 hex.x += ap->hexagonList[side].x;
544 hex.y += ap->hexagonList[side].y;
546 hex2.x = hex.x + ap->hexagonList[side + 1].x / 2;
547 hex2.y = hex.y + ap->hexagonList[side + 1].y / 2;
548 if (truchetstate == side % 3)
549 /* Crude approx of 120 deg, so it will not cause drawing errors. */
550 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
552 (int) ((double) ap->xs * 1.5), (int) ((double) ap->ys * 1.5),
553 ((555 - (side * 60)) % 360) * 64, 90 * 64);
555 } else if (ap->neighbors == 4) {
557 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
558 ap->xb + ap->xs * col - ap->xs / 2,
559 ap->yb + ap->ys * row + ap->ys / 2,
562 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
563 ap->xb + ap->xs * col + ap->xs / 2,
564 ap->yb + ap->ys * row - ap->ys / 2,
568 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
569 ap->xb + ap->xs * col - ap->xs / 2,
570 ap->yb + ap->ys * row - ap->ys / 2,
573 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
574 ap->xb + ap->xs * col + ap->xs / 2,
575 ap->yb + ap->ys * row + ap->ys / 2,
579 } else if (ap->neighbors == 3) {
580 int orient = (col + row) % 2; /* O left 1 right */
584 tri.x = ap->xb + col * ap->xs;
585 tri.y = ap->yb + row * ap->ys;
587 tri.x += (ap->xs / 2 - 2);
589 tri.x -= (ap->xs / 2 + 2);
591 for (side = 0; side < 3; side++) {
593 tri.x += ap->triangleList[orient][side].x;
594 tri.y += ap->triangleList[orient][side].y;
596 if (truchetstate == side % 3) {
598 ang = (518 - side * 120) % 360; /* Right */
600 ang = (690 - side * 120) % 360; /* Left */
601 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
602 tri.x - ap->xs / 2, tri.y - 3 * ap->ys / 4,
603 ap->xs, 3 * ap->ys / 2,
611 drawcell(ModeInfo * mi, int col, int row, unsigned char color)
613 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
617 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
619 } else if (MI_NPIXELS(mi) > 2) {
620 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
621 MI_PIXEL(mi, ap->colors[color - 1]));
626 gcv.stipple = ap->pixmaps[color - 1];
627 gcv.foreground = MI_WIN_WHITE_PIXEL(mi);
628 gcv.background = MI_WIN_BLACK_PIXEL(mi);
629 XChangeGC(MI_DISPLAY(mi), ap->stippledGC,
630 GCStipple | GCForeground | GCBackground, &gcv);
633 fillcell(mi, gc, col, row);
637 drawtruchet(ModeInfo * mi, int col, int row,
638 unsigned char color, unsigned char truchetstate)
640 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
643 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
644 else if (MI_NPIXELS(mi) > 2 || color > ap->ncolors / 2)
645 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
647 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
648 truchetcell(mi, col, row, truchetstate);
652 draw_anant(ModeInfo * mi, int col, int row)
654 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
655 fillcell(mi, MI_GC(mi), col, row);
656 #if 0 /* Can not see eyes */
658 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
659 Display *display = MI_DISPLAY(mi);
660 Window window = MI_WINDOW(mi);
662 if (ap->xs > 2 && ap->ys > 2) { /* Draw Eyes */
664 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
667 XDrawPoint(display, window, MI_GC(mi),
668 ap->xb + ap->xs - 1, ap->yb + 1);
669 XDrawPoint(display, window, MI_GC(mi),
670 ap->xb + ap->xs - 1, ap->yb + ap->ys - 2);
673 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + 1);
674 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + ap->ys - 2);
676 if (neighbors == 4) {
678 XDrawPoint(display, window, MI_GC(mi), ap->xb + 1, ap->yb);
679 XDrawPoint(display, window, MI_GC(mi),
680 ap->xb + ap->xs - 2, ap->yb);
683 XDrawPoint(display, window, MI_GC(mi),
684 ap->xb + 1, ap->yb + ap->ys - 1);
685 XDrawPoint(display, window, MI_GC(mi),
686 ap->xb + ap->xs - 2, ap->yb + ap->ys - 1);
701 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
702 int row, col, mrow = 0;
704 for (row = 0; row < ap->nrows; ++row) {
705 for (col = 0; col < ap->ncols; ++col) {
706 ap->old[col + mrow] = (unsigned char) NRAND((int) ap->ncolors);
707 drawcell(mi, col, row, ap->old[col + mrow]);
716 fromTableDirection(unsigned char dir, int neighbors)
722 return (ANGLES / neighbors);
724 return (ANGLES / (2 * neighbors));
728 return (ANGLES - ANGLES / (2 * neighbors));
730 return (ANGLES - ANGLES / neighbors);
734 return (ANGLES + ANGLES / neighbors);
736 return (ANGLES + ANGLES / (2 * neighbors));
738 return (3 * ANGLES / 2);
740 return (2 * ANGLES - ANGLES / (2 * neighbors));
742 return (2 * ANGLES - ANGLES / neighbors);
744 (void) fprintf(stderr, "wrong direction %d\n", dir);
750 getTable(ModeInfo * mi, int i)
752 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
754 unsigned char *patptr;
756 patptr = &tables[i][0];
757 ap->ncolors = *patptr++;
758 ap->nstates = *patptr++;
759 total = ap->ncolors * ap->nstates;
760 if (MI_WIN_IS_VERBOSE(mi))
761 (void) fprintf(stdout,
762 "neighbors %d, table number %d, colors %d, states %d\n",
763 ap->neighbors, i, ap->ncolors, ap->nstates);
764 for (j = 0; j < total; j++) {
765 ap->machine[j].color = *patptr++;
766 ap->machine[j].direction = fromTableDirection(*patptr++, ap->neighbors);
767 ap->machine[j].next = *patptr++;
773 getTurk(ModeInfo * mi, int i)
775 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
776 int power2, j, number, total;
778 /* To force a number, say <i = 2;> has i + 2 (or 4) digits in binary */
779 power2 = 1 << (i + 1);
780 /* Dont want numbers which in binary are all 1's. */
781 number = NRAND(power2 - 1) + power2;
782 /* To force a particular number, say <number = 10;> */
786 total = ap->ncolors * ap->nstates;
787 for (j = 0; j < total; j++) {
788 ap->machine[j].color = (j + 1) % total;
789 ap->machine[j].direction = (power2 & number) ?
790 fromTableDirection(TRS, ap->neighbors) :
791 fromTableDirection(TLS, ap->neighbors);
792 ap->machine[j].next = 0;
795 if (ap->neighbors != 3 && ap->neighbors != 4 && ap->neighbors != 6)
799 if (MI_WIN_IS_VERBOSE(mi))
800 (void) fprintf(stdout, "neighbors %d, Turk's number %d, colors %d\n",
801 ap->neighbors, number, ap->ncolors);
805 init_ant(ModeInfo * mi)
807 Display *display = MI_DISPLAY(mi);
808 Window window = MI_WINDOW(mi);
809 int size = MI_SIZE(mi);
812 int col, row, i, dir;
814 /* jwz sez: small sizes look like crap */
816 size = NRAND(-size)+1;
820 if (antfarms == NULL) {
821 if ((antfarms = (antfarmstruct *) calloc(MI_NUM_SCREENS(mi),
822 sizeof (antfarmstruct))) == NULL)
825 ap = &antfarms[MI_SCREEN(mi)];
827 if (MI_NPIXELS(mi) <= 2) {
828 if (ap->stippledGC == None) {
829 gcv.fill_style = FillOpaqueStippled;
830 ap->stippledGC = XCreateGC(display, window, GCFillStyle, &gcv);
832 if (ap->init_bits == 0) {
833 for (i = 0; i < COLORS - 1; i++)
834 ANTBITS(patterns[i], PATTERNSIZE, PATTERNSIZE);
838 ap->n = MI_BATCHCOUNT(mi);
839 if (ap->n < -MINANTS) {
840 /* if ap->n is random ... the size can change */
841 if (ap->ants != NULL) {
842 (void) free((void *) ap->ants);
845 ap->n = NRAND(-ap->n - MINANTS + 1) + MINANTS;
846 } else if (ap->n < MINANTS)
849 ap->width = MI_WIN_WIDTH(mi);
850 ap->height = MI_WIN_HEIGHT(mi);
852 if (neighbors == 8 || neighbors == 9 || neighbors == 12)
853 ap->neighbors = neighbors; /* Discourage but not deny use... */
855 for (i = 0; i < NEIGHBORKINDS; i++) {
856 if (neighbors == initVal[i]) {
857 ap->neighbors = initVal[i];
860 if (i == NEIGHBORKINDS - 1) {
861 ap->neighbors = initVal[NRAND(NEIGHBORKINDS)];
866 if (ap->neighbors == 6) {
874 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
875 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
876 else if (size < MINSIZE) {
878 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
882 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
885 nccols = MAX(ap->width / ap->xs - 2, 2);
886 ncrows = MAX(ap->height / ap->ys - 1, 2);
887 ap->ncols = nccols / 2;
888 ap->nrows = 2 * (ncrows / 4);
889 ap->xb = (ap->width - ap->xs * nccols) / 2 + ap->xs / 2;
890 ap->yb = (ap->height - ap->ys * (ncrows / 2) * 2) / 2 + ap->ys;
891 for (i = 0; i < 7; i++) {
892 ap->hexagonList[i].x = (ap->xs - 1) * hexagonUnit[i].x;
893 ap->hexagonList[i].y = ((ap->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
895 } else if (ap->neighbors == 4 && ap->neighbors == 8) {
897 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
898 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
899 else if (size < MINSIZE) {
901 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
905 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
908 ap->ncols = MAX(ap->width / ap->xs, 2);
909 ap->nrows = MAX(ap->height / ap->ys, 2);
910 ap->xb = (ap->width - ap->xs * ap->ncols) / 2;
911 ap->yb = (ap->height - ap->ys * ap->nrows) / 2;
920 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
921 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
922 else if (size < MINSIZE) {
924 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
928 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
930 ap->xs = (int) (1.52 * ap->ys);
931 ap->ncols = (MAX(ap->width / ap->xs - 1, 2) / 2) * 2;
932 ap->nrows = (MAX(ap->height / ap->ys - 1, 2) / 2) * 2;
933 ap->xb = (ap->width - ap->xs * ap->ncols) / 2 + ap->xs / 2;
934 ap->yb = (ap->height - ap->ys * ap->nrows) / 2 + ap->ys;
935 for (orient = 0; orient < 2; orient++) {
936 for (i = 0; i < 4; i++) {
937 ap->triangleList[orient][i].x =
938 (ap->xs - 2) * triangleUnit[orient][i].x;
939 ap->triangleList[orient][i].y =
940 (ap->ys - 2) * triangleUnit[orient][i].y;
944 XClearWindow(display, MI_WINDOW(mi));
946 /* Exclude odd # of neighbors, stepping forward not defined */
947 if (!NRAND(COLORS) && ((ap->neighbors + 1) % 2)) {
948 getTable(mi, (int) (NRAND(NTABLES)));
950 getTurk(mi, (int) (NRAND(COLORS - 1)));
951 if (MI_NPIXELS(mi) > 2)
952 for (i = 0; i < (int) ap->ncolors - 1; i++)
953 ap->colors[i] = (NRAND(MI_NPIXELS(mi)) +
954 i * MI_NPIXELS(mi)) / ((int) (ap->ncolors - 1));
955 if (ap->ants == NULL)
956 ap->ants = (antstruct *) malloc(ap->n * sizeof (antstruct));
957 if (ap->tape != NULL)
958 (void) free((void *) ap->tape);
959 ap->tape = (unsigned char *)
960 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
962 if (ap->truchet_state != NULL)
963 (void) free((void *) ap->truchet_state);
964 ap->truchet_state = (unsigned char *)
965 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
969 dir = NRAND(ap->neighbors) * ANGLES / ap->neighbors;
970 /* Have them all start in the same spot, why not? */
971 for (i = 0; i < ap->n; i++) {
972 ap->ants[i].col = col;
973 ap->ants[i].row = row;
974 ap->ants[i].direction = dir;
975 ap->ants[i].state = 0;
977 draw_anant(mi, col, row);
981 draw_ant(ModeInfo * mi)
983 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
986 int i, state_pos, tape_pos;
988 short chg_dir, old_dir;
990 for (i = 0; i < ap->n; i++) {
991 anant = &ap->ants[i];
992 tape_pos = anant->col + anant->row * ap->ncols;
993 color = ap->tape[tape_pos]; /* read tape */
994 state_pos = color + anant->state * ap->ncolors;
995 status = &(ap->machine[state_pos]);
996 drawcell(mi, anant->col, anant->row, status->color);
997 ap->tape[tape_pos] = status->color; /* write on tape */
999 /* Find direction of Bees or Ants. */
1000 /* Translate relative direction to actual direction */
1001 old_dir = anant->direction;
1002 chg_dir = (2 * ANGLES - status->direction) % ANGLES;
1003 anant->direction = (chg_dir + old_dir) % ANGLES;
1007 if (ap->neighbors == 6) {
1008 a = (old_dir / 60) % 3;
1009 b = (anant->direction / 60) % 3;
1010 a = (a + b + 1) % 3;
1011 drawtruchet(mi, anant->col, anant->row, status->color, a);
1012 } else if (ap->neighbors == 4) {
1014 b = anant->direction / 180;
1015 a = ((a && !b) || (b && !a));
1016 drawtruchet(mi, anant->col, anant->row, status->color, a);
1017 } else if (ap->neighbors == 3) {
1019 a = (2 + anant->direction / 120) % 3;
1021 a = (1 + anant->direction / 120) % 3;
1022 drawtruchet(mi, anant->col, anant->row, status->color, a);
1024 ap->truchet_state[tape_pos] = a + 1;
1026 anant->state = status->next;
1028 /* If edge than wrap it */
1029 old_dir = ((status->direction < ANGLES) ? anant->direction : old_dir);
1030 position_of_neighbor(ap, old_dir, &(anant->col), &(anant->row));
1031 draw_anant(mi, anant->col, anant->row);
1033 if (++ap->generation > MI_CYCLES(mi)) {
1035 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
1039 if (ap->redrawing) {
1040 for (i = 0; i < REDRAWSTEP; i++) {
1041 if (ap->tape[ap->redrawpos] ||
1042 (ap->truchet && ap->truchet_state[ap->redrawpos])) {
1043 drawcell(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1044 ap->tape[ap->redrawpos]);
1046 drawtruchet(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1047 ap->tape[ap->redrawpos],
1048 ap->truchet_state[ap->redrawpos] - 1);
1050 if (++(ap->redrawpos) >= ap->ncols * ap->nrows) {
1059 release_ant(ModeInfo * mi)
1061 if (antfarms != NULL) {
1064 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1065 antfarmstruct *ap = &antfarms[screen];
1068 if (ap->stippledGC != None) {
1069 XFreeGC(MI_DISPLAY(mi), ap->stippledGC);
1071 for (shade = 0; shade < ap->init_bits; shade++)
1072 XFreePixmap(MI_DISPLAY(mi), ap->pixmaps[shade]);
1073 if (ap->tape != NULL)
1074 (void) free((void *) ap->tape);
1075 if (ap->ants != NULL)
1076 (void) free((void *) ap->ants);
1077 if (ap->truchet_state != NULL)
1078 (void) free((void *) ap->truchet_state);
1080 (void) free((void *) antfarms);
1086 refresh_ant(ModeInfo * mi)
1088 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];