Advertisement
lemmy101

Untitled

Apr 24th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 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. function DigMoveForward()
  155. if mov.forward(1) == false then
  156. dig.forward()
  157. sleep(0.5)
  158. DigMoveForward()
  159. end
  160. end
  161.  
  162. 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. 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.  
  232. if(currentDir == east) then
  233. x = x + 1
  234. elseif(currentDir == north) then
  235. y = y + 1
  236. elseif(currentDir == south) then
  237. y = y - 1
  238. elseif(currentDir == west) then
  239. x = x - 1
  240. end
  241. end
  242.  
  243. function goWest()
  244. faceWest()
  245. forward()
  246. end
  247.  
  248. function goEast()
  249. faceWest()
  250. forward()
  251. end
  252.  
  253. function goNorth()
  254. faceNorth()
  255. forward()
  256. end
  257.  
  258. function goSouth()
  259. faceWest()
  260. forward()
  261. end
  262.  
  263. function goUp()
  264. DigMoveUp()
  265.  
  266. end
  267.  
  268. function goDown()
  269. DigMoveDown()
  270.  
  271. end
  272.  
  273.  
  274. local function readLines(sPath)
  275. local file = fs.open(sPath, "r") -- open the file
  276. if file then -- check if it's open
  277. local tLines = {} -- table to store the lines
  278. local sLine = file.readLine() -- read a line from the file
  279. while sLine do -- while there's a line in the file
  280. table.insert(tLines, sLine) -- add it to the table
  281. sLine = file.readLine() -- get the next line
  282. end
  283. file.close() -- close the file
  284. return tLines -- return the table with the lines
  285. end
  286. return nil -- there was an error opening the file, return nil to let the user know
  287. end
  288.  
  289. local function writeLines(sPath, tLines)
  290. local file = fs.open(sPath, "w") -- open the file
  291. if file then -- check if the file is open
  292. for _, sLine in ipairs(tLines) do -- for each line in the table
  293. file.writeLine(sLine) -- write the line
  294. end
  295. file.close() -- close the file
  296. end
  297. end
  298.  
  299. function filereadline(filename, line)
  300. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  301. if not tLines then -- if there was an error
  302. return nil -- return nil/error
  303. end
  304. return tLines[line] -- return the line
  305. end
  306.  
  307. function filewriteline(filename, line, text)
  308. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  309. if tLines then -- if there was no error
  310. tLines[line] = text -- set the line
  311. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  312. end
  313. end
  314.  
  315. function goto(tx, ty, tz)
  316.  
  317. local dx = tx - x
  318. local dy = ty - y
  319. local dz = tz - z
  320.  
  321. -- if dz ~= 0 then
  322. while z ~= tz do
  323. print(z .. " - " .. tz)
  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. print(x .. " - " .. tx)
  334. if x > tx then
  335. goWest()
  336. else
  337. goEast()
  338. end
  339. end
  340. -- end
  341. -- if dy ~= 0 then
  342. while y ~= ty do
  343. print(y .. " - " .. ty)
  344. if y > ty then
  345. goSouth()
  346. else
  347. goNorth()
  348. end
  349. end
  350. -- end
  351.  
  352. end
  353.  
  354. turtle.refuel()
  355.  
  356. goto (-10, -10, startz - 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement