shiryavsky

mineX

Nov 20th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.95 KB | None | 0 0
  1. --test miner by shaolin
  2.  
  3. currentFacing = 0
  4. currentlySelectedSlot = 1
  5. fuelLevelToRefuelAt = 5
  6. dirtBlockCount = 0
  7. minSlot = 1
  8. maxSlot = 12
  9.  
  10. tunnelLengthMax = 0
  11. torchesEvery = 8
  12.  
  13. baseY = 0
  14.  
  15. currentY = 0
  16. currentLevel = 1
  17. mineName = ""
  18.  
  19. doHole = true
  20.  
  21. args = { ... }
  22.  
  23. function main()
  24.  
  25.  
  26.   if #args > 0 then
  27.     if args[1]=="n" then
  28.      print("No hole mode on")
  29.      doHole = false
  30.     end
  31.   end  
  32.  
  33.   -- check parameters
  34.   print("[ mineX loaded ]")
  35.   --fuel
  36.   while (turtle.getItemCount(16) < 2) do
  37.     selectSlot(16)
  38.     if turtle.refuel(1) then
  39.       awaitFix("Too low fuel items")
  40.     else
  41.       awaitFix("No fuel in selected slot")
  42.     end
  43.   end
  44.   --torches
  45.   while (turtle.getItemCount(15) < 16) do
  46.     selectSlot(15)
  47.     awaitFix("Too low torches in selected slot")
  48.   end
  49.   --signs
  50.   while (turtle.getItemCount(14) < 4) do
  51.     selectSlot(14)
  52.     awaitFix("Too low signs in selected slot")
  53.   end
  54.   --chests
  55.   while (turtle.getItemCount(13) < 4) do
  56.     selectSlot(13)
  57.     awaitFix("Too small chests in selected slot")
  58.   end
  59.   -- dirt blocks  
  60.   getDirtBlock() --init
  61.   while dirtBlockCount==0  do
  62.     awaitFix("No dirt blocks in first slots")
  63.     getDirtBlock()
  64.   end
  65.   -- y coord
  66.   while baseY==0 do
  67.     print("Enter my Y coordinate: ")
  68.     baseY =  tonumber(io.read())
  69.     if baseY < 16 then
  70.       print(".. not valid")
  71.       baseY = 0
  72.     end
  73.   end
  74.  
  75.   print("Enter mine name: ")
  76.   mineName =  io.read()
  77.   if mineName=="" then
  78.      mineName = getRandomName()
  79.      print("Will be: "..mineName)    
  80.   end
  81.  
  82.   while tunnelLengthMax==0 do
  83.     print("Enter tunnel length: ")
  84.     tunnelLengthMax =  tonumber(io.read())
  85.     if (tunnelLengthMax < 8) or (tunnelLengthMax > 64) then
  86.       print(".. not valid")
  87.       tunnelLengthMax = 0
  88.     end
  89.     if tunnelLengthMax < 16  then
  90.       torchesEvery = 4
  91.     end
  92.   end  
  93.  
  94.   -- init some  
  95.   minSlot = dirtBlockCount + 1
  96.  
  97.   if doHole then
  98.     print("Making Hole Down")
  99.     holeDown()
  100.   else
  101.     print("Making hole skipped")
  102.   end
  103.  
  104.   print("Go first level (11)")
  105.  
  106.   --go first level
  107.   for y=baseY-1,11,-1 do
  108.       go("down", true)
  109.   end
  110.  
  111.  
  112.   for l=11,baseY-5,5 do
  113.     --level
  114.     currentY = l
  115.     print("Level: "..currentY)
  116.     go("forward", true)
  117.     go("forward", true)
  118.     selectSlot(13)
  119.     if not turtle.compare() then --if no chest here
  120.       makeShaftHall(tunnelLengthMax)
  121.       --minig process
  122.       turnRight()
  123.       go("forward", true)
  124.  
  125.       turnLeft() -- i'm on right side of shaftHall
  126.       for tunPos=0,tunnelLengthMax-1,1 do
  127.         go("forward", true)  -- first go 0,1,2,3,4,5 .. pos
  128.         putDirtNE("down")
  129.         if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
  130.            --the best pos to tunneling
  131.            turnRight()
  132.            makeShaft(tunPos+3-1) -- -1 for one turtle can't eat other
  133.            
  134.            
  135.            
  136.            if (getEmptySlots() < 3) then
  137.              turnLeft()
  138.              -- go to tunnel locking chest if needs
  139.              for rX=tunPos-1,0,-1 do
  140.                go("forward", true)
  141.              end
  142.              turnRight()
  143.              if not turtle.detect() then
  144.                print("Placing chest...")
  145.                placeChest("forward")
  146.              end
  147.              print("Unloading...")
  148.              prettyUnload("forward")
  149.              turnRight()
  150.              for rX=tunPos-1,0,-1 do
  151.                go("forward", true)
  152.              end   
  153.            else
  154.              turnRight()
  155.            end         
  156.          
  157.  
  158.            
  159.            
  160.            
  161.            
  162. --         turnRight() --i'm again on right Side
  163.            if isNeedstoReturn() then
  164.              print("OMG! I need to refuel some shit! returning..")
  165.              --returning to fuel chest
  166.              turnLeft()
  167.              turnLeft()            
  168.              for rX=tunPos,0,-1 do
  169.                go("forward", true)
  170.              end
  171.              turnRight()
  172.              go("forward", true)
  173.              turnLeft() --shaft back
  174.              go("forward", true)
  175.              go("forward", true)
  176.              for rY=currentY,baseY-1,1 do
  177.                go("up", true)
  178.              end
  179.              --chest must be here
  180.              selectSlot(13)
  181.              while not turtle.compare() do
  182.                awaitFix("There is no chest!")
  183.              end
  184.              --load              
  185.              reload("fwd")
  186.              while isNeedstoReturn() do
  187.                 awaitFix("Place resources to chest!")
  188.                 reload("fwd")
  189.              end
  190.              --go back
  191.             for rY=baseY-1,currentY,-1 do
  192.                go("down",true)
  193.              end
  194.              turnLeft()
  195.              go("forward", true)
  196.              turnLeft() -- i'm on right side
  197.              go("forward", true)
  198.              go("forward", true)
  199.          for rX=tunPos,0,-1 do
  200.            go("forward", true)
  201.              end
  202.              -- yepp! returned!
  203.            end --if isNeedstoReturn()
  204.         end --digShaft
  205.       end  --for tunnel
  206.      
  207.       turnLeft()
  208.       go("forward", true)
  209.       putDirtNE("down")
  210.       go("forward", true)
  211.       putDirtNE("down")
  212.       turnLeft()
  213.  
  214.  
  215.       for tunPos=tunnelLengthMax-1,0,-1 do        
  216.         if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
  217.            --the best pos to tunneling
  218.            turnRight()
  219.            makeShaft(tunPos+3)
  220.            
  221.            if (getEmptySlots() < 3) then
  222.              turnRight()
  223.              -- go to tunnel locking chest if needs
  224.              for rX=tunPos-1,0,-1 do
  225.                go("forward", true)
  226.              end
  227.              turnLeft()
  228.              if not turtle.detect() then
  229.                print("Placing chest...")
  230.                placeChest("forward")
  231.              end
  232.              print("Unloading...")
  233.              prettyUnload("forward")
  234.              turnLeft()
  235.              for rX=tunPos-1,0,-1 do
  236.                go("forward", true)
  237.              end   
  238.              turnRight()
  239.              turnRight()
  240.            else
  241.              turnRight()
  242.            end 
  243.            
  244.  
  245.            if isNeedstoReturn() then
  246.              print("OMG! I need to refuel some shit! returning..")
  247.              --returning to fuel chest
  248.              for rX=tunPos,0,-1 do
  249.              go("forward", true)
  250.              end
  251.              turnLeft()
  252.              go("forward", true)
  253.              turnRight() --shaft behind
  254.              go("forward", true)
  255.              go("forward", true)
  256.              for rY=currentY,baseY-1,1 do
  257.                go("up", true)
  258.              end
  259.              --chest must be here
  260.              selectSlot(13)
  261.              while not turtle.compare() do
  262.                awaitFix("There is no chest!")
  263.              end
  264.              --load              
  265.              reload("fwd")
  266.              while isNeedstoReturn() do
  267.                 awaitFix("Place resources to chest!")
  268.                 reload("fwd")
  269.              end
  270.              --go back
  271.              for rY=baseY-1,currentY,-1 do
  272.                go("down",true)
  273.              end
  274.              turnRight()
  275.              go("forward", true)
  276.              turnRight() -- i'm on right side
  277.              go("forward", true)
  278.              go("forward", true)
  279.          for rX=tunPos,0,-1 do
  280.            go("forward", true)
  281.              end
  282.              turnLeft()
  283.              turnLeft()
  284.              -- yepp! returned!
  285.            end --if isNeedstoReturn()
  286.         end --digShaft
  287.         go("forward", true)  -- first go -63,-62,-61,-60 ..
  288.         putDirtNE("down")
  289.       end  --for tunnel
  290.       turnLeft()
  291.       go("forward", true)
  292.       turnLeft() -- i'm on base position    
  293.  
  294.     else  --if chest, skipping level
  295.       print("Chest! Skip the floor.")
  296.     end
  297.     turnLeft()
  298.     turnLeft()
  299.     go("forward", true)
  300.     go("forward", true)
  301.     turnLeft()
  302.     turnLeft()
  303.     for i=1,5,1 do
  304.       go("up", true)
  305.     end    
  306.     --did it again
  307.     currentLevel = currentLevel + 1
  308.   end
  309.  
  310.   --small go up
  311.   for y=currentY,baseY-6,1 do --TODO: check this
  312.     go("up", true)
  313.   end
  314.  
  315.   print("Well Done!")
  316.  
  317. end
  318.  
  319. function holeDown()
  320.   local myY = baseY
  321.   --go first level
  322.   for y=baseY-1,11,-1 do
  323.       go("down", true)
  324.       myY = myY - 1
  325.   end
  326.   for l=11,baseY-5,5 do
  327.     for i=1,4,1 do
  328.       turnRight()
  329.       go("forward", true)
  330.       if i==1 then
  331.         putDirtNE("down")
  332.       end
  333.       turnLeft()
  334.       go("forward", true)
  335.       turnRight()
  336.       go("forward", true)
  337.       turnLeft()
  338.       go("forward", true)
  339.       turnRight()
  340.       digNE("forward", false)
  341.       if i==1 then
  342.         if (getEmptySlots() < 3) then --place chest          
  343.           if not turtle.detect() then
  344.             print("Placing chest...")
  345.             placeChest("forward")
  346.           end
  347.           print("Unloading...")
  348.           prettyUnload("forward")
  349.         end        
  350.       end
  351.       turnLeft()
  352.       turnLeft()
  353.       go("forward", true)
  354.       go("forward", true)
  355.       if i==1 then
  356.         putDirtNE("down")
  357.       end
  358.       go("forward", true)
  359.       digNE("forward", false)
  360.       turnLeft()
  361.       digNE("forward", false)
  362.       turnLeft()
  363.       go("forward", true)
  364.       turnRight()
  365.       go("forward", true)
  366.       go("forward", true)
  367.       turnRight()
  368.       turnRight()
  369.       go("up", true)
  370.       myY = myY + 1
  371.     end
  372.     turnRight()
  373.     digNE("forward", false)
  374.     placeTorch()
  375.     turnLeft()
  376.     go("up", true)
  377.     myY = myY + 1
  378.   end
  379.   for l=myY,baseY-1,1 do  --return to baseY
  380.     go("up", true)
  381.     myY = myY + 1
  382.   end
  383. end
  384.  
  385. -- make shaft hall, return and unload to placed chest FWD
  386. function makeShaftHall(tunnelLength)
  387.   print("Making shaft hall.")
  388.   local tunPos = 0    
  389.   while tunPos < tunnelLength do
  390.     go('forward', true)
  391.     --turn left and dig all in side & up
  392.     turnLeft()
  393.     for i=1,3,1 do
  394.       digNE('forward', false)
  395.       digNE('up', false)
  396.       go('up',true)
  397.     end
  398.     digNE('forward', false)
  399.     -- go right and again down
  400.     turnRight()
  401.     turnRight()
  402.     digNE('forward', false)
  403.     if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
  404.       placeTorch() -- place torch on right side above tunnels
  405.     end
  406.     for i=1,3,1 do
  407.       go('down',true)
  408.       digNE('forward', false)
  409.     end
  410.     turnLeft()
  411.     if tunPos < (tunnelLength-1) then
  412.       digNE('forward', false)
  413.     end  
  414.     if checkEmpty('down') then
  415.       print("making bridge")
  416.       putDirtBlock("down") -- for fucking peoples
  417.     end
  418.     tunPos = tunPos + 1
  419.     if math.floor(tunPos/8) == (tunPos/8) then
  420.        if (tunPos~=tunnelLength) and (getEmptySlots() < 3) then
  421.            print("Repacking..")
  422.            prettyRepack()
  423.        end
  424.      end    
  425.   end
  426.   print("Fin! I go back.")
  427.   turnRight()
  428.   go('up', true) -- place Sign on right side
  429.   local signText = mineName.."\nLevel: "..currentLevel.." ("..currentY..")\nLength: "..tunnelLength.."\nEXIT --->"
  430.   placeSign(signText)
  431.   go('down', true)
  432.   turnRight()  
  433.   while tunPos > 1 do
  434.     go('forward', true)
  435.     tunPos = tunPos - 1
  436.   end
  437.   turnLeft()
  438.   go('up', true) -- place Sign on right side
  439.   local signText = mineName.."\nLevel: "..currentLevel.." ("..currentY..")\nLength: "..tunnelLength.."\n< MINE | EXIT >"
  440.   placeSign(signText)
  441.   go('down', true)
  442.   turnRight()
  443.   go('forward', true)
  444.   turnLeft()
  445.   turnLeft()
  446.   --we at start position  
  447.   while not placeChest() do
  448.     awaitFix("Can't place chest")
  449.   end
  450.   prettyUnload("forward")
  451. end
  452.  
  453. -- makes single shaft
  454. -- 1: turtle creates shaft fwd
  455. -- 2: digging..
  456. -- z: turtle behind shaft place chest if need and unload
  457. function makeShaft(tunnelLength)
  458.   print("Making single shaft: "..tunnelLength.." blocks")
  459.   local tunPos = 0
  460.   local lastTorch = 0
  461.   local fromSlot = dirtBlockCount + 1
  462.   while tunPos < tunnelLength do    
  463.     --first fwd
  464.     go('forward', true)
  465.     --turn left and look
  466.     turnLeft() 
  467.     digNEND("fwd", true)
  468.     go('up', true)
  469.     digNEND("fwd", true)
  470.     go('up', true)
  471.     digNEND("fwd", true)
  472.     if lastTorch >=torchesEvery then --torches   
  473.       turnLeft() --turn back
  474.       placeTorch()  --back
  475.       turnRight()        
  476.       lastTorch = 0
  477.     end
  478.         digNEND("up", true)  --look up
  479.     turnRight()
  480.     turnRight()
  481.     digNEND("fwd", true)
  482.     go('down', true)
  483.     digNEND("fwd", true)
  484.     go('down', true)
  485.     digNEND("fwd", true)
  486.     turnLeft()
  487.     if not checkEmpty('down') then
  488.       if not checkDirt('down') then    
  489.         dig("down")
  490.         putDirtBlock("down")
  491.       end
  492.     else
  493.       print("Making bridge...")
  494.       putDirtBlock("down") -- for fucking peoples
  495.     end
  496.         if math.floor(tunPos/8) == (tunPos/8) then
  497.           if (tunPos~=tunnelLength) and (getEmptySlots() < 2) then
  498.              print("Repacking..")
  499.              prettyRepack()
  500.           end
  501.         end  
  502.     --and repeat process
  503.     tunPos = tunPos + 1
  504.     lastTorch = lastTorch + 1
  505.   end
  506.   print("Fin! I'll go back")
  507.   turnLeft()
  508.   turnLeft()  
  509.   while tunPos > 1 do
  510.     go('forward', true)
  511.     tunPos = tunPos - 1
  512.   end
  513.   putDirtBlock("up") --close tunnel
  514.   go('forward', true)
  515.   tunPos = tunPos - 1  
  516.   print("Returned from shaft!")
  517.  
  518. --  if (getEmptySlots() < 3) then --place chest
  519. --    if not turtle.detect() then
  520. --      print("Placing chest...")
  521. --      placeChest("forward")
  522. --    end
  523. --    print("Unloading...")
  524. --    prettyUnload("forward")
  525. --  end
  526. end
  527.  
  528. -- BOOL if too small resources
  529. function isNeedstoReturn()
  530.   local result = (turtle.getItemCount(16) < 5) or (turtle.getItemCount(15) < 10) or (turtle.getItemCount(14) < 4) or (turtle.getItemCount(13) < 4)
  531.   return result
  532. end
  533.  
  534. -- INT place chest FWD
  535. function placeChest()
  536.   ensureFuel()
  537.   if turtle.getItemCount(13) <=1 then
  538.     print("I have no chests...")
  539.     return false
  540.   else
  541.     selectSlot(13)
  542.     if not turtle.place() then
  543.       print("Can't place chest!")
  544.       return false
  545.     end
  546.   end
  547.   return true
  548. end
  549.  
  550. -- INT place sign FWD with label
  551. function placeSign(label)
  552.   ensureFuel()
  553.   if turtle.getItemCount(14) <=1 then
  554.     print("I have no signs...")
  555.     return false
  556.   else
  557.     selectSlot(14)
  558.     if not turtle.place(label) then
  559.       print("Can't place sign!")
  560.       return false
  561.     end
  562.   end
  563.   return true
  564. end
  565.  
  566. -- INT get count of empty slots
  567. function getEmptySlots()
  568.   local emptyslots = 0
  569.   for slot=maxSlot,minSlot,-1 do
  570.     if turtle.getItemCount(slot)==0 then
  571.       emptyslots = emptyslots + 1
  572.     end
  573.   end
  574.   return emptyslots
  575. end
  576.  
  577. -- compress items in inventory and kick dirt
  578. function prettyRepack()    
  579.   local isDirt = true
  580.   local cnt = 0
  581.   for slot=maxSlot,minSlot,-1 do
  582.     if turtle.compareTo(16) then --this is fuel, okay!
  583.       turtle.transferTo(16)
  584.     end
  585.     selectSlot(slot)
  586.     isDirt = false
  587.     for dirt=1,dirtBlockCount,1 do  
  588.       if turtle.compareTo(dirt) then
  589.         isDirt = true
  590.       end
  591.     end
  592.     --
  593.     for slotB=minSlot,maxSlot,1 do
  594.       if turtle.compareTo(slotB) then
  595.         --try to fill B (move to upslot)
  596.         turtle.transferTo(slotB)
  597.       end
  598.     end    
  599.     if (turtle.getItemCount(slot) ~= 0) and (getEmptySlots() < 2) and isDirt then
  600.       print("I'l drop this stuff cuz need more space!")
  601.       turtle.drop()  -- we have no two empty slots and drop all dirt in this slot
  602.     end
  603.   end
  604.   for dirt=1,dirtBlockCount,1 do
  605.      cnt = turtle.getItemCount(dirt)
  606.      if cnt > 10 then
  607.        selectSlot(dirt)
  608.        turtle.drop(cnt-10)  -- save 10 blocks to put in holes
  609.      end
  610.   end
  611. end
  612.  
  613. --unload to side chest
  614. -- TODO: make unloading not dirt item first
  615. function prettyUnload(direction)
  616.   ensureFuel()
  617.   for slot=minSlot,maxSlot,1 do
  618.     selectSlot(slot)
  619.     if turtle.compareTo(16) then
  620.       --try to store fuel
  621.       turtle.transferTo(16)
  622.       print("Fuel! I'll take a little bit.")
  623.     end
  624.     if direction=="up" then
  625.       turtle.dropUp()
  626.     elseif direction=="down" then
  627.       turtle.dropDown()
  628.     else
  629.       turtle.drop()
  630.     end
  631.   end
  632.   for dirt=1,dirtBlockCount,1 do
  633.      cnt = turtle.getItemCount(dirt)
  634.      if cnt > 10 then
  635.        selectSlot(dirt)
  636.        turtle.drop(cnt-10)  -- save 10 blocks to put in holes
  637.      end
  638.   end  
  639. end
  640.  
  641. --reload from side chest
  642. function reload(direction)
  643.   ensureFuel()  
  644.   for slot=13,16,1 do
  645.     selectSlot(slot)
  646.    if direction=="up" then
  647.       turtle.suckUp()
  648.     elseif direction=="down" then
  649.       turtle.suckDown()
  650.     else
  651.       turtle.suck()
  652.     end        
  653.     for slot2=minSlot,maxSlot,1 do
  654.       if turtle.compareTo(slot) then
  655.         selectSlot(slot2)
  656.         -- unload unused
  657.         if direction=="up" then
  658.           turtle.dropUp()
  659.         elseif direction=="down" then
  660.           turtle.dropDown()
  661.         else
  662.           turtle.drop()
  663.         end
  664.       end        
  665.     end
  666.   end  
  667. end
  668.  
  669. -- dig if block ON side and put Dirt - crazy %)
  670. function digNE(direction, putDirt)
  671.  if not checkEmpty(direction) then
  672.     while not checkEmpty(direction) do  --for gravel ore
  673.       dig(direction)
  674.     end
  675.     if putDirt then
  676.       putDirtBlock(direction)
  677.     end    
  678.   end
  679. end
  680.  
  681. -- dig if block ON side and this block NOT dirt, and put Dirt
  682. function digNEND(direction, putDirt)
  683.   if not checkEmpty(direction) then
  684.     if not checkDirt(direction) then
  685.     while not checkEmpty(direction) do  --for gravel ore
  686.       dig(direction)
  687.         end
  688.         if putDirt then
  689.           putDirtBlock(direction)
  690.         end      
  691.     end
  692.   end
  693. end
  694.  
  695. -- BOOL place torch on side
  696. function placeTorch()  
  697.   ensureFuel()
  698.   if turtle.getItemCount(15) <=1 then
  699.     print("I have no torches...")
  700.     return false
  701.   else
  702.     selectSlot(15)
  703.     if not turtle.place() then
  704.       print("Can't place torch!")
  705.       return false
  706.     end
  707.   end
  708.   return true
  709. end
  710.  
  711. --
  712. function putDirtNE(side)
  713.  if not checkEmpty(direction) then
  714.    putDirtBlock(direction)
  715.   end
  716. end
  717.  
  718. -- BOOL put dirt block on side
  719. function putDirtBlock(side)
  720.   ensureFuel()
  721.   local toSelect = 1    
  722.   toSelect = getDirtBlock()
  723.   if toSelect==false then
  724.     return false
  725.   end
  726.   selectSlot(toSelect)
  727.   if side=="up" then
  728.     return turtle.placeUp()
  729.   elseif side=="down" then
  730.     return turtle.placeDown()
  731.   else
  732.     return turtle.place()
  733.   end  
  734.   return  false
  735. end
  736.  
  737. -- INT get first inventory id with dirt blocks
  738. function getDirtBlock()
  739.   if dirtBlockCount==0 then
  740.     selectSlot(1)
  741.     while turtle.getItemCount(currentlySelectedSlot)~=0 do
  742.       dirtBlockCount = dirtBlockCount + 1
  743.       selectSlot(currentlySelectedSlot + 1)
  744.     end
  745.   end
  746.   if dirtBlockCount==0 then
  747.     return false
  748.   end
  749.   local fromSlot = dirtBlockCount + 1
  750.   for i=fromSlot,11,1 do
  751.     if turtle.getItemCount(i)>1 then
  752.       selectSlot(i)
  753.       for j=1,dirtBlockCount,1 do
  754.         if turtle.compareTo(j) then
  755.             return i
  756.         end
  757.       end
  758.     end
  759.   end  
  760.   for i=1,dirtBlockCount,1 do
  761.     if turtle.getItemCount(i)>2 then --get from 1st slot
  762.       return i
  763.     end
  764.   end
  765.   return false
  766. end
  767.  
  768. -- BOOL check if block on side is Dirt
  769. function checkDirt(side)
  770.   local result = false
  771.   if dirtBlockCount==0 then
  772.     getDirtBlock()
  773.   end
  774.   for j=1,dirtBlockCount,1 do
  775.     selectSlot(j)
  776.     if side=="up" then
  777.       result = turtle.compareUp()
  778.     elseif side=="down" then
  779.       result = turtle.compareDown()
  780.     else
  781.       result = turtle.compare()
  782.     end
  783.     if result then
  784.       return result
  785.     end  
  786.   end
  787.   return result
  788. end
  789.  
  790. -- BOOL check if block on side is not Exists
  791. function checkEmpty(side)
  792.     local result = false
  793.     if side=="up" then
  794.       result = not turtle.detectUp()
  795.     elseif side=="down" then
  796.       result = not turtle.detectDown()
  797.     else
  798.       result = not turtle.detect()
  799.     end
  800.     return result
  801. end
  802.  
  803. --select slot
  804. function selectSlot(x)
  805.   if x<1 then
  806.     x = 1
  807.   elseif x > 16 then
  808.     x = 16
  809.   end
  810.   turtle.select(x)
  811.   currentlySelectedSlot = x
  812. end
  813.  
  814.  
  815. --Check fuel, refueling from inventory special slot or not
  816. function ensureFuel()
  817.   -- Determine whether a refuel is required
  818.   local fuelLevel = turtle.getFuelLevel()
  819.   if (fuelLevel ~= "unlimited") then
  820.     if (fuelLevel < fuelLevelToRefuelAt) then
  821.       -- Need to refuel
  822.       selectSlot(16)
  823.       local fuelItems = turtle.getItemCount(16)
  824.       -- Do we need to impact the emergency fuel to continue? (always  
  825.       -- keep one fuel item in slot 16)
  826.       if (fuelItems == 0) then
  827.         awaitFix("I want more fuel!")
  828.       elseif (fuelItems == 1) then
  829.         --load fuel from another slot
  830.     local fueled = false
  831.     local fromSlot = dirtBlockCount + 1
  832.         for i=fromSlot,15,1 do
  833.           selectSlot(i)
  834.       if (turtle.compareTo(16)) then
  835.         print("Refueling from not fuel slot: "+i)
  836.         turtle.refuel(1)
  837.         turtle.transferTo(16)          
  838.         fueled = true
  839.           end
  840.         end
  841.         if not fueled then
  842.           print("completely out of fuel!")
  843.         end
  844.       else --fuelItems > 1
  845.         print("Time to refueling!")
  846.         turtle.refuel(1)
  847.       end
  848.     end
  849.   end
  850. end
  851.  
  852.  
  853. --turns
  854. function turnFacing(facing)
  855.   while facing ~= currentFacing do
  856.     turnRight()
  857.   end
  858. end
  859.  
  860. --turns
  861. function turnLeft()
  862.   ensureFuel()
  863.   turtle.turnLeft()
  864.   currentFacing = currentFacing - 1
  865.   if currentFacing < 0 then
  866.     currentFacing = 3
  867.   end
  868. end
  869.  
  870. --turns
  871. function turnRight()
  872.   ensureFuel()
  873.   turtle.turnRight()
  874.   currentFacing = currentFacing + 1
  875.   if currentFacing >= 4 then
  876.     currentFacing = 0
  877.   end
  878. end
  879.  
  880. -- BOOL dig direction with retries
  881. function dig(direction)
  882.   local result = false
  883.   local tries = 0
  884.   selectSlot(1)
  885.   while (not result) and (tries < 10) do
  886.     ensureFuel()   
  887.     if direction=="up" then
  888.         result = turtle.digUp()
  889.     elseif direction=="down" then
  890.         result = turtle.digDown()    
  891.     else
  892.         result = turtle.dig()
  893.     end  
  894.     tries = tries + 1
  895.   end
  896.   if not result then
  897.     print ("can't dig in "..direction)
  898.   end
  899.   return result
  900. end
  901.  
  902. --go direction with breaking blocks and refueling
  903. function go(direction, allowDig)
  904.     if direction=="right" then
  905.       turnRight()
  906.       direction="forward"
  907.     end
  908.     if direction=="left" then
  909.       turnLeft()
  910.       direction="forward"
  911.     end
  912.     if direction=="forward" then      
  913.        while turtle.detect() do
  914.          if allowDig then
  915.            ensureFuel()
  916.            turtle.dig()
  917.          else
  918.            awaitFix("can't forward")
  919.          end
  920.        end
  921.        
  922.        while not turtle.forward() do
  923.          ensureFuel()
  924.          turtle.dig()      
  925.        end
  926.     end
  927.     if direction=="up" then
  928.        while turtle.detectUp() do
  929.          if allowDig then
  930.            ensureFuel()
  931.            turtle.digUp()
  932.          else
  933.            awaitFix("can't up")
  934.          end
  935.        end     
  936.        while not turtle.up() do
  937.            ensureFuel()
  938.            turtle.digUp()      
  939.        end
  940.     end
  941.     if direction=="down" then
  942.        while turtle.detectDown() do
  943.          if allowDig then
  944.            ensureFuel()
  945.            turtle.digDown()
  946.          else
  947.            awaitFix("can't down")
  948.          end
  949.        end     
  950.         while not turtle.down() do
  951.            ensureFuel()
  952.            turtle.digDown()    
  953.        end
  954.     end
  955. end
  956.  
  957. --wait for user action
  958. function awaitFix(message)
  959.     print("I have problem: " .. message)
  960.     print("Please fix it and press Enter to continue")
  961.     io.read()
  962. end
  963.  
  964. function getRandomName()
  965.   math.randomseed(os.time())
  966.   local Names = {"HollyMine","DirtyMine","DataMine","PooMine","HolaMine","SherMines","OreMine","CrocoMine","CrazyShitMine","NaziMine","Querty","NotQuarry","GayMine","FullShitMine","0001001"}
  967.   return Names[math.random(1,#Names)]
  968. end
  969.  
  970. main() --execute ALL
Advertisement
Add Comment
Please, Sign In to add comment