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