Secret to No Man’s Sky and creating infinite video games
About
The video “The secret to No Man’s Sky and creating infinite video games” discusses how games can use procedurally generated content. This video is not part of a series, however, if you would like to see more videos on this topic, please comment below!
Transcript – The secret to No Man’s Sky and creating infinite video games
Hi! If you haven’t seen the trailer for No Man’s Sky from E3 2015, you’ll want to check it out. The interesting take away from the trailer is everything in the game is procedurally generated content. What does that mean? It means every level, every world, every plant, and every creature is randomly generated by the computer. That sounds great, but how do you make sure the world isn’t completely new when you come back to it? There’s a trick to it, and that’s what I’m going to show you in this video!
Randomness
The key to a good video game is randomness. Randomness is what makes a game interesting. If the player knows what to expect at every turn, the game quickly becomes boring. We’ve all seen this in video games. If you remember the game Dragon’s Lair, it had awesome graphics for the time. It was like watching a cartoon. However nothing was random. Once you figured out the pattern, the game became very boring. There was no replay value at all.
Platform games, like Mario and Sonic, are the same. Even Call of Duty. There’s more moving on the screen which makes it seem a bit more random. The levels are much deeper, and it takes longer to get through, but once you’re through you’re done.
The reason is these games were planned out by the developer ahead of time. They want to make sure there’s a bad guy guarding the treasure chest every time. They want to make sure the game is consistent between play, and more difficult the further you go. The tradeoff is, the game will end.
It doesn’t have to be this way. You can make a game that makes more content the further you go. Just just use random numbers to generate everything. Randomly generate where the world is, where the bad guys are, what they look like, where the cities and merchants are, and so on. This means you don’t have to waste hard drive space for every level. Just generate a new one, and the player can play forever.
The one problem with this is, if you’re always using random numbers, everything will always be random. If your game is a space game, you want the stars and planets to be predictable. They should always be in the same place. Otherwise the player will be forever lost. It’s a contradiction. You want randomness, but you also want predictability. When you turn off your computer, you want to return to the world you were playing. Not a completely different one. So what do we do?
Random numbers are impossible on computers
Here’s an interesting thought. Random numbers on computers are not random. In fact, there’s no such thing as a random number on a computer. Computers are deterministic, so it’s impossible for it to generate a random number. Computers can generate a list of numbers that appear to be random, but you can always generate the same exact list of random numbers.
Every random number generator uses something called a seed number. This number tells your random number generator where to start. If you don’t provide a seed value, your computer will use something like the time of day. If you can predict the number your computer is using, you can predict the random number sequence.
Let’s go do it in code.
How does that help us create infinite video games
Back to video games. We now know a computer cannot generate random numbers. It can only generate pseudo random numbers. Pseudo random means a process that looks random, but is not. How is that a good thing?
Well we know the numbers that a computer does generate look random. To keep things simple, lets create a universe that is 5 x 5. That gives us 25 regions in our universe.
Each region has a galaxy. We can say every position has an x,y coordinate from 0 to 4. We can encode each position in the map with two digits. The first digit is the x, and the second is the y. We now have a unique seed for each galaxy in our universe.
Say we have 3 different types of galaxies: spiral, elliptical, and irregular. For each position, we seed our random number generator with the position. Then we generate a number from 0 to 2. That gives us the galaxy type.
Since we are seeding our random number generator, we will get the same answer every time we ask. Let’s try that.
We now have a procedure that can tell us what type of galaxy is at a given position. We do not need to store the information. When we need it, we just generate it using the same position seed. This is called procedurally generated content. That’s the secret sauce in No Man’s Sky. When you use the same seed, you’ll generate the exact same sequence. So if your seed is 42, it will always generate “irregular”. While our universe looks random, it will be the same every time you generate it.
When a player enters a galaxy, you can then break the galaxy up into smaller parts and use those locations as seeds to determine stars, star positions, star types, etc. Again you use the same seed. You can keep doing this all the way down to the surface of a planet. Using pseudo random numbers in the terrain engine, the planet will look the same every time you visit it. You just plug in the seed, and generate the same random planet. Mountains will generate in the same place. Cities will generate in the same place. Where they are from planet to planet will be different. You apply the rules on how game artifacts are generated, and the computer will generate the same random list of numbers.
Now we did our universe example with a 10 by 10 matrix. Imagine if it was 1000s by 1000s. It won’t take more space in our game, but the playing field will be dramatically larger. Hopefully you see the way this can scale. Really the only things you need to keep track of are important game objects that can be affected by time. If it can be moved, and you need to remember it’s position, just save that game object separate from your procedurally generated content.
Close
Now this video over simplified many things. For example, every region has to have a unique seed. Otherwise you’ll get areas that repeat. That can force you down paths of encoding information in your seeds like we did for the x and y coordinates of a universe. How do you handle crossing the universe edge? Ideally you’d like a new region to be generated and go on forever. The procedures to generate more complex things like cities and roads between cities can be complex. The point is this isn’t a magic bullet. You’re trading off storage and design requirements for procedural complexity in your code.
Hopefully this gives you some exciting ideas for your next game! If you want to see more videos on this topic, let me know in the comments below. See you in the next video!
Tools Used
- Java
- NetBeans
Media Credits
All media created and owned by DJ Spiess unless listed below.
- Background image from Pixabay.com
Following images from PixaBay.com used under CC0 License
- https://pixabay.com/en/alien-planet-exoplanet-ocean-583719/
- https://pixabay.com/en/exoplanet-exomoon-planet-gas-giant-571906/
- https://pixabay.com/en/exoplanet-planet-alien-gas-giant-571900/
Hubble Image via NASA
http://hubblesite.org/gallery/album/galaxy/pr2012010a/xlarge_web/
Galaxy images via Wikipedia
Media used under Fair Use (https://en.wikipedia.org/wiki/Fair_use)
- No Man’s Sky trailer owned by Hello Games (http://www.hellogames.org)
- Dragon’s Lair published by Cinematronics, currently owned by Digital Leisure (http://www.digitalleisure.com)
- Sonic the Hedgehog owned by Sega Holdings Co, Ltd. (www.sega.com)
Tiled Map Editor for Mac (Open Source on GitHub – https://github.com/bjorn/tiled/wiki)
Get the code
The complete video lesson list for this free online programming course can be found on the course syllabus page.
The source code for “The Secret to No Man’s Sky and Creating Infinite Video Games” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:
git clone https://github.com/deege/deegeu-java-infinite-worlds.git
Go to the Support > Getting the Code page for more help.
If you find any errors in the code, feel free to let me know or issue a pull request in Git.
Don’t miss another video!
New videos come out every week. Make sure you subscribe!
Comments
DJ Spiess
Your personal instructor
My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.