c9a15e25c711d51d47cf89c2d1f0a2cb042a351e
[xscreensaver] / hacks / demon.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* demon --- David Griffeath's cellular automata */
3
4 #if 0
5 static const char sccsid[] = "@(#)demon.c       5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1995 by David Bagley.
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * Revision History:
24  * 01-Nov-2000: Allocation checks
25  * 10-May-1997: Compatible with xscreensaver
26  * 16-Apr-1997: -neighbors 3, 9 (not sound mathematically), 12, and 8 added
27  * 30-May-1996: Ron Hitchens <ron@idiom.com>
28  *            Fixed memory management that caused leaks
29  * 14-Apr-1996: -neighbors 6 runtime-time option added
30  * 21-Aug-1995: Coded from A.K. Dewdney's "Computer Recreations", Scientific
31  *              American Magazine" Aug 1989 pp 102-105.  Also very similar
32  *              to hodgepodge machine described in A.K. Dewdney's "Computer
33  *              Recreations", Scientific American Magazine" Aug 1988
34  *              pp 104-107.  Also used life.c as a guide.
35  */
36
37 /*-
38  * A cellular universe of 4 phases debris, droplets, defects, and demons.
39  */
40
41 /*-
42   Grid     Number of Neighbors
43   ----     ------------------
44   Square   4 or 8
45   Hexagon  6
46   Triangle 3, 9, or 12
47 */
48
49 #ifndef HAVE_COCOA
50 # define DO_STIPPLE
51 #endif
52
53 #ifdef STANDALONE
54 # define MODE_demon
55 # define DEFAULTS       "*delay:   50000 \n" \
56                                         "*count:   0     \n" \
57                                         "*cycles:  1000  \n" \
58                                         "*size:    -7    \n" \
59                                         "*ncolors: 64    \n" \
60                                         "*fpsSolid: true    \n" \
61
62 # define demon_handle_event 0
63 # define UNIFORM_COLORS
64 # include "xlockmore.h"         /* in xscreensaver distribution */
65 #else /* STANDALONE */
66 # include "xlock.h"             /* in xlockmore distribution */
67 #endif /* STANDALONE */
68 #include "automata.h"
69
70 #ifdef MODE_demon
71
72 /*-
73  * neighbors of 0 randomizes it between 3, 4, 6, 8, 9, and 12.
74  */
75 #define DEF_NEIGHBORS  "0"      /* choose random value */
76
77 static int  neighbors;
78
79 static XrmOptionDescRec opts[] =
80 {
81         {"-neighbors", ".demon.neighbors", XrmoptionSepArg, 0}
82 };
83
84 static argtype vars[] =
85 {
86         {&neighbors, "neighbors", "Neighbors", DEF_NEIGHBORS, t_Int}
87 };
88 static OptionStruct desc[] =
89 {
90         {"-neighbors num", "squares 4 or 8, hexagons 6, triangles 3, 9 or 12"}
91 };
92
93 ENTRYPOINT ModeSpecOpt demon_opts =
94 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
95
96 #ifdef USE_MODULES
97 ModStruct   demon_description =
98 {"demon", "init_demon", "draw_demon", "release_demon",
99  "refresh_demon", "init_demon", (char *) NULL, &demon_opts,
100  50000, 0, 1000, -7, 64, 1.0, "",
101  "Shows Griffeath's cellular automata", 0, NULL};
102
103 #endif
104
105 #define DEMONBITS(n,w,h)\
106   if ((dp->pixmaps[dp->init_bits]=\
107   XCreatePixmapFromBitmapData(display,window,(char *)n,w,h,1,0,1))==None){\
108   free_demon(display,dp); return;} else {dp->init_bits++;}
109
110 #define REDRAWSTEP 2000         /* How many cells to draw per cycle */
111 #define MINSTATES 2
112 #define MINGRIDSIZE 24
113 #define MINSIZE 4
114 #define NEIGHBORKINDS 6
115
116 /* Singly linked list */
117 typedef struct _CellList {
118         XPoint      pt;
119         struct _CellList *next;
120 } CellList;
121
122 typedef struct {
123         int         generation;
124         int         xs, ys;
125         int         xb, yb;
126         int         nrows, ncols;
127         int         width, height;
128         int         states;
129         int         state;
130         int         redrawing, redrawpos;
131         int        *ncells;
132         CellList  **cellList;
133         unsigned char *oldcell, *newcell;
134         int         neighbors;
135         int         init_bits;
136         GC          stippledGC;
137         Pixmap      pixmaps[NUMSTIPPLES - 1];
138         union {
139                 XPoint      hexagon[6];
140                 XPoint      triangle[2][3];
141         } shape;
142 } demonstruct;
143
144 static char plots[2][NEIGHBORKINDS] =
145 {
146         {3, 4, 6, 8, 9, 12},    /* Neighborhoods */
147         {12, 16, 18, 20, 22, 24}        /* Number of states */
148 };
149
150 static demonstruct *demons = (demonstruct *) NULL;
151
152 static void
153 drawcell(ModeInfo * mi, int col, int row, unsigned char state)
154 {
155         demonstruct *dp = &demons[MI_SCREEN(mi)];
156         GC          gc;
157
158         if (!state) {
159                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
160                 gc = MI_GC(mi);
161         } else if (MI_NPIXELS(mi) >= NUMSTIPPLES) {
162                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
163                            MI_PIXEL(mi, (((int) state - 1) * MI_NPIXELS(mi) /
164                                          (dp->states - 1)) % MI_NPIXELS(mi)));
165                 gc = MI_GC(mi);
166         } else {
167                 XGCValues   gcv;
168 #ifdef DO_STIPPLE
169                 gcv.stipple = dp->pixmaps[(state - 1) % (NUMSTIPPLES - 1)];
170 #endif /* DO_STIPPLE */
171                 gcv.foreground = MI_WHITE_PIXEL(mi);
172                 gcv.background = MI_BLACK_PIXEL(mi);
173                 XChangeGC(MI_DISPLAY(mi), dp->stippledGC,
174                           GCStipple | GCForeground | GCBackground, &gcv);
175                 gc = dp->stippledGC;
176         }
177         if (dp->neighbors == 6) {
178                 int         ccol = 2 * col + !(row & 1), crow = 2 * row;
179
180                 dp->shape.hexagon[0].x = dp->xb + ccol * dp->xs;
181                 dp->shape.hexagon[0].y = dp->yb + crow * dp->ys;
182                 if (dp->xs == 1 && dp->ys == 1)
183                         XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi),
184                                        gc, dp->shape.hexagon[0].x, dp->shape.hexagon[0].y);
185                 else
186                         XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
187                             dp->shape.hexagon, 6, Convex, CoordModePrevious);
188         } else if (dp->neighbors == 4 || dp->neighbors == 8) {
189                 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
190                 dp->xb + dp->xs * col, dp->yb + dp->ys * row,
191                 dp->xs - (dp->xs > 3), dp->ys - (dp->ys > 3));
192         } else {                /* TRI */
193                 int         orient = (col + row) % 2;   /* O left 1 right */
194
195                 dp->shape.triangle[orient][0].x = dp->xb + col * dp->xs;
196                 dp->shape.triangle[orient][0].y = dp->yb + row * dp->ys;
197                 if (dp->xs <= 3 || dp->ys <= 3)
198                         XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
199                         ((orient) ? -1 : 1) + dp->shape.triangle[orient][0].x,
200                                        dp->shape.triangle[orient][0].y);
201                 else {
202                         if (orient)
203                                 dp->shape.triangle[orient][0].x += (dp->xs / 2 - 1);
204                         else
205                                 dp->shape.triangle[orient][0].x -= (dp->xs / 2 - 1);
206                         XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
207                                      dp->shape.triangle[orient], 3, Convex, CoordModePrevious);
208
209                 }
210         }
211 }
212
213 static Bool
214 addtolist(ModeInfo * mi, int col, int row, unsigned char state)
215 {
216         demonstruct *dp = &demons[MI_SCREEN(mi)];
217         CellList   *current;
218
219         current = dp->cellList[state];
220         if ((dp->cellList[state] = (CellList *)
221                 malloc(sizeof (CellList))) == NULL) {
222                 return False;
223         }
224         dp->cellList[state]->pt.x = col;
225         dp->cellList[state]->pt.y = row;
226         dp->cellList[state]->next = current;
227         dp->ncells[state]++;
228         return True;
229 }
230
231 #ifdef DEBUG
232 static void
233 print_state(ModeInfo * mi, int state)
234 {
235         demonstruct *dp = &demons[MI_SCREEN(mi)];
236         CellList   *locallist;
237         int         i = 0;
238
239         locallist = dp->cellList[state];
240         (void) printf("state %d\n", state);
241         while (locallist) {
242                 (void) printf("%d       x %d, y %d\n", i,
243                               locallist->pt.x, locallist->pt.y);
244                 locallist = locallist->next;
245                 i++;
246         }
247 }
248
249 #endif
250
251 static void
252 free_state(demonstruct * dp, int state)
253 {
254         CellList   *current;
255
256         while (dp->cellList[state]) {
257                 current = dp->cellList[state];
258                 dp->cellList[state] = dp->cellList[state]->next;
259                 (void) free((void *) current);
260         }
261         dp->cellList[state] = (CellList *) NULL;
262         if (dp->ncells != NULL)
263                 dp->ncells[state] = 0;
264 }
265
266
267 static void
268 free_list(demonstruct * dp)
269 {
270         int         state;
271
272         for (state = 0; state < dp->states; state++)
273                 free_state(dp, state);
274         (void) free((void *) dp->cellList);
275         dp->cellList = (CellList **) NULL;
276 }
277
278 static void
279 free_struct(demonstruct * dp)
280 {
281         if (dp->cellList != NULL) {
282                 free_list(dp);
283         }
284         if (dp->ncells != NULL) {
285                 (void) free((void *) dp->ncells);
286                 dp->ncells = (int *) NULL;
287         }
288         if (dp->oldcell != NULL) {
289                 (void) free((void *) dp->oldcell);
290                 dp->oldcell = (unsigned char *) NULL;
291         }
292         if (dp->newcell != NULL) {
293                 (void) free((void *) dp->newcell);
294                 dp->newcell = (unsigned char *) NULL;
295         }
296 }
297
298 static void
299 free_demon(Display *display, demonstruct *dp)
300 {
301         int         shade;
302
303         if (dp->stippledGC != None) {
304                 XFreeGC(display, dp->stippledGC);
305                 dp->stippledGC = None;
306         }
307         for (shade = 0; shade < dp->init_bits; shade++) {
308                 XFreePixmap(display, dp->pixmaps[shade]);
309         }
310         dp->init_bits = 0;
311         free_struct(dp);
312 }
313
314 static Bool
315 draw_state(ModeInfo * mi, int state)
316 {
317         demonstruct *dp = &demons[MI_SCREEN(mi)];
318         GC          gc;
319         XRectangle *rects;
320         CellList   *current;
321
322         if (!state) {
323                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
324                 gc = MI_GC(mi);
325         } else if (MI_NPIXELS(mi) >= NUMSTIPPLES) {
326                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
327                            MI_PIXEL(mi, (((int) state - 1) * MI_NPIXELS(mi) /
328                                          (dp->states - 1)) % MI_NPIXELS(mi)));
329                 gc = MI_GC(mi);
330         } else {
331                 XGCValues   gcv;
332
333 #ifdef DO_STIPPLE
334                 gcv.stipple = dp->pixmaps[(state - 1) % (NUMSTIPPLES - 1)];
335 #endif /* DO_STIPPLE */
336                 gcv.foreground = MI_WHITE_PIXEL(mi);
337                 gcv.background = MI_BLACK_PIXEL(mi);
338                 XChangeGC(MI_DISPLAY(mi), dp->stippledGC,
339                           GCStipple | GCForeground | GCBackground, &gcv);
340                 gc = dp->stippledGC;
341         }
342         if (dp->neighbors == 6) {       /* Draw right away, slow */
343                 current = dp->cellList[state];
344                 while (current) {
345                         int         col, row, ccol, crow;
346
347                         col = current->pt.x;
348                         row = current->pt.y;
349                         ccol = 2 * col + !(row & 1), crow = 2 * row;
350                         dp->shape.hexagon[0].x = dp->xb + ccol * dp->xs;
351                         dp->shape.hexagon[0].y = dp->yb + crow * dp->ys;
352                         if (dp->xs == 1 && dp->ys == 1)
353                                 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi),
354                                                gc, dp->shape.hexagon[0].x, dp->shape.hexagon[0].y);
355                         else
356                                 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
357                                              dp->shape.hexagon, 6, Convex, CoordModePrevious);
358                         current = current->next;
359                 }
360         } else if (dp->neighbors == 4 || dp->neighbors == 8) {
361                 /* Take advantage of XDrawRectangles */
362                 int         ncells = 0;
363
364                 /* Create Rectangle list from part of the cellList */
365                 if ((rects = (XRectangle *) malloc(dp->ncells[state] *
366                          sizeof (XRectangle))) == NULL) {
367                         return False;
368                 }
369                 current = dp->cellList[state];
370                 while (current) {
371                         rects[ncells].x = dp->xb + current->pt.x * dp->xs;
372                         rects[ncells].y = dp->yb + current->pt.y * dp->ys;
373                         rects[ncells].width = dp->xs - (dp->xs > 3);
374                         rects[ncells].height = dp->ys - (dp->ys > 3);
375                         current = current->next;
376                         ncells++;
377                 }
378                 /* Finally get to draw */
379                 XFillRectangles(MI_DISPLAY(mi), MI_WINDOW(mi), gc, rects, ncells);
380                 /* Free up rects list and the appropriate part of the cellList */
381                 (void) free((void *) rects);
382         } else {                /* TRI */
383                 current = dp->cellList[state];
384                 while (current) {
385                         int         col, row, orient;
386
387                         col = current->pt.x;
388                         row = current->pt.y;
389                         orient = (col + row) % 2;       /* O left 1 right */
390                         dp->shape.triangle[orient][0].x = dp->xb + col * dp->xs;
391                         dp->shape.triangle[orient][0].y = dp->yb + row * dp->ys;
392                         if (dp->xs <= 3 || dp->ys <= 3)
393                                 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
394                                                ((orient) ? -1 : 1) + dp->shape.triangle[orient][0].x,
395                                       dp->shape.triangle[orient][0].y);
396                         else {
397                                 if (orient)
398                                         dp->shape.triangle[orient][0].x += (dp->xs / 2 - 1);
399                                 else
400                                         dp->shape.triangle[orient][0].x -= (dp->xs / 2 - 1);
401                                 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
402                                              dp->shape.triangle[orient], 3, Convex, CoordModePrevious);
403                         }
404                         current = current->next;
405                 }
406         }
407         free_state(dp, state);
408         return True;
409 }
410
411 static void
412 RandomSoup(ModeInfo * mi)
413 {
414         demonstruct *dp = &demons[MI_SCREEN(mi)];
415         int         row, col, mrow = 0;
416
417         for (row = 0; row < dp->nrows; ++row) {
418                 for (col = 0; col < dp->ncols; ++col) {
419                         dp->oldcell[col + mrow] =
420                                 (unsigned char) LRAND() % ((unsigned char) dp->states);
421                         if (!addtolist(mi, col, row, dp->oldcell[col + mrow]))
422                                 return; /* sparse soup */
423                 }
424                 mrow += dp->ncols;
425         }
426 }
427
428 ENTRYPOINT void
429 init_demon (ModeInfo * mi)
430 {
431         Display    *display = MI_DISPLAY(mi);
432         int         size = MI_SIZE(mi), nk;
433         demonstruct *dp;
434
435         if (demons == NULL) {
436                 if ((demons = (demonstruct *) calloc(MI_NUM_SCREENS(mi),
437                                               sizeof (demonstruct))) == NULL)
438                         return;
439         }
440         dp = &demons[MI_SCREEN(mi)];
441
442         dp->generation = 0;
443         dp->redrawing = 0;
444 #ifdef DO_STIPPLE
445         if (MI_NPIXELS(mi) < NUMSTIPPLES) {
446           Window window = MI_WINDOW(mi);
447           if (dp->stippledGC == None) {
448                         XGCValues   gcv;
449
450                         gcv.fill_style = FillOpaqueStippled;
451                         if ((dp->stippledGC = XCreateGC(display, window,
452                                  GCFillStyle, &gcv)) == None) {
453                                 free_demon(display, dp);
454                                 return;
455                         }
456                 }
457                 if (dp->init_bits == 0) {
458                         int         i;
459
460                         for (i = 1; i < NUMSTIPPLES; i++) {
461                                 DEMONBITS(stipples[i], STIPPLESIZE, STIPPLESIZE);
462                         }
463                 }
464         }
465 #endif /* DO_STIPPLE */
466         free_struct(dp);
467
468 #ifdef HAVE_COCOA
469     jwxyz_XSetAntiAliasing (MI_DISPLAY(mi), MI_GC(mi), False);
470 #endif
471
472         for (nk = 0; nk < NEIGHBORKINDS; nk++) {
473                 if (neighbors == plots[0][nk]) {
474                         dp->neighbors = plots[0][nk];
475                         break;
476                 }
477                 if (nk == NEIGHBORKINDS - 1) {
478                         nk = NRAND(NEIGHBORKINDS);
479                         dp->neighbors = plots[0][nk];
480                         break;
481                 }
482         }
483
484         dp->states = MI_COUNT(mi);
485         if (dp->states < -MINSTATES)
486                 dp->states = NRAND(-dp->states - MINSTATES + 1) + MINSTATES;
487         else if (dp->states < MINSTATES)
488                 dp->states = plots[1][nk];
489         if ((dp->cellList = (CellList **) calloc(dp->states,
490                 sizeof (CellList *))) == NULL) {
491                 free_demon(display, dp);
492                 return;
493         }
494         if ((dp->ncells = (int *) calloc(dp->states, sizeof (int))) == NULL) {
495                 free_demon(display, dp);
496                 return;
497         }
498
499         dp->state = 0;
500
501         dp->width = MI_WIDTH(mi);
502         dp->height = MI_HEIGHT(mi);
503
504         if (dp->neighbors == 6) {
505                 int         nccols, ncrows, i;
506
507                 if (dp->width < 8)
508                         dp->width = 8;
509                 if (dp->height < 8)
510                         dp->height = 8;
511                 if (size < -MINSIZE)
512                         dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
513                                       MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
514                 else if (size < MINSIZE) {
515                         if (!size)
516                                 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
517                         else
518                                 dp->ys = MINSIZE;
519                 } else
520                         dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
521                                                MINGRIDSIZE));
522                 dp->xs = dp->ys;
523                 nccols = MAX(dp->width / dp->xs - 2, 2);
524                 ncrows = MAX(dp->height / dp->ys - 1, 4);
525                 dp->ncols = nccols / 2;
526                 dp->nrows = 2 * (ncrows / 4);
527                 dp->xb = (dp->width - dp->xs * nccols) / 2 + dp->xs / 2;
528                 dp->yb = (dp->height - dp->ys * (ncrows / 2) * 2) / 2 + dp->ys - 2;
529                 for (i = 0; i < 6; i++) {
530                         dp->shape.hexagon[i].x = (dp->xs - 1) * hexagonUnit[i].x;
531                         dp->shape.hexagon[i].y = ((dp->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
532                 }
533         } else if (dp->neighbors == 4 || dp->neighbors == 8) {
534                 if (size < -MINSIZE)
535                         dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
536                                       MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
537                 else if (size < MINSIZE) {
538                         if (!size)
539                                 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
540                         else
541                                 dp->ys = MINSIZE;
542                 } else
543                         dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
544                                                MINGRIDSIZE));
545                 dp->xs = dp->ys;
546                 dp->ncols = MAX(dp->width / dp->xs, 2);
547                 dp->nrows = MAX(dp->height / dp->ys, 2);
548                 dp->xb = (dp->width - dp->xs * dp->ncols) / 2;
549                 dp->yb = (dp->height - dp->ys * dp->nrows) / 2;
550         } else {                /* TRI */
551                 int         orient, i;
552
553                 if (dp->width < 2)
554                         dp->width = 2;
555                 if (dp->height < 2)
556                         dp->height = 2;
557                 if (size < -MINSIZE)
558                         dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
559                                       MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
560                 else if (size < MINSIZE) {
561                         if (!size)
562                                 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
563                         else
564                                 dp->ys = MINSIZE;
565                 } else
566                         dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
567                                                MINGRIDSIZE));
568                 dp->xs = (int) (1.52 * dp->ys);
569                 dp->ncols = (MAX(dp->width / dp->xs - 1, 2) / 2) * 2;
570                 dp->nrows = (MAX(dp->height / dp->ys - 1, 2) / 2) * 2;
571                 dp->xb = (dp->width - dp->xs * dp->ncols) / 2 + dp->xs / 2;
572                 dp->yb = (dp->height - dp->ys * dp->nrows) / 2 + dp->ys / 2;
573                 for (orient = 0; orient < 2; orient++) {
574                         for (i = 0; i < 3; i++) {
575                                 dp->shape.triangle[orient][i].x =
576                                         (dp->xs - 2) * triangleUnit[orient][i].x;
577                                 dp->shape.triangle[orient][i].y =
578                                         (dp->ys - 2) * triangleUnit[orient][i].y;
579                         }
580                 }
581         }
582
583         MI_CLEARWINDOW(mi);
584
585         if ((dp->oldcell = (unsigned char *)
586                 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
587                 free_demon(display, dp);
588                 return;
589         }
590
591         if ((dp->newcell = (unsigned char *)
592                 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
593                 free_demon(display, dp);
594                 return;
595         }
596
597         RandomSoup(mi);
598 }
599
600 ENTRYPOINT void
601 draw_demon (ModeInfo * mi)
602 {
603         int         i, j, k, l, mj = 0, ml;
604         demonstruct *dp;
605
606         if (demons == NULL)
607                 return;
608         dp = &demons[MI_SCREEN(mi)];
609         if (dp->cellList == NULL)
610                 return;
611
612         MI_IS_DRAWN(mi) = True;
613         if (dp->state >= dp->states) {
614                 (void) memcpy((char *) dp->newcell, (char *) dp->oldcell,
615                               dp->ncols * dp->nrows * sizeof (unsigned char));
616
617                 if (dp->neighbors == 6) {
618                         for (j = 0; j < dp->nrows; j++) {
619                                 for (i = 0; i < dp->ncols; i++) {
620                                         /* NE */
621                                         if (!(j & 1))
622                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
623                                         else
624                                                 k = i;
625                                         l = (!j) ? dp->nrows - 1 : j - 1;
626                                         ml = l * dp->ncols;
627                                         if (dp->oldcell[k + ml] ==
628                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
629                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
630                                         /* E */
631                                         k = (i + 1 == dp->ncols) ? 0 : i + 1;
632                                         ml = mj;
633                                         if (dp->oldcell[k + ml] ==
634                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
635                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
636                                         /* SE */
637                                         if (!(j & 1))
638                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
639                                         else
640                                                 k = i;
641                                         l = (j + 1 == dp->nrows) ? 0 : j + 1;
642                                         ml = l * dp->ncols;
643                                         if (dp->oldcell[k + ml] ==
644                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
645                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
646                                         /* SW */
647                                         if (j & 1)
648                                                 k = (!i) ? dp->ncols - 1 : i - 1;
649                                         else
650                                                 k = i;
651                                         l = (j + 1 == dp->nrows) ? 0 : j + 1;
652                                         ml = l * dp->ncols;
653                                         if (dp->oldcell[k + ml] ==
654                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
655                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
656                                         /* W */
657                                         k = (!i) ? dp->ncols - 1 : i - 1;
658                                         ml = mj;
659                                         if (dp->oldcell[k + ml] ==
660                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
661                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
662                                         /* NW */
663                                         if (j & 1)
664                                                 k = (!i) ? dp->ncols - 1 : i - 1;
665                                         else
666                                                 k = i;
667                                         l = (!j) ? dp->nrows - 1 : j - 1;
668                                         ml = l * dp->ncols;
669                                         if (dp->oldcell[k + ml] ==
670                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
671                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
672                                 }
673                                 mj += dp->ncols;
674                         }
675                 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
676                         for (j = 0; j < dp->nrows; j++) {
677                                 for (i = 0; i < dp->ncols; i++) {
678                                         /* N */
679                                         k = i;
680                                         l = (!j) ? dp->nrows - 1 : j - 1;
681                                         ml = l * dp->ncols;
682                                         if (dp->oldcell[k + ml] ==
683                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
684                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
685                                         /* E */
686                                         k = (i + 1 == dp->ncols) ? 0 : i + 1;
687                                         ml = mj;
688                                         if (dp->oldcell[k + ml] ==
689                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
690                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
691                                         /* S */
692                                         k = i;
693                                         l = (j + 1 == dp->nrows) ? 0 : j + 1;
694                                         ml = l * dp->ncols;
695                                         if (dp->oldcell[k + ml] ==
696                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
697                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
698                                         /* W */
699                                         k = (!i) ? dp->ncols - 1 : i - 1;
700                                         /*l = j;*/
701                                         ml = mj;
702                                         if (dp->oldcell[k + ml] ==
703                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
704                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
705                                 }
706                                 mj += dp->ncols;
707                         }
708                         if (dp->neighbors == 8) {
709                                 mj = 0;
710                                 for (j = 0; j < dp->nrows; j++) {
711                                         for (i = 0; i < dp->ncols; i++) {
712                                                 /* NE */
713                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
714                                                 l = (!j) ? dp->nrows - 1 : j - 1;
715                                                 ml = l * dp->ncols;
716                                                 if (dp->oldcell[k + ml] ==
717                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
718                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
719                                                 /* SE */
720                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
721                                                 l = (j + 1 == dp->nrows) ? 0 : j + 1;
722                                                 ml = l * dp->ncols;
723                                                 if (dp->oldcell[k + ml] ==
724                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
725                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
726                                                 /* SW */
727                                                 k = (!i) ? dp->ncols - 1 : i - 1;
728                                                 l = (j + 1 == dp->nrows) ? 0 : j + 1;
729                                                 ml = l * dp->ncols;
730                                                 if (dp->oldcell[k + ml] ==
731                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
732                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
733                                                 /* NW */
734                                                 k = (!i) ? dp->ncols - 1 : i - 1;
735                                                 l = (!j) ? dp->nrows - 1 : j - 1;
736                                                 ml = l * dp->ncols;
737                                                 if (dp->oldcell[k + ml] ==
738                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
739                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
740                                         }
741                                         mj += dp->ncols;
742                                 }
743                         }
744                 } else if (dp->neighbors == 3 || dp->neighbors == 9 ||
745                            dp->neighbors == 12) {
746                         for (j = 0; j < dp->nrows; j++) {
747                                 for (i = 0; i < dp->ncols; i++) {
748                                         if ((i + j) % 2) {      /* right */
749                                                 /* W */
750                                                 k = (!i) ? dp->ncols - 1 : i - 1;
751                                                 ml = mj;
752                                                 if (dp->oldcell[k + ml] ==
753                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
754                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
755                                         } else {        /* left */
756                                                 /* E */
757                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
758                                                 ml = mj;
759                                                 if (dp->oldcell[k + ml] ==
760                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
761                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
762                                         }
763                                         /* N */
764                                         k = i;
765                                         l = (!j) ? dp->nrows - 1 : j - 1;
766                                         ml = l * dp->ncols;
767                                         if (dp->oldcell[k + ml] ==
768                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
769                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
770                                         /* S */
771                                         k = i;
772                                         l = (j + 1 == dp->nrows) ? 0 : j + 1;
773                                         ml = l * dp->ncols;
774                                         if (dp->oldcell[k + ml] ==
775                                             (int) (dp->oldcell[i + mj] + 1) % dp->states)
776                                                 dp->newcell[i + mj] = dp->oldcell[k + ml];
777                                 }
778                                 mj += dp->ncols;
779                         }
780                         if (dp->neighbors == 9 || dp->neighbors == 12) {
781                                 mj = 0;
782                                 for (j = 0; j < dp->nrows; j++) {
783                                         for (i = 0; i < dp->ncols; i++) {
784                                                 /* NN */
785                                                 k = i;
786                                                 if (!j)
787                                                         l = dp->nrows - 2;
788                                                 else if (!(j - 1))
789                                                         l = dp->nrows - 1;
790                                                 else
791                                                         l = j - 2;
792                                                 ml = l * dp->ncols;
793                                                 if (dp->oldcell[k + ml] ==
794                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
795                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
796                                                 /* SS */
797                                                 k = i;
798                                                 if (j + 1 == dp->nrows)
799                                                         l = 1;
800                                                 else if (j + 2 == dp->nrows)
801                                                         l = 0;
802                                                 else
803                                                         l = j + 2;
804                                                 ml = l * dp->ncols;
805                                                 if (dp->oldcell[k + ml] ==
806                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
807                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
808                                                 /* NW */
809                                                 k = (!i) ? dp->ncols - 1 : i - 1;
810                                                 l = (!j) ? dp->nrows - 1 : j - 1;
811                                                 ml = l * dp->ncols;
812                                                 if (dp->oldcell[k + ml] ==
813                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
814                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
815                                                 /* NE */
816                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
817                                                 l = (!j) ? dp->nrows - 1 : j - 1;
818                                                 ml = l * dp->ncols;
819                                                 if (dp->oldcell[k + ml] ==
820                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
821                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
822                                                 /* SW */
823                                                 k = (!i) ? dp->ncols - 1 : i - 1;
824                                                 l = (j + 1 == dp->nrows) ? 0 : j + 1;
825                                                 ml = l * dp->ncols;
826                                                 if (dp->oldcell[k + ml] ==
827                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
828                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
829                                                 /* SE */
830                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
831                                                 l = (j + 1 == dp->nrows) ? 0 : j + 1;
832                                                 ml = l * dp->ncols;
833                                                 if (dp->oldcell[k + ml] ==
834                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
835                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
836                                         }
837                                         mj += dp->ncols;
838                                 }
839                                 if (dp->neighbors == 12) {
840                                         mj = 0;
841                                         for (j = 0; j < dp->nrows; j++) {
842                                                 for (i = 0; i < dp->ncols; i++) {
843                                                         if ((i + j) % 2) {      /* right */
844                                                                 /* NNW */
845                                                                 k = (!i) ? dp->ncols - 1 : i - 1;
846                                                                 if (!j)
847                                                                         l = dp->nrows - 2;
848                                                                 else if (!(j - 1))
849                                                                         l = dp->nrows - 1;
850                                                                 else
851                                                                         l = j - 2;
852                                                                 ml = l * dp->ncols;
853                                                                 if (dp->oldcell[k + ml] ==
854                                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
855                                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
856                                                                 /* SSW */
857                                                                 k = (!i) ? dp->ncols - 1 : i - 1;
858                                                                 if (j + 1 == dp->nrows)
859                                                                         l = 1;
860                                                                 else if (j + 2 == dp->nrows)
861                                                                         l = 0;
862                                                                 else
863                                                                         l = j + 2;
864                                                                 ml = l * dp->ncols;
865                                                                 if (dp->oldcell[k + ml] ==
866                                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
867                                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
868                                                                 /* EE */
869                                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
870                                                                 /*l = j;*/
871                                                                 ml = mj;
872                                                                 if (dp->oldcell[k + ml] ==
873                                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
874                                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
875                                                         } else {        /* left */
876                                                                 /* NNE */
877                                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
878                                                                 if (!j)
879                                                                         l = dp->nrows - 2;
880                                                                 else if (!(j - 1))
881                                                                         l = dp->nrows - 1;
882                                                                 else
883                                                                         l = j - 2;
884                                                                 ml = l * dp->ncols;
885                                                                 if (dp->oldcell[k + ml] ==
886                                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
887                                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
888                                                                 /* SSE */
889                                                                 k = (i + 1 == dp->ncols) ? 0 : i + 1;
890                                                                 if (j + 1 == dp->nrows)
891                                                                         l = 1;
892                                                                 else if (j + 2 == dp->nrows)
893                                                                         l = 0;
894                                                                 else
895                                                                         l = j + 2;
896                                                                 ml = l * dp->ncols;
897                                                                 if (dp->oldcell[k + ml] ==
898                                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
899                                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
900                                                                 /* WW */
901                                                                 k = (!i) ? dp->ncols - 1 : i - 1;
902                                                                 /*l = j;*/
903                                                                 ml = mj;
904                                                                 if (dp->oldcell[k + ml] ==
905                                                                     (int) (dp->oldcell[i + mj] + 1) % dp->states)
906                                                                         dp->newcell[i + mj] = dp->oldcell[k + ml];
907                                                         }
908                                                 }
909                                                 mj += dp->ncols;
910                                         }
911                                 }
912                         }
913                 }
914                 mj = 0;
915                 for (j = 0; j < dp->nrows; j++) {
916                         for (i = 0; i < dp->ncols; i++)
917                                 if (dp->oldcell[i + mj] != dp->newcell[i + mj]) {
918                                         dp->oldcell[i + mj] = dp->newcell[i + mj];
919                                         if (!addtolist(mi, i, j, dp->oldcell[i + mj])) {
920                                                 free_demon(MI_DISPLAY(mi), dp);
921                                                 return;
922                                         }
923                                 }
924                         mj += dp->ncols;
925                 }
926                 if (++dp->generation > MI_CYCLES(mi))
927                         init_demon(mi);
928                 dp->state = 0;
929         } else {
930                 if (dp->ncells[dp->state])
931                         if (!draw_state(mi, dp->state)) {
932                                 free_demon(MI_DISPLAY(mi), dp);
933                                 return;
934                         }
935                 dp->state++;
936         }
937         if (dp->redrawing) {
938                 for (i = 0; i < REDRAWSTEP; i++) {
939                         if (dp->oldcell[dp->redrawpos]) {
940                                 drawcell(mi, dp->redrawpos % dp->ncols, dp->redrawpos / dp->ncols,
941                                          dp->oldcell[dp->redrawpos]);
942                         }
943                         if (++(dp->redrawpos) >= dp->ncols * dp->nrows) {
944                                 dp->redrawing = 0;
945                                 break;
946                         }
947                 }
948         }
949 }
950
951
952 ENTRYPOINT void
953 reshape_demon(ModeInfo * mi, int width, int height)
954 {
955   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
956   init_demon (mi);
957 }
958
959
960 ENTRYPOINT void
961 release_demon (ModeInfo * mi)
962 {
963         if (demons != NULL) {
964                 int         screen;
965
966                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
967                         free_demon(MI_DISPLAY(mi), &demons[screen]);
968                 (void) free((void *) demons);
969                 demons = (demonstruct *) NULL;
970         }
971 }
972
973 ENTRYPOINT void
974 refresh_demon (ModeInfo * mi)
975 {
976         demonstruct *dp;
977
978         if (demons == NULL)
979                 return;
980         dp = &demons[MI_SCREEN(mi)];
981
982         dp->redrawing = 1;
983         dp->redrawpos = 0;
984 }
985
986 XSCREENSAVER_MODULE ("Demon", demon)
987
988 #endif /* MODE_demon */