Advertisement
lemmy101

Untitled

Apr 24th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 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. print("mov.down");
  70. for i=1,times do
  71. if turtle.detectDown() == false then
  72. print("!detectDown");
  73. while not turtle.down() do
  74. sleep(1)
  75. end
  76. v = true
  77. else
  78. print("detectDown");
  79. v = false
  80. return v
  81. end
  82. end
  83. return v
  84. end
  85.  
  86. mov.place = function()
  87. while not turtle.place() do
  88. sleep(1)
  89. end
  90. return true
  91. end
  92.  
  93. rot.right = function()
  94. while not turtle.turnRight() do
  95. sleep(1)
  96. end
  97.  
  98. if RotatePosition == "FORWARD" then
  99. RotatePosition = "RIGHT"
  100. elseif RotatePosition == "LEFT" then
  101. RotatePosition = "FORWARD"
  102. end
  103. filewriteline("BranchMineData", 5, RotatePosition)
  104. return true
  105. end
  106.  
  107. rot.left = function()
  108. while not turtle.turnLeft() do
  109. sleep(1)
  110. end
  111.  
  112. if RotatePosition == "FORWARD" then
  113. RotatePosition = "LEFT"
  114. elseif RotatePosition == "RIGHT" then
  115. RotatePosition = "FORWARD"
  116. end
  117. filewriteline("BranchMineData", 5, RotatePosition)
  118. return true
  119. end
  120.  
  121. dig.forward = function()
  122. if turtle.detect() then
  123. while not turtle.dig() do
  124. sleep(1)
  125. end
  126. return true
  127. else
  128. print("No Block to mine forward")
  129. return false
  130. end
  131. end
  132.  
  133. dig.up = function()
  134. if turtle.detectUp() then
  135. while not turtle.digUp() do
  136. sleep(1)
  137. end
  138. return true
  139. else
  140. print("No Block to mine up")
  141. return false
  142. end
  143. end
  144.  
  145. dig.down = function()
  146. if turtle.detectDown() then
  147. while not turtle.digDown() do
  148. sleep(1)
  149. end
  150. return true
  151. else
  152. print("No Block to mine down")
  153. return false
  154. end
  155. end
  156.  
  157. function DigMoveForward()
  158. if mov.forward(1) == false then
  159. dig.forward()
  160. sleep(0.5)
  161. DigMoveForward()
  162. end
  163. end
  164.  
  165. function DigMoveUp()
  166. if mov.up(1) == false then
  167. dig.up()
  168. sleep(0.5)
  169. DigMoveUp()
  170.  
  171. else
  172. z = z + 1
  173. end
  174.  
  175. end
  176.  
  177. function DigMoveDown()
  178. print("moving down")
  179. if mov.down(1) == false then
  180. print("failed moving down - digging")
  181. dig.down()
  182. sleep(0.5)
  183. print("trying again")
  184. DigMoveDown()
  185. else
  186. print("aucceeeded")
  187. z = z - 1
  188. end
  189. end
  190.  
  191.  
  192. function faceEast()
  193. if(currentDir == north) then
  194. turtle.turnRight()
  195. elseif(currentDir == west) then
  196. turtle.turnRight()
  197. turtle.turnRight()
  198. elseif(currentDir == south) then
  199. turtle.turnLeft()
  200. end
  201. currentDir = east;
  202. end
  203.  
  204. function faceNorth()
  205. if(currentDir == east) then
  206. turtle.turnLeft()
  207. elseif(currentDir == south) then
  208. turtle.turnRight()
  209. turtle.turnRight()
  210. elseif(currentDir == west) then
  211. turtle.turnRight()
  212. end
  213. currentDir = north;
  214. end
  215.  
  216. function faceSouth()
  217. if(currentDir == east) then
  218. turtle.turnRight()
  219. elseif(currentDir == north) then
  220. turtle.turnRight()
  221. turtle.turnRight()
  222. elseif(currentDir == west) then
  223. turtle.turnLeft()
  224. end
  225. currentDir = south;
  226. end
  227.  
  228. function faceWest()
  229. if(currentDir == south) then
  230. turtle.turnRight()
  231. elseif(currentDir == east) then
  232. turtle.turnRight()
  233. turtle.turnRight()
  234. elseif(currentDir == north) then
  235. turtle.turnLeft()
  236. end
  237. currentDir = west;
  238. end
  239.  
  240. function forward()
  241. DigMoveForward()
  242.  
  243. if(currentDir == east) then
  244. x = x + 1
  245. elseif(currentDir == north) then
  246. y = y - 1
  247. elseif(currentDir == south) then
  248. y = y + 1
  249. elseif(currentDir == west) then
  250. x = x - 1
  251. end
  252. end
  253.  
  254. function goWest()
  255. faceWest()
  256. forward()
  257. end
  258.  
  259. function goEast()
  260. faceEast()
  261. forward()
  262. end
  263.  
  264. function goNorth()
  265. faceNorth()
  266. forward()
  267. end
  268.  
  269. function goSouth()
  270. faceSouth()
  271. forward()
  272. end
  273.  
  274. function goUp()
  275. DigMoveUp()
  276.  
  277. end
  278.  
  279. function goDown()
  280. DigMoveDown()
  281.  
  282. end
  283.  
  284.  
  285. local function readLines(sPath)
  286. local file = fs.open(sPath, "r") -- open the file
  287. if file then -- check if it's open
  288. local tLines = {} -- table to store the lines
  289. local sLine = file.readLine() -- read a line from the file
  290. while sLine do -- while there's a line in the file
  291. table.insert(tLines, sLine) -- add it to the table
  292. sLine = file.readLine() -- get the next line
  293. end
  294. file.close() -- close the file
  295. return tLines -- return the table with the lines
  296. end
  297. return nil -- there was an error opening the file, return nil to let the user know
  298. end
  299.  
  300. local function writeLines(sPath, tLines)
  301. local file = fs.open(sPath, "w") -- open the file
  302. if file then -- check if the file is open
  303. for _, sLine in ipairs(tLines) do -- for each line in the table
  304. file.writeLine(sLine) -- write the line
  305. end
  306. file.close() -- close the file
  307. end
  308. end
  309.  
  310. function filereadline(filename, line)
  311. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  312. if not tLines then -- if there was an error
  313. return nil -- return nil/error
  314. end
  315. return tLines[line] -- return the line
  316. end
  317.  
  318. function filewriteline(filename, line, text)
  319. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  320. if tLines then -- if there was no error
  321. tLines[line] = text -- set the line
  322. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  323. end
  324. end
  325.  
  326. function goto(tx, ty, tz)
  327.  
  328. local dx = tx - x
  329. local dy = ty - y
  330. local dz = tz - z
  331.  
  332. -- if dz ~= 0 then
  333. while z ~= tz do
  334. print(z .. " - " .. tz)
  335. if z > tz then
  336. goDown()
  337. else
  338. goUp()
  339. end
  340. end
  341. -- end
  342. -- if dx ~= 0 then
  343. while x ~= tx do
  344. print(x .. " - " .. tx)
  345. if x > tx then
  346. goWest()
  347. else
  348. goEast()
  349. end
  350. end
  351. -- end
  352. -- if dy ~= 0 then
  353. while y ~= ty do
  354. print(y .. " - " .. ty)
  355. if y < ty then
  356. goSouth()
  357. else
  358. goNorth()
  359. end
  360. end
  361. -- end
  362.  
  363. end
  364.  
  365. function refuelStation()
  366. faceSouth()
  367. turtle.select(1)
  368. turtle.suck()
  369. turtle.refuel()
  370. end
  371.  
  372. refuelStation()
  373.  
  374. goto (0, 0, 5)
  375. goto (0, -64, 5)
  376. goto (0, -64, 6)
  377. goto (0, 0, 6)
  378. goto (0, 0, startz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement