Commit | Line | Data |
---|---|---|
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 | 10 | struct 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 AT |
48 | /* At the end of the simulation, the user is given time to admire the */ |
49 | /* output. Delay is available to user as CLI option. */ | |
50 | Bool admiration_in_progress; | |
51 | size_t admiration_delay; /* ...in microseconds. */ | |
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 | 64 | enum seed_population { |
1f5d1274 AT |
65 | random_cell, |
66 | middle_cell, | |
67 | edge_cell | |
14d68c5b AT |
68 | }; |
69 | ||
14d68c5b AT |
70 | struct curated_ruleset { |
71 | uint8_t rule; | |
72 | enum seed_population seed; | |
73 | }; | |
74 | ||
14d68c5b | 75 | static 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 | 116 | struct 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 | 136 | static 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 | 179 | static void |
14d68c5b AT |
180 | generate_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. */ | |
191 | static size_t | |
14d68c5b AT |
192 | sindex(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 */ | |
205 | static Bool | |
14d68c5b AT |
206 | calculate_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 | 223 | static void |
14d68c5b AT |
224 | render_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 |
242 | static Bool |
243 | WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event) | |
244 | { | |
245 | return False; | |
246 | } | |
247 | ||
248 | static void | |
249 | WolframAutomata_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 |
258 | static void * |
259 | WolframAutomata_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 | ||
279 | state->admiration_delay = 5000000; | |
280 | state->admiration_in_progress = False; | |
7ce88c8e | 281 | |
d0f3b852 | 282 | if (get_boolean_resource(state->dpy, "random-colors", "Boolean")) { |
b130361b | 283 | XColor fg, bg; |
d0f3b852 | 284 | size_t rand_i = random() % sizeof(color_list)/sizeof(color_list[0]); |
b130361b AT |
285 | fg.red = color_list[rand_i].fg_red; |
286 | fg.green = color_list[rand_i].fg_green; | |
287 | fg.blue = color_list[rand_i].fg_blue; | |
288 | bg.red = color_list[rand_i].bg_red; | |
289 | bg.green = color_list[rand_i].bg_green; | |
290 | bg.blue = color_list[rand_i].bg_blue; | |
291 | /* TODO: Since I 'alloc', presumably I must also 'free' these colors */ | |
292 | /* at some point. Where/how? I don't want to eventually crash my */ | |
293 | /* X server after months of use. */ | |
294 | XAllocColor(state->dpy, xgwa.colormap, &fg); | |
295 | XAllocColor(state->dpy, xgwa.colormap, &bg); | |
296 | state->fg = gcv.foreground = fg.pixel; | |
297 | state->bg = gcv.background = bg.pixel; | |
298 | } else { | |
299 | state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); | |
300 | state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); | |
d0f3b852 AT |
301 | } |
302 | ||
7ce88c8e AT |
303 | state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); |
304 | ||
b130361b | 305 | /* Set the size of each simulated cell to NxN pixels for cell_size=N. */ |
d918dd36 AT |
306 | if (get_boolean_resource(state->dpy, "random-pixel-size", "Boolean")) { |
307 | /* Although we are choosing the pixel size 'randomly', a truly random */ | |
308 | /* selection would bias toward large numbers since there are more of */ | |
309 | /* them. To avoid this, we select a random number for a bit shift, */ | |
310 | /* resulting in a pixel size of 1, 2, 4, 8, 16 or 32, equally likely. */ | |
b130361b | 311 | state->cell_size = 1 << (random() % 6); |
d918dd36 | 312 | } else { |
b130361b | 313 | state->cell_size = get_integer_resource(state->dpy, "pixel-size", "Integer"); |
d918dd36 | 314 | } |
b130361b AT |
315 | if (state->cell_size < 1) state->cell_size = 1; |
316 | if (state->cell_size > state->dpy_width) state->cell_size = state->dpy_width; | |
c428f3d5 | 317 | |
b130361b AT |
318 | /* Larger cell sizes won't always evenly divide the number of pixels in */ |
319 | /* our window. In order to avoid a black stripe down the edge, '+1' here */ | |
320 | /* to ensure we are slightly oversize rather than undersize. */ | |
321 | state->number_of_cells = (state->dpy_width / state->cell_size) + 1; | |
c428f3d5 | 322 | |
d918dd36 AT |
323 | /* Set the delay (in microseconds) between simulation of each generation */ |
324 | /* of the simulation, also known as the delay between calls to */ | |
325 | /* WolframAutomata_draw(), which simulates one generation per call. */ | |
326 | if (get_boolean_resource(state->dpy, "random-delay", "Boolean")) { | |
327 | /* When randomly setting the delay, the problem is to avoid being too */ | |
328 | /* fast or too slow, as well as ensuring slower speeds are chosen */ | |
329 | /* with the same likelihood as faster speeds, as perceived by a */ | |
330 | /* human. By empirical observation, we note that for 1x1 up to 4x4 */ | |
331 | /* pixel cell sizes, values for state->delay_microsec between */ | |
332 | /* 2048 (2^11) and 16556 (2^14) produce pleasant scroll rates. To */ | |
b130361b | 333 | /* maintain this appearance, we bitshift state->cell_size down until */ |
d918dd36 AT |
334 | /* it is a maximum of 4x4 pixels in size, record how many bitshifts */ |
335 | /* took place, and then shift our valid window for */ | |
336 | /* state->delay_microsec up by an equal number of bitshifts. For */ | |
b130361b AT |
337 | /* example, if state->cell_size=9, then it takes one right shift to */ |
338 | /* reach state->cell_size=4. Thus, the valid window for */ | |
d918dd36 AT |
339 | /* state->delay_microsec becomes 4096 (2^12) up to 32768 (2^15). */ |
340 | size_t pixel_shift_range = 1; | |
b130361b AT |
341 | size_t cell_size_temp = state->cell_size; |
342 | while (cell_size_temp > 4) { | |
343 | cell_size_temp >>= 1; | |
d918dd36 AT |
344 | pixel_shift_range++; |
345 | } | |
346 | /* In the below line, '3' represents the total range, namely '14-11' */ | |
347 | /* from '2^14' and '2^11' as the endpoints. Similarly, the '11' in */ | |
348 | /* the below line represents the starting point of this range, from */ | |
349 | /* the exponent in '2^11'. */ | |
350 | state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range); | |
351 | } else { | |
352 | state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer"); | |
353 | } | |
354 | if (state->delay_microsec < 0) state->delay_microsec = 0; | |
355 | ||
356 | /* Set the number of generations to simulate before wiping the simulation */ | |
357 | /* and re-running with new settings. */ | |
358 | if (get_boolean_resource(state->dpy, "random-num-generations", "Boolean")) { | |
359 | /* By empirical observation, keep the product */ | |
b130361b | 360 | /* state->num_generations * state->cell_size */ |
d918dd36 AT |
361 | /* below 10,000 to avoid BadAlloc errors from the X server due to */ |
362 | /* requesting an enormous pixmap. This value works on both a 12 core */ | |
363 | /* Xeon with 108 GiB of RAM and a Sun Ultra 2 with 2 GiB of RAM. */ | |
b130361b | 364 | state->num_generations = random() % (10000 / state->cell_size); |
d918dd36 AT |
365 | /* Ensure selected value is large enough to at least fill the screen. */ |
366 | /* Cast to avoid overflow. */ | |
b130361b AT |
367 | if ((long)state->num_generations * (long)state->cell_size < state->dpy_height) { |
368 | state->num_generations = (state->dpy_height / state->cell_size) + 1; | |
d918dd36 AT |
369 | } |
370 | } else { | |
371 | state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer"); | |
372 | } | |
80cfe219 AT |
373 | /* The minimum number of generations is 2 since we must allocate enough */ |
374 | /* space to hold the seed generation and at least one pass through */ | |
375 | /* WolframAutomata_draw(), which is where we check whether or not we've */ | |
376 | /* reached the end of the pixmap. */ | |
7969381e | 377 | if (state->num_generations < 0) state->num_generations = 2; |
b130361b | 378 | /* The maximum number of generations is cell_size dependent. This is a */ |
d918dd36 AT |
379 | /* soft limit and may be increased if you have plenty of RAM (and a */ |
380 | /* cooperative X server). The value 10,000 was determined empirically. */ | |
b130361b AT |
381 | if ((long)state->num_generations * (long)state->cell_size > 10000) { |
382 | state->num_generations = 10000 / state->cell_size; | |
d918dd36 | 383 | } |
7969381e | 384 | |
80cfe219 AT |
385 | /* Time to figure out which rule to use for this simulation. */ |
386 | /* We ignore any weirdness resulting from the following cast since every */ | |
387 | /* bit pattern is also a valid rule; if the user provides weird input, */ | |
388 | /* then we'll return weird (but well-defined!) output. */ | |
389 | state->rule_requested = (uint8_t) get_integer_resource(state->dpy, "rule-requested", "Integer"); | |
390 | state->rule_random = get_boolean_resource(state->dpy, "rule-random", "Boolean"); | |
391 | /* Through the following set of branches, we enforce CLI flag precedence. */ | |
392 | if (state->rule_random) { | |
393 | /* If this flag is set, the user wants truly random rules rather than */ | |
394 | /* random rules from a curated list. */ | |
395 | state->rule_number = (uint8_t) random(); | |
396 | } else if (state->rule_requested != 0) { | |
397 | /* Rule 0 is terribly uninteresting, so we are reusing it as a 'null' */ | |
398 | /* value and hoping nobody notices. Finding a non-zero value means */ | |
399 | /* the user requested a specific rule. Use it. */ | |
400 | state->rule_number = state->rule_requested; | |
401 | } else { | |
402 | /* No command-line options were specified, so select rules randomly */ | |
403 | /* from a curated list. */ | |
14d68c5b AT |
404 | size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]); |
405 | curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements]; | |
406 | state->rule_number = curated_ruleset->rule; | |
407 | } | |
408 | ||
409 | /* Time to construct the seed generation for this simulation. */ | |
410 | state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean"); | |
411 | state->population_density = get_integer_resource(state->dpy, "population-density", "Integer"); | |
412 | if (state->population_density < 0 || state->population_density > 100) state->population_density = 50; | |
413 | state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells); | |
414 | if (!state->current_generation) { | |
76b9ae92 | 415 | fprintf(stderr, "ERROR: Failed to calloc() for cell generation in WolframAutomata_init().\n"); |
14d68c5b AT |
416 | exit(EXIT_FAILURE); |
417 | } | |
418 | if (curated_ruleset) { | |
419 | /* If we're using a curated ruleset, ignore any CLI flags related to */ | |
420 | /* setting the seed generation, instead drawing that information from */ | |
421 | /* the curated ruleset. */ | |
422 | switch (curated_ruleset->seed) { | |
1f5d1274 AT |
423 | case random_cell: generate_random_seed(state); break; |
424 | case middle_cell: state->current_generation[state->number_of_cells/2] = True; break; | |
425 | case edge_cell : state->current_generation[0] = True; break; | |
14d68c5b AT |
426 | } |
427 | } else { | |
428 | /* If we're not using a curated ruleset, process any relevant flags */ | |
429 | /* from the user, falling back to a random seed generation if nothing */ | |
430 | /* else is specified. */ | |
431 | if (state->population_single) { | |
432 | state->current_generation[0] = True; | |
433 | } else { | |
434 | generate_random_seed(state); | |
435 | } | |
80cfe219 AT |
436 | } |
437 | ||
b130361b AT |
438 | state->evolution_history = XCreatePixmap(state->dpy, state->win, state->dpy_width, state->num_generations*state->cell_size, xgwa.depth); |
439 | /* Pixmap contents are undefined after creation. Explicitly set a black */ | |
440 | /* background by drawing a black rectangle over the entire pixmap. */ | |
8c85f136 AT |
441 | XColor blackx, blacks; |
442 | XAllocNamedColor(state->dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(state->dpy)), "black", &blacks, &blackx); | |
443 | XSetForeground(state->dpy, state->gc, blacks.pixel); | |
b130361b | 444 | XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->dpy_width, state->num_generations*state->cell_size); |
7ce88c8e | 445 | XSetForeground(state->dpy, state->gc, state->fg); |
14d68c5b | 446 | render_current_generation(state); |
b130361b | 447 | state->ypos += state->cell_size; |
7ce88c8e AT |
448 | |
449 | return state; | |
0c731d4a AT |
450 | } |
451 | ||
0c731d4a AT |
452 | static unsigned long |
453 | WolframAutomata_draw(Display * dpy, Window win, void * closure) | |
454 | { | |
0c731d4a AT |
455 | struct state * state = closure; |
456 | int xpos; | |
7ce88c8e | 457 | int window_y_offset; |
0c731d4a | 458 | |
b130361b AT |
459 | /* Calculate and record new generation. */ |
460 | Bool new_generation[state->dpy_width]; | |
c428f3d5 | 461 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
462 | new_generation[xpos] = calculate_cell(state, xpos); |
463 | } | |
c428f3d5 | 464 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
465 | state->current_generation[xpos] = new_generation[xpos]; |
466 | } | |
467 | render_current_generation(state); | |
468 | ||
b130361b AT |
469 | /* Check for end of simulation. */ |
470 | if (state->ypos/state->cell_size < state->num_generations-1) { | |
471 | /* Life continues. */ | |
472 | state->ypos += state->cell_size; | |
7ce88c8e | 473 | } else { |
b130361b AT |
474 | /* We have reached the end of this simulation. Give the user a moment */ |
475 | /* to bask in the glory of our output, then reset. */ | |
476 | if (state->admiration_in_progress) { | |
477 | WolframAutomata_free(dpy, win, state); | |
478 | closure = WolframAutomata_init(dpy, win); | |
479 | } else { | |
480 | state->admiration_in_progress = True; | |
481 | return state->admiration_delay; | |
482 | } | |
7ce88c8e AT |
483 | } |
484 | ||
b130361b AT |
485 | /* Calculate vertical offset of current 'window' into the CA's history. */ |
486 | /* After the CA evolution exceeds our display extents, make window track */ | |
487 | /* current generation, scrolling display to follow newest generation. */ | |
488 | if (state->ypos < state->dpy_height) { | |
7ce88c8e AT |
489 | window_y_offset = 0; |
490 | } else { | |
b130361b | 491 | window_y_offset = state->ypos - (state->dpy_height - 1); |
7ce88c8e AT |
492 | } |
493 | ||
b130361b AT |
494 | /* Render a window into the CA history. */ |
495 | XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->dpy_width, state->dpy_height, 0, 0); | |
0c731d4a AT |
496 | |
497 | return state->delay_microsec; | |
498 | } | |
499 | ||
500 | static const char * WolframAutomata_defaults[] = { | |
b130361b | 501 | "*delay-usec: 25000", |
7969381e | 502 | "*num-generations: 5000", |
b130361b AT |
503 | "*pixel-size: 2", |
504 | "*population-density: 50", | |
505 | "*population-single: False", | |
506 | "*random-cellsize: False", | |
507 | "*random-color: False", | |
508 | "*random-delay: False", | |
509 | "*random-length: False", | |
510 | "*random-rule: False", | |
511 | "*rule-requested: 0", | |
0c731d4a AT |
512 | 0 |
513 | }; | |
514 | ||
515 | static XrmOptionDescRec WolframAutomata_options[] = { | |
b130361b AT |
516 | { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 }, |
517 | { "-num-generations", ".num-generations", XrmoptionSepArg, 0 }, | |
518 | { "-pixel-size", ".pixel-size", XrmoptionSepArg, 0 }, | |
519 | { "-population-density", ".population-density", XrmoptionSepArg, 0 }, | |
520 | { "-population-single", ".population-single", XrmoptionNoArg, "True" }, | |
521 | { "-random-cellsize", ".random-pixel-size", XrmoptionNoArg, "True" }, | |
522 | { "-random-color", ".random-colors", XrmoptionNoArg, "True" }, | |
523 | { "-random-delay", ".random-delay", XrmoptionNoArg, "True" }, | |
524 | { "-random-length", ".random-num-generations", XrmoptionNoArg, "True" }, | |
525 | { "-random-rule", ".rule-random", XrmoptionNoArg, "True" }, | |
526 | { "-rule", ".rule-requested", XrmoptionSepArg, 0 }, | |
0c731d4a AT |
527 | { 0, 0, 0, 0 } |
528 | }; | |
529 | ||
0c731d4a AT |
530 | static void |
531 | WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) | |
532 | { | |
b130361b AT |
533 | struct state * state = closure; |
534 | XWindowAttributes xgwa; | |
535 | XGetWindowAttributes(state->dpy, state->win, &xgwa); | |
536 | ||
537 | /* Only restart the simulation if the window changed size. */ | |
538 | if (state->dpy_width != xgwa.width || state->dpy_height != xgwa.height) { | |
539 | WolframAutomata_free(dpy, win, closure); | |
540 | closure = WolframAutomata_init(dpy, win); | |
541 | } | |
0c731d4a AT |
542 | } |
543 | ||
b130361b | 544 | XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata) |
0c731d4a | 545 |