MihaMi

Excavate3.lua

Jan 21st, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.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. local xPos = 0
  21. local yPos = 0
  22. local zPos = 0
  23. local dir = 0
  24. local fuelLevel = turtle.getFuelLevel()
  25.  
  26. local oldXPos = 0
  27. local oldYPos = 0
  28. local oldZPos = 0
  29. local oldDir = 0
  30.  
  31.  
  32. -- Movement - no fuel needed
  33. function left()    
  34.     if turtle.turnLeft() then
  35.         dir = dir + 1
  36.         if dir == 4 then
  37.             dir = 0
  38.         end      
  39.         return true
  40.     else        
  41.         return false
  42.     end    
  43. end
  44.  
  45. function right()    
  46.     if turtle.turnRight() then      
  47.         dir = dir - 1
  48.         if dir == -1 then
  49.             dir = 3
  50.         end
  51.         return true
  52.     else        
  53.         return false
  54.     end    
  55. end
  56.  
  57. -- Movement - fuel needed
  58.  
  59. function moveDown()
  60.     turtle.digDown()
  61.     if turtle.down() then
  62.         yPos = yPos+1
  63.         return true
  64.     else        
  65.         return false
  66.     end    
  67. end
  68.  
  69. function moveUp()
  70.     turtle.digUp()
  71.     if turtle.up() then
  72.         yPos = yPos-1
  73.         return true
  74.     else
  75.         return false
  76.     end
  77. end
  78.  
  79.  
  80. function moveForward()
  81.     turtle.dig()
  82.     if turtle.forward() then
  83.         if dir == 0 then
  84.             xPos=xPos+1
  85.         elseif dir == 1 then
  86.             zPos=zPos+1
  87.         elseif dir == 2 then
  88.             xPos=xPos-1
  89.         elseif dir == 3 then
  90.             zPos=zPos-1
  91.         end
  92.     end
  93. end
  94.  
  95. function rotate(d)
  96.     while dir ~= d do
  97.         right()
  98.     end
  99.  
  100. end
  101.  
  102. function goTo(x,y,z,d) -- x,y,z,direction
  103.     if fuel() == true then -- fueling        
  104.         -- X coordinate
  105.         if xPos > x then
  106.             rotate(2)
  107.         elseif xPos < x then
  108.             rotate(0)
  109.         end
  110.         while xPos ~= x do
  111.             moveForward()
  112.         end
  113.         -- Z coordinate
  114.         if zPos > z then
  115.             rotate(3)
  116.         elseif zPos < z then
  117.             rotate(1)
  118.         end
  119.         while zPos ~= z do
  120.             moveForward()
  121.         end                
  122.         -- Y coordinate
  123.         if yPos < y then
  124.             while yPos ~= y do
  125.                 moveDown()
  126.             end
  127.         elseif yPos > y then
  128.             while yPos ~= y do
  129.                 moveUp()
  130.             end
  131.         end
  132.         print("went")
  133.     end
  134. end
  135.  
  136.  
  137. -- Fueling
  138. function fuel()
  139.     local hasFuel = false  
  140.     if turtle.getFuelLevel() < 50 then     
  141.         for i = 1, 16 do -- loop through the slots
  142.             turtle.select(i) -- change to the slot
  143.             if turtle.refuel(0) then -- if it's valid fuel
  144.                 hasFuel = true
  145.                 break
  146.             end        
  147.         end
  148.         if hasFuel == true then
  149.             for j=1,turtle.getItemCount() do
  150.                 if turtle.getFuelLevel() ~= turtle.getFuelLimit() then
  151.                     turtle.refuel(1)
  152.                 end
  153.             end
  154.         else
  155.             print("no fuel - going back")
  156.             --goBack()
  157.         end
  158.     else
  159.         hasFuel = true
  160.     end
  161.     return hasFuel
  162. end
  163.  
  164.  
  165.  
  166.  
  167. function goBack()
  168.     oldXPos = xPos
  169.     oldYPos = yPos
  170.     oldZPos = zPos
  171.     oldDir = dir   
  172. end
  173.  
  174.  
  175. function test()
  176.     shell.run("label","set","Zmiskol")
  177.     term.clear()
  178.     print("Start Fuel Level: ",fuelLevel)
  179.    
  180.     goTo(0,2,0,0)
  181.     sleep(5)
  182.     goTo(2,0,0,2)        
  183. end
  184.  
  185. test()
Add Comment
Please, Sign In to add comment