Skip to content

Instantly share code, notes, and snippets.

@BryanHaley
Last active July 13, 2021 03:02
Show Gist options
  • Save BryanHaley/5932aa58f8a604408013199056fc2a7a to your computer and use it in GitHub Desktop.
Save BryanHaley/5932aa58f8a604408013199056fc2a7a to your computer and use it in GitHub Desktop.
COMPUTERCRAFT: Potato Farmer
START_X = 109
START_Y = 57
START_Z = 769
FARM_X_END_EVEN = 108
FARM_X_END_ODD = 97
FARM_Z_START = 769
FARM_Z_END = 782
NUM_SLOTS = 16
current_dir = 0
dirs = { "XNEG", "ZNEG", "XPOS", "ZPOS" }
function find_dir()
local x,y,z = gps.locate()
for i = 1, 4, 1 do
found_block, data = turtle.inspect()
if not found_block or data["name"] == "minecraft:potato" then
forward()
break
else
turtle.turnLeft()
end
end
local nx,ny,nz = gps.locate()
if (nx > x) then
current_dir = 3
elseif (nx < x) then
current_dir = 1
elseif (nz > z) then
current_dir = 4
elseif (nz < z) then
current_dir = 2
else
-- try again
find_dir()
end
end
function is_col_odd()
if find_col() % 2 == 0 then return false end
return true
end
function turn_left()
local success = turtle.turnLeft()
if success then
current_dir = current_dir - 1
if (current_dir < 1) then current_dir = 4 end
end
return success
end
function turn_right()
local success = turtle.turnRight()
if success then
current_dir = current_dir + 1
if (current_dir > 4) then current_dir = 1 end
end
return success
end
function forward()
turtle.forward()
found_block, data = turtle.inspectDown()
if found_block and data["name"] == "minecraft:potatoes" and data["state"]["age"] >= 7 then
turtle.digDown()
turtle.placeDown()
end
end
function succ()
turtle.suckUp()
turtle.suck()
turtle.suckDown()
end
function get_dir()
return dirs[current_dir]
end
function turn_to_face(dir)
for i = 0, 4, 1 do
if (current_dir == dir) then return end
turn_left()
end
end
function find_col()
x,y,z = gps.locate()
return z - FARM_Z_START + 1
end
function is_last_row()
local col = find_col()
if (col == FARM_Z_END - FARM_Z_START + 1) then
return true
end
return false
end
function move_to_x(nx)
local x,y,z = gps.locate()
if (x == nx) then return end
if (nx > x) then
turn_to_face(3)
else
turn_to_face(1)
end
for i = 0, math.abs(nx-x)-1, 1 do
forward()
end
end
function move_to_z(nz)
local x,y,z = gps.locate()
if (z == nz) then return end
if (nz > z) then
turn_to_face(4)
else
turn_to_face(2)
end
for i = 0, math.abs(nz-z)-1, 1 do
forward()
end
end
function deposit()
for i = 1, NUM_SLOTS, 1 do
turtle.select(i)
local data = turtle.getItemDetail()
if data and data["name"] == "minecraft:potato" then
turtle.drop()
end
end
turtle.select(1)
end
function refuel()
while turtle.getFuelLevel() < 1000 do
turtle.select(1)
turtle.suckDown()
turtle.refuel()
end
end
function drop_remaining()
for i = 1, NUM_SLOTS, 1 do
turtle.select(i)
turtle.dropDown()
end
turtle.select(1)
end
function is_on_farm()
x,y,z = gps.locate()
if x >= FARM_X_END_ODD and x <= FARM_X_END_EVEN and z >= FARM_Z_START and z <= FARM_Z_END then
return true
end
return false
end
function empty_inventory()
for i = 1, 16, 1 do
turtle.select(i)
turtle.refuel()
turtle.dropUp()
end
turtle.select(1)
end
while true do
find_dir()
if not is_on_farm() then
move_to_x(START_X)
move_to_z(START_Z)
refuel()
empty_inventory()
os.sleep(400)
end
while not is_last_row() do
if is_col_odd() then
move_to_x(FARM_X_END_ODD)
else
move_to_x(FARM_X_END_EVEN)
end
turn_to_face(4)
forward()
end
move_to_x(FARM_X_END_EVEN+1)
deposit()
drop_remaining()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment