Advertisement
lemmy101

Untitled

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