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" \
61 "*ignoreRotation: True \n" \
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)));
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 */
470 jwxyz_XSetAntiAliasing (MI_DISPLAY(mi), MI_GC(mi), False);
473 for (nk = 0; nk < NEIGHBORKINDS; nk++) {
474 if (neighbors == plots[0][nk]) {
475 dp->neighbors = plots[0][nk];
478 if (nk == NEIGHBORKINDS - 1) {
479 nk = NRAND(NEIGHBORKINDS);
480 dp->neighbors = plots[0][nk];
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);
495 if ((dp->ncells = (int *) calloc(dp->states, sizeof (int))) == NULL) {
496 free_demon(display, dp);
502 dp->width = MI_WIDTH(mi);
503 dp->height = MI_HEIGHT(mi);
505 if (dp->neighbors == 6) {
506 int nccols, ncrows, i;
513 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
514 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
515 else if (size < MINSIZE) {
517 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
521 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
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;
534 } else if (dp->neighbors == 4 || dp->neighbors == 8) {
536 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
537 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
538 else if (size < MINSIZE) {
540 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
544 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
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;
559 dp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(dp->width, dp->height) /
560 MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
561 else if (size < MINSIZE) {
563 dp->ys = MAX(MINSIZE, MIN(dp->width, dp->height) / MINGRIDSIZE);
567 dp->ys = MIN(size, MAX(MINSIZE, MIN(dp->width, dp->height) /
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;
586 if ((dp->oldcell = (unsigned char *)
587 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
588 free_demon(display, dp);
592 if ((dp->newcell = (unsigned char *)
593 malloc(dp->ncols * dp->nrows * sizeof (unsigned char))) == NULL) {
594 free_demon(display, dp);
602 draw_demon (ModeInfo * mi)
604 int i, j, k, l, mj = 0, ml;
609 dp = &demons[MI_SCREEN(mi)];
610 if (dp->cellList == NULL)
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));
618 if (dp->neighbors == 6) {
619 for (j = 0; j < dp->nrows; j++) {
620 for (i = 0; i < dp->ncols; i++) {
623 k = (i + 1 == dp->ncols) ? 0 : i + 1;
626 l = (!j) ? dp->nrows - 1 : j - 1;
628 if (dp->oldcell[k + ml] ==
629 (int) (dp->oldcell[i + mj] + 1) % dp->states)
630 dp->newcell[i + mj] = dp->oldcell[k + ml];
632 k = (i + 1 == dp->ncols) ? 0 : i + 1;
634 if (dp->oldcell[k + ml] ==
635 (int) (dp->oldcell[i + mj] + 1) % dp->states)
636 dp->newcell[i + mj] = dp->oldcell[k + ml];
639 k = (i + 1 == dp->ncols) ? 0 : i + 1;
642 l = (j + 1 == dp->nrows) ? 0 : j + 1;
644 if (dp->oldcell[k + ml] ==
645 (int) (dp->oldcell[i + mj] + 1) % dp->states)
646 dp->newcell[i + mj] = dp->oldcell[k + ml];
649 k = (!i) ? dp->ncols - 1 : i - 1;
652 l = (j + 1 == dp->nrows) ? 0 : j + 1;
654 if (dp->oldcell[k + ml] ==
655 (int) (dp->oldcell[i + mj] + 1) % dp->states)
656 dp->newcell[i + mj] = dp->oldcell[k + ml];
658 k = (!i) ? dp->ncols - 1 : i - 1;
660 if (dp->oldcell[k + ml] ==
661 (int) (dp->oldcell[i + mj] + 1) % dp->states)
662 dp->newcell[i + mj] = dp->oldcell[k + ml];
665 k = (!i) ? dp->ncols - 1 : i - 1;
668 l = (!j) ? dp->nrows - 1 : j - 1;
670 if (dp->oldcell[k + ml] ==
671 (int) (dp->oldcell[i + mj] + 1) % dp->states)
672 dp->newcell[i + mj] = dp->oldcell[k + ml];
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++) {
681 l = (!j) ? dp->nrows - 1 : j - 1;
683 if (dp->oldcell[k + ml] ==
684 (int) (dp->oldcell[i + mj] + 1) % dp->states)
685 dp->newcell[i + mj] = dp->oldcell[k + ml];
687 k = (i + 1 == dp->ncols) ? 0 : i + 1;
689 if (dp->oldcell[k + ml] ==
690 (int) (dp->oldcell[i + mj] + 1) % dp->states)
691 dp->newcell[i + mj] = dp->oldcell[k + ml];
694 l = (j + 1 == dp->nrows) ? 0 : j + 1;
696 if (dp->oldcell[k + ml] ==
697 (int) (dp->oldcell[i + mj] + 1) % dp->states)
698 dp->newcell[i + mj] = dp->oldcell[k + ml];
700 k = (!i) ? dp->ncols - 1 : i - 1;
703 if (dp->oldcell[k + ml] ==
704 (int) (dp->oldcell[i + mj] + 1) % dp->states)
705 dp->newcell[i + mj] = dp->oldcell[k + ml];
709 if (dp->neighbors == 8) {
711 for (j = 0; j < dp->nrows; j++) {
712 for (i = 0; i < dp->ncols; i++) {
714 k = (i + 1 == dp->ncols) ? 0 : i + 1;
715 l = (!j) ? dp->nrows - 1 : j - 1;
717 if (dp->oldcell[k + ml] ==
718 (int) (dp->oldcell[i + mj] + 1) % dp->states)
719 dp->newcell[i + mj] = dp->oldcell[k + ml];
721 k = (i + 1 == dp->ncols) ? 0 : i + 1;
722 l = (j + 1 == dp->nrows) ? 0 : j + 1;
724 if (dp->oldcell[k + ml] ==
725 (int) (dp->oldcell[i + mj] + 1) % dp->states)
726 dp->newcell[i + mj] = dp->oldcell[k + ml];
728 k = (!i) ? dp->ncols - 1 : i - 1;
729 l = (j + 1 == dp->nrows) ? 0 : j + 1;
731 if (dp->oldcell[k + ml] ==
732 (int) (dp->oldcell[i + mj] + 1) % dp->states)
733 dp->newcell[i + mj] = dp->oldcell[k + ml];
735 k = (!i) ? dp->ncols - 1 : i - 1;
736 l = (!j) ? dp->nrows - 1 : j - 1;
738 if (dp->oldcell[k + ml] ==
739 (int) (dp->oldcell[i + mj] + 1) % dp->states)
740 dp->newcell[i + mj] = dp->oldcell[k + ml];
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 */
751 k = (!i) ? dp->ncols - 1 : i - 1;
753 if (dp->oldcell[k + ml] ==
754 (int) (dp->oldcell[i + mj] + 1) % dp->states)
755 dp->newcell[i + mj] = dp->oldcell[k + ml];
758 k = (i + 1 == dp->ncols) ? 0 : i + 1;
760 if (dp->oldcell[k + ml] ==
761 (int) (dp->oldcell[i + mj] + 1) % dp->states)
762 dp->newcell[i + mj] = dp->oldcell[k + ml];
766 l = (!j) ? dp->nrows - 1 : j - 1;
768 if (dp->oldcell[k + ml] ==
769 (int) (dp->oldcell[i + mj] + 1) % dp->states)
770 dp->newcell[i + mj] = dp->oldcell[k + ml];
773 l = (j + 1 == dp->nrows) ? 0 : j + 1;
775 if (dp->oldcell[k + ml] ==
776 (int) (dp->oldcell[i + mj] + 1) % dp->states)
777 dp->newcell[i + mj] = dp->oldcell[k + ml];
781 if (dp->neighbors == 9 || dp->neighbors == 12) {
783 for (j = 0; j < dp->nrows; j++) {
784 for (i = 0; i < dp->ncols; i++) {
794 if (dp->oldcell[k + ml] ==
795 (int) (dp->oldcell[i + mj] + 1) % dp->states)
796 dp->newcell[i + mj] = dp->oldcell[k + ml];
799 if (j + 1 == dp->nrows)
801 else if (j + 2 == dp->nrows)
806 if (dp->oldcell[k + ml] ==
807 (int) (dp->oldcell[i + mj] + 1) % dp->states)
808 dp->newcell[i + mj] = dp->oldcell[k + ml];
810 k = (!i) ? dp->ncols - 1 : i - 1;
811 l = (!j) ? dp->nrows - 1 : j - 1;
813 if (dp->oldcell[k + ml] ==
814 (int) (dp->oldcell[i + mj] + 1) % dp->states)
815 dp->newcell[i + mj] = dp->oldcell[k + ml];
817 k = (i + 1 == dp->ncols) ? 0 : i + 1;
818 l = (!j) ? dp->nrows - 1 : j - 1;
820 if (dp->oldcell[k + ml] ==
821 (int) (dp->oldcell[i + mj] + 1) % dp->states)
822 dp->newcell[i + mj] = dp->oldcell[k + ml];
824 k = (!i) ? dp->ncols - 1 : i - 1;
825 l = (j + 1 == dp->nrows) ? 0 : j + 1;
827 if (dp->oldcell[k + ml] ==
828 (int) (dp->oldcell[i + mj] + 1) % dp->states)
829 dp->newcell[i + mj] = dp->oldcell[k + ml];
831 k = (i + 1 == dp->ncols) ? 0 : i + 1;
832 l = (j + 1 == dp->nrows) ? 0 : j + 1;
834 if (dp->oldcell[k + ml] ==
835 (int) (dp->oldcell[i + mj] + 1) % dp->states)
836 dp->newcell[i + mj] = dp->oldcell[k + ml];
840 if (dp->neighbors == 12) {
842 for (j = 0; j < dp->nrows; j++) {
843 for (i = 0; i < dp->ncols; i++) {
844 if ((i + j) % 2) { /* right */
846 k = (!i) ? dp->ncols - 1 : i - 1;
854 if (dp->oldcell[k + ml] ==
855 (int) (dp->oldcell[i + mj] + 1) % dp->states)
856 dp->newcell[i + mj] = dp->oldcell[k + ml];
858 k = (!i) ? dp->ncols - 1 : i - 1;
859 if (j + 1 == dp->nrows)
861 else if (j + 2 == dp->nrows)
866 if (dp->oldcell[k + ml] ==
867 (int) (dp->oldcell[i + mj] + 1) % dp->states)
868 dp->newcell[i + mj] = dp->oldcell[k + ml];
870 k = (i + 1 == dp->ncols) ? 0 : i + 1;
873 if (dp->oldcell[k + ml] ==
874 (int) (dp->oldcell[i + mj] + 1) % dp->states)
875 dp->newcell[i + mj] = dp->oldcell[k + ml];
878 k = (i + 1 == dp->ncols) ? 0 : i + 1;
886 if (dp->oldcell[k + ml] ==
887 (int) (dp->oldcell[i + mj] + 1) % dp->states)
888 dp->newcell[i + mj] = dp->oldcell[k + ml];
890 k = (i + 1 == dp->ncols) ? 0 : i + 1;
891 if (j + 1 == dp->nrows)
893 else if (j + 2 == dp->nrows)
898 if (dp->oldcell[k + ml] ==
899 (int) (dp->oldcell[i + mj] + 1) % dp->states)
900 dp->newcell[i + mj] = dp->oldcell[k + ml];
902 k = (!i) ? dp->ncols - 1 : i - 1;
905 if (dp->oldcell[k + ml] ==
906 (int) (dp->oldcell[i + mj] + 1) % dp->states)
907 dp->newcell[i + mj] = dp->oldcell[k + ml];
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);
927 if (++dp->generation > MI_CYCLES(mi))
931 if (dp->ncells[dp->state])
932 if (!draw_state(mi, dp->state)) {
933 free_demon(MI_DISPLAY(mi), dp);
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]);
944 if (++(dp->redrawpos) >= dp->ncols * dp->nrows) {
954 reshape_demon(ModeInfo * mi, int width, int height)
956 XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
962 release_demon (ModeInfo * mi)
964 if (demons != NULL) {
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;
975 refresh_demon (ModeInfo * mi)
981 dp = &demons[MI_SCREEN(mi)];
987 XSCREENSAVER_MODULE ("Demon", demon)
989 #endif /* MODE_demon */