XelaLord

Branch

Jan 18th, 2022 (edited)
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. -- Utility functions --
  2. function sign(x)   
  3.     if x > 0 then
  4.         return 1
  5.     elseif x < 0 then
  6.         return -1
  7.     else
  8.         return 0
  9.     end
  10. end
  11.  
  12. function containsValue (tab, val)
  13.     for index, value in ipairs(tab) do
  14.         if value == val then
  15.             return true
  16.         end
  17.     end
  18.  
  19.     return false
  20. end
  21.  
  22. -- Safe Movement Methods --
  23. function forward()
  24.     while t.detect() do t.dig() end
  25.     while not t.forward() do t.attack() end
  26.  
  27.     if w == 0 then
  28.         x = x + 1           -- 0 is +x (east)
  29.     elseif w == 1 then
  30.         z = z + 1           -- 1 is +z (south)
  31.     elseif w == 2 then
  32.         x = x - 1           -- 2 is -x (west)
  33.     elseif w == 3 then
  34.         z = z - 1           -- 3 is -z (north)
  35.     end
  36. end
  37.  
  38. function back()
  39.     while not t.back() do t.attack() end
  40.    
  41.     if w == 0 then
  42.         x = x - 1           -- 0 is +x (east)
  43.     elseif w == 1 then
  44.         z = z - 1           -- 1 is +z (south)
  45.     elseif w == 2 then
  46.         x = x + 1           -- 2 is -x (west)
  47.     elseif w == 3 then
  48.         z = z + 1           -- 3 is -z (north)
  49.     end
  50. end
  51.  
  52. function up()
  53.     while t.detectUp() do t.digUp() end
  54.     z = z + 1
  55.     while not t.up() do t.attackUp() end
  56.     updateSave()
  57. end
  58.  
  59. function down()
  60.     while t.detectDown() do t.digDown() end
  61.     z = z - 1
  62.     while not t.down() do t.attackDown() end
  63.     updateSave()
  64. end
  65.  
  66. function turnLeft()
  67.     t.turnLeft()
  68.     w = (w-1) % 4  
  69. end
  70.  
  71. function turnRight()
  72.     t.turnRight()
  73.     w = (w+1) % 4  
  74. end
  75.  
  76. function turnTo(a)
  77.     if a == (w-1) % 4 then
  78.         turnLeft()
  79.     else
  80.         while w ~= a do
  81.             turnRight()
  82.         end
  83.     end
  84. end
  85.  
  86. function goTo(a, b, c)
  87.     -- Y
  88.     while y < b do up() end
  89.     while y > b do down() end
  90.    
  91.     -- X
  92.     if a > x then
  93.         turnTo(0)
  94.         while a > x do forward() end
  95.     elseif a < x then
  96.         turnTo(2)
  97.         while a < x do forward() end
  98.     end
  99.    
  100.     -- Z
  101.     if c > z then
  102.         turnTo(1)
  103.         while c > z do forward() end
  104.     elseif c < z then
  105.         turnTo(3)
  106.         while c < z do forward() end
  107.     end
  108. end
  109.  
  110. function selfOrient()
  111.     a, c, b = gps.locate()
  112.     forward()
  113.     x, y, z = gps.locate()
  114.    
  115.     a = x - a
  116.     b = z - b
  117.     print(a,b)
  118.    
  119.     if a == 1 then
  120.         w = 0               -- 0 is +x (east)
  121.     elseif b == 1 then
  122.         w = 1               -- 1 is +z (south)
  123.     elseif a == -1 then
  124.         w = 2               -- 2 is -x (west)
  125.     else
  126.         w = 3               -- 3 is -z (north)
  127.     end
  128.    
  129.     back()
  130.     x, y, z = gps.locate()
  131.     print(string.format("Location:%s, %s, %s, %s", x, y, z, w))
  132. end
  133.  
  134. -- Inventory Functions
  135. function findInInven(ID)
  136.     data = t.getItemDetail()
  137.     if data then
  138.         if data.name == ID then
  139.             return true
  140.         end
  141.     end
  142.    
  143.     for i = 1,16 do
  144.         t.select(i)
  145.         data = t.getItemDetail()
  146.         if data then
  147.             if data.name == ID then
  148.                 return true
  149.             end
  150.         end
  151.     end
  152.     return false
  153. end
  154.  
  155. function checkFuel()
  156.     if t.getFuelLevel() < 200 * 2.5 then
  157.         findInInven("minecraft:coal")
  158.     end
  159. end
  160.  
  161. -- Mining Operation Functions --
  162. function branch(s)
  163.     for i = 1,s do                  -- Go down along the bottom, placing floor blocks as needed
  164.         forward()
  165.         checkSurround()
  166.        
  167.         if not t.detectDown() then
  168.             if findInInven("minecraft:cobblestone") then
  169.                 t.placeDown()
  170.             end
  171.             if findInInven("minecraft:stone") then
  172.                 t.placeDown()
  173.             end
  174.         end
  175.     end
  176.  
  177.     up(); turnLeft(); turnLeft()    -- Turn around at the end
  178.     for i = s,2,-1 do               -- Come back along the top, placing torches every 10 blocks
  179.         if i%10 == 0 then
  180.             if findInInven("minecraft:torch") then
  181.                 t.placeDown()
  182.             end
  183.         end
  184.        
  185.         forward()
  186.         checkSurround()    
  187.     end
  188.     down(); forward();
  189. end
  190.  
  191. function checkSurround() -- This is recursive and will recursivly mine any ore veins that it detects
  192.     for i = 1,4 do
  193.         checkForward()
  194.         turnLeft()
  195.     end
  196.     checkUp()
  197.     checkDown()
  198. end
  199.  
  200. function checkUp()
  201.     success, data = t.inspectUp()
  202.     if success and not containsValue(unwantedBlocks, data.name) then
  203.         s = string.format("%s:%s:%s", x, y+1, z)
  204.         table.insert(flaggedBlocks, s)
  205.    
  206.         up()   
  207.         checkSurround()
  208.         down()
  209.     end
  210. end
  211.  
  212. function checkDown()
  213.     success, data = t.inspectDown()
  214.     if success and not containsValue(unwantedBlocks, data.name) then
  215.         s = string.format("%s:%s:%s", x, y-1, z)
  216.         table.insert(flaggedBlocks, s)
  217.    
  218.         down() 
  219.         checkSurround()
  220.         up()
  221.     end
  222. end
  223.  
  224. function checkForward()
  225.     success, data = t.inspect()
  226.     if success and not containsValue(unwantedBlocks, data.name) then
  227.         if w == 0 then
  228.             s = string.format("%s:%s:%s", x+1, y, z)    -- 0 is +x (east)
  229.         elseif w == 1 then
  230.             s = string.format("%s:%s:%s", x, y, z+1)    -- 1 is +z (south)
  231.         elseif w == 2 then
  232.             s = string.format("%s:%s:%s", x-1, y, z)    -- 2 is -x (west)
  233.         elseif w == 3 then
  234.             s = string.format("%s:%s:%s", x, y, z-1)    -- 3 is -z (north)
  235.         end
  236.        
  237.         table.insert(flaggedBlocks, s)
  238.        
  239.         forward()
  240.         checkSurround()
  241.         back()
  242.     end
  243. end
  244.  
  245. -- Savefile Funcitons --
  246. function updateSave()
  247.  
  248. end
  249.  
  250.  
  251.  
  252. -- Main Loop
  253. t = turtle
  254. unwantedBlocks = {"minecraft:dirt", "minecraft:grass" , "minecraft:stone", "minecraft:cobblestone", "minecraft:sand", "minecraft:gravel", "minecraft:mossy_cobblestone", "minecraft:bedrock", "minecraft:netherrack", "minecraft:soul_sand", "minecraft:torch"}
  255. fuels = {"minecraft:coal", "minecraft:coal_block", "minecraft:lava_bucket"}
  256. flaggedBlocks = {}
  257. x, y, z, w = 0, 0, 0, 0
  258. selfOrient()
  259.  
  260. home = {x, y, z}
  261. homeDirection = w
  262. completeBranches = 0
  263.  
  264. while true do
  265.     for i = 1,3 do
  266.         forward()
  267.         while t.detectUp() do t.digUp() end
  268.         if not t.detectDown() then
  269.             if findInInven("minecraft:cobblestone") then
  270.                 t.placeDown()
  271.             end
  272.             if findInInven("minecraft:stone") then
  273.                 t.placeDown()
  274.             end
  275.         end
  276.     end
  277.     turnLeft()
  278.     branch(10)
  279.     completeBranches = completeBranches + 1
  280.     branch(10)
  281.     completeBranches = completeBranches + 1
  282.     turnRight()
  283. end
  284.  
Add Comment
Please, Sign In to add comment