Lion4ever

Lava Lake Fuel

Sep 11th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. local x,y,z,fx,fz = 0,0,0,1,0
  2. local map = {[0]={[0]={[0]=true}}}
  3.  
  4. local needReturn = false
  5.  
  6. local function lt()
  7.   turtle.turnLeft()
  8.   fx,fz = fz,-fx
  9. end
  10.  
  11. local function rt()
  12.   turtle.turnRight()
  13.   fx,fz = -fz,fx
  14. end
  15.  
  16. local function fd(n)
  17.   for i=1,n do
  18.     repeat
  19.     until turtle.forward()
  20.     x = x + fx
  21.     z = z + fz
  22.   end
  23. end
  24.  
  25. local function tset(t,i1,i2,i3,v)
  26.  if not t[i1] then
  27.    t[i1] = {}
  28.  end
  29.  if not t[i1][i2] then
  30.    t[i1][i2] = {}
  31.  end
  32.  t[i1][i2][i3] = v
  33. end
  34.  
  35. local function tget(t,i1,i2,i3)
  36.   if not t[i1] or not t[i1][i2] or t[i1][i2][i3] == nil then
  37.     return nil
  38.   end  
  39.   return t[i1][i2][i3]
  40. end
  41.  
  42. --newSpot Helper function
  43. local function c(toCheck,reached,j,nx,ny,nz)
  44.   if tget(reached,j[1]+nx,j[2]+ny,j[3]+nz) == nil and
  45.      tget(map,j[1]+nx,j[2]+ny,j[3]+nz) ~= false and
  46.      (not needReturn or tget(map,j[1]+nx,j[2]+ny,j[3]+nz)) then
  47.     table.insert(toCheck,{j[1]+nx,j[2]+ny,j[3]+nz})
  48.     tset(reached,j[1]+nx,j[2]+ny,j[3]+nz,{unpack(j)})
  49.   end
  50. end
  51.  
  52. local function getPath(reached,j)
  53.   local path = {{unpack(j)}}
  54.   repeat
  55.     table.insert(path,tget(reached,unpack(path[#path])))
  56.   until path[#path] == true
  57.   table.remove(path) --true
  58.   table.remove(path) --current pos
  59.   return path
  60. end
  61.  
  62. local function newSpot()
  63.   local toCheck = {{x,y,z}}
  64.   local reached = {[x]={[y]={[z]=true}}}
  65.   for i,j in ipairs(toCheck) do
  66.     if tget(map,unpack(j)) == nil and not needReturn then
  67.       return getPath(reached,j)
  68.     end
  69.     if needReturn and j[1] == 0 and
  70.                       j[2] == 0 and
  71.                       j[3] == 0 then
  72.       return getPath(reached,j)
  73.     end
  74.     if i % 100 == 0 then
  75.       sleep(0)
  76.     end
  77.     c(toCheck,reached,j,0,1,0)
  78.     c(toCheck,reached,j,fx,0,fz)
  79.     c(toCheck,reached,j,-fz,0,fx)
  80.     c(toCheck,reached,j,-fx,0,-fz)
  81.     c(toCheck,reached,j,fz,0,-fx)
  82.     c(toCheck,reached,j,0,-1,0)
  83.   end
  84.   return getPath(reached,{0,0,0})
  85. end
  86.  
  87. local function faceTo(tx,ty,tz)
  88.   if math.abs(x - tx + z - tz) ~= 1 then
  89.     return
  90.   end
  91.   if x + fz == tx and z - fx == tz then
  92.     lt()
  93.   end
  94.   while x + fx ~= tx or z + fz ~= tz do
  95.     rt()
  96.   end
  97. end
  98.  
  99. local function goTo(tx,ty,tz)
  100.   if y < ty then
  101.     repeat until turtle.up()
  102.     y = y + 1
  103.     return
  104.   end
  105.   if y > ty then
  106.     repeat until turtle.down()
  107.     y = y - 1
  108.     return
  109.   end
  110.   faceTo(tx,ty,tz)
  111.   if x + fx == tx and z + fz == tz then
  112.     fd(1)
  113.   end
  114. end
  115.  
  116. local function checkLimit()
  117.   if turtle.getFuelLevel() + 1000 <= turtle.getFuelLimit() then
  118.     turtle.refuel()
  119.     return false
  120.   end
  121.   local bc = 0
  122.   for i=1,16 do
  123.     if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == "minecraft:bucket" then
  124.       bc = bc + turtle.getItemDetail(i).count
  125.       if bc >=2 then
  126.         turtle.select(i)
  127.         return false
  128.       end
  129.     end
  130.   end
  131.   return true
  132. end
  133.  
  134. local function addToMap(isValid, nx, ny, nz)
  135.   if isValid then
  136.     needReturn = checkLimit()
  137.   end
  138.   tset(map,x + nx, y + ny, z + nz, isValid)
  139. end
  140. local goOn = false
  141. for i=1,16 do
  142.   if turtle.getItemCount(i) > 0 and
  143.      turtle.getItemDetail(i).name == "minecraft:bucket" then
  144.     turtle.select(i)
  145.     goOn = true
  146.     break
  147.   end
  148. end
  149. if not goOn then
  150.   print("No bucket in inventory!")
  151. end
  152. while goOn do
  153.   local path = newSpot()
  154.   for i=#path,2,-1 do
  155.     goTo(unpack(path[i]))
  156.   end
  157.   if #path > 0 and
  158.      (path[1][1] ~= 0 or
  159.      path[1][2] ~= 0 or
  160.      path[1][3] ~= 0) then
  161.     faceTo(unpack(path[1]))
  162.     if path[1][2] > y then
  163.       addToMap(turtle.placeUp(),0,1,0)
  164.     end
  165.     if path[1][2] == y then
  166.       addToMap(turtle.place(),fx,0,fz)
  167.     end
  168.     if path[1][2] < y then
  169.       addToMap(turtle.placeDown(),0,-1,0)
  170.     end
  171.   else
  172.     goTo(0,0,0)
  173.     local a,b = turtle.inspectUp()
  174.     if a and b.name == "minecraft:chest" and needReturn then
  175.       for i=1,16 do
  176.         if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == "minecraft:bucket" then
  177.           turtle.select(i)
  178.           turtle.dropUp()
  179.           break
  180.         end
  181.       end
  182.       for i=1,16 do
  183.         if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == "minecraft:lava_bucket" then
  184.           turtle.select(i)
  185.           turtle.dropUp()
  186.         end
  187.       end
  188.       turtle.suckUp()
  189.       while checkLimit() do
  190.         sleep(10)
  191.         turtle.dropUp()
  192.         turtle.suckUp()
  193.       end
  194.       needReturn = false
  195.     else
  196.       faceTo(1,0,0)
  197.       print("Collected all Lava")
  198.       goOn = false
  199.     end
  200.   end
  201. end
Advertisement
Add Comment
Please, Sign In to add comment