rhyser9

Tree Farm v4.0 by rhyser9

Dec 29th, 2012
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.38 KB | None | 0 0
  1. local treeType = 0
  2. local resupplyOn = 0
  3. local sapling = turtle.getItemCount(1) - 1
  4. local bonemeal = turtle.getItemCount(2) - 1
  5. local resupplySlot = 3
  6. local resupplyCount = 0
  7. local resupplyMax = 11
  8.  
  9. --************
  10. --*Print Logo*
  11. --************
  12. function logo()
  13.   shell.run('clear')
  14.   print "***************************************"
  15.   print "*         Variable Tree Farms         *"
  16.   print "*             Version 4.0             *"
  17.   print "*         Created By: rhyser9         *"
  18.   print "***************************************"
  19.   term.setCursorPos(1,6)
  20. end
  21.  
  22. --*****************
  23. --*Terminate Error*
  24. --*****************
  25. function terminate()
  26.   sleep(1)
  27.   print "Terminating Program..."
  28.   sleep(3)
  29.   print "Program Terminated"
  30.   error()
  31. end
  32.  
  33. --*******************
  34. --*Configure Program*
  35. --*******************
  36. function configure()
  37.   logo()
  38.   print "What tree type would you like to farm?"
  39.   write("(Oak, Birch, Spruce, Jungle, or Redwood): ")
  40.   input = read()
  41.   if input == "Oak" or input == "oak" then
  42.     treeType = 0
  43.     print "Oak Trees Selected"
  44.   elseif input == "Birch" or input == "birch" then
  45.     treeType = 0
  46.     print "Birch Trees Selected"
  47.   elseif input == "Spruce" or input == "spruce" then
  48.     treeType = 0
  49.     print "Spruce Trees Selected"
  50.   elseif input == "Jungle" or input == "jungle" then
  51.     treeType = 0
  52.     print "Jungle Trees Selected"
  53.   elseif input == "Redwood" or input == "RedWood" or input == "redwood" then
  54.     treeType = 1
  55.     resupplySlot = 4
  56.     resupplyMax = 10
  57.     print "Redwood Trees Selected"
  58.   else
  59.     print "Couldn't understand tree selection"
  60.     treeSelection = 0
  61.     print "Current Mode: 1x1 Vanilla Trees"
  62.   end
  63.   sleep(1)
  64.  
  65.   logo()
  66.   print "Would you like to enable the Resupply function?"
  67.   write("(Y/N): ")
  68.   input = read()
  69.   if input == "Y" or input == "y" then
  70.     resupplyOn = 1
  71.     print "Resupply Mode Enabled"
  72.   elseif input == "N" or input == "n" then
  73.     resupplyOn = 0
  74.     print "Resupply Mode Not Enabled"
  75.   else
  76.     print "Couldn't Understand Input"
  77.     resupplyOn = 0
  78.     print "Resupply Mode not Enabled"
  79.   end
  80.   sleep(1)
  81. end
  82.  
  83. --******************
  84. --*Gather Materials*
  85. --******************
  86. function materials()
  87.   logo()
  88.   print "Please place Saplings in Slot #1."
  89.     turtle.select(1)
  90.     while turtle.getItemCount(1) == 0 do
  91.       sleep(1)
  92.     end
  93.  
  94.   logo()
  95.   print "Please place bonemeal in Slot #2."
  96.     turtle.select(2)
  97.     while turtle.getItemCount(2) == 0 do
  98.       sleep(1)
  99.     end
  100.  
  101.   if treeType == 1 then
  102.     logo()
  103.     print "Please place Dirt in Slot #3."
  104.       turtle.select(3)
  105.       while turtle.getItemCount(3) == 0 do
  106.         sleep(1)
  107.       end
  108.   end
  109.  
  110.   logo()
  111.   print "Please place a Log in Slot #15."
  112.     turtle.select(15)
  113.     while turtle.getItemCount(15) == 0 do
  114.       sleep(1)
  115.     end
  116.  
  117.   logo()
  118.   print "Please place fuel in Slot #16."
  119.     turtle.select(16)
  120.     while turtle.getItemCount(16) == 0 do
  121.       sleep(1)
  122.     end
  123. end
  124.  
  125. --*******************
  126. --*Calculate Runtime*
  127. --*******************
  128. function runtime()
  129.   if treeType == 1 then
  130.     sapling = math.floor((turtle.getItemCount(1) - 1) / 4)
  131.   end
  132.  
  133.   logo()
  134.   print "Calculating Runtime..."
  135.   sleep(2)
  136.   if sapling <= bonemeal then
  137.     if sapling == 0 then
  138.       print "Not Enough Items to Run"
  139.       sleep(1)
  140.       print "Terminating Program"
  141.       sleep(3)
  142.       print "Program Terminated"
  143.       error()
  144.     elseif sapling == 1 then
  145.       print "The program should run 1 time."
  146.     else
  147.       write("The program should run "..sapling.." times.\n")
  148.     end
  149.   else
  150.     if bonemeal == 0 then
  151.       print "Not Enough Items to Run"
  152.       sleep(1)
  153.       print "Terminating Program"
  154.       sleep(3)
  155.       print "Program Terminated"
  156.       error()
  157.     elseif bonemeal == 1 then
  158.       print "The program should run 1 time."
  159.     else
  160.       write("The program should run "..bonemeal.." times.\n")
  161.     end
  162.   end
  163.  
  164.   for i=0,4 do
  165.     write("\nThe program will initiate in "..5 - i.." seconds")
  166.     sleep(1)
  167.   end
  168.   shell.run('clear')
  169. end
  170.  
  171. --********
  172. --*Refuel*
  173. --********
  174. function refuel()
  175.   while turtle.getFuelLevel() < 50 do
  176.     print "  Low Fuel!"
  177.     print "  Refueling..."
  178.     turtle.select(16)
  179.     turtle.refuel(2)
  180.     sleep(2)
  181.     if turtle.refuel() == false then
  182.       print "  Refueling Failed!"
  183.       terminate()
  184.     else
  185.       print "  Turtle Refueled"
  186.     end
  187.   end
  188. end
  189.  
  190. --**********************
  191. --*Plant & Grow Sapling*
  192. --**********************
  193. function plant()
  194.   print "  Planting Tree..."
  195.   turtle.select(1)
  196.   if treeType == 1 then
  197.     --First Sapling
  198.     turtle.up()
  199.     turtle.forward()
  200.     turtle.placeDown()
  201.     --Second Sapling
  202.     turtle.forward()
  203.     turtle.placeDown()
  204.     --Third Sapling
  205.     turtle.turnRight()
  206.     turtle.forward()
  207.     turtle.placeDown()
  208.     --Fourth Sapling
  209.     turtle.turnRight()
  210.     turtle.forward()
  211.     turtle.placeDown()
  212.     --Return to Start
  213.     turtle.turnRight()
  214.     turtle.forward()
  215.     turtle.turnLeft()
  216.     turtle.forward()
  217.     turtle.turnRight()
  218.     turtle.turnRight()
  219.     turtle.down()
  220.   else
  221.     turtle.place()
  222.   end
  223.   --Bonemeal
  224.   turtle.select(2)
  225.   turtle.place()
  226.   --Verify Process
  227.   turtle.select(15)
  228.   if turtle.compare() then
  229.     print "  Planting Process Successful"
  230.   else
  231.     print "  Planting Process Failed"
  232.     print "  Possible Causes:"
  233.     print "  * Saplings and Logs don't match"
  234.     print "  * Planting Area Blocked"
  235.     terminate()
  236.   end
  237. end
  238.  
  239. --**************
  240. --*Harvest Tree*
  241. --**************
  242. function harvest()
  243.   print "  Harvesting Tree..."
  244.   turtle.select(15)
  245.   if treeType == 1 then
  246.     --Chop First Side
  247.     turtle.dig()
  248.     turtle.forward()
  249.     while turtle.compareUp() == true do
  250.       turtle.dig()
  251.       turtle.digUp()
  252.       turtle.up()
  253.     end
  254.     turtle.dig()
  255.     --Chop Second Side
  256.     turtle.select(3)
  257.     turtle.turnRight()
  258.     turtle.dig()
  259.     turtle.forward()
  260.     turtle.turnLeft()
  261.     while turtle.compareDown() == false do
  262.       turtle.dig()
  263.       turtle.digDown()
  264.       turtle.down()
  265.     end
  266.     turtle.dig()
  267.     --Return to Start
  268.     turtle.turnLeft()
  269.     turtle.forward()
  270.     turtle.turnLeft()
  271.     turtle.forward()
  272.     turtle.turnRight()
  273.     turtle.turnRight()
  274.   else
  275.     while turtle.compare() == true do
  276.       turtle.dig()
  277.       turtle.digUp()
  278.       turtle.up()
  279.     end
  280.     while turtle.detectDown() == false do
  281.       turtle.down()
  282.     end
  283.   end
  284.   print "  Harvest Completed"
  285. end
  286.  
  287. --**********
  288. --*Resupply*
  289. --**********
  290. function resupply()
  291.   --Saplings
  292.   turtle.select(1)
  293.   turtle.suckDown()
  294.   while resupplyCount ~= resupplyMax do
  295.     if turtle.compareTo(resupplySlot + resupplyCount) == true then
  296.       turtle.select(resupplySlot + resupplyCount)
  297.       turtle.dropDown()
  298.       turtle.select(1)
  299.     end
  300.     resupplyCount = resupplyCount + 1
  301.   end
  302.   resupplyCount = 0
  303.   --Bonemeal
  304.   turtle.select(2)
  305.   turtle.turnLeft()
  306.   turtle.suck()
  307.   while resupplyCount ~= resupplyMax do
  308.     if turtle.compareTo(resupplySlot + resupplyCount) == true then
  309.       turtle.select(resupplySlot + resupplyCount)
  310.       turtle.drop()
  311.       turtle.select(2)
  312.     end
  313.     resupplyCount = resupplyCount + 1
  314.   end
  315.   resupplyCount = 0
  316.   --Fuel
  317.   turtle.select(16)
  318.   turtle.turnRight()
  319.   turtle.turnRight()
  320.   turtle.suck()
  321.   turtle.select(16)
  322.   while resupplyCount ~= resupplyMax do
  323.     if turtle.compareTo(resupplySlot + resupplyCount) == true then
  324.       turtle.select(resupplySlot + resupplyCount)
  325.       turtle.drop()
  326.       turtle.select(16)
  327.     end
  328.     resupplyCount = resupplyCount + 1
  329.   end
  330.   resupplyCount = 0
  331.   --Drop Off Extra
  332.   turtle.turnLeft()
  333.   while resupplyCount ~= resupplyMax do
  334.     turtle.select(resupplySlot + resupplyCount)
  335.     turtle.drop()
  336.     resupplyCount = resupplyCount + 1
  337.   end
  338.   turtle.select(15)
  339.   turtle.drop(turtle.getItemCount(15) - 1)
  340.   turtle.turnLeft()
  341.   turtle.turnLeft()
  342. end
  343.  
  344. --***********
  345. --*Main Loop*
  346. --***********
  347. configure()
  348. materials()
  349. runtime()
  350.  
  351. logo()
  352. while turtle.getItemCount(1) > 1 and turtle.getItemCount(2) > 1 do
  353.   logo()
  354.   print "Farming Cycle Initiated"
  355.   refuel()
  356.   plant()
  357.   harvest()
  358.   if resupplyOn == 1 then
  359.     turtle.turnRight()
  360.     turtle.turnRight()
  361.     resupply()
  362.   end
  363.   print "Farming Cycle Completed"
  364.   if treeType == 1 then
  365.     print "Waiting for Leaf Despawn..."
  366.     sleep(480)
  367.   else
  368.     sleep(3)
  369.   end
  370. end
  371.  
  372. print "Program Completed"
Advertisement
Add Comment
Please, Sign In to add comment