Advertisement
Fiixii52

Excavator 2000

May 28th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.82 KB | None | 0 0
  1. Arguments = {...}
  2. MineSlot = 1 -- Enderchest slot for ores
  3. EnderFuelSlot = 2 -- Enderchest slot for fuel
  4. FuelSlot = 3 -- Fuel slot
  5.  
  6. TurnRight = true -- Tells the turtle if it has to turn left or right when digging a full line
  7.  
  8. -- Coordinates :
  9. x = 1
  10. y = 1
  11.  
  12. MiningSave = {} -- This table is where every function will refer to. It will store all the variables of the program and allows the turtle to progress in the mining process without being lost.
  13. -- Here is what every indice refers to :
  14. -- 1 : Length of the quarry
  15. -- 2 : Width of the quarry
  16. -- 3 : Initial height of the turtle
  17. -- 4,5 : Coordinates of the turtle
  18. -- 6 : TurnRight
  19. -- 7,8,9,10,11 : Respectively bedrock, stone, gravel, dirt and sand
  20.  
  21.  
  22. -- Move + Attack + Dig functions
  23.  
  24. function MoveForward() -- Move forward + compare up and down
  25.      while turtle.attack() do
  26.            os.sleep(1)
  27.      end
  28.      Mine()
  29.      while turtle.detect() do
  30.            turtle.dig()
  31.      end
  32.      turtle.forward()
  33. end
  34.  
  35. function MoveUp() -- Move up
  36.    while turtle.attackUp() do
  37.          os.sleep(1)
  38.    end
  39.    while turtle.detectUp() do
  40.          turtle.digUp()
  41.    end
  42.    turtle.up()
  43.    Fuel()
  44. end
  45.  
  46. function MoveDown() -- Move down
  47.     while turtle.attackDown() do
  48.           os.sleep(1)
  49.     end
  50.     while turtle.detectDown() do
  51.           turtle.digDown()
  52.     end
  53.     turtle.down()
  54.     Fuel()
  55. end
  56.  
  57. function ChangeSide() -- Determines if the turtle has to turn left or right to dig the next line
  58.     if MiningSave[6] == true then
  59.           turtle.turnRight()
  60.           MoveForward()
  61.           turtle.turnRight()
  62.           MiningSave[6] = false
  63.     else turtle.turnLeft()
  64.          MoveForward()
  65.          turtle.turnLeft()
  66.          MiningSave[6] = true
  67.     end
  68. end
  69.  
  70. function Bedrock()
  71.     for i=Height-1,1,-1 do
  72.         MoveUp()
  73.         Fuel()
  74.         OreVerif()
  75.     end
  76.     Oresend()
  77.     fs.delete("MiningFile")
  78.     os.sleep()
  79. end
  80.  
  81. -- End of moving functions
  82.  
  83. function Mine()
  84.      if isJunkBlock("up") == false then
  85.            turtle.digUp()
  86.      end
  87.      if isJunkBlock("down") == false then
  88.             turtle.digDown()
  89.      end
  90. end
  91.  
  92. function isJunkBlock(direction)
  93.     for i=9,12 do
  94.         local Success,Data
  95.         if direction == "up" then
  96.              Success,Data = turtle.inspectUp()
  97.         else Success,Data = turtle.inspectDown()
  98.         end
  99.         if Data.name == MiningSave[i] then
  100.             return true
  101.         end
  102.     end
  103.     return false
  104. end
  105.  
  106. function isBedrock()
  107.     local MovingNumber = 0
  108.     for i=1,3 do
  109.         local Success,Data = turtle.inspectDown()
  110.         if Success and Data.name == MiningSave[7] and MovingNumber < 3 then
  111.                  Bedrock()
  112.         else MovingNumber = MovingNumber+1
  113.         end
  114.         MoveDown()
  115.     end
  116. end
  117.  
  118. function OreVerif() -- Detects if there are blocks in the last slot
  119.    if turtle.getItemCount(16) >= 1 then
  120.          if turtle.getFuelLevel() <= 4 then
  121.               Refuel()
  122.          end
  123.          Oresend()
  124.    else return true
  125.    end
  126. end
  127.  
  128. function Oresend() -- Sends ores to the enderchest
  129.    if turtle.detectUp() then
  130.        turtle.digUp()
  131.        os.sleep(0.2)
  132.    end
  133.    MoveUp()
  134.    if turtle.detectUp() then
  135.        turtle.digUp()
  136.        os.sleep(0.2)
  137.    end
  138.    MoveDown()
  139.    turtle.select(MineSlot)
  140.    turtle.placeUp()
  141.    for i=4,16 do
  142.      turtle.select(i)
  143.      turtle.dropUp()
  144.    end
  145.    turtle.select(MineSlot)
  146.    turtle.digUp()
  147. end
  148.  
  149. function Fuel() -- Detects if the fuel level is too low
  150.    if turtle.getFuelLevel() <= 4 then
  151.         turtle.select(FuelSlot)
  152.         if turtle.getItemCount() <= 2 then
  153.              Refuel()
  154.         end
  155.         turtle.refuel(1)
  156.    else return true
  157.    end
  158. end
  159.  
  160. function Refuel() -- Reloads with fuel in the enderchest
  161.    local Space=turtle.getItemSpace(FuelSlot)
  162.    if turtle.detectUp() == true then
  163.        turtle.digUp()
  164.        os.sleep(0.2)
  165.    end
  166.    MoveUp()
  167.    if turtle.detectUp() == true then
  168.        turtle.digUp()
  169.        os.sleep(0.2)
  170.    end
  171.    MoveDown()
  172.    turtle.select(EnderFuelSlot)
  173.    turtle.placeUp()
  174.    turtle.select(FuelSlot)
  175.    turtle.suckUp(Space)
  176.    turtle.select(EnderFuelSlot)
  177.    turtle.digUp()
  178. end  
  179.  
  180. function FsSave()
  181.    local file = fs.open("MiningFile","w")
  182.    file.write(textutils.serialize(MiningSave))
  183.    file.close()
  184. end
  185.  
  186. function FsLoad()
  187.       local file = fs.open("MiningFile","r")
  188.       local MiningData = file.readAll()
  189.       file.close()
  190.       MiningSave = textutils.unserialize(MiningData)
  191.       Perimeter = 2*(MiningSave[1]+MiningSave[2])
  192. end
  193.  
  194. function Recover()
  195.      if turtle.getItemCount(1) == 0 then
  196.         turtle.select(1)
  197.         turtle.digUp()
  198.      elseif turtle.getItemCount(2) == 0 then
  199.             turtle.select(2)
  200.             turtle.digUp()
  201.      end
  202. end
  203.  
  204. function Start()
  205.      if #Arguments == 3 then
  206.           Length = Arguments[1]
  207.           Width = Arguments[2]
  208.           Height = Arguments[3]
  209.      else os.reboot()
  210.      end
  211.      if turtle.getItemCount(1) == 0 or turtle.getItemCount(2) == 0 or turtle.getItemCount(3) == 0 then
  212.           os.reboot()
  213.      end
  214.      print("Excavator 2000 by Fiixii52")
  215.      print("The turtle will dig this zone down to the bedrock.")
  216.      print("This program only works with FLAT bedrock. Please ensure that it is your case !")
  217.      print("Type Yes to start it.")
  218.      local start = io.read()
  219.      if start == "Yes" then
  220.            term.clear()
  221.            turtle.select(4)
  222.            table.insert(MiningSave,Length)
  223.            table.insert(MiningSave,Width)
  224.            table.insert(MiningSave,Height)
  225.            table.insert(MiningSave,x)
  226.            table.insert(MiningSave,y)
  227.            table.insert(MiningSave,TurnRight)
  228.            table.insert(MiningSave,"minecraft:bedrock")
  229.            table.insert(MiningSave,"minecraft:stone")
  230.            table.insert(MiningSave,"minecraft:gravel")
  231.            table.insert(MiningSave,"minecraft:dirt")
  232.            table.insert(MiningSave,"minecraft:sand")
  233.            for i=1,11 do
  234.                print(MiningSave[i])
  235.            end
  236.            Perimeter = 2*(MiningSave[1]+MiningSave[2])
  237.      else os.reboot()
  238.      end
  239. end
  240.  
  241. if fs.exists("MiningFile") == true then
  242.      Recover()
  243.      FsLoad()
  244.      Fuel()
  245.      OreVerif()
  246. else Start()
  247. end
  248.  
  249. while true do
  250.          Fuel()
  251.          OreVerif()
  252.          if MiningSave[4] == MiningSave[1] then
  253.               if MiningSave[5] == MiningSave[2] then
  254.                         if MiningSave[6] == true then
  255.                                 turtle.turnRight()
  256.                                 turtle.turnRight()
  257.                         else turtle.turnRight()
  258.                         end
  259.                         isBedrock()
  260.                         MiningSave[4] = 1
  261.                         MiningSave[5] = 1
  262.                         if MiningSave[6] == false then -- Calculates the new width and length if the turtle finished digging the zone in the opposite direction of when he started.
  263.                            MiningSave[1] = MiningSave[2]
  264.                            MiningSave[2] = (Perimeter-2*MiningSave[1])/2 -- Calculates the new width with the perimeter which stays the same. If we would have typed MiningSave[2] = MiningSave[1], it would have taken the new value of MiningSave[1].
  265.                            MiningSave[6] = true
  266.                         end
  267.               else Mine()
  268.                    ChangeSide()
  269.                    MiningSave[5] = MiningSave[5]+1
  270.                    MiningSave[4] = 1
  271.               end
  272.          else MoveForward()
  273.               MiningSave[4] = MiningSave[4]+1
  274.          end
  275.          FsSave()
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement