forzakenus

craftwerk

May 1st, 2020
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.85 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. if amount==nil then
  17.     amount=1
  18. end
  19. -- Craftin formulas
  20. formulas = {}
  21. formulas["Item Barrel"] = {
  22.     [1] = 'Oak Wood',
  23.     [2] = 'Oak Wood Slab',
  24.     [3] = 'Oak Wood',
  25.     [5] = 'Oak Wood',
  26.     [6] = 'Emptyness is hard',
  27.     [7] = 'Oak Wood',
  28.     [9] = 'Oak Wood',
  29.     [10] = 'Oak Wood',
  30.     [11] = 'Oak Wood',
  31. }
  32. formulas["Double Compressed Cobblestone"]={
  33.             [1] = 'Compressed Cobblestone',
  34.             [2] = 'Compressed Cobblestone',
  35.             [3] = 'Compressed Cobblestone',
  36.             [5] = 'Compressed Cobblestone',
  37.             [6] = 'Compressed Cobblestone',
  38.             [7] = 'Compressed Cobblestone',
  39.             [9] = 'Compressed Cobblestone',
  40.             [10] = 'Compressed Cobblestone',
  41.             [11] = 'Compressed Cobblestone',
  42. }
  43. formulas["Triple Compressed Cobblestone"]={
  44.             [1] = 'Double Compressed Cobblestone',
  45.             [2] = 'Double Compressed Cobblestone',
  46.             [3] = 'Double Compressed Cobblestone',
  47.             [5] = 'Double Compressed Cobblestone',
  48.             [6] = 'Double Compressed Cobblestone',
  49.             [7] = 'Double Compressed Cobblestone',
  50.             [9] = 'Double Compressed Cobblestone',
  51.             [10] = 'Double Compressed Cobblestone',
  52.             [11] = 'Double Compressed Cobblestone',
  53. }
  54. formulas["Quadruple Compressed Cobblestone"]={
  55.             [1] = 'Triple Compressed Cobblestone',
  56.             [2] = 'Triple Compressed Cobblestone',
  57.             [3] = 'Triple Compressed Cobblestone',
  58.             [5] = 'Triple Compressed Cobblestone',
  59.             [6] = 'Triple Compressed Cobblestone',
  60.             [7] = 'Triple Compressed Cobblestone',
  61.             [9] = 'Triple Compressed Cobblestone',
  62.             [10] = 'Triple Compressed Cobblestone',
  63.             [11] = 'Triple Compressed Cobblestone',
  64. }
  65.  
  66. formulas["Compressed Cobblestone"]={
  67.             [1] = 'Cobblestone',
  68.             [2] = 'Cobblestone',
  69.             [3] = 'Cobblestone',
  70.             [5] = 'Cobblestone',
  71.             [6] = 'Cobblestone',
  72.             [7] = 'Cobblestone',
  73.             [9] = 'Cobblestone',
  74.             [10] = 'Cobblestone',
  75.             [11] = 'Cobblestone',
  76. }
  77. craftingSlots = {
  78.     [0] = 1,
  79.     [1] = 2,
  80.     [2] = 3,
  81.     [4] = 5,
  82.     [5] = 6,
  83.     [6] = 7,
  84.     [7] = 9,
  85.     [8] = 10,
  86.     [9] = 11
  87. }
  88. materials_list = {}
  89. materials_formula = {}
  90. function moveStuff(material,to_slot,amount)
  91.     local input_chest = inv_controller.getAllStacks(d).getAll()
  92.     i = 1
  93.     for slot_key, item in pairs(input_chest) do
  94.         if item then
  95.             itemName = item.label
  96.             if itemName == material then
  97.                 robot.select(to_slot)
  98.                 inv_controller.suckFromSlot(d,i,amount)
  99.                 return
  100.             end
  101.         else
  102.         end
  103.         i = i+1
  104.     end
  105. end
  106. function clearTable()
  107.     for key, slot in pairs(craftingSlots) do
  108.         robot.select(slot)
  109.         robot.drop()
  110.     end
  111.     robot.select(1)
  112. end
  113. function dump(o)
  114.    if type(o) == 'table' then
  115.       local s = '{ '
  116.       for k,v in pairs(o) do
  117.          if type(k) ~= 'number' then k = '"'..k..'"' end
  118.          s = s .. '['..k..'] = ' .. dump(v) .. ','
  119.       end
  120.       return s .. '} '
  121.    else
  122.       return tostring(o)
  123.    end
  124. end
  125. function innventoryList(side)
  126.     inv_list = inv_controller.getAllStacks(side).getAll()
  127.     i = 1
  128.     inv_list_array = {}
  129.     for key, inv_slot in pairs(inv_list) do
  130.         if type(inv_slot) == 'table' then
  131.             if inv_slot.label then
  132.                 if inv_slot.label == 'Air' then
  133.                    
  134.                 else
  135.                     if type(inv_list_array[inv_slot.label])=='table' then
  136.                         inv_list_array[inv_slot.label]['totalcount'] = inv_list_array[inv_slot.label]['totalcount'] + inv_slot.size
  137.                         inv_list_array[inv_slot.label]['slots'][i] = {}
  138.                         inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size
  139.                     else
  140.                         inv_list_array[inv_slot.label] = {}
  141.                         inv_list_array[inv_slot.label]['totalcount'] = inv_slot.size
  142.                         inv_list_array[inv_slot.label]['slots'] = {}
  143.                         inv_list_array[inv_slot.label]['slots'][i] = {}
  144.                         inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size
  145.                     end
  146.                 end
  147.                
  148.             end
  149.         end
  150.         i = i + 1
  151.     end
  152.     return inv_list_array
  153. end
  154. function countMaterialsInInventory(list,inv_list_array,direction)
  155.    
  156.     materials_status = {}
  157.     for material, amount_needed in pairs(list) do
  158.         materials_status[material] = {}
  159.         if inv_list_array[material] then
  160.             inv_count = inv_list_array[material]['totalcount']
  161.             if inv_count >= amount_needed then
  162.                 materials_status[material]['have'] = inv_count
  163.                 materials_status[material]['needed'] = 0
  164.             else
  165.                 materials_status[material]['have'] = inv_count
  166.                 materials_status[material]['needed'] = amount_needed - inv_count
  167.             end
  168.         else
  169.             materials_status[material]['have'] = 0
  170.             materials_status[material]['needed'] = amount_needed
  171.         end
  172.     end
  173.     return materials_status
  174. end
  175. function countMaterials(mainMaterial)
  176.     if formulas[mainMaterial] then
  177.         materials_list = countToList(mainMaterial)
  178.     end
  179. end
  180. function countToList(materialName,material_list)
  181.     material_list = {}
  182.     if(formulas[materialName]) then
  183.         for slot, material in pairs(formulas[materialName]) do
  184.             if type(material_list[material])=='table' then
  185.                 if material_list[material]['count'] then
  186.                     material_list[material]['count'] = material_list[material]['count'] + 1
  187.                 else
  188.                     material_list[material]['count'] = 1
  189.                 end
  190.             else
  191.                 material_list[material] = {}
  192.                 material_list[material]['count'] = 1
  193.             end
  194.             if formulas[material] then
  195.                 -- materials_list[material]['submaterials'] = {}
  196.                 materials_list = material_list
  197.                 material_list[material]['submaterials'] = countToList(material,materials_list)
  198.             end
  199.         end
  200.     end
  201.     return material_list
  202. end
  203. function findMissing(materials_list,inv_list,missing_materials,multiplier)
  204.     missing_materials = {}
  205.     for material,materialInfo  in pairs(materials_list) do
  206.         if multiplier then
  207.             multiplier = multiplier * materialInfo['count']
  208.         else
  209.             multiplier = materialInfo['count']
  210.         end
  211.         if inv_list[material] then
  212.            
  213.             print(dump(inv_list[material]))
  214.             if inv_list[material]['totalcount'] then
  215.                 total = inv_list[material]['totalcount']
  216.                 if total < materialInfo['count'] then
  217.                     if materialInfo['submaterials'] then
  218.                         missing_materials[material]['count'] = findMissing(materialInfo['submaterials'],inv_list,missing_materials,multiplier)
  219.                     else
  220.                         if missing_materials[material] then
  221.                             missing_materials[material]['count'] = materials_missing[material]['count'] + 1
  222.                         else
  223.                             missing_materials[material] = {}
  224.                             fullamount =  multiplier
  225.                             fullamount = fullamount - total
  226.                             missing_materials[material]['count'] = fullamount
  227.                         end
  228.                     end
  229.                 end
  230.             end
  231.         else
  232.             if materialInfo['submaterials'] then
  233.                 missing_materials[material] = {}
  234.              
  235.                 missing_materials[material] = findMissing(materialInfo['submaterials'],inv_list,missing_materials,multiplier)
  236.             else
  237.                 if missing_materials[material] then
  238.                     missing_materials[material['count']] = multiplier
  239.                 else
  240.                     missing_materials[material] = {}
  241.                     missing_materials[material]['count'] = multiplier
  242.                 end
  243.             end
  244.         end
  245.     end
  246.     return missing_materials
  247. end
  248. function materials_multiplied(materials_list,amount,mat_multiplied)
  249.     mat_multiplied = {}
  250.     for mat_name, mat_info in pairs(materials_list) do
  251.         multiplier = mat_info['count']
  252.         if amount then
  253.             multiplier = amount * multiplier
  254.         end
  255.         if type(mat_multiplied[mat_name]) == 'table' then
  256.             mat_multiplied[mat_name]['count'] = multiplier
  257.         else
  258.             mat_multiplied[mat_name] = {}
  259.             mat_multiplied[mat_name]['count'] = multiplier
  260.         end
  261.         if mat_info['submaterials'] then
  262.             mat_multiplied[mat_name]['submaterials'] =  materials_multiplied(mat_info['submaterials'],multiplier,mats_multiplied)
  263.         end
  264.     end
  265.     return mat_multiplied
  266. end
  267. function checkMaterials(mats_multiplied,inv_list,inv_total,mats_needed)
  268. mats_needed = {}
  269. for mat, mat_info in pairs(mats_multiplied) do
  270.     if inv_list[mat] then
  271.         inv_total = inv_list[mat]['totalcount']
  272.         if inv_total < mat_info['count'] then
  273.             amount = mat_info['count'] - inv_total
  274.             if inv_total then
  275.                 amount = amount - materials_formula[mat]
  276.             end
  277.             if type(mats_needed[mat]) == 'table' then
  278.                 mats_needed[mat]['missing'] = amount
  279.             else
  280.                 mats_needed[mat] = {}
  281.                 mats_needed[mat]['missing'] = amount
  282.             end
  283.             if mat_info['submaterials'] then
  284.                 mats_needed[mat]['submaterials'] =  checkMaterials(mat_info['submaterials'],inv_list,inv_total)
  285.             end
  286.         end
  287.     else
  288.         amount = mat_info['count']
  289.         if inv_total then
  290.             substract = inv_total * materials_formula[mat]
  291.             amount = amount - substract
  292.         end
  293.         if type(mats_needed[mat]) == 'table' then
  294.             mats_needed[mat]['missing'] = amount
  295.         else
  296.             mats_needed[mat] = {}
  297.             mats_needed[mat]['missing'] = amount
  298.         end
  299.         if mat_info['submaterials'] then
  300.             mats_needed[mat]['submaterials'] =  checkMaterials(mat_info['submaterials'],inv_list)
  301.         end
  302.     end
  303. end
  304. return mats_needed
  305. end
  306. function materialsTable(thingName,mats)
  307.     for mat,mat_info in pairs(mats) do
  308.         if mat_info['count'] then
  309.             materials_formula[mat] = mat_info['count']
  310.         end
  311.         if mat_info['submaterials'] then
  312.             materialsTable(thingName,mat_info['submaterials'])
  313.         end
  314.     end
  315. end
  316. function printMessages(materials_needed)
  317.     for mat, mat_info in pairs(materials_needed) do
  318.         print('Add atleast '.. mat_info['missing']..' ' .. mat)
  319.         if mat_info['submaterials'] then
  320.             print ('or')
  321.             printMessages(mat_info['submaterials'])
  322.         end
  323.     end
  324. end
  325. function craftStuff(thingName,amount)
  326.     countMaterials(thingName)
  327.     mats_multiplied = materials_multiplied(materials_list)
  328.     inv_list = innventoryList(d)
  329.     materialsTable(thingName,materials_list)
  330.  
  331.     materials_needed = checkMaterials(mats_multiplied,inv_list)
  332.     if next(materials_needed)==nil then
  333.         print("We have everything")
  334.     else
  335.         printMessages(materials_needed)
  336.     end
  337. end
  338.  
  339. while true do
  340.     craftStuff(toCraft,amount)
  341.     return
  342. end
Add Comment
Please, Sign In to add comment