Advertisement
meigrafd

Mining Turtle

Jun 11th, 2017
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.72 KB | None | 0 0
  1. --[[
  2.  
  3.  GPS Deploy: http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-11-updated-04012014/page__view__findpost__p__264468
  4.  pastebin get VXAyXqBv gpsDeploy
  5.  
  6.  
  7.  Special Turtle Script which mines everything(!) it can. IMPORTEND: Setup an special mining-Dimension (with RFTools)!!
  8.  Requires an already active GPS-System and NoFuel Mode.
  9.  Digging down till bedrock and ignores bedrock (y = ~4)
  10.  
  11.  Maybe you must craft a 'Wireless Mining Turtle' yourself, but dont use an Advanced Turtle.
  12.  Place the Turtle so that a drop-chest is behind.
  13.  
  14. --]]
  15.  
  16.  
  17.  
  18. function getDirection()
  19.     local f = 0
  20.     local x, y, z = gps.locate(5,false)
  21.     y = nil
  22.     if not turtle.forward() then
  23.         error("I am against a wall.")
  24.     end
  25.     local newx, newy, newz = gps.locate(5,false)
  26.     newy = nil
  27.     turtle.back()
  28.     --f: South=0, West=1, North=2, East=3
  29.     if newz > z then
  30.         f = 0
  31.     elseif newx < x then
  32.         f = 1
  33.     elseif newz < z then
  34.         f = 2
  35.     elseif newx > x then
  36.         f = 3
  37.     end
  38.     return f
  39. end
  40.  
  41. -- ausrichten
  42. -- Receive an absolute directon and does the propiate turns
  43. function align(newDir)
  44.     local rest = math.abs(currentPos.f - newDir)
  45.     while currentPos.f ~= newDir do
  46.         if (rest > 2 and currentPos.f < newDir) or (rest <= 2 and currentPos.f > newDir) then
  47.             turtle.turnLeft()
  48.             currentPos.f = currentPos.f - 1
  49.         else  
  50.             turtle.turnRight()
  51.             currentPos.f = currentPos.f + 1
  52.         end
  53.         if currentPos.f > 3 then
  54.             currentPos.f = 0
  55.         elseif currentPos.f < 0 then
  56.             currentPos.f = 3
  57.         end
  58.     end
  59. end
  60.  
  61.  
  62.  
  63. function forward() --Forward movement
  64.     --Move forward
  65.     local i = 0 --Iterator for bedrock/strong player detection
  66.     while not turtle.forward() do
  67.         if not turtle.dig() then --Detect blocks
  68.             i = i + 1
  69.             turtle.attack() --Detect entities
  70.             if i == 30 then
  71.                 return false --If movement fails
  72.             end
  73.         end
  74.     end
  75.     --Clear above and below
  76.     while turtle.detectUp() do
  77.         turtle.digUp()
  78.     end
  79.     while turtle.detectDown() do
  80.         turtle.digDown()
  81.     end
  82.     --Position tracking
  83.     if currentpos.f == 0 then
  84.         currentpos.z = currentpos.z + 1
  85.     elseif currentpos.f == 1 then
  86.         currentpos.x = currentpos.x - 1
  87.     elseif currentpos.f == 2 then
  88.         currentpos.z = currentpos.z - 1
  89.     elseif currentpos.f == 3 then
  90.         currentpos.x = currentpos.x + 1
  91.     else
  92.         running = false
  93.         error("Something went wrong with the direction :P/>/>/>/>/>/>")
  94.     end
  95.     return true
  96. end
  97.  
  98. --Right turn with position tracking
  99. function turnRight()
  100.     turtle.turnRight()
  101.     if currentpos.f < 3 then
  102.         currentpos.f = currentpos.f + 1
  103.     else
  104.         currentpos.f = 0
  105.     end
  106. end
  107.  
  108. --Left turn with position tracking
  109. function turnLeft()
  110.     turtle.turnLeft()
  111.     if currentpos.f > 0 then
  112.         currentpos.f = currentpos.f - 1
  113.     else
  114.         currentpos.f = 3
  115.     end
  116. end
  117.  
  118. --Downward movement
  119. function down()
  120.     --Move down
  121.     local i = 0 --Iterator for bedrock detection
  122.     while not turtle.down() do
  123.         if not turtle.digDown() then --Detect blocks
  124.             i = i + 1
  125.             turtle.attackDown() --Detect entities
  126.             if i == 25 then
  127.                 return false --If movement fails
  128.             end
  129.         end
  130.     end
  131.     --Position tracking
  132.     currentpos.y = currentpos.y - 1
  133.     if serverID ~= -1 then
  134.         rednet.send(serverID,textutils.serialize({Type = "PosUpdate",Doen=currentpos.y}))
  135.   end
  136.     return true
  137. end
  138.  
  139.  
  140.  
  141.  
  142.  
  143. --Drop mined resources, display amounts
  144. function dropAll()
  145.     local mined = 0
  146.     turtle.turnRight()
  147.     turtle.turnRight()
  148.     for i=1,16 do
  149.         turtle.select(i)
  150.         mined = mined + turtle.getItemCount(i)
  151.         turtle.drop()
  152.     end
  153.     --This will send to rednet soon
  154.     totalMined = totalMined + mined
  155.     print("Minerals mined this run: "..mined)
  156.     print("Total mined: "..totalMined)
  157.     turtle.select(1)
  158.     turtle.turnRight()
  159.     turtle.turnRight()
  160. end
  161.  
  162.  
  163.  
  164. -----------------------------------------------------------------------------
  165.  
  166.  
  167. args = { ... }
  168. term.clear()
  169. term.setCursorPos(1,1)
  170. print("   ---  Mining program  ---")
  171. totalMined = 0 --Total mined out blocks
  172. done = false --Whether turtle has completed
  173. running = true --Whether turtle is currently digging
  174. w, l, d = 0, 0, 0 --Width, length, depth of hole.
  175. currentPos = {} --Current position storage. It's a table just because it is easier
  176. currentPos.x, currentPos.y, currentPos.z, currentPos.f = 0, 0, 0, 0 --Initialise pieces of current position
  177. edge = {} --Boundaries of hole. Same deal as currentpos
  178. -- Directions
  179. South, West, North, East, Up, Down = 0, 1, 2, 3, 4, 5
  180. directionString = { South=0, West=1, North=2, East=3 }
  181. local shortNames = {[North] = "N", [West] = "W", [South] = "S", [East] = "E", [Up] = "U", [Down] = "D" }
  182.  
  183.  
  184. -- Get Startup Direction
  185. currentPos.f = getDirection()
  186.  
  187. -- save current position as start/drop-zone, or even use already saved..
  188. startPos = {}
  189. if not fs.exists(".startzone") then
  190.     startPos.X, startPos.Y, startPos.Z = gps.locate(5,false)
  191.     startPos.F = currentPos.f
  192.     local file = fs.open(".startzone", "w")
  193.     file.write(textutils.serialize(startPos))
  194.     file.close()
  195. else
  196.     local file = fs.open(".startzone", "r")
  197.     startPos = textutils.unserialize(file.readAll())
  198.     file.close()
  199. end
  200.  
  201. -- Current coords:
  202. x,y,z = gps.locate(5,false)
  203. if not x or not y or not z then
  204.     error("Out of GPS range")
  205. end
  206. currentPos.x = x
  207. currentPos.y = y
  208. currentPos.z = z
  209.  
  210. -- last mining coords / mining taring coords:
  211. xWork = x
  212. yWork = y
  213. zWork = z
  214.  
  215.  
  216. --If a square hole is wanted
  217. if #args == 2 and tonumber(args[1]) > 1 and tonumber(args[2]) > 2 then
  218.     --Width, length, depth of hole
  219.     w = tonumber(args[1])
  220.     l = w
  221.     d = tonumber(args[2])
  222. --If a non-square hole is wanted
  223. elseif #args == 3 and tonumber(args[1]) > 1 and tonumber(args[2]) > 1 and tonumber(args[3]) > 2 then
  224.     w = tonumber(args[1])
  225.     l = tonumber(args[2])
  226.     d = tonumber(args[3])
  227. --If arguments improperly input, print usage
  228. else
  229.     print("Usage: \"miner <side> <depth>\" or \"miner <width> <length> <depth>\"")
  230.     print("Note: depth must be at least 3.")
  231.     error()
  232. end
  233.  
  234. --Edge locations
  235. edge.x = w - 1
  236. edge.y = -(d - 1)
  237. edge.z = l - 1
  238.  
  239. print("Digging a hole "..w.." by "..l.." by "..d.." meters.")
  240.  
  241.  
  242.  
  243. -----------------------------------------------------------------------------------
  244.  
  245.  
  246.  
  247.  
  248.  
  249. function rt(ct)
  250.     ct = ct or 1
  251.     for i=1,ct do
  252.         turtle.turnRight()
  253.         f = (f+1)%4
  254.     end
  255.     return true
  256. end
  257. function lt(ct)
  258.     ct = ct or 1
  259.     for i=1,ct do
  260.         turtle.turnLeft()
  261.         f = (f+3)%4
  262.     end
  263.     return true
  264. end
  265.  
  266. -- Receive an absolute directon and does the propiate turns
  267. function face(d)
  268.     if type(d) == "string" then
  269.         dirs = {north=0, east=1, south=2, west=3}
  270.         d = dirs[d]
  271.     elseif type(d) == "number" then
  272.         if d < 0 or d > 4 then
  273.             error("Direction number out of range (0-3).",2)
  274.         end
  275.     end
  276.     if d == nil then error("No direction specified.",2) end
  277.     -- F 0=North, 1=East, 2=South, 3=West
  278.     -- South= +z, East= +x
  279.     local dir = -1
  280.     if dirString[d] ~= nil then
  281.         dir = dirString[d]
  282.     else
  283.         dir = d
  284.     end
  285.  
  286.     if dir == 0 and f == 3 then
  287.         rt()
  288.     elseif dir == 3 and f == 0 then
  289.         lt()
  290.     elseif dir > f then
  291.         rt(dir - f)
  292.     elseif dir < f then
  293.         lt(f - dir)
  294.     end
  295. end
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. function movX(tox)
  304.                 -- Moves the turle to tox in X axix
  305.                 if x > tox then
  306.                                 gira(0)
  307.                 elseif x < tox then
  308.                                 gira(2)
  309.                 end
  310.                 while x ~= tox do
  311.                                 while turtle.detect() do
  312.                                                 turtle.dig()
  313.                                 end
  314.                                 turtle.forward()
  315.                                 if lado == 0 then
  316.                                                 x=x-1
  317.                                 else
  318.                                                 x=x+1
  319.                                 end
  320.                 end
  321. end
  322. function movY(toy)
  323.                 -- Moves the turle to toy in Y axix
  324.                 while y < toy do
  325.                                 if turtle.detectDown() then
  326.                                                 turtle.digDown()
  327.                                 end
  328.                                                 turtle.down()
  329.                                                 y=y+1;
  330.                 end
  331.                 while y > toy do
  332.                                 while turtle.detectUp() do
  333.                                                 turtle.digUp()
  334.                                 end
  335.                                 if turtle.up() then
  336.                                                 y=y-1;
  337.                                 else
  338.                                                 finaliza=true
  339.                                                 break;
  340.                                 end
  341.                 end
  342. end
  343. function movZ(toz)
  344.                 -- Moves the turle to toz in Z axix
  345.                 if z > toz then
  346.                                 gira(3)
  347.                 elseif z < toz then
  348.                                 gira(1)
  349.                 end
  350.                 while z ~= toz do
  351.                                 while turtle.detect() do
  352.                                                 turtle.dig()
  353.                                 end
  354.                                 turtle.forward()
  355.                                 if lado == 1 then
  356.                                                 z=z+1
  357.                                 else
  358.                                                 z=z-1
  359.                                 end
  360.                 end
  361. end
  362. function mueveCords(tox,toy,toz,dir)
  363.                 -- Moves the turtle to the spcified coords mining everithing in its way.
  364.                 modY = 0
  365.                 if dir == "home" then
  366.                                 if y-2 >= toy and y-2 >= minY then
  367.                                                 modY=2
  368.                                                 movY(y-modY)
  369.                                 end
  370.                                 -- x:
  371.                                 movX(tox)
  372.                                 -- z:
  373.                                 movZ(toz)
  374.                                 -- y:
  375.                                 movY(toy)
  376.                 else
  377.                                 if y <= toy-2 and toy-2 >= minY then
  378.                                                 modY=2;
  379.                                 end
  380.                                 movY(toy-modY)
  381.                                 -- z:
  382.                                 movZ(toz)
  383.                                 -- x:
  384.                                 movX(tox)
  385.                                 -- y:
  386.                                 movY(toy)
  387.                 end
  388. end
  389.  
  390. -- Checks the slots. If some slot is full, it will go home to drop everything
  391. function checkInventory()
  392.     local tmp = 9
  393.     local full = false
  394.     while tmp > 0 do
  395.         if turtle.getItemSpace(tmp) == 64 then
  396.             full = true
  397.         else
  398.             tmp = tmp - 1
  399.         end
  400.     end
  401.    
  402.     if full then
  403.         xWork = x
  404.         yWork = y
  405.         zWork = z
  406.         goHome()
  407.         descarga()
  408.         goWork()
  409.     end
  410. end
  411.  
  412. -- Drops everithing
  413. function descarga()
  414.     print("Dropping items...")
  415.     local tmp = 9
  416.     while tmp > 0 do
  417.         turtle.select(tmp)
  418.         if turtle.getItemCount(tmp) > 0 then
  419.             turtle.drop(turtle.getItemCount(tmp))
  420.         end
  421.         tmp = tmp - 1
  422.     end
  423. end
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447. ------------------------------------------ old
  448.  
  449.  
  450. --Retrieve variables/Instantiate Values
  451. local args = {...}
  452.  
  453. --First argument = how long tunnel is
  454. local dist = tonumber(args[1])
  455.  
  456. --Second argument = What direction the tunnels go
  457. local direct = tostring(args[2])
  458.  
  459. --Mines out blocks in front, above, left and right [Moves forward when clear]
  460. local function mineTunnel()
  461.     if turtle.detect() then
  462.         turtle.dig()
  463.     end
  464.     if turtle.detectUp() then
  465.         turtle.digUp()
  466.     end
  467.     if turtle.turnLeft() then
  468.         turtle.dig()
  469.         turtle.turnRight()
  470.     end
  471.     if turtle.turnRight() then
  472.         turtle.dig()
  473.         turtle.turnLeft()
  474.     end
  475.     -- Digs until the turtle can move forward.
  476.     while not turtle.forward() do
  477.         turtle.dig()
  478.         turtle.digUp()
  479.     end
  480. end
  481.  
  482. --Digs a tunnel given distance args[0] (requires args[0])
  483. local function tunnel()
  484.     for i=1,dist,1 do
  485.         mineTunnel()
  486.     end
  487.  
  488.     if direct == "left" then
  489.         turtle.turnLeft()
  490.         mineTunnel()
  491.         turtle.turnLeft()
  492.     end
  493.     if direct == "right" then
  494.         turtle.turnRight()
  495.         mineTunnel()
  496.         turtle.turnRight()
  497.     end
  498.  
  499.     for i=1,dist,1 do
  500.         mineTunnel()
  501.     end
  502. end
  503.  
  504. --Executes until told to stop
  505. while true do
  506. tunnel()
  507. turtle.select(1)
  508. turtle.dig()
  509. turtle.place()
  510. for slot=2,16,1 do
  511.   turtle.select(slot)
  512.   while turtle.getItemCount(slot) > 0 do
  513.     turtle.drop()
  514.   end
  515. end
  516.  
  517. if direct == "left" then
  518.   turtle.turnRight()
  519. end
  520. if direct == "right" then
  521.   turtle.turnLeft()
  522. end
  523.  
  524. turtle.dig()
  525. turtle.forward()
  526. turtle.digUp()
  527. turtle.dig()
  528. turtle.forward()
  529. turtle.digUp()
  530. turtle.dig()
  531. turtle.forward()
  532. turtle.digUp()
  533.  
  534. if direct == "left" then
  535.   turtle.turnRight()
  536. end
  537. if direct == "right" then
  538.   turtle.turnLeft()
  539. end
  540. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement