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