Advertisement
pauljs2

Untitled

Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. targ = {
  2. nextColumnGenerator = function ()
  3. local curpos = {x=0, y=0, z=0, facing=3}
  4. local deph = 0
  5. while true do
  6. for i=0,deph do
  7. if deph%2 == 0 then
  8. curpos.z = curpos.z - 3
  9. curpos.x = curpos.x + 1
  10. else
  11. curpos.z = curpos.z + 3
  12. curpos.x = curpos.x - 1
  13. end
  14. coroutine.yield(curpos)
  15. end
  16.  
  17. if deph%2 == 0 then
  18. curpos.z = curpos.z - 1
  19. curpos.x = curpos.x + 2
  20. else
  21. curpos.z = curpos.z + 2
  22. curpos.x = curpos.x + 1
  23. end
  24. deph = deph + 1
  25. coroutine.yield(curpos)
  26. end
  27. end
  28. }
  29.  
  30. location = {
  31. lockd = false,
  32.  
  33. x = 0,
  34. y = 0,
  35. z = 0,
  36. facing = 3,
  37.  
  38. facingToAxis = {
  39. [0] = { axis = 'z', unit = 1 },
  40. [1] = { axis = 'x', unit = -1 },
  41. [2] = { axis = 'z', unit = -1 },
  42. [3] = { axis = 'x', unit = 1 }
  43. },
  44.  
  45. lock = function()
  46. while location.lockd do
  47. sleep(1)
  48. end
  49. end,
  50.  
  51. getAxisForFacing = function()
  52. return location.facingToAxis[location.facing]['axis'], location.facingToAxis[location.facing]['unit']
  53. end,
  54.  
  55. turnLeft = function()
  56. location.lock()
  57. location.facing = (location.facing - 1) % 4
  58. turtle.turnLeft()
  59. end,
  60.  
  61. turnRight = function()
  62. location.lock()
  63. location.facing = (location.facing + 1) % 4
  64. turtle.turnRight()
  65. end,
  66.  
  67. moveForward = function()
  68. location.lock()
  69. if turtle.forward() then
  70. local axis, unit = location.getAxisForFacing()
  71. location[axis] = location[axis] + unit
  72. return true
  73. end
  74. return false
  75. end,
  76.  
  77. moveBack = function()
  78. location.lock()
  79. if turtle.back() then
  80. local axis, unit = location.getAxisForFacing()
  81. location[axis] = location[axis] - unit
  82. return true
  83. end
  84. return false
  85. end,
  86.  
  87. face = function(faceto)
  88. local dirdif = (faceto - location.facing) % 4
  89. if dirdif == 1 then
  90. location.turnRight()
  91. elseif dirdif == 2 then
  92. location.turnRight()
  93. location.turnRight()
  94. elseif dirdif == 3 then
  95. location.turnLeft()
  96. end
  97. end,
  98.  
  99. movez = function(d)
  100. if d < location.z then location.face(2) elseif d > location.z then location.face(0) end
  101. return location.moveForward()
  102. end,
  103.  
  104. movex = function(d)
  105. if d < location.x then location.face(1) elseif d > location.x then location.face(3) end
  106. return location.moveForward()
  107. end,
  108.  
  109. movey = function(d)
  110. if d < location.y then
  111. return location.moveDown()
  112. end
  113. return location.moveUp()
  114. end,
  115.  
  116. moveUp = function()
  117. location.lock()
  118. if turtle.up() then
  119. location.y = location.y + 1
  120. return true
  121. end
  122. return false
  123. end,
  124.  
  125. moveDown = function()
  126. location.lock()
  127. if turtle.down() then
  128. location.y = location.y - 1
  129. return true
  130. end
  131. return false
  132. end,
  133.  
  134. moveTo = function(x, y, z, facing)
  135. if y > location.y then
  136. while location.y < y do
  137. while not location.moveUp() do
  138. if turtle.detectUp() then
  139. turtle.select(1)
  140. turtle.digUp()
  141. else
  142. turtle.attackUp()
  143. end
  144. end
  145. end
  146. end
  147. while x ~= location.x do
  148. while not location.movex(x) do
  149. if turtle.detect() then
  150. turtle.select(1)
  151. turtle.dig()
  152. else
  153. turtle.attack()
  154. end
  155. end
  156. end
  157. while z ~= location.z do
  158. while not location.movez(z) do
  159. if turtle.detect() then
  160. turtle.select(1)
  161. turtle.dig()
  162. else
  163. turtle.attack()
  164. end
  165. end
  166. end
  167. if y < location.y then
  168. while location.y > y do
  169. while not location.moveDown() do
  170. if turtle.detectDown() then
  171. turtle.select(1)
  172. turtle.digDown()
  173. else
  174. turtle.attackDown()
  175. end
  176. end
  177. end
  178. end
  179. location.face(facing)
  180. return true
  181. end,
  182.  
  183. moveToPos = function(pos)
  184. return location.moveTo(pos.x, pos.y, pos.z, pos.facing)
  185. end,
  186.  
  187. distanceTo = function(x, y, z)
  188. return math.abs(x) - location.x + math.abs(y) - location.y + math.abs(z) - location.z
  189. end,
  190.  
  191. distanceToPos = function(pos)
  192. return location.distanceTo(pos.x, pos.y, pos.z)
  193. end
  194. }
  195.  
  196. mining = {
  197. -- returns false if bedrock
  198. down = function (h)
  199. while not location.movey(-1e10) do
  200. if turtle.detectDown() then
  201. turtle.digDown()
  202. if turtle.detectDown() then
  203. return false
  204. end
  205. else
  206. turtle.attackDown()
  207. end
  208. end
  209. return true
  210. end,
  211.  
  212. checkForward = function()
  213. location.lock()
  214. local success, data = turtle.inspect()
  215. return success and not (data.name == "minecraft:stone" or data.name == "minecraft:dirt" or data.name == "minecraft:gravel")
  216. end,
  217.  
  218. digHole = function ()
  219. while true do
  220. if not mining.down(0) then
  221. break
  222. end
  223. for j=1,4 do
  224. if mining.checkForward() then
  225. if not interface.space() then
  226. controls.returnToChest()
  227. end
  228. turtle.select(1)
  229. turtle.dig()
  230. end
  231. location.turnRight()
  232. end
  233. end
  234. end
  235. }
  236.  
  237. interface = {
  238. refuel = function (ammount)
  239. if turtle.getFuelLevel() == "unlimited" then
  240. return true
  241. end
  242. local needed = ammount or location.distanceToPos({x=0, y=0, z=0, facing=1}) * 2 + 1
  243. if turtle.getFuelLevel() < needed then
  244. term.write("need more fuel. refueling")
  245. for n=1,16 do
  246. location.lock()
  247. if turtle.getItemCount(n) > 0 then
  248. turtle.select(n)
  249. if turtle.refuel(1) then
  250. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  251. turtle.refuel(1)
  252. end
  253. if turtle.getFuelLevel() >= needed then
  254. turtle.select(1)
  255. return true
  256. end
  257. end
  258. end
  259. end
  260. turtle.select(1)
  261. return false
  262. end
  263. return true
  264. end,
  265.  
  266. unload = function ()
  267. for n=1,16 do
  268. location.lock()
  269. if turtle.getItemCount(n) > 0 then
  270. turtle.select(n)
  271. if n <= 0 then
  272. if (not false and n == 1) or n > 1 then
  273. turtle.drop(turtle.getItemCount(n) - 1)
  274. end
  275. else
  276. turtle.drop()
  277. end
  278. end
  279. end
  280. turtle.select(1)
  281. end,
  282.  
  283. space = function ()
  284. for n=1,16 do
  285. if turtle.getItemCount(n) == 0 then
  286. return true
  287. end
  288. end
  289. return false
  290. end
  291. }
  292.  
  293. controls = {
  294. returnToChest = function()
  295. local returnPos = {x = location.x, y = location.y, z = location.z, facing = location.facing}
  296. local fuelNeeded = location.distanceToPos({x=0,y=0,z=0,facing=1}) * 2 + 1
  297. location.moveToPos({x=0,y=0,z=0,facing=1})
  298. location.face((location.facing + 2) % 4)
  299. if not interface.refuel(fuelNeeded) then
  300. interface.unload()
  301. while not interface.refuel(fuelNeeded) do
  302. location.lock()
  303. sleep(1)
  304. end
  305. else
  306. interface.unload()
  307. end
  308. location.moveToPos(returnPos)
  309. end,
  310.  
  311. forward = function ()
  312. if not space() then
  313. controls.returnToChest()
  314. end
  315. while not location.moveForward() do
  316. if turtle.detect() then
  317. turtle.select(1)
  318. turtle.dig()
  319. else
  320. turtle.attack()
  321. end
  322. end
  323. end
  324. }
  325.  
  326.  
  327. function oreThread()
  328. local target = coroutine.create(targ.nextColumnGenerator)
  329. while true do
  330. if not interface.refuel() or not interface.space() then
  331. controls.returnToChest()
  332. end
  333. local error, shaft = coroutine.resume(target)
  334. term.write("mining shaft at x=" .. shaft.x .. " and z=" .. shaft.z)
  335. location.moveToPos(shaft)
  336. mining.digHole()
  337. end
  338. end
  339.  
  340. oreThread()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement