Advertisement
HarvDad

patch

Jun 5th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. -- patch
  2. -- Patches holes in wall, as in side of turel-dug hole
  3. -- Place patch material starting in slot 1
  4. -- Written by HarvDad, June 2014
  5.  
  6. args = {...}
  7. nArgs = #args
  8.  
  9. version = "Rev 0.1"
  10. usage = "Usage: patch <height> <width>"
  11. help1 = "height may be positive or negative"
  12. help2 = "width extends to the left"
  13. help3 = "Turtle must start facing wall."
  14.  
  15. height = 0
  16. width = 0
  17. distance = 0
  18.  
  19. x = 0
  20. y = 0
  21. z = 0
  22.  
  23. materialSlot = 1
  24.  
  25. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  26. print(usage)
  27. print(help1)
  28. print(help2)
  29. print(help3)
  30. return
  31. end
  32.  
  33. if nArgs ~= 2 then
  34. print(usage)
  35. return
  36. end
  37.  
  38. height = tonumber(args[1])
  39. if height == nil then
  40. print("\"", args[1], "\" is not a valid height")
  41. return
  42. end
  43.  
  44. if height > 0 then
  45. distance = height
  46. else
  47. distance = -height
  48. end
  49.  
  50. width = tonumber(args[2])
  51. if width == nil then
  52. print("\"", args[1], "\" is not a valid width")
  53. return
  54. end
  55. if width < 1 then
  56. print("width must be a positive integer")
  57. return
  58. end
  59.  
  60. function ensureMaterial()
  61. if turtle.getItemCount(materialSlot) < 3 then
  62. organizeMaterial()
  63. end
  64. if turtle.getItemCount(materialSlot) < 3 then
  65. print("No more material")
  66. return false
  67. end
  68. return true
  69. end
  70.  
  71. function organizeMaterial()
  72. local i
  73. materialCount = turtle.getItemCount(materialSlot)
  74.  
  75. if materialCount < 3 then
  76. -- print("Attempting to refill slot ", materialSlot)
  77. for i=2,16 do
  78. turtle.select(i)
  79. if turtle.compareTo(materialSlot) then
  80. turtle.transferTo(materialSlot)
  81. end
  82. end
  83. end
  84. turtle.select(materialSlot)
  85. end
  86.  
  87. function patchUp()
  88. for i=1,distance do
  89. if not turtle.detect() then
  90. ensureMaterial()
  91. turtle.place()
  92. end
  93. turtle.up()
  94. y = y+1
  95. end
  96. if not turtle.detect() then
  97. ensureMaterial()
  98. turtle.place()
  99. end
  100. end
  101.  
  102. function patchDown()
  103. for i=1,distance do
  104. if not turtle.detect() then
  105. ensureMaterial()
  106. turtle.place()
  107. end
  108. turtle.down()
  109. y = y-1
  110. end
  111. if not turtle.detect() then
  112. ensureMaterial()
  113. turtle.place()
  114. end
  115. end
  116.  
  117. function home()
  118. if x < 0 then
  119. turtle.turnRight()
  120. while x < 0 do
  121. turtle.forward()
  122. x = x+1
  123. end
  124. end
  125. turtle.turnLeft()
  126.  
  127. if y < 0 then
  128. while y < 0 do
  129. turtle.up()
  130. y = y+1
  131. end
  132. elseif y > 0 then
  133. while y > 0 do
  134. turtle.down()
  135. y = y-1
  136. end
  137. end
  138. end
  139.  
  140. -- Main Program
  141.  
  142. goingUp = true
  143.  
  144. if height < 0 then
  145. goingUp = false
  146. end
  147.  
  148. for i=1,width do
  149. if goingUp then
  150. patchUp()
  151. goingUp = false
  152. else
  153. patchDown()
  154. goingUp = true
  155. end
  156. if i < width then
  157. turtle.turnLeft()
  158. turtle.forward()
  159. turtle.turnRight()
  160. x = x-1
  161. else
  162. break
  163. end
  164. end
  165.  
  166. home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement