t8rt0t

enderminer.lua

Jun 30th, 2024 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.58 KB | None | 0 0
  1. --Works on more peripherals ender bag, advanced peripherals geoscanner
  2. -------------------
  3. --libs
  4. local sps = require("sps")
  5. local completion = require("cc.completion")
  6. local refuel = require("refuel")
  7. -------------------
  8. --init sps
  9. if not sps.init() then
  10.     term.clear()
  11.     term.setCursorPos(1,1)
  12.     print("please input the starting direction (north, south, east, west)")
  13.     local choices = {"north","south","east","west"}
  14.     local startDir = read(nil,nil,function(txt) return completion.choice(txt,choices) end):lower()
  15.     assert(sps.init(startDir),"Please provide a valid starting direction")
  16. end
  17. --init refuel
  18. refuel.init()
  19. -------------------
  20. --constants
  21. enderBag = "peripherals:ender_bag"
  22. geoscanner = "advancedperipherals:geo_scanner"
  23. pickaxe = "minecraft:diamond_pickaxe"
  24. chunkController = "advancedperipherals:chunk_controller"
  25. modem = "computercraft:wireless_modem_advanced"
  26. importantItems = { --ItemID = peripheral type
  27.     [enderBag] = "ender_bag",
  28.     [geoscanner] = "geoScanner",
  29.     [pickaxe] = true, --no peripheral type for this
  30.     [chunkController] = "chunky",
  31.     [modem] = "modem"
  32. }
  33. --Higher number = higher priority ore
  34. targetOres = {
  35.     ["forge:ores/netherite_scrap"] = 100,
  36.     ["forge:ores/diamond"] = 100,
  37.     ["forge:ores/zinc"] = 4,
  38.     ["forge:ores/copper"] = 2,
  39.     ["forge:ores/iron"] = 4,
  40.     ["forge:ores/gold"] = 5,
  41.     ["forge:ores/redstone"] = 5,
  42.     ["forge:ores/lapis"] = 3,
  43.     ["forge:ores/coal"] = 1,
  44.     ["forge:ores/quartz"] = 3
  45. }
  46. --Makes it log when it finds these blocks
  47. POI = {
  48.     ["#forge:chests"] = true
  49. }
  50. --Saving fuel limit to avoid unnecessary function calls and table indexing
  51. fuelLimit = turtle.getFuelLimit()
  52. --Ender chest slots
  53. fuel_slot = 26 --[1-27], the slot in your ender chest reserved for sending fuel
  54. output_slot = 27 --[1-27], the slot in your ender chest reserved for outputting items
  55. local maxY = 13
  56. local minY = -30
  57. -------------------
  58. --Util
  59. local function selectSlot(slot) --Efficiency to avoid unnecessary yields when trying to select the slot you're already on
  60.     return turtle.getSelectedSlot() == slot or turtle.select(slot)
  61. end
  62.  
  63. local function selectItem(name)
  64.     for i=1,16 do
  65.         if (turtle.getItemDetail(i) or {}).name == name then
  66.             selectSlot(i)
  67.             return i
  68.         end
  69.     end
  70.     return false
  71. end
  72.  
  73. local function equip(name)
  74.     if peripheral.getType("left") == importantItems[name] then
  75.         return true
  76.     else
  77.         return selectItem(name) and turtle.equipLeft() or (name == pickaxe)
  78.     end
  79. end
  80. local logAction
  81. if equip(modem) then
  82.     logAction = function(action,...)
  83.         print(action,...)
  84.         equip(modem)
  85.         rednet.open("left")
  86.         rednet.broadcast({...},action)
  87.         turtle.equipLeft() --Puts back the old item
  88.     end
  89. else
  90.     logAction = function(action,...)
  91.         print(action,...)
  92.     end
  93. end
  94.  
  95. local function dumpInventory()
  96.     logAction("dump")
  97.     assert(equip(enderBag),"no ender bag found!")
  98.     local ender = peripheral.wrap("left")
  99.     for i=1,16 do
  100.         local item = (turtle.getItemDetail(i) or {}).name
  101.         if item and not importantItems[item] then
  102.             if not refuel.getFuelValue(item) or refuel.refuelSlot(i) then --if you cant refuel with it (fuel full or not valid fuel)
  103.                 local count = turtle.getItemCount(i)
  104.                 if ender.pullItems(i,count,output_slot) ~= count then
  105.                     logAction("Awaiting Ender Chest Space")
  106.                     return false
  107.                 end --If no items can be transferred return false
  108.             end
  109.         end
  110.     end
  111.     return true
  112. end
  113.  
  114. local function pullFuel()
  115.     logAction("Awaiting Fuel")
  116.     if turtle.getFuelLevel() * 1.5 > fuelLimit then return false end
  117.     local slot = selectItem(nil)
  118.     while not slot do
  119.         dumpInventory()
  120.         slot = selectItem(nil) --selects a blank slot
  121.         sleep(0)
  122.     end
  123.     assert(equip(enderBag),"no ender bag found!")
  124.     local ender = peripheral.wrap("left")
  125.     while turtle.getFuelLevel() < 1000 and ender.pushItems(fuel_slot,64,slot) <= 0 do
  126.         sleep(1)
  127.     end
  128.     selectItem(slot)
  129.     sleep(0)
  130.     print(refuel.refuelSlot(slot))
  131.     return true
  132. end
  133.  
  134. local function fuel()
  135.     if pullFuel() then
  136.         refuel.refuelSlot(turtle.getSelectedSlot())
  137.         return true
  138.     end
  139.     for i=1,16 do
  140.         if refuel.refuelSlot(turtle.getSelectedSlot()) then return true end
  141.     end
  142.     return false
  143. end
  144.    
  145.  
  146. local function forward()
  147.     while turtle.detect() do
  148.         turtle.dig()
  149.     end
  150.     return turtle.forward() or (fuel() and false)
  151. end
  152. local function up()
  153.     while turtle.detectUp() do
  154.         turtle.digUp()
  155.     end
  156.     return turtle.up() or (fuel() and false)
  157. end
  158. local function down()
  159.     while turtle.detectDown() do
  160.         turtle.digDown()
  161.     end
  162.     return turtle.down() or (fuel() and false)
  163. end
  164. local function rotate(dir)
  165.     local _,facing = sps.facing()
  166.     if (dir-facing)%4 > 2 then
  167.         return turtle.turnLeft()
  168.     end
  169.     while facing ~= dir do
  170.         turtle.turnRight()
  171.         _,facing = sps.facing()
  172.     end
  173.     return true
  174. end
  175. local function logPosition()
  176.     local x,y,z = sps.locate()
  177.     local file = fs.open("pos.txt","w+")
  178.     file.write(string.format("%d %d %d",x,y,z))
  179.     file.close()
  180. end
  181.        
  182. local function moveToPos(x,y,z)
  183.     logAction("Moving to position",x,y,z)
  184.     --logPosition()
  185.     assert(equip(pickaxe),"no pickaxe found")
  186.     local x0,y0,z0 = sps.locate()
  187.     while y0 < y do
  188.         up()
  189.         x0,y0 = sps.locate()
  190.     end
  191.     while y0 > y do
  192.         down()
  193.         x0,y0 = sps.locate()
  194.     end
  195.     --logPosition()
  196.     if x0 < x then rotate(1)
  197.     elseif x0 > x then rotate(3) end
  198.     while x0 ~= x do
  199.         forward()
  200.         x0 = sps.locate()
  201.     end
  202.     --logPosition()
  203.     if z0 < z then rotate(2)
  204.     elseif z0 > z then rotate(0) end
  205.     while z0 ~= z do
  206.         forward()
  207.         x0,y0,z0 = sps.locate()
  208.     end
  209.     --logPosition()
  210.     if x0 ~= x or y0 ~= y or z0 ~= z then error("Coordinates do not match") end
  211.     return true
  212. end
  213.  
  214. local function logPOI(block)
  215.     local x,y,z = sps.locate()
  216.     x,y,z = x+block.x,y+block.y,z+block.z
  217.     local file = fs.open("POI/"..block.name:gsub(":","/")..".txt","r+")
  218.     if file.readAll():match(string.format("%d %d %d",x,y,z)) then
  219.         file.close()
  220.         return false
  221.     else
  222.         file.write(string.format("%d %d %d",x,y,z))
  223.         file.close()
  224.         logAction("POI",block.name,x,y,z)
  225.     end
  226.     return true
  227. end
  228. --Returns ores, sorted by the highest priority ores at the end of the table and the lowest priority ores at the start
  229. local function scan()
  230.     equip(geoscanner)
  231.     local geo = peripheral.wrap("left")
  232.     local blocks = geo.scan(geo.getConfiguration().scanBlocks.maxFreeRadius)
  233.     local ores = {}
  234.     local x,y,z = sps.locate()
  235.     for k,v in pairs(blocks) do
  236.         if v.y + y >= minY and v.y + y <= maxY then
  237.             for j,w in pairs(v.tags) do
  238.                 w = "#"..w
  239.                 if targetOres[w] then
  240.                     table.insert(ores,{x=v.x,y=v.y,z=v.z,v=targetOres[w]})--d=(math.abs(v.x)+math.abs(v.y)+math.abs(v.z))/targetOres[w]})
  241.                     break
  242.                 elseif POI[w] then
  243.                     logPOI(v)
  244.                     break
  245.                 end
  246.             end
  247.         end
  248.     end
  249.     -- table.sort(ores,function(a,b) return a.d > b.d end)
  250.     return ores
  251. end
  252.  
  253. local function dist(x,y,z,a,b,c)
  254.     return math.abs(x-a) + math.abs(y-b) + math.abs(z-c)
  255. end
  256.  
  257. local function popOre(ores)
  258.     local x,y,z = sps.locate()
  259.     local priority,idx = dist(ores[1].x,ores[1].y,ores[1].z,x,y,z)/ores[1].v,1
  260.     for i,ore in ipairs(ores) do
  261.         local newPriority = dist(ore.x,ore.y,ore.z,x,y,z)/ore.v
  262.         if newPriority < priority then
  263.             idx = i
  264.             priority = newPriority
  265.         end
  266.     end
  267.     return table.remove(ores,idx)
  268. end
  269. -------------------
  270. --Init Checks
  271. for k,v in ipairs({enderBag,geoscanner,pickaxe}) do
  272.     assert(equip(v),"Missing peripheral ".. v)
  273. end
  274. -------------------
  275. local function cycle()
  276.     while not dumpInventory() do
  277.         sleep(0)
  278.     end
  279.     fuel()
  280.     local ores = scan()
  281.     if #ores == 0 then
  282.         local x,y,z = sps.locate()
  283.         moveToPos(x + math.random(-16,16),math.random(minY,maxY),z+math.random(-16,16))
  284.     else
  285.         while #ores > 0 do
  286.             local ore = popOre(ores)
  287.             logAction("ore found",ore)
  288.             moveToPos(ore.x,ore.y,ore.z)
  289.             dumpInventory()
  290.         end
  291.     end
  292. end
  293. while true do pcall(cycle) end
Add Comment
Please, Sign In to add comment