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 "False"
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 3
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, 4, 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 #if 0 /* jwz sez: this looks like crap */
505 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
506 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
507 ap->xb + ap->xs * col, ap->yb + ap->ys * row, ap->xs, ap->ys);
511 int orient = (col + row) % 2; /* O left 1 right */
513 ap->triangleList[orient][0].x = ap->xb + col * ap->xs;
514 ap->triangleList[orient][0].y = ap->yb + row * ap->ys;
515 if (ap->xs <= 3 || ap->ys <= 3)
516 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
517 ((orient) ? -1 : 1) + ap->triangleList[orient][0].x,
518 ap->triangleList[orient][0].y, 1, 1);
521 ap->triangleList[orient][0].x += (ap->xs / 2 - 1);
523 ap->triangleList[orient][0].x -= (ap->xs / 2 - 1);
524 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
525 ap->triangleList[orient], 3, Convex, CoordModePrevious);
531 truchetcell(ModeInfo * mi, int col, int row, int truchetstate)
533 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
535 if (ap->neighbors == 6) {
536 int ccol = 2 * col + !(row & 1), crow = 2 * row;
540 /* Very crude approx of Sqrt 3, so it will not cause drawing errors. */
541 hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs * 1.6 / 2.0);
542 hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys * 1.6 / 2.0);
543 for (side = 0; side < 6; side++) {
545 hex.x += ap->hexagonList[side].x;
546 hex.y += ap->hexagonList[side].y;
548 hex2.x = hex.x + ap->hexagonList[side + 1].x / 2;
549 hex2.y = hex.y + ap->hexagonList[side + 1].y / 2;
550 if (truchetstate == side % 3)
551 /* Crude approx of 120 deg, so it will not cause drawing errors. */
552 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
554 (int) ((double) ap->xs * 1.5), (int) ((double) ap->ys * 1.5),
555 ((555 - (side * 60)) % 360) * 64, 90 * 64);
557 } else if (ap->neighbors == 4) {
559 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
560 ap->xb + ap->xs * col - ap->xs / 2,
561 ap->yb + ap->ys * row + ap->ys / 2,
564 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
565 ap->xb + ap->xs * col + ap->xs / 2,
566 ap->yb + ap->ys * row - ap->ys / 2,
570 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
571 ap->xb + ap->xs * col - ap->xs / 2,
572 ap->yb + ap->ys * row - ap->ys / 2,
575 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
576 ap->xb + ap->xs * col + ap->xs / 2,
577 ap->yb + ap->ys * row + ap->ys / 2,
581 } else if (ap->neighbors == 3) {
582 int orient = (col + row) % 2; /* O left 1 right */
586 tri.x = ap->xb + col * ap->xs;
587 tri.y = ap->yb + row * ap->ys;
589 tri.x += (ap->xs / 2 - 2);
591 tri.x -= (ap->xs / 2 + 2);
593 for (side = 0; side < 3; side++) {
595 tri.x += ap->triangleList[orient][side].x;
596 tri.y += ap->triangleList[orient][side].y;
598 if (truchetstate == side % 3) {
600 ang = (518 - side * 120) % 360; /* Right */
602 ang = (690 - side * 120) % 360; /* Left */
603 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
604 tri.x - ap->xs / 2, tri.y - 3 * ap->ys / 4,
605 ap->xs, 3 * ap->ys / 2,
613 drawcell(ModeInfo * mi, int col, int row, unsigned char color)
615 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
619 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
621 } else if (MI_NPIXELS(mi) > 2) {
622 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
623 MI_PIXEL(mi, ap->colors[color - 1]));
628 gcv.stipple = ap->pixmaps[color - 1];
629 gcv.foreground = MI_WIN_WHITE_PIXEL(mi);
630 gcv.background = MI_WIN_BLACK_PIXEL(mi);
631 XChangeGC(MI_DISPLAY(mi), ap->stippledGC,
632 GCStipple | GCForeground | GCBackground, &gcv);
635 fillcell(mi, gc, col, row);
639 drawtruchet(ModeInfo * mi, int col, int row,
640 unsigned char color, unsigned char truchetstate)
642 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
645 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
646 else if (MI_NPIXELS(mi) > 2 || color > ap->ncolors / 2)
647 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
649 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
650 truchetcell(mi, col, row, truchetstate);
654 draw_anant(ModeInfo * mi, int col, int row)
656 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
657 fillcell(mi, MI_GC(mi), col, row);
658 #if 0 /* Can not see eyes */
660 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
661 Display *display = MI_DISPLAY(mi);
662 Window window = MI_WINDOW(mi);
664 if (ap->xs > 2 && ap->ys > 2) { /* Draw Eyes */
666 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
669 XDrawPoint(display, window, MI_GC(mi),
670 ap->xb + ap->xs - 1, ap->yb + 1);
671 XDrawPoint(display, window, MI_GC(mi),
672 ap->xb + ap->xs - 1, ap->yb + ap->ys - 2);
675 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + 1);
676 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + ap->ys - 2);
678 if (neighbors == 4) {
680 XDrawPoint(display, window, MI_GC(mi), ap->xb + 1, ap->yb);
681 XDrawPoint(display, window, MI_GC(mi),
682 ap->xb + ap->xs - 2, ap->yb);
685 XDrawPoint(display, window, MI_GC(mi),
686 ap->xb + 1, ap->yb + ap->ys - 1);
687 XDrawPoint(display, window, MI_GC(mi),
688 ap->xb + ap->xs - 2, ap->yb + ap->ys - 1);
703 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
704 int row, col, mrow = 0;
706 for (row = 0; row < ap->nrows; ++row) {
707 for (col = 0; col < ap->ncols; ++col) {
708 ap->old[col + mrow] = (unsigned char) NRAND((int) ap->ncolors);
709 drawcell(mi, col, row, ap->old[col + mrow]);
718 fromTableDirection(unsigned char dir, int neighbors)
724 return (ANGLES / neighbors);
726 return (ANGLES / (2 * neighbors));
730 return (ANGLES - ANGLES / (2 * neighbors));
732 return (ANGLES - ANGLES / neighbors);
736 return (ANGLES + ANGLES / neighbors);
738 return (ANGLES + ANGLES / (2 * neighbors));
740 return (3 * ANGLES / 2);
742 return (2 * ANGLES - ANGLES / (2 * neighbors));
744 return (2 * ANGLES - ANGLES / neighbors);
746 (void) fprintf(stderr, "wrong direction %d\n", dir);
752 getTable(ModeInfo * mi, int i)
754 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
756 unsigned char *patptr;
758 patptr = &tables[i][0];
759 ap->ncolors = *patptr++;
760 ap->nstates = *patptr++;
761 total = ap->ncolors * ap->nstates;
762 if (MI_WIN_IS_VERBOSE(mi))
763 (void) fprintf(stdout,
764 "neighbors %d, table number %d, colors %d, states %d\n",
765 ap->neighbors, i, ap->ncolors, ap->nstates);
766 for (j = 0; j < total; j++) {
767 ap->machine[j].color = *patptr++;
768 ap->machine[j].direction = fromTableDirection(*patptr++, ap->neighbors);
769 ap->machine[j].next = *patptr++;
775 getTurk(ModeInfo * mi, int i)
777 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
778 int power2, j, number, total;
780 /* To force a number, say <i = 2;> has i + 2 (or 4) digits in binary */
781 power2 = 1 << (i + 1);
782 /* Dont want numbers which in binary are all 1's. */
783 number = NRAND(power2 - 1) + power2;
784 /* To force a particular number, say <number = 10;> */
788 total = ap->ncolors * ap->nstates;
789 for (j = 0; j < total; j++) {
790 ap->machine[j].color = (j + 1) % total;
791 ap->machine[j].direction = (power2 & number) ?
792 fromTableDirection(TRS, ap->neighbors) :
793 fromTableDirection(TLS, ap->neighbors);
794 ap->machine[j].next = 0;
797 if (ap->neighbors != 3 && ap->neighbors != 4 && ap->neighbors != 6)
801 if (MI_WIN_IS_VERBOSE(mi))
802 (void) fprintf(stdout, "neighbors %d, Turk's number %d, colors %d\n",
803 ap->neighbors, number, ap->ncolors);
807 init_ant(ModeInfo * mi)
809 Display *display = MI_DISPLAY(mi);
810 Window window = MI_WINDOW(mi);
811 int size = MI_SIZE(mi);
814 int col, row, i, dir;
816 /* jwz sez: small sizes look like crap */
818 size = NRAND(-size)+1;
822 if (antfarms == NULL) {
823 if ((antfarms = (antfarmstruct *) calloc(MI_NUM_SCREENS(mi),
824 sizeof (antfarmstruct))) == NULL)
827 ap = &antfarms[MI_SCREEN(mi)];
829 if (MI_NPIXELS(mi) <= 2) {
830 if (ap->stippledGC == None) {
831 gcv.fill_style = FillOpaqueStippled;
832 ap->stippledGC = XCreateGC(display, window, GCFillStyle, &gcv);
834 if (ap->init_bits == 0) {
835 for (i = 0; i < COLORS - 1; i++)
836 ANTBITS(patterns[i], PATTERNSIZE, PATTERNSIZE);
840 ap->n = MI_BATCHCOUNT(mi);
841 if (ap->n < -MINANTS) {
842 /* if ap->n is random ... the size can change */
843 if (ap->ants != NULL) {
844 (void) free((void *) ap->ants);
847 ap->n = NRAND(-ap->n - MINANTS + 1) + MINANTS;
848 } else if (ap->n < MINANTS)
851 ap->width = MI_WIN_WIDTH(mi);
852 ap->height = MI_WIN_HEIGHT(mi);
854 if (neighbors == 8 || neighbors == 9 || neighbors == 12)
855 ap->neighbors = neighbors; /* Discourage but not deny use... */
857 for (i = 0; i < NEIGHBORKINDS; i++) {
858 if (neighbors == initVal[i]) {
859 ap->neighbors = initVal[i];
862 if (i == NEIGHBORKINDS - 1) {
863 ap->neighbors = initVal[NRAND(NEIGHBORKINDS)];
868 if (ap->neighbors == 6) {
876 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
877 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
878 else if (size < MINSIZE) {
880 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
884 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
887 nccols = MAX(ap->width / ap->xs - 2, 2);
888 ncrows = MAX(ap->height / ap->ys - 1, 2);
889 ap->ncols = nccols / 2;
890 ap->nrows = 2 * (ncrows / 4);
891 ap->xb = (ap->width - ap->xs * nccols) / 2 + ap->xs / 2;
892 ap->yb = (ap->height - ap->ys * (ncrows / 2) * 2) / 2 + ap->ys;
893 for (i = 0; i < 7; i++) {
894 ap->hexagonList[i].x = (ap->xs - 1) * hexagonUnit[i].x;
895 ap->hexagonList[i].y = ((ap->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
897 } else if (ap->neighbors == 4 && ap->neighbors == 8) {
899 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
900 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
901 else if (size < MINSIZE) {
903 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
907 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
910 ap->ncols = MAX(ap->width / ap->xs, 2);
911 ap->nrows = MAX(ap->height / ap->ys, 2);
912 ap->xb = (ap->width - ap->xs * ap->ncols) / 2;
913 ap->yb = (ap->height - ap->ys * ap->nrows) / 2;
922 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
923 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
924 else if (size < MINSIZE) {
926 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
930 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
932 ap->xs = (int) (1.52 * ap->ys);
933 ap->ncols = (MAX(ap->width / ap->xs - 1, 2) / 2) * 2;
934 ap->nrows = (MAX(ap->height / ap->ys - 1, 2) / 2) * 2;
935 ap->xb = (ap->width - ap->xs * ap->ncols) / 2 + ap->xs / 2;
936 ap->yb = (ap->height - ap->ys * ap->nrows) / 2 + ap->ys;
937 for (orient = 0; orient < 2; orient++) {
938 for (i = 0; i < 4; i++) {
939 ap->triangleList[orient][i].x =
940 (ap->xs - 2) * triangleUnit[orient][i].x;
941 ap->triangleList[orient][i].y =
942 (ap->ys - 2) * triangleUnit[orient][i].y;
946 XClearWindow(display, MI_WINDOW(mi));
948 /* Exclude odd # of neighbors, stepping forward not defined */
949 if (!NRAND(COLORS) && ((ap->neighbors + 1) % 2)) {
950 getTable(mi, (int) (NRAND(NTABLES)));
952 getTurk(mi, (int) (NRAND(COLORS - 1)));
953 if (MI_NPIXELS(mi) > 2)
954 for (i = 0; i < (int) ap->ncolors - 1; i++)
955 ap->colors[i] = (NRAND(MI_NPIXELS(mi)) +
956 i * MI_NPIXELS(mi)) / ((int) (ap->ncolors - 1));
957 if (ap->ants == NULL)
958 ap->ants = (antstruct *) malloc(ap->n * sizeof (antstruct));
959 if (ap->tape != NULL)
960 (void) free((void *) ap->tape);
961 ap->tape = (unsigned char *)
962 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
964 if (ap->truchet_state != NULL)
965 (void) free((void *) ap->truchet_state);
966 ap->truchet_state = (unsigned char *)
967 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
971 dir = NRAND(ap->neighbors) * ANGLES / ap->neighbors;
972 /* Have them all start in the same spot, why not? */
973 for (i = 0; i < ap->n; i++) {
974 ap->ants[i].col = col;
975 ap->ants[i].row = row;
976 ap->ants[i].direction = dir;
977 ap->ants[i].state = 0;
979 draw_anant(mi, col, row);
983 draw_ant(ModeInfo * mi)
985 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
988 int i, state_pos, tape_pos;
990 short chg_dir, old_dir;
992 for (i = 0; i < ap->n; i++) {
993 anant = &ap->ants[i];
994 tape_pos = anant->col + anant->row * ap->ncols;
995 color = ap->tape[tape_pos]; /* read tape */
996 state_pos = color + anant->state * ap->ncolors;
997 status = &(ap->machine[state_pos]);
998 drawcell(mi, anant->col, anant->row, status->color);
999 ap->tape[tape_pos] = status->color; /* write on tape */
1001 /* Find direction of Bees or Ants. */
1002 /* Translate relative direction to actual direction */
1003 old_dir = anant->direction;
1004 chg_dir = (2 * ANGLES - status->direction) % ANGLES;
1005 anant->direction = (chg_dir + old_dir) % ANGLES;
1009 if (ap->neighbors == 6) {
1010 a = (old_dir / 60) % 3;
1011 b = (anant->direction / 60) % 3;
1012 a = (a + b + 1) % 3;
1013 drawtruchet(mi, anant->col, anant->row, status->color, a);
1014 } else if (ap->neighbors == 4) {
1016 b = anant->direction / 180;
1017 a = ((a && !b) || (b && !a));
1018 drawtruchet(mi, anant->col, anant->row, status->color, a);
1019 } else if (ap->neighbors == 3) {
1021 a = (2 + anant->direction / 120) % 3;
1023 a = (1 + anant->direction / 120) % 3;
1024 drawtruchet(mi, anant->col, anant->row, status->color, a);
1026 ap->truchet_state[tape_pos] = a + 1;
1028 anant->state = status->next;
1030 /* If edge than wrap it */
1031 old_dir = ((status->direction < ANGLES) ? anant->direction : old_dir);
1032 position_of_neighbor(ap, old_dir, &(anant->col), &(anant->row));
1033 draw_anant(mi, anant->col, anant->row);
1035 if (++ap->generation > MI_CYCLES(mi)) {
1037 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
1041 if (ap->redrawing) {
1042 for (i = 0; i < REDRAWSTEP; i++) {
1043 if (ap->tape[ap->redrawpos] ||
1044 (ap->truchet && ap->truchet_state[ap->redrawpos])) {
1045 drawcell(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1046 ap->tape[ap->redrawpos]);
1048 drawtruchet(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1049 ap->tape[ap->redrawpos],
1050 ap->truchet_state[ap->redrawpos] - 1);
1052 if (++(ap->redrawpos) >= ap->ncols * ap->nrows) {
1061 release_ant(ModeInfo * mi)
1063 if (antfarms != NULL) {
1066 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1067 antfarmstruct *ap = &antfarms[screen];
1070 if (ap->stippledGC != None) {
1071 XFreeGC(MI_DISPLAY(mi), ap->stippledGC);
1073 for (shade = 0; shade < ap->init_bits; shade++)
1074 XFreePixmap(MI_DISPLAY(mi), ap->pixmaps[shade]);
1075 if (ap->tape != NULL)
1076 (void) free((void *) ap->tape);
1077 if (ap->ants != NULL)
1078 (void) free((void *) ap->ants);
1079 if (ap->truchet_state != NULL)
1080 (void) free((void *) ap->truchet_state);
1082 (void) free((void *) antfarms);
1088 refresh_ant(ModeInfo * mi)
1090 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];