Advertisement
M0n5t3r

ender_query

Mar 29th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.11 KB | None | 0 0
  1. local Length
  2. local Width
  3. local Depth
  4.  
  5. local home_x
  6. local home_y
  7. local home_z
  8.  
  9. local start_direction
  10.  
  11. local home_direction_y
  12. local home_direction_x
  13. local dig_direction
  14. local total_forward
  15.  
  16. -- startup
  17. local x, y, z = gps.locate(5)
  18. while not x do --If gps.locate didn't work, it won't return anything. So check to see if it did.
  19.     error('No GPS!')
  20.     sleep(5)
  21. end
  22. if x then
  23.     x = math.floor(x)
  24.     y = math.floor(y)
  25.     z = math.floor(z)
  26. end
  27.  
  28. -- empty inventory in ender chest
  29. function emptyInv()
  30.     turtle.turnLeft()
  31.     turtle.turnLeft()
  32.     turtle.dig()
  33.     turtle.select(1)
  34.     turtle.place()
  35.    
  36.     local success, t = turtle.inspect()
  37.     if success then
  38.         print("found something")
  39.         if string.find(t.name, "EnderStorage") == nil then
  40.             print("error!")
  41.             while true do
  42.                 sleep(5)
  43.             end
  44.         else
  45.             for q=2,16 do
  46.                 turtle.select(q)
  47.                 turtle.drop()
  48.             end
  49.             turtle.select(1)
  50.             turtle.dig()
  51.             turtle.turnRight()
  52.             turtle.turnRight()
  53.             checkCompas()
  54.         end
  55.     else
  56.         print("error!")
  57.         while true do
  58.             sleep(5)
  59.         end
  60.     end
  61.  
  62. end
  63.  
  64. -- check compas function (where am i?)
  65. function checkCompas()
  66.     print("function: checkCompas")
  67.     local x, y, z = gps.locate(5)
  68.     if turtle.back() then
  69.         local new_x, new_y, new_z = gps.locate(5)
  70.         new_x = math.floor(new_x)
  71.         new_y = math.floor(new_y)
  72.  
  73.    
  74.         if math.floor(new_y) ~= math.floor(y) then
  75.             print("Y| " .. new_y .. " - " .. y)
  76.             if math.floor(y) > math.floor(new_y) then
  77.                 dig_direction = "S"
  78.             else
  79.                 dig_direction = "N"
  80.             end
  81.         elseif math.floor(new_x) ~= math.floor(x) then
  82.             print("X| " .. math.floor(new_x) .. " - " .. math.floor(x))
  83.             if math.floor(x) < math.floor(new_x) then
  84.                 dig_direction = "W"
  85.             else
  86.                 dig_direction = "E"
  87.             end
  88.         end
  89.         turtle.forward()
  90.     else
  91.         turtle.dig()
  92.         turtle.dig()
  93.         turtle.forward()
  94.        
  95.         local new_x, new_y, new_z = gps.locate(5)
  96.         new_x = math.floor(new_x)
  97.         new_y = math.floor(new_y)
  98.  
  99.        
  100.         if math.floor(new_y) ~= math.floor(y) then
  101.             print("Y| " .. new_y .. " - " .. y)
  102.             if math.floor(y) < math.floor(new_y) then
  103.                 dig_direction = "S"
  104.             else
  105.                 dig_direction = "N"
  106.             end
  107.         elseif math.floor(new_x) ~= math.floor(x) then
  108.             print("X| " .. new_x .. " - " .. x)
  109.             if math.floor(x) > math.floor(new_x) then
  110.                 dig_direction = "W"
  111.             else
  112.                 dig_direction = "E"
  113.             end
  114.         end
  115.         turtle.back()
  116.     end
  117.     print("direction = ")
  118.     print(dig_direction)
  119. end
  120.  
  121. -- change dir to left & update compass
  122. function changeDir()
  123.     -- print("Function: ChangeDir")
  124.     if dig_direction == nil then
  125.         checkCompas()
  126.     end
  127.     turtle.turnLeft()
  128.     if dig_direction == "S" then
  129.         dig_direction = "E"
  130.         -- print("Facing east")
  131.     elseif dig_direction == "E" then
  132.         dig_direction = "N"
  133.         -- print("Facing north")
  134.     elseif dig_direction == "N" then
  135.         dig_direction = "W"
  136.         -- print("Facing west")
  137.     elseif dig_direction == "W" then
  138.         dig_direction = "S"
  139.         -- print("Facing south")
  140.     end
  141. end
  142.  
  143. -- change dir to right & update compass
  144. function changeDirRight()
  145.     turtle.turnRight()
  146.     if dig_direction == "S" then
  147.         dig_direction = "W"
  148.         -- print("Facing west")
  149.     elseif dig_direction == "W" then
  150.         dig_direction = "N"
  151.         -- print("Facing north")
  152.     elseif dig_direction == "N" then
  153.         dig_direction = "E"
  154.         -- print("Facing east")
  155.     elseif dig_direction == "E" then
  156.         dig_direction = "S"
  157.         -- print("Facing south")
  158.     end
  159. end
  160.  
  161. -- go up by an ammount (for returning)
  162. function goUp(amount)
  163.     print("Function: goUp")
  164.     if amount == 0 then
  165.         print("nothing to go up")
  166.     else
  167.         if amount > 0 then
  168.             local _up = 0
  169.             repeat
  170.                 if turtle.up() then
  171.                     _up = _up + 1
  172.                 else
  173.                     turtle.digUp()
  174.                 end
  175.             until _up == amount
  176.         elseif amount < 0 then
  177.             local _up = 0
  178.             repeat
  179.                 if turtle.down() then
  180.                     _up = _up - 1
  181.                 else
  182.                     turtle.digDown()
  183.                 end
  184.             until _up == amount
  185.         end
  186.     end
  187. end
  188.  
  189. -- delete all files not needed anymore
  190. function deleteOrNot()
  191.     term.write("Delete all? y/n  ")
  192.     Delete = read()
  193.     if Delete == "y" then
  194.         print("Deleting query folder and startup file")
  195.         fs.delete("startup")
  196.         if fs.exists("startup") then
  197.             print("startup was not deleted")
  198.         end
  199.         fs.delete("query")
  200.         if fs.exists("query") then
  201.             print("query folder not deleted")
  202.         end
  203.     elseif Delete == "n" then
  204.         print("Ok.")
  205.     else
  206.         print("not a valid input")
  207.         deleteOrNot()
  208.     end
  209. end
  210.  
  211. -- if a crash occuerd, make a crash report & try to query again
  212. function crashOccured()
  213.     print("Function: crashOccured")
  214.     if fs.exists("query/crash_report") then
  215.         print("deleting old crash report")
  216.         fs.delete("query/crash_report")
  217.         if fs.exists("query/crash_report") then
  218.             print("crash_report not deleted")
  219.         end
  220.     end
  221.    
  222.     checkCompas()
  223.     print(start_direction)
  224.     if start_direction == "E" or start_direction == "W" then
  225.         if dig_direction == "N" or dig_direction == "S" then
  226.             print("Wrong direction")
  227.             print(start_direction .. " - " .. dig_direction)
  228.             changeDir()
  229.             if turtle.detect() == false then
  230.                 changeDirRight()
  231.                 changeDirRight()
  232.             end
  233.         end
  234.     elseif start_direction == "N" or start_direction == "S" then
  235.         if dig_direction == "W" or dig_direction == "E" then
  236.             print("Wrong direction")
  237.             print(start_direction .. " - " .. dig_direction)
  238.             changeDir()
  239.             if turtle.detect() == false then
  240.                 changeDirRight()
  241.                 changeDirRight()
  242.             end
  243.         end
  244.     end
  245.  
  246.     print("making a crash report file")
  247.     local h = fs.open("query/crash_report", "w")
  248.     h.writeLine(x)
  249.     h.writeLine(y)
  250.     h.writeLine(z)
  251.     h.writeLine(dig_direction)
  252.     h.close()
  253.     sleep(1)
  254.    
  255.     if turtle.getItemCount(16) > 0 then
  256.         emptyInv()
  257.     end
  258.     --mine further
  259.     digQuery()
  260. end
  261.  
  262. -- go home function
  263. function goHome()
  264.    
  265.     print("function: goHome")
  266.     local x, y, z = gps.locate(5)
  267.     -- print(math.floor(x))
  268.     if math.floor(home_x) == math.floor(x) and math.floor(home_y) == math.floor(y) and math.floor(home_z) == math.floor(z) then
  269.         print("we already home")
  270.     else
  271.         checkCompas()
  272.         local x_amount = math.floor(home_x) - math.floor(x)
  273.         local y_amount = math.floor(home_y) - math.floor(y)
  274.         local z_amount = math.floor(home_z) - math.floor(z)
  275.        
  276.         if y_amount < 0 then
  277.             home_direction_y = "N"
  278.         else
  279.             home_direction_y = "S"
  280.         end
  281.  
  282.         if x_amount < 0 then
  283.             home_direction_x = "W"
  284.         else
  285.             home_direction_x = "E"
  286.         end
  287.        
  288.  
  289.        
  290.         if z_amount ~= 0 then
  291.             goUp(z_amount)
  292.         end
  293.        
  294.         if dig_direction ~= home_direction_x then
  295.             repeat
  296.                 changeDir()
  297.             until dig_direction == home_direction_x
  298.         end
  299.         if x_amount ~= 0 then
  300.             x_amount = math.floor(x_amount)
  301.             forward(x_amount)
  302.         end
  303.  
  304.         if dig_direction ~= home_direction_y then
  305.             repeat
  306.                 changeDir()
  307.             until dig_direction == home_direction_y
  308.         end
  309.         if y_amount ~= 0 then
  310.             y_amount = math.floor(y_amount)
  311.             forward(y_amount)
  312.         end
  313.         sleep(1)
  314.        
  315.     end
  316. end
  317.  
  318. -- forward movement
  319. function forward(amount)
  320.     local _forward = 0
  321.     local counter = 0
  322.     if amount > 0 then
  323.         repeat
  324.             if turtle.forward() then
  325.                 _forward = _forward + 1
  326.             else
  327.                 turtle.dig()
  328.             end
  329.         until _forward >= amount
  330.     else
  331.         repeat
  332.             if turtle.forward() then
  333.                 _forward = _forward - 1
  334.             else
  335.                 turtle.dig()
  336.             end
  337.         until _forward <= amount
  338.     end
  339. end
  340.  
  341. -- dig function
  342. function dig(amount)
  343.     local _dig_forward = 0
  344.     if amount ~= 0 then
  345.         if amount > 0 then
  346.         repeat
  347.             turtle.digDown()
  348.             while turtle.forward() == false do
  349.                 turtle.dig()
  350.             end
  351.             turtle.digDown()
  352.             _dig_forward = _dig_forward + 1
  353.             checkEmpty()
  354.         until _dig_forward == amount
  355.         elseif amount < 0 then
  356.             repeat
  357.                 turtle.digDown()
  358.                 while turtle.forward() == false do
  359.                     turtle.dig()
  360.                 end
  361.                 turtle.digDown()
  362.                 _dig_forward = _dig_forward - 1
  363.                 checkEmpty()
  364.             until _dig_forward == amount
  365.         end
  366.     end
  367. end
  368.  
  369.  
  370. function checkEmpty()
  371.     if turtle.getItemCount(16) > 0 then
  372.         emptyInv()
  373.     end
  374. end
  375.  
  376. function digQuery()
  377.     turtle.select(1)
  378.     print("Function: digQuery")
  379.     local rows
  380.     if math.floor(Depth) == 0 then
  381.         print("Going to bedrock")
  382.         repeat
  383.             local x, y, z = gps.locate(5)
  384.                 repeat
  385.                     local x, y, z = gps.locate(5)
  386.                     local height_row = math.floor(home_z) - math.floor(z)
  387.                     -- print("height_row = " .. height_row)
  388.                     if start_direction == "W" or start_direction == "E" then
  389.                         if start_direction == "W" then
  390.                             if height_row % 4 == 2 then
  391.                                 rows = (math.floor(y) - math.floor(home_y))
  392.                             else
  393.                                 rows = (math.floor(home_y) - (math.floor(y - Width))-1)
  394.                             end
  395.                         else
  396.                             if height_row % 4 == 2 then
  397.                                 rows = (math.floor(home_y) - math.floor(y))
  398.                             else
  399.                                 rows = (math.floor(y) - (math.floor(home_y - Width))-1)
  400.                             end
  401.                         end
  402.  
  403.                         print("rows: " .. rows)
  404.  
  405.                         if dig_direction == start_direction then
  406.                             local x_amount = math.floor(x) - (math.floor(home_x - Length )+1)
  407.                             print(x_amount)
  408.                             dig(x_amount)
  409.                             if rows > 0 then
  410.                                 if height_row % 4 == 2 then
  411.                                     changeDirRight()
  412.                                     dig(1)
  413.                                     changeDirRight()
  414.                                 else
  415.                                     changeDir()
  416.                                     dig(1)
  417.                                     changeDir()
  418.                                 end
  419.                             end
  420.                         else
  421.                             local x_amount = math.floor(home_x) - math.floor(x)
  422.                             print(x_amount)
  423.                             dig(x_amount)
  424.                             if rows > 0 then
  425.                                 if height_row % 4 == 2 then
  426.                                     changeDir()
  427.                                     dig(1)
  428.                                     changeDir()
  429.                                 else
  430.                                     changeDirRight()
  431.                                     dig(1)
  432.                                     changeDirRight()
  433.                                 end
  434.                             end
  435.                         end
  436.                     elseif start_direction == "N" or start_direction == "S" then
  437.                         if start_direction == "N" then
  438.                             if height_row % 4 == 2 then
  439.                                 rows = (math.floor(home_x) - math.floor(x))
  440.                             else
  441.                                 rows = (math.floor(x) - (math.floor(home_x - Width))-1)
  442.                             end
  443.                         else
  444.                             if height_row % 4 == 2 then
  445.                                 rows = (math.floor(x) - math.floor(home_x))
  446.                             else
  447.                                 rows = (math.floor(home_x) - (math.floor(x - Width))-1)
  448.                             end
  449.                         end
  450.                        
  451.                         print("rows: " .. rows)
  452.  
  453.                         if dig_direction == start_direction then
  454.                             local y_amount = math.floor(y) - (math.floor(home_y - Length)+1)
  455.                             print(y_amount)
  456.                             dig(y_amount)
  457.                            
  458.                             if rows > 0 then
  459.                                 if height_row % 4 == 2 then
  460.                                     changeDirRight()
  461.                                     dig(1)
  462.                                     changeDirRight()
  463.                                 else
  464.                                     changeDir()
  465.                                     dig(1)
  466.                                     changeDir()
  467.                                 end
  468.                             end
  469.                         else
  470.                             local y_amount = math.floor(home_y) - math.floor(y)
  471.                             print(y_amount)
  472.                             dig(y_amount)
  473.                             if rows > 0 then
  474.                                 if height_row % 4 == 2 then
  475.                                     changeDir()
  476.                                     dig(1)
  477.                                     changeDir()
  478.                                 else
  479.                                     changeDirRight()
  480.                                     dig(1)
  481.                                     changeDirRight()
  482.                                 end
  483.                             end
  484.                         end
  485.                     end
  486.                 until rows == 0
  487.                 local x, y, z = gps.locate(5)
  488.             if (math.floor(z) - 2) > 5 then
  489.                 print(math.floor(z) + 2)
  490.                 turtle.digDown()
  491.                 turtle.down()
  492.                 turtle.digDown()
  493.                 turtle.down()
  494.                 changeDir()
  495.                 changeDir()
  496.             end
  497.         until (math.floor(z) - 2) <= 5
  498.     else
  499.         if math.floor(z) > (math.floor(home_z) - math.floor(Depth)) then
  500.             repeat
  501.                 local x, y, z = gps.locate(5)
  502.                 repeat
  503.                     local x, y, z = gps.locate(5)
  504.                     local height_row = math.floor(home_z) - math.floor(z)
  505.                     -- print("height_row = " .. height_row)
  506.                     if start_direction == "W" or start_direction == "E" then
  507.                         if start_direction == "W" then
  508.                             if height_row % 4 == 2 then
  509.                                 rows = (math.floor(y) - math.floor(home_y))
  510.                             else
  511.                                 rows = (math.floor(home_y) - (math.floor(y - Width))-1)
  512.                             end
  513.                         else
  514.                             if height_row % 4 == 2 then
  515.                                 rows = (math.floor(home_y) - math.floor(y))
  516.                             else
  517.                                 rows = (math.floor(y) - (math.floor(home_y - Width))-1)
  518.                             end
  519.                         end
  520.  
  521.                         print("rows: " .. rows)
  522.  
  523.                         if dig_direction == start_direction then
  524.                             local x_amount = math.floor(x) - (math.floor(home_x - Length )+1)
  525.                             print(x_amount)
  526.                             dig(x_amount)
  527.                             if rows > 0 then
  528.                                 if height_row % 4 == 2 then
  529.                                     changeDirRight()
  530.                                     dig(1)
  531.                                     changeDirRight()
  532.                                 else
  533.                                     changeDir()
  534.                                     dig(1)
  535.                                     changeDir()
  536.                                 end
  537.                             end
  538.                         else
  539.                             local x_amount = math.floor(home_x) - math.floor(x)
  540.                             print(x_amount)
  541.                             dig(x_amount)
  542.                             if rows > 0 then
  543.                                 if height_row % 4 == 2 then
  544.                                     changeDir()
  545.                                     dig(1)
  546.                                     changeDir()
  547.                                 else
  548.                                     changeDirRight()
  549.                                     dig(1)
  550.                                     changeDirRight()
  551.                                 end
  552.                             end
  553.                         end
  554.                     elseif start_direction == "N" or start_direction == "S" then
  555.                         if start_direction == "N" then
  556.                             if height_row % 4 == 2 then
  557.                                 rows = (math.floor(home_x) - math.floor(x))
  558.                             else
  559.                                 rows = (math.floor(x) - (math.floor(home_x - Width))-1)
  560.                             end
  561.                         else
  562.                             if height_row % 4 == 2 then
  563.                                 rows = (math.floor(x) - math.floor(home_x))
  564.                             else
  565.                                 rows = (math.floor(home_x) - (math.floor(x - Width))-1)
  566.                             end
  567.                         end
  568.                        
  569.                         print("rows: " .. rows)
  570.  
  571.                         if dig_direction == start_direction then
  572.                             local y_amount = math.floor(y) - (math.floor(home_y - Length)+1)
  573.                             print(y_amount)
  574.                             dig(y_amount)
  575.                            
  576.                             if rows > 0 then
  577.                                 if height_row % 4 == 2 then
  578.                                     changeDirRight()
  579.                                     dig(1)
  580.                                     changeDirRight()
  581.                                 else
  582.                                     changeDir()
  583.                                     dig(1)
  584.                                     changeDir()
  585.                                 end
  586.                             end
  587.                         else
  588.                             local y_amount = math.floor(home_y) - math.floor(y)
  589.                             print(y_amount)
  590.                             dig(y_amount)
  591.                             if rows > 0 then
  592.                                 if height_row % 4 == 2 then
  593.                                     changeDir()
  594.                                     dig(1)
  595.                                     changeDir()
  596.                                 else
  597.                                     changeDirRight()
  598.                                     dig(1)
  599.                                     changeDirRight()
  600.                                 end
  601.                             end
  602.                         end
  603.                     end
  604.                 until rows == 0
  605.                 local x, y, z = gps.locate(5)
  606.  
  607.                 print(z)
  608.                 print((home_z - Depth) + 2)
  609.  
  610.                 if math.floor(z) > ((home_z - Depth) + 2) then
  611.                    
  612.                     turtle.digDown()
  613.                     turtle.down()
  614.                     turtle.digDown()
  615.                     turtle.down()
  616.                     changeDir()
  617.                     changeDir()
  618.                 end
  619.             until math.floor(z) <= ((math.floor(home_z) - math.floor(Depth)) + 2)
  620.         end
  621.     end
  622.     emptyInv()
  623.     goHome()
  624.     --end
  625.  
  626.     local h = fs.open("query/firstInput", "r")
  627.     local readline_1 = h.readLine()
  628.     local readline_2 = h.readLine()
  629.     local doneOrNot = tostring(h.readLine())
  630.     local readline_4 = h.readLine()
  631.     local readline_5 = h.readLine()
  632.     h.close()
  633.  
  634.     fs.delete("query/firstInput")
  635.     if fs.exists("query/firstInput") then
  636.         print("file not deleted or edited")
  637.     else
  638.         local h = fs.open("query/firstInput", "w")
  639.         h.writeLine(readline_1)
  640.         h.writeLine(readline_2)
  641.         h.writeLine("done")
  642.         h.writeLine(readline_4)
  643.         h.writeLine(readline_5)
  644.         h.close()
  645.     end
  646.     print("Done!")
  647.     deleteOrNot()
  648. end
  649.  
  650. --start
  651. if fs.exists("query/firstInput") then
  652.     --we zijn gecrasht...
  653.     local h = fs.open("query/firstInput", "r")
  654.     local given_length = h.readLine()
  655.     local given_width = h.readLine()
  656.     local doneOrNot = tostring(h.readLine())
  657.     start_direction = h.readLine()
  658.     local given_depth = h.readLine()
  659.     h.close()
  660.     sleep(1)
  661.     local h = fs.open("query/home_pos", "r")
  662.     local given_x = h.readLine()
  663.     local given_y = h.readLine()
  664.     local given_z = h.readLine()
  665.     h.close()
  666.     sleep(1)
  667.  
  668.     if tostring(doneOrNot) == "notDone" then
  669.         Length = given_length
  670.         Width = given_width
  671.         Depth = given_depth
  672.         home_x = given_x
  673.         home_y = given_y
  674.         home_z = given_z
  675.         crashOccured()
  676.     elseif tostring(doneOrNot) == 'done' then
  677.         home_x = given_x
  678.         home_y = given_y
  679.         home_z = given_z
  680.         print("Okey")
  681.         if math.floor(home_x) == math.floor(x) and math.floor(home_y) == math.floor(y) and math.floor(home_z) == math.floor(z) then
  682.             deleteOrNot()
  683.         else
  684.             goHome()
  685.             deleteOrNot()
  686.         end
  687.     end
  688. else
  689.     --eerste input
  690.     print("V1 of the query bot with ender chest. Remember to put an ender chest in slot 1")
  691.     term.write("Insert Length: ")
  692.     Length = tonumber(read())
  693.     print(" ")
  694.  
  695.     term.write("Insert Width: ")
  696.     Width = tonumber(read())
  697.     print(" ")
  698.  
  699.     term.write("Insert Depth (0 = to bedrock): ")
  700.     Depth = tonumber(read())
  701.     print(" ")
  702.  
  703.     --make it so we know first input was true
  704.     print("making folder")
  705.     fs.makeDir("query")
  706.  
  707.     checkCompas()
  708.  
  709.     start_direction = dig_direction
  710.  
  711.     print("making a first input file")
  712.     local h = fs.open("query/firstInput", "w")
  713.     h.writeLine(Length)
  714.     h.writeLine(Width)
  715.     h.writeLine("notDone")
  716.     h.writeLine(start_direction)
  717.     h.writeLine(Depth)
  718.     h.close()
  719.  
  720.     local x, y, z = gps.locate(5)
  721.     x = math.floor(x)
  722.     y = math.floor(y)
  723.     z = math.floor(z)
  724.  
  725.     print("Making home pos file")
  726.     local h = fs.open("query/home_pos", "w")
  727.     h.writeLine(x)
  728.     h.writeLine(y)
  729.     h.writeLine(z)
  730.     h.close()
  731.  
  732.     print("Making startup file")
  733.     if fs.exists("startup") then
  734.         fs.delete("startup")
  735.         local h = fs.open("startup", "w")
  736.         h.writeLine("sleep(5)")
  737.         h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  738.         h.close()
  739.     else
  740.         h.writeLine("sleep(5)")
  741.         local h = fs.open("startup", "w")
  742.         h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  743.         h.close()
  744.     end
  745.    
  746.     Length = Length
  747.     Width = Width
  748.     home_x = x
  749.     home_y = y
  750.     home_z = z
  751.  
  752.     digQuery()
  753. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement