Advertisement
lemmy101

Untitled

Apr 24th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. north = 1
  2. south = 2
  3. west = 3
  4. east = 4
  5.  
  6. northDir = {0, 1}
  7. southDir = {0, -1}
  8. eastDir = {1, 0}
  9. westDir = {-1, 0}
  10. startz = 70
  11. startx = 0
  12. starty = 0
  13. x = 0
  14. y = 0
  15. z = startz
  16.  
  17. -- start at facing north
  18. currentDir = north
  19.  
  20. local mov = {}
  21. local rot = {}
  22. local dig = {}
  23.  
  24. mov.forward = function(times)
  25. local boolean v = false
  26. for i=1,times do
  27. if turtle.detect() == false then
  28. while not turtle.forward() do
  29. sleep(1)
  30. end
  31. v = true
  32. else
  33. v = false
  34. return v
  35. end
  36. end
  37. return v
  38. end
  39.  
  40. mov.back = function(times)
  41. local boolean v = false
  42. for i=1,times do
  43. while not turtle.back() do
  44. sleep(1)
  45. end
  46. v = true
  47. end
  48. return v
  49. end
  50.  
  51. mov.up = function(times)
  52. local boolean v = false
  53. for i=1,times do
  54. if turtle.detectUp() == false then
  55. while not turtle.up() do
  56. sleep(1)
  57. end
  58. v = true
  59. else
  60. v = false
  61. return v
  62. end
  63. end
  64. return v
  65. end
  66.  
  67. mov.down = function(times)
  68. local boolean v = false
  69. for i=1,times do
  70. if turtle.detectDown() == false then
  71. while not turtle.down() do
  72. sleep(1)
  73. end
  74. v = true
  75. else
  76. v = false
  77. return v
  78. end
  79. end
  80. return v
  81. end
  82.  
  83. mov.place = function()
  84. while not turtle.place() do
  85. sleep(1)
  86. end
  87. return true
  88. end
  89.  
  90. rot.right = function()
  91. while not turtle.turnRight() do
  92. sleep(1)
  93. end
  94.  
  95. if RotatePosition == "FORWARD" then
  96. RotatePosition = "RIGHT"
  97. elseif RotatePosition == "LEFT" then
  98. RotatePosition = "FORWARD"
  99. end
  100. filewriteline("BranchMineData", 5, RotatePosition)
  101. return true
  102. end
  103.  
  104. rot.left = function()
  105. while not turtle.turnLeft() do
  106. sleep(1)
  107. end
  108.  
  109. if RotatePosition == "FORWARD" then
  110. RotatePosition = "LEFT"
  111. elseif RotatePosition == "RIGHT" then
  112. RotatePosition = "FORWARD"
  113. end
  114. filewriteline("BranchMineData", 5, RotatePosition)
  115. return true
  116. end
  117.  
  118. dig.forward = function()
  119. if turtle.detect() then
  120. while not turtle.dig() do
  121. sleep(1)
  122. end
  123. return true
  124. else
  125. print("No Block to mine forward")
  126. return false
  127. end
  128. end
  129.  
  130. dig.up = function()
  131. if turtle.detectUp() then
  132. while not turtle.digUp() do
  133. sleep(1)
  134. end
  135. return true
  136. else
  137. print("No Block to mine up")
  138. return false
  139. end
  140. end
  141.  
  142. dig.down = function()
  143. if turtle.detectDown() then
  144. while not turtle.digDown() do
  145. sleep(1)
  146. end
  147. return true
  148. else
  149. print("No Block to mine down")
  150. return false
  151. end
  152. end
  153.  
  154. local function DigMoveForward()
  155. if mov.forward(1) == false then
  156. dig.forward()
  157. sleep(0.5)
  158. DigMoveForward()
  159. end
  160. end
  161.  
  162. local function DigMoveUp()
  163. if mov.up(1) == false then
  164. dig.up()
  165. sleep(0.5)
  166. DigMoveUp()
  167. end
  168. z = z + 1
  169. end
  170.  
  171. local function DigMoveDown()
  172. if mov.down(1) == false then
  173. dig.down()
  174. sleep(0.5)
  175. DigMoveDown()
  176. end
  177. z = z - 1
  178. end
  179.  
  180.  
  181. function faceEast()
  182. if(currentDir == north) then
  183. turtle.turnRight()
  184. elseif(currentDir == west) then
  185. turtle.turnRight()
  186. turtle.turnRight()
  187. elseif(currentDir == south) then
  188. turtle.turnLeft()
  189. end
  190. currentDir = east;
  191. end
  192.  
  193. function faceNorth()
  194. if(currentDir == east) then
  195. turtle.turnLeft()
  196. elseif(currentDir == south) then
  197. turtle.turnRight()
  198. turtle.turnRight()
  199. elseif(currentDir == west) then
  200. turtle.turnRight()
  201. end
  202. currentDir = north;
  203. end
  204.  
  205. function faceSouth()
  206. if(currentDir == east) then
  207. turtle.turnRight()
  208. elseif(currentDir == north) then
  209. turtle.turnRight()
  210. turtle.turnRight()
  211. elseif(currentDir == west) then
  212. turtle.turnLeft()
  213. end
  214. currentDir = south;
  215. end
  216.  
  217. function faceWest()
  218. if(currentDir == south) then
  219. turtle.turnRight()
  220. elseif(currentDir == east) then
  221. turtle.turnRight()
  222. turtle.turnRight()
  223. elseif(currentDir == north) then
  224. turtle.turnLeft()
  225. end
  226. currentDir = west;
  227. end
  228.  
  229. function forward()
  230. DigiMoveForward()
  231. if(currentDir == east) then
  232. x = x + 1
  233. elseif(currentDir == north) then
  234. y = y + 1
  235. elseif(currentDir == south) then
  236. y = y - 1
  237. elseif(currentDir == west) then
  238. x = x - 1
  239. end
  240. end
  241.  
  242. function goWest()
  243. faceWest()
  244. forward()
  245. end
  246.  
  247. function goEast()
  248. faceWest()
  249. forward()
  250. end
  251.  
  252. function goNorth()
  253. faceNorth()
  254. forward()
  255. end
  256.  
  257. function goSouth()
  258. faceWest()
  259. forward()
  260. end
  261.  
  262. function goUp()
  263. DigMoveUp()
  264.  
  265. end
  266.  
  267. function goDown()
  268. DigMoveDown()
  269.  
  270. end
  271.  
  272.  
  273. local function readLines(sPath)
  274. local file = fs.open(sPath, "r") -- open the file
  275. if file then -- check if it's open
  276. local tLines = {} -- table to store the lines
  277. local sLine = file.readLine() -- read a line from the file
  278. while sLine do -- while there's a line in the file
  279. table.insert(tLines, sLine) -- add it to the table
  280. sLine = file.readLine() -- get the next line
  281. end
  282. file.close() -- close the file
  283. return tLines -- return the table with the lines
  284. end
  285. return nil -- there was an error opening the file, return nil to let the user know
  286. end
  287.  
  288. local function writeLines(sPath, tLines)
  289. local file = fs.open(sPath, "w") -- open the file
  290. if file then -- check if the file is open
  291. for _, sLine in ipairs(tLines) do -- for each line in the table
  292. file.writeLine(sLine) -- write the line
  293. end
  294. file.close() -- close the file
  295. end
  296. end
  297.  
  298. function filereadline(filename, line)
  299. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  300. if not tLines then -- if there was an error
  301. return nil -- return nil/error
  302. end
  303. return tLines[line] -- return the line
  304. end
  305.  
  306. function filewriteline(filename, line, text)
  307. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  308. if tLines then -- if there was no error
  309. tLines[line] = text -- set the line
  310. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  311. end
  312. end
  313.  
  314. turtle.refuel()
  315.  
  316. function goto(tx, ty, tz)
  317.  
  318. local dx = tx - x
  319. local dy = ty - y
  320. local dz = tz - z
  321.  
  322. if dz ~= 0 then
  323. while z ~= tz do
  324. if z > tz then
  325. goDown()
  326. else
  327. goUp()
  328. end
  329. end
  330. end
  331. if dx ~= 0 then
  332. while x ~= tx do
  333. if x > tx then
  334. goWest()
  335. else
  336. goEast()
  337. end
  338. end
  339. end
  340. if dy ~= 0 then
  341. while y ~= ty do
  342. if y > ty then
  343. goSouth()
  344. else
  345. goNorth()
  346. end
  347. end
  348. end
  349.  
  350. end
  351.  
  352. goto (-10, -10, startz - 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement