Advertisement
HarvDad

bwb

Mar 15th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. -- bwb (branch with bark)
  2. -- Creates a 1x1x3 tunnel with branches at intervals
  3. -- Each branch is the of the length specified by the user
  4. -- Seals all sides of tunnel against monsters
  5. -- Torches, if supplied, will be placed on the wall at 8 block interval
  6. -- Chests, if supplied, are placed at the end of each branch
  7. -- Written by HarvDad, March 2014
  8.  
  9. args = {...}
  10. nArgs = #args
  11. usage = "Usage: bwb <length>"
  12.  
  13. print("bwb: Rev 1.2")
  14.  
  15. loop = 0;
  16. x = 0
  17. y = 0
  18. z = 0
  19. face = 0
  20. patchSlot = 1
  21. minimumFuel = 100
  22. missionMessage = "Mission complete."
  23. abort = false
  24. local currentFuelLevel = turtle.getFuelLevel()
  25.  
  26. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  27. print("Creates a 1x3 tunnel with sealed sides.")
  28. print("Place patch material, like cobblestone, in slot", patchSlot)
  29. print("If torches are desired, place torches in slot 16.")
  30. printf("Chests, if supplied, are placed at the end of each branch")
  31. print("Usage: branch <length>")
  32. return
  33. end
  34.  
  35. if nArgs ~= 1 then
  36. print("Usage: branch <length>")
  37. return
  38. end
  39.  
  40. length = tonumber(args[1])
  41. if length == nil then
  42. print("\"", args[1], "\" is not a valid length")
  43. return
  44. end
  45. if length < 1 then
  46. print("length must be a positive number greater than zero")
  47. end
  48.  
  49. targetArea = length
  50. areaCovered = 1;
  51.  
  52.  
  53. function left()
  54. if face == 0 then face = 1 turtle.turnLeft() return end
  55. if face == 1 then face = 2 turtle.turnLeft() return end
  56. if face == 2 then face = 3 turtle.turnLeft() return end
  57. if face == 3 then face = 0 turtle.turnLeft() return end
  58. print("function left\(\): Bad face value: ", face)
  59. end
  60.  
  61. function right()
  62. if face == 0 then face = 3 turtle.turnRight() return end
  63. if face == 1 then face = 0 turtle.turnRight() return end
  64. if face == 2 then face = 1 turtle.turnRight() return end
  65. if face == 3 then face = 2 turtle.turnRight() return end
  66. print("function right\(\): Bad face value: ", face)
  67. end
  68.  
  69. function up()
  70. success = false
  71.  
  72. repeat
  73. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  74. turtle.digUp()
  75. end
  76. success = turtle.up()
  77. until success
  78. y = y+1
  79. end
  80.  
  81. function rise(nBlocks)
  82. local i
  83. for i=1,nBlocks do
  84. up()
  85. end
  86. end
  87.  
  88. function descend(nBlocks)
  89. local i
  90. for i=1,nBlocks do
  91. down()
  92. end
  93. end
  94.  
  95. function down()
  96. if turtle.detectDown() then
  97. turtle.digDown()
  98. end
  99. turtle.down()
  100. y = y-1
  101. end
  102.  
  103.  
  104. function forward()
  105. while turtle.detect() do -- This loop added in case of falling sand or whatever
  106. turtle.dig()
  107. end
  108. for i=1,10 do
  109. if turtle.forward() then
  110. break
  111. end
  112. turtle.attack()
  113. sleep(2)
  114. end
  115. if face == 0 then z = z+1 return end
  116. if face == 1 then x = x-1 return end
  117. if face == 2 then z = z-1 return end
  118. if face == 3 then x = x+1 return end
  119. end
  120.  
  121. function patch()
  122. turtle.select(2)
  123. turtle.place()
  124. gatherPatchMaterial(false);
  125. end
  126.  
  127. function patchUp()
  128. turtle.select(2)
  129. turtle.placeUp()
  130. gatherPatchMaterial(false);
  131. end
  132.  
  133. function patchDown()
  134. turtle.select(2)
  135. turtle.placeDown()
  136. gatherPatchMaterial(false);
  137. end
  138.  
  139. function slurp()
  140. local i
  141. print("slurp...")
  142.  
  143. for i=2,13 do
  144. turtle.select(i)
  145. if turtle.suck() then
  146. print("Sucky success for slot ", i)
  147. break
  148. else
  149. end
  150. end
  151. print("Total sucky failure")
  152. end
  153.  
  154. function slurpUp()
  155. local i
  156.  
  157. for i=2,13 do
  158. turtle.select(i)
  159. if turtle.suckUp() then
  160. break
  161. end
  162. end
  163. end
  164.  
  165. function slurpDown()
  166. local i
  167.  
  168. for i=2,13 do
  169. turtle.select(i)
  170. if turtle.suckDown() then
  171. break
  172. end
  173. end
  174. end
  175.  
  176. function gatherPatchMaterial(force)
  177. local i
  178. patchCount = turtle.getItemCount(patchSlot)
  179.  
  180. if (patchCount < 30) or force then
  181. print("Attempting to refill slot ", patchSlot)
  182. for i=1,14 do
  183. turtle.select(i)
  184. if turtle.compareTo(patchSlot) then
  185. turtle.transferTo(patchSlot, 64-patchCount)
  186. print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
  187. end
  188. end
  189. end
  190. turtle.select(patchSlot)
  191. end
  192.  
  193. addTorch = true
  194. torchSpacing = 8
  195. torchSpot = 6
  196.  
  197. function _mineForward()
  198. forward()
  199.  
  200. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  201. turtle.digUp()
  202. -- slurpUp()
  203. end
  204.  
  205. if turtle.detectDown() then
  206. turtle.digDown()
  207. -- slurpDown()
  208. end
  209.  
  210. if addTorch then
  211. if (torchSpot % torchSpacing) == 0 then
  212. left()
  213. left()
  214. turtle.select(16)
  215. turtle.place()
  216. right()
  217. right()
  218. end
  219. end
  220. torchSpot = torchSpot + 1
  221. end
  222.  
  223. function mineForwardUp()
  224. forward()
  225. -- slurp()
  226. if not turtle.detectDown() then
  227. patchDown()
  228. end
  229. patchSides()
  230.  
  231. up()
  232. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  233. turtle.digUp()
  234. -- slurpUp()
  235. end
  236. patchSides()
  237.  
  238. torchCheck()
  239.  
  240. up()
  241. if not turtle.detectUp() then
  242. patchUp()
  243. end
  244. patchSides()
  245.  
  246. torchSpot = torchSpot + 1
  247. end
  248.  
  249. function mineForwardDown()
  250. forward()
  251. -- slurp()
  252. if not turtle.detectUp() then
  253. patchUp()
  254. end
  255. patchSides()
  256.  
  257. down()
  258. patchSides()
  259.  
  260. torchCheck()
  261.  
  262. down()
  263. if not turtle.detectDown() then
  264. patchDown()
  265. end
  266. patchSides()
  267.  
  268. torchSpot = torchSpot + 1
  269. end
  270.  
  271. function patchCeiling()
  272. end
  273.  
  274. function patchFloor()
  275. end
  276.  
  277. function patchSides()
  278. if x == 0 then
  279. return
  280. end
  281.  
  282. local currentFace = face
  283. left()
  284.  
  285. if not turtle.detect() then
  286. patch()
  287. end
  288.  
  289. right()
  290. right()
  291.  
  292. if not turtle.detect() then
  293. patch()
  294. end
  295.  
  296. setFace(currentFace)
  297. end
  298.  
  299. function _patchSides()
  300. local currentFace = face
  301. left()
  302. if not turtle.detect() then
  303. if face == 0 and x ~= -1 then
  304. patch()
  305. end
  306. end
  307. right()
  308. right()
  309. if not turtle.detect() then
  310. if face == 0 and x ~= -1 then
  311. patch()
  312. else
  313. print("patchSides: opted to NOT patch, face = ", face, ", x = ", x)
  314. end
  315. end
  316. setFace(currentFace)
  317. -- left()
  318. end
  319.  
  320. function torchCheck()
  321. if addTorch then
  322. if (torchSpot % torchSpacing) == 0 then
  323. right()
  324. right()
  325. turtle.select(16)
  326. turtle.place()
  327. left()
  328. left()
  329. end
  330. end
  331. end
  332.  
  333. function setFace(f)
  334. if f == 0 then
  335. if face == 0 then return end
  336. if face == 1 then right() return end
  337. if face == 2 then right() right() return end
  338. if face == 3 then left() return end
  339. end
  340.  
  341. if f == 1 then
  342. if face == 0 then left() return end
  343. if face == 1 then return end
  344. if face == 2 then right() return end
  345. if face == 3 then right() right() return end
  346. end
  347.  
  348. if f == 2 then
  349. if face == 0 then left() left() return end
  350. if face == 1 then left() return end
  351. if face == 2 then return end
  352. if face == 3 then right() return end
  353. end
  354.  
  355. if f == 3 then
  356. if face == 0 then right() return end
  357. if face == 1 then left() left() return end
  358. if face == 2 then left() return end
  359. if face == 3 then return end
  360. end
  361. end
  362.  
  363. function checkFuel()
  364. if currentFuelLevel == "unlimited" then
  365. return true
  366. end
  367.  
  368. if currentFuelLevel < minimumFuel then
  369. if not turtle.refuel(200) then
  370. areaCovered = targetArea
  371. missionMessage = "Mission aborted due to low fuel."
  372. abort = true
  373. return false
  374. end
  375. end
  376. return true
  377. end
  378.  
  379. function home(targetY)
  380. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  381. if x < 0 then
  382. setFace(3)
  383. while x < 0 do
  384. forward()
  385. end
  386. else
  387. if x > 0 then
  388. setFace(1)
  389. while x > 0 do
  390. forward()
  391. end
  392. end
  393. end
  394.  
  395. if z < 0 then
  396. setFace(0)
  397. while z < 0 do
  398. forward()
  399. end
  400. else
  401. if z > 0 then
  402. setFace(2)
  403. while z > 0 do
  404. forward()
  405. end
  406. end
  407. end
  408. end
  409.  
  410. function dump()
  411. gatherPatchMaterial(true)
  412. for i=2,14 do
  413. turtle.select(i)
  414. turtle.dropDown()
  415. end
  416. end
  417.  
  418. function mineAhead()
  419. if y == 0 then
  420. mineForwardUp()
  421. else
  422. mineForwardDown()
  423. end
  424. end
  425.  
  426. function branchLoop()
  427. torchSpot = 6
  428. local i
  429.  
  430. for i=1,length do
  431. mineAhead()
  432. end
  433.  
  434. right()
  435.  
  436. for i=1,3 do
  437. mineAhead()
  438. end
  439.  
  440. right()
  441.  
  442. for i=1,length do
  443. mineAhead()
  444. end
  445.  
  446. setFace(0)
  447. while y > 0 do
  448. down()
  449. end
  450.  
  451. if turtle.detectDown() then
  452. turtle.select(15)
  453. if not turtle.compareDown() then
  454. turtle.digDown()
  455. -- slurpDown()
  456. turtle.placeDown()
  457. end
  458. end
  459. if turtle.compareDown() then
  460. dump()
  461. turtle.select(2)
  462. end
  463. end
  464.  
  465. -- MAIN PROGRAM
  466.  
  467. turtle.select(1)
  468.  
  469. print("Current Fuel Level: ", currentFuelLevel)
  470.  
  471. if currentFuelLevel ~= "unlimited" then
  472. if currentFuelLevel < minimumFuel then
  473. if not turtle.refuel() then
  474. print("No fuel")
  475. return
  476. end
  477. end
  478. end
  479.  
  480. for loop=1,5 do
  481. addTorch = false
  482.  
  483. -- rise(1)
  484. for i=1,5 do
  485. mineAhead()
  486. end
  487. turtle.back()
  488. turtle.back()
  489. left()
  490. addTorch = true
  491. branchLoop()
  492. end
  493.  
  494.  
  495. print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement