From 6b4b1b563ebbd3ddc4069eff5e56a260f2e92828 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 10 Jun 2021 16:20:20 -0700 Subject: [PATCH] Reworked CLI flags for starting seed in README and WolframAutomata source. --- hacks/WolframAutomata/README.md | 63 +++++++++++++------------ hacks/WolframAutomata/WolframAutomata.c | 46 ++++++++++++------ 2 files changed, 65 insertions(+), 44 deletions(-) diff --git a/hacks/WolframAutomata/README.md b/hacks/WolframAutomata/README.md index 6bbd56a..d3546d1 100644 --- a/hacks/WolframAutomata/README.md +++ b/hacks/WolframAutomata/README.md @@ -84,23 +84,6 @@ are re-randomized every time the simulation is reset, such as after a simulation completes or after resizing the window. -CLI: Simulation Seed --------------------- - -If none of the following options are specified, the starting seed will contain -randomly interspersed active/inactive cells at a 30/70, 50/50, or 70/30 ratio, -itself also randomly selected. - - - **`-seed-left`**: Seeds a single active cell on the left side of the - display. All other cells are inactive. - - - **`-seed-center`**: As above, but in the center. - - - **`-seed-right`**: As above, but on the right side. - - - **`-seed-density N`**: Generates random seed with `N` percent active cells. - - CLI: Rule Selection ------------------- @@ -114,21 +97,26 @@ from `curated_ruleset_list[]` in `WolframAutomata.c`. from 0-255 inclusive are valid. -CLI: Simulation Speed ---------------------- +CLI: Simulation Seed +-------------------- -If neither of the following two options are passed, the simulation runs as -though `-delay 25000` was passed. +The following seed related CLI flags apply only when using the +`-true-random-rule` or `-rule N` flags. Without these flags, the program draws +rules from `curated_ruleset_list[]` which also includes curated seeds, all of +which override any seed related CLI flags. - - **`-random-delay`**: A random delay is selected, but not truly random. For - more details, read `WolframAutomata.c` starting around the comment, "When - randomly setting the delay, the problem is to avoid ..." +If the curated rule list is not in use and none of the following options are +specified, the starting seed will contain randomly interspersed active/inactive +cells at a 30/70, 50/50, or 70/30 ratio, itself also randomly selected. - - **`-delay N`**: Request `N` microsecond delay between each frame/generation - of the simulation. Note that this is only a request; XScreensaver reserves - the right to ignore requested values, and of course we execute at the mercy - of the kernel's scheduling. In practice, non-absurd values are reasonably - well respected. + - **`-seed-left`**: Seeds a single active cell on the left side of the + display. All other cells are inactive. + + - **`-seed-center`**: As above, but in the center. + + - **`-seed-right`**: As above, but on the right side. + + - **`-seed-density N`**: Generates random seed with `N` percent active cells. CLI: Simulation Length @@ -149,6 +137,23 @@ generations is cell_size dependent. This is a soft limit and may be increased if ..." +CLI: Simulation Speed +--------------------- + +If neither of the following two options are passed, the simulation runs as +though `-delay 25000` was passed. + + - **`-random-delay`**: A random delay is selected, but not truly random. For + more details, read `WolframAutomata.c` starting around the comment, "When + randomly setting the delay, the problem is to avoid ..." + + - **`-delay N`**: Request `N` microsecond delay between each frame/generation + of the simulation. Note that this is only a request; XScreensaver reserves + the right to ignore requested values, and of course we execute at the mercy + of the kernel's scheduling. In practice, non-absurd values are reasonably + well respected. + + CLI: Cell Dimensions -------------------- diff --git a/hacks/WolframAutomata/WolframAutomata.c b/hacks/WolframAutomata/WolframAutomata.c index 01246f2..edf1dc3 100644 --- a/hacks/WolframAutomata/WolframAutomata.c +++ b/hacks/WolframAutomata/WolframAutomata.c @@ -36,11 +36,6 @@ struct state { /* 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; @@ -53,6 +48,7 @@ struct state { /* 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. */ @@ -176,12 +172,22 @@ static const struct color_pair color_list[] = { /* 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++) { - 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. */ - 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"); @@ -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) { - 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; } @@ -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 (state->population_single) { + if (get_boolean_resource(state->dpy, "seed-left", "Boolean")) { 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 { + randomize_seed_density(state); generate_random_seed(state); } } @@ -505,8 +517,10 @@ static const char * WolframAutomata_defaults[] = { "*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", @@ -521,8 +535,10 @@ static XrmOptionDescRec WolframAutomata_options[] = { { "-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" }, -- 2.20.1