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
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) )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.