dadragon84

do_sugarplant

Feb 26th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. -- do_sugarplant
  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 Sugar Cane 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.select(15)
  124.     turtle.placeDown()
  125.     totalPlanted = totalPlanted + 1
  126.   end
  127. end
  128.  
  129. function plantDown()
  130.   while tonumber(currentPos) ~= tonumber(length) do
  131.     fuelItUp()
  132.     plant()
  133.     turtle.forward()
  134.     currentPos = currentPos + 1
  135.   end
  136.   if tonumber(currentRow) ~= tonumber(rows) then
  137.     if tonumber(currentRow) % 2 == 0 then
  138.       plant()
  139.       rightTurn()
  140.     else
  141.       plant()
  142.       leftTurn()
  143.     end
  144.   else
  145.     plant()
  146.     ending()
  147.   end
  148. end
  149.  
  150. function leftTurn()
  151.   turtle.turnLeft()
  152.   turtle.forward()
  153.   turtle.forward()
  154.   turtle.turnLeft()
  155.   currentRow = currentRow + 1
  156.   plantBack()
  157. end
  158.  
  159. function rightTurn()
  160.   turtle.turnRight()
  161.   turtle.forward()
  162.   turtle.forward()
  163.   turtle.turnRight()
  164.   currentRow = currentRow + 1
  165.   plantDown()
  166. end
  167.  
  168. function plantBack()
  169.   while tonumber(currentPos) ~= tonumber(startBlock) do
  170.     fuelItUp()
  171.     plant()
  172.     turtle.forward()
  173.     currentPos = currentPos - 1
  174.   end
  175.   if tonumber(currentRow) ~= tonumber(rows) then
  176.     if tonumber(currentRow) % 2 == 0 then
  177.       plant()
  178.       rightTurn()
  179.     else
  180.       plant()
  181.       leftTurn()
  182.     end
  183.   else
  184.     plant()
  185.     ending()
  186.   end
  187. end
  188.  
  189. function ending()
  190. updateStatus("Finished")
  191.   if tonumber(currentRow) % 2 == 0 then
  192.     turtle.forward()
  193.     turtle.turnLeft()
  194.     turtle.down()
  195.     turtle.down()
  196.   else
  197.     turtle.turnLeft()
  198.     turtle.turnLeft()
  199.     while tonumber(currentPos) ~= tonumber(startBlock - 1) do
  200.       turtle.forward()
  201.       currentPos = currentPos - 1
  202.     end
  203.     turtle.turnLeft()
  204.     turtle.down()
  205.     turtle.down()
  206.   end
  207.   while tonumber(currentRow) ~= tonumber(startRow) do
  208.     turtle.forward()
  209.     turtle.forward()
  210.     currentRow = currentRow - 1
  211.   end
  212.   dropoff()
  213. end
  214.  
  215. function dropoff()
  216.   s = 1
  217.   total = 0
  218.   while s < 16 do
  219.     items = turtle.getItemCount(s)
  220.     total = items + total
  221.     if items > 0 then
  222.       turtle.select(s)
  223.       turtle.drop()
  224.     end
  225.     s = s + 1
  226.   end
  227.   turtle.turnLeft()
  228.   print("Total Items Deposited:")
  229.   print(total)
  230.   updateStatus("Total Items Deposited  " ..total)
  231. end
  232.  
  233. questions()
  234. print("Press Enter to Continue")
  235. io.read()
  236. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment