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.11 98/06/18 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 (or 9, 12)
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" \
66 "*sharpturn: False \n"
67 # include "xlockmore.h" /* in xscreensaver distribution */
69 #else /* STANDALONE */
70 # include "xlock.h" /* in xlockmore distribution */
71 #endif /* STANDALONE */
76 * neighbors of 0 randomizes it between 3, 4 and 6.
77 * 8, 9 12 are available also but not recommended.
84 #endif /* !STANDALONE */
86 #define DEF_TRUCHET "False"
87 #define DEF_SHARPTURN "False"
88 #define DEF_NEIGHBORS "0"
91 static Bool sharpturn;
93 static XrmOptionDescRec opts[] =
95 {"-truchet", ".ant.truchet", XrmoptionNoArg, (caddr_t) "on"},
96 {"+truchet", ".ant.truchet", XrmoptionNoArg, (caddr_t) "off"},
97 {"-sharpturn", ".ant.sharpturn", XrmoptionNoArg, (caddr_t) "on"},
98 {"+sharpturn", ".ant.sharpturn", XrmoptionNoArg, (caddr_t) "off"},
101 {"-neighbors", ".ant.neighbors", XrmoptionSepArg, (caddr_t) 0},
102 {"+neighbors", ".ant.neighbors", XrmoptionSepArg, (caddr_t) 0}
103 #endif /* STANDALONE */
106 static argtype vars[] =
108 {(caddr_t *) & truchet, "truchet", "Truchet", DEF_TRUCHET, t_Bool},
109 {(caddr_t *) & sharpturn, "sharpturn", "SharpTurn", DEF_SHARPTURN, t_Bool},
111 {(caddr_t *) & neighbors, "neighbors", "Neighbors", DEF_NEIGHBORS, t_Int}
112 #endif /* STANDALONE */
114 static OptionStruct desc[] =
116 {"-/+truchet", "turn on/off Truchet lines"},
117 {"-/+sharpturn", "turn on/off sharp turns (6 or 12 neighbors only)"}
120 ModeSpecOpt ant_opts =
121 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
124 ModStruct ant_description =
125 {"ant", "init_ant", "draw_ant", "release_ant",
126 "refresh_ant", "init_ant", NULL, &ant_opts,
127 1000, -3, 40000, -12, 64, 1.0, "",
128 "Shows Langton's and Turk's generalized ants", 0, NULL};
132 #define ANTBITS(n,w,h)\
133 ap->pixmaps[ap->init_bits++]=\
134 XCreatePixmapFromBitmapData(display,window,(char *)n,w,h,1,0,1)
136 /* If you change the table you may have to change the following 2 constants */
139 #define REDRAWSTEP 2000 /* How much tape to draw per cycle */
140 #define MINGRIDSIZE 24
142 #define MINRANDOMSIZE 5
166 unsigned char ncolors, nstates;
168 int redrawing, redrawpos;
169 int truchet; /* Only for Turk modes */
170 int sharpturn; /* Only for even neighbors > 4 (i.e. 6 and 12) */
171 statestruct machine[NUMSTIPPLES * STATES];
173 unsigned char *truchet_state;
176 unsigned char colors[NUMSTIPPLES - 1];
178 Pixmap pixmaps[NUMSTIPPLES - 1];
180 XPoint hexagon[7]; /* Need more than 6 for truchet */
181 XPoint triangle[2][4]; /* Need more than 3 for truchet */
185 static char plots[] =
187 #if 1 /* Without this... this mode is misnamed... */
190 6}; /* Neighborhoods, 8 just makes a mess */
192 #define NEIGHBORKINDS (long) (sizeof plots / sizeof *plots)
194 /* Relative ant moves */
195 #define FS 0 /* Step */
196 #define TRS 1 /* Turn right, then step */
197 #define THRS 2 /* Turn hard right, then step */
198 #define TBS 3 /* Turn back, then step */
199 #define THLS 4 /* Turn hard left, then step */
200 #define TLS 5 /* Turn left, then step */
201 #define SF 6 /* Step */
202 #define STR 7 /* Step then turn right */
203 #define STHR 8 /* Step then turn hard right */
204 #define STB 9 /* Step then turn back */
205 #define STHL 10 /* Step then turn hard left */
206 #define STL 11 /* Step then turn left */
208 static antfarmstruct *antfarms = NULL;
210 /* LANGTON'S ANT (10) Chaotic after 500, Builder after 10,000 (104p) */
211 /* TURK'S 100 ANT Always chaotic?, tested past 150,000,000 */
212 /* TURK'S 101 ANT Always chaotic? */
213 /* TURK'S 110 ANT Builder at 150 (18p) */
214 /* TURK'S 1000 ANT Always chaotic? */
215 /* TURK'S 1100 SYMMETRIC ANT all even run 1's and 0's are symmetric */
216 /* other examples 1001, 110011, 110000, 1001101 */
217 /* TURK'S 1101 ANT Builder after 250,000 (388p) */
218 /* Once saw a chess horse type builder (i.e. non-45 degree builder) */
221 /* All alternating 10 appear symmetric, no proof (i.e. 10, 1010, etc) */
222 /* Even runs of 0's and 1's are also symmetric */
223 /* I have seen Hexagonal builders but they are more rare. */
225 static unsigned char tables[][3 * NUMSTIPPLES * STATES + 2] =
228 /* Here just so you can figure out notation */
229 { /* Langton's ant */
234 /* First 2 numbers are the size (ncolors, nstates) */
235 { /* LADDER BUILDER */
237 1, STR, 0, 2, STL, 0, 3, TRS, 0, 0, TLS, 0
239 { /* SPIRALING PATTERN */
244 { /* SQUARE (HEXAGON) BUILDER */
252 #define NTABLES (sizeof tables / sizeof tables[0])
255 position_of_neighbor(antfarmstruct * ap, int dir, int *pcol, int *prow)
257 int col = *pcol, row = *prow;
259 if (ap->neighbors == 6) {
262 col = (col + 1 == ap->ncols) ? 0 : col + 1;
266 col = (col + 1 == ap->ncols) ? 0 : col + 1;
267 row = (!row) ? ap->nrows - 1 : row - 1;
271 col = (!col) ? ap->ncols - 1 : col - 1;
272 row = (!row) ? ap->nrows - 1 : row - 1;
275 col = (!col) ? ap->ncols - 1 : col - 1;
279 col = (!col) ? ap->ncols - 1 : col - 1;
280 row = (row + 1 == ap->nrows) ? 0 : row + 1;
284 col = (col + 1 == ap->ncols) ? 0 : col + 1;
285 row = (row + 1 == ap->nrows) ? 0 : row + 1;
288 (void) fprintf(stderr, "wrong direction %d\n", dir);
290 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
293 col = (col + 1 == ap->ncols) ? 0 : col + 1;
296 col = (col + 1 == ap->ncols) ? 0 : col + 1;
297 row = (!row) ? ap->nrows - 1 : row - 1;
300 row = (!row) ? ap->nrows - 1 : row - 1;
303 col = (!col) ? ap->ncols - 1 : col - 1;
304 row = (!row) ? ap->nrows - 1 : row - 1;
307 col = (!col) ? ap->ncols - 1 : col - 1;
310 col = (!col) ? ap->ncols - 1 : col - 1;
311 row = (row + 1 == ap->nrows) ? 0 : row + 1;
314 row = (row + 1 == ap->nrows) ? 0 : row + 1;
317 col = (col + 1 == ap->ncols) ? 0 : col + 1;
318 row = (row + 1 == ap->nrows) ? 0 : row + 1;
321 (void) fprintf(stderr, "wrong direction %d\n", dir);
324 if ((col + row) % 2) { /* right */
327 col = (!col) ? ap->ncols - 1 : col - 1;
331 col = (!col) ? ap->ncols - 1 : col - 1;
332 row = (!row) ? ap->nrows - 1 : row - 1;
335 col = (!col) ? ap->ncols - 1 : col - 1;
353 row = (!row) ? ap->nrows - 1 : row - 1;
357 col = (col + 1 == ap->ncols) ? 0 : col + 1;
358 row = (!row) ? ap->nrows - 1 : row - 1;
361 col = (col + 1 == ap->ncols) ? 0 : col + 1;
365 col = (col + 1 == ap->ncols) ? 0 : col + 1;
366 row = (row + 1 == ap->nrows) ? 0 : row + 1;
369 row = (row + 1 == ap->nrows) ? 0 : row + 1;
373 if (row + 1 == ap->nrows)
375 else if (row + 2 == ap->nrows)
381 col = (!col) ? ap->ncols - 1 : col - 1;
382 if (row + 1 == ap->nrows)
384 else if (row + 2 == ap->nrows)
391 col = (!col) ? ap->ncols - 1 : col - 1;
392 row = (row + 1 == ap->nrows) ? 0 : row + 1;
395 (void) fprintf(stderr, "wrong direction %d\n", dir);
400 col = (col + 1 == ap->ncols) ? 0 : col + 1;
404 col = (col + 1 == ap->ncols) ? 0 : col + 1;
405 row = (row + 1 == ap->nrows) ? 0 : row + 1;
408 col = (col + 1 == ap->ncols) ? 0 : col + 1;
409 if (row + 1 == ap->nrows)
411 else if (row + 2 == ap->nrows)
418 if (row + 1 == ap->nrows)
420 else if (row + 2 == ap->nrows)
426 row = (row + 1 == ap->nrows) ? 0 : row + 1;
430 col = (!col) ? ap->ncols - 1 : col - 1;
431 row = (row + 1 == ap->nrows) ? 0 : row + 1;
434 col = (!col) ? ap->ncols - 1 : col - 1;
438 col = (!col) ? ap->ncols - 1 : col - 1;
439 row = (!row) ? ap->nrows - 1 : row - 1;
442 row = (!row) ? ap->nrows - 1 : row - 1;
454 col = (col + 1 == ap->ncols) ? 0 : col + 1;
464 col = (col + 1 == ap->ncols) ? 0 : col + 1;
465 row = (!row) ? ap->nrows - 1 : row - 1;
468 (void) fprintf(stderr, "wrong direction %d\n", dir);
477 fillcell(ModeInfo * mi, GC gc, int col, int row)
479 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
481 if (ap->neighbors == 6) {
482 int ccol = 2 * col + !(row & 1), crow = 2 * row;
484 ap->shape.hexagon[0].x = ap->xb + ccol * ap->xs;
485 ap->shape.hexagon[0].y = ap->yb + crow * ap->ys;
486 if (ap->xs == 1 && ap->ys == 1)
487 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
488 ap->shape.hexagon[0].x, ap->shape.hexagon[0].y, 1, 1);
490 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
491 ap->shape.hexagon, 6, Convex, CoordModePrevious);
492 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
493 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
494 ap->xb + ap->xs * col, ap->yb + ap->ys * row,
495 ap->xs - (ap->xs > 3), ap->ys - (ap->ys > 3));
497 int orient = (col + row) % 2; /* O left 1 right */
499 ap->shape.triangle[orient][0].x = ap->xb + col * ap->xs;
500 ap->shape.triangle[orient][0].y = ap->yb + row * ap->ys;
501 if (ap->xs <= 3 || ap->ys <= 3)
502 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
503 ((orient) ? -1 : 1) + ap->shape.triangle[orient][0].x,
504 ap->shape.triangle[orient][0].y, 1, 1);
507 ap->shape.triangle[orient][0].x += (ap->xs / 2 - 1);
509 ap->shape.triangle[orient][0].x -= (ap->xs / 2 - 1);
510 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
511 ap->shape.triangle[orient], 3, Convex, CoordModePrevious);
517 truchetcell(ModeInfo * mi, int col, int row, int truchetstate)
519 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
521 if (ap->neighbors == 6) {
523 int ccol = 2 * col + !(row & 1), crow = 2 * row;
525 int fudge = 7; /* fudge because the hexagons are not exact */
529 hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs / 2.0) - 1;
530 hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys / 2.0) - 1;
531 for (side = 0; side < 6; side++) {
533 hex.x += ap->shape.hexagon[side].x;
534 hex.y += ap->shape.hexagon[side].y;
536 if (truchetstate == side % 2)
537 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
538 hex.x, hex.y, ap->xs, ap->ys,
539 ((570 - (side * 60) + fudge) % 360) * 64, (120 - 2 * fudge) * 64);
543 /* Very crude approx of Sqrt 3, so it will not cause drawing errors. */
544 hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs * 1.6 / 2.0);
545 hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys * 1.6 / 2.0);
546 for (side = 0; side < 6; side++) {
548 hex.x += ap->shape.hexagon[side].x;
549 hex.y += ap->shape.hexagon[side].y;
551 hex2.x = hex.x + ap->shape.hexagon[side + 1].x / 2;
552 hex2.y = hex.y + ap->shape.hexagon[side + 1].y / 2;
553 if (truchetstate == side % 3)
554 /* Crude approx of 120 deg, so it will not cause drawing errors. */
555 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
557 (int) ((double) ap->xs * 1.5), (int) ((double) ap->ys * 1.5),
558 ((555 - (side * 60)) % 360) * 64, 90 * 64);
561 } else if (ap->neighbors == 4) {
563 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
564 ap->xb + ap->xs * col - ap->xs / 2+ 1,
565 ap->yb + ap->ys * row + ap->ys / 2 - 1,
566 ap->xs - 2, ap->ys - 2,
568 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
569 ap->xb + ap->xs * col + ap->xs / 2 - 1,
570 ap->yb + ap->ys * row - ap->ys / 2 + 1,
571 ap->xs - 2, ap->ys - 2,
574 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
575 ap->xb + ap->xs * col - ap->xs / 2 + 1,
576 ap->yb + ap->ys * row - ap->ys / 2 + 1,
577 ap->xs - 2, ap->ys - 2,
579 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
580 ap->xb + ap->xs * col + ap->xs / 2 - 1,
581 ap->yb + ap->ys * row + ap->ys / 2 - 1,
582 ap->xs - 2, ap->ys - 2,
585 } else if (ap->neighbors == 3) {
586 int orient = (col + row) % 2; /* O left 1 right */
588 int fudge = 7; /* fudge because the triangles are not exact */
591 tri.x = ap->xb + col * ap->xs;
592 tri.y = ap->yb + row * ap->ys;
594 tri.x += (ap->xs / 2 - 1);
596 tri.x -= (ap->xs / 2 - 1);
598 for (side = 0; side < 3; side++) {
600 tri.x += ap->shape.triangle[orient][side].x;
601 tri.y += ap->shape.triangle[orient][side].y;
603 if (truchetstate == side % 3) {
605 ang = (510 - side * 120) % 360; /* Right */
607 ang = (690 - side * 120) % 360; /* Left */
608 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
609 tri.x - ap->xs / 2, tri.y - 3 * ap->ys / 4,
610 ap->xs, 3 * ap->ys / 2,
611 (ang + fudge) * 64, (60 - 2 * fudge) * 64);
618 drawcell(ModeInfo * mi, int col, int row, unsigned char color)
620 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
624 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
626 } else if (MI_NPIXELS(mi) > 2) {
627 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
628 MI_PIXEL(mi, ap->colors[color - 1]));
633 gcv.stipple = ap->pixmaps[color - 1];
634 gcv.foreground = MI_WHITE_PIXEL(mi);
635 gcv.background = MI_BLACK_PIXEL(mi);
636 XChangeGC(MI_DISPLAY(mi), ap->stippledGC,
637 GCStipple | GCForeground | GCBackground, &gcv);
640 fillcell(mi, gc, col, row);
644 drawtruchet(ModeInfo * mi, int col, int row,
645 unsigned char color, unsigned char truchetstate)
647 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
650 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
651 else if (MI_NPIXELS(mi) > 2 || color > ap->ncolors / 2)
652 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
654 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
655 truchetcell(mi, col, row, truchetstate);
659 draw_anant(ModeInfo * mi, int col, int row)
661 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
662 fillcell(mi, MI_GC(mi), col, row);
663 #if 0 /* Can not see eyes */
665 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
666 Display *display = MI_DISPLAY(mi);
667 Window window = MI_WINDOW(mi);
669 if (ap->xs > 2 && ap->ys > 2) { /* Draw Eyes */
671 XSetForeground(display, MI_GC(mi), MI_BLACK_PIXEL(mi));
674 XDrawPoint(display, window, MI_GC(mi),
675 ap->xb + ap->xs - 1, ap->yb + 1);
676 XDrawPoint(display, window, MI_GC(mi),
677 ap->xb + ap->xs - 1, ap->yb + ap->ys - 2);
680 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + 1);
681 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + ap->ys - 2);
683 if (neighbors == 4) {
685 XDrawPoint(display, window, MI_GC(mi), ap->xb + 1, ap->yb);
686 XDrawPoint(display, window, MI_GC(mi),
687 ap->xb + ap->xs - 2, ap->yb);
690 XDrawPoint(display, window, MI_GC(mi),
691 ap->xb + 1, ap->yb + ap->ys - 1);
692 XDrawPoint(display, window, MI_GC(mi),
693 ap->xb + ap->xs - 2, ap->yb + ap->ys - 1);
708 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
709 int row, col, mrow = 0;
711 for (row = 0; row < ap->nrows; ++row) {
712 for (col = 0; col < ap->ncols; ++col) {
713 ap->old[col + mrow] = (unsigned char) NRAND((int) ap->ncolors);
714 drawcell(mi, col, row, ap->old[col + mrow]);
723 fromTableDirection(unsigned char dir, int neighbors)
729 return (ANGLES / neighbors);
731 return (ANGLES / 2 - ANGLES / neighbors);
735 return (ANGLES / 2 + ANGLES / neighbors);
737 return (ANGLES - ANGLES / neighbors);
741 return (ANGLES + ANGLES / neighbors);
743 return (3 * ANGLES / 2 - ANGLES / neighbors);
745 return (3 * ANGLES / 2);
747 return (3 * ANGLES / 2 + ANGLES / neighbors);
749 return (2 * ANGLES - ANGLES / neighbors);
751 (void) fprintf(stderr, "wrong direction %d\n", dir);
757 getTable(ModeInfo * mi, int i)
759 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
761 unsigned char *patptr;
763 patptr = &tables[i][0];
764 ap->ncolors = *patptr++;
765 ap->nstates = *patptr++;
766 total = ap->ncolors * ap->nstates;
767 if (MI_IS_VERBOSE(mi))
768 (void) fprintf(stdout,
769 "ants %d, neighbors %d, table number %d, colors %d, states %d\n",
770 ap->n, ap->neighbors, i, ap->ncolors, ap->nstates);
771 for (j = 0; j < total; j++) {
772 ap->machine[j].color = *patptr++;
773 if (ap->sharpturn && ap->neighbors > 4 && !(ap->neighbors % 2)) {
804 ap->machine[j].direction = fromTableDirection(k, ap->neighbors);
806 ap->machine[j].direction = fromTableDirection(*patptr++, ap->neighbors);
808 ap->machine[j].next = *patptr++;
814 getTurk(ModeInfo * mi, int i)
816 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
817 int power2, j, number, total;
819 /* To force a number, say <i = 2;> has i + 2 (or 4) digits in binary */
820 power2 = 1 << (i + 1);
821 /* Dont want numbers which in binary are all 1's. */
822 number = NRAND(power2 - 1) + power2;
823 /* To force a particular number, say <number = 10;> */
827 total = ap->ncolors * ap->nstates;
828 for (j = 0; j < total; j++) {
829 ap->machine[j].color = (j + 1) % total;
830 if (ap->sharpturn && ap->neighbors > 4 && !(ap->neighbors % 2)) {
831 ap->machine[j].direction = (power2 & number) ?
832 fromTableDirection(THRS, ap->neighbors) :
833 fromTableDirection(THLS, ap->neighbors);
835 ap->machine[j].direction = (power2 & number) ?
836 fromTableDirection(TRS, ap->neighbors) :
837 fromTableDirection(TLS, ap->neighbors);
839 ap->machine[j].next = 0;
842 ap->truchet = (ap->truchet && ap->xs > 2 && ap->ys > 2 &&
843 (ap->neighbors == 3 || ap->neighbors == 4 || ap->neighbors == 6));
844 if (MI_IS_VERBOSE(mi))
845 (void) fprintf(stdout,
846 "ants %d, neighbors %d, Turk's number %d, colors %d\n",
847 ap->n, ap->neighbors, number, ap->ncolors);
851 init_ant(ModeInfo * mi)
853 Display *display = MI_DISPLAY(mi);
854 Window window = MI_WINDOW(mi);
855 int size = MI_SIZE(mi);
860 /* jwz sez: small sizes look like crap */
862 size = NRAND(-size)+1;
866 if (antfarms == NULL) {
867 if ((antfarms = (antfarmstruct *) calloc(MI_NUM_SCREENS(mi),
868 sizeof (antfarmstruct))) == NULL)
871 ap = &antfarms[MI_SCREEN(mi)];
873 if (MI_NPIXELS(mi) <= 2) {
874 if (ap->stippledGC == None) {
877 gcv.fill_style = FillOpaqueStippled;
878 ap->stippledGC = XCreateGC(display, window, GCFillStyle, &gcv);
880 if (ap->init_bits == 0) {
881 for (i = 1; i < NUMSTIPPLES; i++)
882 ANTBITS(stipples[i], STIPPLESIZE, STIPPLESIZE);
886 ap->n = MI_COUNT(mi);
887 if (ap->n < -MINANTS) {
888 /* if ap->n is random ... the size can change */
889 if (ap->ants != NULL) {
890 (void) free((void *) ap->ants);
893 ap->n = NRAND(-ap->n - MINANTS + 1) + MINANTS;
894 } else if (ap->n < MINANTS)
897 ap->width = MI_WIDTH(mi);
898 ap->height = MI_HEIGHT(mi);
900 if (neighbors == 8 || neighbors == 9 || neighbors == 12)
901 ap->neighbors = neighbors; /* Discourage but not deny use... */
903 for (i = 0; i < NEIGHBORKINDS; i++) {
904 if (neighbors == plots[i]) {
905 ap->neighbors = plots[i];
908 if (i == NEIGHBORKINDS - 1) {
909 ap->neighbors = plots[NRAND(NEIGHBORKINDS)];
914 if (ap->neighbors == 6) {
921 if (size < -MINSIZE) {
922 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
923 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
924 if (ap->ys < MINRANDOMSIZE)
925 ap->ys = MIN(MINRANDOMSIZE,
926 MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
927 } else if (size < MINSIZE) {
929 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
933 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
936 nccols = MAX(ap->width / ap->xs - 2, 2);
937 ncrows = MAX(ap->height / ap->ys - 1, 2);
938 ap->ncols = nccols / 2;
939 ap->nrows = 2 * (ncrows / 4);
940 ap->xb = (ap->width - ap->xs * nccols) / 2 + ap->xs / 2;
941 ap->yb = (ap->height - ap->ys * (ncrows / 2) * 2) / 2 + ap->ys;
942 for (i = 0; i < 6; i++) {
943 ap->shape.hexagon[i].x = (ap->xs - 1) * hexagonUnit[i].x;
944 ap->shape.hexagon[i].y = ((ap->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
946 /* Avoid array bounds read of hexagonUnit */
947 ap->shape.hexagon[6].x = 0;
948 ap->shape.hexagon[6].y = 0;
949 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
950 if (size < -MINSIZE) {
951 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
952 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
953 if (ap->ys < MINRANDOMSIZE)
954 ap->ys = MIN(MINRANDOMSIZE,
955 MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
956 } else if (size < MINSIZE) {
958 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
962 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
965 ap->ncols = MAX(ap->width / ap->xs, 2);
966 ap->nrows = MAX(ap->height / ap->ys, 2);
967 ap->xb = (ap->width - ap->xs * ap->ncols) / 2;
968 ap->yb = (ap->height - ap->ys * ap->nrows) / 2;
976 if (size < -MINSIZE) {
977 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
978 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
979 if (ap->ys < MINRANDOMSIZE)
980 ap->ys = MIN(MINRANDOMSIZE,
981 MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
982 } else if (size < MINSIZE) {
984 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
988 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
990 ap->xs = (int) (1.52 * ap->ys);
991 ap->ncols = (MAX(ap->width / ap->xs - 1, 2) / 2) * 2;
992 ap->nrows = (MAX(ap->height / ap->ys - 1, 2) / 2) * 2;
993 ap->xb = (ap->width - ap->xs * ap->ncols) / 2 + ap->xs / 2;
994 ap->yb = (ap->height - ap->ys * ap->nrows) / 2 + ap->ys;
995 for (orient = 0; orient < 2; orient++) {
996 for (i = 0; i < 3; i++) {
997 ap->shape.triangle[orient][i].x =
998 (ap->xs - 2) * triangleUnit[orient][i].x;
999 ap->shape.triangle[orient][i].y =
1000 (ap->ys - 2) * triangleUnit[orient][i].y;
1002 /* Avoid array bounds read of triangleUnit */
1003 ap->shape.triangle[orient][3].x = 0;
1004 ap->shape.triangle[orient][3].y = 0;
1008 XSetLineAttributes(display, MI_GC(mi), 1, LineSolid, CapNotLast, JoinMiter);
1010 ap->painted = False;
1012 if (MI_IS_FULLRANDOM(mi)) {
1013 ap->truchet = (Bool) (LRAND() & 1);
1014 ap->sharpturn = (Bool) (LRAND() & 1);
1016 ap->truchet = truchet;
1017 ap->sharpturn = sharpturn;
1019 /* Exclude odd # of neighbors, stepping forward not defined */
1020 if (!NRAND(NUMSTIPPLES) && ((ap->neighbors + 1) % 2)) {
1021 getTable(mi, (int) (NRAND(NTABLES)));
1023 getTurk(mi, (int) (NRAND(NUMSTIPPLES - 1)));
1024 if (MI_NPIXELS(mi) > 2)
1025 for (i = 0; i < (int) ap->ncolors - 1; i++)
1026 ap->colors[i] = (unsigned char) (NRAND(MI_NPIXELS(mi)) +
1027 i * MI_NPIXELS(mi)) / ((int) (ap->ncolors - 1));
1028 if (ap->ants == NULL)
1029 ap->ants = (antstruct *) malloc(ap->n * sizeof (antstruct));
1030 if (ap->tape != NULL)
1031 (void) free((void *) ap->tape);
1032 ap->tape = (unsigned char *)
1033 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
1035 if (ap->truchet_state != NULL)
1036 (void) free((void *) ap->truchet_state);
1037 ap->truchet_state = (unsigned char *)
1038 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
1040 col = ap->ncols / 2;
1041 row = ap->nrows / 2;
1042 dir = NRAND(ap->neighbors) * ANGLES / ap->neighbors;
1043 /* Have them all start in the same spot, why not? */
1044 for (i = 0; i < ap->n; i++) {
1045 ap->ants[i].col = col;
1046 ap->ants[i].row = row;
1047 ap->ants[i].direction = dir;
1048 ap->ants[i].state = 0;
1050 draw_anant(mi, col, row);
1054 draw_ant(ModeInfo * mi)
1056 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
1058 statestruct *status;
1059 int i, state_pos, tape_pos;
1060 unsigned char color;
1061 short chg_dir, old_dir;
1063 MI_IS_DRAWN(mi) = True;
1066 for (i = 0; i < ap->n; i++) {
1067 anant = &ap->ants[i];
1068 tape_pos = anant->col + anant->row * ap->ncols;
1069 color = ap->tape[tape_pos]; /* read tape */
1070 state_pos = color + anant->state * ap->ncolors;
1071 status = &(ap->machine[state_pos]);
1072 drawcell(mi, anant->col, anant->row, status->color);
1073 ap->tape[tape_pos] = status->color; /* write on tape */
1075 /* Find direction of Bees or Ants. */
1076 /* Translate relative direction to actual direction */
1077 old_dir = anant->direction;
1078 chg_dir = (2 * ANGLES - status->direction) % ANGLES;
1079 anant->direction = (chg_dir + old_dir) % ANGLES;
1083 if (ap->neighbors == 6) {
1084 if (ap->sharpturn) {
1085 a = (chg_dir / 120 == 2);
1086 drawtruchet(mi, anant->col, anant->row, status->color, a);
1088 a = (old_dir / 60) % 3;
1089 b = (anant->direction / 60) % 3;
1090 a = (a + b + 1) % 3;
1091 drawtruchet(mi, anant->col, anant->row, status->color, a);
1093 } else if (ap->neighbors == 4) {
1095 b = anant->direction / 180;
1096 a = ((a && !b) || (b && !a));
1097 drawtruchet(mi, anant->col, anant->row, status->color, a);
1098 } else if (ap->neighbors == 3) {
1100 a = (2 + anant->direction / 120) % 3;
1102 a = (1 + anant->direction / 120) % 3;
1103 drawtruchet(mi, anant->col, anant->row, status->color, a);
1105 ap->truchet_state[tape_pos] = a + 1;
1107 anant->state = status->next;
1109 /* If edge than wrap it */
1110 old_dir = ((status->direction < ANGLES) ? anant->direction : old_dir);
1111 position_of_neighbor(ap, old_dir, &(anant->col), &(anant->row));
1112 draw_anant(mi, anant->col, anant->row);
1114 if (++ap->generation > MI_CYCLES(mi)) {
1116 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
1120 if (ap->redrawing) {
1121 for (i = 0; i < REDRAWSTEP; i++) {
1122 if (ap->tape[ap->redrawpos] ||
1123 (ap->truchet && ap->truchet_state[ap->redrawpos])) {
1124 drawcell(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1125 ap->tape[ap->redrawpos]);
1127 drawtruchet(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1128 ap->tape[ap->redrawpos],
1129 ap->truchet_state[ap->redrawpos] - 1);
1131 if (++(ap->redrawpos) >= ap->ncols * ap->nrows) {
1140 release_ant(ModeInfo * mi)
1142 if (antfarms != NULL) {
1145 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1146 antfarmstruct *ap = &antfarms[screen];
1149 if (ap->stippledGC != None) {
1150 XFreeGC(MI_DISPLAY(mi), ap->stippledGC);
1152 for (shade = 0; shade < ap->init_bits; shade++)
1153 XFreePixmap(MI_DISPLAY(mi), ap->pixmaps[shade]);
1154 if (ap->tape != NULL)
1155 (void) free((void *) ap->tape);
1156 if (ap->ants != NULL)
1157 (void) free((void *) ap->ants);
1158 if (ap->truchet_state != NULL)
1159 (void) free((void *) ap->truchet_state);
1161 (void) free((void *) antfarms);
1167 refresh_ant(ModeInfo * mi)
1169 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];