Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Works on more peripherals ender bag, advanced peripherals geoscanner
- -------------------
- --libs
- local sps = require("sps")
- local completion = require("cc.completion")
- local refuel = require("refuel")
- -------------------
- --init sps
- if not sps.init() then
- term.clear()
- term.setCursorPos(1,1)
- print("please input the starting direction (north, south, east, west)")
- local choices = {"north","south","east","west"}
- local startDir = read(nil,nil,function(txt) return completion.choice(txt,choices) end):lower()
- assert(sps.init(startDir),"Please provide a valid starting direction")
- end
- --init refuel
- refuel.init()
- -------------------
- --constants
- enderBag = "peripherals:ender_bag"
- geoscanner = "advancedperipherals:geo_scanner"
- pickaxe = "minecraft:diamond_pickaxe"
- chunkController = "advancedperipherals:chunk_controller"
- modem = "computercraft:wireless_modem_advanced"
- importantItems = { --ItemID = peripheral type
- [enderBag] = "ender_bag",
- [geoscanner] = "geoScanner",
- [pickaxe] = true, --no peripheral type for this
- [chunkController] = "chunky",
- [modem] = "modem"
- }
- --Higher number = higher priority ore
- targetOres = {
- ["forge:ores/netherite_scrap"] = 100,
- ["forge:ores/diamond"] = 100,
- ["forge:ores/zinc"] = 4,
- ["forge:ores/copper"] = 2,
- ["forge:ores/iron"] = 4,
- ["forge:ores/gold"] = 5,
- ["forge:ores/redstone"] = 5,
- ["forge:ores/lapis"] = 3,
- ["forge:ores/coal"] = 1,
- ["forge:ores/quartz"] = 3
- }
- --Makes it log when it finds these blocks
- POI = {
- ["#forge:chests"] = true
- }
- --Saving fuel limit to avoid unnecessary function calls and table indexing
- fuelLimit = turtle.getFuelLimit()
- --Ender chest slots
- fuel_slot = 26 --[1-27], the slot in your ender chest reserved for sending fuel
- output_slot = 27 --[1-27], the slot in your ender chest reserved for outputting items
- local maxY = 13
- local minY = -30
- -------------------
- --Util
- local function selectSlot(slot) --Efficiency to avoid unnecessary yields when trying to select the slot you're already on
- return turtle.getSelectedSlot() == slot or turtle.select(slot)
- end
- local function selectItem(name)
- for i=1,16 do
- if (turtle.getItemDetail(i) or {}).name == name then
- selectSlot(i)
- return i
- end
- end
- return false
- end
- local function equip(name)
- if peripheral.getType("left") == importantItems[name] then
- return true
- else
- return selectItem(name) and turtle.equipLeft() or (name == pickaxe)
- end
- end
- local logAction
- if equip(modem) then
- logAction = function(action,...)
- print(action,...)
- equip(modem)
- rednet.open("left")
- rednet.broadcast({...},action)
- turtle.equipLeft() --Puts back the old item
- end
- else
- logAction = function(action,...)
- print(action,...)
- end
- end
- local function dumpInventory()
- logAction("dump")
- assert(equip(enderBag),"no ender bag found!")
- local ender = peripheral.wrap("left")
- for i=1,16 do
- local item = (turtle.getItemDetail(i) or {}).name
- if item and not importantItems[item] then
- if not refuel.getFuelValue(item) or refuel.refuelSlot(i) then --if you cant refuel with it (fuel full or not valid fuel)
- local count = turtle.getItemCount(i)
- if ender.pullItems(i,count,output_slot) ~= count then
- logAction("Awaiting Ender Chest Space")
- return false
- end --If no items can be transferred return false
- end
- end
- end
- return true
- end
- local function pullFuel()
- logAction("Awaiting Fuel")
- if turtle.getFuelLevel() * 1.5 > fuelLimit then return false end
- local slot = selectItem(nil)
- while not slot do
- dumpInventory()
- slot = selectItem(nil) --selects a blank slot
- sleep(0)
- end
- assert(equip(enderBag),"no ender bag found!")
- local ender = peripheral.wrap("left")
- while turtle.getFuelLevel() < 1000 and ender.pushItems(fuel_slot,64,slot) <= 0 do
- sleep(1)
- end
- selectItem(slot)
- sleep(0)
- print(refuel.refuelSlot(slot))
- return true
- end
- local function fuel()
- if pullFuel() then
- refuel.refuelSlot(turtle.getSelectedSlot())
- return true
- end
- for i=1,16 do
- if refuel.refuelSlot(turtle.getSelectedSlot()) then return true end
- end
- return false
- end
- local function forward()
- while turtle.detect() do
- turtle.dig()
- end
- return turtle.forward() or (fuel() and false)
- end
- local function up()
- while turtle.detectUp() do
- turtle.digUp()
- end
- return turtle.up() or (fuel() and false)
- end
- local function down()
- while turtle.detectDown() do
- turtle.digDown()
- end
- return turtle.down() or (fuel() and false)
- end
- local function rotate(dir)
- local _,facing = sps.facing()
- if (dir-facing)%4 > 2 then
- return turtle.turnLeft()
- end
- while facing ~= dir do
- turtle.turnRight()
- _,facing = sps.facing()
- end
- return true
- end
- local function logPosition()
- local x,y,z = sps.locate()
- local file = fs.open("pos.txt","w+")
- file.write(string.format("%d %d %d",x,y,z))
- file.close()
- end
- local function moveToPos(x,y,z)
- logAction("Moving to position",x,y,z)
- --logPosition()
- assert(equip(pickaxe),"no pickaxe found")
- local x0,y0,z0 = sps.locate()
- while y0 < y do
- up()
- x0,y0 = sps.locate()
- end
- while y0 > y do
- down()
- x0,y0 = sps.locate()
- end
- --logPosition()
- if x0 < x then rotate(1)
- elseif x0 > x then rotate(3) end
- while x0 ~= x do
- forward()
- x0 = sps.locate()
- end
- --logPosition()
- if z0 < z then rotate(2)
- elseif z0 > z then rotate(0) end
- while z0 ~= z do
- forward()
- x0,y0,z0 = sps.locate()
- end
- --logPosition()
- if x0 ~= x or y0 ~= y or z0 ~= z then error("Coordinates do not match") end
- return true
- end
- local function logPOI(block)
- local x,y,z = sps.locate()
- x,y,z = x+block.x,y+block.y,z+block.z
- local file = fs.open("POI/"..block.name:gsub(":","/")..".txt","r+")
- if file.readAll():match(string.format("%d %d %d",x,y,z)) then
- file.close()
- return false
- else
- file.write(string.format("%d %d %d",x,y,z))
- file.close()
- logAction("POI",block.name,x,y,z)
- end
- return true
- end
- --Returns ores, sorted by the highest priority ores at the end of the table and the lowest priority ores at the start
- local function scan()
- equip(geoscanner)
- local geo = peripheral.wrap("left")
- local blocks = geo.scan(geo.getConfiguration().scanBlocks.maxFreeRadius)
- local ores = {}
- local x,y,z = sps.locate()
- for k,v in pairs(blocks) do
- if v.y + y >= minY and v.y + y <= maxY then
- for j,w in pairs(v.tags) do
- w = "#"..w
- if targetOres[w] then
- 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]})
- break
- elseif POI[w] then
- logPOI(v)
- break
- end
- end
- end
- end
- -- table.sort(ores,function(a,b) return a.d > b.d end)
- return ores
- end
- local function dist(x,y,z,a,b,c)
- return math.abs(x-a) + math.abs(y-b) + math.abs(z-c)
- end
- local function popOre(ores)
- local x,y,z = sps.locate()
- local priority,idx = dist(ores[1].x,ores[1].y,ores[1].z,x,y,z)/ores[1].v,1
- for i,ore in ipairs(ores) do
- local newPriority = dist(ore.x,ore.y,ore.z,x,y,z)/ore.v
- if newPriority < priority then
- idx = i
- priority = newPriority
- end
- end
- return table.remove(ores,idx)
- end
- -------------------
- --Init Checks
- for k,v in ipairs({enderBag,geoscanner,pickaxe}) do
- assert(equip(v),"Missing peripheral ".. v)
- end
- -------------------
- local function cycle()
- while not dumpInventory() do
- sleep(0)
- end
- fuel()
- local ores = scan()
- if #ores == 0 then
- local x,y,z = sps.locate()
- moveToPos(x + math.random(-16,16),math.random(minY,maxY),z+math.random(-16,16))
- else
- while #ores > 0 do
- local ore = popOre(ores)
- logAction("ore found",ore)
- moveToPos(ore.x,ore.y,ore.z)
- dumpInventory()
- end
- end
- end
- while true do pcall(cycle) end
Add Comment
Please, Sign In to add comment