Crusader Posted July 23, 2008 Share Posted July 23, 2008 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 More sharing options...
corbin Posted July 23, 2008 Share Posted July 23, 2008 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 https://forums.phpfreaks.com/topic/116197-solved-random-map-algorithm/#findComment-597795 Share on other sites More sharing options...
Crusader Posted July 23, 2008 Author Share Posted July 23, 2008 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 https://forums.phpfreaks.com/topic/116197-solved-random-map-algorithm/#findComment-598050 Share on other sites More sharing options...
corbin Posted July 23, 2008 Share Posted July 23, 2008 So 14 should be in 4's exits array? Link to comment https://forums.phpfreaks.com/topic/116197-solved-random-map-algorithm/#findComment-598059 Share on other sites More sharing options...
Crusader Posted July 23, 2008 Author Share Posted July 23, 2008 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 https://forums.phpfreaks.com/topic/116197-solved-random-map-algorithm/#findComment-598072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.