Advertisement
thatparadox

carriageTurtle

Aug 29th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. con = "off"
  4.  
  5. function topToBack() -- move turtle from top of controller to back
  6. turtle.forward()
  7. turtle.down()
  8. turtle.turnRight()
  9. turtle.turnRight()
  10. pos = "back"
  11. file = fs.open("pos", "w")
  12. file.write(pos)
  13. file:close()
  14. eng = peripheral.wrap("front")
  15. end
  16.  
  17. function backToTop() -- move turtle from behind engine to top
  18. turtle.up()
  19. turtle.forward()
  20. turtle.turnRight()
  21. turtle.turnRight()
  22. pos = "top"
  23. file = fs.open("pos", "w")
  24. file.write(pos)
  25. file:close()
  26. eng = peripheral.wrap("bottom")
  27. end
  28.  
  29.  
  30. function goDown()
  31. dir = 29
  32. eng.move(0, false, false)
  33. turtle.down()
  34. yPos = yPos - 1
  35. file = fs.open("y", "w")
  36. file.write(yPos)
  37. file:close()
  38. sleep(1)
  39. end
  40.  
  41. function goUp()
  42. dir = 42
  43. eng.move(1, false, false)
  44. turtle.up()
  45. yPos = yPos + 1
  46. file = fs.open("y", "w")
  47. file.write(yPos)
  48. file:close()
  49. sleep(1)
  50. end
  51.  
  52. function goForward()
  53. dir = 17
  54. eng.move(2, false, false)
  55. os.sleep(1)
  56. zPos = zPos - 1
  57. file = fs.open("z", "w")
  58. file.write(zPos)
  59. file:close()
  60. turtle.forward()
  61. end
  62.  
  63. function goBack()
  64. dir = 31
  65. eng.move(3, false, false)
  66. turtle.forward()
  67. zPos = zPos + 1
  68. file = fs.open("z", "w")
  69. file.write(zPos)
  70. file:close()
  71. sleep(1)
  72. end
  73.  
  74. function goRight()
  75. dir = 32
  76. eng.move(5, false, false)
  77. turtle.turnRight()
  78. turtle.forward()
  79. turtle.turnLeft()
  80. xPos = xPos + 1
  81. file = fs.open("x", "w")
  82. file.write(xPos)
  83. file:close()
  84. os.sleep(0.2)
  85. end
  86.  
  87. function goLeft()
  88. dir = 30
  89. eng.move(4, false, false)
  90. turtle.turnLeft()
  91. turtle.forward()
  92. turtle.turnRight()
  93. xPos = xPos - 1
  94. file = fs.open("x", "w")
  95. file.write(xPos)
  96. file:close()
  97. os.sleep(0.2)
  98. end
  99.  
  100. function display() -- screen display
  101. term.clear()
  102. x,y = term.getSize()
  103. term.setCursorPos(x/2, y/3-1)
  104. print("W")
  105. term.setCursorPos((x/5*4) - 2, y/3-1)
  106. print("Auto(R)")
  107. term.setCursorPos(x/2-3, y/3)
  108. print("Forward")
  109. term.setCursorPos(x/5*4, y/3)
  110. print(con)
  111. term.setCursorPos(x/5, y/3*2-1)
  112. print("A")
  113. term.setCursorPos(x/2, y/3*2-1)
  114. print("S")
  115. term.setCursorPos(x/5*4, y/3*2-1)
  116. print("D")
  117. term.setCursorPos(x/5-1, y/3*2)
  118. print("Left")
  119. term.setCursorPos(x/2-1, y/3*2)
  120. print("Back")
  121. term.setCursorPos(x/5*4-2, y/3*2)
  122. print("Right")
  123. term.setCursorPos(1, y-3)
  124. print("[Shift]")
  125. term.setCursorPos(x/5*4, y-3)
  126. print("C")
  127. term.setCursorPos(3,y-2)
  128. print("Up")
  129. term.setCursorPos(x/5*4 - 1,y-2)
  130. print("Set")
  131. term.setCursorPos(1, y-1)
  132. print("[Ctrl]")
  133. term.setCursorPos(x/5*4-5, y-1)
  134. print("Destination")
  135. term.setCursorPos(2, y)
  136. print("Down")
  137. end
  138.  
  139. function waypoint() -- lets user set a waypoint to travel to
  140. term.clear()
  141. term.setCursorPos(1,1)
  142. write("Enter x coordinate of destination: ")
  143. xDest = io.read()
  144. xDest = tonumber(xDest)
  145. term.clear()
  146. term.setCursorPos(1,1)
  147. write("Enter y coordinate of destination: ")
  148. yDest = io.read()
  149. yDest = tonumber(yDest)
  150. term.clear()
  151. term.setCursorPos(1,1)
  152. write("Enter z coordinate of destination: ")
  153. zDest = io.read()
  154. zDest = tonumber(zDest)
  155. term.clear()
  156. term.setCursorPos(1,1)
  157. print("Traveling to "..xDest.." "..yDest.." "..zDest)
  158. if pos == "top" then
  159. topToBack()
  160. end
  161. if yPos < yDest then
  162. for i = 1, yDest - yPos do
  163. goUp()
  164. end
  165. elseif yPos > yDest then
  166. for i = 1, yPos - yDest do
  167. goDown()
  168. end
  169. end
  170. if xPos < xDest then
  171. if xDest - xPos < 0 then
  172. xDis = (xDest - xPos) * -1
  173. else
  174. xDis = xDest - xPos
  175. end
  176. for i = 1, xDis do
  177. goRight()
  178. end
  179. elseif xPos > xDest then
  180. if xPos - xDest < 0 then
  181. xDis = (xPos - xDest) * -1
  182. else
  183. xDis = xPos - xDest
  184. end
  185. for i = 1, xDis do
  186. goLeft()
  187. end
  188. end
  189. if zPos < zDest then
  190. if zPos - zDest < 0 then
  191. zDis = (zPos - zDest) * -1
  192. else
  193. --zDis = zDest - xPos
  194. zDis = zPos - zDest
  195. end
  196. if pos == "back" then
  197. backToTop()
  198. end
  199. for i = 1, zDis do
  200. goBack()
  201. end
  202. elseif zPos > zDest then
  203. if zDest - zPos < 0 then
  204. zDis = (zPos - zDest) * -1
  205. else
  206. zDis = zPos - zDest
  207. end
  208. for i = 1, xDis do
  209. goForward()
  210. end
  211. end
  212. display()
  213. end
  214.  
  215.  
  216. -- determine turtle position above or behind the engine
  217. result = fs.exists("pos")
  218. if result == true then
  219. file = io.open("pos", "r")
  220. pos = file.read()
  221. file:close()
  222. else
  223. pos = "back"
  224. file = fs.open("pos", "w")
  225. file.write(pos)
  226. file:close()
  227. end
  228. if pos == "back" then
  229. eng = peripheral.wrap("front")
  230. else
  231. eng = peripheral.wrap("bottom")
  232. end
  233.  
  234.  
  235. function coords()
  236. file = io.open("x", "r")
  237. xPos = file.read()
  238. xPos = tonumber(xPos)
  239. file:close()
  240. file = io.open("y", "r")
  241. yPos = file.read()
  242. yPos = tonumber(yPos)
  243. file:close()
  244. file = io.open("z", "r")
  245. zPos = file.read()
  246. zPos = tonumber(zPos)
  247. file:close()
  248. end
  249.  
  250. result = fs.exists("x") --determine if turtle knows it's position
  251. if result == true then
  252. file = io.open("x", "r")
  253. xPos = file.read()
  254. xPos = tonumber(xPos)
  255. file:close()
  256. file = io.open("y", "r")
  257. yPos = file.read()
  258. yPos = tonumber(yPos)
  259. file:close()
  260. file = io.open("z", "r")
  261. zPos = file.read()
  262. zPos = tonumber(zPos)
  263. file:close()
  264. else
  265. term.clear()
  266. term.setCursorPos(1,1)
  267. write("Enter the Turtle's x coordinate: ")
  268. xPos = io.read()
  269. file = fs.open("x", "w")
  270. file.write(xPos)
  271. file:close()
  272. term.clear()
  273. xPos = tonumber(xPos)
  274. term.setCursorPos(1,1)
  275. write("Enter the Turtle's y coordinate: ")
  276. yPos = io.read()
  277. file = fs.open("y", "w")
  278. file.write(yPos)
  279. file:close()
  280. term.clear()
  281. yPos = tonumber(yPos)
  282. term.setCursorPos(1,1)
  283. write("Enter the Turtle's z coordinate: ")
  284. zPos = io.read()
  285. file = fs.open("z", "w")
  286. file.write(zPos)
  287. file:close()
  288. zPos = tonumber(zPos)
  289. term.clear()
  290. term.setCursorPos(1,1)
  291. end
  292.  
  293. display()
  294. while true do
  295. coords()
  296. timeout = os.startTimer(0.5)
  297. event, param = os.pullEvent()
  298. if event == "timer" then
  299. if con == "on" then
  300. if dir ~= 31 then
  301. if pos == "top" then
  302. topToBack()
  303. end
  304. if dir == 17 then
  305. goForward()
  306. elseif dir == 32 then
  307. goRight()
  308. elseif dir == 30 then
  309. goLeft()
  310. elseif dir == 42 then
  311. goUp()
  312. elseif dir == 29 then
  313. goDown()
  314. end
  315. elseif dir == 31 then
  316. dir = 31
  317. if pos == "back" then
  318. backToTop()
  319. end
  320. goBack()
  321. end
  322. end
  323. end
  324. if param ~= 31 and event == "key" then
  325. if pos == "top" then
  326. topToBack()
  327. end
  328. if param == 17 then
  329. goForward()
  330. elseif param == 32 then
  331. goRight()
  332. elseif param == 30 then
  333. goLeft()
  334. elseif param == 42 then
  335. goUp()
  336. elseif param == 29 then
  337. goDown()
  338. elseif param == 19 then
  339. if con == "on" then
  340. con = "off"
  341. else
  342. con = "on"
  343. end
  344. os.sleep(0.5)
  345. dir = nil
  346. term.setCursorPos(x/5*4, y/3-1)
  347. print(con.." ")
  348. elseif param == 46 then
  349. waypoint()
  350. end
  351. elseif param == 31 then
  352. dir = 31
  353. if pos == "back" then
  354. backToTop()
  355. end
  356. goBack()
  357. end
  358. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement