1 /* petri, simulate mold in a petri dish. v2.6
2 * by Dan Bornstein, danfuzz@milk.com
3 * with help from Jamie Zawinski, jwz@jwz.org
4 * Copyright (c) 1992-1999 Dan Bornstein.
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation. No representations are made about the suitability of this
11 * software for any purpose. It is provided "as is" without express or
15 * Brief description of options/resources:
17 * delay: the delay in microseconds between iterations
18 * size: the size of a cell in pixels
19 * count: the number of different kinds of mold (minimum: 2)
20 * diaglim: the age limit for diagonal growth as a multiplier of orthogonal
21 * growth (minimum: 1, maximum 2). 1 means square growth, 1.414
22 * (i.e., sqrt(2)) means approximately circular growth, 2 means diamond
24 * anychan: the chance (fraction, between 0 and 1) that at each iteration,
25 * any new cell will be born
26 * minorchan: the chance (fraction, between 0 and 1) that, given that new
27 * cells will be added, that only two will be added (a minor cell birth
29 * instantdeathchan: the chance (fraction, between 0 and 1) that, given
30 * that death and destruction will happen, that instead of using plague
31 * cells, death will be instantaneous
32 * minlifespan: the minimum lifespan of a colony (before black death ensues)
33 * maxlifespan: the maximum lifespan of a colony (before black death ensues)
34 * minlifespeed: the minimum speed for living cells as a fraction of the
35 * maximum possible speed (fraction, between 0 and 1)
36 * maxlifespeed: the maximum speed for living cells as a fraction of the
37 * maximum possible speed (fraction, between 0 and 1)
38 * mindeathspeed: the minimum speed for black death cells as a fraction of the
39 * maximum possible speed (fraction, between 0 and 1)
40 * maxdeathspeed: the maximum speed for black death cells as a fraction of the
41 * maximum possible speed (fraction, between 0 and 1)
42 * originalcolors: if true, count must be 8 or less and the colors are a
43 * fixed set of primary and secondary colors (the artist's original choices)
45 * Interesting settings:
47 * petri -originalcolors -size 8
49 * petri -size 8 -diaglim 1.8
52 * petri -count 4 -anychan 0.01 -minorchan 1 \
53 * -minlifespan 2000 -maxlifespan 5000
55 * petri -count 3 -anychan 1 -minlifespan 100000 \
58 * petri -minlifespeed 0.02 -maxlifespeed 0.03 -minlifespan 1 \
59 * -maxlifespan 1 -instantdeathchan 0 -minorchan 0 \
60 * -anychan 0.3 -delay 4000
64 #include "screenhack.h"
68 #define RAND_FLOAT (((FLOAT) (random() & 0xffff)) / ((FLOAT) 0x10000))
74 unsigned char col; /* 4 */
75 unsigned char isnext; /* 5 */
76 unsigned char nextcol; /* 6 */
78 struct cell_s *next; /* 8 - */
79 struct cell_s *prev; /* 12 */
80 FLOAT speed; /* 16 - */
81 FLOAT growth; /* 20 24 */
82 FLOAT nextspeed; /* 24 32 - */
87 static int arr_height;
93 static int blastcount;
95 static Display *display;
97 static GC *coloredGCs;
99 static int windowWidth;
100 static int windowHeight;
106 static FLOAT orthlim = 1.0;
107 static FLOAT diaglim;
108 static FLOAT anychan;
109 static FLOAT minorchan;
110 static FLOAT instantdeathchan;
111 static int minlifespan;
112 static int maxlifespan;
113 static FLOAT minlifespeed;
114 static FLOAT maxlifespeed;
115 static FLOAT mindeathspeed;
116 static FLOAT maxdeathspeed;
117 static Bool originalcolors;
119 static int random_life_value (void)
121 return (int) ((RAND_FLOAT * (maxlifespan - minlifespan)) + minlifespan);
124 static void setup_random_colormap (XWindowAttributes *xgwa)
128 int ncolors = count - 1;
130 XColor *colors = (XColor *) calloc (sizeof(*colors), count*2);
132 colors[0].pixel = get_pixel_resource ("background", "Background",
133 display, xgwa->colormap);
135 make_random_colormap (display, xgwa->visual, xgwa->colormap,
136 colors+1, &ncolors, True, True, 0, True);
139 fprintf (stderr, "%s: couldn't allocate any colors\n", progname);
146 memcpy (colors + count, colors, count * sizeof(*colors));
147 colors[count].pixel = get_pixel_resource ("foreground", "Foreground",
148 display, xgwa->colormap);
150 for (n = 1; n < count; n++)
153 colors[n].red = colors[m].red / 2;
154 colors[n].green = colors[m].green / 2;
155 colors[n].blue = colors[m].blue / 2;
157 if (!XAllocColor (display, xgwa->colormap, &colors[n]))
160 colors[n] = colors[m];
167 "%s: unable to allocate %d half-intensity colors.\n",
171 for (n = 0; n < count*2; n++)
173 gcv.foreground = colors[n].pixel;
174 coloredGCs[n] = XCreateGC (display, window, GCForeground, &gcv);
180 static void setup_original_colormap (XWindowAttributes *xgwa)
185 XColor *colors = (XColor *) calloc (sizeof(*colors), count*2);
187 colors[0].pixel = get_pixel_resource ("background", "Background",
188 display, xgwa->colormap);
190 colors[count].pixel = get_pixel_resource ("foreground", "Foreground",
191 display, xgwa->colormap);
193 for (n = 1; n < count; n++)
196 colors[n].red = ((n & 0x01) != 0) * 0x8000;
197 colors[n].green = ((n & 0x02) != 0) * 0x8000;
198 colors[n].blue = ((n & 0x04) != 0) * 0x8000;
200 if (!XAllocColor (display, xgwa->colormap, &colors[n]))
203 colors[n] = colors[0];
206 colors[m].red = colors[n].red + 0x4000;
207 colors[m].green = colors[n].green + 0x4000;
208 colors[m].blue = colors[n].blue + 0x4000;
210 if (!XAllocColor (display, xgwa->colormap, &colors[m]))
213 colors[m] = colors[count];
220 "%s: unable to allocate %d colors.\n",
224 for (n = 0; n < count*2; n++)
226 gcv.foreground = colors[n].pixel;
227 coloredGCs[n] = XCreateGC (display, window, GCForeground, &gcv);
233 static void setup_display (void)
235 XWindowAttributes xgwa;
238 int cell_size = get_integer_resource ("size", "Integer");
239 int osize, alloc_size, oalloc;
240 int mem_throttle = 0;
243 if (cell_size < 1) cell_size = 1;
247 s = get_string_resource ("memThrottle", "MemThrottle");
252 if (1 == sscanf (s, " %d M %c", &n, &c) ||
253 1 == sscanf (s, " %d m %c", &n, &c))
254 mem_throttle = n * (1 << 20);
255 else if (1 == sscanf (s, " %d K %c", &n, &c) ||
256 1 == sscanf (s, " %d k %c", &n, &c))
257 mem_throttle = n * (1 << 10);
258 else if (1 == sscanf (s, " %d %c", &n, &c))
262 fprintf (stderr, "%s: invalid memThrottle \"%s\" (try \"10M\")\n",
270 XGetWindowAttributes (display, window, &xgwa);
272 originalcolors = get_boolean_resource ("originalcolors", "Boolean");
274 count = get_integer_resource ("count", "Integer");
275 if (count < 2) count = 2;
276 if (count > (1L << (xgwa.depth-1)))
277 count = (1L << (xgwa.depth-1));
279 if (originalcolors && (count > 8))
284 coloredGCs = (GC *) calloc (sizeof(GC), count * 2);
286 diaglim = get_float_resource ("diaglim", "Float");
291 else if (diaglim > 2.0)
297 anychan = get_float_resource ("anychan", "Float");
302 else if (anychan > 1.0)
307 minorchan = get_float_resource ("minorchan","Float");
312 else if (minorchan > 1.0)
317 instantdeathchan = get_float_resource ("instantdeathchan","Float");
318 if (instantdeathchan < 0.0)
320 instantdeathchan = 0.0;
322 else if (instantdeathchan > 1.0)
324 instantdeathchan = 1.0;
327 minlifespan = get_integer_resource ("minlifespan", "Integer");
333 maxlifespan = get_integer_resource ("maxlifespan", "Integer");
334 if (maxlifespan < minlifespan)
336 maxlifespan = minlifespan;
339 minlifespeed = get_float_resource ("minlifespeed", "Float");
340 if (minlifespeed < 0.0)
344 else if (minlifespeed > 1.0)
349 maxlifespeed = get_float_resource ("maxlifespeed", "Float");
350 if (maxlifespeed < minlifespeed)
352 maxlifespeed = minlifespeed;
354 else if (maxlifespeed > 1.0)
359 mindeathspeed = get_float_resource ("mindeathspeed", "Float");
360 if (mindeathspeed < 0.0)
364 else if (mindeathspeed > 1.0)
369 maxdeathspeed = get_float_resource ("maxdeathspeed", "Float");
370 if (maxdeathspeed < mindeathspeed)
372 maxdeathspeed = mindeathspeed;
374 else if (maxdeathspeed > 1.0)
379 minlifespeed *= diaglim;
380 maxlifespeed *= diaglim;
381 mindeathspeed *= diaglim;
382 maxdeathspeed *= diaglim;
384 cmap = xgwa.colormap;
386 windowWidth = xgwa.width;
387 windowHeight = xgwa.height;
389 arr_width = windowWidth / cell_size;
390 arr_height = windowHeight / cell_size;
392 alloc_size = sizeof(cell) * arr_width * arr_height;
395 if (mem_throttle > 0)
396 while (cell_size < windowWidth/10 &&
397 cell_size < windowHeight/10 &&
398 alloc_size > mem_throttle)
401 arr_width = windowWidth / cell_size;
402 arr_height = windowHeight / cell_size;
403 alloc_size = sizeof(cell) * arr_width * arr_height;
406 if (osize != cell_size)
408 static int warned = 0;
412 "%s: throttling cell size from %d to %d because of %dM limit.\n",
413 progname, osize, cell_size, mem_throttle / (1 << 20));
414 fprintf (stderr, "%s: %dx%dx%d = %.1fM, %dx%dx%d = %.1fM.\n",
416 windowWidth, windowHeight, osize,
417 ((float) oalloc) / (1 << 20),
418 windowWidth, windowHeight, cell_size,
419 ((float) alloc_size) / (1 << 20));
424 xSize = windowWidth / arr_width;
425 ySize = windowHeight / arr_height;
435 xOffset = (windowWidth - (arr_width * xSize)) / 2;
436 yOffset = (windowHeight - (arr_height * ySize)) / 2;
440 setup_original_colormap (&xgwa);
444 setup_random_colormap (&xgwa);
448 static void drawblock (int x, int y, unsigned char c)
450 if (xSize == 1 && ySize == 1)
451 XDrawPoint (display, window, coloredGCs[c], x + xOffset, y + yOffset);
453 XFillRectangle (display, window, coloredGCs[c],
454 x * xSize + xOffset, y * ySize + yOffset,
458 static void setup_arr (void)
467 XFillRectangle (display, window, coloredGCs[0], 0, 0,
468 windowWidth, windowHeight);
470 arr = (cell *) calloc (sizeof(cell), arr_width * arr_height);
473 fprintf (stderr, "%s: out of memory allocating %dx%d grid\n",
474 progname, arr_width, arr_height);
478 for (y = 0; y < arr_height; y++)
480 int row = y * arr_width;
481 for (x = 0; x < arr_width; x++)
485 arr[row+x].speed = 0.0;
486 arr[row+x].growth = 0.0;
488 arr[row+x].isnext = 0;
496 head = (cell *) malloc (sizeof (cell));
501 tail = (cell *) malloc (sizeof (cell));
509 blastcount = random_life_value ();
512 static void newcell (cell *c, unsigned char col, FLOAT sp)
516 if (c->col == col) return;
523 c->next = head->next;
530 static void killcell (cell *c)
532 c->prev->next = c->next;
533 c->next->prev = c->prev;
536 drawblock (c->x, c->y, c->col);
540 static void randblip (int doit)
545 && (blastcount-- >= 0)
546 && (RAND_FLOAT > anychan))
555 blastcount = random_life_value ();
556 if (RAND_FLOAT < instantdeathchan)
558 /* clear everything every so often to keep from getting into a
564 else if (RAND_FLOAT <= minorchan)
570 n = random () % 3 + 3;
575 int x = random () % arr_width;
576 int y = random () % arr_height;
582 s = RAND_FLOAT * (maxdeathspeed - mindeathspeed) + mindeathspeed;
586 c = (random () % (count-1)) + 1;
587 s = RAND_FLOAT * (maxlifespeed - minlifespeed) + minlifespeed;
589 newcell (&arr[y * arr_width + x], c, s);
593 static void update (void)
597 for (a = head->next; a != tail; a = a->next)
599 static XPoint coords1[] = {{-1, 0}, { 1, 0}, {0, -1}, {0, 1}};
600 static XPoint coords2[] = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
604 if (a->speed == 0) continue;
605 a->growth += a->speed;
606 if (a->growth >= orthlim)
609 if (a->growth >= diaglim)
613 for (i = 0; i < 4; i++)
615 int x = a->x + coords[i].x;
616 int y = a->y + coords[i].y;
618 if (x < 0) x = arr_width - 1;
619 else if (x >= arr_width) x = 0;
621 if (y < 0) y = arr_height - 1;
622 else if (y >= arr_height) y = 0;
624 newcell (&arr[y * arr_width + x], a->col, a->speed);
627 if (a->growth >= diaglim)
631 randblip ((head->next) == tail);
633 for (a = head->next; a != tail; a = a->next)
638 a->speed = a->nextspeed;
641 drawblock (a->x, a->y, a->col + count);
647 char *progclass = "Petri";
649 char *defaults [] = {
650 ".background: black",
651 ".foreground: white",
658 "*instantdeathchan: 0.2",
660 "*maxlifespan: 1500",
661 "*minlifespeed: 0.04",
662 "*maxlifespeed: 0.13",
663 "*mindeathspeed: 0.42",
664 "*maxdeathspeed: 0.46",
665 "*originalcolors: false",
666 "*memThrottle: 22M", /* don't malloc more than this much.
667 Scale the pixels up if necessary. */
671 XrmOptionDescRec options [] = {
672 { "-delay", ".delay", XrmoptionSepArg, 0 },
673 { "-size", ".size", XrmoptionSepArg, 0 },
674 { "-count", ".count", XrmoptionSepArg, 0 },
675 { "-diaglim", ".diaglim", XrmoptionSepArg, 0 },
676 { "-anychan", ".anychan", XrmoptionSepArg, 0 },
677 { "-minorchan", ".minorchan", XrmoptionSepArg, 0 },
678 { "-instantdeathchan", ".instantdeathchan", XrmoptionSepArg, 0 },
679 { "-minlifespan", ".minlifespan", XrmoptionSepArg, 0 },
680 { "-maxlifespan", ".maxlifespan", XrmoptionSepArg, 0 },
681 { "-minlifespeed", ".minlifespeed", XrmoptionSepArg, 0 },
682 { "-maxlifespeed", ".maxlifespeed", XrmoptionSepArg, 0 },
683 { "-mindeathspeed", ".mindeathspeed", XrmoptionSepArg, 0 },
684 { "-maxdeathspeed", ".maxdeathspeed", XrmoptionSepArg, 0 },
685 { "-originalcolors", ".originalcolors", XrmoptionNoArg, "true" },
686 { "-mem-throttle", ".memThrottle", XrmoptionSepArg, 0 },
690 void screenhack (Display *dpy, Window win)
692 int delay = get_integer_resource ("delay", "Delay");
705 screenhack_handle_events (dpy);