/* 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. */
+#include <X11/Intrinsic.h>
#include "screenhack.h"
+/*
+ * We do a few manual manipulations of X resources in this hack, like picking
+ * random colors. In order to ensure our manual manipulations always use the
+ * same X resource specification as Xscreensaver, we pass HACKNAME to
+ * Xscreensaver via the XSCREENSAVER_MODULE() line at the bottom of this file,
+ * and then always use HACKNAME or MAKE_STRING(HACKNAME) as the base of the
+ * resource specification when making manual manipulations.
+ */
+#define HACKNAME WolframAutomata
+#define MAKE_STRING_X(s) #s
+#define MAKE_STRING(s) MAKE_STRING_X(s)
+
// Command line options
// directory to output XBM files of each run (and call an external command to convert to PNGs?)
// -save-dir STRING
// 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)
+// -random-colors (highest precedence)
+// -foreground "COLORNAME"
+// -background "COLORNAME"
+// (default is black and white)
+// (mention sample color combinations in manpage, and link to: https://en.wikipedia.org/wiki/X11_color_names)
// 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?
// In order of precedence:
-// -random (select a random rule on each run)
+// -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? Or random? Or one bit in middle? Or one bit on edge? (For random: Can I allow specifying a density like 25%, 50%, 75%?)
-// Options (with precedence): -population STRING (string is a comma separated list of cell IDs to populate, starting from 0)
-// -population-curated
-// -population-random
+// 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 */
Display * dpy;
uint8_t rule_requested; // Note: Repurposing Rule 0 as a null value.
Bool rule_random;
+ // TODO: Describe these.
+ int population_density;
+ Bool population_single;
+
/* 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(). */
size_t number_of_cells;
};
-// TODO: Check the full set of 256 CAs for visually interesting examples.
-static const uint8_t curated_rule_list[] = {
- 22,
- 30,
- 45,
- 57,
- 73,
- 86
+// TODO: Decorations
+enum seed_population {
+ random_cell,
+ middle_cell,
+ edge_cell
+};
+
+// TODO: Decorations
+struct curated_ruleset {
+ uint8_t rule;
+ enum seed_population seed;
+};
+
+// TODO: Decorations
+static const struct curated_ruleset curated_ruleset_list[] = {
+ {18, middle_cell},
+ {30, middle_cell},
+ {45, middle_cell},
+ {54, middle_cell},
+ {57, middle_cell},
+ {73, middle_cell},
+ {105, middle_cell},
+ {109, middle_cell},
+ {129, middle_cell},
+ {133, middle_cell},
+ {135, middle_cell},
+ {150, middle_cell},
+ {30, edge_cell},
+ {45, edge_cell},
+ {57, edge_cell},
+ {60, edge_cell},
+ {75, edge_cell},
+ {107, edge_cell},
+ {110, edge_cell},
+ {133, edge_cell},
+ {137, edge_cell},
+ {169, edge_cell},
+ {225, edge_cell},
+ {22, random_cell},
+ {30, random_cell},
+ {54, random_cell},
+ {62, random_cell},
+ {90, random_cell},
+ {105, random_cell},
+ {108, random_cell},
+ {110, random_cell},
+ {126, random_cell},
+ {146, random_cell},
+ {150, random_cell},
+ {182, random_cell},
+ {184, random_cell},
+ {225, random_cell},
+ {240, random_cell}
+};
+
+// TODO: Decorations
+struct color_pair {
+ char * fg;
+ char * bg;
+};
+
+// TODO: Decorations
+// TODO: Populate this table with more examples.
+static const struct color_pair color_list[] = {
+ {"white", "black"},
};
+/* -------------------------------------------------------------------------- */
+/* Helper Functions */
+/* -------------------------------------------------------------------------- */
+
+// 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?
+// TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology).
+size_t
+sindex(struct state * state, int index)
+{
+ while (index < 0) {
+ index += state->number_of_cells;
+ }
+ while (index >= state->number_of_cells) {
+ index -= state->number_of_cells;
+ }
+ return (size_t) index;
+}
+
+// TODO: function decorations?
+// TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation.
+Bool
+calculate_cell(struct state * state, int cell_id)
+{
+ uint8_t cell_pattern = 0;
+ int i;
+ for (i = -1; i < 2; i++) {
+ cell_pattern = cell_pattern << 1;
+ if (state->current_generation[sindex(state, cell_id+i)] == True) {
+ cell_pattern |= 1;
+ }
+ }
+ if ((state->rule_number >> cell_pattern) & 1) {
+ return True;
+ } else {
+ return False;
+ }
+}
+
+// TODO: function decorations?
+void
+render_current_generation(struct state * state)
+{
+ size_t 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->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;
state->ylim = xgwa.height;
state->ypos = 0; // TODO: Explain why.
+ if (get_boolean_resource(state->dpy, "random-colors", "Boolean")) {
+ XrmDatabase db = XtDatabase(state->dpy);
+ size_t rand_i = random() % sizeof(color_list)/sizeof(color_list[0]);
+ XrmPutStringResource(&db, MAKE_STRING(HACKNAME) ".background", color_list[rand_i].bg);
+ XrmPutStringResource(&db, MAKE_STRING(HACKNAME) ".foreground", color_list[rand_i].fg);
+ }
+
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);
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 */
} else {
/* No command-line options were specified, so select rules randomly */
/* from a curated list. */
- size_t number_of_array_elements = sizeof(curated_rule_list)/sizeof(curated_rule_list[0]);
- state->rule_number = curated_rule_list[random() % number_of_array_elements];
+ 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_cell: generate_random_seed(state); break;
+ case middle_cell: state->current_generation[state->number_of_cells/2] = True; break;
+ case edge_cell : state->current_generation[0] = 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->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?
- // 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[0] = 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);
- // TODO: Need to draw starting generation on pixmap and increment state->ypos.
+ render_current_generation(state);
+ state->ypos += state->pixel_size;
return state;
}
-// TODO: function decorations?
-// TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology).
-size_t
-sindex(struct state * state, int index)
-{
- while (index < 0) {
- index += state->number_of_cells;
- }
- while (index >= state->number_of_cells) {
- index -= state->number_of_cells;
- }
- return (size_t) index;
-}
-
-// TODO: function decorations?
-// TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation.
-Bool
-calculate_cell(struct state * state, int cell_id)
-{
- uint8_t cell_pattern = 0;
- int i;
- for (i = -1; i < 2; i++) {
- cell_pattern = cell_pattern << 1;
- if (state->current_generation[sindex(state, cell_id+i)] == True) {
- cell_pattern |= 1;
- }
- }
- if ((state->rule_number >> cell_pattern) & 1) {
- return True;
- } else {
- return False;
- }
-}
-
-// TODO: function decorations?
-void
-render_current_generation(struct state * state)
-{
- size_t 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->pixel_size, state->ypos, state->pixel_size, state->pixel_size);
- }
- }
-}
-
static unsigned long
WolframAutomata_draw(Display * dpy, Window win, void * closure)
{
static const char * WolframAutomata_defaults[] = {
".background: black",
".foreground: white",
+ "*random-colors: False",
"*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[] = {
+ { "-background", ".background", XrmoptionSepArg, 0},
+ { "-foreground", ".foreground", XrmoptionSepArg, 0},
+ { "-random-colors", ".random-colors", XrmoptionNoArg, "True"},
{ "-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 }
};
closure = WolframAutomata_init(dpy, win);
}
-XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata)
+XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", HACKNAME)