Advertisement
hevohevo

ComputerCraft: right-hand farming0_1

Jun 4th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. -- #############################
  2. -- Right-hand Farming
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- Features:
  7. -- - Implements Right-hand Maze algorithm
  8. -- - Requires redstone signal for detecting right side wall
  9. -- - Turtle can restart from the last interruption, if this program's name is "startup"
  10.  
  11. -- TOP View(o: millets,  R: redstone signal, T: Home position)
  12. -- RRRRRRRRRR
  13. -- RooRooRooR
  14. -- RooRooRooR
  15. -- RooRooRooR
  16. -- RooRooRooR
  17. -- R        R
  18. -- RRRRRRRRTR
  19. --        RRR
  20.  
  21. -- config
  22. MIN_FUEL_LEVEL = 1000
  23. SLEEP_TIME = 300
  24. -- functions
  25. function count_items()
  26.   local sum = 0
  27.   for i=1,16 do
  28.     sum = sum + turtle.getItemCount(i)
  29.   end
  30.   return sum
  31. end
  32.  
  33. function drop_all()
  34.   for i=1,16 do
  35.     turtle.select(i)
  36.     turtle.dropUp()
  37.   end
  38.   turtle.select(1)
  39. end
  40.  
  41. function fwd_and_dig()
  42.   if rs.getInput("front") then return end
  43.   turtle.dig()
  44.   turtle.forward()
  45.   turtle.digDown()
  46. end
  47.  
  48.  
  49. function get_fuel()
  50.   if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
  51.     turtle.suck()
  52.     turtle.refuel()
  53.     if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
  54.       error("More fuel!")
  55.     end
  56.   end
  57. end
  58.  
  59. -- main
  60. turtle.select(1)
  61.  
  62. while true do
  63. while true do
  64.   -- Home Position
  65.   if turtle.detectUp() and count_items() > 0 then
  66.     print('drop')
  67.     drop_all()
  68.     get_fuel()
  69.     turtle.turnRight()
  70.     turtle.turnRight()
  71.     os.sleep(SLEEP_TIME)
  72.     fwd_and_dig()
  73.     break
  74.   end
  75.  
  76.   if not rs.getInput("right") then
  77.     turtle.turnRight()
  78.     fwd_and_dig()
  79.     break
  80.   end
  81.  
  82.   if rs.getInput("front") then
  83.     turtle.turnLeft()
  84.     fwd_and_dig()
  85.     break
  86.   end
  87.  
  88.   fwd_and_dig()
  89. end
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement