shiryavsky

Big Mine

Nov 19th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.69 KB | None | 0 0
  1. --test miner
  2.  
  3. currentFacing = 0
  4. currentlySelectedSlot = 1
  5. fuelLevelToRefuelAt = 5
  6. dirtBlockCount = 0
  7. minSlot = 1
  8. maxSlot = 12
  9.  
  10. tunnelLengthMax = 8
  11. torchesEvery = 3
  12.  
  13. baseY = 0
  14.  
  15. currentY = 0
  16. currentLevel = 1
  17. mineName = "MineX"
  18.  
  19. function main()
  20.   -- check parameters
  21.  
  22.   --fuel
  23.   while (turtle.getItemCount(16) < 2) do
  24.     selectSlot(16)
  25.     if turtle.refuel(1) then
  26.       awaitFix("Too low fuel items")
  27.     else
  28.       awaitFix("No fuel in selected slot")
  29.     end
  30.   end
  31.   --torches
  32.   while (turtle.getItemCount(15) < 16) do
  33.     selectSlot(15)
  34.     awaitFix("Too low torches in selected slot")
  35.   end
  36.   --signs
  37.   while (turtle.getItemCount(14) < 4) do
  38.     selectSlot(14)
  39.     awaitFix("Too low signs in selected slot")
  40.   end
  41.   --chests
  42.   while (turtle.getItemCount(13) < 4) do
  43.     selectSlot(13)
  44.     awaitFix("Too small chests in selected slot")
  45.   end
  46.   -- dirt blocks  
  47.   getDirtBlock() --init
  48.   while dirtBlockCount==0  do
  49.     awaitFix("No dirt blocks in 1sts slots")
  50.     getDirtBlock()
  51.   end
  52.   -- y coord
  53.   while baseY==0 do
  54.     print("Enter my Y coordinate: ")
  55.     baseY =  tonumber(io.read())
  56.     if baseY < 16 then
  57.       print(".. not valid")
  58.       baseY = 0
  59.     end
  60.   end
  61.  
  62.   -- init some  
  63.   minSlot = dirtBlockCount + 1
  64.  
  65.    print("Go first level (11)")
  66.  
  67.   --go first level
  68.   for y=baseY-1,11,-1 do
  69.       go("down", true)
  70.   end
  71.  
  72.  
  73.   for l=11,baseY-5,5 do
  74.     --level
  75.     currentY = l
  76.     print("Level: "..currentY)
  77.     selectSlot(13)
  78.     if not turtle.compare() then --if no chest here
  79.       makeShaftHall(tunnelLengthMax)
  80.       --minig process
  81.       turnRight()
  82.       go("forward", true)
  83.  
  84.       turnLeft() -- i'm on right side of shaftHall
  85.       for tunPos=0,tunnelLengthMax-1,1 do
  86.         go("forward", true)  -- first go 0,1,2,3,4,5 .. pos
  87.         if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
  88.            --the best pos to tunneling
  89.            turnRight()
  90.            makeShaft(tunPos+3)
  91.        turnRight() --i'm again on right Side
  92.            if isNeedstoReturn() then
  93.              print("OMG! I need to refuel some shit! returning..")
  94.              --returning to fuel chest
  95.              turnLeft()
  96.              turnLeft()            
  97.              for rX=tunPos,0,-1 do
  98.            go("forward", true)
  99.              end
  100.              turnRight()
  101.          go("forward", true)
  102.              turnLeft() --saw back
  103.              for rY=currentY,baseY-1,1 do
  104.                go("up", true)
  105.              end
  106.              --chest must be here
  107.              selectSlot(13)
  108.              while not turtle.compare() do
  109.                awaitFix("There is no chest!")
  110.              end
  111.              --load              
  112.              reload("fwd")
  113.              while isNeedstoReturn() do
  114.                 awaitFix("Place resources to chest!")
  115.                 reload("fwd")
  116.              end
  117.              --go back
  118.             for rY=baseY-1,currentY,-1 do
  119.                go("down",true)
  120.              end
  121.              turnLeft()
  122.              go("forward", true)
  123.              turnLeft() -- i'm on right side
  124.          for rX=tunPos,0,-1 do
  125.            go("forward", true)
  126.              end
  127.              -- yepp! returned!
  128.            end --if isNeedstoReturn()
  129.         end --digShaft
  130.       end  --for tunnel
  131.      
  132.       -- go("forward", true)
  133.       turnLeft()
  134.       go("forward", true)
  135.       go("forward", true)
  136.       turnLeft()
  137.  
  138.  
  139.       for tunPos=tunnelLengthMax-1,0,-1 do        
  140.         if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
  141.            --the best pos to tunneling
  142.            turnRight()
  143.            makeShaft(tunPos+3)
  144.            turnRight() --i'm again on right Side
  145.            if isNeedstoReturn() then
  146.              print("OMG! I need to refuel some shit! returning..")
  147.              --returning to fuel chest
  148.              for rX=tunPos,0,-1 do
  149.            go("forward", true)
  150.              end
  151.              turnLeft()
  152.          go("forward", true)
  153.              turnRight() --saw back
  154.              for rY=currentY,baseY-1,1 do
  155.                go("up", true)
  156.              end
  157.              --chest must be here
  158.              selectSlot(13)
  159.              while not turtle.compare() do
  160.                awaitFix("There is no chest!")
  161.              end
  162.              --load              
  163.              reload("fwd")
  164.              while isNeedstoReturn() do
  165.                 awaitFix("Place resources to chest!")
  166.                 reload("fwd")
  167.              end
  168.              --go back
  169.          for rY=baseY-1,currentY,-1 do
  170.                go("down",true)
  171.              end
  172.              turnRight()
  173.              go("forward", true)
  174.              turnRight() -- i'm on right side
  175.          for rX=tunPos,0,-1 do
  176.            go("forward", true)
  177.              end
  178.              turnLeft()
  179.              turnLeft()
  180.              -- yepp! returned!
  181.            end --if isNeedstoReturn()
  182.         end --digShaft
  183.         go("forward", true)  -- first go -63,-62,-61,-60 ..
  184.       end  --for tunnel
  185.       turnLeft()
  186.       go("forward", true)
  187.       turnLeft() -- i'm on base position    
  188.     end --if chest, skipping level
  189.     for i=1,5,1 do
  190.       go("up", true)
  191.     end    
  192.     --did it again
  193.     currentLevel = currentLevel + 1
  194.   end
  195.  
  196.   --small go up
  197.   for y=currentY,baseY,1 do
  198.     go("up", true)
  199.   end
  200.  
  201.   print("Well Done!")
  202.  
  203. end
  204.  
  205. -- make shaft hall, return and unload to placed chest FWD
  206. function makeShaftHall(tunnelLength)
  207.   local tunPos = 0    
  208.   while tunPos < tunnelLength do
  209.     go('forward', true)
  210.     --turn left and dig all in side & up
  211.     turnLeft()
  212.     for i=1,3,1 do
  213.       digNE('forward', false)
  214.       digNE('up', false)
  215.       go('up',true)
  216.     end
  217.     digNE('forward', false)
  218.     -- go right and again down
  219.     turnRight()
  220.     turnRight()
  221.     digNE('forward', false)
  222.     if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
  223.       placeTorch() -- place torch on right side above tunnels
  224.     end
  225.     for i=1,3,1 do
  226.       go('down',true)
  227.       digNE('forward', false)
  228.     end
  229.     turnLeft()
  230.     if tunPos < (tunnelLength-1) then
  231.       digNE('forward', false)
  232.     end  
  233.     if checkEmpty('down') then
  234.       print("making bridge")
  235.       putDirtBlock("down") -- for fucking peoples
  236.     end
  237.     tunPos = tunPos + 1
  238.     if math.floor(tunPos/5) == (tunPos/5) then
  239.       if getEmptySlots() < 2 then
  240.         print("Repacking..")
  241.         prettyRepack()
  242.       end
  243.     end    
  244.   end
  245.   print("Ohh.. fin! I go back")
  246.   turnRight()
  247.   go('up', true) -- place Sign on right side
  248.   local signText = mineName.."\nLevel: "..currentLevel.." ("..currentY..")\nLength: "..tunnelLength.."\nEXIT --->"
  249.   placeSign(signText)
  250.   go('down', true)
  251.   turnRight()  
  252.   while tunPos > 0 do
  253.     go('forward', true)
  254.     tunPos = tunPos - 1
  255.   end
  256.   turnLeft()
  257.   turnLeft()
  258.   --we at start position  
  259.   while not placeChest() do
  260.     awaitFix("Can't place chest")
  261.   end
  262.   prettyUnload("forward")
  263. end
  264.  
  265. -- makes single shaft
  266. -- 1: turtle creates shaft fwd
  267. -- 2: digging..
  268. -- z: turtle behind shaft place chest if need and unload
  269. function makeShaft(tunnelLength)
  270.   local tunPos = 0
  271.   local lastTorch = 0
  272.   local fromSlot = dirtBlockCount + 1
  273.   while tunPos < tunnelLength do
  274.     print("tunPos: "..tunPos)
  275.     --first fwd
  276.     go('forward', true)
  277.     --turn left and look
  278.     turnLeft() 
  279.     digNEND("fwd", true)
  280.     go('up', true)
  281.     digNEND("fwd", true)
  282.     go('up', true)
  283.     digNEND("fwd", true)
  284.     if lastTorch >=torchesEvery then --torches   
  285.       turnLeft() --turn back
  286.       placeTorch()  --back
  287.       turnRight()        
  288.       lastTorch = 0
  289.     end
  290.         digNEND("up", true)  --look up
  291.     turnRight()
  292.     turnRight()
  293.     digNEND("fwd", true)
  294.     go('down', true)
  295.     digNEND("fwd", true)
  296.     go('down', true)
  297.     digNEND("fwd", true)
  298.     turnLeft()
  299.     if not checkEmpty('down') then
  300.       if not checkDirt('down') then    
  301.         dig("down")
  302.         putDirtBlock("down")
  303.       end
  304.     else
  305.       print("Making bridge...")
  306.       putDirtBlock("down") -- for fucking peoples
  307.     end
  308.     --and repeat process
  309.     tunPos = tunPos + 1
  310.     lastTorch = lastTorch + 1
  311.   end
  312.   print("Ohh.. fin! I'll go back")
  313.   turnLeft()
  314.   turnLeft()  
  315.   while tunPos > 1 do
  316.     go('forward', true)
  317.     tunPos = tunPos - 1
  318.   end
  319.   putDirtBlock("up") --close tunnel
  320.   go('forward', true)
  321.   tunPos = tunPos - 1  
  322.   print("Returned from shaft!")
  323.   if (getEmptySlots() < 3) then --place chest
  324.     if not turtle.detect() then
  325.       print("Placing chest...")
  326.       placeChest("forward")
  327.     end
  328.     print("Unloading...")
  329.     prettyUnload("forward")
  330.   end
  331. end
  332.  
  333. -- BOOL if too small resources
  334. function isNeedstoReturn()
  335.   local result = (turtle.getItemCount(16) < 5) or (turtle.getItemCount(15) < 10) or (turtle.getItemCount(14) < 4) or (turtle.getItemCount(13) < 4)
  336.   return result
  337. end
  338.  
  339. -- INT place chest FWD
  340. function placeChest()
  341.   ensureFuel()
  342.   if turtle.getItemCount(13) <=1 then
  343.     print("I have no chests...")
  344.     return false
  345.   else
  346.     selectSlot(13)
  347.     if not turtle.place() then
  348.       print("Can't place chest!")
  349.       return false
  350.     end
  351.   end
  352.   return true
  353. end
  354.  
  355. -- INT place sign FWD with label
  356. function placeSign(label)
  357.   ensureFuel()
  358.   if turtle.getItemCount(14) <=1 then
  359.     print("I have no signs...")
  360.     return false
  361.   else
  362.     selectSlot(14)
  363.     if not turtle.place(label) then
  364.       print("Can't place sign!")
  365.       return false
  366.     end
  367.   end
  368.   return true
  369. end
  370.  
  371. -- INT get count of empty slots
  372. function getEmptySlots()
  373.   local emptyslots = 0
  374.   for slot=maxSlot,minSlot,-1 do
  375.     if turtle.getItemCount(slot)==0 then
  376.       emptyslots = emptyslots + 1
  377.     end
  378.   end
  379.   return emptyslots
  380. end
  381.  
  382. -- compress items in inventory and kick dirt
  383. function prettyRepack()    
  384.   local isDirt = true
  385.   local emptySlots = 0
  386.   local cnt = 0
  387.   for slot=maxSlot,minSlot,-1 do
  388.     if turtle.compareTo(16) then --this is fuel, okay!
  389.       turtle.transferTo(16)
  390.     end
  391.     selectSlot(slot)
  392.     isDirt = false
  393.     for dirt=1,dirtBlockCount,1 do  
  394.       if turtle.compareTo(dirt) then
  395.         isDirt = true
  396.       end
  397.     end
  398.     --
  399.     for slotB=minSlot,maxSlot,1 do
  400.       if turtle.compareTo(slotB) then
  401.         --try to fill B (move to upslot)
  402.         turtle.transferTo(slotB)
  403.       end
  404.     end    
  405.     if (turtle.getItemCount(slot) ~= 0) and (emptySlots < 2) and not isDirt then
  406.       turtle.drop()  -- we have no two empty slots and drop all dirt in this slot
  407.     end
  408.   end
  409.   for dirt=1,dirtBlockCount,1 do
  410.      cnt = turtle.getItemCount(dirt)
  411.      if cnt > 1 then
  412.        selectSlot(dirt)
  413.        turtle.drop(cnt-1)
  414.      end
  415.   end
  416. end
  417.  
  418. --unload to side chest
  419. -- TODO: make unloading not dirt item first
  420. function prettyUnload(direction)
  421.   ensureFuel()
  422.   for slot=minSlot,maxSlot,1 do
  423.     selectSlot(slot)
  424.     if direction=="up" then
  425.       turtle.dropUp()
  426.     elseif direction=="down" then
  427.       turtle.dropDown()
  428.     else
  429.       turtle.drop()
  430.     end
  431.   end
  432.   for dirt=1,dirtBlockCount,1 do
  433.      cnt = turtle.getItemCount(dirt)
  434.      if cnt > 1 then
  435.        selectSlot(dirt)
  436.        turtle.drop(cnt-1)
  437.      end
  438.   end  
  439. end
  440.  
  441. --reload from side chest
  442. function reload(direction)
  443.   ensureFuel()
  444.   for slot=13,16,1 do
  445.     selectSlot(slot)
  446.     if direction=="up" then
  447.       turtle.suckUp()
  448.     elseif direction=="down" then
  449.       turtle.suckDown()
  450.     else
  451.       turtle.suck()
  452.     end    
  453.     for slot2=minSlot,maxSlot,1 do
  454.       if turtle.compareTo(slot) then
  455.         selectSlot(slot2)
  456.         -- unload unused
  457.         if direction=="up" then
  458.           turtle.dropUp()
  459.         elseif direction=="down" then
  460.           turtle.dropDown()
  461.         else
  462.           turtle.drop()
  463.         end
  464.       end        
  465.     end
  466.   end  
  467. end
  468.  
  469. -- dig if block ON side and put Dirt - crazy %)
  470. function digNE(direction, putDirt)
  471.  if not checkEmpty(direction) then
  472.     while not checkEmpty(direction) do  --for gravel ore
  473.       dig(direction)
  474.     end
  475.     if putDirt then
  476.       putDirtBlock(direction)
  477.     end    
  478.   end
  479. end
  480.  
  481. -- dig if block ON side and this block NOT dirt, and put Dirt
  482. function digNEND(direction, putDirt)
  483.   if not checkEmpty(direction) then
  484.     if not checkDirt(direction) then
  485.     while not checkEmpty(direction) do  --for gravel ore
  486.       dig(direction)
  487.         end
  488.         if putDirt then
  489.           putDirtBlock(direction)
  490.         end      
  491.     end
  492.   end
  493. end
  494.  
  495. -- BOOL place torch on side
  496. function placeTorch()  
  497.   ensureFuel()
  498.   if turtle.getItemCount(15) <=1 then
  499.     print("I have no torches...")
  500.     return false
  501.   else
  502.     selectSlot(15)
  503.     if not turtle.place() then
  504.       print("Can't place torch!")
  505.       return false
  506.     end
  507.   end
  508.   return true
  509. end
  510.  
  511. -- BOOL put dirt block on side
  512. function putDirtBlock(side)
  513.   ensureFuel()
  514.   local toSelect = 1    
  515.   toSelect = getDirtBlock()
  516.   if toSelect==false then
  517.     return false
  518.   end
  519.   selectSlot(toSelect)
  520.   if side=="up" then
  521.     return turtle.placeUp()
  522.   elseif side=="down" then
  523.     return turtle.placeDown()
  524.   else
  525.     return turtle.place()
  526.   end  
  527.   return  false
  528. end
  529.  
  530. -- INT get first inventory id with dirt blocks
  531. function getDirtBlock()
  532.   if dirtBlockCount==0 then
  533.     selectSlot(1)
  534.     while turtle.getItemCount(currentlySelectedSlot)~=0 do
  535.       dirtBlockCount = dirtBlockCount + 1
  536.       selectSlot(currentlySelectedSlot + 1)
  537.     end
  538.   end
  539.   if dirtBlockCount==0 then
  540.     return false
  541.   end
  542.   local fromSlot = dirtBlockCount + 1
  543.   for i=fromSlot,11,1 do
  544.     if turtle.getItemCount(i)>1 then
  545.       selectSlot(i)
  546.       for j=1,dirtBlockCount,1 do
  547.         if turtle.compareTo(j) then
  548.             return i
  549.         end
  550.       end
  551.     end
  552.   end  
  553.   return false
  554. end
  555.  
  556. -- BOOL check if block on side is Dirt
  557. function checkDirt(side)
  558.   local result = false
  559.   if dirtBlockCount==0 then
  560.     getDirtBlock()
  561.   end
  562.   for j=1,dirtBlockCount,1 do
  563.     selectSlot(j)
  564.     if side=="up" then
  565.       result = turtle.compareUp()
  566.     elseif side=="down" then
  567.       result = turtle.compareDown()
  568.     else
  569.       result = turtle.compare()
  570.     end
  571.     if result then
  572.       return result
  573.     end  
  574.   end
  575.   return result
  576. end
  577.  
  578. -- BOOL check if block on side is not Exists
  579. function checkEmpty(side)
  580.     local result = false
  581.     if side=="up" then
  582.       result = not turtle.detectUp()
  583.     elseif side=="down" then
  584.       result = not turtle.detectDown()
  585.     else
  586.       result = not turtle.detect()
  587.     end
  588.     return result
  589. end
  590.  
  591. --select slot
  592. function selectSlot(x)
  593.   if x<1 then
  594.     x = 1
  595.   elseif x > 16 then
  596.     x = 16
  597.   end
  598.   turtle.select(x)
  599.   currentlySelectedSlot = x
  600. end
  601.  
  602.  
  603. --Check fuel, refueling from inventory special slot or not
  604. function ensureFuel()
  605.   -- Determine whether a refuel is required
  606.   local fuelLevel = turtle.getFuelLevel()
  607.   if (fuelLevel ~= "unlimited") then
  608.     if (fuelLevel < fuelLevelToRefuelAt) then
  609.       -- Need to refuel
  610.       selectSlot(16)
  611.       local fuelItems = turtle.getItemCount(16)
  612.       -- Do we need to impact the emergency fuel to continue? (always  
  613.       -- keep one fuel item in slot 16)
  614.       if (fuelItems == 0) then
  615.         awaitFix("I want more fuel!")
  616.       elseif (fuelItems == 1) then
  617.         --load fuel from another slot
  618.     local fueled = false
  619.     local fromSlot = dirtBlockCount + 1
  620.         for i=fromSlot,15,1 do
  621.           selectSlot(i)
  622.       if (turtle.compareTo(16)) then
  623.         print("Refueling from not fuel slot: "+i)
  624.         turtle.refuel(1)
  625.         turtle.transferTo(16)          
  626.         fueled = true
  627.           end
  628.         end
  629.         if not fueled then
  630.           print("completely out of fuel!")
  631.         end
  632.       else --fuelItems > 1
  633.         print("Time to refueling!")
  634.         turtle.refuel(1)
  635.       end
  636.     end
  637.   end
  638. end
  639.  
  640.  
  641. --turns
  642. function turnFacing(facing)
  643.   while facing ~= currentFacing do
  644.     turnRight()
  645.   end
  646. end
  647.  
  648. --turns
  649. function turnLeft()
  650.   ensureFuel()
  651.   turtle.turnLeft()
  652.   currentFacing = currentFacing - 1
  653.   if currentFacing < 0 then
  654.     currentFacing = 3
  655.   end
  656. end
  657.  
  658. --turns
  659. function turnRight()
  660.   ensureFuel()
  661.   turtle.turnRight()
  662.   currentFacing = currentFacing + 1
  663.   if currentFacing >= 4 then
  664.     currentFacing = 0
  665.   end
  666. end
  667.  
  668. -- BOOL dig direction with retries
  669. function dig(direction)
  670.   local result = false
  671.   local tries = 0
  672.   selectSlot(1)
  673.   while (not result) and (tries < 10) do
  674.     ensureFuel()   
  675.     if direction=="up" then
  676.         result = turtle.digUp()
  677.     elseif direction=="down" then
  678.         result = turtle.digDown()    
  679.     else
  680.         result = turtle.dig()
  681.     end  
  682.     tries = tries + 1
  683.   end
  684.   if not result then
  685.     print ("can't dig in "..direction)
  686.   end
  687.   return result
  688. end
  689.  
  690. --go direction with breaking blocks and refueling
  691. function go(direction, allowDig)
  692.     if direction=="right" then
  693.       turnRight()
  694.       direction="forward"
  695.     end
  696.     if direction=="left" then
  697.       turnLeft()
  698.       direction="forward"
  699.     end
  700.     if direction=="forward" then      
  701.        while turtle.detect() do
  702.          if allowDig then
  703.            ensureFuel()
  704.            turtle.dig()
  705.          else
  706.            awaitFix("can't forward")
  707.          end
  708.        end
  709.        
  710.        while not turtle.forward() do
  711.          ensureFuel()
  712.          turtle.dig()      
  713.        end
  714.     end
  715.     if direction=="up" then
  716.        while turtle.detectUp() do
  717.          if allowDig then
  718.            ensureFuel()
  719.            turtle.digUp()
  720.          else
  721.            awaitFix("can't up")
  722.          end
  723.        end     
  724.        while not turtle.up() do
  725.            ensureFuel()
  726.            turtle.digUp()      
  727.        end
  728.     end
  729.     if direction=="down" then
  730.        while turtle.detectDown() do
  731.          if allowDig then
  732.            ensureFuel()
  733.            turtle.digDown()
  734.          else
  735.            awaitFix("can't down")
  736.          end
  737.        end     
  738.         while not turtle.down() do
  739.            ensureFuel()
  740.            turtle.digDown()    
  741.        end
  742.     end
  743. end
  744.  
  745. --wait for user action
  746. function awaitFix(message)
  747.     print("I have problem: " .. message)
  748.     print("Please fix it and press Enter to continue")
  749.     io.read()
  750. end
  751.  
  752. main() --execute ALL
Advertisement
Add Comment
Please, Sign In to add comment