Advertisement
HarvDad

walls

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