Advertisement
lemmy101

Untitled

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