Enchanting

**Written by Tycho Young**

The enchanting system is a complex that inputs a multitude of factors, calculates some internal values, and spits out a randomized HashMap of enchantments.

## Prerequisites

### Parameters

There are a many factors that affect the player’s ability to enchant an item, but there are 5 primary factors.

***Enchanting Skill*** - This is the AureliumSkills skill that ranges from 1→50. It increases enchantment strength.

**Enchantment Weight** - Enchantment weight is the core of the enchanting system - weight values are assigned based on the enchantment’s rarity relative to other enchantments, ranging from 0→100.

***Fuel -*** Fuel is the most complex aspect of the enchanting system, while simple at its core, fuel will be able to provide special effects based on what type of fuel is used. As for the core of Enchanting Fuel, it is still rather basic. Upon enchanting, a single fuel will be consumed - the fuel value will affect enchantment strength.

*Bookshelf Count* - Bookshelf count is one of the system’s more trivial factors. Bookshelf count, ranging from 0→50, will directly reflect on the enchantment variety a user can obtain.

***Enchant Level -*** An enchantment level the player selects that multiplies the strength value

### Variables

***Strength*** - Enchantment strength is a variable that affects the level of the enchantments received. Strength ranges from 0 to 1, with 1 giving the maximum level of enchantments at maximum chance.

***Variety*** - Variety controls how many different kinds of enchants you get upon enchanting an item. Varying between 1 and 2, granting up to 2 different enchantments per enchantment attempt at high enchanting levels.

## Enchanting

### Checks

Finally- we are arriving at the actual process of enchanting. When the player selects an enchanting level, given a fuel and item, we quickly check the validity of a few things.

* The Item’s Type
* The Fuel Value
* The Player’s Enchanting Skill
* The Player’s XP Level

If any of these requirements aren’t met, then we cannot proceed with the enchanting. The requirements for the player’s XP level and enchanting skill will depend on the level of enchanting the player selected.

### Enchanting Level

Let’s quickly discuss enchanting level- enchanting level is one of three levels selected by the player, producing different XP and Skill costs and requirements.

Level 1 → 30 XP Levels - 1 Fuel - Enchanting Skill 0 → 0.4x Strength

Level 2 → 40 XP Levels - 2 Fuel - Enchanting Skill 20 → 0.7x Strength

Level 3 → 50 XP Levels - 3 Fuel - Enchanting Skill 35 → 1x Strength

As you can see, these are analogous to Minecraft’s enchanting options, but they cost the full amount of levels, and require Enchanting Skill. These directly influence enchanting strength, emulating Minecraft’s options further.

Let’s deduct the proper amount of XP Levels and fuel, then enchant.

### Random Chances

Time to actually enchant the item! But wait - how do we know which enchantments to apply- and what if some enchantments should be rarer than others?

The solution to this is **Enchantment Weight.** Each enchantment is assigned a “weight” value, and this is weighted across all other enchantments valid for that type.

**Note that these weights are not the actual weights, and are just example values.**

All of the valid enchantments add up to 100%! Look at that! The method to assign a random enchant based on weight will be fairly simple. We will add up all the weights, and assign each enchant to an index in which it it is chosen. A random integer will be generated between 0 and the total weight, and the method will loop through the list to see which sector it landed in- this way, there is a perfectly weighted chance for every single enchantment, regardless of order.

For example, if we generate 153, then we get fire aspect!

Next, we need to choose a level for this enchantment- this is where enchantment strength plays in. In order to get the enchantment level, we use this formula:

This depends the enchanting level directly on strength, and forces extremely low weight enchantments to have an extremely low chance of getting a max level.

### Enchantment Variety

At this point, you may be wondering what we will be doing with the variety variable. Every time we finish the enchanting process, we decrement the variety variable by 1. If there is still a decimal left, then we will roll a  to decide whether to run the enchant loop again. We remove the enchantment we applied from the possible list of enchants to apply, and append it to the HashMap that contains the enchants to apply to the item.

When Variety has become less than or equal to 0, then we stop the enchant loop. We should be left with a HashMap<ComplexEnchantment, Integer> that contains the enchantments we need to apply to the item. We loop through the item’s current enchants, and procedurally combine them with a utility method.

After that, we should be done. We update the item’s enchants, and apply the proper amount of enchanting XP. And if we didn’t have enough formulas already - here’s the formula for the amount of XP to give to the player.

And that’s it! All of EvoCraft’s over-the-top enchanting system, in one, nice, compiled page for your pleasure.