Advertisement
kssr3951

chunkDig6

Apr 28th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.12 KB | None | 0 0
  1. -- --------
  2. -- chunkDig6
  3. -- --------
  4. local function showUsage()
  5.   print("chunkDig6")
  6.   --print("slot1 : torch x 64")
  7.   --print("slot2 : wood  x  1")
  8. end
  9.  
  10. local INITIAL_HEIGHT = 58
  11. local BOTTOM_HEIGHT  = 54 --5
  12. local WIDTH          = 10
  13. local DEPTH          = 10
  14.  
  15. local SLOT_TORCH       = 1
  16. local SLOT_CHEST       = 2
  17. local SLOT_COBBLESTONE = 3
  18. local SLOT_STONE       = 4
  19. local SLOT_GRAVEL      = 5
  20. local SLOT_DART        = 6
  21. local SLOTS_FREE       = { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
  22.  
  23. local SLOTS_USELESS_SAMPLE = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART, SLOT_COBBLESTONE }
  24. local SLOTS_KEEP_32        = { SLOT_COBBLESTONE }
  25. local SLOTS_KEEP_1         = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
  26.  
  27. -- ---------------
  28. -- turtle position and direction
  29. -- ---------------
  30. local turtlePos = { x = 0, y = 0, z = 0, dir = 0, mineMove = {} }
  31. local dzWhenFwd = {[0]=1, [1]=0, [2]=-1, [3]= 0}
  32. local dxWhenFwd = {[0]=0, [1]=1, [2]= 0, [3]=-1}
  33. local moveLog = nil
  34. -- ---------------
  35. -- Move/Dig Functions
  36. -- ---------------
  37. function surelyUp()
  38.   while not turtle.up() do turtle.digUp() end
  39.   turtlePos.y = turtlePos.y + 1
  40.   if nil ~= moveLog then
  41.     table.insert(moveLog, "u")
  42.   end
  43. end
  44. function surelyDown()
  45.   while not turtle.down() do turtle.digDown() end
  46.   turtlePos.y = turtlePos.y - 1
  47.   if nil ~= moveLog then
  48.     table.insert(moveLog, "d")
  49.   end
  50. end
  51. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  52. function surelyDigUp()
  53.   while turtle.digUp() do
  54.     os.sleep(0.4)
  55.   end
  56. end
  57. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  58. function surelyDig()
  59.   while turtle.dig() do end
  60. end
  61. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  62. function surelyFwd()
  63.   for i=1,4 do
  64.     local status, err = turtle.forward()
  65.     if status then
  66.       turtlePos.x = turtlePos.x + dxWhenFwd[turtlePos.dir]
  67.       turtlePos.z = turtlePos.z + dzWhenFwd[turtlePos.dir]
  68.       if nil ~= moveLog then
  69.         table.insert(moveLog, "f")
  70.       end
  71.       return true  -- success!
  72.     elseif err=="Out of fuel" then
  73.       return status, err
  74.     end
  75.  
  76.     surelyDig() -- face to a normal block or a sand(gravel) hill
  77.  
  78.     if turtle.detect() and not turtle.dig() then
  79.       return false, "bedrock!"
  80.     end
  81.  
  82.     if turtle.forward() then
  83.       turtlePos.x = turtlePos.x + dxWhenFwd[turtlePos.dir]
  84.       turtlePos.z = turtlePos.z + dzWhenFwd[turtlePos.dir]
  85.       if nil ~= moveLog then
  86.         table.insert(moveLog, "f")
  87.       end
  88.       return true
  89.     end -- success!
  90.  
  91.     if turtle.attack() then
  92.       -- face to monster-mob
  93.       while turtle.attack() do end
  94.     else
  95.       -- face to sand-blocks which is dropping long time
  96.       os.sleep(5) -- probably, adjustment is required
  97.     end
  98.   end
  99.   return turtle.forward()
  100. end
  101. -- --------
  102. --
  103. -- --------
  104. local f = surelyFwd -- function() while not turtle.forward() do end end
  105. --local b = function() while not turtle.back() do end end
  106. local l = function()
  107.               turtle.turnLeft()
  108.               turtlePos.dir = (turtlePos.dir + 4 - 1) % 4
  109.               if nil ~= moveLog then
  110.                 table.insert(moveLog, "l")
  111.               end
  112.             end
  113. local r = function()
  114.               turtle.turnRight()
  115.               turtlePos.dir = (turtlePos.dir + 4 + 1) % 4
  116.               if nil ~= moveLog then
  117.                 table.insert(moveLog, "r")
  118.               end
  119.             end
  120. local u = surelyUp
  121. local d = surelyDown
  122. local F = surelyFwd
  123. local U = surelyUp
  124. local D = surelyDown
  125. local e0 = surelyDigUp
  126. local e1 = surelyDig
  127. local e2 = turtle.digDown -- excavate
  128. local p2 = turtle.placeDown
  129. local sel = turtle.select
  130. --local sSapling = function() turtle.select(SLOT_1_SAPLING) end
  131. --local sWood    = function() turtle.select(SLOT_2_WOOD) end
  132. function vacuum(slotNo)
  133.   local result = false
  134.   turtle.select(slotNo)
  135.   for i = 1, 16 do
  136.     if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
  137.       turtle.select(i)
  138.       turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
  139.       result = true
  140.       if 0 == turtle.getItemSpace(slotNo) then
  141.         break
  142.       end
  143.       turtle.select(slotNo)
  144.     end
  145.   end
  146.   turtle.select(slotNo)
  147.   return result
  148. end
  149. local function makeVacuumFunc(slotNo)
  150.   return function()
  151.       if 1 == turtle.getItemCount(slotNo) then
  152.         vacuum(slotNo)
  153.       end
  154.     end
  155. end
  156. local function getTotal(slotNo)
  157.   local cnt = turtle.getItemCount(slotNo)
  158.   turtle.select(slotNo)
  159.   for i=1,16 do
  160.     if i ~= slotNo and turtle.compareTo(i) then
  161.       cnt = cnt + turtle.getItemCount(i)
  162.     end
  163.   end
  164.   return cnt
  165. end
  166. local function dropDownExceptK(slotNo, dropCnt, keepCnt)
  167.   local function slotAction(i, mustKeep, mustDrop, slotNo)
  168.     local cnt = turtle.getItemCount(i)
  169.     if mustKeep < cnt then
  170.       local dropAble = cnt - mustKeep
  171.       local nowDrop = math.min(dropAble, mustDrop)
  172.       mustKeep = 0
  173.       mustDrop = mustDrop - nowDrop
  174.       turtle.select(i)
  175.       turtle.dropDown(nowDrop)
  176.       turtle.select(slotNo)
  177.     else
  178.       mustKeep = mustKeep - cnt
  179.     end
  180.   end
  181.   turtle.select(slotNo)
  182.   local mustKeep = keepCnt
  183.   local mustDrop = dropCnt
  184.   for i=1,16 do
  185.     if i ~= slotNo and turtle.compareTo(i) then
  186.       slotAction(i, mustKeep, mustDrop, slotNo)
  187.     end
  188.   end
  189.   vacuum(slotNo)
  190.   slotAction(slotNo, mustKeep, mustDrop, slotNo)
  191. end
  192. local function dropDownExcept1(slotNo)
  193.   turtle.select(slotNo)
  194.   turtle.dropDown(turtle.getItemCount()-1)
  195.   for i=1,16 do
  196.     if i ~= slotNo and turtle.compareTo(i) then
  197.       turtle.select(i)
  198.       turtle.dropDown()
  199.       turtle.select(slotNo)
  200.     end
  201.   end
  202.   vacuum(slotNo)
  203. end
  204. local function dropDownHalf(slotNo)
  205.   turtle.select(slotNo)
  206.   local c = turtle.getItemCount(slotNo)
  207.   turtle.dropDown((c-1)/2)
  208.   for i=1,16 do
  209.     if i~=slotNo and turtle.compareTo(i) then
  210.       turtle.select(i)
  211.       c = turtle.getItemCount(i)
  212.       turtle.dropDown(c/2)
  213.       turtle.select(slotNo)
  214.     end
  215.   end
  216. end
  217. local function dropDownUselessBlocks()
  218.   local cnt
  219.   for _, v in ipairs(SLOTS_KEEP_1) do
  220.     cnt = turtle.getItemCount(v)
  221.     if 1 < cnt then
  222.       turtle.select(v)
  223.       turtle.dropDown(cnt - 1)
  224.     end
  225.   end
  226.   for _, v in ipairs(SLOTS_KEEP_32) do
  227.     cnt = turtle.getItemCount(v)
  228.     if 32 < cnt then
  229.       turtle.select(v)
  230.       turtle.dropDown(cnt - 32)
  231.     end
  232.   end
  233.   for _, v in ipairs(SLOTS_FREE) do
  234.     cnt = turtle.getItemCount(v)
  235.     if 0 < cnt then
  236.       turtle.select(v)
  237.       for _, w in ipairs(SLOTS_USELESS_SAMPLE) do
  238.         if turtle.compareTo(w) then
  239.           turtle.dropDown()
  240.           break
  241.         end
  242.       end
  243.     end
  244.   end
  245.   turtle.select(1)
  246. end
  247. -- ---------------
  248. -- refuelPrompt
  249. -- ---------------
  250. function refuelPrompt(moveFunc)
  251.   local firstTime = true;
  252.   while true do
  253.     local rslt, msg;
  254.     if nil ~= moveFunc then
  255.       rslt, msg = moveFunc();
  256.     else
  257.       if firstTime then
  258.         firstTime = false;
  259.         rslt = false;
  260.         msg  = "Out of fuel";
  261.       else
  262.         return true;
  263.       end
  264.     end
  265.     if not rslt and msg == "Out of fuel" then
  266.       print("Out of fuel. please select [r]efuel or [q]uit.");
  267.       print("(r/q)");
  268.       local ch = read();
  269.       if "q" == ch then
  270.         print("Are you sure you want to stop the script?");
  271.         print("(y/n)");
  272.         ch = read();
  273.         if "y" == ch then
  274.           error("out of fuel.");
  275.         end
  276.       elseif "r" == ch then
  277.         local selSlot = 1;
  278.         while true do
  279.           print("Please type current selected slot number.");
  280.           ch = read();
  281.           local num = tonumber(ch);
  282.           if nil ~= num and 1 <= num and num <= 16 then
  283.             selSlot = num;
  284.             break;
  285.           else
  286.             print("Wrong input. Please retry.");
  287.           end
  288.         end
  289.         while true do
  290.           print("please set fuel item and type slot number.");
  291.           print("When you finish refueling, Please type q.");
  292.           print("FuelLevel = " .. turtle.getFuelLevel());
  293.           ch = read();
  294.           local num = tonumber(ch);
  295.           if nil ~= num and 1 <= num and num <= 16 then
  296.             turtle.select(num);
  297.             local fRslt, fMsg = turtle.refuel();
  298.             if not fRslt then
  299.               print(fMsg);
  300.             end
  301.           elseif "q" == ch then
  302.             turtle.select(selSlot);
  303.             break;
  304.           else
  305.             print("Wrong input. Please retry.");
  306.           end
  307.         end
  308.       end
  309.     else
  310.       return rslt, msg;
  311.     end
  312.   end
  313. end
  314.  
  315. local function tableInsertAll(tbl, list)
  316.   for _, v in ipairs(list) do
  317.     table.insert(tbl, v)
  318.   end
  319. end
  320. local TAG_ANOTHER_WHEN_LAST = "anotherWhenLast"
  321. local TAG_SWITCH_ODD_EVEN = "switchOddEven"
  322. local function toFlatList(list)
  323.   local rslt = { }
  324.   if 3 == #list and "string" == type(list[1])
  325.     and TAG_ANOTHER_WHEN_LAST == list[1] then
  326.     table.insert(rslt, list)
  327.   elseif 3 == #list and "string" == type(list[1])
  328.     and TAG_SWITCH_ODD_EVEN == list[1] then
  329.     table.insert(rslt, list)
  330.   else
  331.     for i, v in ipairs(list) do
  332.       if "function" == type(v) then
  333.         table.insert(rslt, v)
  334.       elseif "table" == type(v) then
  335.         tableInsertAll(rslt, toFlatList(v))
  336.       end
  337.     end
  338.   end
  339.   return rslt
  340. end
  341. local function _breakWhenLast_()
  342. end
  343. local function _anotherWhenLast_(ordinaryList, anotherList)
  344.   return { TAG_ANOTHER_WHEN_LAST, ordinaryList, anotherList }
  345. end
  346. local function _switchOddEven_(firstList, secondList)
  347.   return { TAG_SWITCH_ODD_EVEN, firstList, secondList }
  348. end
  349. local breakLast = _breakWhenLast_
  350. local anotherLast = _anotherWhenLast_
  351. local swOddEven = _switchOddEven_
  352. local function rep(num, ...)
  353.   local function tableInsertAllForRep(rslt, list, i, num)
  354.     for j, v in ipairs(list) do
  355.       if _breakWhenLast_ == v then
  356.         if i == num then
  357.           break
  358.         end
  359.       elseif "table" == type(v) and TAG_ANOTHER_WHEN_LAST == v[1] then
  360.         if i ~= num then
  361.           tableInsertAllForRep(rslt, toFlatList(v[2]), i, num)
  362.         else
  363.           tableInsertAllForRep(rslt, toFlatList(v[3]), i, num)
  364.         end
  365.       elseif "table" == type(v) and TAG_SWITCH_ODD_EVEN == v[1] then
  366.         if i % 2 == 1 then
  367.           tableInsertAllForRep(rslt, toFlatList(v[2]), i, num)
  368.         else
  369.           tableInsertAllForRep(rslt, toFlatList(v[3]), i, num)
  370.         end
  371.       else
  372.         table.insert(rslt, v)
  373.       end
  374.     end
  375.   end
  376.   local list = toFlatList({...})
  377.   local rslt = {}
  378.   for i = 1, num do
  379.     tableInsertAllForRep(rslt, list, i, num)
  380.   end
  381.   return rslt
  382. end
  383. local function exec(...)
  384.   for i, func in ipairs(toFlatList({...})) do
  385.     func()
  386.   end
  387. end
  388. -- --------
  389. -- main
  390. -- --------
  391. local throughFlag = false
  392. local isTopHeight = true
  393. local function switchTopBottom()
  394.   isTopHeight = not isTopHeight
  395. end
  396. local function returnToTopHeight()
  397.   if not isTopHeight then
  398.     exec(rep(3,e0,u))
  399.     isTopHeight = true
  400.   end
  401. end
  402. local function e1WorthOnly()
  403.   local worth = true
  404.   for i, v in ipairs(SLOTS_USELESS_SAMPLE) do
  405.     turtle.select(v)
  406.     if turtle.compare() then
  407.       worth = false
  408.       break
  409.     end
  410.   end
  411.   turtle.select(1)
  412.   if worth then
  413.     e1()
  414.   end
  415. end
  416. local function checkInventory()
  417.   for i = 16,1,-1 do
  418.     if 0 == turtle.getItemCount(i) then
  419.       return true
  420.     end
  421.   end
  422.   return false
  423. end
  424. local function returnAction()
  425.   turtle.turnLeft()
  426.   turtle.turnLeft()
  427.   moveLog = nil
  428.   local len = #turtlePos.mineMove
  429.   for i = len, 1, -1 do
  430.     local ch = turtlePos.mineMove[i]
  431.     if "f" == ch then
  432.       surelyFwd()
  433.     elseif "u" == ch then
  434.       surelyDown()
  435.     elseif "d" == ch then
  436.       surelyUp()
  437.     elseif "l" == ch then
  438.       turtle.turnRight()
  439.     elseif "r" == ch then
  440.       turtle.turnLeft()
  441.     end
  442.   end
  443.   if turtle.getFuelLevel() < 1000 then
  444.     refuelPrompt()
  445.   end
  446.   print("inventory is full.")
  447.   print("take items and hit enter key.")
  448.   io.read()
  449.   turtle.turnLeft()
  450.   turtle.turnLeft()
  451.   for i = 1, len do
  452.     local ch = turtlePos.mineMove[i]
  453.     if "f" == ch then
  454.       surelyFwd()
  455.     elseif "u" == ch then
  456.       surelyUp()
  457.     elseif "d" == ch then
  458.       surelyDown()
  459.     elseif "l" == ch then
  460.       turtle.turnLeft()
  461.     elseif "r" == ch then
  462.       turtle.turnRight()
  463.     end
  464.   end
  465.   moveLog = turtlePos.mineMove
  466. end
  467. local function checkReturn()
  468.   if turtle.getFuelLevel() < 1000 then
  469.     returnAction()
  470.   end
  471.   if false == checkInventory() then
  472.     dropDownUselessBlocks()
  473.     if false == checkInventory() then
  474.       returnAction()
  475.     end
  476.   end
  477. end
  478. local function keepAndPlace(slotNo, placeFunc)
  479.   if 1 == turtle.getItemCount(slotNo) then
  480.     vacuum(slotNo)
  481.   end
  482.   if 2 <= turtle.getItemCount(slotNo) then
  483.     turtle.select(slotNo)
  484.     placeFunc()
  485.   end
  486. end
  487. local function makeUpperRoom(torchFlg)
  488.   exec(u,u,u)
  489.   surelyDigUp()
  490.   keepAndPlace(SLOT_COBBLESTONE, turtle.placeUp)
  491.   surelyDig()
  492.   keepAndPlace(SLOT_COBBLESTONE, turtle.place)
  493.   surelyDown()
  494.   if torchFlg then
  495.     keepAndPlace(SLOT_TORCH, turtle.placeUp)
  496.   end
  497.   surelyDig()
  498.   surelyDown()
  499.   surelyDig()
  500.   surelyDown()
  501.   surelyDig()
  502. end
  503. local function fallPreventionDown()
  504.   if turtlePos.y == -1 then
  505.     keepAndPlace(SLOT_COBBLESTONE, turtle.place)
  506.   elseif turtlePos.y == -2 then
  507.     keepAndPlace(SLOT_COBBLESTONE, turtle.placeUp)
  508.   end
  509. end
  510. local function fallPreventionUp()
  511.   if turtlePos.y == -1 then
  512.     keepAndPlace(SLOT_COBBLESTONE, turtle.place)
  513.   elseif turtlePos.y == 0 then
  514.     keepAndPlace(SLOT_COBBLESTONE, turtle.placeDown)
  515.   end
  516. end
  517. local function miningAction()
  518.   level = INITIAL_HEIGHT - BOTTOM_HEIGHT
  519.   if throughFlag then
  520.     throughFlag = not throughFlag
  521.   else
  522.     throughFlag = not throughFlag
  523.     if isTopHeight then
  524.       makeUpperRoom(true)
  525.       turtlePos.mineMove = {}
  526.       moveLog = turtlePos.mineMove
  527.       exec(rep(level,checkReturn,fallPreventionDown,e2,d,checkReturn,e1WorthOnly),switchTopBottom)
  528.     else
  529.       exec(rep(level,checkReturn,e0,checkReturn,
  530.                 e1WorthOnly,fallPreventionUp,u,fallPreventionUp),switchTopBottom)
  531.       makeUpperRoom(false)
  532.     end
  533.   end
  534. end
  535. while true do
  536.   exec(
  537.     rep(WIDTH,
  538.       rep(DEPTH,
  539.         breakLast,miningAction,checkReturn,e1,f
  540.       ),
  541.       anotherLast(
  542.         {
  543.           swOddEven({r},{l}),miningAction,checkReturn,e1,f,swOddEven({r},{l})
  544.         },
  545.         {
  546.           returnToTopHeight,
  547.           swOddEven(
  548.             {l,rep(WIDTH-1,f),l,rep(DEPTH-1,f),l,l},
  549.             {r,rep(WIDTH-1,f),r})
  550.         }
  551.       )
  552.     )
  553.   )
  554.   break
  555. end
  556.  
  557. --[[
  558. local SLOT_TORCH               = 1
  559. local SLOT_CHEST               = 2
  560. local SLOT_COBBLESTONE         = 3
  561. local SLOT_STONE               = 4
  562. local SLOT_GRAVEL              = 5
  563. local SLOT_DART                = 6
  564. local SLOTS_FREE           = { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
  565.  
  566. local SLOTS_USELESS_SAMPLE = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART, SLOT_COBBLESTONE }
  567. local SLOTS_KEEP_ALL       = { SLOT_TORCH, SLOT_CHEST }
  568. local SLOTS_KEEP_32        = { SLOT_COBBLESTONE }
  569. local SLOTS_KEEP_1         = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
  570.  
  571.  
  572. local INITIAL_HEIGHT = 58
  573. local BOTTOM_HEIGHT  = 5
  574. local LOW_FUEL_LEVEL = 300
  575. local SIZE_LEFT    = 80
  576. local SIZE_FORWARD = 80
  577. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement