Advertisement
leo1553

Untitled

Jan 16th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. -- Advaced Turtle Moviment
  2. -- and
  3. -- Adv Mining System
  4. -- by leo1553
  5. posX = 0
  6. posY = 0
  7. posZ = 0
  8. rot = 0
  9. local rotNames = {"Front", "Left", "Back", "Right"}
  10.  
  11. --local args = {...}
  12.  
  13. -- Move Functions
  14. function forward()
  15. if(turtle.forward() == true) then
  16. if(rot == 0) then
  17. posX = posX + 1
  18. elseif(rot == 1) then
  19. posZ = posZ + 1
  20. elseif(rot == 2) then
  21. posX = posX - 1
  22. elseif(rot == 3) then
  23. posZ = posZ - 1
  24. end
  25. return true
  26. end
  27. return false
  28. end
  29.  
  30. function back()
  31. if(turtle.back() == true) then
  32. if(rot == 0) then
  33. posX = posX - 1
  34. elseif(rot == 1) then
  35. posZ = posZ - 1
  36. elseif(rot == 2) then
  37. posX = posX + 1
  38. elseif(rot == 3) then
  39. posZ = posZ + 1
  40. end
  41. return true
  42. end
  43. return false
  44. end
  45.  
  46. function up()
  47. if(turtle.up() == true) then
  48. posY = posY + 1
  49. return true
  50. end
  51. return false
  52. end
  53.  
  54. function down()
  55. if(turtle.down() == true) then
  56. posY = posY - 1
  57. return true
  58. end
  59. return false
  60. end
  61.  
  62. function turnLeft()
  63. turtle.turnLeft()
  64. rot = rot - 1
  65. if(rot < 0) then
  66. rot = 3
  67. end
  68. end
  69.  
  70. function turnRight()
  71. turtle.turnRight()
  72. rot = rot + 1
  73. if(rot > 3) then
  74. rot = 0
  75. end
  76. end
  77.  
  78. -- Adv Move Functions
  79. function lookAt(finalRot)
  80. if(rot == finalRot) then
  81. return
  82. end
  83.  
  84. local finalValue = rot - finalRot
  85. if(finalValue < 0) then
  86. while(rot ~= finalRot) do
  87. turnRight()
  88. end
  89. else
  90. while(rot ~= finalRot) do
  91. turnLeft()
  92. end
  93. end
  94. end
  95.  
  96. function moveToX(x)
  97. if(x > posX) then
  98. lookAt(0)
  99. else
  100. lookAt(2)
  101. end
  102. while(posX ~= x) do
  103. forward()
  104. printPos()
  105. end
  106. end
  107.  
  108. function moveToY(y)
  109. if(y < posY) then
  110. while(posY ~= y) do
  111. down()
  112. printPos()
  113. end
  114. else
  115. while(posY ~= y) do
  116. up()
  117. printPos()
  118. end
  119. end
  120. end
  121.  
  122. function moveToZ(z)
  123. if(z < posZ) then
  124. lookAt(3)
  125. else
  126. lookAt(1)
  127. end
  128. while(posZ ~= z) do
  129. forward()
  130. printPos()
  131. end
  132. end
  133.  
  134. function moveTo(x, y, z) -- Move Only (Do Not Dig)
  135. moveToY(y)
  136. moveToZ(z)
  137. moveToX(x)
  138. end
  139.  
  140. -- Debug Functions
  141. function getRotationName(rotation)
  142. return rotNames[rotation + 1]
  143. end
  144.  
  145. function printPos()
  146. print("Current Pos:".. posX ..", ".. posY ..", ".. posZ .." - ".. getRotationName(rot))
  147. end
  148.  
  149. -- API End
  150.  
  151. local digX
  152. local digY
  153. local digZ
  154. local diggedBlocks = 0
  155. local digOrientation = 1
  156. local goingToX
  157. local goingToZ
  158.  
  159. local mineBlocks = { false, false }
  160.  
  161. local args = {...}
  162.  
  163. -- Dig Functions
  164. function dig()
  165. if(turtle.detect() == true) then
  166. if(turtle.dig() == true) then
  167. return true
  168. end
  169. return false
  170. end
  171. return true
  172. end
  173.  
  174. function digUp()
  175. if(turtle.detectUp() == true) then
  176. if(turtle.digUp() == true) then
  177. return true
  178. end
  179. return false
  180. end
  181. return true
  182. end
  183.  
  184. function digDown()
  185. if(turtle.detectDown() == true) then
  186. if(turtle.digDown() == true) then
  187. return true
  188. end
  189. return false
  190. end
  191. return true
  192. end
  193.  
  194. -- Adv Dig Functions
  195. function lastDigPos(from, to)
  196. if(from == to) then
  197. return to
  198. elseif(from < to) then
  199. return to - 1
  200. else
  201. return from - 1
  202. end
  203. return from
  204. end
  205.  
  206. function digToX(x)
  207. if(x > posX) then
  208. lookAt(0)
  209. else
  210. lookAt(2)
  211. end
  212. while(posX ~= x) do
  213. if(dig() == false or(mineBlocks[1] == true and digDown() == false) or(mineBlocks[2] == true and digUp() == false)) then
  214. print("Turtle could not break the block. Stopped.")
  215. return
  216. end
  217. forward()
  218. printPos()
  219. end
  220. if((mineBlocks[1] == true and digDown() == false) or(mineBlocks[2] == true and digUp() == false)) then
  221. print("Turtle could not break the block. Stopped.")
  222. return
  223. end
  224. end
  225.  
  226. function digToY(y)
  227. if(y < posY) then
  228. while(posY ~= y) do
  229. if(digDown() == false) then
  230. print("Turtle could not break the block. Stopped.")
  231. return
  232. end
  233. down()
  234. printPos()
  235. end
  236. else
  237. while(posY ~= y) do
  238. if(digUp() == false) then
  239. print("Turtle could not break the block. Stopped.")
  240. return
  241. end
  242. up()
  243. printPos()
  244. end
  245. end
  246. end
  247.  
  248. function digToZ(z)
  249. if(z < posZ) then
  250. lookAt(3)
  251. else
  252. lookAt(1)
  253. end
  254. while(posZ ~= z) do
  255. if(dig() == false or(mineBlocks[1] == true and digDown() == false) or(mineBlocks[2] == true and digUp() == false)) then
  256. print("Turtle could not break the block. Stopped.")
  257. return
  258. end
  259. forward()
  260. printPos()
  261. end
  262. if((mineBlocks[1] == true and digDown() == false) or(mineBlocks[2] == true and digUp() == false)) then
  263. print("Turtle could not break the block. Stopped.")
  264. return
  265. end
  266. end
  267.  
  268. function digTo(x, y, z)
  269. if(x ~= 0) then
  270. x = x - 1
  271. end
  272. if(y ~= 0) then
  273. y = y - 1
  274. end
  275. if(z ~= 0) then
  276. z = z - 1
  277. end
  278.  
  279. digToX(x)
  280. digToZ(z)
  281. digToY(y)
  282. end
  283.  
  284. -- API End
  285. digX = tonumber(args[1])
  286. digY = tonumber(args[2])
  287. digZ = tonumber(args[3])
  288.  
  289. function updateBreakVar()
  290. if(digY > posY + 1) then
  291. if(digY > posY + 2) then
  292. digUp()
  293. up()
  294. mineBlocks = {true, true}
  295. else
  296. mineBlocks = {false, true}
  297. end
  298. end
  299. end
  300.  
  301. function updateGoingToX()
  302. if(goingToZ ~= 0) then
  303. goingToX = 0
  304. else
  305. goingToX = digX - 1
  306. end
  307. end
  308. function updateGoingToZ()
  309. if(goingToZ ~= 0) then
  310. goingToZ = 0
  311. else
  312. goingToZ = digZ - 1
  313. end
  314. end
  315.  
  316. function changeOriotation()
  317. digOrientation = digOrientation * -1
  318. end
  319.  
  320. -- Set Variables
  321. updateBreakVar()
  322. digOrientation = 1
  323. goingToX = digX - 1
  324. goingToZ = digZ - 1
  325.  
  326. -- Dig First Line
  327. digToZ(goingToZ)
  328. updateGoingToZ()
  329.  
  330. while(posY ~= (digY - 1)) do
  331. while(posX ~= goingToX) do
  332. digToX(posX + digOrientation)
  333. digToZ(goingToZ)
  334. updateGoingToZ()
  335. end
  336.  
  337. if(mineBlocks[2] == true) then
  338. print("Done 1")
  339. up()
  340. end
  341. if(posY + 1 ~= digY) then
  342. print("Done 2")
  343. digToY(posY + 1)
  344. updateBreakVar()
  345. changeOriotation()
  346. updateGoingToX()
  347.  
  348. digToZ(goingToZ)
  349. updateGoingToZ()
  350. end
  351. end
  352. moveTo(0, 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement