kerrnel

Shearman 2.1

May 25th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. -- Shearman 2.1
  2. -- by MCKerrnel
  3. --
  4. -- A ComputerCraft program to shear
  5. -- sheep. This version includes
  6. -- reloading shears when they break.
  7.  
  8. -- How much fuel it takes to
  9. -- complete one cycle.
  10. local fuelCycle = 54
  11.  
  12. function goForward(blocks)
  13. for count = 1, blocks, 1 do
  14. turtle.forward()
  15. turtle.suck()
  16. end
  17. end
  18.  
  19. function turn(direction)
  20. if direction == "left" then
  21. turtle.turnLeft()
  22. else
  23. turtle.turnRight()
  24. end
  25. end
  26.  
  27. -- A quick function to convert a decimal
  28. -- value into an integer. Dunno why
  29. -- LUA has to be so difficult with this.
  30. -- Should be a built-in function!
  31. function toInt(decimalNum)
  32. local D2I = string.format("%i", decimalNum)
  33. return D2I
  34. end
  35.  
  36. function shearCycle(direction, needShears)
  37. if direction == "left" then
  38. returndir = "right"
  39. else
  40. returndir = "left"
  41. end
  42.  
  43. -- Chest to Start of Row
  44. turn(direction)
  45. goForward(3)
  46. turn(direction)
  47.  
  48. -- Shear Row
  49. for count = 1, 8, 1 do
  50. needShears = shearPen(direction, needShears)
  51. end
  52.  
  53. -- Return to Chest
  54. turtle.turnLeft()
  55. turtle.turnLeft()
  56. goForward(23)
  57. turn(returndir)
  58. goForward(3)
  59. turn(returndir)
  60.  
  61. -- Unload
  62. if needShears then
  63. startSlot = 1
  64. else
  65. startSlot = 2
  66. end
  67. for slot = startSlot, 16, 1 do
  68. turtle.select(slot)
  69. turtle.drop()
  70. end
  71.  
  72. turtle.turnLeft()
  73. turtle.turnLeft()
  74.  
  75. return needShears
  76. end
  77.  
  78. -- Shear the sheep in a 2x1 pen.
  79. function shearPen(direction, needShears)
  80. turtle.select(1)
  81. for count = 1, 2, 1 do
  82. turtle.forward()
  83. turn(direction)
  84.  
  85. -- If we still have shears
  86. -- then shear and reset
  87. -- the check status.
  88. -- If no shears, then
  89. -- don't try to shear.
  90. if needShears == false then
  91. turtle.place()
  92. needShears = needShearsCheck()
  93. end
  94. os.sleep(2)
  95. turtle.suck()
  96. if direction == "left" then
  97. turtle.turnRight()
  98. else
  99. turtle.turnLeft()
  100. end
  101. end
  102. turtle.forward()
  103.  
  104. return needShears
  105. end
  106.  
  107. -- Check to see if the shears
  108. -- are broken.
  109. function needShearsCheck()
  110. -- Check the # of items
  111. -- in the shears slot
  112. -- that are needed to
  113. -- complete a full stack.
  114. -- 0=Full stack
  115. -- 64=No item
  116. -- 1-63=Wool
  117. if turtle.getItemSpace(1) == 0 then
  118. return false
  119. else
  120. return true
  121. end
  122. end
  123.  
  124. -- Check to see if we have
  125. -- enough fuel for a cycle.
  126. function needFuelCheck()
  127. if turtle.getFuelLevel() - fuelCycle < 0 then
  128. return true
  129. else
  130. return false
  131. end
  132. end
  133.  
  134. -- Reload shears from the chest
  135. -- below. If able to reload then
  136. -- send back false to reset the
  137. -- needShears variable, otherwise
  138. -- return true for failure and
  139. -- still needing shears. The
  140. -- turtle inventory should be empty
  141. -- when it attempts to load items.
  142. function reloadShears(needShears)
  143. if needShears then
  144. turtle.select(1)
  145. -- If we still need shears
  146. -- then alert console.
  147. if turtle.suckDown() == false then
  148. print "NO SHEARS: Reload the SHEARS chest."
  149. else
  150. needShears = false
  151. end
  152. end
  153. return needShears
  154. end
  155.  
  156. function refuel(needFuel)
  157. -- Refuel. This is a regular turtle
  158. -- so can only hold 20,000 units.
  159. if needFuel then
  160. turtle.select(2)
  161.  
  162. -- Try to fill up, but if not enough to
  163. -- fill up, that's ok.
  164. while needFuel == true or turtle.getFuelLevel < 20000 do
  165. -- If there's nothing to grab, error out.
  166. if turtle.suckUp() == false then
  167. break
  168. end
  169. turtle.refuel()
  170. needFuel = needFuelCheck()
  171. end
  172.  
  173. -- If we still need fuel
  174. -- then alert console.
  175. if needFuel then
  176. print "NO FUEL: Reload the FUEL chest."
  177. end
  178. end
  179. return needFuel
  180. end
  181.  
  182. -- Not used in this version...wanted to
  183. -- document it to have it for 3.0
  184. function toTerm(column, row, text)
  185. -- Terminal size = 39 x 13
  186. setCursorPos(column, row)
  187. term.write(text)
  188. end
  189.  
  190.  
  191. -- MAINLINE
  192. -- Check to see if we need shears.
  193. local needShears = needShearsCheck()
  194. local needFuel = needFuelCheck()
  195.  
  196. local fuelCycles = turtle.getFuelLevel() / fuelCycle
  197. print("Enough fuel to shear ", toInt(fuelCycles), " rows.")
  198.  
  199. -- This is where it all happens.
  200. while true do
  201. -- Reload shears, if needed.
  202. needShears = reloadShears(needShears)
  203.  
  204. -- Refuel.
  205. needFuel = refuel(needFuel)
  206.  
  207. -- Shear North Row
  208. print "CYCLE: North Row"
  209. if needShears == false and needFuel == false then
  210. needShears = shearCycle("left", needShears)
  211. end
  212.  
  213. -- Reload shears, if needed.
  214. needShears = reloadShears(needShears)
  215.  
  216. -- Shear South Row
  217. print "CYCLE: South Row"
  218. if needShears == false and needFuel == false then
  219. needShears = shearCycle("right", needShears)
  220. end
  221.  
  222. -- Wait 2 minutes to allow
  223. -- sheep to regrow wool.
  224. print "CYCLE: Sleep 2 min"
  225. os.sleep(120)
  226. end
Advertisement
Add Comment
Please, Sign In to add comment