Advertisement
HarvDad

hut1

Jun 1st, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. -- hut1
  2. -- Places stairs in the form of a pitched roof
  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 0.3"
  10. mission = "Put walls to the length, width, height specified"
  11. instructions = "Place stack(s) of material starting in first slot."
  12.  
  13. usage = "hut1 <length> <width> <height>"
  14.  
  15. x = 0
  16. y = 0
  17. z = 0
  18. face = 0
  19. north = 0
  20. west = 1
  21. south = 2
  22. east = 3
  23.  
  24. above = 1
  25. below = 0
  26. placement = above
  27. destructive = false
  28. hasFace = false
  29.  
  30. minimumFuel = 100
  31. missionMessage = "Mission complete."
  32. abort = false
  33. startLevel = 0
  34. local currentFuelLevel = turtle.getFuelLevel()
  35. areaCovered = 0
  36.  
  37. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  38. print(version)
  39. print(mission)
  40. print(instructions)
  41. print(usage)
  42. return
  43. end
  44.  
  45. if nArgs ~= 3 then
  46. print(usage)
  47. return
  48. end
  49.  
  50. length = tonumber(args[1])
  51. if length == nil then
  52. print("\"", args[1], "\" is not a valid length")
  53. return
  54. end
  55. if length < 1 then
  56. print("length must be a positive number greater than zero")
  57. end
  58.  
  59. width = tonumber(args[2])
  60. if width == nil then
  61. print("\"", args[2], "\" is not a valid width")
  62. return
  63. end
  64. if width < 1 then
  65. print("width must be a positive number greater than zero")
  66. end
  67.  
  68. height = tonumber(args[3])
  69. if startLevel == nil then
  70. print("\"", args[3], "\" is not a valid height")
  71. return
  72. end
  73.  
  74. currentSlot = 2
  75. materialSlot = 1
  76. maxSlot = 16
  77.  
  78. nextTurn = "left"
  79.  
  80. local clock = os.clock
  81. function sleep(n) -- seconds
  82. local t0 = clock()
  83. while clock() - t0 <= n do end
  84. end
  85.  
  86. function left()
  87. if face == 0 then face = 1 turtle.turnLeft() return end
  88. if face == 1 then face = 2 turtle.turnLeft() return end
  89. if face == 2 then face = 3 turtle.turnLeft() return end
  90. if face == 3 then face = 0 turtle.turnLeft() return end
  91. print("function left\(\): Bad face value: ", face)
  92. end
  93.  
  94. function right()
  95. if face == 0 then face = 3 turtle.turnRight() return end
  96. if face == 1 then face = 0 turtle.turnRight() return end
  97. if face == 2 then face = 1 turtle.turnRight() return end
  98. if face == 3 then face = 2 turtle.turnRight() return end
  99. print("function right\(\): Bad face value: ", face)
  100. end
  101.  
  102. function up()
  103. turtle.up()
  104. y = y+1
  105. end
  106.  
  107. function rise(nBlocks)
  108. local i
  109. for i=1,nBlocks do
  110. up()
  111. end
  112. end
  113.  
  114. function down()
  115. if turtle.detectDown() then
  116. turtle.digDown()
  117. end
  118. turtle.down()
  119. y = y-1
  120. end
  121.  
  122. function turn()
  123. if nextTurn == "left" then
  124. left()
  125. else
  126. right()
  127. end
  128. chomp()
  129. forward()
  130. if nextTurn == "left" then
  131. left()
  132. nextTurn = "right"
  133. else
  134. right()
  135. nextTurn = "left"
  136. end
  137. areaCovered = areaCovered + 1
  138. end
  139.  
  140. function forward()
  141. while turtle.detect() do -- This loop added in case of falling sand or whatever
  142. -- turtle.dig()
  143. return
  144. end
  145. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  146. if turtle.forward() then
  147. break
  148. end
  149. turtle.attack()
  150. sleep(2)
  151. end
  152. areaCovered = areaCovered + 1
  153.  
  154. if face == 0 then z = z+1 return end
  155. if face == 1 then x = x-1 return end
  156. if face == 2 then z = z-1 return end
  157. if face == 3 then x = x+1 return end
  158. end
  159.  
  160. function setFace(f)
  161. if f == 0 then
  162. if face == 0 then return end
  163. if face == 1 then right() return end
  164. if face == 2 then right() right() return end
  165. if face == 3 then left() return end
  166. end
  167.  
  168. if f == 1 then
  169. if face == 0 then left() return end
  170. if face == 1 then return end
  171. if face == 2 then right() return end
  172. if face == 3 then right() right() return end
  173. end
  174.  
  175. if f == 2 then
  176. if face == 0 then left() left() return end
  177. if face == 1 then left() return end
  178. if face == 2 then return end
  179. if face == 3 then right() return end
  180. end
  181.  
  182. if f == 3 then
  183. if face == 0 then right() return end
  184. if face == 1 then left() left() return end
  185. if face == 2 then left() return end
  186. if face == 3 then return end
  187. end
  188. end
  189.  
  190. function home(targetY)
  191. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  192.  
  193. if y > 0 then
  194. while y > targetY do
  195. down()
  196. end
  197. end
  198.  
  199. if x < 0 then
  200. setFace(east)
  201. while x < 0 do
  202. forward()
  203. end
  204. else
  205. if x > 0 then
  206. setFace(west)
  207. while x > 0 do
  208. forward()
  209. end
  210. end
  211. end
  212.  
  213. if z < 0 then
  214. setFace(north)
  215. while z < 0 do
  216. forward()
  217. end
  218. else
  219. if z > 0 then
  220. setFace(south)
  221. while z > 0 do
  222. forward()
  223. end
  224. end
  225. end
  226.  
  227. setFace(north)
  228. end
  229.  
  230. function ensureMaterial()
  231. if turtle.getItemCount(materialSlot) < 3 then
  232. organizeMaterial()
  233. end
  234. if turtle.getItemCount(materialSlot) < 3 then
  235. print("No more material")
  236. return false
  237. end
  238. return true
  239. end
  240.  
  241. function organizeMaterial()
  242. local i
  243. materialCount = turtle.getItemCount(materialSlot)
  244.  
  245. if materialCount < 3 then
  246. -- print("Attempting to refill slot ", materialSlot)
  247. for i=2,16 do
  248. turtle.select(i)
  249. if turtle.compareTo(materialSlot) then
  250. turtle.transferTo(materialSlot)
  251. end
  252. end
  253. end
  254. turtle.select(materialSlot)
  255. end
  256.  
  257. function layBlock()
  258. turtle.select(materialSlot)
  259.  
  260. if ensureMaterial() then
  261. if placement == above then
  262. if destructive then
  263. turtle.digUp()
  264. end
  265. if not turtle.detectUp() then
  266. turtle.placeUp()
  267. end
  268. else
  269. if destructive then
  270. turtle.digDown()
  271. end
  272. if not turtle.detectDown() then
  273. turtle.placeDown()
  274. end
  275. end
  276. end
  277. end
  278.  
  279. function checkFuel()
  280. if currentFuelLevel == "unlimited" then
  281. return true
  282. end
  283.  
  284. if currentFuelLevel < minimumFuel then
  285. if not turtle.refuel() then
  286. areaCovered = targetArea
  287. missionMessage = "Mission aborted due to low fuel."
  288. abort = true
  289. return false
  290. end
  291. end
  292. return true
  293. end
  294.  
  295. currentSlot = 1
  296.  
  297. function layRow(nBlocks)
  298. turtle.select(materialSlot)
  299. for i=1,nBlocks do
  300. if hasFace then
  301. left()
  302. end
  303. layBlock()
  304. if hasFace then
  305. right()
  306. end
  307. turtle.forward()
  308. end
  309. end
  310.  
  311. function stack3()
  312. if ensureMaterial() then turtle.placeUp() end
  313. if ensureMaterial() then turtle.placeDown() end
  314. turtle.back()
  315. if ensureMaterial() then turtle.place() end
  316. end
  317.  
  318. function tripleWall(nBlocks)
  319. turtle.select(materialSlot)
  320. for i=1,nBlocks do
  321. stack3()
  322. end
  323. end
  324.  
  325. function nextLayer(vertical)
  326. setFace(north)
  327. forward()
  328. left()
  329. forward()
  330. for i=1,vertical do
  331. up()
  332. end
  333. setFace(north)
  334.  
  335. length = length - 2
  336. width = width - 2
  337.  
  338. currentLevel = currentLevel + vertical
  339. end
  340.  
  341. -- MAIN PROGRAM
  342.  
  343. turtle.select(1)
  344.  
  345. print("fuelLevel = ", currentFuelLevel)
  346. print("length = ", length)
  347. print("width = ", width)
  348. print("face = ", face)
  349.  
  350. print("Current Fuel Level: ", currentFuelLevel)
  351.  
  352. if currentFuelLevel ~= "unlimited" then
  353. if currentFuelLevel < minimumFuel then
  354. if not turtle.refuel() then
  355. print("No fuel")
  356. return
  357. end
  358. end
  359. end
  360.  
  361. notFinished = true
  362. hasFace = true
  363. materialSlot = 1
  364. currentLevel = 0
  365. originalLength = length
  366. originalWidth = width
  367.  
  368. --[[
  369. -- Floor
  370.  
  371. hasFace = false
  372. placement = below
  373. destructive = true
  374.  
  375. while length > 0 or width > 0 do
  376. setFace(north)
  377. layRow(length - 1)
  378.  
  379. setFace(west)
  380. layRow(width - 1)
  381.  
  382. setFace(south)
  383. layRow(length - 1)
  384.  
  385. setFace(east)
  386. layRow(width - 1)
  387.  
  388. nextLayer(0)
  389. end
  390. --]]
  391.  
  392. rise(3)
  393.  
  394. -- Roof Slope
  395.  
  396. materialSlot = 1
  397. hasFace = true
  398. length = originalLength
  399. width = originalWidth
  400.  
  401. while currentLevel < height do
  402. setFace(north)
  403. layRow(length - 1)
  404.  
  405. setFace(west)
  406. layRow(width - 1)
  407.  
  408. setFace(south)
  409. layRow(length - 1)
  410.  
  411. setFace(east)
  412. layRow(width - 1)
  413.  
  414. nextLayer(1)
  415. end
  416.  
  417. -- Roof Top
  418.  
  419. materialSlot = 5
  420. hasFace = false
  421. --length = originalLength
  422. --width = originalWidth
  423.  
  424. while length > 0 or width > 0 do
  425. setFace(north)
  426. layRow(length - 1)
  427.  
  428. setFace(west)
  429. layRow(width - 1)
  430.  
  431. setFace(south)
  432. layRow(length - 1)
  433.  
  434. setFace(east)
  435. layRow(width - 1)
  436.  
  437. nextLayer(0)
  438. if turtle.detectUp() then
  439. break
  440. end
  441. end
  442.  
  443. home(2)
  444. length = originalLength
  445. width = originalWidth
  446.  
  447. -- Window Panes
  448.  
  449. materialSlot = 9
  450. hasFace = false
  451.  
  452. setFace(north)
  453. layRow(length - 1)
  454.  
  455. setFace(west)
  456. layRow(width - 1)
  457.  
  458. setFace(south)
  459. layRow(length - 1)
  460.  
  461. setFace(east)
  462. layRow(width - 1)
  463.  
  464. home(1)
  465.  
  466. length = originalLength
  467. width = originalWidth
  468.  
  469. -- 3-high wall
  470.  
  471. materialSlot = 13
  472. hasFace = false
  473.  
  474. setFace(south)
  475. tripleWall(length - 1)
  476.  
  477. setFace(east)
  478. tripleWall(width - 1)
  479.  
  480. setFace(north)
  481. tripleWall(length - 1)
  482.  
  483. setFace(west)
  484. tripleWall(width - 1)
  485. print(missionMessage)
  486. print(" Current fuel level is ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement