Skip to content

Instantly share code, notes, and snippets.

@znepb
Last active January 16, 2023 17:21
Show Gist options
  • Save znepb/b373e1d3806ce747a059fb8ca5379924 to your computer and use it in GitHub Desktop.
Save znepb/b373e1d3806ce747a059fb8ca5379924 to your computer and use it in GitHub Desktop.
--- Setup
-- This program requires Tuttle: https://gist.github.com/znepb/72981ce3e7a2e1f4e3423ed0b2ca9510
-- The turtle's home should have a chest below (the output chest) and a chest above (the fuel chest).
-- As well as this, there should be NO OBSTACLES within the farm's area. They will be destroyed over time as the farm works.
--- Configuration Section
-- The block to search for and dig
local block = "minecraft:wheat"
-- The item to deposit into chests
local item = "minecraft:wheat"
-- The seed item. This can be commented out for melon & pumpkins.
local seed = "minecraft:wheat_seeds"
-- The age of the crop when it is ready to be harvested. This can be commented out for pumpkins.
local age = 7
-- The home position of the turtle
local hX, hY, hZ
-- How many items have to be in the inventory to go home.
-- If the amount of seeds is large than homeThreshold + 16, then the turtle will return home.
local homeThreshold = 48
-- When the amount of fuel in the turtle is less than this, it will return home and refuel.
local fuelThreshold = 2048
--[[
Copyright 2023 Marcus Wenzel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to
do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local tuttle = require("tuttle")
local items = tuttle.scan()
local function countItems(item)
local count = 0
for i = 1, 16 do
if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == item then
count = count + turtle.getItemCount(i)
end
end
return count
end
print("Starting up")
while true do
local blocks = tuttle.scan()
local x, y, z = gps.locate()
for i, v in pairs(blocks) do
if v.y == -1 and v.name == block then
if age and v.state and v.state.age == age or age == nil then
tuttle.goToCoordinates(x + v.x, y, z + v.z, true)
turtle.select(3)
local exists, data = turtle.inspectDown()
if exists and data.name == block then
tuttle.dig("down")
if seed then
tuttle.select(seed)
turtle.placeDown()
end
end
end
end
end
if countItems(item) > homeThreshold or countItems(seed) > homeThreshold + 24 or turtle.getFuelLevel() < fuelThreshold then
tuttle.goToCoordinates(hX + 1, hY, hZ)
tuttle.goToCoordinates(hX, hY, hZ)
while tuttle.select(item) do turtle.dropDown() end
local needToDrop = countItems(seed) - homeThreshold
local attempts = 0
if needToDrop > 0 then
print("Dropping seeds")
repeat
local _, i = tuttle.select(seed)
if i then
local count = turtle.getItemCount(i)
local amount = math.min(count, needToDrop)
turtle.dropDown(amount)
needToDrop = needToDrop - amount
else
break
end
attempts = attempts + 1
until needToDrop <= 0 or attempts >= 17
end
if turtle.getFuelLevel() < fuelThreshold then
local slot = 0
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
turtle.select(i)
turtle.suckUp()
turtle.refuel()
print("Refueled!")
break
end
end
end
turtle.back()
end
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment