1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* demon --- David Griffeath's cellular automata */
5 static const char sccsid[] = "@(#)demon.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1995 by David Bagley.
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.
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.
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.
38 * A cellular universe of 4 phases debris, droplets, defects, and demons.
42 Grid Number of Neighbors
43 ---- ------------------
55 # define DEFAULTS "*delay: 50000 \n" \
60 "*fpsSolid: true \n" \
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 */
73 * neighbors of 0 randomizes it between 3, 4, 6, 8, 9, and 12.
75 #define DEF_NEIGHBORS "0" /* choose random value */
79 static XrmOptionDescRec opts[] =
81 {"-neighbors", ".demon.neighbors", XrmoptionSepArg, 0}
84 static argtype vars[] =
86 {&neighbors, "neighbors", "Neighbors", DEF_NEIGHBORS, t_Int}
88 static OptionStruct desc[] =
90 {"-neighbors num", "squares 4 or 8, hexagons 6, triangles 3, 9 or 12"}
93 ENTRYPOINT ModeSpecOpt demon_opts =
94 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
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};
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++;}
110 #define REDRAWSTEP 2000 /* How many cells to draw per cycle */
112 #define MINGRIDSIZE 24
114 #define NEIGHBORKINDS 6
116 /* Singly linked list */
117 typedef struct _CellList {
119 struct _CellList *next;
130 int redrawing, redrawpos;
133 unsigned char *oldcell, *newcell;
137 Pixmap pixmaps[NUMSTIPPLES - 1];
140 XPoint triangle[2][3];
144 static char plots[2][NEIGHBORKINDS] =
146 {3, 4, 6, 8, 9, 12}, /* Neighborhoods */
147 {12, 16, 18, 20, 22, 24} /* Number of states */
150 static demonstruct *demons = (demonstruct *) NULL;
153 drawcell(ModeInfo * mi, int col, int row, unsigned char state)
155 demonstruct *dp = &demons[MI_SCREEN(mi)];
159 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(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)));
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);
178 if (dp->neighbors == 6) {
179 int ccol = 2 * col + !(row & 1), crow = 2 * row;
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);
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));
194 int orient = (col + row) % 2; /* O left 1 right */
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);
204 dp->shape.triangle[orient][0].x += (dp->xs / 2 - 1);
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);
215 addtolist(ModeInfo * mi, int col, int row, unsigned char state)
217 demonstruct *dp = &demons[MI_SCREEN(mi)];
220 current = dp->cellList[state];
221 if ((dp->cellList[state] = (CellList *)
222 malloc(sizeof (CellList))) == NULL) {
225 dp->cellList[state]->pt.x = col;
226 dp->cellList[state]->pt.y = row;
227 dp->cellList[state]->next = current;
234 print_state(ModeInfo * mi, int state)
236 demonstruct *dp = &demons[MI_SCREEN(mi)];
240 locallist = dp->cellList[state];
241 (void) printf("state %d\n", state);
243 (void) printf("%d x %d, y %d\n", i,
244 locallist->pt.x, locallist->pt.y);
245 locallist = locallist->next;
253 free_state(demonstruct * dp, int state)
257 while (dp->cellList[state]) {
258 current = dp->cellList[state];
259 dp->cellList[state] = dp->cellList[state]->next;
260 (void) free((void *) current);
262 dp->cellList[state] = (CellList *) NULL;
263 if (dp->ncells != NULL)
264 dp->ncells[state] = 0;
269 free_list(demonstruct * dp)
273 for (state = 0; state < dp->states; state++)
274 free_state(dp, state);
275 (void) free((void *) dp->cellList);
276 dp->cellList = (CellList **) NULL;
280 free_struct(demonstruct * dp)
282 if (dp->cellList != NULL) {
285 if (dp->ncells != NULL) {
286 (void) free((void *) dp->ncells);
287 dp->ncells = (int *) NULL;
289 if (dp->oldcell != NULL) {
290 (void) free((void *) dp->oldcell);
291 dp->oldcell = (unsigned char *) NULL;
293 if (dp->newcell != NULL) {
294 (void) free((void *) dp->newcell);
295 dp->newcell = (unsigned char *) NULL;
300 free_demon(Display *display, demonstruct *dp)
304 if (dp->stippledGC != None) {
305 XFreeGC(display, dp->stippledGC);
306 dp->stippledGC = None;
308 for (shade = 0; shade < dp->init_bits; shade++) {
309 XFreePixmap(display, dp->pixmaps[shade]);
316 draw_state(ModeInfo * mi, int state)
318 demonstruct *dp = &demons[MI_SCREEN(mi)];
324 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(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)));
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);
343 if (dp->neighbors == 6) { /* Draw right away, slow */
344 current = dp->cellList[state];
346 int col, row, ccol, crow;
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);
357 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
358 dp->shape.hexagon, 6, Convex, CoordModePrevious);
359 current = current->next;
361 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
362 /* Take advantage of XDrawRectangles */
365 /* Create Rectangle list from part of the cellList */
366 if ((rects = (XRectangle *) malloc(dp->ncells[state] *
367 sizeof (XRectangle))) == NULL) {
370 current = dp->cellList[state];
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;
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);
384 current = dp->cellList[state];
386 int col, row, orient;
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);
399 dp->shape.triangle[orient][0].x += (dp->xs / 2 - 1);
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);
405 current = current->next;
408 free_state(dp, state);
413 RandomSoup(ModeInfo * mi)
415 demonstruct *dp = &demons[MI_SCREEN(mi)];
416 int row, col, mrow = 0;
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 */
430 init_demon (ModeInfo * mi)
432 Display *display = MI_DISPLAY(mi);
433 int size = MI_SIZE(mi), nk;
436 if (demons == NULL) {
437 if ((demons = (demonstruct *) calloc(MI_NUM_SCREENS(mi),
438 sizeof (demonstruct))) == NULL)
441 dp = &demons[MI_SCREEN(mi)];
446 if (MI_NPIXELS(mi) < NUMSTIPPLES) {
447 Window window = MI_WINDOW(mi);
448 if (dp->stippledGC == None) {
451 gcv.fill_style = FillOpaqueStippled;
452 if ((dp->stippledGC = XCreateGC(display, window,
453 GCFillStyle, &gcv)) == None) {
454 free_demon(display, dp);
458 if (dp->init_bits == 0) {
461 for (i = 1; i < NUMSTIPPLES; i++) {
462 DEMONBITS(stipples[i], STIPPLESIZE, STIPPLESIZE);
466 #endif /* DO_STIPPLE */
469 for (nk = 0; nk < NEIGHBORKINDS; nk++) {
470 if (neighbors == plots[0][nk]) {
471 dp->neighbors = plots[0][nk];
474 if (nk == NEIGHBORKINDS - 1) {
475 nk = NRAND(NEIGHBORKINDS);
476 dp->neighbors = plots[0][nk];
481 dp->states = MI_COUNT(mi);
482 if (dp->states < -MINSTATES)
483 dp->states = NRAND(-dp->states - MINSTATES + 1) + MINSTATES;
484 else if (dp->states < MINSTATES)
485 dp->states = plots[1][nk];
486 if ((dp->cellList = (CellList **) calloc(dp->states,
487 sizeof (CellList *))) == NULL) {
488 free_demon(display, dp);
491 if ((dp->ncells = (int *) calloc(dp->states, sizeof (int))) == NULL) {
492 free_demon(display, dp);
498 dp->width = MI_WIDTH(mi);
499 dp->height = MI_HEIGHT(mi);
501 if (dp->neighbors == 6) {
502 int nccols, ncrows, i;
509 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
510 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
511 else if (size < MINSIZE) {
513 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
517 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
520 nccols = MAX(dp->width / dp->xs - 2, 2);
521 ncrows = MAX(dp->height / dp->ys - 1, 4);
522 dp->ncols = nccols / 2;
523 dp->nrows = 2 * (ncrows / 4);
524 dp->xb = (dp->width - dp->xs * nccols) / 2 + dp->xs / 2;
525 dp->yb = (dp->height - dp->ys * (ncrows / 2) * 2) / 2 + dp->ys - 2;
526 for (i = 0; i < 6; i++) {
527 dp->shape.hexagon[i].x = (dp->xs - 1) * hexagonUnit[i].x;
528 dp->shape.hexagon[i].y = ((dp->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
530 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
532 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
533 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
534 else if (size < MINSIZE) {
536 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
540 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
543 dp->ncols = MAX(dp->width / dp->xs, 2);
544 dp->nrows = MAX(dp->height / dp->ys, 2);
545 dp->xb = (dp->width - dp->xs * dp->ncols) / 2;
546 dp->yb = (dp->height - dp->ys * dp->nrows) / 2;
555 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
556 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
557 else if (size < MINSIZE) {
559 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
563 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
565 dp->xs = (int) (1.52 * dp->ys);
566 dp->ncols = (MAX(dp->width / dp->xs - 1, 2) / 2) * 2;
567 dp->nrows = (MAX(dp->height / dp->ys - 1, 2) / 2) * 2;
568 dp->xb = (dp->width - dp->xs * dp->ncols) / 2 + dp->xs / 2;
569 dp->yb = (dp->height - dp->ys * dp->nrows) / 2 + dp->ys / 2;
570 for (orient = 0; orient < 2; orient++) {
571 for (i = 0; i < 3; i++) {
572 dp->shape.triangle[orient][i].x =
573 (dp->xs - 2) * triangleUnit[orient][i].x;
574 dp->shape.triangle[orient][i].y =
575 (dp->ys - 2) * triangleUnit[orient][i].y;
582 if ((dp->oldcell = (unsigned char *)
583 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
584 free_demon(display, dp);
588 if ((dp->newcell = (unsigned char *)
589 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
590 free_demon(display, dp);
598 draw_demon (ModeInfo * mi)
600 int i, j, k, l, mj = 0, ml;
605 dp = &demons[MI_SCREEN(mi)];
606 if (dp->cellList == NULL)
609 MI_IS_DRAWN(mi) = True;
610 if (dp->state >= dp->states) {
611 (void) memcpy((char *) dp->newcell, (char *) dp->oldcell,
612 dp->ncols * dp->nrows * sizeof (unsigned char));
614 if (dp->neighbors == 6) {
615 for (j = 0; j < dp->nrows; j++) {
616 for (i = 0; i < dp->ncols; i++) {
619 k = (i + 1 == dp->ncols) ? 0 : i + 1;
622 l = (!j) ? dp->nrows - 1 : j - 1;
624 if (dp->oldcell[k + ml] ==
625 (int) (dp->oldcell[i + mj] + 1) % dp->states)
626 dp->newcell[i + mj] = dp->oldcell[k + ml];
628 k = (i + 1 == dp->ncols) ? 0 : i + 1;
630 if (dp->oldcell[k + ml] ==
631 (int) (dp->oldcell[i + mj] + 1) % dp->states)
632 dp->newcell[i + mj] = dp->oldcell[k + ml];
635 k = (i + 1 == dp->ncols) ? 0 : i + 1;
638 l = (j + 1 == dp->nrows) ? 0 : j + 1;
640 if (dp->oldcell[k + ml] ==
641 (int) (dp->oldcell[i + mj] + 1) % dp->states)
642 dp->newcell[i + mj] = dp->oldcell[k + ml];
645 k = (!i) ? dp->ncols - 1 : i - 1;
648 l = (j + 1 == dp->nrows) ? 0 : j + 1;
650 if (dp->oldcell[k + ml] ==
651 (int) (dp->oldcell[i + mj] + 1) % dp->states)
652 dp->newcell[i + mj] = dp->oldcell[k + ml];
654 k = (!i) ? dp->ncols - 1 : i - 1;
656 if (dp->oldcell[k + ml] ==
657 (int) (dp->oldcell[i + mj] + 1) % dp->states)
658 dp->newcell[i + mj] = dp->oldcell[k + ml];
661 k = (!i) ? dp->ncols - 1 : i - 1;
664 l = (!j) ? dp->nrows - 1 : j - 1;
666 if (dp->oldcell[k + ml] ==
667 (int) (dp->oldcell[i + mj] + 1) % dp->states)
668 dp->newcell[i + mj] = dp->oldcell[k + ml];
672 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
673 for (j = 0; j < dp->nrows; j++) {
674 for (i = 0; i < dp->ncols; i++) {
677 l = (!j) ? dp->nrows - 1 : j - 1;
679 if (dp->oldcell[k + ml] ==
680 (int) (dp->oldcell[i + mj] + 1) % dp->states)
681 dp->newcell[i + mj] = dp->oldcell[k + ml];
683 k = (i + 1 == dp->ncols) ? 0 : i + 1;
685 if (dp->oldcell[k + ml] ==
686 (int) (dp->oldcell[i + mj] + 1) % dp->states)
687 dp->newcell[i + mj] = dp->oldcell[k + ml];
690 l = (j + 1 == dp->nrows) ? 0 : j + 1;
692 if (dp->oldcell[k + ml] ==
693 (int) (dp->oldcell[i + mj] + 1) % dp->states)
694 dp->newcell[i + mj] = dp->oldcell[k + ml];
696 k = (!i) ? dp->ncols - 1 : i - 1;
699 if (dp->oldcell[k + ml] ==
700 (int) (dp->oldcell[i + mj] + 1) % dp->states)
701 dp->newcell[i + mj] = dp->oldcell[k + ml];
705 if (dp->neighbors == 8) {
707 for (j = 0; j < dp->nrows; j++) {
708 for (i = 0; i < dp->ncols; i++) {
710 k = (i + 1 == dp->ncols) ? 0 : i + 1;
711 l = (!j) ? dp->nrows - 1 : j - 1;
713 if (dp->oldcell[k + ml] ==
714 (int) (dp->oldcell[i + mj] + 1) % dp->states)
715 dp->newcell[i + mj] = dp->oldcell[k + ml];
717 k = (i + 1 == dp->ncols) ? 0 : i + 1;
718 l = (j + 1 == dp->nrows) ? 0 : j + 1;
720 if (dp->oldcell[k + ml] ==
721 (int) (dp->oldcell[i + mj] + 1) % dp->states)
722 dp->newcell[i + mj] = dp->oldcell[k + ml];
724 k = (!i) ? dp->ncols - 1 : i - 1;
725 l = (j + 1 == dp->nrows) ? 0 : j + 1;
727 if (dp->oldcell[k + ml] ==
728 (int) (dp->oldcell[i + mj] + 1) % dp->states)
729 dp->newcell[i + mj] = dp->oldcell[k + ml];
731 k = (!i) ? dp->ncols - 1 : i - 1;
732 l = (!j) ? dp->nrows - 1 : j - 1;
734 if (dp->oldcell[k + ml] ==
735 (int) (dp->oldcell[i + mj] + 1) % dp->states)
736 dp->newcell[i + mj] = dp->oldcell[k + ml];
741 } else if (dp->neighbors == 3 || dp->neighbors == 9 ||
742 dp->neighbors == 12) {
743 for (j = 0; j < dp->nrows; j++) {
744 for (i = 0; i < dp->ncols; i++) {
745 if ((i + j) % 2) { /* right */
747 k = (!i) ? dp->ncols - 1 : i - 1;
749 if (dp->oldcell[k + ml] ==
750 (int) (dp->oldcell[i + mj] + 1) % dp->states)
751 dp->newcell[i + mj] = dp->oldcell[k + ml];
754 k = (i + 1 == dp->ncols) ? 0 : i + 1;
756 if (dp->oldcell[k + ml] ==
757 (int) (dp->oldcell[i + mj] + 1) % dp->states)
758 dp->newcell[i + mj] = dp->oldcell[k + ml];
762 l = (!j) ? dp->nrows - 1 : j - 1;
764 if (dp->oldcell[k + ml] ==
765 (int) (dp->oldcell[i + mj] + 1) % dp->states)
766 dp->newcell[i + mj] = dp->oldcell[k + ml];
769 l = (j + 1 == dp->nrows) ? 0 : j + 1;
771 if (dp->oldcell[k + ml] ==
772 (int) (dp->oldcell[i + mj] + 1) % dp->states)
773 dp->newcell[i + mj] = dp->oldcell[k + ml];
777 if (dp->neighbors == 9 || dp->neighbors == 12) {
779 for (j = 0; j < dp->nrows; j++) {
780 for (i = 0; i < dp->ncols; i++) {
790 if (dp->oldcell[k + ml] ==
791 (int) (dp->oldcell[i + mj] + 1) % dp->states)
792 dp->newcell[i + mj] = dp->oldcell[k + ml];
795 if (j + 1 == dp->nrows)
797 else if (j + 2 == dp->nrows)
802 if (dp->oldcell[k + ml] ==
803 (int) (dp->oldcell[i + mj] + 1) % dp->states)
804 dp->newcell[i + mj] = dp->oldcell[k + ml];
806 k = (!i) ? dp->ncols - 1 : i - 1;
807 l = (!j) ? dp->nrows - 1 : j - 1;
809 if (dp->oldcell[k + ml] ==
810 (int) (dp->oldcell[i + mj] + 1) % dp->states)
811 dp->newcell[i + mj] = dp->oldcell[k + ml];
813 k = (i + 1 == dp->ncols) ? 0 : i + 1;
814 l = (!j) ? dp->nrows - 1 : j - 1;
816 if (dp->oldcell[k + ml] ==
817 (int) (dp->oldcell[i + mj] + 1) % dp->states)
818 dp->newcell[i + mj] = dp->oldcell[k + ml];
820 k = (!i) ? dp->ncols - 1 : i - 1;
821 l = (j + 1 == dp->nrows) ? 0 : j + 1;
823 if (dp->oldcell[k + ml] ==
824 (int) (dp->oldcell[i + mj] + 1) % dp->states)
825 dp->newcell[i + mj] = dp->oldcell[k + ml];
827 k = (i + 1 == dp->ncols) ? 0 : i + 1;
828 l = (j + 1 == dp->nrows) ? 0 : j + 1;
830 if (dp->oldcell[k + ml] ==
831 (int) (dp->oldcell[i + mj] + 1) % dp->states)
832 dp->newcell[i + mj] = dp->oldcell[k + ml];
836 if (dp->neighbors == 12) {
838 for (j = 0; j < dp->nrows; j++) {
839 for (i = 0; i < dp->ncols; i++) {
840 if ((i + j) % 2) { /* right */
842 k = (!i) ? dp->ncols - 1 : i - 1;
850 if (dp->oldcell[k + ml] ==
851 (int) (dp->oldcell[i + mj] + 1) % dp->states)
852 dp->newcell[i + mj] = dp->oldcell[k + ml];
854 k = (!i) ? dp->ncols - 1 : i - 1;
855 if (j + 1 == dp->nrows)
857 else if (j + 2 == dp->nrows)
862 if (dp->oldcell[k + ml] ==
863 (int) (dp->oldcell[i + mj] + 1) % dp->states)
864 dp->newcell[i + mj] = dp->oldcell[k + ml];
866 k = (i + 1 == dp->ncols) ? 0 : i + 1;
869 if (dp->oldcell[k + ml] ==
870 (int) (dp->oldcell[i + mj] + 1) % dp->states)
871 dp->newcell[i + mj] = dp->oldcell[k + ml];
874 k = (i + 1 == dp->ncols) ? 0 : i + 1;
882 if (dp->oldcell[k + ml] ==
883 (int) (dp->oldcell[i + mj] + 1) % dp->states)
884 dp->newcell[i + mj] = dp->oldcell[k + ml];
886 k = (i + 1 == dp->ncols) ? 0 : i + 1;
887 if (j + 1 == dp->nrows)
889 else if (j + 2 == dp->nrows)
894 if (dp->oldcell[k + ml] ==
895 (int) (dp->oldcell[i + mj] + 1) % dp->states)
896 dp->newcell[i + mj] = dp->oldcell[k + ml];
898 k = (!i) ? dp->ncols - 1 : i - 1;
901 if (dp->oldcell[k + ml] ==
902 (int) (dp->oldcell[i + mj] + 1) % dp->states)
903 dp->newcell[i + mj] = dp->oldcell[k + ml];
912 for (j = 0; j < dp->nrows; j++) {
913 for (i = 0; i < dp->ncols; i++)
914 if (dp->oldcell[i + mj] != dp->newcell[i + mj]) {
915 dp->oldcell[i + mj] = dp->newcell[i + mj];
916 if (!addtolist(mi, i, j, dp->oldcell[i + mj])) {
917 free_demon(MI_DISPLAY(mi), dp);
923 if (++dp->generation > MI_CYCLES(mi))
927 if (dp->ncells[dp->state])
928 if (!draw_state(mi, dp->state)) {
929 free_demon(MI_DISPLAY(mi), dp);
935 for (i = 0; i < REDRAWSTEP; i++) {
936 if (dp->oldcell[dp->redrawpos]) {
937 drawcell(mi, dp->redrawpos % dp->ncols, dp->redrawpos / dp->ncols,
938 dp->oldcell[dp->redrawpos]);
940 if (++(dp->redrawpos) >= dp->ncols * dp->nrows) {
950 reshape_demon(ModeInfo * mi, int width, int height)
952 XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
958 release_demon (ModeInfo * mi)
960 if (demons != NULL) {
963 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
964 free_demon(MI_DISPLAY(mi), &demons[screen]);
965 (void) free((void *) demons);
966 demons = (demonstruct *) NULL;
971 refresh_demon (ModeInfo * mi)
977 dp = &demons[MI_SCREEN(mi)];
983 XSCREENSAVER_MODULE ("Demon", demon)
985 #endif /* MODE_demon */