le_Fish

Dig api v12

Apr 27th, 2020
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.28 KB | None | 0 0
  1. Dig = {
  2.     pos = vector.new(0, 0, 0),
  3.     dir = "e",
  4.     fluid = {},
  5.     walls = {},
  6.     initialHeight = 85
  7. }
  8.  
  9. function Dig:new(pos, dir, initialHeight)
  10.     local pos = pos
  11.     local dir = dir
  12.     local initialHeight = initialHeight or 85
  13.     if type(pos) ~= "table" then
  14.         error("new pos expects to be a vector, currently is " .. type(pos))
  15.     elseif type(dir) ~= "string" then
  16.         error("new dir expects to be a string, currently is " .. type(dir))
  17.     elseif dir:lower() ~= "n" and dir:lower() ~= "s" and dir:lower() ~= "w" and dir:lower() ~= "e" then
  18.         error("new dir expects to be n, s, w or e, currently is " .. dir:lower())
  19.     elseif type(initialHeight) ~= "number" then
  20.         error("new initialHeight expects to be a number, currently is " .. type(initialHeight))
  21.     elseif initialHeight > 256 or initialHeight < 6 then
  22.         error("new initialHeight should be lower than 256 and higher than 6, currently is " .. initialHeight)
  23.     end
  24.  
  25.     setmetatable({}, Dig)
  26.     self.pos = pos
  27.     self.dir = dir:lower()
  28.     self.fluid = {}
  29.     self.initialHeight = initialHeight
  30.     return self
  31. end
  32.  
  33. -- basic movement
  34. function Dig:forward()
  35.     if turtle.forward() then
  36.         if self.dir == "n" then
  37.             self.pos.y = self.pos.y - 1
  38.         elseif self.dir == "s" then
  39.             self.pos.y = self.pos.y + 1
  40.         elseif self.dir == "e" then
  41.             self.pos.x = self.pos.x + 1
  42.         elseif self.dir == "w" then
  43.             self.pos.x = self.pos.x - 1
  44.         end
  45.         return true
  46.     else
  47.         turtle.dig()
  48.         turtle.attack()
  49.         return false
  50.     end
  51. end
  52. function Dig:back(times)
  53.     local times = times or 1
  54.     if (type(times) ~= "number") then
  55.         error("back expects a number, currently is " .. type(times))
  56.     end
  57.     for i = 1, times do
  58.         while not turtle.back() do
  59.             self:left()
  60.             self:left()
  61.             turtle.dig()
  62.             turtle.attack()
  63.             self:left()
  64.             self:left()
  65.         end
  66.         if self.dir == "n" then
  67.             self.pos.y = self.pos.y + 1
  68.         elseif self.dir == "s" then
  69.             self.pos.y = self.pos.y - 1
  70.         elseif self.dir == "e" then
  71.             self.pos.x = self.pos.x - 1
  72.         elseif self.dir == "w" then
  73.             self.pos.x = self.pos.x + 1
  74.         end
  75.     end
  76.     return true
  77. end
  78. function Dig:up(times)
  79.     local times = times or 1
  80.     if (type(times) ~= "number") then
  81.         error("up expects a number, currently is " .. type(times))
  82.     end
  83.     for i = 1, times do
  84.         while not turtle.up() do
  85.             turtle.attackUp()
  86.             turtle.digUp()
  87.         end
  88.         self.pos.z = self.pos.z + 1
  89.     end
  90.     return true
  91. end
  92. function Dig:down(times)
  93.     local times = times or 1
  94.     if (type(times) ~= "number") then
  95.         error("down expects a number, currently is " .. type(times))
  96.     end
  97.     for i = 1, times do
  98.         while not turtle.down() do
  99.             turtle.attackDown()
  100.             turtle.digDown()
  101.         end
  102.         self.pos.z = self.pos.z - 1
  103.     end
  104.     return true
  105. end
  106. function Dig:left(times)
  107.     local times = times or 1
  108.     if (type(times) ~= "number") then
  109.         error("left expects a number, currently is " .. type(times))
  110.     end
  111.     for i = 1, times do
  112.         if turtle.turnLeft() then
  113.             if self.dir == "n" then
  114.                 self.dir = "w"
  115.             elseif self.dir == "w" then
  116.                 self.dir = "s"
  117.             elseif self.dir == "s" then
  118.                 self.dir = "e"
  119.             elseif self.dir == "e" then
  120.                 self.dir = "n"
  121.             end
  122.         end
  123.     end
  124. end
  125. function Dig:right(times)
  126.     local times = times or 1
  127.     if (type(times) ~= "number") then
  128.         error("right expects a number, currently is " .. type(times))
  129.     end
  130.     for i = 1, times do
  131.         if turtle.turnRight() then
  132.             if self.dir == "n" then
  133.                 self.dir = "e"
  134.             elseif self.dir == "e" then
  135.                 self.dir = "s"
  136.             elseif self.dir == "s" then
  137.                 self.dir = "w"
  138.             elseif self.dir == "w" then
  139.                 self.dir = "n"
  140.             end
  141.         end
  142.     end
  143. end
  144. function Dig:turn(direction)
  145.     if type(direction) ~= "string" then
  146.         error("turn expects a string, currently is " .. type(direction), 2)
  147.     elseif
  148.         direction:lower() ~= "e" and direction:lower() ~= "s" and direction:lower() ~= "w" and direction:lower() ~= "n"
  149.      then
  150.         error("turn expects a dir (N,E,S,W), currently is " .. type(direction) .. ", " .. direction, 2)
  151.     end
  152.     local direction = direction:lower()
  153.     local degree = 0
  154.     local selfDegree = 0
  155.     local dif
  156.  
  157.     if direction == "s" then
  158.         degree = 90
  159.     elseif direction == "w" then
  160.         degree = 180
  161.     elseif direction == "n" then
  162.         degree = 270
  163.     end
  164.     if self.dir == "s" then
  165.         selfDegree = 90
  166.     elseif self.dir == "w" then
  167.         selfDegree = 180
  168.     elseif self.dir == "n" then
  169.         selfDegree = 270
  170.     end
  171.  
  172.     dif = degree - selfDegree
  173.     if dif < 0 then
  174.         dif = dif + 360
  175.     end
  176.     if dif > 180 then
  177.         while self.dir ~= direction do
  178.             self:left()
  179.         end
  180.     else
  181.         while self.dir ~= direction do
  182.             self:right()
  183.         end
  184.     end
  185. end
  186.  
  187. function Dig:goToX(position)
  188.     local vector = vector.new(0, 0, 0)
  189.     if type(position) == "number" then
  190.         vector.x = position
  191.     elseif type(vector) ~= "table" and type(position) ~= "number" then
  192.         error("goToX expects a vector or number, currently is " .. type(vector))
  193.     else
  194.         vector = position
  195.     end
  196.     if self.pos.x ~= vector.x then
  197.         if self.pos.x > vector.x then
  198.             self:turn("w")
  199.         elseif self.pos.x < vector.x then
  200.             self:turn("e")
  201.         end
  202.         while self.pos.x ~= vector.x do
  203.             self:forward()
  204.         end
  205.     end
  206. end
  207. function Dig:goToY(position)
  208.     local vector = vector.new(0, 0, 0)
  209.     if type(position) == "number" then
  210.         vector.y = position
  211.     elseif type(vector) ~= "table" and type(position) ~= "number" then
  212.         error("goToY expects a vector or number, currently is " .. type(vector))
  213.     else
  214.         vector = position
  215.     end
  216.     if self.pos.y ~= vector.y then
  217.         if self.pos.y < vector.y then
  218.             self:turn("s")
  219.         elseif self.pos.y > vector.y then
  220.             self:turn("n")
  221.         end
  222.         while self.pos.y ~= vector.y do
  223.             self:forward()
  224.         end
  225.     end
  226. end
  227. function Dig:goToZ(position)
  228.     local vector = vector.new(0, 0, 0)
  229.     if type(position) == "number" then
  230.         vector.z = position
  231.     elseif type(vector) ~= "table" and type(vector) ~= "number" then
  232.         error("goToZ expects a vector or number, currently is " .. type(vector))
  233.     else
  234.         vector = position
  235.     end
  236.     if self.pos.z ~= vector.z then
  237.         if self.pos.z > vector.z then
  238.             while self.pos.z ~= vector.z do
  239.                 self:down()
  240.             end
  241.         elseif self.pos.z < vector.z then
  242.             while self.pos.z ~= vector.z do
  243.                 self:up()
  244.             end
  245.         end
  246.     end
  247. end
  248.  
  249. function Dig:detectFluid(check)
  250.     local save = false
  251.     local saveWall = true
  252.     if check == false then
  253.         saveWall = false
  254.     end
  255.  
  256.     if saveWall ~= false and saveWall ~= true then
  257.         error("Dig detectFluid check should be a bool, currently is " .. saveWall, 2)
  258.     end
  259.     local boolU, up = turtle.inspectUp()
  260.     local boolD, down = turtle.inspectDown()
  261.     local boolF, forward = turtle.inspect()
  262.     local waterU, waterD, waterF, lavaU, lavaD, lavaF = false, false, false, false, false, false
  263.     local doubleCheck = false
  264.     -- Forward
  265.     if
  266.         boolF and forward.metadata == 0 and
  267.             (forward.name == "minecraft:flowing_water" or boolF and forward.name == "minecraft:water")
  268.      then
  269.         waterF = true
  270.     elseif
  271.         boolF and (forward.metadata == 0 or forward.metadata == 1) and
  272.             (forward.name == "minecraft:flowing_lava" or forward.name == "minecraft:lava" or
  273.                 forward.name == "MineFactoryReloaded:sludge.still" or
  274.                 forward.name == "Railcraft:fluid.creosote")
  275.      then
  276.         lavaF = true
  277.     end
  278.     -- Up
  279.     if boolU and up.metadata == 0 and (up.name == "minecraft:flowing_water" or boolU and up.name == "minecraft:water") then
  280.         waterU = true
  281.     elseif
  282.         boolU and (up.metadata == 0 or up.metadata == 1) and
  283.             (up.name == "minecraft:flowing_lava" or up.name == "minecraft:lava" or
  284.                 up.name == "MineFactoryReloaded:sludge.still" or
  285.                 up.name == "Railcraft:fluid.creosote")
  286.      then
  287.         lavaU = true
  288.     end
  289.     -- Down
  290.     if boolD and down.metadata == 0 and (down.name == "minecraft:flowing_water" or down.name == "minecraft:water") then
  291.         waterD = true
  292.     elseif
  293.         boolD and (down.metadata == 0 or down.metadata == 1) and
  294.             (down.name == "minecraft:flowing_lava" or down.name == "minecraft:lava" or
  295.                 down.name == "MineFactoryReloaded:sludge.still" or
  296.                 down.name == "Railcraft:fluid.creosote")
  297.      then
  298.         lavaD = true
  299.     end
  300.  
  301.     local fakePos = self:copy(self.pos)
  302.  
  303.     if self.dir == "n" then
  304.         fakePos.y = fakePos.y - 1
  305.     elseif self.dir == "s" then
  306.         fakePos.y = fakePos.y + 1
  307.     elseif self.dir == "e" then
  308.         fakePos.x = fakePos.x + 1
  309.     elseif self.dir == "w" then
  310.         fakePos.x = fakePos.x - 1
  311.     end
  312.     -- Save positions
  313.     if waterF then
  314.         if self.fluid[self.pos.z] == nil then
  315.             self.fluid[self.pos.z] = {}
  316.         end
  317.         if
  318.             self:dataExists(fakePos.x, fakePos.y, fakePos.z) and fakePos.x ~= 0 and fakePos.x ~= 17 and fakePos.y ~= 0 and
  319.                 fakePos.y ~= 17
  320.          then
  321.             self.fluid[self.pos.z][#self.fluid[self.pos.z] + 1] = {fakePos.x, fakePos.y}
  322.         end
  323.         save = true
  324.     end
  325.     if saveWall and (waterF or lavaF) then
  326.         if
  327.             (self.pos.x == 1 or self.pos.x == 16 or self.pos.y == 1 or self.pos.y == 16) and
  328.                 self.walls[self.pos.z] == nil
  329.          then
  330.             self.walls[self.pos.z] = {}
  331.         end
  332.         if self.pos.x == 1 and self:dataExists(0, self.pos.y, self.pos.z, "walls") then
  333.             self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {0, self.pos.y}
  334.         elseif self.pos.x == 16 and self:dataExists(17, self.pos.y, self.pos.z, "walls") then
  335.             self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {17, self.pos.y}
  336.         end
  337.         if self.pos.y == 1 and self:dataExists(self.pos.x, 0, self.pos.z, "walls") then
  338.             self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {self.pos.x, 0}
  339.         elseif self.pos.y == 16 and self:dataExists(self.pos.x, 17, self.pos.z, "walls") then
  340.             self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {self.pos.x, 17}
  341.         end
  342.     end
  343.     -- up
  344.     if waterU then
  345.         if self.fluid[self.pos.z + 1] == nil then
  346.             self.fluid[self.pos.z + 1] = {}
  347.         end
  348.         if self:dataExists(self.pos.x, self.pos.y, self.pos.z + 1) then
  349.             self.fluid[self.pos.z + 1][#self.fluid[self.pos.z + 1] + 1] = {self.pos.x, self.pos.y}
  350.         end
  351.         save = true
  352.     elseif lavaU then
  353.         self:up()
  354.         self:down()
  355.     end
  356.     if saveWall and (waterU or lavaU) then
  357.         if
  358.             (self.pos.x == 1 or self.pos.x == 16 or self.pos.y == 1 or self.pos.y == 16) and
  359.                 self.walls[self.pos.z + 1] == nil
  360.          then
  361.             self.walls[self.pos.z + 1] = {}
  362.         end
  363.         if self.pos.x == 1 and self:dataExists(0, self.pos.y, self.pos.z + 1, "walls") then
  364.             self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {0, self.pos.y}
  365.         elseif self.pos.x == 16 and self:dataExists(17, self.pos.y, self.pos.z + 1, "walls") then
  366.             self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {17, self.pos.y}
  367.         end
  368.         if self.pos.y == 1 and self:dataExists(self.pos.x, 0, self.pos.z + 1, "walls") then
  369.             self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {self.pos.x, 0}
  370.         elseif self.pos.y == 16 and self:dataExists(self.pos.x, 17, self.pos.z + 1, "walls") then
  371.             self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {self.pos.x, 17}
  372.         end
  373.     end
  374.     -- Down
  375.     if waterD then
  376.         if self.fluid[self.pos.z - 1] == nil then
  377.             self.fluid[self.pos.z - 1] = {}
  378.         end
  379.         if self:dataExists(self.pos.x, self.pos.y, self.pos.z - 1) then
  380.             self.fluid[self.pos.z - 1][#self.fluid[self.pos.z - 1] + 1] = {self.pos.x, self.pos.y}
  381.         end
  382.         save = true
  383.     elseif lavaU then
  384.         self:up()
  385.         self:down()
  386.     end
  387.     if saveWall and (waterD or lavaD) then
  388.         if
  389.             (self.pos.x == 1 or self.pos.x == 16 or self.pos.y == 1 or self.pos.y == 16) and
  390.                 self.walls[self.pos.z - 1] == nil
  391.          then
  392.             self.walls[self.pos.z - 1] = {}
  393.         end
  394.         if self.pos.x == 1 and self:dataExists(0, self.pos.y, self.pos.z - 1, "walls") then
  395.             self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {0, self.pos.y}
  396.         elseif self.pos.x == 16 and self:dataExists(17, self.pos.y, self.pos.z - 1, "walls") then
  397.             self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {17, self.pos.y}
  398.         end
  399.         if self.pos.y == 1 and self:dataExists(self.pos.x, 0, self.pos.z - 1, "walls") then
  400.             self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {self.pos.x, 0}
  401.         elseif self.pos.y == 16 and self:dataExists(self.pos.x, 17, self.pos.z - 1, "walls") then
  402.             self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {self.pos.x, 17}
  403.         end
  404.     end
  405.     -- Save
  406.     if save then
  407.         self:saveFluidData()
  408.     end
  409. end
  410. function Dig:dataExists(x, y, z, type)
  411.     local type = type or "fluid"
  412.     if type == "fluid" then
  413.         if x == 0 or x == 17 or y == 0 or x == 17 then
  414.             return false
  415.         end
  416.         if self.fluid[z] then
  417.             for k, v in pairs(self.fluid[z]) do
  418.                 if v[1] == x and v[2] == y then
  419.                     return false
  420.                 end
  421.             end
  422.         end
  423.     elseif type == "walls" then
  424.         if self.walls[z] then
  425.             for k, v in pairs(self.walls[z]) do
  426.                 if v[1] == x and v[2] == y then
  427.                     return false
  428.                 end
  429.             end
  430.         end
  431.     end
  432.     return true
  433. end
  434. function Dig:removeFluid()
  435.     print("remove fluid")
  436.     local initialZ = tonumber(self.pos.z)
  437.     local initialDir = self.dir
  438.     local heights = {}
  439.     local wallHeights = {}
  440.     local ignoreList = {}
  441.     local boolWallD, wallD
  442.  
  443.     if not fs.exists("data/dig/removeFluids") then
  444.         local file = fs.open("data/dig/removeFluids", "w")
  445.         file.writeLine(initialZ)
  446.         file.writeLine(self.dir)
  447.         file.close()
  448.     else
  449.         local file = fs.open("data/dig/removeFluids", "r")
  450.         initialZ = tonumber(file.readLine())
  451.         initialDir = file.readLine()
  452.         file.close()
  453.     end
  454.     for k, v in pairs(self.fluid) do
  455.         heights[#heights + 1] = k
  456.     end
  457.  
  458.     -- Reverse list for shortest path
  459.     table.sort(
  460.         heights,
  461.         function(a, b)
  462.             return a > b
  463.         end
  464.     )
  465.     table.sort(
  466.         wallHeights,
  467.         function(a, b)
  468.             return a > b
  469.         end
  470.     )
  471.  
  472.     for i = 1, #wallHeights do
  473.         heights[#heights + 1] = wallHeights[i]
  474.     end
  475.  
  476.     local sortedList = {}
  477.     for k, v in pairs(heights) do
  478.         if v >= 5 then
  479.             self:goToZ(v)
  480.             -- Walls
  481.             if self.walls[v] ~= nil then
  482.                 if self.pos.y > 8 then
  483.                     sortedList = self:sortList(self.walls[v], "max", {self.pos.x, self.pos.y})
  484.                 else
  485.                     sortedList = self:sortList(self.walls[v], "min", {self.pos.x, self.pos.y})
  486.                 end
  487.                 for p = 1, #sortedList do
  488.                     if sortedList[p][2] == 0 then
  489.                         self:goToY(1)
  490.                         wallDir = "n"
  491.                     elseif sortedList[p][2] == 17 then
  492.                         self:goToY(16)
  493.                         wallDir = "s"
  494.                     else
  495.                         self:goToY(sortedList[p][2])
  496.                     end
  497.                     if sortedList[p][1] == 0 then
  498.                         self:goToX(1)
  499.                         wallDir = "w"
  500.                     elseif sortedList[p][1] == 17 then
  501.                         self:goToX(16)
  502.                         wallDir = "e"
  503.                     else
  504.                         self:goToX(sortedList[p][1])
  505.                     end
  506.  
  507.                     self:turn(wallDir)
  508.                     local boolWallF, wallF = turtle.inspect()
  509.                     if
  510.                         boolWallF and wallF.metadata == 0 and
  511.                             (wallF.name == "minecraft:flowing_water" or wallF.name == "minecraft:water" or
  512.                                 wallF.name == "minecraft:flowing_lava" or
  513.                                 wallF.name == "minecraft:lava" or
  514.                                 wallF.name == "MineFactoryReloaded:sludge.still" or
  515.                                 wallF.name == "Railcraft:fluid.creosote")
  516.                      then
  517.                         self:selectBlock()
  518.                         turtle.place()
  519.                     end
  520.                 end
  521.             end
  522.             -- Fluids
  523.             if self.fluid[v] ~= nil and next(self.fluid[v]) ~= nil then
  524.                 -- Go up one
  525.                 self:goToZ(v + 1)
  526.                 -- Make floor
  527.  
  528.                 if self.pos.y > 8 then
  529.                     sortedList = self:sortList(self.fluid[v], "max", {self.pos.x, self.pos.y})
  530.                 else
  531.                     sortedList = self:sortList(self.fluid[v], "min", {self.pos.x, self.pos.y})
  532.                 end
  533.                 for i = 1, #sortedList do
  534.                     if
  535.                         sortedList[i][2] == 0 or sortedList[i][2] == 17 or sortedList[i][1] == 0 or
  536.                             sortedList[i][1] == 17
  537.                      then
  538.                         print("somehow I should be going to X:" .. sortedList[i][1] .. " Y:" .. sortedList[i][2])
  539.                         print("But I'm not.")
  540.                     else
  541.                         self:goToY(sortedList[i][2])
  542.                         self:goToX(sortedList[i][1])
  543.                         boolWallD, wallD = turtle.inspectDown()
  544.  
  545.                         if
  546.                             boolWallD and (wallD.metadata == 0 or wallD.metadata == 1) and
  547.                                 (wallD.name == "minecraft:flowing_water" or wallD.name == "minecraft:water" or
  548.                                     wallD.name == "minecraft:flowing_lava" or
  549.                                     wallD.name == "minecraft:lava" or
  550.                                     wallD.name == "MineFactoryReloaded:sludge.still" or
  551.                                     wallD.name == "Railcraft:fluid.creosote")
  552.                          then
  553.                             self:selectBlock()
  554.                             turtle.placeDown()
  555.                         end
  556.                     end
  557.                 end
  558.                 -- Remove floor
  559.                 turtle.select(2)
  560.                 for i = #sortedList, 1, -1 do
  561.                     if
  562.                         sortedList[i][2] == 0 or sortedList[i][2] == 17 or sortedList[i][1] == 0 or
  563.                             sortedList[i][1] == 17
  564.                      then
  565.                         print("somehow I should be going to X:" .. sortedList[i][1] .. " Y:" .. sortedList[i][2])
  566.                         print("But I'm not.")
  567.                     else
  568.                         self:goToY(sortedList[i][2])
  569.                         self:goToX(sortedList[i][1])
  570.                         turtle.digDown()
  571.                     end
  572.                 end
  573.             end
  574.             if self.walls[v] ~= nil then
  575.                 self.walls[v] = nil
  576.             end
  577.             self.fluid[v] = nil
  578.             self:saveFluidData()
  579.         end
  580.     end
  581.     if topOrNorm == "norm" or topOrNorm == "normal" then
  582.         self:goToZ(initialZ)
  583.         self:turn(initialDir)
  584.     end
  585.     print("Done with fluids?")
  586.     -- Remove fluids data
  587.     fs.delete("data/dig/removeFluids")
  588. end
  589.  
  590. function Dig:calculateBlocksNeeded()
  591.     local totalWalls = {}
  592.     local total = 0
  593.     local totalFluids = {}
  594.     local highestFluid = 0
  595.  
  596.     local highest = 0
  597.     -- Check fluids
  598.     for k, v in pairs(self.fluid) do
  599.         totalFluids[#totalFluids + 1] = #v
  600.     end
  601.     for y = 1, #totalFluids do
  602.         if totalFluids[y] > highestFluid then
  603.             highestFluid = totalFluids[y]
  604.         end
  605.     end
  606.  
  607.     for k, v in pairs(self.walls) do
  608.         total = total + #v
  609.     end
  610.     return total + highestFluid
  611. end
  612. function Dig:calculateBlocksInInventory()
  613.     local totalBlocks = 0
  614.     for q = 2, 16 do
  615.         local item_detail = turtle.getItemDetail(q)
  616.         if
  617.             item_detail and
  618.                 (string.find(item_detail.name:lower(), "cobblestone") or string.find(item_detail.name:lower(), "dirt") or
  619.                     string.find(item_detail.name:lower(), "sandstone"))
  620.          then
  621.             totalBlocks = totalBlocks + item_detail.count
  622.         end
  623.     end
  624.     return totalBlocks
  625. end
  626. function Dig:selectBlock()
  627.     local item_detail = turtle.getItemDetail()
  628.     if
  629.         item_detail and
  630.             (string.find(item_detail.name:lower(), "cobblestone") or string.find(item_detail.name:lower(), "dirt") or
  631.                 string.find(item_detail.name:lower(), "sandstone"))
  632.      then
  633.         return
  634.     else
  635.         for q = 2, 16 do
  636.             local item_detail = turtle.getItemDetail(q)
  637.             if
  638.                 item_detail and
  639.                     (string.find(item_detail.name:lower(), "cobblestone") or
  640.                         string.find(item_detail.name:lower(), "dirt") or
  641.                         string.find(item_detail.name:lower(), "sandstone"))
  642.              then
  643.                 turtle.select(q)
  644.                 return
  645.             else
  646.                 turtle.select(2)
  647.             end
  648.         end
  649.     end
  650. end
  651. function Dig:emptyInv()
  652.     local neededBlocks = self:calculateBlocksNeeded()
  653.     if turtle.getItemCount(16) > 0 then
  654.         while turtle.detectUp() do
  655.             turtle.digUp()
  656.         end
  657.         turtle.select(1)
  658.         turtle.placeUp()
  659.  
  660.         local success, t = turtle.inspectUp()
  661.         if success then
  662.             if string.find(t.name, "EnderStorage") == nil then
  663.                 error("No ender chest found?")
  664.             else
  665.                 while turtle.getItemCount(16) ~= 0 do
  666.                     for q = 2, 16 do
  667.                         if neededBlocks > 0 and self.pos.z < 30 then
  668.                             local item_detail = turtle.getItemDetail(q)
  669.                             if
  670.                                 item_detail and
  671.                                     (string.find(item_detail.name:lower(), "cobblestone") or
  672.                                         string.find(item_detail.name:lower(), "dirt") or
  673.                                         string.find(item_detail.name:lower(), "sandstone"))
  674.                              then
  675.                                 -- stel, needed is 63 & itemcount = 64. if itemcount - needed > 0 then drop(itemcount-needed)
  676.                                 if item_detail.count - neededBlocks > 0 then
  677.                                     turtle.select(q)
  678.                                     turtle.dropUp(item_detail.count - neededBlocks)
  679.                                     neededBlocks = 0
  680.                                 else
  681.                                     neededBlocks = neededBlocks - item_detail.count
  682.                                 end
  683.                             elseif item_detail and (string.find(item_detail.name:lower(), "computer")) then
  684.                                 turtle.select(q)
  685.                                 self:left()
  686.                                 self:left()
  687.                                 turtle.place()
  688.                                 self:right()
  689.                                 self:right()
  690.                             else
  691.                                 turtle.select(q)
  692.                                 turtle.dropUp()
  693.                             end
  694.                         else
  695.                             local item_detail = turtle.getItemDetail(q)
  696.                             if item_detail and (string.find(item_detail.name:lower(), "computer")) then
  697.                                 turtle.select(q)
  698.                                 self:left()
  699.                                 self:left()
  700.                                 turtle.place()
  701.                                 self:right()
  702.                                 self:right()
  703.                             else
  704.                                 turtle.select(q)
  705.                                 turtle.dropUp()
  706.                             end
  707.                         end
  708.                     end
  709.                 end
  710.                 turtle.select(1)
  711.                 turtle.digUp()
  712.             end
  713.         else
  714.             error("No ender chest found?")
  715.         end
  716.     end
  717. end
  718.  
  719. -- Sort functions
  720. function Dig:getFirst(inputList, min_max)
  721.     local min_max = min_max or "max"
  722.     local list = self:copy(inputList)
  723.     local some_x = nil
  724.     local some_y = nil
  725.  
  726.     if type(min_max) ~= "string" then
  727.         error("min_max should be string, currently is " .. type(min_max), 2)
  728.     elseif min_max ~= "max" and min_max ~= "min" then
  729.         error("Type is not max or min, currently is " .. min_max, 2)
  730.     elseif type(list) ~= "table" then
  731.         error("List is not a table, currently is " .. type(list), 2)
  732.     elseif #list == 0 then
  733.         error("List is empty", 2)
  734.     end
  735.     for i = 1, #list do
  736.         if some_x == nil then
  737.             some_x = list[i][1]
  738.         end
  739.         if (min_max == "max" and list[i][1] > some_x) or (min_max == "min" and list[i][1] < some_x) then
  740.             some_x = list[i][1]
  741.         end
  742.     end
  743.     for i = 1, #list do
  744.         if list[i][1] == some_x then
  745.             if some_y == nil then
  746.                 some_y = list[i][2]
  747.             end
  748.             if (min_max == "max" and list[i][2] > some_y) or (min_max == "min" and list[i][2] < some_y) then
  749.                 some_y = list[i][2]
  750.             end
  751.         end
  752.     end
  753.     return {some_x, some_y}
  754. end
  755. function Dig:sortList(list, min_max, start_point)
  756.     local min_max = min_max or "max"
  757.     local list = list
  758.     local copy_list = self:copy(list)
  759.     local highest = start_point or self:getFirst(copy_list, min_max)
  760.  
  761.     if type(min_max) ~= "string" then
  762.         error("min_max should be string, currently is " .. type(min_max), 2)
  763.     elseif min_max ~= "max" and min_max ~= "min" then
  764.         error("Type is not max or min, currently is " .. min_max, 2)
  765.     elseif type(list) ~= "table" then
  766.         error("List is not a table, currently is " .. type(list), 2)
  767.     elseif #list == 0 then
  768.         error("List is empty", 2)
  769.     elseif highest ~= nil and type(highest) ~= "table" then
  770.         error("highest is not nil and not a table, currently is " .. type(highest), 2)
  771.     elseif highest ~= nil and (type(highest[1]) ~= "number" or type(highest[2]) ~= "number") then
  772.         error("highest is not a x,y cord, currently is " .. highest[1] .. ", " .. highest[2], 2)
  773.     end
  774.  
  775.     local sorted_list = {}
  776.     local lengths = {}
  777.     while next(copy_list) ~= nil do
  778.         lengths = {}
  779.         for i = 1, #copy_list do
  780.             length = math.abs(highest[1] - copy_list[i][1]) + math.abs(highest[2] - copy_list[i][2])
  781.             lengths[i] = length
  782.         end
  783.         local key = next(lengths)
  784.         local min = lengths[key]
  785.         for k, v in pairs(lengths) do
  786.             if lengths[k] < min then
  787.                 key, min = k, v
  788.             end
  789.         end
  790.         highest = copy_list[key]
  791.         sorted_list[#sorted_list + 1] = highest
  792.         copy_list = self:removeItem(copy_list, highest)
  793.     end
  794.     return sorted_list
  795. end
  796. function Dig:removeItem(list, item)
  797.     local list = list
  798.     local newList = {}
  799.     if type(list) ~= "table" then
  800.         error("List is not a table, currently is " .. type(list), 2)
  801.     elseif #list == 0 then
  802.         error("List is empty", 2)
  803.     end
  804.     for i = 1, #list do
  805.         if list[i][1] ~= item[1] or list[i][2] ~= item[2] then
  806.             newList[#newList + 1] = list[i]
  807.         end
  808.     end
  809.     return newList
  810. end
  811.  
  812. function Dig:digForward(height)
  813.     height = height or 1
  814.     if type(height) ~= "number" then
  815.         error("Height is expected to be a number, currently is " .. type(height))
  816.     elseif height ~= 0 and height ~= 1 and height ~= 2 then
  817.         error("Height is expected to be 0, 1 or 2, currently is " .. height)
  818.     end
  819.  
  820.     if (height == 0) then
  821.         while self:forward() == false do
  822.             turtle.dig()
  823.         end
  824.     elseif (height == 1) then
  825.         while self:forward() == false do
  826.             turtle.dig()
  827.         end
  828.         turtle.digDown()
  829.     elseif (height == 2) then
  830.         while self:forward() == false do
  831.             turtle.dig()
  832.         end
  833.         turtle.digDown()
  834.     end
  835.     while turtle.detectUp() do
  836.         turtle.digUp()
  837.     end
  838.     self:detectFluid()
  839.     self:emptyInv()
  840. end
  841. function Dig:quarryChunk(vector, dir, startDir)
  842.     local vector = vector
  843.     local dir = dir
  844.     local startDir = startDir
  845.     if type(vector) ~= "table" then
  846.         error("quarryChunk expects a vector, currently is " .. type(vector))
  847.     elseif
  848.         type(dir) ~= "string" and dir:lower() ~= "n" and dir:lower() ~= "e" and dir:lower() ~= "s" and
  849.             dir:lower() ~= "w"
  850.      then
  851.         error("quarryChunk expects a dir (N,E,S,W), currently is " .. type(dir) .. ", " .. dir)
  852.     elseif
  853.         type(startDir) ~= "string" and startDir:lower() ~= "n" and startDir:lower() ~= "e" and startDir:lower() ~= "s" and
  854.             startDir:lower() ~= "w"
  855.      then
  856.         error("quarryChunk expects a startDir (N,E,S,W), currently is " .. type(startDir) .. ", " .. startDir)
  857.     end
  858.     startDir = startDir:lower()
  859.     dir = dir:lower()
  860.  
  861.     local maxX = 16
  862.     local maxY = 16
  863.     local y
  864.     local x
  865.     local digHeight = 2
  866.  
  867.     self:calculatePosition(vector, dir, startDir)
  868.     if fs.exists("data/dig/fluidData") and fs.exists("data/dig/wallsData") then
  869.         -- calculate what my position is using vector
  870.         local file = fs.open("data/dig/fluidData", "r")
  871.         local data = file.readAll()
  872.         file.close()
  873.         self.fluid = textutils.unserialize(data)
  874.         local file = fs.open("data/dig/wallsData", "r")
  875.         local data = file.readAll()
  876.         file.close()
  877.         self.walls = textutils.unserialize(data)
  878.     else
  879.         self:saveFluidData()
  880.     end
  881.  
  882.     if fs.exists("data/dig/removeFluids") then
  883.         self:removeFluid()
  884.     end
  885.     if not fs.exists("data/dig/done") then
  886.         while self.pos.z - 5 > 2 or not fs.exists("data/dig/done") do
  887.             if self.pos.z - 5 < 2 then
  888.                 digHeight = self.pos.z - 5
  889.             end
  890.             while self.pos.y ~= maxY or self.pos.x ~= 1 do
  891.                 if self.pos.y % 2 == 1 and self.dir ~= "e" then
  892.                     self:turn("e")
  893.                 elseif self.pos.y % 2 == 0 and self.dir ~= "w" then
  894.                     self:turn("w")
  895.                 end
  896.                 if self.pos.x == maxX and self.pos.y % 2 == 1 then
  897.                     self:detectFluid(false)
  898.                     self:right()
  899.                     self:detectFluid(false)
  900.                     if self.pos.y ~= maxY then
  901.                         self:digForward(digHeight)
  902.                     end
  903.                     self:right()
  904.                     self:detectFluid(false)
  905.                 elseif self.pos.x == 1 and self.pos.y % 2 == 0 then
  906.                     self:detectFluid(false)
  907.                     self:left()
  908.                     self:detectFluid(false)
  909.                     if self.pos.y ~= maxY then
  910.                         self:digForward(digHeight)
  911.                     end
  912.                     self:left()
  913.                     self:detectFluid(false)
  914.                 else
  915.                     if self.pos.x == 1 and self.pos.y == 1 then
  916.                         self:detectFluid()
  917.                     else
  918.                         self:detectFluid(false)
  919.                     end
  920.                     self:digForward(digHeight)
  921.                 end
  922.             end
  923.             if next(self.fluid) ~= nil then
  924.                 if self:calculateBlocksNeeded() <= self:calculateBlocksInInventory() then
  925.                     self:removeFluid()
  926.                 end
  927.             end
  928.  
  929.             if self.pos.z - 5 < 2 and self.pos.y == maxY and self.pos.x == 1 then
  930.                 local file = fs.open("data/dig/done", "w")
  931.                 file.writeLine("true")
  932.                 file.close()
  933.             end
  934.  
  935.             self:goToX(1)
  936.             self:goToY(1)
  937.             if self.pos.z ~= 5 then
  938.                 turtle.digDown()
  939.             end
  940.  
  941.             -- Go down one
  942.             if self.pos.z - 5 == 1 or self.pos.z - 5 == 2 then
  943.                 self:down(1)
  944.             elseif self.pos.z - 5 ~= 0 then
  945.                 self:down(3)
  946.             end
  947.         end
  948.         if next(self.fluid) ~= nil then
  949.             if self:calculateBlocksNeeded() <= self:calculateBlocksInInventory() then
  950.                 self:removeFluid("top")
  951.             end
  952.         end
  953.         self:goHome()
  954.     else
  955.         self:goHome()
  956.     end
  957. end
  958.  
  959. function Dig:goHome()
  960.     if self.pos.x ~= 1 then
  961.         self:goToX(1)
  962.     end
  963.     if self.pos.y ~= 1 then
  964.         self:goToY(1)
  965.     end
  966.     if self.pos.z ~= self.initialHeight then
  967.         self:goToZ(self.initialHeight)
  968.     end
  969.     self:turn("e")
  970.     if fs.exists("data/dig/removeFluids") then
  971.         fs.delete("data/dig/removeFluids")
  972.     end
  973.     if fs.exists("data/dig/fluidData") then
  974.         fs.delete("data/dig/fluidData")
  975.     end
  976.     if fs.exists("data/dig/wallsData") then
  977.         fs.delete("data/dig/wallsData")
  978.     end
  979.     if fs.exists("data/dig/done") then
  980.         fs.delete("data/dig/done")
  981.     end
  982. end
  983.  
  984. function Dig:calculatePosition(vector, dir, startDir)
  985.     local vector = vector
  986.     local dir = dir:lower()
  987.     local startDir = startDir:lower()
  988.     if type(vector) ~= "table" then
  989.         error("calculatePosition vector expects a vector, currently is " .. type(vector), 2)
  990.     elseif type(dir) ~= "string" then
  991.         error("calculatePosition dir expects a string, currently is " .. type(dir), 2)
  992.     elseif type(startDir) ~= "string" then
  993.         error("calculatePosition startDir expects a string, currently is " .. type(dir), 2)
  994.     end
  995.     x = (vector.x % 16)
  996.     y = (vector.y % 16)
  997.     -- Calculate mining position of my x and y cords vs world x and y
  998.     -- e = (3,2)    | x = x         & y = y
  999.     -- s = (14,3)   | x = 16 - y    & y = x
  1000.     -- w = (13,14)  | x = 16 - x    & y = 16 - y
  1001.     -- n = (2,13)   | x = y         & y = 16 - x
  1002.     if startDir == "s" then --0 degree (s = e)
  1003.         self.dir = dir
  1004.         self.pos.x = x + 1
  1005.         self.pos.y = y + 1
  1006.     elseif startDir == "w" then --90 degree
  1007.         if dir == "n" then
  1008.             self.dir = "w"
  1009.         elseif dir == "e" then
  1010.             self.dir = "n"
  1011.         elseif dir == "s" then
  1012.             self.dir = "e"
  1013.         elseif dir == "w" then
  1014.             self.dir = "s"
  1015.         end
  1016.         self.pos.x = y + 1
  1017.         self.pos.y = 16 - x
  1018.     elseif startDir == "n" then --180 degree
  1019.         if dir == "n" then
  1020.             self.dir = "s"
  1021.         elseif dir == "e" then
  1022.             self.dir = "w"
  1023.         elseif dir == "s" then
  1024.             self.dir = "n"
  1025.         elseif dir == "w" then
  1026.             self.dir = "e"
  1027.         end
  1028.         self.pos.x = 16 - x
  1029.         self.pos.y = 16 - y
  1030.     elseif startDir == "e" then --270 degree
  1031.         if dir == "n" then
  1032.             self.dir = "e"
  1033.         elseif dir == "e" then
  1034.             self.dir = "s"
  1035.         elseif dir == "s" then
  1036.             self.dir = "w"
  1037.         elseif dir == "w" then
  1038.             self.dir = "n"
  1039.         end
  1040.         self.pos.x = 16 - y
  1041.         self.pos.y = x + 1
  1042.     end
  1043.     self.pos.z = vector.z
  1044. end
  1045. function Dig:saveFluidData()
  1046.     local file = fs.open("data/dig/fluidData", "w")
  1047.     file.writeLine(textutils.serialize(self.fluid))
  1048.     file.close()
  1049.     local file = fs.open("data/dig/wallsData", "w")
  1050.     file.writeLine(textutils.serialize(self.walls))
  1051.     file.close()
  1052. end
  1053. function Dig:copy(obj)
  1054.     if type(obj) ~= "table" then
  1055.         return obj
  1056.     end
  1057.     local res = {}
  1058.     for k, v in pairs(obj) do
  1059.         res[self:copy(k)] = self:copy(v)
  1060.     end
  1061.     return res
  1062. end
Advertisement
Add Comment
Please, Sign In to add comment