Advertisement
lemmy101

Untitled

Apr 24th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 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.  
  168. else
  169. z = z + 1
  170. end
  171.  
  172. end
  173.  
  174. function DigMoveDown()
  175. print("moving down")
  176. if mov.down(1) == false then
  177. print("failed moving down - digging")
  178. dig.down()
  179. --sleep(0.5)
  180. print("trying again")
  181. DigMoveDown()
  182. else
  183. print("aucceeeded")
  184. z = z - 1
  185. end
  186. end
  187.  
  188.  
  189. function faceEast()
  190. if(currentDir == north) then
  191. turtle.turnRight()
  192. elseif(currentDir == west) then
  193. turtle.turnRight()
  194. turtle.turnRight()
  195. elseif(currentDir == south) then
  196. turtle.turnLeft()
  197. end
  198. currentDir = east;
  199. end
  200.  
  201. function faceNorth()
  202. if(currentDir == east) then
  203. turtle.turnLeft()
  204. elseif(currentDir == south) then
  205. turtle.turnRight()
  206. turtle.turnRight()
  207. elseif(currentDir == west) then
  208. turtle.turnRight()
  209. end
  210. currentDir = north;
  211. end
  212.  
  213. function faceSouth()
  214. if(currentDir == east) then
  215. turtle.turnRight()
  216. elseif(currentDir == north) then
  217. turtle.turnRight()
  218. turtle.turnRight()
  219. elseif(currentDir == west) then
  220. turtle.turnLeft()
  221. end
  222. currentDir = south;
  223. end
  224.  
  225. function faceWest()
  226. if(currentDir == south) then
  227. turtle.turnRight()
  228. elseif(currentDir == east) then
  229. turtle.turnRight()
  230. turtle.turnRight()
  231. elseif(currentDir == north) then
  232. turtle.turnLeft()
  233. end
  234. currentDir = west;
  235. end
  236.  
  237. function forward()
  238. DigiMoveForward()
  239.  
  240. if(currentDir == east) then
  241. x = x + 1
  242. elseif(currentDir == north) then
  243. y = y + 1
  244. elseif(currentDir == south) then
  245. y = y - 1
  246. elseif(currentDir == west) then
  247. x = x - 1
  248. end
  249. end
  250.  
  251. function goWest()
  252. faceWest()
  253. forward()
  254. end
  255.  
  256. function goEast()
  257. faceWest()
  258. forward()
  259. end
  260.  
  261. function goNorth()
  262. faceNorth()
  263. forward()
  264. end
  265.  
  266. function goSouth()
  267. faceWest()
  268. forward()
  269. end
  270.  
  271. function goUp()
  272. DigMoveUp()
  273.  
  274. end
  275.  
  276. function goDown()
  277. DigMoveDown()
  278.  
  279. end
  280.  
  281.  
  282. local function readLines(sPath)
  283. local file = fs.open(sPath, "r") -- open the file
  284. if file then -- check if it's open
  285. local tLines = {} -- table to store the lines
  286. local sLine = file.readLine() -- read a line from the file
  287. while sLine do -- while there's a line in the file
  288. table.insert(tLines, sLine) -- add it to the table
  289. sLine = file.readLine() -- get the next line
  290. end
  291. file.close() -- close the file
  292. return tLines -- return the table with the lines
  293. end
  294. return nil -- there was an error opening the file, return nil to let the user know
  295. end
  296.  
  297. local function writeLines(sPath, tLines)
  298. local file = fs.open(sPath, "w") -- open the file
  299. if file then -- check if the file is open
  300. for _, sLine in ipairs(tLines) do -- for each line in the table
  301. file.writeLine(sLine) -- write the line
  302. end
  303. file.close() -- close the file
  304. end
  305. end
  306.  
  307. function filereadline(filename, line)
  308. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  309. if not tLines then -- if there was an error
  310. return nil -- return nil/error
  311. end
  312. return tLines[line] -- return the line
  313. end
  314.  
  315. function filewriteline(filename, line, text)
  316. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  317. if tLines then -- if there was no error
  318. tLines[line] = text -- set the line
  319. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  320. end
  321. end
  322.  
  323. function goto(tx, ty, tz)
  324.  
  325. local dx = tx - x
  326. local dy = ty - y
  327. local dz = tz - z
  328.  
  329. -- if dz ~= 0 then
  330. while z ~= tz do
  331. print(z .. " - " .. tz)
  332. if z > tz then
  333. goDown()
  334. else
  335. goUp()
  336. end
  337. end
  338. -- end
  339. -- if dx ~= 0 then
  340. while x ~= tx do
  341. print(x .. " - " .. tx)
  342. if x > tx then
  343. goWest()
  344. else
  345. goEast()
  346. end
  347. end
  348. -- end
  349. -- if dy ~= 0 then
  350. while y ~= ty do
  351. print(y .. " - " .. ty)
  352. if y > ty then
  353. goSouth()
  354. else
  355. goNorth()
  356. end
  357. end
  358. -- end
  359.  
  360. end
  361.  
  362. turtle.refuel()
  363.  
  364. goto (-10, -10, startz - 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement