Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------- Direwolf20 autominer script - controller turtle
- -- Use group of turtles for automining.
- -- Controller turtle give commands to other turtles for real job and provides power.
- -- Tutorial: http://www.youtube.com/watch?v=uXhBzaaEtAs
- ------ Turtle setup (inventory slot = contents):
- -- FuelBoss/Miner: 13|14|15|16 = coal chest | drop off chest | Mining well | Energy conduit
- -- Chunkloader: 13|16 = coal chest | chunkloader
- -- Controller: 1|2 = energy tesseract setup with shiny metal | Energy condiut
- ------ Usage:
- ---- Legend:
- -- P = controller turtle (see mainProg script), must be 1 level above other turtles
- -- F = fuel boss + automining turtle - fuels P, must be next to it
- -- A = automining turtle, repeat as much as you need
- -- C = chunkloader turtle, not shown below, should be before and after mining group
- ---- Place turtles vertically like this:
- -- P
- -- AAF
- ---- Startup program:
- -- for P - any, just execute mainProg NNN
- -- F: shell.run("miner","fuelBoss")
- -- A: shell.run("miner","Miner")
- -- C: shell.run("miner","chunkloader")
- rednet.open("right")
- local miners = {}
- local minersNoChest = {}
- local loaders = {}
- local fuelBosses = {}
- local tArgs = {...}
- function findTurtles()
- local gotMsg = true
- local id, msg, dist
- rednet.broadcast("checkIn")
- while gotMsg do
- id,msg,dist = rednet.receive(1)
- -- communication debugging
- --if msg ~= nil then print ("Got msg "..msg) end
- local turtleName, hasEnderChests = parseMessage(msg)
- if turtleName == "Miner" then
- print(id..":"..turtleName..", chests:"..hasEnderChests)
- miners[#miners+1] = id
- if tonumber(hasEnderChests) == 0 then
- minersNoChest[#minersNoChest+1] = id
- end
- elseif turtleName == "chunkloader" then
- print(id..":"..msg)
- loaders[#loaders+1] = id
- elseif turtleName == "fuelBoss" then
- print(id..":"..msg)
- fuelBosses[#fuelBosses+1] = id
- miners[#miners+1] = id
- elseif turtleName == "Done" then
- elseif turtleName == "checkIn" then
- else
- print("Done")
- gotMsg = false
- end
- end
- end
- function parseMessage(msg)
- local parts, turtleName, hasEnderChests
- if msg ~= nil then
- parts = string.split(msg, "|")
- turtleName = parts[1]
- hasEnderChests = parts[2]
- else
- turtleName = nil
- hasEnderChests = nil
- end
- return turtleName,hasEnderChests
- end
- function string:split(sep)
- local sep, fields = sep or "|", {}
- local pattern = string.format("([^%s]+)", sep)
- self:gsub(pattern, function(c) fields[#fields+1] = c end)
- return fields
- end
- function checkFuel()
- if turtle.getFuelLevel() < 900 then
- rednet.send(fuelBosses[1], "fuelBoss")
- rednet.receive()
- turtle.select(3)
- turtle.refuel()
- turtle.select(1)
- end
- end
- function place()
- turtle.select(1)
- turtle.place()
- turtle.down()
- turtle.select(2)
- turtle.place()
- turtle.attack()
- end
- function remove()
- turtle.select(2)
- turtle.dig()
- turtle.up()
- turtle.select(1)
- turtle.dig()
- end
- function minersGo()
- for x,y in pairs(miners) do
- rednet.send(y, "cycle")
- end
- place()
- local total = 0
- while total < #miners do
- local id,msg,dist=rednet.receive()
- total = total+1
- end
- remove()
- end
- function moveLoaders()
- for x,y in pairs(loaders) do
- rednet.send(y,"chunkLoad")
- rednet.receive()
- sleep(0.5)
- end
- end
- function fuelChestless()
- -- ask if someone needs fuel
- print("Chestless turtles: "..#minersNoChest)
- for x,y in pairs(minersNoChest) do
- rednet.send(y, "reportFuelRequests")
- end
- local total = 0
- local needsFuel = {}
- while total < #minersNoChest do
- local id,msg,dist=rednet.receive()
- if msg == "needFuel" then
- needsFuel[#needsFuel+1] = id
- elseif msg == "Done" then
- total = total+1
- end
- end
- -- chestsless turtles need fuel, tell fuelboss to place fuel chest
- if #needsFuel>0 then
- print("Turtles in need of fuel: "..#needsFuel)
- rednet.send(fuelBosses[1], "placeFuelChest")
- local id,msg,dist=rednet.receive()
- end
- for x,y in pairs(needsFuel) do
- print(y..": grabFuel")
- rednet.send(y, "grabFuel")
- local id,msg,dist=rednet.receive()
- end
- if #needsFuel>0 then
- rednet.send(fuelBosses[1], "removeFuelChest")
- local id,msg,dist=rednet.receive()
- end
- end
- function shout(msg)
- rednet.broadcast(msg)
- print("Shouted: "..tArgs[2]..".")
- end
- ---------------- Main program
- if tArgs[1] ~= nil and tArgs[1].."" == "diagnose" then
- findTurtles()
- print("Diagnostics done. Exiting.")
- error()
- end
- if tArgs[1] ~= nil and tArgs[1].."" == "reboot" then
- rednet.broadcast("reboot")
- print("Reboot sent. Exiting.")
- error()
- end
- if tArgs[1] ~= nil and tArgs[1].."" == "refuel" then
- findTurtles()
- fuelChestless()
- print("Fueling done. Exiting.")
- error()
- end
- if tArgs[1] ~= nil and tArgs[1].."" == "shout" and tArgs[2] ~= nil then
- local msg = tArgs[2]
- shout(msg)
- error()
- end
- if tArgs[1] ~= nil and tArgs[1].."" == "tell" and tArgs[2] ~= nil and tArgs[3] ~= nil then
- local id = tonumber(tArgs[2])
- local idList
- local msg = tArgs[3]
- if id ~= nil then
- idList = {id}
- elseif tArgs[2] == "all" then
- shout(msg)
- error()
- else
- -- turtle type based broadcast
- findTurtles()
- local turtleType = tArgs[2]
- if turtleType == "miners" then
- idList = miners
- elseif turtleType == "loaders" then
- idList = loaders
- elseif turtleType == "fuelBoss" then
- idList = fuelBosses
- else
- error(1)
- end
- end
- for i=1,#idList do
- print("Telling id: "..idList[i]..", msg: "..msg..".")
- rednet.send(idList[i], msg)
- end
- error()
- end
- findTurtles()
- if tArgs[1] == nil then tArgs[1] = 1 end
- for i = 1,tArgs[1] do
- -- manual iteration break - put two same items in first two slots
- turtle.select(1)
- if turtle.compareTo(2) then
- break
- end
- print("Interation: "..tostring(i).." of "..tostring(tArgs[1]))
- minersGo()
- -- here we hope there is no obstacle, because fuelBoss has to clear any obstacles
- -- turtle has no capacity to clear those itself
- turtle.forward()
- checkFuel()
- moveLoaders()
- fuelChestless()
- end
Advertisement
Add Comment
Please, Sign In to add comment