Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dig = {
- pos = vector.new(0, 0, 0),
- dir = "e",
- fluid = {},
- walls = {},
- initialHeight = 85
- }
- function Dig:new(pos, dir, initialHeight)
- local pos = pos
- local dir = dir
- local initialHeight = initialHeight or 85
- if type(pos) ~= "table" then
- error("new pos expects to be a vector, currently is " .. type(pos))
- elseif type(dir) ~= "string" then
- error("new dir expects to be a string, currently is " .. type(dir))
- elseif dir:lower() ~= "n" and dir:lower() ~= "s" and dir:lower() ~= "w" and dir:lower() ~= "e" then
- error("new dir expects to be n, s, w or e, currently is " .. dir:lower())
- elseif type(initialHeight) ~= "number" then
- error("new initialHeight expects to be a number, currently is " .. type(initialHeight))
- elseif initialHeight > 256 or initialHeight < 6 then
- error("new initialHeight should be lower than 256 and higher than 6, currently is " .. initialHeight)
- end
- setmetatable({}, Dig)
- self.pos = pos
- self.dir = dir:lower()
- self.fluid = {}
- self.initialHeight = initialHeight
- return self
- end
- -- basic movement
- function Dig:forward()
- if turtle.forward() then
- if self.dir == "n" then
- self.pos.y = self.pos.y - 1
- elseif self.dir == "s" then
- self.pos.y = self.pos.y + 1
- elseif self.dir == "e" then
- self.pos.x = self.pos.x + 1
- elseif self.dir == "w" then
- self.pos.x = self.pos.x - 1
- end
- return true
- else
- turtle.dig()
- turtle.attack()
- return false
- end
- end
- function Dig:back(times)
- local times = times or 1
- if (type(times) ~= "number") then
- error("back expects a number, currently is " .. type(times))
- end
- for i = 1, times do
- while not turtle.back() do
- self:left()
- self:left()
- turtle.dig()
- turtle.attack()
- self:left()
- self:left()
- end
- if self.dir == "n" then
- self.pos.y = self.pos.y + 1
- elseif self.dir == "s" then
- self.pos.y = self.pos.y - 1
- elseif self.dir == "e" then
- self.pos.x = self.pos.x - 1
- elseif self.dir == "w" then
- self.pos.x = self.pos.x + 1
- end
- end
- return true
- end
- function Dig:up(times)
- local times = times or 1
- if (type(times) ~= "number") then
- error("up expects a number, currently is " .. type(times))
- end
- for i = 1, times do
- while not turtle.up() do
- turtle.attackUp()
- turtle.digUp()
- end
- self.pos.z = self.pos.z + 1
- end
- return true
- end
- function Dig:down(times)
- local times = times or 1
- if (type(times) ~= "number") then
- error("down expects a number, currently is " .. type(times))
- end
- for i = 1, times do
- while not turtle.down() do
- turtle.attackDown()
- turtle.digDown()
- end
- self.pos.z = self.pos.z - 1
- end
- return true
- end
- function Dig:left(times)
- local times = times or 1
- if (type(times) ~= "number") then
- error("left expects a number, currently is " .. type(times))
- end
- for i = 1, times do
- if turtle.turnLeft() then
- if self.dir == "n" then
- self.dir = "w"
- elseif self.dir == "w" then
- self.dir = "s"
- elseif self.dir == "s" then
- self.dir = "e"
- elseif self.dir == "e" then
- self.dir = "n"
- end
- end
- end
- end
- function Dig:right(times)
- local times = times or 1
- if (type(times) ~= "number") then
- error("right expects a number, currently is " .. type(times))
- end
- for i = 1, times do
- if turtle.turnRight() then
- if self.dir == "n" then
- self.dir = "e"
- elseif self.dir == "e" then
- self.dir = "s"
- elseif self.dir == "s" then
- self.dir = "w"
- elseif self.dir == "w" then
- self.dir = "n"
- end
- end
- end
- end
- function Dig:turn(direction)
- if type(direction) ~= "string" then
- error("turn expects a string, currently is " .. type(direction), 2)
- elseif
- direction:lower() ~= "e" and direction:lower() ~= "s" and direction:lower() ~= "w" and direction:lower() ~= "n"
- then
- error("turn expects a dir (N,E,S,W), currently is " .. type(direction) .. ", " .. direction, 2)
- end
- local direction = direction:lower()
- local degree = 0
- local selfDegree = 0
- local dif
- if direction == "s" then
- degree = 90
- elseif direction == "w" then
- degree = 180
- elseif direction == "n" then
- degree = 270
- end
- if self.dir == "s" then
- selfDegree = 90
- elseif self.dir == "w" then
- selfDegree = 180
- elseif self.dir == "n" then
- selfDegree = 270
- end
- dif = degree - selfDegree
- if dif < 0 then
- dif = dif + 360
- end
- if dif > 180 then
- while self.dir ~= direction do
- self:left()
- end
- else
- while self.dir ~= direction do
- self:right()
- end
- end
- end
- function Dig:goToX(position)
- local vector = vector.new(0, 0, 0)
- if type(position) == "number" then
- vector.x = position
- elseif type(vector) ~= "table" and type(position) ~= "number" then
- error("goToX expects a vector or number, currently is " .. type(vector))
- else
- vector = position
- end
- if self.pos.x ~= vector.x then
- if self.pos.x > vector.x then
- self:turn("w")
- elseif self.pos.x < vector.x then
- self:turn("e")
- end
- while self.pos.x ~= vector.x do
- self:forward()
- end
- end
- end
- function Dig:goToY(position)
- local vector = vector.new(0, 0, 0)
- if type(position) == "number" then
- vector.y = position
- elseif type(vector) ~= "table" and type(position) ~= "number" then
- error("goToY expects a vector or number, currently is " .. type(vector))
- else
- vector = position
- end
- if self.pos.y ~= vector.y then
- if self.pos.y < vector.y then
- self:turn("s")
- elseif self.pos.y > vector.y then
- self:turn("n")
- end
- while self.pos.y ~= vector.y do
- self:forward()
- end
- end
- end
- function Dig:goToZ(position)
- local vector = vector.new(0, 0, 0)
- if type(position) == "number" then
- vector.z = position
- elseif type(vector) ~= "table" and type(vector) ~= "number" then
- error("goToZ expects a vector or number, currently is " .. type(vector))
- else
- vector = position
- end
- if self.pos.z ~= vector.z then
- if self.pos.z > vector.z then
- while self.pos.z ~= vector.z do
- self:down()
- end
- elseif self.pos.z < vector.z then
- while self.pos.z ~= vector.z do
- self:up()
- end
- end
- end
- end
- function Dig:detectFluid(check)
- local save = false
- local saveWall = true
- if check == false then
- saveWall = false
- end
- if saveWall ~= false and saveWall ~= true then
- error("Dig detectFluid check should be a bool, currently is " .. saveWall, 2)
- end
- local boolU, up = turtle.inspectUp()
- local boolD, down = turtle.inspectDown()
- local boolF, forward = turtle.inspect()
- local waterU, waterD, waterF, lavaU, lavaD, lavaF = false, false, false, false, false, false
- local doubleCheck = false
- -- Forward
- if
- boolF and forward.metadata == 0 and
- (forward.name == "minecraft:flowing_water" or boolF and forward.name == "minecraft:water")
- then
- waterF = true
- elseif
- boolF and (forward.metadata == 0 or forward.metadata == 1) and
- (forward.name == "minecraft:flowing_lava" or forward.name == "minecraft:lava" or
- forward.name == "MineFactoryReloaded:sludge.still" or
- forward.name == "Railcraft:fluid.creosote")
- then
- lavaF = true
- end
- -- Up
- if boolU and up.metadata == 0 and (up.name == "minecraft:flowing_water" or boolU and up.name == "minecraft:water") then
- waterU = true
- elseif
- boolU and (up.metadata == 0 or up.metadata == 1) and
- (up.name == "minecraft:flowing_lava" or up.name == "minecraft:lava" or
- up.name == "MineFactoryReloaded:sludge.still" or
- up.name == "Railcraft:fluid.creosote")
- then
- lavaU = true
- end
- -- Down
- if boolD and down.metadata == 0 and (down.name == "minecraft:flowing_water" or down.name == "minecraft:water") then
- waterD = true
- elseif
- boolD and (down.metadata == 0 or down.metadata == 1) and
- (down.name == "minecraft:flowing_lava" or down.name == "minecraft:lava" or
- down.name == "MineFactoryReloaded:sludge.still" or
- down.name == "Railcraft:fluid.creosote")
- then
- lavaD = true
- end
- local fakePos = self:copy(self.pos)
- if self.dir == "n" then
- fakePos.y = fakePos.y - 1
- elseif self.dir == "s" then
- fakePos.y = fakePos.y + 1
- elseif self.dir == "e" then
- fakePos.x = fakePos.x + 1
- elseif self.dir == "w" then
- fakePos.x = fakePos.x - 1
- end
- -- Save positions
- if waterF then
- if self.fluid[self.pos.z] == nil then
- self.fluid[self.pos.z] = {}
- end
- if
- self:dataExists(fakePos.x, fakePos.y, fakePos.z) and fakePos.x ~= 0 and fakePos.x ~= 17 and fakePos.y ~= 0 and
- fakePos.y ~= 17
- then
- self.fluid[self.pos.z][#self.fluid[self.pos.z] + 1] = {fakePos.x, fakePos.y}
- end
- save = true
- end
- if saveWall and (waterF or lavaF) then
- if
- (self.pos.x == 1 or self.pos.x == 16 or self.pos.y == 1 or self.pos.y == 16) and
- self.walls[self.pos.z] == nil
- then
- self.walls[self.pos.z] = {}
- end
- if self.pos.x == 1 and self:dataExists(0, self.pos.y, self.pos.z, "walls") then
- self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {0, self.pos.y}
- elseif self.pos.x == 16 and self:dataExists(17, self.pos.y, self.pos.z, "walls") then
- self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {17, self.pos.y}
- end
- if self.pos.y == 1 and self:dataExists(self.pos.x, 0, self.pos.z, "walls") then
- self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {self.pos.x, 0}
- elseif self.pos.y == 16 and self:dataExists(self.pos.x, 17, self.pos.z, "walls") then
- self.walls[self.pos.z][#self.walls[self.pos.z] + 1] = {self.pos.x, 17}
- end
- end
- -- up
- if waterU then
- if self.fluid[self.pos.z + 1] == nil then
- self.fluid[self.pos.z + 1] = {}
- end
- if self:dataExists(self.pos.x, self.pos.y, self.pos.z + 1) then
- self.fluid[self.pos.z + 1][#self.fluid[self.pos.z + 1] + 1] = {self.pos.x, self.pos.y}
- end
- save = true
- elseif lavaU then
- self:up()
- self:down()
- end
- if saveWall and (waterU or lavaU) then
- if
- (self.pos.x == 1 or self.pos.x == 16 or self.pos.y == 1 or self.pos.y == 16) and
- self.walls[self.pos.z + 1] == nil
- then
- self.walls[self.pos.z + 1] = {}
- end
- if self.pos.x == 1 and self:dataExists(0, self.pos.y, self.pos.z + 1, "walls") then
- self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {0, self.pos.y}
- elseif self.pos.x == 16 and self:dataExists(17, self.pos.y, self.pos.z + 1, "walls") then
- self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {17, self.pos.y}
- end
- if self.pos.y == 1 and self:dataExists(self.pos.x, 0, self.pos.z + 1, "walls") then
- self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {self.pos.x, 0}
- elseif self.pos.y == 16 and self:dataExists(self.pos.x, 17, self.pos.z + 1, "walls") then
- self.walls[self.pos.z + 1][#self.walls[self.pos.z + 1] + 1] = {self.pos.x, 17}
- end
- end
- -- Down
- if waterD then
- if self.fluid[self.pos.z - 1] == nil then
- self.fluid[self.pos.z - 1] = {}
- end
- if self:dataExists(self.pos.x, self.pos.y, self.pos.z - 1) then
- self.fluid[self.pos.z - 1][#self.fluid[self.pos.z - 1] + 1] = {self.pos.x, self.pos.y}
- end
- save = true
- elseif lavaU then
- self:up()
- self:down()
- end
- if saveWall and (waterD or lavaD) then
- if
- (self.pos.x == 1 or self.pos.x == 16 or self.pos.y == 1 or self.pos.y == 16) and
- self.walls[self.pos.z - 1] == nil
- then
- self.walls[self.pos.z - 1] = {}
- end
- if self.pos.x == 1 and self:dataExists(0, self.pos.y, self.pos.z - 1, "walls") then
- self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {0, self.pos.y}
- elseif self.pos.x == 16 and self:dataExists(17, self.pos.y, self.pos.z - 1, "walls") then
- self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {17, self.pos.y}
- end
- if self.pos.y == 1 and self:dataExists(self.pos.x, 0, self.pos.z - 1, "walls") then
- self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {self.pos.x, 0}
- elseif self.pos.y == 16 and self:dataExists(self.pos.x, 17, self.pos.z - 1, "walls") then
- self.walls[self.pos.z - 1][#self.walls[self.pos.z - 1] + 1] = {self.pos.x, 17}
- end
- end
- -- Save
- if save then
- self:saveFluidData()
- end
- end
- function Dig:dataExists(x, y, z, type)
- local type = type or "fluid"
- if type == "fluid" then
- if x == 0 or x == 17 or y == 0 or x == 17 then
- return false
- end
- if self.fluid[z] then
- for k, v in pairs(self.fluid[z]) do
- if v[1] == x and v[2] == y then
- return false
- end
- end
- end
- elseif type == "walls" then
- if self.walls[z] then
- for k, v in pairs(self.walls[z]) do
- if v[1] == x and v[2] == y then
- return false
- end
- end
- end
- end
- return true
- end
- function Dig:removeFluid()
- print("remove fluid")
- local initialZ = tonumber(self.pos.z)
- local initialDir = self.dir
- local heights = {}
- local wallHeights = {}
- local ignoreList = {}
- local boolWallD, wallD
- if not fs.exists("data/dig/removeFluids") then
- local file = fs.open("data/dig/removeFluids", "w")
- file.writeLine(initialZ)
- file.writeLine(self.dir)
- file.close()
- else
- local file = fs.open("data/dig/removeFluids", "r")
- initialZ = tonumber(file.readLine())
- initialDir = file.readLine()
- file.close()
- end
- for k, v in pairs(self.fluid) do
- heights[#heights + 1] = k
- end
- -- Reverse list for shortest path
- table.sort(
- heights,
- function(a, b)
- return a > b
- end
- )
- table.sort(
- wallHeights,
- function(a, b)
- return a > b
- end
- )
- for i = 1, #wallHeights do
- heights[#heights + 1] = wallHeights[i]
- end
- local sortedList = {}
- for k, v in pairs(heights) do
- if v >= 5 then
- self:goToZ(v)
- -- Walls
- if self.walls[v] ~= nil then
- if self.pos.y > 8 then
- sortedList = self:sortList(self.walls[v], "max", {self.pos.x, self.pos.y})
- else
- sortedList = self:sortList(self.walls[v], "min", {self.pos.x, self.pos.y})
- end
- for p = 1, #sortedList do
- if sortedList[p][2] == 0 then
- self:goToY(1)
- wallDir = "n"
- elseif sortedList[p][2] == 17 then
- self:goToY(16)
- wallDir = "s"
- else
- self:goToY(sortedList[p][2])
- end
- if sortedList[p][1] == 0 then
- self:goToX(1)
- wallDir = "w"
- elseif sortedList[p][1] == 17 then
- self:goToX(16)
- wallDir = "e"
- else
- self:goToX(sortedList[p][1])
- end
- self:turn(wallDir)
- local boolWallF, wallF = turtle.inspect()
- if
- boolWallF and wallF.metadata == 0 and
- (wallF.name == "minecraft:flowing_water" or wallF.name == "minecraft:water" or
- wallF.name == "minecraft:flowing_lava" or
- wallF.name == "minecraft:lava" or
- wallF.name == "MineFactoryReloaded:sludge.still" or
- wallF.name == "Railcraft:fluid.creosote")
- then
- self:selectBlock()
- turtle.place()
- end
- end
- end
- -- Fluids
- if self.fluid[v] ~= nil and next(self.fluid[v]) ~= nil then
- -- Go up one
- self:goToZ(v + 1)
- -- Make floor
- if self.pos.y > 8 then
- sortedList = self:sortList(self.fluid[v], "max", {self.pos.x, self.pos.y})
- else
- sortedList = self:sortList(self.fluid[v], "min", {self.pos.x, self.pos.y})
- end
- for i = 1, #sortedList do
- if
- sortedList[i][2] == 0 or sortedList[i][2] == 17 or sortedList[i][1] == 0 or
- sortedList[i][1] == 17
- then
- print("somehow I should be going to X:" .. sortedList[i][1] .. " Y:" .. sortedList[i][2])
- print("But I'm not.")
- else
- self:goToY(sortedList[i][2])
- self:goToX(sortedList[i][1])
- boolWallD, wallD = turtle.inspectDown()
- if
- boolWallD and (wallD.metadata == 0 or wallD.metadata == 1) and
- (wallD.name == "minecraft:flowing_water" or wallD.name == "minecraft:water" or
- wallD.name == "minecraft:flowing_lava" or
- wallD.name == "minecraft:lava" or
- wallD.name == "MineFactoryReloaded:sludge.still" or
- wallD.name == "Railcraft:fluid.creosote")
- then
- self:selectBlock()
- turtle.placeDown()
- end
- end
- end
- -- Remove floor
- turtle.select(2)
- for i = #sortedList, 1, -1 do
- if
- sortedList[i][2] == 0 or sortedList[i][2] == 17 or sortedList[i][1] == 0 or
- sortedList[i][1] == 17
- then
- print("somehow I should be going to X:" .. sortedList[i][1] .. " Y:" .. sortedList[i][2])
- print("But I'm not.")
- else
- self:goToY(sortedList[i][2])
- self:goToX(sortedList[i][1])
- turtle.digDown()
- end
- end
- end
- if self.walls[v] ~= nil then
- self.walls[v] = nil
- end
- self.fluid[v] = nil
- self:saveFluidData()
- end
- end
- if topOrNorm == "norm" or topOrNorm == "normal" then
- self:goToZ(initialZ)
- self:turn(initialDir)
- end
- print("Done with fluids?")
- -- Remove fluids data
- fs.delete("data/dig/removeFluids")
- end
- function Dig:calculateBlocksNeeded()
- local totalWalls = {}
- local total = 0
- local totalFluids = {}
- local highestFluid = 0
- local highest = 0
- -- Check fluids
- for k, v in pairs(self.fluid) do
- totalFluids[#totalFluids + 1] = #v
- end
- for y = 1, #totalFluids do
- if totalFluids[y] > highestFluid then
- highestFluid = totalFluids[y]
- end
- end
- for k, v in pairs(self.walls) do
- total = total + #v
- end
- return total + highestFluid
- end
- function Dig:calculateBlocksInInventory()
- local totalBlocks = 0
- for q = 2, 16 do
- local item_detail = turtle.getItemDetail(q)
- if
- item_detail and
- (string.find(item_detail.name:lower(), "cobblestone") or string.find(item_detail.name:lower(), "dirt") or
- string.find(item_detail.name:lower(), "sandstone"))
- then
- totalBlocks = totalBlocks + item_detail.count
- end
- end
- return totalBlocks
- end
- function Dig:selectBlock()
- local item_detail = turtle.getItemDetail()
- if
- item_detail and
- (string.find(item_detail.name:lower(), "cobblestone") or string.find(item_detail.name:lower(), "dirt") or
- string.find(item_detail.name:lower(), "sandstone"))
- then
- return
- else
- for q = 2, 16 do
- local item_detail = turtle.getItemDetail(q)
- if
- item_detail and
- (string.find(item_detail.name:lower(), "cobblestone") or
- string.find(item_detail.name:lower(), "dirt") or
- string.find(item_detail.name:lower(), "sandstone"))
- then
- turtle.select(q)
- return
- else
- turtle.select(2)
- end
- end
- end
- end
- function Dig:emptyInv()
- local neededBlocks = self:calculateBlocksNeeded()
- if turtle.getItemCount(16) > 0 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- turtle.select(1)
- turtle.placeUp()
- local success, t = turtle.inspectUp()
- if success then
- if string.find(t.name, "EnderStorage") == nil then
- error("No ender chest found?")
- else
- while turtle.getItemCount(16) ~= 0 do
- for q = 2, 16 do
- if neededBlocks > 0 and self.pos.z < 30 then
- local item_detail = turtle.getItemDetail(q)
- if
- item_detail and
- (string.find(item_detail.name:lower(), "cobblestone") or
- string.find(item_detail.name:lower(), "dirt") or
- string.find(item_detail.name:lower(), "sandstone"))
- then
- -- stel, needed is 63 & itemcount = 64. if itemcount - needed > 0 then drop(itemcount-needed)
- if item_detail.count - neededBlocks > 0 then
- turtle.select(q)
- turtle.dropUp(item_detail.count - neededBlocks)
- neededBlocks = 0
- else
- neededBlocks = neededBlocks - item_detail.count
- end
- elseif item_detail and (string.find(item_detail.name:lower(), "computer")) then
- turtle.select(q)
- self:left()
- self:left()
- turtle.place()
- self:right()
- self:right()
- else
- turtle.select(q)
- turtle.dropUp()
- end
- else
- local item_detail = turtle.getItemDetail(q)
- if item_detail and (string.find(item_detail.name:lower(), "computer")) then
- turtle.select(q)
- self:left()
- self:left()
- turtle.place()
- self:right()
- self:right()
- else
- turtle.select(q)
- turtle.dropUp()
- end
- end
- end
- end
- turtle.select(1)
- turtle.digUp()
- end
- else
- error("No ender chest found?")
- end
- end
- end
- -- Sort functions
- function Dig:getFirst(inputList, min_max)
- local min_max = min_max or "max"
- local list = self:copy(inputList)
- local some_x = nil
- local some_y = nil
- if type(min_max) ~= "string" then
- error("min_max should be string, currently is " .. type(min_max), 2)
- elseif min_max ~= "max" and min_max ~= "min" then
- error("Type is not max or min, currently is " .. min_max, 2)
- elseif type(list) ~= "table" then
- error("List is not a table, currently is " .. type(list), 2)
- elseif #list == 0 then
- error("List is empty", 2)
- end
- for i = 1, #list do
- if some_x == nil then
- some_x = list[i][1]
- end
- if (min_max == "max" and list[i][1] > some_x) or (min_max == "min" and list[i][1] < some_x) then
- some_x = list[i][1]
- end
- end
- for i = 1, #list do
- if list[i][1] == some_x then
- if some_y == nil then
- some_y = list[i][2]
- end
- if (min_max == "max" and list[i][2] > some_y) or (min_max == "min" and list[i][2] < some_y) then
- some_y = list[i][2]
- end
- end
- end
- return {some_x, some_y}
- end
- function Dig:sortList(list, min_max, start_point)
- local min_max = min_max or "max"
- local list = list
- local copy_list = self:copy(list)
- local highest = start_point or self:getFirst(copy_list, min_max)
- if type(min_max) ~= "string" then
- error("min_max should be string, currently is " .. type(min_max), 2)
- elseif min_max ~= "max" and min_max ~= "min" then
- error("Type is not max or min, currently is " .. min_max, 2)
- elseif type(list) ~= "table" then
- error("List is not a table, currently is " .. type(list), 2)
- elseif #list == 0 then
- error("List is empty", 2)
- elseif highest ~= nil and type(highest) ~= "table" then
- error("highest is not nil and not a table, currently is " .. type(highest), 2)
- elseif highest ~= nil and (type(highest[1]) ~= "number" or type(highest[2]) ~= "number") then
- error("highest is not a x,y cord, currently is " .. highest[1] .. ", " .. highest[2], 2)
- end
- local sorted_list = {}
- local lengths = {}
- while next(copy_list) ~= nil do
- lengths = {}
- for i = 1, #copy_list do
- length = math.abs(highest[1] - copy_list[i][1]) + math.abs(highest[2] - copy_list[i][2])
- lengths[i] = length
- end
- local key = next(lengths)
- local min = lengths[key]
- for k, v in pairs(lengths) do
- if lengths[k] < min then
- key, min = k, v
- end
- end
- highest = copy_list[key]
- sorted_list[#sorted_list + 1] = highest
- copy_list = self:removeItem(copy_list, highest)
- end
- return sorted_list
- end
- function Dig:removeItem(list, item)
- local list = list
- local newList = {}
- if type(list) ~= "table" then
- error("List is not a table, currently is " .. type(list), 2)
- elseif #list == 0 then
- error("List is empty", 2)
- end
- for i = 1, #list do
- if list[i][1] ~= item[1] or list[i][2] ~= item[2] then
- newList[#newList + 1] = list[i]
- end
- end
- return newList
- end
- function Dig:digForward(height)
- height = height or 1
- if type(height) ~= "number" then
- error("Height is expected to be a number, currently is " .. type(height))
- elseif height ~= 0 and height ~= 1 and height ~= 2 then
- error("Height is expected to be 0, 1 or 2, currently is " .. height)
- end
- if (height == 0) then
- while self:forward() == false do
- turtle.dig()
- end
- elseif (height == 1) then
- while self:forward() == false do
- turtle.dig()
- end
- turtle.digDown()
- elseif (height == 2) then
- while self:forward() == false do
- turtle.dig()
- end
- turtle.digDown()
- end
- while turtle.detectUp() do
- turtle.digUp()
- end
- self:detectFluid()
- self:emptyInv()
- end
- function Dig:quarryChunk(vector, dir, startDir)
- local vector = vector
- local dir = dir
- local startDir = startDir
- if type(vector) ~= "table" then
- error("quarryChunk expects a vector, currently is " .. type(vector))
- elseif
- type(dir) ~= "string" and dir:lower() ~= "n" and dir:lower() ~= "e" and dir:lower() ~= "s" and
- dir:lower() ~= "w"
- then
- error("quarryChunk expects a dir (N,E,S,W), currently is " .. type(dir) .. ", " .. dir)
- elseif
- type(startDir) ~= "string" and startDir:lower() ~= "n" and startDir:lower() ~= "e" and startDir:lower() ~= "s" and
- startDir:lower() ~= "w"
- then
- error("quarryChunk expects a startDir (N,E,S,W), currently is " .. type(startDir) .. ", " .. startDir)
- end
- startDir = startDir:lower()
- dir = dir:lower()
- local maxX = 16
- local maxY = 16
- local y
- local x
- local digHeight = 2
- self:calculatePosition(vector, dir, startDir)
- if fs.exists("data/dig/fluidData") and fs.exists("data/dig/wallsData") then
- -- calculate what my position is using vector
- local file = fs.open("data/dig/fluidData", "r")
- local data = file.readAll()
- file.close()
- self.fluid = textutils.unserialize(data)
- local file = fs.open("data/dig/wallsData", "r")
- local data = file.readAll()
- file.close()
- self.walls = textutils.unserialize(data)
- else
- self:saveFluidData()
- end
- if fs.exists("data/dig/removeFluids") then
- self:removeFluid()
- end
- if not fs.exists("data/dig/done") then
- while self.pos.z - 5 > 2 or not fs.exists("data/dig/done") do
- if self.pos.z - 5 < 2 then
- digHeight = self.pos.z - 5
- end
- while self.pos.y ~= maxY or self.pos.x ~= 1 do
- if self.pos.y % 2 == 1 and self.dir ~= "e" then
- self:turn("e")
- elseif self.pos.y % 2 == 0 and self.dir ~= "w" then
- self:turn("w")
- end
- if self.pos.x == maxX and self.pos.y % 2 == 1 then
- self:detectFluid(false)
- self:right()
- self:detectFluid(false)
- if self.pos.y ~= maxY then
- self:digForward(digHeight)
- end
- self:right()
- self:detectFluid(false)
- elseif self.pos.x == 1 and self.pos.y % 2 == 0 then
- self:detectFluid(false)
- self:left()
- self:detectFluid(false)
- if self.pos.y ~= maxY then
- self:digForward(digHeight)
- end
- self:left()
- self:detectFluid(false)
- else
- if self.pos.x == 1 and self.pos.y == 1 then
- self:detectFluid()
- else
- self:detectFluid(false)
- end
- self:digForward(digHeight)
- end
- end
- if next(self.fluid) ~= nil then
- if self:calculateBlocksNeeded() <= self:calculateBlocksInInventory() then
- self:removeFluid()
- end
- end
- if self.pos.z - 5 < 2 and self.pos.y == maxY and self.pos.x == 1 then
- local file = fs.open("data/dig/done", "w")
- file.writeLine("true")
- file.close()
- end
- self:goToX(1)
- self:goToY(1)
- if self.pos.z ~= 5 then
- turtle.digDown()
- end
- -- Go down one
- if self.pos.z - 5 == 1 or self.pos.z - 5 == 2 then
- self:down(1)
- elseif self.pos.z - 5 ~= 0 then
- self:down(3)
- end
- end
- if next(self.fluid) ~= nil then
- if self:calculateBlocksNeeded() <= self:calculateBlocksInInventory() then
- self:removeFluid("top")
- end
- end
- self:goHome()
- else
- self:goHome()
- end
- end
- function Dig:goHome()
- if self.pos.x ~= 1 then
- self:goToX(1)
- end
- if self.pos.y ~= 1 then
- self:goToY(1)
- end
- if self.pos.z ~= self.initialHeight then
- self:goToZ(self.initialHeight)
- end
- self:turn("e")
- if fs.exists("data/dig/removeFluids") then
- fs.delete("data/dig/removeFluids")
- end
- if fs.exists("data/dig/fluidData") then
- fs.delete("data/dig/fluidData")
- end
- if fs.exists("data/dig/wallsData") then
- fs.delete("data/dig/wallsData")
- end
- if fs.exists("data/dig/done") then
- fs.delete("data/dig/done")
- end
- end
- function Dig:calculatePosition(vector, dir, startDir)
- local vector = vector
- local dir = dir:lower()
- local startDir = startDir:lower()
- if type(vector) ~= "table" then
- error("calculatePosition vector expects a vector, currently is " .. type(vector), 2)
- elseif type(dir) ~= "string" then
- error("calculatePosition dir expects a string, currently is " .. type(dir), 2)
- elseif type(startDir) ~= "string" then
- error("calculatePosition startDir expects a string, currently is " .. type(dir), 2)
- end
- x = (vector.x % 16)
- y = (vector.y % 16)
- -- Calculate mining position of my x and y cords vs world x and y
- -- e = (3,2) | x = x & y = y
- -- s = (14,3) | x = 16 - y & y = x
- -- w = (13,14) | x = 16 - x & y = 16 - y
- -- n = (2,13) | x = y & y = 16 - x
- if startDir == "s" then --0 degree (s = e)
- self.dir = dir
- self.pos.x = x + 1
- self.pos.y = y + 1
- elseif startDir == "w" then --90 degree
- if dir == "n" then
- self.dir = "w"
- elseif dir == "e" then
- self.dir = "n"
- elseif dir == "s" then
- self.dir = "e"
- elseif dir == "w" then
- self.dir = "s"
- end
- self.pos.x = y + 1
- self.pos.y = 16 - x
- elseif startDir == "n" then --180 degree
- if dir == "n" then
- self.dir = "s"
- elseif dir == "e" then
- self.dir = "w"
- elseif dir == "s" then
- self.dir = "n"
- elseif dir == "w" then
- self.dir = "e"
- end
- self.pos.x = 16 - x
- self.pos.y = 16 - y
- elseif startDir == "e" then --270 degree
- if dir == "n" then
- self.dir = "e"
- elseif dir == "e" then
- self.dir = "s"
- elseif dir == "s" then
- self.dir = "w"
- elseif dir == "w" then
- self.dir = "n"
- end
- self.pos.x = 16 - y
- self.pos.y = x + 1
- end
- self.pos.z = vector.z
- end
- function Dig:saveFluidData()
- local file = fs.open("data/dig/fluidData", "w")
- file.writeLine(textutils.serialize(self.fluid))
- file.close()
- local file = fs.open("data/dig/wallsData", "w")
- file.writeLine(textutils.serialize(self.walls))
- file.close()
- end
- function Dig:copy(obj)
- if type(obj) ~= "table" then
- return obj
- end
- local res = {}
- for k, v in pairs(obj) do
- res[self:copy(k)] = self:copy(v)
- end
- return res
- end
Advertisement
Add Comment
Please, Sign In to add comment