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