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])
- -- 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
- }
- function countMaterials(craft,amount)
- if formulas[craft] then
- materials_list = loopCountMaterials(formulas[craft])
- return materials_list
- end
- end
- function loopCountMaterials(formula)
- for key, material in pairs(formula) do
- if formulas[material] then
- loopCountMaterials(formulas[material])
- else
- if materials_list[material] then
- materials_list[material] = materials_list[material] + 1
- else
- materials_list[material] = 1
- end
- end
- end
- return materials_list
- end
- 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 craftStuff(thingName,amount)
- materials_list = {}
- materials_list = countMaterials(thingName,amount)
- inv_list = innventoryList(d)
- materials_status = countMaterialsInInventory(materials_list,inv_list,d)
- can_craft = true
- for material, status in pairs(materials_status) do
- if status['needed'] > 0 then
- print('We need more ' .. material)
- print('Total amount needed ' .. status['needed'])
- can_craft = false
- end
- end
- if can_craft == true then
- print('Just do it')
- else
- print('Waiting for listed materials .... ')
- os.sleep(10)
- craftStuff(thingName,amount)
- end
- end
- while true do
- craftStuff(toCraft,amount)
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement