jeddell

Turtle script 1

Apr 26th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 KB | None | 0 0
  1. --functions
  2. local refuel -- refuels the turtle if it gets below 10
  3. local depth -- gets the depth variable (d)
  4. local length -- gets the length variable(l)
  5. local width -- gets the width variable (w)
  6. local length1 -- lays blocks along the length wall using the (l) variable
  7. local width1 -- lays blocks along the width wall using the (w) variable
  8. local slots -- checks  the slot for items and moves to the next slot when empty
  9. local wall -- puts it all together and rasied the height of t he turtle using the (d) variable
  10.  
  11. --Varaibles
  12. local d = 1 --depth variable
  13. local l = 1 -- length variable
  14. local w = 1 --Width varable
  15. local s = 1 -- Slot variable
  16. local a = 0 -- returns the total amount of blocks required for the build.
  17. local diag = 1 -- returns the length plus width count to place into the minimum refuel check. This ensures that the refueling is done in the corners.
  18. local xCoord = -459
  19. local zCoord = 63
  20. local yCoord = -376
  21. local xhome = xCoord
  22. local zhome = zCoord
  23. local yhome = yCoord
  24. local orientation = 3
  25. local zDiff = {-1, 0, 1, 0}
  26. local xDiff = {0, 1, 0, -1}
  27. local yMin = yCoord
  28. local xProgress = 999
  29. local zProgress = 999
  30. local yProgress = 999
  31. local oProgress = 1
  32. local yTvl = 85
  33.  
  34. orientations = {"north","east","south","west"}
  35.  
  36. function empty()
  37.   turtle.select(16)
  38.   if turtle.getItmeCount() ~= 0 then
  39.     turtle.select(s)
  40.     return
  41.   end
  42. end
  43.  
  44. function look(direction)
  45.   while direction ~= orientations[orientation] do
  46.     right()
  47.   end
  48. end
  49.  
  50. function left()
  51.   orientation = orientation - 1
  52.   orientation = (orientation - 1)%4
  53.   orientation = orientation + 1
  54.   turtle.turnLeft()  
  55. end
  56.  
  57. function right()
  58.   orientation = orientation - 1
  59.   orientation = (orientation + 1)%4
  60.   orientation = orientation + 1
  61.   turtle.turnRight()  
  62. end
  63.  
  64. function fwd()
  65.   xCoord = xCoord + xDiff[orientation]
  66.   zCoord = zCoord + zDiff[orientation]
  67.   moved = false
  68.   while not(moved) do
  69.     moved = turtle.forward()
  70.   end
  71. end
  72.  
  73. function moveUp()
  74.   yCoord = yCoord +1
  75.   moved = false
  76.   while not(moved) do
  77.     moved = turtle.up()
  78.   end
  79. end
  80.  
  81. function moveDown()
  82.   yCoord = yCoord -1
  83.   moved = false
  84.   while not(moved) do
  85.     moved = turtle.down()
  86.   end
  87.   if yMin > yCoord then
  88.     yMin = yCoord
  89.   end
  90. end
  91.  
  92. function moveto(xtgt, ztgt, ytgt)
  93.   while ytgt < yCoord do
  94.     moveDown()
  95.   end
  96.   while ytgt > yCoord do
  97.     moveUp()
  98.   end
  99.   while xtgt < xCoord do
  100.   if xtgt < xCoord then
  101.     look("west")
  102.     while xtgt < xCoord do
  103.       fwd()
  104.     end
  105.   end
  106.   if xtgt > xCoord then
  107.     look("east")
  108.     while xtgt > xCoord do
  109.       fwd()
  110.     end
  111.   end
  112. end
  113.   if ztgt < zCoord then
  114.     look ("north")
  115.     while ztgt < zCoord do
  116.       fwd()
  117.     end
  118.   end
  119.   if ztgt > zCoord then
  120.     look("south")
  121.     while ztgt > zCoord do
  122.       fwd()
  123.     end
  124.   end  
  125. end
  126.  
  127. function restock()
  128.   xProgress = xCoord
  129.   zProgress = zCoord
  130.   yProgress = yCoord
  131.   oProgress = orientation
  132.  
  133.   moveto(xHome, zHome, yTvl)
  134.   moveto(xHome, zHome,yHome)
  135.   while turtle.getItemCount(16) ~= 64 do
  136.   print ("Stocking up")
  137.   end
  138.   turtle.select(2)
  139.     s=2
  140.   moveto(xProgress, zProgress, yTvl)   
  141.   moveto(xProgress, zProgress, yProgress)
  142.   look(orientations[oProgress])
  143. end
  144.  
  145. function refuel()
  146.   if turtle.getFuelLevel() < 10 then
  147.     turtle.refuel(1)
  148.   end
  149. end
  150.  
  151. function refuel3()
  152.   if turtle.getFuelLevel() > diag then
  153.     print("The fuel level is: "..turtle.getFuelLevel())
  154.     return
  155.   else
  156.   turtle.select(1)
  157.   turtle.refuel(1)
  158.   turtle.select(s)
  159.   print("Refuelled. New level is: "..turtle.getFuelLevel())
  160.   return
  161.   end
  162. end
  163.  
  164. function depth()  
  165.   fwd()
  166.   while turtle.detectDown() ~= true do
  167.     moveDown()
  168.     d = d+1
  169.     refuel()
  170.     os.sleep(0.5)
  171.   end
  172. end
  173.  
  174. function length()
  175.   right()
  176.     while turtle.detect() ~= true do
  177.       fwd()
  178.       l=l+1
  179.       refuel()
  180.       os.sleep(0.5)
  181.     end
  182. end
  183.  
  184. function width()
  185.   left()
  186.     while turtle.detect() ~= true do
  187.       fwd()
  188.       w=w+1
  189.       refuel()
  190.       os.sleep(0.5)
  191.     end
  192. end
  193.  
  194. function slots()
  195.     if turtle.getItemCount(s) == 0 then
  196.       if s < 16 then
  197.         turtle.select(s+1)
  198.         s = s+1
  199.         return
  200.       else
  201.         restock()
  202.       end
  203.      os.sleep(0.5)
  204.     end
  205.   end
  206.  
  207. function length1()
  208.   for i=1,(l-1) do
  209.     slots()
  210.     turtle.placeDown()
  211.     fwd()
  212.     os.sleep(0.5)
  213.   end
  214. end
  215.  
  216. function width1()
  217.   for i=1, (w-1) do
  218.     slots()
  219.     turtle.placeDown()
  220.     fwd()
  221.     os.sleep(0.5)
  222.   end
  223. end
  224.  
  225. function wall()
  226.   left()
  227.   moveUp()
  228.   for i=1,d-1 do
  229.     length1()
  230.     left()
  231.     width1()
  232.     left()
  233.     --os.sleep(10) -- debugging
  234.     refuel3()
  235.     length1()
  236.     left()
  237.     width1()
  238.     left()
  239.     turtle.up()
  240.     refuel3()
  241.   end
  242.   fwd()
  243. end
  244.  
  245.  
  246. turtle.select(s)
  247. turtle.refuel(1)
  248. turtle.select(2)
  249. s = 2
  250. depth()
  251. length()
  252. width()
  253. diag = l+w -- sets the refuel check number
  254. wall()
  255.  
  256. print ("length is"..l)
  257. print("width is"..w)
  258. print("diag value is"..diag)
Advertisement
Add Comment
Please, Sign In to add comment