JS1k 2012
Part 2 : terrain generation

My 1K Minecraft needed a good terrain generator, so I searched for javascript implementations of random noise generators. There are pretty cool things out there but it was clear I would have to lower my mathematical ambitions to fit the algorithm in less than 1K of javascript. So I kept randomly dumping bell-shaped functions on a flat base, which gives pretty satisfying results. I tried various functions (exp(-x²), 1/(1+x²), cos((π/2) * x²/(1/4+x²)), etc.). I finally used the exponential, as it seemed a good compromise between simplicity and speed.

So I had my N*N table of altitudes. Negative figures represent different depth of water, 0 is the sea level (sand), and positive are emerged lands, with snow appearing above a certain threshold. To compensate an inherent problem of visibility in the isometric projection, I gave the possibility to rotate the view by +90° steps, thus facing alternatively N-E, W-N, S-W, E-S, and back to N-E. And to avoid the disturbing "world's end effect", I made the terrain "infinite" by cycling it in all directions, which may be considered superfluous since memory is not the real problem here. When digging, the altitude value for the current cell is decreased.

The possibilty to add blocks was a non-negociable feature. So I turned each cell of the table into an array, the first value being the altitude, and the others representing a stack of the incremental altitudes of blocks put on this location. So pushing 1 onto this stack adds a block, popping the stack removes the top block, and incrementing the top value lifts the top block, which allows to have more complex buildings than just piles of stone blocks.