From 5679a80c3fddf0e991a216ab9a574f35910bb7de Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 18 Mar 2026 17:29:09 +0000 Subject: [PATCH] simulation: neighbour based iteration strategy Instead of randomly picking two cells, randomly pick the first then choose a neighbour. Changes are more local and thus we should see clearer fluid movement instead of the chaos from before. --- src/simulation.c | 56 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/simulation.c b/src/simulation.c index d2513a1..da80ceb 100644 --- a/src/simulation.c +++ b/src/simulation.c @@ -42,11 +42,61 @@ static struct ProgramConcat *a_b_concat = NULL; void simulation_iterate(simulation_t *sim) { u64 a = 0, b = 0; - while (a == b) + // Strategy 1: Pick two random cells + // while (a == b) + // { + // a = rand() % NUM_PROGRAMS; + // b = rand() % NUM_PROGRAMS; + // } + + // Strategy 2: Pick a random cell, then iterate on all neighbours + a = rand() % NUM_PROGRAMS; + u64 a_x = a % SIMULATION_ROW_SIZE; + u64 a_y = a / SIMULATION_ROW_SIZE; + + bool valid_dir[4] = { + a_x > 0, + a_x != (SIMULATION_ROW_SIZE - 1), + a_y > 0, + a_y < (SIMULATION_ROW_SIZE - 1), + }; + + u64 neighbours[8] = {0}; + u64 size = 0; + if (valid_dir[0]) { - a = rand() % NUM_PROGRAMS; - b = rand() % NUM_PROGRAMS; + neighbours[size++] = a - 1; + if (valid_dir[2]) + { + neighbours[size++] = a - SIMULATION_ROW_SIZE - 1; + } + if (valid_dir[3]) + { + neighbours[size++] = a + SIMULATION_ROW_SIZE - 1; + } } + if (valid_dir[1]) + { + neighbours[size++] = a + 1; + if (valid_dir[2]) + { + neighbours[size++] = a - SIMULATION_ROW_SIZE + 1; + } + if (valid_dir[3]) + { + neighbours[size++] = a + SIMULATION_ROW_SIZE + 1; + } + } + if (valid_dir[2]) + { + neighbours[size++] = a - SIMULATION_ROW_SIZE; + } + if (valid_dir[3]) + { + neighbours[size++] = a + SIMULATION_ROW_SIZE; + } + + b = neighbours[rand() % size]; // Perform the reaction if (!a_b_concat)