Advertisement
Zekrommaster110

test

Feb 2nd, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.17 KB | None | 0 0
  1. --[[
  2. MIT LICENCE
  3.  
  4. Copyright (c) 2020 zekro Development
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. --]]
  24.  
  25. -----------------------------------------------------------------
  26. -- CONFIGURABLE VALUES
  27. -----------------------------------------------------------------
  28. -- DELAY is the ammount of seconds which are
  29. -- waited until the next block in front of or
  30. -- above the turtle will be detected to dig
  31. -- falling blocks like sand or gravel.
  32. -- 0.4s is the minimum which should be set. If
  33. -- you notice problems digging gravel or sand,
  34. -- you should crank this up a bit.
  35. local DELAY  = 0.4
  36. -- DEPTH is the length of the corridor arms
  37. -- which are digged left and right of the mine
  38. -- shaft.
  39.     local DEPTH  = 4
  40. -- SPACES is the ammount of inventory fields
  41. -- which must be free to dig a new corridor
  42. -- part. If this value is 1, for example, the
  43. -- turtle will go back to the base to drop the
  44. -- inventory, if set on startup, when only
  45. -- 1 or less inventory fields are free.
  46.     local SPACES = 1
  47. -----------------------------------------------------------------
  48.  
  49. local VERSION = '4.0.1-ccv1.6'
  50. local ARG, ARG2, ARG3 = ...
  51.  
  52. local PROGNAME = shell.getRunningProgram()
  53. local HELP = 'Usage: ' .. PROGNAME .. ' LEN [STORE]\n\
  54. LEN is the length of the mining shaft\
  55. in corridors, so the total length is\
  56. LEN * 3 blocks.\n\
  57. STORE is the type of storage used:\
  58. - 0: no storage (default)\
  59. - 1: double/single chest'
  60.  
  61. local VERSIONTXT = 'stripmining v.' .. VERSION .. '\
  62. Compatible with ComputerCraft >= 1.6.\n\
  63. (c) 2020 zekro Development\
  64. Covered by the MIT Licence.'
  65.  
  66. ----------------------------------------------------------------
  67. -- HELPER FUNCTIONS
  68. ----------------------------------------------------------------
  69.  
  70. -- Output help message and exit program
  71. -- with error().
  72. function printHelp(extended)
  73.     print(HELP)
  74.     error()
  75. end
  76.  
  77. -- Clear screen.
  78. function cls()
  79.     shell.run('clear')
  80. end
  81.  
  82. -- Returns the number of the first slot
  83. -- of the turtles inventory which is not
  84. -- empty. If all slots are empty, 0 will
  85. -- be returned.
  86. function firstInvNotEmpty()
  87.     for i = 1, 16, 1 do
  88.         if turtle.getItemCount(i) > 0 then
  89.             return i
  90.         end
  91.     end
  92.     return 0
  93. end
  94.  
  95. -- Returns the ammount of inventory fields
  96. -- that are empty.
  97. function emptyInvFields()
  98.     local empty = 0
  99.     for i = 1, 16, 1 do
  100.         if turtle.getItemCount(i) == 0 then
  101.             empty = empty + 1
  102.         end
  103.     end
  104.     return empty
  105. end
  106.  
  107. -- Blocks until a key was pressed and then
  108. -- returns the name of the pressed key.
  109. function pullKeyEvent(txt)
  110.     if txt then
  111.         print(txt)
  112.     end
  113.     local _, key, _ = os.pullEvent("key")
  114.     return keys.getName(key)
  115. end
  116.  
  117. -- Refuels the turtle with all valid fuel
  118. -- items ignoring slow 1 and 2.
  119. function refuel()
  120.     for i = 3, 16, 1 do
  121.         turtle.select(i)
  122.         if turtle.refuel(0) then
  123.             turtle.refuel(64)
  124.         end
  125.     end
  126. end
  127.  
  128. ----------------------------------------------------------------
  129. -- TURTLE SHORTCUTS
  130. ----------------------------------------------------------------
  131.  
  132. -- Executes the passed function, which
  133. -- returns true on success, n times and
  134. -- retries the function if it fails after
  135. -- 0.1 a delay of seconds.
  136. function _do(n, f)
  137.     n = n and n or 1
  138.     for i = 1, n, 1 do
  139.         while not f() do
  140.             sleep(0.1)
  141.         end
  142.     end
  143. end
  144.  
  145. -- Tries to move the turthe forward
  146. -- n times.
  147. function goForward(n)
  148.     _do(n, turtle.forward)
  149. end
  150.  
  151. -- Tries to move the turthe back
  152. -- n times.
  153. function goBack(n)
  154.     _do(n, turtle.back)
  155. end
  156.  
  157. -- Tries to move the turthe up
  158. -- n times.
  159. function goUp(n)
  160.     _do(n, turtle.up)
  161. end
  162.  
  163. -- Tries to move the turthe down
  164. -- n times.
  165. function goDown(n)
  166.     _do(n, turtle.down)
  167. end
  168.  
  169. -- Tries to turn the turtle n times
  170. -- to the right.
  171. function turnRight(n)
  172.     _do(n, turtle.turnRight)
  173. end
  174.  
  175. -- Tries to turn the turtle n times
  176. -- to the left.
  177. function turnLeft(n)
  178.     _do(n, turtle.turnLeft)
  179. end
  180.  
  181. -- Drops the whole inventory except the
  182. -- items in the first slot. Also, at least
  183. -- 5 blocks for filling ground are kept in
  184. -- the first slot of the inventory.
  185. function emptyInv()
  186.     local amm1 = turtle.getItemCount(2)
  187.     local dropAmm = amm1 <= 5 and 0 or (amm1 - 5)
  188.     turtle.select(2)
  189.     turtle.drop(dropAmm)
  190.     for i = 3, 16, 1 do
  191.         turtle.select(i)
  192.         turtle.drop(64)
  193.     end
  194.     turtle.select(1)
  195. end
  196.  
  197. ----------------------------------------------------------------
  198. -- PROGRAM STRUCTURE FUNCTIONS
  199. ----------------------------------------------------------------
  200.  
  201. -- Checks if the turtle has a label set and
  202. -- warns the user if the turtle has no label set.
  203. function checkLabel()
  204.     if not os.getComputerLabel() then
  205.         print('ATTENTION!\nThis computer does not have a label!\n' ..
  206.               'You should set a label to this computer to save ' ..
  207.               'fuel and program state on breaking the computer.\n')
  208.         local key = pullKeyEvent('Do you want to continue anyway? [yN]')
  209.         if key ~= 'y' then
  210.             print('Canceled.')
  211.             error()
  212.         end
  213.     end
  214. end
  215.  
  216. -- Checks if the turtle has estimated enought
  217. -- fuel to fulfil the mining process and refuels
  218. -- the turtle on user input of fuel into the
  219. -- inventory.
  220. function checkFuel(len)
  221.     cls()
  222.     local fuelRequired = len * 30
  223.     local fuelExistent = turtle.getFuelLevel()
  224.     local fuelDelta = fuelRequired - fuelExistent
  225.     if fuelExistent < fuelRequired then
  226.         print('Not enough fuel to fulfil the process. Please refuel:\n\n' ..
  227.               ' - ' .. fuelDelta / 80 .. ' coal\n' ..
  228.               ' - ' .. fuelDelta / 15 .. ' wooden plancks\n\n' ..
  229.               'Just put the fuel to be refueld somewhere in the turtles inventory.')
  230.         while firstInvNotEmpty() < 3 do
  231.             sleep(0.5)
  232.         end
  233.         refuel()
  234.         return false
  235.     end
  236.     return true
  237. end
  238.  
  239. -- Displays fuel level and shows information to
  240. -- prepare the inventory of the turtle for
  241. -- the mining process.
  242. function checkRequirements(len, store)
  243.     cls()
  244.     write('Fuel level is ' .. turtle.getFuelLevel() .. '.\n\n' ..
  245.           'Please put ' .. len .. ' torches in slot 1')
  246.     if store > 0 then
  247.         write(' and one or two chests in slot 2')
  248.     end
  249.     print('.\n\nThen press any key to continue or [c] to cancel.')
  250.     local key = pullKeyEvent()
  251.     if key == 'c' then
  252.         print('Canceled.')
  253.         error()
  254.     end
  255. end
  256.  
  257. -- Digs and moves forward by digging 1x2x1
  258. -- blocks with checking for falling blocks
  259. -- (like sand or gravel) and missing ground
  260. -- blocks which will be filled.
  261. function digForward(n)
  262.     n = n and n or 1
  263.     for i = 1, n, 1 do
  264.         while turtle.detect() do
  265.             turtle.dig()
  266.             sleep(DELAY)
  267.         end
  268.         goForward()
  269.         sleep(DELAY)
  270.         while turtle.detectUp() do
  271.             turtle.digUp()
  272.             sleep(DELAY)
  273.         end
  274.         if not turtle.detectDown() then
  275.             turtle.select(2)
  276.             turtle.placeDown()
  277.         end
  278.     end
  279. end
  280.  
  281. -- Digs the base for one or two chests,
  282. -- depending on passed chest count.
  283. -- If storage type is > 0 and 0 chests
  284. -- are passed, a base bay of the size of
  285. -- 1 will be created where items will
  286. -- be like there was a chest.
  287. function makeBase(store, chests)
  288.     local place = true
  289.     if chests == 0 then
  290.         place = false
  291.         chests = 1
  292.     else
  293.         chests = chests > 2 and 2 or chests
  294.     end
  295.     for i = 1, chests, 1 do
  296.         digForward()
  297.         turnRight()
  298.         digForward()
  299.         goBack()
  300.         if place then
  301.             turtle.select(2)
  302.             turtle.place()
  303.             turtle.select(1)
  304.         end
  305.         turnLeft()
  306.     end
  307. end
  308.  
  309. -- One instance of digging one corridor
  310. -- pair with or without placing a torch,
  311. -- depending on the passed value.
  312. function digCorridorPart(torches)
  313.     digForward(3)
  314.     if torches > 0 then
  315.         goBack()
  316.         turtle.select(1)
  317.         turtle.placeUp()
  318.         digForward()
  319.     end
  320.     turnRight()
  321.     digForward(DEPTH)
  322.     turnRight(2)
  323.     digForward(DEPTH * 2)
  324.     turnRight(2)
  325.     digForward(DEPTH)
  326.     turnLeft()
  327. end
  328.  
  329. ----------------------------------------------------------------
  330. -- MAIN ROUTINE
  331. ----------------------------------------------------------------
  332.  
  333. if ARG == 'v' or ARG == 'version' then
  334.     print(VERSIONTXT)
  335.     error()
  336. end
  337.  
  338. local len = tonumber(ARG)
  339. local store = tonumber(ARG2)
  340. store = store and store or 0
  341.  
  342. if not len or len < 1 then
  343.     printHelp()
  344. end
  345.  
  346. if store and (store < 0 or store > 1) then
  347.     printHelp()
  348. end
  349.  
  350. checkLabel()
  351.  
  352. while not checkFuel(len) do end
  353.  
  354. checkRequirements(len, store)
  355.  
  356. local torches = turtle.getItemCount(1)
  357. local chests = turtle.getItemCount(2)
  358.  
  359. turtle.select(1)
  360.  
  361. if store > 0 then
  362.     makeBase(store, chests)
  363. end
  364.  
  365. local way = 0
  366. for i = 1, len, 1 do
  367.     digCorridorPart(torches)
  368.     torches = torches - 1
  369.     way = way + 3
  370.     if store > 0 and emptyInvFields() <= SPACES then
  371.         turnRight(2)
  372.         digForward(way)
  373.         turnLeft()
  374.         emptyInv()
  375.         turnLeft()
  376.         goForward(way)
  377.     end
  378. end
  379.  
  380. turnRight(2)
  381. digForward(way)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement