I have been working on a 2D random tilemap map generator algorithm. At first i just started a rand.next(-2,2) for the height of the next tile. Almost each map sky rocketed out of my array. So i tried rand.next(-200,200)/100 and that worked out a bit more but still my map graduatly goes up.
Now i implemented a lot more, some of that random but i added the code below too, it does seem to flat it out some more but out of the 100 times i generated the map the right end of the map was only 1 time lower then the left end. Maybe i'm paying too much attention to it since it was so obvious whit the rand.next(-2,2) but it still makes me wonder.
int shaper = rand.Next(0, 1000);
//flat
if (shaper < 100)
{
int collumns = rand.Next(10, 20);
if (tileCollumn + collumns >= endTile) collumns = endTile - tileCollumn;
LandGenerator(startTile + (heightCount * mapWidth), collumns, -1, 1, minTotalHeight, maxTotalHeight); //<-- This one seems to never increase or decrease hight... weird... I am seeing a lot of flat area's without a single bump indent or a single step up or down.
startTile += collumns;
tileCollumn += collumns;
}
else if (shaper < 300)
{
int collumns = rand.Next(5, 15);
if (tileCollumn + collumns >= endTile) collumns = endTile - tileCollumn;
LandGenerator(startTile + (heightCount * mapWidth), collumns, 0, 2, minTotalHeight, maxTotalHeight);
startTile += collumns;
tileCollumn += collumns;
}
etc..etc...
this is the land generator:
private void LandGenerator(int startTile, int collumns, int minHeight, int maxHeight, int minTotalHeight, int maxTotalHeight)
{
int step;
Random rand = new Random();
for (int i = 0; i < collumns; i++)
{
//TODO: Lower the possibility of the max and min numbers.
step = rand.Next(minHeight * 100, maxHeight * 100) / 100;
if (heightCount + step > minTotalHeight && heightCount + step < maxTotalHeight)
{
startTile += (step * mapWidth);
heightCount += step;
}
int location = startTile;
for (int y = location; y < mapHeight * mapWidth; y += mapWidth)
{
tiles[y].textureName = "dirt";
}
//increment start tile to the next collumn
startTile++;
}
}
Forgive me if the code seems a little cluttered i'm always making a mess when trying stuff out .