WEBVTT
Kind: captions
Language: en

00:00:00.320 --> 00:00:05.200
This $1 microcontroller is the&nbsp;
world’s smallest Minecraft server,&nbsp;&nbsp;

00:00:05.200 --> 00:00:09.360
with infinite world generation,&nbsp;
multiple biomes and caves, crafting,&nbsp;&nbsp;

00:00:09.360 --> 00:00:14.640
mining, building, and pretty much everything&nbsp;
you'd expect in a survival Minecraft server,&nbsp;&nbsp;

00:00:14.640 --> 00:00:20.000
all running on a chip tens of thousands&nbsp;
of times weaker than a modern smartphone.

00:00:20.000 --> 00:00:24.880
Now, hold on – Minecraft servers are&nbsp;
notorious for requiring gigabytes of&nbsp;&nbsp;

00:00:24.880 --> 00:00:31.280
"dedotated wam", but here I'm claiming&nbsp;
to do it in under 300 kilobytes. And,&nbsp;&nbsp;

00:00:31.280 --> 00:00:35.920
while computer processors from two decades&nbsp;
ago measure their clock speeds in gigahertz,&nbsp;&nbsp;

00:00:35.920 --> 00:00:42.160
this thing tops out at 160&nbsp;
megahertz. How is this possible?

00:00:42.160 --> 00:00:47.280
Now, obviously, this isn't running an authentic&nbsp;
Mojang-approved Minecraft server. To make this&nbsp;&nbsp;

00:00:47.280 --> 00:00:52.560
work, I wrote my own Minecraft server from&nbsp;
the ground up in the C programming language,&nbsp;&nbsp;

00:00:52.560 --> 00:00:57.520
making sure to squeeze every bit of performance&nbsp;
out of the target hardware. That's why I'm calling&nbsp;&nbsp;

00:00:57.520 --> 00:01:03.600
the project "bareiron" – it requires working very&nbsp;
close to the bare-metal hardware that it runs on.

00:01:03.600 --> 00:01:08.720
First, let's set the record straight. I'm not&nbsp;
saying that the Notchian server is horribly&nbsp;&nbsp;

00:01:08.720 --> 00:01:12.640
optimized (even though it is), and that&nbsp;
somehow just writing better code leads to&nbsp;&nbsp;

00:01:12.640 --> 00:01:18.080
a million percent performance increase. Instead,&nbsp;
I'm just cutting corners wherever possible,&nbsp;&nbsp;

00:01:18.080 --> 00:01:22.320
prioritizing performance over&nbsp;
features in every step of the way.

00:01:22.320 --> 00:01:26.240
And one major example of&nbsp;
this is terrain generation.

00:01:26.240 --> 00:01:31.040
At its core, a world generation algorithm&nbsp;
is just a bunch of random noise, adjusted&nbsp;&nbsp;

00:01:31.040 --> 00:01:35.040
to look like a recognizable landscape.&nbsp;
Of course, it can't be purely random,&nbsp;&nbsp;

00:01:35.040 --> 00:01:37.200
because then the terrain wouldn't make any sense.

00:01:37.200 --> 00:01:42.240
Instead, vanilla Minecraft uses Perlin noise,&nbsp;
which is an algorithm that generates a continuous&nbsp;&nbsp;

00:01:42.240 --> 00:01:47.840
noise gradient, creating a sort of foggy or&nbsp;
cloudy texture instead of pure TV static.

00:01:47.840 --> 00:01:52.800
But now this has the problem of being too&nbsp;
smooth, so to fix that, multiple layers of&nbsp;&nbsp;

00:01:52.800 --> 00:01:58.080
Perlin noise are stacked in so-called octaves,&nbsp;
to create more natural-looking randomness.

00:01:58.080 --> 00:02:02.640
To determine specific landscape features,&nbsp;
such as hills, rivers and vegetation,&nbsp;&nbsp;

00:02:02.640 --> 00:02:07.920
various biome-specific parameters are applied&nbsp;
to the noise maps. In the overworld, these are:&nbsp;&nbsp;

00:02:07.920 --> 00:02:13.280
temperature, humidity, continentalness,&nbsp;
erosion, weirdness, and depth.

00:02:13.280 --> 00:02:17.920
This is still just the tip of the iceberg when it&nbsp;
comes to world generation in vanilla Minecraft.&nbsp;&nbsp;

00:02:17.920 --> 00:02:22.480
It should be no surprise that simply loading&nbsp;
the world for a party of friends requires a&nbsp;&nbsp;

00:02:22.480 --> 00:02:27.840
beefy computer. While Perlin noise isn't&nbsp;
computationally expensive, it's not free,&nbsp;&nbsp;

00:02:27.840 --> 00:02:31.760
either. Especially when you have to&nbsp;
stack multiple octaves. The field of&nbsp;&nbsp;

00:02:31.760 --> 00:02:38.480
biome parameters also adds complexity and memory&nbsp;
usage, which is something we really can't afford.

00:02:38.480 --> 00:02:43.600
This is a big part of the reason why a Minecraft&nbsp;
server requires a ton of RAM – chunks that&nbsp;&nbsp;

00:02:43.600 --> 00:02:49.120
are actively in use are stored in memory, to&nbsp;
avoid repeating these expensive calculations.&nbsp;&nbsp;

00:02:49.120 --> 00:02:53.920
But on my microcontroller, doing so for&nbsp;
just a handful of chunks would already&nbsp;&nbsp;

00:02:53.920 --> 00:02:59.280
take up more RAM than I have, and I want this&nbsp;
thing to support infinite world generation.

00:02:59.280 --> 00:03:04.160
In essence, I need an approach that allows&nbsp;
for a continuous noise gradient without&nbsp;&nbsp;

00:03:04.160 --> 00:03:07.760
layering expensive noise maps,&nbsp;
supports biome-specific features&nbsp;&nbsp;

00:03:07.760 --> 00:03:13.440
without complicated parameters, and doesn't&nbsp;
require storing anything in the server's memory.

00:03:13.440 --> 00:03:17.680
Effectively, what I'm asking for is a magic&nbsp;
function that can tell me what block sits&nbsp;&nbsp;

00:03:17.680 --> 00:03:21.760
at a given coordinate, without referring&nbsp;
to memory or calculating expensive noise&nbsp;&nbsp;

00:03:21.760 --> 00:03:28.080
maps. And I think I know exactly the right tool&nbsp;
for the job. When it comes to cheap gradients,&nbsp;&nbsp;

00:03:28.080 --> 00:03:32.720
nothing beats the efficiency&nbsp;
of bilinear interpolation.

00:03:32.720 --> 00:03:35.280
While it might sound like a bunch&nbsp;
of word soup at first glance,&nbsp;&nbsp;

00:03:35.280 --> 00:03:39.200
you've probably actually encountered&nbsp;
bilinear interpolation yourself.

00:03:39.200 --> 00:03:43.200
For example, when resizing a pixelated&nbsp;
image to an arbitrary factor,&nbsp;&nbsp;

00:03:43.200 --> 00:03:48.560
the borders between the pixels often become&nbsp;
blurred. This is because image software often&nbsp;&nbsp;

00:03:48.560 --> 00:03:54.800
applies bilinear scaling. And, crucially,&nbsp;
the result of this is a continuous gradient.

00:03:54.800 --> 00:03:59.600
That already ticks the first item off of the&nbsp;
list, but how do we obtain the underlying noise&nbsp;&nbsp;

00:03:59.600 --> 00:04:05.120
without having to store or generate a noise map?&nbsp;
I mean, in the image example, the pixelated icon&nbsp;&nbsp;

00:04:05.120 --> 00:04:10.560
could be considered the noise map. And, yes, when&nbsp;
we scale up a bunch of actual noise, we do end up&nbsp;&nbsp;

00:04:10.560 --> 00:04:16.000
with a smoother noise gradient, comparable in some&nbsp;
ways to the Perlin noise of vanilla Minecraft.

00:04:16.000 --> 00:04:19.920
But this requires having the random&nbsp;
data to begin with. If I just threw&nbsp;&nbsp;

00:04:19.920 --> 00:04:24.800
a random number generator at it, then the&nbsp;
terrain would change every time it's loaded.

00:04:24.800 --> 00:04:28.720
I need to "anchor" the RNG to&nbsp;
something deterministic – for example,&nbsp;&nbsp;

00:04:28.720 --> 00:04:31.840
the X and Z coordinates of the&nbsp;
chunk. For any given chunk,&nbsp;&nbsp;

00:04:31.840 --> 00:04:35.600
these will obviously never change,&nbsp;
but they're also kind of predictable.&nbsp;&nbsp;

00:04:35.600 --> 00:04:39.040
This is what it would look like if I used&nbsp;
just the coordinates for the noise data.&nbsp;&nbsp;

00:04:39.040 --> 00:04:43.680
Since the coordinates change incrementally,&nbsp;
the terrain follows a similar pattern.

00:04:43.680 --> 00:04:47.520
But what if, instead of using&nbsp;
the coordinates by themselves,&nbsp;&nbsp;

00:04:47.520 --> 00:04:52.480
I'd provide them to the RNG function&nbsp;
as a seed – that way I'm still getting&nbsp;&nbsp;

00:04:52.480 --> 00:04:57.600
a random-looking number, but it'll&nbsp;
be the same random number each time.

00:04:57.600 --> 00:05:00.080
This now ticks the second item off of the list,&nbsp;&nbsp;

00:05:00.080 --> 00:05:05.600
because all I need to do to get a data point for&nbsp;
the noise is generate one pseudo-random number,&nbsp;&nbsp;

00:05:05.600 --> 00:05:10.160
which is a relatively cheap operation,&nbsp;
even on a very slow processor.

00:05:10.160 --> 00:05:14.880
Let's see how this works in practice. Say&nbsp;
I'm trying to generate the surface of just&nbsp;&nbsp;

00:05:14.880 --> 00:05:19.760
one chunk for now. There are 4 corners for&nbsp;
this chunk, each with unique coordinates. I&nbsp;&nbsp;

00:05:19.760 --> 00:05:24.880
pass these coordinates into the RNG function&nbsp;
to get fixed-digit pseudo-random numbers.&nbsp;&nbsp;

00:05:24.880 --> 00:05:30.640
The last digit of these numbers is then treated&nbsp;
as the height of each point. And finally,&nbsp;&nbsp;

00:05:30.640 --> 00:05:36.880
to generate the shape of the terrain, I apply&nbsp;
bilinear interpolation on these 4 points.

00:05:36.880 --> 00:05:42.880
Any adjacent chunks will share up to 2 points with&nbsp;
this chunk, making the interpolation continuous&nbsp;&nbsp;

00:05:42.880 --> 00:05:49.360
across chunk boundaries. Continuing this pattern,&nbsp;
we get something resembling irregular terrain.

00:05:49.360 --> 00:05:53.440
For this example, I'm using just one of the&nbsp;
digits from the random number. Adding up&nbsp;&nbsp;

00:05:53.440 --> 00:05:57.680
multiple digits makes the terrain more regular&nbsp;
– kind of like how the more coin flips you do,&nbsp;&nbsp;

00:05:57.680 --> 00:06:03.360
the closer the distribution gets to an even 50/50.&nbsp;
In turn, reducing the amount of factors makes the&nbsp;&nbsp;

00:06:03.360 --> 00:06:09.280
terrain more jagged. Using these parameters, we&nbsp;
can play around with the shape of the terrain.

00:06:09.280 --> 00:06:13.360
Of course, I'm not actually using digits&nbsp;
from a decimal number – under the hood,&nbsp;&nbsp;

00:06:13.360 --> 00:06:18.000
it's a binary value, and I'm just picking&nbsp;
how many bits to use for each factor.

00:06:18.000 --> 00:06:22.960
Now, that's all well and good for surface&nbsp;
terrain, but what about the underground?&nbsp;&nbsp;

00:06:22.960 --> 00:06:27.680
In vanilla Minecraft, caves have their own&nbsp;
separate algorithm, and in newer versions&nbsp;&nbsp;

00:06:27.680 --> 00:06:33.120
even their own set of biomes. That sounds&nbsp;
like we're about to double the workload.

00:06:33.120 --> 00:06:41.200
Then again, we've already generated valid&nbsp;
terrain, so why don't we just... repurpose that?

00:06:41.200 --> 00:06:45.760
My approach was to simply mirror&nbsp;
the existing surface terrain.

00:06:45.760 --> 00:06:49.680
In-game, this ends up looking a lot&nbsp;
like one of those wide deepslate caves,&nbsp;&nbsp;

00:06:49.680 --> 00:06:53.600
and as long as you don't think about&nbsp;
it too much, it passes as good enough.&nbsp;&nbsp;

00:06:53.600 --> 00:06:57.920
All that was left was to sprinkle in some&nbsp;
ores – once again, due to cutting corners,&nbsp;&nbsp;

00:06:57.920 --> 00:07:03.040
these don't come in veins like they do in vanilla.&nbsp;
And, to make sure that mining isn't an entirely&nbsp;&nbsp;

00:07:03.040 --> 00:07:09.440
extinct activity, I hid the diamonds under&nbsp;
the lowest point of most of the cave volume.

00:07:09.440 --> 00:07:13.680
Let's take a look at our list again – the&nbsp;
next two points mention those biome-specific&nbsp;&nbsp;

00:07:13.680 --> 00:07:19.280
features. But in this case, I don't really see&nbsp;
a reason to calculate temperature or humidity,&nbsp;&nbsp;

00:07:19.280 --> 00:07:23.360
given that our noise is, at the end of&nbsp;
the day, relatively primitive. Instead,&nbsp;&nbsp;

00:07:23.360 --> 00:07:25.600
I'll just sprinkle trees throughout the forests,&nbsp;&nbsp;

00:07:25.600 --> 00:07:30.640
cacti and dead bushes throughout deserts,&nbsp;
short grass shrubs among the snow, and so on.

00:07:30.640 --> 00:07:35.520
The position of these elements is determined by&nbsp;
one of the random numbers of the chunk corners,&nbsp;&nbsp;

00:07:35.520 --> 00:07:37.520
so that's not really that interesting.&nbsp;&nbsp;

00:07:37.520 --> 00:07:42.640
The biomes themselves, through –&nbsp;
well, that's a different story.

00:07:42.640 --> 00:07:46.000
What would make the most sense is&nbsp;
to layer some kind of biome noise&nbsp;&nbsp;

00:07:46.000 --> 00:07:50.880
on top of our terrain noise. However, the&nbsp;
linear interpolation trick starts to fall&nbsp;&nbsp;

00:07:50.880 --> 00:07:56.640
apart on a scale like this. And remember – I'm&nbsp;
trying to avoid unnecessarily layering noise,&nbsp;&nbsp;

00:07:56.640 --> 00:08:00.880
as that would double the compute requirements.&nbsp;
There's also a lot of work required to make&nbsp;&nbsp;

00:08:00.880 --> 00:08:06.720
sensible transitions between biomes, and to&nbsp;
prevent anomalies like overly tiny or huge biomes,&nbsp;&nbsp;

00:08:06.720 --> 00:08:10.800
so I decided to go with a&nbsp;
simpler tile map approach.

00:08:10.800 --> 00:08:16.160
Basically, we'll make each biome a circular&nbsp;
island, surrounded by other such islands in a&nbsp;&nbsp;

00:08:16.160 --> 00:08:20.880
grid. The type of biome that each island&nbsp;
holds is then determined by a small,&nbsp;&nbsp;

00:08:20.880 --> 00:08:24.640
repeating pattern table, which is&nbsp;
calculated from the world seed.

00:08:24.640 --> 00:08:30.080
Yes, this does make each biome the same size, and&nbsp;
makes the placement of biomes painfully grid-like&nbsp;&nbsp;

00:08:30.080 --> 00:08:36.160
and predictable, but as you'll soon find out,&nbsp;
this is a necessary sacrifice for performance.

00:08:36.160 --> 00:08:41.360
To make the landscape unique for each biome, we&nbsp;
just have to adjust the amount and scale of the&nbsp;&nbsp;

00:08:41.360 --> 00:08:47.040
random factors that we combine. In the forest&nbsp;
or plains, 4 separate factors are used to get&nbsp;&nbsp;

00:08:47.040 --> 00:08:53.120
mostly flat terrain with occasional features. In&nbsp;
the desert, terrain height variation is limited 6&nbsp;&nbsp;

00:08:53.120 --> 00:08:59.840
blocks, and never goes below sea level. In snowy&nbsp;
plains, terrain height varies by up to 14 blocks,&nbsp;&nbsp;

00:08:59.840 --> 00:09:03.920
using only 2 random factors to&nbsp;
increase the variety of hills.

00:09:03.920 --> 00:09:09.440
Checking our list now, there's only one thing&nbsp;
left. We must avoid storing any of these&nbsp;&nbsp;

00:09:09.440 --> 00:09:13.920
blocks in memory. For world generation,&nbsp;
this just means regenerating the chunks&nbsp;&nbsp;

00:09:13.920 --> 00:09:18.240
each time a player loads them. Of course,&nbsp;
that is a lot of repetitive compute work,&nbsp;&nbsp;

00:09:18.240 --> 00:09:22.320
but the silver lining is that we only need&nbsp;
to do this when the player crosses into a&nbsp;&nbsp;

00:09:22.320 --> 00:09:28.880
new area. Otherwise, the Minecraft client is&nbsp;
able to keep nearby chunks in its own memory.

00:09:28.880 --> 00:09:33.920
However, in many cases, we still need&nbsp;
to be able to query specific blocks,&nbsp;&nbsp;

00:09:33.920 --> 00:09:37.520
like to know what item to give a&nbsp;
player when they've mined something.

00:09:37.520 --> 00:09:43.200
It would suck to have to generate the entire&nbsp;
chunk each time that a player mines a block,&nbsp;&nbsp;

00:09:43.200 --> 00:09:48.480
but luckily, we don't have to. Chunk&nbsp;
corner coordinates can be obtained&nbsp;&nbsp;

00:09:48.480 --> 00:09:52.960
from the player's coordinates, and bilinear&nbsp;
interpolation can return the height value at&nbsp;&nbsp;

00:09:52.960 --> 00:09:57.760
any point on the plane, so by design&nbsp;
this approach already lets us query&nbsp;&nbsp;

00:09:57.760 --> 00:10:02.800
for specific blocks with minimal&nbsp;
compute and no net memory usage.

00:10:02.800 --> 00:10:08.160
That said, I've left one last thing unanswered,&nbsp;
and that's about what happens to the blocks&nbsp;&nbsp;

00:10:08.160 --> 00:10:13.120
that get mined or placed. While we're not&nbsp;
storing terrain data, we do have to store&nbsp;&nbsp;

00:10:13.120 --> 00:10:18.800
player-made block changes. For this, I'm&nbsp;
using an array of 6-byte data structures:&nbsp;&nbsp;

00:10:18.800 --> 00:10:23.600
2 bytes for the X coordinate, 1 byte for the&nbsp;
Y coordinate, 2 bytes for the Z coordinate,&nbsp;&nbsp;

00:10:23.600 --> 00:10:29.920
and 1 byte for the block ID. When a player changes&nbsp;
a block, the new block at that location is stored&nbsp;&nbsp;

00:10:29.920 --> 00:10:35.360
in the first available array slot. And, when that&nbsp;
change is "undone", as in, the terrain is returned&nbsp;&nbsp;

00:10:35.360 --> 00:10:41.600
to its original form, the entry gets cleared,&nbsp;
ensuring tight packing of only necessary data.

00:10:41.600 --> 00:10:46.880
Unfortunately, these data types set a build&nbsp;
limit for 32 thousand blocks horizontally, and&nbsp;&nbsp;

00:10:46.880 --> 00:10:53.200
256 blocks vertically. Using these smaller data&nbsp;
types just takes up less memory, so that's where&nbsp;&nbsp;

00:10:53.200 --> 00:10:58.720
the compromise comes from. On my microcontroller,&nbsp;
with this layout, I have enough memory for about&nbsp;&nbsp;

00:10:58.720 --> 00:11:04.640
25 thousand block changes, which is equivalent&nbsp;
to digging out just one and a half chunks. If I&nbsp;&nbsp;

00:11:04.640 --> 00:11:09.600
used larger data types, that would cut in&nbsp;
half the amount of changes you can make.&nbsp;&nbsp;

00:11:09.600 --> 00:11:15.840
This also means that there are only 256 different&nbsp;
block types, but considering that I don't plan on&nbsp;&nbsp;

00:11:15.840 --> 00:11:22.320
implementing Waxed Lightly Weathered Cut Copper&nbsp;
Stairs any time soon, I think this'll suffice.

00:11:22.320 --> 00:11:28.240
Let's take a step back and see how all of this&nbsp;
performs. On my desktop computer with a Ryzen 5&nbsp;&nbsp;

00:11:28.240 --> 00:11:35.360
3600 CPU clocked at 3.6 GHz, each chunk takes&nbsp;
about half a millisecond to generate. On the&nbsp;&nbsp;

00:11:35.360 --> 00:11:41.360
ESP32-C3 microcontroller, which you can easily&nbsp;
pick up on Aliexpress for less than a dollar,&nbsp;&nbsp;

00:11:41.360 --> 00:11:48.160
it takes on average about 200 milliseconds&nbsp;
(that's a fifth of a second) to load one chunk,&nbsp;&nbsp;

00:11:48.160 --> 00:11:51.440
which in most cases is perfectly playable.

00:11:51.440 --> 00:11:57.200
Of course, this is only possible thanks to the&nbsp;
minimalistic algorithm we just discussed, but in&nbsp;&nbsp;

00:11:57.200 --> 00:12:02.560
order to implement it efficiently, you need a very&nbsp;
good low-level understanding of the programming&nbsp;&nbsp;

00:12:02.560 --> 00:12:06.880
language you’re working with. And if that's what’s&nbsp;
keeping you from taking on a cool project, then&nbsp;&nbsp;

00:12:06.880 --> 00:12:12.320
let me introduce you to Coddy.Tech – they’re the&nbsp;
sponsor of this video, but it’s also a genuinely&nbsp;&nbsp;

00:12:12.320 --> 00:12:16.640
great way to learn a programming language.&nbsp;
When I first started learning how to code,&nbsp;&nbsp;

00:12:16.640 --> 00:12:21.440
I would scour the web for JavaScript tutorials&nbsp;
that would keep my attention, but everything&nbsp;&nbsp;

00:12:21.440 --> 00:12:26.960
just seemed so dull. I didn’t have Coddy back&nbsp;
then, but that’s pretty much exactly what I was&nbsp;&nbsp;

00:12:26.960 --> 00:12:31.920
looking for. Think of it as Duolingo, but for&nbsp;
programming. Lessons are interactive, and split&nbsp;&nbsp;

00:12:31.920 --> 00:12:37.440
up into digestible chunks. Each new subject has a&nbsp;
clearly defined and easily achievable goal, with&nbsp;&nbsp;

00:12:37.440 --> 00:12:42.560
practical challenges that get you writing actual&nbsp;
code. It’s the kind of thing that you can pick up&nbsp;&nbsp;

00:12:42.560 --> 00:12:47.440
and put down for 5 minutes every day, just to keep&nbsp;
up your streak, and end up actually learning how&nbsp;&nbsp;

00:12:47.440 --> 00:12:52.720
to code without even noticing. And it's not just&nbsp;
for beginners, either – I’m actually using Coddy&nbsp;&nbsp;

00:12:52.720 --> 00:12:57.760
myself to learn Rust, which arguably might’ve been&nbsp;
a better choice for this project, but I digress.&nbsp;&nbsp;

00:12:57.760 --> 00:13:03.360
If Coddy sounds right for you, use the link in&nbsp;
the description to let them know I sent you.

00:13:03.360 --> 00:13:07.280
Minecraft isn't much fun&nbsp;
without the mining and crafting,&nbsp;&nbsp;

00:13:07.280 --> 00:13:09.840
so here's how I implemented those aspects.

00:13:09.840 --> 00:13:12.640
You might've already noticed&nbsp;
that whenever I mine a block,&nbsp;&nbsp;

00:13:12.640 --> 00:13:18.160
the item for it goes straight into my inventory.&nbsp;
In other words, blocks don't drop anything.&nbsp;&nbsp;

00:13:18.160 --> 00:13:22.720
Simulating those item entities would take&nbsp;
up valuable memory and compute resources.

00:13:22.720 --> 00:13:26.560
I did end up implementing the animation&nbsp;
of an item flying towards you,&nbsp;&nbsp;

00:13:26.560 --> 00:13:30.640
but that's purely visual, and doesn't&nbsp;
account for distance in any way.

00:13:30.640 --> 00:13:37.360
On the topic of items, crafting is implemented&nbsp;
in a rather crude manner. For a modular system,&nbsp;&nbsp;

00:13:37.360 --> 00:13:41.360
it would make sense to define 3-by-3&nbsp;
matrices for each crafting recipe,&nbsp;&nbsp;

00:13:41.360 --> 00:13:46.240
and then use a kernel-fitting algorithm to&nbsp;
match those onto the crafting grid. Again,&nbsp;&nbsp;

00:13:46.240 --> 00:13:51.680
that's the modular solution. But those&nbsp;
matrices take up a sizeable amount of&nbsp;&nbsp;

00:13:51.680 --> 00:13:55.920
memory, and that kernel-fitting&nbsp;
algorithm is relatively complex.

00:13:55.920 --> 00:14:00.560
Instead, I opted to make my code look 10&nbsp;
times more disgusting and unmaintainable,&nbsp;&nbsp;

00:14:00.560 --> 00:14:02.480
just to save a bit of performance.

00:14:02.480 --> 00:14:06.880
In a nutshell, the function first counts&nbsp;
the amount of slots taken up by the recipe,&nbsp;&nbsp;

00:14:06.880 --> 00:14:11.200
making note of the first item it encountered,&nbsp;
and tracking whether the rest of the items are&nbsp;&nbsp;

00:14:11.200 --> 00:14:15.840
identical to the first. Right away, this&nbsp;
lets us implement some simpler recipes really&nbsp;&nbsp;

00:14:15.840 --> 00:14:20.080
cheaply. To craft a furnace, you just need&nbsp;
to check that there are 8 items in the grid,&nbsp;&nbsp;

00:14:20.080 --> 00:14:24.400
that the first one is cobblestone and the rest of&nbsp;
them are identical, and that the central slot is&nbsp;&nbsp;

00:14:24.400 --> 00:14:30.640
empty. After just these 4 operations, you have&nbsp;
confidently eliminated every other recipe. Or,&nbsp;&nbsp;

00:14:30.640 --> 00:14:35.040
swap out just one of those conditions,&nbsp;
and you've got the recipe for a chest.

00:14:35.040 --> 00:14:40.160
For recipes with more complex shapes, you simply&nbsp;
use the index of the first encountered item and&nbsp;&nbsp;

00:14:40.160 --> 00:14:44.800
check for the other items in relation to&nbsp;
it. For example, when crafting shears,&nbsp;&nbsp;

00:14:44.800 --> 00:14:50.480
there'll be 2 items total – the first is an iron&nbsp;
ingot, and the second is also an iron ingot,&nbsp;&nbsp;

00:14:50.480 --> 00:14:55.120
either on the bottom-left or bottom-right&nbsp;
of the first ingot. These conditions will&nbsp;&nbsp;

00:14:55.120 --> 00:14:59.440
then be satisfied regardless of where&nbsp;
the recipe starts in the crafting grid.

00:14:59.440 --> 00:15:03.200
This also lets us re-use&nbsp;
similar recipes. For example,&nbsp;&nbsp;

00:15:03.200 --> 00:15:07.840
instead of having 5 recipes for all of&nbsp;
the pickaxes, we can define one shared&nbsp;&nbsp;

00:15:07.840 --> 00:15:13.680
shape and switch the output item based&nbsp;
on the material in those first 3 slots.

00:15:13.680 --> 00:15:18.560
That's the crafting table, but what about&nbsp;
other containers? The furnace might seem much&nbsp;&nbsp;

00:15:18.560 --> 00:15:24.080
simpler in comparison, given that it's only got 2&nbsp;
inputs and an output. But, in vanilla Minecraft,&nbsp;&nbsp;

00:15:24.080 --> 00:15:29.840
furnaces are among the few containers that&nbsp;
take time to process their inputs. And,&nbsp;&nbsp;

00:15:29.840 --> 00:15:35.120
unlike a crafting table, you can leave your&nbsp;
items in the furnace and come back to it later.

00:15:35.120 --> 00:15:40.720
This requires not only storing item data for each&nbsp;
furnace, but also assigning some kind of timer&nbsp;&nbsp;

00:15:40.720 --> 00:15:46.960
to that furnace and keeping it updated. That's&nbsp;
yet again more memory and compute. Ultimately,&nbsp;&nbsp;

00:15:46.960 --> 00:15:51.600
I felt like replicating the exact&nbsp;
mechanics here wasn't worth it – instead,&nbsp;&nbsp;

00:15:51.600 --> 00:15:55.680
I made the furnace function just like&nbsp;
the crafting table. In goes a recipe,&nbsp;&nbsp;

00:15:55.680 --> 00:15:59.440
and out comes the product.&nbsp;
No waiting, no item storage.

00:15:59.440 --> 00:16:02.160
Initially, I was gonna&nbsp;
leave it at that. After all,&nbsp;&nbsp;

00:16:02.160 --> 00:16:07.440
the crafting table and furnace are what define&nbsp;
a Minecraft base, what more could you need?

00:16:07.440 --> 00:16:10.480
But, after playtesting this&nbsp;
with a few of my viewers,&nbsp;&nbsp;

00:16:10.480 --> 00:16:15.680
the demand for a chest turned out to&nbsp;
be loud. Without a way to share items,&nbsp;&nbsp;

00:16:15.680 --> 00:16:20.880
some people ended up starving despite others&nbsp;
having tons of food. When mining, we'd often&nbsp;&nbsp;

00:16:20.880 --> 00:16:26.080
fill up our inventories to the point where&nbsp;
they started overflowing into the armor slots.

00:16:26.080 --> 00:16:31.040
So, yes, I added the chest. But this now&nbsp;
meant that I was faced with the annoying&nbsp;&nbsp;

00:16:31.040 --> 00:16:36.080
task of finding where to store the item data. If&nbsp;
you're not familiar with embedded programming,&nbsp;&nbsp;

00:16:36.080 --> 00:16:41.200
or just programming in C, it might not be&nbsp;
immediately obvious why this is a problem.&nbsp;&nbsp;

00:16:41.200 --> 00:16:46.560
Long story short, I can't just allocate memory&nbsp;
whenever it's needed. I need to know ahead of&nbsp;&nbsp;

00:16:46.560 --> 00:16:51.360
time how much of it I will need and where.&nbsp;
Otherwise, I risk running into issues like&nbsp;&nbsp;

00:16:51.360 --> 00:16:55.520
heap fragmentation, which will eventually&nbsp;
crash the program if you're not careful.

00:16:55.520 --> 00:17:00.880
Since block changes are stored in one big array,&nbsp;
it seemed like the obvious answer was to store the&nbsp;&nbsp;

00:17:00.880 --> 00:17:08.240
item data in this same array next to the chest as&nbsp;
effectively invisible blocks. This actually works&nbsp;&nbsp;

00:17:08.240 --> 00:17:13.840
out better than you'd expect – here's the data&nbsp;
structure for a block change again. 16 bits X,&nbsp;&nbsp;

00:17:13.840 --> 00:17:21.600
8 bits Y, 16 bits Z, and 8 bits block ID. And,&nbsp;
as it just so happens, the way I store items is&nbsp;&nbsp;

00:17:21.600 --> 00:17:28.640
by using a 16 bit ID number, and an 8 bit number&nbsp;
to encode the stack size. By pure coincidence,&nbsp;&nbsp;

00:17:28.640 --> 00:17:34.000
it works out that each block entry&nbsp;
can perfectly store two item entries.

00:17:34.000 --> 00:17:37.200
This is one of the excuses for why&nbsp;
I don't use a hashmap for the block&nbsp;&nbsp;

00:17:37.200 --> 00:17:42.240
changes – not only does it save on memory, but&nbsp;
it allows me to do silly hacks like this one.

00:17:42.240 --> 00:17:48.560
And finally, to add some more dynamics to the&nbsp;
game, I implemented health, mobs, and combat.

00:17:48.560 --> 00:17:53.040
Let's start with health and hunger. Fall&nbsp;
damage was by far the simplest type of&nbsp;&nbsp;

00:17:53.040 --> 00:17:57.280
damage to implement – you just keep track of the&nbsp;
Y level at which the player last left the ground,&nbsp;&nbsp;

00:17:57.280 --> 00:18:00.720
and subtract from it their Y coordinate&nbsp;
once they touch the ground again.

00:18:00.720 --> 00:18:05.280
Healing on the other hand wouldn't be so&nbsp;
straightforward. The hunger mechanics in&nbsp;&nbsp;

00:18:05.280 --> 00:18:09.840
modern Minecraft aren't that simple – you have&nbsp;
to keep track of health, hunger, saturation,&nbsp;&nbsp;

00:18:09.840 --> 00:18:13.680
and update all of those variables over&nbsp;
time based on what the player is doing.

00:18:13.680 --> 00:18:17.280
For a moment, I considered taking&nbsp;
inspiration from the old alpha days,&nbsp;&nbsp;

00:18:17.280 --> 00:18:22.160
where simply right-clicking with food in your&nbsp;
hand would directly replenish your health. But,&nbsp;&nbsp;

00:18:22.160 --> 00:18:28.160
luckily, I found a workaround that let me&nbsp;
implement the hunger system completely for free.

00:18:28.160 --> 00:18:32.400
When you're moving, your game client sends&nbsp;
movement packets to the server at a rate&nbsp;&nbsp;

00:18:32.400 --> 00:18:39.440
of about 20 per second. And, crucially, when&nbsp;
you're not moving, you send just one. Since my&nbsp;&nbsp;

00:18:39.440 --> 00:18:44.000
server has to process these packets one&nbsp;
way or another, it's effectively a free&nbsp;&nbsp;

00:18:44.000 --> 00:18:49.040
indicator of how much the player is moving.&nbsp;
I added a counter that decrements each time&nbsp;&nbsp;

00:18:49.040 --> 00:18:53.760
one of those packets is received. When it&nbsp;
hits 0, your hunger bar is reduced by 1,&nbsp;&nbsp;

00:18:53.760 --> 00:18:59.920
and the counter resets. Increasing saturation is&nbsp;
then just a matter of incrementing this counter.

00:18:59.920 --> 00:19:05.920
Of course, for any of this to actually matter,&nbsp;
I had to add mobs – hostiles to act as a threat,&nbsp;&nbsp;

00:19:05.920 --> 00:19:09.120
and passives to act as a food source. Passive mobs&nbsp;&nbsp;

00:19:09.120 --> 00:19:13.760
simply wander around aimlessly in one of&nbsp;
8 directions. To save memory and compute,&nbsp;&nbsp;

00:19:13.760 --> 00:19:18.560
I made the decision to store only integer&nbsp;
positions, which is why it looks so choppy.

00:19:18.560 --> 00:19:23.840
And hostile mobs have an equally simple algorithm&nbsp;
– they just move in the direction of the nearest&nbsp;&nbsp;

00:19:23.840 --> 00:19:28.960
player. No pathfinding, no obstacle avoidance,&nbsp;
just walking straight towards the target.&nbsp;&nbsp;

00:19:28.960 --> 00:19:32.800
The one major difference is that once&nbsp;
a zombie gets within 2 blocks of you,&nbsp;&nbsp;

00:19:32.800 --> 00:19:38.560
it starts dealing 3 hearts of damage per second.&nbsp;
Now, you'll notice that's way more than vanilla,&nbsp;&nbsp;

00:19:38.560 --> 00:19:42.400
but that's to compensate for the&nbsp;
otherwise dumb and predictable movement.

00:19:42.400 --> 00:19:46.640
This damage can, of course, be decreased&nbsp;
by wearing armor. To reduce complexity,&nbsp;&nbsp;

00:19:46.640 --> 00:19:51.360
I'm using the old damage reduction formula,&nbsp;
before the combat update. This means that a&nbsp;&nbsp;

00:19:51.360 --> 00:19:57.040
full set of diamond armor absorbs nearly all&nbsp;
of the impact, but I think that's fair, given&nbsp;&nbsp;

00:19:57.040 --> 00:20:02.800
that on a microcontroller things can get pretty&nbsp;
laggy, even after all of these optimizations.

00:20:02.800 --> 00:20:07.760
These are sort of the main 3 challenges that&nbsp;
I kept refining throughout development. But,&nbsp;&nbsp;

00:20:07.760 --> 00:20:11.280
of course, there are several more&nbsp;
places where corners have been cut,&nbsp;&nbsp;

00:20:11.280 --> 00:20:16.240
and several implementation details that would&nbsp;
require separate videos to properly discuss.

00:20:16.240 --> 00:20:20.080
For example, there are no random ticks.&nbsp;
Saplings won't grow on their own,&nbsp;&nbsp;

00:20:20.080 --> 00:20:24.720
so you have to use bone meal from composters.&nbsp;
Mobs won't appear randomly either. Instead,&nbsp;&nbsp;

00:20:24.720 --> 00:20:29.520
there's a chance for one to spawn each time you&nbsp;
cross a chunk boundary. Liquids flow instantly,&nbsp;&nbsp;

00:20:29.520 --> 00:20:34.720
to avoid having to queue block updates in&nbsp;
memory. Torches are sent after all other blocks,&nbsp;&nbsp;

00:20:34.720 --> 00:20:39.200
forcing the client to caclulate light data&nbsp;
without having to do it on the server.

00:20:39.200 --> 00:20:42.960
I could go on and on, but I think&nbsp;
you get the point – this was a much&nbsp;&nbsp;

00:20:42.960 --> 00:20:46.400
bigger project than I can describe in one video.

00:20:46.400 --> 00:20:50.080
Naturally, now that everything was&nbsp;
implemented, the final step was to&nbsp;&nbsp;

00:20:50.080 --> 00:20:56.640
test this out with real players on a real&nbsp;
microcontroller. And, to my surprise...

00:20:56.640 --> 00:21:02.240
It wasn't a total failure. I mean, sure, with&nbsp;
more than 3 players online, things do start to&nbsp;&nbsp;

00:21:02.240 --> 00:21:07.440
lag quite a bit, but I'd say it's comparable&nbsp;
to playing 2b2t during peak hours. Now,&nbsp;&nbsp;

00:21:07.440 --> 00:21:10.800
there are a few things to consider when&nbsp;
playing with real people – first of all,&nbsp;&nbsp;

00:21:10.800 --> 00:21:14.960
the server trusts the client fully. If&nbsp;
your client tells the server that it's&nbsp;&nbsp;

00:21:14.960 --> 00:21:19.680
now got a stack of diamonds in its inventory, the&nbsp;
server won't even bother to check, because that&nbsp;&nbsp;

00:21:19.680 --> 00:21:24.560
would require additional computation and memory.&nbsp;
Similarly, if your client said that it just mined&nbsp;&nbsp;

00:21:24.560 --> 00:21:30.000
an entire chunk of blocks in less than a second,&nbsp;
the server would happily accept that as fact. What&nbsp;&nbsp;

00:21:30.000 --> 00:21:35.360
I'm saying is that this really isn't a good idea&nbsp;
if you can't trust the people that you play with.

00:21:35.360 --> 00:21:39.600
Secondly, there's no network compression&nbsp;
whatsoever. In a vanilla server,&nbsp;&nbsp;

00:21:39.600 --> 00:21:44.560
when an outgoing packet exceeds a certain size&nbsp;
threshold, its contents are compressed using&nbsp;&nbsp;

00:21:44.560 --> 00:21:50.720
the zlib compression algorithm. Needless to say,&nbsp;
compression is very computationally expensive,&nbsp;&nbsp;

00:21:50.720 --> 00:21:55.600
so it's out of the question. This makes it&nbsp;
near impossible to host the server on a slow&nbsp;&nbsp;

00:21:55.600 --> 00:21:59.760
internet connection, and makes the game&nbsp;
unplayable to players with bad internet.&nbsp;&nbsp;

00:21:59.760 --> 00:22:03.920
This also means that, although on modern&nbsp;
computers the server can easily generate&nbsp;&nbsp;

00:22:03.920 --> 00:22:09.120
hundreds of chunks per second, sending&nbsp;
those chunks quickly becomes unrealistic.

00:22:09.120 --> 00:22:14.160
With all that said, you might be wondering&nbsp;
– what's the point of all this? Why did I&nbsp;&nbsp;

00:22:14.160 --> 00:22:18.480
spend a month of my life developing&nbsp;
an embedded Minecraft server? Well,&nbsp;&nbsp;

00:22:18.480 --> 00:22:23.040
honestly, I just really like the idea&nbsp;
that this tiny little chip that costs&nbsp;&nbsp;

00:22:23.040 --> 00:22:28.640
one friggin' dollar and consumes half a Watt&nbsp;
can run something as advanced as Minecraft.

00:22:28.640 --> 00:22:34.880
As a great man once said – "Science&nbsp;
isn't about 'why', it's about 'why not.'"

00:22:34.880 --> 00:22:38.960
If you want to try this for yourself, either&nbsp;
on a computer or an actual microcontroller,&nbsp;&nbsp;

00:22:38.960 --> 00:22:43.280
I'll leave a link to the GitHub repository in&nbsp;
the description. There's no payment or anything,&nbsp;&nbsp;

00:22:43.280 --> 00:22:45.520
though I would appreciate if you subscribed. A big&nbsp;&nbsp;

00:22:45.520 --> 00:22:50.480
thanks to my channel members for their&nbsp;
patience, and thank you for watching.

