Advertisement
wolfboyft

`time love . > out` w/ this 4 main.lua. will see file "out"

Dec 10th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 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 sacrifice_priorWorkings = sacrifice.priorWorkings
  38.     local cost =
  39.         2 ^ target_priorWorkings - 1 +
  40.         2 ^ sacrifice_priorWorkings - 1
  41.     target.priorWorkings = math_max(sacrifice_priorWorkings, target_priorWorkings) + 1
  42.     for enchantment, level in pairs(sacrifice.enchantments) do
  43.         local l1, l2, max = target.enchantments[enchantment] or 0, level, maximum[enchantment]
  44.         target.enchantments[enchantment] = math_min(l1 == l2 and l1 + 1 or math_max(l1, l2), max)
  45.         cost = cost + target.enchantments[enchantment] * multiplier[enchantment]
  46.     end
  47.    
  48.     return target, cost
  49. end
  50.  
  51. local lowestSpentLevels = math.huge
  52. local finalStates
  53.  
  54. local function step(state)
  55.     for i = 1, #state do
  56.         for j = 1, #state do
  57.             if i ~= j then
  58.                 local stateCopy = {spentLevels = state.spentLevels, history = {}}
  59.                 for i, item in ipairs(state) do
  60.                     stateCopy[i] = copy(item)
  61.                 end
  62.                 for i, v in ipairs(state.history) do
  63.                     stateCopy.history[i] = v
  64.                 end
  65.                 stateCopy.history[#stateCopy.history + 1] = i
  66.                 stateCopy.history[#stateCopy.history + 1] = j
  67.                
  68.                 local target, sacrifice
  69.                 if i < j then
  70.                     sacrifice = table_remove(stateCopy, j)
  71.                     target = table_remove(stateCopy, i)
  72.                 else
  73.                     target = table_remove(stateCopy, i)
  74.                     sacrifice = table_remove(stateCopy, j)
  75.                 end
  76.                
  77.                 if sacrifice.type ~= "sword" then
  78.                     target, cost = combine(target, sacrifice)
  79.                     local stateCopy_spentLevels = stateCopy.spentLevels + cost
  80.                     stateCopy.spentLevels = stateCopy_spentLevels
  81.                     table_insert(stateCopy, target)
  82.                     stateCopy.history[#stateCopy.history + 1] = cost
  83.                    
  84.                     if cost < 40 then
  85.                         if #stateCopy == 1 then
  86.                             if stateCopy_spentLevels == lowestSpentLevels then
  87.                                 table_insert(finalStates, stateCopy)
  88.                             elseif stateCopy_spentLevels < lowestSpentLevels then
  89.                                 lowestSpentLevels = stateCopy_spentLevels
  90.                                 finalStates = {stateCopy}
  91.                                 print(lowestSpentLevels)
  92.                             end
  93.                         else
  94.                             step(stateCopy)
  95.                         end
  96.                     -- else "Too expensive!"
  97.                     end
  98.                 end
  99.             end
  100.         end
  101.     end
  102. end
  103.  
  104. local function book(enchantment, level)
  105.     return {type = "book", enchantments = {[enchantment] = level}, priorWorkings = 0}
  106. end
  107.  
  108. step({
  109.     history = {},
  110.     spentLevels = 0,
  111.  
  112.     {type = "sword", enchantments = {}, priorWorkings = 0},
  113.     book("sharpness", 4),
  114.     book("sharpness", 4),
  115.     book("looting", 3),
  116.     book("unbreaking", 3),
  117.     book("sweepingEdge", 3),
  118.     book("fireAspect", 2),
  119.     book("knockback", 2),
  120.     book("mending", 1)
  121. })
  122.  
  123.  
  124. local function traverse(t, n)
  125.     for k, v in pairs(t) do
  126.         if type(v) == "table" then
  127.             print(string.rep("\t", n) .. k .. ":")
  128.             traverse(v, n + 1)
  129.         else
  130.             print(string.rep("\t", n) .. k .. " = " .. v)
  131.         end
  132.     end
  133. end
  134. traverse(finalStates, 0)
  135.  
  136. love.event.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement