Zekrommaster110

[LUA | CC1.6] stripmining 4.1.0-ccv1.6

Jan 1st, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.83 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. -- UPDATEINFO defines if update information
  29. -- should be displayed on each startup when
  30. -- a new version of this program is released.
  31.     local UPDATEINFO = true
  32. -- DELAY is the ammount of seconds which are
  33. -- waited until the next block in front of or
  34. -- above the turtle will be detected to dig
  35. -- falling blocks like sand or gravel.
  36. -- 0.4s is the minimum which should be set. If
  37. -- you notice problems digging gravel or sand,
  38. -- you should crank this up a bit.
  39.     local DELAY  = 0.4
  40. -- DEPTH is the length of the corridor arms
  41. -- which are digged left and right of the mine
  42. -- shaft.
  43.     local DEPTH  = 4
  44. -- SPACES is the ammount of inventory fields
  45. -- which must be free to dig a new corridor
  46. -- part. If this value is 1, for example, the
  47. -- turtle will go back to the base to drop the
  48. -- inventory, if set on startup, when only
  49. -- 1 or less inventory fields are free.
  50.     local SPACES = 1
  51. -----------------------------------------------------------------
  52.  
  53. local VERSION = '4.1.0-ccv1.6'
  54. local PBCODE = 'UHsCxFk7'
  55. local ARG, ARG2, ARG3 = ...
  56.  
  57. local PROGNAME = shell.getRunningProgram()
  58. local HELP = 'Usage: ' .. PROGNAME .. ' LEN [STORE]\n\
  59. LEN is the length of the mining shaft\
  60. in corridors, so the total length is\
  61. LEN * 3 blocks.\n\
  62. STORE is the type of storage used:\
  63. - 0: no storage (default)\
  64. - 1: double/single chest'
  65.  
  66. local VERSIONTXT = 'stripmining v.' .. VERSION .. '\
  67. Compatible with ComputerCraft >= 1.6.\n\
  68. (c) 2020 zekro Development\
  69. Covered by the MIT Licence.'
  70.  
  71. ----------------------------------------------------------------
  72. -- HELPER FUNCTIONS
  73. ----------------------------------------------------------------
  74.  
  75. -- Output help message and exit program
  76. -- with error().
  77. function printHelp(extended)
  78.     print(HELP)
  79.     error()
  80. end
  81.  
  82. -- Clear screen.
  83. function cls()
  84.     shell.run('clear')
  85. end
  86.  
  87. -- Returns the number of the first slot
  88. -- of the turtles inventory which is not
  89. -- empty. If all slots are empty, 0 will
  90. -- be returned.
  91. function firstInvNotEmpty()
  92.     for i = 1, 16, 1 do
  93.         if turtle.getItemCount(i) > 0 then
  94.             return i
  95.         end
  96.     end
  97.     return 0
  98. end
  99.  
  100. -- Returns the ammount of inventory fields
  101. -- that are empty.
  102. function emptyInvFields()
  103.     local empty = 0
  104.     for i = 1, 16, 1 do
  105.         if turtle.getItemCount(i) == 0 then
  106.             empty = empty + 1
  107.         end
  108.     end
  109.     return empty
  110. end
  111.  
  112. -- Blocks until a key was pressed and then
  113. -- returns the name of the pressed key.
  114. function pullKeyEvent(txt)
  115.     if txt then
  116.         print(txt)
  117.     end
  118.     local _, key, _ = os.pullEvent("key")
  119.     return keys.getName(key)
  120. end
  121.  
  122. -- Refuels the turtle with all valid fuel
  123. -- items ignoring slot 1 and 2.
  124. function refuel()
  125.     for i = 3, 16, 1 do
  126.         turtle.select(i)
  127.         if turtle.refuel(0) then
  128.             turtle.refuel(64)
  129.         end
  130.     end
  131. end
  132.  
  133. -- Looks for the verson in the passed
  134. -- pastebin post and compares it with the
  135. -- version passed of the current file.
  136. -- If they differ, an update information
  137. -- will be printed and true will be returned.
  138. function checkVersion(version, pbCode, silent)
  139.     local CHECK = 'local VERSION = \''
  140.  
  141.     local res = http.get('https://pastebin.com/raw/' .. pbCode)
  142.     if not res or res.getResponseCode() ~= 200 then
  143.         return nil
  144.     end
  145.  
  146.     local line = ''
  147.     local vindex = nil
  148.  
  149.     while line do
  150.         line = res.readLine()
  151.         vindex = string.find(line, CHECK, 1, true)
  152.         if vindex then
  153.             break
  154.         end
  155.     end
  156.  
  157.     res.close()
  158.  
  159.     local latestVersion = string.sub(line, vindex + string.len(CHECK), -2)
  160.     local isOutdated = latestVersion ~= version
  161.     if isOutdated and silent ~= true then
  162.         print('New version available!')
  163.         print('Do: pastebin get ' .. pbCode .. ' ' .. shell.getRunningProgram())
  164.     end
  165.  
  166.     return isOutdated
  167. end
  168.  
  169. ----------------------------------------------------------------
  170. -- TURTLE SHORTCUTS
  171. ----------------------------------------------------------------
  172.  
  173. -- Executes the passed function, which
  174. -- returns true on success, n times and
  175. -- retries the function if it fails after
  176. -- 0.1 a delay of seconds.
  177. function _do(n, f)
  178.     n = n and n or 1
  179.     for i = 1, n, 1 do
  180.         while not f() do
  181.             sleep(0.1)
  182.         end
  183.     end
  184. end
  185.  
  186. -- Tries to move the turtle forward
  187. -- n times.
  188. function goForward(n)
  189.     _do(n, turtle.forward)
  190. end
  191.  
  192. -- Tries to move the turtle back
  193. -- n times.
  194. function goBack(n)
  195.     _do(n, turtle.back)
  196. end
  197.  
  198. -- Tries to move the turtle up
  199. -- n times.
  200. function goUp(n)
  201.     _do(n, turtle.up)
  202. end
  203.  
  204. -- Tries to move the turtle down
  205. -- n times.
  206. function goDown(n)
  207.     _do(n, turtle.down)
  208. end
  209.  
  210. -- Tries to turn the turtle n times
  211. -- to the right.
  212. function turnRight(n)
  213.     _do(n, turtle.turnRight)
  214. end
  215.  
  216. -- Tries to turn the turtle n times
  217. -- to the left.
  218. function turnLeft(n)
  219.     _do(n, turtle.turnLeft)
  220. end
  221.  
  222. -- Drops the whole inventory except the
  223. -- items in the first slot. Also, at least
  224. -- 5 blocks for filling ground are kept in
  225. -- the first slot of the inventory.
  226. function emptyInv()
  227.     local amm1 = turtle.getItemCount(2)
  228.     local dropAmm = amm1 <= 5 and 0 or (amm1 - 5)
  229.     turtle.select(2)
  230.     turtle.drop(dropAmm)
  231.     for i = 3, 16, 1 do
  232.         turtle.select(i)
  233.         turtle.drop(64)
  234.     end
  235.     turtle.select(1)
  236. end
  237.  
  238. ----------------------------------------------------------------
  239. -- PROGRAM STRUCTURE FUNCTIONS
  240. ----------------------------------------------------------------
  241.  
  242. -- Checks if the turtle has a label set and
  243. -- warns the user if the turtle has no label set.
  244. function checkLabel()
  245.     if not os.getComputerLabel() then
  246.         print('ATTENTION!\nThis computer does not have a label!\n' ..
  247.               'You should set a label to this computer to save ' ..
  248.               'fuel and program state on breaking the computer.\n')
  249.         local key = pullKeyEvent('Do you want to continue anyway? [yN]')
  250.         if key ~= 'y' then
  251.             print('Canceled.')
  252.             error()
  253.         end
  254.     end
  255. end
  256.  
  257. -- Checks if the turtle has estimated enought
  258. -- fuel to fulfil the mining process and refuels
  259. -- the turtle on user input of fuel into the
  260. -- inventory.
  261. function checkFuel(len)
  262.     cls()
  263.     -- 27.375 is the calculated value for
  264.     -- one corridor multiplied by an estimated
  265.     -- variation factor of 5%.
  266.     local fuelRequired = len * 27.375 * 1.05
  267.     local fuelExistent = turtle.getFuelLevel()
  268.     local fuelDelta = fuelRequired - fuelExistent
  269.     if fuelExistent < fuelRequired then
  270.         print('Not enough fuel to fulfil the process. Please refuel:\n\n' ..
  271.               ' - ' .. fuelDelta / 80 .. ' coal\n' ..
  272.               ' - ' .. fuelDelta / 15 .. ' wooden plancks\n\n' ..
  273.               ' - ' .. fuelDelta / 1000 .. ' lava buckets\n\n' ..
  274.               'Just put the fuel to be refueld somewhere in the turtles inventory.')
  275.         while firstInvNotEmpty() < 3 do
  276.             sleep(0.5)
  277.         end
  278.         refuel()
  279.         return false
  280.     end
  281.     return true
  282. end
  283.  
  284. -- Displays fuel level and shows information to
  285. -- prepare the inventory of the turtle for
  286. -- the mining process.
  287. function checkRequirements(len, store)
  288.     cls()
  289.     write('Fuel level is ' .. turtle.getFuelLevel() .. '.\n\n' ..
  290.           'Please put ' .. len .. ' torches in slot 1')
  291.     if store > 0 then
  292.         write(' and one or two chests in slot 2')
  293.     end
  294.     print('.\n\nThen press any key to continue or [c] to cancel.')
  295.     local key = pullKeyEvent()
  296.     if key == 'c' then
  297.         print('Canceled.')
  298.         error()
  299.     end
  300. end
  301.  
  302. -- Digs and moves forward by digging 1x2x1
  303. -- blocks with checking for falling blocks
  304. -- (like sand or gravel) and missing ground
  305. -- blocks which will be filled.
  306. function digForward(n, onlyBottom)
  307.     n = n and n or 1
  308.     for i = 1, n, 1 do
  309.         while turtle.detect() do
  310.             turtle.dig()
  311.             sleep(DELAY)
  312.         end
  313.         goForward()
  314.  
  315.         if onlyBottom ~= true then
  316.             sleep(DELAY)
  317.             while turtle.detectUp() do
  318.                 turtle.digUp()
  319.                 sleep(DELAY)
  320.             end
  321.         end
  322.  
  323.         if not turtle.detectDown() then
  324.             turtle.select(2)
  325.             turtle.placeDown()
  326.         end
  327.     end
  328. end
  329.  
  330. -- Digs the base for one or two chests,
  331. -- depending on passed chest count.
  332. -- If storage type is > 0 and 0 chests
  333. -- are passed, a base bay of the size of
  334. -- 1 will be created where items will
  335. -- be like there was a chest.
  336. function makeBase(store, chests)
  337.     local place = true
  338.     if chests == 0 then
  339.         place = false
  340.         chests = 1
  341.     else
  342.         chests = chests > 2 and 2 or chests
  343.     end
  344.     for i = 1, chests, 1 do
  345.         digForward()
  346.         turnRight()
  347.         digForward()
  348.         goBack()
  349.         if place then
  350.             turtle.select(2)
  351.             turtle.place()
  352.             turtle.select(1)
  353.         end
  354.         turnLeft()
  355.     end
  356. end
  357.  
  358. -- One instance of digging one corridor
  359. -- pair with or without placing a torch,
  360. -- depending on the passed value.
  361. function digCorridorPart(torches)
  362.     digForward(3)
  363.     if torches > 0 then
  364.         goBack()
  365.         turtle.select(1)
  366.         turtle.placeUp()
  367.         digForward()
  368.     end
  369.     turnRight()
  370.     digForward(DEPTH)
  371.     turnRight(2)
  372.     digForward(DEPTH * 2)
  373.     turnRight(2)
  374.     digForward(DEPTH)
  375.     turnLeft()
  376. end
  377.  
  378. ----------------------------------------------------------------
  379. -- MAIN ROUTINE
  380. ----------------------------------------------------------------
  381.  
  382. if ARG == 'v' or ARG == 'version' then
  383.     print(VERSIONTXT .. '\n')
  384.     checkVersion(VERSION, PBCODE)
  385.     error()
  386. end
  387.  
  388. local len = tonumber(ARG)
  389. local store = tonumber(ARG2)
  390. store = store and store or 0
  391.  
  392. if not len or len < 1 then
  393.     printHelp()
  394. end
  395.  
  396. if store and (store < 0 or store > 1) then
  397.     printHelp()
  398. end
  399.  
  400. checkLabel()
  401.  
  402. if checkVersion(VERSION, PBCODE) and UPDATEINFO then
  403.     pullKeyEvent('\nPress any butto nto continue...')
  404.     print("\n")
  405. end
  406.  
  407. while not checkFuel(len) do end
  408.  
  409. checkRequirements(len, store)
  410.  
  411. local torches = turtle.getItemCount(1)
  412. local chests = turtle.getItemCount(2)
  413.  
  414. turtle.select(1)
  415.  
  416. if store > 0 then
  417.     makeBase(store, chests)
  418. end
  419.  
  420. local way = 0
  421. for i = 1, len, 1 do
  422.     digCorridorPart(torches)
  423.     torches = torches - 1
  424.     way = way + 3
  425.     if store > 0 and emptyInvFields() <= SPACES then
  426.         turnRight(2)
  427.         digForward(way, true)
  428.         turnLeft()
  429.         emptyInv()
  430.         turnLeft()
  431.         goForward(way)
  432.     end
  433. end
  434.  
  435. turnRight(2)
  436. digForward(way, true)
Add Comment
Please, Sign In to add comment