Advertisement
Chutny

ComputerCraft - Less Stupid: Lumberjack Module builder Turtle

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