Random Room Maze Experiment Continued

Having ~45 minutes total free today, I decided to do what I suggested; things that would make the algorithm more useful (from my last post).

The changes I made were:

  • Made the rectangles a list, only populating the list initially with 1 Rectangle
  • Added functions to help checking against the list for whether there was an intersection (or a point is in a rectangle)
  • Made the corridor system more generic
  • Made the program choose the number of rectangles to add
  • Gave a maximum number of iterations before it gives up on finding a place for a room to be positioned
  • Made the code output the map to a text file.
  • Made the map size 128x128

The added functions were ListIntersect and ListInside, and both are easy to follow:

bool ListInside(Rectangle *rects[16], int x, int y, int count)
{
	for (int i = 0; i = rects[i]->X && 
			x X + rects[i]->W && 
			y >= rects[i]->Y && 
			y Y + rects[i]->H)
		{
			//cout

The first rectangle insertion was adjusted, and for a certain number of iterations you add more rectangles:

		//first rectangle
		x = Next(128);
		y = Next(128);
		r = Next(32) + 32;
		rectangles[0] =  new Rectangle();
		rectangles[0]->X = x - r/2;
		rectangles[0]->Y = y - r/2;
		rectangles[0]->H = r;
		rectangles[0]->W = r;

		count = 1;

		//additional rectangles
		// at least 6 more
		// at most 15 more
		int iterations = 6 + Next(9);
		for (int i = 0; i X = x - r/2;
			rectangles[count]->Y = y - r/2;
			rectangles[count]->H = r;
			rectangles[count]->W = r;

			int maxiter = 100;
			while (ListIntersects(rectangles, rectangles[count], count) && maxiter >= 0)
			{
				rectangles[count]->X = Next(128) - r/2;
				rectangles[count]->Y = Next(128) - r/2;
				maxiter--;
			}
			count++;
		}

The changes to the corridors was making the corridors go from the first rectangle to the second last, and make a path from the current rectangle to the next rectangle, swapping each time between the 2 methods used previously between 1&2 and 2&3. The result of which you can see for yourself.

All in all, the algorithm is still simple, and it is once again available on my GitHub.

3 comments

16
May

ah its not wordpress, its drupal right?

21
May

Yeah, it is Drupal, the only thing I dislike is how between versions everything breaks :(

17
May

what a great post, just signed up to your RSS feed and hope to read more of your posts in the future. keep it up!