Advertisement
cing5000

restart quarry

Apr 9th, 2015
1,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.81 KB | None | 0 0
  1. local checkItems = {
  2.   "minecraft:dirt",
  3.   "minecraft:stone",
  4.   "minecraft:gravel",
  5.   "minecraft:chest",
  6.   "minecraft:flowing_lava"
  7.     }
  8.  
  9. local cDir = "north"
  10. local cX = 0
  11. local cY = 0
  12. local cZ = 0
  13. local home
  14. local workingY
  15. local status
  16. local workingRow
  17. local turns = 0
  18. local special = "none"
  19. local preMoveFuel
  20.  
  21. function clear()
  22.         term.clear()
  23.         term.setCursorPos(1,1)
  24. end
  25.  
  26. --saves data to file
  27. function saveCor()
  28.         local fileHandle = fs.open("path","w")
  29.         fileHandle.writeLine(cX)
  30.         fileHandle.writeLine(cY)
  31.         fileHandle.writeLine(cZ)
  32.         fileHandle.writeLine(cDir)
  33.         fileHandle.writeLine(workingY)
  34.         fileHandle.writeLine(size)
  35.         fileHandle.writeLine(toY)
  36.         fileHandle.writeLine(home)
  37.         fileHandle.writeLine(status)
  38.         fileHandle.writeLine(turns)
  39.         fileHandle.writeLine(special)
  40.         fileHandle.writeLine(preMoveFuel)
  41.         fileHandle.close()
  42. end
  43.  
  44. --deletes the file
  45. function deleteFiles()
  46.         fs.delete("path")
  47. end
  48.  
  49. --overide of turtle.turnRightfucntion
  50. function turnRight()
  51.   if cDir == "north" then
  52.     cDir = "east"
  53.   elseif cDir == "east" then
  54.     cDir = "south"
  55.   elseif cDir == "south" then
  56.     cDir = "west"
  57.   elseif cDir == "west" then
  58.     cDir = "north"
  59.   else
  60.     print("Error!!")
  61.         error()
  62.   end
  63.   turtle.turnRight()
  64. end
  65.  
  66. --overide of turtle.turnLeft fucntion
  67. function turnLeft()
  68.         if cDir == "north" then
  69.                 cDir = "west"
  70.         elseif cDir == "east" then
  71.                 cDir = "north"
  72.         elseif cDir == "south" then
  73.                 cDir = "east"
  74.         elseif cDir == "west" then
  75.                 cDir = "south"
  76.         else
  77.                 print("Error!!")
  78.                 error()
  79.         end
  80.         turtle.turnLeft()
  81. end
  82.  
  83. --turns to specified direction
  84. function turnTo(dir)
  85.   if cDir ~= dir then
  86.     repeat
  87.      turnRight()
  88.     until cDir == dir
  89.   end
  90. end
  91.  
  92. --overide of turtle.moveForward fucntion
  93. function moveForward()
  94.         local succes, data = turtle.inspect()
  95.         if succes then
  96.                 if data.name == checkItems[4] then
  97.                         for i = 1, 27 do      
  98.                                 turtle.suck()
  99.                         end
  100.                 end
  101.         end
  102.         if cDir == "north" then
  103.                 cZ = cZ-1
  104.         elseif cDir == "east" then
  105.                 cX = cX+1
  106.         elseif cDir == "south" then
  107.                 cZ = cZ+1
  108.         elseif cDir == "west" then
  109.                 cX = cX-1
  110.         else
  111.                 print("Error!!")
  112.                 error()
  113.         end
  114.         while turtle.detect() do
  115.                 turtle.dig()
  116.         end
  117.         status = "forward"
  118.         preMoveFuel = turtle.getFuelLevel()
  119.         saveCor()
  120.         while not turtle.forward() do
  121.                 turtle.dig()
  122.         end
  123.         status = "none"
  124.         saveCor()
  125. end
  126.  
  127. --overide of turtle.moveUp fucntion
  128. function moveUp()
  129.         cY = cY+1
  130.         while turtle.detectUp() do    
  131.                 turtle.digUp()
  132.                 sleep(0.1)
  133.         end
  134.         status = "up"
  135.         preMoveFuel = turtle.getFuelLevel()
  136.         saveCor()
  137.         while not turtle.up() do
  138.                 turtle.digUp()
  139.         end
  140.         status = "none"
  141.         saveCor()
  142. end
  143.  
  144. --overide of turtle.movedown fucntion
  145. function moveDown()
  146.         cY = cY-1
  147.         while turtle.detectDown() do  
  148.                 turtle.digDown()
  149.         end
  150.         status = "down"
  151.         preMoveFuel = turtle.getFuelLevel()
  152.         saveCor()
  153.         while not turtle.down() do
  154.                 turtle.digDown()
  155.         end
  156.         status = "none"
  157.         saveCor()
  158. end
  159.    
  160. --move to specific y  
  161. function moveToX(x)
  162.   if cX < x then
  163.     turnTo("east")
  164.     repeat
  165.       moveForward()
  166.     until cX == x
  167.   elseif cX > x then
  168.     turnTo("west")
  169.     repeat
  170.       moveForward()
  171.     until cX == x
  172.   else
  173.     --print("already there")
  174.   end
  175. end
  176.  
  177. --move to specific z
  178. function moveToZ(z)
  179.   if cZ < z then
  180.     turnTo("south")
  181.     repeat
  182.       moveForward()
  183.     until cZ == z
  184.   elseif cZ > z then
  185.     turnTo("north")
  186.     repeat
  187.       moveForward()
  188.     until cZ == z
  189.   else
  190.    --print("already there")
  191.   end
  192. end    
  193.  
  194. --move to specific y
  195. function moveToY(y)
  196.   if cY < y then
  197.     repeat
  198.       moveUp()
  199.     until cY == y
  200.   elseif cY > y then
  201.     repeat
  202.       moveDown()
  203.     until cY == y
  204.   else
  205.    --print("already there")
  206.   end
  207. end
  208.  
  209. --goes to a specific place
  210. function goTo(x, y, z)
  211.   moveToX(x)  
  212.   moveToY(y)
  213.   moveToZ(z)
  214.   turnTo("north")
  215. end
  216.  
  217. --if there is space in his inventory
  218. function checkInventory()
  219.   if turtle.getItemDetail(13) == nil then
  220.   else
  221.     dropItems()
  222.   end
  223. end
  224.  
  225. --drops its items in his enderchest
  226. function dropItems()
  227.         special = "drop items"
  228.         saveCor()
  229.         turnRight()
  230.         turnRight()
  231.         turtle.select(16)
  232.         while not turtle.place() do
  233.                 turtle.dig()
  234.         end  
  235.         for i = 1, 14 do
  236.                 turtle.select(i)
  237.                 turtle.drop()
  238.         end
  239.         turtle.select(16)
  240.         turtle.dig()
  241.         turtle.select(1)
  242.         turnRight()
  243.         turnRight()
  244.         special = "none"
  245.         saveCor()
  246. end
  247.  
  248. --Needed for checkDown()
  249. function  lavaDown()
  250.         local data = turtle.getItemDetail(15)
  251.         if data.name == "minecraft:bucket" then
  252.                 turtle.select(15)
  253.                 turtle.placeDown()
  254.                 turtle.refuel()
  255.                 turtle.select(1)
  256.         elseif data == nil then
  257.         else
  258.                 turtle.select(15)
  259.                 turtle.placeDown()
  260.                 turtle.select(1)
  261.         end
  262. end
  263.  
  264. --needed for checkUp()      
  265. function  lavaUp()
  266.         local data = turtle.getItemDetail(15)
  267.         if data.name == "minecraft:bucket" then
  268.                 turtle.select(15)
  269.                 turtle.placeUp()
  270.                 turtle.refuel()
  271.                 turtle.select(1)
  272.         elseif data == nil then
  273.         else
  274.                 turtle.select(15)
  275.                 turtle.placeUp()
  276.                 turtle.select(1)
  277.         end
  278. end
  279.  
  280. -checks block down
  281. function checkDown()
  282. local succes, data = turtle.inspectDown()
  283.   if succes then
  284.     if data.name == checkItems[1] then
  285.     elseif data.name == checkItems[2] then
  286.       elseif data.name == checkItems[3] then  
  287.         elseif data.name == checkItems[4] then
  288.           for i = 1, 27 do
  289.             turtle.suckDown()
  290.           end
  291.           turtle.digDown()
  292.                         elseif data.name == checkItems[5] then
  293.                                 lavaDown()
  294.             else
  295.           turtle.digDown()      
  296.       end
  297.    end
  298. end
  299.  
  300. --checks block up
  301. function checkUp()
  302. local succes, data = turtle.inspectUp()
  303.   if succes then
  304.     if data.name == checkItems[1] then
  305.       elseif data.name == checkItems[2] then
  306.           elseif data.name == checkItems[4] then
  307.             for i = 1, 27 do
  308.               turtle.suckUp()
  309.             end
  310.             turtle.digUp()
  311.             elseif data.name== checkItems[5] then
  312.                                 lavaUp()
  313.           else
  314.           turtle.digUp()    
  315.        end    
  316.     end
  317. end
  318.  
  319. --Checks the blocks above and below
  320. function checkBlocks()
  321.         checkUp()
  322.         checkDown()
  323.         checkInventory()
  324. end
  325.  
  326. --part of square
  327. function row()
  328.         checkBlocks()  
  329.         for i = 1, size-1 do
  330.                 moveForward()
  331.                 checkBlocks()
  332.         end
  333. end
  334.  
  335. --Does one layer of mining
  336. function square()
  337. workingY = cY
  338. saveCor()
  339. print("Layer: "..workingY)
  340. local count = 1
  341.         for i = 1, size do
  342.                 row()
  343.                 if count ~= size then
  344.                         if turns == 0 then
  345.                                 turnRight()
  346.                                 moveForward()
  347.                                 turnRight()
  348.                                 turns = 1
  349.                         else
  350.                                 turnLeft()
  351.                                 moveForward()
  352.                                 turnLeft()
  353.                                 turns = 0
  354.                         end
  355.                         count = count+1
  356.                 end
  357.         end
  358. end
  359.  
  360. --goes to the next layer
  361. function nextLayer()
  362.   special = "Next_layer"
  363.   saveCor()
  364.   for i = 1,3 do
  365.     moveUp()
  366.   end
  367.   turnRight()
  368.   turnRight()
  369.   special = "none"
  370.   saveCor()
  371. end
  372.  
  373. --main mining program
  374. function Bedrock()
  375.         square()
  376.         while cY ~= toY do
  377.                 if cY == toY-1  then
  378.                         break
  379.                 elseif cY == toY-2 then
  380.                         moveUp()
  381.                         moveUp()
  382.                         turnRight()
  383.                         turnRight()
  384.                         square()
  385.                 else
  386.                         nextLayer()
  387.                         square()
  388.                 end
  389.         end
  390. end
  391.  
  392. --calculates the energy needed
  393. function calcEnergy()
  394.         eNeeded = size*size*(cY/3)+cY*2
  395. end
  396.  
  397. --starting program part 1
  398. function startProg()
  399.         print("---------------------------------------")
  400.         print("Advanced ore mining quarry")
  401.         print("Made by: Cing")
  402.         print("---------------------------------------")
  403.  
  404.         print("Please put first coal in slot 1...")
  405.         print("a bucket or filling blocks in slot 15..")
  406.         print("and a enderchest in slot 16.")
  407.         print("And don't forget to chunkload the area.")
  408.         os.pullEvent()
  409.        
  410.         while turtle.getItemDetail(16) == nil do
  411.                 print("Please put a enderchest in slot 16.")
  412.                 os.pullEvent()
  413.         end
  414.        
  415.         write("What is the YLevel: ")
  416.         cY = tonumber(read())
  417.         home = cY
  418.         workingY = 2
  419.        
  420.         write("What size do you want: ")
  421.         size = tonumber(read())
  422.        
  423.         write("Until what Y should I dig: ")
  424.         toY = tonumber(read())
  425.        
  426.         turtle.select(1)
  427.         turtle.refuel()
  428.         calcEnergy()
  429.         while eNeeded > turtle.getFuelLevel() do
  430.                 print("Please put more coal in slot 1")
  431.                 os.pullEvent()
  432.                 turtle.refuel()
  433.         end
  434.        
  435.         saveCor()
  436. end
  437.  
  438. --starting program part 2
  439. function working()
  440.         special = "going down"
  441.         saveCor()
  442.         while cY ~= 2 do
  443.                 moveDown()
  444.         end
  445.         special = "none"
  446.         saveCor()
  447.         clear()
  448.         print("Digging!")
  449.         Bedrock()
  450.         special = "going up"
  451.         saveCor()
  452.         goTo(0,home,0)
  453.         dropItems()
  454.         deleteFiles()
  455. end
  456.  
  457. --check if the turtle has moved or not
  458. function checkMove()
  459.         sleep(1)
  460.         if status == "forward" then
  461.                 if preMoveFuel == turtle.getFuelLevel() then
  462.                         if cDir == "north" then
  463.                                 cZ = cZ+1
  464.                         elseif cDir == "east" then
  465.                                 cX = cX-1
  466.                         elseif cDir == "south" then
  467.                                 cZ = cZ-1
  468.                         elseif cDir == "west" then
  469.                                 cX = cX+1
  470.                         else
  471.                                 print("There went something wrong!")
  472.                                 error()
  473.                         end
  474.                         status = "none"
  475.                         saveCor()
  476.                 elseif preMoveFuel == turtle.getFuelLevel()-1 then
  477.                         status = "none"
  478.                         saveCor()
  479.                 else
  480.                         print("There went something wrong!")
  481.                         error()
  482.                 end
  483.         elseif status == "up" then
  484.                 if preMoveFuel == turtle.getFuelLevel() then
  485.                         cY = cY-1
  486.                         status = "none"
  487.                         saveCor()
  488.                 elseif preMoveFuel == turtle.getFuelLevel()-1 then
  489.                         status = "none"
  490.                         saveCor()
  491.                 else
  492.                         print("There went something wrong!")
  493.                         error()
  494.                 end
  495.         elseif status == "down" then
  496.                 if preMoveFuel == turtle.getFuelLevel() then
  497.                         cY = cY+1
  498.                         status = "none"
  499.                         saveCor()
  500.                 elseif preMoveFuel == turtle.getFuelLevel()-1 then
  501.                         status = "none"
  502.                         saveCor()
  503.                 else
  504.                         print("There went something wrong!")
  505.                         error()
  506.                 end
  507.         elseif status == "none" then
  508.         else
  509.                 print("There went something wrong!")
  510.                 error()
  511.         end
  512. end
  513.  
  514.  
  515. --code for resuming after shutdown
  516. function resume()
  517.         if cDir == "east" or cDir == "west" or special == "Next_layer" then
  518.                 goTo(0,workingY,0)
  519.                 turns = 0
  520.         else
  521.                 if cDir == "north" then
  522.                         while (cZ*-1)+1 ~= size do
  523.                                 moveForward()
  524.                                 checkBlocks()
  525.                         end
  526.                         if turns ==  0 then
  527.                                 if cX ~= size-1 then
  528.                                         turnRight()
  529.                                         moveForward()
  530.                                         turnRight()
  531.                                         turns = 1
  532.                                 end
  533.                         else
  534.                                 turnLeft()
  535.                                 moveForward()
  536.                                 turnLeft()
  537.                                 turns = 0      
  538.                         end            
  539.                 elseif cDir == "south" then
  540.                         while cZ ~= 0 do
  541.                                 moveForward()
  542.                                 checkBlocks()
  543.                         end
  544.                         if turns == 1  then
  545.                                 if cX ~= size-1 then
  546.                                         turnLeft()
  547.                                         moveForward()
  548.                                         turnLeft()
  549.                                         turns = 0
  550.                                 end
  551.                         else
  552.                                 if cX ~= 0 then
  553.                                         turnRight()
  554.                                         moveForward()
  555.                                         turnLeft()
  556.                                         turns = 1
  557.                                 end
  558.                         end
  559.                 end
  560.                 if cDir == "north" and turns == 0 then
  561.                         print("Layer Positief")
  562.                         if cX+1 ~= size then
  563.                                 for i = 1, (size-(cX+1)) do
  564.                                         row()
  565.                                         if cX ~= size-1 then
  566.                                                 if turns == 0 then
  567.                                                         turnRight()
  568.                                                         moveForward()
  569.                                                         turnRight()
  570.                                                         turns = 1
  571.                                                 else
  572.                                                         turnLeft()
  573.                                                         moveForward()
  574.                                                         turnLeft()
  575.                                                         turns = 0
  576.                                                 end
  577.                                         end
  578.                                 end
  579.                                 row()
  580.                         end
  581.                 elseif cDir == "south" and turns == 1 then
  582.                         print("Layer Positief")
  583.                         if cX+1 ~= size then
  584.                                 for i = 1, (size-(cX+1)) do
  585.                                         row()
  586.                                         if cX ~= size-1 then
  587.                                                 if turns == 0 then
  588.                                                         turnRight()
  589.                                                         moveForward()
  590.                                                         turnRight()
  591.                                                         turns = 1
  592.                                                 else
  593.                                                         turnLeft()
  594.                                                         moveForward()
  595.                                                         turnLeft()
  596.                                                         turns = 0
  597.                                                 end
  598.                                         end
  599.                                 end
  600.                                 row()
  601.                         end
  602.                 elseif cDir == "north" and turns == 1 then
  603.                         print("Layer Negetiaf")
  604.                         if cX ~= 0 then
  605.                                 for i = 1, cX+1 do
  606.                                         row()
  607.                                         if cX ~= 0 then
  608.                                                 if turns == 0 then
  609.                                                         turnRight()
  610.                                                         moveForward()
  611.                                                         turnRight()
  612.                                                         turns = 1
  613.                                                 else
  614.                                                         turnLeft()
  615.                                                         moveForward()
  616.                                                         turnLeft()
  617.                                                         turns = 0
  618.                                                 end
  619.                                         end
  620.                                 end
  621.                                 row()
  622.                         end
  623.                 elseif cDir == "south" and turns == 0 then
  624.                         print("Layer Negetiaf")
  625.                         if cX ~= 0 then
  626.                                 for i = 1, cX+1 do
  627.                                         row()
  628.                                         if cX ~= 0 then
  629.                                                 if turns == 0 then
  630.                                                         turnRight()
  631.                                                         moveForward()
  632.                                                         turnRight()
  633.                                                         turns = 1
  634.                                                 else
  635.                                                         turnLeft()
  636.                                                         moveForward()
  637.                                                         turnLeft()
  638.                                                         turns = 0
  639.                                                 end
  640.                                         end
  641.                                 end
  642.                                 row()
  643.                         end
  644.                 else
  645.                         print("There went something wrong!")
  646.                         error()
  647.                 end
  648.                 if cY == toY then
  649.                         goTo(0,cY,0)
  650.                         turns = 0
  651.                         saveCor()
  652.                 elseif cY == toY-1 then
  653.                         goTo(0,cY,0)
  654.                         turns = 0
  655.                         saveCor()      
  656.                 elseif cY == toY-2 then
  657.                         goTo(0,(cY+2),0)
  658.                         turns = 0
  659.                         saveCor()
  660.                 else
  661.                         nextLayer()
  662.                         goTo(0,cY,0)
  663.                         turns = 0
  664.                         saveCor()
  665.                 end
  666.         end
  667. end
  668.  
  669. --Main program
  670. if fs.exists("path") then
  671.         local file = fs.open("path","r")
  672.                 cX = tonumber(file.readLine())
  673.                 cY = tonumber(file.readLine())
  674.                 cZ = tonumber(file.readLine())
  675.                 cDir = file.readLine()
  676.                 workingY = tonumber(file.readLine())
  677.                 size = tonumber(file.readLine())
  678.                 toY = tonumber(file.readLine())
  679.                 home =  tonumber(file.readLine())
  680.                 status = file.readLine()
  681.                 turns = tonumber(file.readLine())
  682.                 special = file.readLine()
  683.                 preMoveFuel = tonumber(file.readLine())
  684.         file.close()
  685.        
  686.         checkMove()
  687.        
  688.         if special == "drop items" then
  689.                 if turtle.getItemCount(16) == 0 then  
  690.                         print("No enderchest")
  691.                         error()
  692.                 else
  693.                         goTo(0,workingY,0)
  694.                         turns = 0
  695.                         special = "none"
  696.                         saveCor()
  697.                 end
  698.         elseif special == "going down"  then
  699.                 goTo(0,2,0)
  700.                 turns = 0
  701.         elseif special == "going up" then
  702.                 goTo(0,home,0)
  703.                 dropItems()
  704.                 deleteFiles()
  705.                 clear()
  706.                 print("Done!!")
  707.                 error()
  708.         else
  709.                 resume()
  710.         end
  711.         Bedrock()
  712.         goTo(0,home,0)
  713.         dropItems()
  714.         deleteFiles()
  715.         clear()
  716.         print("Done!!!")
  717. else
  718.         startProg()
  719.         working()
  720.         clear()
  721.         print("Done!!!")
  722. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement