Advertisement
Wired2coffee

Turtle Coordinates Saver API

Feb 27th, 2012
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. function forward()
  2. file = io.open("tortoiseDir","r")
  3. direction = file:read()
  4. file:close()
  5. file = io.open("tortoiseX","r")
  6. x = file:read()
  7. file:close()
  8. file = io.open("tortoiseY","r")
  9. y = file:read()
  10. file:close()
  11. file = io.open("tortoiseZ","r")
  12. z = file:read()
  13. file:close()
  14. lawl = turtle.forward()
  15. if lawl == true then
  16. if direction == "north" then
  17. fs.delete("tortoiseZ")
  18. file = io.open("tortoiseZ","w")
  19. z = tonumber(z)
  20. file:write(z-1)
  21. file:close()
  22. end
  23. if direction == "west" then
  24. fs.delete("tortoiseX")
  25. file = io.open("tortoiseX","w")
  26. x = tonumber(x)
  27. file:write(x-1)
  28. file:close()
  29. end
  30. if direction == "south" then
  31. fs.delete("tortoiseZ")
  32. file = io.open("tortoiseZ","w")
  33. z = tonumber(z)
  34. file:write(z+1)
  35. file:close()
  36. end
  37. if direction == "east" then
  38. fs.delete("tortoiseX")
  39. file = io.open("tortoiseX","w")
  40. x = tonumber(x)
  41. file:write(x+1)
  42. file:close()
  43. end
  44. end
  45. end
  46. function getXYZD()
  47. file = io.open("tortoiseDir","r")
  48. direction = file:read()
  49. file:close()
  50. file = io.open("tortoiseX","r")
  51. x = tonumber(file:read())
  52. file:close()
  53. file = io.open("tortoiseY","r")
  54. y = tonumber(file:read())
  55. file:close()
  56. file = io.open("tortoiseZ","r")
  57. z = tonumber(file:read())
  58. file:close()
  59. return x,y,z,direction
  60. end
  61. function printXYZD()
  62. file = io.open("tortoiseDir","r")
  63. direction = file:read()
  64. file:close()
  65. file = io.open("tortoiseX","r")
  66. x = tonumber(file:read())
  67. file:close()
  68. file = io.open("tortoiseY","r")
  69. y = tonumber(file:read())
  70. file:close()
  71. file = io.open("tortoiseZ","r")
  72. z = tonumber(file:read())
  73. file:close()
  74. print(x.." "..y.." "..z.." "..direction)
  75. end
  76. function up()
  77. file = io.open("tortoiseY","r")
  78. y = file:read()
  79. y = tonumber(y)
  80. file:close()
  81. lawl = turtle.up()
  82. if lawl == true then
  83. fs.delete("tortoiseY")
  84. file = io.open("tortoiseY","w")
  85. if y < 126 and y >= 1 then
  86. file:write(y+1)
  87. else
  88. file = io.open("tortoiseY","w")
  89. file:write(y)
  90. end
  91. file:close()
  92. end
  93. end
  94. function turnLeft()
  95. file = io.open("tortoiseDir","r")
  96. direction = file:read()
  97. file:close()
  98. fs.delete("tortoiseDir")
  99. turtle.turnLeft()
  100. file = io.open("tortoiseDir","w")
  101. if direction == "west" then
  102. file:write("south")
  103. end
  104. if direction == "east" then
  105. file:write("north")
  106. end
  107. if direction == "south" then
  108. file:write("east")
  109. end
  110. if direction == "north" then
  111. file:write("west")
  112. end
  113. file:close()
  114. end
  115. function turnRight()
  116. file = io.open("tortoiseDir","r")
  117. direction = file:read()
  118. file:close()
  119. fs.delete("tortoiseDir")
  120. turtle.turnRight()
  121. file = io.open("tortoiseDir","w")
  122. if direction == "west" then
  123. file:write("north")
  124. end
  125. if direction == "east" then
  126. file:write("south")
  127. end
  128. if direction == "south" then
  129. file:write("west")
  130. end
  131. if direction == "north" then
  132. file:write("east")
  133. end
  134. file:close()
  135. end
  136. function setup()
  137. term.clear()
  138. term.setCursorPos( 1, 1 )
  139. print("What direction is your turtle currently facing? (lower case)")
  140. out = read()
  141. file = io.open("tortoiseDir","w")
  142. file:write(out)
  143. file:close()
  144. print("Current X Coord?")
  145. out = read()
  146. file = io.open("tortoiseX","w")
  147. file:write(out)
  148. file:close()
  149. print("Current Z Coord?")
  150. out = read()
  151. file = io.open("tortoiseZ","w")
  152. file:write(out)
  153. file:close()
  154. print("Current Y Coord?")
  155. out = read()
  156. file = io.open("tortoiseY","w")
  157. file:write(out)
  158. file:close()
  159. term.clear()
  160. term.setCursorPos( 1, 1 )
  161. print("Finished")
  162. end
  163. function down()
  164. file = io.open("tortoiseY","r")
  165. y = file:read()
  166. y = tonumber(y)
  167. file:close()
  168. lawl = turtle.down()
  169. fs.delete("tortoiseY")
  170. if lawl == true then
  171. file = io.open("tortoiseY","w")
  172. if y <= 126 and y > 1 then
  173. file:write(y-1)
  174. end
  175. else
  176. file = io.open("tortoiseY","w")
  177. file:write(y)
  178. end
  179. file:close()
  180. end
  181. function goto(x,y,z)
  182. xx,yy,zz,dd = tortoise.getXYZD()
  183. if yy < y then
  184. for i = 1,math.abs(yy-y) do
  185. tortoise.up()
  186. end
  187. elseif yy > y then
  188. for i = 1,math.abs(y-yy) do
  189. tortoise.down()
  190. end
  191. end
  192. if xx < x then
  193. for i = 1,4 do
  194. lol,lol,lol,dd = tortoise.getXYZD()
  195. if dd ~= "east" then
  196. tortoise.turnLeft()
  197. end
  198. end
  199. for i = 1,math.abs(xx-x) do
  200. tortoise.forward()
  201. end
  202. elseif xx > x then
  203. for i = 1,4 do
  204. lol,lol,lol,dd = tortoise.getXYZD()
  205. if dd ~= "west" then
  206. tortoise.turnRight()
  207. end
  208. end
  209. for i = 1,math.abs(x-xx) do
  210. tortoise.forward()
  211. end
  212. end
  213. if zz < z then
  214. for i = 1,4 do
  215. lol,lol,lol,dd = tortoise.getXYZD()
  216. if dd ~= "south" then
  217. tortoise.turnLeft()
  218. end
  219. end
  220. for i = 1,math.abs(zz-z) do
  221. tortoise.forward()
  222. end
  223. elseif zz > z then
  224. for i = 1,4 do
  225. lol,lol,lol,dd = tortoise.getXYZD()
  226. if dd ~= "north" then
  227. tortoise.turnRight()
  228. end
  229. end
  230. for i = 1,math.abs(z-zz) do
  231. tortoise.forward()
  232. end
  233. end
  234. xx,yy,zz,dd = tortoise.getXYZD()
  235. if xx ~= x or yy ~= y or zz ~= z then
  236. print("Obstructed during flight.")
  237. return false
  238. else
  239. return true
  240. end
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement