r/howdidtheycodeit • • May 23 '21

Question Minecraft World Saving

In a minecraft world there are lot's of blocks and their states. And some npc's. How do they save it.

46 Upvotes

11 comments sorted by

View all comments

Show parent comment

41

u/kid38 May 23 '21

To add a little bit of "how" to your great answer: Minecraft world consists of "chunks", 16×16×256 block segments. And chunks group up into regions, consisting of 32×32 chunks, which is how the game actually stores all the data (if you delete one of the region files, it erases all the changes to the world in that region, like you say). So that allows the game to only load a small part of the world around players (plus the chunk loaders).

12

u/nurdle11 May 23 '21

oh, of course, thank you! I completely forgot to go any of the fundamentals for how Minecraft actually works lmao

9

u/Ikaron May 24 '21 edited May 24 '21

I thought with the release or the Anvil format, chunk size changed to... 16x16x16? And it uses a 3d layout of chunks now.

Additionally, I'm pretty sure it's not only saving the chunks that you changed, but all chunks that you ever were near enough for them to be generated. So that includes all chunks at the edge of your view range.

Generally, a block has an ID, 0 for air, 1 for dirt I believe, 2 for stone, etc. So a chunk just stores 16x16x16 of these numbers (I think 16 bit at this point) so 8KiB of data for block IDs per chunk.

Some blocks though, like chests, need a lot of extra storage space to store some other information, like what items are contained in them. This is called "metadata" and every chunk gets 64KiB for it if I remember correctly. Every block then also has a "pointer" or "offset" of where its own metadata (if it has any) is located in that storage space. I don't know the exact data format for this but I assume it's a 16 bit offset, so another 8KiB per chunk.

Metadata is allocated on an "as needed" basis, meaning a chest that has fewer (or less complex) items in it will use up less of the combined metadata storage.

It used to be possible (and probably still is) to run out of metadata space, e.g. if the whole chunk is made up of chests filled with items that have a lot of data, e.g. weapons with multiple enchantments that aren't full durability. I think this just crashes the game.

Last but not least we have the storage space for entities that reside within this chunk. I am not sure if this is separate from the metadata storage or allocated from it. Either way it contains a list of all entities within this chunk, including their type, metadata (like horse colour), health, etc. If I had to guess, I'd say this is probably like 16KiB as that should allow for a few thousand entities per chunk.

Based on my numbers (which could definitely be off by a factor of 16: I wouldn't be surprised if metadata was only 4KiB per chunk. It is also possible that every "stack of chunks" shares a common metadata pool. A lot of the things I assume here are based on the old, pre-anvil format, where one chunk was 16x as large), we'd get these results: A chunk is made up of ~96KiB of data. This means a "stack of chunks" from bedrock to height limit is made up of 16 chunks = 1.6MiB. A region of 32x32 of these chunk columns then takes up around 1.2GiB of storage space. Quite a lot! That's why Minecraft worlds are compressed.

I don't know the exact encoding but I believe it's similar to run-length encoding. If there are 10 stone blocks next to each other, instead of storing "stone stone stone stone .... stone" (32 * 10 = 320 Byte) it'd just store "stone x10". (32 + 32 + 8 = 72 Byte), a 77.5% reduction in storage space!

Similarly, unused metadata space would probably not be saved to files. Which is significant, as a chunk consisting entirely of simple blocks, of which there are tens of thousands in a 32x32 region, would need 0 Bytes of metadata. In fact, almost all chunks are like this. Others, like chunks with plants (they remember how soon until they'll next grow! That's metadata), will need only a few hundred Bytes. This would be reducing storage consumption by about 98% here, for your average chunk in your average world.

The entity storage can be similarly compressed, probably to about 100 Bytes on average.

All of this combined, the average chunk (without loads of chests and the like) will take up about 3KiB. So a column takes up ~50KiB and a 32x32 region will take up about 40MiB. Much better!

3

u/kid38 May 24 '21

Thank you for your thorough explanation!