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 struct cell_s *(adj)[3][3]; /* 16 */
82 FLOAT growth; /* 56 60 */
83 FLOAT nextspeed; /* 60 68 */
88 static int arr_height;
94 static int blastcount;
96 static Display *display;
98 static GC *coloredGCs;
100 static int windowWidth;
101 static int windowHeight;
107 static FLOAT orthlim = 1.0;
108 static FLOAT diaglim;
109 static FLOAT anychan;
110 static FLOAT minorchan;
111 static FLOAT instantdeathchan;
112 static int minlifespan;
113 static int maxlifespan;
114 static FLOAT minlifespeed;
115 static FLOAT maxlifespeed;
116 static FLOAT mindeathspeed;
117 static FLOAT maxdeathspeed;
118 static Bool originalcolors;
120 static int random_life_value (void)
122 return (int) ((RAND_FLOAT * (maxlifespan - minlifespan)) + minlifespan);
125 static void setup_random_colormap (XWindowAttributes *xgwa)
129 int ncolors = count - 1;
131 XColor *colors = (XColor *) calloc (sizeof(*colors), count*2);
133 colors[0].pixel = get_pixel_resource ("background", "Background",
134 display, xgwa->colormap);
136 make_random_colormap (display, xgwa->visual, xgwa->colormap,
137 colors+1, &ncolors, True, True, 0, True);
144 memcpy (colors + count, colors, count * sizeof(*colors));
145 colors[count].pixel = get_pixel_resource ("foreground", "Foreground",
146 display, xgwa->colormap);
148 for (n = 1; n < count; n++)
151 colors[n].red = colors[m].red / 2;
152 colors[n].green = colors[m].green / 2;
153 colors[n].blue = colors[m].blue / 2;
155 if (!XAllocColor (display, xgwa->colormap, &colors[n]))
158 colors[n] = colors[m];
165 "%s: unable to allocate %d half-intensity colors.\n",
169 for (n = 0; n < count*2; n++)
171 gcv.foreground = colors[n].pixel;
172 coloredGCs[n] = XCreateGC (display, window, GCForeground, &gcv);
178 static void setup_original_colormap (XWindowAttributes *xgwa)
183 XColor *colors = (XColor *) calloc (sizeof(*colors), count*2);
185 colors[0].pixel = get_pixel_resource ("background", "Background",
186 display, xgwa->colormap);
188 colors[count].pixel = get_pixel_resource ("foreground", "Foreground",
189 display, xgwa->colormap);
191 for (n = 1; n < count; n++)
194 colors[n].red = ((n & 0x01) != 0) * 0x8000;
195 colors[n].green = ((n & 0x02) != 0) * 0x8000;
196 colors[n].blue = ((n & 0x04) != 0) * 0x8000;
198 if (!XAllocColor (display, xgwa->colormap, &colors[n]))
201 colors[n] = colors[0];
204 colors[m].red = colors[n].red + 0x4000;
205 colors[m].green = colors[n].green + 0x4000;
206 colors[m].blue = colors[n].blue + 0x4000;
208 if (!XAllocColor (display, xgwa->colormap, &colors[m]))
211 colors[m] = colors[count];
218 "%s: unable to allocate %d colors.\n",
222 for (n = 0; n < count*2; n++)
224 gcv.foreground = colors[n].pixel;
225 coloredGCs[n] = XCreateGC (display, window, GCForeground, &gcv);
231 static void setup_display (void)
233 XWindowAttributes xgwa;
236 int cell_size = get_integer_resource ("size", "Integer");
237 if (cell_size < 1) cell_size = 1;
239 XGetWindowAttributes (display, window, &xgwa);
241 originalcolors = get_boolean_resource ("originalcolors", "Boolean");
243 count = get_integer_resource ("count", "Integer");
244 if (count < 2) count = 2;
245 if (count > (1L << (xgwa.depth-1)))
246 count = (1L << (xgwa.depth-1));
248 if (originalcolors && (count > 8))
253 coloredGCs = (GC *) calloc (sizeof(GC), count * 2);
255 diaglim = get_float_resource ("diaglim", "Float");
260 else if (diaglim > 2.0)
266 anychan = get_float_resource ("anychan", "Float");
271 else if (anychan > 1.0)
276 minorchan = get_float_resource ("minorchan","Float");
281 else if (minorchan > 1.0)
286 instantdeathchan = get_float_resource ("instantdeathchan","Float");
287 if (instantdeathchan < 0.0)
289 instantdeathchan = 0.0;
291 else if (instantdeathchan > 1.0)
293 instantdeathchan = 1.0;
296 minlifespan = get_integer_resource ("minlifespan", "Integer");
302 maxlifespan = get_integer_resource ("maxlifespan", "Integer");
303 if (maxlifespan < minlifespan)
305 maxlifespan = minlifespan;
308 minlifespeed = get_float_resource ("minlifespeed", "Float");
309 if (minlifespeed < 0.0)
313 else if (minlifespeed > 1.0)
318 maxlifespeed = get_float_resource ("maxlifespeed", "Float");
319 if (maxlifespeed < minlifespeed)
321 maxlifespeed = minlifespeed;
323 else if (maxlifespeed > 1.0)
328 mindeathspeed = get_float_resource ("mindeathspeed", "Float");
329 if (mindeathspeed < 0.0)
333 else if (mindeathspeed > 1.0)
338 maxdeathspeed = get_float_resource ("maxdeathspeed", "Float");
339 if (maxdeathspeed < mindeathspeed)
341 maxdeathspeed = mindeathspeed;
343 else if (maxdeathspeed > 1.0)
348 minlifespeed *= diaglim;
349 maxlifespeed *= diaglim;
350 mindeathspeed *= diaglim;
351 maxdeathspeed *= diaglim;
353 cmap = xgwa.colormap;
355 windowWidth = xgwa.width;
356 windowHeight = xgwa.height;
358 arr_width = windowWidth / cell_size;
359 arr_height = windowHeight / cell_size;
361 xSize = windowWidth / arr_width;
362 ySize = windowHeight / arr_height;
372 xOffset = (windowWidth - (arr_width * xSize)) / 2;
373 yOffset = (windowHeight - (arr_height * ySize)) / 2;
377 setup_original_colormap (&xgwa);
381 setup_random_colormap (&xgwa);
385 static void drawblock (int x, int y, unsigned char c)
387 if (xSize == 1 && ySize == 1)
388 XDrawPoint (display, window, coloredGCs[c], x + xOffset, y + yOffset);
390 XFillRectangle (display, window, coloredGCs[c],
391 x * xSize + xOffset, y * ySize + yOffset,
395 static void setup_arr (void)
406 XFillRectangle (display, window, coloredGCs[0], 0, 0,
407 windowWidth, windowHeight);
409 arr = (cell *) calloc (sizeof(cell), arr_width * arr_height);
412 fprintf (stderr, "%s: out of memory allocating %dx%d grid\n",
413 progname, arr_width, arr_height);
417 for (y = 0; y < arr_height; y++)
419 int row = y * arr_width;
420 for (x = 0; x < arr_width; x++)
424 arr[row+x].speed = 0.0;
425 arr[row+x].growth = 0.0;
427 arr[row+x].isnext = 0;
430 for (i = 0; i < 3; i++)
433 if (a < 0) a = arr_width - 1;
434 else if (a >= arr_width) a = 0;
435 for (j = 0; j < 3; j++)
438 if (b < 0) b = arr_height - 1;
439 else if (b >= arr_height) b = 0;
440 arr[row+x].adj[i][j] = &arr[(b * arr_width) + a];
448 head = (cell *) malloc (sizeof (cell));
453 tail = (cell *) malloc (sizeof (cell));
461 blastcount = random_life_value ();
464 static void newcell (cell *c, unsigned char col, FLOAT sp)
468 if (c->col == col) return;
475 c->next = head->next;
482 static void killcell (cell *c)
484 c->prev->next = c->next;
485 c->next->prev = c->prev;
488 drawblock (c->x, c->y, c->col);
492 static void randblip (int doit)
497 && (blastcount-- >= 0)
498 && (RAND_FLOAT > anychan))
507 blastcount = random_life_value ();
508 if (RAND_FLOAT < instantdeathchan)
510 /* clear everything every so often to keep from getting into a
516 else if (RAND_FLOAT <= minorchan)
522 n = random () % 3 + 3;
527 int x = random () % arr_width;
528 int y = random () % arr_height;
534 s = RAND_FLOAT * (maxdeathspeed - mindeathspeed) + mindeathspeed;
538 c = (random () % (count-1)) + 1;
539 s = RAND_FLOAT * (maxlifespeed - minlifespeed) + minlifespeed;
541 newcell (&arr[y * arr_width + x], c, s);
545 static void update (void)
549 for (a = head->next; a != tail; a = a->next)
551 if (a->speed == 0) continue;
552 a->growth += a->speed;
553 if (a->growth >= orthlim)
555 newcell (a->adj[0][1], a->col, a->speed);
556 newcell (a->adj[2][1], a->col, a->speed);
557 newcell (a->adj[1][0], a->col, a->speed);
558 newcell (a->adj[1][2], a->col, a->speed);
560 if (a->growth >= diaglim)
562 newcell (a->adj[0][0], a->col, a->speed);
563 newcell (a->adj[0][2], a->col, a->speed);
564 newcell (a->adj[2][0], a->col, a->speed);
565 newcell (a->adj[2][2], a->col, a->speed);
570 randblip ((head->next) == tail);
572 for (a = head->next; a != tail; a = a->next)
577 a->speed = a->nextspeed;
580 drawblock (a->x, a->y, a->col + count);
586 char *progclass = "Petri";
588 char *defaults [] = {
589 ".background: black",
590 ".foreground: white",
597 "*instantdeathchan: 0.2",
599 "*maxlifespan: 1500",
600 "*minlifespeed: 0.04",
601 "*maxlifespeed: 0.13",
602 "*mindeathspeed: 0.42",
603 "*maxdeathspeed: 0.46",
604 "*originalcolors: false",
608 XrmOptionDescRec options [] = {
609 { "-delay", ".delay", XrmoptionSepArg, 0 },
610 { "-size", ".size", XrmoptionSepArg, 0 },
611 { "-count", ".count", XrmoptionSepArg, 0 },
612 { "-diaglim", ".diaglim", XrmoptionSepArg, 0 },
613 { "-anychan", ".anychan", XrmoptionSepArg, 0 },
614 { "-minorchan", ".minorchan", XrmoptionSepArg, 0 },
615 { "-instantdeathchan", ".instantdeathchan", XrmoptionSepArg, 0 },
616 { "-minlifespan", ".minlifespan", XrmoptionSepArg, 0 },
617 { "-maxlifespan", ".maxlifespan", XrmoptionSepArg, 0 },
618 { "-minlifespeed", ".minlifespeed", XrmoptionSepArg, 0 },
619 { "-maxlifespeed", ".maxlifespeed", XrmoptionSepArg, 0 },
620 { "-mindeathspeed", ".mindeathspeed", XrmoptionSepArg, 0 },
621 { "-maxdeathspeed", ".maxdeathspeed", XrmoptionSepArg, 0 },
622 { "-originalcolors", ".originalcolors", XrmoptionNoArg, "true" },
626 void screenhack (Display *dpy, Window win)
628 int delay = get_integer_resource ("delay", "Delay");
641 screenhack_handle_events (dpy);