Advertisement
PaffcioStudio

turtle smartmovement.txt

Feb 2nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. function flyingloop()
  2.     while (fly == 1) do
  3.         if (tonumber(y) < tonumber(ty)) then
  4.         tup()
  5.         elseif (tonumber(z) > tonumber(tz)) then
  6.         facedir("n")
  7.         tforward()
  8.         elseif (tonumber(z) < tonumber(tz)) then
  9.         facedir("s")
  10.         tforward()
  11.         elseif (tonumber(x) < tonumber(tx)) then
  12.         facedir("e")
  13.         tforward()
  14.         elseif (tonumber(x) > tonumber(tx)) then
  15.         facedir("w")
  16.         tforward()
  17.         elseif (tonumber(y) > tonumber(ty)) then
  18.         tdown()
  19.         else
  20.         fly = 0
  21.         end
  22.     end
  23. end
  24.  
  25. function facedir(dir)
  26. if (dir == "n") then
  27. dirnum = 1
  28. elseif (dir == "e") then
  29. dirnum = 2
  30. elseif (dir == "s") then
  31. dirnum = 3
  32. elseif (dir == "w") then
  33. dirnum = 4
  34. end
  35. facingprocess = 1
  36. while (facingprocess == 1) do
  37.         if (facingnum < dirnum) then
  38.             turtle.turnRight()
  39.             facingnum = (facingnum + 1)
  40.             elseif (facingnum > dirnum) then
  41.             turtle.turnLeft()
  42.             facingnum = (facingnum - 1)
  43.             elseif (facingnum == dirnum) then
  44.             facingprocess = 0
  45.         end
  46.        
  47.         if (facingnum == 5) then
  48.             facingnum = 1
  49.             elseif (facingnum == 0) then
  50.             facingnum = 4
  51.         end
  52. end
  53. end
  54.  
  55. function tforward()
  56.     turtle.dig()
  57.     turtle.forward()
  58.     if (facingnum == 1) then
  59.     z = (z - 1)
  60.     elseif (facingnum == 2) then
  61.     x = (x + 1)
  62.     elseif (facingnum == 3) then
  63.     z = (z + 1)
  64.     elseif (facingnum == 4) then
  65.     x = (x - 1)
  66.     end
  67. end
  68.  
  69. function tup()
  70.     turtle.digUp()
  71.     turtle.up()
  72.     y = (y+1)
  73. end
  74.  
  75. function tdown()
  76.     turtle.digDown()
  77.     turtle.down()
  78.     y = (y-1)
  79. end
  80.  
  81. function flyto(tarx,tary,tarz)
  82. tx = tonumber(tarx)
  83. ty = tonumber(tary)
  84. tz = tonumber(tarz)
  85. fly = 1
  86. flyingloop()
  87. end
  88.  
  89. function setcoords()
  90. term.clear()
  91. term.setCursorPos(1,1)
  92. print("Please input target coordinates")
  93. write("X: ")
  94. targetx = read()
  95. write("Y: ")
  96. targety = read()
  97. write("Z: ")
  98. targetz = read()
  99. print("Coordinates set.")
  100. sleep(2)
  101. end
  102.  
  103. function status()
  104. term.clear()
  105. term.setCursorPos(1,1)
  106. if (facingnum == 1) then
  107. facing = "n"
  108. elseif (facingnum == 2) then
  109. facing = "e"
  110. elseif (facingnum == 3) then
  111. facing = "s"
  112. elseif (facingnum == 4) then
  113. facing = "w"
  114. end
  115. print("X: "..x.." Y: "..y.." Z: "..z.." Facing: "..facing)
  116. sleep(4)
  117. end
  118.  
  119. function calibrate()
  120. print("Please input turtles current X, Y and Z coordinates.")
  121. write("X: ")
  122. tempx = read()
  123. x = tonumber(tempx)
  124. write("Y: ")
  125. tempy = read()
  126. y = tonumber(tempy)
  127. write("Z: ")
  128. tempz = read()
  129. z = tonumber(tempz)
  130. print("")
  131. print("Coordinates set.")
  132. print("Please input the facing of the turtle, n / e / s / w")
  133. write("Facing: ")
  134. facingloop = 1
  135. while (facingloop == 1) do
  136. facing = read()
  137. if (facing == "n") then
  138. facingnum = 1
  139. facingloop = 0
  140. elseif (facing == "e") then
  141. facingloop = 0
  142. facingnum = 2
  143. elseif (facing == "s") then
  144. facingloop = 0
  145. facingnum = 3
  146. elseif (facing == "w") then
  147. facingloop = 0
  148. facingnum = 4
  149. else
  150. print("Invalid input")
  151. end
  152. end
  153. print("Facing set.")
  154. sleep(1)
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement