From 0baa521ba34d2cd4e0f732f83d23b807605786a2 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Sat, 16 Nov 2019 10:27:06 -0800 Subject: More and more progress. Split out functions in serializer Figured out how to allocate memory (for sequences) --- Source/Randomizer2.cpp | 207 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 169 insertions(+), 38 deletions(-) (limited to 'Source/Randomizer2.cpp') diff --git a/Source/Randomizer2.cpp b/Source/Randomizer2.cpp index 537c30b..b5218bf 100644 --- a/Source/Randomizer2.cpp +++ b/Source/Randomizer2.cpp @@ -3,6 +3,9 @@ #include "Random.h" #include "Solver.h" #include "Memory.h" +#include "PuzzlerSerializer.h" +#include +#include void FloodFillInternal(const Puzzle& p, std::vector>& reached, int x, int y) { if (x%2 == 1 && y%2 == 1) return; @@ -62,10 +65,10 @@ void Randomizer2::Randomize() { for (int j=0; j(edges.size() - 1)); Pos pos = edges[edge]; - p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; edges.erase(edges.begin() + edge); if (FloodFill(p)) { + p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; break; } else { p.grid[pos.x][pos.y].gap = Cell::Gap::NONE; @@ -108,10 +111,10 @@ void Randomizer2::Randomize() { int edge = Random::RandInt(0, static_cast(edges.size() - 1)); Pos pos = edges[edge]; - p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; edges.erase(edges.begin() + edge); if (FloodFill(p)) { + p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; break; } else { p.grid[pos.x][pos.y].gap = Cell::Gap::NONE; @@ -152,19 +155,100 @@ void Randomizer2::Randomize() { } +void DebugColorGrid(const std::vector>& colorGrid) { + for (int y=0; y>& colorGrid, int color, int x, int y) { + if (!p.SafeCell(x, y)) return; + if (colorGrid[x][y] != 0) return; // Already processed. + colorGrid[x][y] = color; + + FloodFill(p, colorGrid, color, x, y+1); + FloodFill(p, colorGrid, color, x, y-1); + FloodFill(p, colorGrid, color, x+1, y); + FloodFill(p, colorGrid, color, x-1, y); +} + +void FloodFillOutside(const Puzzle&p, std::vector>& colorGrid, int x, int y) { + if (!p.SafeCell(x, y)) return; + if (colorGrid[x][y] != 0) return; // Already processed. + if (x%2 != y%2 && p.grid[x][y].gap != Cell::Gap::FULL) return; // Only flood-fill through full gaps + colorGrid[x][y] = 1; // Outside color + + FloodFillOutside(p, colorGrid, x, y+1); + FloodFillOutside(p, colorGrid, x, y-1); + FloodFillOutside(p, colorGrid, x+1, y); + FloodFillOutside(p, colorGrid, x-1, y); +} + +/* + undefined -> 1 (color of outside) or * (any colored cell) or -1 (edge/intersection not part of any region) + + 0 -> {} (this is a special edge case, which I don't need right now) + 1 -> 0 (uncolored / ready to color) + 2 -> +*/ +std::vector> CreateColorGrid(const Puzzle& p) { + std::vector> colorGrid; + colorGrid.resize(p.width); + + for (int x=0; x edges; + for (int x=0; x gates = {0x00344, 0x00488, 0x00489, 0x00495, 0x00496}; + for (int i=0; i<5; i++) { + for (int j=0; j(edges.size() - 1)); + Pos pos = edges[edge]; + edges.erase(edges.begin() + edge); + + int color1 = 0; + int color2 = 0; + if (pos.x%2 == 0 && pos.y%2 == 1) { // Vertical + if (pos.x > 0) color1 = colorGrid[pos.x-1][pos.y]; + else color1 = 1; + + if (pos.x < p.width - 1) color2 = colorGrid[pos.x+1][pos.y]; + else color2 = 1; + } else { // Horizontal + assert(pos.x%2 == 1 && pos.y%2 == 0); + if (pos.y > 0) color1 = colorGrid[pos.x][pos.y-1]; + else color1 = 1; + + if (pos.y < p.height - 1) color2 = colorGrid[pos.x][pos.y+1]; + else color2 = 1; + } + // Enforce color1 < color2 + if (color1 > color2) std::swap(color1, color2); + + // Colors mismatch, valid cut + if (color1 != color2) { + // @Performance... have a lookup table instead? + for (int x=0; xWritePanelData(panel, POSITION, {x, y, 19.1f}); + auto solutions = Solver::Solve(copy); + assert(solutions.size() == 1); + p.sequence = solutions[0].sequence; + PuzzleSerializer(_memory).WritePuzzle(solutions[0], 0x139); +} - float z, w; +void Randomizer2::SetGate(int panel, int X, int Y) { + float x, y, z, w; if (X%2 == 0 && Y%2 == 1) { // Horizontal - z = -0.1f; - w = 1.0f; - } else if (X%2 == 1 && Y%2 == 0) { // Vertical + x = -1.49f * X + 0.22f * Y + 66.58f; + y = 0.275f * X + 1.6f * Y + 108.4f; z = -.77f; w = .63f; - } else { - assert(false); - return; + } else { // Vertical + assert(X%2 == 1 && Y%2 == 0); + x = -1.6f * X + 0.35f * Y + 66.5f; + y = 0.25f * X + 1.6f * Y + 108.55f; + z = -0.1f; + w = 1.0f; } + SetPos(panel, x, y, 19.2f); _memory->WritePanelData(panel, ORIENTATION, {0.0f, 0.0f, z, w}); +} + +void Randomizer2::SetPos(int panel, float x, float y, float z) { + _memory->WritePanelData(panel, POSITION, {x, y, z}); } \ No newline at end of file -- cgit 1.4.1