Monday, July 30, 2012

On Minecraft Enchantment Levels and Bookshelves Part III: The Changes in 1.3

The much anticipated 1.3 update for Minecraft is coming out soon, and along with a whole host of new additions to the game, it also brings an overhaul to the enchanting system. This, of course, completely changes the optimum enchanting level-bookshelf relations that I derived for the previous enchanting system, just like the 1.1 update broke the one before that.

I've been digging in the source for the 12w26a snapshot in order to find out what the changes are. To my knowledge, the calculations have not changed since the rebalancing introduced in the 12w22a snapshot released at the end of May. Comparing these to the snippets of source code I had saved from 1.1, I have worked out how the new enchanting levels are calculated.

But, before I get into that, let me once again reiterate why you'd want to understand the enchanting level-bookshelf relation. There are simulators which can calculate the probability of getting a certain enchantment at a certain level (there is an excellent calculator here). This means that if you are looking for a specific enchantment or combination of enchantments, there is an optimum level that would maximise you chances of getting the combination of enchantments that you want. Of course, you can keep rolling new levels to find the level you want, but it is quicker to set the number of bookshelves so that the level you want is most likely to show up, especially if you tend to enchant at the same level every time.

Lets start by looking at the relevant changes as stated in the version history on the wiki:
• "The maximum enchantment level has been lowered from 50 to 30. As a result enchantment tables now require only 15 bookcases around them to allow maximum level enchantments."
• "With a full enchantment table set with bookshelves, at the bottom of the 3 enchantments, it always shows you the highest number possible"

First, before we get into source code, let's try some in-game testing. The first point is definitely true. Level 30 is indeed the maximum level, and there is no difference in the distribution of levels with more than 15 bookshelves. Now testing the second point. With no bookshelves, it seems we are offered random levels in the bottom slot between 1 and 8. The first obvious difference is that maximum with no bookshelves has been upped from 5. Also it seems you are not always shown the highest number possible. In fact, with 5 bookshelves, you are offered levels between 10 and 15 in the bottom slot, and although 10 bookshelves usually offers level 20, it occasionally offers as high as level 23 in the bottom slot (as in the screenshot below). With 15 bookshelves, however, you always seem to be offered level 30 in the bottom slot. The second point, it seems, is indeed only true for a "full enchantment table set".

Figure 1: Screenshot showing enchantment level 23 being offered with 10 bookshelves (which happens about 1.13% of the time)

So, what's actually happening in the source code? There are actually only 4 small changes in functionality between 1.1 and 12w26a. The relevant snippets of code (edited slightly to make them more readable) are:

- In version 1.1:
if (nBookShelves > 30) { nBookShelves = 30; }
int level = random.nextInt(5) + 1 + (nBookShelves / 2) +
random.nextInt(nBookShelves + 1);
if (slotNum == 0) { return (level/2) + 1; }
if (slotNum == 1) { return (level*2)/3 + 1; }
else { return level; }

- and in snapshotn 12w26a:
if (nBookShelves > 15) { nBookShelves = 15; }
int level = random.nextInt(8) + 1 + (nBookShelves /2) +
random.nextInt(nBookShelves + 1);
if (slotNum == 0) { return max(level/3, 1); }
if (slotNum == 1) { return (level*2)/3 + 1; }
else { return max(level, nBookShelves*2); }

The changes are:
1. The number of bookshelves is capped at 15, rather than 30.
2. The random modifier for the level is between 1 and 8, rather than 1 and 5.
3. The top slot shows one third of the calculated level, rather than one half.
4. The level offered in the bottom slot now has a lower bound equal to twice the number of bookshelves.

It is a fairly simple matter to use this to validate the upper and lower bounds for the enchantment levels that we find in-game. The table below shows what these upper and lower bounds are for each number of bookshelves.

 Shelves Top Middle Bottom 0 1-2 1-6 1-8 1 1-3 1-7 2-9 2 1-3 2-8 4-11 3 1-4 2-9 6-12 4 1-4 3-10 8-14 5 1-5 3-11 10-15 6 1-5 3-12 12-17 7 1-6 3-13 14-18 8 1-6 4-14 16-20 9 1-7 4-15 18-21 10 2-7 5-16 20-23 11 2-8 5-17 22-24 12 2-8 5-18 24-26 13 2-9 5-19 26-27 14 2-9 6-20 28-29 15 2-10 6-21 30

For the previous system, I plotted the distributions of levels offered in the bottom slot, but the bottom slot is now heavily skewed toward offering a level equal to twice the number of bookshelves. For instance, with 14 bookshelves, you will be offered a level 28 enchantment 99.2% of the time, with a level 29 enchantment only showing up an average of once every 120 tries. This makes odd levelled enchantments exceptionally difficult to obtain (although none is rarer than the level 50 enchantment was in the 1.1. enchanting system). It also makes a distribution graph look pretty stupid. Even so, I plotted it. Don't ask me why.

Figure 2: Distribution of enchantment levels appearing in the bottom slot

The optimum number of bookshelves to use to obtain a specific enchantment level is easy to calculate and is shown in the table below, together with the probability that that enchantment will be offered at that level.

 Level Shelves Probability 1 0 71.3% 2 1 55.6% 3 2 40.6% 4 2 34.4% 5 2 34.4% 6 3 50.8% 7 3 31.6% 8 4 55.0% 9 4 23.4% 10 5 70.1% 11 10 18.2% 12 6 73.7% 13 12 15.4% 14 7 84.4% 15 14 13.3% 16 8 86.1% 17 15 10.2% 18 9 92.5% 19 15 5.5% 20 10 93.2% 21 10 3.4% 22 11 96.9% 23 11 2.1% 24 12 97.1% 25 12 1.9% 26 13 99.1% 27 13 0.9% 28 14 99.2% 29 14 0.8% 30 15 100.0%

Due to the rarity of high odd numbered levels, it is probably quicker for players to restrict themselves to the even number that gives the best probability for the combination of enchantments they want, rather than use an odd number with a slightly higher probability. As a rule of thumb, for even numbers, take half of the level you want, and use that many bookshelves. It's much easier than it was before.

[Source: minecraft.jar for versions 1.1 and snapshot 12w26a, decompiled with MCP 5.6 and MCP 6.15 respectively.]

If you enjoyed this post, then don't forget to like, tweet, +1, or upvote on reddit. If you have any questions, comments or complaints, post them using the form below.
. . . . . . . . . . . . . . . . . . . . . . . .

Thursday, July 19, 2012

On Seeing the World in Polarised Light

Polarised light has always fascinated me, but being as stingy as I am, I have always only bought myself cheap sunglasses, so until recently, I have never actually owned a pair of Polaroid sunglasses. Earlier this year, after my previous pair of sunglasses broke in the mid summer sun, I decided to fork out a bit extra and get myself a slightly higher quality pair of sunglasses (although they were about the cheapest in the store when I bought them). I keep noticing things that are absolutely amazing, and I wonder to myself whether people who don't know much about the physics can really appreciate the world around them.

I'm not going to go into the physics here but I will leave links to Wikipedia's basic explanation and more advanced explanation here.

My first encounter with polarised light was when I was about 8 or 9 years old, and a friend at school showed me that if you turned the filter on the calculator screen over, you could invert the colours of the numbers. I played around with a couple of filters (I wasn't ever afraid of digging broken calculators out of bins - which are more common than you'd think) and discovered that holding them parallel to each other did nothing, but if you turned one of them perpendicular, the intersecting area would darken. I also found that if you used two filters on the calculator display, and the back filter was a little bent or trampled (because you can't be worried about quality when you're digging out of bins), there would be rainbow colours across the screen. I had a nice unique calculator display with a black background and rainbows through the numbers. I was a little disappointed when I got to high school and found that the fancier calculators had no removable filter.

Over the years, I have collected a number of filters from various cheap digital toys, 3D glasses (about the only thing they're good for, since I have very poor stereoscopic depth perception), and the like. I keep them in a little transparent plastic box from which I once freed a set of playing cards. I liked the way that the plastic box had little rainbow patterns when viewed between two filters. In physics during my first year at university, I finally learned a bit about how residual stresses in plastics cause them to absorb and admit light of different wavelengths in differing amounts (an effect known as dichroism). That was interesting in itself, but I also learned that sellotape is dichroic. By placing sellotape in layers over a transparent surface, you can create complex patterns of shapes and colours that can only be seen when placed between two polarising filters.

That's all very nice, but it did not prepare me for the experience of seeing the entire world through polarised light. Everything is different. Before I had Polariod sunglasses, I did not really appreciate the way that almost all reflected light is polarised to a degree. In fact, unless the sun is behind you and the surface is reflecting straight at you, the glare from it will be at least partially polarised. That applies to almost every surface. I don't know if I can convey the absolute magnitude of that. Almost every surface.

I don't think I've noticed before how shiny absolutely every surface. Nothing is truly matte, it seems. The interesting part is that normally while wearing polarising sunglasses, the shine off any horizontal surface is greatly reduced (of course, that's the whole point in polarising sunglasses). However, if you tilt your head at 90°, the shine of all vertical surfaces is reduced, and you still get the glare of horizontal surfaces.

I can't remember exactly when I got these sunglasses, but it was around four or five months ago. I still can't get over it. While driving, I continuously tilt my head and watch how the reflections change on the surface of the dashboard, other cars, paving, windows, lakes, and pretty much everything around me. It's also interesting that the displays in my car for the clock, radio, petrol meter and the like all polarise light at 45°. This means that they are dimmed slightly when I'm wearing my sunglasses, unless I tilt my head to the right. If I tilt my head to the left (which is the likely case when I'm leaning over to change the station or volume), the display turns black, and becomes unreadable. I wonder if there's a specific reason for the polarisation angle being the way it is. Anyway, I assume people who see me driving a long just think I'm swaying my head to music or something.

While walking through a nursery over the weekend, I was tilting my head from side to side, looking at the reflections off the plant leaves, when my girlfriend bluntly pointed out that I looked like a retard. I know this, but still. It's just so fascinating.

If you enjoyed this post, then don't forget to like, tweet, +1, or upvote on reddit. If you have any questions, comments or complaints, post them using the form below.
. . . . . . . . . . . . . . . . . . . . . . . .

Tuesday, July 10, 2012

On Everything. It is Just So Damn Amazing.

It's hard not to live life in complete and utter amazement. The shear fact that I am alive in such an incredibly improbable world is unbelievably overwhelming. This is the sort of thing that can only be explained with incredibly long sentences, so I suggest that you brace yourself. Even more improbable is the fact that I happen to be a human, capable not only of thinking these thoughts, glimpsing the tiny clues as to where our universe came from, but sharing them with others who are equally (and in many cases much more) capable of appreciating and expanding them. I could easily have been born a bacteria, running off nothing more than simple chemical impulses, with no concious awareness of my surroundings, no room to make decisions and manipulate my environment to suit me.

Of course, my concious awareness and ability to manipulate my environment may just be due to chemical impulses as well, but is independent chemical impulses separately directing trillions of cells to interact for mutual benefit, driving such a vastly complex mechanism that the illusions of awareness, conciousness and free will inexplicably emerge from the chaotic interaction at a lower level. And when it comes right down to it, all those chemical interactions are just unimaginably small particles floating through emptiness, pushing each other around according to a simple set of quantitative rules.

And we can look at the universe, and realise that things can be counted, and that everything we see follows simple quantitative rules. If you have two apples, and find another apple, then you have three apples. It is unfathomably simple, and never fails. And everything we see can be counted like this. And when we break it down into smaller parts, there are different rules, but they can still be quantified with the same mathematics. We need more advanced tools to deal with the scale of the mathematics, but on a fundamental level, they are all just extensions of the same counting techniques we use to keep track of how many pieces of fruit we have. We can keep going, on and on, and as we get deeper and deeper, our set of rules keeps on going, and all of it can be quantified by logical extensions of this simple system that we came up with to make sure our neighbour wasn't stealing our cattle. And its not like these applying these rules gives just an estimate of our universe. We demand that they work every single time. If they do, we mark them as just maybe. And if they don't, even if it's just once, we mark them as definitely wrong, but even so, we have rules that can quantify exactly how wrong they are, and those same rules can tell us how much confidence we can have in our "maybes" as well.

And in the end, we're left with this massively complex set of simple, elegant, quantifiable rules. But there are so many rules that no one really knows them all, and no one is even capable of understanding a fraction of them, never mind applying them. But in just a few hundred years, the millions of human minds who have considered them have collectively put together a sufficiently powerful rule set that we can now consider the possibility that our entire universe is just a momentary interaction caused by a chance collision of two multi-dimensional curtains flapping in an an extra-universal emptiness. Not only consider, but actually have at least a superficial understanding of the quantifiable rules that are theoretically capable of explaining and predicting absolutely everything that will possibly happen in our universe, in a quantifiable manner.

Of course, while the rules themselves are so ridiculously simple, the shear number of ways in which they can interact makes them so incredibly complex. The overwhelming majority of them are just special cases of interactions between even simpler rules, which themselves arise out of an even simpler set. And at the bottom, there is a tiny set of ridiculously simple, self-containing rules, whose interactions are so complex that the only way of solving them is to watch an entire meta-universe play out.

And then within that meta-universe, out of who-knows-how-many possible universes, there is this one. And in it, out of who-knows-how-many galactic clusters, we have this one, and in it, this galaxy. And out of the hundreds of billions of stars, we have our sun, and it's eight little major planets, as many as a couple hundred dwarf planets, and several hundred thousand minor planets orbiting around it. And out of all those, our planet happens to exist. And over the last four and a half billion years, has warped and cooled, to a relatively stable, water-covered ball. And the water has been kept at one of an endless number of combinations of temperatures and pressures that allow it to stay liquid, but repeatedly heated and cooled just enough to cause the convection currents required to mix chemicals formed in long dead stars in just the right ways that they could bond together and make crude but functional copies of themselves using the fragments of similar chemicals floating around them. Our simple set of rules allow this, but it is so endlessly unlikely that it possibly took half a billion years of the right conditions for it to happen. We don't know how - we weren't there to see, and know one has yet had the half a billion years spare time it would take to run an experiment to see it.