Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function showUsage()
- print("chunkDig4")
- print("slot1 : chest x 64")
- print("slot2 : stone x 1")
- print("slot3 : gravel x 1")
- print("slot4 : dart x 1")
- print("slot5 : cobblestone x 32")
- end
- local SLOT_CHEST = 1
- local SLOT_STONE = 2
- local SLOT_GRAVEL = 3
- local SLOT_DART = 4
- local SLOT_COBBLESTONE = 5
- local SLOTS_FREE = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
- local SLOTS_KEEP_ALL = { SLOT_CHEST }
- local SLOTS_KEEP_32 = { SLOT_COBBLESTONE }
- local SLOTS_KEEP_1 = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
- local SLOTS_USELESS_SAMPLE = { SLOT_COBBLESTONE, SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
- -- -------------------------------
- -- config
- -- -------------------------------
- local DEFAULT_WIDTH = 16
- local DEFAULT_DEPTH = 16 -- forward() direction
- local PREVENTION_FROM_FALL = false
- local EXCAVATE_ALL = false
- local KEEP_USELESS_BLOCKS = false
- local SHOW_USAGE = true
- --
- local ASK_INITIAL_HEIGHT = true
- local TURTLE_INITIAL_Y = 71
- -- HEIGHT_MODE
- -- true : use BEADROCK_HEIGHT
- -- false : use RELATIVE_HEIGHT
- local HEIGHT_MODE = true
- local BEADROCK_HEIGHT = 5
- local RELATIVE_HEIGHT = 3
- -- repository map
- local USE_REPOSITORY_MAP = true
- local PLACE_SUPPLEMENT_CHEST = true
- local REPOSITORY_MAP = {
- [3] = " 4 4 4 4 4 4",
- [2] = " 4 4 4 4 4 4",
- [1] = "F C "} -- 1-9:ore chest / 0:ore chest(no limit) / F:fuel chest / C:chest chest
- local REPOSITORY_OFFSET_LR = 0 -- (width direction)
- local REPOSITORY_OFFSET_BF = 0 -- (depth direction)
- -- refuel
- local FUEL_LEVEL_MINIMUM = 1000
- local FUEL_LEVEL_REFUEL = 5000
- -- for debug
- local DEBUG_LOG = false
- local SIMULATION = false
- -- -------------------------------
- -- utility
- -- -------------------------------
- -- local log
- local LOCAL_ENABLED = false
- local LOCAL_LOG_FILE_NAME = "debug.log"
- -- remote log
- local REMOTE_ENABLED = false
- local REMOTE_ADDR = 20
- local REMOTE_MODEM_DIR = "right"
- local function debug(txt)
- if DEBUG_LOG then
- if LOCAL_ENABLED then
- local hFile
- if fs.exists(LOCAL_LOG_FILE_NAME) then
- hFile = fs.open(LOCAL_LOG_FILE_NAME, "a");
- else
- hFile = fs.open(LOCAL_LOG_FILE_NAME, "w");
- end
- hFile.writeLine(txt);
- hFile.close();
- end
- if REMOTE_ENABLED then
- rednet.open(REMOTE_MODEM_DIR)
- rednet.send(REMOTE_ADDR, txt)
- rednet.close()
- end
- end
- end
- local function inRange(posX, posY, width, height)
- return 0 <= posX and posX < width and 0 <= posY and posY < height
- end
- local function sign(val)
- if val == 0 then
- return 0
- else
- return math.abs(val) / val
- end
- end
- function surelyUp()
- while not turtle.up() do turtle.digUp() end
- end
- function surelyDown()
- while not turtle.down() do end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyDigUp()
- while turtle.digUp() do
- os.sleep(0.4)
- end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyDig()
- while turtle.dig() do end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyFwd()
- for i=1,4 do
- local status, err = turtle.forward()
- if status then
- return true -- success!
- elseif err=="Out of fuel" then
- return status, err
- end
- surelyDig() -- face to a normal block or a sand(gravel) hill
- if turtle.detect() and not turtle.dig() then
- return false, "bedrock!"
- end
- if turtle.forward() then return true end -- success!
- if turtle.attack() then
- -- face to monster-mob
- while turtle.attack() do end
- else
- -- face to sand-blocks which is dropping long time
- os.sleep(5) -- probably, adjustment is required
- end
- end
- return turtle.forward()
- end
- -- -------------------------------
- -- application
- -- -------------------------------
- -- ---------------
- -- direction control
- -- ---------------
- local COMPARE_BLOCKS = {SLOT_COBBLESTONE, SLOT_STONE, SLOT_DART, SLOT_GRAVEL }
- local DIR_FORWARD = 1
- local DIR_RIGHT = 2
- local DIR_BACK = 3
- local DIR_LEFT = 4
- local DIR_NOT = 5
- local DIR_TO_NAME = {
- [DIR_FORWARD] = "F",
- [DIR_RIGHT ] = "R",
- [DIR_BACK ] = "B",
- [DIR_LEFT ] = "L",
- [DIR_NOT ] = "*"}
- local TURN_NOT = 1
- local TURN_LEFT = 2
- local TURN_RIGHT = 3
- local TURN_OPPOSIT = 4
- local TURN_COSTS = { [TURN_NOT] = 0, [TURN_LEFT] = 1,
- [TURN_RIGHT] = 2, [TURN_OPPOSIT] = 3 }
- local TURN_RULES = { -- dir_forward, dir_right, dir_back, dir_left
- [DIR_FORWARD] = { TURN_NOT , TURN_RIGHT , TURN_OPPOSIT, TURN_LEFT },
- [DIR_RIGHT ] = { TURN_LEFT , TURN_NOT , TURN_RIGHT , TURN_OPPOSIT },
- [DIR_BACK ] = { TURN_OPPOSIT, TURN_LEFT , TURN_NOT , TURN_RIGHT },
- [DIR_LEFT ] = { TURN_RIGHT , TURN_OPPOSIT, TURN_LEFT , TURN_NOT }}
- local TURN_RELATIVE = { -- turn_left, turn_right
- [DIR_FORWARD] = { [TURN_LEFT] = DIR_LEFT , [TURN_RIGHT] = DIR_RIGHT },
- [DIR_RIGHT ] = { [TURN_LEFT] = DIR_FORWARD, [TURN_RIGHT] = DIR_BACK },
- [DIR_BACK ] = { [TURN_LEFT] = DIR_RIGHT , [TURN_RIGHT] = DIR_LEFT },
- [DIR_LEFT ] = { [TURN_LEFT] = DIR_BACK , [TURN_RIGHT] = DIR_FORWARD }}
- local OPPOSIT_DIR = {DIR_BACK, DIR_LEFT, DIR_FORWARD, DIR_LEFT}
- -- ---------------
- -- findTarget
- -- ---------------
- local TargetFinder = {
- iterator = function(width, depth)
- findTarget = function(scanX, scanZ, reverse, rangeWidth, rangeDepth)
- local TILE_PATTERN = {"*","b","r","l","f"}
- local SHAPE_DIRECTION = { DIR_NOT, DIR_FORWARD, DIR_LEFT,
- DIR_RIGHT, DIR_BACK }
- local idx = ( (scanZ%5) + (scanX%5)*3 )%5 + 1
- local dirChar = TILE_PATTERN[idx]
- if (not reverse and "b" == dirChar) or
- ( reverse and "f" == dirChar) then
- return false
- end
- local DIR_TO_CENTER = {{ dx = 0, dz = 0 }, -- *
- { dx = 0, dz = -1 }, -- b
- { dx = 1, dz = 0 }, -- r
- { dx = -1, dz = 0 }, -- l
- { dx = 0, dz = 1 }} -- f
- local isCenter = false
- local found = false
- local newX, newZ
- local shape = {}
- local shapeDbg = ""
- local centerX = scanX + DIR_TO_CENTER[idx].dx
- local centerZ = scanZ + DIR_TO_CENTER[idx].dz
- if inRange(centerX, centerZ, rangeWidth, rangeDepth) then
- found = true
- isCenter = true
- newX = centerX
- newZ = centerZ
- else
- table.insert(shape, DIR_NOT)
- shapeDbg = shapeDbg .. "*"
- end
- local testX, testZ
- for i = 2, 5 do
- testX = centerX - DIR_TO_CENTER[i].dx
- testZ = centerZ - DIR_TO_CENTER[i].dz
- if inRange(testX, testZ, rangeWidth, rangeDepth) then
- if isCenter then
- table.insert(shape, SHAPE_DIRECTION[i])
- shapeDbg = shapeDbg .. DIR_TO_NAME[SHAPE_DIRECTION[i]]
- end
- if not found then
- newX = testX
- newZ = testZ
- end
- found = true
- end
- end
- if not found then
- return false
- end
- return true, newX, newZ, shape
- end
- local xAxisIterator = function(width)
- local pos = 0
- local loopEnd = math.floor((width + 1) / 3)
- return function()
- local current = pos
- pos = pos + 1
- if loopEnd < current then
- return nil
- else
- return current * 3
- end
- end
- end
- local zAxisIterator = function(x, depth)
- local pos, step
- if 0 == x % 2 then
- pos, step = -1, 1
- else
- pos, step = depth, -1
- end
- return function()
- local current = pos
- pos = pos + step
- if current < -1 or depth < current then
- return nil
- else
- return current
- end
- end
- end
- local xIter = xAxisIterator(width)
- local zIter = nil
- local x
- local previousX
- local previousZ
- return function()
- while true do
- if nil == zIter then
- x = xIter()
- if nil == x then
- return nil
- end
- zIter = zAxisIterator(x, depth)
- end
- local z = zIter()
- if nil == z then
- zIter = nil
- else
- --return x, z
- local found, newX, newZ, shape
- = findTarget(x, z, 0 == x%2, width, depth)
- if found and (previousX ~= newX or previousZ ~= newZ) then
- previousX = newX
- previousZ = newZ
- return newX, newZ, shape
- end
- end
- end
- end
- end
- }
- -- ---------------
- -- mining control
- -- ---------------
- MOVE_H_TOP = "MOVE_H_TOP"
- MOVE_V_DOWN = "MOVE_V_DOWN"
- MOVE_H_BOTTOM = "MOVE_H_BOTTOM"
- MOVE_V_UP = "MOVE_V_UP"
- local mineCtrl = {
- status,
- RANGE_WIDTH,
- RANGE_DEPTH,
- BOTTOM_HEIGHT,
- newX,
- newZ,
- lastX = -1,
- lastZ = -1,
- --reopsitoryMemo = { },
- repositoryInfo = {
- currentLevel = 1,
- currentSeq = 0
- },
- pointBeforeX,
- pointBeforeZ,
- turtleIsTop = true,
- turtleInitialY = 0,
- interruptX,
- interruptZ,
- interruptY,
- vacantChestList = { },
- occupiedChestList = { },
- underInterruption = false,
- }
- -- ---------------
- -- turtle
- -- ---------------
- local MyTurtle = {}
- MyTurtle.new = function(initialY)
- local obj = {}
- obj.turtleX = 0
- obj.turtleZ = 0
- obj.turtleY = initialY
- obj.turtleDir = DIR_FORWARD
- return setmetatable(obj, {__index = MyTurtle})
- end
- MyTurtle.turnTo = function(self, direction)
- local turn = TURN_RULES[self.turtleDir][direction]
- if TURN_LEFT == turn then
- if not SIMULATION then
- turtle.turnLeft()
- end
- elseif TURN_RIGHT == turn then
- if not SIMULATION then
- turtle.turnRight()
- end
- elseif TURN_OPPOSIT == turn then
- if not SIMULATION then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- self.turtleDir = direction
- end
- MyTurtle.runEventHandlers = function(self, evHandlers)
- if nil == evHandlers then
- return
- end
- for _, func in ipairs(evHandlers) do
- func()
- end
- end
- MyTurtle.adjustX = function(self, aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
- if self.turtleX < aimX and DIR_RIGHT ~= self.turtleDir then
- self:turnTo(DIR_RIGHT)
- elseif aimX < self.turtleX and DIR_LEFT ~= self.turtleDir then
- self:turnTo(DIR_LEFT)
- end
- while self.turtleX ~= aimX do
- if not SIMULATION then
- self:runEventHandlers(beforeEvHandlers)
- surelyFwd()
- self:runEventHandlers(afterEvHandlers)
- if withDigUp then
- surelyDigUp()
- end
- end
- self.turtleX = self.turtleX + sign(aimX - self.turtleX)
- end
- end
- MyTurtle.adjustZ = function(self, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- if self.turtleZ < aimZ and DIR_FORWARD ~= self.turtleDir then
- self:turnTo(DIR_FORWARD)
- elseif aimZ < self.turtleZ and DIR_BACK ~= self.turtleDir then
- self:turnTo(DIR_BACK)
- end
- while self.turtleZ ~= aimZ do
- if not SIMULATION then
- self:runEventHandlers(beforeEvHandlers)
- surelyFwd()
- self:runEventHandlers(afterEvHandlers)
- if withDigUp then
- surelyDigUp()
- end
- end
- self.turtleZ = self.turtleZ + sign(aimZ - self.turtleZ)
- end
- end
- MyTurtle.moveTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- end
- MyTurtle.backTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
- end
- MyTurtle.moveVertical = function(self, targetY, beforeEvHandlers, afterEvHandlers)
- local vDir = sign(targetY - self.turtleY)
- while targetY ~= self.turtleY do
- if not SIMULATION then
- if 1 == vDir then
- self:runEventHandlers(beforeEvHandlers)
- turtle.digUp()
- surelyUp()
- self.turtleY = self.turtleY + vDir
- self:runEventHandlers(afterEvHandlers)
- elseif -1 == vDir then
- self:runEventHandlers(beforeEvHandlers)
- turtle.digDown()
- surelyDown()
- self.turtleY = self.turtleY + vDir
- self:runEventHandlers(afterEvHandlers)
- end
- end
- end
- end
- -- ---------------
- -- additional action : vertical mining
- -- ---------------
- local function verticalMiningAction(_myT, _shape)
- local turnToNearestEdge = function (myT, shape)
- local turnMinCost = 99
- local smallestTurnDir = DIR_NOT
- local turnTmp, costTmp
- for _, dir in ipairs(shape) do
- turnTmp = TURN_RULES[myT.turtleDir][dir]
- costTmp = TURN_COSTS[turnTmp]
- if costTmp < turnMinCost then
- turnMinCost = costTmp
- smallestTurnDir = dir
- end
- end
- myT:turnTo(smallestTurnDir)
- end
- local getTurnFuncName = function(myT, shape)
- local dirWhenTurnLeft = TURN_RELATIVE[myT.turtleDir][TURN_LEFT ]
- local dirWhenTurnRight = TURN_RELATIVE[myT.turtleDir][TURN_RIGHT]
- for _, dir in ipairs(shape) do
- if dir == dirWhenTurnLeft then
- return TURN_LEFT
- elseif dir == dirWhenTurnRight then
- return TURN_RIGHT
- end
- end
- end
- local digValuableBlockOnly = function()
- local selSlot = 16
- local checkedSlot = -1
- for _, slot in ipairs(COMPARE_BLOCKS) do
- if selSlot == slot then
- if turtle.compare() then
- return
- end
- checkedSlot = slot
- break
- end
- end
- for _, slot in ipairs(COMPARE_BLOCKS) do
- if checkedSlot ~= slot then
- turtle.select(slot)
- if turtle.compare() then
- return
- end
- end
- end
- turtle.dig()
- return true
- end
- local myT = _myT
- local shape = _shape
- local firstFlg = true
- local turnLR
- return function()
- if firstFlg then
- firstFlg = not firstFlg
- if 2 == #shape or 3 == #shape then
- turnToNearestEdge(myT, shape)
- turnLR = getTurnFuncName(myT, shape)
- else
- turnLR = TURN_RIGHT
- end
- end
- if 1 < #shape then
- for i = 1, #shape do
- if not SIMULATION then
- if EXCAVATE_ALL then
- surelyDig()
- dug = true
- else
- dug = digValuableBlockOnly()
- end
- end
- if i < #shape then
- myT:turnTo(TURN_RELATIVE[myT.turtleDir][turnLR])
- end
- end
- if 2 == #shape or 3 == #shape then
- if TURN_LEFT == turnLR then
- turnLR = TURN_RIGHT
- else
- turnLR = TURN_LEFT
- end
- end
- end
- end
- end
- -- ---------------
- -- repository map
- -- ---------------
- local REPO_C_CHEST = "C"
- local REPO_F_FUEL = "F"
- local REPO_O_ORE = "O"
- local REPO_BLANK = " "
- local function makeRepoMap()
- local repoMap = { }
- for i = 1, table.maxn(REPOSITORY_MAP) do
- local line = REPOSITORY_MAP[i]
- local lineAry = { }
- for j = 1, string.len(line) do
- local ch = string.sub(line, j, j)
- table.insert(lineAry, ch)
- end
- table.insert(repoMap, lineAry)
- end
- return repoMap
- end
- local function nvl(val, subVal)
- if nil == val then
- return subVal
- else
- return val
- end
- end
- local function findRepo(repoMap, chestType, no)
- local function found(col, chestType)
- for _, v in ipairs(chestType) do
- if REPO_O_ORE == v then
- if nil ~= string.find("0123456789", col) then
- return true
- end
- elseif nil ~= string.find("0123456789", v)
- and nil ~= string.find("0123456789", col) then
- if "0" == col then
- return true
- elseif tonumber(v) <= tonumber(col) then
- return true
- end
- else
- if v == col then
- return true
- end
- end
- end
- return false
- end
- local findNo = 0
- for i = 1, table.maxn(repoMap) do
- local row = repoMap[i]
- for j = 1, table.maxn(row) do
- local col = row[j]
- if found(col, chestType) and no <= findNo then
- debug("fr[" .. col .. "] x, z = " .. tostring(j) .. ", " .. tostring(i))
- return j - 1 + REPOSITORY_OFFSET_LR,
- i - 1 + REPOSITORY_OFFSET_BF,
- findNo,
- col
- end
- findNo = findNo + 1
- end
- end
- return nil
- end
- -- ---------------
- -- additional action : inventory check
- -- ---------------
- local function checkInventoryAction(_myT, _mineCtrl)
- local function dropUselessBlocks()
- local cnt
- for _, v in ipairs(SLOTS_KEEP_1) do
- cnt = turtle.getItemCount(v)
- if 1 < cnt then
- turtle.select(v)
- turtle.dropDown(cnt - 1)
- end
- end
- for _, v in ipairs(SLOTS_KEEP_32) do
- cnt = turtle.getItemCount(v)
- if 32 < cnt then
- turtle.select(v)
- turtle.dropDown(cnt - 32)
- end
- end
- local freeCnt = 0
- for _, v in ipairs(SLOTS_FREE) do
- cnt = turtle.getItemCount(v)
- if 0 < cnt then
- turtle.select(v)
- for _, w in ipairs(SLOTS_USELESS_SAMPLE) do
- if turtle.compareTo(w) then
- turtle.dropDown()
- freeCnt = freeCnt + 1
- break
- end
- end
- else
- freeCnt = freeCnt + 1
- end
- end
- turtle.select(1)
- debug("==== dropUselessBlocks() makes " .. tostring(freeCnt) .." vacantSlot(s)")
- return 0 < freeCnt
- end
- local function checkInventory()
- if turtle.getFuelLevel() <= FUEL_LEVEL_MINIMUM then
- return false
- end
- for _, i in ipairs(SLOTS_FREE) do
- if 0 == turtle.getItemCount(i) then
- return true
- end
- end
- return false
- end
- local function findNearestAccessPos(mineCtrl, myT, repoMap, xx, zz, currentLevel)
- --local lr = xx - REPOSITORY_OFFSET_LR
- --local bf = zz - REPOSITORY_OFFSET_BF
- local minDist = nil
- local rsltX, rsltZ, rsltDir
- local acDirs = {
- { dir = DIR_NOT , LV1only = true , lr = 0, bf = 0 },
- { dir = DIR_BACK , LV1only = false, lr = 0, bf = 1 },
- { dir = DIR_LEFT , LV1only = false, lr = 1, bf = 0 },
- { dir = DIR_FORWARD, LV1only = false, lr = 0, bf = -1 },
- { dir = DIR_RIGHT , LV1only = false, lr = -1, bf = 0 }}
- for _, v in ipairs(acDirs) do
- local testX = xx + v.lr
- local testZ = zz + v.bf
- local idxX = testX - REPOSITORY_OFFSET_LR + 1
- local idxZ = testZ - REPOSITORY_OFFSET_BF + 1
- --debug("testZ - REPOSITORY_OFFSET_BF + 1 = " .. tostring(testZ - REPOSITORY_OFFSET_BF + 1))
- --debug("testX - REPOSITORY_OFFSET_LR + 1 = " .. tostring(testX - REPOSITORY_OFFSET_LR + 1))
- if 0 <= testX and testX < mineCtrl.RANGE_WIDTH and
- 0 <= testZ and testZ < mineCtrl.RANGE_DEPTH and
- 0 < idxZ and idxZ <= table.maxn(repoMap) and
- 0 < idxX and idxX <= table.maxn(repoMap[1]) and
- REPO_BLANK == repoMap[idxZ][idxX] then
- local dist = math.abs(myT.turtleX - testX) + math.abs(myT.turtleZ - testZ)
- if nil == minDist or dist < minDist then
- minDist = dist
- rsltX = testX
- rsltZ = testZ
- rsltDir = v.dir
- end
- end
- end
- if nil == minDist then
- return nil
- else
- return rsltX, rsltZ, rsltDir
- end
- end
- local function doDump(dropFunc)
- local result = true
- for _, v in ipairs(SLOTS_FREE) do
- cnt = turtle.getItemCount(v)
- if 0 < cnt then
- turtle.select(v)
- if KEEP_USELESS_BLOCKS and 0 < turtle.getItemCount(v) then
- if not dropFunc() then
- result = false
- end
- else
- for _, w in ipairs(SLOTS_USELESS_SAMPLE) do
- if not turtle.compareTo(w) and 0 < turtle.getItemCount(v) then
- if not dropFunc() then
- result = false
- end
- end
- break
- end
- end
- end
- end
- return result
- end
- local function doRefuel(myT, repoMap)
- local xx, zz
- local findSeq = 0
- local fuelChestEmpty = false
- while true do
- xx, zz, findSeq = findRepo(repoMap, {REPO_F_FUEL}, findSeq)
- if nil == xx then
- fuelChestEmpty = true
- xx, zz, findSeq = findRepo(repoMap, {REPO_F_FUEL}, 0)
- end
- findSeq = findSeq + 1
- myT:moveTo(xx, zz, false)
- surelyDigUp()
- surelyUp()
- turtle.select(SLOT_CHEST)
- turtle.placeDown()
- turtle.dropDown()
- debug("refuel before = " .. tostring(turtle.getFuelLevel()))
- while true do
- if fuelChestEmpty then
- print("fuel level is low. put fuel in the chest.")
- while true do
- if turtle.suckUp() then
- break
- end
- print("sleep 5")
- os.sleep(5)
- end
- else
- turtle.suckUp()
- end
- if not fuelChestEmpty then
- if 0 == turtle.getItemCount() then
- break
- end
- end
- local before = turtle.getFuelLevel()
- turtle.refuel(1)
- local after = turtle.getFuelLevel()
- local unitVal = after - before
- local reqCnt = (FUEL_LEVEL_REFUEL - after) / unitVal
- debug("refuel bef, aft, unit, reqCnt = "
- .. tostring(before) .. ", "
- .. tostring(after) .. ", "
- .. tostring(unitVal) .. ", "
- .. tostring(reqCnt))
- if turtle.getItemCount() <= reqCnt then
- turtle.refuel()
- else
- if 0 < reqCnt then
- turtle.refuel(math.floor(reqCnt))
- end
- turtle.dropUp()
- end
- if FUEL_LEVEL_REFUEL <= turtle.getFuelLevel() then
- break
- end
- end
- fuelChestEmpty = false
- turtle.suckDown()
- turtle.digDown()
- surelyDown()
- if FUEL_LEVEL_REFUEL <= turtle.getFuelLevel() then
- break
- end
- end
- end
- local function backToTheRepository(myT, chestType, mineCtrl)
- local repoMap = makeRepoMap()
- local myT4 = MyTurtle.new(0)
- myT4.turtleY = myT.turtleY
- myT4.turtleX = myT.turtleX
- myT4.turtleZ = myT.turtleZ
- myT4.turtleDir = myT.turtleDir
- if turtle.getFuelLevel() <= FUEL_LEVEL_MINIMUM then
- doRefuel(myT4, repoMap)
- end
- local xx, zz
- local cell
- local findSeq = mineCtrl.repositoryInfo.currentSeq
- local currLevel = mineCtrl.repositoryInfo.currentLevel
- local dumpDone = false
- while true do
- if REPO_O_ORE ~= chestType then
- xx, zz, findSeq, cell = findRepo(repoMap, { chestType }, findSeq)
- if nil == xx then
- break
- end
- else
- xx, zz, findSeq, cell = findRepo(repoMap, { currLevel }, findSeq)
- if nil == xx then
- currLevel = currLevel + 1
- findSeq = 0
- xx, zz, findSeq, cell = findRepo(repoMap, { currLevel }, findSeq)
- if nil == xx then
- break
- end
- end
- end
- local xx2, zz2, accessDir = findNearestAccessPos(mineCtrl, myT4, repoMap, xx, zz, currLevel)
- if nil ~= xx2 then
- myT4:moveTo(xx2, zz2, false)
- local upDown = currLevel
- if DIR_NOT ~= accessDir then
- myT4:turnTo(accessDir)
- upDown = upDown + 1
- end
- for i = 1, upDown do
- surelyDigUp()
- surelyUp()
- end
- local funcs
- if DIR_NOT == accessDir then
- funcs = { compare = turtle.compareUp, dig = surelyDigUp,
- place = turtle.placeUp , drop = turtle.dropUp }
- else
- funcs = { compare = turtle.compare , dig = surelyDig,
- place = turtle.place , drop = turtle.drop }
- end
- turtle.select(1)
- if not funcs.compare() then
- funcs.dig()
- funcs.place()
- end
- dumpDone = doDump(funcs.drop)
- for i = 1, upDown do
- turtle.digDown()
- surelyDown()
- end
- if dumpDone then
- break
- else
- findSeq = findSeq + 1
- end
- end
- end
- mineCtrl.repositoryInfo.currentSeq = findSeq
- mineCtrl.repositoryInfo.currentLevel = currLevel
- myT4:backTo(myT.turtleX, myT.turtleZ, false)
- myT4:turnTo(myT.turtleDir)
- end
- local function dump()
- surelyDigUp()
- surelyUp()
- turtle.select(SLOT_CHEST)
- turtle.placeUp()
- doDump(turtle.dropUp)
- turtle.select(1)
- turtle.digDown()
- surelyDown()
- end
- local function backToTheGround(myT, mineCtrl)
- debug("====== backToTheGround()")
- debug("====== status = " .. mineCtrl.status)
- debug("====== pos x,z,y = " .. myT.turtleX .. ", " .. myT.turtleZ .. ", " .. myT.turtleY)
- debug("====== last x,z = " .. mineCtrl.lastX .. ", " .. mineCtrl.lastZ)
- local t2 = MyTurtle.new()
- t2.turtleX = myT.turtleX
- t2.turtleZ = myT.turtleZ
- t2.turtleY = myT.turtleY
- t2.turtleDir = myT.turtleDir
- if MOVE_H_TOP == mineCtrl.status then
- debug("<<<>>> backToTheGround [MOVE_H_TOP] begin")
- t2:backTo(mineCtrl.lastX, mineCtrl.lastZ, nil, nil)
- debug("<<<>>> backToTheGround [MOVE_H_TOP] end")
- elseif MOVE_V_DOWN == mineCtrl.status then
- debug("<<<>>> backToTheGround [MOVE_V_DOWN] begin")
- t2:moveVertical(mineCtrl.turtleInitialY, nil, nil)
- debug("<<<>>> backToTheGround [MOVE_V_DOWN] end")
- elseif MOVE_H_BOTTOM == mineCtrl.status then
- debug("<<<>>> backToTheGround [MOVE_H_BOTTOM] begin")
- t2:backTo(mineCtrl.lastX, mineCtrl.lastZ, nil, nil)
- t2:moveVertical(mineCtrl.turtleInitialY, nil, nil)
- debug("<<<>>> backToTheGround [MOVE_H_BOTTOM] end")
- elseif MOVE_V_UP == mineCtrl.status then
- debug("<<<>>> backToTheGround [MOVE_V_UP] begin")
- t2:moveVertical(mineCtrl.BOTTOM_HEIGHT, nil, nil)
- t2:backTo(mineCtrl.lastX, mineCtrl.lastZ, nil, nil)
- t2:moveVertical(mineCtrl.turtleInitialY, nil, nil)
- debug("<<<>>> backToTheGround [MOVE_V_UP] end")
- end
- if USE_REPOSITORY_MAP then
- backToTheRepository(t2, REPO_O_ORE, mineCtrl)
- else
- dump()
- end
- if MOVE_H_TOP == mineCtrl.status then
- t2:moveTo(myT.turtleX, myT.turtleZ)
- elseif MOVE_V_DOWN == mineCtrl.status then
- t2:moveVertical(myT.turtleY, nil, nil)
- elseif MOVE_H_BOTTOM == mineCtrl.status then
- t2:moveVertical(mineCtrl.BOTTOM_HEIGHT, nil, nil)
- t2:moveTo(myT.turtleX, myT.turtleZ)
- elseif MOVE_V_UP == mineCtrl.status then
- t2:moveVertical(mineCtrl.BOTTOM_HEIGHT, nil, nil)
- t2:moveTo(myT.turtleX, myT.turtleZ)
- t2:moveVertical(myT.turtleY, nil, nil)
- end
- t2:turnTo(myT.turtleDir)
- debug("<<<>>> compreted?")
- debug(" <<<>>> myT x,z,y = " .. myT.turtleX .. ", " .. myT.turtleZ .. ", " .. myT.turtleY)
- debug(" <<<>>> t2 x,z,y = " .. t2.turtleX .. ", " .. t2.turtleZ .. ", " .. t2.turtleY)
- end
- return function(force)
- if nil ~= force and force == true then
- backToTheRepository(_myT, REPO_O_ORE, _mineCtrl)
- else
- if not checkInventory() then
- debug("====================================")
- debug("== checkInventory() returns false")
- debug("== at x : " .. tostring(_myT.turtleX) ..
- " / z : " .. tostring(_myT.turtleZ) ..
- " / y : " .. tostring(_myT.turtleY))
- debug("====================================")
- local result = false
- if not KEEP_USELESS_BLOCKS then
- result = dropUselessBlocks()
- end
- if not result then
- backToTheGround(_myT, _mineCtrl)
- end
- end
- end
- end
- end
- -- ---------------
- -- initialize
- -- ---------------
- local function initialize()
- if SHOW_USAGE then
- showUsage()
- print("Hit any key when ready.")
- read()
- end
- if ASK_INITIAL_HEIGHT then
- print("Input current y")
- term.write(">")
- local ch = read()
- if nil == tonumber(ch) then
- error("Not a Number!")
- end
- mineCtrl.turtleInitialY = tonumber(ch)
- else
- mineCtrl.turtleInitialY = TURTLE_INITIAL_Y
- end
- mineCtrl.RANGE_WIDTH = DEFAULT_WIDTH
- mineCtrl.RANGE_DEPTH = DEFAULT_DEPTH
- if HEIGHT_MODE then
- -- true : use BEADROCK_HEIGHT
- mineCtrl.BOTTOM_HEIGHT = BEADROCK_HEIGHT
- else
- -- false : use RELATIVE_HEIGHT
- mineCtrl.BOTTOM_HEIGHT = mineCtrl.turtleInitialY - RELATIVE_HEIGHT
- end
- end
- local function placeSupplementChest()
- local reMap = makeRepoMap()
- local myT3 = MyTurtle.new(64)
- local xx, zz
- local findSeq = 0
- while true do
- xx, zz, findSeq = findRepo(reMap, {REPO_C_CHEST, REPO_F_FUEL}, findSeq)
- if nil == xx then
- break
- end
- findSeq = findSeq + 1
- myT3:moveTo(xx, zz, false)
- turtle.select(1)
- surelyDigUp()
- surelyUp()
- turtle.select(1)
- if not turtle.compareUp() then
- surelyDigUp()
- turtle.placeUp()
- end
- turtle.digDown()
- surelyDown()
- xx = xx + 1
- end
- myT3:backTo(0, 0, false)
- myT3:turnTo(DIR_FORWARD)
- end
- -- -------------------------------
- -- main
- -- -------------------------------
- debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
- debug("@@ chunkDig4.2.lua")
- debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
- initialize()
- if USE_REPOSITORY_MAP and PLACE_SUPPLEMENT_CHEST then
- placeSupplementChest()
- end
- local myT = MyTurtle.new(mineCtrl.turtleInitialY)
- local beforeEvHandlers = { }
- local afterEvHandlers = { }
- local checkInventoryA = checkInventoryAction(myT, mineCtrl)
- local newX, newZ
- mineCtrl.lastX = -1
- mineCtrl.lastZ = -1
- mineCtrl.status = MOVE_H_TOP
- for newX, newZ, shape in TargetFinder.iterator(mineCtrl.RANGE_WIDTH, mineCtrl.RANGE_DEPTH) do
- mineCtrl.newX = newX
- mineCtrl.newZ = newZ
- if -1 == mineCtrl.lastX then
- mineCtrl.lastX = mineCtrl.newX
- mineCtrl.lastZ = mineCtrl.newZ
- end
- debug("=[for loop]====================")
- debug("== newX, newZ = " .. tostring(mineCtrl.newX) .. ", " .. tostring(mineCtrl.newZ ))
- debug("== lastX, lastZ = " .. tostring(mineCtrl.lastX) .. ", " .. tostring(mineCtrl.lastZ))
- debug("===============================")
- myT:moveTo(mineCtrl.newX, mineCtrl.newZ, {}, { checkInventoryA })
- if MOVE_H_TOP == mineCtrl.status then
- mineCtrl.status = MOVE_V_DOWN
- beforeEvHandlers = { }
- afterEvHandlers = { verticalMiningAction(myT, shape),
- checkInventoryA }
- myT:moveVertical(mineCtrl.BOTTOM_HEIGHT,
- beforeEvHandlers, afterEvHandlers)
- mineCtrl.status = MOVE_H_BOTTOM
- elseif MOVE_H_BOTTOM == mineCtrl.status then
- mineCtrl.status = MOVE_V_UP
- beforeEvHandlers = { verticalMiningAction(myT, shape),
- checkInventoryA }
- afterEvHandlers = { }
- myT:moveVertical(mineCtrl.turtleInitialY,
- beforeEvHandlers, afterEvHandlers)
- mineCtrl.status = MOVE_H_TOP
- else
- error("unexpected condition.")
- end
- mineCtrl.lastX = mineCtrl.newX
- mineCtrl.lastZ = mineCtrl.newZ
- end
- myT:backTo(0, 0, {}, { checkInventoryA })
- myT:turnTo(DIR_FORWARD)
- myT:moveVertical(mineCtrl.turtleInitialY, {checkInventoryA}, {})
- checkInventoryA(true)
- print("chunkDig4.2.lua was compreted!!")
- debug("chunkDig4.2.lua was compreted!!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement