Jump to content

[SOLVED] Random map algorithm


Crusader

Recommended Posts

I recently ported a Python map making algorithm to PHP and decided to improve upon it. My script works fine but unfortunately it's pretty slow. It can generate perfect mazes but it takes at least 2 minutes to generate a 5x5 map.

 

You can view the source here: http://pastebin.com/m56bdc991

 

Is there anything I can do to speed it up or generally more efficient?

 

Thanks.

 

 

PS: If you want to use/modify the code I wrote feel free to but PLEASE DO NOT SELL IT!

Link to comment
https://forums.phpfreaks.com/topic/116197-solved-random-map-algorithm/
Share on other sites

			while($valid == 0)
			{
				++$i;
				$dir	= rand(0,3);
				$valid	= $this->spawn($x_pos, $y_pos, $width, $height, $dir, ($this->limit_list[$z] - 1), $mobility);
			}

 

Goes into an infinite loop.

 

 

Edit: found it.  This is never true:

if( (($ny != $y) || ($nx != $x)) && ($this->cell[($ny * $width) + $nx] == 0) )

I just tested it and it does come up as true :S

 

With some help, I found the problem. It was in the function spawn.

 

Here's the fix:

 

if(rand(0, 99) < $mobility)
		{
			if($ny != $y)
				$this->y_walls[($cy * $width) + $cx] = 0;
			else
				$this->x_walls[($cy * $width) + $cx] = 0;

			$max_limit_2++;

			//return 1;
		}
		return 1; // MOVE IT HERE

 

Now the only issue I have is that some sectors don't show their connections to others.

 

[4] => Array
        (
            [x] => 0
            [y] => 3
            [exits] => 3, 5
        )

[14] => Array
        (
            [x] => 1
            [y] => 3
            [exits] => 4, 15
        )

 

Note how 4 doesn't know 14 connects to it?

 

Thanks again for help.

It should be. I figured it out though.

 

This:

if(($this->map[$v]['x'] > 0) && ($this->x_walls[$pos - 1] >= 0))
			{
				if($exits == '')
					$exits .= $this->sectors[$x_pos - 1][$y_pos];
				else
					$exits .= ', ' . $this->sectors[$x_pos - 1][$y_pos];
			}

 

Should be this:

if(($this->map[$v]['x'] > 0) && ($this->x_walls[$pos - 1] == 0))
			{
				if($exits == '')
					$exits .= $this->sectors[$x_pos - 1][$y_pos];
				else
					$exits .= ', ' . $this->sectors[$x_pos - 1][$y_pos];
			}

 

Any more improvements, bugs you notice would be appreciated.

 

Thanks for your time!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.