Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local math_min, math_max, table_insert, table_remove = math.min, math.max, table.insert, table.remove
- local maximum = {
- sharpness = 5,
- sweepingEdge = 3,
- unbreaking = 3,
- looting = 3,
- fireAspect = 2,
- knockback = 2,
- mending = 1
- }
- local multiplier = {
- sweepingEdge = 2,
- looting = 2,
- fireAspect = 2,
- mending = 2,
- sharpness = 1,
- unbreaking = 1,
- knockback = 1
- }
- local function copy(item)
- local item2 = {
- enchantments = {},
- priorWorkings = item.priorWorkings,
- type = item.type
- }
- for enchantment, level in pairs(item.enchantments) do
- item2.enchantments[enchantment] = level
- end
- return item2
- end
- local function combine(target, sacrifice)
- local target_priorWorkings = target.priorWorkings
- local cost =
- 2 ^ target_priorWorkings - 1 +
- 2 ^ sacrifice.priorWorkings - 1
- target.priorWorkings = target_priorWorkings + 1
- for enchantment, level in pairs(sacrifice.enchantments) do
- local l1, l2, max = target.enchantments[enchantment] or 0, level, maximum[enchantment]
- target.enchantments[enchantment] = math_min(l1 == l2 and l1 + 1 or math_max(l1, l2), max)
- cost = cost + target.enchantments[enchantment] * multiplier[enchantment]
- end
- return target, cost
- end
- local lowestSpentLevels = math.huge
- local finalStates
- local function step(state)
- for i = 1, #state do
- for j = 1, #state do
- if i ~= j then
- local stateCopy = {spentLevels = state.spentLevels, previous = state}
- for i, item in ipairs(state) do
- stateCopy[i] = copy(item)
- end
- local target, sacrifice
- if i < j then
- sacrifice = table_remove(stateCopy, j)
- target = table_remove(stateCopy, i)
- else
- target = table_remove(stateCopy, i)
- sacrifice = table_remove(stateCopy, j)
- end
- if sacrifice.type ~= "sword" then
- target, cost = combine(target, sacrifice)
- local stateCopy_spentLevels = stateCopy.spentLevels + cost
- stateCopy.spentLevels = stateCopy_spentLevels
- table_insert(stateCopy, target)
- if cost < 40 then
- if #stateCopy == 1 then
- if stateCopy_spentLevels == lowestSpentLevels then
- table_insert(finalStates, stateCopy)
- elseif stateCopy_spentLevels < lowestSpentLevels then
- lowestSpentLevels = stateCopy_spentLevels
- finalStates = {stateCopy}
- print(lowestSpentLevels)
- end
- else
- step(stateCopy)
- end
- -- else "Too expensive!"
- end
- end
- end
- end
- end
- end
- local function book(enchantment, level)
- return {type = "book", enchantments = {[enchantment] = level}, priorWorkings = 0}
- end
- step({
- spentLevels = 0,
- {type = "sword", enchantments = {}, priorWorkings = 0},
- book("sharpness", 4),
- book("sharpness", 4),
- book("looting", 3),
- book("unbreaking", 3),
- book("sweepingEdge", 3),
- book("fireAspect", 2),
- book("knockback", 2),
- book("mending", 1)
- })
- local function traverse(t, n)
- for k, v in pairs(t) do
- if type(v) == "table" then
- print(string.rep("\t", n) .. k .. ":")
- traverse(v, n + 1)
- else
- print(string.rep("\t", n) .. k .. " = " .. v)
- end
- end
- end
- traverse(finalStates, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement