Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local role = ...
- if not role or (role~="kill" and role~="sort" and role~="craft") then
- error(string.format([[Usage:
- %s [kill|sort|craft] ]],shell.getRunningProgram()),-1)
- return
- end
- local function dots(n)
- return string.rep(".",math.floor(os.clock()*10)%n)
- end
- local function echo(str,line)
- term.setCursorPos(1,line)
- term.clearLine()
- term.write(str)
- end
- local blink
- local function runningTime()
- blink = not blink
- echo("running: "..textutils.formatTime(os.clock()/3600,true)..(blink and "." or " "),2)
- end
- local function select(slot)
- if turtle.getSelectedSlot() ~= slot then
- turtle.select(slot)
- end
- end
- --
- -- GRINDER
- --
- local function grinder_step()
- echo("Attack!",3)
- local getSomething = false
- while turtle.attack() do
- getSomething = true
- end
- local slot=1
- if getSomething then
- echo("Manage items",3)
- while slot<=16 do
- if turtle.getItemCount(slot)>0 then
- select(slot)
- turtle.dropDown(64)
- end
- slot=slot+1
- end
- else
- echo("Nothing"..dots(5),3)
- end
- end
- local function grinder_main()
- local timer = os.startTimer(.2)
- local continue = true
- while continue do
- local e,k = os.pullEvent()
- if e=="key" and k==keys.t then
- break
- elseif e=="timer" then
- grinder_step()
- timer = os.startTimer(.2)
- runningTime()
- end
- end
- end
- --
- -- TRIEUR
- --
- local function sort_faceInputChest()
- local test,data = turtle.inspect()
- if not test then
- turtle.turnRight()
- elseif data.name~="minecraft:chest" or data.state.facing~="west"then
- if data.name=="minecraft:chest" then
- turtle.turnLeft()
- end
- turtle.turnLeft()
- end
- end
- local function sort_faceOutputChest()
- local test,data = turtle.inspect()
- if not test then
- turtle.turnLeft()
- elseif data.name~="minecraft:trapped_chest" then
- if data.name=="minecraft:chest" then
- turtle.turnRight()
- end
- turtle.turnRight()
- end
- end
- local function sort_hasBowToRepair()
- local count,space = turtle.getItemCount(2),turtle.getItemSpace(2)
- if count==1 and space==0 then return true
- elseif count==0 then return false
- else
- sort_faceOutputChest()
- select(2)
- turtle.drop(64)
- turtle.select(1)
- return false
- end
- end
- local function sort_suck()
- sort_faceInputChest()
- select(1)
- return turtle.suck(64)
- end
- local function sort_manageItem()
- select(1)
- local data = turtle.getItemDetail(1)
- if not data then
- echo("Manage item : nothing",3)
- return
- elseif data.name=="minecraft:bone"
- or data.name=="minecraft:arrow" then
- echo("Manage item : deposit mainstream",3)
- sort_faceOutputChest()
- turtle.drop(64)
- elseif data.name=="minecraft:bow" then
- if data.damage > 0 then
- if sort_hasBowToRepair() then
- echo("Manage item : attempt to repair",3)
- turtle.craft()
- sort_manageItem()
- else
- echo("Manage item : keep scrap aside",3)
- turtle.transferTo(2)
- end
- else
- echo("Manage item : stock bow",3)
- turtle.dropDown()
- end
- else
- echo("Manage item : stock "..data.name,3)
- turtle.dropDown()
- end
- end
- local function sort_step()
- if turtle.getItemCount(1)>0 then
- echo("Item management",3)
- sort_manageItem()
- else
- echo("Suck item",3)
- if not sort_suck() then
- echo("Nothing"..dots(5),3)
- sleep(.5)
- end
- end
- end
- local function sort_main()
- local timer = os.startTimer(.2)
- local continue,blink = true,false
- while continue do
- local e,k = os.pullEvent()
- if e=="key" and k==keys.t then
- print("Terminated")
- break
- elseif e=="timer" then
- sort_step()
- runningTime()
- timer = os.startTimer(.2)
- end
- end
- end
- --
- -- CRAFT (dispenser)
- --
- function craft_isBowInputChest(data)
- return data
- and data.name=="minecraft:chest"
- and data.state.facing=="south"
- end
- function craft_isRedstoneInputChest(data)
- return data
- and data.name=="minecraft:chest"
- and data.state.facing=="west"
- end
- function craft_isOuputChest(data)
- return data
- and data.name=="minecraft:trapped_chest"
- and data.state.facing=="west"
- end
- function craft_isTrashSide(data)
- return data
- and data.name=="minecraft:wall_sign"
- end
- function craft_faceBowChest()
- echo("Facing Bow chest",3)
- local test,data = turtle.inspect()
- if craft_isBowInputChest(data) then return true
- elseif craft_isOuputChest(data) then return turtle.turnRight()
- elseif craft_isRedstoneInputChest(data) then turtle.turnLeft()
- end
- return turtle.turnLeft()
- end
- function craft_faceRedstoneChest()
- echo("Facing redstone chest",3)
- local test,data = turtle.inspect()
- if craft_isRedstoneInputChest(data) then return true
- elseif craft_isTrashSide(data) then return turtle.turnRight()
- elseif craft_isBowInputChest(data) then turtle.turnLeft()
- end
- return turtle.turnLeft()
- end
- function craft_faceOutputChest()
- echo("Facing output chest",3)
- local test,data = turtle.inspect()
- if craft_isOuputChest(data) then return true
- elseif craft_isRedstoneInputChest(data) then return turtle.turnRight()
- elseif craft_isTrashSide(data) then turtle.turnLeft()
- end
- return turtle.turnLeft()
- end
- function craft_faceTrashSide()
- echo("Facing trash side",3)
- local test,data = turtle.inspect()
- if craft_isTrashSide(data) then return true
- elseif craft_isBowInputChest(data) then return turtle.turnRight()
- elseif craft_isOuputChest(data) then turtle.turnLeft()
- end
- return turtle.turnLeft()
- end
- local function craft_restockRedstone(slot)
- echo("Restock redstone",3)
- craft_faceRedstoneChest()
- turtle.select(slot)
- turtle.drop(64)
- turtle.transferTo(10)
- end
- local function craft_trash(slot)
- echo("Trash",3)
- craft_faceTrashSide()
- turtle.select(slot)
- turtle.drop(64)
- end
- local function craft_fillRedstoneSlot()
- echo("Fill redstone slot",3)
- local data = turtle.getItemDetail(10)
- if data and data.name~="minecraft:redstone" then
- craft_trash(10)
- end
- local count = turtle.getItemSpace(10)
- if count>0 then
- craft_faceRedstoneChest()
- turtle.select(10)
- turtle.suck(count)
- end
- end
- local function craft_fillCobblestone()
- echo("Fill cobblestone slots",3)
- local slots,s,i = {1,2,3,5,7,9,11}
- for i,s in ipairs(slots) do
- local data = turtle.getItemDetail(s)
- if data and data.name~="minecraft:cobblestone" then
- if data.name=="redstone" then
- craft_restockRedstone(s)
- end
- if turtle.getItemCount(s)>0 then
- craft_trash(s)
- end
- end
- turtle.select(s)
- while turtle.getItemCount(s)<=0 do
- if turtle.detectDown() and not turtle.digDown() then
- sleep(.3)
- end
- sleep(.1)
- end
- end
- end
- local function craft_fastCountCobblestone()
- if turtle.getItemSpace(1)>0 then return 1
- elseif turtle.getItemSpace(2)>0 then return 2
- elseif turtle.getItemSpace(3)>0 then return 3
- elseif turtle.getItemSpace(5)>0 then return 5
- elseif turtle.getItemSpace(7)>0 then return 7
- elseif turtle.getItemSpace(9)>0 then return 9
- elseif turtle.getItemSpace(11)>0 then return 11
- else return false end
- end
- local slowFillCobblestoneCount = 0
- local function slowcraft_fillCobblestone()
- echo("Slow fill cobblestone slots",3)
- local slots = {1,2,3,5,7,9,11}
- local s = slots[(slowFillCobblestoneCount%#slots)+1]
- slowFillCobblestoneCount = slowFillCobblestoneCount+1>=#slots and 0 or slowFillCobblestoneCount + 1
- local data = turtle.getItemDetail(s)
- if data and data.name~="minecraft:cobblestone" then
- if data.name=="redstone" then
- craft_restockRedstone(s)
- end
- if turtle.getItemCount(s)>0 then
- craft_trash(s)
- end
- end
- turtle.select(s)
- while not turtle.detectDown() do
- echo("Waiting cobblestone generator",3)
- sleep(.1)
- end
- if turtle.getItemSpace(s)>0 then
- turtle.digDown()
- end
- end
- local function craft_fillBow()
- echo("Fill bow slot",3)
- local data = turtle.getItemDetail(6)
- if data and (data.name~="minecraft:bow" or data.damage>0) then
- if data.name=="redstone" then
- craft_restockRedstone(6)
- end
- if turtle.getItemCount(6)>0 then
- craft_trash(6)
- end
- end
- craft_faceBowChest()
- turtle.select(6)
- local continue = true
- while continue do
- if not turtle.suck(1) then
- term.setCursorPos(1,3)
- term.clearLine()
- print("No more bows")
- return false
- end
- local data = turtle.getItemDetail(6)
- if data.name ~= "minecraft:bow" or (data.name=="minecraft:bow" and data.damage>0) then
- craft_trash(6)
- craft_faceBowChest()
- turtle.select(6)
- sleep(.3)
- else return true
- end
- sleep(.1)
- end
- return false
- end
- local function confirmCraft()
- local rsSlot = turtle.getItemDetail(10)
- local bowSlot = turtle.getItemDetail(6)
- local confirm = turtle.craft(0)
- and bowSlot
- and rsSlot.name=="minecraft:redstone"
- and bowSlot.name=="minecraft:bow"
- and bowSlot.damage==0
- echo(confirm and "Craft recipe confirm" or "Craft : missing items",3)
- return confirm
- end
- local function rsInput()
- echo("RS check",3)
- return rs.getInput("left")
- or rs.getInput("right")
- or rs.getInput("front")
- or rs.getInput("back")
- end
- local function craft_step()
- echo("Process",3)
- craft_fillRedstoneSlot()
- craft_fillCobblestone()
- if not craft_fillBow() then
- return
- end
- turtle.select(6)
- while confirmCraft() and turtle.craft(1) do
- craft_faceOutputChest()
- turtle.select(6)
- turtle.drop(64)
- if not craft_fillBow() then
- return
- end
- end
- end
- local function craft_main()
- local timer = os.startTimer(.5)
- local continue = true
- while continue do
- local e,k = os.pullEvent()
- if e=="key" and k==keys.t then
- print("Terminated")
- continue=false
- break
- elseif e=="redstone" and rsInput() then
- while rsInput() do
- sleep(.1)
- end
- craft_step()
- timer = os.startTimer(.5)
- elseif e=="timer" then
- if craft_fastCountCobblestone() then
- slowcraft_fillCobblestone()
- end
- timer = os.startTimer(.5)
- runningTime()
- end
- end
- end
- --
- -- MAIN
- --
- term.clear()
- echo(string.format("#%d %s - Press [T]",os.getComputerID(),os.getComputerLabel()),1)
- if role=="kill" then
- rs.setOutput("top",true)
- grinder_main()
- rs.setOutput("top",false)
- elseif role=="sort" then
- sort_main()
- elseif role=="craft" then
- craft_main()
- end
- term.clearLine()
- print("Terminated")
- sleep(.5)
Advertisement
Add Comment
Please, Sign In to add comment