Advertisement
_DudeWhat_

TreeFarm (WIP)

Jun 9th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.19 KB | None | 0 0
  1. PathBlocksNeeded = nil
  2. patchLength = nil
  3. patchWidth = nil
  4. bowCount = nil
  5.  
  6. -- Calculates the amount of bow the turtle can do on the given patch
  7. local function GetBowCount()
  8.     print("GetBowCount("..tostring(patchWidth)..")")
  9.     return ((patchWidth - 6) / 6 - (((patchWidth - 6) / 6) % 1))
  10. end
  11.  
  12. -- Reads a number that is bigger than the minimum from the STDIO Stream and prints an error message (<INPUT>..ErrorMessage)
  13. local function GetInt(ErrorMessage, Minimum)
  14.     print("GetInt("..tostring(ErrorMessage)..", "..tostring(Minimum)..")")
  15.     local success = false
  16.     while true do
  17.         local input = read()
  18.         local number = tonumber(input)
  19.         if number == nil then
  20.             print(input..ErrorMessage)
  21.         elseif number < Minimum then
  22.             print("The number has to be at least "..Minimum)
  23.         else
  24.             return number
  25.         end
  26.     end
  27. end
  28.  
  29. -- Calculates the amount of blocks needed to lay a path
  30. local function GetPathLength()
  31.     print("GetPathLength("..tostring(patchLength)..", "..tostring(patchWidth)..")")
  32.     local possibleBowCount = bowCount
  33.     return (4 * patchWidth + 6 * possibleBowCount - 6)
  34. end
  35.  
  36. -- Initiation
  37. shell.run("clear")
  38.  
  39. -- Initial info
  40. 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.")
  41. print("Press Enter to continue...")
  42. read()
  43. shell.run("clear")
  44.  
  45. -- Gets the patch length
  46. print("Length is defined as dirt in front of the turtle.")
  47. print("Give the length of the dirt patch:")
  48. patchLength = GetInt(" is not a valid length!", 12)
  49. shell.run("clear")
  50.  
  51. -- Gets the patch width
  52. print("Width is defined as dirt to the right of the turtle.")
  53. print("Give the width of the dirt patch:")
  54. patchWidth = GetInt(" is not a valid width!", 12)
  55. bowCount = GetBowCount()
  56. shell.run("clear")
  57.  
  58. -- Calculate the amount of bricks needed to lay out the path
  59. print("Do you want to lay bricks on the path of the turtle? (yes/no)")
  60. local layPath = nil
  61. -- Requests input wheter the user wishes a path or not
  62. repeat
  63.     local input = read()
  64.     if (input == "yes") or (input == "no") then
  65.         layPath = (input == "yes")
  66.     end
  67.     term.clearLine()
  68. until layPath ~= nil
  69.  
  70. -- Moves forward
  71. local function ForwardOverride()
  72.     while not turtle.forward() do
  73.         ForceRefuel()
  74.     end
  75. end
  76.  
  77. -- Moves back
  78. local function BackOverride()
  79.     while not turtle.back() do
  80.         ForceRefuel()
  81.     end
  82. end
  83.  
  84. -- Moves up
  85. local function UpOverride()
  86.     while not turtle.up() do
  87.         ForceRefuel()
  88.     end
  89. end
  90.  
  91. -- Moves down
  92. local function DownOverride()
  93.     while not turtle.down() do
  94.         ForceRefuel()
  95.     end
  96. end
  97.  
  98. -- Turns right
  99. local function TurnRightOverride()
  100.     turtle.turnRight()
  101. end
  102.  
  103. -- Turns left
  104. local function TurnLeftOverride()
  105.     turtle.turnLeft()
  106. end
  107.  
  108. -- Forces the turtle to refuel from Slot 1
  109. local function ForceRefuel()
  110.     local selectedSlot = turtle.getSelectedSlot()
  111.     print("Please put fuel in Slot 1")
  112.     turtle.select(1)
  113.     while not turtle.refuel() do
  114.         sleep(0.1)
  115.     end
  116.     turtle.select(selectedSlot)
  117.     print("Fuel level is not at "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
  118. end
  119.  
  120. -- Aquires the blocks needed for the Path from the User
  121. local function GetPathBlocks()
  122.     print("GetPathBlocks("..tostring(patchLength)..", "..tostring(patchWidth)..")")
  123.     local PathBlocksNeededTemp = PathBlocksNeeded
  124.     -- Transfer multiple stacks if possible
  125.     if PathBlocksNeededTemp <= 64 then
  126.         if PathBlocksNeededTemp == 64 then
  127.             print("Please insert one stack of blocks of any type into Slot 1")
  128.         end
  129.         print("Please insert exactly "..PathBlocksNeededTemp.." blocks of any type into Slot 1")
  130.         repeat sleep(0.1) until turtle.getItemCount(1) == PathBlocksNeededTemp
  131.         turtle.transferTo(16, PathBlocksNeededTemp)
  132.     else
  133.         local currentSlot = 16
  134.         while PathBlocksNeededTemp >= 64 do
  135.             print("Please insert one stack of blocks any type into Slot 1")
  136.             repeat sleep(0.1) until turtle.getItemCount(1) == 64
  137.             turtle.transferTo(currentSlot, 64)
  138.             currentSlot = currentSlot - 1
  139.             PathBlocksNeededTemp = PathBlocksNeededTemp - 64
  140.         end
  141.         if PathBlocksNeededTemp == 64 then
  142.             print("Please insert one stack of blocks of any type into Slot 1")
  143.         end
  144.         print("Please insert exactly "..PathBlocksNeededTemp.." blocks of any type into Slot 1")
  145.         repeat sleep(0.1) until turtle.getItemCount(1) == PathBlocksNeededTemp
  146.         turtle.transferTo(currentSlot, PathBlocksNeededTemp)
  147.     end
  148. end
  149.  
  150. -- Executes a round on the path, does not execute beginning from the initial position but from (1|1)offset
  151. local function GetPathFunction()
  152.     return function(ExecuteOnPath)
  153.         local bowCount = GetBowCount(patchWidth)
  154.         local forward = function(execute)
  155.             ForwardOverride()
  156.             ExecuteOnPath()
  157.         end
  158.         for i=1,(patchLength - 2) do
  159.             forward()
  160.         end
  161.         TurnRightOverride()
  162.         for i=1,(3 + (6 * bowCount)) do
  163.             forward()
  164.         end
  165.         TurnRightOverride()
  166.         for i=1,(patchLength - 3) do
  167.             forward()
  168.         end
  169.         TurnRightOverride()
  170.        
  171.         -- Bow
  172.         for i=1,bowCount do
  173.             for j=1,3 do
  174.                 forward()
  175.             end
  176.             TurnRightOverride()
  177.             for j=1,(patchLength - 6) do
  178.                 forward()
  179.             end
  180.             TurnLeftOverride()
  181.             for j=1,3 do
  182.                 forward()
  183.             end
  184.             TurnLeftOverride()
  185.             for j=1,(patchLength - 6) do
  186.                 forward()
  187.             end
  188.             TurnRightOverride()
  189.         end
  190.        
  191.         for i=1,3 do
  192.             forward()
  193.         end
  194.         TurnRightOverride()
  195.     end
  196. end
  197.  
  198. -- Lays the path
  199. local function LayPath(length, width)
  200.     print("LayPath("..tostring(patchLength)..", "..tostring(patchWidth)..")")
  201.     TurnRightOverride()
  202.     ForwardOverride()
  203.     TurnLeftOverride()
  204.     ForwardOverride()
  205.     turtle.select(16)
  206.     local path = GetPathFunction(length, patchWidth)
  207.     path(PlaceBlockDown)
  208. end
  209.  
  210. local PlaceBlockDown = function()
  211.     if turtle.getItemCount() < 1 then
  212.         local currentSlot = turtle.getSelectedSlot()
  213.         turtle.select(currentSlot - 1)
  214.     end
  215.     while not turtle.digDown() do sleep(0.1) end
  216.     if turtle.getItemCount() < 1 then
  217.         print("A block is missing, please redo the setup...")
  218.         return
  219.     end
  220.     while not turtle.placeDown() do sleep(0.1) end
  221. end
  222.  
  223. local CheckNearbySaplings = function()
  224.     TurnLeftOverride()
  225.     CheckSapling()
  226.     for i=1,2 do
  227.         TurnRightOverride()
  228.     end
  229.     CheckSapling()
  230.     TurnLeftOverride()
  231. end
  232.  
  233. local function CheckSapling()
  234.     if turtle.detect() then
  235.         -- Check if sapling exists
  236.         turtle.select(2)
  237.         -- Checks if there is an item in the given slot
  238.         if turtle.getItemCount() < 1 then
  239.             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!")
  240.             while turtle.getItemCount() < 1 do sleep(0.1) end
  241.         end
  242.         if not turtle.compare() then
  243.             -- Check if log exists
  244.             turtle.select(3)
  245.             -- Checks if there is an item in the given slot
  246.             if turtle.getItemCount() < 1 then
  247.                 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!")
  248.                 while turtle.getItemCount() < 1 do sleep(0.1) end
  249.             end
  250.             if turtle.compare() then
  251.                 HarvestTree()
  252.             else
  253.                 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...")
  254.             end
  255.         end
  256.     else
  257.         PlaceSapling()
  258.     end    
  259. end
  260.  
  261. local function PlaceSapling()
  262.     turtle.select(2)
  263.     RefillSaplingsIfNeeded()
  264.     while not turtle.place() do sleep(0.1) end
  265. end
  266.  
  267. -- If there are no Saplings in the selected Slot, it waits until the user puts saplings in
  268. local function RefillSaplingsIfNeeded()
  269.     while turtle.getItemCount() < 1 do
  270.         print("Out of Saplings, please place Saplings in Slot 2!")
  271.         repeat sleep(0.1) until turtle.getItemCount() >= 1
  272.     end
  273. end
  274.  
  275. -- If a path shall be laid, do so
  276. if layPath == true then
  277.     print(patchLength)
  278.     print(patchWidth)
  279.     PathBlocksNeeded = GetPathLength(patchLength, patchWidth)
  280.     print(PathBlocksNeeded.." blocks are needed to lay down the turtles path.")
  281.     GetPathBlocks()
  282.     print("Press Enter to begin the path setup...")
  283.     read()
  284.     LayPath(patchLength, patchWidth)
  285. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement