Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------
- -- WHAT IS THIS? --
- --------------------------------------
- -- This is a Lua program for the Minecraft
- -- mod Computercraft (or CC: Tweaked),
- -- specifically for the crafting turtle.
- -- This is used in an "Infinite Ore Machine"
- -- that I have set up in Enigmatica 2: Expert.
- -- The machine consists in
- -- ° A Cobblestone Generator (NuclearCraft)
- -- .. feeding ..
- -- ° An Auto Hammer (Ex Compressum)
- -- .. feeding ..
- -- ° An Auto Sieve (Ex Compressum) with a Diamond Stiffened Mesh (Ex Nihilo Creatio)
- -- .. storing everything in a chest*
- -- * This chest is the inputSide (USER OPTIONS section)
- -- of this program.
- -- The outputSide (USER OPTIONS section) will collect
- -- the ores crafted by the turtle and the
- -- "final products" that can be found in
- -- noCraftingItems (USER OPTIONS section)
- -- TIP:
- -- Connect the outputSide chest to an
- -- ore grinding machine and that machine
- -- to a smelting machine to
- -- complete the setup.
- -- WARNING: Remember that not all items
- -- in the output chest are actually ores,
- -- filter those out.
- ---------------------------------------
- -- THIS PROGRAM FOR DUMMIES --
- -- What this basically does is it:
- --
- -- * It fills the turtle with items from a storage block placed on the
- -- inputSide (set it under USER OPTIONS) side of the turtle.
- --
- -- * if it finds any item with a name
- -- equal to one of the entries in noCraftingItems
- -- (set it under USER OPTIONS) it directly deposits
- -- it in the storage block placed on the outputSide
- -- (set it under USER OPTIONS) side of the turtle.
- --
- -- * It finds the most abundant item in the turtle,
- -- deposits on inputSide all of the other items,
- -- then proceeds to place the remaining item in a
- -- 2x2 grid, tries to craft, then deposits the result
- -- in the outputSide
- --
- -- * Cleans the turtle from any remaining items (may
- -- happen due to machine or software mafunctions)
- --
- -- * Loops.
- --
- ---------------------------------------
- -- HOW TO INSTALL IT? --
- ---------------------------------------
- -- Simply run
- -- pastebin get pHgVd5au startup
- -- then run 'reboot' or press CTRL+R
- -- for a few seconds
- -- WARNING
- -- Remember to have the chests and
- -- everything set up before restarting.
- ---------------------------------------
- -- USER OPTIONS --
- ---------------------------------------
- -- These are constants, set the values
- -- of inputSide and outputSide to one
- -- of these 3 values (NOT THE SAME ONE)
- TOP = 0
- BOTTOM = 1
- FRONT = 2
- -- The side on which the input/output
- -- chests are situated
- inputSide = FRONT
- outputSide = TOP
- -- These are the items that are considered
- -- already a final product and will be
- -- directly deposited in the output chest
- -- during the removeAllExcept phase
- noCraftingItems = {['minecraft:diamond']=true,
- ['minecraft:emerald']=true,
- ['actuallyadditions:item_dust']=true}
- ---------------------------------------
- -- ACTUAL CODE --
- ---------------------------------------
- -- The function that gets called repeatedly
- function loop()
- assureEmptyTurtle()
- itemMap = loadTurtle()
- record, itemMap = extractMostAbundantItem(itemMap)
- removeAll(itemMap)
- craft(record)
- end
- -- Be sure that the turtle is empty
- -- might be full after a world restart or such
- function assureEmptyTurtle()
- rejectFunc = getRejectFunc()
- for i=1, 16 do
- if turtle.getItemCount(i)>0 then
- turtle.select(i)
- rejectFunc()
- end
- end
- end
- function loadTurtle()
- -- The function used to suck up
- -- a slot of input materials
- suckFunc = getSuckFunc()
- -- The function used to deposit
- -- materials that don't need crafting
- deliverFunc = getDeliveryFunc()
- -- This is where the items get registered.
- -- dict(<item_name>: list(dict("slot": int, "count": int))
- itemMap = {}
- firstAvailableSlot = 16
- turtle.select(1)
- suckFunc()
- while turtle.getItemCount() > 0 and firstAvailableSlot > 0 do
- name = getItemName(1)
- -- The item is a final product,
- -- deposit it
- if noCraftingItems[name] then
- deliverFunc()
- -- This is an item we need to handle
- else
- slotList = itemMap[name]
- -- We don't have the item registered
- if slotList == nil then
- record = {slot=firstAvailableSlot, count=turtle.getItemCount()}
- itemMap[name] = {record}
- turtle.transferTo(firstAvailableSlot)
- firstAvailableSlot = firstAvailableSlot - 1
- -- The item was already acquired
- -- by the turtle
- else
- -- The last used slot has space, fill it
- if slotList[#slotList]["count"] < 64 then
- turtle.transferTo(slotList[#slotList]["slot"])
- -- The last used slot is full, get a new one
- else
- slotList[#slotList+1] = {slot=firstAvailableSlot, count=turtle.getItemCount()}
- turtle.transferTo(firstAvailableSlot)
- firstAvailableSlot = firstAvailableSlot - 1
- end
- end
- end
- if turtle.getItemCount() == 0 then
- suckFunc()
- end
- end
- return itemMap
- end
- -- Returns the record of the most abundant
- -- item, and the map without said item in it
- function extractMostAbundantItem(itemMap)
- itemToKeep = nil
- stacks = 0
- for name, list in pairs(itemMap) do
- if #list > stacks then
- itemToKeep = name
- stacks = #list
- elseif #list == stacks then
- challengerList = itemMap[itemToKeep]
- if list[#list]["count"] > challengerList[#challengerList]["count"] then
- itemToKeep = name
- end
- end
- end
- result = itemMap[itemToKeep]
- itemMap[itemToKeep] = nil
- return result, itemMap
- end
- -- Returns all the items in
- -- the turtle back to the input
- -- chest, except fot the
- -- item needed for crafting
- function removeAll(itemMap)
- rejectFunc = getRejectFunc()
- for name, list in pairs(itemMap) do
- for i, record in ipairs(list) do
- turtle.select(record["slot"])
- rejectFunc()
- end
- end
- end
- function craft(recordList)
- tot = (#recordList - 1) * 64 + recordList[#recordList]["count"]
- deliverFunc = getDeliveryFunc()
- rejectFunc = getRejectFunc()
- if #recordList > 4 then
- for i=5, #recordList do
- turtle.select(recordList[i]["slot"])
- rejectFunc()
- recordList[i] = nil
- end
- tot = 64 * 4
- elseif tot % 4 ~= 0 then
- turtle.select(recordList[#recordList]["slot"])
- rejectFunc(tot % 4)
- tot = tot - tot % 4
- end
- slots = {16, 15, 12, 11}
- for i=1, 4 do
- k = 1
- rec = recordList[i]
- if rec == nil then
- break
- end
- selectedSlot = rec["slot"]
- count = rec["count"]
- turtle.select(selectedSlot)
- if selectedSlot == 16 or selectedSlot == 15 or selectedSlot == 12 or selectedSlot == 11 then
- minimum = tot / 4
- else
- minimum = 0
- end
- while turtle.getItemCount() > minimum and k < 5 do
- if selectedSlot == slots[k] or turtle.getItemCount(slots[k]) >= tot / 4 then
- k = k + 1
- else
- turtle.transferTo(slots[k], tot / 4 - turtle.getItemCount(slots[k]))
- end
- end
- end
- turtle.craft()
- deliverFunc()
- end
- -- If there is an item in slot "i"
- -- returns its name, if the slot is
- -- empty returns "nil"
- function getItemName(i)
- if turtle.getItemCount(i) == 0 then
- return nil
- end
- return turtle.getItemDetail(i)["name"]
- end
- -- Returns the callable that sucks
- -- the input from the input chest
- function getSuckFunc()
- if inputSide == FRONT then
- return turtle.suck
- elseif inputSide == TOP then
- return turtle.suckUp
- else
- return turtle.suckDown
- end
- end
- -- Returns the callable that drops
- -- unwanted input back into the
- -- input chest
- function getRejectFunc()
- if inputSide == FRONT then
- return turtle.drop
- elseif inputSide == TOP then
- return turtle.dropUp
- else
- return turtle.dropDown
- end
- end
- -- Returns the callable that drops
- -- the resulting product to the
- -- output chest
- function getDeliveryFunc()
- if outputSide == FRONT then
- return turtle.drop
- elseif outputSide == TOP then
- return turtle.dropUp
- else
- return turtle.dropDown
- end
- end
- ---------------------------------------
- -- MAIN LOOP --
- ---------------------------------------
- -- Check for wrong Configuration
- if not pcall(turtle.craft) then
- error("This turtle should be a crafty turtle, but turtle.craft() couldn't be called")
- end
- if inputSide == outputSide then
- error("inputSide and outputSide were set to the same value, but this is not possible")
- end
- -- Actual loop
- while true do
- loop()
- end
Add Comment
Please, Sign In to add comment