[ComputerCraft] Turtle Wheat Farm

By SReject on Apr 11, 2014

After seeing a few people post their Lua/CC scripts, I figured I'd add a few of mine to the repository.

Everything you need to know is in the code's comments

--[[
    WheatFarm.lua by SReject
    2014 All Rights Reserved

    Usage:
        While seeds, bone meal and valid soil are available, the turtle will plant
        the seeds, apply bone meal, then harvest the wheat

    Setup:
        Turtle: Farming (Turtle + Hoe in crafting bench)
        Input: Applicatable inventory below turtle for bonemeal input
        Output: Applicatable inventory above turtle for wheat and extra seeds
        Other: Tilled & watered soil in front but lowered one block from the turtle

    Starting:
        Place seeds in the first(top-left) slot of turtle.
        Place bone meal in the inventory below the turtle
        Execute the script/program
]]--

--[[
    emptyInventory:
        Arguments:
            forceEmpty (Boolean) - If true, forces the turtle to empty its inventory

        Does:
            Empties Turtle's inventory except the first slot(seeds) and second slot(bonemeal)
]]--
function emptyInventory(forceEmpty)

    -- Check if the turtle's inventory is full or the emptying should be forced
    if (turtle.getItemCount(16) > 0 or forceEmpty == true) then

        -- Refill seeds in slot#1
        refillSeeds(true)

        -- Loop to check each slot, 3 through 16, of the turtle's inventory
        for n = 3, 16, 1 do

            -- check if there's items in the slot
            if (turtle.getItemCount(n) > 0) then

                -- select the turtle's inventory slot, and 'dropUp()' (Place in the inventory above the turtle)
                turtle.select(n)
                turtle.dropUp()
            end
        end

    -- Otherwise, refillSeeds
    else
        refillSeeds(false)
    end
end

--[[
    refillSeeds:
        Arguments:
            forceRefill (boolean) - If true, forces the turtle to refill seeds

        Does:
            Refills the first slot of the turtle with seeds from else where in the turtle's inventory
]]--
function refillSeeds(forceRefill)

    --[[ Variables:
        seedCount: number of seeds in slot#1
        refillCount: number of seeds required to refill slot#1 ]]
    local seedCount, refillCount = turtle.getItemCount(1), 0
    refillCount = 64 - seedCount

    -- Check if there's no seeds in slot #1
    if (seedCount == 0) then

        -- Print the turtle is out of seeds
        print("Out of seeds: place seeds in first slot to continue")

        -- Wait for seeds
        repeat
            sleep(1)
        until (turtle.getItemCount(1) > 0)

    -- Check if the turtle needs to refill seeds or the 'forceRefill' argument is true
    elseif (seedCount < 16) or (forceRefill == true and seedCount < 64)  then

        -- select the first slot
        turtle.select(1)

        -- loop over slots #3 through #16
        for n = 3, 16, 1 do

            -- check if the slot looped over contains seeds
            if (turtle.compareTo(n) == true) then

                -- Select the looped over slot
                turtle.select(n)

                -- If there's not enough seeds to completely refill slot#1
                if (refillCount > turtle.getItemCount(n)) then

                    -- transfer the seeds to slot one
                    turtle.transferTo(1, turtle.getItemCount(n))

                    -- Adjust refillCount
                    refillCount = 64 - turtle.getItemCount(1)

                -- If there's more than enough seeds in the looped over slot to refill slot#1
                else

                    -- transfer enough seeds to fill slot#1 then exit the loop
                    turtle.transferTo(1, refillCount)
                    break
                end

                -- select slot#1 for the next literation of the loop
                turtle.select(1)
            end
        end
    end
end

--[[
    getBoneMeal
        Arguments:
            none
        Usage:
            Refills slot#2 with bone meal from an inventory directly below the turtle
]]
function getBoneMeal()

    -- select the turtle's second inventory slot
    turtle.select(2)

    -- Check to see if the turtle needs more bonemeal
    if (turtle.getItemCount(2) <= 8) then

        -- Attempt to retrieve bone meal from inventory below the turtle
        if (turtle.suckDown() == false) then

            -- Print the turtle is out of bone meal and waiting
            print("Out of bonemeal, waiting...")

            -- Wait for bone meal to be placed in inventory below the turtle
            repeat
                sleep(1)
            until (turtle.suckDown() == true)
        end

        -- loop over the turtle's inventory slots #3 through 16
        for n = 3, 16, 1 do

            -- if the slot has bone meal(extra) place it in the inventory above
            if (turtle.compareTo(n) == true) then
                turtle.select(n)
                turtle.dropUp()
                turtle.select(2)
            end
        end
    end
end

--[[
    Start Up processing:
        Checks if the turtle has seeds in slot#1.
            If not, waits until it does

        Harvests any wheat/seeds that were planted just before the program was terminated:
            Chunk Unloaded, player log off, etc

        Empties Inventory of any extra wheat, seeds, or bonemeal that were left after the program terminated

        Refills Bone Meal if needs
]]--
turtle.select(1)
if (turtle.getItemCount(1) == 0) then
    print("Place seeds in first slot to start")
    repeat
        sleep(1)
    until (turtle.getItemCount(1) > 0)
end
turtle.dig()
emptyInventory(true)
turtle.select(2)
turtle.dropUp()
turtle.suckDown()

--[[
    Processing Loop
]]--
while true do
    -- Empty inventory and refill bonemeal if needed
    emptyInventory()
    getBoneMeal()

    -- Attempt to plant seeds from slot #1
    turtle.select(1)
    if (turtle.place() == false) then

        -- If unable, print as such
        print("Unable to place seeds, waiting...")

        -- Attempt to plant seeds once a second until successful
        repeat
            sleep(1)
        until (turtle.place() == true)
    end

    -- Attempt to use bone meal from slot#2 on seeds
    turtle.select(2)
    if (turtle.place() == false) then

        -- If unable, print as such
        print("Unable to bone meal, waiting...")

        -- Attempt to bone meal seeds once a second until successful
        repeat
            sleep(1)
        until (turtle.place() == true)
    end

    -- Continue bone mealing until unable(1.5+ bone meal nerf fix)
    while turtle.place() == true do
        getBoneMeal()
    end

    -- Check if there's a 'block'(wheat or seeds) infront of the turtle, and attempt to break it if so
    if (turtle.detect() == true and turtle.dig() == false) then

        -- Print the turtle was unable to harvest the wheat
        print("Unable to break block, waiting...")

        -- Recheck & attempt once a second until successful
        repeat
            sleep(1)
        until (turtle.detect() == false or turtle.dig() == true)
    end
end

Comments

Sign in to comment.
fehudohyk   -  Apr 26, 2021

thanks

 Respond  
Sorasyn   -  Apr 11, 2014

Very cool! I had a small something like this worked up on an old FTB world of mine, although it wasn't nearly as comprehensive. I'll have to give this a shot next time I get a chance.

 Respond  
Hawkee   -  Apr 11, 2014

Do you have a youtube video of this in action? Sounds interesting.

SReject  -  Apr 11, 2014

My computer sucks too much for recording Minecraft/Feed-The-Beast

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.