I'll not stop making procedural generators for my game until I run out of things to generate.
This one makes background terrain to go with the trees from before. It currently has a weird bias towards gentle slopes while rising (towards the right) but steep slopes while falling ¯\_(ツ)_/¯
For those curious how it works (1/?)
It first generates a height at each tile boundary, one height for each x coordinate, stored as a simple 1D array. The first image is a representation of this height map, drawn onto the tile grid.
The possible height changes are pre-defined based on the available tiles. In this case, that's 0 (flat), 0.5 (half-slopes), and 1 (cliffs). It chooses randomly, influenced by the user-given parameters. If it chooses 1 (cliff), it generates a random cliff height.
For those curious how it works (2/?)
Once it's got a height map, it passes it to a function that matches it to tiles. It determines the slope between each adjacent pair of x-coordinates, which in turn determines the tiles to be used.
For example, if the slope is 0, it'll place a random "flat" tile at the given height. If the slope is 0.5, it also looks at the height to determine which tile to use (half-slopes are made of two tiles). Then, it fills everything below with "filled" tiles.
For those curious how it works (3/?)
Cliffs are special in that there aren't specific tiles for each particular slope, instead it places a "steepSlope" tile at the top of the cliff, and then fills the rest of the cliff with "cliff" tiles. So if the height at x = 4 is 10 and the height at x = 5 is 6, it'll put a "steepSlope" tile at (4,10), and "cliff" tiles at (4,9), (4,8), (4,7), and (4,6). Everything below that gets "filled" tiles.
...unless it decides to put a "cave" there instead.
For those curious how it works (4/?)
Filling in cliffs is done in a simple while (y >= bottom of cliff) loop. To make the cliffs more varied/organic, I also carve 1-tile-wide "caves" out of them. This makes the while loop more complicated:
At every y that's high enough,we have a chance to start a cave. If we do, we put a "caveTop" tile there, and set the "inCave" flag.
Whenever the "inCave" flag is true, the cliff tiles are placed 1 tile into the ground instead of at the "actual" cliff edge.
For those curious how it works (5/5)
Whenever we've in a cave, if we reach the bottom of the cliff or if we roll to stop the cave, the "inCave" flag is unset, and we place a "steepSlope" tile to transition back to the actual face of the cliff.
That's all that "caves" are, but they make the shapes so much better.
Also, whenever a cliff or flat tile is placed, if it happens to be the solid dark tile, there's a chance to place a detail "tileTop" or "tileSide" tile with it. Helps hide the grid :]
Mastodon.ART — Your friendly creative home on the Fediverse! Interact with friends and discover new ones, all on a platform that is community-owned and ad-free. Admin: @Curator. Moderators: @EmergencyBattle, @ScribbleAddict, @TapiocaPearl, @Otherbuttons, @katwylder