Advertisement
massacring

MassaMiningTurtleLib

Jun 27th, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.83 KB | None | 0 0
  1. local MassaLib = require("MassaMainLib")
  2.  
  3. local modem
  4. for i, name in ipairs(peripheral.getNames()) do
  5.     modem = peripheral.wrap(name)
  6.     if peripheral.getType(modem) == "modem" then
  7.         if modem.isWireless() then break
  8.         else modem = nil end
  9.     else modem = nil end
  10. end
  11. local channel = 4200 -- Info Log channel, never replies
  12. if modem then modem.open(channel) end
  13.  
  14. local DIRECTIONS = {
  15.     FRONT = 1,
  16.     RIGHT = 2,
  17.     BACK = 3,
  18.     LEFT = 4
  19. }
  20.  
  21. local facing = DIRECTIONS.FRONT
  22. local x,y,z = 0,0,0
  23.  
  24. local moves = {}
  25.  
  26. local broadcast, digAndMove
  27.  
  28. local function face(direction)
  29.     if facing == direction then return end
  30.     local difference = facing - direction
  31.     if difference < 0 then difference = difference * -1 end
  32.     if difference == 1 then
  33.         if facing < direction then
  34.             turtle.turnRight()
  35.         else
  36.             turtle.turnLeft()
  37.         end
  38.     end
  39.     if difference == 2 then
  40.         turtle.turnRight()
  41.         turtle.turnRight()
  42.     end
  43.     if difference == 3 then
  44.         if facing > direction then
  45.             turtle.turnRight()
  46.         else
  47.             turtle.turnLeft()
  48.         end
  49.     end
  50.     facing = direction
  51. end
  52.  
  53. local function turn(direction)
  54.     face(facing+direction)
  55. end
  56.  
  57. local function digAndMoveForward(override)
  58.     return digAndMove(function ()
  59.         turtle.dig()
  60.         local success = turtle.forward()
  61.         if success then
  62.             if facing == DIRECTIONS.FRONT then
  63.                 x = x + 1
  64.             elseif facing == DIRECTIONS.RIGHT then
  65.                 z = z + 1
  66.             elseif facing == DIRECTIONS.BACK then
  67.                 x = x - 1
  68.             else
  69.                 z = z - 1
  70.             end
  71.             table.insert(moves, {x, y, z})
  72.         end
  73.         return success
  74.     end, override)
  75. end
  76.  
  77. local function digAndMoveUp(override)
  78.     return digAndMove(function ()
  79.         turtle.digUp()
  80.         local success = turtle.up()
  81.         if success then
  82.             y = y + 1
  83.             table.insert(moves, {x, y, z})
  84.         end
  85.         return success
  86.     end, override)
  87. end
  88.  
  89. local function digAndMoveDown(override)
  90.     return digAndMove(function ()
  91.         turtle.digDown()
  92.         local success = turtle.down()
  93.         if success then
  94.             y = y - 1
  95.             table.insert(moves, {x, y, z})
  96.         end
  97.         return success
  98.     end, override)
  99. end
  100.  
  101. local function checkRefuelOption()
  102.     if turtle.getFuelLevel() > 100 then return -1 end
  103.     if turtle.getFuelLevel() % 10 == 0 then broadcast("Low on fuel.") end
  104.     if turtle.getFuelLevel() > 0 then
  105.         if turtle.detect() then
  106.             broadcast("No room to refuel, continuing mission.")
  107.             return -1
  108.         end
  109.     else broadcast("Out of fuel. Forcefully refueling.") end
  110.     broadcast("Attempting to refuel.")
  111.     local tankSlot = 0
  112.     for i = 1, 16, 1 do
  113.         local data = turtle.getItemDetail(i)
  114.         if not data then goto continue end
  115.         local itemName = data["name"]
  116.         local storageNames = { "dimstorage:dimensional_tank", "enderchests:ender_tank" }
  117.         local result = false
  118.         for _,name in ipairs(storageNames) do
  119.             if name == itemName then
  120.                 result = true
  121.                 break
  122.             end
  123.         end
  124.         if result then
  125.             tankSlot = i
  126.             break
  127.         end
  128.         ::continue::
  129.     end
  130.     if tankSlot == 0 then
  131.         broadcast("Turtle has no fuel tank.")
  132.     end
  133.     return tankSlot
  134. end
  135.  
  136. local function refuel(tankSlotId)
  137.     local bucketSlot = 0
  138.     for i = 1, 16, 1 do
  139.         local data = turtle.getItemDetail(i)
  140.         if not data then goto continue end
  141.         local itemName = data["name"]
  142.         local result = itemName == "minecraft:bucket"
  143.         if result then
  144.             bucketSlot = i
  145.             break
  146.         end
  147.         ::continue::
  148.     end
  149.     if bucketSlot == 0 then
  150.         broadcast("Turtle has no bucket.")
  151.         return false
  152.     end
  153.  
  154.     turtle.dig()
  155.     turtle.select(tankSlotId)
  156.     automataCore.useOnBlock()
  157.  
  158.     turtle.select(bucketSlot)
  159.     turtle.place()
  160.     local item = turtle.getItemDetail(turtle.getSelectedSlot())
  161.     if item.name ~= "minecraft:lava_bucket" then
  162.         turtle.place()
  163.         turtle.dig()
  164.         broadcast("Could not refuel.")
  165.         return false
  166.     end
  167.  
  168.     turtle.refuel()
  169.     turtle.dig()
  170.     return true
  171. end
  172.  
  173. local function abortMission()
  174.     if turtle.getFuelLevel() > 100 then return end
  175.     broadcast("Low on fuel, returning to surface.")
  176.  
  177.     while y < 69 do
  178.         local success = digAndMoveUp(true)
  179.         if not success then goto continue end
  180.         for i = 1, 4, 1 do
  181.             if digAndMoveForward(true) then break end
  182.             face(facing+1)
  183.         end
  184.         ::continue::
  185.     end
  186.     broadcast("Back on surface. Shutting down...")
  187.     exit = true
  188. end
  189.  
  190. local function checkInventory()
  191.     local storageSlot = 0
  192.     local count = 0
  193.     for i = 1, 16, 1 do
  194.         local data = turtle.getItemDetail(i)
  195.         if data then count = count + 1
  196.         else goto continue end
  197.         local itemName = data["name"]
  198.         local storageNames = { "dimstorage:dimensional_chest", "enderchests:ender_chest", "cyclic:crate", "minecraft:shulker_box" }
  199.         local storageTypes = { "^sophisticatedstorage:%a+_shulker_box", "^furnish:%a+_crate" }
  200.         local result = false
  201.         for _,name in ipairs(storageNames) do
  202.             if name == itemName then
  203.                 result = true
  204.                 break
  205.             end
  206.         end
  207.         if not result then for _,type in ipairs(storageTypes) do
  208.             if itemName:find(type) then
  209.                 result = true
  210.                 break
  211.             end
  212.         end end
  213.         if result then
  214.             storageSlot = i
  215.             break
  216.         end
  217.         ::continue::
  218.     end
  219.     if storageSlot == 0 then return end
  220.     if count < 8 then return end
  221.  
  222.     turtle.digUp()
  223.     turtle.select(storageSlot)
  224.     turtle.placeUp()
  225.     for i = 1, 16, 1 do
  226.         turtle.select(i)
  227.         turtle.dropUp()
  228.     end
  229.     turtle.digUp()
  230. end
  231.  
  232. local function checkData(inclusive, has_block, data, ids, tags)
  233.     if inclusive == nil then inclusive = true end
  234.     if not has_block then return false end
  235.     local result = false
  236.     for _,id in ipairs(ids) do
  237.         if inclusive then
  238.             if data.name == id then result = true end
  239.         else
  240.             if not data.name == id then result = true end
  241.         end
  242.     end
  243.     for _,tag in ipairs(tags) do
  244.         if inclusive then
  245.             if data.tags[tag] then result = true end
  246.         else
  247.             if not data.tags[tag] then result = true end
  248.         end
  249.     end
  250.     if not result then return false end
  251.     return true
  252. end
  253.  
  254. local function veinMine(inclusive, ids, tags)
  255.     if (not ids) and (not tags) then return end
  256.     do
  257.         local has_block, data = turtle.inspectUp()
  258.         if not checkData(inclusive, has_block, data, ids, tags) then goto continue end
  259.  
  260.         local success = digAndMoveUp()
  261.         veinMine(inclusive, ids, tags)
  262.         if success then digAndMoveDown() end
  263.  
  264.         ::continue::
  265.     end
  266.     do
  267.         local has_block, data = turtle.inspectDown()
  268.         if not checkData(inclusive, has_block, data, ids, tags) then goto continue end
  269.  
  270.         local success = digAndMoveDown()
  271.         veinMine(inclusive, ids, tags)
  272.         if success then digAndMoveUp() end
  273.  
  274.         ::continue::
  275.     end
  276.     for i = 1, 4, 1 do
  277.         face(facing+1)
  278.         local has_block, data = turtle.inspect()
  279.         if not checkData(inclusive, has_block, data, ids, tags) then goto continue end
  280.  
  281.         local success = digAndMoveForward()
  282.         veinMine(inclusive, ids, tags)
  283.         if success then
  284.             face(facing+2)
  285.             digAndMoveForward()
  286.             face(facing+2)
  287.         end
  288.  
  289.         ::continue::
  290.     end
  291. end
  292.  
  293. function digAndMove(direction, override)
  294.     local refuelOptions = -1 --checkRefuelOption()
  295.     local refuelResult = refuelOptions < 0
  296.     if refuelOptions > 0 then
  297.         refuelResult = refuel(refuelOptions)
  298.     end
  299.     if not refuelResult or refuelOptions == 0 then
  300.         broadcast("Refuel failed. Aborting mission.")
  301.         --abortMission()
  302.         error("Mission aborted.", 0)
  303.         return
  304.     end
  305.     checkInventory()
  306.     if exit then return end
  307.     local count = 0
  308.     repeat
  309.         count = count + 1
  310.         local success = direction()
  311.     until success or count > 20
  312.     return count <= 20
  313. end
  314.  
  315. function broadcast(message)
  316.     print(message)
  317.     if not modem then return end
  318.     modem.transmit(0, channel, message)
  319. end
  320.  
  321. return { DIRECTIONS = DIRECTIONS, face = face, turn = turn, digAndMoveForward = digAndMoveForward, digAndMoveUp = digAndMoveUp, digAndMoveDown = digAndMoveDown, veinMine = veinMine, broadcast = broadcast }
  322.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement