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