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. */ | |
13 | ||
14 | ||
15 | /* 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. */ | |
16 | /* TODO: Verify everything in this file is C89. Get rid of things like '//' comments, pack all my declarations upfront, no stdint, etc. */ | |
0c731d4a AT |
17 | |
18 | #include "screenhack.h" | |
19 | ||
20 | // Command line options | |
7ce88c8e | 21 | // directory to output XBM files of each run (and call an external command to convert to PNGs?) |
2b742550 | 22 | // -save-dir STRING |
7ce88c8e | 23 | // number of generations to simulate |
2b742550 | 24 | // -num-generations N |
7ce88c8e | 25 | // delay time (speed of simulation) |
2b742550 | 26 | // -delay-usec N |
7ce88c8e | 27 | // foreground and background color |
2b742550 | 28 | // ??? (strings of some sort, but I need to look up what X resources to interact with) |
7ce88c8e | 29 | // display info overlay with CA number and start conditions? |
2b742550 | 30 | // -overlay |
7ce88c8e | 31 | // which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples? |
80cfe219 AT |
32 | // In order of precedence: |
33 | // -random (select a random rule on each run) | |
34 | // -rule N (always simulate Rule N on each run) | |
35 | // (if neither of the above two are specified, then a random CURATED rule is selected on each run) | |
7ce88c8e | 36 | // which starting population to use? Or random? Or one bit in middle? Or one bit on edge? (For random: Can I allow specifying a density like 25%, 50%, 75%?) |
2b742550 AT |
37 | // Options (with precedence): -population STRING (string is a comma separated list of cell IDs to populate, starting from 0) |
38 | // -population-curated | |
39 | // -population-random | |
40 | // size of pixel square (e.g. 1x1, 2x2, 3x3, etc) | |
41 | // -pixel-size N | |
0c731d4a AT |
42 | |
43 | struct state { | |
7ce88c8e AT |
44 | /* Various X resources */ |
45 | Display * dpy; | |
46 | Window win; | |
47 | GC gc; | |
48 | ||
49 | // TODO: Explain that this holds the whole evolution of the CA and the actual displayed visualization is simply a snapshot into this pixmap. | |
50 | Pixmap evolution_history; | |
7ce88c8e AT |
51 | |
52 | // TODO: Explain all of these. | |
7ce88c8e AT |
53 | unsigned long fg, bg; |
54 | 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. | |
55 | Bool display_info; | |
7ce88c8e AT |
56 | |
57 | Bool * current_generation; | |
80cfe219 AT |
58 | |
59 | // TODO: Describe these. | |
60 | uint8_t rule_number; // Note: This is not a CLI option. You're thinking of rule_requested. | |
61 | uint8_t rule_requested; // Note: Repurposing Rule 0 as a null value. | |
62 | Bool rule_random; | |
c428f3d5 AT |
63 | |
64 | /* Misc Commandline Options */ | |
65 | int pixel_size; /* Size of CA cell in pixels (e.g. pixel_size=3 means 3x3 pixels per cell). */ | |
66 | int delay_microsec; /* Requested delay to screenhack framework before next call to WolframAutomata_draw(). */ | |
7969381e | 67 | int num_generations; /* Number of generations of the CA to simulate before restarting. */ |
c428f3d5 AT |
68 | |
69 | /* Expository Variables - Not strictly necessary, but makes some code easier to read. */ | |
70 | size_t number_of_cells; | |
0c731d4a AT |
71 | }; |
72 | ||
80cfe219 AT |
73 | // TODO: Check the full set of 256 CAs for visually interesting examples. |
74 | static const uint8_t curated_rule_list[] = { | |
75 | 22, | |
76 | 30, | |
77 | 45, | |
78 | 57, | |
79 | 73, | |
80 | 86 | |
81 | }; | |
82 | ||
0c731d4a AT |
83 | static void * |
84 | WolframAutomata_init(Display * dpy, Window win) | |
85 | { | |
7ce88c8e AT |
86 | struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call |
87 | XGCValues gcv; | |
88 | XWindowAttributes xgwa; | |
89 | ||
90 | state->dpy = dpy; | |
91 | state->win = win; | |
92 | ||
93 | XGetWindowAttributes(state->dpy, state->win, &xgwa); | |
94 | state->xlim = xgwa.width; | |
95 | state->ylim = xgwa.height; | |
96 | state->ypos = 0; // TODO: Explain why. | |
97 | ||
98 | state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); | |
99 | state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); | |
100 | state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); | |
101 | ||
c428f3d5 | 102 | state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer"); |
7ce88c8e AT |
103 | if (state->delay_microsec < 0) state->delay_microsec = 0; |
104 | ||
c428f3d5 AT |
105 | state->pixel_size = get_integer_resource(state->dpy, "pixel-size", "Integer"); |
106 | if (state->pixel_size < 1) state->pixel_size = 1; | |
107 | if (state->pixel_size > state->xlim) state->pixel_size = state->xlim; | |
108 | ||
109 | state->number_of_cells = state->xlim / state->pixel_size; | |
110 | ||
80cfe219 AT |
111 | /* The minimum number of generations is 2 since we must allocate enough */ |
112 | /* space to hold the seed generation and at least one pass through */ | |
113 | /* WolframAutomata_draw(), which is where we check whether or not we've */ | |
114 | /* reached the end of the pixmap. */ | |
7969381e AT |
115 | state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer"); |
116 | if (state->num_generations < 0) state->num_generations = 2; | |
117 | ||
80cfe219 AT |
118 | /* Time to figure out which rule to use for this simulation. */ |
119 | /* We ignore any weirdness resulting from the following cast since every */ | |
120 | /* bit pattern is also a valid rule; if the user provides weird input, */ | |
121 | /* then we'll return weird (but well-defined!) output. */ | |
122 | state->rule_requested = (uint8_t) get_integer_resource(state->dpy, "rule-requested", "Integer"); | |
123 | state->rule_random = get_boolean_resource(state->dpy, "rule-random", "Boolean"); | |
124 | /* Through the following set of branches, we enforce CLI flag precedence. */ | |
125 | if (state->rule_random) { | |
126 | /* If this flag is set, the user wants truly random rules rather than */ | |
127 | /* random rules from a curated list. */ | |
128 | state->rule_number = (uint8_t) random(); | |
129 | } else if (state->rule_requested != 0) { | |
130 | /* Rule 0 is terribly uninteresting, so we are reusing it as a 'null' */ | |
131 | /* value and hoping nobody notices. Finding a non-zero value means */ | |
132 | /* the user requested a specific rule. Use it. */ | |
133 | state->rule_number = state->rule_requested; | |
134 | } else { | |
135 | /* No command-line options were specified, so select rules randomly */ | |
136 | /* from a curated list. */ | |
137 | size_t number_of_array_elements = sizeof(curated_rule_list)/sizeof(curated_rule_list[0]); | |
138 | state->rule_number = curated_rule_list[random() % number_of_array_elements]; | |
139 | } | |
140 | ||
7ce88c8e AT |
141 | // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first. |
142 | state->display_info = True; | |
7ce88c8e | 143 | |
c428f3d5 | 144 | state->current_generation = calloc(1, (sizeof(*(state->current_generation))*state->number_of_cells)); // TODO: Check calloc() call TODO: Can't recall precedence; can I eliminate any parenthesis? |
7ce88c8e AT |
145 | // TODO: Make the starting state a user-configurable option. At least give the user some options like 'random', 'one-middle', 'one edge', etc. |
146 | // Ideally accept something like a list of integers representing starting pixels to be "on". | |
c428f3d5 | 147 | state->current_generation[0] = True; |
7ce88c8e | 148 | |
c428f3d5 | 149 | state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth); |
7ce88c8e AT |
150 | // Pixmap contents are undefined after creation. Explicitly set a black |
151 | // background by drawing a black rectangle over the entire pixmap. | |
152 | XSetForeground(state->dpy, state->gc, state->bg); | |
c428f3d5 | 153 | XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size); |
7ce88c8e AT |
154 | XSetForeground(state->dpy, state->gc, state->fg); |
155 | // TODO: Need to draw starting generation on pixmap and increment state->ypos. | |
156 | ||
157 | return state; | |
0c731d4a AT |
158 | } |
159 | ||
160 | // TODO: function decorations? | |
161 | // TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology). | |
162 | size_t | |
163 | sindex(struct state * state, int index) | |
164 | { | |
7ce88c8e | 165 | while (index < 0) { |
c428f3d5 | 166 | index += state->number_of_cells; |
7ce88c8e | 167 | } |
c428f3d5 AT |
168 | while (index >= state->number_of_cells) { |
169 | index -= state->number_of_cells; | |
7ce88c8e AT |
170 | } |
171 | return (size_t) index; | |
0c731d4a AT |
172 | } |
173 | ||
174 | // TODO: function decorations? | |
175 | // TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation. | |
176 | Bool | |
177 | calculate_cell(struct state * state, int cell_id) | |
178 | { | |
7ce88c8e AT |
179 | uint8_t cell_pattern = 0; |
180 | int i; | |
181 | for (i = -1; i < 2; i++) { | |
182 | cell_pattern = cell_pattern << 1; | |
183 | if (state->current_generation[sindex(state, cell_id+i)] == True) { | |
184 | cell_pattern |= 1; | |
185 | } | |
186 | } | |
80cfe219 | 187 | if ((state->rule_number >> cell_pattern) & 1) { |
7ce88c8e AT |
188 | return True; |
189 | } else { | |
190 | return False; | |
191 | } | |
0c731d4a AT |
192 | } |
193 | ||
194 | // TODO: function decorations? | |
195 | void | |
196 | render_current_generation(struct state * state) | |
197 | { | |
7ce88c8e | 198 | size_t xpos; |
c428f3d5 | 199 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e | 200 | if (state->current_generation[xpos] == True) { |
c428f3d5 | 201 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size); |
7ce88c8e AT |
202 | } |
203 | } | |
0c731d4a AT |
204 | } |
205 | ||
206 | static unsigned long | |
207 | WolframAutomata_draw(Display * dpy, Window win, void * closure) | |
208 | { | |
209 | // TODO: Mark these basic sections of the function | |
210 | //draw() | |
7ce88c8e AT |
211 | // calculate (and store) new generation |
212 | // draw new generation as line of pixels on pixmap | |
213 | // calculate current 'viewport' into pixmap | |
214 | // display on screen | |
0c731d4a AT |
215 | // check for termination condition |
216 | ||
217 | struct state * state = closure; | |
218 | int xpos; | |
7ce88c8e | 219 | int window_y_offset; |
0c731d4a | 220 | |
7ce88c8e | 221 | Bool new_generation[state->xlim]; |
c428f3d5 | 222 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
223 | new_generation[xpos] = calculate_cell(state, xpos); |
224 | } | |
c428f3d5 | 225 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
226 | state->current_generation[xpos] = new_generation[xpos]; |
227 | } | |
228 | render_current_generation(state); | |
229 | ||
230 | // Was this the final generation of this particular simulation? If so, give | |
231 | // the user a moment to bask in the glory of our output and then start a | |
232 | // new simulation. | |
c428f3d5 AT |
233 | if (state->ypos/state->pixel_size < state->num_generations-1) { |
234 | state->ypos += state->pixel_size; | |
7ce88c8e AT |
235 | } else { |
236 | // TODO: Wait for a second or two, clear the screen and do a new iteration with suitably changed settings. | |
237 | // 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 | 238 | printf("infinite hamster wheel\n"); |
7ce88c8e AT |
239 | while (1) continue; |
240 | } | |
241 | ||
242 | // Calculate the vertical offset of the current 'window' into the history | |
243 | // of the CA. After the CA's evolution extends past what we can display, have | |
244 | // the window track the current generation and most recent history. | |
245 | if (state->ypos < state->ylim) { | |
246 | window_y_offset = 0; | |
247 | } else { | |
248 | window_y_offset = state->ypos - (state->ylim - 1); | |
249 | } | |
250 | ||
251 | // Render everything to the display. | |
252 | XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->xlim, state->ylim, 0, 0); | |
253 | // 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 |
254 | |
255 | return state->delay_microsec; | |
256 | } | |
257 | ||
c428f3d5 | 258 | // TODO: Fix formatting |
0c731d4a AT |
259 | static const char * WolframAutomata_defaults[] = { |
260 | ".background: black", | |
261 | ".foreground: white", | |
80cfe219 | 262 | "*delay-usec: 25000", |
7969381e AT |
263 | // TODO: Difference between dot and asterisk? Presumably the asterisk matches all resouces of attribute "pixelsize"? Apply answer to all new options. |
264 | "*pixel-size: 2", | |
265 | "*num-generations: 5000", | |
80cfe219 AT |
266 | "*rule-requested: 0", |
267 | "*rule-random: False", | |
0c731d4a AT |
268 | 0 |
269 | }; | |
270 | ||
c428f3d5 | 271 | // TODO: Fix formatting |
0c731d4a | 272 | static XrmOptionDescRec WolframAutomata_options[] = { |
c428f3d5 AT |
273 | { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 }, |
274 | { "-pixel-size", ".pixel-size", XrmoptionSepArg, 0 }, | |
7969381e | 275 | { "-num-generations", ".num-generations", XrmoptionSepArg, 0 }, |
80cfe219 AT |
276 | { "-rule", ".rule-requested", XrmoptionSepArg, 0 }, |
277 | { "-rule-random", ".rule-random", XrmoptionNoArg, "True" }, | |
0c731d4a AT |
278 | { 0, 0, 0, 0 } |
279 | }; | |
280 | ||
281 | static Bool | |
282 | WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event) | |
283 | { | |
284 | return False; | |
285 | } | |
286 | ||
287 | static void | |
288 | WolframAutomata_free(Display * dpy, Window win, void * closure) | |
289 | { | |
290 | struct state * state = closure; | |
291 | XFreeGC(state->dpy, state->gc); | |
7ce88c8e AT |
292 | XFreePixmap(state->dpy, state->evolution_history); |
293 | free(state->current_generation); | |
0c731d4a AT |
294 | free(state); |
295 | } | |
296 | ||
297 | static void | |
298 | WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) | |
299 | { | |
7ce88c8e | 300 | WolframAutomata_free(dpy, win, closure); |
b0ea929b | 301 | closure = WolframAutomata_init(dpy, win); |
0c731d4a AT |
302 | } |
303 | ||
304 | XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata) | |
305 |