Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Shearman 2.1
- -- by MCKerrnel
- --
- -- A ComputerCraft program to shear
- -- sheep. This version includes
- -- reloading shears when they break.
- -- How much fuel it takes to
- -- complete one cycle.
- local fuelCycle = 54
- function goForward(blocks)
- for count = 1, blocks, 1 do
- turtle.forward()
- turtle.suck()
- end
- end
- function turn(direction)
- if direction == "left" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- -- A quick function to convert a decimal
- -- value into an integer. Dunno why
- -- LUA has to be so difficult with this.
- -- Should be a built-in function!
- function toInt(decimalNum)
- local D2I = string.format("%i", decimalNum)
- return D2I
- end
- function shearCycle(direction, needShears)
- if direction == "left" then
- returndir = "right"
- else
- returndir = "left"
- end
- -- Chest to Start of Row
- turn(direction)
- goForward(3)
- turn(direction)
- -- Shear Row
- for count = 1, 8, 1 do
- needShears = shearPen(direction, needShears)
- end
- -- Return to Chest
- turtle.turnLeft()
- turtle.turnLeft()
- goForward(23)
- turn(returndir)
- goForward(3)
- turn(returndir)
- -- Unload
- if needShears then
- startSlot = 1
- else
- startSlot = 2
- end
- for slot = startSlot, 16, 1 do
- turtle.select(slot)
- turtle.drop()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- return needShears
- end
- -- Shear the sheep in a 2x1 pen.
- function shearPen(direction, needShears)
- turtle.select(1)
- for count = 1, 2, 1 do
- turtle.forward()
- turn(direction)
- -- If we still have shears
- -- then shear and reset
- -- the check status.
- -- If no shears, then
- -- don't try to shear.
- if needShears == false then
- turtle.place()
- needShears = needShearsCheck()
- end
- os.sleep(2)
- turtle.suck()
- if direction == "left" then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- turtle.forward()
- return needShears
- end
- -- Check to see if the shears
- -- are broken.
- function needShearsCheck()
- -- Check the # of items
- -- in the shears slot
- -- that are needed to
- -- complete a full stack.
- -- 0=Full stack
- -- 64=No item
- -- 1-63=Wool
- if turtle.getItemSpace(1) == 0 then
- return false
- else
- return true
- end
- end
- -- Check to see if we have
- -- enough fuel for a cycle.
- function needFuelCheck()
- if turtle.getFuelLevel() - fuelCycle < 0 then
- return true
- else
- return false
- end
- end
- -- Reload shears from the chest
- -- below. If able to reload then
- -- send back false to reset the
- -- needShears variable, otherwise
- -- return true for failure and
- -- still needing shears. The
- -- turtle inventory should be empty
- -- when it attempts to load items.
- function reloadShears(needShears)
- if needShears then
- turtle.select(1)
- -- If we still need shears
- -- then alert console.
- if turtle.suckDown() == false then
- print "NO SHEARS: Reload the SHEARS chest."
- else
- needShears = false
- end
- end
- return needShears
- end
- function refuel(needFuel)
- -- Refuel. This is a regular turtle
- -- so can only hold 20,000 units.
- if needFuel then
- turtle.select(2)
- -- Try to fill up, but if not enough to
- -- fill up, that's ok.
- while needFuel == true or turtle.getFuelLevel < 20000 do
- -- If there's nothing to grab, error out.
- if turtle.suckUp() == false then
- break
- end
- turtle.refuel()
- needFuel = needFuelCheck()
- end
- -- If we still need fuel
- -- then alert console.
- if needFuel then
- print "NO FUEL: Reload the FUEL chest."
- end
- end
- return needFuel
- end
- -- Not used in this version...wanted to
- -- document it to have it for 3.0
- function toTerm(column, row, text)
- -- Terminal size = 39 x 13
- setCursorPos(column, row)
- term.write(text)
- end
- -- MAINLINE
- -- Check to see if we need shears.
- local needShears = needShearsCheck()
- local needFuel = needFuelCheck()
- local fuelCycles = turtle.getFuelLevel() / fuelCycle
- print("Enough fuel to shear ", toInt(fuelCycles), " rows.")
- -- This is where it all happens.
- while true do
- -- Reload shears, if needed.
- needShears = reloadShears(needShears)
- -- Refuel.
- needFuel = refuel(needFuel)
- -- Shear North Row
- print "CYCLE: North Row"
- if needShears == false and needFuel == false then
- needShears = shearCycle("left", needShears)
- end
- -- Reload shears, if needed.
- needShears = reloadShears(needShears)
- -- Shear South Row
- print "CYCLE: South Row"
- if needShears == false and needFuel == false then
- needShears = shearCycle("right", needShears)
- end
- -- Wait 2 minutes to allow
- -- sheep to regrow wool.
- print "CYCLE: Sleep 2 min"
- os.sleep(120)
- end
Advertisement
Add Comment
Please, Sign In to add comment