Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args ={...}
- local component = require("component")
- local robot = require("robot")
- local sides = require("sides")
- -- Main output side
- local f = sides.forward
- -- Main input sides
- local u = sides.up
- local d = sides.down
- -- Controllers named
- inv_controller = component.inventory_controller
- -- What to craft
- toCraft = args[1]
- --Amount to craft
- amount = tonumber(args[2])
- if amount==nil then
- amount=1
- end
- -- Craftin formulas
- formulas = {}
- formulas["Double Compressed Cobblestone"]={
- [1] = 'Compressed Cobblestone',
- [2] = 'Compressed Cobblestone',
- [3] = 'Compressed Cobblestone',
- [5] = 'Compressed Cobblestone',
- [6] = 'Compressed Cobblestone',
- [7] = 'Compressed Cobblestone',
- [9] = 'Compressed Cobblestone',
- [10] = 'Compressed Cobblestone',
- [11] = 'Compressed Cobblestone',
- }
- formulas["Triple Compressed Cobblestone"]={
- [1] = 'Double Compressed Cobblestone',
- [2] = 'Double Compressed Cobblestone',
- [3] = 'Double Compressed Cobblestone',
- [5] = 'Double Compressed Cobblestone',
- [6] = 'Double Compressed Cobblestone',
- [7] = 'Double Compressed Cobblestone',
- [9] = 'Double Compressed Cobblestone',
- [10] = 'Double Compressed Cobblestone',
- [11] = 'Double Compressed Cobblestone',
- }
- formulas["Quadruple Compressed Cobblestone"]={
- [1] = 'Triple Compressed Cobblestone',
- [2] = 'Triple Compressed Cobblestone',
- [3] = 'Triple Compressed Cobblestone',
- [5] = 'Triple Compressed Cobblestone',
- [6] = 'Triple Compressed Cobblestone',
- [7] = 'Triple Compressed Cobblestone',
- [9] = 'Triple Compressed Cobblestone',
- [10] = 'Triple Compressed Cobblestone',
- [11] = 'Triple Compressed Cobblestone',
- }
- formulas["Compressed Cobblestone"]={
- [1] = 'Cobblestone',
- [2] = 'Cobblestone',
- [3] = 'Cobblestone',
- [5] = 'Cobblestone',
- [6] = 'Cobblestone',
- [7] = 'Cobblestone',
- [9] = 'Cobblestone',
- [10] = 'Cobblestone',
- [11] = 'Cobblestone',
- }
- craftingSlots = {
- [0] = 1,
- [1] = 2,
- [2] = 3,
- [4] = 5,
- [5] = 6,
- [6] = 7,
- [7] = 9,
- [8] = 10,
- [9] = 11
- }
- materials_list = {}
- function moveStuff(material,to_slot,amount)
- local input_chest = inv_controller.getAllStacks(d).getAll()
- i = 1
- for slot_key, item in pairs(input_chest) do
- if item then
- itemName = item.label
- if itemName == material then
- robot.select(to_slot)
- inv_controller.suckFromSlot(d,i,amount)
- return
- end
- else
- end
- i = i+1
- end
- end
- function clearTable()
- for key, slot in pairs(craftingSlots) do
- robot.select(slot)
- robot.drop()
- end
- robot.select(1)
- end
- function dump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. dump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
- end
- function innventoryList(side)
- inv_list = inv_controller.getAllStacks(side).getAll()
- i = 1
- inv_list_array = {}
- for key, inv_slot in pairs(inv_list) do
- if type(inv_slot) == 'table' then
- if inv_slot.label then
- if inv_slot.label == 'Air' then
- else
- if type(inv_list_array[inv_slot.label])=='table' then
- inv_list_array[inv_slot.label]['totalcount'] = inv_list_array[inv_slot.label]['totalcount'] + inv_slot.size
- inv_list_array[inv_slot.label]['slots'][i] = {}
- inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size
- else
- inv_list_array[inv_slot.label] = {}
- inv_list_array[inv_slot.label]['totalcount'] = inv_slot.size
- inv_list_array[inv_slot.label]['slots'] = {}
- inv_list_array[inv_slot.label]['slots'][i] = {}
- inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size
- end
- end
- end
- end
- i = i + 1
- end
- return inv_list_array
- end
- function countMaterialsInInventory(list,inv_list_array,direction)
- materials_status = {}
- for material, amount_needed in pairs(list) do
- materials_status[material] = {}
- if inv_list_array[material] then
- inv_count = inv_list_array[material]['totalcount']
- if inv_count >= amount_needed then
- materials_status[material]['have'] = inv_count
- materials_status[material]['needed'] = 0
- else
- materials_status[material]['have'] = inv_count
- materials_status[material]['needed'] = amount_needed - inv_count
- end
- else
- materials_status[material]['have'] = 0
- materials_status[material]['needed'] = amount_needed
- end
- end
- return materials_status
- end
- function countMaterials(mainMaterial)
- if formulas[mainMaterial] then
- materials_list = countToList(mainMaterial)
- end
- end
- function countToList(materialName,material_list)
- material_list = {}
- if(formulas[materialName]) then
- for slot, material in pairs(formulas[materialName]) do
- if type(material_list[material])=='table' then
- if material_list[material]['count'] then
- material_list[material]['count'] = material_list[material]['count'] + 1
- else
- material_list[material]['count'] = 1
- end
- else
- material_list[material] = {}
- material_list[material]['count'] = 1
- end
- if formulas[material] then
- -- materials_list[material]['submaterials'] = {}
- materials_list = material_list
- material_list[material]['submaterials'] = countToList(material,materials_list)
- end
- end
- end
- return material_list
- end
- function findMissing(materials_list,inv_list,missing_materials,multiplier)
- missing_materials = {}
- for material,materialInfo in pairs(materials_list) do
- if multiplier then
- multiplier = multiplier * materialInfo['count']
- else
- multiplier = materialInfo['count']
- end
- if inv_list[material] then
- print(dump(inv_list[material]))
- if inv_list[material]['totalcount'] then
- total = inv_list[material]['totalcount']
- if total < materialInfo['count'] then
- if materialInfo['submaterials'] then
- missing_materials[material]['count'] = findMissing(materialInfo['submaterials'],inv_list,missing_materials,multiplier)
- else
- if missing_materials[material] then
- missing_materials[material]['count'] = materials_missing[material]['count'] + 1
- else
- missing_materials[material] = {}
- fullamount = multiplier
- fullamount = fullamount - total
- missing_materials[material]['count'] = fullamount
- end
- end
- end
- end
- else
- if materialInfo['submaterials'] then
- missing_materials[material] = {}
- missing_materials[material] = findMissing(materialInfo['submaterials'],inv_list,missing_materials,multiplier)
- else
- if missing_materials[material] then
- missing_materials[material['count']] = multiplier
- else
- missing_materials[material] = {}
- missing_materials[material]['count'] = multiplier
- end
- end
- end
- end
- return missing_materials
- end
- function craftStuff(thingName,amount)
- countMaterials(thingName)
- inv_list = innventoryList(d)
- materials_missing = findMissing(materials_list,inv_list)
- print(dump(materials_missing))
- end
- while true do
- craftStuff(toCraft,amount)
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement