Advertisement
vittoema96

Sieve-to-Ore

Jan 23rd, 2022 (edited)
612
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This program must be pasted to a ComputerCraft CraftyTurtle
  2. -- The CraftyTurtle must face a Chest that receives output from
  3. -- an AutoSieve processing Gravel (with any Ex-Nihilo Mesh)
  4. -- It will output autocrafted ores to a chest paced on the bottom
  5.  
  6. -- Note:
  7. -- Possible outputs from sieved gravel:
  8. -- Ex Nihilo Ore Piece:
  9. --      Gold, Copper, Iron, Tin, Aluminum, Lead, Silver, Nickel
  10. -- Minecraft:
  11. --      Flint, Lapis lazuli, coal, Diamond, Emerald
  12. -- JAOPCA:
  13. --      Osmium Ore Piece
  14. -- Actually Additions:
  15. --      Crushed Black Quartz
  16.  
  17.  
  18. function canCraftWith(itemName)
  19.     if string.find(itemName, "exnihilocreatio:item_ore_") or string.find(itemName, "jaopca:item_piece") then
  20.         return true
  21.     else
  22.         return false
  23.     end
  24. end
  25.  
  26. -- Fills the turtle inventory from items placed in a chest in front of it
  27. function fillTurtle()
  28.     local canTakeItem = true
  29.     while canTakeItem do
  30.         canTakeItem = turtle.suck()
  31.     end
  32. end
  33.  
  34. -- Returns a table with pairs key - value
  35. -- with keys being the unique names of items in inventory
  36. -- and values being the total number of items with that name in the inventory
  37. function getQuantities()
  38.     local quantitiesTable = {}
  39.     for i=1, 16 do
  40.         turtle.select(i)
  41.         local itemName = ""
  42.         local itemCount = turtle.getItemCount()
  43.         if itemCount>0 then
  44.             itemName = turtle.getItemDetail().name
  45.             if quantitiesTable[itemName] == nil then
  46.                 quantitiesTable[itemName] = 0
  47.             end
  48.             quantitiesTable[itemName] = quantitiesTable[itemName] + itemCount
  49.         end
  50.     end
  51.     return quantitiesTable
  52. end
  53.  
  54. -- Returns the key of the table with the highest int value
  55. function getKeyWithHighestValue(quantitiesTable)
  56.     local maxKey = "none"
  57.     quantitiesTable["none"] = 0
  58.     for k, v in pairs(quantitiesTable) do
  59.         if v>quantitiesTable[maxKey] and canCraftWith(k) then
  60.             maxKey = k
  61.         end
  62.     end
  63.     return maxKey
  64. end
  65.  
  66. function removeAllExcept(itemToKeep)
  67.     for i=1, 16 do
  68.         turtle.select(i)
  69.         if turtle.getItemCount()>0 then
  70.             if turtle.getItemDetail().name ~= itemToKeep then
  71.                 if canCraftWith(turtle.getItemDetail().name) then
  72.                     turtle.drop()
  73.                 else
  74.                     turtle.dropDown()
  75.                 end
  76.             else
  77.                 local transferSlot = 1
  78.                 while turtle.getSelectedSlot()>transferSlot do
  79.                     turtle.transferTo(transferSlot)
  80.                     if turtle.getItemCount()>0 then
  81.                         transferSlot = transferSlot + 1
  82.                     else
  83.                         transferSlot = 100
  84.                     end
  85.                 end
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. while true do
  92.     fillTurtle()
  93.     local quantities = getQuantities()
  94.     local itemToCraft = getKeyWithHighestValue(quantities)
  95.     removeAllExcept(itemToCraft)
  96.     local availableResources = quantities[itemToCraft]
  97.     local quarter = math.floor(availableResources/4)
  98.     if quarter > 64 then
  99.         for i=5, 16 do
  100.             turtle.select(i)
  101.             turtle.drop()  
  102.         end
  103.     end
  104.     for i=1, 4 do
  105.         turtle.select(i)
  106.         if turtle.getItemCount()>0 then
  107.             local q = math.floor(turtle.getItemCount()/4)
  108.             turtle.transferTo(16, q)
  109.             turtle.transferTo(15, q)
  110.             turtle.transferTo(12, q)
  111.             turtle.transferTo(11, q)
  112.             if turtle.getItemCount()>0 then
  113.                 turtle.drop()
  114.             end
  115.         end
  116.     end
  117.     turtle.select(1)
  118.     turtle.craft()
  119.     turtle.dropDown()
  120. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement