Advertisement
fatboychummy

Absolute and utter pain (DeepResonance crystal determiner function, slow)

Aug 6th, 2023 (edited)
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.53 KB | None | 0 0
  1. -- These values are per 500mB, since a crystal takes 6000mB to make, we will need to divide everything by 12 later to get their actual increment in values.
  2. ---@type table<string, laser_item>
  3. local LASER_VALUES = {}
  4. do
  5.   local function ilv(name, purity, strength, efficiency, max_purity, max_strength, max_efficiency)
  6.     LASER_VALUES[name] = {
  7.       purity = purity or 0,
  8.       strength = strength or 0,
  9.       efficiency = efficiency or 0,
  10.       max_purity = max_purity or 100,
  11.       max_strength = max_strength or 100,
  12.       max_efficiency = max_efficiency or 100
  13.     }
  14.   end
  15.  
  16.   ilv("minecraft:ender_pearl", 2, 0, 0, 100, 0, 0)
  17.   ilv("minecraft:quartz", -1, 0, 7, 0, 0, 80)
  18.   ilv("minecraft:gunpowder", -5, 8, 4, 0, 70, 60)
  19.   ilv("minecraft:diamond", 5, 0, 0, 100, 0, 0)
  20.   ilv("minecraft:glowstone_dust", -2, 6, 3, 0, 50, 50)
  21.   ilv("minecraft:iron_ingot", 0, -2, 1, 0, 0, 20)
  22.   ilv("rftoolsbase:dimensionalshard", 1, 8, 8, 100, 80, 80)
  23.   ilv("minecraft:prismarine_shard", 0, 3, 3, 0, 30, 30)
  24.   ilv("minecraft:gold_ingot", 0, -1, 1, 0, 0, 30)
  25.   ilv("minecraft:prismarine_crystals", 0, 4, 4, 0, 35, 35)
  26.   ilv("minecraft:coal", -1, -10, 0, 0, 0, 0)             -- Not sure why you'd want to use coal.
  27.   ilv("minecraft:nether_star", -60, 90, 90, 0, 100, 100) -- Very powerful, but also requires a repurification.
  28.   ilv("minecraft:nether_wart", -3, 2, -2, 0, 35, 1)
  29.   ilv("minecraft:redstone", -1, 5, 0, 0, 60, 0)
  30.   ilv("minecraft:slime_ball", 0, 0, -10, 0, 0, 1) -- Not sure why you'd want to use slime balls.
  31.   ilv("minecraft:emerald", 8, 0, 0, 100, 0, 0)    -- May be used as a subsitute to repurification?
  32.   ilv("minecraft:blaze_powder", -6, 5, 5, 0, 70, 70)
  33.   ilv("minecraft:ghast_tear", -20, 25, 15, 0, 100, 100)
  34.   ilv("minecraft:snowball", 1, 0, 1, 30, 0, 40)
  35. end
  36.  
  37. --- This function will bruteforce the best combination of items to use in the lasers.
  38. --- It will return a table of items to use, and the amount of items to use, and in what order.
  39. --- This function only needs to care about strength and efficiency, though we must note purity CANNOT go below 1%
  40. --- This function will also take into account the amount of items we have in the storage chest.
  41. --- We will likely need to use some heavy recursion here.
  42. ---@param current laser_list? The current list of items we are using, along with the current strength, efficiency, and purity.
  43. ---@param item_list short_laser_item[]? The list of items we can use.
  44. ---@return table<integer, string> list The best list of items found.
  45. ---@return integer strength The strength of the best list of items found.
  46. ---@return integer efficiency The efficiency of the best list of items found.
  47. ---@return integer purity The purity of the best list of items found.
  48. local function best_item_combo(current, item_list)
  49.   local x = not current
  50.   -- Initial purity (after leaving the purifier) is 85%, initial strength is 10%, initial efficiency is 10%.
  51.   current = current or { list = {}, used = {}, strength = 10, efficiency = 10, purity = 85 }
  52.  
  53.   if not item_list then
  54.     -- We will need to initialize the list of items available, searching the storage chest and adding items as we see them.
  55.     -- We should collapse all similar items so all we have are the item names and the amount of items.
  56.     item_list = {}
  57.  
  58.     local storage = config.peripherals.chests.storage
  59.     local list = smn.call(storage, "list")
  60.     for _, item in pairs(list) do
  61.       local found = false
  62.       for _, item2 in ipairs(item_list) do
  63.         if item.name == item2.name then
  64.           item2.count = item2.count + item.count
  65.           found = true
  66.           break
  67.         end
  68.       end
  69.  
  70.       if not found then
  71.         table.insert(item_list, { name = item.name, count = item.count })
  72.       end
  73.     end
  74.   end
  75.  
  76.   -- We will need to loop through the item list, and for each item, we will need to add it to the current list, then call this function again.
  77.   -- We will need to keep track of the best item list, and the best strength and efficiency.
  78.   local best_strength = current.strength
  79.   local best_efficiency = current.efficiency
  80.   local best_purity = current.purity
  81.   local best_list = {}
  82.  
  83.   for _, item in ipairs(item_list) do
  84.     if x then print(item.name) end
  85.     -- Check how much of this item remains
  86.     local remaining = item.count - (current.used[item.name] or 0)
  87.  
  88.     -- If there's items remaining, and this is a laserable item...
  89.     if remaining > 0 and LASER_VALUES[item.name] then
  90.       -- Add this item to both lists.
  91.       table.insert(current.list, item.name)
  92.       current.used[item.name] = (current.used[item.name] or 0) + 1
  93.  
  94.       -- Calculate the new strength, efficiency, and purity.
  95.       local laser_value = LASER_VALUES[item.name]
  96.       local new_strength, new_efficiency, new_purity
  97.  
  98.       -- If the values are above zero, we cannot go above the maximum.
  99.       -- If the values are below or equal to zero, we can go down essentially forever (to a minimum of zero)
  100.       -- We will also hard-cap each side at 100 and 0, just to ensure nothing goes crazy anywhere.
  101.  
  102.       if laser_value.strength > 0 then
  103.         new_strength = math.min(current.strength + LASER_VALUES[item.name].strength / 12, laser_value.max_strength, 100)
  104.       else
  105.         new_strength = math.max(current.strength + LASER_VALUES[item.name].strength / 12, laser_value.max_strength, 0)
  106.       end
  107.  
  108.       if laser_value.efficiency > 0 then
  109.         new_efficiency = math.min(current.efficiency + LASER_VALUES[item.name].efficiency / 12, laser_value.max_efficiency, 100)
  110.       else
  111.         new_efficiency = math.max(current.efficiency + LASER_VALUES[item.name].efficiency / 12, laser_value.max_efficiency, 0)
  112.       end
  113.  
  114.       if laser_value.purity > 0 then
  115.         new_purity = math.min(current.purity + LASER_VALUES[item.name].purity / 12, laser_value.max_purity, 100)
  116.       else
  117.         new_purity = math.max(current.purity + LASER_VALUES[item.name].purity / 12, laser_value.max_purity, 0)
  118.       end
  119.  
  120.       -- If the new strength and efficiency are better than the best, we will need to update the best.
  121.       -- However, if the purity is below or equal to 1%, we will discard this attempt.
  122.       if new_purity > 1 and new_strength > best_strength and new_efficiency > best_efficiency then
  123.         -- If the new strength and efficiency are better than the best, we will need to update the best.
  124.         best_strength = new_strength
  125.         best_efficiency = new_efficiency
  126.         best_purity = new_purity
  127.         best_list = deep_copy(current.list)
  128.       end
  129.  
  130.       local new_current = deep_copy(current)
  131.       new_current.strength = new_strength
  132.       new_current.efficiency = new_efficiency
  133.       new_current.purity = new_purity
  134.  
  135.       -- Now we will recurse, with the new values and list.
  136.       local best_recursed_list, best_recursed_strength, best_recursed_efficiency, best_recursed_purity = best_item_combo(new_current, item_list)
  137.  
  138.       -- Then we will need to check if the recursed values are better than the current best.
  139.       if best_recursed_purity > 1 and best_recursed_strength > best_strength and best_recursed_efficiency > best_efficiency then
  140.         best_strength = best_recursed_strength
  141.         best_efficiency = best_recursed_efficiency
  142.         best_purity = best_recursed_purity
  143.         best_list = best_recursed_list
  144.       end
  145.  
  146.       -- Now we will need to remove the item from the current list.
  147.       table.remove(current.list, #current.list)
  148.       current.used[item.name] = current.used[item.name] - 1
  149.     end -- else we go to the next item.
  150.   end
  151.  
  152.   return best_list, best_strength, best_efficiency, best_purity
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement