Advertisement
WhiteFire_Sondergaar

Sugarcane

Apr 23rd, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. -- Sugarcane Harvesting
  2. -- By fenthis
  3.  
  4. depth = 15
  5. width = 15
  6. delay = 120 -- Delay in seconds between runs
  7.  
  8.  
  9. total_count = 0
  10.  
  11.  
  12. function do_left_or_right(dir)
  13.     if dir then
  14.         turtle.turnLeft()
  15.     else
  16.         turtle.turnRight()
  17.     end
  18. end
  19.  
  20. function is_odd(n)
  21.     return ((n % 2) == 1)
  22. end
  23.  
  24. function try_n(times)
  25.     for i = 1, times do
  26.         act.try()
  27.     end
  28. end
  29.  
  30. function harvest()
  31.     -- Into first square
  32.     turtle.up()
  33.     act.try()
  34.  
  35.     -- Loop through the field.
  36.     for r = 1,width do
  37.         for x = 1,depth do
  38.             turtle.digDown()
  39.             if x ~= depth then
  40.                 act.try()
  41.             end
  42.         end
  43.  
  44.         if (r ~= width) then
  45.             do_left_or_right(is_odd(r))
  46.             act.try()
  47.             do_left_or_right(is_odd(r))
  48.         end
  49.     end
  50.  
  51.     -- Return
  52.     if not is_odd(width) then
  53.         act.try()
  54.     else
  55.         -- In case it's regrown
  56.         turtle.up()
  57.         turtle.turnLeft()
  58.         turtle.turnLeft()
  59.         try_n(depth)
  60.         turtle.down()
  61.     end
  62.     turtle.turnLeft()
  63.     try_n(width - 1)
  64.     turtle.down()
  65.  
  66.     -- Ok, chest time
  67.     turtle.turnRight()
  68.     count = 0
  69.     for i = 1, 16 do
  70.         items = turtle.getItemCount(i)
  71.         count = count + items
  72.         if items > 0 then
  73.             turtle.select(i)
  74.             turtle.drop()
  75.         end
  76.     end
  77.     total_count = total_count + count
  78.     turtle.turnLeft()
  79.     turtle.turnLeft()
  80.  
  81.     print("Harvest complete, collected " .. count .. " items for a total of " ..
  82.         total_count .. " with " .. turtle.getFuelLevel() .. " fuel remaining.")
  83. end
  84.  
  85. -- Do something
  86. print("Running with a delay of " .. delay .. " seconds. Press Q to quit.")
  87. while true do
  88.     -- First, go for it.
  89.     harvest()
  90.  
  91.     -- Then wait
  92.     os.startTimer(delay)
  93.     repeat
  94.         e, a1 = os.pullEvent()
  95.         if e == "char" and (a1 == "Q" or a1 == "q") then
  96.             print("Quitting!")
  97.             return
  98.         end
  99.     until e == "timer"
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement