Advertisement
forzakenus

Saltshaker

Dec 2nd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.18 KB | None | 0 0
  1. local args ={...}
  2. local component = require("component")
  3. local robot = require("robot")
  4. local sides = require("sides")
  5. -- Main output side
  6. local f = sides.forward
  7. -- Main input sides
  8. local u = sides.up
  9. local d = sides.down
  10. -- Controllers named
  11. inv_controller = component.inventory_controller
  12. -- What to craft
  13. toCraft = args[1]
  14. --Amount to craft
  15. amount = tonumber(args[2])
  16.  
  17. -- Craftin formulas
  18. formulas = {}
  19. formulas["Double Compressed Cobblestone"]={
  20.             [1] = 'Compressed Cobblestone',
  21.             [2] = 'Compressed Cobblestone',
  22.             [3] = 'Compressed Cobblestone',
  23.             [5] = 'Compressed Cobblestone',
  24.             [6] = 'Compressed Cobblestone',
  25.             [7] = 'Compressed Cobblestone',
  26.             [9] = 'Compressed Cobblestone',
  27.             [10] = 'Compressed Cobblestone',
  28.             [11] = 'Compressed Cobblestone',
  29. }
  30. formulas["Triple Compressed Cobblestone"]={
  31.             [1] = 'Double Compressed Cobblestone',
  32.             [2] = 'Double Compressed Cobblestone',
  33.             [3] = 'Double Compressed Cobblestone',
  34.             [5] = 'Double Compressed Cobblestone',
  35.             [6] = 'Double Compressed Cobblestone',
  36.             [7] = 'Double Compressed Cobblestone',
  37.             [9] = 'Double Compressed Cobblestone',
  38.             [10] = 'Double Compressed Cobblestone',
  39.             [11] = 'Double Compressed Cobblestone',
  40. }
  41. formulas["Quadruple Compressed Cobblestone"]={
  42.             [1] = 'Triple Compressed Cobblestone',
  43.             [2] = 'Triple Compressed Cobblestone',
  44.             [3] = 'Triple Compressed Cobblestone',
  45.             [5] = 'Triple Compressed Cobblestone',
  46.             [6] = 'Triple Compressed Cobblestone',
  47.             [7] = 'Triple Compressed Cobblestone',
  48.             [9] = 'Triple Compressed Cobblestone',
  49.             [10] = 'Triple Compressed Cobblestone',
  50.             [11] = 'Triple Compressed Cobblestone',
  51. }
  52.  
  53. formulas["Compressed Cobblestone"]={
  54.             [1] = 'Cobblestone',
  55.             [2] = 'Cobblestone',
  56.             [3] = 'Cobblestone',
  57.             [5] = 'Cobblestone',
  58.             [6] = 'Cobblestone',
  59.             [7] = 'Cobblestone',
  60.             [9] = 'Cobblestone',
  61.             [10] = 'Cobblestone',
  62.             [11] = 'Cobblestone',
  63. }
  64. craftingSlots = {
  65.     [0] = 1,
  66.     [1] = 2,
  67.     [2] = 3,
  68.     [4] = 5,
  69.     [5] = 6,
  70.     [6] = 7,
  71.     [7] = 9,
  72.     [8] = 10,
  73.     [9] = 11
  74. }
  75. function countMaterials(craft,amount)
  76.     if formulas[craft] then
  77.         materials_list = loopCountMaterials(formulas[craft])
  78.         return materials_list
  79.     end
  80. end
  81. function loopCountMaterials(formula)
  82.  
  83.         for key, material in pairs(formula) do
  84.             if formulas[material] then
  85.                 loopCountMaterials(formulas[material])
  86.             else
  87.                 if materials_list[material] then
  88.                     materials_list[material] = materials_list[material] + 1
  89.                 else
  90.                     materials_list[material] = 1
  91.                 end
  92.             end
  93.         end
  94.     return materials_list
  95. end
  96. function moveStuff(material,to_slot,amount)
  97.     local input_chest = inv_controller.getAllStacks(d).getAll()
  98.     i = 1
  99.     for slot_key, item in pairs(input_chest) do
  100.         if item then
  101.             itemName = item.label
  102.             if itemName == material then
  103.                 robot.select(to_slot)
  104.                 inv_controller.suckFromSlot(d,i,amount)
  105.                 return
  106.             end
  107.         else
  108.         end
  109.         i = i+1
  110.     end
  111. end
  112. function clearTable()
  113.     for key, slot in pairs(craftingSlots) do
  114.         robot.select(slot)
  115.         robot.drop()
  116.     end
  117.     robot.select(1)
  118. end
  119. function dump(o)
  120.    if type(o) == 'table' then
  121.       local s = '{ '
  122.       for k,v in pairs(o) do
  123.          if type(k) ~= 'number' then k = '"'..k..'"' end
  124.          s = s .. '['..k..'] = ' .. dump(v) .. ','
  125.       end
  126.       return s .. '} '
  127.    else
  128.       return tostring(o)
  129.    end
  130. end
  131. function innventoryList(side)
  132.     inv_list = inv_controller.getAllStacks(side).getAll()
  133.     i = 1
  134.     inv_list_array = {}
  135.     for key, inv_slot in pairs(inv_list) do
  136.         if type(inv_slot) == 'table' then
  137.             if inv_slot.label then
  138.                 if inv_slot.label == 'Air' then
  139.                    
  140.                 else
  141.                     if type(inv_list_array[inv_slot.label])=='table' then
  142.                         inv_list_array[inv_slot.label]['totalcount'] = inv_list_array[inv_slot.label]['totalcount'] + inv_slot.size
  143.                         inv_list_array[inv_slot.label]['slots'][i] = {}
  144.                         inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size
  145.                     else
  146.                         inv_list_array[inv_slot.label] = {}
  147.                         inv_list_array[inv_slot.label]['totalcount'] = inv_slot.size
  148.                         inv_list_array[inv_slot.label]['slots'] = {}
  149.                         inv_list_array[inv_slot.label]['slots'][i] = {}
  150.                         inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size
  151.                     end
  152.                 end
  153.                
  154.             end
  155.         end
  156.         i = i + 1
  157.     end
  158.     return inv_list_array
  159. end
  160. function countMaterialsInInventory(list,inv_list_array,direction)
  161.    
  162.     materials_status = {}
  163.     for material, amount_needed in pairs(list) do
  164.         materials_status[material] = {}
  165.         if inv_list_array[material] then
  166.             inv_count = inv_list_array[material]['totalcount']
  167.             if inv_count >= amount_needed then
  168.                 materials_status[material]['have'] = inv_count
  169.                 materials_status[material]['needed'] = 0
  170.             else
  171.                 materials_status[material]['have'] = inv_count
  172.                 materials_status[material]['needed'] = amount_needed - inv_count
  173.             end
  174.         else
  175.             materials_status[material]['have'] = 0
  176.             materials_status[material]['needed'] = amount_needed
  177.         end
  178.     end
  179.     return materials_status
  180. end
  181. function craftStuff(thingName,amount)
  182.   materials_list = {}
  183.   materials_list =  countMaterials(thingName,amount)
  184.   inv_list = innventoryList(d)
  185.   materials_status = countMaterialsInInventory(materials_list,inv_list,d)
  186.   can_craft = true
  187.   for material, status in pairs(materials_status) do
  188.       if status['needed'] > 0 then
  189.           print('We need more ' .. material)
  190.           print('Total amount needed ' .. status['needed'])
  191.           can_craft = false
  192.       end
  193.   end
  194.   if can_craft == true then
  195.     print('Just do it')
  196.   else
  197.    
  198.     print('Waiting for listed materials .... ')
  199.     os.sleep(10)
  200.     craftStuff(thingName,amount)
  201.   end
  202. end
  203. while true do
  204.     craftStuff(toCraft,amount)
  205.     return
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement