Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --By xXxMoNkEyMaNxXx
- --Slot 1 is for a fuel supply enderchest
- --Slot 2 is for a melon unloading enderchest
- local ceil=math.ceil
- local max=math.max
- local min=math.min
- local farm_len=...
- local s=3
- turtle.select(3)
- local function unload(slot)
- turtle.select(slot)
- if (not turtle.detect() or turtle.dig()) and turtle.place() then
- for i=1,16 do
- if i~=slot and turtle.getItemCount(i)>0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- turtle.select(slot)
- turtle.dig()
- end
- turtle.select(s)
- end
- local function getRealFuel()
- local f=turtle.getFuelLevel()
- return type(f)=="number" and f or math.huge
- end
- local function fillTo(slot,req)
- turtle.select(slot)
- local ret
- if (not turtle.detect() or turtle.dig()) and turtle.place() then
- local i=0
- local f_0=getRealFuel()
- print("Fuel: "..f_0)
- print("Required: "..req)
- if f_0<req then
- print'Refuelling...'
- local t=1
- while turtle.getItemCount(t)>0 and t<=16 do
- t=t+1
- end
- if t<=16 and turtle.select(t) then
- while getRealFuel()<req and turtle.suck() do
- local f0=getRealFuel()
- local i0=turtle.getItemSpace(t)
- assert(turtle.refuel(1),"That's not fuel!")
- local f1=getRealFuel()
- local i1=turtle.getItemSpace(t)
- local df,di=f1-f0,i1-i0
- local rate=df/di
- local f_req=ceil((req-f1)/rate)
- local c=turtle.getItemCount(t)
- if c>0 then
- assert(turtle.refuel(min(max(0,f_req),c)),"That's not fuel!")
- end
- i=i+turtle.getItemSpace(t)-i0
- local c=turtle.getItemCount(t)
- if c>0 then
- if turtle.drop(c) then
- break
- else
- turtle.turnRight()
- turtle.turnRight()
- turtle.drop(c)
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- end
- turtle.select(s)
- local f_1=getRealFuel()
- print("Gained "..f_1-f_0.." fuel from "..i.." fuel items.")
- ret=f_1>=req
- else
- print'Unable to refuel.\n(No room in the turtle)'
- ret=false
- end
- else
- ret=true
- end
- turtle.select(slot)
- turtle.dig()
- end
- turtle.select(s)
- return ret
- end
- turtle.turnRight()
- local runs=0
- local fuel=fillTo(2*(farm_len or 2^runs)+2,1)
- while fuel do
- turtle.turnLeft()
- while farm_len and not turtle.detect() do sleep(1) end
- local distance=0
- while farm_len and distance<farm_len or turtle.detectDown() and distance+3<getRealFuel() do
- if turtle.getItemSpace(s)>0 then
- if turtle.detect() then
- turtle.dig()
- end
- if turtle.forward() then
- distance=distance+1
- end
- elseif s==15 then
- unload(2)
- s=2
- turtle.select(2)
- else
- s=s+1
- turtle.select(s)
- end
- end
- if not turtle.detectDown() then
- farm_len=distance-1
- end
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- local d=distance
- while d>0 do
- if turtle.getItemSpace(s)>0 then
- if turtle.detect() then
- turtle.dig()
- end
- if turtle.forward() then
- d=d-1
- end
- elseif s==15 then
- unload(2)
- s=2
- turtle.select(2)
- else
- s=s+1
- turtle.select(s)
- end
- end
- turtle.turnLeft()
- turtle.forward()
- runs=runs+1
- if farm_len then
- fuel=fillTo(2*farm_len+2,1)
- else
- fuel=fillTo(2*2^runs+2,1)
- end
- end
- turtle.turnLeft()
- print'Out of fuel.'
Advertisement
Add Comment
Please, Sign In to add comment