Whitemambaa

Melon Farm

May 15th, 2014
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. --By xXxMoNkEyMaNxXx
  2. --Designed for a 2xN melon farm
  3. --Put the turtle on the right ride facing the farm
  4. --Put a chest of fuel to the right of the turtle
  5. --Put a chest for melons behind the turtle
  6.  
  7.  
  8. local tick=os.clock
  9. local ceil=math.ceil
  10. local floor=math.floor
  11. local max=math.max
  12. local min=math.min
  13.  
  14. local interval=2*60
  15. local efficiency=0.5 --wait for 50% of melons to grow before harvesting
  16. local probability=1/(1+floor(25/(2*0.75)))
  17. local wait=math.log(1-efficiency)/(20*math.log(1-probability*3/(16*16*16)))
  18.  
  19. local farm_len=...
  20. local s=1
  21. turtle.select(s)
  22. local function unload()
  23.     if turtle.detect() then
  24.         for i=1,16 do
  25.             if turtle.getItemCount(i)>0 then
  26.                 turtle.select(i)
  27.                 turtle.drop()
  28.             end
  29.         end
  30.     end
  31. end
  32.  
  33. local function getRealFuel()
  34.     local f=turtle.getFuelLevel()
  35.     return type(f)=="number" and f or math.huge
  36. end
  37.  
  38. local function fillTo(req,slot)
  39.     turtle.select(slot)
  40.     local ret
  41.     local i=0
  42.     local f_0=getRealFuel()
  43.     print("Fuel: "..f_0)
  44.     print("Required: "..req)
  45.     if f_0<req then
  46.             if turtle.detect() then
  47.             print'Refuelling...'
  48.             local t=1
  49.             while turtle.getItemCount(t)>0 and t<=16 do
  50.                 t=t+1
  51.             end
  52.             if t<=16 and turtle.select(t) then
  53.                 while getRealFuel()<req and turtle.suck() do
  54.                     local f0=getRealFuel()
  55.                     local i0=turtle.getItemSpace(t)
  56.                     assert(turtle.refuel(1),"That's not fuel!")
  57.                     local f1=getRealFuel()
  58.                     local i1=turtle.getItemSpace(t)
  59.  
  60.                     local df,di=f1-f0,i1-i0
  61.                     local rate=df/di
  62.                     local f_req=ceil((req-f1)/rate)
  63.  
  64.                     local c=turtle.getItemCount(t)
  65.                     if c>0 then
  66.                         assert(turtle.refuel(min(max(0,f_req),c)),"That's not fuel!")
  67.                     end
  68.                     i=i+turtle.getItemSpace(t)-i0
  69.                     local c=turtle.getItemCount(t)
  70.                     if c>0 then
  71.                         if turtle.drop(c) then
  72.                             break
  73.                         else
  74.                             turtle.turnRight()
  75.                             turtle.turnRight()
  76.                             turtle.drop(c)
  77.                             turtle.turnLeft()
  78.                             turtle.turnLeft()
  79.                         end
  80.                     end
  81.                 end
  82.                 turtle.select(s)
  83.                 local f_1=getRealFuel()
  84.                 print("Gained "..f_1-f_0.." fuel from "..i.." fuel items.")
  85.                 ret=f_1>=req
  86.             else
  87.                 print'Unable to refuel.\n(No room in the turtle)'
  88.                 ret=false
  89.             end
  90.         end
  91.         turtle.select(slot)
  92.     else
  93.         ret=true
  94.     end
  95.     turtle.select(s)
  96.     return ret
  97. end
  98. local unfirst=false
  99. turtle.turnRight()
  100. local runs=0
  101. local fuel=fillTo(2*(farm_len or 2^runs)+2,1)
  102. local start
  103. while fuel do
  104.     turtle.turnLeft()
  105.     --while farm_len and not turtle.detect() do sleep(1) end
  106.     if unfirst then
  107.         local delay=wait+start-tick()
  108.         if delay>0 then
  109.             local show_delay=floor(delay*10)/10
  110.             print("Waiting "..show_delay.." second"..(show_delay==1 and "" or "s")..".")
  111.             sleep(delay%interval)
  112.             for d=floor(delay/interval),1,-1 do
  113.                 print("Harvesting in "..(d*interval/60).." min.")
  114.                 sleep(interval)
  115.             end
  116.         end
  117.     else
  118.         unfirst=true
  119.     end
  120.     start=tick()
  121.     local distance=0
  122.     while farm_len and distance<farm_len or turtle.detectDown() and distance+3<getRealFuel() do
  123.         if turtle.getItemSpace(s)>0 then
  124.             if turtle.detect() then
  125.                 turtle.dig()
  126.             end
  127.             if turtle.forward() then
  128.                 distance=distance+1
  129.             end
  130.         elseif s==16 then
  131.             error'TOO MUCH WATERMELON FUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU'
  132.             unload(2)
  133.             s=2
  134.             turtle.select(2)
  135.         else
  136.             s=s+1
  137.             turtle.select(s)
  138.         end
  139.     end
  140.     if not turtle.detectDown() then
  141.         farm_len=distance-1
  142.     end
  143.     turtle.turnLeft()
  144.     turtle.forward()
  145.     turtle.turnLeft()
  146.     local d=distance
  147.     while d>0 do
  148.         if turtle.getItemSpace(s)>0 then
  149.             if turtle.detect() then
  150.                 turtle.dig()
  151.             end
  152.             if turtle.forward() then
  153.                 d=d-1
  154.             end
  155.         elseif s==16 then
  156.             error'TOO MUCH WATERMELON FUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU'
  157.             unload(2)
  158.             s=2
  159.             turtle.select(2)
  160.         else
  161.             s=s+1
  162.             turtle.select(s)
  163.         end
  164.     end
  165.     turtle.turnLeft()
  166.     turtle.forward()
  167.     turtle.turnRight()
  168.     unload()
  169.     turtle.turnLeft()
  170.     runs=runs+1
  171.     if farm_len then
  172.         fuel=fillTo(2*farm_len+2,1)
  173.     else
  174.         fuel=fillTo(2*2^runs+2,1)
  175.     end
  176. end
  177. turtle.turnLeft()
  178. print'Out of fuel.'
Advertisement
Add Comment
Please, Sign In to add comment