kerrnel

Jackman 1.0

Jun 4th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. -- Jackman 1.0
  2. -- by MCKerrnel
  3. --
  4. -- A ComputerCraft program to
  5. -- operate a smalltree farm.
  6. -- Turtle will chop down trees
  7. -- and replant in the same cycle.
  8.  
  9. -- Size of farm grid that turtle
  10. -- will traverse.
  11. local width = 5
  12. local length = 13
  13.  
  14. -- Fuel needed to start a cycle.
  15. local fuelCycle = 250
  16.  
  17. -- Replants a sapling.
  18. function plantTree()
  19. -- We are already one block
  20. -- above ground, so let's
  21. -- just place a sapling
  22. -- underneath us.
  23. turtle.select(1)
  24. turtle.placeDown()
  25. end
  26.  
  27. -- Chops a tree down starting at
  28. -- the base and going to top.
  29. -- Does not work for anything
  30. -- bigger than 1x1.
  31. function chopTree()
  32. turtle.dig()
  33. turtle.forward()
  34. turtle.digDown()
  35.  
  36. local vertical = 0
  37. while turtle.detectUp() do
  38. turtle.digUp()
  39. turtle.up()
  40. vertical = vertical + 1
  41. end
  42.  
  43. while vertical > 0 do
  44. turtle.down()
  45. vertical = vertical - 1
  46. end
  47.  
  48. -- Replant a new sapling.
  49. plantTree()
  50. end
  51.  
  52. function chopCycle()
  53. local pass = 1
  54. local colPos = 1
  55. local rowPos = 1
  56.  
  57. while pass <= 2 do
  58. for rowPos = 1, length, 1 do
  59. if turtle.detect() then
  60. chopTree()
  61. else
  62. turtle.forward()
  63. end
  64. end
  65.  
  66. turtle.turnLeft()
  67.  
  68. if pass == 1 then
  69. for colPos = 1, width, 1 do
  70. if turtle.detect() then
  71. chopTree()
  72. else
  73. turtle.forward()
  74. end
  75. end
  76. turtle.turnLeft()
  77. end
  78. pass = pass + 1
  79. end
  80. end
  81.  
  82. -- Unloads the wood from the turtle
  83. -- into the chest for wood.
  84. function emptyWood()
  85. turtle.turnRight()
  86. for slot = 2, 16, 1 do
  87. turtle.select(slot)
  88. turtle.drop()
  89. end
  90. turtle.turnLeft()
  91. end
  92.  
  93. -- A quick function to convert a decimal
  94. -- value into an integer. Dunno why
  95. -- LUA has to be so difficult with this.
  96. -- Should be a built-in function!
  97. function toInt(decimalNum)
  98. local D2I = string.format("%i", decimalNum)
  99. return D2I
  100. end
  101.  
  102. function refuel(fuelCycles)
  103. -- Refuel. This is a regular turtle
  104. -- so can only hold 20,000 units.
  105. if fuelCycles < 1 then
  106. turtle.turnRight()
  107. turtle.select(2)
  108.  
  109. -- Try to fill up, but if not enough to
  110. -- fill up, that's ok.
  111. while fuelCycles < 1 or turtle.getFuelLevel() < 20000 do
  112. -- If there's nothing to grab, error out.
  113. if turtle.suck() == false then
  114. break
  115. end
  116. turtle.refuel()
  117. fuelCycles = turtle.getFuelLevel() / fuelCycle
  118. end
  119.  
  120. -- If we still need fuel
  121. -- then alert console.
  122. if fuelCycles < 1 then
  123. print "NO FUEL: Reload the FUEL chest."
  124. end
  125. turtle.turnLeft()
  126. end
  127. return fuelCycles
  128. end
  129.  
  130.  
  131. -- MAINLINE
  132. local fuelCycles = turtle.getFuelLevel() / fuelCycle
  133. print("Enough fuel to chop ", toInt(fuelCycles), " cycles.")
  134.  
  135. -- This is where it all happens.
  136. while true do
  137. -- Fuel Up
  138. fuelCycles = turtle.getFuelLevel() / fuelCycle
  139. if fuelCycles < 1 then
  140. fuelCycles = refuel(fuelCycles)
  141. end
  142.  
  143. --Restock Saplings
  144. if turtle.getItemCount(1) < 7 then
  145. turtle.select(1)
  146. turtle.suckDown()
  147. if turtle.getItemCount(1) < 7 then
  148. print("NO SAPLINGS: Restock sapling chest!")
  149. end
  150. end
  151.  
  152. if fuelCycles >= 1 and turtle.getItemCount(1) > 6 then
  153. -- Send turtle out to chop!
  154. turtle.up()
  155. chopCycle()
  156.  
  157. -- Return to start
  158. for count = 1, 5, 1 do
  159. turtle.forward()
  160. end
  161. turtle.down()
  162.  
  163. -- Empty inventory
  164. emptyWood()
  165. turtle.turnLeft()
  166. end
  167.  
  168. sleep(300)
  169. end
Advertisement
Add Comment
Please, Sign In to add comment