Reworked CLI flags for starting seed in README and WolframAutomata source.
[screensavers] / hacks / WolframAutomata / WolframAutomata.c
index 01246f2..edf1dc3 100644 (file)
@@ -36,11 +36,6 @@ struct state {
     /* the 'evolution_history' Pixmap and subsequently ignored.               */
     Bool * current_generation;
 
     /* the 'evolution_history' Pixmap and subsequently ignored.               */
     Bool * current_generation;
 
-    /* When randomizing the seed generation, we can specify a population      */
-    /* density, or we can restrict to a single living cell.                   */
-    int population_density;
-    Bool population_single;
-
     /* For more information on the encoding used for rule_number and on the   */
     /* method used to apply it: https://en.wikipedia.org/wiki/Wolfram_code    */
     uint8_t rule_number;
     /* For more information on the encoding used for rule_number and on the   */
     /* method used to apply it: https://en.wikipedia.org/wiki/Wolfram_code    */
     uint8_t rule_number;
@@ -53,6 +48,7 @@ struct state {
     /* The following values correspond directly to independent CLI options.   */
     Bool    random_rule;
     int     requested_rule;
     /* The following values correspond directly to independent CLI options.   */
     Bool    random_rule;
     int     requested_rule;
+    int     seed_density;
     int     cell_size; /* If cell_size=N then draw NxN pixels per cell.       */
     int     delay_microsec; /* ...between calls to WolframAutomata_draw().    */
     int     num_generations; /* Reset simulation after this many generations. */
     int     cell_size; /* If cell_size=N then draw NxN pixels per cell.       */
     int     delay_microsec; /* ...between calls to WolframAutomata_draw().    */
     int     num_generations; /* Reset simulation after this many generations. */
@@ -176,12 +172,22 @@ static const struct color_pair color_list[] = {
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
 
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
 
+static void
+randomize_seed_density(struct state * state)
+{
+    switch (random() % 3) {
+        case 0: state->seed_density = 30; break;
+        case 1: state->seed_density = 50; break;
+        case 2: state->seed_density = 70; break;
+    }
+}
+
 static void
 generate_random_seed(struct state * state)
 {
     int i;
     for (i = 0; i < state->number_of_cells; i++) {
 static 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;
+        state->current_generation[i] = ((random() % 100) < state->seed_density) ? True : False;
     }
 }
 
     }
 }
 
@@ -409,9 +415,6 @@ WolframAutomata_init(Display * dpy, Window win)
     }
 
     /* Time to construct the seed generation for this simulation.             */
     }
 
     /* 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() for cell generation in WolframAutomata_init().\n");
     state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells);
     if (!state->current_generation) {
         fprintf(stderr, "ERROR: Failed to calloc() for cell generation in WolframAutomata_init().\n");
@@ -422,7 +425,7 @@ WolframAutomata_init(Display * dpy, Window win)
         /* setting the seed generation, instead drawing that information from */
         /* the curated ruleset.                                               */
         switch (curated_ruleset->seed) {
         /* 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 random_cell: randomize_seed_density(state); 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;
         }
             case middle_cell: state->current_generation[state->number_of_cells/2] = True; break;
             case edge_cell  : state->current_generation[0] = True;                        break;
         }
@@ -430,9 +433,18 @@ WolframAutomata_init(Display * dpy, Window win)
         /* 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 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) {
+        if (get_boolean_resource(state->dpy, "seed-left", "Boolean")) {
             state->current_generation[0] = True;
             state->current_generation[0] = True;
+        } else if (get_boolean_resource(state->dpy, "seed-center", "Boolean")) {
+            state->current_generation[state->number_of_cells/2] = True;
+        } else if (get_boolean_resource(state->dpy, "seed-right", "Boolean")) {
+            state->current_generation[state->number_of_cells-1] = True;
+        } else if (get_integer_resource(state->dpy, "seed-density", "Integer") != -1) {
+            state->seed_density = get_integer_resource(state->dpy, "seed-density", "Integer");
+            if (state->seed_density < 0 || state->seed_density > 100) state->seed_density = 50;
+            generate_random_seed(state);
         } else {
         } else {
+            randomize_seed_density(state);
             generate_random_seed(state);
         }
     }
             generate_random_seed(state);
         }
     }
@@ -505,8 +517,10 @@ static const char * WolframAutomata_defaults[] = {
     "*length:             5000",
     "*cell-size:          2",
     "*color-index:        -1",
     "*length:             5000",
     "*cell-size:          2",
     "*color-index:        -1",
-    "*population-density: 50",
-    "*population-single:  False",
+    "*seed-density:       -1",
+    "*seed-left:          False",
+    "*seed-center:        False",
+    "*seed-right:         False",
     "*random-cell-size:   False",
     "*random-delay:       False",
     "*random-length:      False",
     "*random-cell-size:   False",
     "*random-delay:       False",
     "*random-length:      False",
@@ -521,8 +535,10 @@ static XrmOptionDescRec WolframAutomata_options[] = {
     { "-length",             ".length",                 XrmoptionSepArg, 0      },
     { "-cell-size",          ".cell-size",              XrmoptionSepArg, 0      },
     { "-color-index",        ".color-index",            XrmoptionSepArg, 0      },
     { "-length",             ".length",                 XrmoptionSepArg, 0      },
     { "-cell-size",          ".cell-size",              XrmoptionSepArg, 0      },
     { "-color-index",        ".color-index",            XrmoptionSepArg, 0      },
-    { "-population-density", ".population-density",     XrmoptionSepArg, 0      },
-    { "-population-single",  ".population-single",      XrmoptionNoArg,  "True" },
+    { "-seed-density",       ".seed-density",           XrmoptionSepArg, 0      },
+    { "-seed-left",          ".seed-left",              XrmoptionNoArg,  "True" },
+    { "-seed-center",        ".seed-center",            XrmoptionNoArg,  "True" },
+    { "-seed-right",         ".seed-right",             XrmoptionNoArg,  "True" },
     { "-random-cell-size",   ".random-cell-size",       XrmoptionNoArg,  "True" },
     { "-random-delay",       ".random-delay",           XrmoptionNoArg,  "True" },
     { "-random-length",      ".random-length",          XrmoptionNoArg,  "True" },
     { "-random-cell-size",   ".random-cell-size",       XrmoptionNoArg,  "True" },
     { "-random-delay",       ".random-delay",           XrmoptionNoArg,  "True" },
     { "-random-length",      ".random-length",          XrmoptionNoArg,  "True" },