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 | ||
4 | ||
5 | /* TODO: Write description explaining that this simulates all 1D NN CAs, and explain briefly what all those terms imply. */ | |
6 | /* TODO: Explain things like the topology of the space. */ | |
7 | /* TODO: Explain how the numbering for a CA expands to the actual rules. */ | |
8 | /* TODO: Briefly explain the four different classes of behavior and their implications. */ | |
9 | /* TODO: Include a link to Wikipedia. */ | |
10 | /* TODO: I suppose a lot of this stuff goes in the README instead. */ | |
11 | /* TODO: Explain the data structures in detail. */ | |
12 | /* TODO: Explain all the options, like the various starting conditions. */ | |
76b9ae92 | 13 | /* TODO: Explain all the dependencies like libXpm. */ |
0c731d4a AT |
14 | |
15 | ||
76b9ae92 | 16 | /* TODO: Add a #define for the hack version. */ |
0c731d4a AT |
17 | /* TODO: Check manpage for all functions I use and ensure my includes are correct. I don't want to depend on picking up includes via screenhack.h. */ |
18 | /* TODO: Verify everything in this file is C89. Get rid of things like '//' comments, pack all my declarations upfront, no stdint, etc. */ | |
0c731d4a | 19 | |
d0f3b852 | 20 | #include <X11/Intrinsic.h> |
0c731d4a AT |
21 | #include "screenhack.h" |
22 | ||
d0f3b852 AT |
23 | /* |
24 | * We do a few manual manipulations of X resources in this hack, like picking | |
25 | * random colors. In order to ensure our manual manipulations always use the | |
26 | * same X resource specification as Xscreensaver, we pass HACKNAME to | |
27 | * Xscreensaver via the XSCREENSAVER_MODULE() line at the bottom of this file, | |
28 | * and then always use HACKNAME or MAKE_STRING(HACKNAME) as the base of the | |
29 | * resource specification when making manual manipulations. | |
30 | */ | |
31 | #define HACKNAME WolframAutomata | |
32 | #define MAKE_STRING_X(s) #s | |
33 | #define MAKE_STRING(s) MAKE_STRING_X(s) | |
34 | ||
0c731d4a | 35 | // Command line options |
7ce88c8e | 36 | // directory to output XBM files of each run (and call an external command to convert to PNGs?) |
2b742550 | 37 | // -save-dir STRING |
76b9ae92 AT |
38 | // (could use libXpm to save an XPM and then convert to PNG with ImageMagick) (this is a single function call to go from pixmap -> file) |
39 | // (since it depends on an external library, make this whole feature optional at build-time?) | |
7ce88c8e | 40 | // number of generations to simulate |
76b9ae92 | 41 | // -random-generations |
2b742550 | 42 | // -num-generations N |
7ce88c8e | 43 | // delay time (speed of simulation) |
76b9ae92 | 44 | // -random-delay |
2b742550 | 45 | // -delay-usec N |
7ce88c8e | 46 | // foreground and background color |
d0f3b852 AT |
47 | // -random-colors (highest precedence) |
48 | // -foreground "COLORNAME" | |
49 | // -background "COLORNAME" | |
50 | // (default is black and white) | |
51 | // (mention sample color combinations in manpage, and link to: https://en.wikipedia.org/wiki/X11_color_names) | |
b33d7b7f | 52 | // (note to the user that most color names they can naturally think of (e.g. red, purple, gray, pink, etc) are valid X11 color names for these CLI options.) |
7ce88c8e | 53 | // display info overlay with CA number and start conditions? |
2b742550 | 54 | // -overlay |
7ce88c8e | 55 | // which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples? |
80cfe219 | 56 | // In order of precedence: |
14d68c5b | 57 | // -rule-random (select a random rule on each run) |
80cfe219 AT |
58 | // -rule N (always simulate Rule N on each run) |
59 | // (if neither of the above two are specified, then a random CURATED rule is selected on each run) | |
14d68c5b AT |
60 | // which starting population to use, random or one bit? (for random: allow specifying a density) |
61 | // In order of precedence: | |
62 | // -population-single | |
63 | // -population-random DENSITY | |
64 | // (the two options above only apply to the simulation under the -rule-random or -rule N options. in curated mode, starting population is defined in the curation array) | |
65 | // TODO: In the future, add the option for user to pass list of cell IDs to turn ON. | |
2b742550 | 66 | // size of pixel square (e.g. 1x1, 2x2, 3x3, etc) |
76b9ae92 | 67 | // -random-pixel-size |
2b742550 | 68 | // -pixel-size N |
0c731d4a | 69 | |
14d68c5b AT |
70 | /* -------------------------------------------------------------------------- */ |
71 | /* Data Structures */ | |
72 | /* -------------------------------------------------------------------------- */ | |
73 | ||
0c731d4a | 74 | struct state { |
7ce88c8e AT |
75 | /* Various X resources */ |
76 | Display * dpy; | |
77 | Window win; | |
78 | GC gc; | |
79 | ||
80 | // TODO: Explain that this holds the whole evolution of the CA and the actual displayed visualization is simply a snapshot into this pixmap. | |
81 | Pixmap evolution_history; | |
7ce88c8e AT |
82 | |
83 | // TODO: Explain all of these. | |
7ce88c8e AT |
84 | unsigned long fg, bg; |
85 | int xlim, ylim, ypos; // explain roughly how and where we use these. Note: I'm not thrilled xlim/ylim since they are actually the width of the display, not the limit of the index (off by one). Change those names. | |
86 | Bool display_info; | |
7ce88c8e AT |
87 | |
88 | Bool * current_generation; | |
80cfe219 AT |
89 | |
90 | // TODO: Describe these. | |
91 | uint8_t rule_number; // Note: This is not a CLI option. You're thinking of rule_requested. | |
92 | uint8_t rule_requested; // Note: Repurposing Rule 0 as a null value. | |
93 | Bool rule_random; | |
c428f3d5 | 94 | |
14d68c5b AT |
95 | // TODO: Describe these. |
96 | int population_density; | |
97 | Bool population_single; | |
98 | ||
c428f3d5 AT |
99 | /* Misc Commandline Options */ |
100 | int pixel_size; /* Size of CA cell in pixels (e.g. pixel_size=3 means 3x3 pixels per cell). */ | |
101 | int delay_microsec; /* Requested delay to screenhack framework before next call to WolframAutomata_draw(). */ | |
7969381e | 102 | int num_generations; /* Number of generations of the CA to simulate before restarting. */ |
c428f3d5 AT |
103 | |
104 | /* Expository Variables - Not strictly necessary, but makes some code easier to read. */ | |
105 | size_t number_of_cells; | |
0c731d4a AT |
106 | }; |
107 | ||
14d68c5b AT |
108 | // TODO: Decorations |
109 | enum seed_population { | |
1f5d1274 AT |
110 | random_cell, |
111 | middle_cell, | |
112 | edge_cell | |
14d68c5b AT |
113 | }; |
114 | ||
115 | // TODO: Decorations | |
116 | struct curated_ruleset { | |
117 | uint8_t rule; | |
118 | enum seed_population seed; | |
119 | }; | |
120 | ||
1f5d1274 | 121 | // TODO: Decorations |
14d68c5b | 122 | static const struct curated_ruleset curated_ruleset_list[] = { |
1f5d1274 AT |
123 | {18, middle_cell}, |
124 | {30, middle_cell}, | |
125 | {45, middle_cell}, | |
126 | {54, middle_cell}, | |
127 | {57, middle_cell}, | |
128 | {73, middle_cell}, | |
129 | {105, middle_cell}, | |
130 | {109, middle_cell}, | |
131 | {129, middle_cell}, | |
132 | {133, middle_cell}, | |
133 | {135, middle_cell}, | |
134 | {150, middle_cell}, | |
135 | {30, edge_cell}, | |
136 | {45, edge_cell}, | |
137 | {57, edge_cell}, | |
138 | {60, edge_cell}, | |
139 | {75, edge_cell}, | |
140 | {107, edge_cell}, | |
141 | {110, edge_cell}, | |
142 | {133, edge_cell}, | |
143 | {137, edge_cell}, | |
144 | {169, edge_cell}, | |
145 | {225, edge_cell}, | |
146 | {22, random_cell}, | |
147 | {30, random_cell}, | |
148 | {54, random_cell}, | |
149 | {62, random_cell}, | |
150 | {90, random_cell}, | |
151 | {105, random_cell}, | |
152 | {108, random_cell}, | |
153 | {110, random_cell}, | |
154 | {126, random_cell}, | |
155 | {146, random_cell}, | |
156 | {150, random_cell}, | |
157 | {182, random_cell}, | |
158 | {184, random_cell}, | |
159 | {225, random_cell}, | |
160 | {240, random_cell} | |
80cfe219 AT |
161 | }; |
162 | ||
d0f3b852 AT |
163 | // TODO: Decorations |
164 | struct color_pair { | |
165 | char * fg; | |
166 | char * bg; | |
167 | }; | |
168 | ||
169 | // TODO: Decorations | |
d0f3b852 | 170 | static const struct color_pair color_list[] = { |
b33d7b7f AT |
171 | {"red", "black"}, |
172 | {"olive", "black"}, | |
173 | {"teal", "black"}, | |
174 | {"slateblue", "black"}, | |
175 | {"violet", "black"}, | |
176 | {"purple", "black"}, | |
d0f3b852 | 177 | {"white", "black"}, |
b33d7b7f AT |
178 | {"white", "darkgreen"}, |
179 | {"white", "darkmagenta"}, | |
180 | {"white", "darkred"}, | |
181 | {"white", "darkblue"}, | |
182 | {"darkslategray", "darkslategray1"}, | |
183 | {"lightsteelblue", "darkslategray"}, | |
184 | {"royalblue4", "royalblue"}, | |
185 | {"antiquewhite2", "antiquewhite4"}, | |
186 | {"darkolivegreen1", "darkolivegreen"}, | |
187 | {"darkseagreen1", "darkseagreen4"}, | |
188 | {"pink", "darkred"}, | |
189 | {"lightblue", "darkgreen"}, | |
190 | {"red", "blue"}, | |
191 | {"red", "darkgreen"}, | |
192 | {"aqua", "teal"}, | |
193 | {"darkblue", "teal"}, | |
194 | {"khaki", "seagreen"}, | |
195 | {"khaki", "darkolivegreen"}, | |
196 | {"lightslategray", "darkslategray"}, | |
197 | {"tomato", "darkslategray"}, | |
198 | {"tomato", "darkcyan"} | |
d0f3b852 AT |
199 | }; |
200 | ||
14d68c5b AT |
201 | /* -------------------------------------------------------------------------- */ |
202 | /* Helper Functions */ | |
203 | /* -------------------------------------------------------------------------- */ | |
204 | ||
205 | // TODO: decorations? inline? | |
206 | void | |
207 | generate_random_seed(struct state * state) | |
208 | { | |
209 | int i; | |
210 | for (i = 0; i < state->number_of_cells; i++) { | |
211 | state->current_generation[i] = ((random() % 100) < state->population_density) ? True : False; | |
212 | } | |
213 | } | |
214 | ||
215 | // TODO: function decorations? | |
216 | // TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology). | |
217 | size_t | |
218 | sindex(struct state * state, int index) | |
219 | { | |
220 | while (index < 0) { | |
221 | index += state->number_of_cells; | |
222 | } | |
223 | while (index >= state->number_of_cells) { | |
224 | index -= state->number_of_cells; | |
225 | } | |
226 | return (size_t) index; | |
227 | } | |
228 | ||
229 | // TODO: function decorations? | |
230 | // TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation. | |
231 | Bool | |
232 | calculate_cell(struct state * state, int cell_id) | |
233 | { | |
234 | uint8_t cell_pattern = 0; | |
235 | int i; | |
236 | for (i = -1; i < 2; i++) { | |
237 | cell_pattern = cell_pattern << 1; | |
238 | if (state->current_generation[sindex(state, cell_id+i)] == True) { | |
239 | cell_pattern |= 1; | |
240 | } | |
241 | } | |
242 | if ((state->rule_number >> cell_pattern) & 1) { | |
243 | return True; | |
244 | } else { | |
245 | return False; | |
246 | } | |
247 | } | |
248 | ||
249 | // TODO: function decorations? | |
250 | void | |
251 | render_current_generation(struct state * state) | |
252 | { | |
253 | size_t xpos; | |
254 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { | |
255 | if (state->current_generation[xpos] == True) { | |
256 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size); | |
8c85f136 AT |
257 | } else { |
258 | XSetForeground(state->dpy, state->gc, state->bg); | |
259 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size); | |
260 | XSetForeground(state->dpy, state->gc, state->fg); | |
14d68c5b AT |
261 | } |
262 | } | |
263 | } | |
264 | ||
265 | /* -------------------------------------------------------------------------- */ | |
266 | /* Screenhack API Functions */ | |
267 | /* -------------------------------------------------------------------------- */ | |
268 | ||
0c731d4a AT |
269 | static void * |
270 | WolframAutomata_init(Display * dpy, Window win) | |
271 | { | |
76b9ae92 AT |
272 | struct state * state = calloc(1, sizeof(*state)); |
273 | if (!state) { | |
274 | fprintf(stderr, "ERROR: Failed to calloc() for state struct in WolframAutomata_init().\n"); | |
275 | exit(EXIT_FAILURE); | |
276 | } | |
277 | ||
7ce88c8e AT |
278 | XGCValues gcv; |
279 | XWindowAttributes xgwa; | |
14d68c5b | 280 | const struct curated_ruleset * curated_ruleset = NULL; |
7ce88c8e AT |
281 | |
282 | state->dpy = dpy; | |
283 | state->win = win; | |
284 | ||
285 | XGetWindowAttributes(state->dpy, state->win, &xgwa); | |
286 | state->xlim = xgwa.width; | |
287 | state->ylim = xgwa.height; | |
288 | state->ypos = 0; // TODO: Explain why. | |
289 | ||
d0f3b852 AT |
290 | if (get_boolean_resource(state->dpy, "random-colors", "Boolean")) { |
291 | XrmDatabase db = XtDatabase(state->dpy); | |
292 | size_t rand_i = random() % sizeof(color_list)/sizeof(color_list[0]); | |
293 | XrmPutStringResource(&db, MAKE_STRING(HACKNAME) ".background", color_list[rand_i].bg); | |
294 | XrmPutStringResource(&db, MAKE_STRING(HACKNAME) ".foreground", color_list[rand_i].fg); | |
295 | } | |
296 | ||
7ce88c8e AT |
297 | state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); |
298 | state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); | |
299 | state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); | |
300 | ||
d918dd36 AT |
301 | /* Set the size of each simulated cell as NxN pixels for pixel_size=N. */ |
302 | if (get_boolean_resource(state->dpy, "random-pixel-size", "Boolean")) { | |
303 | /* Although we are choosing the pixel size 'randomly', a truly random */ | |
304 | /* selection would bias toward large numbers since there are more of */ | |
305 | /* them. To avoid this, we select a random number for a bit shift, */ | |
306 | /* resulting in a pixel size of 1, 2, 4, 8, 16 or 32, equally likely. */ | |
307 | state->pixel_size = 1 << (random() % 6); | |
308 | } else { | |
309 | state->pixel_size = get_integer_resource(state->dpy, "pixel-size", "Integer"); | |
310 | } | |
c428f3d5 AT |
311 | if (state->pixel_size < 1) state->pixel_size = 1; |
312 | if (state->pixel_size > state->xlim) state->pixel_size = state->xlim; | |
313 | ||
314 | state->number_of_cells = state->xlim / state->pixel_size; | |
14d68c5b | 315 | // TODO: Do we want to enforce that number_of_cells > 0? |
c428f3d5 | 316 | |
d918dd36 AT |
317 | /* Set the delay (in microseconds) between simulation of each generation */ |
318 | /* of the simulation, also known as the delay between calls to */ | |
319 | /* WolframAutomata_draw(), which simulates one generation per call. */ | |
320 | if (get_boolean_resource(state->dpy, "random-delay", "Boolean")) { | |
321 | /* When randomly setting the delay, the problem is to avoid being too */ | |
322 | /* fast or too slow, as well as ensuring slower speeds are chosen */ | |
323 | /* with the same likelihood as faster speeds, as perceived by a */ | |
324 | /* human. By empirical observation, we note that for 1x1 up to 4x4 */ | |
325 | /* pixel cell sizes, values for state->delay_microsec between */ | |
326 | /* 2048 (2^11) and 16556 (2^14) produce pleasant scroll rates. To */ | |
327 | /* maintain this appearance, we bitshift state->pixel_size down until */ | |
328 | /* it is a maximum of 4x4 pixels in size, record how many bitshifts */ | |
329 | /* took place, and then shift our valid window for */ | |
330 | /* state->delay_microsec up by an equal number of bitshifts. For */ | |
331 | /* example, if state->pixel_size=9, then it takes one right shift to */ | |
332 | /* reach state->pixel_size=4. Thus, the valid window for */ | |
333 | /* state->delay_microsec becomes 4096 (2^12) up to 32768 (2^15). */ | |
334 | size_t pixel_shift_range = 1; | |
335 | size_t pixel_size_temp = state->pixel_size; | |
336 | while (pixel_size_temp > 4) { | |
337 | pixel_size_temp >>= 1; | |
338 | pixel_shift_range++; | |
339 | } | |
340 | /* In the below line, '3' represents the total range, namely '14-11' */ | |
341 | /* from '2^14' and '2^11' as the endpoints. Similarly, the '11' in */ | |
342 | /* the below line represents the starting point of this range, from */ | |
343 | /* the exponent in '2^11'. */ | |
344 | state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range); | |
345 | } else { | |
346 | state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer"); | |
347 | } | |
348 | if (state->delay_microsec < 0) state->delay_microsec = 0; | |
349 | ||
350 | /* Set the number of generations to simulate before wiping the simulation */ | |
351 | /* and re-running with new settings. */ | |
352 | if (get_boolean_resource(state->dpy, "random-num-generations", "Boolean")) { | |
353 | /* By empirical observation, keep the product */ | |
354 | /* state->num_generations * state->pixel_size */ | |
355 | /* below 10,000 to avoid BadAlloc errors from the X server due to */ | |
356 | /* requesting an enormous pixmap. This value works on both a 12 core */ | |
357 | /* Xeon with 108 GiB of RAM and a Sun Ultra 2 with 2 GiB of RAM. */ | |
358 | state->num_generations = random() % (10000 / state->pixel_size); | |
359 | /* Ensure selected value is large enough to at least fill the screen. */ | |
360 | /* Cast to avoid overflow. */ | |
361 | if ((long)state->num_generations * (long)state->pixel_size < state->ylim) { | |
362 | state->num_generations = (state->ylim / state->pixel_size) + 1; | |
363 | } | |
364 | } else { | |
365 | state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer"); | |
366 | } | |
80cfe219 AT |
367 | /* The minimum number of generations is 2 since we must allocate enough */ |
368 | /* space to hold the seed generation and at least one pass through */ | |
369 | /* WolframAutomata_draw(), which is where we check whether or not we've */ | |
370 | /* reached the end of the pixmap. */ | |
7969381e | 371 | if (state->num_generations < 0) state->num_generations = 2; |
d918dd36 AT |
372 | /* The maximum number of generations is pixel_size dependent. This is a */ |
373 | /* soft limit and may be increased if you have plenty of RAM (and a */ | |
374 | /* cooperative X server). The value 10,000 was determined empirically. */ | |
375 | if ((long)state->num_generations * (long)state->pixel_size > 10000) { | |
376 | state->num_generations = 10000 / state->pixel_size; | |
377 | } | |
7969381e | 378 | |
80cfe219 AT |
379 | /* Time to figure out which rule to use for this simulation. */ |
380 | /* We ignore any weirdness resulting from the following cast since every */ | |
381 | /* bit pattern is also a valid rule; if the user provides weird input, */ | |
382 | /* then we'll return weird (but well-defined!) output. */ | |
383 | state->rule_requested = (uint8_t) get_integer_resource(state->dpy, "rule-requested", "Integer"); | |
384 | state->rule_random = get_boolean_resource(state->dpy, "rule-random", "Boolean"); | |
385 | /* Through the following set of branches, we enforce CLI flag precedence. */ | |
386 | if (state->rule_random) { | |
387 | /* If this flag is set, the user wants truly random rules rather than */ | |
388 | /* random rules from a curated list. */ | |
389 | state->rule_number = (uint8_t) random(); | |
390 | } else if (state->rule_requested != 0) { | |
391 | /* Rule 0 is terribly uninteresting, so we are reusing it as a 'null' */ | |
392 | /* value and hoping nobody notices. Finding a non-zero value means */ | |
393 | /* the user requested a specific rule. Use it. */ | |
394 | state->rule_number = state->rule_requested; | |
395 | } else { | |
396 | /* No command-line options were specified, so select rules randomly */ | |
397 | /* from a curated list. */ | |
14d68c5b AT |
398 | size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]); |
399 | curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements]; | |
400 | state->rule_number = curated_ruleset->rule; | |
401 | } | |
402 | ||
403 | /* Time to construct the seed generation for this simulation. */ | |
404 | state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean"); | |
405 | state->population_density = get_integer_resource(state->dpy, "population-density", "Integer"); | |
406 | if (state->population_density < 0 || state->population_density > 100) state->population_density = 50; | |
407 | state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells); | |
408 | if (!state->current_generation) { | |
76b9ae92 | 409 | fprintf(stderr, "ERROR: Failed to calloc() for cell generation in WolframAutomata_init().\n"); |
14d68c5b AT |
410 | exit(EXIT_FAILURE); |
411 | } | |
412 | if (curated_ruleset) { | |
413 | /* If we're using a curated ruleset, ignore any CLI flags related to */ | |
414 | /* setting the seed generation, instead drawing that information from */ | |
415 | /* the curated ruleset. */ | |
416 | switch (curated_ruleset->seed) { | |
1f5d1274 AT |
417 | case random_cell: generate_random_seed(state); break; |
418 | case middle_cell: state->current_generation[state->number_of_cells/2] = True; break; | |
419 | case edge_cell : state->current_generation[0] = True; break; | |
14d68c5b AT |
420 | } |
421 | } else { | |
422 | /* If we're not using a curated ruleset, process any relevant flags */ | |
423 | /* from the user, falling back to a random seed generation if nothing */ | |
424 | /* else is specified. */ | |
425 | if (state->population_single) { | |
426 | state->current_generation[0] = True; | |
427 | } else { | |
428 | generate_random_seed(state); | |
429 | } | |
80cfe219 AT |
430 | } |
431 | ||
7ce88c8e AT |
432 | // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first. |
433 | state->display_info = True; | |
7ce88c8e | 434 | |
c428f3d5 | 435 | state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth); |
7ce88c8e AT |
436 | // Pixmap contents are undefined after creation. Explicitly set a black |
437 | // background by drawing a black rectangle over the entire pixmap. | |
8c85f136 AT |
438 | XColor blackx, blacks; |
439 | XAllocNamedColor(state->dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(state->dpy)), "black", &blacks, &blackx); | |
440 | XSetForeground(state->dpy, state->gc, blacks.pixel); | |
c428f3d5 | 441 | XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size); |
7ce88c8e | 442 | XSetForeground(state->dpy, state->gc, state->fg); |
14d68c5b AT |
443 | render_current_generation(state); |
444 | state->ypos += state->pixel_size; | |
7ce88c8e AT |
445 | |
446 | return state; | |
0c731d4a AT |
447 | } |
448 | ||
0c731d4a AT |
449 | static unsigned long |
450 | WolframAutomata_draw(Display * dpy, Window win, void * closure) | |
451 | { | |
452 | // TODO: Mark these basic sections of the function | |
453 | //draw() | |
7ce88c8e AT |
454 | // calculate (and store) new generation |
455 | // draw new generation as line of pixels on pixmap | |
456 | // calculate current 'viewport' into pixmap | |
457 | // display on screen | |
0c731d4a AT |
458 | // check for termination condition |
459 | ||
460 | struct state * state = closure; | |
461 | int xpos; | |
7ce88c8e | 462 | int window_y_offset; |
0c731d4a | 463 | |
7ce88c8e | 464 | Bool new_generation[state->xlim]; |
c428f3d5 | 465 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
466 | new_generation[xpos] = calculate_cell(state, xpos); |
467 | } | |
c428f3d5 | 468 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
469 | state->current_generation[xpos] = new_generation[xpos]; |
470 | } | |
471 | render_current_generation(state); | |
472 | ||
473 | // Was this the final generation of this particular simulation? If so, give | |
474 | // the user a moment to bask in the glory of our output and then start a | |
475 | // new simulation. | |
c428f3d5 AT |
476 | if (state->ypos/state->pixel_size < state->num_generations-1) { |
477 | state->ypos += state->pixel_size; | |
7ce88c8e AT |
478 | } else { |
479 | // TODO: Wait for a second or two, clear the screen and do a new iteration with suitably changed settings. | |
480 | // Note: Since we can't actually loop or sleep here, we need to add a flag to the state struct to indicate that we're in an 'admiration timewindow' (and indicate when it should end) | |
c428f3d5 | 481 | printf("infinite hamster wheel\n"); |
7ce88c8e AT |
482 | while (1) continue; |
483 | } | |
484 | ||
485 | // Calculate the vertical offset of the current 'window' into the history | |
486 | // of the CA. After the CA's evolution extends past what we can display, have | |
487 | // the window track the current generation and most recent history. | |
488 | if (state->ypos < state->ylim) { | |
489 | window_y_offset = 0; | |
490 | } else { | |
491 | window_y_offset = state->ypos - (state->ylim - 1); | |
492 | } | |
493 | ||
494 | // Render everything to the display. | |
495 | XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->xlim, state->ylim, 0, 0); | |
496 | // TODO: Print info on screen if display_info is true. Will need fonts/etc. Do I want to create a separate pixmap for this during the init() function and then just copy the pixmap each time we draw the screen in draw()? | |
0c731d4a AT |
497 | |
498 | return state->delay_microsec; | |
499 | } | |
500 | ||
c428f3d5 | 501 | // TODO: Fix formatting |
0c731d4a AT |
502 | static const char * WolframAutomata_defaults[] = { |
503 | ".background: black", | |
504 | ".foreground: white", | |
d0f3b852 | 505 | "*random-colors: False", |
80cfe219 | 506 | "*delay-usec: 25000", |
7969381e AT |
507 | // TODO: Difference between dot and asterisk? Presumably the asterisk matches all resouces of attribute "pixelsize"? Apply answer to all new options. |
508 | "*pixel-size: 2", | |
509 | "*num-generations: 5000", | |
80cfe219 AT |
510 | "*rule-requested: 0", |
511 | "*rule-random: False", | |
14d68c5b AT |
512 | "*population-density: 50", |
513 | "*population-single: False", | |
d918dd36 AT |
514 | "*random-delay: False", |
515 | "*random-pixel-size: False", | |
516 | "*random-num-generations: False", | |
0c731d4a AT |
517 | 0 |
518 | }; | |
519 | ||
c428f3d5 | 520 | // TODO: Fix formatting |
0c731d4a | 521 | static XrmOptionDescRec WolframAutomata_options[] = { |
d0f3b852 AT |
522 | { "-background", ".background", XrmoptionSepArg, 0}, |
523 | { "-foreground", ".foreground", XrmoptionSepArg, 0}, | |
524 | { "-random-colors", ".random-colors", XrmoptionNoArg, "True"}, | |
c428f3d5 AT |
525 | { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 }, |
526 | { "-pixel-size", ".pixel-size", XrmoptionSepArg, 0 }, | |
7969381e | 527 | { "-num-generations", ".num-generations", XrmoptionSepArg, 0 }, |
80cfe219 AT |
528 | { "-rule", ".rule-requested", XrmoptionSepArg, 0 }, |
529 | { "-rule-random", ".rule-random", XrmoptionNoArg, "True" }, | |
14d68c5b AT |
530 | { "-population-density", ".population-density", XrmoptionSepArg, 0 }, |
531 | { "-population-single", ".population-single", XrmoptionNoArg, "True" }, | |
d918dd36 AT |
532 | { "-random-delay", ".random-delay", XrmoptionNoArg, "True" }, |
533 | { "-random-pixel-size", ".random-pixel-size", XrmoptionNoArg, "True" }, | |
534 | { "-random-num-generations", ".random-num-generations", XrmoptionNoArg, "True" }, | |
535 | ||
0c731d4a AT |
536 | { 0, 0, 0, 0 } |
537 | }; | |
538 | ||
539 | static Bool | |
540 | WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event) | |
541 | { | |
542 | return False; | |
543 | } | |
544 | ||
545 | static void | |
546 | WolframAutomata_free(Display * dpy, Window win, void * closure) | |
547 | { | |
548 | struct state * state = closure; | |
549 | XFreeGC(state->dpy, state->gc); | |
7ce88c8e AT |
550 | XFreePixmap(state->dpy, state->evolution_history); |
551 | free(state->current_generation); | |
0c731d4a AT |
552 | free(state); |
553 | } | |
554 | ||
555 | static void | |
556 | WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) | |
557 | { | |
7ce88c8e | 558 | WolframAutomata_free(dpy, win, closure); |
b0ea929b | 559 | closure = WolframAutomata_init(dpy, win); |
0c731d4a AT |
560 | } |
561 | ||
d0f3b852 | 562 | XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", HACKNAME) |
0c731d4a | 563 |