I would have a table containing what should generate every, say, 10 layers.
local layers = {
-- Each index is 10 layers; 1 is the first 10, 2 is the next 10, etc.
{{"Dirt", 80}, {"Stone", 10}, {"Copper", 9}, {"Coal", 1}},
{{"Dirt", 50}, {"Stone", 20}, {"Copper", 15}, {"Coal", 10}, {"Iron", 5}}
-- And so on for every 10 layers
}
Then, every time a player digs a block, it should check how deep it is. If the player is 5 blocks deep, go in layers[1]
as it is within the first 10 blocks and randomly choose what will generate. If the player is say, 12 blocks deep, go to layers[2]
. Note how all the numbers add up to 100, a full percentage. You would ideally do math.random(1, 100)
to pick what will generate in the space made available. You can see that Dirt is the most likely to generate while other materials are less likely. Then, as the list goes on, Dirt loses its frequency and other materials get rarer. By the time you reach say, the 8th item on the list, you could have rare stuff like {"Diamonds", 5}
to start generating those.
This is a basic idea of how I’d go about it. If anyone has a simpler idea feel free to chime in though as I’ve never actually made a full on mining game before, though I’ve played a whole bunch of them like Epic Mining 2.