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 AT |
22 | // directory to output XBM files of each run (and call an external command to convert to PNGs?) |
23 | // number of generations to simulate | |
24 | // delay time (speed of simulation) | |
25 | // foreground and background color | |
26 | // display info overlay with CA number and start conditions? | |
27 | // which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples? | |
28 | // 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%?) | |
0c731d4a AT |
29 | |
30 | struct state { | |
7ce88c8e AT |
31 | /* Various X resources */ |
32 | Display * dpy; | |
33 | Window win; | |
34 | GC gc; | |
35 | ||
36 | // TODO: Explain that this holds the whole evolution of the CA and the actual displayed visualization is simply a snapshot into this pixmap. | |
37 | Pixmap evolution_history; | |
38 | size_t num_generations; | |
39 | ||
40 | // TODO: Explain all of these. | |
41 | int delay_microsec; // per generation | |
42 | unsigned long fg, bg; | |
43 | 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. | |
44 | Bool display_info; | |
45 | // TODO: Add an option for 'pixel size', so the user can define 1x1 or 2x2 or 3x3 or ... pixels. But then I need to deal with leftover pixels. | |
46 | ||
47 | Bool * current_generation; | |
48 | uint8_t ruleset; | |
0c731d4a AT |
49 | }; |
50 | ||
51 | static void * | |
52 | WolframAutomata_init(Display * dpy, Window win) | |
53 | { | |
7ce88c8e AT |
54 | struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call |
55 | XGCValues gcv; | |
56 | XWindowAttributes xgwa; | |
57 | ||
58 | state->dpy = dpy; | |
59 | state->win = win; | |
60 | ||
61 | XGetWindowAttributes(state->dpy, state->win, &xgwa); | |
62 | state->xlim = xgwa.width; | |
63 | state->ylim = xgwa.height; | |
64 | state->ypos = 0; // TODO: Explain why. | |
65 | ||
66 | state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); | |
67 | state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); | |
68 | state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); | |
69 | ||
70 | state->delay_microsec = get_integer_resource(state->dpy, "delay", "Integer"); | |
71 | if (state->delay_microsec < 0) state->delay_microsec = 0; | |
72 | ||
73 | // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first. | |
74 | state->display_info = True; | |
75 | state->ruleset = 30; | |
76 | state->num_generations = 10000; // 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. | |
77 | ||
78 | state->current_generation = calloc(1, (sizeof(*(state->current_generation))*(state->xlim))); // TODO: Check calloc() call TODO: Can't recall precedence; can I eliminate any parenthesis? | |
79 | // TODO: Make the starting state a user-configurable option. At least give the user some options like 'random', 'one-middle', 'one edge', etc. | |
80 | // Ideally accept something like a list of integers representing starting pixels to be "on". | |
81 | state->current_generation[state->xlim-1] = True; | |
82 | ||
83 | state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations, xgwa.depth); | |
84 | // Pixmap contents are undefined after creation. Explicitly set a black | |
85 | // background by drawing a black rectangle over the entire pixmap. | |
86 | XSetForeground(state->dpy, state->gc, state->bg); | |
87 | XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations); | |
88 | XSetForeground(state->dpy, state->gc, state->fg); | |
89 | // TODO: Need to draw starting generation on pixmap and increment state->ypos. | |
90 | ||
91 | return state; | |
0c731d4a AT |
92 | } |
93 | ||
94 | // TODO: function decorations? | |
95 | // TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology). | |
96 | size_t | |
97 | sindex(struct state * state, int index) | |
98 | { | |
7ce88c8e AT |
99 | while (index < 0) { |
100 | index += state->xlim; | |
101 | } | |
102 | while (index >= state->xlim) { | |
103 | index -= state->xlim; | |
104 | } | |
105 | return (size_t) index; | |
0c731d4a AT |
106 | } |
107 | ||
108 | // TODO: function decorations? | |
109 | // TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation. | |
110 | Bool | |
111 | calculate_cell(struct state * state, int cell_id) | |
112 | { | |
7ce88c8e AT |
113 | uint8_t cell_pattern = 0; |
114 | int i; | |
115 | for (i = -1; i < 2; i++) { | |
116 | cell_pattern = cell_pattern << 1; | |
117 | if (state->current_generation[sindex(state, cell_id+i)] == True) { | |
118 | cell_pattern |= 1; | |
119 | } | |
120 | } | |
121 | if ((state->ruleset >> cell_pattern) & 1) { | |
122 | return True; | |
123 | } else { | |
124 | return False; | |
125 | } | |
0c731d4a AT |
126 | } |
127 | ||
128 | // TODO: function decorations? | |
129 | void | |
130 | render_current_generation(struct state * state) | |
131 | { | |
7ce88c8e | 132 | size_t xpos; |
0c731d4a | 133 | for (xpos = 0; xpos < state->xlim; xpos++) { |
7ce88c8e AT |
134 | if (state->current_generation[xpos] == True) { |
135 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos, state->ypos, 1, 1); | |
136 | } | |
137 | } | |
0c731d4a AT |
138 | } |
139 | ||
140 | static unsigned long | |
141 | WolframAutomata_draw(Display * dpy, Window win, void * closure) | |
142 | { | |
143 | // TODO: Mark these basic sections of the function | |
144 | //draw() | |
7ce88c8e AT |
145 | // calculate (and store) new generation |
146 | // draw new generation as line of pixels on pixmap | |
147 | // calculate current 'viewport' into pixmap | |
148 | // display on screen | |
0c731d4a AT |
149 | // check for termination condition |
150 | ||
151 | struct state * state = closure; | |
152 | int xpos; | |
7ce88c8e | 153 | int window_y_offset; |
0c731d4a | 154 | |
7ce88c8e | 155 | Bool new_generation[state->xlim]; |
0c731d4a | 156 | for (xpos = 0; xpos < state->xlim; xpos++) { |
7ce88c8e AT |
157 | new_generation[xpos] = calculate_cell(state, xpos); |
158 | } | |
0c731d4a | 159 | for (xpos = 0; xpos < state->xlim; xpos++) { |
7ce88c8e AT |
160 | state->current_generation[xpos] = new_generation[xpos]; |
161 | } | |
162 | render_current_generation(state); | |
163 | ||
164 | // Was this the final generation of this particular simulation? If so, give | |
165 | // the user a moment to bask in the glory of our output and then start a | |
166 | // new simulation. | |
167 | if (state->ypos < state->num_generations-1) { | |
168 | state->ypos++; | |
169 | } else { | |
170 | // TODO: Wait for a second or two, clear the screen and do a new iteration with suitably changed settings. | |
171 | // 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) | |
172 | while (1) continue; | |
173 | } | |
174 | ||
175 | // Calculate the vertical offset of the current 'window' into the history | |
176 | // of the CA. After the CA's evolution extends past what we can display, have | |
177 | // the window track the current generation and most recent history. | |
178 | if (state->ypos < state->ylim) { | |
179 | window_y_offset = 0; | |
180 | } else { | |
181 | window_y_offset = state->ypos - (state->ylim - 1); | |
182 | } | |
183 | ||
184 | // Render everything to the display. | |
185 | XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->xlim, state->ylim, 0, 0); | |
186 | // 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 |
187 | |
188 | return state->delay_microsec; | |
189 | } | |
190 | ||
191 | static const char * WolframAutomata_defaults[] = { | |
192 | ".background: black", | |
193 | ".foreground: white", | |
194 | "*delay: 2500", | |
195 | 0 | |
196 | }; | |
197 | ||
198 | static XrmOptionDescRec WolframAutomata_options[] = { | |
199 | { "-delay", ".delay", XrmoptionSepArg, 0 }, | |
200 | { 0, 0, 0, 0 } | |
201 | }; | |
202 | ||
203 | static Bool | |
204 | WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event) | |
205 | { | |
206 | return False; | |
207 | } | |
208 | ||
209 | static void | |
210 | WolframAutomata_free(Display * dpy, Window win, void * closure) | |
211 | { | |
212 | struct state * state = closure; | |
213 | XFreeGC(state->dpy, state->gc); | |
7ce88c8e AT |
214 | XFreePixmap(state->dpy, state->evolution_history); |
215 | free(state->current_generation); | |
0c731d4a AT |
216 | free(state); |
217 | } | |
218 | ||
219 | static void | |
220 | WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) | |
221 | { | |
7ce88c8e AT |
222 | WolframAutomata_free(dpy, win, closure); |
223 | WolframAutomata_init(dpy, win); | |
0c731d4a AT |
224 | } |
225 | ||
226 | XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata) | |
227 |