Cleaned up CLI flags for cell size in WolframAutomata source code and README.
[screensavers] / hacks / WolframAutomata / WolframAutomata.c
CommitLineData
0c731d4a
AT
1/* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com> */
2/* See LICENSE.txt file for copyright and license details. */
3
0c731d4a
AT
4#include "screenhack.h"
5
14d68c5b
AT
6/* -------------------------------------------------------------------------- */
7/* Data Structures */
8/* -------------------------------------------------------------------------- */
9
0c731d4a 10struct state {
b130361b 11 /* Various X resources */
7ce88c8e
AT
12 Display * dpy;
13 Window win;
14 GC gc;
15
b130361b
AT
16 /* These hold the pixel value of the foreground and background colors in */
17 /* the same format as an XColor struct's "pixel" member. */
7ce88c8e 18 unsigned long fg, bg;
7ce88c8e 19
b130361b
AT
20 /* This Pixmap will eventually hold the entire evolution of the CA. The */
21 /* displayed portion of the CA's evolution is merely a viewport into this */
22 /* Pixmap. */
23 Pixmap evolution_history;
80cfe219 24
b130361b
AT
25 /* Together, these three values define the display viewport into the */
26 /* 'evolution_history' Pixmap. The pair 'dpy_width' and 'dpy_height' are */
27 /* simply the width and height of the display window. They remain */
28 /* unchanged during normal operation. However, 'ypos' tracks the location */
29 /* of the viewport in the 'evolution_history'. It must always keep the */
30 /* newest generation onscreen and display as much history as possible. */
31 int dpy_width, dpy_height, ypos;
32
33 /* In the 'current_generation' array, the value True means a cell is */
34 /* alive. We only need to track the current generation since our rulesets */
35 /* never consider older generations. Anything older can be rendered to */
36 /* the 'evolution_history' Pixmap and subsequently ignored. */
37 Bool * current_generation;
c428f3d5 38
b130361b
AT
39 /* When randomizing the seed generation, we can specify a population */
40 /* density, or we can restrict to a single living cell. */
14d68c5b
AT
41 int population_density;
42 Bool population_single;
43
b130361b
AT
44 /* For more information on the encoding used for rule_number and on the */
45 /* method used to apply it: https://en.wikipedia.org/wiki/Wolfram_code */
46 uint8_t rule_number;
c428f3d5 47
b130361b 48 /* At the end of the simulation, the user is given time to admire the */
4ff197f3 49 /* output. Delay is available to user as CLI option '-admiration-delay'. */
b130361b 50 Bool admiration_in_progress;
4ff197f3 51 size_t admiration_delay; /* ...in seconds. */
b130361b
AT
52
53 /* The following values correspond directly to independent CLI options. */
54 Bool rule_random;
55 uint8_t rule_requested; /* Note: Repurposing Rule 0 as null value. */
56 int cell_size; /* If cell_size=N then draw NxN pixels per cell. */
57 int delay_microsec; /* ...between calls to WolframAutomata_draw(). */
58 int num_generations; /* Reset simulation after this many generations. */
59
60 /* Not strictly necessary, but makes some code easier to read. */
c428f3d5 61 size_t number_of_cells;
0c731d4a
AT
62};
63
14d68c5b 64enum seed_population {
1f5d1274
AT
65 random_cell,
66 middle_cell,
67 edge_cell
14d68c5b
AT
68};
69
14d68c5b
AT
70struct curated_ruleset {
71 uint8_t rule;
72 enum seed_population seed;
73};
74
14d68c5b 75static const struct curated_ruleset curated_ruleset_list[] = {
b130361b
AT
76 { 18, middle_cell},
77 { 30, middle_cell},
78 { 45, middle_cell},
79 { 54, middle_cell},
80 { 57, middle_cell},
81 { 73, middle_cell},
1f5d1274
AT
82 {105, middle_cell},
83 {109, middle_cell},
84 {129, middle_cell},
85 {133, middle_cell},
86 {135, middle_cell},
87 {150, middle_cell},
b130361b
AT
88 { 30, edge_cell},
89 { 45, edge_cell},
90 { 57, edge_cell},
91 { 60, edge_cell},
92 { 75, edge_cell},
1f5d1274
AT
93 {107, edge_cell},
94 {110, edge_cell},
95 {133, edge_cell},
96 {137, edge_cell},
97 {169, edge_cell},
98 {225, edge_cell},
b130361b
AT
99 { 22, random_cell},
100 { 30, random_cell},
101 { 54, random_cell},
102 { 62, random_cell},
103 { 90, random_cell},
1f5d1274
AT
104 {105, random_cell},
105 {108, random_cell},
106 {110, random_cell},
107 {126, random_cell},
108 {146, random_cell},
109 {150, random_cell},
110 {182, random_cell},
111 {184, random_cell},
112 {225, random_cell},
113 {240, random_cell}
80cfe219
AT
114};
115
d0f3b852 116struct color_pair {
b130361b
AT
117 /* The type 'unsigned short' comes from the XColor struct definition, */
118 /* reproduced below. */
119 /* */
120 /* typedef struct { */
121 /* unsigned long pixel; */
122 /* unsigned short red, green, blue; */
123 /* char flags; */
124 /* char pad; */
125 /* } XColor; */
126 /* */
127 /* The red, green, and blue values are always in the range 0 to 65535 */
128 /* inclusive, independent of the number of bits actually used in the */
129 /* display hardware. The server scales these values to the range used */
130 /* by the hardware. Black is represented by (0,0,0), and white is */
131 /* represented by (65535,65535,65535). */
132 unsigned short fg_red, fg_green, fg_blue;
133 unsigned short bg_red, bg_green, bg_blue;
d0f3b852
AT
134};
135
d0f3b852 136static const struct color_pair color_list[] = {
b130361b
AT
137 /* For mapping X11 color names to RGB values: */
138 /* https://www.ehdp.com/methods/x11-color-names-rgb-values.htm */
139 /* Remember that our values range from 0-65535 inclusive, so scale the */
140 /* usual 0-255 range accordingly. */
141 /* */
142 /* +---------------------------------------+ */
143 /* | foreground | | background | */
144 /* | red,green,blue | | red,green,blue | */
145 {65535, 0, 0, 0, 0, 0}, /* {"red", "black"}, */
146 {32767,32767, 0, 0, 0, 0}, /* {"olive", "black"}, */
147 { 0,32767,32767, 0, 0, 0}, /* {"teal", "black"}, */
148 {27524,22937,52428, 0, 0, 0}, /* {"slateblue", "black"}, */
149 {60947,33422,60947, 0, 0, 0}, /* {"violet", "black"}, */
150 {41287, 8519,61602, 0, 0, 0}, /* {"purple", "black"}, */
151 {65535,65535,65535, 0, 0, 0}, /* {"white", "black"}, */
152 {65535,65535,65535, 0,25558, 0}, /* {"white", "darkgreen"}, */
153 {65535,65535,65535, 36044, 0,36044}, /* {"white", "darkmagenta"}, */
154 {65535,65535,65535, 36044, 0, 0}, /* {"white", "darkred"}, */
155 {65535,65535,65535, 0, 0,36044}, /* {"white", "darkblue"}, */
156 {11796,20315,20315, 36494,65535,65535}, /* {"darkslategray", "darkslategray1"}, */
157 {45219,50461,57015, 11796,20315,20315}, /* {"lightsteelblue", "darkslategray"}, */
158 {10023,16448,35723, 16383,26869,57670}, /* {"royalblue4", "royalblue"}, */
159 {61166,57311,52428, 35723,33667,30840}, /* {"antiquewhite2", "antiquewhite4"}, */
160 {51914,65535,28784, 21626,27524,11796}, /* {"darkolivegreen1", "darkolivegreen"}, */
161 {49601,65535,49601, 26985,35723,26985}, /* {"darkseagreen1", "darkseagreen4"}, */
162 {65535,49151,52428, 36044, 0, 0}, /* {"pink", "darkred"}, */
163 {44563,55704,58981, 0,25558, 0}, /* {"lightblue", "darkgreen"}, */
164 {65535, 0, 0, 0, 0,65535}, /* {"red", "blue"}, */
165 {65535, 0, 0, 0,25558, 0}, /* {"red", "darkgreen"}, */
166 { 0,65535,65535, 0,32767,32767}, /* {"aqua", "teal"}, */
167 { 0, 0,36044, 0,32767,32767}, /* {"darkblue", "teal"}, */
168 {61602,58981,32767, 11796,36044,22281}, /* {"khaki", "seagreen"}, */
169 {61602,58981,32767, 21626,27524,11796}, /* {"khaki", "darkolivegreen"}, */
170 {30801,34733,39321, 11796,20315,20315}, /* {"lightslategray", "darkslategray"}, */
171 {65535,25558,18349, 11796,20315,20315}, /* {"tomato", "darkslategray"}, */
172 {65535,25558,18349, 0,36044,36044} /* {"tomato", "darkcyan"} */
d0f3b852
AT
173};
174
14d68c5b
AT
175/* -------------------------------------------------------------------------- */
176/* Helper Functions */
177/* -------------------------------------------------------------------------- */
178
b130361b 179static void
14d68c5b
AT
180generate_random_seed(struct state * state)
181{
182 int i;
183 for (i = 0; i < state->number_of_cells; i++) {
184 state->current_generation[i] = ((random() % 100) < state->population_density) ? True : False;
185 }
186}
187
b130361b
AT
188/* This function sanitizes the index used to access cells in a generation. */
189/* Specifically, it wraps the index, creating a circular universe for the */
190/* cells and ensuring every cell has two neighbors. */
191static size_t
14d68c5b
AT
192sindex(struct state * state, int index)
193{
194 while (index < 0) {
195 index += state->number_of_cells;
196 }
197 while (index >= state->number_of_cells) {
198 index -= state->number_of_cells;
199 }
200 return (size_t) index;
201}
202
b130361b
AT
203/* For more information on the encoding used for state->rule_number and on */
204/* the method used to apply it: https://en.wikipedia.org/wiki/Wolfram_code */
205static Bool
14d68c5b
AT
206calculate_cell(struct state * state, int cell_id)
207{
208 uint8_t cell_pattern = 0;
209 int i;
210 for (i = -1; i < 2; i++) {
211 cell_pattern = cell_pattern << 1;
212 if (state->current_generation[sindex(state, cell_id+i)] == True) {
213 cell_pattern |= 1;
214 }
215 }
216 if ((state->rule_number >> cell_pattern) & 1) {
217 return True;
218 } else {
219 return False;
220 }
221}
222
b130361b 223static void
14d68c5b
AT
224render_current_generation(struct state * state)
225{
226 size_t xpos;
227 for (xpos = 0; xpos < state->number_of_cells; xpos++) {
228 if (state->current_generation[xpos] == True) {
b130361b 229 XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->cell_size, state->ypos, state->cell_size, state->cell_size);
8c85f136
AT
230 } else {
231 XSetForeground(state->dpy, state->gc, state->bg);
b130361b 232 XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->cell_size, state->ypos, state->cell_size, state->cell_size);
8c85f136 233 XSetForeground(state->dpy, state->gc, state->fg);
14d68c5b
AT
234 }
235 }
236}
237
238/* -------------------------------------------------------------------------- */
239/* Screenhack API Functions */
240/* -------------------------------------------------------------------------- */
241
20848f70
AT
242static Bool
243WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event)
244{
245 return False;
246}
247
248static void
249WolframAutomata_free(Display * dpy, Window win, void * closure)
250{
251 struct state * state = closure;
252 XFreeGC(state->dpy, state->gc);
253 XFreePixmap(state->dpy, state->evolution_history);
254 free(state->current_generation);
255 free(state);
256}
257
0c731d4a
AT
258static void *
259WolframAutomata_init(Display * dpy, Window win)
260{
76b9ae92
AT
261 struct state * state = calloc(1, sizeof(*state));
262 if (!state) {
263 fprintf(stderr, "ERROR: Failed to calloc() for state struct in WolframAutomata_init().\n");
264 exit(EXIT_FAILURE);
265 }
266
7ce88c8e
AT
267 XGCValues gcv;
268 XWindowAttributes xgwa;
14d68c5b 269 const struct curated_ruleset * curated_ruleset = NULL;
7ce88c8e
AT
270
271 state->dpy = dpy;
272 state->win = win;
273
274 XGetWindowAttributes(state->dpy, state->win, &xgwa);
b130361b
AT
275 state->dpy_width = xgwa.width;
276 state->dpy_height = xgwa.height;
277 state->ypos = 0;
278
4ff197f3 279 state->admiration_delay = get_integer_resource(state->dpy, "admiration-delay", "Integer");
b130361b 280 state->admiration_in_progress = False;
7ce88c8e 281
39e6fe44
AT
282 /* Set foreground and background colors for active/inactive cells. Either */
283 /* the user provided an index into the pre-defined color_list[] or a */
284 /* random entry from that same array should be selected. */
285 size_t color_index = get_integer_resource(state->dpy, "color-index", "Integer");
286 if (color_index == -1) {
287 color_index = random() % sizeof(color_list)/sizeof(color_list[0]);
288 } else if (color_index >= sizeof(color_list)/sizeof(color_list[0])) {
289 fprintf(stderr, "WARNING: Color index out of range.\n");
290 color_index = 0;
d0f3b852 291 }
39e6fe44
AT
292 XColor fg, bg;
293 fg.red = color_list[color_index].fg_red;
294 fg.green = color_list[color_index].fg_green;
295 fg.blue = color_list[color_index].fg_blue;
296 bg.red = color_list[color_index].bg_red;
297 bg.green = color_list[color_index].bg_green;
298 bg.blue = color_list[color_index].bg_blue;
299 /* TODO: Since I 'alloc', presumably I must also 'free' these colors */
300 /* at some point. Where/how? I don't want to eventually crash my */
301 /* X server after months of use. */
302 XAllocColor(state->dpy, xgwa.colormap, &fg);
303 XAllocColor(state->dpy, xgwa.colormap, &bg);
304 state->fg = gcv.foreground = fg.pixel;
305 state->bg = gcv.background = bg.pixel;
d0f3b852 306
7ce88c8e
AT
307 state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv);
308
b130361b 309 /* Set the size of each simulated cell to NxN pixels for cell_size=N. */
30934676 310 if (get_boolean_resource(state->dpy, "random-cell-size", "Boolean")) {
d918dd36
AT
311 /* Although we are choosing the pixel size 'randomly', a truly random */
312 /* selection would bias toward large numbers since there are more of */
313 /* them. To avoid this, we select a random number for a bit shift, */
314 /* resulting in a pixel size of 1, 2, 4, 8, 16 or 32, equally likely. */
b130361b 315 state->cell_size = 1 << (random() % 6);
d918dd36 316 } else {
30934676 317 state->cell_size = get_integer_resource(state->dpy, "cell-size", "Integer");
d918dd36 318 }
b130361b
AT
319 if (state->cell_size < 1) state->cell_size = 1;
320 if (state->cell_size > state->dpy_width) state->cell_size = state->dpy_width;
c428f3d5 321
b130361b
AT
322 /* Larger cell sizes won't always evenly divide the number of pixels in */
323 /* our window. In order to avoid a black stripe down the edge, '+1' here */
324 /* to ensure we are slightly oversize rather than undersize. */
325 state->number_of_cells = (state->dpy_width / state->cell_size) + 1;
c428f3d5 326
d918dd36
AT
327 /* Set the delay (in microseconds) between simulation of each generation */
328 /* of the simulation, also known as the delay between calls to */
329 /* WolframAutomata_draw(), which simulates one generation per call. */
330 if (get_boolean_resource(state->dpy, "random-delay", "Boolean")) {
331 /* When randomly setting the delay, the problem is to avoid being too */
332 /* fast or too slow, as well as ensuring slower speeds are chosen */
333 /* with the same likelihood as faster speeds, as perceived by a */
334 /* human. By empirical observation, we note that for 1x1 up to 4x4 */
335 /* pixel cell sizes, values for state->delay_microsec between */
336 /* 2048 (2^11) and 16556 (2^14) produce pleasant scroll rates. To */
b130361b 337 /* maintain this appearance, we bitshift state->cell_size down until */
d918dd36
AT
338 /* it is a maximum of 4x4 pixels in size, record how many bitshifts */
339 /* took place, and then shift our valid window for */
340 /* state->delay_microsec up by an equal number of bitshifts. For */
b130361b
AT
341 /* example, if state->cell_size=9, then it takes one right shift to */
342 /* reach state->cell_size=4. Thus, the valid window for */
d918dd36
AT
343 /* state->delay_microsec becomes 4096 (2^12) up to 32768 (2^15). */
344 size_t pixel_shift_range = 1;
b130361b
AT
345 size_t cell_size_temp = state->cell_size;
346 while (cell_size_temp > 4) {
347 cell_size_temp >>= 1;
d918dd36
AT
348 pixel_shift_range++;
349 }
350 /* In the below line, '3' represents the total range, namely '14-11' */
351 /* from '2^14' and '2^11' as the endpoints. Similarly, the '11' in */
352 /* the below line represents the starting point of this range, from */
353 /* the exponent in '2^11'. */
354 state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range);
355 } else {
356 state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer");
357 }
358 if (state->delay_microsec < 0) state->delay_microsec = 0;
359
360 /* Set the number of generations to simulate before wiping the simulation */
361 /* and re-running with new settings. */
362 if (get_boolean_resource(state->dpy, "random-num-generations", "Boolean")) {
363 /* By empirical observation, keep the product */
b130361b 364 /* state->num_generations * state->cell_size */
d918dd36
AT
365 /* below 10,000 to avoid BadAlloc errors from the X server due to */
366 /* requesting an enormous pixmap. This value works on both a 12 core */
367 /* Xeon with 108 GiB of RAM and a Sun Ultra 2 with 2 GiB of RAM. */
b130361b 368 state->num_generations = random() % (10000 / state->cell_size);
d918dd36
AT
369 /* Ensure selected value is large enough to at least fill the screen. */
370 /* Cast to avoid overflow. */
b130361b
AT
371 if ((long)state->num_generations * (long)state->cell_size < state->dpy_height) {
372 state->num_generations = (state->dpy_height / state->cell_size) + 1;
d918dd36
AT
373 }
374 } else {
375 state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer");
376 }
80cfe219
AT
377 /* The minimum number of generations is 2 since we must allocate enough */
378 /* space to hold the seed generation and at least one pass through */
379 /* WolframAutomata_draw(), which is where we check whether or not we've */
380 /* reached the end of the pixmap. */
7969381e 381 if (state->num_generations < 0) state->num_generations = 2;
b130361b 382 /* The maximum number of generations is cell_size dependent. This is a */
d918dd36
AT
383 /* soft limit and may be increased if you have plenty of RAM (and a */
384 /* cooperative X server). The value 10,000 was determined empirically. */
b130361b
AT
385 if ((long)state->num_generations * (long)state->cell_size > 10000) {
386 state->num_generations = 10000 / state->cell_size;
d918dd36 387 }
7969381e 388
80cfe219
AT
389 /* Time to figure out which rule to use for this simulation. */
390 /* We ignore any weirdness resulting from the following cast since every */
391 /* bit pattern is also a valid rule; if the user provides weird input, */
392 /* then we'll return weird (but well-defined!) output. */
393 state->rule_requested = (uint8_t) get_integer_resource(state->dpy, "rule-requested", "Integer");
394 state->rule_random = get_boolean_resource(state->dpy, "rule-random", "Boolean");
395 /* Through the following set of branches, we enforce CLI flag precedence. */
396 if (state->rule_random) {
397 /* If this flag is set, the user wants truly random rules rather than */
398 /* random rules from a curated list. */
399 state->rule_number = (uint8_t) random();
400 } else if (state->rule_requested != 0) {
401 /* Rule 0 is terribly uninteresting, so we are reusing it as a 'null' */
402 /* value and hoping nobody notices. Finding a non-zero value means */
403 /* the user requested a specific rule. Use it. */
404 state->rule_number = state->rule_requested;
405 } else {
406 /* No command-line options were specified, so select rules randomly */
407 /* from a curated list. */
14d68c5b
AT
408 size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]);
409 curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements];
410 state->rule_number = curated_ruleset->rule;
411 }
412
413 /* Time to construct the seed generation for this simulation. */
414 state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean");
415 state->population_density = get_integer_resource(state->dpy, "population-density", "Integer");
416 if (state->population_density < 0 || state->population_density > 100) state->population_density = 50;
417 state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells);
418 if (!state->current_generation) {
76b9ae92 419 fprintf(stderr, "ERROR: Failed to calloc() for cell generation in WolframAutomata_init().\n");
14d68c5b
AT
420 exit(EXIT_FAILURE);
421 }
422 if (curated_ruleset) {
423 /* If we're using a curated ruleset, ignore any CLI flags related to */
424 /* setting the seed generation, instead drawing that information from */
425 /* the curated ruleset. */
426 switch (curated_ruleset->seed) {
1f5d1274
AT
427 case random_cell: generate_random_seed(state); break;
428 case middle_cell: state->current_generation[state->number_of_cells/2] = True; break;
429 case edge_cell : state->current_generation[0] = True; break;
14d68c5b
AT
430 }
431 } else {
432 /* If we're not using a curated ruleset, process any relevant flags */
433 /* from the user, falling back to a random seed generation if nothing */
434 /* else is specified. */
435 if (state->population_single) {
436 state->current_generation[0] = True;
437 } else {
438 generate_random_seed(state);
439 }
80cfe219
AT
440 }
441
b130361b
AT
442 state->evolution_history = XCreatePixmap(state->dpy, state->win, state->dpy_width, state->num_generations*state->cell_size, xgwa.depth);
443 /* Pixmap contents are undefined after creation. Explicitly set a black */
444 /* background by drawing a black rectangle over the entire pixmap. */
8c85f136
AT
445 XColor blackx, blacks;
446 XAllocNamedColor(state->dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(state->dpy)), "black", &blacks, &blackx);
447 XSetForeground(state->dpy, state->gc, blacks.pixel);
b130361b 448 XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->dpy_width, state->num_generations*state->cell_size);
7ce88c8e 449 XSetForeground(state->dpy, state->gc, state->fg);
14d68c5b 450 render_current_generation(state);
b130361b 451 state->ypos += state->cell_size;
7ce88c8e
AT
452
453 return state;
0c731d4a
AT
454}
455
0c731d4a
AT
456static unsigned long
457WolframAutomata_draw(Display * dpy, Window win, void * closure)
458{
0c731d4a
AT
459 struct state * state = closure;
460 int xpos;
7ce88c8e 461 int window_y_offset;
0c731d4a 462
b130361b
AT
463 /* Calculate and record new generation. */
464 Bool new_generation[state->dpy_width];
c428f3d5 465 for (xpos = 0; xpos < state->number_of_cells; xpos++) {
7ce88c8e
AT
466 new_generation[xpos] = calculate_cell(state, xpos);
467 }
c428f3d5 468 for (xpos = 0; xpos < state->number_of_cells; xpos++) {
7ce88c8e
AT
469 state->current_generation[xpos] = new_generation[xpos];
470 }
471 render_current_generation(state);
472
b130361b
AT
473 /* Check for end of simulation. */
474 if (state->ypos/state->cell_size < state->num_generations-1) {
475 /* Life continues. */
476 state->ypos += state->cell_size;
7ce88c8e 477 } else {
b130361b
AT
478 /* We have reached the end of this simulation. Give the user a moment */
479 /* to bask in the glory of our output, then reset. */
480 if (state->admiration_in_progress) {
481 WolframAutomata_free(dpy, win, state);
482 closure = WolframAutomata_init(dpy, win);
483 } else {
484 state->admiration_in_progress = True;
4ff197f3 485 return 1000000 * state->admiration_delay;
b130361b 486 }
7ce88c8e
AT
487 }
488
b130361b
AT
489 /* Calculate vertical offset of current 'window' into the CA's history. */
490 /* After the CA evolution exceeds our display extents, make window track */
491 /* current generation, scrolling display to follow newest generation. */
492 if (state->ypos < state->dpy_height) {
7ce88c8e
AT
493 window_y_offset = 0;
494 } else {
b130361b 495 window_y_offset = state->ypos - (state->dpy_height - 1);
7ce88c8e
AT
496 }
497
b130361b
AT
498 /* Render a window into the CA history. */
499 XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->dpy_width, state->dpy_height, 0, 0);
0c731d4a
AT
500
501 return state->delay_microsec;
502}
503
504static const char * WolframAutomata_defaults[] = {
b130361b 505 "*delay-usec: 25000",
4ff197f3 506 "*admiration-delay: 5",
7969381e 507 "*num-generations: 5000",
30934676 508 "*cell-size: 2",
39e6fe44 509 "*color-index: -1",
b130361b
AT
510 "*population-density: 50",
511 "*population-single: False",
30934676 512 "*random-cell-size: False",
b130361b
AT
513 "*random-delay: False",
514 "*random-length: False",
515 "*random-rule: False",
516 "*rule-requested: 0",
0c731d4a
AT
517 0
518};
519
520static XrmOptionDescRec WolframAutomata_options[] = {
b130361b 521 { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 },
4ff197f3 522 { "-admiration-delay", ".admiration-delay", XrmoptionSepArg, 0 },
b130361b 523 { "-num-generations", ".num-generations", XrmoptionSepArg, 0 },
30934676 524 { "-cell-size", ".cell-size", XrmoptionSepArg, 0 },
39e6fe44 525 { "-color-index", ".color-index", XrmoptionSepArg, 0 },
b130361b
AT
526 { "-population-density", ".population-density", XrmoptionSepArg, 0 },
527 { "-population-single", ".population-single", XrmoptionNoArg, "True" },
30934676 528 { "-random-cell-size", ".random-cell-size", XrmoptionNoArg, "True" },
b130361b
AT
529 { "-random-delay", ".random-delay", XrmoptionNoArg, "True" },
530 { "-random-length", ".random-num-generations", XrmoptionNoArg, "True" },
531 { "-random-rule", ".rule-random", XrmoptionNoArg, "True" },
532 { "-rule", ".rule-requested", XrmoptionSepArg, 0 },
0c731d4a
AT
533 { 0, 0, 0, 0 }
534};
535
0c731d4a
AT
536static void
537WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h)
538{
b130361b
AT
539 struct state * state = closure;
540 XWindowAttributes xgwa;
541 XGetWindowAttributes(state->dpy, state->win, &xgwa);
542
543 /* Only restart the simulation if the window changed size. */
544 if (state->dpy_width != xgwa.width || state->dpy_height != xgwa.height) {
545 WolframAutomata_free(dpy, win, closure);
546 closure = WolframAutomata_init(dpy, win);
547 }
0c731d4a
AT
548}
549
b130361b 550XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata)
0c731d4a 551