Advertisement
dadragon84

do_veggies

Feb 26th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. -- do_veggies  (part of FarmerTurt)
  2. -----------------------------------------------------------------------
  3. --[[
  4. ("This program is currently intended for use with melon and pumpkin farms")
  5. ("The turtle must be placed with 1 empty block between the first possible farm item and the turtle")
  6. ("There must be a chest to the right of where the turtle is placed before starting the program to ensure correct dropoff")
  7. ("This program assumes a 4 wide system for Melons and Pumpkins, the length is variable:")
  8. ("D = Dirt(or other): W = Water: S = Seed: C = Chest: T = Turtle: M = Melon or Pumpkin Crop")
  9. ("      W   DDDDDDDDDDDD      ")
  10. ("      I   DSSSSSSSSSSDC     ")
  11. ("      D   DMMMMMMMMMM T     ")
  12. ("      T   DMMMMMMMMMM       ")
  13. ("      H   DSSSSSSSSSSD      ")
  14. ("          DWWWWWWWWWWD      ")
  15. ("              LENGTH        ")
  16. --]]
  17.  
  18. term.clear()
  19. term.setCursorPos(1, 1)
  20. minerID = os.getComputerID()
  21. turtleName = nil
  22. consoleID = 0
  23. version = "1.0"
  24. function getConsoleID()
  25.   local fname = "ConsoleID"
  26.   if fs.exists(fname) then
  27.     file = fs.open(fname, "r")
  28.     consoleID = tonumber(file.readAll())
  29.     file.close()
  30.     print("Monitor Console ID: "..consoleID)
  31.   else
  32.     updateStatus("Waiting for Console ID...",true)
  33.     term.write("Monitor Console ID: ")
  34.     consoleID = io.read()
  35.     file = fs.open(fname, "w")
  36.     file.write(consoleID)
  37.     file.close()
  38.     consoleID = tonumber(consoleID)
  39.   end
  40. end
  41. function updateStatus(msg, silent)
  42.   if not rednet.isOpen("right") then rednet.open("right") end
  43.   if rednet.isOpen("right") then
  44.     if turtleName == nil then return end
  45.     networkmsg = "MM:"..turtleName..": "..msg
  46.     if consoleID == 0 then
  47.       rednet.broadcast(networkmsg)
  48.     else
  49.       rednet.send(consoleID,networkmsg)
  50.     end
  51.   end
  52.   if not silent then print(msg) end
  53. end
  54. function getTurtleName()
  55.   local fname = "TurtleName"
  56.   if fs.exists(fname) then
  57.     file = fs.open(fname, "r")
  58.     turtleName = file.readAll()
  59.     file.close()
  60.   else
  61.     term.write("Turtle Name: ")
  62.     turtleName = io.read()
  63.     file = fs.open(fname, "w")
  64.     file.write(turtleName)
  65.     file.close()
  66.   end
  67. end
  68. -- Main Program
  69. updateStatus("Starting")
  70. term.clear()
  71. term.setCursorPos(1,1)
  72. getTurtleName()
  73. getConsoleID()
  74. function update()
  75. end
  76. function questions()
  77. --Questions that are asked in order to get the variables needed to function customizeably
  78.   print("How many blocks long to farm?")
  79.   blocksDown = io.read()
  80.   print("How many times to run?")
  81.   qtyRepeat = io.read()
  82.   if tonumber(qtyRepeat) > 1 then
  83.     print("How long to wait between farmings?")
  84.     farmWait = io.read()
  85.   else
  86.     farmWait = 0
  87.   end
  88.   print("How many farms wide?")
  89.   numFarms = io.read()
  90.   declarations()
  91. end
  92. function declarations()
  93. --Sets all variable needed to restart the program from scratch
  94.   i = 0
  95.   currentQty = 0
  96.   startBlock = 0
  97.   currentFarm = 1
  98.   prime()
  99. end
  100. function prime()
  101. --Calls the fueling fuction and gets the turtle to the correct spot to start farming
  102.   fuelItUp()
  103.   turtle.forward()
  104.   farmdown()
  105. end
  106. function fuelItUp()
  107. updateStatus("...Working")
  108. --Finds out if the turtle needs to refuel and if it has fuel to run
  109.   needsFuel = turtle.getFuelLevel()
  110.   minimumFuelLevel = 10
  111.   hasFuel = turtle.getItemCount(16)
  112.   useFuel = turtle.refuel()
  113.   if needsFuel == "unlimited" then
  114.    
  115.   else
  116.     if tonumber(needsFuel) < tonumber(minimumFuelLevel) then
  117.       if hasFuel > 0 then
  118.         turtle.select(16)
  119.         turtle.refuel(1)
  120.         turtle.select(1)
  121.       else
  122.         print("Please insert fuel into slot 16")
  123.         updateStatus("...Need Fuel")
  124.         repeat os.sleep(1)
  125.         until tonumber(turtle.getItemCount(16)) > 0
  126.         fuelItUp()
  127.       end
  128.     end
  129.   end
  130. end
  131. function farmdown()
  132. turtle.select(1)
  133. --Moves the turtle down the set number of blocks checking for items infront of him and harvesting if there is
  134.   while i ~= tonumber(blocksDown) do
  135.     fuelItUp()
  136.     detected = turtle.detect()
  137.     if detected == true then
  138.       turtle.dig()
  139.       turtle.suck()
  140.       turtle.forward()
  141.       i = i + 1
  142.     else
  143.       turtle.forward()
  144.       i = i + 1
  145.     end
  146.     detected = false
  147.   end
  148.   turn()
  149. end
  150. function turn()
  151. --Turns the robot to the left once the number of blocks down has been reached and if there is a block when he turns, he harvests it
  152.   turtle.turnLeft()
  153.   detected = turtle.detect()
  154.     if detected == true then
  155.       turtle.dig()
  156.       turtle.suck()
  157.     end
  158.   detected = false
  159.   turtle.forward()
  160.   turtle.turnLeft()
  161.   farmback()
  162. end
  163. function farmback()
  164. --Same as the farmDown function but in reverse
  165.   while i ~= startBlock do
  166.   fuelItUp()
  167.     detected = turtle.detect()
  168.     if detected == true then
  169.       turtle.dig()
  170.       turtle.suck()
  171.       turtle.forward()
  172.       i = i - 1
  173.     else
  174.       turtle.forward()
  175.       i = i - 1
  176.     end
  177.     detected = false
  178.   end
  179.   movefarms()
  180. end
  181. function movefarms()
  182.   farmMessage = "Farm " .. currentFarm .. " of " .. numFarms .. " completed!"
  183.   print(farmMessage)
  184.   if tonumber(currentFarm) ~= tonumber(numFarms) then
  185.     turtle.forward()
  186.     turtle.turnRight()
  187.     turtle.forward()
  188.     turtle.forward()
  189.     turtle.forward()
  190.     turtle.forward()
  191.     turtle.turnRight()
  192.     currentFarm = currentFarm + 1
  193.     prime()
  194.   else
  195.     dropoff()
  196.   end
  197.    
  198. end
  199. function dropoff()
  200. updateStatus("Finished")
  201. --Moves the robot from the ending position to the chest for item dropoff and counts how many items collected in total, does not deposit the fuel in slot 16
  202.   turtle.forward()
  203.   turtle.turnLeft()
  204.   turtle.forward()
  205.   while tonumber(currentFarm) ~= 1 do
  206.     fuelItUp()
  207.     turtle.forward()
  208.     turtle.forward()
  209.     turtle.forward()
  210.     turtle.forward()
  211.     turtle.forward()
  212.     currentFarm = currentFarm - 1
  213.   end
  214.   s = 1
  215.   total = 0
  216.   while s < 16 do
  217.     items = turtle.getItemCount(s)
  218.     total = items + total
  219.     if items > 0 then
  220.       turtle.select(s)
  221.       turtle.drop()
  222.     end
  223.     s = s + 1
  224.   end
  225.   turtle.turnLeft()
  226.   print("Total Items Deposited:")
  227.   print(total)
  228.   updateStatus("Total Items Deposited  " ..total)
  229.   if tonumber(qtyRepeat) < 1 then
  230.     qtyRepeat = 1
  231.   end
  232.   currentQty = currentQty + 1
  233.   completeMessage = "Round " .. currentQty .. " of " .. qtyRepeat .. " completed!"
  234.   print(completeMessage)
  235.   --This repeats the program while the current turn is not equal to the repeat quantity specified
  236.   while tonumber(currentQty) ~= tonumber(qtyRepeat) do
  237.    
  238.     os.sleep(tonumber(farmWait))
  239.     prime()
  240.   end
  241. end
  242. questions()
  243. print("Press Enter to Continue")
  244. io.read()
  245. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement