--[[
WheatFarm.lua by SReject
2014 All Rights Reserved
Purpose:
Instructs a turtle to plant seeds, bone meal them, then harvest the wheat repeatedly
If required resources are missing(Seeds/bonemeal/improper soil) the program waits until they are available
Setup:
Inventory directly below the turtle for refilling bone meal
Inventory directly above the turtle for wheat and extra seeds
Watered & Tilled soil directly infront of but lowered one block from the turtle
Starting
Place a stack of seeds in first slot
Place a stack of bone meal in second slot
At the turtle's CC prompt type: WheatFarm.lua
]]--
-- Empties the turtles inventory slots 3 through 16
function emptyInventory(forceEmpty)
-- Arguments:
-- forceEmpty:
-- Makes the turtle empty items in invo slots 3 through 16, reguardless if there's an item in slot 16
-- Check if there is an item in the 16th slot or 'forceEmpty' has been specified
if (turtle.getItemCount(16) > 0 or forceEmpty == true) then
-- If so, call the refillSeeds function (w/ the forceRefill argument being true)
refillSeeds(true)
-- Loop through inventory slots 3 through 16 and place items in the chest above
for n = 3, 16, 1 do
turtle.select(n)
turtle.dropUp()
end
-- If there's not an item in the 16th slot and 'forceEmpty' Isnt true
else
-- Refill seeds (w/ the forceRefill argument being false)
refillSeeds(false)
end
end
-- Refills first slot with seeds from inventory slots 3 to 16 as needed:
-- Before the turtle empties its inventory
-- When the turtle has less than 16 seeds in the first slot
function refillSeeds(forceRefill)
-- Arguments:
-- forceRefill:
-- Refills seeds in slot #1 with seeds from slots 3 through 16 regardless of the amount of seeds in slot #1
-- Variables:
-- seedCount: current item count of slot #1(seeds)
-- refillCount: Number of seeds required to fill slot #1 to 64 seeds
local seedCount, refillCount = turtle.getItemCount(1), 0
refillCount = 64 - seedCount
-- Check if slot #1(seedCount) is empty
if (seedCount == 0) then
-- if slot #1(seedCount) is empty, print we need seeds
print("Out of seeds: place seeds in first slot to continue")
-- wait 1 second
repeat
sleep(1)
-- check if slot one is NOT empty, if it still is loop back
until (turtle.getItemCount(1) > 0)
-- If we have seeds in slot#1, and either forceRefill is true and we have less than 64 seeds in slot one OR we have less than 16 seeds in slot #1
elseif (forceRefill == true and seedCount < 64) or (seedCount < 16) then
-- Select the first slot for comparing to other slots that may contain seeds
turtle.select(1)
-- Loop over slots 3 through 16
for n = 3, 16, 1 do
-- See if the selected slot(#1) is the same item as the one we have looped to
if (turtle.compareTo(n) == true) then
-- select the looped to slot
turtle.select(n)
-- Check if the selected slot DOESN'T have enough seeds to refill slot one
if (refillCount > turtle.getItemCount(n)) then
-- transfer items(seeds) from the selected slot to slot one
turtle.transferTo(1, turtle.getItemCount(n))
-- adjust refillCount to account for the transfer seeds
refillCount = 64 - turtle.getItemCount(1)
-- If the selected slot will fill fill slot#1 to 64 items(seeds)
else
-- transfer just enough items to fill slot#1 with seeds
turtle.transferTo(1, refillCount)
-- exit the loop
break
end
-- reset the selected slot to slot#1 for the next literation of the loop
turtle.select(1)
end
end
end
end
-- Retrives Bonemeal from an inventory below, waits if none is available
function getBoneMeal()
-- Arguments: none
-- Variables: none
-- select slot #2(Bone meal Slot)
turtle.select(2)
-- check to see if the turtle has less than or equal to 8 items in slot#2
if (turtle.getItemCount(2) <= 8) then
-- If an attempt to suck items(bone meal) from an inventory below the turtle fails
if (turtle.suckDown() == false) then
-- print the turtle is out of bone meal
print("Out of bonemeal, waiting...")
-- wait 1 second
repeat
sleep(1)
-- continue looping back until the turtle is able to suck in items from the inventory below
until (turtle.suckDown() == true)
end
-- This section removes excess bonemeal from the turtles inventory
-- Loop over slots 3 through 16
for n = 3, 16, 1 do
-- if the looped over slot has the same item as slot #2(bone meal) drop the items into a chest above
if (turtle.compareTo(n) == true) then
turtle.select(n)
turtle.dropUp()
turtle.select(2)
end
end
end
end
-- On Start, prompt for seeds if needed
turtle.select(1)
if (turtle.getItemCount(1) == 0) then
-- Tell the user the turtle needs seeds in the first slot
print("Place seeds in first slot to start")
-- loop + sleep until there are seeds in first slot
repeat
sleep(1)
until (turtle.getItemCount(1) > 0)
end
-- Resets state for start up:
-- Clears soil
-- Empties Inventory
-- Refills Bone Meal
turtle.dig()
emptyInventory(forceEmpty)
turtle.select(2)
turtle.dropUp()
turtle.suckDown()
-- Processing Loop
while true do
-- Empty Inventory & refill Bone Meal
emptyInventory()
getBoneMeal()
-- Attempt to plant seeds until successful:
turtle.select(1)
if (turtle.place() == false) then
print("Unable to place seeds, waiting...")
repeat
sleep(1)
until (turtle.place() == true)
end
-- Attempt to bone meal until seccessful
turtle.select(2)
if (turtle.place() == false) then
print("Unable to bone meal, waiting...")
repeat
sleep(1)
until (turtle.place() == true)
end
-- Bone Meal nerf fix:
-- Continue to loop over turtle.place() until it fails
while (turtle.place() == true) do
end
-- Harvest Wheat
-- Check to see if there is a block infront of the turtle
-- and if so check if the turtle ISN'T able to dig/harvest it
if (turtle.detect() == true and turtle.dig() == false) then
-- Tell the user the turtle isn't able to dig()/harvest the block infront of the turtle
print("Unable to break block, waiting...")
-- Attempt to dig()/harvest every second until successful
repeat
sleep(1)
until (turtle.detect() == false or turtle.dig() == true)
end
end