Chutny

ComputerCraft - Less Stupid: Lumberjack Turtle

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