the-vindex

Direwolf20 mining script - main prog

Oct 1st, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.59 KB | None | 0 0
  1. ---------------------- Direwolf20 autominer script - controller turtle
  2. -- Use group of turtles for automining.
  3. -- Controller turtle give commands to other turtles for real job and provides power.
  4. -- Tutorial: http://www.youtube.com/watch?v=uXhBzaaEtAs
  5.  
  6. ------ Turtle setup (inventory slot = contents):
  7. -- FuelBoss/Miner: 13|14|15|16 = coal chest | drop off chest | Mining well | Energy conduit
  8. -- Chunkloader: 13|16 = coal chest | chunkloader
  9. -- Controller: 1|2 = energy tesseract setup with shiny metal | Energy condiut
  10.  
  11. ------ Usage:
  12. ---- Legend:
  13. -- P = controller turtle (see mainProg script), must be 1 level above other turtles
  14. -- F = fuel boss + automining turtle - fuels P, must be next to it
  15. -- A = automining turtle, repeat as much as you need
  16. -- C = chunkloader turtle, not shown below, should be before and after mining group
  17. ---- Place turtles vertically like this:
  18. --     P
  19. --  AAF
  20.  
  21. ---- Startup program:
  22. -- for P - any, just execute mainProg NNN
  23. -- F: shell.run("miner","fuelBoss")
  24. -- A: shell.run("miner","Miner")
  25. -- C: shell.run("miner","chunkloader")
  26.  
  27. rednet.open("right")
  28. local miners = {}
  29. local minersNoChest = {}
  30. local loaders = {}
  31. local fuelBosses = {}
  32. local tArgs = {...}
  33.  
  34.  
  35. function findTurtles()
  36.    local gotMsg = true
  37.    local id, msg, dist
  38.    rednet.broadcast("checkIn")
  39.    while gotMsg do
  40.       id,msg,dist = rednet.receive(1)
  41.      
  42.       -- communication debugging
  43.       --if msg ~= nil then print ("Got msg "..msg) end
  44.      
  45.       local turtleName, hasEnderChests = parseMessage(msg)
  46.      
  47.       if turtleName == "Miner" then
  48.          print(id..":"..turtleName..", chests:"..hasEnderChests)
  49.          miners[#miners+1] = id
  50.          if tonumber(hasEnderChests) == 0 then
  51.             minersNoChest[#minersNoChest+1] = id
  52.          end
  53.       elseif turtleName == "chunkloader" then
  54.          print(id..":"..msg)
  55.          loaders[#loaders+1] = id
  56.       elseif turtleName == "fuelBoss" then
  57.          print(id..":"..msg)
  58.          fuelBosses[#fuelBosses+1] = id
  59.          miners[#miners+1] = id
  60.       elseif turtleName == "Done" then
  61.       elseif turtleName == "checkIn" then
  62.       else
  63.          print("Done")
  64.          gotMsg = false
  65.       end
  66.    end      
  67. end
  68.  
  69. function parseMessage(msg)
  70.   local parts, turtleName, hasEnderChests
  71.   if msg ~= nil then
  72.      parts = string.split(msg, "|")
  73.      turtleName = parts[1]
  74.      hasEnderChests = parts[2]
  75.   else
  76.      turtleName = nil
  77.      hasEnderChests = nil
  78.   end
  79.   return turtleName,hasEnderChests
  80. end
  81.  
  82. function string:split(sep)
  83.         local sep, fields = sep or "|", {}
  84.         local pattern = string.format("([^%s]+)", sep)
  85.         self:gsub(pattern, function(c) fields[#fields+1] = c end)
  86.         return fields
  87. end
  88.  
  89. function checkFuel()
  90.    if turtle.getFuelLevel() < 900 then
  91.       rednet.send(fuelBosses[1], "fuelBoss")
  92.       rednet.receive()
  93.       turtle.select(3)
  94.       turtle.refuel()
  95.       turtle.select(1)
  96.    end
  97. end
  98.  
  99. function place()
  100.    turtle.select(1)
  101.    turtle.place()
  102.    turtle.down()
  103.    turtle.select(2)
  104.    turtle.place()
  105.    turtle.attack()
  106. end
  107.  
  108. function remove()
  109.    turtle.select(2)
  110.    turtle.dig()
  111.    turtle.up()
  112.    turtle.select(1)
  113.    turtle.dig()
  114. end
  115.  
  116. function minersGo()
  117.    for x,y in pairs(miners) do
  118.       rednet.send(y, "cycle")
  119.    end
  120.    place()
  121.    local total = 0
  122.    while total < #miners do
  123.       local id,msg,dist=rednet.receive()
  124.       total = total+1
  125.    end  
  126.    remove()
  127. end
  128.  
  129. function moveLoaders()
  130.    for x,y in pairs(loaders) do
  131.       rednet.send(y,"chunkLoad")
  132.       rednet.receive()
  133.       sleep(0.5)
  134.    end
  135. end
  136.  
  137.  
  138. function fuelChestless()
  139.  
  140.    -- ask if someone needs fuel
  141.    print("Chestless turtles: "..#minersNoChest)
  142.    for x,y in pairs(minersNoChest) do
  143.       rednet.send(y, "reportFuelRequests")
  144.    end
  145.    
  146.    local total = 0
  147.    local needsFuel = {}
  148.    while total < #minersNoChest do
  149.       local id,msg,dist=rednet.receive()
  150.  
  151.       if msg == "needFuel" then
  152.          needsFuel[#needsFuel+1] = id
  153.       elseif msg == "Done" then
  154.          total = total+1
  155.       end
  156.    end
  157.    
  158.    -- chestsless turtles need fuel, tell fuelboss to place fuel chest
  159.    if #needsFuel>0 then
  160.       print("Turtles in need of fuel: "..#needsFuel)
  161.       rednet.send(fuelBosses[1], "placeFuelChest")
  162.       local id,msg,dist=rednet.receive()
  163.    end
  164.  
  165.    for x,y in pairs(needsFuel) do
  166.       print(y..": grabFuel")
  167.       rednet.send(y, "grabFuel")
  168.       local id,msg,dist=rednet.receive()
  169.    end
  170.    
  171.   if #needsFuel>0 then
  172.       rednet.send(fuelBosses[1], "removeFuelChest")
  173.       local id,msg,dist=rednet.receive()
  174.    end
  175. end
  176.  
  177. function shout(msg)
  178.    rednet.broadcast(msg)
  179.    print("Shouted: "..tArgs[2]..".")
  180. end
  181. ---------------- Main program
  182.  
  183.  
  184. if tArgs[1] ~= nil and tArgs[1].."" == "diagnose" then
  185.    findTurtles()
  186.    print("Diagnostics done. Exiting.")
  187.    error()
  188. end
  189.  
  190. if tArgs[1] ~= nil and tArgs[1].."" == "reboot" then
  191.    rednet.broadcast("reboot")
  192.    print("Reboot sent. Exiting.")
  193.    error()
  194. end
  195.  
  196. if tArgs[1] ~= nil and tArgs[1].."" == "refuel" then
  197.    findTurtles()
  198.    fuelChestless()
  199.    print("Fueling done. Exiting.")
  200.    error()
  201. end
  202.  
  203. if tArgs[1] ~= nil and tArgs[1].."" == "shout" and tArgs[2] ~= nil then
  204.    local msg = tArgs[2]
  205.    shout(msg)
  206.    error()
  207. end
  208.  
  209. if tArgs[1] ~= nil and tArgs[1].."" == "tell" and tArgs[2] ~= nil and tArgs[3] ~= nil then
  210.    
  211.    local id = tonumber(tArgs[2])
  212.    local idList
  213.    local msg = tArgs[3]
  214.    
  215.    if id ~= nil then
  216.       idList = {id}
  217.    elseif tArgs[2] == "all" then
  218.       shout(msg)
  219.       error()
  220.    else
  221.       -- turtle type based broadcast
  222.       findTurtles()
  223.      
  224.       local turtleType = tArgs[2]
  225.       if turtleType == "miners" then
  226.          idList = miners
  227.       elseif turtleType == "loaders" then
  228.          idList = loaders
  229.       elseif turtleType == "fuelBoss" then
  230.          idList = fuelBosses
  231.       else
  232.          error(1)
  233.       end  
  234.    end
  235.    
  236.    for i=1,#idList do
  237.       print("Telling id: "..idList[i]..", msg: "..msg..".")
  238.       rednet.send(idList[i], msg)
  239.    end
  240.    error()
  241. end
  242.  
  243.  
  244. findTurtles()
  245.  
  246. if tArgs[1] == nil then tArgs[1] = 1 end
  247. for i = 1,tArgs[1] do
  248.  
  249.    -- manual iteration break - put two same items in first two slots
  250.    turtle.select(1)
  251.    if turtle.compareTo(2) then
  252.       break
  253.    end
  254.    
  255.    print("Interation: "..tostring(i).." of "..tostring(tArgs[1]))
  256.    minersGo()
  257.                            
  258.    -- here we hope there is no obstacle, because fuelBoss has to clear any obstacles
  259.    -- turtle has no capacity to clear those itself      
  260.    turtle.forward()
  261.    
  262.    checkFuel()
  263.    moveLoaders()
  264.    
  265.    fuelChestless()
  266. end
Advertisement
Add Comment
Please, Sign In to add comment