Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've read many other questions/answers about random level generation but most of them deal with either randomly/proceduraly generating 2D levels viewed from the side or 3D levels. What I'm trying to achieve is sort of like you were looking straight down on a Minecraft map. There is no height, but the borders of each "biome" or "section" of the map are random and varied. I already have basic code that can generate a perfectly square level with the same tileset (randomly picking segments from the tileset image), but I've encountered a major issue for wanting the level to be infinite: Beyond a certain point, the tiles' positions become negative on one or both of the axis. The code I use to only draw tiles the player can see relies on taking the tiles position and converting it to the index number that represents it in the array. As you well know, arrays cannot have a negative index. Here is some of my code:

This generates the square (or rectangle) of tiles:

    //Scale is in tiles
    public void Generate(int sX, int sY)
    {
        scaleX = sX;
        scaleY = sY;

        for (int y = 0; y <= scaleY; y++)
        {
            tiles.Add(new List<Tile>());

            for (int x = 0; x <= scaleX; x++)
            {
                tiles[tiles.Count - 1].Add(tileset.randomTile(x * tileset.TileSize, y * tileset.TileSize));
            }
        }
    }

Before I changed the code after realizing an array index couldn't be negative my for loops looked something like this to center the map around (0, 0):

for (int y = -scaleY / 2; y <= scaleY / 2; y++)
for (int x = -scaleX / 2; x <= scaleX / 2; x++)

Here is the code that draws the tiles:

            int startX = (int)Math.Floor((player.Position.X - (graphics.Viewport.Width) - tileset.TileSize) / tileset.TileSize);
        int endX = (int)Math.Ceiling((player.Position.X + (graphics.Viewport.Width) + tileset.TileSize) / tileset.TileSize);
        int startY = (int)Math.Floor((player.Position.Y - (graphics.Viewport.Height) - tileset.TileSize) / tileset.TileSize);
        int endY = (int)Math.Ceiling((player.Position.Y + (graphics.Viewport.Height) + tileset.TileSize) / tileset.TileSize);

        for (int y = startY; y < endY; y++)
        {
            for (int x = startX; x < endX; x++)
            {
                if (x >= 0 && y >= 0 && x <= scaleX && y <= scaleY)
                    tiles[y][x].Draw(spriteBatch);
            }
        }

So to summarize what I'm asking: First, how do I randomly generate a top-down 2D map with different sections (not chunks per se, but areas with different tile sets) and second, how do I get past this negative array index issue?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Both of your questions are answered separately on this site already.

For example, this question: Random map generation, can easily be applied to your situation. Simply ignore the height (or don't display it but use it in biome generation). As well as this one: How can I create a random "world" in a tile engine?

Secondly, the issue with negative arrays can easily be solved. Don't use arrays. There are plenty of basic data structures that would work. Linked lists for example. Check out this question: A way to store potentially infinite 2D map data?

share|improve this answer
    
Thank you for the response but due to my lack of experience I've decided that--for the time being--infinite procedural level generation is beyond me. I'm going to stick to using a very large map for now. –  Bagofsheep Sep 9 '12 at 14:41

You'll probably also want to look at http://pcg.wikidot.com/category-pcg-algorithms Doesn't seem to be updated anymore, but contains a good amount of information about various procedural generation techniques for various purposes.

share|improve this answer
    
Welcome to the site. That link is already mentioned in the second link of Byte56's answer. It's no use duplicating it here. –  Eric Oct 3 '12 at 10:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.