shiryavsky

mineX2

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