Advertisement
HarvDad

room

Mar 18th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.66 KB | None | 0 0
  1. -- room
  2. -- Creates a room with solid walls
  3. -- Clears the interior
  4. -- Turtle returns to its starting point when mission is completed or fuel runs low
  5. -- Written by HarvDad, March 2014
  6.  
  7. args = {...}
  8. nArgs = #args
  9. version = "room: Rev 3.2"
  10. usage = "Usage: room <length> <width> <height>"
  11.  
  12. x = 0
  13. y = 0
  14. z = 0
  15. face = 0
  16. minimumFuel = 100
  17. missionMessage = "Mission complete."
  18. abort = false
  19. local currentFuelLevel = turtle.getFuelLevel()
  20. patchSlot = 1
  21. chestSlot = 15
  22. torchSlot = 16
  23. garbageSlot = 5
  24. torchSpacing = 2
  25. torchSpot = 5
  26. nextTurn = "left"
  27. addTorches = false
  28.  
  29. -- Status data for marking a spot to return to after going 'home'
  30.  
  31. markX = 0
  32. markY = 0
  33. markZ = 0
  34. markFace = 0
  35.  
  36. -- The following 'face' directions are relative to the starting position of the turtle in this program
  37. north = 0
  38. west = 1
  39. south = 2
  40. east = 3
  41.  
  42. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  43. print(version)
  44. print("Creates a room with solid walls")
  45. print("Clears the interior")
  46. print("Turtle should be pre-fueled")
  47. print("Slot 1 is for patch material")
  48. print("Slot 5 is for garbage material")
  49. print("Slot 15 is for storage chests")
  50. print("Slot 16 is for torches")
  51. print(usage)
  52. return
  53. end
  54.  
  55. if nArgs ~= 3 then
  56. print(usage)
  57. return
  58. end
  59.  
  60. length = tonumber(args[1])
  61. if length == nil then
  62. print("\"", args[1], "\" is not a valid length")
  63. return
  64. end
  65. if length < 1 then
  66. print("length must be a positive number greater than zero")
  67. end
  68.  
  69. width = tonumber(args[2])
  70. if width == nil then
  71. print("\"", args[2], "\" is not a valid width")
  72. return
  73. end
  74. if width < 1 then
  75. print("width must be a positive number greater than zero")
  76. end
  77.  
  78. height = tonumber(args[3])
  79. if height == nil then
  80. print("\"", args[3], "\" is not a valid height")
  81. return
  82. end
  83. if height < 1 then
  84. print("height must be a positive number greater than zero")
  85. end
  86.  
  87. targetArea = length * width
  88. areaCovered = 1;
  89.  
  90. function patch()
  91. if turtle.getItemCount(patchSlot) < 2 then
  92. gatherPatchMaterial()
  93. if turtle.getItemCount(patchSlot) < 2 then
  94. abort = true
  95. areaCovered = targetArea
  96. missionMessage = "Aborted: No more wall patch"
  97. return
  98. end
  99. end
  100. turtle.select(patchSlot)
  101. turtle.place()
  102. end
  103.  
  104. function patchUp()
  105. if turtle.getItemCount(patchSlot) < 2 then
  106. gatherPatchMaterial()
  107. if turtle.getItemCount(patchSlot) < 2 then
  108. abort = true
  109. areaCovered = targetArea
  110. missionMessage = "Aborted: No more patch material"
  111. return
  112. end
  113. end
  114. turtle.select(patchSlot)
  115. turtle.placeUp()
  116. gatherPatchMaterial();
  117. end
  118.  
  119. function patchDown()
  120. if turtle.getItemCount(patchSlot) < 2 then
  121. gatherPatchMaterial()
  122. if turtle.getItemCount(patchSlot) < 2 then
  123. abort = true
  124. areaCovered = targetArea
  125. missionMessage = "Aborted: No more patch material"
  126. return
  127. end
  128. end
  129. turtle.select(patchSlot)
  130. turtle.placeUp()
  131. gatherPatchMaterial();
  132. end
  133.  
  134. function patchDown()
  135. turtle.select(patchSlot)
  136. turtle.placeDown()
  137. gatherPatchMaterial();
  138. end
  139.  
  140. function gatherPatchMaterial()
  141. local i
  142. patchCount = turtle.getItemCount(patchSlot)
  143. garbageCount = turtle.getItemCount(garbageSlot)
  144.  
  145. if patchCount < 3 or garbageCount < 20 then
  146. -- print("Attempting to refill slot ", patchSlot)
  147. for i=2,14 do
  148. turtle.select(i)
  149. if turtle.compareTo(patchSlot) then
  150. turtle.transferTo(patchSlot, 64-patchCount)
  151. -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
  152. end
  153. if turtle.compareTo(garbageSlot) then
  154. turtle.transferTo(garbageSlot)
  155. end
  156. end
  157. end
  158. turtle.select(patchSlot)
  159. end
  160.  
  161. function left()
  162. if face == 0 then face = 1 turtle.turnLeft() return end
  163. if face == 1 then face = 2 turtle.turnLeft() return end
  164. if face == 2 then face = 3 turtle.turnLeft() return end
  165. if face == 3 then face = 0 turtle.turnLeft() return end
  166. print("function left\(\): Bad face value: ", face)
  167. end
  168.  
  169. function right()
  170. if face == 0 then face = 3 turtle.turnRight() return end
  171. if face == 1 then face = 0 turtle.turnRight() return end
  172. if face == 2 then face = 1 turtle.turnRight() return end
  173. if face == 3 then face = 2 turtle.turnRight() return end
  174. print("function right\(\): Bad face value: ", face)
  175. end
  176.  
  177. function up()
  178. for i=1,10 do
  179. if not turtle.up() then
  180. if turtle.detectUp() then
  181. digUp()
  182. else
  183. turtle.attackUp()
  184. sleep(2)
  185. end
  186. else
  187. break
  188. end
  189. end
  190. y = y+1
  191. end
  192.  
  193. function down()
  194. for i=1,10 do
  195. if not turtle.down() then
  196. if turtle.detectDown() then
  197. digDown()
  198. else
  199. turtle.attackDown()
  200. sleep(2)
  201. end
  202. else
  203. break
  204. end
  205. end
  206. y = y-1
  207. end
  208.  
  209. function dig()
  210. if noMoreEmptySlots() then
  211. jettisonGarbage()
  212. goEmptyInventoryToChestAndReturn()
  213. end
  214. turtle.dig()
  215. end
  216.  
  217. function digUp()
  218. if noMoreEmptySlots() then
  219. -- jettisonGarbage()
  220. goEmptyInventoryToChestAndReturn()
  221. end
  222. turtle.digUp()
  223. end
  224.  
  225. function digDown()
  226. if noMoreEmptySlots() then
  227. -- jettisonGarbage()
  228. goEmptyInventoryToChestAndReturn()
  229. end
  230. turtle.digDown()
  231. end
  232.  
  233. function noMoreEmptySlots()
  234. return (turtle.getItemCount(14) > 0)
  235. end
  236.  
  237.  
  238. function setFace(f)
  239. if f == 0 then
  240. if face == 0 then return end
  241. if face == 1 then right() return end
  242. if face == 2 then right() right() return end
  243. if face == 3 then left() return end
  244. end
  245.  
  246. if f == 1 then
  247. if face == 0 then left() return end
  248. if face == 1 then return end
  249. if face == 2 then right() return end
  250. if face == 3 then right() right() return end
  251. end
  252.  
  253. if f == 2 then
  254. if face == 0 then left() left() return end
  255. if face == 1 then left() return end
  256. if face == 2 then return end
  257. if face == 3 then right() return end
  258. end
  259.  
  260. if f == 3 then
  261. if face == 0 then right() return end
  262. if face == 1 then left() left() return end
  263. if face == 2 then left() return end
  264. if face == 3 then return end
  265. end
  266. end
  267.  
  268. -- digAboveForward: This function requires that the current level is already cleared
  269.  
  270. function DigAboveForward()
  271. while turtle.detect() do -- This loop added in case of falling sand or whatever
  272. turtle.dig()
  273. end
  274. end
  275.  
  276. function forward(keepTrackOfArea)
  277. if keepTrackOfArea == nil then
  278. keepTrackOfArea = true
  279. end
  280. turtle.select(chestSlot)
  281. if not turtle.compare() then
  282. turtle.select(garbageSlot)
  283. turtle.place() -- in case we're facing an undectable water/lava source block, replace
  284. end
  285. turtle.select(patchSlot)
  286. while turtle.detect() do -- This loop added in case of falling sand or whatever
  287. dig()
  288. end
  289. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  290. if not turtle.forward() then
  291. if turtle.detect() then
  292. dig()
  293. else
  294. turtle.attack()
  295. sleep(2)
  296. end
  297. else
  298. break
  299. end
  300. end
  301. if keepTrackOfArea then
  302. areaCovered = areaCovered + 1
  303. end
  304. torchCheck()
  305.  
  306. if face == 0 then z = z+1 return end
  307. if face == 1 then x = x-1 return end
  308. if face == 2 then z = z-1 return end
  309. if face == 3 then x = x+1 return end
  310. end
  311.  
  312. function sealRow(distance)
  313. local count = 0
  314.  
  315. turtle.select(patchSlot)
  316. for count=1,distance do
  317. if y == height-1 then
  318. patchUp()
  319. if height > 1 then
  320. turtle.digDown()
  321. end
  322. -- turtle.placeUp() -- experiment: if solid block is there it will just fail. Maybe help with lava/water?
  323. end
  324. if y == 0 then
  325. patchDown()
  326. if height > 1 then
  327. while turtle.detectUp() do
  328. turtle.digUp()
  329. end
  330. end
  331. end
  332. right()
  333. patch()
  334. left()
  335. if count < distance then
  336. forward()
  337. end
  338. end
  339. end
  340.  
  341. function torchCheck()
  342. if addTorches then
  343. if y == 1 then
  344. if (x % torchSpacing) == 0 then
  345. if (z % torchSpacing) == 0 then
  346. turtle.select(torchSlot)
  347. turtle.placeDown()
  348. end
  349. end
  350. end
  351. end
  352. turtle.select(patchSlot)
  353. end
  354.  
  355. function clearLevel()
  356. areaCovered = 0
  357.  
  358. setFace(west)
  359. forward()
  360. setFace(north)
  361. forward()
  362. if y == height-1 then
  363. turtle.select(patchSlot)
  364. turtle.placeUp()
  365. end
  366. nextTurn = "left"
  367. areaCovered = 0
  368.  
  369. while areaCovered < targetArea do
  370. if abort then
  371. break
  372. end
  373. for w=1,width-2 do
  374. if abort then
  375. -- print("clearLevel: ABORT ABORT ABORT")
  376. break
  377. end
  378. for l=1,length-2 do
  379. if abort then
  380. break
  381. end
  382. if l < length-2 then
  383. forward()
  384. if (y == 0) then
  385. turtle.placeDown()
  386. if height > 1 then
  387. while turtle.detectUp() do
  388. turtle.digUp()
  389. end
  390. end
  391. end
  392. if y == height-1 then
  393. turtle.select(patchSlot) -- experiment: if solid block is there, it will just fail. Who cares?
  394. if turtle.getItemCount(patchSlot) < 1 then
  395. print("clearLevel: Ran out of patch material!")
  396. end
  397. turtle.placeUp()
  398. if height > 1 then
  399. turtle.digDown()
  400. end
  401. end
  402. end
  403. end
  404.  
  405. if w == width-2 then
  406. -- print("There's no more to do at this level. Bailing out.")
  407. levelsCompleted = levelsCompleted + 1
  408. return
  409. end
  410. if w <= width-3 then
  411. if nextTurn == "left" then
  412. left()
  413. forward()
  414.  
  415. if y == 0 then
  416. if height > 1 then
  417. while turtle.detectUp() do
  418. turtle.digUp()
  419. end
  420. end
  421. turtle.placeDown()
  422. end
  423.  
  424. if y == height-1 then
  425. turtle.select(patchSlot)
  426. turtle.placeUp()
  427. if height > 1 then
  428. turtle.digDown()
  429. end
  430. end
  431.  
  432. left()
  433. nextTurn = "right"
  434. else
  435. right()
  436. forward()
  437.  
  438. if y == 0 then
  439. if height > 1 then
  440. while turtle.detectUp() do
  441. turtle.digUp()
  442. end
  443. end
  444. turtle.placeDown()
  445. end
  446.  
  447. if y == height-1 then
  448. turtle.select(patchSlot)
  449. turtle.placeUp()
  450. if height > 1 then
  451. turtle.digDown()
  452. end
  453. end
  454.  
  455. right()
  456. nextTurn = "left"
  457. end
  458. else
  459. break
  460. end
  461. end
  462. end
  463. levelsCompleted = levelsCompleted + 1
  464. end
  465.  
  466. function bounce()
  467. local saveY = y
  468. for i=1,255 do
  469. if y > 0 then
  470. turtle.down()
  471. y = y - 1
  472. else
  473. break
  474. end
  475. end
  476. dump()
  477. while y<saveY do
  478. turtle.up()
  479. y = y + 1
  480. end
  481. end
  482.  
  483. function sealLevel()
  484. sealRow(length)
  485. left()
  486. sealRow(width)
  487. left()
  488. sealRow(length)
  489. left()
  490. sealRow(width)
  491. left()
  492. end
  493.  
  494. function sealWalls()
  495. local i = 0
  496. for i=1,height do
  497. sealLevel()
  498. if y ~= 1 then
  499. clearLevel()
  500. end
  501. if i < height then
  502. up()
  503. home()
  504. end
  505. end
  506. end
  507.  
  508. function sealCeiling()
  509. areaCovered = 0
  510.  
  511. while areaCovered < targetArea do
  512. if abort then
  513. break
  514. end
  515. for w=1,width do
  516. if abort then
  517. break
  518. end
  519. for z=1,length do
  520. if abort then
  521. break
  522. end
  523. if not turtle.detectUp() then
  524. patchUp()
  525. end
  526. if z < length then
  527. forward()
  528. end
  529. end
  530. if w < width then
  531. if nextTurn == "left" then
  532. left()
  533. forward()
  534. left()
  535. nextTurn = "right"
  536. else
  537. right()
  538. forward()
  539. right()
  540. nextTurn = "left"
  541. end
  542. else
  543. return
  544. end
  545. end
  546. end
  547. end
  548.  
  549. function sealFloor()
  550. areaCovered = 0
  551.  
  552. while areaCovered < targetArea do
  553. if abort then
  554. break
  555. end
  556. for w=1,width do
  557. if abort then
  558. break
  559. end
  560. for z=1,length do
  561. if abort then
  562. break
  563. end
  564. if not turtle.detectDown() then
  565. patchDown()
  566. end
  567. while turtle.detectUp() do
  568. turtle.digUp()
  569. end
  570. if z < length then
  571. forward()
  572. end
  573. end
  574. if w < width then
  575. if nextTurn == "left" then
  576. left()
  577. forward()
  578. left()
  579. nextTurn = "right"
  580. else
  581. right()
  582. forward()
  583. right()
  584. nextTurn = "left"
  585. end
  586. else
  587. return
  588. end
  589. end
  590. end
  591. end
  592.  
  593. function dump()
  594. turtle.select(chestSlot)
  595. if not turtle.compareDown() then
  596. turtle.digDown()
  597. turtle.placeDown()
  598. setFace(west)
  599. forward()
  600. turtle.digDown()
  601. turtle.select(chestSlot)
  602. turtle.placeDown()
  603. setFace(east)
  604. forward()
  605. setFace(north)
  606. end
  607.  
  608. gatherPatchMaterial()
  609. for i=2,4 do
  610. turtle.select(i)
  611. if not turtle.compareTo(patchSlot) then
  612. if not turtle.dropDown() then
  613. print("Dump failed. Chest is full.")
  614. return
  615. end
  616. end
  617. end
  618. for i=6,14 do
  619. turtle.select(i)
  620. if turtle.getItemCount(i) > 0 then
  621. if not turtle.dropDown() then
  622. print("Dump failed. Chest is full.")
  623. return
  624. end
  625. end
  626. end
  627. turtle.select(patchSlot)
  628. setFace(north)
  629. end
  630.  
  631. function home()
  632. if x < 0 then
  633. setFace(east)
  634. while x < 0 do
  635. forward()
  636. end
  637. else
  638. if x > 0 then
  639. setFace(west)
  640. while x > 0 do
  641. forward()
  642. end
  643. end
  644. end
  645.  
  646. if z < 0 then
  647. setFace(north)
  648. while z < 0 do
  649. forward()
  650. end
  651. else
  652. if z > 0 then
  653. setFace(south)
  654. while z > 0 do
  655. forward()
  656. end
  657. end
  658. end
  659. setFace(north)
  660. nextTurn = "left"
  661. end
  662.  
  663. function ceiling()
  664. while y < height-1 do
  665. up()
  666. end
  667. end
  668.  
  669. function floor()
  670. while y > 0 do
  671. y = y - 1
  672. turtle.down()
  673. end
  674. end
  675.  
  676.  
  677. workHeight = height
  678. levelsCompleted = 0
  679.  
  680. -- Clear the top level and seal the ceiling
  681.  
  682. setFace(east)
  683. turtle.select(patchSlot)
  684. for i=1,height-1 do
  685. up()
  686. patch()
  687. end
  688. setFace(north)
  689. sealLevel()
  690. home()
  691. bounce()
  692. clearLevel()
  693. home()
  694. while y > 0 do
  695. turtle.down()
  696. y = y-1
  697. end
  698. dump()
  699.  
  700. -- Clear the first level and seal the floor
  701.  
  702. sealLevel()
  703. home()
  704. dump()
  705. clearLevel()
  706. home()
  707. dump()
  708. setFace(north)
  709.  
  710. -- Clear the rest of the levels
  711.  
  712. addTorches = true -- torchCheck only adds torches when turtle is at level 1
  713. workHeight = 1
  714. while levelsCompleted < height do
  715. while y < workHeight do
  716. up()
  717. end
  718. sealLevel()
  719. home()
  720. bounce()
  721. clearLevel()
  722. home()
  723. while y > 0 do
  724. turtle.down()
  725. y = y-1
  726. end
  727. dump()
  728. workHeight = workHeight + 1
  729. end
  730.  
  731. home()
  732. while y > 0 do
  733. turtle.down()
  734. y = y-1
  735. end
  736.  
  737. print(missionMessage)
  738. print("Fuel level is now ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement