Advertisement
Sedrowow

ComputerCraft - Less Stupid: Planer improved

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