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:
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!
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:
- public static World Generate(int width, int height)
- {
- // Create a new world
- World world = new World();
- // Generate a random generator
- Random rand = new Random();
- // Set the coordinates
- int x = 0, y = 0; string biome, image;
- // Until the X and Y values are outside the map: generate
- while (y < height)
- {
- // Create a seed for randomization (from 0 up to 9)
- int seed = rand.Next(10);
- // Split the map into zones of sizes based on the seed
- int zoneWidth = Widths[seed], zoneHeight = Heights[seed];
- // Get biome
- biome = Biomes[rand.Next(Biomes.Length)];
- // Cover area in tiles
- int row, col;
- for (int cy = 0; cy < zoneHeight; cy++)
- {
- // Set new row and column hvalues
- row = 1 + rand.Next(5);
- col = 1 + rand.Next(6);
- for (int cx = 0; cx < zoneWidth; cx++)
- {
- // Add landmarks in the middle of zones
- if (cx == zoneWidth / 2 && cy == zoneHeight / 2)
- world.Landmarks.Add(new Landmark(biome, x, y));
- // Randomize ground tiles slightly
- image = cx % rand.Next(5, 15) == 0 ?
- biome + "_Ground" : biome + "_GroundSpecial";
- // Place ground tile only if within bounds of world
- if (x < width && y < height)
- {
- // Place tile
- world.Ground.Add(new Tile(image, x, y));
- // Scenery placements (somewhat randomly)
- if (cx % col == 0 && cy % row == 0)
- {
- int r = rand.Next(Scenery.Biome(biome).Count);
- world.Sceneries.Add(Scenery.Get(Scenery.Biome(biome)[r],
- new Vector2(x, y)));
- }
- x++;
- }
- else if (x >= width)
- x = 0;
- }
- y++;
- }
- }
- Console.WriteLine(world.Ground.Count);
- // Return the world
- return world;
- }
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!