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 reshape_demon 0
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 */
74 * neighbors of 0 randomizes it between 3, 4, 6, 8, 9, and 12.
76 #define DEF_NEIGHBORS "0" /* choose random value */
80 static XrmOptionDescRec opts[] =
82 {"-neighbors", ".demon.neighbors", XrmoptionSepArg, 0}
85 static argtype vars[] =
87 {&neighbors, "neighbors", "Neighbors", DEF_NEIGHBORS, t_Int}
89 static OptionStruct desc[] =
91 {"-neighbors num", "squares 4 or 8, hexagons 6, triangles 3, 9 or 12"}
94 ENTRYPOINT ModeSpecOpt demon_opts =
95 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
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};
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++;}
111 #define REDRAWSTEP 2000 /* How many cells to draw per cycle */
113 #define MINGRIDSIZE 24
115 #define NEIGHBORKINDS 6
117 /* Singly linked list */
118 typedef struct _CellList {
120 struct _CellList *next;
131 int redrawing, redrawpos;
134 unsigned char *oldcell, *newcell;
138 Pixmap pixmaps[NUMSTIPPLES - 1];
141 XPoint triangle[2][3];
145 static char plots[2][NEIGHBORKINDS] =
147 {3, 4, 6, 8, 9, 12}, /* Neighborhoods */
148 {12, 16, 18, 20, 22, 24} /* Number of states */
151 static demonstruct *demons = (demonstruct *) NULL;
154 drawcell(ModeInfo * mi, int col, int row, unsigned char state)
156 demonstruct *dp = &demons[MI_SCREEN(mi)];
160 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(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)));
171 gcv.stipple = dp->pixmaps[(state - 1) % (NUMSTIPPLES - 1)];
172 #endif /* DO_STIPPLE */
173 gcv.foreground = MI_WHITE_PIXEL(mi);
174 gcv.background = MI_BLACK_PIXEL(mi);
175 XChangeGC(MI_DISPLAY(mi), dp->stippledGC,
176 GCStipple | GCForeground | GCBackground, &gcv);
179 if (dp->neighbors == 6) {
180 int ccol = 2 * col + !(row & 1), crow = 2 * row;
182 dp->shape.hexagon[0].x = dp->xb + ccol * dp->xs;
183 dp->shape.hexagon[0].y = dp->yb + crow * dp->ys;
184 if (dp->xs == 1 && dp->ys == 1)
185 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi),
186 gc, dp->shape.hexagon[0].x, dp->shape.hexagon[0].y);
188 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
189 dp->shape.hexagon, 6, Convex, CoordModePrevious);
190 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
191 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
192 dp->xb + dp->xs * col, dp->yb + dp->ys * row,
193 dp->xs - (dp->xs > 3), dp->ys - (dp->ys > 3));
195 int orient = (col + row) % 2; /* O left 1 right */
197 dp->shape.triangle[orient][0].x = dp->xb + col * dp->xs;
198 dp->shape.triangle[orient][0].y = dp->yb + row * dp->ys;
199 if (dp->xs <= 3 || dp->ys <= 3)
200 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
201 ((orient) ? -1 : 1) + dp->shape.triangle[orient][0].x,
202 dp->shape.triangle[orient][0].y);
205 dp->shape.triangle[orient][0].x += (dp->xs / 2 - 1);
207 dp->shape.triangle[orient][0].x -= (dp->xs / 2 - 1);
208 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
209 dp->shape.triangle[orient], 3, Convex, CoordModePrevious);
216 addtolist(ModeInfo * mi, int col, int row, unsigned char state)
218 demonstruct *dp = &demons[MI_SCREEN(mi)];
221 current = dp->cellList[state];
222 if ((dp->cellList[state] = (CellList *)
223 malloc(sizeof (CellList))) == NULL) {
226 dp->cellList[state]->pt.x = col;
227 dp->cellList[state]->pt.y = row;
228 dp->cellList[state]->next = current;
235 print_state(ModeInfo * mi, int state)
237 demonstruct *dp = &demons[MI_SCREEN(mi)];
241 locallist = dp->cellList[state];
242 (void) printf("state %d\n", state);
244 (void) printf("%d x %d, y %d\n", i,
245 locallist->pt.x, locallist->pt.y);
246 locallist = locallist->next;
254 free_state(demonstruct * dp, int state)
258 while (dp->cellList[state]) {
259 current = dp->cellList[state];
260 dp->cellList[state] = dp->cellList[state]->next;
261 (void) free((void *) current);
263 dp->cellList[state] = (CellList *) NULL;
264 if (dp->ncells != NULL)
265 dp->ncells[state] = 0;
270 free_list(demonstruct * dp)
274 for (state = 0; state < dp->states; state++)
275 free_state(dp, state);
276 (void) free((void *) dp->cellList);
277 dp->cellList = (CellList **) NULL;
281 free_struct(demonstruct * dp)
283 if (dp->cellList != NULL) {
286 if (dp->ncells != NULL) {
287 (void) free((void *) dp->ncells);
288 dp->ncells = (int *) NULL;
290 if (dp->oldcell != NULL) {
291 (void) free((void *) dp->oldcell);
292 dp->oldcell = (unsigned char *) NULL;
294 if (dp->newcell != NULL) {
295 (void) free((void *) dp->newcell);
296 dp->newcell = (unsigned char *) NULL;
301 free_demon(Display *display, demonstruct *dp)
305 if (dp->stippledGC != None) {
306 XFreeGC(display, dp->stippledGC);
307 dp->stippledGC = None;
309 for (shade = 0; shade < dp->init_bits; shade++) {
310 XFreePixmap(display, dp->pixmaps[shade]);
317 draw_state(ModeInfo * mi, int state)
319 demonstruct *dp = &demons[MI_SCREEN(mi)];
325 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_BLACK_PIXEL(mi));
327 } else if (MI_NPIXELS(mi) >= NUMSTIPPLES) {
328 XSetForeground(MI_DISPLAY(mi), MI_GC(mi),
329 MI_PIXEL(mi, (((int) state - 1) * MI_NPIXELS(mi) /
330 (dp->states - 1)) % MI_NPIXELS(mi)));
336 gcv.stipple = dp->pixmaps[(state - 1) % (NUMSTIPPLES - 1)];
337 #endif /* DO_STIPPLE */
338 gcv.foreground = MI_WHITE_PIXEL(mi);
339 gcv.background = MI_BLACK_PIXEL(mi);
340 XChangeGC(MI_DISPLAY(mi), dp->stippledGC,
341 GCStipple | GCForeground | GCBackground, &gcv);
344 if (dp->neighbors == 6) { /* Draw right away, slow */
345 current = dp->cellList[state];
347 int col, row, ccol, crow;
351 ccol = 2 * col + !(row & 1), crow = 2 * row;
352 dp->shape.hexagon[0].x = dp->xb + ccol * dp->xs;
353 dp->shape.hexagon[0].y = dp->yb + crow * dp->ys;
354 if (dp->xs == 1 && dp->ys == 1)
355 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi),
356 gc, dp->shape.hexagon[0].x, dp->shape.hexagon[0].y);
358 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
359 dp->shape.hexagon, 6, Convex, CoordModePrevious);
360 current = current->next;
362 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
363 /* Take advantage of XDrawRectangles */
366 /* Create Rectangle list from part of the cellList */
367 if ((rects = (XRectangle *) malloc(dp->ncells[state] *
368 sizeof (XRectangle))) == NULL) {
371 current = dp->cellList[state];
373 rects[ncells].x = dp->xb + current->pt.x * dp->xs;
374 rects[ncells].y = dp->yb + current->pt.y * dp->ys;
375 rects[ncells].width = dp->xs - (dp->xs > 3);
376 rects[ncells].height = dp->ys - (dp->ys > 3);
377 current = current->next;
380 /* Finally get to draw */
381 XFillRectangles(MI_DISPLAY(mi), MI_WINDOW(mi), gc, rects, ncells);
382 /* Free up rects list and the appropriate part of the cellList */
383 (void) free((void *) rects);
385 current = dp->cellList[state];
387 int col, row, orient;
391 orient = (col + row) % 2; /* O left 1 right */
392 dp->shape.triangle[orient][0].x = dp->xb + col * dp->xs;
393 dp->shape.triangle[orient][0].y = dp->yb + row * dp->ys;
394 if (dp->xs <= 3 || dp->ys <= 3)
395 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
396 ((orient) ? -1 : 1) + dp->shape.triangle[orient][0].x,
397 dp->shape.triangle[orient][0].y);
400 dp->shape.triangle[orient][0].x += (dp->xs / 2 - 1);
402 dp->shape.triangle[orient][0].x -= (dp->xs / 2 - 1);
403 XFillPolygon(MI_DISPLAY(mi), MI_WINDOW(mi), gc,
404 dp->shape.triangle[orient], 3, Convex, CoordModePrevious);
406 current = current->next;
409 free_state(dp, state);
414 RandomSoup(ModeInfo * mi)
416 demonstruct *dp = &demons[MI_SCREEN(mi)];
417 int row, col, mrow = 0;
419 for (row = 0; row < dp->nrows; ++row) {
420 for (col = 0; col < dp->ncols; ++col) {
421 dp->oldcell[col + mrow] =
422 (unsigned char) LRAND() % ((unsigned char) dp->states);
423 if (!addtolist(mi, col, row, dp->oldcell[col + mrow]))
424 return; /* sparse soup */
431 init_demon (ModeInfo * mi)
433 Display *display = MI_DISPLAY(mi);
434 int size = MI_SIZE(mi), nk;
437 if (demons == NULL) {
438 if ((demons = (demonstruct *) calloc(MI_NUM_SCREENS(mi),
439 sizeof (demonstruct))) == NULL)
442 dp = &demons[MI_SCREEN(mi)];
447 if (MI_NPIXELS(mi) < NUMSTIPPLES) {
448 Window window = MI_WINDOW(mi);
449 if (dp->stippledGC == None) {
452 gcv.fill_style = FillOpaqueStippled;
453 if ((dp->stippledGC = XCreateGC(display, window,
454 GCFillStyle, &gcv)) == None) {
455 free_demon(display, dp);
459 if (dp->init_bits == 0) {
462 for (i = 1; i < NUMSTIPPLES; i++) {
463 DEMONBITS(stipples[i], STIPPLESIZE, STIPPLESIZE);
467 #endif /* DO_STIPPLE */
470 for (nk = 0; nk < NEIGHBORKINDS; nk++) {
471 if (neighbors == plots[0][nk]) {
472 dp->neighbors = plots[0][nk];
475 if (nk == NEIGHBORKINDS - 1) {
476 nk = NRAND(NEIGHBORKINDS);
477 dp->neighbors = plots[0][nk];
482 dp->states = MI_COUNT(mi);
483 if (dp->states < -MINSTATES)
484 dp->states = NRAND(-dp->states - MINSTATES + 1) + MINSTATES;
485 else if (dp->states < MINSTATES)
486 dp->states = plots[1][nk];
487 if ((dp->cellList = (CellList **) calloc(dp->states,
488 sizeof (CellList *))) == NULL) {
489 free_demon(display, dp);
492 if ((dp->ncells = (int *) calloc(dp->states, sizeof (int))) == NULL) {
493 free_demon(display, dp);
499 dp->width = MI_WIDTH(mi);
500 dp->height = MI_HEIGHT(mi);
502 if (dp->neighbors == 6) {
503 int nccols, ncrows, i;
510 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
511 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
512 else if (size < MINSIZE) {
514 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
518 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
521 nccols = MAX(dp->width / dp->xs - 2, 2);
522 ncrows = MAX(dp->height / dp->ys - 1, 4);
523 dp->ncols = nccols / 2;
524 dp->nrows = 2 * (ncrows / 4);
525 dp->xb = (dp->width - dp->xs * nccols) / 2 + dp->xs / 2;
526 dp->yb = (dp->height - dp->ys * (ncrows / 2) * 2) / 2 + dp->ys - 2;
527 for (i = 0; i < 6; i++) {
528 dp->shape.hexagon[i].x = (dp->xs - 1) * hexagonUnit[i].x;
529 dp->shape.hexagon[i].y = ((dp->ys - 1) * hexagonUnit[i].y / 2) * 4 / 3;
531 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
533 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
534 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
535 else if (size < MINSIZE) {
537 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
541 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
544 dp->ncols = MAX(dp->width / dp->xs, 2);
545 dp->nrows = MAX(dp->height / dp->ys, 2);
546 dp->xb = (dp->width - dp->xs * dp->ncols) / 2;
547 dp->yb = (dp->height - dp->ys * dp->nrows) / 2;
556 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
557 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
558 else if (size < MINSIZE) {
560 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
564 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
566 dp->xs = (int) (1.52 * dp->ys);
567 dp->ncols = (MAX(dp->width / dp->xs - 1, 2) / 2) * 2;
568 dp->nrows = (MAX(dp->height / dp->ys - 1, 2) / 2) * 2;
569 dp->xb = (dp->width - dp->xs * dp->ncols) / 2 + dp->xs / 2;
570 dp->yb = (dp->height - dp->ys * dp->nrows) / 2 + dp->ys / 2;
571 for (orient = 0; orient < 2; orient++) {
572 for (i = 0; i < 3; i++) {
573 dp->shape.triangle[orient][i].x =
574 (dp->xs - 2) * triangleUnit[orient][i].x;
575 dp->shape.triangle[orient][i].y =
576 (dp->ys - 2) * triangleUnit[orient][i].y;
583 if ((dp->oldcell = (unsigned char *)
584 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
585 free_demon(display, dp);
589 if ((dp->newcell = (unsigned char *)
590 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
591 free_demon(display, dp);
599 draw_demon (ModeInfo * mi)
601 int i, j, k, l, mj = 0, ml;
606 dp = &demons[MI_SCREEN(mi)];
607 if (dp->cellList == NULL)
610 MI_IS_DRAWN(mi) = True;
611 if (dp->state >= dp->states) {
612 (void) memcpy((char *) dp->newcell, (char *) dp->oldcell,
613 dp->ncols * dp->nrows * sizeof (unsigned char));
615 if (dp->neighbors == 6) {
616 for (j = 0; j < dp->nrows; j++) {
617 for (i = 0; i < dp->ncols; i++) {
620 k = (i + 1 == dp->ncols) ? 0 : i + 1;
623 l = (!j) ? dp->nrows - 1 : j - 1;
625 if (dp->oldcell[k + ml] ==
626 (int) (dp->oldcell[i + mj] + 1) % dp->states)
627 dp->newcell[i + mj] = dp->oldcell[k + ml];
629 k = (i + 1 == dp->ncols) ? 0 : i + 1;
631 if (dp->oldcell[k + ml] ==
632 (int) (dp->oldcell[i + mj] + 1) % dp->states)
633 dp->newcell[i + mj] = dp->oldcell[k + ml];
636 k = (i + 1 == dp->ncols) ? 0 : i + 1;
639 l = (j + 1 == dp->nrows) ? 0 : j + 1;
641 if (dp->oldcell[k + ml] ==
642 (int) (dp->oldcell[i + mj] + 1) % dp->states)
643 dp->newcell[i + mj] = dp->oldcell[k + ml];
646 k = (!i) ? dp->ncols - 1 : i - 1;
649 l = (j + 1 == dp->nrows) ? 0 : j + 1;
651 if (dp->oldcell[k + ml] ==
652 (int) (dp->oldcell[i + mj] + 1) % dp->states)
653 dp->newcell[i + mj] = dp->oldcell[k + ml];
655 k = (!i) ? dp->ncols - 1 : i - 1;
657 if (dp->oldcell[k + ml] ==
658 (int) (dp->oldcell[i + mj] + 1) % dp->states)
659 dp->newcell[i + mj] = dp->oldcell[k + ml];
662 k = (!i) ? dp->ncols - 1 : i - 1;
665 l = (!j) ? dp->nrows - 1 : j - 1;
667 if (dp->oldcell[k + ml] ==
668 (int) (dp->oldcell[i + mj] + 1) % dp->states)
669 dp->newcell[i + mj] = dp->oldcell[k + ml];
673 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
674 for (j = 0; j < dp->nrows; j++) {
675 for (i = 0; i < dp->ncols; i++) {
678 l = (!j) ? dp->nrows - 1 : j - 1;
680 if (dp->oldcell[k + ml] ==
681 (int) (dp->oldcell[i + mj] + 1) % dp->states)
682 dp->newcell[i + mj] = dp->oldcell[k + ml];
684 k = (i + 1 == dp->ncols) ? 0 : i + 1;
686 if (dp->oldcell[k + ml] ==
687 (int) (dp->oldcell[i + mj] + 1) % dp->states)
688 dp->newcell[i + mj] = dp->oldcell[k + ml];
691 l = (j + 1 == dp->nrows) ? 0 : j + 1;
693 if (dp->oldcell[k + ml] ==
694 (int) (dp->oldcell[i + mj] + 1) % dp->states)
695 dp->newcell[i + mj] = dp->oldcell[k + ml];
697 k = (!i) ? dp->ncols - 1 : i - 1;
700 if (dp->oldcell[k + ml] ==
701 (int) (dp->oldcell[i + mj] + 1) % dp->states)
702 dp->newcell[i + mj] = dp->oldcell[k + ml];
706 if (dp->neighbors == 8) {
708 for (j = 0; j < dp->nrows; j++) {
709 for (i = 0; i < dp->ncols; i++) {
711 k = (i + 1 == dp->ncols) ? 0 : i + 1;
712 l = (!j) ? dp->nrows - 1 : j - 1;
714 if (dp->oldcell[k + ml] ==
715 (int) (dp->oldcell[i + mj] + 1) % dp->states)
716 dp->newcell[i + mj] = dp->oldcell[k + ml];
718 k = (i + 1 == dp->ncols) ? 0 : i + 1;
719 l = (j + 1 == dp->nrows) ? 0 : j + 1;
721 if (dp->oldcell[k + ml] ==
722 (int) (dp->oldcell[i + mj] + 1) % dp->states)
723 dp->newcell[i + mj] = dp->oldcell[k + ml];
725 k = (!i) ? dp->ncols - 1 : i - 1;
726 l = (j + 1 == dp->nrows) ? 0 : j + 1;
728 if (dp->oldcell[k + ml] ==
729 (int) (dp->oldcell[i + mj] + 1) % dp->states)
730 dp->newcell[i + mj] = dp->oldcell[k + ml];
732 k = (!i) ? dp->ncols - 1 : i - 1;
733 l = (!j) ? dp->nrows - 1 : j - 1;
735 if (dp->oldcell[k + ml] ==
736 (int) (dp->oldcell[i + mj] + 1) % dp->states)
737 dp->newcell[i + mj] = dp->oldcell[k + ml];
742 } else if (dp->neighbors == 3 || dp->neighbors == 9 ||
743 dp->neighbors == 12) {
744 for (j = 0; j < dp->nrows; j++) {
745 for (i = 0; i < dp->ncols; i++) {
746 if ((i + j) % 2) { /* right */
748 k = (!i) ? dp->ncols - 1 : i - 1;
750 if (dp->oldcell[k + ml] ==
751 (int) (dp->oldcell[i + mj] + 1) % dp->states)
752 dp->newcell[i + mj] = dp->oldcell[k + ml];
755 k = (i + 1 == dp->ncols) ? 0 : i + 1;
757 if (dp->oldcell[k + ml] ==
758 (int) (dp->oldcell[i + mj] + 1) % dp->states)
759 dp->newcell[i + mj] = dp->oldcell[k + ml];
763 l = (!j) ? dp->nrows - 1 : j - 1;
765 if (dp->oldcell[k + ml] ==
766 (int) (dp->oldcell[i + mj] + 1) % dp->states)
767 dp->newcell[i + mj] = dp->oldcell[k + ml];
770 l = (j + 1 == dp->nrows) ? 0 : j + 1;
772 if (dp->oldcell[k + ml] ==
773 (int) (dp->oldcell[i + mj] + 1) % dp->states)
774 dp->newcell[i + mj] = dp->oldcell[k + ml];
778 if (dp->neighbors == 9 || dp->neighbors == 12) {
780 for (j = 0; j < dp->nrows; j++) {
781 for (i = 0; i < dp->ncols; i++) {
791 if (dp->oldcell[k + ml] ==
792 (int) (dp->oldcell[i + mj] + 1) % dp->states)
793 dp->newcell[i + mj] = dp->oldcell[k + ml];
796 if (j + 1 == dp->nrows)
798 else if (j + 2 == dp->nrows)
803 if (dp->oldcell[k + ml] ==
804 (int) (dp->oldcell[i + mj] + 1) % dp->states)
805 dp->newcell[i + mj] = dp->oldcell[k + ml];
807 k = (!i) ? dp->ncols - 1 : i - 1;
808 l = (!j) ? dp->nrows - 1 : j - 1;
810 if (dp->oldcell[k + ml] ==
811 (int) (dp->oldcell[i + mj] + 1) % dp->states)
812 dp->newcell[i + mj] = dp->oldcell[k + ml];
814 k = (i + 1 == dp->ncols) ? 0 : i + 1;
815 l = (!j) ? dp->nrows - 1 : j - 1;
817 if (dp->oldcell[k + ml] ==
818 (int) (dp->oldcell[i + mj] + 1) % dp->states)
819 dp->newcell[i + mj] = dp->oldcell[k + ml];
821 k = (!i) ? dp->ncols - 1 : i - 1;
822 l = (j + 1 == dp->nrows) ? 0 : j + 1;
824 if (dp->oldcell[k + ml] ==
825 (int) (dp->oldcell[i + mj] + 1) % dp->states)
826 dp->newcell[i + mj] = dp->oldcell[k + ml];
828 k = (i + 1 == dp->ncols) ? 0 : i + 1;
829 l = (j + 1 == dp->nrows) ? 0 : j + 1;
831 if (dp->oldcell[k + ml] ==
832 (int) (dp->oldcell[i + mj] + 1) % dp->states)
833 dp->newcell[i + mj] = dp->oldcell[k + ml];
837 if (dp->neighbors == 12) {
839 for (j = 0; j < dp->nrows; j++) {
840 for (i = 0; i < dp->ncols; i++) {
841 if ((i + j) % 2) { /* right */
843 k = (!i) ? dp->ncols - 1 : i - 1;
851 if (dp->oldcell[k + ml] ==
852 (int) (dp->oldcell[i + mj] + 1) % dp->states)
853 dp->newcell[i + mj] = dp->oldcell[k + ml];
855 k = (!i) ? dp->ncols - 1 : i - 1;
856 if (j + 1 == dp->nrows)
858 else if (j + 2 == dp->nrows)
863 if (dp->oldcell[k + ml] ==
864 (int) (dp->oldcell[i + mj] + 1) % dp->states)
865 dp->newcell[i + mj] = dp->oldcell[k + ml];
867 k = (i + 1 == dp->ncols) ? 0 : i + 1;
870 if (dp->oldcell[k + ml] ==
871 (int) (dp->oldcell[i + mj] + 1) % dp->states)
872 dp->newcell[i + mj] = dp->oldcell[k + ml];
875 k = (i + 1 == dp->ncols) ? 0 : i + 1;
883 if (dp->oldcell[k + ml] ==
884 (int) (dp->oldcell[i + mj] + 1) % dp->states)
885 dp->newcell[i + mj] = dp->oldcell[k + ml];
887 k = (i + 1 == dp->ncols) ? 0 : i + 1;
888 if (j + 1 == dp->nrows)
890 else if (j + 2 == dp->nrows)
895 if (dp->oldcell[k + ml] ==
896 (int) (dp->oldcell[i + mj] + 1) % dp->states)
897 dp->newcell[i + mj] = dp->oldcell[k + ml];
899 k = (!i) ? dp->ncols - 1 : i - 1;
902 if (dp->oldcell[k + ml] ==
903 (int) (dp->oldcell[i + mj] + 1) % dp->states)
904 dp->newcell[i + mj] = dp->oldcell[k + ml];
913 for (j = 0; j < dp->nrows; j++) {
914 for (i = 0; i < dp->ncols; i++)
915 if (dp->oldcell[i + mj] != dp->newcell[i + mj]) {
916 dp->oldcell[i + mj] = dp->newcell[i + mj];
917 if (!addtolist(mi, i, j, dp->oldcell[i + mj])) {
918 free_demon(MI_DISPLAY(mi), dp);
924 if (++dp->generation > MI_CYCLES(mi))
928 if (dp->ncells[dp->state])
929 if (!draw_state(mi, dp->state)) {
930 free_demon(MI_DISPLAY(mi), dp);
936 for (i = 0; i < REDRAWSTEP; i++) {
937 if (dp->oldcell[dp->redrawpos]) {
938 drawcell(mi, dp->redrawpos % dp->ncols, dp->redrawpos / dp->ncols,
939 dp->oldcell[dp->redrawpos]);
941 if (++(dp->redrawpos) >= dp->ncols * dp->nrows) {
951 release_demon (ModeInfo * mi)
953 if (demons != NULL) {
956 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
957 free_demon(MI_DISPLAY(mi), &demons[screen]);
958 (void) free((void *) demons);
959 demons = (demonstruct *) NULL;
964 refresh_demon (ModeInfo * mi)
970 dp = &demons[MI_SCREEN(mi)];
976 XSCREENSAVER_MODULE ("Demon", demon)
978 #endif /* MODE_demon */