Minecraft Source Code Is Interesting
11 hours ago
- #Game Development
- #Minecraft
- #C++ Optimization
- Minecraft's old console source code was leaked, revealing interesting C++ optimizations and tricks used by 4J Studios.
- 4J Studios ported Minecraft from Java to C++ for consoles, rewriting everything from terrain generation to redstone mechanics.
- A pointer and counter were packed into a single 64-bit integer to save memory and avoid locks for light data storage.
- A garbage collector was implemented in 20 lines using a rotating set of delete queues for memory reclamation.
- Light data compression reduced memory usage by 80% by exploiting the fact that most light planes are either fully dark or fully bright.
- Bit-scattered coordinates (Z-order curve) improved cache performance by ensuring spatially close blocks are also close in memory.
- Self-describing compression tags allowed efficient storage of uniform blocks (e.g., solid stone or air) without extra data.
- Direct physical memory allocation (XPhysicalAlloc) was used to bypass the broken heap manager on Xbox 360.
- Java's standard library behaviors (e.g., Random, HashMap) were meticulously reimplemented in C++ to ensure world generation consistency.
- Explosions were simulated using 1,352 rays instead of physics, with each ray subtracting block resistance values.
- Entity IDs were managed using a bitfield allocator to prevent ghost entities in multiplayer.
- Cave generation used a "worm" algorithm with randomized ellipsoidal carving for natural-looking caves.
- Water color at biome boundaries was averaged across neighboring biomes for smooth transitions.
- Trees were generated top-down to optimize heightmap updates, placing leaves before trunks.
- Zombie sunlight burning was probabilistic, scaling with brightness for natural behavior.
- Charged creepers simply doubled their explosion radius via a boolean flag.
- Terrain generation used Perlin noise with multi-octave stacking for fractal-like landscapes.
- AABB pooling (1024 pre-allocated boxes per thread) eliminated heap allocations for collision detection.
- XOR trees enabled fast memory comparison for chunk compression checks.
- Developer comments revealed candid insights into the challenges of porting Java to C++.