towwey

builder

Aug 30th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.43 KB | None | 0 0
  1. slot=1
  2. choice=3
  3. isFull=false
  4. builder=true
  5. gatherer=false
  6. patternString="Line"
  7. pattern=1
  8. sideLength=0
  9. sideHeight=0
  10. sideWidth=0
  11. builderString="Building"
  12. drawinglength=false
  13. drawingheight=false
  14. drawingwidth=false
  15. running=false
  16. --checks if the turtle has material in any of the first 14 slots (last two slots are for fuel) will place a block below the turtle.--
  17. --It is possible to script a pattern of 14 different stages by placing specific quantities into the turtle in from top left across then down.--
  18. drawWorking=function()
  19. term.setCursorPos(2,3)
  20. term.write(" Currently:")term.write(builderString)term.write(" a ")term.write(patternString)
  21. end
  22. boarder=function()
  23. term.clear()
  24. term.setCursorPos(1,1)
  25. print("===========Turtle Builder==============")
  26. print("| |")
  27. print("| |")
  28. print("| |")
  29. print("| |")
  30. print("| |")
  31. print("| |")
  32. print("| |")
  33. print("| |")
  34. print("| |")
  35. print("| |")
  36. print("===========Turtle Builder==============")
  37. end
  38. mainhud=function()
  39. term.setCursorPos(2,3)
  40. term.write(" 1 Build/Break:")
  41. term.setCursorPos(2,4)
  42. term.write(" 2 Gather:")
  43. term.setCursorPos(2,5)
  44. term.write(" 3 Pattern:")
  45. term.setCursorPos(2,6)
  46. term.write(" 4 Run!")
  47. term.setCursorPos(2,7)
  48. term.write(" 5 Exit!")
  49. if pattern>1 then
  50. term.setCursorPos(1,8)
  51. term.write("< 7 > Side Length: ")term.write(tostring(sideLength))
  52. drawinglength=true
  53. else
  54. drawinglength=false
  55. end
  56. if pattern==4 or pattern==2 or pattern==3 then
  57. term.setCursorPos(1,9)
  58. term.write("< 8 > Side Height: ")term.write(tostring(sideHeight))
  59. drawingheight=true
  60. else
  61. drawingheight=false
  62. end
  63. if pattern==1 and not builder then
  64. term.setCursorPos(1,8)
  65. term.write("< 7 > Side Length: ")term.write(tostring(sideLength))
  66. end
  67. if pattern==3 then
  68. term.setCursorPos(1,10)
  69. term.write("< 9 > Side Width: ")term.write(tostring(sideWidth))
  70. drawingwidth=true
  71. else
  72. drawingwidth=false
  73. end
  74. end
  75. drawStatus=function(builder,gatherer,patternString)
  76. term.setCursorPos(18,3)
  77. if builder then
  78. builderString="Building"
  79. else
  80. builderString="Breaking"
  81. end
  82. term.write(builderString)
  83. term.setCursorPos(13,4)
  84. term.write(gatherer)
  85. term.setCursorPos(14,5)
  86. if pattern==1 then patternString="Line"
  87. elseif pattern==2 then patternString="Square"
  88. elseif pattern==3 then patternString="Floor"
  89. elseif pattern==4 then patternString="Wall"
  90. end
  91. term.write(patternString)
  92. end
  93. drawChoice=function(choiceNum)
  94. term.setCursorPos(2,choiceNum)
  95. term.write("[")
  96. term.setCursorPos(4,choiceNum)
  97. term.write("]")
  98. end
  99. place=function()
  100. counted=turtle.getItemCount(slot)
  101. turtle.select(slot)
  102. if counted<1 then
  103. empty=true
  104. end
  105.  
  106. while empty do
  107. slot=slot+1
  108. if slot>14 then
  109. error("out of blocks")
  110. end
  111.  
  112. turtle.select(slot)
  113. counted=turtle.getItemCount(slot)
  114.  
  115. if counted>0 then
  116. empty=false
  117. end
  118.  
  119. term.write("still empty")
  120.  
  121. end
  122.  
  123. turtle.placeDown()
  124. end
  125. --IMPORTANT! when a turtle is being used to collect blocks with this program the first slot must remain open as a buffer for new items coming in--
  126. sort=function()
  127. for i=2,14,1 do
  128. turtle.select(1)
  129. sames=turtle.compareTo(i)
  130. if sames then
  131. turtle.select(1)
  132. turtle.transferTo(i)
  133. end
  134. end
  135. if turtle.getItemCount(1)>0 then
  136. for j=2,14,1 do
  137. empty=turtle.getItemCount(j)
  138. if empty==0 and turtle.getItemCount(1)>0 then
  139. turtle.select(1)
  140. turtle.transferTo(j)
  141. end
  142. end
  143. if turtle.getItemCount(1)>0 then
  144. isFull=true
  145. return
  146. end
  147. end
  148. end
  149. --takes up blocks around the turtle making use of the sort function to store them--
  150. gather=function()
  151. turtle.drop()
  152. end
  153. --Breaks the block below the turtle and gathers it optionaly. depends on boolean 'shouldGather'--
  154. dig=function()
  155. turtle.digDown()
  156. if not gatherer==true then
  157. gather()
  158. end
  159. end
  160. digFront=function()
  161. turtle.dig()
  162. if gatherer==true then
  163. --gather()
  164. end
  165. end
  166. --breaks the block in front of the turtle and gathers it optionally. depends on the boolean 'shouldGather'--
  167. --will check for fuel and the need for it then take the appropriate action.--
  168. fuel=function()
  169. fuelSlot=15
  170. hasFuel=turtle.getFuelLevel()
  171. if hasFuel<2 then
  172. turtle.select(fuelSlot)
  173. counted=turtle.getItemCount(fuelSlot)
  174. if counted>0 then
  175. turtle.refuel(1)
  176. else
  177. fuelSlot=fuelSlot+1
  178. turtle.select(fuelSlot)
  179. counted=turtle.getItemCount(fuelSlot)
  180. if counted>0 then
  181. turtle.refuel(1)
  182. end
  183. end
  184. hasFuel=turtle.getFuelLevel()
  185. if hasFuel<2 then
  186. error("Out of Fuel")
  187. end
  188. term.write("fuel complete")
  189. end
  190. end
  191. -- will place blocks in a line untill the turtle discovers an obstruction.--
  192. -- It is possible to controll the length of the line without an obstruction by limiting the building materials located in the first 14 slots of the turtle.--
  193. bLine=function()
  194. runsline=true
  195. term.clear()
  196. boarder()
  197. term.setCursorPos(2,3)
  198. term.write("Starting")
  199.  
  200. while runsline do
  201. if turtle.getFuelLevel()<2 then
  202. fuel()
  203. end
  204.  
  205. isBlocked=turtle.detect()
  206. hasPlaced=turtle.detectDown()
  207.  
  208. if hasPlaced and isBlocked then
  209. term.write("Operation Stopped due to obstruction")
  210. runsline=false
  211. elseif isBlocked and not hasPlaced then
  212. place()
  213. runsline=false
  214. elseif not isBlocked and not hasPlaced then
  215. place()
  216. end
  217. turtle.forward()
  218. end
  219. term.write("Finshed")
  220. end
  221. brLine=function(sideLength)
  222. term.clear()
  223. boarder()
  224. term.setCursorPos(2,3)
  225. term.write("Starting")
  226. for i=0,sideLength,1 do
  227. if turtle.getFuelLevel()<2 then
  228. fuel()
  229. end
  230. turtle.dig()
  231. if gatherer==true then
  232. term.write("stupid gatherer")
  233. --gather()
  234. end
  235. turtle.forward()
  236. end
  237. end
  238. bSquare=function(sideLength,sideHeight)
  239. term.clear()
  240. boarder()
  241. term.setCursorPos(2,3)
  242. term.write("Starting")
  243. building=true
  244. while building do
  245. place()
  246. for i=1,sideHeight do
  247. for j=2,sideLength do
  248. if turtle.getFuelLevel()<2 then
  249. fuel()
  250. end
  251. place()
  252. isBlocked=turtle.detect()
  253. if isBlocked then
  254. error("path blocked")
  255. else
  256. turtle.forward()
  257. end
  258. end
  259. turtle.turnRight()
  260. for j=2,sideLength do
  261. if turtle.getFuelLevel()<2 then
  262. fuel()
  263. end
  264. place()
  265. isBlocked=turtle.detect()
  266. if isBlocked then
  267. error("path blocked")
  268. else
  269. turtle.forward()
  270. end
  271. end
  272. turtle.turnRight()
  273. for j=2,sideLength do
  274. if turtle.getFuelLevel()<2 then
  275. fuel()
  276. end
  277. place()
  278. isBlocked=turtle.detect()
  279. if isBlocked then
  280. error("path blocked")
  281. else
  282. turtle.forward()
  283. end
  284. end
  285. turtle.turnRight()
  286. for j=3,sideLength do
  287. if turtle.getFuelLevel()<2 then
  288. fuel()
  289. end
  290. place()
  291. isBlocked=turtle.detect()
  292. if isBlocked then
  293. error("path blocked")
  294. else
  295. turtle.forward()
  296. end
  297. end
  298. place()
  299. turtle.up()
  300. isBlocked=turtle.detect()
  301. if isBlocked then
  302. error("path blocked")
  303. else
  304. turtle.forward()
  305. end
  306. place()
  307. turtle.turnRight()
  308. end
  309. end
  310. running=false
  311. end
  312. brSquare=function(sideLength,sideHeight)
  313. term.clear()
  314. boarder()
  315. term.setCursorPos(2,3)
  316. term.write("Starting")
  317. building=true
  318. while building do
  319. dig()
  320. for i=1,sideHeight do
  321. for j=2,sideLength do
  322. if turtle.getFuelLevel()<2 then
  323. fuel()
  324. end
  325. dig()
  326. isBlocked=turtle.detect()
  327. if isBlocked then
  328. error("path blocked")
  329. else
  330. turtle.forward()
  331. end
  332. end
  333. turtle.turnRight()
  334. for j=2,sideLength do
  335. if turtle.getFuelLevel()<2 then
  336. fuel()
  337. end
  338. dig()
  339. isBlocked=turtle.detect()
  340. if isBlocked then
  341. error("path blocked")
  342. else
  343. turtle.forward()
  344. end
  345. end
  346. turtle.turnRight()
  347. for j=2,sideLength do
  348. if turtle.getFuelLevel()<2 then
  349. fuel()
  350. end
  351. dig()
  352. isBlocked=turtle.detect()
  353. if isBlocked then
  354. error("path blocked")
  355. else
  356. turtle.forward()
  357. end
  358. end
  359. turtle.turnRight()
  360. for j=3,sideLength do
  361. if turtle.getFuelLevel()<2 then
  362. fuel()
  363. end
  364. dig()
  365. isBlocked=turtle.detect()
  366. if isBlocked then
  367. error("path blocked")
  368. else
  369. turtle.forward()
  370. end
  371. end
  372. dig()
  373. turtle.down()
  374. isBlocked=turtle.detect()
  375. if isBlocked then
  376. error("path blocked")
  377. else
  378. turtle.forward()
  379. end
  380. dig()
  381. turtle.turnRight()
  382. end
  383. building=false
  384. end
  385. running=false
  386. end
  387. bFloor=function(sideLength,sideWidth,sideHeight)
  388. term.clear()
  389. boarder()
  390. term.setCursorPos(2,3)
  391. term.write("Starting")
  392. building=true
  393. turnRight=true
  394. while building do
  395. for i=1,sideHeight,1 do
  396. for i=1,sideWidth,1 do
  397. for i=2,sideLength,1 do
  398. --walking forward placing blocks
  399. if turtle.getFuelLevel()<2 then
  400. fuel()
  401. end
  402. place()
  403. isBlocked=turtle.detect()
  404. if isBlocked then
  405. error("path blocked")
  406. else
  407. turtle.forward()
  408. end
  409. place()
  410. end
  411. --decides to zig or zag then does it placing it facing back the opposite way over an empty space
  412. if turnRight then
  413. turtle.turnRight()
  414. isBlocked=turtle.detect()
  415. if isBlocked then
  416. error("path blocked")
  417. else
  418. turtle.forward()
  419. end
  420. turtle.turnRight()
  421. end
  422. if not turnRight then
  423. turtle.turnLeft()
  424. isBlocked=turtle.detect()
  425. if isBlocked then
  426. error("path blocked")
  427. else
  428. turtle.forward()
  429. end
  430. turtle.turnLeft()
  431. end
  432. turnRight= not turnRight
  433. end
  434. --moves turtle up then checks to see if it just went left or right
  435. turtle.up()
  436. if not turnRight then
  437. --if the turtle just went right then it should
  438. turtle.turnRight()
  439. isBlocked=turtle.detect()
  440. if isBlocked then
  441. error("path blocked")
  442. else
  443. turtle.forward()
  444. end
  445. turtle.turnLeft()
  446. end
  447. if turnRight then
  448. turtle.turnLeft()
  449. isBlocked=turtle.detect()
  450. if isBlocked then
  451. error("path blocked")
  452. else
  453. turtle.forward()
  454. end
  455. turtle.turnRight()
  456. end
  457. turnRight=not turnRight
  458. end
  459. building=false
  460. end
  461. end
  462. brFloor=function()
  463. term.clear()
  464. boarder()
  465. term.setCursorPos(2,3)
  466. term.write("Starting")
  467. building=true
  468. turnRight=true
  469. while building do
  470. for i=1,sideHeight,1 do
  471. for j=1,sideWidth,1 do
  472. for i=2,sideLength,1 do
  473. --walking forward placing blocks
  474. if turtle.getFuelLevel()<2 then
  475. fuel()
  476. end
  477. dig()
  478. isBlocked=turtle.detect()
  479. if isBlocked and i<sideLength then
  480. digFront()
  481. turtle.forward()
  482. else
  483. turtle.forward()
  484. end
  485. dig()
  486. end
  487. --decides to zig or zag then does it placing it facing back the opposite way over an empty space
  488. if turnRight and j<sideWidth then
  489. turtle.turnRight()
  490. isBlocked=turtle.detect()
  491. if isBlocked then
  492. dig()
  493. turtle.forward()
  494. else
  495. turtle.forward()
  496. end
  497. turtle.turnRight()
  498. end
  499. if not turnRight and j<sideWidth then
  500. turtle.turnLeft()
  501. isBlocked=turtle.detect()
  502. if isBlocked then
  503. dig()
  504. turtle.forward()
  505. else
  506. turtle.forward()
  507. end
  508. turtle.turnLeft()
  509. end
  510. turnRight= not turnRight
  511. end
  512. --after completing a whole floor turtle turns about and descends and contunues on.
  513. turtle.turnRight()
  514. turtle.turnRight()
  515. turtle.down()
  516. turnRight=not turnRight
  517. end
  518. building=false
  519. end
  520. end
  521. bWall=function(sideLength,sideHeight)
  522. term.clear()
  523. boarder()
  524. term.setCursorPos(2,3)
  525. term.write("Starting")
  526. for i=1,sideHeight,1 do
  527. for j=2,sideLength,1 do
  528. if turtle.getFuelLevel()<2 then
  529. fuel()
  530. end
  531. place()
  532. isBlocked=turtle.detect()
  533. if isBlocked and i<sideLength then
  534. digFront()
  535. turtle.forward()
  536. else
  537. turtle.forward()
  538. end
  539. place()
  540. end
  541. turtle.turnRight()
  542. turtle.turnRight()
  543. turtle.up()
  544. end
  545. end
  546. brWall=function()
  547. term.clear()
  548. boarder()
  549. term.setCursorPos(2,3)
  550. term.write("Starting")
  551. for i=1,sideHeight,1 do
  552. for j=2,sideLength,1 do
  553. if turtle.getFuelLevel()<2 then
  554. fuel()
  555. end
  556. dig()
  557. isBlocked=turtle.detect()
  558. if isBlocked and i<sideLength then
  559. digFront()
  560. turtle.forward()
  561. else
  562. turtle.forward()
  563. end
  564. dig()
  565. end
  566. turtle.turnRight()
  567. turtle.turnRight()
  568. turtle.down()
  569. end
  570. end
  571. while true do
  572. boarder()
  573. mainhud()
  574. drawStatus(builder,gatherer,patternString)
  575. drawChoice(choice)
  576. event,param1=os.pullEvent("key")
  577. if event== "key" and param1==200 then
  578. choice = choice-1
  579. if choice<3 and drawingwidth then
  580. choice=10
  581. end
  582. if choice<3 and drawingheight and not drawingwidth then
  583. choice=9
  584. end
  585. if choice<3 and drawinglength and not drawingheight then
  586. choice=8
  587. end
  588. if choice<3 and not drawinglength then
  589. choice=7
  590. end
  591. end
  592. if event== "key" and param1==208 then
  593. choice=choice+1
  594. if choice>10 then
  595. choice=3
  596. end
  597. if choice>7 and not drawinglength and not drawingwidth then
  598. choice=3
  599. end
  600. if choice>8 and not drawingheight then
  601. choice=3
  602. end
  603. if choice>9 and not drawingwidth then
  604. choice=3
  605. end
  606. end
  607. if event== "key" and param1==28 then
  608. if choice==3 then
  609. builder=not builder
  610. end
  611. if choice==4 then
  612. gatherer= not gatherer
  613. end
  614. if choice==5 then
  615. pattern=pattern+1
  616. if pattern>4 then
  617. pattern=1
  618. end
  619. end
  620. if choice==6 and param1==28 then
  621. running=true
  622. --primary loop when all choices have been made--
  623. while running do
  624. --line order--
  625. if pattern==1 then
  626. if builder and running then
  627. bLine()
  628. elseif not builder then
  629. brLine(sideLength)
  630. end
  631. --square order--
  632. elseif pattern==2 then
  633. if builder then
  634. bSquare(sideLength,sideHeight)
  635. end
  636. if not builder then
  637. brSquare(sideLength,sideHeight)
  638. end
  639. --floor order--
  640. elseif pattern==3 then
  641. if builder then
  642. bFloor(sideLength,sideWidth,sideHeight)
  643. end
  644. if not builder then
  645. brFloor(sideLength,sideWidth,sideHeight)
  646. end
  647. --wall order--
  648. elseif pattern==4 then
  649. if builder then
  650. bWall(sideLength,sideHeight)
  651. end
  652. if not builder then
  653. brWall(sideLength,sideHeight)
  654. end
  655. end
  656. term.setCursorPos(2,6)
  657. term.write("Operation Finished")
  658. os.sleep(1)
  659. running=false
  660. end
  661. --end of the main loop area--
  662. end
  663. if choice==7 then
  664. term.clear()
  665. error("thank you for using Turtle Builder!")
  666. end
  667.  
  668. end
  669. if event== "key" and param1==205 and choice==8 then
  670. sideLength=sideLength+1
  671. end
  672. if event== "key" and param1==203 and choice==8 then
  673. sideLength=sideLength-1
  674. if sideLength<=0 then
  675. sideLength=0
  676. end
  677. end
  678. if event== "key" and param1==205 and choice==9 then
  679. sideHeight=sideHeight+1
  680. end
  681. if event== "key" and param1==203 and choice==9 then
  682. sideHeight=sideHeight-1
  683. if sideHeight<=0 then
  684. sideHeight=0
  685. end
  686. end
  687. if event== "key" and param1==205 and choice==10 then
  688. sideWidth=sideWidth+1
  689. end
  690. if event== "key" and param1==203 and choice==10 then
  691. sideWidth=sideWidth-1
  692. if sideWidth<=0 then
  693. sideWidth=0
  694. end
  695. end
  696. end
  697.  
Add Comment
Please, Sign In to add comment