Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CHANNEL_BROADCAST = 11
- CHANNEL_PROTOCOL = "recipe_11"
- CHANNEL_FEEDBACK_PROTOCOL = "recipe_11_feedback"
- rednet.open("right")
- local storage_slots = { 1, 2, 3, 4, 8, 12 }
- local function transferTo(slots, targetSlot, count)
- for _, slot in ipairs(slots) do
- if slot.count > 0 then
- turtle.select(slot.slot)
- if count > slot.count then
- turtle.transferTo(targetSlot, slot.count)
- count = count - slot.count
- slot.count = 0
- else
- turtle.transferTo(targetSlot, count)
- slot.count = slot.count - count
- break
- end
- end
- end
- end
- local function clearInventory()
- for i = 1, 16 do
- local count = turtle.getItemCount(i)
- if count > 0 then
- turtle.select(i)
- turtle.dropDown(count)
- end
- end
- end
- local function craft(recipe)
- local materials = {}
- for slot = 1, 9 do
- turtle.select(slot)
- if turtle.suck() == false then
- break
- end
- local itemDetail = turtle.getItemDetail(slot)
- if materials[itemDetail.name] == nil then
- materials[itemDetail.name] = { total = 0, slots = {} }
- end
- materials[itemDetail.name].total = materials[itemDetail.name].total + itemDetail.count
- table.insert(materials[itemDetail.name].slots, { slot = slot, count = itemDetail.count })
- -- 排序slots, 从大到小
- table.sort(materials[itemDetail.name].slots, function(a, b) return a.slot > b.slot end)
- end
- for index = #recipe, 1, -1 do
- if recipe[index] ~= nil then
- local name = recipe[index].name
- transferTo(materials[name].slots, recipe[index].slot, recipe[index].count)
- end
- end
- turtle.select(1)
- turtle.craft()
- for slot = 1, 16 do
- turtle.select(slot)
- local success = turtle.dropUp()
- if success == false then
- break
- end
- end
- end
- while true do
- local _, message = rednet.receive(CHANNEL_PROTOCOL)
- print("received recipe: " .. message)
- clearInventory()
- local recipe = textutils.unserialise(message)
- craft(recipe)
- rednet.broadcast("done", CHANNEL_FEEDBACK_PROTOCOL)
- end
Add Comment
Please, Sign In to add comment