Advertisement
HarvDad

wall

May 5th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. -- wall
  2. -- Build a wall of specified length and height specified
  3. -- Turtle returns to its starting point when mission is completed or fuel runs low
  4. -- Written by HarvDad, May 2014
  5.  
  6. args = {...}
  7. nArgs = #args
  8.  
  9. version = "walls: Rev 0.1"
  10. mission = "Build a wall of length and height specified"
  11. instructions = "Place stack(s) of material starting in first slot."
  12.  
  13. usage = "wall <start> <end> <height>"
  14.  
  15. x = 0
  16. y = 0
  17. z = 0
  18. face = 0
  19. minimumFuel = 100
  20. missionMessage = "Mission complete."
  21. abort = false
  22. startLevel = 0
  23. local currentFuelLevel = turtle.getFuelLevel()
  24.  
  25. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  26. print(version)
  27. print(mission)
  28. print(instructions)
  29. print(usage)
  30. return
  31. end
  32.  
  33. if nArgs ~= 2 then
  34. print(usage)
  35. return
  36. end
  37.  
  38. length = tonumber(args[1])
  39. if length == nil then
  40. print("\"", args[1], "\" is not a valid length")
  41. return
  42. end
  43. if length < 1 then
  44. print("length must be a positive number greater than zero")
  45. end
  46.  
  47. height = tonumber(args[2])
  48. if startLevel == nil then
  49. print("\"", args[2], "\" is not a valid height")
  50. return
  51. end
  52.  
  53. currentSlot = 2
  54. materialSlot = 1
  55. maxSlot = 16
  56.  
  57. nextTurn = "left"
  58.  
  59. local clock = os.clock
  60. function sleep(n) -- seconds
  61. local t0 = clock()
  62. while clock() - t0 <= n do end
  63. end
  64.  
  65. function left()
  66. if face == 0 then face = 1 turtle.turnLeft() return end
  67. if face == 1 then face = 2 turtle.turnLeft() return end
  68. if face == 2 then face = 3 turtle.turnLeft() return end
  69. if face == 3 then face = 0 turtle.turnLeft() return end
  70. print("function left\(\): Bad face value: ", face)
  71. end
  72.  
  73. function right()
  74. if face == 0 then face = 3 turtle.turnRight() return end
  75. if face == 1 then face = 0 turtle.turnRight() return end
  76. if face == 2 then face = 1 turtle.turnRight() return end
  77. if face == 3 then face = 2 turtle.turnRight() return end
  78. print("function right\(\): Bad face value: ", face)
  79. end
  80.  
  81. function up()
  82. turtle.up()
  83. y = y+1
  84. end
  85.  
  86. function rise(nBlocks)
  87. local i
  88. for i=1,nBlocks do
  89. up()
  90. end
  91. end
  92.  
  93. function down()
  94. if turtle.detectDown() then
  95. turtle.digDown()
  96. end
  97. turtle.down()
  98. y = y-1
  99. end
  100.  
  101. function turn()
  102. if nextTurn == "left" then
  103. left()
  104. else
  105. right()
  106. end
  107. chomp()
  108. forward()
  109. if nextTurn == "left" then
  110. left()
  111. nextTurn = "right"
  112. else
  113. right()
  114. nextTurn = "left"
  115. end
  116. areaCovered = areaCovered + 1
  117. end
  118.  
  119. function forward()
  120. while turtle.detect() do -- This loop added in case of falling sand or whatever
  121. turtle.dig()
  122. end
  123. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  124. if turtle.forward() then
  125. break
  126. end
  127. turtle.attack()
  128. sleep(2)
  129. end
  130.  
  131. if face == 0 then z = z+1 return end
  132. if face == 1 then x = x-1 return end
  133. if face == 2 then z = z-1 return end
  134. if face == 3 then x = x+1 return end
  135. end
  136.  
  137. function back()
  138. turtle.back()
  139.  
  140. if face == 0 then z = z-1 return end
  141. if face == 1 then x = x+1 return end
  142. if face == 2 then z = z+1 return end
  143. if face == 3 then x = x-1 return end
  144. end
  145.  
  146. function setFace(f)
  147. if f == 0 then
  148. if face == 0 then return end
  149. if face == 1 then right() return end
  150. if face == 2 then right() right() return end
  151. if face == 3 then left() return end
  152. end
  153.  
  154. if f == 1 then
  155. if face == 0 then left() return end
  156. if face == 1 then return end
  157. if face == 2 then right() return end
  158. if face == 3 then right() right() return end
  159. end
  160.  
  161. if f == 2 then
  162. if face == 0 then left() left() return end
  163. if face == 1 then left() return end
  164. if face == 2 then return end
  165. if face == 3 then right() return end
  166. end
  167.  
  168. if f == 3 then
  169. if face == 0 then right() return end
  170. if face == 1 then left() left() return end
  171. if face == 2 then left() return end
  172. if face == 3 then return end
  173. end
  174. end
  175.  
  176. function home(targetY)
  177. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  178. if x < 0 then
  179. setFace(3)
  180. while x < 0 do
  181. forward()
  182. end
  183. else
  184. if x > 0 then
  185. setFace(1)
  186. while x > 0 do
  187. forward()
  188. end
  189. end
  190. end
  191.  
  192. if z < 0 then
  193. setFace(0)
  194. while z < 0 do
  195. forward()
  196. end
  197. else
  198. if z > 0 then
  199. setFace(2)
  200. while z > 0 do
  201. forward()
  202. end
  203. end
  204. end
  205.  
  206. if y > 0 then
  207. while y > 0 do
  208. down()
  209. end
  210. end
  211.  
  212. setFace(0)
  213. end
  214.  
  215. function ensureMaterial()
  216. if turtle.getItemCount(materialSlot) < 3 then
  217. organizeMaterial()
  218. end
  219. if turtle.getItemCount(materialSlot) < 3 then
  220. print("No more material")
  221. return false
  222. end
  223. return true
  224. end
  225.  
  226. function organizeMaterial()
  227. local i
  228. materialCount = turtle.getItemCount(materialSlot)
  229.  
  230. if materialCount < 3 then
  231. -- print("Attempting to refill slot ", materialSlot)
  232. for i=2,16 do
  233. turtle.select(i)
  234. if turtle.compareTo(materialSlot) then
  235. turtle.transferTo(materialSlot)
  236. end
  237. end
  238. end
  239. turtle.select(materialSlot)
  240. end
  241.  
  242. function layBlock()
  243. turtle.select(materialSlot)
  244.  
  245. if startLevel > 2 then
  246. if turtle.detectUp() then
  247. if turtle.compareUp() then
  248. return
  249. else
  250. turtle.digUp()
  251. end
  252. end
  253. if ensureMaterial() then
  254. turtle.placeUp()
  255. end
  256. else
  257. if turtle.detectDown() then
  258. if turtle.compareDown() then
  259. return
  260. else
  261. turtle.digDown()
  262. end
  263. end
  264. if ensureMaterial() then
  265. turtle.placeDown()
  266. end
  267. end
  268. end
  269.  
  270. function checkFuel()
  271. if currentFuelLevel == "unlimited" then
  272. return true
  273. end
  274.  
  275. if currentFuelLevel < minimumFuel then
  276. if not turtle.refuel() then
  277. areaCovered = targetArea
  278. missionMessage = "Mission aborted due to low fuel."
  279. abort = true
  280. return false
  281. end
  282. end
  283. return true
  284. end
  285.  
  286. currentSlot = 1
  287.  
  288. directon = "forward"
  289.  
  290. function layRow(nBlocks)
  291. turtle.select(currentSlot)
  292.  
  293. for i=1,nBlocks do
  294. if turtle.getItemCount(currentSlot) > 0 then
  295. turtle.placeDown()
  296. if direction == "forward" then
  297. if i < length then
  298. forward()
  299. end
  300. else
  301. if i < length then
  302. back()
  303. end
  304. end
  305. else
  306. currentSlot = currentSlot + 1
  307. if turtle.getItemCount(currentSlot) > 0 then
  308. turtle.select(currentSlot)
  309. turtle.placeDown()
  310. if direction == "forward" then
  311. if i < length then
  312. forward()
  313. end
  314. else
  315. if i < length then
  316. back()
  317. end
  318. end
  319. else
  320. print("Can't find more material")
  321. return
  322. end
  323. end
  324. end
  325. end
  326.  
  327. -- MAIN PROGRAM
  328.  
  329. turtle.select(1)
  330.  
  331. print("fuelLevel = ", currentFuelLevel)
  332. print("length = ", length)
  333. print("face = ", face)
  334.  
  335. print("Current Fuel Level: ", currentFuelLevel)
  336.  
  337. if currentFuelLevel ~= "unlimited" then
  338. if currentFuelLevel < minimumFuel then
  339. if not turtle.refuel() then
  340. print("No fuel")
  341. return
  342. end
  343. end
  344. end
  345.  
  346. notFinished = true
  347. direction = "forward"
  348. print("wall: Setting face to local north")
  349. setFace(0)
  350. z = 0
  351.  
  352. while notFinished do
  353. if y == height then
  354. notFinished = false
  355. break
  356. end
  357.  
  358. up()
  359.  
  360. layRow(length)
  361. if direction == "forward" then
  362. direction = "backward"
  363. else
  364. direction = "forward"
  365. end
  366. end
  367.  
  368. while z > 0 do
  369. turtle.back()
  370. z = z - 1
  371. end
  372. back()
  373. while y > 0 do
  374. turtle.down()
  375. y = y - 1
  376. end
  377. print(missionMessage)
  378. print(" Current fuel level is ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement