LayZee

ComputerCraft lzStrip

Jun 21st, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 KB | None | 0 0
  1. --[[
  2.  lzStrip Turtle program
  3.  
  4.  Strip mine while chunk loaded
  5.  
  6.  Dependencies:
  7.  - LAMA: https://github.com/fnuecke/lama
  8.  
  9.  @copyright (C) LayZee 2014
  10. --]]
  11.  
  12. --[[
  13. os.loadAPI('lz')
  14. lz.load('turtle', 't')
  15. --]]
  16.  
  17. local DEFAULT_BRANCH_LENGTH = 60
  18. local DEFAULT_ENDER_CHEST_MODE = true
  19.  
  20. local config = {}
  21.  
  22. local arguments = {...}
  23.  
  24. -- Declare functions
  25. local branchMine, buildCeiling, buildCeilingAndFloor, buildFloor, dig1x3,
  26.     dig2x3, digDown, digForward, digMoveDown, digMoveForward, digMoveUp,
  27.     digToNextBranch, digUp, displayGuide, emptyInventory, emptyInventoryIfFull,
  28.     handleLowFuel, initializeConfig, initializeLama, moveBack, moveForward,
  29.     placeTorch, waitForFallingBlocks
  30.  
  31. branchMine = function()
  32.     local branch = 1
  33.  
  34.     while turtle.getFuelLevel() >= config.branchLength * 2 + 12 + config.branchLength / 7 do
  35.         for step = 1, config.branchLength do
  36.             dig2x3(step)
  37.         end
  38.  
  39.         branch = branch + 1
  40.         digToNextBranch(branch)
  41.     end
  42.  
  43.     handleLowFuel()
  44. end
  45.  
  46. buildCeiling = function()
  47.     if not turtle.detectUp() and turtle.getItemCount(config.cobblestoneSlot) > 1 then
  48.         turtle.select(config.cobblestoneSlot)
  49.         turtle.placeUp()
  50.     end
  51. end
  52.  
  53. buildCeilingAndFloor = function()
  54.     if lama.getY() == 1 then
  55.         buildCeiling()
  56.         digMoveDown()
  57.         buildFloor()
  58.     else
  59.         buildFloor()
  60.         digMoveUp()
  61.         buildCeiling()
  62.     end
  63. end
  64.  
  65. buildFloor = function()
  66.     if not turtle.detectDown() and turtle.getItemCount(config.cobblestoneSlot) > 1 then
  67.         turtle.select(config.cobblestoneSlot)
  68.         turtle.placeUp()
  69.     end
  70. end
  71.  
  72. dig1x3 = function(step)
  73.     -- Face left block
  74.     lama.turnLeft()
  75.     digForward()
  76.  
  77.     -- Face right block
  78.     lama.turnAround()
  79.     digForward()
  80.  
  81.     -- Place torch every 7 steps, starting at first step
  82.     if step % 7 == 1 and lama.getY() == 1 then
  83.         placeTorch()
  84.     end
  85.  
  86.     -- Face forward
  87.     lama.turnLeft()
  88.  
  89.     -- Build ceiling or floor
  90.     if lama.getY() == 1 then
  91.         buildCeiling()
  92.     else
  93.         buildFloor()
  94.     end
  95. end
  96.  
  97. dig2x3 = function(step)
  98.     emptyInventoryIfFull()
  99.  
  100.     digMoveForward()
  101.     dig1x3(step)
  102.  
  103.     if lama.getY() == 1 then
  104.         digMoveDown()
  105.     else
  106.         digMoveUp()
  107.     end
  108.  
  109.     dig1x3(step)
  110. end
  111.  
  112. digDown = function()
  113.     for try = 1, 120 do
  114.         local isBlocked = turtle.detectDown()
  115.  
  116.         if not isBlocked then
  117.             break
  118.         end
  119.  
  120.         turtle.digDown()
  121.         waitForFallingBlocks()
  122.     end
  123.  
  124.     local tries = 0
  125. end
  126.  
  127. digForward = function()
  128.     for try = 1, 120 do
  129.         local isBlocked = turtle.detect()
  130.  
  131.         if not isBlocked then
  132.             break
  133.         end
  134.  
  135.         turtle.dig()
  136.         waitForFallingBlocks()
  137.     end
  138.  
  139.     local tries = 0
  140. end
  141.  
  142. digMoveDown = function()
  143.     turtle.select(1)
  144.  
  145.     lama.down(120, true)
  146. end
  147.  
  148. digMoveForward = function()
  149.     turtle.select(1)
  150.  
  151.     lama.forward(120, true)
  152. end
  153.  
  154. digMoveUp = function()
  155.     turtle.select(1)
  156.  
  157.     lama.up(120, true)
  158. end
  159.  
  160. digToNextBranch = function(branch)
  161.     local isOddBranch = branch % 2 == 1
  162.     local turnAction
  163.  
  164.     if isOddBranch then
  165.         turnAction = lama.turnRight
  166.     else
  167.         turnAction = lama.turnLeft
  168.     end
  169.  
  170.     digMoveForward()
  171.     buildCeilingAndFloor()
  172.     turnAction()
  173.  
  174.     for step = 1, 5 do
  175.         digMoveForward()
  176.         buildCeilingAndFloor()
  177.     end
  178.  
  179.     turnAction()
  180. end
  181.  
  182. digUp = function()
  183.     for try = 1, 120 do
  184.         local isBlocked = turtle.detectUp()
  185.  
  186.         if not isBlocked then
  187.             break
  188.         end
  189.  
  190.         turtle.digUp()
  191.         waitForFallingBlocks()
  192.     end
  193.  
  194.     local tries = 0
  195. end
  196.  
  197. displayGuide = function()
  198.     local chestString
  199.  
  200.     if config.enderChestMode then
  201.         chestString = 'an Ender Chest'
  202.     else
  203.         chestString = 'Chests'
  204.     end
  205.  
  206.     print("lzStrip v0.3")
  207.     print("(C) LayZee 2014")
  208.     print("")
  209.     print("Place Cobblestones in slot 1")
  210.     print("Place Torches in slot 15")
  211.     print("Place " .. chestString .. " in slot 16")
  212.     print("Press Enter when ready")
  213.  
  214.     local _, key
  215.  
  216.     repeat
  217.       _, key = os.pullEvent("key")
  218.     until key == keys.enter
  219.  
  220.     print 'Starting...'
  221. end
  222.  
  223. emptyInventory = function()
  224.     lama.turnAround()
  225.     digForward()
  226.     turtle.select(config.chestSlot)
  227.  
  228.     local chestString
  229.  
  230.     if config.enderChestMode then
  231.         chestString = 'Ender Chest'
  232.     else
  233.         chestString = 'Chest'
  234.     end
  235.  
  236.     while turtle.getItemCount(config.chestSlot) == 0 do
  237.         print('No ', chestString, ' in slot ', config.chestSlot, '. Press Enter when ready')
  238.  
  239.         repeat
  240.             local _, key = os.pullEvent('key')
  241.         until key == keys.enter
  242.     end
  243.  
  244.     turtle.place()
  245.  
  246.     for slot = 2, 14 do
  247.         if turtle.getItemCount(slot) > 0 then
  248.             turtle.select(slot)
  249.             turtle.drop()
  250.         end
  251.     end
  252.  
  253.     turtle.select(config.chestSlot)
  254.  
  255.     if config.enderChestMode then
  256.         digForward()
  257.     end
  258.  
  259.     lama.turnAround()
  260. end
  261.  
  262. emptyInventoryIfFull = function()
  263.     local inventoryIsFull = true
  264.  
  265.     for slot = 2, 14 do
  266.         if turtle.getItemCount(slot) == 0 then
  267.             inventoryIsFull = false
  268.  
  269.             break
  270.         end
  271.     end
  272.  
  273.     if inventoryIsFull then
  274.         emptyInventory()
  275.     end
  276. end
  277.  
  278. handleLowFuel = function()
  279.     print("Out of fuel")
  280.     print("")
  281.     print("1. Remove Cobblestones from slot 1")
  282.     print("2. Place fuel source in slot 1")
  283.     print("3. Press any key to refuel")
  284.     os.pullEvent("key")
  285.  
  286.     while turtle.getFuelLevel() < branchLength * 2 + 12 do
  287.       turtle.select(1)
  288.       lama.refuel()
  289.  
  290.       local timerId = os.startTimer(2)
  291.       local event = ""
  292.       local id = 0
  293.  
  294.       while timerId ~= id do
  295.         event, id = os.pullEvent("timer")
  296.       end
  297.     end
  298.  
  299.     print("4. Place Cobblestone in slot 1")
  300.     print("5. Press Enter to resume strip mining")
  301.  
  302.     repeat
  303.       event, key = os.pullEvent("key")
  304.     until key == keys.enter
  305. end
  306.  
  307. initializeConfig = function()
  308.     local branchLength = tonumber(arguments[1])
  309.  
  310.     branchLength = branchLength or DEFAULT_BRANCH_LENGTH
  311.  
  312.     local enderChestMode = tostring(arguments[2])
  313.  
  314.     if enderChestMode == nil then
  315.         enderChestMode = DEFAULT_ENDER_CHEST_MODE
  316.     elseif enderChestMode:lower() == 'enderchest' then
  317.         enderChestMode = true
  318.     elseif enderChestMode:lower() == 'chest' then
  319.         enderChestMode = false
  320.     end
  321.  
  322.     config.branchLength = branchLength
  323.     config.enderChestMode = enderChestMode
  324.     config.chestSlot = 16
  325.     config.cobblestoneSlot = 1
  326.     config.fuelSlot = 1
  327.     config.torchSlot = 15
  328. end
  329.  
  330. initializeLama = function()
  331.     print 'calling lama.init'
  332.     --lama.init()
  333.     print 'setting lama pos'
  334.     lama.set(0, 0, 0, lama.side.north)
  335.     print 'lama done'
  336. end
  337.  
  338. moveBack = function()
  339.     lama.back(120)
  340. end
  341.  
  342. moveForward = function()
  343.     lama.forward(120, true)
  344. end
  345.  
  346. placeTorch = function()
  347.     if turtle.getItemCount(config.torchSlot) > 1 then
  348.         turtle.select(config.torchSlot)
  349.  
  350.         if not turtle.place() and turtle.getItemCount(config.cobblestoneSlot) > 1 then
  351.             moveForward()
  352.             turtle.select(config.cobblestoneSlot)
  353.             turtle.place()
  354.             moveBack()
  355.             turtle.select(config.torchSlot)
  356.             turtle.place()
  357.         end
  358.     end
  359. end
  360.  
  361. waitForFallingBlocks = function()
  362.     local timerId = os.startTimer(0.8)
  363.  
  364.     repeat
  365.         local _, id = os.pullEvent('timer')
  366.     until id == timerId
  367. end
  368.  
  369. -- Program entry point
  370. (function()
  371.     os.loadAPI('apis/lama')
  372.     initializeConfig()
  373.     displayGuide()
  374.     print 'Initializing Lama...'
  375.     initializeLama()
  376.     print 'Initialized Lama'
  377.  
  378.     while true do
  379.         branchMine()
  380.     end
  381. end)()
Advertisement
Add Comment
Please, Sign In to add comment