Advertisement
wolfboyft

Outdated/wrong enchantment brute forcer

Dec 9th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local math_min, math_max, table_insert, table_remove = math.min, math.max, table.insert, table.remove
  2.  
  3. local maximum = {
  4.     sharpness = 5,
  5.     sweepingEdge = 3,
  6.     unbreaking = 3,
  7.     looting = 3,
  8.     fireAspect = 2,
  9.     knockback = 2,
  10.     mending = 1
  11. }
  12.  
  13. local multiplier = {
  14.     sweepingEdge = 2,
  15.     looting = 2,
  16.     fireAspect = 2,
  17.     mending = 2,
  18.     sharpness = 1,
  19.     unbreaking = 1,
  20.     knockback = 1
  21. }
  22.  
  23. local function copy(item)
  24.     local item2 = {
  25.         enchantments = {},
  26.         priorWorkings = item.priorWorkings,
  27.         type = item.type
  28.     }
  29.     for enchantment, level in pairs(item.enchantments) do
  30.         item2.enchantments[enchantment] = level
  31.     end
  32.     return item2
  33. end
  34.  
  35. local function combine(target, sacrifice)
  36.     local target_priorWorkings = target.priorWorkings
  37.     local cost =
  38.         2 ^ target_priorWorkings - 1 +
  39.         2 ^ sacrifice.priorWorkings - 1
  40.     target.priorWorkings = target_priorWorkings + 1
  41.     for enchantment, level in pairs(sacrifice.enchantments) do
  42.         local l1, l2, max = target.enchantments[enchantment] or 0, level, maximum[enchantment]
  43.         target.enchantments[enchantment] = math_min(l1 == l2 and l1 + 1 or math_max(l1, l2), max)
  44.         cost = cost + target.enchantments[enchantment] * multiplier[enchantment]
  45.     end
  46.    
  47.     return target, cost
  48. end
  49.  
  50. local lowestSpentLevels = math.huge
  51. local finalStates
  52.  
  53. local function step(state)
  54.     for i = 1, #state do
  55.         for j = 1, #state do
  56.             if i ~= j then
  57.                 local stateCopy = {spentLevels = state.spentLevels, previous = state}
  58.                 for i, item in ipairs(state) do
  59.                     stateCopy[i] = copy(item)
  60.                 end
  61.                
  62.                 local target, sacrifice
  63.                 if i < j then
  64.                     sacrifice = table_remove(stateCopy, j)
  65.                     target = table_remove(stateCopy, i)
  66.                 else
  67.                     target = table_remove(stateCopy, i)
  68.                     sacrifice = table_remove(stateCopy, j)
  69.                 end
  70.                
  71.                 if sacrifice.type ~= "sword" then
  72.                     target, cost = combine(target, sacrifice)
  73.                     local stateCopy_spentLevels = stateCopy.spentLevels + cost
  74.                     stateCopy.spentLevels = stateCopy_spentLevels
  75.                     table_insert(stateCopy, target)
  76.                    
  77.                     if cost < 40 then
  78.                         if #stateCopy == 1 then
  79.                             if stateCopy_spentLevels == lowestSpentLevels then
  80.                                 table_insert(finalStates, stateCopy)
  81.                             elseif stateCopy_spentLevels < lowestSpentLevels then
  82.                                 lowestSpentLevels = stateCopy_spentLevels
  83.                                 finalStates = {stateCopy}
  84.                                 print(lowestSpentLevels)
  85.                             end
  86.                         else
  87.                             step(stateCopy)
  88.                         end
  89.                     -- else "Too expensive!"
  90.                     end
  91.                 end
  92.             end
  93.         end
  94.     end
  95. end
  96.  
  97. local function book(enchantment, level)
  98.     return {type = "book", enchantments = {[enchantment] = level}, priorWorkings = 0}
  99. end
  100.  
  101. step({
  102.     spentLevels = 0,
  103.    
  104.     {type = "sword", enchantments = {}, priorWorkings = 0},
  105.     book("sharpness", 4),
  106.     book("sharpness", 4),
  107.     book("looting", 3),
  108.     book("unbreaking", 3),
  109.     book("sweepingEdge", 3),
  110.     book("fireAspect", 2),
  111.     book("knockback", 2),
  112.     book("mending", 1)
  113. })
  114.  
  115. local function traverse(t, n)
  116.     for k, v in pairs(t) do
  117.         if type(v) == "table" then
  118.             print(string.rep("\t", n) .. k .. ":")
  119.             traverse(v, n + 1)
  120.         else
  121.             print(string.rep("\t", n) .. k .. " = " .. v)
  122.         end
  123.     end
  124. end
  125. traverse(finalStates, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement