Advertisement
bueddl

Untitled

Jun 4th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. MiningTurtle = {
  2. position = {
  3. x = 0,
  4. y = 0,
  5. z = 0
  6. },
  7. direction = 0
  8. -- 0 = SOUTH -> Z++
  9. -- 1 = WEST -> X--
  10. -- 2 = NORTH -> Z--
  11. -- 3 = EAST -> X++
  12. }
  13.  
  14. -- Constructor
  15.  
  16. function MiningTurtle:new()
  17.  
  18. local object = { }
  19.  
  20. setmetatable(object, { __index = MiningTurtle })
  21. return object
  22. end
  23.  
  24. -- Check if the Turtle needs to be refueled ( <= refuelLevel )
  25. -- if yes, try to refuel it taking the contents of the inventory
  26. -- if this is not possible or not enough, go back to base
  27. function MiningTurtle:requiredRefuel( refuelLevel, requiredFuelLevel )
  28. local slot
  29.  
  30. if refuelLevel == nil then
  31. refuelLevel = 25
  32. end
  33.  
  34. if requiredFuelLevel == nil then
  35. requiredFuelLevel = 500
  36. end
  37.  
  38. -- should we refuel?
  39. if turtle.getFuelLevel() <= refuelLevel then
  40.  
  41. -- iterate through our inventory
  42. for slot = 1,16 do
  43.  
  44. -- select slot
  45. turtle.select(slot)
  46.  
  47. -- refuel from active slot...
  48. if turtle.refuel( turtle.getItemCount( slot ) ) then
  49.  
  50. end
  51.  
  52. -- ...and check again
  53. if turtle.getFuelLevel() >= requiredFuelLevel then
  54. return true
  55. end
  56.  
  57. end
  58.  
  59. -- check if we did not refueled enough alreay
  60. if not (turtle.getFuelLevel() >= requiredFuelLevel) then
  61. -- if yes, go back to base
  62.  
  63. MiningTurtle:toBase()
  64. -- refuel: to be implemented!!!!
  65. end
  66.  
  67. return false
  68. end
  69.  
  70. return true
  71. end
  72.  
  73. -- Go back to the base of the MiningTurtle
  74. function MiningTurtle:toBase( )
  75.  
  76. end
  77.  
  78. -- Go to ground
  79. function MiningTurtle:toGround( )
  80.  
  81. while not turtle.detectDown() do
  82. MiningTurtle:down()
  83. end
  84.  
  85. end
  86.  
  87. -- Go the our "base" Ground (z = 0)
  88. function MiningTurtle:toBaseLevel( )
  89.  
  90. while self.position.y > 0 do
  91. MiningTurtle:saveDown()
  92. end
  93.  
  94. end
  95.  
  96. -- forward
  97. function MiningTurtle:forward()
  98. local ret
  99.  
  100. ret = turtle.forward()
  101.  
  102. if ret then
  103.  
  104.  
  105. if self.direction == 0 then
  106. -- 0 = SOUTH -> Z++
  107. self.position.z = self.position.z + 1
  108.  
  109. elseif self.direction == 1 then
  110. -- 1 = WEST -> X--
  111. self.position.x = self.position.x - 1
  112.  
  113. elseif self.direction == 2 then
  114. -- 2 = NORTH -> Z--
  115. self.position.z = self.position.z - 1
  116.  
  117. elseif self.direction == 3 then
  118. -- 3 = EAST -> X++
  119. self.position.x = self.position.x + 1
  120.  
  121. end
  122.  
  123. end
  124.  
  125. MiningTurtle:outPos()
  126.  
  127. return ret
  128. end
  129.  
  130. -- back
  131. function MiningTurtle:back()
  132. local ret
  133.  
  134. ret = turtle.back()
  135.  
  136. if ret then
  137.  
  138. -- all directions reverse!!! (moving back)
  139.  
  140. if self.direction == 0 then
  141. -- 0 = SOUTH -> Z++
  142. self.position.z = self.position.z - 1
  143.  
  144. elseif self.direction == 1 then
  145. -- 1 = WEST -> X--
  146. self.position.x = self.position.x + 1
  147.  
  148. elseif self.direction == 2 then
  149. -- 2 = NORTH -> Z--
  150. self.position.z = self.position.z + 1
  151.  
  152. elseif self.direction == 3 then
  153. -- 3 = EAST -> X++
  154. self.position.x = self.position.x - 1
  155.  
  156. end
  157.  
  158. end
  159.  
  160. MiningTurtle:outPos()
  161.  
  162. return ret
  163. end
  164.  
  165. -- up
  166. function MiningTurtle:up()
  167. local ret
  168.  
  169. ret = turtle.up()
  170.  
  171. if ret then
  172. self.position.y = self.position.y + 1
  173. end
  174.  
  175. MiningTurtle:outPos()
  176.  
  177. return ret
  178. end
  179.  
  180. -- down
  181. function MiningTurtle:down()
  182. local ret
  183.  
  184. ret = turtle.down()
  185.  
  186. if ret then
  187. self.position.y = self.position.y - 1
  188. end
  189.  
  190. MiningTurtle:outPos()
  191.  
  192. return ret
  193. end
  194.  
  195. -- turn Left
  196. function MiningTurtle:turnLeft()
  197. local ret
  198.  
  199. ret = turtle.turnLeft()
  200.  
  201. if ret then
  202. self.direction = self.direction + 1
  203.  
  204. while self.direction >= 4 do
  205. self.direction = self.direction - 4
  206. end
  207. end
  208.  
  209. MiningTurtle:outPos()
  210.  
  211. return ret
  212. end
  213.  
  214. -- turn Right
  215. function MiningTurtle:turnRight()
  216. local ret
  217.  
  218. ret = turtle.turnRight()
  219.  
  220. if ret then
  221. self.direction = self.direction - 1
  222.  
  223. while self.direction < 0 do
  224. self.direction = self.direction - 4
  225. end
  226. end
  227.  
  228. MiningTurtle:outPos()
  229.  
  230. return ret
  231. end
  232.  
  233. -- Save forward
  234. function MiningTurtle:saveForward()
  235.  
  236. while not MiningTurtle:forward() do
  237. turtle.dig()
  238. end
  239. end
  240.  
  241. -- Save up
  242. function MiningTurtle:saveUp()
  243.  
  244. while not MiningTurtle:up() do
  245. turtle.digUp()
  246. end
  247. end
  248.  
  249. -- Save down
  250. function MiningTurtle:saveDown()
  251.  
  252. while not MiningTurtle:down() do
  253. turtle.digDown()
  254. end
  255. end
  256.  
  257. -- 180 degree turn
  258. function MiningTurtle:turn()
  259.  
  260. MiningTurtle:turnRight()
  261. MiningTurtle:turnRight()
  262. end
  263.  
  264. -- output pos data
  265. function MiningTurtle:outPos()
  266.  
  267. print("Pos: " .. tostring(self.position.x) .. "|" .. tostring(self.position.z) .. "|" .. tostring(self.position.y) .. " o: " .. tostring(self.direction) .. " f: " .. tostring(turtle.getFuelLevel()) )
  268.  
  269. end
  270.  
  271. -- Turn to direction
  272. function MiningTurtle:turnTo( newOrientation )
  273. while self.direction ~= newOrientation do
  274. if math.abs(self.direction - newOrientation) < 180 then
  275. MiningTurtle:turnRight()
  276. else
  277. MiningTurtle:turnLeft()
  278. end
  279. end
  280. end
  281.  
  282. -- Go to
  283. function MiningTurtle:goTo( newPosition )
  284. print("GoTo: " .. newPosition.x .. "|" .. newPosition.z .. "|" .. newPosition.y)
  285.  
  286. -- 0 = SOUTH -> Z++
  287. -- 1 = WEST -> X--
  288. -- 2 = NORTH -> Z--
  289. -- 3 = EAST -> X++
  290.  
  291. while not ( (newPosition.x - self.position.x) == 0 and (newPosition.z - self.position.z) == 0 and (newPosition.y - self.position.y) == 0 ) do
  292.  
  293. if (newPosition.x - self.position.x) < 0 then
  294. MiningTurtle:turnTo(1)
  295. MiningTurtle:saveForward()
  296. elseif (newPosition.x - self.position.x) > 0 then
  297. MiningTurtle:turnTo(3)
  298. MiningTurtle:saveForward()
  299. end
  300.  
  301. if (newPosition.z - self.position.z) < 0 then
  302. MiningTurtle:turnTo(2)
  303. MiningTurtle:saveForward()
  304. elseif (newPosition.z - self.position.z) > 0 then
  305. MiningTurtle:turnTo(0)
  306. MiningTurtle:saveForward()
  307. end
  308.  
  309. if (newPosition.y - self.position.y) < 0 then
  310. MiningTurtle:saveDown()
  311. elseif (newPosition.y - self.position.y) > 0 then
  312. MiningTurtle:saveUp()
  313. end
  314.  
  315. end
  316.  
  317. end
  318.  
  319. -- Make a tunnel
  320. function MiningTurtle:tunnel( length, height, width, ret )
  321.  
  322. -- default values for parameters, only length is required (!)
  323. if height == nil then
  324. height = 2
  325. end
  326.  
  327. if width == nil then
  328. width = 1
  329. end
  330.  
  331. if ret == nil then
  332. ret = false
  333. end
  334.  
  335. -- Save our current positon back for later return
  336.  
  337. returnPos = {}
  338. returnPos.x = self.position.x
  339. returnPos.z = self.position.z
  340. returnPos.y = self.position.y
  341. returnDirection = self.direction
  342.  
  343. -- Place the turtle at the lower left corner of the new tunnel (so that we were starting from the middle of the new tunnel)
  344. -- We only need this, if we have a tonnel with a width > 2 (<=2 means we are already on the right position)
  345.  
  346. if width > 2 then
  347.  
  348. MiningTurtle:turnLeft()
  349.  
  350. for i = 0,math.floor( width / 2 ) do
  351. MiningTurtle:saveForward()
  352. end
  353.  
  354. MiningTurtle:turnRight()
  355. end
  356.  
  357. -- Length loop
  358.  
  359. for i = 0,length-1 do
  360.  
  361. MiningTurtle:saveForward()
  362.  
  363. if i % 2 == 0 then
  364. -- even
  365.  
  366. -- only change direction if we mine a tunnel wider than 1 block
  367. if width > 1 then
  368. MiningTurtle:turnRight()
  369. end
  370.  
  371. for x = 0,width-1 do
  372.  
  373. MiningTurtle:tick()
  374.  
  375. for y = 0,height-3 do
  376.  
  377. if x % 2 == 0 then
  378. MiningTurtle:saveUp()
  379. else
  380. MiningTurtle:saveDown()
  381. end
  382.  
  383. end
  384.  
  385. if height > 1 then
  386. turtle.digUp()
  387. end
  388.  
  389. if x < width-1 then
  390. MiningTurtle:saveForward()
  391. end
  392. end
  393.  
  394. -- same as above
  395. if width > 1 then
  396. MiningTurtle:turnLeft()
  397. end
  398.  
  399. else
  400. -- odd
  401.  
  402. if width > 1 then
  403. MiningTurtle:turnLeft()
  404. end
  405.  
  406. for x = 0,width-1 do
  407.  
  408. MiningTurtle:tick()
  409.  
  410. for y = 0,height-3 do
  411.  
  412. if x % 2 == 1 or width % 2 == 0 then
  413. MiningTurtle:saveUp()
  414. else
  415. MiningTurtle:saveDown()
  416. end
  417.  
  418. end
  419.  
  420. if height > 1 then
  421. turtle.digUp()
  422. end
  423.  
  424. if x < width-1 then
  425. MiningTurtle:saveForward()
  426. end
  427. end
  428.  
  429. if width > 1 then
  430. MiningTurtle:turnRight()
  431. end
  432. end
  433.  
  434. end
  435.  
  436. if ret then
  437.  
  438. -- Go up to not remove torches on the floow
  439. MiningTurtle:saveUp()
  440.  
  441. MiningTurtle:goTo( returnPos )
  442.  
  443. MiningTurtle:turnTo( returnDirection )
  444.  
  445. end
  446.  
  447. end
  448.  
  449. function MiningTurtle:tick()
  450.  
  451. -- refuel loop
  452. while not MiningTurtle:requiredRefuel( 20, 50 ) do
  453. sleep(1)
  454. end
  455. end
  456.  
  457. --
  458. function MiningTurtle:matrix( rows, cols )
  459.  
  460. MiningTurtle:tick()
  461.  
  462. --MiningTurtle:toBaseLevel()
  463.  
  464.  
  465. MiningTurtle:tunnel(5, 2, 1, true)
  466.  
  467.  
  468. end
  469.  
  470.  
  471. -- main program
  472.  
  473. baby = MiningTurtle:new()
  474.  
  475. baby:matrix( 10, 10 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement