Whitemambaa

Enderchest Melon Farm

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