rdalkire

Obsidian Miner 1.0.0

Nov 10th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2015
  3. Robert David Alkire II, IGN ian_xw
  4. Distributed under the MIT License.
  5. (See accompanying file LICENSE or copy
  6. at http://opensource.org/licenses/MIT)
  7. ]]
  8.  
  9. local trtl
  10. if turtle then
  11.   trtl = turtle
  12. else
  13.   trtl = require "mockTurtle"
  14. end
  15.  
  16. local oMnr = {}
  17.  
  18. oMnr.MxSlt = 11
  19. oMnr.MXLTH = 50
  20.  
  21. local slotLngth = 0
  22. local OKFuel = true
  23. local theresLava = true
  24.  
  25. -- Fetches a bucket of lava and places
  26. -- it into the slot.
  27. -- returns canGo (true if the turtle
  28. --  can move to the next place in the
  29. --  slot), and sltDone (if this
  30. -- position is more than MxSlt or it
  31. -- was blocked)
  32. oMnr.fetchLavaBucket = function()
  33.  
  34.     -- Fetches a bucket-full
  35.     local gotBckt = false
  36.     local cntFwds = 0
  37.     local lp = false
  38.    
  39.     local canGo = true
  40.    
  41.     -- Until has bucket or obstructed
  42.     while not gotBckt and canGo do
  43.       canGo = trtl.forward()
  44.       if canGo then
  45.         cntFwds = cntFwds + 1
  46.         -- Tries to get bucket of lava
  47.         gotBckt = trtl.placeDown()
  48.         if gotBckt then
  49.           theresLava = true
  50.         end
  51.       end
  52.      
  53.       -- Limits any robot run-away
  54.       if cntFwds >= oMnr.MXLTH then
  55.         canGo = false
  56.       end
  57.     end
  58.    
  59.     print("gotBckt, canGo: ",
  60.         gotBckt, canGo )
  61.    
  62.     -- Takes the lava back to the slot
  63.     for bk = 1, cntFwds do
  64.       trtl.back()
  65.     end
  66.    
  67.     -- Into slot, places lava, rises
  68.     trtl.down()
  69.     lp = trtl.placeDown()
  70.     trtl.up()
  71.    
  72.     -- Tries to get to next place in slot
  73.     trtl.turnLeft()
  74.     canGo = trtl.forward()
  75.     trtl.turnRight()
  76.     if canGo then
  77.       slotLngth = slotLngth + 1
  78.     end
  79.    
  80.     -- Sees whether slot is done
  81.     local sltDone = false
  82.     if slotLngth >= oMnr.MxSlt or
  83.         not canGo then
  84.       sltDone = true
  85.       print( "slotLngth, canGo: ",
  86.           slotLngth, canGo )
  87.     end
  88.    
  89.     return canGo, sltDone
  90.  
  91. end
  92.  
  93. -- Fills the slot.  With lava.
  94. -- Also assigns the theresLava flag
  95. oMnr.fillSlot = function()
  96.   -- TODO Allow longer slots
  97.  
  98.   print("Starting fillSlot().")
  99.  
  100.   slotLngth = 0
  101.   theresLava = false
  102.   local sltDone = false
  103.  
  104.   while OKFuel and not sltDone do
  105.    
  106.     local canGo = true
  107.    
  108.     -- Ensure there's enough fuel to
  109.     -- get the next bucket and finish
  110.     local fuelLvl= trtl.getFuelLevel()
  111.    
  112.     if fuelLvl ~= "unlimited" then
  113.       local fuelNeed =
  114.           oMnr.MXLTH * 2 + -- a bucket
  115.           10 +          -- place water
  116.           24 +         -- to mine obby
  117.           slotLngth + 2 -- to get back
  118.      
  119.       if fuelNeed > fuelLvl then
  120.         OKFuel = false
  121.         canGo = false
  122.         print( "There may not be "..
  123.             "enough fuel to continue "..
  124.             "safely." )
  125.       end
  126.     end
  127.    
  128.     if canGo then
  129.       canGo, sltDone =
  130.           oMnr.fetchLavaBucket()
  131.     end
  132.    
  133.   end
  134.  
  135.   print( "theresLava: ", theresLava )
  136.  
  137. end
  138.  
  139. -- Gets and places water, to turn lava
  140. -- into obsidian.  Distances are
  141. -- currently hard-coded.
  142. -- Fuel cost: 10
  143. oMnr.getAndPlaceWater = function()
  144.  
  145.   print("Starting getAndPlaceWater()")
  146.  
  147.   -- Go to the infinite water source
  148.   for n = 1, 2 do trtl.forward() end
  149.   local lp = trtl.placeDown() -- gets water
  150.  
  151.   -- To start
  152.   for n = 1, 2 do trtl.back() end
  153.  
  154.   -- To middle of slot
  155.   local half = math.floor( slotLngth/2 )
  156.   for n = 1, half do trtl.back() end
  157.   lp = trtl.placeDown()
  158.  
  159.   -- Back to start again
  160.   for n = 1, half do trtl.forward() end
  161.   trtl.turnLeft()
  162.  
  163. end
  164.  
  165. -- Mines obsidian.
  166. -- Fuel cost, assuming slotLngth is
  167. -- MxSlt 11: 24
  168. oMnr.mineObby = function()
  169.   print("Starting mineObby()")
  170.   -- Mine the obby
  171.   trtl.turnLeft()
  172.   trtl.down()
  173.   local canDig = false
  174.   for mn = 1, slotLngth do
  175.     canDig = trtl.digDown()
  176.     trtl.forward()
  177.   end
  178.   trtl.digDown()
  179.   trtl.up()
  180.   -- Come on back
  181.   for b = 1, slotLngth do
  182.     trtl.back()
  183.   end
  184.   trtl.turnRight()
  185. end
  186.  
  187. oMnr.go = function()
  188.  
  189.   local invHasSpace = true;
  190.   local countGoRepeats = 0;
  191.  
  192.   while theresLava and invHasSpace and
  193.       OKFuel and
  194.       (countGoRepeats <= oMnr.MXLTH) do
  195.    
  196.     print( "countGoRepeats: ",
  197.         countGoRepeats )
  198.    
  199.     print("fuel level: ",
  200.         trtl.getFuelLevel() )
  201.    
  202.     oMnr.fillSlot()
  203.    
  204.     -- Back to beginning of slot
  205.     trtl.turnRight()
  206.     for bk = 1, slotLngth do
  207.       trtl.forward()
  208.     end
  209.    
  210.     if not theresLava then
  211.       trtl.turnLeft()
  212.     else
  213.       oMnr.getAndPlaceWater()  
  214.       oMnr.mineObby()
  215.      
  216.       -- Checks inventory space
  217.       local frSpace = 0
  218.       for i = 1, 16 do
  219.         frSpace= frSpace+ trtl.getItemSpace(i)
  220.       end
  221.       invHasSpace= frSpace >= slotLngth
  222.       print("free space: ", frSpace)
  223.      
  224.       countGoRepeats= countGoRepeats+ 1
  225.     end
  226.   end
  227.  
  228. end
  229.  
  230. oMnr.go()
  231.  
  232. return oMnr
Advertisement
Add Comment
Please, Sign In to add comment