Skip to content

Instantly share code, notes, and snippets.

@BryanHaley
Last active July 3, 2021 04:10
Show Gist options
  • Save BryanHaley/8cd822914f5e33b7ff1372d8150ca633 to your computer and use it in GitHub Desktop.
Save BryanHaley/8cd822914f5e33b7ff1372d8150ca633 to your computer and use it in GitHub Desktop.
COMPUTERCRAFT: Kelp Farming Turtle
-- Globals
FARM_X_SIZE = 14
FARM_Y_SIZE = 13
NUM_SLOTS = 16
-- Utils
function harvest_kelp()
turtle.dig()
turtle.suck()
turtle.forward()
turtle.suckUp()
turtle.suckDown()
turtle.placeDown()
end
function empty_inventory()
for i = 1, NUM_SLOTS, 1 do
turtle.select(i)
turtle.dropDown()
end
end
-- States
function farm()
print("CURRENT STATE: FARM")
-- Move off of platfrom onto farm
turtle.forward()
-- Loop through full X by Y size of farm
for x = 1, FARM_X_SIZE, 1 do
for y = 1, FARM_Y_SIZE-1, 1 do
harvest_kelp()
end
if (x%2) == 0 and x < FARM_X_SIZE then
turtle.turnLeft()
harvest_kelp()
turtle.turnLeft()
elseif x < FARM_X_SIZE then
turtle.turnRight()
harvest_kelp()
turtle.turnRight()
end
end
end
function get_floaters()
print("CURRENT STATE: GET_FLOATERS")
-- Attempt to recover kelp that floated to the top
turtle.turnRight()
turtle.turnRight()
turtle.up()
for x = 1, FARM_X_SIZE, 1 do
for y = 1, FARM_Y_SIZE-1, 1 do
turtle.dig()
turtle.forward()
turtle.suck()
turtle.suckUp()
end
if (x%2) == 0 and x < FARM_X_SIZE then
turtle.turnRight()
turtle.forward()
turtle.suck()
turtle.suckUp()
turtle.turnRight()
elseif x <= FARM_X_SIZE then
turtle.turnLeft()
turtle.forward()
turtle.suck()
turtle.suckUp()
turtle.turnLeft()
end
end
end
function go_to_kelp_chest()
print("CURRENT STATE: GO_TO_KELP_CHEST")
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnRight()
end
function deposit_kelp()
print("CURRENT STATE: DEPOSIT_KELP")
-- Loop through inventory and find potatoes
for i = 1, NUM_SLOTS, 1 do
x = turtle.getItemDetail(i)
turtle.select(i)
if x ~= nil and x['name'] == "minecraft:kelp" then
turtle.place()
end
end
end
function retrieve_dried_kelp()
print("CURRENT STATE: RETRIEVE_DRIED_KELP")
empty_inventory()
-- Get over chest
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.down()
turtle.down()
-- Wait until furnace is no longer lit
turtle.turnLeft()
furnace = turtle.inspect()
while (furnace['state']['lit'] == true) do
os.sleep(10)
furnace = turtle.inspect()
end
-- Fill the 3x3 top left square of inventory
sequence = {1,2,3,5,6,7,9,10,11}
index = 1
turtle.select(1)
while (turtle.suckDown(1)) do
index = index + 1
if (index > 9) then index = 1 end
turtle.select(sequence[index])
end
end
function craft_blocks()
print("CURRENT STATE: CRAFT_BLOCKS")
turtle.turnLeft()
turtle.craft()
-- Drop anything that isn't a dried kelp block
for i = 1, NUM_SLOTS, 1 do
x = turtle.getItemDetail(i)
turtle.select(i)
if x ~= nil and x['name'] ~= "minecraft:dried_kelp_block" then
turtle.drop()
end
end
end
function refuel()
print("CURRENT STATE: REFUEL")
while (turtle.getFuelLevel() < 1000) do
turtle.refuel(1)
end
end
function go_to_fuel_chest()
print("CURRENT STATE: GO_TO_FUEL_CHEST")
turtle.up()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.turnRight()
end
function place_fuel()
print("CURRENT STATE: PLACE_FUEL")
for i = 1, NUM_SLOTS, 1 do
x = turtle.getItemDetail(i)
turtle.select(i)
if x ~= nil and x['name'] == "minecraft:dried_kelp_block" then
turtle.place()
end
end
empty_inventory()
end
function return_to_home()
print("CURRENT STATE: RETURN_TO_HOME")
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.down()
turtle.turnLeft()
turtle.turnLeft()
end
while (true) do
farm()
get_floaters()
go_to_kelp_chest()
deposit_kelp()
retrieve_dried_kelp()
craft_blocks()
refuel()
go_to_fuel_chest()
place_fuel()
return_to_home()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment