Okay, so the way Samphi generates Terrain has been the same since my very first prototype. At some point I'll go back to this and tighten it up I'm sure, but for now it works as follows:
I'll start by explaining that the room is a grid 16*16. When it is generated for the first time an object is placed in it that generates the terrain. It does so by choosing a random integer between 1 and 12 (in a variable named spawn), and then will perform different actions depending on the value. For example if spawn is less than or equal to 3 it will generate terrain forward, if it's greater 3 but less than 6 it will go down etc*. The object goes through this loop until it reaches the end of the screen and then it destroys itself. I want to move away from using an object and have it all handled by script. That's next on my to-do list.
*It's a little more complex than that as it has to stay within certain bounds, but that's a good enough explanation for the purpose of the article! I do this through a switch statement.
While deciding whether to go up, down or straight it also decided where to place trees, generate water and animals; it's all handled by this 1 object when the level 1st generates. I can do it this way because my rooms aren't huge. If I had a huge map like that of Terraria my method would just fail (and mine isn't very elegant) but it does its job, and like I say I'm going to re-visit it in the near future.
The top few layers of the terrain are dirt (objects), I COULD use all tiles but I find objects much easier to control, and under that is just tiles drawn to screen to fill in the lower terrain until you reach it.
So recently I've totally rewritten the way Samphi loads and saves terrain. I'll start with my previous method: it was truly horrible.
In the version of Samphi that is currently out now, when you save the game you are only saving the object in the room such as trees and animals. It saves the number of these objects and the x and y coordinates of each. Then when you load the game it loads each back into the room.
The terrain however is never saved, only the seed* used when the level was first generated is saved. This means that every time you load the level it is totally re-generated, then I load all the places where you previously dug and subtract those blocks from terrain then re-calculate all lighting and sprites for the second time ... This is really poor and was due to me not knowing a better way to do it! This is my first game so I'm learning as I go!
*A seed is the starting point for the algorithm that generates random numbers. If this is not changed then the "random" numbers will be generated the same each time, not random at all.
OH! Also the game doesn't use ANY tiles in the current version meaning every block is an object which is RIDICULOUSLY inefficient and causes problems on lower spec machines.
To my new, much better method
Now the level is only ‘generated' (by that I mean through my algorithm using the seed) when the level is first created.
When you save the game you now save ALL objects in the room, including the dirt blocks. I didn't do this the first time as I thought it would be too much data to save and load; I realize now that's mental, it loads the whole level in roughly 2 seconds. This switch has made the terrain much more stable, quicker loading and has given the ability to make is longer. It's currently 2-3 times longer than in the released version.
If you open the .ini file in the new version (the file I write all your save data to) you will see a HUGE list of items. For example dx = [Value]. The d means it's a dirt block, the x means it's its x coordinate, the number in the  is used in the loop when loading the blocks back so it can iterate through all saved blocks. This was I save each block's x and y coordinated and can easily reread them to load the level.
This will also make it easier to create and share maps; create your map, save the game, send you mate your level data and he can load that puppy right up and experience the same level as you!
So this post is a little more involved than normal but hope some of you find it interesting. All of this is work in progress code and systems that can be improved massively. If you have any suggestions leave them in the comments below! I'll try to have more visual updates out soon so stay tuned.