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
01-21-61-8
11-31-72-9
21-32-84-11
31-42-96-12
41-43-108-14
51-53-1110-15
61-53-1212-17
71-63-1314-18
81-64-1416-20
91-74-1518-21
102-75-1620-23
112-85-1722-24
122-85-1824-26
132-95-1926-27
142-96-2028-29
152-106-2130

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.
. . . . . . . . . . . . . . . . . . . . . . . .



1 comment:

ActionScripter9109 said...

Highly informative. Thanks for doing the busy work and sharing your findings. The new enchanting system is now completely demystified for me.