Advertisement
enderpro100

VolumeMining

Dec 5th, 2020 (edited)
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.00 KB | Gaming | 0 0
  1. --- Programme : volume mining
  2. --- Auteur : LightKnight51
  3. --- Dernière modifications : 15/04/2023
  4.  
  5.  
  6. --- Variables
  7.    
  8.  
  9. --- Functions
  10.  
  11. -- TODO Add function what put chest if turtle is full
  12.  
  13. local arg = { ... }
  14.  
  15. local exit = false
  16.  
  17. --- Utils API
  18. os.loadAPI("MarquitoLuaUtils")
  19.  
  20. -- Put chest up
  21. function DeposeChest()
  22.     if MarquitoLuaUtils.BlockDetection("up", false) == "minecraft:bedrock" then
  23.         MarquitoLuaUtils.Error("minecraft:bedrock")
  24.     elseif string.find(MarquitoLuaUtils.BlockDetection("up", false),"chest") ~= nil then
  25.         print("Coffre deja place !")
  26.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Chest already in place")
  27.         turtle.select(1)
  28.     else
  29.         turtle.digUp()
  30.         chestId = MarquitoLuaUtils.FindIDContains("chest", false)
  31.         if chestId ~= nil then
  32.             turtle.select(chestId)
  33.             turtle.placeUp()
  34.             print("Coffre place !")
  35.             turtle.select(1)
  36.             MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Chest has been placed")
  37.         end
  38.     end
  39. end
  40.  
  41. -- TODO To finish
  42. function SecureTurtle()
  43.     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Secure turtle position")
  44.     itemID = nil
  45.     if not MarquitoLuaUtils.BlockDetection("up", true) then
  46.         if MarquitoLuaUtils.SelectionID("minecraft:cobblestone") ~= nil then
  47.             itemID = MarquitoLuaUtils.SelectionID("minecraft:cobblestone")
  48.         elseif MarquitoLuaUtils.SelectionID("minecraft:dirt") ~= nil then
  49.             itemID = MarquitoLuaUtils.SelectionID("minecraft:dirt")
  50.         elseif MarquitoLuaUtils.SelectionID("minecraft:netherrack") ~= nil then
  51.             itemID = MarquitoLuaUtils.SelectionID("minecraft:netherrack")
  52.         elseif MarquitoLuaUtils.SelectionID("minecraft:cobbled_deepslate") ~= nil then
  53.             itemID = MarquitoLuaUtils.SelectionID("minecraft:cobbled_deepslate")
  54.         end
  55.         if itemID ~= nil then
  56.             turtle.select(itemID)
  57.             turtle.placeDown()
  58.             turtle.place()
  59.             turtle.turnRight()
  60.             turtle.place()
  61.             turtle.turnRight()
  62.             turtle.place()
  63.             turtle.turnRight()
  64.             turtle.place()
  65.             turtle.turnRight()
  66.         end
  67.     end
  68. end
  69.  
  70. function AskDimension(type, numArg)
  71.     dimension = 0
  72.     bRetry = false
  73.     print(type .. " ?")
  74.     while true do
  75.         if arg[numArg] ~= nil and not bRetry then
  76.             dimension = tonumber(arg[numArg])
  77.             print(dimension)
  78.         else
  79.             dimension = tonumber(read())
  80.         end
  81.         if not dimension or dimension == 0 then
  82.             print("Valeur incorrecte !")
  83.             bRetry = true
  84.         else
  85.             break
  86.         end
  87.     end
  88.     return dimension
  89. end
  90.  
  91. function MiningLine(length)
  92.     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Start mining line of length : " .. length)
  93.     for iLength=1,length - 1 do
  94.         while MarquitoLuaUtils.BlockDetection("front", true) == false do
  95.             turtle.dig()
  96.             sleep(0.3)
  97.         end
  98.         turtle.forward()
  99.     end
  100. end
  101.  
  102. function MiningDown(height)
  103.     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Start mining down of : " .. height - 1)
  104.     for iHeight=1,height - 1 do
  105.         turtle.digDown()
  106.         turtle.down()
  107.     end
  108. end
  109.  
  110. function Mining(length, width, height)
  111.     if width == 1 and height == 1 then
  112.         MiningLine(length)
  113.     elseif length == 1 and height == 1 then
  114.         turtle.turnRight()
  115.         MiningLine(width)
  116.     elseif length == 1 and width == 1 then
  117.         MiningDown(height)
  118.     else
  119.         for iHeight = 1, height do
  120.             for iWidth = 1, width do
  121.                 MiningLine(length)
  122.                 if iWidth ~= width then
  123.                     chooseDirectionForTurn(iWidth, width, iHeight)
  124.                     turtle.dig()
  125.                     turtle.forward()
  126.                     chooseDirectionForTurn(iWidth, width, iHeight)
  127.                 end
  128.             end
  129.             if iHeight ~= height then
  130.                 turtle.turnRight()
  131.                 turtle.turnRight()
  132.                 MiningDown(2)
  133.             end
  134.         end
  135.     end
  136.     ReturnInitPos(length, width, height)
  137. end
  138.  
  139. function chooseDirectionForTurn(number, totalWidth, iHeight)
  140.     if isEven(totalWidth) then
  141.         if isEven(iHeight) then
  142.             if isEven(number) then
  143.                 turtle.turnRight()
  144.             else
  145.                 turtle.turnLeft()
  146.             end
  147.         else
  148.             chooseDirectionForTurnClassic(number)
  149.         end
  150.     else
  151.         chooseDirectionForTurnClassic(number)
  152.     end
  153. end
  154.  
  155. function chooseDirectionForTurnClassic(number)
  156.     if isEven(number) then
  157.         turtle.turnLeft()
  158.     else
  159.         turtle.turnRight()
  160.     end
  161. end
  162.  
  163. function isEven(number)
  164.     return math.mod(number, 2) == 0
  165. end
  166.  
  167. function MoveLine(length)
  168.     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Start moving of : " .. length)
  169.     for iLength=1,length - 1 do
  170.         if not MarquitoLuaUtils.BlockDetection("front", true) then
  171.             if string.find(MarquitoLuaUtils.BlockDetection("front", false),"chest") == nil then
  172.                 turtle.dig()
  173.             else
  174.                 -- Un coffre a été déposé, cas a traiter
  175.             end
  176.         end
  177.         turtle.forward()
  178.     end
  179. end
  180.  
  181. function MoveUp(height)
  182.     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Start moving up of : " .. height - 1)
  183.     for iHeight=1,height - 1 do
  184.         if not MarquitoLuaUtils.BlockDetection("up", true) then
  185.             if string.find(MarquitoLuaUtils.BlockDetection("up", false),"chest") == nil then
  186.                 turtle.digUp()
  187.             else
  188.                 -- Un coffre a été déposé, cas a traiter
  189.             end
  190.         end
  191.         turtle.up()
  192.     end
  193. end
  194.  
  195. function ReturnInitPos(length, width, height)
  196.     if width == 1 and height == 1 then
  197.         turtle.turnRight()
  198.         turtle.turnRight()
  199.         MoveLine(length)
  200.     elseif length == 1 and height == 1 then
  201.         turtle.turnRight()
  202.         turtle.turnRight()
  203.         MoveLine(width)
  204.         turtle.turnRight()
  205.     elseif length == 1 and width == 1 then
  206.         MoveUp(height)
  207.     else
  208.         MoveUp(height)
  209.         widthHeight = width * height
  210.         if isEven(widthHeight) then
  211.             if isEven(height) then
  212.                 turtle.turnRight()
  213.                 turtle.turnRight()
  214.             else
  215.                 turtle.turnRight()
  216.                 MoveLine(width)
  217.                 turtle.turnRight()
  218.             end
  219.         else
  220.             if isEven(height) then
  221.                 turtle.turnRight()
  222.                 turtle.turnRight()
  223.             else
  224.                 turtle.turnLeft()
  225.                 MoveLine(width)
  226.                 turtle.turnLeft()
  227.                 MoveLine(length)
  228.                 turtle.turnRight()
  229.                 turtle.turnRight()
  230.             end
  231.         end
  232.     end
  233. end
  234.  
  235. function MainProgram()
  236.     print("Bienvenue dans le programme de minage de volume !")
  237.     local length, width, height = 1
  238.     length = AskDimension("Longueur", 1)
  239.     width = AskDimension("Largeur", 2)
  240.     height = AskDimension("Hauteur", 3)
  241.     if length == 1 and width == 1 and height == 1 then
  242.        print("Il n'y a rien a miner.")
  243.        MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Nothing to dig")
  244.     else
  245.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "VolumeMining launch")
  246.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Length : " .. length)
  247.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Width : " .. width)
  248.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Height : " .. height)
  249.         turtle.digDown()
  250.         sleep(0.5)
  251.         turtle.down()
  252.         DeposeChest()
  253.         Mining(length, width, height)
  254.         SecureTurtle()
  255.         MarquitoLuaUtils.DropAllItemsInChest(false, {})
  256.     end
  257.     MarquitoLuaUtils.EndLog()
  258. end
  259.  
  260. -- Program
  261. parallel.waitForAny(MainProgram, MarquitoLuaUtils.Refuel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement