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