// Name: // Submenu: // Author: // Title: // Version: // Desc: // Keywords: // URL: // Help: // Force Aliased Selection // Force Single Threaded #region UICode DoubleSliderControl Ratio = 0.5; // [0,1,5] Wall-Path Ratio ReseedButtonControl ButtonReseed = 0; // Reseed #endregion // This single-threaded function is called after the UI changes and before the Render function is called // The purpose is to prepare anything you'll need in the Render function void PreRender(Surface dst, Surface src) { Rectangle selection = EnvironmentParameters.SelectionBounds; //code from https://github.com/tomdam/spcoderscripts/blob/master/Scripts/Maze/BasicMaze.csx //declaring "directions" (North, South, East and West) variables that we will use later during the printing byte N = 1; byte S = 2; byte E = 4; byte W = 8; //declaring utility dictionaries used during the maze generation. //regular sides var SIDES = new Dictionary() {{ 'N', 1 }, { 'S', 2 }, { 'E', 4 }, { 'W', 8 }}; //dictionary that contains values that help us go horizontally (east => + 1; west => - 1) during the random generation var DX = new Dictionary() {{ 'E', 1 }, { 'W', -1 }, { 'N', 0 }, { 'S', 0 } }; //dictionary that contains values that help us go vertically (south => + 1; north => - 1) during the random generation var DY = new Dictionary() {{ 'E', 0 }, { 'W', 0 }, { 'N', -1 }, { 'S', 1 }}; //oppisite sides var OPPOSITE = new Dictionary() {{ 'E', W }, { 'W', E }, { 'N', S }, { 'S', N }}; //declare random variable Random r = new Random(); //Method used for shuffling the directions array void RandomizeDirections(char[] array) { for (var i = array.Length - 1; i > 0; i--) { int j = r.Next(i + 1); char temp = array[i]; array[i] = array[j]; array[j] = temp; } } //Recirsive method that contains the main generation logic //It takes coordinates of current cell and the grid void MazeGenerationPassage(int cx, int cy, int[,] grid) { //generate the array of all 4 directions var directions = new char[] {'N', 'S', 'E', 'W'}; //shuffle the array //based on the result of this shuffling will the alghoritm choose the next cells to visit RandomizeDirections(directions); //go through the array of directions for(var i = 0; i < directions.Length; i++) { //get the current direction var direction = directions[i]; //calculate the next x value (add the current x value to the value from DX, so nx can go 1 cell left or right from cx, or remain the same) int nx = (int)(cx + DX[direction]); //calculate the next y value (add the current y value to the value from DY, so nx can go 1 cell up or down from cy, or remain the same) int ny = (int)(cy + DY[direction]); //check if nx and ny coordinates are inside the grid and also check if the cell with those coordinates is still unvisited (has value 0) if ((ny >= 0 && ny < grid.GetLength(0)) && (nx >= 0 && nx < grid.GetLength(1)) && grid[ny,nx] == 0) { //change the value of current cell by removing the borders between it and the cell next to it in the "direction" direction //(the one that we are currently handling in for loop) grid[cy,cx] |= SIDES[direction]; //change the value of ny,nx cell by removing the borders between it and the current cell in the "opposite direction" direction //(opposite to the one that we are currently handling in for loop) grid[ny,nx] |= OPPOSITE[direction]; //Recursively call the same method for the next cell (nx,ny) MazeGenerationPassage(nx, ny, grid); } } } //declare height of the grid int height = selection.Height; //declare width of the grid int width = selection.Width; //declare the matrix that will contain the data //every cell of the matrix contains the byte whose bits hold the value for the walls between that cell and 4 cells around it int[,] matrix = new int[height, width]; //call the function for maze generation, starting from top left cell (0,0) MazeGenerationPassage(0, 0, matrix); //generate random start wall int startRow = (int)r.Next(height); //generate random end wall int finishRow = (int)r.Next(height); //end } // Here is the main multi-threaded render function // The dst canvas is broken up into rectangles and // your job is to write to each pixel of that rectangle void Render(Surface dst, Surface src, Rectangle rect) { // Delete these lines if you don't need the primary or secondary color ColorBgra PrimaryColor = EnvironmentParameters.PrimaryColor; ColorBgra SecondaryColor = EnvironmentParameters.SecondaryColor; // Step through each row of the current rectangle for (int y = rect.Top; y < rect.Bottom; y++) { if (IsCancelRequested) return; // Step through each pixel on the current row of the rectangle for (int x = rect.Left; x < rect.Right; x++) { ColorBgra SrcPixel = src[x,y]; ColorBgra DstPixel = dst[x,y]; ColorBgra CurrentPixel = SrcPixel; // TODO: Add additional pixel processing code here dst[x,y] = CurrentPixel; } } }