diff --git a/mower.lua b/mower.lua new file mode 100644 index 0000000..bb1b4d3 --- /dev/null +++ b/mower.lua @@ -0,0 +1,85 @@ +--[[ +This script will make a turtle into a mower! +It will drive in a zigzag pattern in the field and remove weeds. +You can choose which objects it will destroy by changing the +appropriate string below. In order to dig, you also need to place a tool +in slot 1 of the turtle. + +Copyright 2016, George Kaklamanos +You may use this script under the terms of the +GNU General Public License version 3 or later. +For more info please read the LICENSE, which is included in this repository. +--]] + + +-- Select the item in slot 1 and equip the right hand side with it. +turtle.select(1) +turtle.equipRight(1) + +goRight = true +while true do + success = moveAndDestroy() + if not success then + changeRow() + end +end + +-- Is the block in front of us of the kind we want to destroy? +function wantedBlock() + local s,data = turtle.inspect() + return data["name"] == "minecraft:yellow_flower" +end + +-- Move forward one block, dig if necessary. +function moveAndDestroy() + local success + if wantedBlock() then + turtle.dig() + end + if not turtle.detect() then + success = turtle.forward() + end + return success +end + +function turn() + if goRight then + turtle.turnRight() + else + turtle.turnLeft() + end +end + +function antiturn() + if goRight then + turtle.turnLeft() + else + turtle.turnRight() + end +end + + +-- Move to the next row of the zigzag pattern. +function changeRow() + local success + -- Turn towards the right direction + turn() + -- If something is blocking the way, go around it. + while not moveAndDestroy() do + turn() + moveAndDestroy() + antiturn() + end + -- Now we are in the next row, let's go to the beginning of it. + antiturn() + repeat + success = moveAndDestroy() + until not success + + -- Turn around, towards the end of the row. + turn() + turn() + + goRight = not goRight +end +