HarvDad

structure

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