the-vindex

Direwolf20 mining script - miner

Oct 1st, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.55 KB | None | 0 0
  1. ---------------------- Direwolf20 autominer script - mining 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. -- Miner: 13|14|15|16 = coal chest | drop off chest | Mining well | Energy conduit
  8. -- FuelBoss: 12|13|14|15|16 = cobblestone | coal chest | drop off chest | Mining well | Energy conduit
  9. -- Chunkloader: 13|16 = coal chest | chunkloader
  10. -- Controller: 1|2 = energy tesseract setup with shiny metal | Energy condiut
  11.  
  12. ------ Usage:
  13. ---- Legend:
  14. -- P = controller turtle (see mainProg script), must be 1 level above other turtles
  15. -- F = fuel boss + automining turtle - fuels P, must be next to it
  16. -- A = automining turtle, repeat as much as you need
  17. -- C = chunkloader turtle, not shown below, should be before and after mining group
  18. ---- Place turtles vertically like this:
  19. --     P
  20. --  AAF
  21.  
  22. ---- Startup program:
  23. -- for P - any, just execute mainProg NNN
  24. -- F: shell.run("miner","fuelBoss")
  25. -- A: shell.run("miner","Miner")
  26. -- C: shell.run("miner","chunkloader")
  27.  
  28. rednet.open("right")
  29. local func, compileResult
  30. local tArgs = {...}
  31. local myName
  32. local enderChests = 1
  33.  
  34. function cleanup()
  35.    if enderChests==0 then
  36.       turtle.turnRight() -- if we don't have ender chest, we face our neighbour to the right
  37.    end
  38.  
  39.    -- clean up general slots
  40.    for i = 1,11 do
  41.       turtle.select(i)
  42.      
  43.       -- just for fuelBoss - stock up with cobblestone in 12
  44.       if myName=="fuelBoss" and turtle.compareTo(12) then
  45.          turtle.transferTo(12)  
  46.       end
  47.      
  48.       cleanupDrop()
  49.    end
  50.    
  51.    -- fuelBoss keeps cobble in 12, others just drop it
  52.    if not myName=="fuelBoss" then
  53.       turtle.select(12)
  54.       cleanupDrop()
  55.    end
  56.    
  57.   if enderChests==0 then
  58.       turtle.turnLeft() -- at the end face original direction
  59.    end                      
  60. end
  61.  
  62. function cleanupDrop()
  63.    if enderChests>0 then
  64.       turtle.dropDown() -- if we have enderchest, we drop down to it
  65.    else
  66.       turtle.drop() -- else we drop to the turtle in front of use (assuming rotation already took place)
  67.   end
  68. end
  69.  
  70.  
  71. function place()
  72.    clearFront()
  73.    turtle.select(16)
  74.    turtle.place()
  75.    clearDown()
  76.    turtle.down()
  77.    clearFront()
  78.    turtle.select(15)
  79.    turtle.place()
  80.    clearDown()
  81.    turtle.select(14)
  82.    turtle.placeDown()
  83. end
  84.  
  85. function remove()
  86.    turtle.select(14)
  87.    turtle.trasnferTo(1)
  88.    turtle.dropUp()
  89.    turtle.digDown()
  90.    turtle.select(15)
  91.    turtle.trasnferTo(2)
  92.    turtle.dropUp()
  93.    turtle.dig()
  94.    turtle.up()
  95.    turtle.select(16)
  96.    turtle.trasnferTo(3)
  97.    turtle.dropUp()
  98.    turtle.dig()
  99. end
  100.  
  101. function fuel()
  102.    print("In fuel")
  103.    if enderChests == 1 then
  104.      print("Fueling")
  105.      placeFuelChest()
  106.      turtle.select(1)
  107.      turtle.suckUp()
  108.      turtle.dropUp(turtle.getItemCount(1) - 4)
  109.      turtle.refuel()
  110.      removeFuelChest()
  111.    end
  112. end
  113.  
  114. function fuelBoss()
  115.   placeFuelChest()
  116.   turtle.select(1)
  117.   turtle.suckUp()
  118.   turtle.dropUp(turtle.getItemCount(1) - 4)
  119.   removeFuelChest()
  120.   turtle.up()
  121.   turtle.turnRight()
  122.   turtle.select(1)
  123.   turtle.drop()
  124.   turtle.turnLeft()
  125.   turtle.down()
  126. end
  127.  
  128. function placeFuelChest()
  129.   turtle.select(13)
  130.   turtle.placeUp()
  131. end
  132.  
  133. function removeFuelChest()
  134.   turtle.select(13)
  135.   turtle.digUp()
  136. end
  137.  
  138. function checkFuel()
  139.    if turtle.getFuelLevel() < 400 then
  140.       fuel()
  141.    end
  142. end
  143.  
  144. function isMiningWellActive()
  145.     local miningWell = peripheral.wrap("front")
  146.     return miningWell.isActive()
  147. end
  148.  
  149. function cycle()
  150.    place()
  151.    
  152. --   local totalWorkDuration = os.startTimer(25)
  153.    local countDown = 20
  154.    local countDownThreshold = 21
  155.    local hadItem = false
  156.    
  157.    sleep(5)
  158.    while countDown > 0 do
  159.       if turtle.getItemCount(1) > 0 then
  160.         print("Now I had item")
  161.         hadItem = true
  162.       end
  163.      
  164.       cleanup()
  165.       sleep(3)
  166.       local hasItem = false
  167.       if hadItem and countDown < countDownThreshold  then -- wait for first item to appear, then check for new items
  168.           for slot=1,11 do
  169.             if turtle.getItemCount(slot) > 0 then
  170.                 hasItem = true
  171.             end
  172.           end
  173.       end
  174.      
  175.       if(not(hasItem)) then
  176.          print("No items, so probably done")
  177.          break
  178.       end
  179.      
  180.       countDown = countDown - 1
  181.    end
  182.    
  183.    while turtle.getItemCount(1) > 0 do
  184.       print("Still not clean")
  185.       cleanup()
  186.       sleep(2)
  187.    end
  188.    remove()
  189.    checkFuel()
  190.    
  191.    
  192.    -- fuel boss will leave track of cobblestone behind it - takes from slot 12
  193.    -- if we run out of cobble, we will keep 1 specimen for restocking - see cleanup function
  194.    if myName == "fuelBoss" and turtle.getItemCount(12)>1 then
  195.       turtle.select(12)
  196.       turtle.placeDown()
  197.    end
  198.  
  199.    -- Step forward - for resiliency against obstacles break anything around
  200.    clearFront()      
  201.    turtle.forward()
  202.    clearUp()
  203.    clearDown()
  204.    
  205.    -- fuelBoss also makes way for controller turtle
  206.    if myName == "fuelBoss" then
  207.       clearFront()
  208.       turtle.forward()
  209.       turtle.turnRight()
  210.       clearFront()
  211.       turtle.forward()
  212.       clearUp()
  213.       clearDown()
  214.       turtle.back()
  215.       turtle.turnLeft()
  216.       turtle.back()
  217.    end  
  218. end
  219.  
  220. -- custom sleep function with double timers
  221. function waitForTimer(mainTimer, sleepDuration)
  222.    local sleepTimer = os.startTimer(sleepDuration)
  223.    local bigTimerHit = 0  
  224.    repeat
  225.      local sEvent, param = os.pullEvent( "timer" )
  226.      if param == mainTimer then
  227.         bigTimerHit = 1
  228.      end  
  229.    until param == sleepTimer
  230.    
  231.    return bigTimerHit == 1    
  232. end
  233.  
  234.  
  235. function clearFront()
  236.    turtle.select(2)
  237.    turtle.dig()
  238.    turtle.drop()
  239. end
  240.  
  241. function clearDown()
  242.    turtle.select(2)
  243.    turtle.digDown()
  244.    turtle.dropDown()
  245. end
  246.  
  247. function clearUp()
  248.    turtle.select(2)
  249.    turtle.digUp()
  250.    turtle.dropUp()
  251. end
  252.  
  253. function checkIn(fromID)
  254.    local msg = myName
  255.    if myName == "Miner" or myName == "fuelBoss" then
  256.       msg = myName.."|"..enderChests      
  257.    end  
  258.    rednet.send(fromID, msg)
  259. end
  260.  
  261. function reboot()
  262.    print("Rebooting...")
  263.    sleep(1)
  264.    os.reboot()
  265. end
  266.  
  267. function chunkLoad()
  268.    turtle.select(16)
  269.    turtle.digUp()
  270.    checkFuel()
  271.    clearFront()
  272.    turtle.forward()
  273.    
  274.    clearUp()
  275.    turtle.select(16)          
  276.    turtle.placeUp()
  277. end
  278.  
  279. function update(id)
  280.    fs.delete("mine_update")
  281.    shell.run("pastebin","get","0jbMsR9D","mine_update")
  282.    if fs.exists("mine_update") then
  283.         fs.delete("mine_backup")
  284.         if fs.exists("mine") then
  285.             fs.move("mine", "mine_backup")
  286.         end
  287.         fs.move("mine_update", "mine")
  288.    end
  289.    shell.run("pastebin","get","5Tx1TifC","mainProg_update")
  290.    if fs.exists("mainProg_update") then
  291.         fs.delete("mainProg_backup")
  292.         if fs.exists("mainProg") then
  293.             fs.move("mainProg", "mainProg_backup")
  294.         end
  295.         fs.move("mainProg_update", "mainProg")
  296.    end
  297.    if id ~= nil then
  298.     rednet.send(id,"Done")  
  299.     os.reboot()
  300.    end
  301. end
  302.  
  303. function reportFuelRequests(id)
  304.    if turtle.getFuelLevel() < 400 then
  305.       print("Need fuel")
  306.       rednet.send(id, "needFuel")
  307.    end
  308. end
  309.  
  310. function grabFuel()
  311.    print ("Grabbing fuel")
  312.    turtle.select(1)
  313.    turtle.drop()
  314.    
  315.    turtle.up()
  316.    turtle.turnRight()
  317.    local distance = 0
  318.    while turtle.forward() do
  319.       distance = distance + 1
  320.    end
  321.    turtle.select(1)
  322.    turtle.suck()
  323.    turtle.drop(turtle.getItemCount(1) - 4)
  324.    turtle.refuel()
  325.    print("Fuel level: "..turtle.getFuelLevel())
  326.    
  327.    for i=1,distance do
  328.      turtle.back()
  329.    end
  330.    
  331.    turtle.turnLeft()
  332.    turtle.down()
  333. end
  334.  
  335. function moveForward()
  336.    cycleFunction(50, function() return turtle.forward() end)
  337. end
  338.  
  339. function moveBack()
  340.    cycleFunction(50, function() return turtle.back() end)
  341. end
  342.  
  343. function turnLeft()
  344.    turtle.turnLeft()
  345. end
  346.  
  347. function turnRight()
  348.    turtle.turnRight()
  349. end
  350.  
  351. function moveDown()
  352.    cycleFunction(50, function() return turtle.down() end)
  353. end
  354.  
  355. function moveUp()
  356.    cycleFunction(50, function() return turtle.up() end)
  357. end
  358.  
  359. function cycleFunction(count,func)
  360.    while not func() and count>0 do
  361.       count = count - 1
  362.       sleep(0.3)
  363.    end
  364.    if count == 0 then
  365.       print("Limit reached! Stopping cycle!")
  366.    end
  367. end
  368.  
  369. function echo(id)
  370.    print("Echo from "..id)
  371. end
  372.  
  373. function writeStartup(turtleName)
  374.     fs.delete("startup")
  375.     local startupFile = fs.open("startup", "w")
  376.     startupFile.writeLine("shell.run(\"mine\",\""..turtleName.."\")")
  377.     startupFile.close()
  378. end
  379. -------------------------
  380. print("Start time: ", textutils.formatTime(os.time(), true))
  381.  
  382. if #tArgs == 1 and "update" == (tArgs[1].."") then
  383.     update()
  384.     error()
  385. end
  386.  
  387.  
  388. if #tArgs == 1 and "startup-fuelBoss" == (tArgs[1].."") then
  389.     writeStartup("fuelBoss")
  390.     error()
  391. end
  392.  
  393. if #tArgs == 1 and "startup-chunkloader" == (tArgs[1].."") then
  394.     writeStartup("chunkloader")
  395.     error()
  396. end
  397.  
  398. if #tArgs == 1 and "startup-Miner" == (tArgs[1].."") then
  399.     writeStartup("Miner")
  400.     error()
  401. end
  402.  
  403. myName = tArgs[1]..""
  404. if tArgs[2] ~= nil then
  405.   enderChests = math.min(1, tonumber(tArgs[2]))
  406. end
  407.  
  408. assert(myName == "fuelBoss" or myName == "chunkloader" or myName == "Miner", "Unsupported turtle name"..myName)
  409. print("My name is "..myName..", ender chests: "..enderChests)
  410.  
  411. checkFuel()
  412.  
  413. while true do
  414.    local id,msg,dist = rednet.receive()
  415.    func, compileResult = loadstring(msg.."(...)")
  416.    print(func)
  417.    print(compileResult)
  418.    if func == nil then
  419.       print("Unknown function "..msg..". Skipping execution.")      
  420.    else
  421.       setfenv(func, getfenv())
  422.       func(id)
  423.       rednet.send(id,"Done")
  424.    end
  425. end
Advertisement
Add Comment
Please, Sign In to add comment