Ledger Nano X - The secure hardware wallet
SHARE
TWEET

chunkDig4

kssr3951 Nov 16th, 2014 (edited) 164 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function showUsage()
  2.   print("chunkDig4")
  3.   print("slot1 : chest x 64")
  4.   print("slot2 : stone x 1")
  5.   print("slot3 : gravel x 1")
  6.   print("slot4 : dart x 1")
  7.   print("slot5 : cobblestone x 32")
  8. end
  9. local SLOT_CHEST       = 1
  10. local SLOT_STONE       = 2
  11. local SLOT_GRAVEL      = 3
  12. local SLOT_DART        = 4
  13. local SLOT_COBBLESTONE = 5
  14. local SLOTS_FREE     = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
  15. local SLOTS_KEEP_ALL = { SLOT_CHEST }
  16. local SLOTS_KEEP_32  = { SLOT_COBBLESTONE }
  17. local SLOTS_KEEP_1   = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
  18. local SLOTS_USELESS_SAMPLE = { SLOT_COBBLESTONE, SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
  19. -- -------------------------------
  20. -- config
  21. -- -------------------------------
  22. local DEFAULT_WIDTH        = 16
  23. local DEFAULT_DEPTH        = 16 -- forward() direction
  24. local PREVENTION_FROM_FALL = false
  25. local EXCAVATE_ALL         = false
  26. local KEEP_USELESS_BLOCKS  = false
  27. local SHOW_USAGE           = true
  28. --
  29. local ASK_INITIAL_HEIGHT   = true
  30. local TURTLE_INITIAL_Y     = 71
  31. -- HEIGHT_MODE
  32. --   true  : use BEADROCK_HEIGHT
  33. --   false : use RELATIVE_HEIGHT
  34. local HEIGHT_MODE          = true
  35. local BEADROCK_HEIGHT      = 5
  36. local RELATIVE_HEIGHT      = 3
  37. -- for debug
  38. local DEBUG_LOG            = true
  39. local SIMULATION           = false
  40. -- -------------------------------
  41. -- utility
  42. -- -------------------------------
  43. -- local log
  44. local LOCAL_ENABLED       = true
  45. local LOCAL_LOG_FILE_NAME = "debug.log"
  46. -- remote log
  47. local REMOTE_ENABLED      = false
  48. local REMOTE_ADDR         = 20
  49. local REMOTE_MODEM_DIR    = "right"
  50. local function debug(txt)
  51.   if DEBUG_LOG then
  52.     if LOCAL_ENABLED then
  53.       local hFile
  54.       if fs.exists(LOCAL_LOG_FILE_NAME) then
  55.         hFile = fs.open(LOCAL_LOG_FILE_NAME, "a");
  56.       else
  57.         hFile = fs.open(LOCAL_LOG_FILE_NAME, "w");
  58.       end
  59.       hFile.writeLine(txt);
  60.       hFile.close();
  61.     end
  62.     if REMOTE_ENABLED then
  63.       rednet.open(REMOTE_MODEM_DIR)
  64.       rednet.send(REMOTE_ADDR, txt)
  65.       rednet.close()
  66.     end
  67.   end
  68. end
  69. local function inRange(posX, posY, width, height)
  70.   return 0 <= posX and posX < width and 0 <= posY and posY < height
  71. end
  72. local function sign(val)
  73.   if val == 0 then
  74.     return 0
  75.   else
  76.     return math.abs(val) / val
  77.   end
  78. end
  79. function surelyUp()
  80.   while not turtle.up() do turtle.digUp() end
  81. end
  82. function surelyDown()
  83.   while not turtle.down() do end
  84. end
  85. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  86. function surelyDigUp()
  87.   while turtle.digUp() do
  88.     os.sleep(0.4)
  89.   end
  90. end
  91. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  92. function surelyDig()
  93.   while turtle.dig() do end
  94. end
  95. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  96. function surelyFwd()
  97.   for i=1,4 do
  98.     local status, err = turtle.forward()
  99.     if status then
  100.       return true  -- success!
  101.     elseif err=="Out of fuel" then
  102.       return status, err
  103.     end
  104.  
  105.     surelyDig() -- face to a normal block or a sand(gravel) hill
  106.  
  107.     if turtle.detect() and not turtle.dig() then
  108.       return false, "bedrock!"
  109.     end
  110.  
  111.     if turtle.forward() then return true end -- success!
  112.  
  113.     if turtle.attack() then
  114.       -- face to monster-mob
  115.       while turtle.attack() do end
  116.     else
  117.       -- face to sand-blocks which is dropping long time
  118.       os.sleep(5) -- probably, adjustment is required
  119.     end
  120.   end
  121.   return turtle.forward()
  122. end
  123. -- -------------------------------
  124. -- application
  125. -- -------------------------------
  126. -- ---------------
  127. -- direction control
  128. -- ---------------
  129. local COMPARE_BLOCKS = {SLOT_COBBLESTONE, SLOT_STONE, SLOT_DART, SLOT_GRAVEL }
  130. local DIR_FORWARD = 1
  131. local DIR_RIGHT   = 2
  132. local DIR_BACK    = 3
  133. local DIR_LEFT    = 4
  134. local DIR_NOT     = 5
  135. local DIR_TO_NAME = {
  136.   [DIR_FORWARD] = "F",
  137.   [DIR_RIGHT  ] = "R",
  138.   [DIR_BACK   ] = "B",
  139.   [DIR_LEFT   ] = "L",
  140.   [DIR_NOT    ] = "*"}
  141. local TURN_NOT     = 1
  142. local TURN_LEFT    = 2
  143. local TURN_RIGHT   = 3
  144. local TURN_OPPOSIT = 4
  145. local TURN_COSTS = { [TURN_NOT]   = 0, [TURN_LEFT]    = 1,
  146.                      [TURN_RIGHT] = 2, [TURN_OPPOSIT] = 3 }
  147. local TURN_RULES = { -- dir_forward, dir_right, dir_back, dir_left
  148.   [DIR_FORWARD] = { TURN_NOT    , TURN_RIGHT  , TURN_OPPOSIT, TURN_LEFT    },
  149.   [DIR_RIGHT  ] = { TURN_LEFT   , TURN_NOT    , TURN_RIGHT  , TURN_OPPOSIT },
  150.   [DIR_BACK   ] = { TURN_OPPOSIT, TURN_LEFT   , TURN_NOT    , TURN_RIGHT   },
  151.   [DIR_LEFT   ] = { TURN_RIGHT  , TURN_OPPOSIT, TURN_LEFT   , TURN_NOT     }}
  152. local TURN_RELATIVE = { -- turn_left, turn_right
  153.   [DIR_FORWARD] = { [TURN_LEFT] = DIR_LEFT   , [TURN_RIGHT] = DIR_RIGHT   },
  154.   [DIR_RIGHT  ] = { [TURN_LEFT] = DIR_FORWARD, [TURN_RIGHT] = DIR_BACK    },
  155.   [DIR_BACK   ] = { [TURN_LEFT] = DIR_RIGHT  , [TURN_RIGHT] = DIR_LEFT    },
  156.   [DIR_LEFT   ] = { [TURN_LEFT] = DIR_BACK   , [TURN_RIGHT] = DIR_FORWARD }}
  157. local OPPOSIT_DIR = {DIR_BACK, DIR_LEFT, DIR_FORWARD, DIR_LEFT}
  158. -- ---------------
  159. -- findTarget
  160. -- ---------------
  161. local TargetFinder = {
  162.   iterator = function(width, depth)
  163.     findTarget = function(scanX, scanZ, reverse, rangeWidth, rangeDepth)
  164.       local TILE_PATTERN = {"*","b","r","l","f"}
  165.       local SHAPE_DIRECTION = { DIR_NOT, DIR_FORWARD, DIR_LEFT,
  166.                                 DIR_RIGHT, DIR_BACK }
  167.       local idx = ( (scanZ%5) + (scanX%5)*3 )%5 + 1
  168.       local dirChar = TILE_PATTERN[idx]
  169.       if (not reverse and "b" == dirChar) or
  170.          (    reverse and "f" == dirChar) then
  171.         return false
  172.       end
  173.       local DIR_TO_CENTER = {{ dx =  0, dz =  0 }, -- *
  174.                              { dx =  0, dz = -1 }, -- b
  175.                              { dx =  1, dz =  0 }, -- r
  176.                              { dx = -1, dz =  0 }, -- l
  177.                              { dx =  0, dz =  1 }} -- f
  178.       local isCenter = false
  179.       local found = false
  180.       local newX, newZ
  181.       local shape = {}
  182.       local shapeDbg = ""
  183.       local centerX = scanX + DIR_TO_CENTER[idx].dx
  184.       local centerZ = scanZ + DIR_TO_CENTER[idx].dz
  185.       if inRange(centerX, centerZ, rangeWidth, rangeDepth) then
  186.         found    = true
  187.         isCenter = true
  188.         newX = centerX
  189.         newZ = centerZ
  190.       else
  191.         table.insert(shape, DIR_NOT)
  192.         shapeDbg = shapeDbg .. "*"
  193.       end
  194.       local testX, testZ
  195.       for i = 2, 5 do
  196.         testX = centerX - DIR_TO_CENTER[i].dx
  197.         testZ = centerZ - DIR_TO_CENTER[i].dz
  198.         if inRange(testX, testZ, rangeWidth, rangeDepth) then
  199.           if isCenter then
  200.             table.insert(shape, SHAPE_DIRECTION[i])
  201.             shapeDbg = shapeDbg .. DIR_TO_NAME[SHAPE_DIRECTION[i]]
  202.           end
  203.           if not found then
  204.             newX = testX
  205.             newZ = testZ
  206.           end
  207.           found = true
  208.         end
  209.       end
  210.       if not found then
  211.         return false
  212.       end
  213.       return true, newX, newZ, shape
  214.     end
  215.     local xAxisIterator = function(width)
  216.       local pos = 0
  217.       local loopEnd = math.floor((width + 1) / 3)
  218.       return function()
  219.         local current = pos
  220.         pos = pos + 1
  221.         if loopEnd < current then
  222.           return nil
  223.         else
  224.           return current * 3
  225.         end
  226.       end
  227.     end
  228.     local zAxisIterator = function(x, depth)
  229.       local pos, step
  230.       if 0 == x % 2 then
  231.         pos, step = -1, 1
  232.       else
  233.         pos, step = depth, -1
  234.       end
  235.       return function()
  236.         local current = pos
  237.         pos = pos + step
  238.         if current < -1 or depth < current then
  239.           return nil
  240.         else
  241.           return current
  242.         end
  243.       end
  244.     end
  245.     local xIter = xAxisIterator(width)
  246.     local zIter = nil
  247.     local x
  248.     local previousX
  249.     local previousZ
  250.     return function()
  251.       while true do
  252.         if nil == zIter then
  253.           x = xIter()
  254.           if nil == x then
  255.             return nil
  256.           end
  257.           zIter = zAxisIterator(x, depth)
  258.         end
  259.         local z = zIter()
  260.         if nil == z then
  261.           zIter = nil
  262.         else
  263.           --return x, z
  264.           local found, newX, newZ, shape
  265.             = findTarget(x, z, 0 == x%2, width, depth)
  266.           if found and (previousX ~= newX or previousZ ~= newZ) then
  267.             previousX = newX
  268.             previousZ = newZ
  269.             return newX, newZ, shape
  270.           end
  271.         end
  272.       end
  273.     end
  274.   end
  275. }
  276. -- ---------------
  277. -- mining control
  278. -- ---------------
  279. MOVE_H_TOP    = "MOVE_H_TOP"
  280. MOVE_V_DOWN   = "MOVE_V_DOWN"
  281. MOVE_H_BOTTOM = "MOVE_H_BOTTOM"
  282. MOVE_V_UP     = "MOVE_V_UP"
  283. local mineCtrl = {
  284.   status,
  285.   RANGE_WIDTH,
  286.   RANGE_DEPTH,
  287.   BOTTOM_HEIGHT,
  288.   newX,
  289.   newZ,
  290.   lastX = -1,
  291.   lastZ = -1,
  292.  
  293.   pointBeforeX,
  294.   pointBeforeZ,
  295.   turtleIsTop = true,
  296.   turtleInitialY = 0,
  297.   interruptX,
  298.   interruptZ,
  299.   interruptY,
  300.   vacantChestList = { },
  301.   occupiedChestList = { },
  302.   underInterruption = false,
  303. }
  304. -- ---------------
  305. -- turtle
  306. -- ---------------
  307. local MyTurtle = {}
  308. MyTurtle.new = function(initialY)
  309.   local obj = {}
  310.   obj.turtleX = 0
  311.   obj.turtleZ = 0
  312.   obj.turtleY = initialY
  313.   obj.turtleDir = DIR_FORWARD
  314.   return setmetatable(obj, {__index = MyTurtle})
  315. end
  316. MyTurtle.turnTo = function(self, direction)
  317.   local turn = TURN_RULES[self.turtleDir][direction]
  318.   if TURN_LEFT == turn then
  319.     if not SIMULATION then
  320.       turtle.turnLeft()
  321.     end
  322.   elseif TURN_RIGHT == turn then
  323.     if not SIMULATION then
  324.       turtle.turnRight()
  325.     end
  326.   elseif TURN_OPPOSIT == turn then
  327.     if not SIMULATION then
  328.       turtle.turnLeft()
  329.       turtle.turnLeft()
  330.     end
  331.   end
  332.   self.turtleDir = direction
  333. end
  334. MyTurtle.runEventHandlers = function(self, evHandlers)
  335.   if nil == evHandlers then
  336.     return
  337.   end
  338.   for _, func in ipairs(evHandlers) do
  339.     func()
  340.   end
  341. end
  342. MyTurtle.adjustX = function(self, aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
  343.   if self.turtleX < aimX and DIR_RIGHT ~= self.turtleDir then
  344.     self:turnTo(DIR_RIGHT)
  345.   elseif aimX < self.turtleX and DIR_LEFT ~= self.turtleDir then
  346.     self:turnTo(DIR_LEFT)
  347.   end
  348.   while self.turtleX ~= aimX do
  349.     if not SIMULATION then
  350.       self:runEventHandlers(beforeEvHandlers)
  351.       surelyFwd()
  352.       self:runEventHandlers(afterEvHandlers)
  353.       if withDigUp then
  354.         surelyDigUp()
  355.       end
  356.     end
  357.     self.turtleX = self.turtleX + sign(aimX - self.turtleX)
  358.   end
  359. end
  360. MyTurtle.adjustZ = function(self, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  361.   if self.turtleZ < aimZ and DIR_FORWARD ~= self.turtleDir then
  362.     self:turnTo(DIR_FORWARD)
  363.   elseif aimZ < self.turtleZ and DIR_BACK ~= self.turtleDir then
  364.     self:turnTo(DIR_BACK)
  365.   end
  366.   while self.turtleZ ~= aimZ do
  367.     if not SIMULATION then
  368.       self:runEventHandlers(beforeEvHandlers)
  369.       surelyFwd()
  370.       self:runEventHandlers(afterEvHandlers)
  371.       if withDigUp then
  372.         surelyDigUp()
  373.       end
  374.     end
  375.     self.turtleZ = self.turtleZ + sign(aimZ - self.turtleZ)
  376.   end
  377. end
  378. MyTurtle.moveTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  379.   self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
  380.   self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  381. end
  382. MyTurtle.backTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  383.   self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  384.   self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
  385. end
  386. MyTurtle.moveVertical = function(self, targetY, beforeEvHandlers, afterEvHandlers)
  387.   local vDir = sign(targetY - self.turtleY)
  388.   while targetY ~= self.turtleY do
  389.     if not SIMULATION then
  390.       if 1 == vDir then
  391.         self:runEventHandlers(beforeEvHandlers)
  392.         turtle.digUp()
  393.         surelyUp()
  394.         self.turtleY = self.turtleY + vDir
  395.         self:runEventHandlers(afterEvHandlers)
  396.       elseif -1 == vDir then
  397.         self:runEventHandlers(beforeEvHandlers)
  398.         turtle.digDown()
  399.         surelyDown()
  400.         self.turtleY = self.turtleY + vDir
  401.         self:runEventHandlers(afterEvHandlers)
  402.       end
  403.     end
  404.   end
  405. end
  406. -- ---------------
  407. -- additional action : vertical mining
  408. -- ---------------
  409. local function verticalMiningAction(_myT, _shape)
  410.   local turnToNearestEdge = function (myT, shape)
  411.     local turnMinCost = 99
  412.     local smallestTurnDir = DIR_NOT
  413.     local turnTmp, costTmp
  414.     for _, dir in ipairs(shape) do
  415.       turnTmp = TURN_RULES[myT.turtleDir][dir]
  416.       costTmp = TURN_COSTS[turnTmp]
  417.       if costTmp < turnMinCost then
  418.         turnMinCost = costTmp
  419.         smallestTurnDir = dir
  420.       end
  421.     end
  422.     myT:turnTo(smallestTurnDir)
  423.   end
  424.   local getTurnFuncName = function(myT, shape)
  425.     local dirWhenTurnLeft  = TURN_RELATIVE[myT.turtleDir][TURN_LEFT ]
  426.     local dirWhenTurnRight = TURN_RELATIVE[myT.turtleDir][TURN_RIGHT]
  427.     for _, dir in ipairs(shape) do
  428.       if dir == dirWhenTurnLeft then
  429.         return TURN_LEFT
  430.       elseif dir == dirWhenTurnRight then
  431.         return TURN_RIGHT
  432.       end
  433.     end
  434.   end
  435.   local digValuableBlockOnly = function()
  436.     local selSlot = 16
  437.     local checkedSlot = -1
  438.     for _, slot in ipairs(COMPARE_BLOCKS) do
  439.       if selSlot == slot then
  440.         if turtle.compare() then
  441.           return
  442.         end
  443.         checkedSlot = slot
  444.         break
  445.       end
  446.     end
  447.     for _, slot in ipairs(COMPARE_BLOCKS) do
  448.       if checkedSlot ~= slot then
  449.         turtle.select(slot)
  450.         if turtle.compare() then
  451.           return
  452.         end
  453.       end
  454.     end
  455.     turtle.dig()
  456.     return true
  457.   end
  458.   local myT = _myT
  459.   local shape = _shape
  460.   local firstFlg = true
  461.   local turnLR
  462.   return function()
  463.     if firstFlg then
  464.       firstFlg = not firstFlg
  465.       if 2 == #shape or 3 == #shape then
  466.         turnToNearestEdge(myT, shape)
  467.         turnLR = getTurnFuncName(myT, shape)
  468.       else
  469.         turnLR = TURN_RIGHT
  470.       end
  471.     end
  472.     if 1 < #shape then
  473.       for i = 1, #shape do
  474.         if not SIMULATION then
  475.           if EXCAVATE_ALL then
  476.             surelyDig()
  477.             dug = true
  478.           else
  479.             dug = digValuableBlockOnly()
  480.           end
  481.         end
  482.         if i < #shape then
  483.           myT:turnTo(TURN_RELATIVE[myT.turtleDir][turnLR])
  484.         end
  485.       end
  486.       if 2 == #shape or 3 == #shape then
  487.         if TURN_LEFT == turnLR then
  488.           turnLR = TURN_RIGHT
  489.         else
  490.           turnLR = TURN_LEFT
  491.         end
  492.       end
  493.     end
  494.   end
  495. end
  496. -- ---------------
  497. -- additional action : inventory check
  498. -- ---------------
  499. local function checkInventoryAction(_myT, _mineCtrl)
  500.   local function dropUselessBlocks()
  501.     local cnt
  502.     for _, v in ipairs(SLOTS_KEEP_1) do
  503.       cnt = turtle.getItemCount(v)
  504.       if 1 < cnt then
  505.         turtle.select(v)
  506.         turtle.dropDown(cnt - 1)
  507.       end
  508.     end
  509.     for _, v in ipairs(SLOTS_KEEP_32) do
  510.       cnt = turtle.getItemCount(v)
  511.       if 32 < cnt then
  512.         turtle.select(v)
  513.         turtle.dropDown(cnt - 32)
  514.       end
  515.     end
  516.     local freeCnt = 0
  517.     for _, v in ipairs(SLOTS_FREE) do
  518.       cnt = turtle.getItemCount(v)
  519.       if 0 < cnt then
  520.         turtle.select(v)
  521.         for _, w in ipairs(SLOTS_USELESS_SAMPLE) do
  522.           if turtle.compareTo(w) then
  523.             turtle.dropDown()
  524.             freeCnt = freeCnt + 1
  525.             break
  526.           end
  527.         end
  528.       else
  529.         freeCnt = freeCnt + 1
  530.       end
  531.     end
  532.     turtle.select(1)
  533.     debug("==== dropUselessBlocks() makes " .. tostring(freeCnt) .." vacantSlot(s)")
  534.     return 0 < freeCnt
  535.   end
  536.   local function checkInventory()
  537.     for _, i in ipairs(SLOTS_FREE) do
  538.       if 0 == turtle.getItemCount(i) then
  539.         return true
  540.       end
  541.     end
  542.     return false
  543.   end
  544.   local function dump()
  545.     surelyDigUp()
  546.     surelyUp()
  547.     turtle.select(SLOT_CHEST)
  548.     turtle.placeUp()
  549.     for _, v in ipairs(SLOTS_FREE) do
  550.       cnt = turtle.getItemCount(v)
  551.       if 0 < cnt then
  552.         turtle.select(v)
  553.         if KEEP_USELESS_BLOCKS then
  554.           turtle.dropUp()
  555.         else
  556.           for _, w in ipairs(SLOTS_USELESS_SAMPLE) do
  557.             if not turtle.compareTo(w) then
  558.               turtle.dropUp()
  559.             end
  560.             break
  561.           end
  562.         end
  563.       end
  564.     end
  565.     turtle.select(1)
  566.     turtle.digDown()
  567.     surelyDown()
  568.   end
  569.   local function backToTheGround(myT, mineCtrl)
  570.     debug("====== backToTheGround()")
  571.     debug("======   status    = " .. mineCtrl.status)
  572.     debug("======   pos x,z,y = " .. myT.turtleX .. ", " .. myT.turtleZ .. ", " .. myT.turtleY)
  573.     debug("======   last x,z  = " .. mineCtrl.lastX .. ", " .. mineCtrl.lastZ)
  574.     local t2 = MyTurtle.new()
  575.     t2.turtleX = myT.turtleX
  576.     t2.turtleZ = myT.turtleZ
  577.     t2.turtleY = myT.turtleY
  578.     t2.turtleDir = myT.turtleDir
  579.     if MOVE_H_TOP == mineCtrl.status then
  580.       debug("<<<>>> backToTheGround [MOVE_H_TOP] begin")
  581.       t2:backTo(mineCtrl.lastX, mineCtrl.lastZ, nil, nil)
  582.       debug("<<<>>> backToTheGround [MOVE_H_TOP] end")
  583.     elseif MOVE_V_DOWN == mineCtrl.status then
  584.       debug("<<<>>> backToTheGround [MOVE_V_DOWN] begin")
  585.       t2:moveVertical(mineCtrl.turtleInitialY, nil, nil)
  586.       debug("<<<>>> backToTheGround [MOVE_V_DOWN] end")
  587.  
  588.     elseif MOVE_H_BOTTOM == mineCtrl.status then
  589.       debug("<<<>>> backToTheGround [MOVE_H_BOTTOM] begin")
  590.       t2:backTo(mineCtrl.lastX, mineCtrl.lastZ, nil, nil)
  591.       t2:moveVertical(mineCtrl.turtleInitialY, nil, nil)
  592.       debug("<<<>>> backToTheGround [MOVE_H_BOTTOM] end")
  593.  
  594.     elseif MOVE_V_UP == mineCtrl.status then
  595.       debug("<<<>>> backToTheGround [MOVE_V_UP] begin")
  596.       t2:moveVertical(mineCtrl.BOTTOM_HEIGHT, nil, nil)
  597.       t2:backTo(mineCtrl.lastX, mineCtrl.lastZ, nil, nil)
  598.       t2:moveVertical(mineCtrl.turtleInitialY, nil, nil)
  599.       debug("<<<>>> backToTheGround [MOVE_V_UP] end")
  600.     end
  601.     dump()
  602.     if MOVE_H_TOP == mineCtrl.status then
  603.       t2:moveTo(myT.turtleX, myT.turtleZ)
  604.  
  605.     elseif MOVE_V_DOWN == mineCtrl.status then
  606.       t2:moveVertical(myT.turtleY, nil, nil)
  607.  
  608.     elseif MOVE_H_BOTTOM == mineCtrl.status then
  609.       t2:moveVertical(mineCtrl.BOTTOM_HEIGHT, nil, nil)
  610.       t2:moveTo(myT.turtleX, myT.turtleZ)
  611.  
  612.     elseif MOVE_V_UP == mineCtrl.status then
  613.       t2:moveVertical(mineCtrl.BOTTOM_HEIGHT, nil, nil)
  614.       t2:moveTo(myT.turtleX, myT.turtleZ)
  615.       t2:moveVertical(myT.turtleY, nil, nil)
  616.     end
  617.     t2:turnTo(myT.turtleDir)
  618.  
  619.     debug("<<<>>>  compreted?")
  620.     debug("  <<<>>>  myT x,z,y = " .. myT.turtleX .. ", " .. myT.turtleZ .. ", " .. myT.turtleY)
  621.     debug("  <<<>>>  t2  x,z,y = " .. t2.turtleX .. ", " .. t2.turtleZ .. ", " .. t2.turtleY)
  622.   end
  623.   return function()
  624.     if not checkInventory() then
  625.       debug("====================================")
  626.       debug("== checkInventory() returns false")
  627.       debug("==   at x : " .. tostring(_myT.turtleX) ..
  628.                  " / z : " .. tostring(_myT.turtleZ) ..
  629.                  " / y : " .. tostring(_myT.turtleY))
  630.       debug("====================================")
  631.       local result = false
  632.       if not KEEP_USELESS_BLOCKS then
  633.         result = dropUselessBlocks()
  634.       end
  635.       if not result then
  636.         backToTheGround(_myT, _mineCtrl)
  637.       end
  638.     end
  639.   end
  640. end
  641. -- ---------------
  642. -- initialize
  643. -- ---------------
  644. local function initialize()
  645.   if SHOW_USAGE then
  646.     showUsage()
  647.     print("Hit any key when ready.")
  648.     read()
  649.   end
  650.   if ASK_INITIAL_HEIGHT then
  651.     print("Input current y")
  652.     term.write(">")
  653.     local ch = read()
  654.     if nil == tonumber(ch) then
  655.       error("Not a Number!")
  656.     end
  657.     mineCtrl.turtleInitialY = tonumber(ch)
  658.   else
  659.     mineCtrl.turtleInitialY = TURTLE_INITIAL_Y
  660.   end
  661.   mineCtrl.RANGE_WIDTH = DEFAULT_WIDTH
  662.   mineCtrl.RANGE_DEPTH = DEFAULT_DEPTH
  663.  
  664.   if HEIGHT_MODE then
  665.     -- true  : use BEADROCK_HEIGHT
  666.     mineCtrl.BOTTOM_HEIGHT = BEADROCK_HEIGHT
  667.   else
  668.     -- false : use RELATIVE_HEIGHT
  669.     mineCtrl.BOTTOM_HEIGHT = mineCtrl.turtleInitialY - RELATIVE_HEIGHT
  670.   end
  671. end
  672. -- -------------------------------
  673. -- main
  674. -- -------------------------------
  675. debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
  676. debug("@@ chunkDig4.1.lua")
  677. debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
  678. initialize()
  679. local myT = MyTurtle.new(mineCtrl.turtleInitialY)
  680. local beforeEvHandlers = { }
  681. local afterEvHandlers = { }
  682. local checkInventoryA = checkInventoryAction(myT, mineCtrl)
  683. local newX, newZ
  684. mineCtrl.lastX = -1
  685. mineCtrl.lastZ = -1
  686.  
  687. mineCtrl.status = MOVE_H_TOP
  688. for newX, newZ, shape in TargetFinder.iterator(mineCtrl.RANGE_WIDTH, mineCtrl.RANGE_DEPTH) do
  689.   mineCtrl.newX = newX
  690.   mineCtrl.newZ = newZ
  691.   if -1 == mineCtrl.lastX then
  692.     mineCtrl.lastX = mineCtrl.newX
  693.     mineCtrl.lastZ = mineCtrl.newZ
  694.   end
  695.   debug("=[for loop]====================")
  696.   debug("==  newX,  newZ = " .. tostring(mineCtrl.newX) .. ", " .. tostring(mineCtrl.newZ))
  697.   debug("== lastX, lastZ = " .. tostring(mineCtrl.lastX) .. ", " .. tostring(mineCtrl.lastZ))
  698.   debug("===============================")
  699.   myT:moveTo(mineCtrl.newX, mineCtrl.newZ, {}, { checkInventoryA })
  700.   if MOVE_H_TOP == mineCtrl.status then
  701.  
  702.     mineCtrl.status = MOVE_V_DOWN
  703.     beforeEvHandlers = { }
  704.     afterEvHandlers  = { verticalMiningAction(myT, shape),
  705.                          checkInventoryA }
  706.     myT:moveVertical(mineCtrl.BOTTOM_HEIGHT,
  707.       beforeEvHandlers, afterEvHandlers)
  708.  
  709.     mineCtrl.status = MOVE_H_BOTTOM
  710.   elseif MOVE_H_BOTTOM == mineCtrl.status then
  711.  
  712.     mineCtrl.status = MOVE_V_UP
  713.     beforeEvHandlers = { verticalMiningAction(myT, shape),
  714.                          checkInventoryA }
  715.     afterEvHandlers  = { }
  716.     myT:moveVertical(mineCtrl.turtleInitialY,
  717.       beforeEvHandlers, afterEvHandlers)
  718.  
  719.     mineCtrl.status = MOVE_H_TOP
  720.   else
  721.     error("unexpected condition.")
  722.   end
  723.   mineCtrl.lastX = mineCtrl.newX
  724.   mineCtrl.lastZ = mineCtrl.newZ
  725. end
  726. myT:backTo(0, 0)
  727. myT:turnTo(DIR_FORWARD)
  728. print("chunkDig4.1.lua was compreted!!")
  729. debug("chunkDig4.1.lua was compreted!!")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top