} // collapses the position in grid to a single tile private void Propagate(Vector2Int coords) { //Debug.Log("Started Propagation at " + coords); Vector2Int[] directions = { new Vector2Int(0, -1), new Vector2Int(0, 1), new Vector2Int(-1, 0), new Vector2Int(1, 0) }; Stack <Vector2Int> propagationStack = new Stack <Vector2Int>(); propagationStack.Push(coords); while (propagationStack.Count > 0) { var current = propagationStack.Pop(); int x = current.x, y = current.y; var currPossibleTiles = grid[x + width * y]; for (int d = 0; d < directions.Length; ++d) { int dx = x + directions[d].x, dy = y + directions[d].y; int dirIndex = (dx + width * dy); if (dirIndex > 0 && dirIndex < grid.Length) { //Debug.Log("value: " + dirIndex + " grid " + grid.Length); List <ModuleContainer> otherCoords = grid[dirIndex].ToList(); foreach (var otherTile in otherCoords) { bool isCompat = (from currTile in currPossibleTiles where inputReader.CheckForPossibleTile(currTile.Module, (Direction)d, otherTile.Module) && otherTile.IsPossible select currTile).Any(); if (!isCompat) { otherTile.IsPossible = false; propagationStack.Push(new Vector2Int(dx, dy)); } } grid[dx + width * dy] = otherCoords.Where(val => val.IsPossible == true).ToArray(); if (grid[dx + width * dy].Length == 0) { Debug.Log("Failed"); OutputGrid(); InitializeGrid(); propagationStack.Clear(); break; } //else if (grid[dx + width * dy].Length == 1 && dx > 0 && dx < width && dy > 0 && dy < height) //{ // outputTilemap.SetTile(new Vector3Int(dx - (width / 2), height - (dy + 1), 0), grid[dx + width * dy][0].Module); //} } } } //Debug.Log("Propagated changes at " + coords); } // removes nonviable tiles from neighbours of the tile at coords