Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if KaoShell and KaoShell.getCo("Peripheral monitoring") then KaoShell.remCo("Peripheral monitoring") end --stop those irritating peripheral attachment notifications for all of the chests
- setfenv(nil,_G)
- while not turtle.detectDown() do turtle.down() end
- local level=1
- local chest=peripheral.wrap("front")
- local sDir
- for _,dir in ipairs({"north","east","south","west"}) do
- if pcall(chest.push,"dir",0,0) then
- sDir=dir
- break
- end
- end
- tLevels={
- [1]={}, --dump
- [2]={"diamond pickaxe","computer","chest","turtle","mining turtle"}, --temp
- [3]={"oak wood","spruce wood","birch wood","jungle wood","stick",}, --wood
- [4]={"iron ingot",}, --iron
- [5]={"redstone","redstone torch",}, --redstone
- [6]={"diamond",}, --diamond
- [7]={"stone",}, --stone
- [8]={"glass","glass pane",}, --glass
- [9]={"coal","charcoal",}, --fuel
- [10]={{"sand",8},{"cobblestone",7},}, --forge
- }
- for i=1,4 do --add planks to wood chest
- tLevels[3][i+5]=tLevels[3][i].." planks"
- end
- local tRecipes={
- stick={
- [1]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
- [5]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
- yield=4,
- },
- ["oak wood planks"]={"oak wood",yield=4,},
- ["spruce wood planks"]={"spruce wood",yield=4,},
- ["birch wood planks"]={"birch wood",yield=4,},
- ["jungle wood planks"]={"jungle wood",yield=4,},
- ["glass pane"]={
- [1]="glass",[2]="glass",[3]="glass",
- [5]="glass",[6]="glass",[7]="glass",
- },
- ["redstone torch"]={
- [1]="redstone",
- [5]="stick",
- },
- ["chest"]={
- [1]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[2]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[3]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
- [5]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[7]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
- [9]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[10]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[11]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
- },
- ["computer"]={
- [1]="stone",[2]="stone",[3]="stone",
- [5]="stone",[6]="redstone",[7]="stone",
- [9]="stone",[10]="glass pane",[11]="stone",
- },
- ["diamond pickaxe"]={
- [1]="diamond",[2]="diamond",[3]="diamond",
- [6]="stick",
- [10]="stick",
- },
- turtle={
- [1]="iron ingot",[2]="iron ingot",[3]="iron ingot",
- [5]="iron ingot",[6]="computer",[7]="iron ingot",
- [9]="iron ingot",[10]="chest",[11]="iron ingot",
- },
- ["mining turtle"]={
- [2]="turtle",[3]="diamond pickaxe",
- },
- }
- local function gotoLevel(nLevel)
- while nLevel~=level do
- if nLevel>level then
- if turtle.up() then level=level+1 end
- else
- if turtle.down() then level=level-1 end
- end
- end
- end
- local function findLevel(sItem)
- if type(sItem)~="string" then error("invalid param, string expected, got "..type(sItem).." - "..(tostring(sItem) or ""),2) end
- for nLevel,tItems in ipairs(tLevels) do
- for _,item in ipairs(tItems) do
- if sItem:lower()==(type(item)=="table" and item[1] or item):lower() then
- return nLevel
- end
- end
- end
- return false
- end
- function dump(sItem,nSlot)
- local nLevel=findLevel(sItem)
- if not nLevel then return false end
- gotoLevel(nLevel)
- chest.pull(sDir,nSlot-1,turtle.getItemCount(nSlot))
- return true
- end
- function sort()
- gotoLevel(1)
- turtle.select(1)
- for nSlot=1,16 do
- if turtle.getItemCount(nSlot)>0 then
- chest.pull(sDir,nSlot-1,turtle.getItemCount(nSlot))
- end
- end
- while true do
- gotoLevel(1)
- chest.condense()
- local tStack=chest.getStackInSlot(0)
- if tStack then
- chest.pushIntoSlot(sDir,0,tStack.qty,0)
- if not dump(tStack.name,1) then
- turtle.dropUp()
- end
- else
- break
- end
- end
- end
- function forge(sItem)
- do --empty inv
- gotoLevel(1)
- for nSlot=1,16 do
- if turtle.getItemCount(nSlot)>0 then
- chest.pull(sDir,nSlot-1,turtle.getItemCount(nSlot))
- end
- end
- end
- local nLevel
- do --refuel the furnace
- gotoLevel(9)
- chest.condense()
- if not chest.getStackInSlot(0) then sort() gotoLevel(9) chest.condense() end
- chest.push(sDir,0,64)
- gotoLevel(10)
- chest.condense()
- chest.pushIntoSlot(sDir,0,64,1) --swop item in slot 1 for fuel and try to push fuel into furnace
- chest.pullIntoSlot(sDir,0,64,0)
- chest.pushIntoSlot("up",0,64,1)
- chest.pushIntoSlot(sDir,0,64,0)
- chest.pullIntoSlot("up",2,64,0) --retrieve forged items, record their destination and place then in the turtle's slot 3
- if chest.getStackInSlot(0) then
- nLevel=findLevel(chest.getStackInSlot(0).name)
- else
- nLevel=false
- end
- chest.pushIntoSlot(sDir,0,64,2)
- chest.pullIntoSlot(sDir,1,64,0) --return item taken for fuel and try to forge it or the requested item
- if sItem then
- for nSlot=0,chest.getSizeInventory()-1 do
- local tStack=chest.getStackInSlot(nSlot)
- if tStack and tStack.name:lower()==sItem:lower() then
- chest.pushIntoSlot("up",nSlot,64,0)
- end
- end
- end
- chest.pushIntoSlot("up",0,64,0)
- end
- do --dump the forged items and leftover fuel
- gotoLevel(9)
- chest.pull(sDir,0,64)
- if nLevel then
- gotoLevel(nLevel)
- chest.pull(sDir,2,64)
- end
- end
- end
- function checkForItems(sItem,nAmount)
- nAmount=nAmount or 1
- local nLevel=findLevel(sItem)
- if not nLevel then error("cannot check for unknown item: "..sItem,2) end
- gotoLevel(nLevel)
- local nFound=0
- for slot=0,chest.getSizeInventory()-1 do
- local tStack=chest.getStackInSlot(slot)
- if tStack and tStack.name:lower()==sItem:lower() then
- nFound=nFound+tStack.qty
- if nFound>=nAmount then
- return true
- end
- end
- end
- return false
- end
- function retrieve(sItem,nAmount,nSlot)
- nAmount=nAmount or 1
- nSlot=nSlot or 1
- local nLevel=findLevel(sItem)
- if not nLevel then error("cannot retrieve unknown item: "..sItem,2) end
- gotoLevel(nLevel)
- local nRetrieved=0
- for slot=0,chest.getSizeInventory()-1 do
- local tStack=chest.getStackInSlot(slot)
- if tStack and tStack.name:lower()==sItem:lower() then
- nRetrieved=nRetrieved+chest.pushIntoSlot(sDir,slot,nAmount-nRetrieved,nSlot-1)
- if nRetrieved>=nAmount then
- return true
- end
- end
- end
- return false
- end
- function craft(sItem,destination,nAmount,bDeposit)
- sItem=sItem:lower()
- if not tRecipes[sItem] then return false,"recipe for '"..sItem.."' not known" end
- if type(destination)=="number" and turtle.getItemCount(destination)>0 then
- return false,"slot "..destination.." is already occupied"
- end
- local bInvOccupied=false
- for i=1,16 do
- if turtle.getItemCount(i)>0 then
- bInvOccupied=true
- break
- end
- end
- local tempStorage
- if bInvOccupied then
- gotoLevel(10)
- tempStorage=peripheral.wrap("top")
- for nSlot=0,15 do
- tempStorage.pullIntoSlot("down",nSlot,64,nSlot)
- end
- end
- local tItemsUsed={}
- local tItemsFound={}
- for k,v in pairs(tRecipes[sItem]) do --check that we have everything we need
- sleep(0)
- if type(k)=="number" and k<=16 then
- if type(v)=="table" then
- local found=false
- for _,item in ipairs(v) do
- if checkForItems(item,(tItemsFound[item] or 0)+1) then
- tItemsUsed[k]=item
- tItemsFound[item]=(tItemsFound[item] or 0)+1
- found=true
- break
- end
- end
- if not found then
- local crafted=false
- for _,item in ipairs(v) do
- if craft(item,nil,nil,true) then
- tItemsUsed[k]=item
- tItemsFound[item]=(tItemsFound[item] or 0)+1
- crafted=true
- break
- end
- end
- if not crafted then
- return false,"cannot craft or find "..table.concat(v," / ")
- end
- end
- else
- if checkForItems(v,(tItemsFound[v] or 0)+1) then
- tItemsFound[v]=(tItemsFound[v] or 0)+1
- tItemsUsed[k]=v
- else
- if not craft(v,nil,nil,true) then
- return false,"cannot craft or find "..v
- end
- tItemsFound[v]=(tItemsFound[v] or 0)+1
- tItemsUsed[k]=v
- end
- end
- end
- end
- sleep(0)
- for k,v in pairs(tItemsUsed) do
- if not retrieve(v,1,k) then
- error("stop fucking with the contents of the chests while I am working please",2)
- end
- end
- turtle.craft()
- if type(nAmount)=="number" and turtle.getItemCount(1)>nAmount then
- gotoLevel(findLevel(sItem))
- chest.pull(sDir,0,turtle.getItemCount(1)-nAmount)
- end
- if type(destination)=="number" then
- turtle.select(1)
- turtle.transferTo(destination)
- end
- if bDeposit then
- dump(sItem,destination or 1)
- end
- if bInvOccupied then
- gotoLevel(10)
- for nSlot=0,15 do
- tempStorage.pushIntoSlot("down",nSlot,64,nSlot)
- end
- end
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment