This repository has been archived on 2025-07-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
computercraft-scripts/mower.lua
2016-08-30 05:27:32 +03:00

85 lines
1.9 KiB
Lua

--[[
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 <gkaklas1@gmail.com>
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