Random World Generation

I'm trying to find out a good way to create a method that generates a large world of tiles and objects, where the method itself only takes two parameters: width and height in tiles.

What I want to do, is split the map into "zones" with random sizes picked out from a width and height table using a random seed index. These zones gets a random biome to accompany it. I've been trying various things, and this is the closest I've become:
  1.        public static World Generate(int width, int height)
  2.         {
  3.             // Create a new world
  4.             World world = new World();
  5.  
  6.             // Generate a random generator
  7.             Random rand = new Random();
  8.  
  9.             // Set the coordinates
  10.             int x = 0, y = 0; string biome, image;
  11.             // Until the X and Y values are outside the map: generate
  12.             while (y < height)
  13.             {
  14.                 // Create a seed for randomization (from 0 up to 9)
  15.                 int seed = rand.Next(10);
  16.                 // Split the map into zones of sizes based on the seed
  17.                 int zoneWidth = Widths[seed], zoneHeight = Heights[seed];
  18.  
  19.                 // Get biome
  20.                 biome = Biomes[rand.Next(Biomes.Length)];
  21.  
  22.                 // Cover area in tiles
  23.                 int row, col;
  24.                 for (int cy = 0; cy < zoneHeight; cy++)
  25.                 {
  26.                     // Set new row and column hvalues
  27.                     row = 1 + rand.Next(5);
  28.                     col = 1 + rand.Next(6);
  29.                     for (int cx = 0; cx < zoneWidth; cx++)
  30.                     {
  31.                         // Add landmarks in the middle of zones
  32.                         if (cx == zoneWidth / 2 && cy == zoneHeight / 2)
  33.                             world.Landmarks.Add(new Landmark(biome, x, y));
  34.  
  35.                         // Randomize ground tiles slightly
  36.                         image = cx % rand.Next(5, 15) == 0 ?
  37.                             biome + "_Ground" : biome + "_GroundSpecial";
  38.  
  39.                         // Place ground tile only if within bounds of world
  40.                         if (x < width && y < height)
  41.                         {
  42.                             // Place tile
  43.                             world.Ground.Add(new Tile(image, x, y));
  44.  
  45.                             // Scenery placements (somewhat randomly)
  46.                             if (cx % col == 0 && cy % row == 0)
  47.                             {
  48.                                 int r = rand.Next(Scenery.Biome(biome).Count);
  49.                                 world.Sceneries.Add(Scenery.Get(Scenery.Biome(biome)[r],
  50.                                     new Vector2(x, y)));
  51.                             }
  52.                             x++;
  53.                         }
  54.                         else if (x >= width)
  55.                             x = 0;
  56.                     }
  57.                     y++;
  58.                 }
  59.             }
  60.  
  61.             Console.WriteLine(world.Ground.Count);
  62.  
  63.             // Return the world
  64.             return world;
  65.         }
Expand to see the code.


So when I try to create a world with the sizes 2048 x 2048, it should give me 4194304 tiles, but it gives me about 10 times less than the expected amount (at the console line), and it looks like it skips all but the first Y-axis of each zone. I don't know why, because if I output the X and Y values every time it adds a tile, it shows correct values from start to finish, but still only gives me around 400k tiles in the final list.

It might just be an obvious code mistake, but I don't see it right now. Anyone have a better algorithm or any idea what happens wrong with this code?

Any help is greatly appreciated! Thanks!
I noticed that I have to reset the X back to 0 every time the Y updates, so I added this line and now it's working to some extent. It's still not adding as many tiles as expected, though, so that is still up D:
you try something like this:

http://www.youtube.com/watch?v=fmF4B7AKSBg

?
if yes. this is straight gangsta. i dont even how it works. i accidentially a site and now i cant find it :(
It's hard code. It's not RPG Maker, so that won't help me at all. But thanks for the link.
what are you using for your project?
That is irrelevant, since all I need is to fix the code given in the first post. It's C#, though, there is no editor or anything.
well im no scripter, obviously, but it may be a problem with the different tileset pattern. could it be, that it counts ground, water and stuff like this not as a actual maptile?
I'm not using a tileset, and as stated in the second post, it creates most of the zones, but skips a few zones. Some coordinate issues here, not the tile collection. It's how the X and Y variables are handled.
  1. public static World Generate(int width, int height)
  2.         {
  3.             // Create a new world
  4.             World world = new World();
  5.  
  6.             // Generate a random generator
  7.             Random rand = new Random();
  8.  
  9.             // Create a seed for randomization (from 0 up to 9)
  10.             int seed = rand.Next(Widths.Length);
  11.  
  12.             // Set the coordinates
  13.             int x = 0, y = 0, i = 0, b = rand.Next(Biomes.Length), leftX = 0, leftY = 0; string biome, image;
  14.             int zoneHeight = Heights[seed]; bool noreset = false;
  15.             // Until the X and Y values are outside the map: generate
  16.             while (y < height)
  17.             {
  18.                 // Create a seed for randomization (from 0 up to 9)
  19.                 seed = rand.Next(Widths.Length);
  20.                 // Split the map into zones of sizes based on the seed
  21.                 int zoneWidth = Widths[seed];
  22.  
  23.                 // Set biome
  24.                 b++; noreset = false;
  25.                 if (b == Biomes.Length) b = 0;
  26.  
  27.                 // Get biome
  28.                 biome = Biomes[b];
  29.  
  30.                 // Add zone
  31.                 world.Zones.Add(new Zone(biome, x, y, zoneWidth, zoneHeight));
  32.  
  33.                 // Cover area in tiles
  34.                 int col;
  35.                 for (int cy = 0; cy < zoneHeight; cy++)
  36.                 {
  37.                     // Set new row and column hvalues
  38.                     col = 10 + rand.Next(20);
  39.                     for (int cx = 0; cx < zoneWidth; cx++)
  40.                     {
  41.                         // Randomize ground tiles slightly
  42.                         image = cx % rand.Next(5, 15) == 0 ?
  43.                             biome + "_GroundSpecial" : biome + "_Ground";
  44.  
  45.                         // Place ground tile only if within bounds of world
  46.                         if (x < width && y < height)
  47.                         {
  48.                             // Place tile
  49.                             world.Zones[i].Tiles.Add(new Tile(image, x, y));
  50.  
  51.                             // Scenery placements (somewhat randomly)
  52.                             if (cx % col == 0 && cx != 0)
  53.                             {
  54.                                 int r = rand.Next(Scenery.Biome(biome).Count);
  55.                                 world.Zones[i].Sceneries.Add(Scenery.Get(Scenery.Biome(biome)[r],
  56.                                     new Vector2(x, y)));
  57.                             }
  58.                             x++;
  59.                             if (x >= width)
  60.                                 break;
  61.                         }
  62.                         else if (x >= width)
  63.                             break;
  64.                     }
  65.                     if (x >= width)
  66.                     {
  67.                         x = 0; leftX = 0;
  68.                         leftY += zoneHeight;
  69.                         noreset = true;
  70.                         break;
  71.                     }
  72.                     x = leftX;
  73.                     y++;
  74.                 }
  75.                 if(!noreset)
  76.                     leftX += zoneWidth;
  77.                 x = leftX;
  78.                 y = leftY;
  79.                 i++;
  80.             }
  81.  
  82.             Console.WriteLine(world.Zones.Count);
  83.  
  84.             // Return the world
  85.             return world;
  86.         }
Expand to see the code.

This is the new method. In some cases it skips a few X-values on the second zone row.
If we assume a world made out of 2048 x 2048 tiles, and we have 20 zones per row, the first zone on the second row is kind of ignored and leaves an annoying empty hole.

I do not have performance issues with my tile iterations now that I split the zones into its own classes, only iterating tiles when the zone is within the screen bounds, but I am still trying to wrap my head around the black holes that randomly spawn. Perhaps someone can see a flaw in my code where it handles the X values that makes it skip some X-coordinates after moving to the next row?
sorry but i cant help, but surelly this explaination makes it alot easier to understand for the others :) good luck with this one. may the force be strong with you
I have solved the issue. This is no longer active.
May I ask how did you solve the issue, so that others who happen to have the same problem can learn from what you did?
rosareven wrote:
May I ask how did you solve the issue, so that others who happen to have the same problem can learn from what you did?

Kind of late reply, I apologize for not being available for a long time. The code that was solved in this scenario is very old code and probably isn't very effective, but here's the code that was final after the solution was given.

  1. // Zone Sets
  2.         static int[] Widths = new int[] { 64, 48, 32 };
  3.         static int[] Heights = Widths.Reverse<int>().ToArray();
  4.  
  5.         // Biomes
  6.         static string[] Biomes = new string[] { "Forest", "Desert", "Snow", "Ash", "Lava", "Corruption", "Wasteland" };
  7.  
  8.         public static World Create(int width, int height)
  9.         {
  10.             // Create new world
  11.             World world = new World();
  12.             world.Width = width; world.Height = height;
  13.  
  14.             // Create a random generator
  15.             Random rand = new Random();
  16.  
  17.             // Create a map seed (for randomization)
  18.             int seed;
  19.  
  20.             // Biome index
  21.             int biomeIndex = rand.Next(Biomes.Length);
  22.  
  23.             // Temporary veriables
  24.             int x, y = 0, zoneHeight = 0, zoneWidth;
  25.             bool newRow = true; int originX = 0, originY = 0;
  26.  
  27.             // While the Y-row is less than height
  28.             while (y < height)
  29.             {
  30.                 // Create new seed
  31.                 seed = rand.Next(Widths.Length);
  32.  
  33.                 // If new row: set new height
  34.                 if (newRow)
  35.                 {
  36.                     originY += zoneHeight;
  37.                     originX = 0;
  38.                     zoneHeight = Heights[seed];
  39.                     newRow = false;
  40.                 }
  41.  
  42.                 // Set zone width
  43.                 zoneWidth = Widths[seed];
  44.  
  45.                 // Set X and Y
  46.                 x = originX; y = originY;
  47.  
  48.                 // Get biome and fix index
  49.                 string biome = Biomes[biomeIndex]; biomeIndex++;
  50.                 if (biomeIndex == Biomes.Length) biomeIndex = 0;
  51.  
  52.                 // Add zone
  53.                 world.Zones.Add(new Zone(biome, x, y, zoneWidth, zoneHeight));
  54.  
  55.                 // Fix origin X
  56.                 originX += zoneWidth;
  57.  
  58.                 // Check for new row
  59.                 if (originX >= width) newRow = true;
  60.             }
  61.  
  62.             // Return the final world
  63.             return world;
  64.         }
Expand to see the code.

I'm unaware of what actual change that was made, but this shows all the necessary data needed to understand what I actually did.
Random biome widths, random biome choices, and random landmarks (objects such as trees etc) in each biome.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76255