From 5d27e8f340e87cf2debc06c5991e32781d97d3a4 Mon Sep 17 00:00:00 2001 From: Brandon502 <105077712+Brandon502@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:05:13 -0500 Subject: [PATCH] GoL - Minor Changes Use defined(ARDUINO_ARCH_ESP32) getNeighborIndexes loop changes offsets use int8_t change prevRows/Cols to uint16 --- wled00/FX.cpp | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 26b795ef..c34c2af7 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -5240,8 +5240,8 @@ class GameOfLifeGrid { Cell* cells; const int cols, rows, maxIndex; const int nOffsets[8] = {-cols-1, -cols, -cols+1, -1, 1, cols-1, cols, cols+1}; // Neighbor offsets - const int offsetX[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; - const int offsetY[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; + const int8_t offsetX[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; + const int8_t offsetY[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; public: GameOfLifeGrid(Cell* data, int c, int r) : cells(data), cols(c), rows(r), maxIndex(r * c) {} void getNeighborIndexes(unsigned neighbors[9], unsigned cIndex, unsigned x, unsigned y, bool wrap) { @@ -5251,10 +5251,15 @@ class GameOfLifeGrid { unsigned nIndex = cIndex + nOffsets[i]; if (edgeCell) { int nX = x + offsetX[i], nY = y + offsetY[i]; - if (nX < 0) {if (!wrap) continue; nIndex += cols;} - else if (nX >= cols) {if (!wrap) continue; nIndex -= cols;} - if (nY < 0) {if (!wrap) continue; nIndex += maxIndex;} - else if (nY >= rows) {if (!wrap) continue; nIndex -= maxIndex;} + if (wrap) { + if (nX < 0) nIndex += cols; + else if (nX >= cols) nIndex -= cols; + if (nY < 0) nIndex += maxIndex; + else if (nY >= rows) nIndex -= maxIndex; + } + else { // Wrap disabled, skip out of bound neighbors + if (nX < 0 || nX >= cols || nY < 0 || nY >= rows) continue; + } } neighbors[++neighborCount] = nIndex; } @@ -5302,15 +5307,15 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); const size_t dataSize = SEGMENT.length() * sizeof(Cell); // Cell = 2 bytes - const size_t totalSize = dataSize + 5; // 5 bytes for prevRows, prevCols, prevPalette, prevWrap, soloGlider + const size_t totalSize = dataSize + 7; // 7 bytes for prevRows(2), prevCols(2), prevPalette, prevWrap, soloGlider if (!SEGENV.allocateData(totalSize)) return mode_static(); //allocation failed - uint8_t *prevRows = reinterpret_cast(SEGENV.data); - uint8_t *prevCols = reinterpret_cast(SEGENV.data + 1); - uint8_t *prevPalette = reinterpret_cast(SEGENV.data + 2); - bool *prevWrap = reinterpret_cast (SEGENV.data + 3); - bool *soloGlider = reinterpret_cast (SEGENV.data + 4); - Cell *cells = reinterpret_cast (SEGENV.data + 5); + uint16_t *prevRows = reinterpret_cast(SEGENV.data); + uint16_t *prevCols = reinterpret_cast(SEGENV.data + 2); + uint8_t *prevPalette = reinterpret_cast (SEGENV.data + 4); + bool *prevWrap = reinterpret_cast (SEGENV.data + 5); + bool *soloGlider = reinterpret_cast (SEGENV.data + 6); + Cell *cells = reinterpret_cast (SEGENV.data + 7); uint16_t& generation = SEGENV.aux0; //Rename SEGENV/SEGMENT variables for readability uint16_t& gliderLength = SEGENV.aux1; @@ -5357,15 +5362,14 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: //Setup Grid memset(cells, 0, dataSize); - #if !ESP32 - random16_set_seed(strip.now>>2); //seed the random generator - #else + random16_set_seed(strip.now>>2); //seed the random generator + #if defined(ARDUINO_ARCH_ESP32) const uint32_t chance = UINT32_MAX * 0.32; #endif unsigned cIndex = 0; for (unsigned y = 0; y < rows; ++y) for (unsigned x = 0; x < cols; ++x, ++cIndex) { if (x == 0 || x == cols - 1 || y == 0 || y == rows - 1) cells[cIndex].edgeCell = 1; - #if ESP32 + #if defined(ARDUINO_ARCH_ESP32) if (esp_random() < chance) grid.setCell(cIndex, x, y, true, wrap); // ~32% chance of being alive #else if (random16(100) < 32) grid.setCell(cIndex, x, y, true, wrap); @@ -5382,7 +5386,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: if (palChanged || paused || (SEGMENT.speed != 255 && strip.now - SEGENV.step < 1000 / map2(SEGMENT.speed,0,254,1,60))) { //(1 - 60) updates/sec 255 is uncapped // Redraw if paused (remove blur), palette changed, overlaying background if not max speed (avoid flicker) // Generation 1 draws alive cells randomly and fades dead cells - bool newGame = generation == 1; + bool newGame = generation == 1; if (paused || palChanged || overlayBG) { unsigned cIndex = 0; for (unsigned y = 0; y < rows; ++y) for (unsigned x = 0; x < cols; ++x, ++cIndex) {