Skip to content

Instantly share code, notes, and snippets.

@BryanHaley
Last active July 10, 2021 19:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save BryanHaley/799751f3834f0938df5cf13468d70216 to your computer and use it in GitHub Desktop.
COMPUTERCRAFT: Bamboo Farming Turtle
START_X = 109
START_Y = 61
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:bamboo" 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()
succ()
turtle.dig()
succ()
turtle.forward()
succ()
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 (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 (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()
turtle.select(1)
turtle.refuel()
turtle.select(2)
turtle.refuel()
for i = 1, NUM_SLOTS, 1 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
end
function refuel_extras()
for i = 1, NUM_SLOTS, 1 do
turtle.select(i)
turtle.refuel()
end
turtle.select(1)
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
while true do
find_dir()
if not is_on_farm() then
move_to_x(START_X)
move_to_z(START_Z)
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()
refuel_extras()
drop_remaining()
os.sleep(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment