Advertisement
M0n5t3r

Query bot

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