Basic procedural generated content works, but how could I do the same in reverse?
- by andrew
My 2D world is made up of blocks. At the moment, I create a block and assign it a number between 1 and 4. The number assigned to the nth block is always the same (i.e if the player walks backwards or restarts the game.) and is generated in the function below.
As shown here by this animation, the colours represent the number.
function generate_data(n)
math.randomseed(n) -- resets the random so that the 'random' number for n is always the same
math.random() -- fixes lua random bug
local no = math.random(4)
--print(no, n)
return no
end
Now I want to limit the next block's number - a block of 1 will always have a block 2 after it, while block 2 will either have a block 1,2 or 3 after it, etc.
Before, all the blocks data was randomly generated, initially, and then saved. This data was then loaded and used instead of being randomly called. While working this way, I could specify what the next block would be easily and it would be saved for consistency.
I have now removed this saving/loading in favour of procedural generation as I realised that save whiles would get very big after travelling.
Back to the present. While travelling forward (to the right), it is easy to limit what the next blocks number will be. I can generate it at the same time as the other data.
The problem is when travelling backwards (to the left) I can not think of a way to load the previous block so that it is always the same.
Does anyone have any ideas on how I could sort this out?