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. */ | |
17 | /* TODO: Tabs -> Spaces before each commit. */ | |
18 | ||
19 | #include "screenhack.h" | |
20 | ||
21 | // Command line options | |
7ce88c8e | 22 | // directory to output XBM files of each run (and call an external command to convert to PNGs?) |
2b742550 | 23 | // -save-dir STRING |
7ce88c8e | 24 | // number of generations to simulate |
2b742550 | 25 | // -num-generations N |
7ce88c8e | 26 | // delay time (speed of simulation) |
2b742550 | 27 | // -delay-usec N |
7ce88c8e | 28 | // foreground and background color |
2b742550 | 29 | // ??? (strings of some sort, but I need to look up what X resources to interact with) |
7ce88c8e | 30 | // display info overlay with CA number and start conditions? |
2b742550 | 31 | // -overlay |
7ce88c8e | 32 | // which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples? |
2b742550 AT |
33 | // Options (with precedence): -rule N |
34 | // -rule-curated | |
35 | // -rule-random | |
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; | |
51 | size_t num_generations; | |
52 | ||
53 | // TODO: Explain all of these. | |
7ce88c8e AT |
54 | unsigned long fg, bg; |
55 | 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. | |
56 | Bool display_info; | |
7ce88c8e AT |
57 | |
58 | Bool * current_generation; | |
59 | uint8_t ruleset; | |
c428f3d5 AT |
60 | |
61 | /* Misc Commandline Options */ | |
62 | int pixel_size; /* Size of CA cell in pixels (e.g. pixel_size=3 means 3x3 pixels per cell). */ | |
63 | int delay_microsec; /* Requested delay to screenhack framework before next call to WolframAutomata_draw(). */ | |
64 | ||
65 | /* Expository Variables - Not strictly necessary, but makes some code easier to read. */ | |
66 | size_t number_of_cells; | |
0c731d4a AT |
67 | }; |
68 | ||
69 | static void * | |
70 | WolframAutomata_init(Display * dpy, Window win) | |
71 | { | |
7ce88c8e AT |
72 | struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call |
73 | XGCValues gcv; | |
74 | XWindowAttributes xgwa; | |
75 | ||
76 | state->dpy = dpy; | |
77 | state->win = win; | |
78 | ||
79 | XGetWindowAttributes(state->dpy, state->win, &xgwa); | |
80 | state->xlim = xgwa.width; | |
81 | state->ylim = xgwa.height; | |
82 | state->ypos = 0; // TODO: Explain why. | |
83 | ||
84 | state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); | |
85 | state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); | |
86 | state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); | |
87 | ||
c428f3d5 | 88 | state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer"); |
7ce88c8e AT |
89 | if (state->delay_microsec < 0) state->delay_microsec = 0; |
90 | ||
c428f3d5 AT |
91 | state->pixel_size = get_integer_resource(state->dpy, "pixel-size", "Integer"); |
92 | if (state->pixel_size < 1) state->pixel_size = 1; | |
93 | if (state->pixel_size > state->xlim) state->pixel_size = state->xlim; | |
94 | ||
95 | state->number_of_cells = state->xlim / state->pixel_size; | |
96 | ||
7ce88c8e AT |
97 | // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first. |
98 | state->display_info = True; | |
99 | state->ruleset = 30; | |
c428f3d5 | 100 | state->num_generations = 3000; // TODO: Enforce that this is >1 in order to hold the seed generation and at least one pass through WolframAutomata_draw(), which is where we check for a full pixmap. |
7ce88c8e | 101 | |
c428f3d5 | 102 | 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 |
103 | // TODO: Make the starting state a user-configurable option. At least give the user some options like 'random', 'one-middle', 'one edge', etc. |
104 | // Ideally accept something like a list of integers representing starting pixels to be "on". | |
c428f3d5 | 105 | state->current_generation[0] = True; |
7ce88c8e | 106 | |
c428f3d5 | 107 | state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth); |
7ce88c8e AT |
108 | // Pixmap contents are undefined after creation. Explicitly set a black |
109 | // background by drawing a black rectangle over the entire pixmap. | |
110 | XSetForeground(state->dpy, state->gc, state->bg); | |
c428f3d5 | 111 | XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size); |
7ce88c8e AT |
112 | XSetForeground(state->dpy, state->gc, state->fg); |
113 | // TODO: Need to draw starting generation on pixmap and increment state->ypos. | |
114 | ||
115 | return state; | |
0c731d4a AT |
116 | } |
117 | ||
118 | // TODO: function decorations? | |
119 | // TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology). | |
120 | size_t | |
121 | sindex(struct state * state, int index) | |
122 | { | |
7ce88c8e | 123 | while (index < 0) { |
c428f3d5 | 124 | index += state->number_of_cells; |
7ce88c8e | 125 | } |
c428f3d5 AT |
126 | while (index >= state->number_of_cells) { |
127 | index -= state->number_of_cells; | |
7ce88c8e AT |
128 | } |
129 | return (size_t) index; | |
0c731d4a AT |
130 | } |
131 | ||
132 | // TODO: function decorations? | |
133 | // TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation. | |
134 | Bool | |
135 | calculate_cell(struct state * state, int cell_id) | |
136 | { | |
7ce88c8e AT |
137 | uint8_t cell_pattern = 0; |
138 | int i; | |
139 | for (i = -1; i < 2; i++) { | |
140 | cell_pattern = cell_pattern << 1; | |
141 | if (state->current_generation[sindex(state, cell_id+i)] == True) { | |
142 | cell_pattern |= 1; | |
143 | } | |
144 | } | |
145 | if ((state->ruleset >> cell_pattern) & 1) { | |
146 | return True; | |
147 | } else { | |
148 | return False; | |
149 | } | |
0c731d4a AT |
150 | } |
151 | ||
152 | // TODO: function decorations? | |
153 | void | |
154 | render_current_generation(struct state * state) | |
155 | { | |
7ce88c8e | 156 | size_t xpos; |
c428f3d5 | 157 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e | 158 | if (state->current_generation[xpos] == True) { |
c428f3d5 | 159 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size); |
7ce88c8e AT |
160 | } |
161 | } | |
0c731d4a AT |
162 | } |
163 | ||
164 | static unsigned long | |
165 | WolframAutomata_draw(Display * dpy, Window win, void * closure) | |
166 | { | |
167 | // TODO: Mark these basic sections of the function | |
168 | //draw() | |
7ce88c8e AT |
169 | // calculate (and store) new generation |
170 | // draw new generation as line of pixels on pixmap | |
171 | // calculate current 'viewport' into pixmap | |
172 | // display on screen | |
0c731d4a AT |
173 | // check for termination condition |
174 | ||
175 | struct state * state = closure; | |
176 | int xpos; | |
7ce88c8e | 177 | int window_y_offset; |
0c731d4a | 178 | |
7ce88c8e | 179 | Bool new_generation[state->xlim]; |
c428f3d5 | 180 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
181 | new_generation[xpos] = calculate_cell(state, xpos); |
182 | } | |
c428f3d5 | 183 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
7ce88c8e AT |
184 | state->current_generation[xpos] = new_generation[xpos]; |
185 | } | |
186 | render_current_generation(state); | |
187 | ||
188 | // Was this the final generation of this particular simulation? If so, give | |
189 | // the user a moment to bask in the glory of our output and then start a | |
190 | // new simulation. | |
c428f3d5 AT |
191 | if (state->ypos/state->pixel_size < state->num_generations-1) { |
192 | state->ypos += state->pixel_size; | |
7ce88c8e AT |
193 | } else { |
194 | // TODO: Wait for a second or two, clear the screen and do a new iteration with suitably changed settings. | |
195 | // 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 | 196 | printf("infinite hamster wheel\n"); |
7ce88c8e AT |
197 | while (1) continue; |
198 | } | |
199 | ||
200 | // Calculate the vertical offset of the current 'window' into the history | |
201 | // of the CA. After the CA's evolution extends past what we can display, have | |
202 | // the window track the current generation and most recent history. | |
203 | if (state->ypos < state->ylim) { | |
204 | window_y_offset = 0; | |
205 | } else { | |
206 | window_y_offset = state->ypos - (state->ylim - 1); | |
207 | } | |
208 | ||
209 | // Render everything to the display. | |
210 | XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->xlim, state->ylim, 0, 0); | |
211 | // 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 |
212 | |
213 | return state->delay_microsec; | |
214 | } | |
215 | ||
c428f3d5 | 216 | // TODO: Fix formatting |
0c731d4a AT |
217 | static const char * WolframAutomata_defaults[] = { |
218 | ".background: black", | |
219 | ".foreground: white", | |
c428f3d5 AT |
220 | "*delay-usec: 2500", |
221 | "*pixel-size: 1", // TODO: Difference between dot and asterisk? Presumably the asterisk matches all resouces of attribute "pixelsize"? | |
0c731d4a AT |
222 | 0 |
223 | }; | |
224 | ||
c428f3d5 | 225 | // TODO: Fix formatting |
0c731d4a | 226 | static XrmOptionDescRec WolframAutomata_options[] = { |
c428f3d5 AT |
227 | { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 }, |
228 | { "-pixel-size", ".pixel-size", XrmoptionSepArg, 0 }, | |
0c731d4a AT |
229 | { 0, 0, 0, 0 } |
230 | }; | |
231 | ||
232 | static Bool | |
233 | WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event) | |
234 | { | |
235 | return False; | |
236 | } | |
237 | ||
238 | static void | |
239 | WolframAutomata_free(Display * dpy, Window win, void * closure) | |
240 | { | |
241 | struct state * state = closure; | |
242 | XFreeGC(state->dpy, state->gc); | |
7ce88c8e AT |
243 | XFreePixmap(state->dpy, state->evolution_history); |
244 | free(state->current_generation); | |
0c731d4a AT |
245 | free(state); |
246 | } | |
247 | ||
248 | static void | |
249 | WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) | |
250 | { | |
7ce88c8e | 251 | WolframAutomata_free(dpy, win, closure); |
b0ea929b | 252 | closure = WolframAutomata_init(dpy, win); |
0c731d4a AT |
253 | } |
254 | ||
255 | XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata) | |
256 |