Advertisement
hevohevo

ComputerCraft Tutorial: harvest_millet_3

Nov 27th, 2013
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. -- ####################################
  2. -- harvest_millet
  3. -- version 0.3
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6.  
  7. -- #########################
  8. -- TOP view
  9. -- T: turtle, o: millet, W: water
  10.  
  11. -- Z_SIZE=5, COUNT=1
  12. -- oWo
  13. -- oWo
  14. -- oWo
  15. -- oWo
  16. -- oWo
  17. -- T    
  18.  
  19. -- Z_SIZE=5, COUNT=3
  20. -- oWooWooWo
  21. -- oWooWooWo
  22. -- oWooWooWo
  23. -- oWooWooWo
  24. -- oWooWooWo
  25. -- T
  26.  
  27. -- #########################
  28. -- Side view
  29. -- Y position is 3, same as the top of the millet.
  30. -- T: turtle, o: millet, F: Chest for Fuel items, C: Chest for droping down millets
  31.  
  32. --  F
  33. -- oT
  34. -- oC
  35. -- o
  36.  
  37.  
  38. -- ####################
  39. -- config
  40.  
  41. FUEL_SLOT = 16
  42. Z_SIZE = 5
  43. COUNT = 1
  44.  
  45. -- ####################
  46. -- define functions
  47.  
  48. function goForward(n)
  49.   for i=1,n do
  50.     turtle.dig()
  51.     turtle.forward()
  52.     turtle.digDown()
  53.   end
  54. end
  55.  
  56. function goRightLine()
  57.   turtle.turnRight()
  58.   goForward(2)
  59.   turtle.turnRight()
  60. end
  61. function goLeftLine()
  62.   turtle.turnLeft()
  63.   goForward(1)
  64.   turtle.turnLeft()
  65. end
  66.  
  67. function harvest()
  68.   for i=1,COUNT do
  69.     goForward(Z_SIZE)
  70.     goRightLine()
  71.     goForward(Z_SIZE)
  72.     goLeftLine()
  73.   end
  74. end
  75.  
  76. function backToChest()
  77.   turtle.turnLeft()
  78.   for i=1,COUNT*3 do
  79.     turtle.forward()
  80.   end
  81.   turtle.turnRight()
  82. end
  83.  
  84. function dropItems(begin_slot, end_slot)
  85.   print('drop items: slots ',begin_slot,'-',end_slot)
  86.   for i=begin_slot, end_slot do
  87.     turtle.select(i)
  88.     turtle.dropDown()
  89.   end
  90.   turtle.select(1)
  91. end
  92.  
  93.  
  94. function fuelItUp()
  95.   -- first of all, refuel() in slot 16
  96.   turtle.select(FUEL_SLOT)
  97.   turtle.refuel()
  98.  
  99.   if (turtle.getFuelLevel() < minFuelLevel) then
  100.     -- suckup fuel items and refuel
  101.     turtle.suckUp()
  102.     turtle.refuel()
  103.     if (turtle.getFuelLevel() < minFuelLevel) then
  104.       -- failed to refuel(), and terminated.
  105.       print("Please insert fuel into slot ",FUEL_SLOT)
  106.       return false
  107.     end
  108.     print('Succeed: fuelItUp()')
  109.   end
  110.   return true
  111. end
  112.  
  113. function isHomePosition()
  114.   return (turtle.detectUp() and turtle.detectDown())
  115. end
  116. -- ###################
  117. -- Main
  118.  
  119. args = {...}
  120. if args and #args == 2 then
  121.   Z_SIZE = tonumber(args[1])
  122.   COUNT = tonumber(args[2])
  123. end
  124. minFuelLevel = ((Z_SIZE + 2 + Z_SIZE + 1) * COUNT) + (3 * COUNT)
  125.  
  126. print(string.format("harvest_millet %d %d", Z_SIZE, COUNT))
  127. print(' FuelLevel (current/needed)')
  128. print('  = ',turtle.getFuelLevel(),'/',minFuelLevel)
  129.  
  130. while true do
  131.   if fuelItUp() == false then return false end
  132.   if isHomePosition() == false then
  133.     print("As I'm not the HomePosition, Terminated. ")
  134.     break
  135.   end
  136.   print('Starting harvest!')
  137.   turtle.select(1)
  138.   harvest()
  139.   backToChest()
  140.   dropItems(1,16)
  141.   print('Finished!')
  142.  
  143.   sleep(1200)
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement