Advertisement
k_goos

Staircase Builder

Dec 12th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. local xPos, yPos, zPos = 0, 0, 0 --location zPos (bigger then zero is below startpoint and smaller then zero is above startpoint)
  2. local xSave, ySave, zSave, dirSave = 0, 0, 0, 0
  3. local direction = 0 --direction in for ways 0 is start position
  4. local al = true --alternate
  5.  
  6. local firsttime = true
  7. local length, width, height = 0, 0, 0
  8.  
  9. local args={...}
  10.  
  11. function main()
  12. if #args ~= 3 then
  13. print("ClearOut<height><width><length>")
  14. else
  15. length = tonumber(args[1])
  16. width = tonumber(args[2])
  17. height = tonumber(args[3])
  18.  
  19. for z = 0, height - 1 do
  20. for x = 0, width - 1 do
  21. for y = 0, length - 2 do
  22. if firsttime then
  23. y = y + 1
  24. firsttime = false
  25. end
  26. Forward()
  27. end
  28. if x < length - 1 then
  29. if al then
  30. RightUTurn(true)
  31. al = false
  32. else
  33. LeftUTurn(true)
  34. al = true
  35. end
  36. end
  37. end
  38. if height > 0 then
  39. Down()
  40. else
  41. Up()
  42. end
  43. RightUTurn(false)
  44.  
  45. if direction == 0 then
  46. al = false
  47. else
  48. al = true
  49. end
  50. al2 = false
  51. end
  52. end
  53. end
  54.  
  55. function CheckFuel()
  56. local fuel = turtle.getFuelLevel()
  57. local level = xPos + yPos + math.abs(zPos) + 2
  58.  
  59. if fuel > level then
  60. return true;
  61. else
  62. print("Taking new fuel from slot 1")
  63. if turtle.getItemCount(1) > 0 then
  64. turtle.select(1)
  65. if turtle.refuel(5) == false then
  66. print("No fuel in slot")
  67. SaveLocation()
  68. print("Going back to chest")
  69. Goto(0,0,0)
  70. if CheckChestFuel() == false then
  71. print("no fuel in chest")
  72. shell.exit()
  73. end
  74. CheckFuel()
  75. RestoreLocation()
  76. end
  77. end
  78. end
  79. end
  80.  
  81. function Forward()
  82. CheckFuel()
  83. while turtle.forward() == false do
  84. CheckInventory(0)
  85. turtle.dig()
  86. end
  87.  
  88. if direction == 0 then
  89. yPos = yPos + 1
  90. elseif direction == 2 then
  91. yPos = yPos - 1
  92. elseif direction == 1 then
  93. xPos = xPos + 1
  94. else
  95. xPos = xPos - 1
  96. end
  97. end
  98.  
  99. function Down()
  100. CheckFuel()
  101. while turtle.down() == false do
  102. CheckInventory(2)
  103. turtle.digDown()
  104. end
  105. zPos = zPos + 1
  106. end
  107.  
  108. function Up()
  109. CheckFuel()
  110. while turtle.up() == false do
  111. CheckInventory(1)
  112. turtle.digUp()
  113. end
  114. zPos = zPos - 1
  115. end
  116.  
  117. function Left(forward)
  118. turtle.turnLeft()
  119.  
  120. SubstractDirection()
  121.  
  122. if forward then
  123. Forward()
  124. end
  125. end
  126.  
  127. function Right(forward)
  128. turtle.turnRight()
  129.  
  130. AddDirection()
  131.  
  132. if forward then
  133. Forward()
  134. end
  135. end
  136.  
  137. function AddDirection()
  138. direction = direction + 1
  139.  
  140. if direction > 3 then
  141. direction = 0
  142. end
  143. end
  144.  
  145. function SubstractDirection()
  146. direction = direction - 1
  147.  
  148. if direction < 0 then
  149. direction = 3
  150. end
  151. end
  152.  
  153. function RightUTurn(forward)
  154. Right(forward)
  155. Right(false)
  156. end
  157.  
  158. function LeftUTurn(forward)
  159. Left(forward)
  160. Left(false)
  161. end
  162.  
  163. function Goto(xLoc, yLoc, zLoc)
  164. while zPos ~= zLoc do
  165. if zPos > 0 then
  166. Up()
  167. else
  168. Down()
  169. end
  170. end
  171.  
  172. while direction ~= 3 do
  173. if direction == 0 then
  174. Left(false)
  175. else
  176. Right(false)
  177. end
  178. end
  179.  
  180. while xPos > xLoc do
  181. Forward()
  182. end
  183.  
  184. Left()
  185.  
  186. while yPos > yLoc do
  187. Forward()
  188. end
  189. end
  190.  
  191. function ChangeDirection(dir)
  192. while direction ~= dir do
  193. Right(false)
  194. end
  195. end
  196.  
  197. function SaveLocation()
  198. xSave, ySave, zSave, dirSave = xPos, yPos, zPos, direction
  199. end
  200.  
  201. function RestoreLocation()
  202. Goto(xSave, ySave, zSave)
  203. ChangeDirection(dirSave)
  204. end
  205.  
  206. function CheckInventory(check)
  207. if turtle.getItemCount(9) > 0 then
  208. if BlockFit() == false then
  209. print("Inventory is full")
  210. SaveLocation()
  211. print("Returning to chest...")
  212. Goto(0, 0, 0)
  213.  
  214. print("Empty inventory")
  215. EmptyInventory()
  216.  
  217. print("Restore location")
  218. RestoreLocation()
  219. end
  220. end
  221. end
  222.  
  223. function BlockFit()
  224. for i = 1, 16 do
  225. if turtle.getItemSpace(i) > 0 then
  226. turtle.select(i)
  227. if check == 0 then
  228. if turtle.compare() then
  229. return true
  230. end
  231. elseif check == 1 then
  232. if turtle.compareUp() then
  233. return true
  234. end
  235. else
  236. if turtle.compareDown() then
  237. return true
  238. end
  239. end
  240. end
  241. end
  242. return false
  243. end
  244.  
  245. function EmptyInventory()
  246. for i = 2, 16 do
  247. select(i)
  248. if turtle.drop() == false then
  249. print("Chest if full")
  250. shell.exit()
  251. end
  252. end
  253. end
  254.  
  255. function GetEmptySlot()
  256. for i = 2, 16 do
  257. if turtle.getItemCount(i) == 0 then
  258. return i
  259. end
  260. end
  261. return 0
  262. end
  263.  
  264. function CheckChestFuel()
  265. local slot = GetEmptySlot()
  266. if slot == 0 then
  267. EmptyInventory()
  268. else
  269. turtle.select(slot)
  270. for i = 0, 10 do
  271. if turtle.suck() == false then
  272. print("No fuel found")
  273. shell.exit()
  274. end
  275. if turtle.drop() == false then
  276. print("Chest if full")
  277. shell.exit()
  278. end
  279. end
  280. CheckFuel()
  281. end
  282. end
  283.  
  284. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement