Advertisement
dadragon84

do_wheatplant

Feb 26th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.16 KB | None | 0 0
  1. -- do_wheat  (part of FarmerTurt)
  2. -----------------------------------------------------------------------
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. minerID = os.getComputerID()
  6. turtleName = nil
  7. consoleID = 0
  8. version = "1.0"
  9. function getConsoleID()
  10.   local fname = "ConsoleID"
  11.   if fs.exists(fname) then
  12.     file = fs.open(fname, "r")
  13.     consoleID = tonumber(file.readAll())
  14.     file.close()
  15.     print("Monitor Console ID: "..consoleID)
  16.   else
  17.     updateStatus("Waiting for Console ID...",true)
  18.     term.write("Monitor Console ID: ")
  19.     consoleID = io.read()
  20.     file = fs.open(fname, "w")
  21.     file.write(consoleID)
  22.     file.close()
  23.     consoleID = tonumber(consoleID)
  24.   end
  25. end
  26. function updateStatus(msg, silent)
  27.   if not rednet.isOpen("right") then rednet.open("right") end
  28.   if rednet.isOpen("right") then
  29.     if turtleName == nil then return end
  30.     networkmsg = "MM:"..turtleName..": "..msg
  31.     if consoleID == 0 then
  32.       rednet.broadcast(networkmsg)
  33.     else
  34.       rednet.send(consoleID,networkmsg)
  35.     end
  36.   end
  37.   if not silent then print(msg) end
  38. end
  39. function getTurtleName()
  40.   local fname = "TurtleName"
  41.   if fs.exists(fname) then
  42.     file = fs.open(fname, "r")
  43.     turtleName = file.readAll()
  44.     file.close()
  45.   else
  46.     term.write("Turtle Name: ")
  47.     turtleName = io.read()
  48.     file = fs.open(fname, "w")
  49.     file.write(turtleName)
  50.     file.close()
  51.   end
  52. end
  53. -- Main Program
  54. updateStatus("Starting")
  55. term.clear()
  56. term.setCursorPos(1,1)
  57. getTurtleName()
  58. getConsoleID()
  59. function update()
  60. end
  61.  
  62.  function fuelItUp()
  63.  updateStatus("...Working")
  64. --Finds out if the turtle needs to refuel and if it has fuel to run
  65.   needsFuel = turtle.getFuelLevel()
  66.   minimumFuelLevel = 10
  67.   hasFuel = turtle.getItemCount(16)
  68.   useFuel = turtle.refuel()
  69.   if needsFuel == "unlimited" then
  70.    
  71.   else
  72.     if tonumber(needsFuel) < tonumber(minimumFuelLevel) then
  73.       if hasFuel > 0 then
  74.         turtle.select(16)
  75.         turtle.refuel(1)
  76.         turtle.select(1)
  77.       else
  78.         updateStatus("Please insert fuel into slot 16")
  79.         repeat os.sleep(1)
  80.         until tonumber(turtle.getItemCount(16)) > 0
  81.         fuelItUp()
  82.       end
  83.     end
  84.   end
  85. end
  86. function questions()
  87.   print("How many blocks deep?")
  88.   length = io.read()
  89.   print("How many rows wide?")
  90.   rows = io.read()
  91.   declarations()
  92. end
  93.  
  94. function declarations()
  95.   currentPos = 0
  96.   currentRow = 1
  97.   startBlock = 1
  98.   startRow = 1
  99.   totalPlanted = 0
  100.   prime()
  101. end
  102.  
  103. function prime()
  104.   fuelItUp()
  105.   hasSeeds = turtle.getItemCount(15)
  106.   if tonumber(hasSeeds) > 0 then
  107.     turtle.up()
  108.     turtle.forward()
  109.     currentPos = currentPos + 1
  110.     plantDown()
  111.   else
  112.     updateStatus("Please put Seeds into slot 15")
  113.     repeat os.sleep(1)
  114.     until tonumber(turtle.getItemCount(15)) > 0
  115.     prime()
  116.   end
  117.  
  118. end
  119.  
  120. function plant()
  121.   plantBot = turtle.detectDown()
  122.   if plantBot == false then
  123.     turtle.digDown()
  124.     turtle.select(15)
  125.     turtle.placeDown()
  126.     totalPlanted = totalPlanted + 1
  127.   end
  128.   if turtle.getItemCount(14) > 0 then
  129.       turtle.select(14)
  130.       turtle.placeDown()
  131.   end
  132. end
  133.  
  134. function plantDown()
  135.   while tonumber(currentPos) ~= tonumber(length) do
  136.     fuelItUp()
  137.     plant()
  138.     turtle.forward()
  139.     currentPos = currentPos + 1
  140.   end
  141.   if tonumber(currentRow) ~= tonumber(rows) then
  142.     if tonumber(currentRow) % 2 == 0 then
  143.       plant()
  144.       rightTurn()
  145.     else
  146.       plant()
  147.       leftTurn()
  148.     end
  149.   else
  150.     plant()
  151.     ending()
  152.   end
  153. end
  154.  
  155. function leftTurn()
  156.   turtle.turnLeft()
  157.   turtle.forward()
  158.   turtle.forward()
  159.   turtle.turnLeft()
  160.   currentRow = currentRow + 1
  161.   plantBack()
  162. end
  163.  
  164. function rightTurn()
  165.   turtle.turnRight()
  166.   turtle.forward()
  167.   turtle.forward()
  168.   turtle.turnRight()
  169.   currentRow = currentRow + 1
  170.   plantDown()
  171. end
  172.  
  173. function plantBack()
  174.   while tonumber(currentPos) ~= tonumber(startBlock) do
  175.     fuelItUp()
  176.     plant()
  177.     turtle.forward()
  178.     currentPos = currentPos - 1
  179.   end
  180.   if tonumber(currentRow) ~= tonumber(rows) then
  181.     if tonumber(currentRow) % 2 == 0 then
  182.       plant()
  183.       rightTurn()
  184.     else
  185.       plant()
  186.       leftTurn()
  187.     end
  188.   else
  189.     plant()
  190.     ending()
  191.   end
  192. end
  193.  
  194. function ending()
  195. updateStatus("Finished")
  196.   if tonumber(currentRow) % 2 == 0 then
  197.     turtle.forward()
  198.     turtle.turnLeft()
  199.     turtle.down()
  200.     turtle.down()
  201.   else
  202.     turtle.turnLeft()
  203.     turtle.turnLeft()
  204.     while tonumber(currentPos) ~= tonumber(startBlock - 1) do
  205.       turtle.forward()
  206.       currentPos = currentPos - 1
  207.     end
  208.     turtle.turnLeft()
  209.     turtle.down()
  210.     turtle.down()
  211.   end
  212.   while tonumber(currentRow) ~= tonumber(startRow) do
  213.     turtle.forward()
  214.     turtle.forward()
  215.     currentRow = currentRow - 1
  216.   end
  217.   dropoff()
  218. end
  219.  
  220. function dropoff()
  221.   s = 1
  222.   total = 0
  223.   while s < 16 do
  224.     items = turtle.getItemCount(s)
  225.     total = items + total
  226.     if items > 0 then
  227.       turtle.select(s)
  228.       turtle.drop()
  229.     end
  230.     s = s + 1
  231.   end
  232.   turtle.turnLeft()
  233.   print("Total Items Deposited:")
  234.   print(total)
  235.   updateStatus("Total Items Deposited  " ..total)
  236.  
  237. end
  238.  
  239. questions()
  240. print("Press Enter to Continue")
  241. io.read()
  242. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement