kerrnel

Podman 1.1

Jun 17th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. -- Podman 1.1
  2. -- by MCKerrnel
  3. --
  4. -- A ComputerCraft program to
  5. -- operate a farm in a rectangular
  6. -- area. Turtle will harvest and
  7. -- replant.
  8. --
  9. -- This version adapts start/return
  10. -- movement to accomodate the
  11. -- custom dimensions. Also
  12. -- accommodates return if the
  13. -- dimensions end the turtle
  14. -- at farthest point out. Assumes
  15. -- that the max dimension is 9x9.
  16.  
  17. -- Size of farm grid that turtle
  18. -- will traverse. Middle position
  19. -- is water.
  20. local width = 7
  21. local length = 7
  22.  
  23. -- Fuel needed to start a cycle.
  24. local fuelCycle = width * length + 20
  25.  
  26. -- Harvests and replants the crop.
  27. function harvestCrops(endCol)
  28. local colCount = 0
  29. while colCount < endCol do
  30. for rowCount = 1, length, 1 do
  31. turtle.select(1)
  32.  
  33. -- Don't move forward if this is
  34. -- the first block in the row;
  35. -- we're already on it.
  36. if rowCount ~= 1 then
  37. turtle.forward()
  38. end
  39.  
  40. -- Harvest the crop. If there
  41. -- is only one seed left in slot 1
  42. -- then do not plant it.
  43. turtle.digDown()
  44. if turtle.getItemCount(1) > 1 then
  45. turtle.placeDown()
  46. end
  47. end
  48. colCount = colCount + 1
  49.  
  50. -- Rotate around to start
  51. -- the new row.
  52. endColTurn(colCount)
  53. turtle.forward()
  54. endColTurn(colCount)
  55. end
  56. end
  57.  
  58. -- Determine which direction the
  59. -- turtle should turn, based on
  60. -- which row it just finished.
  61. function endColTurn(column)
  62. -- Turtle will snake, so odd
  63. -- numbered columns turn left,
  64. -- even turn right. Odd
  65. -- numbers will leave a
  66. -- remainder when divided by 2.
  67. local direction = column % 2
  68.  
  69. if direction == 1 then
  70. turtle.turnLeft()
  71. else
  72. turtle.turnRight()
  73. end
  74. end
  75.  
  76. -- Unloads the crops from the turtle
  77. -- into the chest for crops.
  78. function emptyHarvest()
  79. for slot = 2, 16, 1 do
  80. turtle.select(slot)
  81. turtle.drop()
  82. end
  83. end
  84.  
  85. -- A quick function to convert a decimal
  86. -- value into an integer. Dunno why
  87. -- LUA has to be so difficult with this.
  88. -- Should be a built-in function!
  89. function toInt(decimalNum)
  90. local D2I = string.format("%i", decimalNum)
  91. return D2I
  92. end
  93.  
  94. function refuel(fuelCycles)
  95. -- Refuel. We don't need a stupid
  96. -- amount of fuel, so cap it
  97. -- on refuel at 10k.
  98. local fuelMax = 10000
  99.  
  100. if fuelCycles < 1 then
  101. -- Assume the turtle is facing
  102. -- forward and in-between the
  103. -- two chests. Left chest is
  104. -- to dump harvest into, right
  105. -- is for fuel.
  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() < fuelMax 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.  
  126. -- Restore original orientation.
  127. turtle.turnLeft()
  128. end
  129. return fuelCycles
  130. end
  131.  
  132. -- Bring the turtle back at the end
  133. -- of the harvest cycle, dumping off
  134. -- the harvest and returning to
  135. -- between the chests. Assumption
  136. -- here is that the full 9x9 grid has
  137. -- been traveled. Function does not
  138. -- account for custom number of
  139. -- columns.
  140. function turtleReturn(near)
  141. if near == false then
  142. for count = 1, 9 - (5 - toInt(length/2)), 1 do
  143. turtle.forward()
  144. end
  145. turtle.turnLeft()
  146. for count = 1, toInt(width/2) + 1, 1 do
  147. turtle.forward()
  148. end
  149. end
  150. turtle.turnRight()
  151. turtle.forward()
  152. turtle.turnRight()
  153. turtle.down()
  154. emptyHarvest()
  155. turtle.turnRight()
  156.  
  157. end
  158.  
  159.  
  160. -- MAINLINE
  161. local near = false
  162. if length%2 == 0 then
  163. near = true
  164. end
  165.  
  166. local fuelCycles = turtle.getFuelLevel() / fuelCycle
  167. print("Enough fuel to harvest ", toInt(fuelCycles), " cycles.")
  168.  
  169. -- This is where it all happens.
  170. while true do
  171. -- We must have at least 1 seed in slot 1
  172. -- so that the slot never is empty, otherwise
  173. -- the turtle might try planting harvest items.
  174. -- If we do not have a seed, then abort.
  175. if turtle.getItemCount(1) == 0 then
  176. print("There is no target seed in slot 1.")
  177. print("Need at least ONE seed of the crop.")
  178. break
  179. end
  180.  
  181. -- Fuel Up
  182. fuelCycles = turtle.getFuelLevel() / fuelCycle
  183. if fuelCycles < 1 then
  184. fuelCycles = refuel(fuelCycles)
  185. end
  186.  
  187. -- If we have enough fuel, let's harvest!
  188. if fuelCycles >= 1 then
  189. turtle.up()
  190. for count = 1, 5 - toInt(length/2), 1 do
  191. turtle.forward()
  192. end
  193. turtle.turnRight()
  194. for count = 1, toInt(width/2), 1 do
  195. turtle.forward()
  196. end
  197. turtle.turnLeft()
  198. harvestCrops(width)
  199.  
  200. -- Return to start
  201. turtleReturn(near)
  202. end
  203.  
  204. -- To allow crops to grow to make
  205. -- harvesting worthwhile, we will
  206. -- wait 40 minutes. 60s * 40
  207. -- Can probably reduce this if
  208. -- using lilypad of fertility or
  209. -- something.
  210. sleep(2400)
  211. end
Advertisement
Add Comment
Please, Sign In to add comment