Game of Life Optimizations
Adjust mutation logic. Use 1D get/set. Reduce code size.
This commit is contained in:
@@ -5230,20 +5230,20 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
|
||||
// Setup New Game of Life
|
||||
if ((!paused && generation == 0) || setup) {
|
||||
SEGENV.step = strip.now + 1250; // show initial state for 1.25 seconds
|
||||
SEGENV.step = strip.now + 1280; // show initial state for 1.28 seconds
|
||||
generation = 1;
|
||||
paused = true;
|
||||
//Setup Grid
|
||||
memset(cells, 0, maxIndex * sizeof(Cell));
|
||||
|
||||
for (unsigned i = maxIndex; i--; ) {
|
||||
for (unsigned i = 0; i < maxIndex; i++) {
|
||||
bool isAlive = !hw_random8(3); // ~33%
|
||||
cells[i].alive = isAlive;
|
||||
cells[i].faded = !isAlive;
|
||||
unsigned x = i % cols, y = i / cols;
|
||||
cells[i].edgeCell = (x == 0 || x == cols-1 || y == 0 || y == rows-1);
|
||||
|
||||
SEGMENT.setPixelColorXY(x, y, isAlive ? SEGMENT.color_from_palette(hw_random8(), false, PALETTE_SOLID_WRAP, 0) : bgColor);
|
||||
SEGMENT.setPixelColor(i, isAlive ? SEGMENT.color_from_palette(hw_random8(), false, PALETTE_SOLID_WRAP, 0) : bgColor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5251,7 +5251,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
// Redraw if paused or between updates to remove blur
|
||||
for (unsigned i = maxIndex; i--; ) {
|
||||
if (!cells[i].alive) {
|
||||
uint32_t cellColor = SEGMENT.getPixelColorXY(i % cols, i / cols);
|
||||
uint32_t cellColor = SEGMENT.getPixelColor(i);
|
||||
if (cellColor != bgColor) {
|
||||
uint32_t newColor;
|
||||
bool needsColor = false;
|
||||
@@ -5261,12 +5261,11 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
if (blended == cellColor) { blended = bgColor; cells[i].faded = 1; }
|
||||
newColor = blended; needsColor = true;
|
||||
}
|
||||
if (needsColor) SEGMENT.setPixelColorXY(i % cols, i / cols, newColor);
|
||||
if (needsColor) SEGMENT.setPixelColor(i, newColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
return FRAMETIME;
|
||||
|
||||
}
|
||||
|
||||
// Repeat detection
|
||||
@@ -5286,7 +5285,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
|
||||
unsigned neighbors = 0, aliveParents = 0, parentIdx[3];
|
||||
// Count alive neighbors
|
||||
for (int i = 1; i >= -1; i--) for (int j = 1; j >= -1; j--) if (i || j) {
|
||||
for (int i = -1; i <= 1; i++) for (int j = -1; j <= 1; j++) if (i || j) {
|
||||
int nX = x + j, nY = y + i;
|
||||
if (cell.edgeCell) {
|
||||
nX = (nX + cols) % cols;
|
||||
@@ -5295,8 +5294,8 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
unsigned nIndex = nX + nY * cols;
|
||||
Cell& neighbor = cells[nIndex];
|
||||
if (neighbor.alive) {
|
||||
if (++neighbors > 3) break;
|
||||
if (!neighbor.toggleStatus) { // Alive and not dying
|
||||
neighbors++;
|
||||
if (!neighbor.toggleStatus && neighbors < 4) { // Alive and not dying
|
||||
parentIdx[aliveParents++] = nIndex;
|
||||
}
|
||||
}
|
||||
@@ -5308,25 +5307,25 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
if (cell.alive && (neighbors < 2 || neighbors > 3)) { // Loneliness or Overpopulation
|
||||
cell.toggleStatus = 1;
|
||||
if (blur == 255) cell.faded = 1;
|
||||
newColor = cell.faded ? bgColor : color_blend(SEGMENT.getPixelColorXY(x, y), bgColor, blur);
|
||||
newColor = cell.faded ? bgColor : color_blend(SEGMENT.getPixelColor(cIndex), bgColor, blur);
|
||||
needsColor = true;
|
||||
}
|
||||
else if (!cell.alive) {
|
||||
if (neighbors == 3 && (!mutate || hw_random8(128)) || // Normal birth with 1/128 failure chance if mutate
|
||||
(mutate && neighbors == 2 && !hw_random8(128))) { // Mutation birth with 2 neighbors with 1/128 chance if mutate
|
||||
byte mutationRoll = mutate ? hw_random8(128) : 1; // if 0: 3 neighbor births fail and 2 neighbor births mutate
|
||||
if ((neighbors == 3 && mutationRoll) || (mutate && neighbors == 2 && !mutationRoll)) { // Reproduction or Mutation
|
||||
cell.toggleStatus = 1;
|
||||
cell.faded = 0;
|
||||
|
||||
if (aliveParents) {
|
||||
// Set color based on random neighbor
|
||||
unsigned parentIndex = parentIdx[random8(aliveParents)];
|
||||
birthColor = SEGMENT.getPixelColorXY(parentIndex % cols, parentIndex / cols);
|
||||
birthColor = SEGMENT.getPixelColor(parentIndex);
|
||||
}
|
||||
newColor = birthColor;
|
||||
needsColor = true;
|
||||
}
|
||||
else if (!cell.faded) {// No change, fade dead cells
|
||||
uint32_t cellColor = SEGMENT.getPixelColorXY(x, y);
|
||||
uint32_t cellColor = SEGMENT.getPixelColor(cIndex);
|
||||
uint32_t blended = color_blend(cellColor, bgColor, blur);
|
||||
if (blended == cellColor) { blended = bgColor; cell.faded = 1; }
|
||||
newColor = blended;
|
||||
@@ -5334,7 +5333,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
}
|
||||
}
|
||||
|
||||
if (needsColor) SEGMENT.setPixelColorXY(x, y, newColor);
|
||||
if (needsColor) SEGMENT.setPixelColor(cIndex, newColor);
|
||||
}
|
||||
// Loop through cells, if toggle, swap alive status
|
||||
for (unsigned i = maxIndex; i--; ) {
|
||||
@@ -5344,7 +5343,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https:
|
||||
|
||||
if (repeatingOscillator || repeatingSpaceship || emptyGrid) {
|
||||
generation = 0; // reset on next call
|
||||
SEGENV.step += 1000; // pause final generation for 1 second
|
||||
SEGENV.step += 1024; // pause final generation for ~1 second
|
||||
}
|
||||
else {
|
||||
++generation;
|
||||
|
||||
Reference in New Issue
Block a user