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