Advertisement
kssr3951

quickMiner

May 5th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.51 KB | None | 0 0
  1. os.loadAPI("beacon") -- pastebin get hjGie0QG beacon
  2. -- ----------------------------------------------
  3. -- Utilities
  4. -- ----------------------------------------------
  5. -- 第2引数以降には、関数や関数のリストを指定する。リスト内に更にリストが入っていても良い。
  6. -- executeFunction()に渡す際には「toFlatList()」で入れ子の無いリストにすること。
  7. function repeatList(count, ...)
  8.     local rslt = { }
  9.     for i = 1, count do
  10.         table.insert(rslt, {...})
  11.     end
  12.     return rslt
  13. end
  14. function toFlatList(...)
  15.     local rslt = { }
  16.     local list = {...}
  17.     for i = 1, #list do
  18.         if "table" == type(list[i]) then
  19.             for j = 1, #list[i] do
  20.                 if "table" == type(list[i][j]) then
  21.                     local tmp = toFlatList(list[i][j])
  22.                     for k = 1, #tmp do
  23.                         table.insert(rslt, tmp[k])
  24.                     end
  25.                 else
  26.                     table.insert(rslt, list[i][j])
  27.                 end
  28.             end
  29.         else
  30.             table.insert(rslt, list[i])
  31.         end
  32.     end
  33.     return rslt
  34. end
  35. function executeFunction(functionList)
  36.     for i = 1, #functionList do
  37.         functionList[i]()
  38.     end
  39. end
  40. function selectItem(nameAndDamage)
  41.     for i = 1, 16 do
  42.         local d = turtle.getItemDetail(i)
  43.         if nil ~= d then
  44.             if d.name == nameAndDamage.name and d.damage == nameAndDamage.damage then
  45.                 turtle.select(i)
  46.                 return true
  47.             end
  48.         end
  49.     end
  50.     return false
  51. end
  52. function getTotalItemCount(name, damage)
  53.     local count = 0
  54.     for i = 1, 16 do
  55.         local dat = turtle.getItemDetail(i)
  56.         if nil ~= dat then
  57.             if dat.name == name and dat.damage == damage then
  58.                 count = count + turtle.getItemCount(i)
  59.             end
  60.         end
  61.     end
  62.     return count
  63. end
  64. function dropEx(name, damage, keepCount)
  65.     local count = 0
  66.     for i = 1, 16 do
  67.         local dat = turtle.getItemDetail(i)
  68.         if nil ~= dat then
  69.             if dat.name == name and dat.damage == damage then
  70.                 local tmp = turtle.getItemCount(i)
  71.                 if count + tmp <= keepCount then
  72.                     count = count + tmp
  73.                 else
  74.                     turtle.select(i)
  75.                     turtle.dropDown(count + tmp - keepCount)
  76.                 end
  77.             end
  78.         end
  79.     end
  80. end
  81. function doSelfRefuel(inspectFunc, placeFunc)
  82.     local function findOne(name, metadata)
  83.         for i = 1, 16 do
  84.             if 1 == turtle.getItemCount(i) then
  85.                 local detail = turtle.getItemDetail(i)
  86.                 if nil ~= detail and detail.name == name and detail.damage == metadata then
  87.                     return i
  88.                 end
  89.             end
  90.         end
  91.         return -1
  92.     end
  93.     local selected = turtle.getSelectedSlot()
  94.     local rslt, inspect = inspectFunc()
  95.     if true == rslt and inspect.name == "minecraft:flowing_lava" and inspect.metadata == 0 then
  96.         local bucketSlot = findOne("minecraft:bucket", 0) -- とりあえず空バケツ1個の場合だけ実装する
  97.         if -1 ~= bucketSlot then
  98.             turtle.select(bucketSlot)
  99.             placeFunc() -- 溶岩を汲む
  100.             local lavaBucketSlot = findOne("minecraft:lava_bucket", 0)
  101.             if -1 ~= lavaBucketSlot then
  102.                 turtle.select(lavaBucketSlot)
  103.                 turtle.refuel()
  104.             end
  105.         end
  106.     end
  107.     turtle.select(selected)
  108. end
  109. function selfRefuelUp()
  110.     return doSelfRefuel(turtle.inspectUp, turtle.placeUp)
  111. end
  112. function selfRefuel()
  113.     return doSelfRefuel(turtle.inspect, turtle.place)
  114. end
  115. function selfRefuelDown()
  116.     return doSelfRefuel(turtle.inspectDown, turtle.placeDown)
  117. end
  118. function vacuumAll()
  119.     local dA, dB
  120.     for i = 1, 15 do
  121.         if 0 < turtle.getItemCount(i) and 0 < turtle.getItemSpace(i) then
  122.             dA = turtle.getItemDetail(i)
  123.             for j = i + 1, 16 do
  124.                 if 0 < turtle.getItemCount(j) and 0 < turtle.getItemSpace(j) then
  125.                     dB = turtle.getItemDetail(j)
  126.                     if dA.name == dB.name and dA.damage == dB.damage then
  127.                         turtle.select(j)
  128.                         turtle.transferTo(i)
  129.                         if 0 == turtle.getItemSpace(i) then
  130.                             break
  131.                         end
  132.                     end
  133.                 end
  134.             end
  135.         end
  136.     end
  137. end
  138. -- ----------------------------------------------
  139. -- Config
  140. -- ----------------------------------------------
  141. local INSPECT__USELESS_BLOCKS = {
  142.     { name = "minecraft:cobblestone", metadata = 0, keepCount = 64 },
  143.     { name = "minecraft:dirt"       , metadata = 0, keepCount =  0 },
  144.     { name = "minecraft:gravel"     , metadata = 0, keepCount =  0 },
  145.     { name = "minecraft:stone"      , metadata = 0, keepCount =  0 }
  146. }
  147. local MODEM_DIRECTION = "right"
  148. -- ----------------------------------------------
  149. -- Application
  150. -- ----------------------------------------------
  151. local ITEM_DETAIL__TORCH        = { name = "minecraft:torch"      , damage = 0 }
  152. local ITEM_DETAIL__COBBLE_STONE = { name = "minecraft:cobblestone", damage = 0 }
  153. local ITEM_DETAIL__CHEST        = { name = "minecraft:chest"      , damage = 0 }
  154. local downCount = 0
  155. local E0 = function()
  156.     local sel = turtle.getSelectedSlot()
  157.     if 1 == sel then
  158.         selfRefuelUp()
  159.         turtle.digUp()
  160.     else
  161.         turtle.select(1)
  162.         selfRefuelUp()
  163.         turtle.digUp()
  164.         turtle.select(sel)
  165.     end
  166. end
  167. local E1 = function()
  168.     local sel = turtle.getSelectedSlot()
  169.     if 1 == sel then
  170.         selfRefuel()
  171.         turtle.dig()
  172.     else
  173.         turtle.select(1)
  174.         selfRefuel()
  175.         turtle.dig()
  176.         turtle.select(sel)
  177.     end
  178. end
  179. local E2 = function()
  180.     local sel = turtle.getSelectedSlot()
  181.     if 1 == sel then
  182.         selfRefuelDown()
  183.         turtle.digDown()
  184.     else
  185.         turtle.select(1)
  186.         selfRefuelDown()
  187.         turtle.digDown()
  188.         turtle.select(sel)
  189.     end
  190. end
  191. local F = function()
  192.     while false == turtle.forward() do
  193.         turtle.dig()
  194.         if 0 < downCount then
  195.             turtle.attack()
  196.         end
  197.     end
  198.     return true
  199. end
  200. local U = function()
  201.     while false == turtle.up() do
  202.         turtle.digUp()
  203.         if 0 < downCount then
  204.             turtle.attackUp()
  205.         end
  206.     end
  207.     return true
  208. end
  209. local D = function()
  210.     while false == turtle.down() do
  211.         turtle.digDown()
  212.         if 0 < downCount then
  213.             turtle.attackDown()
  214.         end
  215.     end
  216.     return true
  217. end
  218. local L = turtle.turnLeft
  219. local R = turtle.turnRight
  220. local P0 = function() selfRefuelUp() turtle.placeUp() end
  221. local P1 = function() selfRefuel() turtle.place() end
  222. local P2 = function() selfRefuelDown() turtle.placeDown() end
  223. local SelCobble = function() local r = selectItem(ITEM_DETAIL__COBBLE_STONE) return r end
  224. local SelTorch = function() local r = selectItem(ITEM_DETAIL__TORCH) return r end
  225. local SelectChest = function() local r = selectItem(ITEM_DETAIL__CHEST) return r end
  226. local function isUseful(inspectFunction)
  227.     local flg, data = inspectFunction()
  228.     if false == flg then
  229.         return false
  230.     end
  231.     for i = 1, #INSPECT__USELESS_BLOCKS do
  232.         local useless = INSPECT__USELESS_BLOCKS[i]
  233.         local totalCnt = getTotalItemCount(useless.name, useless.metadata)
  234.         if data.name == useless.name and data.metadata == useless.metadata then
  235.             if useless.keepCount < getTotalItemCount(useless.name, useless.metadata) then
  236.                 return true
  237.             else
  238.                 return false
  239.             end
  240.         end
  241.     end
  242.     return true
  243. end
  244. local dropUselessBlocks = function()
  245.     for i = 1, #INSPECT__USELESS_BLOCKS do
  246.         local useless = INSPECT__USELESS_BLOCKS[i]
  247.         local totalCnt = getTotalItemCount(useless.name, useless.metadata)
  248.         if useless.keepCount < getTotalItemCount(useless.name, useless.metadata) then
  249.             dropEx(useless.name, useless.metadata, useless.keepCount)
  250.         end
  251.     end
  252. end
  253. local ResetDownCount = function()
  254.     downCount = 0
  255. end
  256. local MineDown = function()
  257.     vacuumAll()
  258.     while true do
  259.         if false == turtle.digDown() and true == turtle.detectDown() then
  260.             return true
  261.         end
  262.         D()
  263.         downCount = downCount + 1
  264.         if 2 == downCount then
  265.             SelCobble()
  266.             P0()
  267.         end
  268.         if true == isUseful(turtle.inspect) then
  269.             E1()
  270.         end
  271.         if 1 == downCount then
  272.             SelCobble()
  273.             E1()
  274.             P1()
  275.         end
  276.     end
  277. end
  278. local MineUp = function()
  279.     dropUselessBlocks()
  280.     vacuumAll()
  281.     for i = downCount, 1, -1 do
  282.         if true == isUseful(turtle.inspect) then
  283.             turtle.dig()
  284.         end
  285.         if 1 == downCount then
  286.             SelCobble()
  287.             E1()
  288.             P1()
  289.         end
  290.         E0()
  291.         U()
  292.         downCount = downCount - 1
  293.         if 0 == downCount then
  294.             SelCobble()
  295.             P2()
  296.         end
  297.     end
  298.     return true
  299. end
  300. -- 奥行き(前):8、幅(右):6、高さ(上):4の空間を作成する。
  301. -- 漏水や溶岩を防ぐため、掘削対象の空間と天井を張り直す。
  302. -- 天井の、前:3、右:1の位置に丸石、前:4、右:1の位置に松明を設置する。
  303. local MAKE_ROOM_LINE = {
  304.     repeatList(4,
  305.         SelCobble, P2,
  306.         repeatList(3, SelCobble, P0, E0, U),
  307.         SelCobble, E0, P0,
  308.         SelCobble, P1, E1, F,
  309.         SelCobble, E0, P0,
  310.         repeatList(3, SelCobble, P2, E2, D),
  311.         SelCobble, P2, SelCobble, P1, E1, F
  312.     ),
  313.     L, L, E1, F, L, L
  314. }
  315. local MAKE_ROOM = {
  316.     repeatList(3,
  317.         MAKE_ROOM_LINE, SelCobble, P1, R, SelCobble, P1, E1, F, R,
  318.         MAKE_ROOM_LINE,                L, SelCobble, P1, E1, F, L,
  319.         dropUselessBlocks
  320.     )
  321. }
  322. local MAKE_LIGHT_AND_BACK_HOME = {
  323.     L, E1, F, L, L, SelCobble, P1, L,
  324.     repeatList(3, E0, U),
  325.     repeatList(3, E1, F), L,
  326.     repeatList(2, E1, F), SelCobble, E1, P1, L, L, E1, F, L, L,
  327.     SelTorch, E1, P1, L, E1, F, R,
  328.     repeatList(4, E1, F), L,
  329.     repeatList(2, E1, F), L, L,
  330.     repeatList(3, E2, D)
  331. }
  332. local MINING_FL = {
  333.     ResetDownCount, MineDown, L, MineUp
  334. }
  335. local MINING = {
  336.     repeatList(2,
  337.         R,
  338.         MINING_FL, R, E1, F, E1, F, L, E1, F, L,
  339.         MINING_FL, E1, F, L, E1, F,
  340.         MINING_FL, R, E1, F, E1, F, L, E1, F, L,
  341.         MINING_FL, L, L, E1, F, E1, F, L,
  342.         MINING_FL, E1, F, R, E1, F, E1, F, L, L,
  343.         MINING_FL, E1, F, L, E1, F,
  344.         MINING_FL, E1, F, R, E1, F, E1, F, L, L,
  345.         MINING_FL, E1, F, E1, F,
  346.         dropUselessBlocks,
  347.         SelectSlot1
  348.     )
  349. }
  350. local main = function()
  351.     local initialFuelLevel = turtle.getFuelLevel()
  352.     executeFunction(
  353.         toFlatList(
  354.             MAKE_ROOM,
  355.             MAKE_LIGHT_AND_BACK_HOME,
  356.             MINING
  357.         )
  358.     )
  359.     vacuumAll()
  360.     print("completed.")
  361.     local fuel = initialFuelLevel - turtle.getFuelLevel()
  362.     print("consumed " .. tostring(fuel) .. " fuel.")
  363. end
  364. beacon.TRANSMITTER_MODEM_DIRECTION = MODEM_DIRECTION
  365. parallel.waitForAll(main, beacon.transmitter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement