le_Fish

MineShaft with anti crash and anti lava

Jul 24th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.88 KB | None | 0 0
  1. local Length
  2. local Width
  3. local Depth
  4.  
  5. local X
  6. local Y
  7. local Z
  8. local direction
  9. local goingBack
  10.  
  11. digheights = {}
  12. local dig_height
  13. local counter
  14.  
  15. function digQuery()
  16.     -- check if startposition or if digheight is greater than 3, 2 or 1
  17.     for i=counter,#digheights do
  18.         dig_height = tonumber(digheights[i])
  19.  
  20.         -- dig rows
  21.         digRow(dig_height)
  22.  
  23.         if(i ~= #digheights) then
  24.             -- reset robot to lowest point
  25.             if(dig_height >= 2) then
  26.                 digDown(2)
  27.             else
  28.                 digDown(1)
  29.             end
  30.            
  31.             -- check if done with one plot
  32.             if(Y == tonumber(Width)-1 and goingBack == "false" and Z ~= Depth) then
  33.                 turnLeft()
  34.                 turnLeft()
  35.                 goingBack = "true"
  36.                 local new_dig_height = digheights[i + 1]
  37.                 if(new_dig_height == 2) then
  38.                     digDown(1)
  39.                     digUp(1)
  40.                 elseif(new_dig_height == 3) then
  41.                     digDown(2)
  42.                     digUp(1)
  43.                 end
  44.             elseif(Y == 0 and goingBack == "true"  and Z ~= Depth) then
  45.                 turnRight()
  46.                 turnRight()
  47.                 goingBack = "false"
  48.                 local new_dig_height = digheights[i + 1]
  49.                 if(new_dig_height == 2) then
  50.                     digDown(1)
  51.                     digUp(1)
  52.                 elseif(new_dig_height == 3) then
  53.                     digDown(2)
  54.                     digUp(1)
  55.                 end
  56.             end
  57.         end
  58.         local h = fs.open("query/counter", "w")
  59.         h.writeLine(i)
  60.         h.close()
  61.     end
  62.     doneMining()
  63.     home()
  64. end
  65.  
  66. -- [TURTLE FUNCTIONS]
  67.  
  68. function turnLeft()
  69.     turtle.turnLeft()
  70.     if direction == "N" then
  71.         direction = "W"
  72.     elseif direction == "W" then
  73.         direction = "S"
  74.     elseif direction == "S" then
  75.         direction = "E"
  76.     elseif direction == "E" then
  77.         direction = "N"
  78.     end
  79.  
  80.     local h = fs.open("query/homePositions", "w")
  81.     h.writeLine(X)
  82.     h.writeLine(Y)
  83.     h.writeLine(Z)
  84.     h.writeLine(direction)
  85.     h.writeLine(goingBack)
  86.     h.close()
  87. end
  88.  
  89. function turnRight()
  90.     turtle.turnRight()
  91.     if direction == "N" then
  92.         direction = "E"
  93.     elseif direction == "E" then
  94.         direction = "S"
  95.     elseif direction == "S" then
  96.         direction = "W"
  97.     elseif direction == "W" then
  98.         direction = "N"
  99.     end
  100.  
  101.     local h = fs.open("query/homePositions", "w")
  102.     h.writeLine(X)
  103.     h.writeLine(Y)
  104.     h.writeLine(Z)
  105.     h.writeLine(direction)
  106.     h.writeLine(goingBack)
  107.     h.close()
  108. end
  109.  
  110. function digForward(height)
  111.     if(height == 1) then
  112.         while turtle.forward() == false do
  113.             turtle.dig()
  114.         end
  115.         savePos()
  116.     elseif(height == 2) then
  117.         while turtle.forward() == false do
  118.             turtle.dig()
  119.         end
  120.         savePos()
  121.         turtle.digDown()
  122.     elseif(height == 3) then
  123.         while turtle.forward() == false do
  124.             turtle.dig()
  125.         end
  126.         savePos()
  127.         turtle.digUp()
  128.         turtle.digDown()
  129.     else
  130.         print("Error: height given for function [digForward] is not 1, 2 or 3")
  131.     end
  132.  
  133.     if(height == 3 or height == 2 or height == 1) then
  134.         emptyInv()
  135.     end
  136. end
  137.  
  138. function digDown(amount)
  139.     local _down = 0
  140.     repeat
  141.         while turtle.down() == false do
  142.             turtle.digDown()
  143.         end
  144.  
  145.         Z = Z + 1
  146.  
  147.         local h = fs.open("query/homePositions", "w")
  148.         h.writeLine(X)
  149.         h.writeLine(Y)
  150.         h.writeLine(Z)
  151.         h.writeLine(direction)
  152.         h.writeLine(goingBack)
  153.         h.close()
  154.         _down = _down + 1
  155.     until _down == amount
  156. end
  157.  
  158. function digUp(amount)
  159.     local _up = 0
  160.     repeat
  161.         while turtle.up() == false do
  162.             turtle.digUp()
  163.         end
  164.  
  165.         Z = Z - 1
  166.  
  167.         local h = fs.open("query/homePositions", "w")
  168.         h.writeLine(X)
  169.         h.writeLine(Y)
  170.         h.writeLine(Z)
  171.         h.writeLine(direction)
  172.         h.writeLine(goingBack)
  173.         h.close()
  174.         _up = _up + 1
  175.     until _up == amount
  176. end
  177.  
  178. function goForward(amount)
  179.     local _forward = 0
  180.     repeat
  181.         while turtle.forward() == false do
  182.             turtle.dig()
  183.         end
  184.         _forward = _forward + 1
  185.     until _forward == amount
  186. end
  187. -- [OTHER FUNCTIONS]
  188.  
  189. function savePos()
  190.     if direction == "N" then
  191.         X = X + 1
  192.     elseif direction == "S" then
  193.         X = X - 1
  194.     elseif direction == "E" then
  195.         Y = Y - 1
  196.     elseif direction == "W" then
  197.         Y = Y + 1
  198.     end
  199.  
  200.     local h = fs.open("query/homePositions", "w")
  201.     h.writeLine(X)
  202.     h.writeLine(Y)
  203.     h.writeLine(Z)
  204.     h.writeLine(direction)
  205.     h.writeLine(goingBack)
  206.     h.close()
  207.     printInfo()
  208. end
  209.  
  210. function isStartPosition()
  211.     if(X == 0.0 and Y == 0.0 and Z == 0.0 and direction == "N") then
  212.         print("start position = true, meaning we are at the begin of the query")
  213.         return true
  214.     else
  215.         print("start position = false, meaning we are in the middle of the query")
  216.         return false
  217.     end
  218. end
  219.  
  220. function digRow(my_dig_height)
  221.     my_dig_height = tonumber(my_dig_height)
  222.     if(goingBack == "false") then
  223.         while(tonumber(Y) ~= (tonumber(Width) - 1)) do
  224.             if(direction == "N") then
  225.                 repeat
  226.                     digForward(my_dig_height)
  227.                 until tonumber(X) >= (tonumber(Length) - 1)
  228.                 turnLeft()
  229.                 digForward(my_dig_height)
  230.                 turnLeft()
  231.             elseif(direction == "S") then
  232.                 repeat
  233.                     digForward(my_dig_height)
  234.                 until tonumber(X) <= 0
  235.                 turnRight()
  236.                 digForward(my_dig_height)
  237.                 turnRight()
  238.             elseif(tonumber(X) >= (tonumber(Length) - 1) and direction == "W") then
  239.                 turnLeft()
  240.             elseif(tonumber(X) <= 0 and direction == "E") then
  241.                 turnRight()
  242.             end
  243.         end
  244.         -- once more
  245.         if(direction == "N") then
  246.             repeat
  247.                 digForward(my_dig_height)
  248.             until tonumber(X) >= (tonumber(Length) - 1)
  249.         elseif(direction == "S") then
  250.             repeat
  251.                 digForward(my_dig_height)
  252.             until tonumber(X) <= 0
  253.         end
  254.     elseif(goingBack == "true") then
  255.         while(tonumber(Y) ~= 0) do
  256.             if(direction == "N") then
  257.                 repeat
  258.                     digForward(my_dig_height)
  259.                 until tonumber(X) == (tonumber(Length) - 1)
  260.                 turnRight()
  261.                 digForward(my_dig_height)
  262.                 turnRight()
  263.             elseif(direction == "S") then
  264.                 repeat
  265.                     digForward(my_dig_height)
  266.                 until tonumber(X) == 0
  267.                 turnLeft()
  268.                 digForward(my_dig_height)
  269.                 turnLeft()
  270.             elseif(tonumber(X) >= (tonumber(Length) - 1) and direction == "W") then
  271.                 turnRight()
  272.             elseif(tonumber(X) <= 0 and direction == "E") then
  273.                 turnLeft()
  274.             end
  275.         end
  276.         -- once more
  277.         if(direction == "N") then
  278.             repeat
  279.                 digForward(my_dig_height)
  280.             until tonumber(X) == (tonumber(Length) - 1)
  281.         elseif(direction == "S") then
  282.             repeat
  283.                 digForward(my_dig_height)
  284.             until tonumber(X) == 0
  285.         end
  286.     end
  287. end
  288.  
  289. function emptyInv()
  290.     if turtle.getItemCount(16) > 0 then
  291.         turnLeft()
  292.         turnLeft()
  293.         turtle.dig()
  294.         turtle.select(1)
  295.         turtle.place()
  296.        
  297.         local success, t = turtle.inspect()
  298.         if success then
  299.             print("found something")
  300.             if string.find(t.name, "EnderStorage") == nil then
  301.                 print("error!")
  302.                 while true do
  303.                     sleep(5)
  304.                 end
  305.             else
  306.                 for q=2,16 do
  307.                     turtle.select(q)
  308.                     turtle.drop()
  309.                 end
  310.                 turtle.select(1)
  311.                 turtle.dig()
  312.                 turnRight()
  313.                 turnRight()
  314.             end
  315.         else
  316.             print("error!")
  317.             while true do
  318.                 sleep(5)
  319.             end
  320.         end
  321.     end
  322. end
  323.  
  324. function home()
  325.     if(Z > 0) then
  326.         digUp(Z)
  327.     end
  328.     if(X > 0) then
  329.         while(direction ~= "S") do
  330.                 turnRight()
  331.         end
  332.         goForward(X)
  333.     end
  334.     if(Y > 0) then
  335.         while(direction ~= "E") do
  336.                 turnLeft()
  337.         end
  338.         goForward(Y)
  339.     end
  340.     if(X == 0 and Y == 0 and Z == 0) then
  341.         while(direction ~= "N") do
  342.             turnLeft()
  343.         end
  344.         turtle.select(1)
  345.         turtle.place()
  346.        
  347.         local success, t = turtle.inspect()
  348.         if success then
  349.             print("found something")
  350.             if string.find(t.name, "EnderStorage") == nil then
  351.                 print("error!")
  352.                 while true do
  353.                     sleep(5)
  354.                 end
  355.             else
  356.                 for q=2,16 do
  357.                     turtle.select(q)
  358.                     turtle.drop()
  359.                 end
  360.                 turtle.select(1)
  361.                 turtle.dig()
  362.                 turnRight()
  363.                 turnRight()
  364.             end
  365.         else
  366.             print("error!")
  367.             while true do
  368.                 sleep(5)
  369.             end
  370.         end
  371.         fs.delete("query")
  372.         fs.delete("startup")
  373.         clearScreen()
  374.         print("Done and deleted /query & startup files")  
  375.     end
  376.  
  377. end
  378.  
  379. function doneMining()
  380.     local h = fs.open("query/done", "w")
  381.     h.writeLine(true)
  382.     h.close()
  383. end
  384.  
  385. function printInfo()
  386.     clearScreen()
  387.     print("--------------------------------")
  388.     print("X: " .. X)
  389.     print("Y: " .. Y)
  390.     print("Z: " .. Z)
  391.     print("--------------------------------")
  392.     print("Direction: " .. direction)
  393.     print("Current digheight: " .. dig_height)
  394.     print("--------------------------------")
  395.     if goingBack == true then
  396.         print("Going back to home chest")
  397.     end
  398. end
  399. function clearScreen()
  400.     term.clear()
  401.     term.setCursorPos(1,1)
  402. end
  403.  
  404. -- [STARTUP AND INIT]
  405.  
  406. function init_function()
  407.     if fs.exists("query/done") then
  408.         home()
  409.     elseif fs.exists("query/firstInput") then
  410.         --crashed
  411.         print("reading first input, probably a crash occured")
  412.         -- getting users input
  413.         local h = fs.open("query/firstInput", "r")
  414.         Length = tonumber(h.readLine())
  415.         Width = tonumber(h.readLine())
  416.         Depth = tonumber(h.readLine())
  417.         h.close()
  418.  
  419.         -- getting his positions
  420.         print("reading my positions and direction")
  421.         local h = fs.open("query/homePositions", "r")
  422.         X = tonumber(h.readLine())
  423.         Y = tonumber(h.readLine())
  424.         Z = tonumber(h.readLine())
  425.         direction = h.readLine()
  426.         goingBack = h.readLine()
  427.         h.close()
  428.  
  429.         -- getting depth array
  430.         local h = fs.open("query/depths", "r")
  431.         line = h.readLine()
  432.         i = 1
  433.         while line do
  434.             digheights[i] = tonumber(line) -- will make it true if the read line is true, else false.
  435.             i = i + 1
  436.             line = h.readLine()
  437.         end
  438.         h.close()
  439.        
  440.         -- getting current counter status (height)
  441.         local h = fs.open("query/counter", "r")
  442.         counter = tonumber(h.readLine())
  443.         h.close()
  444.  
  445.         -- Check if crashed while empty inv.
  446.         local data = turtle.getItemDetail(1)
  447.         if(data == nil) then
  448.             local second_data = turtle.inspect()
  449.             if(second_data == nil) then
  450.                 repeat
  451.                     local data = turtle.getItemDetail(1)
  452.                     sleep(3)
  453.                 until data ~= nil and string.find(data.name, "EnderStorage")
  454.             elseif(string.find(second_data.name, "EnderStorage")) then
  455.                 for q=2,16 do
  456.                     turtle.select(q)
  457.                     turtle.drop()
  458.                 end
  459.                 turtle.select(1)
  460.                 turtle.dig()
  461.                 turnRight()
  462.                 turnRight()
  463.             end
  464.         end
  465.  
  466.         -- Display given numers
  467.         print(" " )
  468.         print("Given numbers")
  469.         print("Length: " .. Length .. " , Width: " .. Width .. " , Depth: " .. Depth)
  470.         print("My numbers")
  471.         print("X: " .. X .. " , Y: " .. Y .. " , Z: " .. Z .. " , direction: " .. direction)
  472.         print(" " )
  473.         print("Counter: " .. counter .. " / Times down: " .. #digheights)
  474.         print("Starting..")
  475.  
  476.         if(digheights[counter] > 1) then
  477.             turtle.digUp()
  478.             turtle.digDown()
  479.         end
  480.         digQuery()
  481.     else
  482.         startup()
  483.     end
  484. end
  485.  
  486. function startup()
  487.     print("V1 of the query bot with ender chest. Remember to put an ender chest in slot 1")
  488.     term.write("Insert Length: ")
  489.     Length = tonumber(read())
  490.     print(" ")
  491.    
  492.     term.write("Insert Width: ")
  493.     Width = tonumber(read())
  494.     print(" ")
  495.  
  496.     term.write("Insert Depth: ")
  497.     Depth = tonumber(read())
  498.     print(" ")
  499.  
  500.     term.clear()
  501.     term.setCursorPos(1,1)
  502.  
  503.     print("Is this the right input: ")
  504.     print("Length: " .. Length)
  505.     print("Width: " .. Width)
  506.     print("Depth: " .. Depth)
  507.     print("Press enter if it's correct, otherwhise: Ctrl+R for reboot")
  508.  
  509.     local rightInput = false
  510.     while (rightInput == false) do
  511.         local event, param = os.pullEvent()
  512.         if event == "key" and param == 28 then -- key 28 is the 'enter' key
  513.             rightInput = true
  514.         end
  515.         sleep(0.2)
  516.     end
  517.  
  518.     local data = turtle.getItemDetail(1)
  519.     if(data == nil) then
  520.         print("Put an enderchest in slot 1")
  521.         repeat
  522.             local data = turtle.getItemDetail(1)
  523.             sleep(0.2)
  524.         until data ~= nil and string.find(data.name, "EnderStorage")
  525.     end
  526.  
  527.     term.clear()
  528.     term.setCursorPos(1,1)
  529.  
  530.     --Query folder, easy to delete
  531.     print("making folder")
  532.     fs.makeDir("query")
  533.  
  534.     print("saving first input")
  535.     local h = fs.open("query/firstInput", "w")
  536.     h.writeLine(Length)
  537.     h.writeLine(Width)
  538.     h.writeLine(Depth)
  539.     h.close()
  540.  
  541.     print("making home positions")
  542.     local h = fs.open("query/homePositions", "w")
  543.     h.writeLine(0)
  544.     h.writeLine(0)
  545.     h.writeLine(0)
  546.     h.writeLine("N")
  547.     h.writeLine(false)
  548.     h.close()
  549.     X = 0
  550.     Y = 0
  551.     Z = 0
  552.     direction = "N"
  553.     goingBack = "false"
  554.  
  555.     print("Making startup file")
  556.     if fs.exists("startup") then
  557.         fs.delete("startup")
  558.         local h = fs.open("startup", "w")
  559.         h.writeLine("sleep(5)")
  560.         h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  561.         h.close()
  562.     else
  563.         h.writeLine("sleep(5)")
  564.         local h = fs.open("startup", "w")
  565.         h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  566.         h.close()
  567.     end
  568.  
  569.     -- calculate depths
  570.     local depth_counter = 0
  571.    
  572.     while(depth_counter < Depth) do
  573.         depth_counter = depth_counter + 3
  574.         digheights[#digheights+1]=3
  575.     end
  576.  
  577.     if(depth_counter > Depth) then
  578.         digheights[#digheights]=3-(depth_counter-Depth)
  579.     end
  580.  
  581.     local h = fs.open("query/depths", "w")
  582.     for i = 1, #digheights do
  583.         h.writeLine(digheights[i]) -- write to file
  584.     end
  585.     h.close()
  586.  
  587.     local h = fs.open("query/counter", "w")
  588.     h.writeLine(1)
  589.     h.close()
  590.  
  591.     counter = 1
  592.  
  593.     if(digheights[1] == 2) then
  594.         digDown(1)
  595.         digUp(1)
  596.     elseif(digheights[1] == 3) then
  597.         digDown(2)
  598.         digUp(1)
  599.     end
  600.  
  601.     digQuery()
  602. end
  603.  
  604. -- start script
  605. init_function()
Advertisement
Add Comment
Please, Sign In to add comment