Skip to content

Instantly share code, notes, and snippets.

@arav97531
Last active March 2, 2019 19:47
Show Gist options
  • Select an option

  • Save arav97531/8d07a81f791932e2cc1b130df9972648 to your computer and use it in GitHub Desktop.

Select an option

Save arav97531/8d07a81f791932e2cc1b130df9972648 to your computer and use it in GitHub Desktop.
-- Program for Farming Turtle from ComputerCraft mod.
-- AutoFarm Ver 1.0
-- Copyright (c) 2015 Arav <[email protected]>
-- Farming 9x9 field
-- I S x x x x x x x x
-- x x x x x x x x x
-- x x x x x x x x x
-- x x x x x x x x x
-- x x x x W x x x x
-- x x x x x x x x x
-- x x x x x x x x x
-- x x x x x x x x x
-- x x x x x x x x x
-- S - start position
-- I - possible inventory with which interacts turtle in the end (do_end_stuff)
-- W - water block in center
-- The turtle moves clockwise from S to W by spiral,
-- moves back to the S, drop stuff to I and stops.
-- The turtle must be in one block above the field.
-- MAIN PROGRAM
args = {...}
chest_name_part = 'Chest' -- a part of name of the I chest
rings = {9,7,5,3}
fuel_slot = 16
function _refuel(fuel)
turtle.select(fuel_slot)
turtle.refuel(fuel)
end
function process_field(do_stuff, do_end_stuff)
for i = 1, #rings do
local n = rings[i]
for j = 1, 4 do
if j == 4 then n = n - 1 end
for k = 1, n-1 do
do_stuff()
turtle.forward()
end
turtle.turnRight()
end
do_stuff()
turtle.forward()
end
-- moving back on start position
turtle.turnLeft()
for i = 1, 4 do turtle.forward() end
turtle.turnLeft()
for i = 1, 4 do turtle.forward() end
do_end_stuff()
turtle.turnLeft()
turtle.turnLeft()
end
-- PLANTING
plant_slots = {1,2,3,4}
function plant()
for i = 1, #plant_slots do
turtle.select(plant_slots[i])
if turtle.getItemCount(plant_slots[i]) > 0 then
turtle.placeDown()
return
end
end
end
function planting()
local s,_ = turtle.inspectDown()
if s == false then
turtle.digDown()
plant()
end
end
function on_end_planting()
end
-- HARVESTING
harvest_slots = {5,6,7,8}
wheat = {name = 'minecraft:wheat', metadata = 7} -- growed up wheat
function harvest(object)
local _,m = turtle.inspectDown()
if m.name ~= object.name or m.metadata ~= object.metadata then return end
for i = 1, #harvest_slots do
turtle.select(harvest_slots[i])
if turtle.getItemSpace(harvest_slots[i]) > 0 then
turtle.digDown()
return
end
end
end
function harvesting()
harvest(wheat)
end
function on_end_harvesting()
local _,m = turtle.inspect()
if m.name:find(chest_name_part) == nil then return end
for i = 1, #harvest_slots do
turtle.select(harvest_slots[i])
turtle.drop()
end
end
-- HARVEST AND PLANT
function harvest_and_plant( ... )
harvesting()
planting()
end
-- Controlling program
if args[1] == 'p' then
_refuel(2)
process_field(planting, on_end_planting)
elseif args[1] == 'h' then
_refuel(2)
process_field(harvesting, on_end_harvesting)
elseif args[1] == 'ph' then
_refuel(2)
process_field(harvest_and_plant, on_end_harvesting)
else
print('p - for planting')
print('h - for harvest')
print('ph - for harvest and plant')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment