// // Created by Wouter Groeneveld on 30/11/18. // #include "ConwayScene.h" #include "bg.h" #include #include #include #include // See http://www.coranac.com/tonc/text/gfx.htm#cd-qran int __qran_seed= 42; // Seed / rnd holder std::vector ConwayScene::sprites() { return {}; } std::vector ConwayScene::backgrounds() { return { bg.get() }; } u16 ConwayScene::getNextState(int x, int y, int pos) { int amountAlive = countAmountOfNeighbouringCellsAlive(pos, x, y); int currentState = map[pos]; // speed optimization: skip the ifs. "? ALIVE : DEAD" can also be skipped if you're sure they are 1 and 0. return (amountAlive == 3 || (amountAlive == 2 && (currentState == ALIVE))) ? ALIVE : DEAD; } int ConwayScene::countAmountOfNeighbouringCellsAlive(int pos, int x, int y) { int amountOfNeightbouringCellsAlive = 0; for(int x_i = x - 1; x_i <= x + 1; x_i++) { for(int y_j = y - 1; y_j <= y + 1; y_j++) { int toCheckPos = y_j * MAP_WIDTH + x_i; if(toCheckPos >= 0 && toCheckPos < MAP_SIZE - 1 && pos != toCheckPos && map[toCheckPos] == ALIVE) { amountOfNeightbouringCellsAlive++; } } } return amountOfNeightbouringCellsAlive; } void ConwayScene::load() { backgroundPalette = std::unique_ptr(new BackgroundPaletteManager(conway_palette, sizeof(conway_palette))); seedRandomMap(((MAP_WIDTH * MAP_HEIGHT) / 100) * percentageSeed); bg = std::unique_ptr(new Background(1, conway_data, sizeof(conway_data), map, sizeof(map))); bg.get()->useMapScreenBlock(16); postload(); } void ConwayScene::seedRandomMap(int seedcount) { for(int i = 0; i < seedcount; i++) { int x = qran_range(0, MAP_WIDTH); int y = qran_range(0, MAP_HEIGHT); map[y * MAP_WIDTH + x] = ALIVE; } }