Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PathBlocksNeeded = nil
- patchLength = nil
- patchWidth = nil
- bowCount = nil
- -- Calculates the amount of bow the turtle can do on the given patch
- local function GetBowCount()
- print("GetBowCount("..tostring(patchWidth)..")")
- return ((patchWidth - 6) / 6 - (((patchWidth - 6) / 6) % 1))
- end
- -- Reads a number that is bigger than the minimum from the STDIO Stream and prints an error message (<INPUT>..ErrorMessage)
- local function GetInt(ErrorMessage, Minimum)
- print("GetInt("..tostring(ErrorMessage)..", "..tostring(Minimum)..")")
- local success = false
- while true do
- local input = read()
- local number = tonumber(input)
- if number == nil then
- print(input..ErrorMessage)
- elseif number < Minimum then
- print("The number has to be at least "..Minimum)
- else
- return number
- end
- end
- end
- -- Calculates the amount of blocks needed to lay a path
- local function GetPathLength()
- print("GetPathLength("..tostring(patchLength)..", "..tostring(patchWidth)..")")
- local possibleBowCount = bowCount
- return (4 * patchWidth + 6 * possibleBowCount - 6)
- end
- -- Initiation
- shell.run("clear")
- -- Initial info
- print("Place the turtle in the lower left corner of a PLAIN grass patch in form of a rectangle. The Saplings to be placed are picked from Slot 2, so make sure Slot 2 is always filled. Place a log from the type of wood you are planting in Slot 3. It is adviced to use a vacuum hopper to suck up falling saplings since this program will not refill saplings automatically.")
- print("Press Enter to continue...")
- read()
- shell.run("clear")
- -- Gets the patch length
- print("Length is defined as dirt in front of the turtle.")
- print("Give the length of the dirt patch:")
- patchLength = GetInt(" is not a valid length!", 12)
- shell.run("clear")
- -- Gets the patch width
- print("Width is defined as dirt to the right of the turtle.")
- print("Give the width of the dirt patch:")
- patchWidth = GetInt(" is not a valid width!", 12)
- bowCount = GetBowCount()
- shell.run("clear")
- -- Calculate the amount of bricks needed to lay out the path
- print("Do you want to lay bricks on the path of the turtle? (yes/no)")
- local layPath = nil
- -- Requests input wheter the user wishes a path or not
- repeat
- local input = read()
- if (input == "yes") or (input == "no") then
- layPath = (input == "yes")
- end
- term.clearLine()
- until layPath ~= nil
- -- Moves forward
- local function ForwardOverride()
- while not turtle.forward() do
- ForceRefuel()
- end
- end
- -- Moves back
- local function BackOverride()
- while not turtle.back() do
- ForceRefuel()
- end
- end
- -- Moves up
- local function UpOverride()
- while not turtle.up() do
- ForceRefuel()
- end
- end
- -- Moves down
- local function DownOverride()
- while not turtle.down() do
- ForceRefuel()
- end
- end
- -- Turns right
- local function TurnRightOverride()
- turtle.turnRight()
- end
- -- Turns left
- local function TurnLeftOverride()
- turtle.turnLeft()
- end
- -- Forces the turtle to refuel from Slot 1
- local function ForceRefuel()
- local selectedSlot = turtle.getSelectedSlot()
- print("Please put fuel in Slot 1")
- turtle.select(1)
- while not turtle.refuel() do
- sleep(0.1)
- end
- turtle.select(selectedSlot)
- print("Fuel level is not at "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
- end
- -- Aquires the blocks needed for the Path from the User
- local function GetPathBlocks()
- print("GetPathBlocks("..tostring(patchLength)..", "..tostring(patchWidth)..")")
- local PathBlocksNeededTemp = PathBlocksNeeded
- -- Transfer multiple stacks if possible
- if PathBlocksNeededTemp <= 64 then
- if PathBlocksNeededTemp == 64 then
- print("Please insert one stack of blocks of any type into Slot 1")
- end
- print("Please insert exactly "..PathBlocksNeededTemp.." blocks of any type into Slot 1")
- repeat sleep(0.1) until turtle.getItemCount(1) == PathBlocksNeededTemp
- turtle.transferTo(16, PathBlocksNeededTemp)
- else
- local currentSlot = 16
- while PathBlocksNeededTemp >= 64 do
- print("Please insert one stack of blocks any type into Slot 1")
- repeat sleep(0.1) until turtle.getItemCount(1) == 64
- turtle.transferTo(currentSlot, 64)
- currentSlot = currentSlot - 1
- PathBlocksNeededTemp = PathBlocksNeededTemp - 64
- end
- if PathBlocksNeededTemp == 64 then
- print("Please insert one stack of blocks of any type into Slot 1")
- end
- print("Please insert exactly "..PathBlocksNeededTemp.." blocks of any type into Slot 1")
- repeat sleep(0.1) until turtle.getItemCount(1) == PathBlocksNeededTemp
- turtle.transferTo(currentSlot, PathBlocksNeededTemp)
- end
- end
- -- Executes a round on the path, does not execute beginning from the initial position but from (1|1)offset
- local function GetPathFunction()
- return function(ExecuteOnPath)
- local bowCount = GetBowCount(patchWidth)
- local forward = function(execute)
- ForwardOverride()
- ExecuteOnPath()
- end
- for i=1,(patchLength - 2) do
- forward()
- end
- TurnRightOverride()
- for i=1,(3 + (6 * bowCount)) do
- forward()
- end
- TurnRightOverride()
- for i=1,(patchLength - 3) do
- forward()
- end
- TurnRightOverride()
- -- Bow
- for i=1,bowCount do
- for j=1,3 do
- forward()
- end
- TurnRightOverride()
- for j=1,(patchLength - 6) do
- forward()
- end
- TurnLeftOverride()
- for j=1,3 do
- forward()
- end
- TurnLeftOverride()
- for j=1,(patchLength - 6) do
- forward()
- end
- TurnRightOverride()
- end
- for i=1,3 do
- forward()
- end
- TurnRightOverride()
- end
- end
- -- Lays the path
- local function LayPath(length, width)
- print("LayPath("..tostring(patchLength)..", "..tostring(patchWidth)..")")
- TurnRightOverride()
- ForwardOverride()
- TurnLeftOverride()
- ForwardOverride()
- turtle.select(16)
- local path = GetPathFunction(length, patchWidth)
- path(PlaceBlockDown)
- end
- local PlaceBlockDown = function()
- if turtle.getItemCount() < 1 then
- local currentSlot = turtle.getSelectedSlot()
- turtle.select(currentSlot - 1)
- end
- while not turtle.digDown() do sleep(0.1) end
- if turtle.getItemCount() < 1 then
- print("A block is missing, please redo the setup...")
- return
- end
- while not turtle.placeDown() do sleep(0.1) end
- end
- local CheckNearbySaplings = function()
- TurnLeftOverride()
- CheckSapling()
- for i=1,2 do
- TurnRightOverride()
- end
- CheckSapling()
- TurnLeftOverride()
- end
- local function CheckSapling()
- if turtle.detect() then
- -- Check if sapling exists
- turtle.select(2)
- -- Checks if there is an item in the given slot
- if turtle.getItemCount() < 1 then
- print("For the turtle to work correctly it is necessary that there is a sapling in Slot 3. Place at least one wood log in Slot 3!")
- while turtle.getItemCount() < 1 do sleep(0.1) end
- end
- if not turtle.compare() then
- -- Check if log exists
- turtle.select(3)
- -- Checks if there is an item in the given slot
- if turtle.getItemCount() < 1 then
- print("For the turtle to work correctly it is necessary that there is a wood log in Slot 3. Place at least one wood log in Slot 3!")
- while turtle.getItemCount() < 1 do sleep(0.1) end
- end
- if turtle.compare() then
- HarvestTree()
- else
- print("An unknown block was found in front of the turtle. Please make sure, that there is a wood log in Slot 3 that grows directly from the given saplings in Slot 2! The turtle will ignore this block and continue...")
- end
- end
- else
- PlaceSapling()
- end
- end
- local function PlaceSapling()
- turtle.select(2)
- RefillSaplingsIfNeeded()
- while not turtle.place() do sleep(0.1) end
- end
- -- If there are no Saplings in the selected Slot, it waits until the user puts saplings in
- local function RefillSaplingsIfNeeded()
- while turtle.getItemCount() < 1 do
- print("Out of Saplings, please place Saplings in Slot 2!")
- repeat sleep(0.1) until turtle.getItemCount() >= 1
- end
- end
- -- If a path shall be laid, do so
- if layPath == true then
- print(patchLength)
- print(patchWidth)
- PathBlocksNeeded = GetPathLength(patchLength, patchWidth)
- print(PathBlocksNeeded.." blocks are needed to lay down the turtles path.")
- GetPathBlocks()
- print("Press Enter to begin the path setup...")
- read()
- LayPath(patchLength, patchWidth)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement