Whitemambaa

Melon farm.Lua

Dec 20th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. --By xXxMoNkEyMaNxXx
  2. local ceil=math.ceil
  3. local max=math.max
  4. local min=math.min
  5. local farm_len=...
  6. local s=3
  7. turtle.select(3)
  8. local function unload(slot)
  9.     turtle.select(slot)
  10.     if (not turtle.detect() or turtle.dig()) and turtle.place() then
  11.         for i=1,16 do
  12.             if i~=slot and turtle.getItemCount(i)>0 then
  13.                 turtle.select(i)
  14.                 turtle.drop()
  15.             end
  16.         end
  17.         turtle.select(slot)
  18.         turtle.dig()
  19.     end
  20.     turtle.select(s)
  21. end
  22.  
  23. local function getRealFuel()
  24.     local f=turtle.getFuelLevel()
  25.     return type(f)=="number" and f or math.huge
  26. end
  27.  
  28. local function fillTo(slot,req)
  29.     turtle.select(slot)
  30.     local ret
  31.     if (not turtle.detect() or turtle.dig()) and turtle.place() then
  32.         local i=0
  33.         local f_0=getRealFuel()
  34.         print("Fuel: "..f_0)
  35.         print("Required: "..req)
  36.         if f_0<req then
  37.             print'Refuelling...'
  38.             local t=1
  39.             while turtle.getItemCount(t)>0 and t<=16 do
  40.                 t=t+1
  41.             end
  42.             if t<=16 and turtle.select(t) then
  43.                 while getRealFuel()<req and turtle.suck() do
  44.                     local f0=getRealFuel()
  45.                     local i0=turtle.getItemSpace(t)
  46.                     assert(turtle.refuel(1),"That's not fuel!")
  47.                     local f1=getRealFuel()
  48.                     local i1=turtle.getItemSpace(t)
  49.  
  50.                     local df,di=f1-f0,i1-i0
  51.                     local rate=df/di
  52.                     local f_req=ceil((req-f1)/rate)
  53.  
  54.                     local c=turtle.getItemCount(t)
  55.                     if c>0 then
  56.                         assert(turtle.refuel(min(max(0,f_req),c)),"That's not fuel!")
  57.                     end
  58.                     i=i+turtle.getItemSpace(t)-i0
  59.                     local c=turtle.getItemCount(t)
  60.                     if c>0 then
  61.                         if turtle.drop(c) then
  62.                             break
  63.                         else
  64.                             turtle.turnRight()
  65.                             turtle.turnRight()
  66.                             turtle.drop(c)
  67.                             turtle.turnLeft()
  68.                             turtle.turnLeft()
  69.                         end
  70.                     end
  71.                 end
  72.                 turtle.select(s)
  73.                 local f_1=getRealFuel()
  74.                 print("Gained "..f_1-f_0.." fuel from "..i.." fuel items.")
  75.                 ret=f_1>=req
  76.             else
  77.                 print'Unable to refuel.\n(No room in the turtle)'
  78.                 ret=false
  79.             end
  80.         else
  81.             ret=true
  82.         end
  83.         turtle.select(slot)
  84.         turtle.dig()
  85.     end
  86.     turtle.select(s)
  87.     return ret
  88. end
  89. turtle.turnRight()
  90. local runs=0
  91. local fuel=fillTo(2*(farm_len or 2^runs)+2,1)
  92. while fuel do
  93.     turtle.turnLeft()
  94.     while farm_len and not turtle.detect() do sleep(1) end
  95.     local distance=0
  96.     while farm_len and distance<farm_len or turtle.detectDown() and distance+3<getRealFuel() do
  97.         if turtle.getItemSpace(s)>0 then
  98.             if turtle.detect() then
  99.                 turtle.dig()
  100.             end
  101.             if turtle.forward() then
  102.                 distance=distance+1
  103.             end
  104.         elseif s==15 then
  105.             unload(2)
  106.             s=2
  107.             turtle.select(2)
  108.         else
  109.             s=s+1
  110.             turtle.select(s)
  111.         end
  112.     end
  113.     if not turtle.detectDown() then
  114.         farm_len=distance-1
  115.     end
  116.     turtle.turnLeft()
  117.     turtle.forward()
  118.     turtle.turnLeft()
  119.     local d=distance
  120.     while d>0 do
  121.         if turtle.getItemSpace(s)>0 then
  122.             if turtle.detect() then
  123.                 turtle.dig()
  124.             end
  125.             if turtle.forward() then
  126.                 d=d-1
  127.             end
  128.         elseif s==15 then
  129.             unload(2)
  130.             s=2
  131.             turtle.select(2)
  132.         else
  133.             s=s+1
  134.             turtle.select(s)
  135.         end
  136.     end
  137.     turtle.turnLeft()
  138.     turtle.forward()
  139.     runs=runs+1
  140.     if farm_len then
  141.         fuel=fillTo(2*farm_len+2,1)
  142.     else
  143.         fuel=fillTo(2*2^runs+2,1)
  144.     end
  145. end
  146. turtle.turnLeft()
  147. print'Out of fuel.'
Advertisement
Add Comment
Please, Sign In to add comment