Procedural World Generation | How does it work?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers i'm griff batch and today we are talking about 2d landscape generation two examples of this that i'm better known for can be found in the game's paper minecraft and terraria stamped both of these games are able to generate seemingly random yet exciting worlds out of tiled blocks and i know a lot of you would love to know just how it was done well this is the first video i've made on the subject and what i want to do today is step you through the process i have used successfully in both of these games to really bring these virtual environments to life let's begin by taking a walk about a paper minecraft world and here we are yes it's paper minecraft instantly recognizable from the classic minecraft textures and the plain sky block size is quite large 40 pixels square if i remember correctly which means we see just 75 full tiles on the screen at once view distance was always rather limited but to counter this i added mouse look around so you can see a bit further ahead so the basic surface of the terrain is made up of dirt blocks topped with a single layer of grass the ground is very flat with just a few ups and downs here and there on the grass grows the odd plant and aha a tree awesome wow there's a lot of animals in this level ha even spawning animals is an interesting subject of its own ah a village i can't tell you how funny it was when i first introduced the village house spawning eggs into paper minecraft being able to pop down a house run away a bit and watch a house pop out of nowhere was such a laugh the level generation just made use of this popping down a number of house eggs and voila a whole village is born okay now so i've already dug a hole down here to show you the basic underground cave falling down past some coal oh zombie and we're down and right away we have some lava that's cool i'm in creative mode so i can safely explore this is a naturally formed cave typical in paper minecraft deep caves tend to have lava flows from the ceiling the height of the cave is between two and four blocks and it runs in any direction even vertically for short periods okay oh wow we've stumbled upon a hidden dungeon area with its chaser test an unexpected treat for us oh go on let's snag the crown yeah i rule okay then so i'm thinking this is where the cave system ends right time to turn on x-ray mode that makes everything visible to us now and oh wow the cave does actually continue on but not for long we hit bedrock so what i want you to notice is just how simple the level design underground is it's mostly rock scattered with patches of oars and cut up here and there by wiggling cave systems added to this we have the water and lava flows a splattering of special level features surface grass and trees but mainly what the main craft level is made up of is loads and loads of featureless rock as we speed through we find patches of ores and these tend to be rough circles of various sizes they can look a little odd when seen in x-ray mode but as soon as the lighting engine comes on they look quite natural right so minecraft levels don't have a lot more to them than this sure it has biomes and a few more surface minibiomes like lakes and sand but there's not a lot more to it so let's stop there and take a look at the more ambitious project terraria stamped just like with paper minecraft the game begins with a world generation phase and the levels are generated in much the same way however the scale of the world is far more detailed you can see right away that the tile size is so much smaller these tiles are just 8 by 8 pixels you could fit 25 of these inside a single paper minecraft block what's more there are actually five layers of tiles a background a wall a liquid tile a foreground tile and a brightness tile this leads us to having to draw almost 3 000 tiles for every screen remember in paper minecraft we only needed 75 to do the same job so yeah this is way way more demanding and that is why tavares stamped could not be a scrolling game but this extra detail means we get to see the level generation in far more detail which is why i wanted to focus on this engine as i describe how everything works as you can see we again have a mainly dirt based ground layer but due to the smaller block size the slope or curve of the ground is more pronounced grass is growing on the edges of dirt but isn't limited to the top blocks like in minecraft it also grows on the edges and bottoms of tiles too what does grow on the top are the extra grass shoots and flowers and also these marvelous trees talking of which i want to show you the exciting cave that i can see here below me so i need some wood to make a torch let's chop down a tree okay i'll craft a few torches and off we go place a torch and look at how the light spreads outwards i love that have you watched my pathfinding tutorial there's a link under this video because the spread of light here follows exactly the same rules as the pathfinding scripts so you might want to watch that again and see if you can see why this might be now do you see how we have vines growing down from the roof of caves that is so cool right and also we have wall tiles underground behind me hiding the sky this makes the underworld much more atmospheric the cave wiggling walls are well defined here and ah you can see some stone showing through too ah okay before we get deeper into this cave let me just take you back to the surface and finish exploring that we should find a small lake each level has a lake and a cave to be found yeah here it is and oh wow i can see an awesome cave just under the surface too and yeah this is as far as the level goes sadly unlike with paper minecraft we can't skip off to another biome by walking off the edge of the level gosh this is just too tempting i am going to break through the floor of this lake i love these sandbox simulations [Music] oh no oh the fishy help them i don't have a net to catch them with of you it's okay he's safe panic over right now that we have seen the surface let's pull up our terraria map yes we have a map introvert just press f and there we go the cross marks where we are right now okay this is really useful as we can see the shape of the terrain do you see the undulating curves of the grass what remains of the dugout lake oops and the beginnings of the cave system to the right even the tree stumps are nicely visible now if i switch into creative mode and bring back the map and this this is why i wanted to use the project to explain level generation now we can see everything every pixel on this screen is a tile in the game world that is a lot of tiles yes over one hundred thousand of them so what are the main components that make up etiva map we have solid land and sky the land has a peaceful up and down curve going on but then here and there are small bites taken out of it for interest a larger bite on the surface is filled with water or at least it was before i drained it ignoring the caves for a moment and looking behind them you'll see the top third of the ground is mostly dirt with a splattering of large stone blobs but from two-thirds down it reverses and the ground is stone with splashes and streaks of dirt breaking all of this up we have the capes and notice the crazy shapes they appear in like blobs slashes zigzags vertical and horizontal and all over the place some wide some narrow it looks crazy but this is what makes exploring the game so exciting you just can't predict it after this all that remains are the mainly smaller dots of oars and gems the areas of water how can i forget these and these quite exciting underground abandoned houses for you to stumble across deeper underground okay come on before i let you in on the secret of how this was generated let's just check out what it looks like deeper down underground back into the cave and now in creative mode i can dig really fast let's go straight down [Music] it's getting dark so i'll place a torch the light leaks through quite a few layers of dirt before it fades out oh cool we've broken through into a cave yeah look here we are on the map see my new tunnel looks like this isn't a very big cave but it's a cute little ecosystem with water and a squibble right back up we go exploring these caves is quite dangerous when you start out in terraria it's better to build yourself a safe house to begin with on the surface die slime [Music] ah nice an underground chest these contain cool booty i'll take that boomerang notice we've reached a depth in the level where the background wall has been replaced by an underground theme we can no longer place torches on this which makes things a bit harder yeah these larger vertical drops are very difficult to climb back up when we start out in survival mode do you see how exciting the cave system is i think it's far more fun to explore than the more simple minecraft caves but as far as world generation they are actually done very similarly this is just on a grander scale now at this depth the green vines stop growing and we will see no more grass but ores become more common we do have to watch out for more difficult mobs too though and it's a lot further to get back up to the surface if anything goes wrong hey just look at the shape of this cave isn't it complicated and cool looking can you even imagine how this could be generated randomly and automatically it's hard to believe but it wasn't actually as difficult as you may think now watch this let me turn on x-ray mode wow and everything is revealed now the interesting thing here is how flat everything suddenly looks and although the cave is still pretty awesome in detail it no longer looks quite so impressive here let me toggle back and forward a few times do you see how much the lighting transforms a game like this and look flying over here you can vividly see the horizontal line that separates the dirt biome from the lower stone biome and look how circular these stone areas are and this small cave too very circular just remember that for now okay the map continues down this way let's go deeper rushing down now oh man these cobwebs even slow me down in creative i should get out my sword okay we've hit some big water now calculating water levels in terraria is a lot more intensive than in paper minecraft but also a lot more fun don't ask me to teach you that for a while okay right i think we are at the bottom of the major cave system now we need to make our own way deeper underground let's go over and see one of those abandoned houses yeah i love that as we dig out this stone the water level behind us drops it's so cool and here we go my what a boring little abode just a barrel inside often they have a bit more than this can we find another this one perhaps of course in the real game in survival mode finding these places can be really tricky and very fun indeed look we have a clock two anvils and a dresser these would be very useful for decorating your own homes upon the surface notice these houses are complete with back walls too these need to be removed using the hammer tool okay before i conclude my little tour just take a look again at the shapes of the oars and caves and pretty much everything we come across do you see it circles circles everywhere just of different sizes and randomness so i keep pointing out circles why is that you'll soon see because it's time to learn how we generate these awesome worlds well to explain the basic idea i'm going to load up the terraria world map into my handy paint package if i zoom right in you can see that each coloured pixel here represents one tile of the game be it a brown dirt tile or a grey stone tile this tile data as you might have guessed is stored in our scratch game in a grid list look up my tutorial on grid lists if you want to know more there's a link under the video what you should realize though is how creating a map is much like painting a picture we just need to know the steps to take and the order to do them in so although in this tutorial i'm not going to teach the scratch coding today we will be looking at all the steps after all to code something up first you have to understand exactly what you are trying to code once you can do the drawing by hand then you can reproduce the same actions using scripts and automation we can start by filling the entire world with dirt and stone like so this would just require us to fill a scratch list with ones and zeros say that was the easy bit now you might think the next step would be to draw the sky but actually it's better to keep that until much later instead we address the fact that these blocks of color are looking a bit too perfect if we reference our original map we can see that the dirt biome still has many smaller stone areas and the lower stone biomes also have longer veins of dirt so how do we generate these organic patterns now you may be thinking purlin noise or some other clever mathematical technique would be the answer and wow yes we could do that and it would be awesome however if there is a way of getting close to the same results in a simpler way then i'm all for trying that out and what i've actually done is much simpler to understand than all that math wizardry let's zoom in and take a look at the lower region first and hone in on that dirt that appears within the stone do you see how it forms thin wiggly patterns i can take a small circular pen in my paint package and starting at one end trace a zigzagging line yes can you see how i can reproduce a very similar pattern remember how i kept drawing your attention to the circular nature of some of these ore veins well this is why this is the secret to my world generation drawing scribbles using a circular pen just in a controlled and yet random fashion here let me demonstrate using the dirt color i pick a pen size this round pen and then picking a random start position i draw a zigzagging scribble adjusting my pen size two if i want to as i go once we have enough dirt veins we stop ta-dah of course the question you may be wondering is how in scratch do we draw a thick line into a grid list hmm i think best not to worry too much about that right now but if you can figure out how to paint a filled circle of tiles pythagoras is your friend here and then do this over and over again down a set path then you'll be well on your way so what about the upper dirt layer well this one has scribbles of stone only the ones towards the surface are really small and short and the ones lower down well they become longer and much more bunched up like they are scribbled all over themselves let's draw those in next now the variety of scribble styles makes things feel a lot more natural as finally there are scattering of stone blobs using a smaller pen size but these don't follow any longer paths but remain small even at lower depths so we continued the theme by adding in the various oars these come in varying amounts at different depths but are almost always just small smudged circles of color that we splatter around in bands around the map right next up we are creating the caves and once more it's going to be a matter of creating some epic scribbles only this time using an eraser rather than a pen to allow us to remove the areas of solid rock and ore so draw with the empty tile our scribbles are diverse at times much thicker than before but also changing size frequently they also zigzag quite violently at times and even move up and down as they go just mainly downwards as a rule it's worth noting that these cave roots often cross over each other forming much more exciting caving patterns than the single zigzags could on their own then as a rule each level adds one far larger cave that begins at the top of the world breaking the surface and travels downwards again in a zigzagging pattern but we ensure it runs deep and very long this adds a lot of excitement to the level as the player has the option to adventure deep into the game perhaps even before they are ready to do so but the choice remains theirs now wow we are almost there so it's definitely time to draw in the sky the majority of this is easy we just remove a large rectangle of the upper dirt but well that leaves the surface too straight we want this biome to contain that pleasant undulating hills that we are so used to walking upon at spawn for this we will borrow a simple mathematical sine curve only to avoid being bored to death we can combine a couple of curves together to make something a bit more interesting this ends up cutting a nice shape something like this across our landscape cool can you see now why we did this last because this way it allows us to easily cut across any of our painted ore patterns cleanly if we'd done this first all these extra oars would have stuck out above the surface so we are on the home stretch now at this point we want to add in the more scripted structures like underground abandoned houses or in minecraft that dungeon the trick here is to be able to scan the map for cave areas of sufficient size to contain the structures and then just to place it there and make the required adjustments to meld it into the surroundings next we transform any dirt tiles that were on the surface into grass it's at this point that the drawing with a paint package reaches the limit of its usefulness so sticking with the map we see that further to just planting grass on the surface we then need to cycle our grass growing code to extend the grass around connecting tiles i love this code so much because if you watch it sped up it also causes grass on the underside of the tiles to sprout vines and these in turn drop down new grass onto the tiles below oh it's so cool in this way the whole upper biome can seem to spring into life oh yeah and we also grow a few trees on the grassy surface then we have the reskin step this step doesn't exist in paper minecraft but in terraria it makes a huge difference without this our level looks like oh this the small tiles need their costumes switching around to make them all connect together the right way if you have watched my mario tile scrolling tutorials on automatic tile arranging then that pretty much explains how this process works yeah i'll leave a link under the video and then just when you thought the hard work was done along comes water oh gosh i love water it's just so fun to simulate and play around with but oh man we are not even going to touch this today let's just check out the map and see how the water sits i hope it's enough to say that we just need to pick a number of random areas on the map and programmatically pour a volume of water from each point flowing it downwards until it reaches a local minima that is it can't flow down any further and then begin the process of filling and flowing until the water is all accounted for what you want to end up with is a number of distributed natural looking flat topped pools of water and with that apart from adding a splattering of chests and other loot the level generation is complete all that remains is for us to illuminate the sky and run the light flow algorithm to pretty the whole thing up just remember that a level can contain over 100 000 tiles so be prepared for some epic computation time and there we go that covers the entire manual level generation now the next step would be to work our way through each of these phases of generation and make scripts that do just the same thing in a lot of ways it was the scale of the terraria game that made it an immensely fun challenge to tackle in scratch and i enjoyed the challenge thoroughly generating the minecraft worlds turns out to be a lot easier but nearly all the main strategies remain the same if you want to make the surface biomes more exciting then perhaps we might consider investing more time in implementing the mathematics of purlin noise but for our needs this far simpler approach does us very well sometimes simple is better when coding with blocks overall i think the similarity of the terraria's stamp level with the original torii game is testament to the effectiveness of these techniques so i do hope you've enjoyed this video and that you might have learned something useful along the way this has been a very different video since we've not even placed a single block of code even so please do smash that like button and subscribe to the channel to catch the next exciting and informative video next week we'll do some real scratching but not on level generation i suspect if we do tackle this you can tell it will need a lot of episodes and i'm not even sure i'm quite ready to tackle that myself so until next time have a very pleasant week ahead and scratch on guys [Music]
Info
Channel: griffpatch
Views: 82,096
Rating: undefined out of 5
Keywords: scratch coding, scratch programming, scratch game, scratch 3, griffpatch, griffpatcho, mining, landmass generation, cave formation, coding, make a world, minecraft world
Id: Pgt82G4Jxac
Channel Id: undefined
Length: 23min 33sec (1413 seconds)
Published: Mon Nov 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.