Advertisement
Guest User

Untitled

a guest
Sep 15th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. path = argument[0];
  2. density = argument[1];
  3. minLen = argument[2];
  4. maxLen = argument[3];
  5. edgeEncourageFactor = argument[4];
  6.  
  7. //Enum used for direction
  8. enum Direction { right = 0, up = 1, down = 2, left = 3 };
  9.  
  10. //Plot first point in center
  11. path_add_point(path, room_width / 2, room_height / 2, 1);
  12.  
  13. //Keep track of previously chosen direciton
  14. prevDir = 0;
  15.  
  16. //Iteratively create new points
  17. for (i = 0; i < density; i++)
  18. {
  19.     //Randomize the random seed
  20.     randomize();
  21.    
  22.     //Get random point index to append line to
  23.     chosenPoint = irandom_range(0, path_get_number(path));
  24.    
  25.     //Make sure its inside screen, if not, skip iteration, subtracting from i
  26.     if (path_get_point_x(path, chosenPoint) < 0 || path_get_point_x(path, chosenPoint) > window_get_width() ||
  27.         path_get_point_y(path, chosenPoint) < 0 || path_get_point_y(path, chosenPoint) > window_get_height())
  28.     {
  29.         i--;
  30.         continue;
  31.     }
  32.    
  33.     //Get the inverse of prev dir to avoid
  34.     invPrevDir = -1;
  35.     if (prevDir == Direction.right) invPrevDir = Direction.left;
  36.     else if (prevDir == Direction.left) invPrevDir = Direction.right;
  37.     else if (prevDir == Direction.up) invPrevDir = Direction.down;
  38.     else if (prevDir == Direction.down) invPrevDir = Direction.up;
  39.    
  40.     //Get random direction
  41.     dir = -1;
  42.     while (dir == -1 || dir == prevDir || dir == invPrevDir)
  43.     {
  44.         randomize();
  45.         dir = irandom_range(0, 3);
  46.     }
  47.     prevDir = dir;
  48.    
  49.     //Get random length
  50.     len = irandom_range(minLen, maxLen);
  51.    
  52.     //Calc new location
  53.     ptX = path_get_point_x(path, chosenPoint);
  54.     ptY = path_get_point_y(path, chosenPoint);
  55.     if (ptX == 0 || ptY == 0) continue;
  56.     if (dir == Direction.right)
  57.         ptX += len;
  58.     else if (dir == Direction.left)
  59.         ptX -= len;
  60.     else if (dir == Direction.up)
  61.         ptY -= len;
  62.     else if (dir == Direction.down)
  63.         ptY += len;
  64.    
  65.     //Check the distance from center in normalized screen space
  66.     distX = (ptX - (room_width / 2)) / (room_width / 2);
  67.     distY = (ptY - (room_height / 2)) / (room_height / 2);
  68.     dist = sqrt(sqr(distX) + sqr(distY));
  69.    
  70.     //Randomly skip over with chances based on the distance (closer = likely skipped)
  71.     if (dist < random_range(0, edgeEncourageFactor)) continue;
  72.    
  73.     //Add a point at the chosen point to use to split
  74.     path_insert_point(path, chosenPoint, path_get_point_x(path, chosenPoint), path_get_point_y(path, chosenPoint), 1);
  75.    
  76.     //Add point from other chosen point, in the correct length and direction
  77.     path_insert_point(path, chosenPoint + 1, ptX, ptY, 1);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement