I have read many topics on random world generation on this site and others, and I roughly understand most of it. What I can't see is how to randomly place objects on the generated tile map I get. Say I want to generate a forest. How would I go about doing this? I have tried studying other people's code. I delved into Notch's Minicraft to look at how it generates its random worlds.
I have identified this part of the code that generates trees for example.
for (int i = 0; i < w * h / 400; i++) {
int x = random.nextInt(w);
int y = random.nextInt(h);
for (int j = 0; j < 200; j++) {
int xx = x + random.nextInt(15) - random.nextInt(15);
int yy = y + random.nextInt(15) - random.nextInt(15);
if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
if (map[xx + yy * w] == Tile.grass.id) {
map[xx + yy * w] = Tile.tree.id;
}
}
}
}
However, this just confuses me. I need to be able to see how this code translates into spread out trees. There is also code for generating the desert.
for (int i = 0; i < w * h / 2800; i++) {
int xs = random.nextInt(w);
int ys = random.nextInt(h);
for (int k = 0; k < 10; k++) {
int x = xs + random.nextInt(21) - 10;
int y = ys + random.nextInt(21) - 10;
for (int j = 0; j < 100; j++) {
int xo = x + random.nextInt(5) - random.nextInt(5);
int yo = y + random.nextInt(5) - random.nextInt(5);
for (int yy = yo - 1; yy <= yo + 1; yy++)
for (int xx = xo - 1; xx <= xo + 1; xx++)
if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
if (map[xx + yy * w] == Tile.grass.id) {
map[xx + yy * w] = Tile.sand.id;
}
}
}
}
}
Is this just way over my head or is there some kind of algorithm that I'm missing here?