From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / ant.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /*-
3  * ant --- Chris Langton's generalized turing machine ants (also known
4  *         as Greg Turk's turmites) whose tape is the screen
5  */
6
7 #if 0
8 static const char sccsid[] = "@(#)ant.c 5.00 2000/11/01 xlockmore";
9 #endif
10
11 /*-
12  * Copyright (c) 1995 by David Bagley.
13  *
14  * Permission to use, copy, modify, and distribute this software and its
15  * documentation for any purpose and without fee is hereby granted,
16  * provided that the above copyright notice appear in all copies and that
17  * both that copyright notice and this permission notice appear in
18  * supporting documentation.
19  *
20  * This file is provided AS IS with no warranties of any kind.  The author
21  * shall have no liability with respect to the infringement of copyrights,
22  * trade secrets or any patents by this file or any part thereof.  In no
23  * event will the author be liable for any lost revenue or profits or
24  * other special, indirect and consequential damages.
25  *
26  * Revision History:
27  * 01-Nov-2000: Allocation checks
28  * 10-May-1997: Compatible with xscreensaver
29  * 16-Apr-1997: -neighbors 3 and 8 added
30  * 01-Jan-1997: 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
33  *              new Ladder ant.
34  * 04-Apr-1996: -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-1995: Memory leak in ant fixed.  Now random colors.
38  * 05-Sep-1995: 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.
43  */
44
45 /*-
46   Species Grid     Number of Neighbors
47   ------- ----     ------------------
48   Ants    Square   4 (or 8)
49   Bees    Hexagon  6
50   Bees    Triangle 3 (or 9, 12)
51
52   Neighbors 6 and neighbors 3 produce the same Turk ants.
53 */
54
55 #ifndef HAVE_JWXYZ
56 /*# define DO_STIPPLE*/
57 #endif
58
59 #ifdef STANDALONE
60 # define MODE_ant
61 # define DEFAULTS       "*delay:   20000 \n" \
62                                         "*count:   -3    \n" \
63                                         "*cycles:  40000 \n" \
64                                         "*size:    -12   \n" \
65                                         "*ncolors: 64    \n" \
66                                         "*fpsSolid: true    \n" \
67
68 # define reshape_ant 0
69 # define ant_handle_event 0
70 # include "xlockmore.h"         /* in xscreensaver distribution */
71 # include "erase.h"
72 #else /* STANDALONE */
73 # include "xlock.h"             /* in xlockmore distribution */
74 #endif /* STANDALONE */
75 #include "automata.h"
76
77 #ifdef MODE_ant
78
79 /*-
80  * neighbors of 0 randomizes it for 3, 4, 6, 8, 12 (last 2 are less likely)
81  */
82
83 #define DEF_NEIGHBORS  "0"      /* choose random value */
84 #define DEF_TRUCHET  "False"
85 #define DEF_EYES  "False"
86 #define DEF_SHARPTURN  "False"
87
88 static int  neighbors;
89 static Bool truchet;
90 static Bool eyes;
91 static Bool sharpturn;
92
93 static XrmOptionDescRec opts[] =
94 {
95         {"-neighbors", ".ant.neighbors", XrmoptionSepArg, 0},
96         {"-truchet", ".ant.truchet", XrmoptionNoArg, "on"},
97         {"+truchet", ".ant.truchet", XrmoptionNoArg, "off"},
98         {"-eyes", ".ant.eyes", XrmoptionNoArg, "on"},
99         {"+eyes", ".ant.eyes", XrmoptionNoArg, "off"},
100         {"-sharpturn", ".ant.sharpturn", XrmoptionNoArg, "on"},
101         {"+sharpturn", ".ant.sharpturn", XrmoptionNoArg, "off"},
102 };
103 static argtype vars[] =
104 {
105         {&neighbors, "neighbors", "Neighbors", DEF_NEIGHBORS, t_Int},
106         {&truchet,   "truchet",   "Truchet",   DEF_TRUCHET,   t_Bool},
107         {&eyes,      "eyes",      "Eyes",      DEF_EYES,      t_Bool},
108     {&sharpturn, "sharpturn", "SharpTurn", DEF_SHARPTURN, t_Bool},
109 };
110 static OptionStruct desc[] =
111 {
112         {"-neighbors num", "squares 4 or 8, hexagons 6, triangles 3 or 12"},
113         {"-/+truchet", "turn on/off Truchet lines"},
114         {"-/+eyes", "turn on/off eyes"},
115         {"-/+sharpturn", "turn on/off sharp turns (6, 8 or 12 neighbors only)"}
116 };
117
118 ENTRYPOINT ModeSpecOpt ant_opts =
119 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
120
121 #ifdef USE_MODULES
122 const ModStruct ant_description =
123 {"ant",
124  "init_ant", "draw_ant", "release_ant",
125  "refresh_ant", "init_ant", (char *) NULL, &ant_opts,
126  1000, -3, 40000, -12, 64, 1.0, "",
127  "Shows Langton's and Turk's generalized ants", 0, NULL};
128
129 #endif
130
131 #define ANTBITS(n,w,h)\
132   if ((ap->pixmaps[ap->init_bits]=\
133   XCreatePixmapFromBitmapData(display,window,(char *)n,w,h,1,0,1))==None){\
134   free_ant(display,ap); return;} else {ap->init_bits++;}
135
136 /* If you change the table you may have to change the following 2 constants */
137 #define STATES 2
138 #define MINANTS 1
139 #define REDRAWSTEP 2000         /* How much tape to draw per cycle */
140 #define MINGRIDSIZE 24
141 #define MINSIZE 1
142 #define MINRANDOMSIZE 5
143 #define ANGLES 360
144
145 typedef struct {
146         unsigned char color;
147         short       direction;
148         unsigned char next;
149 } statestruct;
150
151 typedef struct {
152         int         col, row;
153         short       direction;
154         unsigned char state;
155 } antstruct;
156
157 typedef struct {
158         Bool        painted;
159         int         neighbors;
160         int         generation;
161         int         xs, ys;
162         int         xb, yb;
163         int         init_dir;
164         int         nrows, ncols;
165         int         width, height;
166         unsigned char ncolors, nstates;
167         int         n;
168         int         redrawing, redrawpos;
169         int         truchet;    /* Only for Turk modes */
170         int         eyes;
171         int         sharpturn;
172         statestruct machine[NUMSTIPPLES * STATES];
173         unsigned char *tape;
174         unsigned char *truchet_state;
175         antstruct  *ants;
176         int         init_bits;
177         unsigned char colors[NUMSTIPPLES - 1];
178 # ifdef DO_STIPPLE
179         GC          stippledGC;
180 # endif /* DO_STIPPLE */
181         Pixmap      pixmaps[NUMSTIPPLES - 1];
182         union {
183                 XPoint      hexagon[7];         /* Need more than 6 for truchet */
184                 XPoint      triangle[2][4];     /* Need more than 3 for truchet */
185         } shape;
186 #ifdef STANDALONE
187   eraser_state *eraser;
188 #endif
189 } antfarmstruct;
190
191 static char plots[] =
192 {3, 4, 6, 8,
193 #ifdef NUMBER_9
194  9,
195 #endif
196  12};
197
198 #define NEIGHBORKINDS ((long) (sizeof plots / sizeof *plots))
199 #define GOODNEIGHBORKINDS 3
200
201 /* Relative ant moves */
202 #define FS 0                    /* Step */
203 #define TRS 1                   /* Turn right, then step */
204 #define THRS 2                  /* Turn hard right, then step */
205 #define TBS 3                   /* Turn back, then step */
206 #define THLS 4                  /* Turn hard left, then step */
207 #define TLS 5                   /* Turn left, then step */
208 #define SF 6                    /* Step */
209 #define STR 7                   /* Step then turn right */
210 #define STHR 8                  /* Step then turn hard right */
211 #define STB 9                   /* Step then turn back */
212 #define STHL 10                 /* Step then turn hard left */
213 #define STL 11                  /* Step then turn left */
214
215 static antfarmstruct *antfarms = (antfarmstruct *) NULL;
216
217 /* LANGTON'S ANT (10) Chaotic after 500, Builder after 10,000 (104p) */
218 /* TURK'S 100 ANT Always chaotic?, tested past 150,000,000 */
219 /* TURK'S 101 ANT Always chaotic? */
220 /* TURK'S 110 ANT Builder at 150 (18p) */
221 /* TURK'S 1000 ANT Always chaotic? */
222 /* TURK'S 1100 SYMMETRIC ANT  all even run 1's and 0's are symmetric */
223 /* other examples 1001, 110011, 110000, 1001101 */
224 /* TURK'S 1101 ANT Builder after 250,000 (388p) */
225 /* Once saw a chess horse type builder (i.e. non-45 degree builder) */
226
227 /* BEE ONLY */
228 /* All alternating 10 appear symmetric, no proof (i.e. 10, 1010, etc) */
229 /* Even runs of 0's and 1's are also symmetric */
230 /* I have seen Hexagonal builders but they are more rare. */
231
232 static unsigned char tables[][3 * NUMSTIPPLES * STATES + 2] =
233 {
234 #if 0
235   /* Here just so you can figure out notation */
236         {                       /* Langton's ant */
237                 2, 1,
238                 1, TLS, 0, 0, TRS, 0
239         },
240 #else
241   /* First 2 numbers are the size (ncolors, nstates) */
242         {                       /* LADDER BUILDER */
243                 4, 1,
244                 1, STR, 0, 2, STL, 0, 3, TRS, 0, 0, TLS, 0
245         },
246         {                       /* SPIRALING PATTERN */
247                 2, 2,
248                 1, TLS, 0, 0, FS, 1,
249                 1, TRS, 0, 1, TRS, 0
250         },
251         {                       /* SQUARE (HEXAGON) BUILDER */
252                 2, 2,
253                 1, TLS, 0, 0, FS, 1,
254                 0, TRS, 0, 1, TRS, 0
255         },
256 #endif
257 };
258
259 #define NTABLES   (sizeof tables / sizeof tables[0])
260
261 static void
262 position_of_neighbor(antfarmstruct * ap, int dir, int *pcol, int *prow)
263 {
264         int         col = *pcol, row = *prow;
265
266         if (ap->neighbors == 6) {
267                 switch (dir) {
268                         case 0:
269                                 col = (col + 1 == ap->ncols) ? 0 : col + 1;
270                                 break;
271                         case 60:
272                                 if (!(row & 1))
273                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
274                                 row = (!row) ? ap->nrows - 1 : row - 1;
275                                 break;
276                         case 120:
277                                 if (row & 1)
278                                         col = (!col) ? ap->ncols - 1 : col - 1;
279                                 row = (!row) ? ap->nrows - 1 : row - 1;
280                                 break;
281                         case 180:
282                                 col = (!col) ? ap->ncols - 1 : col - 1;
283                                 break;
284                         case 240:
285                                 if (row & 1)
286                                         col = (!col) ? ap->ncols - 1 : col - 1;
287                                 row = (row + 1 == ap->nrows) ? 0 : row + 1;
288                                 break;
289                         case 300:
290                                 if (!(row & 1))
291                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
292                                 row = (row + 1 == ap->nrows) ? 0 : row + 1;
293                                 break;
294                         default:
295                                 (void) fprintf(stderr, "wrong direction %d\n", dir);
296                 }
297         } else if (ap->neighbors == 4 || ap->neighbors == 8) {
298                 switch (dir) {
299                         case 0:
300                                 col = (col + 1 == ap->ncols) ? 0 : col + 1;
301                                 break;
302                         case 45:
303                                 col = (col + 1 == ap->ncols) ? 0 : col + 1;
304                                 row = (!row) ? ap->nrows - 1 : row - 1;
305                                 break;
306                         case 90:
307                                 row = (!row) ? ap->nrows - 1 : row - 1;
308                                 break;
309                         case 135:
310                                 col = (!col) ? ap->ncols - 1 : col - 1;
311                                 row = (!row) ? ap->nrows - 1 : row - 1;
312                                 break;
313                         case 180:
314                                 col = (!col) ? ap->ncols - 1 : col - 1;
315                                 break;
316                         case 225:
317                                 col = (!col) ? ap->ncols - 1 : col - 1;
318                                 row = (row + 1 == ap->nrows) ? 0 : row + 1;
319                                 break;
320                         case 270:
321                                 row = (row + 1 == ap->nrows) ? 0 : row + 1;
322                                 break;
323                         case 315:
324                                 col = (col + 1 == ap->ncols) ? 0 : col + 1;
325                                 row = (row + 1 == ap->nrows) ? 0 : row + 1;
326                                 break;
327                         default:
328                                 (void) fprintf(stderr, "wrong direction %d\n", dir);
329                 }
330         } else {                /* TRI */
331                 if ((col + row) % 2) {  /* right */
332                         switch (dir) {
333                                 case 0:
334                                         col = (!col) ? ap->ncols - 1 : col - 1;
335                                         break;
336                                 case 30:
337                                 case 40:
338                                         col = (!col) ? ap->ncols - 1 : col - 1;
339                                         row = (!row) ? ap->nrows - 1 : row - 1;
340                                         break;
341                                 case 60:
342                                         col = (!col) ? ap->ncols - 1 : col - 1;
343                                         if (!row)
344                                                 row = ap->nrows - 2;
345                                         else if (!(row - 1))
346                                                 row = ap->nrows - 1;
347                                         else
348                                                 row = row - 2;
349                                         break;
350                                 case 80:
351                                 case 90:
352                                         if (!row)
353                                                 row = ap->nrows - 2;
354                                         else if (!(row - 1))
355                                                 row = ap->nrows - 1;
356                                         else
357                                                 row = row - 2;
358                                         break;
359                                 case 120:
360                                         row = (!row) ? ap->nrows - 1 : row - 1;
361                                         break;
362                                 case 150:
363                                 case 160:
364                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
365                                         row = (!row) ? ap->nrows - 1 : row - 1;
366                                         break;
367                                 case 180:
368                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
369                                         break;
370                                 case 200:
371                                 case 210:
372                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
373                                         row = (row + 1 == ap->nrows) ? 0 : row + 1;
374                                         break;
375                                 case 240:
376                                         row = (row + 1 == ap->nrows) ? 0 : row + 1;
377                                         break;
378                                 case 270:
379                                 case 280:
380                                         if (row + 1 == ap->nrows)
381                                                 row = 1;
382                                         else if (row + 2 == ap->nrows)
383                                                 row = 0;
384                                         else
385                                                 row = row + 2;
386                                         break;
387                                 case 300:
388                                         col = (!col) ? ap->ncols - 1 : col - 1;
389                                         if (row + 1 == ap->nrows)
390                                                 row = 1;
391                                         else if (row + 2 == ap->nrows)
392                                                 row = 0;
393                                         else
394                                                 row = row + 2;
395                                         break;
396                                 case 320:
397                                 case 330:
398                                         col = (!col) ? ap->ncols - 1 : col - 1;
399                                         row = (row + 1 == ap->nrows) ? 0 : row + 1;
400                                         break;
401                                 default:
402                                         (void) fprintf(stderr, "wrong direction %d\n", dir);
403                         }
404                 } else {        /* left */
405                         switch (dir) {
406                                 case 0:
407                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
408                                         break;
409                                 case 30:
410                                 case 40:
411                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
412                                         row = (row + 1 == ap->nrows) ? 0 : row + 1;
413                                         break;
414                                 case 60:
415                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
416                                         if (row + 1 == ap->nrows)
417                                                 row = 1;
418                                         else if (row + 2 == ap->nrows)
419                                                 row = 0;
420                                         else
421                                                 row = row + 2;
422                                         break;
423                                 case 80:
424                                 case 90:
425                                         if (row + 1 == ap->nrows)
426                                                 row = 1;
427                                         else if (row + 2 == ap->nrows)
428                                                 row = 0;
429                                         else
430                                                 row = row + 2;
431                                         break;
432                                 case 120:
433                                         row = (row + 1 == ap->nrows) ? 0 : row + 1;
434                                         break;
435                                 case 150:
436                                 case 160:
437                                         col = (!col) ? ap->ncols - 1 : col - 1;
438                                         row = (row + 1 == ap->nrows) ? 0 : row + 1;
439                                         break;
440                                 case 180:
441                                         col = (!col) ? ap->ncols - 1 : col - 1;
442                                         break;
443                                 case 200:
444                                 case 210:
445                                         col = (!col) ? ap->ncols - 1 : col - 1;
446                                         row = (!row) ? ap->nrows - 1 : row - 1;
447                                         break;
448                                 case 240:
449                                         row = (!row) ? ap->nrows - 1 : row - 1;
450                                         break;
451                                 case 270:
452                                 case 280:
453                                         if (!row)
454                                                 row = ap->nrows - 2;
455                                         else if (row == 1)
456                                                 row = ap->nrows - 1;
457                                         else
458                                                 row = row - 2;
459                                         break;
460                                 case 300:
461                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
462                                         if (!row)
463                                                 row = ap->nrows - 2;
464                                         else if (row == 1)
465                                                 row = ap->nrows - 1;
466                                         else
467                                                 row = row - 2;
468                                         break;
469                                 case 320:
470                                 case 330:
471                                         col = (col + 1 == ap->ncols) ? 0 : col + 1;
472                                         row = (!row) ? ap->nrows - 1 : row - 1;
473                                         break;
474                                 default:
475                                         (void) fprintf(stderr, "wrong direction %d\n", dir);
476                         }
477                 }
478         }
479         *pcol = col;
480         *prow = row;
481 }
482
483 static void
484 fillcell(ModeInfo * mi, GC gc, int col, int row)
485 {
486         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
487
488         if (ap->neighbors == 6) {
489                 int         ccol = 2 * col + !(row & 1), crow = 2 * row;
490
491                 ap->shape.hexagon[0].x = ap->xb + ccol * ap->xs;
492                 ap->shape.hexagon[0].y = ap->yb + crow * ap->ys;
493                 if (ap->xs == 1 && ap->ys == 1)
494                         XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
495                         ap->shape.hexagon[0].x, ap->shape.hexagon[0].y);
496                 else
497                         XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
498                             ap->shape.hexagon, 6, Convex, CoordModePrevious);
499         } else if (ap->neighbors == 4 || ap->neighbors == 8) {
500                 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
501                 ap->xb + ap->xs * col, ap->yb + ap->ys * row,
502                 ap->xs - (ap->xs > 3), ap->ys - (ap->ys > 3));
503         } else {                /* TRI */
504                 int         orient = (col + row) % 2;   /* O left 1 right */
505
506                 ap->shape.triangle[orient][0].x = ap->xb + col * ap->xs;
507                 ap->shape.triangle[orient][0].y = ap->yb + row * ap->ys;
508                 if (ap->xs <= 3 || ap->ys <= 3)
509                         XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
510                         ((orient) ? -1 : 1) + ap->shape.triangle[orient][0].x,
511                                        ap->shape.triangle[orient][0].y);
512                 else {
513                         if (orient)
514                                 ap->shape.triangle[orient][0].x += (ap->xs / 2 - 1);
515                         else
516                                 ap->shape.triangle[orient][0].x -= (ap->xs / 2 - 1);
517                         XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
518                                      ap->shape.triangle[orient], 3, Convex, CoordModePrevious);
519                 }
520         }
521 }
522
523 static void
524 truchetcell(ModeInfo * mi, int col, int row, int truchetstate)
525 {
526         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
527
528         if (ap->neighbors == 6) {
529
530                 int         ccol = 2 * col + !(row & 1), crow = 2 * row;
531                 int         side;
532                 int         fudge = 7;  /* fudge because the hexagons are not exact */
533                 XPoint      hex, hex2;
534
535                 if (ap->sharpturn) {
536                         hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs / 2.0) - 1;
537                         hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys / 2.0) - 1;
538                         for (side = 0; side < 6; side++) {
539                                 if (side) {
540                                         hex.x += ap->shape.hexagon[side].x;
541                                         hex.y += ap->shape.hexagon[side].y;
542                                 }
543                                 if (truchetstate == side % 2)
544                                         XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
545                                                  hex.x, hex.y, ap->xs, ap->ys,
546                                                  ((570 - (side * 60) + fudge) % 360) * 64, (120 - 2 * fudge) * 64);
547                         }
548                 } else {
549                         /* Very crude approx of Sqrt 3, so it will not cause drawing errors. */
550                         hex.x = ap->xb + ccol * ap->xs - (int) ((double) ap->xs * 1.6 / 2.0) - 1;
551                         hex.y = ap->yb + crow * ap->ys - (int) ((double) ap->ys * 1.6 / 2.0) - 1;
552                         for (side = 0; side < 6; side++) {
553                                 if (side) {
554                                         hex.x += ap->shape.hexagon[side].x;
555                                         hex.y += ap->shape.hexagon[side].y;
556                                 }
557                                 hex2.x = hex.x + ap->shape.hexagon[side + 1].x / 2;
558                                 hex2.y = hex.y + ap->shape.hexagon[side + 1].y / 2 + 1;
559                                 /* Lots of fudging here */
560                                 if (side == 1) {
561                                         hex2.x += (short) (ap->xs * 0.1 + 1);
562                                         hex2.y += (short) (ap->ys * 0.1 - ((ap->ys > 5) ? 1 : 0));
563                                 } else if (side == 2) {
564                                         hex2.x += (short) (ap->xs * 0.1);
565                                 } else if (side == 4) {
566                                         hex2.x += (short) (ap->xs * 0.1);
567                                         hex2.y += (short) (ap->ys * 0.1 - 1);
568                                 } else if (side == 5) {
569                                         hex2.x += (short) (ap->xs * 0.5);
570                                         hex2.y += (short) (-ap->ys * 0.3 + 1);
571                                 }
572                                 if (truchetstate == side % 3)
573                                         /* Crude approx of 120 deg, so it will not cause drawing errors. */
574                                         XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
575                                                  hex2.x, hex2.y,
576                                                  (int) ((double) ap->xs * 1.5), (int) ((double) ap->ys * 1.5),
577                                                  ((555 - (side * 60)) % 360) * 64, 90 * 64);
578                         }
579                 }
580         } else if (ap->neighbors == 4) {
581                 if (truchetstate) {
582                         XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
583                                  ap->xb + ap->xs * col - ap->xs / 2 + 1,
584                                  ap->yb + ap->ys * row + ap->ys / 2 - 1,
585                                  ap->xs - 2, ap->ys - 2,
586                                  0 * 64, 90 * 64);
587                         XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
588                                  ap->xb + ap->xs * col + ap->xs / 2 - 1,
589                                  ap->yb + ap->ys * row - ap->ys / 2 + 1,
590                                  ap->xs - 2, ap->ys - 2,
591                                  -90 * 64, -90 * 64);
592                 } else {
593                         XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
594                                  ap->xb + ap->xs * col - ap->xs / 2 + 1,
595                                  ap->yb + ap->ys * row - ap->ys / 2 + 1,
596                                  ap->xs - 2, ap->ys - 2,
597                                  0 * 64, -90 * 64);
598                         XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
599                                  ap->xb + ap->xs * col + ap->xs / 2 - 1,
600                                  ap->yb + ap->ys * row + ap->ys / 2 - 1,
601                                  ap->xs - 2, ap->ys - 2,
602                                  90 * 64, 90 * 64);
603                 }
604         } else if (ap->neighbors == 3) {
605                 int         orient = (col + row) % 2;   /* O left 1 right */
606                 int         side, ang;
607                 int         fudge = 7;  /* fudge because the triangles are not exact */
608                 double      fudge2 = 1.18;
609                 XPoint      tri;
610
611                 tri.x = ap->xb + col * ap->xs;
612                 tri.y = ap->yb + row * ap->ys;
613                 if (orient) {
614                         tri.x += (ap->xs / 2 - 1);
615                 } else {
616                         tri.x -= (ap->xs / 2 - 1);
617                 }
618                 for (side = 0; side < 3; side++) {
619                         if (side > 0) {
620                                 tri.x += ap->shape.triangle[orient][side].x;
621                                 tri.y += ap->shape.triangle[orient][side].y;
622                         }
623                         if (truchetstate == side) {
624                                 if (orient)
625                                         ang = (510 - side * 120) % 360;         /* Right */
626                                 else
627                                         ang = (690 - side * 120) % 360;         /* Left */
628                                 XDrawArc(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
629                                   (int) (tri.x - ap->xs * fudge2 / 2),
630           (int) (tri.y - 3 * ap->ys * fudge2 / 4),
631                                   (unsigned int) (ap->xs * fudge2),
632           (unsigned int) (3 * ap->ys * fudge2 / 2),
633                                   (ang + fudge) * 64, (60 - 2 * fudge) * 64);
634                         }
635                 }
636         }
637 }
638
639 static void
640 drawcell(ModeInfo * mi, int col, int row, unsigned char color)
641 {
642         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
643         GC          gc;
644
645         if (!color) {
646                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
647                 gc = MI_GC(mi);
648 # ifdef DO_STIPPLE
649         } else if (MI_NPIXELS(mi) <= 2) {
650                 XGCValues   gcv;
651                 gcv.foreground = MI_WHITE_PIXEL(mi);
652                 gcv.background = MI_BLACK_PIXEL(mi);
653         gcv.stipple = ap->pixmaps[color - 1];
654                 XChangeGC(MI_DISPLAY(mi), ap->stippledGC,
655                           GCStipple | GCForeground | GCBackground, &gcv);
656                 gc = ap->stippledGC;
657 # endif /* !DO_STIPPLE */
658         } else {
659                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
660                                MI_PIXEL(mi, ap->colors[color - 1]));
661                 gc = MI_GC(mi);
662         }
663         fillcell(mi, gc, col, row);
664 }
665
666 static void
667 drawtruchet(ModeInfo * mi, int col, int row,
668             unsigned char color, unsigned char truchetstate)
669 {
670         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
671
672         if (!color)
673                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
674         else if (MI_NPIXELS(mi) > 2 || color > ap->ncolors / 2)
675                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
676         else
677                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WHITE_PIXEL(mi));
678         truchetcell(mi, col, row, truchetstate);
679 }
680
681 static void
682 draw_anant(ModeInfo * mi, int direction, int col, int row)
683 {
684         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
685         Display    *display = MI_DISPLAY(mi);
686         Window      window = MI_WINDOW(mi);
687
688         XSetForeground(display, MI_GC(mi), MI_WHITE_PIXEL(mi));
689         fillcell(mi, MI_GC(mi), col, row);
690         if (ap->eyes) {         /* Draw Eyes */
691                 XSetForeground(display, MI_GC(mi), MI_BLACK_PIXEL(mi));
692                 if (ap->neighbors == 6) {
693                         int         ccol = 2 * col + !(row & 1), crow = 2 * row;
694                         int         side, ang;
695                         XPoint      hex;
696
697                         if (!(ap->xs > 3 && ap->ys > 3))
698                                 return;
699                         hex.x = ap->xb + ccol * ap->xs;
700                         hex.y = ap->yb + crow * ap->ys + ap->ys / 2;
701                         ang = direction * ap->neighbors / ANGLES;
702                         for (side = 0; side < ap->neighbors; side++) {
703                                 if (side) {
704                                         hex.x -= ap->shape.hexagon[side].x / 2;
705                                         hex.y += ap->shape.hexagon[side].y / 2;
706                                 }
707                                 if (side == (ap->neighbors + ang - 2) % ap->neighbors)
708                                         XDrawPoint(display, window, MI_GC(mi), hex.x, hex.y);
709                                 if (side == (ap->neighbors + ang - 1) % ap->neighbors)
710                                         XDrawPoint(display, window, MI_GC(mi), hex.x, hex.y);
711                         }
712                 } else if (ap->neighbors == 4 || ap->neighbors == 8) {
713                         if (!(ap->xs > 3 && ap->ys > 3))
714                                 return;
715                         switch (direction) {
716                                 case 0:
717                                         XDrawPoint(display, window, MI_GC(mi),
718                                                 ap->xb + ap->xs * (col + 1) - 3,
719                                                 ap->yb + ap->ys * row + ap->ys / 2 - 2);
720                                         XDrawPoint(display, window, MI_GC(mi),
721                                                 ap->xb + ap->xs * (col + 1) - 3,
722                                                 ap->yb + ap->ys * row + ap->ys / 2);
723                                         break;
724                                 case 45:
725                                         XDrawPoint(display, window, MI_GC(mi),
726                                                 ap->xb + ap->xs * (col + 1) - 4,
727                                                 ap->yb + ap->ys * row + 1);
728                                         XDrawPoint(display, window, MI_GC(mi),
729                                                 ap->xb + ap->xs * (col + 1) - 3,
730                                                 ap->yb + ap->ys * row + 2);
731                                         break;
732                                 case 90:
733                                         XDrawPoint(display, window, MI_GC(mi),
734                                                 ap->xb + ap->xs * col + ap->xs / 2 - 2,
735                                                 ap->yb + ap->ys * row + 1);
736                                         XDrawPoint(display, window, MI_GC(mi),
737                                                 ap->xb + ap->xs * col + ap->xs / 2,
738                                                 ap->yb + ap->ys * row + 1);
739                                         break;
740                                 case 135:
741                                         XDrawPoint(display, window, MI_GC(mi),
742                                                 ap->xb + ap->xs * col + 2,
743                                                 ap->yb + ap->ys * row + 1);
744                                         XDrawPoint(display, window, MI_GC(mi),
745                                                 ap->xb + ap->xs * col + 1,
746                                                 ap->yb + ap->ys * row + 2);
747                                         break;
748                                 case 180:
749                                         XDrawPoint(display, window, MI_GC(mi),
750                                                 ap->xb + ap->xs * col + 1,
751                                                 ap->yb + ap->ys * row + ap->ys / 2 - 2);
752                                         XDrawPoint(display, window, MI_GC(mi),
753                                                 ap->xb + ap->xs * col + 1,
754                                                 ap->yb + ap->ys * row + ap->ys / 2);
755                                         break;
756                                 case 225:
757                                         XDrawPoint(display, window, MI_GC(mi),
758                                                 ap->xb + ap->xs * col + 2,
759                                                 ap->yb + ap->ys * (row + 1) - 3);
760                                         XDrawPoint(display, window, MI_GC(mi),
761                                                 ap->xb + ap->xs * col + 1,
762                                                 ap->yb + ap->ys * (row + 1) - 4);
763                                         break;
764                                 case 270:
765                                         XDrawPoint(display, window, MI_GC(mi),
766                                                 ap->xb + ap->xs * col + ap->xs / 2 - 2,
767                                                 ap->yb + ap->ys * (row + 1) - 3);
768                                         XDrawPoint(display, window, MI_GC(mi),
769                                                 ap->xb + ap->xs * col + ap->xs / 2,
770                                                 ap->yb + ap->ys * (row + 1) - 3);
771                                         break;
772                                 case 315:
773                                         XDrawPoint(display, window, MI_GC(mi),
774                                                 ap->xb + ap->xs * (col + 1) - 4,
775                                                 ap->yb + ap->ys * (row + 1) - 3);
776                                         XDrawPoint(display, window, MI_GC(mi),
777                                                 ap->xb + ap->xs * (col + 1) - 3,
778                                                 ap->yb + ap->ys * (row + 1) - 4);
779                                         break;
780                                 default:
781                                         (void) fprintf(stderr, "wrong eyes direction %d for ant eyes\n", direction);
782                         }
783                 } else {                /* TRI */
784                         int         orient = (col + row) % 2;   /* O left 1 right */
785                         int         side, ang;
786                         XPoint      tri;
787
788                         if (!(ap->xs > 6 && ap->ys > 6))
789                                 return;
790                         tri.x = ap->xb + col * ap->xs;
791                         tri.y = ap->yb + row * ap->ys;
792                         if (orient)
793                                 tri.x += (ap->xs / 6 - 1);
794                         else
795                                 tri.x -= (ap->xs / 6 - 1);
796                         ang = direction * ap->neighbors / ANGLES;
797                         /* approx... does not work that well for even numbers */
798                         if (
799 #ifdef NUMBER_9
800                         ap->neighbors == 9 ||
801 #endif
802                         ap->neighbors == 12) {
803 #ifdef UNDER_CONSTRUCTION
804                                 /* Not sure why this does not work */
805                                 ang = ((ang + ap->neighbors / 6) / (ap->neighbors / 3)) % 3;
806 #else
807                                 return;
808 #endif
809                         }
810                         for (side = 0; side < 3; side++) {
811                                 if (side) {
812                                         tri.x += ap->shape.triangle[orient][side].x / 3;
813                                         tri.y += ap->shape.triangle[orient][side].y / 3;
814                                 }
815                                 /* Either you have the eyes in back or one eye in front */
816 #if 0
817                                 if (side == ang)
818                                         XDrawPoint(display, window, MI_GC(mi), tri.x, tri.y);
819 #else
820                                 if (side == (ang + 2) % 3)
821                                         XDrawPoint(display, window, MI_GC(mi), tri.x, tri.y);
822                                 if (side == (ang + 1) % 3)
823                                         XDrawPoint(display, window, MI_GC(mi), tri.x, tri.y);
824 #endif
825                         }
826                 }
827         }
828 }
829
830 #if 0
831 static void
832 RandomSoup(mi)
833         ModeInfo   *mi;
834 {
835         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
836         int         row, col, mrow = 0;
837
838         for (row = 0; row < ap->nrows; ++row) {
839                 for (col = 0; col < ap->ncols; ++col) {
840                         ap->old[col + mrow] = (unsigned char) NRAND((int) ap->ncolors);
841                         drawcell(mi, col, row, ap->old[col + mrow]);
842                 }
843                 mrow += ap->nrows;
844         }
845 }
846
847 #endif
848
849 static short
850 fromTableDirection(unsigned char dir, int local_neighbors)
851 {
852         /* Crafted to work for odd number of neighbors */
853         switch (dir) {
854                 case FS:
855                         return 0;
856                 case TLS:
857                         return (ANGLES / local_neighbors);
858                 case THLS:
859                         return (2 * ANGLES / local_neighbors);
860                 case TBS:
861                         return ((local_neighbors / 2) * ANGLES / local_neighbors);
862                 case THRS:
863                         return (ANGLES - 2 * ANGLES / local_neighbors);
864                 case TRS:
865                         return (ANGLES - ANGLES / local_neighbors);
866                 case SF:
867                         return ANGLES;
868                 case STL:
869                         return (ANGLES + ANGLES / local_neighbors);
870                 case STHL:
871                         return (ANGLES + 2 * ANGLES / local_neighbors);
872                 case STB:
873                         return (ANGLES + (local_neighbors / 2) * ANGLES / local_neighbors);
874                 case STHR:
875                         return (2 * ANGLES - 2 * ANGLES / local_neighbors);
876                 case STR:
877                         return (2 * ANGLES - ANGLES / local_neighbors);
878                 default:
879                         (void) fprintf(stderr, "wrong direction %d from table\n", dir);
880         }
881         return -1;
882 }
883
884 static void
885 getTable(ModeInfo * mi, int i)
886 {
887         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
888         int         j, total;
889         unsigned char *patptr;
890
891         patptr = &tables[i][0];
892         ap->ncolors = *patptr++;
893         ap->nstates = *patptr++;
894         total = ap->ncolors * ap->nstates;
895         if (MI_IS_VERBOSE(mi))
896                 (void) fprintf(stdout,
897                                "ants %d, neighbors %d, table number %d, colors %d, states %d\n",
898                           ap->n, ap->neighbors, i, ap->ncolors, ap->nstates);
899         for (j = 0; j < total; j++) {
900                 ap->machine[j].color = *patptr++;
901                 if (ap->sharpturn && ap->neighbors > 4) {
902                         int         k = *patptr++;
903
904                         switch (k) {
905                                 case TRS:
906                                         k = THRS;
907                                         break;
908                                 case THRS:
909                                         k = TRS;
910                                         break;
911                                 case THLS:
912                                         k = TLS;
913                                         break;
914                                 case TLS:
915                                         k = THLS;
916                                         break;
917                                 case STR:
918                                         k = STHR;
919                                         break;
920                                 case STHR:
921                                         k = STR;
922                                         break;
923                                 case STHL:
924                                         k = STL;
925                                         break;
926                                 case STL:
927                                         k = STHL;
928                                         break;
929                                 default:
930                                         break;
931                         }
932                         ap->machine[j].direction = fromTableDirection(k, ap->neighbors);
933                 } else {
934                         ap->machine[j].direction = fromTableDirection(*patptr++, ap->neighbors);
935                 }
936                 ap->machine[j].next = *patptr++;
937         }
938         ap->truchet = False;
939 }
940
941 static void
942 getTurk(ModeInfo * mi, int i)
943 {
944         antfarmstruct *ap = &antfarms[MI_SCREEN(mi)];
945         int         power2, j, number, total;
946
947         /* To force a number, say <i = 2;> has  i + 2 (or 4) binary digits */
948         power2 = 1 << (i + 1);
949         /* Do not want numbers which in binary are all 1's. */
950         number = NRAND(power2 - 1) + power2;
951         /* To force a particular number, say <number = 10;> */
952
953         ap->ncolors = i + 2;
954         ap->nstates = 1;
955         total = ap->ncolors * ap->nstates;
956         for (j = 0; j < total; j++) {
957                 ap->machine[j].color = (j + 1) % total;
958                 if (ap->sharpturn && ap->neighbors > 4) {
959                         ap->machine[j].direction = (power2 & number) ?
960                                 fromTableDirection(THRS, ap->neighbors) :
961                                 fromTableDirection(THLS, ap->neighbors);
962                 } else {
963                         ap->machine[j].direction = (power2 & number) ?
964                                 fromTableDirection(TRS, ap->neighbors) :
965                                 fromTableDirection(TLS, ap->neighbors);
966                 }
967                 ap->machine[j].next = 0;
968                 power2 >>= 1;
969         }
970         ap->truchet = (ap->truchet && ap->xs > 2 && ap->ys > 2 &&
971            (ap->neighbors == 3 || ap->neighbors == 4 || ap->neighbors == 6));
972         if (MI_IS_VERBOSE(mi))
973                 (void) fprintf(stdout,
974                       "ants %d, neighbors %d, Turk's number %d, colors %d\n",
975                                ap->n, ap->neighbors, number, ap->ncolors);
976 }
977
978 static void
979 free_ant(Display *display, antfarmstruct *ap)
980 {
981         int         shade;
982
983 #ifdef DO_STIPPLE
984         if (ap->stippledGC != None) {
985                 XFreeGC(display, ap->stippledGC);
986                 ap->stippledGC = None;
987         }
988 #endif /* DO_STIPPLE */
989         for (shade = 0; shade < ap->init_bits; shade++) {
990                 XFreePixmap(display, ap->pixmaps[shade]);
991         }
992         ap->init_bits = 0;
993         if (ap->tape != NULL) {
994                 (void) free((void *) ap->tape);
995                 ap->tape = (unsigned char *) NULL;
996         }
997         if (ap->ants != NULL) {
998                 (void) free((void *) ap->ants);
999                 ap->ants = (antstruct *) NULL;
1000         }
1001         if (ap->truchet_state != NULL) {
1002                 (void) free((void *) ap->truchet_state);
1003                 ap->truchet_state = (unsigned char *) NULL;
1004         }
1005 }
1006
1007 ENTRYPOINT void
1008 init_ant(ModeInfo * mi)
1009 {
1010         Display    *display = MI_DISPLAY(mi);
1011         int         size = MI_SIZE(mi);
1012         antfarmstruct *ap;
1013         int         col, row, dir;
1014         int         i;
1015
1016     MI_INIT (mi, antfarms, 0);
1017         ap = &antfarms[MI_SCREEN(mi)];
1018
1019         ap->redrawing = 0;
1020 #ifdef DO_STIPPLE
1021         if (MI_NPIXELS(mi) <= 2) {
1022           Window      window = MI_WINDOW(mi);
1023           if (ap->stippledGC == None) {
1024                         XGCValues   gcv;
1025
1026                         gcv.fill_style = FillOpaqueStippled;
1027                         if ((ap->stippledGC = XCreateGC(display, window,
1028                                                         GCFillStyle,
1029                                         &gcv)) == None) {
1030                                 free_ant(display, ap);
1031                                 return;
1032                         }
1033                 }
1034                 if (ap->init_bits == 0) {
1035                         for (i = 1; i < NUMSTIPPLES; i++) {
1036                                 ANTBITS(stipples[i], STIPPLESIZE, STIPPLESIZE);
1037                         }
1038                 }
1039         }
1040 #endif /* DO_STIPPLE */
1041         ap->generation = 0;
1042         ap->n = MI_COUNT(mi);
1043         if (ap->n < -MINANTS) {
1044                 /* if ap->n is random ... the size can change */
1045                 if (ap->ants != NULL) {
1046                         (void) free((void *) ap->ants);
1047                         ap->ants = (antstruct *) NULL;
1048                 }
1049                 ap->n = NRAND(-ap->n - MINANTS + 1) + MINANTS;
1050         } else if (ap->n < MINANTS)
1051                 ap->n = MINANTS;
1052
1053         ap->width = MI_WIDTH(mi);
1054         ap->height = MI_HEIGHT(mi);
1055
1056         for (i = 0; i < NEIGHBORKINDS; i++) {
1057                 if (neighbors == plots[i]) {
1058                         ap->neighbors = plots[i];
1059                         break;
1060                 }
1061                 if (i == NEIGHBORKINDS - 1) {
1062                         if (!NRAND(10)) {
1063                                 /* Make above 6 rare */
1064                                 ap->neighbors = plots[NRAND(NEIGHBORKINDS)];
1065                         } else {
1066                                 ap->neighbors = plots[NRAND(GOODNEIGHBORKINDS)];
1067                         }
1068                         break;
1069                 }
1070         }
1071
1072         if (ap->neighbors == 6) {
1073                 int         nccols, ncrows;
1074
1075                 if (ap->width < 8)
1076                         ap->width = 8;
1077                 if (ap->height < 8)
1078                         ap->height = 8;
1079                 if (size < -MINSIZE) {
1080                         ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1081                                       MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
1082                         if (ap->ys < MINRANDOMSIZE)
1083                                 ap->ys = MIN(MINRANDOMSIZE,
1084                                              MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
1085                 } else if (size < MINSIZE) {
1086                         if (!size)
1087                                 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
1088                         else
1089                                 ap->ys = MINSIZE;
1090                 } else
1091                         ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1092                                                MINGRIDSIZE));
1093                 ap->xs = ap->ys;
1094                 nccols = MAX(ap->width / ap->xs - 2, 2);
1095                 ncrows = MAX(ap->height / ap->ys - 1, 4);
1096                 ap->ncols = nccols / 2;
1097                 ap->nrows = 2 * (ncrows / 4);
1098                 ap->xb = (ap->width - ap->xs * nccols) / 2 + ap->xs / 2;
1099                 ap->yb = (ap->height - ap->ys * (ncrows / 2) * 2) / 2 + ap->ys - 2;
1100                 for (i = 0; i < 6; i++) {
1101                         ap->shape.hexagon[i].x = (ap->xs - 1) * hexagonUnit[i].x;
1102                         ap->shape.hexagon[i].y = ((ap->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
1103                 }
1104                 /* Avoid array bounds read of hexagonUnit */
1105                 ap->shape.hexagon[6].x = 0;
1106                 ap->shape.hexagon[6].y = 0;
1107         } else if (ap->neighbors == 4 || ap->neighbors == 8) {
1108                 if (size < -MINSIZE) {
1109                         ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1110                                       MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
1111                         if (ap->ys < MINRANDOMSIZE)
1112                                 ap->ys = MIN(MINRANDOMSIZE,
1113                                              MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
1114                 } else if (size < MINSIZE) {
1115                         if (!size)
1116                                 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
1117                         else
1118                                 ap->ys = MINSIZE;
1119                 } else
1120                         ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1121                                                MINGRIDSIZE));
1122                 ap->xs = ap->ys;
1123                 ap->ncols = MAX(ap->width / ap->xs, 2);
1124                 ap->nrows = MAX(ap->height / ap->ys, 2);
1125                 ap->xb = (ap->width - ap->xs * ap->ncols) / 2;
1126                 ap->yb = (ap->height - ap->ys * ap->nrows) / 2;
1127         } else {                /* TRI */
1128                 int         orient;
1129
1130                 if (ap->width < 2)
1131                         ap->width = 2;
1132                 if (ap->height < 2)
1133                         ap->height = 2;
1134                 if (size < -MINSIZE) {
1135                         ap->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1136                                       MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
1137                         if (ap->ys < MINRANDOMSIZE)
1138                                 ap->ys = MIN(MINRANDOMSIZE,
1139                                              MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE));
1140                 } else if (size < MINSIZE) {
1141                         if (!size)
1142                                 ap->ys = MAX(MINSIZE, MIN(ap->width, ap->height) / MINGRIDSIZE);
1143                         else
1144                                 ap->ys = MINSIZE;
1145                 } else
1146                         ap->ys = MIN(size, MAX(MINSIZE, MIN(ap->width, ap->height) /
1147                                                MINGRIDSIZE));
1148                 ap->xs = (int) (1.52 * ap->ys);
1149                 ap->ncols = (MAX(ap->width / ap->xs - 1, 2) / 2) * 2;
1150                 ap->nrows = (MAX(ap->height / ap->ys - 1, 2) / 2) * 2;
1151                 ap->xb = (ap->width - ap->xs * ap->ncols) / 2 + ap->xs / 2;
1152                 ap->yb = (ap->height - ap->ys * ap->nrows) / 2 + ap->ys;
1153                 for (orient = 0; orient < 2; orient++) {
1154                         for (i = 0; i < 3; i++) {
1155                                 ap->shape.triangle[orient][i].x =
1156                                         (ap->xs - 2) * triangleUnit[orient][i].x;
1157                                 ap->shape.triangle[orient][i].y =
1158                                         (ap->ys - 2) * triangleUnit[orient][i].y;
1159                         }
1160                         /* Avoid array bounds read of triangleUnit */
1161                         ap->shape.triangle[orient][3].x = 0;
1162                         ap->shape.triangle[orient][3].y = 0;
1163                 }
1164         }
1165
1166         XSetLineAttributes(display, MI_GC(mi), 1, LineSolid, CapNotLast, JoinMiter);
1167         MI_CLEARWINDOW(mi);
1168         ap->painted = False;
1169
1170         if (MI_IS_FULLRANDOM(mi)) {
1171                 ap->truchet = (Bool) (LRAND() & 1);
1172                 ap->eyes = (Bool) (LRAND() & 1);
1173                 ap->sharpturn = (Bool) (LRAND() & 1);
1174         } else {
1175                 ap->truchet = truchet;
1176                 ap->eyes = eyes;
1177                 ap->sharpturn = sharpturn;
1178         }
1179         if (!NRAND(NUMSTIPPLES)) {
1180                 getTable(mi, (int) (NRAND(NTABLES)));
1181         } else
1182                 getTurk(mi, (int) (NRAND(NUMSTIPPLES - 1)));
1183         if (MI_NPIXELS(mi) > 2)
1184                 for (i = 0; i < (int) ap->ncolors - 1; i++)
1185                         ap->colors[i] = (unsigned char) (NRAND(MI_NPIXELS(mi)) +
1186                              i * MI_NPIXELS(mi)) / ((int) (ap->ncolors - 1));
1187         if (ap->ants == NULL) {
1188                 if ((ap->ants = (antstruct *) malloc(ap->n * sizeof (antstruct))) ==
1189                                 NULL) {
1190                         free_ant(display, ap);
1191                         return;
1192                 }
1193         }
1194         if (ap->tape != NULL) 
1195                 (void) free((void *) ap->tape);
1196         if ((ap->tape = (unsigned char *) calloc(ap->ncols * ap->nrows,
1197                         sizeof (unsigned char))) == NULL) {
1198                 free_ant(display, ap);
1199                 return;
1200         }
1201         if (ap->truchet_state != NULL)
1202                 (void) free((void *) ap->truchet_state);
1203         if ((ap->truchet_state = (unsigned char *) calloc(ap->ncols * ap->nrows,
1204                         sizeof (unsigned char))) == NULL) {
1205                 free_ant(display, ap);
1206                 return;
1207         }
1208
1209         row = ap->nrows / 2;
1210         col = ap->ncols / 2;
1211         if (col > 0 && ((ap->neighbors % 2) || ap->neighbors == 12) && (LRAND() & 1))
1212                 col--;
1213         dir = NRAND(ap->neighbors) * ANGLES / ap->neighbors;
1214         ap->init_dir = dir;
1215 #ifdef NUMBER_9
1216         if (ap->neighbors == 9 && !((col + row) & 1))
1217                 dir = (dir + ANGLES - ANGLES / (ap->neighbors * 2)) % ANGLES;
1218 #endif
1219         /* Have them all start in the same spot, why not? */
1220         for (i = 0; i < ap->n; i++) {
1221                 ap->ants[i].col = col;
1222                 ap->ants[i].row = row;
1223                 ap->ants[i].direction = dir;
1224                 ap->ants[i].state = 0;
1225         }
1226         draw_anant(mi, dir, col, row);
1227 }
1228
1229 ENTRYPOINT void
1230 draw_ant(ModeInfo * mi)
1231 {
1232         antstruct  *anant;
1233         statestruct *status;
1234         int         i, state_pos, tape_pos;
1235         unsigned char color;
1236         short       chg_dir, old_dir;
1237         antfarmstruct *ap;
1238
1239         if (antfarms == NULL)
1240                 return;
1241         ap = &antfarms[MI_SCREEN(mi)];
1242         if (ap->ants == NULL)
1243                 return;
1244
1245 #ifdef STANDALONE
1246     if (ap->eraser) {
1247       ap->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), ap->eraser);
1248       return;
1249     }
1250 #endif
1251
1252         MI_IS_DRAWN(mi) = True;
1253         ap->painted = True;
1254         for (i = 0; i < ap->n; i++) {
1255                 anant = &ap->ants[i];
1256                 tape_pos = anant->col + anant->row * ap->ncols;
1257                 color = ap->tape[tape_pos];     /* read tape */
1258                 state_pos = color + anant->state * ap->ncolors;
1259                 status = &(ap->machine[state_pos]);
1260                 drawcell(mi, anant->col, anant->row, status->color);
1261                 ap->tape[tape_pos] = status->color;     /* write on tape */
1262
1263                 /* Find direction of Bees or Ants. */
1264                 /* Translate relative direction to actual direction */
1265                 old_dir = anant->direction;
1266                 chg_dir = (2 * ANGLES - status->direction) % ANGLES;
1267                 anant->direction = (chg_dir + old_dir) % ANGLES;
1268                 if (ap->truchet) {
1269                         int         a = 0, b;
1270
1271                         if (ap->neighbors == 6) {
1272                                 if (ap->sharpturn) {
1273                                         a = (((ANGLES + anant->direction - old_dir) % ANGLES) == 240);
1274         /* should be some way of getting rid of the init_dir dependency... */
1275                                         b = !(ap->init_dir % 120);
1276                                         a = ((a && !b) || (b && !a));
1277                                         drawtruchet(mi, anant->col, anant->row, status->color, a);
1278                                 } else {
1279                                         a = (old_dir / 60) % 3;
1280                                         b = (anant->direction / 60) % 3;
1281                                         a = (a + b + 1) % 3;
1282                                         drawtruchet(mi, anant->col, anant->row, status->color, a);
1283                                 }
1284                         } else if (ap->neighbors == 4) {
1285                                 a = old_dir / 180;
1286                                 b = anant->direction / 180;
1287                                 a = ((a && !b) || (b && !a));
1288                                 drawtruchet(mi, anant->col, anant->row, status->color, a);
1289                         } else if (ap->neighbors == 3) {
1290                                 if (chg_dir == 240)
1291                                         a = (2 + anant->direction / 120) % 3;
1292                                 else
1293                                         a = (1 + anant->direction / 120) % 3;
1294                                 drawtruchet(mi, anant->col, anant->row, status->color, a);
1295                         }
1296                         ap->truchet_state[tape_pos] = a + 1;
1297                 }
1298                 anant->state = status->next;
1299
1300                 /* Allow step first and turn */
1301                 old_dir = ((status->direction < ANGLES) ? anant->direction : old_dir);
1302 #if DEBUG
1303                 (void) printf("old_dir %d, col %d, row %d", old_dir, anant->col, anant->row);
1304 #endif
1305                 position_of_neighbor(ap, old_dir, &(anant->col), &(anant->row));
1306 #if DEBUG
1307                 (void) printf(", ncol %d, nrow %d\n", anant->col, anant->row);
1308 #endif
1309                 draw_anant(mi, anant->direction, anant->col, anant->row);
1310         }
1311         if (++ap->generation > MI_CYCLES(mi)) {
1312 #ifdef STANDALONE
1313       ap->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), ap->eraser);
1314 #endif
1315                 init_ant(mi);
1316         }
1317         if (ap->redrawing) {
1318                 for (i = 0; i < REDRAWSTEP; i++) {
1319                         if (ap->tape[ap->redrawpos] ||
1320                          (ap->truchet && ap->truchet_state[ap->redrawpos])) {
1321                                 drawcell(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1322                                          ap->tape[ap->redrawpos]);
1323                                 if (ap->truchet)
1324                                         drawtruchet(mi, ap->redrawpos % ap->ncols, ap->redrawpos / ap->ncols,
1325                                                     ap->tape[ap->redrawpos],
1326                                         ap->truchet_state[ap->redrawpos] - 1);
1327                         }
1328                         if (++(ap->redrawpos) >= ap->ncols * ap->nrows) {
1329                                 ap->redrawing = 0;
1330                                 break;
1331                         }
1332                 }
1333         }
1334 }
1335
1336 ENTRYPOINT void
1337 release_ant(ModeInfo * mi)
1338 {
1339         if (antfarms != NULL) {
1340                 int         screen;
1341
1342                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
1343                         free_ant(MI_DISPLAY(mi), &antfarms[screen]);
1344                 (void) free((void *) antfarms);
1345                 antfarms = (antfarmstruct *) NULL;
1346         }
1347 }
1348
1349 ENTRYPOINT void
1350 refresh_ant(ModeInfo * mi)
1351 {
1352         antfarmstruct *ap;
1353
1354         if (antfarms == NULL)
1355                 return;
1356         ap = &antfarms[MI_SCREEN(mi)];
1357
1358         if (ap->painted) {
1359                 MI_CLEARWINDOW(mi);
1360                 ap->redrawing = 1;
1361                 ap->redrawpos = 0;
1362         }
1363 }
1364
1365 XSCREENSAVER_MODULE ("Ant", ant)
1366
1367 #endif /* MODE_ant */