X-Git-Url: http://git.subgeniuskitty.com/screensavers/.git/blobdiff_plain/7ce88c8e5a6ac5b1d1ccd2fa90e241b21e76ab44..14d68c5b4f6569f9f2e58f04984cce31fffcc36b:/hacks/WolframAutomata/WolframAutomata.c diff --git a/hacks/WolframAutomata/WolframAutomata.c b/hacks/WolframAutomata/WolframAutomata.c index 3ccb242..4f882a2 100644 --- a/hacks/WolframAutomata/WolframAutomata.c +++ b/hacks/WolframAutomata/WolframAutomata.c @@ -14,18 +14,37 @@ /* 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. */ /* TODO: Verify everything in this file is C89. Get rid of things like '//' comments, pack all my declarations upfront, no stdint, etc. */ -/* TODO: Tabs -> Spaces before each commit. */ #include "screenhack.h" // Command line options // directory to output XBM files of each run (and call an external command to convert to PNGs?) +// -save-dir STRING // number of generations to simulate +// -num-generations N // delay time (speed of simulation) +// -delay-usec N // foreground and background color +// ??? (strings of some sort, but I need to look up what X resources to interact with) // display info overlay with CA number and start conditions? +// -overlay // which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples? -// 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%?) +// In order of precedence: +// -rule-random (select a random rule on each run) +// -rule N (always simulate Rule N on each run) +// (if neither of the above two are specified, then a random CURATED rule is selected on each run) +// which starting population to use, random or one bit? (for random: allow specifying a density) +// In order of precedence: +// -population-single +// -population-random DENSITY +// (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) +// TODO: In the future, add the option for user to pass list of cell IDs to turn ON. +// size of pixel square (e.g. 1x1, 2x2, 3x3, etc) +// -pixel-size N + +/* -------------------------------------------------------------------------- */ +/* Data Structures */ +/* -------------------------------------------------------------------------- */ struct state { /* Various X resources */ @@ -35,60 +54,64 @@ struct state { // TODO: Explain that this holds the whole evolution of the CA and the actual displayed visualization is simply a snapshot into this pixmap. Pixmap evolution_history; - size_t num_generations; // TODO: Explain all of these. - int delay_microsec; // per generation unsigned long fg, bg; 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. Bool display_info; - // 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. Bool * current_generation; - uint8_t ruleset; -}; -static void * -WolframAutomata_init(Display * dpy, Window win) -{ - struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call - XGCValues gcv; - XWindowAttributes xgwa; + // TODO: Describe these. + uint8_t rule_number; // Note: This is not a CLI option. You're thinking of rule_requested. + uint8_t rule_requested; // Note: Repurposing Rule 0 as a null value. + Bool rule_random; - state->dpy = dpy; - state->win = win; + // TODO: Describe these. + int population_density; + Bool population_single; - XGetWindowAttributes(state->dpy, state->win, &xgwa); - state->xlim = xgwa.width; - state->ylim = xgwa.height; - state->ypos = 0; // TODO: Explain why. + /* Misc Commandline Options */ + int pixel_size; /* Size of CA cell in pixels (e.g. pixel_size=3 means 3x3 pixels per cell). */ + int delay_microsec; /* Requested delay to screenhack framework before next call to WolframAutomata_draw(). */ + int num_generations; /* Number of generations of the CA to simulate before restarting. */ - state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); - state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); - state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); + /* Expository Variables - Not strictly necessary, but makes some code easier to read. */ + size_t number_of_cells; +}; - state->delay_microsec = get_integer_resource(state->dpy, "delay", "Integer"); - if (state->delay_microsec < 0) state->delay_microsec = 0; +// TODO: Decorations +enum seed_population { + left_only, + middle_only, + right_only, + random_seed +}; - // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first. - state->display_info = True; - state->ruleset = 30; - 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. +// TODO: Decorations +struct curated_ruleset { + uint8_t rule; + enum seed_population seed; +}; - 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? - // TODO: Make the starting state a user-configurable option. At least give the user some options like 'random', 'one-middle', 'one edge', etc. - // Ideally accept something like a list of integers representing starting pixels to be "on". - state->current_generation[state->xlim-1] = True; +// TODO: Check the full set of 256 CAs for visually interesting examples. +// TODO: Add comments explaining why each ruleset is interesting. +static const struct curated_ruleset curated_ruleset_list[] = { + {110, random_seed} +}; - state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations, xgwa.depth); - // Pixmap contents are undefined after creation. Explicitly set a black - // background by drawing a black rectangle over the entire pixmap. - XSetForeground(state->dpy, state->gc, state->bg); - XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations); - XSetForeground(state->dpy, state->gc, state->fg); - // TODO: Need to draw starting generation on pixmap and increment state->ypos. +/* -------------------------------------------------------------------------- */ +/* Helper Functions */ +/* -------------------------------------------------------------------------- */ - return state; +// TODO: decorations? inline? +void +generate_random_seed(struct state * state) +{ + int i; + for (i = 0; i < state->number_of_cells; i++) { + state->current_generation[i] = ((random() % 100) < state->population_density) ? True : False; + } } // TODO: function decorations? @@ -97,10 +120,10 @@ size_t sindex(struct state * state, int index) { while (index < 0) { - index += state->xlim; + index += state->number_of_cells; } - while (index >= state->xlim) { - index -= state->xlim; + while (index >= state->number_of_cells) { + index -= state->number_of_cells; } return (size_t) index; } @@ -118,7 +141,7 @@ calculate_cell(struct state * state, int cell_id) cell_pattern |= 1; } } - if ((state->ruleset >> cell_pattern) & 1) { + if ((state->rule_number >> cell_pattern) & 1) { return True; } else { return False; @@ -130,13 +153,123 @@ void render_current_generation(struct state * state) { size_t xpos; - for (xpos = 0; xpos < state->xlim; xpos++) { + for (xpos = 0; xpos < state->number_of_cells; xpos++) { if (state->current_generation[xpos] == True) { - XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos, state->ypos, 1, 1); + XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size); } } } +/* -------------------------------------------------------------------------- */ +/* Screenhack API Functions */ +/* -------------------------------------------------------------------------- */ + +static void * +WolframAutomata_init(Display * dpy, Window win) +{ + struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call + XGCValues gcv; + XWindowAttributes xgwa; + const struct curated_ruleset * curated_ruleset = NULL; + + state->dpy = dpy; + state->win = win; + + XGetWindowAttributes(state->dpy, state->win, &xgwa); + state->xlim = xgwa.width; + state->ylim = xgwa.height; + state->ypos = 0; // TODO: Explain why. + + state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); + state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); + state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); + + state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer"); + if (state->delay_microsec < 0) state->delay_microsec = 0; + + state->pixel_size = get_integer_resource(state->dpy, "pixel-size", "Integer"); + if (state->pixel_size < 1) state->pixel_size = 1; + if (state->pixel_size > state->xlim) state->pixel_size = state->xlim; + + state->number_of_cells = state->xlim / state->pixel_size; + // TODO: Do we want to enforce that number_of_cells > 0? + + /* The minimum number of generations is 2 since we must allocate enough */ + /* space to hold the seed generation and at least one pass through */ + /* WolframAutomata_draw(), which is where we check whether or not we've */ + /* reached the end of the pixmap. */ + state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer"); + if (state->num_generations < 0) state->num_generations = 2; + + /* Time to figure out which rule to use for this simulation. */ + /* We ignore any weirdness resulting from the following cast since every */ + /* bit pattern is also a valid rule; if the user provides weird input, */ + /* then we'll return weird (but well-defined!) output. */ + state->rule_requested = (uint8_t) get_integer_resource(state->dpy, "rule-requested", "Integer"); + state->rule_random = get_boolean_resource(state->dpy, "rule-random", "Boolean"); + /* Through the following set of branches, we enforce CLI flag precedence. */ + if (state->rule_random) { + /* If this flag is set, the user wants truly random rules rather than */ + /* random rules from a curated list. */ + state->rule_number = (uint8_t) random(); + } else if (state->rule_requested != 0) { + /* Rule 0 is terribly uninteresting, so we are reusing it as a 'null' */ + /* value and hoping nobody notices. Finding a non-zero value means */ + /* the user requested a specific rule. Use it. */ + state->rule_number = state->rule_requested; + } else { + /* No command-line options were specified, so select rules randomly */ + /* from a curated list. */ + size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]); + curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements]; + state->rule_number = curated_ruleset->rule; + } + + /* Time to construct the seed generation for this simulation. */ + state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean"); + state->population_density = get_integer_resource(state->dpy, "population-density", "Integer"); + if (state->population_density < 0 || state->population_density > 100) state->population_density = 50; + state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells); + if (!state->current_generation) { + fprintf(stderr, "ERROR: Failed to calloc() in WolframAutomata_init().\n"); + exit(EXIT_FAILURE); + } + if (curated_ruleset) { + /* If we're using a curated ruleset, ignore any CLI flags related to */ + /* setting the seed generation, instead drawing that information from */ + /* the curated ruleset. */ + switch (curated_ruleset->seed) { + case random_seed: generate_random_seed(state); break; + case left_only: state->current_generation[0] = True; break; + case right_only: state->current_generation[state->number_of_cells-1] = True; break; + case middle_only: state->current_generation[state->number_of_cells/2] = True; break; + } + } else { + /* If we're not using a curated ruleset, process any relevant flags */ + /* from the user, falling back to a random seed generation if nothing */ + /* else is specified. */ + if (state->population_single) { + state->current_generation[0] = True; + } else { + generate_random_seed(state); + } + } + + // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first. + state->display_info = True; + + state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth); + // Pixmap contents are undefined after creation. Explicitly set a black + // background by drawing a black rectangle over the entire pixmap. + XSetForeground(state->dpy, state->gc, state->bg); + XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size); + XSetForeground(state->dpy, state->gc, state->fg); + render_current_generation(state); + state->ypos += state->pixel_size; + + return state; +} + static unsigned long WolframAutomata_draw(Display * dpy, Window win, void * closure) { @@ -153,10 +286,10 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure) int window_y_offset; Bool new_generation[state->xlim]; - for (xpos = 0; xpos < state->xlim; xpos++) { + for (xpos = 0; xpos < state->number_of_cells; xpos++) { new_generation[xpos] = calculate_cell(state, xpos); } - for (xpos = 0; xpos < state->xlim; xpos++) { + for (xpos = 0; xpos < state->number_of_cells; xpos++) { state->current_generation[xpos] = new_generation[xpos]; } render_current_generation(state); @@ -164,11 +297,12 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure) // Was this the final generation of this particular simulation? If so, give // the user a moment to bask in the glory of our output and then start a // new simulation. - if (state->ypos < state->num_generations-1) { - state->ypos++; + if (state->ypos/state->pixel_size < state->num_generations-1) { + state->ypos += state->pixel_size; } else { // TODO: Wait for a second or two, clear the screen and do a new iteration with suitably changed settings. // 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) + printf("infinite hamster wheel\n"); while (1) continue; } @@ -188,15 +322,30 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure) return state->delay_microsec; } +// TODO: Fix formatting static const char * WolframAutomata_defaults[] = { ".background: black", ".foreground: white", - "*delay: 2500", + "*delay-usec: 25000", + // TODO: Difference between dot and asterisk? Presumably the asterisk matches all resouces of attribute "pixelsize"? Apply answer to all new options. + "*pixel-size: 2", + "*num-generations: 5000", + "*rule-requested: 0", + "*rule-random: False", + "*population-density: 50", + "*population-single: False", 0 }; +// TODO: Fix formatting static XrmOptionDescRec WolframAutomata_options[] = { - { "-delay", ".delay", XrmoptionSepArg, 0 }, + { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 }, + { "-pixel-size", ".pixel-size", XrmoptionSepArg, 0 }, + { "-num-generations", ".num-generations", XrmoptionSepArg, 0 }, + { "-rule", ".rule-requested", XrmoptionSepArg, 0 }, + { "-rule-random", ".rule-random", XrmoptionNoArg, "True" }, + { "-population-density", ".population-density", XrmoptionSepArg, 0 }, + { "-population-single", ".population-single", XrmoptionNoArg, "True" }, { 0, 0, 0, 0 } }; @@ -220,7 +369,7 @@ static void WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) { WolframAutomata_free(dpy, win, closure); - WolframAutomata_init(dpy, win); + closure = WolframAutomata_init(dpy, win); } XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata)