jig487

Another bad mining program

Jun 14th, 2021 (edited)
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local blackList = {} -- blacklisted items get thrown out with the checkInv function
  4. local gps = {0, 0, 0, 0} --{X coord, Y coord, Z coord, rotation of turtle} all relative to starting position
  5.  
  6. --  Gets all of the items in a table
  7. local function id()
  8.     local tbl = {}
  9.     for i=1,16 do
  10.       tbl[i] = turtle.getItemDetail(i)
  11.     end
  12.     return tbl
  13. end
  14.  
  15. --  this function will return a table with all of the details in all slots you can get a data of specific slot like this:
  16. --local slot = someslot
  17. --local item = itemDetail()
  18. --print(item[slot])
  19. local function frwd()
  20.     while not turtle.forward() do
  21.         turtle.dig()
  22.     end
  23. end
  24.  
  25. local function mine() -- Mines in a straight line / above and updates local GPS
  26.     turtle.digUp()
  27.     if gps[4] == 0 then
  28.         gps[3] = gps[3] + 1
  29.     else
  30.         gps[3] = gps[3] - 1
  31.     end
  32.     frwd()
  33. end
  34.  
  35. local function newX() -- changes rows for X coord
  36.     turtle.digUp()
  37.     if gps[4] == 0 then
  38.         turtle.turnRight()
  39.         frwd()
  40.         turtle.turnRight()
  41.         gps[4] = gps[4] + 180
  42.         print("NewX, R: "..gps[4].." (back)")
  43.     elseif gps[4] == 180 then
  44.         turtle.turnLeft()
  45.         frwd()
  46.         turtle.turnLeft()
  47.         gps[4] = 0
  48.         print("NewX, R: "..gps[4].." (front)")
  49.     end
  50.     gps[1] = gps[1] + 1
  51.     print("X: "..gps[1]..", Z: "..gps[3])
  52. end
  53.  
  54.  
  55. local function screenInv() -- checks inventory for blacklisted items and drops them
  56.     local data = id()
  57.     for i = 1, 16 do
  58.         if data[i] and data[i].name == blacklist then
  59.             turtle.select(i)
  60.             turtle.dropDown()
  61.         end
  62.     end
  63. end
  64.  
  65. local function resetPos() -- goes back to starting position. Does not change GPS
  66.     for i = 1, gps[2] do
  67.         while not turtle.down() do
  68.             turtle.digDown()
  69.         end
  70.     end
  71.     if gps[4] == 0 then
  72.         for i = 1, gps[3] do
  73.             turtle.back()
  74.         end
  75.         turtle.turnLeft()
  76.         for i = 1, gps[1] do
  77.             frwd()
  78.         end
  79.         turtle.turnRight()
  80.     elseif gps[4] == 180 then
  81.         for i = 1, gps[3] do
  82.             frwd()
  83.         end
  84.         turtle.turnRight()
  85.         for i = 1, gps[1] do
  86.             frwd()
  87.         end
  88.         turtle.turnRight()
  89.     else
  90.         print("Error: rotation = "..gps[4])
  91.     end
  92. end
  93.  
  94. local function newY() -- goes up 2 blocks and changes rotation / Y value
  95.     resetPos()
  96.     gps[2] = gps[2] + 2
  97.     for h = 1, gps[2] do
  98.     turtle.digUp()
  99.     turtle.up()
  100.     end
  101. --    turtle.turnLeft()
  102. --    turtle.turnLeft()
  103. --    gps[4] = gps[4] + 180
  104.     if gps[4] >= 360 then
  105.         gps[4] = gps[4] - 360
  106.     end
  107.     gps[1] = 0
  108.     gps[3] = 0
  109.     gps[4] = 0
  110.     print("NewY, R: "..gps[4].." Y: "..gps[2])
  111. end
  112.  
  113. local function resume() -- goes back to position before calling resetPos() function to resume mining
  114.     if gps[4] == 0 then
  115.         for i = 1, gps[3] do
  116.             frwd()
  117.         end
  118.         turtle.turnRight()
  119.         for i = 1, gps[1] do
  120.             frwd()
  121.         end
  122.         turtle.turnLeft()
  123.     elseif gps[4] == 180 then
  124.         for i = 1, gps[3] do
  125.             frwd()
  126.         end
  127.         turtle.turnRight()
  128.         for i = 1, gps[1] do
  129.             frwd()
  130.         end
  131.         turtle.turnRight()
  132.     else
  133.         print("Error: rotation = "..gps[4])
  134.     end
  135.     for i = 1, gps[2] do
  136.         turtle.up()
  137.     end
  138. end
  139.  
  140. local function dump() --dumps inv
  141.     for i = 2, 16 do
  142.         turtle.select(i)
  143.         turtle.dropDown()
  144.     end
  145. end
  146.  
  147. local function invFull() -- if the inventory is full (if there is a block is slot 16), go dump the inventory in the dump chest except for slot 1 which has fuel, then return to the previous position to work again
  148.     turtle.select(16)
  149.     local stack = turtle.getItemCount()
  150.     print("Count in 16: "..stack)
  151.     if stack ~= 0 then
  152.         print("Dumping inventory..")
  153.         resetPos()
  154.         dump()
  155.         print("resuming")
  156.         resume()
  157.     end
  158.     turtle.select(1)
  159. end
  160.  
  161. local function refuel() -- checks if fuel is below 5000. True = goes to slot 1 and refuels. If no fuel, spits out an error and goes to get more
  162.     if turtle.getFuelLevel() < 5000 then
  163.       turtle.select(1)
  164.       turtle.refuel(10)
  165.    end
  166. end
  167.  
  168. --Main functions:
  169. -- mine
  170. -- invFull
  171. -- screenInv
  172. -- newX
  173. -- newY
  174. -- refuel
  175.  
  176. --Sub functions
  177. -- resume
  178. -- resetPos
  179.  
  180. --Main code:
  181.  
  182. for y = 1, tArgs[2] / 2 do
  183.     refuel()
  184.     for x = 1, tArgs[1] do
  185.         for u = 1, tArgs[3] do
  186.             mine()
  187.         end
  188.     --screenInv()
  189.     newX()
  190.     invFull()
  191.     end
  192.     if y ~= tArgs[2] / 2 then
  193.         newY()
  194.     end
  195. end
  196. resetPos()
  197. dump()
Add Comment
Please, Sign In to add comment