brensen11

EfficientMine

Mar 25th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 KB | None | 0 0
  1. local length = 10 -- x
  2. local width = 10 -- y
  3. local height = 3 -- z
  4. local key1 = false
  5. local key2 = false
  6. local key3 = false
  7. local chestName = "minecraft:ender_chest"
  8. --"enderstorage:ender_storage"
  9.  
  10. local slotCount = 16
  11.  
  12. --Sets the dimensions for the Mining Area from command line arguments
  13. -- Default size is listed above
  14. function SetDimensions(...)
  15.     local args = {...}
  16.     if(#args == 3) then
  17.         length = tonumber(args[1])
  18.         width = tonumber(args[2])
  19.         if(tonumber(args[3]) > 1) then
  20.             height = tonumber(args[3])
  21.         else
  22.             print("minimum height is 2, for walkability reasons, setting height = 3")
  23.         end
  24.     else
  25.         print("Malformed input given, please input using the format: 'Command' 'x' 'y' 'z' ")
  26.         print("Defaulting to 10x10x3")
  27.     end
  28. end
  29.  
  30. SetDimensions(...)
  31.  
  32. --Checks fuel level and refules if fuel level is less than 10
  33. --refuels by checking each slot for fuel
  34. function CheckFuel()
  35.     if(turtle.getFuelLevel() < 10) then
  36.         for slot = 1, slotCount do
  37.             print("Attempting to refuel on slot " .. slot)
  38.             turtle.select(slot)
  39.             if(turtle.refuel(1)) then
  40.                 return true
  41.              end
  42.         end
  43.         return false
  44.     end
  45.     return true
  46. end
  47.  
  48. --Checks each inv slot for enderchest
  49. --returns ender chest slot number
  50. function GetEnderIndex()
  51.     print("Searching for chest")
  52.     for slot = 1, slotCount do
  53.         local item = turtle.getItemDetail(slot)
  54.         if(item ~= nil) then
  55.             if(item["name"] == chestName) then
  56.                 return slot
  57.             end
  58.         end
  59.     end
  60.     return nil
  61. end
  62.  
  63. --Checks if all inventory slots are full
  64. --if full returns true
  65. function InvFullCheck()
  66.     print("Checking if Inventory Full")
  67.     local sum = 0
  68.     for slot = 1, slotCount do
  69.         if(turtle.getItemCount(slot) > 0) then
  70.             sum = sum + 1
  71.         end
  72.     end
  73.     if (sum == slotCount) then
  74.         return true
  75.     end
  76.     return false
  77. end
  78.  
  79. --Dumps all items but coal into an enderchest its carrying
  80. function DumpItems()
  81.     print("Attempting to dump items")
  82.     turtle.select(GetEnderIndex())
  83.     turtle.digUp()
  84.     turtle.placeUp()
  85.     print("Dumping Items")
  86.     for slot = 1, slotCount do
  87.         if(turtle.getItemDetail(slot) ~= nil) then
  88.             local item = turtle.getItemDetail(slot)
  89.            
  90.             if(item["name"] ~= "minecraft:coal") then
  91.                 turtle.select(slot)
  92.                 turtle.dropUp()
  93.             end
  94.         end
  95.     end
  96.     turtle.digUp()
  97. end
  98.  
  99. --For cases of gravel
  100. function DetectAndDig()
  101.     while(turtle.detect()) do
  102.         turtle.dig()
  103.     end
  104. end
  105.  
  106. function Forward()
  107.     DetectAndDig()
  108.     turtle.forward()
  109. end
  110.  
  111. function OneLayer()
  112.     Forward()
  113. end
  114.  
  115. function TwoLayers()
  116.     Forward()
  117.     turtle.digDown()
  118. end
  119.  
  120. function ThreeLayers()
  121.     TwoLayers()
  122.     turtle.digUp()
  123. end
  124.  
  125. function Up()
  126.     while(turtle.detectUp()) do
  127.         turtle.digUp()
  128.     end
  129.     turtle.up()
  130.     if(key3) then
  131.         turtle.digUp()
  132.     end
  133. end
  134.  
  135. function turnRight()
  136.     turtle.turnRight()
  137.     DigLayerX()
  138.     turtle.turnRight()
  139. end
  140.  
  141. function turnLeft()
  142.     turtle.turnLeft()
  143.     DigLayerX()
  144.     turtle.turnLeft()
  145. end
  146.  
  147. --Calculates the number of Up()s taken based on the turtles position always being between
  148. --base and last layer of a 3 layer square, (i.e. 2, in 1-3) by checking if z index is less than layer index
  149. --then calculates number of times the turtle needs to go Up by comparing
  150. function CalculateUp(z)
  151.     if(z < height - 2) then
  152.         if((z + 3) == height) then
  153.             Up()
  154.             Up()
  155.         elseif((z + 3) < height) then
  156.             Up()
  157.             Up()
  158.             Up()
  159.         end
  160.     end
  161. end
  162.  
  163. function SetKey(z)
  164.     if(z == height) then
  165.         key1 = true
  166.         key2 = false
  167.         key3 = false
  168.     elseif(height - z == 1) then
  169.         key2 = true
  170.  
  171.         key1 = false
  172.         key3 = false
  173.     else
  174.         key3 = true
  175.  
  176.         key1 = false
  177.         key2 = false
  178.     end
  179. end
  180.  
  181. function TurnAround()
  182.     turtle.turnRight()
  183.     turtle.turnRight()
  184. end
  185.    
  186. --Checks to see if has ender chest and coal
  187. function ReadyForJourney()
  188.     return true
  189. end
  190.  
  191. function DigLayerX()
  192.     if(key1)then
  193.         OneLayer()
  194.     elseif(key2)then
  195.         TwoLayers()
  196.     elseif(key3)then
  197.         ThreeLayers()
  198.     end
  199. end
  200.  
  201. --------------Main----------------------
  202. function Main()
  203. if (not ReadyForJourney()) then
  204. print("Your turtle isnt ready to journey out!")
  205. print("Make sure your turtle has enough fuel and an enderchest!")
  206. return
  207. end
  208.  
  209. for z = 1, height, 3 do                             --
  210.  
  211.     SetKey(z)
  212.  
  213.     for y = 1, width do                         --starting length cuts one turn off
  214.  
  215.         for x = 1, length - 1 do                    --width cuts one length down off the next
  216.             if(not CheckFuel()) then
  217.                 print("Your turtle is out of fuel!")
  218.                 print("Powering down....")
  219.                 return
  220.             end
  221.            
  222.             if(InvFullCheck == true) then
  223.                 DumpItems()
  224.             end
  225.  
  226.             DigLayerX()
  227.             --run some function with some local variable as a param, that gets changed every z changed
  228.             --based on the number the function will run the right, function of mine one two or three layers
  229.             --somehow WITHOUT doing checks (because if it does checks, its redundancy)
  230.  
  231.         end
  232.  
  233.         if(y - width ~= 0) then
  234.             if(math.fmod(y, 2) == 0) then
  235.                 turnLeft()
  236.             else
  237.                 turnRight()
  238.             end
  239.         else
  240.             TurnAround()
  241.         end
  242.  
  243.     end
  244.  
  245.     CalculateUp(z)
  246.  
  247. end
  248. end
  249.  
  250. Main()
  251.  
Add Comment
Please, Sign In to add comment