Advertisement
Descaii

Quarry

Feb 2nd, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.33 KB | None | 0 0
  1. Blacklist = {}
  2. Whitelist = {}
  3. function Black(...)
  4.     local stuff = {...}
  5.     for i,v in pairs(stuff) do Blacklist[tostring(v):lower()]=true end
  6. end
  7. function White(...)
  8.     local stuff = {...}
  9.     for i,v in pairs(stuff) do Whitelist[tostring(v):lower()]=true end
  10. end
  11. Black("Copper","Dirt","Stone","Gravel","Flint","Sand","Rack","Apatite")
  12. White("Red")
  13. Position = {x=0,y=0,z=0}
  14. Forward = {x=1,y=0,z=0}
  15. function OnBlack(tx)
  16.     for i,v in pairs(Blacklist) do
  17.         if tx:find(i) then
  18.             return true
  19.         end
  20.     end
  21. end
  22. function OnWhite(tx)
  23.     for i,v in pairs(Whitelist) do
  24.         if tx:find(i) then
  25.             return true
  26.         end
  27.     end
  28. end
  29. function Dig(dir)
  30.     if dir == "forward" then
  31.         if turtle.detect() then repeat turtle.dig() until not turtle.detect() end
  32.     end
  33.     if dir == "up" then
  34.         if turtle.detectUp() then repeat turtle.digUp() until not turtle.detectUp() end
  35.     end
  36.     if dir == "down" then
  37.         if turtle.detectDown() then repeat turtle.digDown() until not turtle.detectDown() end
  38.     end
  39. end
  40. function ShiftOffsetForward()
  41.     Position.x=Position.x+Forward.x
  42.     Position.y=Position.y+Forward.y
  43.     Position.z=Position.z+Forward.z
  44. end
  45. function shift(direction)
  46.     local x,z = Forward.x,Forward.z
  47.     if direction=="right" then
  48.         Forward.x = (z == 1 and -1) or (z == -1 and 1) or (x ~= 0 and 0)
  49.         Forward.z = (x == 1 and 1) or (x == -1 and -1) or (z ~= 0 and 0)
  50.     end
  51.     if direction=="left" then
  52.         Forward.z = (x == 1 and -1) or (x == -1 and 1) or (z ~= 0 and 0)
  53.         Forward.x = (z == 1 and 1) or (z == -1 and -1) or (x ~= 0 and 0)
  54.     end
  55. end
  56. function GetDirection()
  57.     if Forward.x==1 then
  58.         return "forward"
  59.     elseif Forward.x==-1 then
  60.         return "backwards"
  61.     elseif Forward.z==1 then
  62.         return "right"
  63.     elseif Forward.z==-1 then
  64.         return "left"
  65.     end
  66. end
  67. function IsFull()
  68.     local f = true
  69.     for i = 1,16 do
  70.         if turtle.getItemCount(i) == 0 then
  71.             f=false
  72.         end
  73.     end
  74.     return f
  75. end
  76. function SetDirection(dir)
  77.     local DIR = GetDirection()
  78.     if DIR~=dir:lower() then
  79.         local l,r = 0,0
  80.         if DIR == "left" then
  81.             l,r = ((dir=="backwards"and 1)or 0),((dir=="forward"and 1)or(dir=="right"and 2)or 0)
  82.         elseif DIR == "right" then
  83.             l,r = ((dir=="forward"and 1)or 0),((dir=="backwards"and 1)or(dir=="left"and 2)or 0)
  84.         elseif DIR == "backwards" then
  85.             l,r = ((dir=="right"and 1)or 0),((dir=="left"and 1)or(dir=="forward"and 2)or 0)
  86.         elseif DIR == "forward" then
  87.             l,r = ((dir=="left"and 1)or 0),((dir=="right"and 1)or(dir=="backwards"and 2)or 0)
  88.         end
  89.         for i = 1,r do shift("right") turtle.turnRight() end
  90.         for i = 1,l do shift("left") turtle.turnLeft() end
  91.     end
  92. end
  93. function Move(direction)
  94.     CheckFuel()
  95.     local tcheck = false
  96.     if direction =="up" then
  97.         Dig("up")
  98.         repeat local t,e = turtle.up() if t then tcheck=true else Display("Error: Movement obstructed","Error Details: "..tostring(e)) Dig("up") end until tcheck
  99.         Position.y=Position.y+1
  100.     elseif direction == "down" then
  101.         Dig("down")
  102.         repeat local t,e = turtle.down() if t then tcheck=true else Display("Error: Movement obstructed","Error Details: "..tostring(e)) Dig("down") end until tcheck
  103.         Position.y=Position.y-1
  104.     else
  105.         SetDirection(direction)
  106.         if GetDirection()==direction then
  107.             Dig("forward")
  108.             repeat local t,e = turtle.forward() if t then tcheck=true else Display("Error: Movement obstructed","Error Details: "..tostring(e)) Dig("forward") end until tcheck
  109.             ShiftOffsetForward()
  110.         end
  111.     end
  112. end
  113. function GoTo(x,y,z)
  114.     local x,y,z = x,y,z
  115.     repeat
  116.         if Position.x>x then
  117.             Move("backwards")
  118.         elseif Position.x<x then
  119.             Move("forward")
  120.         end
  121.     until Position.x == x
  122.     repeat
  123.         if Position.z<z then
  124.             Move("right")
  125.         elseif Position.z>z then
  126.             Move("left")
  127.         end
  128.     until Position.z == z
  129.     repeat
  130.         if Position.y<y then
  131.             Move("up")
  132.         elseif Position.y>y then
  133.             Move("down")
  134.         end
  135.     until Position.y == y
  136. end
  137. function DepositAll()
  138.     for i = 1,16 do
  139.         turtle.select(i)
  140.         turtle.drop(turtle.getItemCount(i))
  141.     end
  142. end
  143. function DepositExtras()
  144.     local f = true
  145.     for i = 1,16 do
  146.         local name = turtle.getItemDetail(i).name:lower()
  147.         if turtle.getItemCount(i)>0 and OnBlack(name) and not OnWhite(name) then
  148.             f = false
  149.             turtle.select(i)
  150.             turtle.drop(turtle.getItemCount(i))
  151.         end
  152.     end
  153.     return f
  154. end
  155. function Display(...)
  156.     local tab = {...}
  157.     local n = 0
  158.     term.clear()
  159.     for i,v in pairs(tab) do
  160.         n = n+1
  161.         term.setCursorPos(1,n)
  162.         print(v)
  163.     end
  164. end
  165. function CheckFuel()
  166.     if turtle.getFuelLevel()==0 then
  167.         repeat
  168.             sleep(1)
  169.             Display("Place fuel in selected slot.")
  170.         until
  171.             turtle.getItemCount(turtle.getSelectedSlot())>0 and turtle.getItemDetail(turtle.getSelectedSlot()).name:find("coal")
  172.             turtle.refuel(turtle.getItemCount(turtle.getSelectedSlot()))
  173.     end
  174. end
  175. function Quarry(x,y,z)
  176.     local StartPos = {x=Position.x,y=Position.y,z=Position.z}
  177.     local TDirection = ""
  178.     local TPos = {x=0,y=0,z=0}
  179.     local ax,ay,az = x,y,z
  180.     local Progress = 0
  181.     for y = 1,y/3 do
  182.         for i = 1,(y~=1 and 3 or 2) do
  183.             Progress = Progress+1
  184.             Move("down")
  185.         end
  186.         for x = 1,x/2 do
  187.             for z = 1,z-1 do
  188.                 Progress = Progress+3
  189.                 Display("Creating Quarry","Progress: "..math.ceil((Progress/(ax*ay*az))*100).."%","Fuel: "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
  190.                 Dig("up")
  191.                 Dig("down")
  192.                 Move("right")
  193.                 if IsFull() and DepositExtras() then
  194.                     TPos = {x=Position.x,y=Position.y,z=Position.z}
  195.                     TDirection = GetDirection()
  196.                     GoTo(0,0,0)
  197.                     SetDirection("backwards")
  198.                     DepositAll()
  199.                     GoTo(TPos.x,TPos.y,TPos.z)
  200.                     SetDirection(TDirection)
  201.                 end
  202.             end
  203.             Dig("up")
  204.             Dig("down")
  205.             if Position.x~=ax-1 then
  206.                 Progress = Progress+1
  207.                 Move("forward")
  208.             end
  209.             for z = 1,z-1 do
  210.                 Progress = Progress+3
  211.                 Display("Creating Quarry","Progress: "..math.ceil((Progress/(ax*ay*az))*100).."%","Fuel: "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
  212.                 Dig("up")
  213.                 Dig("down")
  214.                 Move("left")
  215.                 if IsFull() and DepositExtras() then
  216.                     TPos = {x=Position.x,y=Position.y,z=Position.z}
  217.                     TDirection = GetDirection()
  218.                     GoTo(0,0,0)
  219.                     SetDirection("backwards")
  220.                     DepositAll()
  221.                     GoTo(TPos.x,TPos.y,TPos.z)
  222.                     SetDirection(TDirection)
  223.                 end
  224.             end
  225.             Dig("up")
  226.             Dig("down")
  227.             if Position.x~=ax-1 then
  228.                 Progress = Progress+1
  229.                 Move("forward")
  230.             end
  231.         end
  232.         GoTo(StartPos.x,Position.y,StartPos.z)
  233.     end
  234.     GoTo(0,0,0)
  235.     SetDirection("backwards")
  236.     DepositAll()
  237.     SetDirection("forward")
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement