Advertisement
Pinkishu

quarry

Jan 2nd, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. local tArgs = {...}
  2. local dim = tonumber(tArgs[1])
  3. local flip = false
  4.  
  5. an.resetGPS()
  6.  
  7. function readIn(q)
  8.     term.clear()
  9.     term.setCursorPos(1,1)
  10.     print(q)
  11.     return read()
  12. end
  13.  
  14. local mx = readIn("X?")
  15. local my = readIn("Y?")
  16. local mz = readIn("Z?")
  17.  
  18. term.clear()
  19. term.setCursorPos(1,1)
  20. print(mx.."/"..my.."/"..mz.."?")
  21. local inp = read()
  22.  
  23. if inp ~= "y" then error("") end
  24.  
  25. an.setBaseData("fuelSlot",16)
  26. an.setBaseData("navOrigin",vector.new(mx,my,mz))
  27.  
  28. local wps = {}
  29.  
  30. function regWP(name,x,y,z,dir)
  31.     wps[name] = {vector.new(x,y,z),dir}
  32.  
  33. end
  34.  
  35. function moveWP(name,moveOrder)
  36.     an.goToWorld(wps[name][1],moveOrder)
  37.     if wps[name][2] ~= nil then
  38.         an.turnDir(wps[name][2])
  39.     end
  40. end
  41.  
  42. regWP("home",mx,my,mz,1)
  43. regWP("drop_chest",-288,57,39)
  44. regWP("drop_chest_queue",-288,57,55)
  45.  
  46. function goHome()
  47.     moveWP("home",{"y","x","z"})
  48. end
  49.  
  50. function savePos()
  51.     local vec = an.localToWorld(an.getLoc())
  52.     --print("saving: "..vec.y)
  53.     --print("local: "..an.getLoc().y)
  54.     --read()
  55.     regWP("saved",vec.x,vec.y,vec.z,an.getDir())
  56. end
  57.  
  58. function goBack()
  59.     moveWP("saved",{"y","x","z"})
  60.     --print("moved back") read()
  61. end
  62.  
  63.  
  64.  
  65. local quarryBounds = {vector.new(0,0,0),vector.new(dim-1,0,dim-1)}
  66.  
  67. function blockAllowed(blockPos)
  68.     local localPos = an.worldToLocal(blockPos)
  69.     if localPos.x < quarryBounds[1].x or localPos.z < quarryBounds[1].z or localPos.x > quarryBounds[2].x or localPos.z > quarryBounds[2].z then return false end
  70.     if localPos.y >= 0 then return false end
  71.     return true
  72. end
  73.  
  74. function restockFuel()
  75.     if turtle.getItemCount(16) < 64 then
  76.         for i=1,15,1 do
  77.             an.select(16)
  78.             if turtle.compareTo(i) then
  79.                 an.select(i)
  80.                 turtle.transferTo(16)
  81.                 break
  82.             end
  83.         end
  84.     end
  85.     an.select(1)
  86. end
  87.  
  88. function dropOff()
  89.     savePos()
  90.     goHome()
  91.     moveWP("drop_chest_queue",{"y","z","x"})
  92.     moveWP("drop_chest",{"x","z","y"})
  93.     restockFuel()
  94.     for i=1,15,1 do
  95.         an.select(i)
  96.         turtle.dropDown()
  97.     end
  98.     an.select(1)
  99.     goHome()
  100.     goBack()
  101. end
  102.  
  103. function homeDist()
  104.     return math.abs(an.getLoc().y)+10
  105. end
  106.  
  107. function getRowOff()
  108.     local r = an.getLoc().x
  109.     return r
  110. end
  111.  
  112. function getRowEnd(row)
  113.     row = row or getRowOff()
  114.     if (row % 2 == 0 ) then return (flip and 0 or dim-1)
  115.     else return (flip and dim-1 or 0) end
  116. end
  117.  
  118. function translatePos(tPos)
  119.     --print("translate: " .. type(tPos))
  120.     return an.localToWorld(an.getLoc())+tPos
  121. end
  122.  
  123. function tryDig()
  124.     if blockAllowed(translatePos(an.getVecMod())) then
  125.         local r = turtle.dig()
  126.         return true,(r or not turtle.detect() )
  127.     end
  128.     return false
  129. end
  130.  
  131. function tryDigDown()
  132.     if blockAllowed(translatePos(vector.new(0,-1,0))) then
  133.         local r = turtle.digDown()
  134.         return true,(r or not turtle.detectDown())
  135.     end
  136.     return false
  137. end
  138.  
  139. function tryRefuel()
  140.     if turtle.getItemCount(16) > 0 then
  141.         an.select(16)
  142.         local r = turtle.refuel(1)
  143.         if r then return true end
  144.     end
  145.     for i=1,15,1 do
  146.         if turtle.getItemCount(i) > 0 then
  147.             an.select(i)
  148.             local r = turtle.refuel(1)
  149.             if r then return true end
  150.         end
  151.     end
  152.     an.select(1)
  153.     return false
  154. end
  155.  
  156. function waitFuel()
  157.     while true do
  158.         print("Need fuel")
  159.         read()
  160.         local r = tryRefuel()
  161.         if r then break end
  162.     end
  163. end
  164.  
  165. function doRow()
  166.     local row = getRowOff()
  167.     print("clearing row: "..row)
  168.     local rowDir = (flip and 3 or 1)
  169.     if row % 2 == 1 then rowDir = (flip and 1 or 3) end
  170.  
  171.     an.turnDir(rowDir)
  172.     print("check "..an.getLoc().z .. " == "..getRowEnd())
  173.     while an.getLoc().z ~= getRowEnd() do
  174.         --print("MOVE")
  175.         local _,r1 = tryDig()
  176.         local _,r2 = tryDigDown()
  177.         if turtle.getItemCount(15) > 0 then print("dropOff") dropOff() end
  178.         if homeDist() >= turtle.getFuelLevel() then
  179.             print("going home => out of fuel")
  180.             local rf = tryRefuel()
  181.             if not rf then savePos() goHome() waitFuel() goBack() end
  182.         end
  183.         if r1 == false or r2 == false then print("bedrock") return false end
  184.         an.forward()
  185.     end
  186.     print("row ended")
  187.     tryDigDown()
  188.     return true
  189. end
  190.  
  191.  
  192. function exitKey()
  193.     while true do
  194.         local ev,p1 = os.pullEvent("char")
  195.         if ev == "char" and p1 == "x" then break end
  196.     end
  197. end
  198.  
  199. function quarry()
  200.     while true do
  201.         local r = doRow()
  202.         if not r then break end
  203.         --local wPos = an.localToWorld(an.getLoc())
  204.    
  205.         local xMod = 1
  206.         if flip then xMod = -1 end
  207.         if getRowOff() == (flip and 0 or dim-1) then
  208.             local _,rb = tryDigDown()
  209.             if rb == false then break end
  210.             an.down()
  211.             _rb = tryDigDown()
  212.             if rb == false then break end
  213.             an.down()
  214.             flip = not flip
  215.         else
  216.             --print("turning")
  217.             an.turnDir(an.getDirFromVec(vector.new(xMod,0,0)))
  218.             --print("digging")
  219.             tryDig()
  220.             an.goTo(vector.new(xMod,0,0),{"x"})
  221.         end
  222.    
  223.  
  224.     end
  225. end
  226.  
  227. print("starting") read()
  228. parallel.waitForAny(exitKey,quarry)
  229.  
  230. goHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement