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 # include "automata.h"
72 #endif /* STANDALONE */
75 * neighbors of 0 randomizes it between 3, 4 and 6.
76 * 8, 9 12 are available also but not recommended.
83 #endif /* !STANDALONE */
85 #define DEF_TRUCHET "False"
86 #define DEF_SHARPTURN "False"
89 static Bool sharpturn;
91 static XrmOptionDescRec opts[] =
93 {"-truchet", ".ant.truchet", XrmoptionNoArg, (caddr_t) "on"},
94 {"+truchet", ".ant.truchet", XrmoptionNoArg, (caddr_t) "off"},
95 {"-sharpturn", ".ant.sharpturn", XrmoptionNoArg, (caddr_t) "on"},
96 {"+sharpturn", ".ant.sharpturn", XrmoptionNoArg, (caddr_t) "off"},
99 {"-neighbors", ".ant.neighbors", XrmoptionSepArg, (caddr_t) 0},
100 {"+neighbors", ".ant.neighbors", XrmoptionSepArg, (caddr_t) 0}
101 #endif /* STANDALONE */
104 static argtype vars[] =
106 {(caddr_t *) & truchet, "truchet", "Truchet", DEF_TRUCHET, t_Bool},
107 {(caddr_t *) & sharpturn, "sharpturn", "SharpTurn", DEF_SHARPTURN, t_Bool},
109 {(caddr_t *) & neighbors, "neighbors", "Neighbors", 0, t_Int}
110 #endif /* STANDALONE */
112 static OptionStruct desc[] =
114 {"-/+truchet", "turn on/off Truchet lines"},
115 {"-/+sharpturn", "turn on/off sharp turns (6 or 12 neighbors only)"}
118 ModeSpecOpt ant_opts =
119 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
122 ModStruct ant_description =
123 {"ant", "init_ant", "draw_ant", "release_ant",
124 "refresh_ant", "init_ant", NULL, &ant_opts,
125 1000, -3, 40000, -12, 64, 1.0, "",
126 "Shows Langton's and Turk's generalized ants", 0, NULL};
130 #define ANTBITS(n,w,h)\
131 ap->pixmaps[ap->init_bits++]=\
132 XCreatePixmapFromBitmapData(display,window,(char *)n,w,h,1,0,1)
134 /* If you change the table you may have to change the following 2 constants */
137 #define REDRAWSTEP 2000 /* How much tape to draw per cycle */
138 #define MINGRIDSIZE 24
140 #define MINRANDOMSIZE 5
145 #define NUMSTIPPLES 11
146 #define STIPPLESIZE 8
148 static XPoint hexagonUnit[6] =
158 static XPoint triangleUnit[2][3] =
173 static unsigned char stipples[NUMSTIPPLES][STIPPLESIZE] =
175 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* white */
176 {0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22}, /* grey+white | stripe */
177 {0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00}, /* spots */
178 {0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11}, /* lt. / stripe */
179 {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}, /* | bars */
180 {0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa}, /* 50% grey */
181 {0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00}, /* - bars */
182 {0xee, 0xdd, 0xbb, 0x77, 0xee, 0xdd, 0xbb, 0x77}, /* dark \ stripe */
183 {0xff, 0x99, 0x99, 0xff, 0xff, 0x99, 0x99, 0xff}, /* spots */
184 {0xaa, 0xff, 0xff, 0x55, 0xaa, 0xff, 0xff, 0x55}, /* black+grey - stripe */
185 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} /* black */
188 #endif /* STANDALONE */
210 unsigned char ncolors, nstates;
212 int redrawing, redrawpos;
213 int truchet; /* Only for Turk modes */
214 int sharpturn; /* Only for even neighbors > 4 (i.e. 6 and 12) */
215 statestruct machine[NUMSTIPPLES * STATES];
217 unsigned char *truchet_state;
220 unsigned char colors[NUMSTIPPLES - 1];
222 Pixmap pixmaps[NUMSTIPPLES - 1];
224 XPoint hexagon[7]; /* Need more than 6 for truchet */
225 XPoint triangle[2][4]; /* Need more than 3 for truchet */
229 static char plots[] =
231 #if 1 /* Without this... this mode is misnamed... */
234 6}; /* Neighborhoods, 8 just makes a mess */
236 #define NEIGHBORKINDS (long) (sizeof plots / sizeof *plots)
238 /* Relative ant moves */
239 #define FS 0 /* Step */
240 #define TRS 1 /* Turn right, then step */
241 #define THRS 2 /* Turn hard right, then step */
242 #define TBS 3 /* Turn back, then step */
243 #define THLS 4 /* Turn hard left, then step */
244 #define TLS 5 /* Turn left, then step */
245 #define SF 6 /* Step */
246 #define STR 7 /* Step then turn right */
247 #define STHR 8 /* Step then turn hard right */
248 #define STB 9 /* Step then turn back */
249 #define STHL 10 /* Step then turn hard left */
250 #define STL 11 /* Step then turn left */
252 static antfarmstruct *antfarms = NULL;
254 /* LANGTON'S ANT (10) Chaotic after 500, Builder after 10,000 (104p) */
255 /* TURK'S 100 ANT Always chaotic?, tested past 150,000,000 */
256 /* TURK'S 101 ANT Always chaotic? */
257 /* TURK'S 110 ANT Builder at 150 (18p) */
258 /* TURK'S 1000 ANT Always chaotic? */
259 /* TURK'S 1100 SYMMETRIC ANT all even run 1's and 0's are symmetric */
260 /* other examples 1001, 110011, 110000, 1001101 */
261 /* TURK'S 1101 ANT Builder after 250,000 (388p) */
262 /* Once saw a chess horse type builder (i.e. non-45 degree builder) */
265 /* All alternating 10 appear symmetric, no proof (i.e. 10, 1010, etc) */
266 /* Even runs of 0's and 1's are also symmetric */
267 /* I have seen Hexagonal builders but they are more rare. */
269 static unsigned char tables[][3 * NUMSTIPPLES * STATES + 2] =
272 /* Here just so you can figure out notation */
273 { /* Langton's ant */
278 /* First 2 numbers are the size (ncolors, nstates) */
279 { /* LADDER BUILDER */
281 1, STR, 0, 2, STL, 0, 3, TRS, 0, 0, TLS, 0
283 { /* SPIRALING PATTERN */
288 { /* SQUARE (HEXAGON) BUILDER */
296 #define NTABLES (sizeof tables / sizeof tables[0])
299 position_of_neighbor(antfarmstruct * ap, int dir, int *pcol, int *prow)
301 int col = *pcol, row = *prow;
303 if (ap->neighbors == 6) {
306 col = (col + 1 == ap->ncols) ? 0 : col + 1;
310 col = (col + 1 == ap->ncols) ? 0 : col + 1;
311 row = (!row) ? ap->nrows - 1 : row - 1;
315 col = (!col) ? ap->ncols - 1 : col - 1;
316 row = (!row) ? ap->nrows - 1 : row - 1;
319 col = (!col) ? ap->ncols - 1 : col - 1;
323 col = (!col) ? ap->ncols - 1 : col - 1;
324 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);
334 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
337 col = (col + 1 == ap->ncols) ? 0 : col + 1;
340 col = (col + 1 == ap->ncols) ? 0 : col + 1;
341 row = (!row) ? ap->nrows - 1 : row - 1;
344 row = (!row) ? ap->nrows - 1 : row - 1;
347 col = (!col) ? ap->ncols - 1 : col - 1;
348 row = (!row) ? ap->nrows - 1 : row - 1;
351 col = (!col) ? ap->ncols - 1 : col - 1;
354 col = (!col) ? ap->ncols - 1 : col - 1;
355 row = (row + 1 == ap->nrows) ? 0 : row + 1;
358 row = (row + 1 == ap->nrows) ? 0 : row + 1;
361 col = (col + 1 == ap->ncols) ? 0 : col + 1;
362 row = (row + 1 == ap->nrows) ? 0 : row + 1;
365 (void) fprintf(stderr, "wrong direction %d\n", dir);
368 if ((col + row) % 2) { /* right */
371 col = (!col) ? ap->ncols - 1 : col - 1;
375 col = (!col) ? ap->ncols - 1 : col - 1;
376 row = (!row) ? ap->nrows - 1 : row - 1;
379 col = (!col) ? ap->ncols - 1 : col - 1;
397 row = (!row) ? ap->nrows - 1 : row - 1;
401 col = (col + 1 == ap->ncols) ? 0 : col + 1;
402 row = (!row) ? ap->nrows - 1 : row - 1;
405 col = (col + 1 == ap->ncols) ? 0 : col + 1;
409 col = (col + 1 == ap->ncols) ? 0 : col + 1;
410 row = (row + 1 == ap->nrows) ? 0 : row + 1;
413 row = (row + 1 == ap->nrows) ? 0 : row + 1;
417 if (row + 1 == ap->nrows)
419 else if (row + 2 == ap->nrows)
425 col = (!col) ? ap->ncols - 1 : col - 1;
426 if (row + 1 == ap->nrows)
428 else if (row + 2 == ap->nrows)
435 col = (!col) ? ap->ncols - 1 : col - 1;
436 row = (row + 1 == ap->nrows) ? 0 : row + 1;
439 (void) fprintf(stderr, "wrong direction %d\n", dir);
444 col = (col + 1 == ap->ncols) ? 0 : col + 1;
448 col = (col + 1 == ap->ncols) ? 0 : col + 1;
449 row = (row + 1 == ap->nrows) ? 0 : row + 1;
452 col = (col + 1 == ap->ncols) ? 0 : col + 1;
453 if (row + 1 == ap->nrows)
455 else if (row + 2 == ap->nrows)
462 if (row + 1 == ap->nrows)
464 else if (row + 2 == ap->nrows)
470 row = (row + 1 == ap->nrows) ? 0 : row + 1;
474 col = (!col) ? ap->ncols - 1 : col - 1;
475 row = (row + 1 == ap->nrows) ? 0 : row + 1;
478 col = (!col) ? ap->ncols - 1 : col - 1;
482 col = (!col) ? ap->ncols - 1 : col - 1;
483 row = (!row) ? ap->nrows - 1 : row - 1;
486 row = (!row) ? ap->nrows - 1 : row - 1;
498 col = (col + 1 == ap->ncols) ? 0 : col + 1;
508 col = (col + 1 == ap->ncols) ? 0 : col + 1;
509 row = (!row) ? ap->nrows - 1 : row - 1;
512 (void) fprintf(stderr, "wrong direction %d\n", dir);
521 fillcell(ModeInfo * mi, GC gc, int col, int row)
523 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
525 if (ap->neighbors == 6) {
526 int ccol = 2 * col + !(row & 1), crow = 2 * row;
528 ap->shape.hexagon[0].x = ap->xb + ccol * ap->xs;
529 ap->shape.hexagon[0].y = ap->yb + crow * ap->ys;
530 if (ap->xs == 1 && ap->ys == 1)
531 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
532 ap->shape.hexagon[0].x, ap->shape.hexagon[0].y, 1, 1);
534 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
535 ap->shape.hexagon, 6, Convex, CoordModePrevious);
536 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
537 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
538 ap->xb + ap->xs * col, ap->yb + ap->ys * row,
539 ap->xs - (ap->xs > 3), ap->ys - (ap->ys > 3));
541 int orient = (col + row) % 2; /* O left 1 right */
543 ap->shape.triangle[orient][0].x = ap->xb + col * ap->xs;
544 ap->shape.triangle[orient][0].y = ap->yb + row * ap->ys;
545 if (ap->xs <= 3 || ap->ys <= 3)
546 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
547 ((orient) ? -1 : 1) + ap->shape.triangle[orient][0].x,
548 ap->shape.triangle[orient][0].y, 1, 1);
551 ap->shape.triangle[orient][0].x += (ap->xs / 2 - 1);
553 ap->shape.triangle[orient][0].x -= (ap->xs / 2 - 1);
554 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
555 ap->shape.triangle[orient], 3, Convex, CoordModePrevious);
561 truchetcell(ModeInfo * mi, int col, int row, int truchetstate)
563 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
565 if (ap->neighbors == 6) {
567 int ccol = 2 * col + !(row & 1), crow = 2 * row;
569 int fudge = 7; /* fudge because the hexagons are not exact */
573 hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs / 2.0) - 1;
574 hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys / 2.0) - 1;
575 for (side = 0; side < 6; side++) {
577 hex.x += ap->shape.hexagon[side].x;
578 hex.y += ap->shape.hexagon[side].y;
580 if (truchetstate == side % 2)
581 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
582 hex.x, hex.y, ap->xs, ap->ys,
583 ((570 - (side * 60) + fudge) % 360) * 64, (120 - 2 * fudge) * 64);
587 /* Very crude approx of Sqrt 3, so it will not cause drawing errors. */
588 hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs * 1.6 / 2.0);
589 hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys * 1.6 / 2.0);
590 for (side = 0; side < 6; side++) {
592 hex.x += ap->shape.hexagon[side].x;
593 hex.y += ap->shape.hexagon[side].y;
595 hex2.x = hex.x + ap->shape.hexagon[side + 1].x / 2;
596 hex2.y = hex.y + ap->shape.hexagon[side + 1].y / 2;
597 if (truchetstate == side % 3)
598 /* Crude approx of 120 deg, so it will not cause drawing errors. */
599 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
601 (int) ((double) ap->xs * 1.5), (int) ((double) ap->ys * 1.5),
602 ((555 - (side * 60)) % 360) * 64, 90 * 64);
605 } else if (ap->neighbors == 4) {
607 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
608 ap->xb + ap->xs * col - ap->xs / 2+ 1,
609 ap->yb + ap->ys * row + ap->ys / 2 - 1,
610 ap->xs - 2, ap->ys - 2,
612 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
613 ap->xb + ap->xs * col + ap->xs / 2 - 1,
614 ap->yb + ap->ys * row - ap->ys / 2 + 1,
615 ap->xs - 2, ap->ys - 2,
618 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
619 ap->xb + ap->xs * col - ap->xs / 2 + 1,
620 ap->yb + ap->ys * row - ap->ys / 2 + 1,
621 ap->xs - 2, ap->ys - 2,
623 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
624 ap->xb + ap->xs * col + ap->xs / 2 - 1,
625 ap->yb + ap->ys * row + ap->ys / 2 - 1,
626 ap->xs - 2, ap->ys - 2,
629 } else if (ap->neighbors == 3) {
630 int orient = (col + row) % 2; /* O left 1 right */
632 int fudge = 7; /* fudge because the triangles are not exact */
635 tri.x = ap->xb + col * ap->xs;
636 tri.y = ap->yb + row * ap->ys;
638 tri.x += (ap->xs / 2 - 1);
640 tri.x -= (ap->xs / 2 - 1);
642 for (side = 0; side < 3; side++) {
644 tri.x += ap->shape.triangle[orient][side].x;
645 tri.y += ap->shape.triangle[orient][side].y;
647 if (truchetstate == side % 3) {
649 ang = (510 - side * 120) % 360; /* Right */
651 ang = (690 - side * 120) % 360; /* Left */
652 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
653 tri.x - ap->xs / 2, tri.y - 3 * ap->ys / 4,
654 ap->xs, 3 * ap->ys / 2,
655 (ang + fudge) * 64, (60 - 2 * fudge) * 64);
662 drawcell(ModeInfo * mi, int col, int row, unsigned char color)
664 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
668 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
670 } else if (MI_NPIXELS(mi) > 2) {
671 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
672 MI_PIXEL(mi, ap->colors[color - 1]));
677 gcv.stipple = ap->pixmaps[color - 1];
678 gcv.foreground = MI_WHITE_PIXEL(mi);
679 gcv.background = MI_BLACK_PIXEL(mi);
680 XChangeGC(MI_DISPLAY(mi), ap->stippledGC,
681 GCStipple | GCForeground | GCBackground, &gcv);
684 fillcell(mi, gc, col, row);
688 drawtruchet(ModeInfo * mi, int col, int row,
689 unsigned char color, unsigned char truchetstate)
691 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
694 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
695 else if (MI_NPIXELS(mi) > 2 || color > ap->ncolors / 2)
696 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
698 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
699 truchetcell(mi, col, row, truchetstate);
703 draw_anant(ModeInfo * mi, int col, int row)
705 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
706 fillcell(mi, MI_GC(mi), col, row);
707 #if 0 /* Can not see eyes */
709 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
710 Display *display = MI_DISPLAY(mi);
711 Window window = MI_WINDOW(mi);
713 if (ap->xs > 2 && ap->ys > 2) { /* Draw Eyes */
715 XSetForeground(display, MI_GC(mi), MI_BLACK_PIXEL(mi));
718 XDrawPoint(display, window, MI_GC(mi),
719 ap->xb + ap->xs - 1, ap->yb + 1);
720 XDrawPoint(display, window, MI_GC(mi),
721 ap->xb + ap->xs - 1, ap->yb + ap->ys - 2);
724 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + 1);
725 XDrawPoint(display, window, MI_GC(mi), ap->xb, ap->yb + ap->ys - 2);
727 if (neighbors == 4) {
729 XDrawPoint(display, window, MI_GC(mi), ap->xb + 1, ap->yb);
730 XDrawPoint(display, window, MI_GC(mi),
731 ap->xb + ap->xs - 2, ap->yb);
734 XDrawPoint(display, window, MI_GC(mi),
735 ap->xb + 1, ap->yb + ap->ys - 1);
736 XDrawPoint(display, window, MI_GC(mi),
737 ap->xb + ap->xs - 2, ap->yb + ap->ys - 1);
752 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
753 int row, col, mrow = 0;
755 for (row = 0; row < ap->nrows; ++row) {
756 for (col = 0; col < ap->ncols; ++col) {
757 ap->old[col + mrow] = (unsigned char) NRAND((int) ap->ncolors);
758 drawcell(mi, col, row, ap->old[col + mrow]);
767 fromTableDirection(unsigned char dir, int neighbors)
773 return (ANGLES / neighbors);
775 return (ANGLES / 2 - ANGLES / neighbors);
779 return (ANGLES / 2 + ANGLES / neighbors);
781 return (ANGLES - ANGLES / neighbors);
785 return (ANGLES + ANGLES / neighbors);
787 return (3 * ANGLES / 2 - ANGLES / neighbors);
789 return (3 * ANGLES / 2);
791 return (3 * ANGLES / 2 + ANGLES / neighbors);
793 return (2 * ANGLES - ANGLES / neighbors);
795 (void) fprintf(stderr, "wrong direction %d\n", dir);
801 getTable(ModeInfo * mi, int i)
803 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
805 unsigned char *patptr;
807 patptr = &tables[i][0];
808 ap->ncolors = *patptr++;
809 ap->nstates = *patptr++;
810 total = ap->ncolors * ap->nstates;
811 if (MI_IS_VERBOSE(mi))
812 (void) fprintf(stdout,
813 "ants %d, neighbors %d, table number %d, colors %d, states %d\n",
814 ap->n, ap->neighbors, i, ap->ncolors, ap->nstates);
815 for (j = 0; j < total; j++) {
816 ap->machine[j].color = *patptr++;
817 if (ap->sharpturn && ap->neighbors > 4 && !(ap->neighbors % 2)) {
848 ap->machine[j].direction = fromTableDirection(k, ap->neighbors);
850 ap->machine[j].direction = fromTableDirection(*patptr++, ap->neighbors);
852 ap->machine[j].next = *patptr++;
858 getTurk(ModeInfo * mi, int i)
860 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
861 int power2, j, number, total;
863 /* To force a number, say <i = 2;> has i + 2 (or 4) digits in binary */
864 power2 = 1 << (i + 1);
865 /* Dont want numbers which in binary are all 1's. */
866 number = NRAND(power2 - 1) + power2;
867 /* To force a particular number, say <number = 10;> */
871 total = ap->ncolors * ap->nstates;
872 for (j = 0; j < total; j++) {
873 ap->machine[j].color = (j + 1) % total;
874 if (ap->sharpturn && ap->neighbors > 4 && !(ap->neighbors % 2)) {
875 ap->machine[j].direction = (power2 & number) ?
876 fromTableDirection(THRS, ap->neighbors) :
877 fromTableDirection(THLS, ap->neighbors);
879 ap->machine[j].direction = (power2 & number) ?
880 fromTableDirection(TRS, ap->neighbors) :
881 fromTableDirection(TLS, ap->neighbors);
883 ap->machine[j].next = 0;
886 ap->truchet = (ap->truchet && ap->xs > 2 && ap->ys > 2 &&
887 (ap->neighbors == 3 || ap->neighbors == 4 || ap->neighbors == 6));
888 if (MI_IS_VERBOSE(mi))
889 (void) fprintf(stdout,
890 "ants %d, neighbors %d, Turk's number %d, colors %d\n",
891 ap->n, ap->neighbors, number, ap->ncolors);
895 init_ant(ModeInfo * mi)
897 Display *display = MI_DISPLAY(mi);
898 Window window = MI_WINDOW(mi);
899 int size = MI_SIZE(mi);
904 /* jwz sez: small sizes look like crap */
906 size = NRAND(-size)+1;
910 if (antfarms == NULL) {
911 if ((antfarms = (antfarmstruct *) calloc(MI_NUM_SCREENS(mi),
912 sizeof (antfarmstruct))) == NULL)
915 ap = &antfarms[MI_SCREEN(mi)];
917 if (MI_NPIXELS(mi) <= 2) {
918 if (ap->stippledGC == None) {
921 gcv.fill_style = FillOpaqueStippled;
922 ap->stippledGC = XCreateGC(display, window, GCFillStyle, &gcv);
924 if (ap->init_bits == 0) {
925 for (i = 1; i < NUMSTIPPLES; i++)
926 ANTBITS(stipples[i], STIPPLESIZE, STIPPLESIZE);
930 ap->n = MI_COUNT(mi);
931 if (ap->n < -MINANTS) {
932 /* if ap->n is random ... the size can change */
933 if (ap->ants != NULL) {
934 (void) free((void *) ap->ants);
937 ap->n = NRAND(-ap->n - MINANTS + 1) + MINANTS;
938 } else if (ap->n < MINANTS)
941 ap->width = MI_WIDTH(mi);
942 ap->height = MI_HEIGHT(mi);
944 if (neighbors == 8 || neighbors == 9 || neighbors == 12)
945 ap->neighbors = neighbors; /* Discourage but not deny use... */
947 for (i = 0; i < NEIGHBORKINDS; i++) {
948 if (neighbors == plots[i]) {
949 ap->neighbors = plots[i];
952 if (i == NEIGHBORKINDS - 1) {
953 ap->neighbors = plots[NRAND(NEIGHBORKINDS)];
958 if (ap->neighbors == 6) {
965 if (size < -MINSIZE) {
966 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
967 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
968 if (ap->ys < MINRANDOMSIZE)
969 ap->ys = MIN(MINRANDOMSIZE,
970 MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
971 } else if (size < MINSIZE) {
973 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
977 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
980 nccols = MAX(ap->width / ap->xs - 2, 2);
981 ncrows = MAX(ap->height / ap->ys - 1, 2);
982 ap->ncols = nccols / 2;
983 ap->nrows = 2 * (ncrows / 4);
984 ap->xb = (ap->width - ap->xs * nccols) / 2 + ap->xs / 2;
985 ap->yb = (ap->height - ap->ys * (ncrows / 2) * 2) / 2 + ap->ys;
986 for (i = 0; i < 6; i++) {
987 ap->shape.hexagon[i].x = (ap->xs - 1) * hexagonUnit[i].x;
988 ap->shape.hexagon[i].y = ((ap->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
990 /* Avoid array bounds read of hexagonUnit */
991 ap->shape.hexagon[6].x = 0;
992 ap->shape.hexagon[6].y = 0;
993 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
994 if (size < -MINSIZE) {
995 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
996 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
997 if (ap->ys < MINRANDOMSIZE)
998 ap->ys = MIN(MINRANDOMSIZE,
999 MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
1000 } else if (size < MINSIZE) {
1002 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
1006 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1009 ap->ncols = MAX(ap->width / ap->xs, 2);
1010 ap->nrows = MAX(ap->height / ap->ys, 2);
1011 ap->xb = (ap->width - ap->xs * ap->ncols) / 2;
1012 ap->yb = (ap->height - ap->ys * ap->nrows) / 2;
1020 if (size < -MINSIZE) {
1021 ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1022 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
1023 if (ap->ys < MINRANDOMSIZE)
1024 ap->ys = MIN(MINRANDOMSIZE,
1025 MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
1026 } else if (size < MINSIZE) {
1028 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
1032 ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1034 ap->xs = (int) (1.52 * ap->ys);
1035 ap->ncols = (MAX(ap->width / ap->xs - 1, 2) / 2) * 2;
1036 ap->nrows = (MAX(ap->height / ap->ys - 1, 2) / 2) * 2;
1037 ap->xb = (ap->width - ap->xs * ap->ncols) / 2 + ap->xs / 2;
1038 ap->yb = (ap->height - ap->ys * ap->nrows) / 2 + ap->ys;
1039 for (orient = 0; orient < 2; orient++) {
1040 for (i = 0; i < 3; i++) {
1041 ap->shape.triangle[orient][i].x =
1042 (ap->xs - 2) * triangleUnit[orient][i].x;
1043 ap->shape.triangle[orient][i].y =
1044 (ap->ys - 2) * triangleUnit[orient][i].y;
1046 /* Avoid array bounds read of triangleUnit */
1047 ap->shape.triangle[orient][3].x = 0;
1048 ap->shape.triangle[orient][3].y = 0;
1052 XSetLineAttributes(display, MI_GC(mi), 1, LineSolid, CapNotLast, JoinMiter);
1054 ap->painted = False;
1056 if (MI_IS_FULLRANDOM(mi)) {
1057 ap->truchet = (Bool) (LRAND() & 1);
1058 ap->sharpturn = (Bool) (LRAND() & 1);
1060 ap->truchet = truchet;
1061 ap->sharpturn = sharpturn;
1063 /* Exclude odd # of neighbors, stepping forward not defined */
1064 if (!NRAND(NUMSTIPPLES) && ((ap->neighbors + 1) % 2)) {
1065 getTable(mi, (int) (NRAND(NTABLES)));
1067 getTurk(mi, (int) (NRAND(NUMSTIPPLES - 1)));
1068 if (MI_NPIXELS(mi) > 2)
1069 for (i = 0; i < (int) ap->ncolors - 1; i++)
1070 ap->colors[i] = (unsigned char) (NRAND(MI_NPIXELS(mi)) +
1071 i * MI_NPIXELS(mi)) / ((int) (ap->ncolors - 1));
1072 if (ap->ants == NULL)
1073 ap->ants = (antstruct *) malloc(ap->n * sizeof (antstruct));
1074 if (ap->tape != NULL)
1075 (void) free((void *) ap->tape);
1076 ap->tape = (unsigned char *)
1077 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
1079 if (ap->truchet_state != NULL)
1080 (void) free((void *) ap->truchet_state);
1081 ap->truchet_state = (unsigned char *)
1082 calloc(ap->ncols * ap->nrows, sizeof (unsigned char));
1084 col = ap->ncols / 2;
1085 row = ap->nrows / 2;
1086 dir = NRAND(ap->neighbors) * ANGLES / ap->neighbors;
1087 /* Have them all start in the same spot, why not? */
1088 for (i = 0; i < ap->n; i++) {
1089 ap->ants[i].col = col;
1090 ap->ants[i].row = row;
1091 ap->ants[i].direction = dir;
1092 ap->ants[i].state = 0;
1094 draw_anant(mi, col, row);
1098 draw_ant(ModeInfo * mi)
1100 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
1102 statestruct *status;
1103 int i, state_pos, tape_pos;
1104 unsigned char color;
1105 short chg_dir, old_dir;
1107 MI_IS_DRAWN(mi) = True;
1110 for (i = 0; i < ap->n; i++) {
1111 anant = &ap->ants[i];
1112 tape_pos = anant->col + anant->row * ap->ncols;
1113 color = ap->tape[tape_pos]; /* read tape */
1114 state_pos = color + anant->state * ap->ncolors;
1115 status = &(ap->machine[state_pos]);
1116 drawcell(mi, anant->col, anant->row, status->color);
1117 ap->tape[tape_pos] = status->color; /* write on tape */
1119 /* Find direction of Bees or Ants. */
1120 /* Translate relative direction to actual direction */
1121 old_dir = anant->direction;
1122 chg_dir = (2 * ANGLES - status->direction) % ANGLES;
1123 anant->direction = (chg_dir + old_dir) % ANGLES;
1127 if (ap->neighbors == 6) {
1128 if (ap->sharpturn) {
1129 a = (chg_dir / 120 == 2);
1130 drawtruchet(mi, anant->col, anant->row, status->color, a);
1132 a = (old_dir / 60) % 3;
1133 b = (anant->direction / 60) % 3;
1134 a = (a + b + 1) % 3;
1135 drawtruchet(mi, anant->col, anant->row, status->color, a);
1137 } else if (ap->neighbors == 4) {
1139 b = anant->direction / 180;
1140 a = ((a && !b) || (b && !a));
1141 drawtruchet(mi, anant->col, anant->row, status->color, a);
1142 } else if (ap->neighbors == 3) {
1144 a = (2 + anant->direction / 120) % 3;
1146 a = (1 + anant->direction / 120) % 3;
1147 drawtruchet(mi, anant->col, anant->row, status->color, a);
1149 ap->truchet_state[tape_pos] = a + 1;
1151 anant->state = status->next;
1153 /* If edge than wrap it */
1154 old_dir = ((status->direction < ANGLES) ? anant->direction : old_dir);
1155 position_of_neighbor(ap, old_dir, &(anant->col), &(anant->row));
1156 draw_anant(mi, anant->col, anant->row);
1158 if (++ap->generation > MI_CYCLES(mi)) {
1160 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
1164 if (ap->redrawing) {
1165 for (i = 0; i < REDRAWSTEP; i++) {
1166 if (ap->tape[ap->redrawpos] ||
1167 (ap->truchet && ap->truchet_state[ap->redrawpos])) {
1168 drawcell(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1169 ap->tape[ap->redrawpos]);
1171 drawtruchet(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1172 ap->tape[ap->redrawpos],
1173 ap->truchet_state[ap->redrawpos] - 1);
1175 if (++(ap->redrawpos) >= ap->ncols * ap->nrows) {
1184 release_ant(ModeInfo * mi)
1186 if (antfarms != NULL) {
1189 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1190 antfarmstruct *ap = &antfarms[screen];
1193 if (ap->stippledGC != None) {
1194 XFreeGC(MI_DISPLAY(mi), ap->stippledGC);
1196 for (shade = 0; shade < ap->init_bits; shade++)
1197 XFreePixmap(MI_DISPLAY(mi), ap->pixmaps[shade]);
1198 if (ap->tape != NULL)
1199 (void) free((void *) ap->tape);
1200 if (ap->ants != NULL)
1201 (void) free((void *) ap->ants);
1202 if (ap->truchet_state != NULL)
1203 (void) free((void *) ap->truchet_state);
1205 (void) free((void *) antfarms);
1211 refresh_ant(ModeInfo * mi)
1213 antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];