Advertisement
rickydaan

[Lua][CC][Turtles] MyTurtleCoords API [Build 2]

Feb 29th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. -- COPYRIGHTED CONTENT
  2. -- Do not distribute
  3. -- (c) 2012 February 28 - Rickydaan and Turboneater, ask Rickydaan on Minecraftforums to copy this into your own program
  4.  
  5. if turtle then
  6.  
  7.  
  8.  
  9.  
  10.  
  11. function updateDir(value)
  12. file = io.open("cur/dir", "w")
  13. file : write(value)
  14. file : close()
  15. end
  16.  
  17. function turnLeft()
  18. file = io.open("cur/dir", "r")
  19. curdirection = file : read()
  20. file : close()
  21.  
  22. if curdirection == "1" then -- north
  23. updateDir("4")
  24. elseif curdirection == "2" then -- east
  25. updateDir("1")
  26. elseif curdirection == "3" then -- south
  27. updateDir("2")
  28. elseif curdirection == "4" then -- west
  29. updateDir("3")
  30. else
  31. print("Unsupported direction")
  32. end
  33. end
  34.  
  35. function turnRight()
  36. file = io.open("cur/dir", "r")
  37. curdirection = file : read()
  38. file : close()
  39.  
  40. if curdirection == "1" then -- north
  41. updateDir("2")
  42. elseif curdirection == "2" then -- east
  43. updateDir("3")
  44. elseif curdirection == "3" then -- then south
  45. updateDir("4")
  46. elseif curdirection == "4" then -- west
  47. updateDir("1")
  48. else
  49. print("Unsupported direction")
  50. end
  51. end
  52.  
  53.  
  54. function printDir()
  55. file = io.open("cur/dir", "r")
  56. direction = file : read()
  57. file : close()
  58.  
  59. if direction == "1" then
  60. print("north")
  61. elseif direction == "2" then
  62. print("east")
  63. elseif direction == "3" then
  64. print("south")
  65. elseif direction == "4" then
  66. print("west")
  67. else
  68. print("Error: Unknown direction")
  69. end
  70. end
  71.  
  72. function updateDirForward()
  73. file = io.open("cur/dir", "r")
  74. direction = file : read()
  75. file : close()
  76.  
  77. if direction == "1" then -- north
  78. file = io.open("cur/z", "r")
  79. ona = file : read()
  80. file : close()
  81. na = tonumber(ona)
  82. cna = na - 1
  83.  
  84. file = io.open("cur/z", "w")
  85. file : write(cna)
  86. file : close()
  87.  
  88.  
  89. elseif direction == "2" then -- east
  90. file = io.open("cur/x", "r")
  91. ocx = file : read()
  92. file : close()
  93. cx = tonumber(ocx)
  94. cnx = cx - 1
  95.  
  96. file = io.open("cur/x", "w")
  97. file : write(cnx)
  98. file : close()
  99.  
  100. elseif direction == "3" then -- south
  101. file = io.open("cur/z", "r")
  102. ocs = file : read()
  103. file : close()
  104. cs = tonumber(ocs)
  105. cns = cs + 1
  106.  
  107. file = io.open("cur/z", "w")
  108. file : write(cns)
  109. file : close()
  110.  
  111. elseif direction == "4" then -- west
  112. file = io.open("cur/x", "r")
  113. ocw = file : read()
  114. file : close()
  115. cw = tonumber(ocw)
  116. cnw = cw + 1
  117.  
  118. file = io.open("cur/x", "w")
  119. file : write(cnw)
  120. file : close()
  121.  
  122. else
  123. print("Error at config -> Please type config in the main terminal to fix this")
  124. end
  125. end
  126.  
  127. function updateDirBack()
  128. file = io.open("cur/dir", "r")
  129. direction = file : read()
  130. file : close()
  131.  
  132. if direction == "1" then -- north
  133. file = io.open("cur/z", "r")
  134. ona = file : read()
  135. file : close()
  136. na = tonumber(ona)
  137. cna = na + 1
  138.  
  139. file = io.open("cur/z", "w")
  140. file : write(cna)
  141. file : close()
  142.  
  143.  
  144. elseif direction == "2" then -- east
  145. file = io.open("cur/x", "r")
  146. ocx = file : read()
  147. file : close()
  148. cx = tonumber(ocx)
  149. cnx = cx + 1
  150.  
  151. file = io.open("cur/x", "w")
  152. file : write(cnx)
  153. file : close()
  154.  
  155. elseif direction == "3" then -- south
  156. file = io.open("cur/z", "r")
  157. ocs = file : read()
  158. file : close()
  159. cs = tonumber(ocs)
  160. cns = cs - 1
  161.  
  162. file = io.open("cur/z", "w")
  163. file : write(cns)
  164. file : close()
  165.  
  166. elseif direction == "4" then -- west
  167. file = io.open("cur/x", "r")
  168. ocw = file : read()
  169. file : close()
  170. cw = tonumber(ocw)
  171. cnw = cw - 1
  172.  
  173. file = io.open("cur/x", "w")
  174. file : write(cnw)
  175. file : close()
  176.  
  177. else
  178. print("Error at config -> Please type config in the main terminal to fix this")
  179. end
  180. end
  181.  
  182. function coordsShow()
  183.  
  184. file = io.open("cur/x", "r")
  185. x = file : read()
  186. file : close()
  187.  
  188. file = io.open("cur/z", "r")
  189. z = file : read()
  190. file : close()
  191.  
  192. file = io.open("cur/y", "r")
  193. y = file : read()
  194. file : close()
  195. printDir()
  196. print("X: " .. x)
  197. print("Y: " .. y)
  198. print("Z: " .. z)
  199. end
  200.  
  201. function coordsForward()
  202. if turtle.detect() then
  203. print("Unable to go forward")
  204. else
  205. turtle.forward()
  206. updateDirBack()
  207. end
  208. end
  209.  
  210. function coordsBack()
  211. turtle.back()
  212. updateDirForward()
  213. end
  214.  
  215. function coordsUp()
  216. if turtle.detectUp() == true then
  217. print("Unable to go up")
  218. else
  219. turtle.up()
  220. file = io.open("cur/y", "r")
  221. chh = file : read()
  222. file : close()
  223. ch = tonumber(chh)
  224.  
  225. nch = ch + 1
  226.  
  227. file = io.open("cur/y", "w")
  228. file : write(nch)
  229. file : close()
  230. end
  231. end
  232.  
  233. function coordsDown()
  234. if turtle.detectDown() == true then
  235. print("Unable to go down")
  236. else
  237. turtle.down()
  238. file = io.open("cur/y", "r")
  239. hc = file : read()
  240. file : close()
  241. hcc = tonumber(hc)
  242.  
  243. nhc = hcc - 1
  244.  
  245. file = io.open("cur/y", "w")
  246. file : write(nhc)
  247. file : close()
  248. end
  249. end
  250.  
  251. function coordsTurnRight()
  252. turtle.turnRight()
  253. turnRight()
  254. end
  255.  
  256. function coordsTurnLeft()
  257. turtle.turnLeft()
  258. turnLeft()
  259. end
  260.  
  261. function coordsTurnBack()
  262. coordsTurnRight()
  263. coordsTurnRight()
  264. end
  265.  
  266.  
  267. function coordsSetx(newx)
  268. x = tonumber(newx)
  269. file = io.open("cur/x", "w")
  270. file : write(x)
  271. file : close()
  272. end
  273.  
  274. function coordsSety(newy)
  275. y = tonumber(newy)
  276. file = io.open("cur/y", "w")
  277. file : write(y)
  278. file : close()
  279. end
  280.  
  281. function coordsSetz(newz)
  282. z = tonumber(newz)
  283. file = io.open("cur/z", "w")
  284. file : write(z)
  285. file : close()
  286. end
  287.  
  288. function coordsSetDir(newdir)
  289. if newdir == "north" then
  290. updateDir("1")
  291. elseif newdir == "east" then
  292. updateDir("2")
  293. elseif newdir == "south" then
  294. updateDir("3")
  295. elseif newdir == "west" then
  296. updateDir("4")
  297. elseif newdir == "1" then
  298. updateDir("1")
  299. elseif newdir == "2" then
  300. updateDir("2")
  301. elseif newdir == "3" then
  302. updateDir("3")
  303. elseif newdir == "4" then
  304. updateDir("4")
  305. else
  306. print("Unsupported direction")
  307. end
  308. end
  309.  
  310. function coordsHelpDir()
  311. print("Type F3, These are coordinate values")
  312. print("(1), North, is when the Z is going down")
  313. print("(2), East, is then the X is going down")
  314. print("(3), South, is when the Z is going up")
  315. print("(4), West, is when the X is going up")
  316. end
  317.  
  318. function coordsHelp()
  319. print("coordsShow() - Shows the coords")
  320. print("coordsForward() - Forward")
  321. print("coordsBack() - Back")
  322. print("coordsUp() - Up")
  323. print("coordsDown() - Down")
  324. print("coordsTurnRight() - turns right")
  325. print("coordsTurnLeft() - turns left")
  326. print("coordsTurnBack() - turns back")
  327. print("coordsSet*(VALUE) - changes a value")
  328. print("coordsSetDir(VALUE) - changes the direction value")
  329. print("coordsHelpDir() - help with the directions")
  330. print("* = x or y or z")
  331. end
  332.  
  333. else
  334. print("This API requires to be runned on a Turtle")
  335. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement