Search code examples
c#xna

Civilization 1 like tilemap generation


Okay so, I have looked at a lot of question from people like my self who are beginners in programming. And most of the time their questions are hard to answer or understand what the question really is. I will do my best to be as specific as possible about my problems so the above mentioned doesn't happen.

First off I have been following the tutorials over at http://xnagpa.net/xna4rpg.php. And my tile engine is based off the one that Jamie McMahon makes in his rpg tutorial series. Just so you know what the general structure of my tile engine is like.

Secondly I will try to explain what I'm trying to do inside the tile engine. I recently found an article about how the original Civilization generated their maps. http://forums.civfanatics.com/showthread.php?t=498630 And I rather like this approach to generating a "world" style map if you will. ie: oceans, continents, islands ect. So I decided to try to take this and implement it into my tile engine. It works for the most part. The parts that I added to the tile engine are supposed to randomly pick a location in the specified map layer (y,x) and then from that location generate a chunk of land(or tiles) and then replace the tiles in the map layer with the tiles created in the chunk of land. (ill show the code in a minute) and then do that for a desired amount of either number of landmasses(chunks) or continue creating chunks of land until the number of land tiles is equal to a desired amount of land tiles.

My Problem: My program does what its supposed to (as mentioned above) except it only ever makes one landmass.(Chunk of land tiles) It does everything else just fine but it for some reason will not make more than one landmass. Now I suspect that it actually is making the other landmasses but somehow the way the tile engine is set up to display map layers is causing the landmass's to be covered up with water. Maybe its a layering issue. But It shouldn't be because the landmass's are all part of the same layer. So I'm completely baffled as to why its doing this.

   public void GenerateLandChunks(MapLayer layer)
    {
        Tile tile = new Tile(0, 3);

            Random random = new Random();

            int x = random.Next(8, layer.Width - 10);

            int y = random.Next(10, layer.Height - 20);

            int length = random.Next(10, 70);
            for (int i = 0; i < length; i++)
            {
                if (length != 0 && x > 8 || x < layer.Width - 10 && y > 10 || y < layer.Height - 20)
                {
                    layer.SetTile(y, x, tile);
                    layer.SetTile(y, x + 1, tile);
                    layer.SetTile(y + 1, x, tile);

                }

                x = random.Next(x - 1, x + 2);
                y = random.Next(y - 1, y + 2);
            }


    }

This is my method for generating the actual chunks it does what I want it to. (ABOVE)

            MapLayer randomLayer = new MapLayer(100, 100);

        for (int y = 0; y < randomLayer.Height; y++)
        {
            for (int x = 0; x < randomLayer.Width; x++)
            {
                Tile tile = new Tile(1, 3);
                randomLayer.SetTile(x, y, tile);
            }
        }

        int landMasses = 5;
        for (int i = 0; i < landMasses; i++)
        {
            randomLayer.GenerateLandChunks(randomLayer);
        }

This is where I create the map layer. I initially set the entire map to water tiles(tile (1,3)) then I tell it to generate 5 landmasses.

It seems like this should work. And it does but like I said only for the first one. It doesn't display the other 4 land masses.

My Question: Is there anything you can see here that I'm doing wrong in order to accomplish what I'm trying to do?

If you need more of the code to understand whats going on let me know and ill post what ever you need. And like I said everything other than what I have posted is the exact same as it is in Jamie McMahon's tile engine.

I'm sorry if I have come off as unclear or if my question is hard to answer. I tried to make it as straight forward as possible. Thank you for your time.


Solution

  • So much text for such a simple answer. The problem is that a new Random object is generated every time, and so the "random" values are the same every time. That is how random number generators work, the numbers are not actually random, but "pseudorandom", and if you use the same random function and the same seed you will get the same progression of seemingly random numbers.

    Default Random constructor seeds the generator with some value based on time, but if two generators are created with very small time period then that time value will be the same.

    Move the Random object outside the function, so that it is used for all random generations, or move the loop inside (but Random outside the loop).