Advertisement
rickydaan

[LUA] [CC] [TURTLES] MyTurtleCoords API [Build 1]

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