bdflynn Posted August 11, 2012 Share Posted August 11, 2012 Hi everybody, I'm trying to generate "world maps" by having PHP generate 4 different types of tiles: Ocean, Grass, Mountains and Desert. When a new tile is being generated it checks the tiles directly West, North West, North and North East of it to help determine what it should be. If a tile is surrounded by Ocean then the chances that it too is Ocean increases. However, say the tile is surrounded by two Ocean tiles, one Grass and one Mountain then there would be a greater chance of it being Grass or Mountain but still relatively less than being Ocean. I hope that makes sense. Right now I have the four types of terrain being represented by 0,1,2,3 respectively. It then grabs the info of the surrounding tiles and ships all of that info into this function: function select_terrain($t_w,$t_nw,$t_n,$t_ne){ // These percentages are representative of what I'd like a generic map without modifiers to contain. // The neighbouring tiles will modify these as they are processed. $odds[0] = 49; //Ocean = 50% $odds[1] = (79-$odds[0]); //Plains = 30% $odds[2] = (89-($odds[0]+$odds[1])); // Mountians = 10% $odds[3] = (99-($odds[0]+$odds[1]+$odds[2])) ; //Desert = 10%; //Grab the incoming arguments and loop through them modifying the original odds. $incoming_args = array($t_w,$t_nw,$t_n,$t_ne); $count = count($incoming_args); for($i=0; $i<$count; $i++){ switch ($incoming_args[$i]) { case 0: $odds[0] = $odds[0] + 10; $odds[1] = $odds[1] + 15; $odds[2] = $odds[2] - 1; $odds[3] = $odds[3] - 1; break; case 1: $odds[0] = $odds[0] - 11; $odds[1] = $odds[1] + 30; $odds[2] = $odds[2] +0; $odds[3] = $odds[3] + 1; break; case 2: $odds[0] = $odds[0] - 0; $odds[1] = $odds[1] +6; $odds[2] = $odds[2] + 10; $odds[3] = $odds[3] + 1; break; case 3: $odds[0] = $odds[0] - 20 ; $odds[1] = $odds[1] +11; $odds[2] = $odds[2] - 1; $odds[3] = $odds[3] + 10; break; case 5: $odds[0] = $odds[0] + 50; $odds[1] = $odds[1] - 2; $odds[2] = $odds[2] - 2; $odds[3] = $odds[3] - 1; break; } } // Get the absolute values of numbers to prevent negatives. $odds[0] = abs($odds[0]); $odds[1] = abs($odds[1]); $odds[2] = abs($odds[2]); $odds[3] = abs($odds[3]); // Pass these absolute values to the percentage_maker function to convert everything into #values/100 $percent = percentage_maker($odds[0],$odds[1], $odds[2], $odds[3]); // Because of the absolute values and shaving off decimal places this doesn't always equal 100, so get the total. $total = ($percent[0]+$percent[1]+$percent[2]+$percent[3]); /* // Echoing this out shows the specific range of each tile generated. If the randomly generated number falls between 0 & percent[0]-1 we get Ocean, and the rest follow suit. echo "<br /> 00 > ".($percent[0]-1); echo "<br />".($percent[0])." > ".(($percent[0]-1) + $percent[1]); echo "<br />".(($percent[0]-1) + ($percent[1]+1))." > ".(($percent[0]-1) + ($percent[1]) + ($percent[2])); echo "<br />".(($percent[0]-1) + ($percent[1]+1) + ($percent[2]))." > 99"; */ // Generate the random number to determine the next tile and compare it against the ranges (odds). If the number falls into that range it generates that type of tile. $t = rand(0,99); if($t >= 0 && $t <= ($percent[0]-1) ){ //Ocean $terrain = 0; }else if($t >= ($percent[0]) && $t <= (($percent[0]-1) + $percent[1]) ){ //Plains $terrain = 1; }else if($t >= (($percent[0]-1) + ($percent[1]+1)) && $t <= (($percent[0]-1) + ($percent[1]) + ($percent[2])) ){ //Mountain $terrain = 2; }else if($t >= (($percent[0]-1) + ($percent[1]+1) + ($percent[2])) && $t <= 99){ //Desert $terrain = 3; }; //Return the number value and repeat until the map is done. return $terrain; } Right now this is generating maps that look like the image attached. I've made a few variations, some better some worse, but nothing that looks "natural" I guess. I'm hoping somebody might have a better solution for, or could point me in the direction of a better solution for, generating tiles so the map contains larger bodies of ocean and continents and islands instead of just sporadic islands. I really appreciate any help or feedback you can give me. Also, I've also attached the PHP file running everything if you want to download it and test it out. Some of the code might be a little messy right now. If you have any suggestions on how to clean it up I'd be more than happy to hear them. Cheers. 18832_.php Quote Link to comment https://forums.phpfreaks.com/topic/266943-probability/ Share on other sites More sharing options...
xyph Posted August 11, 2012 Share Posted August 11, 2012 You need to look at chunks of pixels, and not individuals. Once you're dealing with chunks, you can go several ways to determine how big the water/land mass is. Once you've placed the chunks, you can then go through transitional chunks and generate borders between them based on surrounding chunks Map generation can be a very complex process, especially if you're going for realism. Quote Link to comment https://forums.phpfreaks.com/topic/266943-probability/#findComment-1368620 Share on other sites More sharing options...
bdflynn Posted August 11, 2012 Author Share Posted August 11, 2012 Ok, interesting. So perhaps process 5x5 or 10x10 chunks into either Ocean or Lands and then siphon through each chunk and re-process the outer borders of those chunks to make a transitional zone if the neighbouring chunk is different. Then I guess I can again re-process land chunks to add things like deserts and mountains. Thanks for the response. I'll take a good look at this. Quote Link to comment https://forums.phpfreaks.com/topic/266943-probability/#findComment-1368623 Share on other sites More sharing options...
xyph Posted August 11, 2012 Share Posted August 11, 2012 And re-process water chunks for small islands, etc. Best of luck! Make the general shape first, then refine it Quote Link to comment https://forums.phpfreaks.com/topic/266943-probability/#findComment-1368631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.