Advertisement
pauljs2

Untitled

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