Chutny

ComputerCraft - Less Stupid: Mining Turtle

May 19th, 2021 (edited)
2,742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.98 KB | None | 0 0
  1. local times = ""
  2. local FuelNeeded   -- Minimum level of fuel allowed for turtle to start
  3. local Chest = 0    -- Declaring a chest value to determine if chest should be place in the end
  4.  
  5. local valArray = {}
  6. local aUpdate = {}
  7. mLog = {x = 100, y = 100, z = 100, d = 0}
  8. local quickStart = nil
  9.  
  10. local GlobalLog = {}
  11.  
  12. File = fs.open("Turtle_output.txt", "w")
  13. table.insert(GlobalLog, {x = 0, y = 0, z = 0, block = "minecraft:void"})
  14. table.insert(mLog, {x = 100, y = 100, z = 100, d = 0})
  15. table.insert(valArray, {x = 100, y = 100, z = 100})
  16.  
  17. -- Functions
  18.  
  19. local function tablelength(T)
  20.     local count = 0
  21.     for _ in pairs(T) do count = count + 1 end
  22.     return count
  23. end
  24.  
  25. local function valuable(success, data )
  26.    
  27.     if not success then
  28.         return false
  29.     end
  30.  
  31.     local ignore={
  32.     ["minecraft:diorite"] = true,
  33.     ["minecraft:andesite"] = true,
  34.     ["minecraft:glass"] = true,
  35.     ["minecraft:granite"] = true,
  36.     ["blockus:limestone"] = true,
  37.     ["blockus:bluestone"] = true,
  38.     ["minecraft:dirt"] = true,
  39.     ["minecraft:stone"] = true,
  40.     ["minecraft:cobblestone"] = true,
  41.     ["minecraft:gravel"] = true,
  42.     ["minecraft:void"] = true,
  43.     ["minecraft:sand"] = false,
  44.     ["minecraft:lava"] = true,
  45.     }
  46.    
  47.     if ignore[data.name] then --# quick access trick: we have the name of the block now, if it's in the list we will get true for the if
  48.         return false
  49.     else
  50.         return true
  51.     end
  52. end
  53.  
  54. function Scan()
  55.    
  56.     local success, data = turtle.inspect()
  57.    
  58.     if valuable(success, data) then
  59.         File.writeLine("scn: 0,1")
  60.         InsertFilter(GetPos(0,1))
  61.         File.writeLine("ScanFwd_Get: " .. GetPos(0,1).x .. ", " .. GetPos(0,1).y .. ", " .. GetPos(0,1).z)
  62.  
  63.        
  64.     end
  65.    
  66.     success, data = turtle.inspectUp()
  67.    
  68.     if valuable(success, data) then
  69.         File.writeLine("scn: 0,0,1")
  70.         InsertFilter(GetPos(0,0,1))
  71.         File.writeLine("ScanUp_Get: " .. GetPos(0,0,1).x .. ", " .. GetPos(0,0,1).y .. ", " .. GetPos(0,0,1).z)
  72.  
  73.        
  74.     end
  75.    
  76.     success, data = turtle.inspectDown()
  77.    
  78.     if valuable(success, data) then
  79.         File.writeLine("scn: 0,0,-1")
  80.         InsertFilter(GetPos(0,0,-1))
  81.         File.writeLine("ScanDown_Get: " .. GetPos(0,0,-1).x .. ", " .. GetPos(0,0,-1).y .. ", " .. GetPos(0,0,-1).z)
  82.  
  83.        
  84.     end
  85.  
  86.  
  87.     pos = GetPos()
  88.     --File.writeLine("Scan_Get: " .. pos.x .. ", " .. pos.y .. ", " .. pos.z)
  89.  
  90.     File.writeLine("Scan_Get: " .. pos.x .. ", " .. pos.y .. ", " .. pos.z.. ", " .. pos.d)
  91.    
  92. end
  93.  
  94. function Move(command)
  95.     File.writeLine("---------------------")
  96.  
  97.     function Fwd(shouldLog)
  98.         turtle.dig()
  99.         turtle.forward()
  100.  
  101.         if shouldLog == true then
  102.             table.insert(mLog, {x = 0, y = 1, z = 0})
  103.             File.writeLine("Move('fwd')")
  104.         else
  105.             print("Rtn_Fwd")
  106.         end
  107.         Scan()
  108.        
  109.     end
  110.     function Bwd(shouldLog)
  111.         turtle.back()
  112.  
  113.         if shouldLog == true then
  114.             table.insert(mLog, {x = 0, y = -1, z = 0})
  115.             File.writeLine("Move('bwd')")
  116.         else
  117.             print("Rtn_Bwd")
  118.         end
  119.        
  120.     end
  121.     local function Left(shouldLog)
  122.         turtle.turnLeft()
  123.  
  124.         if shouldLog == true then
  125.  
  126.             table.insert(mLog, {x = 0, y = 0, z = 0, d = -1})
  127.             File.writeLine("Move('Left')")
  128.         else
  129.             print("Rtn_Left")
  130.         end
  131.         Scan()
  132.     end
  133.     local function Right(shouldLog)
  134.         turtle.turnRight()
  135.  
  136.         if shouldLog == true then
  137.             table.insert(mLog, {x = 0, y = 0, z = 0, d = 1})
  138.             File.writeLine("Move('Right')")
  139.         else
  140.             print("Rtn_Right")
  141.         end
  142.         Scan()
  143.     end
  144.     local function Up(shouldLog)
  145.         turtle.digUp()
  146.         turtle.up()
  147.  
  148.         if shouldLog == true then
  149.             table.insert(mLog, {x = 0, y = 0, z = 1, d = 0})
  150.             File.writeLine("Move('Up')")
  151.         else
  152.             print("Rtn_Up")
  153.         end
  154.         Scan()
  155.  
  156.         --Move("tscan")
  157.  
  158.     end
  159.     local function Down(shouldLog)
  160.         turtle.digDown()
  161.         turtle.down()
  162.  
  163.         if shouldLog == true then
  164.             table.insert(mLog, {x = 0, y = 0, z = -1, d = 0})
  165.             File.writeLine("Move('Down')")
  166.         else
  167.             print("Rtn_Down")
  168.         end
  169.         Scan()
  170.  
  171.         --Move("tscan")
  172.     end
  173.  
  174.     function Tscan(shouldLog)
  175.        
  176.         print("TurtleScan")
  177.         File.writeLine("Move('Tscan')")
  178.         Move("right")
  179.         Move("right")
  180.         Move("right")
  181.         Move("right")      
  182.     end
  183.  
  184.     if command == "fwd" then Fwd(true)
  185.     elseif command == "bwd" then Bwd(true)
  186.     elseif command == "left" then Left(true)
  187.     elseif command == "right" then Right(true)
  188.     elseif command == "up" then Up(true)
  189.     elseif command == "down" then Down(true)
  190.     elseif command == "tscan" then Tscan(true)
  191.  
  192.     elseif command == "fwd_NoLog" then Fwd(false)
  193.     elseif command == "bwd_NoLog" then Bwd(false)
  194.     elseif command == "left_NoLog" then Left(false)
  195.     elseif command == "right_NoLog" then Right(false)
  196.     elseif command == "up_NoLog" then Up(false)
  197.     elseif command == "down_NoLog" then Down(false)
  198.     elseif command == "tscan_NoLog" then Tscan(false)
  199.  
  200.     end
  201.    
  202. end
  203.  
  204.  
  205. function InsertFilter(tab)
  206.     File.writeLine("---------------------")
  207.     File.writeLine("InsertFilter()")
  208.     if #valArray > 0 then
  209.  
  210.         counter = 0
  211.  
  212.         for k,v in ipairs(valArray) do
  213.  
  214.             tx = tab.x
  215.             ty = tab.y
  216.             tz = tab.z
  217.  
  218.             vx = v.x
  219.             vy = v.y
  220.             vz = v.z
  221.  
  222.             if tx == vx and (ty == vy and tz == vz) then
  223.                 counter = counter +1
  224.                 File.writeLine("Count: " .. counter)
  225.             end
  226.         end
  227.  
  228.         if counter < 1 then
  229.             table.insert(valArray, tab)
  230.             File.writeLine("ins_" .. " " .. tab.x .. " " .. tab.y .. " " .. tab.z)
  231.         end
  232.     else
  233.         table.insert(valArray, tab)
  234.         File.writeLine("ins_" .. " " .. tab.x .. " " .. tab.y .. " " .. tab.z)
  235.     end
  236. end
  237.  
  238. function GetDiffToCoord(ix,iy,iz)
  239.  
  240.     local cPos = GetPos()
  241.     File.writeLine("GetDiffToCoord_Get: " .. cPos.x .. ", " .. cPos.y .. ", " .. cPos.z .. ", " .. cPos.d)
  242.  
  243.     xDiff = ix - cPos.x
  244.     yDiff = iy - cPos.y
  245.     zDiff = iz - cPos.z
  246.  
  247.     File.writeLine("GetDiff_Get: " .. xDiff .. ", " .. yDiff .. ", " .. zDiff)
  248.  
  249.     return {x = xDiff, y = yDiff, z = zDiff}
  250. end
  251.  
  252. function GetMoveDir(iDiff)
  253.  
  254.     idir = 0
  255.     if iDiff < 0 then
  256.         idir = -1
  257.     elseif iDiff > 0 then
  258.         idir = 1
  259.     end
  260.  
  261.     return idir
  262. end
  263.  
  264. function Vec2Move(coord)
  265.  
  266.     idir = 0
  267.  
  268.     if coord < 0 then
  269.         idir = -1
  270.     elseif coord > 0 then
  271.         idir = 1
  272.     end
  273.  
  274.     return idir
  275. end
  276.  
  277. function ResetDir()
  278.     --while not (GetPos().d == 0 ) do
  279.     File.writeLine("---------------------")
  280.     File.writeLine("ResetDir()")
  281.        
  282.     local d = GetPos().d
  283.  
  284.     if d == 0 then
  285.         return true
  286.     elseif d < 2 then
  287.         Move("left")
  288.         return true
  289.     elseif d > 2 then
  290.         Move("right")
  291.         return true
  292.     else
  293.         Move("right")
  294.         Move("right")
  295.         return true
  296.     end
  297.  
  298. end
  299.  
  300. function ArrayRemove(t, fnKeep)
  301.     local j, n = 1, #t;
  302.  
  303.     for i=1,n do
  304.         if (fnKeep(t, i, j)) then
  305.             -- Move i's kept value to j's position, if it's not already there.
  306.             if (i ~= j) then
  307.                 t[j] = t[i];
  308.                 t[i] = nil;
  309.             end
  310.             j = j + 1; -- Increment position of where we'll place the next kept value.
  311.         else
  312.             t[i] = nil;
  313.         end
  314.     end
  315.  
  316.     return t;
  317. end
  318.  
  319.  
  320. function Dir2Vec3(y, d)
  321.  
  322.     --File.writeLine("---------------------")
  323.     --File.writeLine("Dir2Vec3(" .. y .. "," .. d .. ")")
  324.    
  325.     d = (d+4)%4
  326.  
  327.     mY = 0
  328.     mX = 0
  329.  
  330.     y = Vec2Move(y)
  331.  
  332.     if y == 1 and d == 0 then
  333.         mY = 1
  334.     elseif y == 1 and d == 1 then
  335.         mX = 1
  336.     elseif y == 1 and d == 2 then
  337.         mY = -1
  338.     elseif y == 1 and d == 3 then
  339.         mX = -1
  340.     end
  341.  
  342.     return {x = mX, y = mY}
  343.  
  344. end
  345.  
  346. function MoveTo(x,y,z)
  347.     File.writeLine("---------------------")
  348.     File.writeLine("MoveTo(" .. x .. "," .. y .. "," .. z .. ")")
  349.     --d = GetPos().d
  350.    
  351.     if z == 1 then
  352.         Move("up")
  353.     elseif z == -1 then
  354.         Move("down")
  355.     elseif y == 1 and x == 1 then
  356.         Move("fwd")
  357.         Move("right")
  358.         Move("fwd")
  359.     elseif y == -1 and x == 1 then
  360.         Move("right")
  361.         Move("fwd")
  362.         Move("right")
  363.         Move("fwd")
  364.     elseif y == 1 and x == -1 then
  365.         Move("fwd")
  366.         Move("left")
  367.         Move("fwd")
  368.     elseif y == -1 and x == -1 then
  369.         Move("left")
  370.         Move("fwd")
  371.         Move("left")
  372.         Move("fwd")
  373.     elseif y == 0 and x == -1 then
  374.         Move("left")
  375.         Move("fwd")
  376.     elseif y == 0 and x == 1 then
  377.         Move("right")
  378.         Move("fwd")
  379.     elseif y == 1 and x == 0 then
  380.         ResetDir()
  381.         Move("fwd")
  382.     elseif y == -1 and x == 0 then
  383.         File.writeLine("bwd ->")
  384.         Move("left")
  385.         Move("left")
  386.         Move("fwd")
  387.     else
  388.         File.writeLine("Reached: " .. x .. ", " .. y .. ", " .. z)
  389.         return true
  390.     end
  391.  
  392. end
  393.  
  394. function GetPos(iX,iY,iZ)
  395.  
  396.     local tPos = {}
  397.  
  398.     tX = mLog[1].x
  399.     tY = mLog[1].y
  400.     tZ = mLog[1].z
  401.     tD = mLog[1].d
  402.  
  403.     --vec = 0
  404.     lD = 0
  405.  
  406.     for k, v in ipairs(mLog) do
  407.  
  408.         --File.writeLine("Key: " .. k)
  409.         --File.flush()
  410.  
  411.         if k > 1 then
  412.  
  413.             d = v.d or nil
  414.  
  415.             if type(v.d) == "number" then
  416.  
  417.                 lD = lD + v.d
  418.                 d = (lD+4)%4
  419.                
  420.             end
  421.  
  422.             dCorr = Dir2Vec3(v.y, lD)
  423.  
  424.             tX = tX + dCorr.x
  425.             tY = tY + dCorr.y
  426.  
  427.             d = v.d or lD
  428.  
  429.             d = (d+4)%4
  430.  
  431.             tZ = tZ + v.z
  432.             tD = tD + d
  433.         end
  434.  
  435.     end
  436.    
  437.     iX = iX or 0
  438.     iY = iY or 0
  439.     iZ = iZ or 0
  440.  
  441.     oX = 0
  442.     oY = 0
  443.  
  444.     tD = (lD+4)%4
  445.    
  446.     dCorr = Dir2Vec3(iY, tD)
  447.  
  448.     tX = tX + dCorr.x
  449.     tY = tY + dCorr.y
  450.     tZ = tZ + iZ
  451.  
  452.     tPos = {x = tX, y = tY, z = tZ, d = tD}
  453.    
  454.     return tPos
  455. end
  456.  
  457. function GoTo(x,y,z)
  458.    
  459.     File.writeLine("GoTo(" .. x .. ", " .. y .. ", " .. z .. ")" )
  460.     isReached = false
  461.  
  462.     while isReached == false do
  463.  
  464.         local dif = GetDiffToCoord(x,y,z)
  465.  
  466.         local ix = GetMoveDir(dif.x)
  467.         local iy = GetMoveDir(dif.y)
  468.         local iz = GetMoveDir(dif.z)
  469.  
  470.         File.writeLine("Dir = " .. GetPos().d)
  471.    
  472.         ResetDir()
  473.  
  474.         File.writeLine("Dir = " .. GetPos().d)
  475.  
  476.         if MoveTo(ix,iy,iz) == true then
  477.             isReached = true
  478.             File.writeLine("REACHED")
  479.         end
  480.         File.flush()
  481.        
  482.     end
  483.  
  484.     File.writeLine("GoTo()_DONE")
  485.     return isReached
  486.  
  487. end
  488.  
  489. function RemoveNils(Objects)
  490.     local tbl = {}
  491.     for I = 1, #Objects do
  492.  
  493.         if(Objects[I] ~= nil) then
  494.             table.insert(tbl, Objects[I])
  495.         end
  496.     end
  497.     Objects = tbl
  498. end
  499.  
  500.  
  501. --sleep (2)
  502.  
  503. print ("------------")
  504. print ("Less Stupid: Mining Turtle")
  505. print ("------------")
  506. print ("Place: Chest behind Turtle")
  507. print (" ")
  508. print ("Tunnel length = ")
  509. print ("---------------------------")
  510.  
  511. quickStart = quickStart or read()
  512.  
  513. times = quickStart
  514.  
  515. FuelNeeded = times*7
  516. print ("Fuel needed to dig is "..FuelNeeded)
  517.  
  518. if turtle.getFuelLevel() < FuelNeeded then
  519.     turtle.select(1)
  520.     turtle.refuel()
  521.   print ("Fuel level is: "..turtle.getFuelLevel())
  522.   print ("Turtle is low on fuel. Do you wish to refuel? y/n")
  523.   local event, param1 = os.pullEvent("char")
  524.   if param1 == "y" then
  525.      turtle.select(1)
  526.      turtle.refuel()
  527.      
  528.   else
  529.      print ("!Too low fuel for program to initiate!")
  530.      print ("Turtle rebooting!")
  531.      sleep (2)
  532.      os.reboot()
  533.      
  534.   end
  535. end
  536.  
  537. for i = 1, times do
  538.  
  539.     ResetDir()
  540.     Move("fwd")
  541.     Move("tscan")
  542.     Move("down")
  543.     Move("tscan")
  544.     Move("up")
  545.  
  546.     originalPos = GetPos()
  547.     File.writeLine("originalPos_Get: " .. originalPos.x .. ", " .. originalPos.y .. ", " .. originalPos.z .. ", " .. originalPos.d)
  548.    
  549.     File.writeLine("OGpos: " .. originalPos.x .. "," .. originalPos.y .. "," .. originalPos.z)
  550.     --GoTo(originalPos.x+3, originalPos.y+3, originalPos.z+3)
  551.     File.writeLine("------------------------------------------")
  552.  
  553.     while #valArray > 1 do 
  554.  
  555.         GoTo(valArray[#valArray].x, valArray[#valArray].y, valArray[#valArray].z)
  556.  
  557.         valArray[#valArray] = nil
  558.  
  559.         RemoveNils(valArray)
  560.  
  561.         File.writeLine("Len: " .. #valArray)
  562.         File.flush()
  563.     end
  564.        
  565.     File.writeLine("---------------------------------------------------------------")
  566.  
  567.     GoTo(originalPos.x, originalPos.y, originalPos.z)
  568.  
  569.     times = times+1
  570. end
  571.  
  572. local isDone = false
  573.  
  574.  
  575. GoTo(100, 100, 100)
  576.  
  577. ResetDir()
  578.  
  579. turtle.turnLeft()
  580. turtle.turnLeft()
  581.  
  582. local success, data = turtle.inspect()
  583.  
  584. if data.name == "minecraft:chest" then
  585.  
  586.     print("Chest Found!")
  587.  
  588.     for i = 1, 16 do
  589.         turtle.select(i)
  590.         turtle.drop()
  591.     end
  592.  
  593.     isDone = true
  594. else
  595.    
  596.     print("No Chest found, Searching inventory...")
  597.     turtle.select(16)
  598.     local data = turtle.getItemDetail()
  599.  
  600.     if not(data == nil) and data.name == "minecraft:chest" then
  601.        
  602.         print("Chest Found!")
  603.         sleep (1)
  604.    
  605.         for i = 1, 16 do
  606.             turtle.select(i)
  607.             turtle.drop()  
  608.         end
  609.  
  610.         isDone = true
  611.     end
  612. end
  613.  
  614.  
  615. turtle.turnLeft()
  616. turtle.turnLeft()
  617.  
  618. for index, value in pairs(valArray) do
  619.     File.writeLine("Fin: " .. valArray[index].x .. ", " .. valArray[index].y .. ", " .. valArray[index].z)
  620. end
  621.  
  622. File.close()
Add Comment
Please, Sign In to add comment