MihaMi

Excavate3.lua

Jan 21st, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1.  
  2. --[[
  3. TO-DO:
  4. - return back to chest
  5. - up/down dig
  6. - quarry(x,y,z)
  7. - throw items
  8. - print out location and reset monitor
  9. - min fuel reserve level
  10. --]]
  11.  
  12. --[[
  13. local tArgs = { ... }
  14. if #tArgs ~= 3 then
  15.     print( "Usage: exc3 x y z" )
  16.     return
  17. end
  18. --]]
  19.  
  20. -- Position Variables
  21. local xPos = 0
  22. local yPos = 0
  23. local zPos = 0
  24. local dir = 0
  25.  
  26. local oldXPos = 0
  27. local oldYPos = 0
  28. local oldZPos = 0
  29. local oldDir = 0
  30.  
  31. --Fuel Variables
  32. local fuelLevel = turtle.getFuelLevel()
  33. local coalCount = 0
  34. local coalIndex = 1
  35.  
  36. -- Movement - no fuel needed
  37. function left()    
  38.     if turtle.turnLeft() then
  39.         dir = dir + 1
  40.         if dir == 4 then
  41.             dir = 0
  42.         end      
  43.         return true
  44.     else        
  45.         return false
  46.     end    
  47. end
  48.  
  49. function right()    
  50.     if turtle.turnRight() then      
  51.         dir = dir - 1
  52.         if dir == -1 then
  53.             dir = 3
  54.         end
  55.         return true
  56.     else        
  57.         return false
  58.     end    
  59. end
  60.  
  61. -- Movement - fuel needed
  62.  
  63. function moveDown()
  64.     turtle.digDown()
  65.     if turtle.down() then
  66.         yPos = yPos+1
  67.         return true
  68.     else        
  69.         return false
  70.     end    
  71. end
  72.  
  73. function moveUp()
  74.     turtle.digUp()
  75.     if turtle.up() then
  76.         yPos = yPos-1
  77.         return true
  78.     else
  79.         return false
  80.     end
  81. end
  82.  
  83.  
  84. function moveForward()
  85.     turtle.dig()
  86.     if turtle.forward() then
  87.         if dir == 0 then
  88.             xPos=xPos+1
  89.         elseif dir == 1 then
  90.             zPos=zPos+1
  91.         elseif dir == 2 then
  92.             xPos=xPos-1
  93.         elseif dir == 3 then
  94.             zPos=zPos-1
  95.         end
  96.     end
  97. end
  98.  
  99. function rotate(d)
  100.     while dir ~= d do
  101.         right()
  102.     end
  103.  
  104. end
  105.  
  106. function goTo(x,y,z,d) -- x,y,z,direction
  107.     if fuel() == true then -- fueling        
  108.         -- X coordinate
  109.         if xPos > x then
  110.             rotate(2)
  111.         elseif xPos < x then
  112.             rotate(0)
  113.         end
  114.         while xPos ~= x do
  115.             moveForward()
  116.             if Math.fmod(xPos,10) == 0 then
  117.                 fuel()
  118.             end
  119.         end
  120.         -- Z coordinate
  121.         if zPos > z then
  122.             rotate(3)
  123.         elseif zPos < z then
  124.             rotate(1)
  125.         end
  126.         while zPos ~= z do
  127.             moveForward()
  128.             if Math.fmod(yPos,10) == 0 then
  129.                 fuel()
  130.             end
  131.         end                
  132.         -- Y coordinate
  133.         if yPos < y then
  134.             while yPos ~= y do
  135.                 moveDown()
  136.                 if Math.fmod(yPos,10) == 0 then
  137.                     fuel()
  138.                 end
  139.             end
  140.         elseif yPos > y then
  141.             while yPos ~= y do
  142.                 moveUp()
  143.                 if math.fmod(yPos,10) == 0 then
  144.                     fuel()
  145.                 end
  146.             end
  147.         end
  148.         -- Rotation
  149.         rotate(d)      
  150.     end
  151. end
  152.  
  153. -- Distance
  154. function distBase()
  155.     local sum = math.abs(xPos)+math.abs(zPos)+math.abs(yPos)
  156.     return sum
  157. end
  158.  
  159.  
  160. -- Pickup
  161. function pickUp()
  162.     if turtle.getItemCount(1) ~= 0 then
  163.         turtle.select(1)
  164.         turtle.suck()
  165.         turtle.suckDown()
  166.         turtle.suckUp()
  167.     else
  168.         turtle.select(2)
  169.         turtle.suck()
  170.         turtle.suckDown()
  171.         turtle.suckUp()
  172.     end
  173. end
  174.  
  175. -- Fueling
  176. function checkFuel()
  177.     local hasFuel = false
  178.     local maxCoal = 0
  179.     local maxCoalSlot = 1
  180.     for i = 1, 16 do -- loop through the slots
  181.         turtle.select(i) -- change to the slot
  182.         if turtle.getItemDetail().name == "minecraft:coal" then
  183.             hasFuel = true
  184.             coalCount=coalCount+turtle.getItemCount()
  185.             if maxCoal < turtle.getItemCount() then
  186.                 maxCoal = turtle.getItemCount()
  187.                 maxCoalSlot = i
  188.             end        
  189.         end        
  190.     end
  191.     return hasFuel
  192. end
  193.  
  194. function fuel()
  195.     if turtle.getFuelLevel() < 100 then    
  196.         if checkFuel() == true then
  197.             turtle.select(maxCoalSlot)
  198.             for i=1,turtle.getItemCount() do
  199.                 if turtle.getFuelLevel() ~= turtle.getFuelLimit() or turtle.getItemCount() ~= 0 then
  200.                     turtle.refuel(1)
  201.                 end
  202.             end
  203.         else
  204.             print("not enough fuel - going back")
  205.             goBase()
  206.         end    
  207.     end
  208. end
  209.  
  210.  
  211.  
  212.  
  213. function goBase()
  214.     oldXPos = xPos
  215.     oldYPos = yPos
  216.     oldZPos = zPos
  217.     oldDir = dir   
  218.     goTo(0,0,0,0)      
  219. end
  220.  
  221.  
  222. function test()
  223.     shell.run("label","set","Zmiskol")
  224.     term.clear()
  225.     print("Start Fuel Level: ",fuelLevel)  
  226.     goTo(10,-10,10,0)  
  227.     print(turtle.getFuelLevel())
  228.     goBase()
  229.     print(turtle.getFuelLevel())               
  230. end
  231.  
  232. test()
Add Comment
Please, Sign In to add comment